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