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