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