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