]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/macs/blake2_mac_impl.c
mac: add FIPS error state handling
[thirdparty/openssl.git] / providers / implementations / macs / blake2_mac_impl.c
1 /*
2 * Copyright 2018-2020 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
14 #include "prov/blake2.h"
15 #include "internal/cryptlib.h"
16 #include "prov/providercommonerr.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 size_t blake2_mac_size(void *vmacctx);
43
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
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
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
85 static int blake2_mac_init(void *vmacctx)
86 {
87 struct blake2_mac_data_st *macctx = vmacctx;
88
89 if (!ossl_prov_is_running())
90 return 0;
91
92 /* Check key has been set */
93 if (macctx->params.key_length == 0) {
94 ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
95 return 0;
96 }
97
98 return BLAKE2_INIT_KEY(&macctx->ctx, &macctx->params, macctx->key);
99 }
100
101 static int blake2_mac_update(void *vmacctx,
102 const unsigned char *data, size_t datalen)
103 {
104 struct blake2_mac_data_st *macctx = vmacctx;
105
106 if (datalen == 0)
107 return 1;
108
109 return BLAKE2_UPDATE(&macctx->ctx, data, datalen);
110 }
111
112 static int blake2_mac_final(void *vmacctx,
113 unsigned char *out, size_t *outl,
114 size_t outsize)
115 {
116 struct blake2_mac_data_st *macctx = vmacctx;
117
118 if (!ossl_prov_is_running())
119 return 0;
120
121 *outl = blake2_mac_size(macctx);
122 return BLAKE2_FINAL(out, &macctx->ctx);
123 }
124
125 static const OSSL_PARAM known_gettable_ctx_params[] = {
126 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
127 OSSL_PARAM_END
128 };
129 static const OSSL_PARAM *blake2_gettable_ctx_params(ossl_unused void *provctx)
130 {
131 return known_gettable_ctx_params;
132 }
133
134 static int blake2_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
135 {
136 OSSL_PARAM *p;
137
138 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
139 return OSSL_PARAM_set_size_t(p, blake2_mac_size(vmacctx));
140
141 return 1;
142 }
143
144 static const OSSL_PARAM known_settable_ctx_params[] = {
145 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
146 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
147 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_CUSTOM, NULL, 0),
148 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_SALT, NULL, 0),
149 OSSL_PARAM_END
150 };
151 static const OSSL_PARAM *blake2_mac_settable_ctx_params(ossl_unused void *p_ctx)
152 {
153 return known_settable_ctx_params;
154 }
155
156 /*
157 * ALL parameters should be set before init().
158 */
159 static int blake2_mac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
160 {
161 struct blake2_mac_data_st *macctx = vmacctx;
162 const OSSL_PARAM *p;
163
164 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SIZE)) != NULL) {
165 size_t size;
166
167 if (!OSSL_PARAM_get_size_t(p, &size)
168 || size < 1
169 || size > BLAKE2_OUTBYTES) {
170 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_XOF_OR_INVALID_LENGTH);
171 return 0;
172 }
173 BLAKE2_PARAM_SET_DIGEST_LENGTH(&macctx->params, (uint8_t)size);
174 }
175
176 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
177 size_t len;
178 void *key_p = macctx->key;
179
180 if (!OSSL_PARAM_get_octet_string(p, &key_p, BLAKE2_KEYBYTES, &len)) {
181 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
182 return 0;
183 }
184 /* Pad with zeroes at the end */
185 memset(macctx->key + len, 0, BLAKE2_KEYBYTES - len);
186
187 BLAKE2_PARAM_SET_KEY_LENGTH(&macctx->params, (uint8_t)len);
188 }
189
190 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CUSTOM))
191 != NULL) {
192 /*
193 * The OSSL_PARAM API doesn't provide direct pointer use, so we
194 * must handle the OSSL_PARAM structure ourselves here
195 */
196 if (p->data_size > BLAKE2_PERSONALBYTES) {
197 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CUSTOM_LENGTH);
198 return 0;
199 }
200 BLAKE2_PARAM_SET_PERSONAL(&macctx->params, p->data, p->data_size);
201 }
202
203 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SALT)) != NULL) {
204 /*
205 * The OSSL_PARAM API doesn't provide direct pointer use, so we
206 * must handle the OSSL_PARAM structure ourselves here as well
207 */
208 if (p->data_size > BLAKE2_SALTBYTES) {
209 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
210 return 0;
211 }
212 BLAKE2_PARAM_SET_SALT(&macctx->params, p->data, p->data_size);
213 }
214 return 1;
215 }
216
217 static size_t blake2_mac_size(void *vmacctx)
218 {
219 struct blake2_mac_data_st *macctx = vmacctx;
220
221 return macctx->params.digest_length;
222 }
223
224 const OSSL_DISPATCH BLAKE2_FUNCTIONS[] = {
225 { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))blake2_mac_new },
226 { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))blake2_mac_dup },
227 { OSSL_FUNC_MAC_FREECTX, (void (*)(void))blake2_mac_free },
228 { OSSL_FUNC_MAC_INIT, (void (*)(void))blake2_mac_init },
229 { OSSL_FUNC_MAC_UPDATE, (void (*)(void))blake2_mac_update },
230 { OSSL_FUNC_MAC_FINAL, (void (*)(void))blake2_mac_final },
231 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
232 (void (*)(void))blake2_gettable_ctx_params },
233 { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))blake2_get_ctx_params },
234 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
235 (void (*)(void))blake2_mac_settable_ctx_params },
236 { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))blake2_mac_set_ctx_params },
237 { 0, NULL }
238 };