1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci#include <openssl/core_dispatch.h> 11e1051a39Sopenharmony_ci#include <openssl/core_names.h> 12e1051a39Sopenharmony_ci#include <openssl/params.h> 13e1051a39Sopenharmony_ci#include <openssl/proverr.h> 14e1051a39Sopenharmony_ci 15e1051a39Sopenharmony_ci#include "prov/blake2.h" 16e1051a39Sopenharmony_ci#include "internal/cryptlib.h" 17e1051a39Sopenharmony_ci#include "prov/implementations.h" 18e1051a39Sopenharmony_ci#include "prov/providercommon.h" 19e1051a39Sopenharmony_ci 20e1051a39Sopenharmony_ci/* 21e1051a39Sopenharmony_ci * Forward declaration of everything implemented here. This is not strictly 22e1051a39Sopenharmony_ci * necessary for the compiler, but provides an assurance that the signatures 23e1051a39Sopenharmony_ci * of the functions in the dispatch table are correct. 24e1051a39Sopenharmony_ci */ 25e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_newctx_fn blake2_mac_new; 26e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_dupctx_fn blake2_mac_dup; 27e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_freectx_fn blake2_mac_free; 28e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_gettable_ctx_params_fn blake2_gettable_ctx_params; 29e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_get_ctx_params_fn blake2_get_ctx_params; 30e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_settable_ctx_params_fn blake2_mac_settable_ctx_params; 31e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_set_ctx_params_fn blake2_mac_set_ctx_params; 32e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_init_fn blake2_mac_init; 33e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_update_fn blake2_mac_update; 34e1051a39Sopenharmony_cistatic OSSL_FUNC_mac_final_fn blake2_mac_final; 35e1051a39Sopenharmony_ci 36e1051a39Sopenharmony_cistruct blake2_mac_data_st { 37e1051a39Sopenharmony_ci BLAKE2_CTX ctx; 38e1051a39Sopenharmony_ci BLAKE2_PARAM params; 39e1051a39Sopenharmony_ci unsigned char key[BLAKE2_KEYBYTES]; 40e1051a39Sopenharmony_ci}; 41e1051a39Sopenharmony_ci 42e1051a39Sopenharmony_cistatic void *blake2_mac_new(void *unused_provctx) 43e1051a39Sopenharmony_ci{ 44e1051a39Sopenharmony_ci struct blake2_mac_data_st *macctx; 45e1051a39Sopenharmony_ci 46e1051a39Sopenharmony_ci if (!ossl_prov_is_running()) 47e1051a39Sopenharmony_ci return NULL; 48e1051a39Sopenharmony_ci 49e1051a39Sopenharmony_ci macctx = OPENSSL_zalloc(sizeof(*macctx)); 50e1051a39Sopenharmony_ci if (macctx != NULL) { 51e1051a39Sopenharmony_ci BLAKE2_PARAM_INIT(&macctx->params); 52e1051a39Sopenharmony_ci /* ctx initialization is deferred to BLAKE2b_Init() */ 53e1051a39Sopenharmony_ci } 54e1051a39Sopenharmony_ci return macctx; 55e1051a39Sopenharmony_ci} 56e1051a39Sopenharmony_ci 57e1051a39Sopenharmony_cistatic void *blake2_mac_dup(void *vsrc) 58e1051a39Sopenharmony_ci{ 59e1051a39Sopenharmony_ci struct blake2_mac_data_st *dst; 60e1051a39Sopenharmony_ci struct blake2_mac_data_st *src = vsrc; 61e1051a39Sopenharmony_ci 62e1051a39Sopenharmony_ci if (!ossl_prov_is_running()) 63e1051a39Sopenharmony_ci return NULL; 64e1051a39Sopenharmony_ci 65e1051a39Sopenharmony_ci dst = OPENSSL_zalloc(sizeof(*dst)); 66e1051a39Sopenharmony_ci if (dst == NULL) 67e1051a39Sopenharmony_ci return NULL; 68e1051a39Sopenharmony_ci 69e1051a39Sopenharmony_ci *dst = *src; 70e1051a39Sopenharmony_ci return dst; 71e1051a39Sopenharmony_ci} 72e1051a39Sopenharmony_ci 73e1051a39Sopenharmony_cistatic void blake2_mac_free(void *vmacctx) 74e1051a39Sopenharmony_ci{ 75e1051a39Sopenharmony_ci struct blake2_mac_data_st *macctx = vmacctx; 76e1051a39Sopenharmony_ci 77e1051a39Sopenharmony_ci if (macctx != NULL) { 78e1051a39Sopenharmony_ci OPENSSL_cleanse(macctx->key, sizeof(macctx->key)); 79e1051a39Sopenharmony_ci OPENSSL_free(macctx); 80e1051a39Sopenharmony_ci } 81e1051a39Sopenharmony_ci} 82e1051a39Sopenharmony_ci 83e1051a39Sopenharmony_cistatic size_t blake2_mac_size(void *vmacctx) 84e1051a39Sopenharmony_ci{ 85e1051a39Sopenharmony_ci struct blake2_mac_data_st *macctx = vmacctx; 86e1051a39Sopenharmony_ci 87e1051a39Sopenharmony_ci return macctx->params.digest_length; 88e1051a39Sopenharmony_ci} 89e1051a39Sopenharmony_ci 90e1051a39Sopenharmony_cistatic int blake2_setkey(struct blake2_mac_data_st *macctx, 91e1051a39Sopenharmony_ci const unsigned char *key, size_t keylen) 92e1051a39Sopenharmony_ci{ 93e1051a39Sopenharmony_ci if (keylen > BLAKE2_KEYBYTES || keylen == 0) { 94e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); 95e1051a39Sopenharmony_ci return 0; 96e1051a39Sopenharmony_ci } 97e1051a39Sopenharmony_ci memcpy(macctx->key, key, keylen); 98e1051a39Sopenharmony_ci /* Pad with zeroes at the end if required */ 99e1051a39Sopenharmony_ci if (keylen < BLAKE2_KEYBYTES) 100e1051a39Sopenharmony_ci memset(macctx->key + keylen, 0, BLAKE2_KEYBYTES - keylen); 101e1051a39Sopenharmony_ci BLAKE2_PARAM_SET_KEY_LENGTH(&macctx->params, (uint8_t)keylen); 102e1051a39Sopenharmony_ci return 1; 103e1051a39Sopenharmony_ci} 104e1051a39Sopenharmony_ci 105e1051a39Sopenharmony_cistatic int blake2_mac_init(void *vmacctx, const unsigned char *key, 106e1051a39Sopenharmony_ci size_t keylen, const OSSL_PARAM params[]) 107e1051a39Sopenharmony_ci{ 108e1051a39Sopenharmony_ci struct blake2_mac_data_st *macctx = vmacctx; 109e1051a39Sopenharmony_ci 110e1051a39Sopenharmony_ci if (!ossl_prov_is_running() || !blake2_mac_set_ctx_params(macctx, params)) 111e1051a39Sopenharmony_ci return 0; 112e1051a39Sopenharmony_ci if (key != NULL) { 113e1051a39Sopenharmony_ci if (!blake2_setkey(macctx, key, keylen)) 114e1051a39Sopenharmony_ci return 0; 115e1051a39Sopenharmony_ci } else if (macctx->params.key_length == 0) { 116e1051a39Sopenharmony_ci /* Check key has been set */ 117e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); 118e1051a39Sopenharmony_ci return 0; 119e1051a39Sopenharmony_ci } 120e1051a39Sopenharmony_ci return BLAKE2_INIT_KEY(&macctx->ctx, &macctx->params, macctx->key); 121e1051a39Sopenharmony_ci} 122e1051a39Sopenharmony_ci 123e1051a39Sopenharmony_cistatic int blake2_mac_update(void *vmacctx, 124e1051a39Sopenharmony_ci const unsigned char *data, size_t datalen) 125e1051a39Sopenharmony_ci{ 126e1051a39Sopenharmony_ci struct blake2_mac_data_st *macctx = vmacctx; 127e1051a39Sopenharmony_ci 128e1051a39Sopenharmony_ci if (datalen == 0) 129e1051a39Sopenharmony_ci return 1; 130e1051a39Sopenharmony_ci 131e1051a39Sopenharmony_ci return BLAKE2_UPDATE(&macctx->ctx, data, datalen); 132e1051a39Sopenharmony_ci} 133e1051a39Sopenharmony_ci 134e1051a39Sopenharmony_cistatic int blake2_mac_final(void *vmacctx, 135e1051a39Sopenharmony_ci unsigned char *out, size_t *outl, 136e1051a39Sopenharmony_ci size_t outsize) 137e1051a39Sopenharmony_ci{ 138e1051a39Sopenharmony_ci struct blake2_mac_data_st *macctx = vmacctx; 139e1051a39Sopenharmony_ci 140e1051a39Sopenharmony_ci if (!ossl_prov_is_running()) 141e1051a39Sopenharmony_ci return 0; 142e1051a39Sopenharmony_ci 143e1051a39Sopenharmony_ci *outl = blake2_mac_size(macctx); 144e1051a39Sopenharmony_ci return BLAKE2_FINAL(out, &macctx->ctx); 145e1051a39Sopenharmony_ci} 146e1051a39Sopenharmony_ci 147e1051a39Sopenharmony_cistatic const OSSL_PARAM known_gettable_ctx_params[] = { 148e1051a39Sopenharmony_ci OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), 149e1051a39Sopenharmony_ci OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL), 150e1051a39Sopenharmony_ci OSSL_PARAM_END 151e1051a39Sopenharmony_ci}; 152e1051a39Sopenharmony_cistatic const OSSL_PARAM *blake2_gettable_ctx_params(ossl_unused void *ctx, 153e1051a39Sopenharmony_ci ossl_unused void *provctx) 154e1051a39Sopenharmony_ci{ 155e1051a39Sopenharmony_ci return known_gettable_ctx_params; 156e1051a39Sopenharmony_ci} 157e1051a39Sopenharmony_ci 158e1051a39Sopenharmony_cistatic int blake2_get_ctx_params(void *vmacctx, OSSL_PARAM params[]) 159e1051a39Sopenharmony_ci{ 160e1051a39Sopenharmony_ci OSSL_PARAM *p; 161e1051a39Sopenharmony_ci 162e1051a39Sopenharmony_ci if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL 163e1051a39Sopenharmony_ci && !OSSL_PARAM_set_size_t(p, blake2_mac_size(vmacctx))) 164e1051a39Sopenharmony_ci return 0; 165e1051a39Sopenharmony_ci 166e1051a39Sopenharmony_ci if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL 167e1051a39Sopenharmony_ci && !OSSL_PARAM_set_size_t(p, BLAKE2_BLOCKBYTES)) 168e1051a39Sopenharmony_ci return 0; 169e1051a39Sopenharmony_ci 170e1051a39Sopenharmony_ci return 1; 171e1051a39Sopenharmony_ci} 172e1051a39Sopenharmony_ci 173e1051a39Sopenharmony_cistatic const OSSL_PARAM known_settable_ctx_params[] = { 174e1051a39Sopenharmony_ci OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), 175e1051a39Sopenharmony_ci OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0), 176e1051a39Sopenharmony_ci OSSL_PARAM_octet_string(OSSL_MAC_PARAM_CUSTOM, NULL, 0), 177e1051a39Sopenharmony_ci OSSL_PARAM_octet_string(OSSL_MAC_PARAM_SALT, NULL, 0), 178e1051a39Sopenharmony_ci OSSL_PARAM_END 179e1051a39Sopenharmony_ci}; 180e1051a39Sopenharmony_cistatic const OSSL_PARAM *blake2_mac_settable_ctx_params( 181e1051a39Sopenharmony_ci ossl_unused void *ctx, ossl_unused void *p_ctx) 182e1051a39Sopenharmony_ci{ 183e1051a39Sopenharmony_ci return known_settable_ctx_params; 184e1051a39Sopenharmony_ci} 185e1051a39Sopenharmony_ci 186e1051a39Sopenharmony_ci/* 187e1051a39Sopenharmony_ci * ALL parameters should be set before init(). 188e1051a39Sopenharmony_ci */ 189e1051a39Sopenharmony_cistatic int blake2_mac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[]) 190e1051a39Sopenharmony_ci{ 191e1051a39Sopenharmony_ci struct blake2_mac_data_st *macctx = vmacctx; 192e1051a39Sopenharmony_ci const OSSL_PARAM *p; 193e1051a39Sopenharmony_ci 194e1051a39Sopenharmony_ci if (params == NULL) 195e1051a39Sopenharmony_ci return 1; 196e1051a39Sopenharmony_ci 197e1051a39Sopenharmony_ci if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SIZE)) != NULL) { 198e1051a39Sopenharmony_ci size_t size; 199e1051a39Sopenharmony_ci 200e1051a39Sopenharmony_ci if (!OSSL_PARAM_get_size_t(p, &size) 201e1051a39Sopenharmony_ci || size < 1 202e1051a39Sopenharmony_ci || size > BLAKE2_OUTBYTES) { 203e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_PROV, PROV_R_NOT_XOF_OR_INVALID_LENGTH); 204e1051a39Sopenharmony_ci return 0; 205e1051a39Sopenharmony_ci } 206e1051a39Sopenharmony_ci BLAKE2_PARAM_SET_DIGEST_LENGTH(&macctx->params, (uint8_t)size); 207e1051a39Sopenharmony_ci } 208e1051a39Sopenharmony_ci 209e1051a39Sopenharmony_ci if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL 210e1051a39Sopenharmony_ci && !blake2_setkey(macctx, p->data, p->data_size)) 211e1051a39Sopenharmony_ci return 0; 212e1051a39Sopenharmony_ci 213e1051a39Sopenharmony_ci if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CUSTOM)) 214e1051a39Sopenharmony_ci != NULL) { 215e1051a39Sopenharmony_ci /* 216e1051a39Sopenharmony_ci * The OSSL_PARAM API doesn't provide direct pointer use, so we 217e1051a39Sopenharmony_ci * must handle the OSSL_PARAM structure ourselves here 218e1051a39Sopenharmony_ci */ 219e1051a39Sopenharmony_ci if (p->data_size > BLAKE2_PERSONALBYTES) { 220e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CUSTOM_LENGTH); 221e1051a39Sopenharmony_ci return 0; 222e1051a39Sopenharmony_ci } 223e1051a39Sopenharmony_ci BLAKE2_PARAM_SET_PERSONAL(&macctx->params, p->data, p->data_size); 224e1051a39Sopenharmony_ci } 225e1051a39Sopenharmony_ci 226e1051a39Sopenharmony_ci if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SALT)) != NULL) { 227e1051a39Sopenharmony_ci /* 228e1051a39Sopenharmony_ci * The OSSL_PARAM API doesn't provide direct pointer use, so we 229e1051a39Sopenharmony_ci * must handle the OSSL_PARAM structure ourselves here as well 230e1051a39Sopenharmony_ci */ 231e1051a39Sopenharmony_ci if (p->data_size > BLAKE2_SALTBYTES) { 232e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH); 233e1051a39Sopenharmony_ci return 0; 234e1051a39Sopenharmony_ci } 235e1051a39Sopenharmony_ci BLAKE2_PARAM_SET_SALT(&macctx->params, p->data, p->data_size); 236e1051a39Sopenharmony_ci } 237e1051a39Sopenharmony_ci return 1; 238e1051a39Sopenharmony_ci} 239e1051a39Sopenharmony_ci 240e1051a39Sopenharmony_ciconst OSSL_DISPATCH BLAKE2_FUNCTIONS[] = { 241e1051a39Sopenharmony_ci { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))blake2_mac_new }, 242e1051a39Sopenharmony_ci { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))blake2_mac_dup }, 243e1051a39Sopenharmony_ci { OSSL_FUNC_MAC_FREECTX, (void (*)(void))blake2_mac_free }, 244e1051a39Sopenharmony_ci { OSSL_FUNC_MAC_INIT, (void (*)(void))blake2_mac_init }, 245e1051a39Sopenharmony_ci { OSSL_FUNC_MAC_UPDATE, (void (*)(void))blake2_mac_update }, 246e1051a39Sopenharmony_ci { OSSL_FUNC_MAC_FINAL, (void (*)(void))blake2_mac_final }, 247e1051a39Sopenharmony_ci { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS, 248e1051a39Sopenharmony_ci (void (*)(void))blake2_gettable_ctx_params }, 249e1051a39Sopenharmony_ci { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))blake2_get_ctx_params }, 250e1051a39Sopenharmony_ci { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, 251e1051a39Sopenharmony_ci (void (*)(void))blake2_mac_settable_ctx_params }, 252e1051a39Sopenharmony_ci { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))blake2_mac_set_ctx_params }, 253e1051a39Sopenharmony_ci { 0, NULL } 254e1051a39Sopenharmony_ci}; 255