Path: blob/main/crypto/openssl/providers/implementations/macs/blake2_mac_impl.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/proverr.h>1314#include "prov/blake2.h"15#include "internal/cryptlib.h"16#include "prov/implementations.h"17#include "prov/providercommon.h"1819/*20* Forward declaration of everything implemented here. This is not strictly21* necessary for the compiler, but provides an assurance that the signatures22* of the functions in the dispatch table are correct.23*/24static OSSL_FUNC_mac_newctx_fn blake2_mac_new;25static OSSL_FUNC_mac_dupctx_fn blake2_mac_dup;26static OSSL_FUNC_mac_freectx_fn blake2_mac_free;27static OSSL_FUNC_mac_gettable_ctx_params_fn blake2_gettable_ctx_params;28static OSSL_FUNC_mac_get_ctx_params_fn blake2_get_ctx_params;29static OSSL_FUNC_mac_settable_ctx_params_fn blake2_mac_settable_ctx_params;30static OSSL_FUNC_mac_set_ctx_params_fn blake2_mac_set_ctx_params;31static OSSL_FUNC_mac_init_fn blake2_mac_init;32static OSSL_FUNC_mac_update_fn blake2_mac_update;33static OSSL_FUNC_mac_final_fn blake2_mac_final;3435struct blake2_mac_data_st {36BLAKE2_CTX ctx;37BLAKE2_PARAM params;38unsigned char key[BLAKE2_KEYBYTES];39};4041static void *blake2_mac_new(void *unused_provctx)42{43struct blake2_mac_data_st *macctx;4445if (!ossl_prov_is_running())46return NULL;4748macctx = OPENSSL_zalloc(sizeof(*macctx));49if (macctx != NULL) {50BLAKE2_PARAM_INIT(&macctx->params);51/* ctx initialization is deferred to BLAKE2b_Init() */52}53return macctx;54}5556static void *blake2_mac_dup(void *vsrc)57{58struct blake2_mac_data_st *dst;59struct blake2_mac_data_st *src = vsrc;6061if (!ossl_prov_is_running())62return NULL;6364dst = OPENSSL_zalloc(sizeof(*dst));65if (dst == NULL)66return NULL;6768*dst = *src;69return dst;70}7172static void blake2_mac_free(void *vmacctx)73{74struct blake2_mac_data_st *macctx = vmacctx;7576if (macctx != NULL) {77OPENSSL_cleanse(macctx->key, sizeof(macctx->key));78OPENSSL_free(macctx);79}80}8182static size_t blake2_mac_size(void *vmacctx)83{84struct blake2_mac_data_st *macctx = vmacctx;8586return macctx->params.digest_length;87}8889static int blake2_setkey(struct blake2_mac_data_st *macctx,90const unsigned char *key, size_t keylen)91{92if (keylen > BLAKE2_KEYBYTES || keylen == 0) {93ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);94return 0;95}96memcpy(macctx->key, key, keylen);97/* Pad with zeroes at the end if required */98if (keylen < BLAKE2_KEYBYTES)99memset(macctx->key + keylen, 0, BLAKE2_KEYBYTES - keylen);100BLAKE2_PARAM_SET_KEY_LENGTH(&macctx->params, (uint8_t)keylen);101return 1;102}103104static int blake2_mac_init(void *vmacctx, const unsigned char *key,105size_t keylen, const OSSL_PARAM params[])106{107struct blake2_mac_data_st *macctx = vmacctx;108109if (!ossl_prov_is_running() || !blake2_mac_set_ctx_params(macctx, params))110return 0;111if (key != NULL) {112if (!blake2_setkey(macctx, key, keylen))113return 0;114} else if (macctx->params.key_length == 0) {115/* Check key has been set */116ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);117return 0;118}119return BLAKE2_INIT_KEY(&macctx->ctx, &macctx->params, macctx->key);120}121122static int blake2_mac_update(void *vmacctx,123const unsigned char *data, size_t datalen)124{125struct blake2_mac_data_st *macctx = vmacctx;126127if (datalen == 0)128return 1;129130return BLAKE2_UPDATE(&macctx->ctx, data, datalen);131}132133static int blake2_mac_final(void *vmacctx,134unsigned char *out, size_t *outl,135size_t outsize)136{137struct blake2_mac_data_st *macctx = vmacctx;138139if (!ossl_prov_is_running())140return 0;141142*outl = blake2_mac_size(macctx);143return BLAKE2_FINAL(out, &macctx->ctx);144}145146static const OSSL_PARAM known_gettable_ctx_params[] = {147OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),148OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL),149OSSL_PARAM_END150};151static const OSSL_PARAM *blake2_gettable_ctx_params(ossl_unused void *ctx,152ossl_unused void *provctx)153{154return known_gettable_ctx_params;155}156157static int blake2_get_ctx_params(void *vmacctx, OSSL_PARAM params[])158{159OSSL_PARAM *p;160161if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL162&& !OSSL_PARAM_set_size_t(p, blake2_mac_size(vmacctx)))163return 0;164165if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL166&& !OSSL_PARAM_set_size_t(p, BLAKE2_BLOCKBYTES))167return 0;168169return 1;170}171172static const OSSL_PARAM known_settable_ctx_params[] = {173OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),174OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),175OSSL_PARAM_octet_string(OSSL_MAC_PARAM_CUSTOM, NULL, 0),176OSSL_PARAM_octet_string(OSSL_MAC_PARAM_SALT, NULL, 0),177OSSL_PARAM_END178};179static const OSSL_PARAM *blake2_mac_settable_ctx_params(180ossl_unused void *ctx, ossl_unused void *p_ctx)181{182return known_settable_ctx_params;183}184185/*186* ALL parameters should be set before init().187*/188static int blake2_mac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])189{190struct blake2_mac_data_st *macctx = vmacctx;191const OSSL_PARAM *p;192193if (ossl_param_is_empty(params))194return 1;195196if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SIZE)) != NULL) {197size_t size;198199if (!OSSL_PARAM_get_size_t(p, &size)200|| size < 1201|| size > BLAKE2_OUTBYTES) {202ERR_raise(ERR_LIB_PROV, PROV_R_NOT_XOF_OR_INVALID_LENGTH);203return 0;204}205BLAKE2_PARAM_SET_DIGEST_LENGTH(&macctx->params, (uint8_t)size);206}207208if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL209&& !blake2_setkey(macctx, p->data, p->data_size))210return 0;211212if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CUSTOM))213!= NULL) {214/*215* The OSSL_PARAM API doesn't provide direct pointer use, so we216* must handle the OSSL_PARAM structure ourselves here217*/218if (p->data_size > BLAKE2_PERSONALBYTES) {219ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CUSTOM_LENGTH);220return 0;221}222BLAKE2_PARAM_SET_PERSONAL(&macctx->params, p->data, p->data_size);223}224225if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SALT)) != NULL) {226/*227* The OSSL_PARAM API doesn't provide direct pointer use, so we228* must handle the OSSL_PARAM structure ourselves here as well229*/230if (p->data_size > BLAKE2_SALTBYTES) {231ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);232return 0;233}234BLAKE2_PARAM_SET_SALT(&macctx->params, p->data, p->data_size);235}236return 1;237}238239const OSSL_DISPATCH BLAKE2_FUNCTIONS[] = {240{ OSSL_FUNC_MAC_NEWCTX, (void (*)(void))blake2_mac_new },241{ OSSL_FUNC_MAC_DUPCTX, (void (*)(void))blake2_mac_dup },242{ OSSL_FUNC_MAC_FREECTX, (void (*)(void))blake2_mac_free },243{ OSSL_FUNC_MAC_INIT, (void (*)(void))blake2_mac_init },244{ OSSL_FUNC_MAC_UPDATE, (void (*)(void))blake2_mac_update },245{ OSSL_FUNC_MAC_FINAL, (void (*)(void))blake2_mac_final },246{ OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,247(void (*)(void))blake2_gettable_ctx_params },248{ OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))blake2_get_ctx_params },249{ OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,250(void (*)(void))blake2_mac_settable_ctx_params },251{ OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))blake2_mac_set_ctx_params },252OSSL_DISPATCH_END253};254255256