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