Path: blob/main/crypto/openssl/providers/implementations/macs/poly1305_prov.c
48383 views
/*1* Copyright 2018-2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include <openssl/core_dispatch.h>10#include <openssl/core_names.h>11#include <openssl/params.h>12#include <openssl/evp.h>13#include <openssl/err.h>14#include <openssl/proverr.h>1516#include "crypto/poly1305.h"1718#include "prov/implementations.h"19#include "prov/providercommon.h"2021/*22* Forward declaration of everything implemented here. This is not strictly23* necessary for the compiler, but provides an assurance that the signatures24* of the functions in the dispatch table are correct.25*/26static OSSL_FUNC_mac_newctx_fn poly1305_new;27static OSSL_FUNC_mac_dupctx_fn poly1305_dup;28static OSSL_FUNC_mac_freectx_fn poly1305_free;29static OSSL_FUNC_mac_gettable_params_fn poly1305_gettable_params;30static OSSL_FUNC_mac_get_params_fn poly1305_get_params;31static OSSL_FUNC_mac_settable_ctx_params_fn poly1305_settable_ctx_params;32static OSSL_FUNC_mac_set_ctx_params_fn poly1305_set_ctx_params;33static OSSL_FUNC_mac_init_fn poly1305_init;34static OSSL_FUNC_mac_update_fn poly1305_update;35static OSSL_FUNC_mac_final_fn poly1305_final;3637struct poly1305_data_st {38void *provctx;39int updated;40POLY1305 poly1305; /* Poly1305 data */41};4243static void *poly1305_new(void *provctx)44{45struct poly1305_data_st *ctx;4647if (!ossl_prov_is_running())48return NULL;49ctx = OPENSSL_zalloc(sizeof(*ctx));50if (ctx != NULL)51ctx->provctx = provctx;52return ctx;53}5455static void poly1305_free(void *vmacctx)56{57OPENSSL_free(vmacctx);58}5960static void *poly1305_dup(void *vsrc)61{62struct poly1305_data_st *src = vsrc;63struct poly1305_data_st *dst;6465if (!ossl_prov_is_running())66return NULL;67dst = OPENSSL_malloc(sizeof(*dst));68if (dst == NULL)69return NULL;7071*dst = *src;72return dst;73}7475static size_t poly1305_size(void)76{77return POLY1305_DIGEST_SIZE;78}7980static int poly1305_setkey(struct poly1305_data_st *ctx,81const unsigned char *key, size_t keylen)82{83if (keylen != POLY1305_KEY_SIZE) {84ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);85return 0;86}87Poly1305_Init(&ctx->poly1305, key);88ctx->updated = 0;89return 1;90}9192static int poly1305_init(void *vmacctx, const unsigned char *key,93size_t keylen, const OSSL_PARAM params[])94{95struct poly1305_data_st *ctx = vmacctx;9697/* initialize the context in MAC_ctrl function */98if (!ossl_prov_is_running() || !poly1305_set_ctx_params(ctx, params))99return 0;100if (key != NULL)101return poly1305_setkey(ctx, key, keylen);102/* no reinitialization of context with the same key is allowed */103return ctx->updated == 0;104}105106static int poly1305_update(void *vmacctx, const unsigned char *data,107size_t datalen)108{109struct poly1305_data_st *ctx = vmacctx;110111ctx->updated = 1;112if (datalen == 0)113return 1;114115/* poly1305 has nothing to return in its update function */116Poly1305_Update(&ctx->poly1305, data, datalen);117return 1;118}119120static int poly1305_final(void *vmacctx, unsigned char *out, size_t *outl,121size_t outsize)122{123struct poly1305_data_st *ctx = vmacctx;124125if (!ossl_prov_is_running())126return 0;127ctx->updated = 1;128Poly1305_Final(&ctx->poly1305, out);129*outl = poly1305_size();130return 1;131}132133static const OSSL_PARAM known_gettable_params[] = {134OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),135OSSL_PARAM_END136};137static const OSSL_PARAM *poly1305_gettable_params(void *provctx)138{139return known_gettable_params;140}141142static int poly1305_get_params(OSSL_PARAM params[])143{144OSSL_PARAM *p;145146if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)147return OSSL_PARAM_set_size_t(p, poly1305_size());148149return 1;150}151152static const OSSL_PARAM known_settable_ctx_params[] = {153OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),154OSSL_PARAM_END155};156static const OSSL_PARAM *poly1305_settable_ctx_params(ossl_unused void *ctx,157ossl_unused void *provctx)158{159return known_settable_ctx_params;160}161162static int poly1305_set_ctx_params(void *vmacctx, const OSSL_PARAM *params)163{164struct poly1305_data_st *ctx = vmacctx;165const OSSL_PARAM *p;166167if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL168&& !poly1305_setkey(ctx, p->data, p->data_size))169return 0;170return 1;171}172173const OSSL_DISPATCH ossl_poly1305_functions[] = {174{ OSSL_FUNC_MAC_NEWCTX, (void (*)(void))poly1305_new },175{ OSSL_FUNC_MAC_DUPCTX, (void (*)(void))poly1305_dup },176{ OSSL_FUNC_MAC_FREECTX, (void (*)(void))poly1305_free },177{ OSSL_FUNC_MAC_INIT, (void (*)(void))poly1305_init },178{ OSSL_FUNC_MAC_UPDATE, (void (*)(void))poly1305_update },179{ OSSL_FUNC_MAC_FINAL, (void (*)(void))poly1305_final },180{ OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))poly1305_gettable_params },181{ OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))poly1305_get_params },182{ OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,183(void (*)(void))poly1305_settable_ctx_params },184{ OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))poly1305_set_ctx_params },185OSSL_DISPATCH_END186};187188189