]>
Commit | Line | Data |
---|---|---|
567db2c1 | 1 | /* |
8020d79b | 2 | * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved. |
567db2c1 | 3 | * |
4a8b0c55 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
567db2c1 RL |
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 <string.h> | |
11 | #include <stdarg.h> | |
12 | #include <openssl/evp.h> | |
13 | #include <openssl/err.h> | |
e74bd290 RL |
14 | #include <openssl/core.h> |
15 | #include <openssl/core_names.h> | |
50cd4768 | 16 | #include <openssl/types.h> |
567db2c1 | 17 | #include "internal/nelem.h" |
25f2138b | 18 | #include "crypto/evp.h" |
e74bd290 | 19 | #include "internal/provider.h" |
706457b7 | 20 | #include "evp_local.h" |
567db2c1 | 21 | |
865adf97 | 22 | EVP_MAC_CTX *EVP_MAC_CTX_new(EVP_MAC *mac) |
567db2c1 RL |
23 | { |
24 | EVP_MAC_CTX *ctx = OPENSSL_zalloc(sizeof(EVP_MAC_CTX)); | |
25 | ||
e077455e | 26 | if (ctx != NULL) { |
567db2c1 | 27 | ctx->meth = mac; |
e077455e RL |
28 | if ((ctx->algctx = mac->newctx(ossl_provider_ctx(mac->prov))) == NULL |
29 | || !EVP_MAC_up_ref(mac)) { | |
30 | mac->freectx(ctx->algctx); | |
31 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); | |
32 | OPENSSL_free(ctx); | |
33 | ctx = NULL; | |
34 | } | |
567db2c1 RL |
35 | } |
36 | return ctx; | |
37 | } | |
38 | ||
865adf97 | 39 | void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx) |
567db2c1 | 40 | { |
543e740b RS |
41 | if (ctx == NULL) |
42 | return; | |
7c14d0c1 SL |
43 | ctx->meth->freectx(ctx->algctx); |
44 | ctx->algctx = NULL; | |
543e740b RS |
45 | /* refcnt-- */ |
46 | EVP_MAC_free(ctx->meth); | |
567db2c1 RL |
47 | OPENSSL_free(ctx); |
48 | } | |
49 | ||
865adf97 | 50 | EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src) |
567db2c1 | 51 | { |
7ed66e26 | 52 | EVP_MAC_CTX *dst; |
567db2c1 | 53 | |
7c14d0c1 | 54 | if (src->algctx == NULL) |
be5fc053 KR |
55 | return NULL; |
56 | ||
57 | dst = OPENSSL_malloc(sizeof(*dst)); | |
e077455e | 58 | if (dst == NULL) |
be5fc053 | 59 | return NULL; |
567db2c1 | 60 | |
567db2c1 | 61 | *dst = *src; |
e74bd290 | 62 | if (!EVP_MAC_up_ref(dst->meth)) { |
e077455e | 63 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
e74bd290 RL |
64 | OPENSSL_free(dst); |
65 | return NULL; | |
66 | } | |
567db2c1 | 67 | |
7c14d0c1 SL |
68 | dst->algctx = src->meth->dupctx(src->algctx); |
69 | if (dst->algctx == NULL) { | |
865adf97 | 70 | EVP_MAC_CTX_free(dst); |
be5fc053 KR |
71 | return NULL; |
72 | } | |
73 | ||
74 | return dst; | |
567db2c1 RL |
75 | } |
76 | ||
ed576acd | 77 | EVP_MAC *EVP_MAC_CTX_get0_mac(EVP_MAC_CTX *ctx) |
567db2c1 RL |
78 | { |
79 | return ctx->meth; | |
80 | } | |
81 | ||
eb1b66f0 | 82 | static size_t get_size_t_ctx_param(EVP_MAC_CTX *ctx, const char *name) |
567db2c1 | 83 | { |
e74bd290 RL |
84 | size_t sz = 0; |
85 | ||
7c14d0c1 | 86 | if (ctx->algctx != NULL) { |
e74bd290 RL |
87 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
88 | ||
eb1b66f0 | 89 | params[0] = OSSL_PARAM_construct_size_t(name, &sz); |
92d9d0ae | 90 | if (ctx->meth->get_ctx_params != NULL) { |
7c14d0c1 | 91 | if (ctx->meth->get_ctx_params(ctx->algctx, params)) |
e74bd290 RL |
92 | return sz; |
93 | } else if (ctx->meth->get_params != NULL) { | |
94 | if (ctx->meth->get_params(params)) | |
95 | return sz; | |
96 | } | |
97 | } | |
98 | /* | |
99 | * If the MAC hasn't been initialized yet, or there is no size to get, | |
100 | * we return zero | |
101 | */ | |
567db2c1 RL |
102 | return 0; |
103 | } | |
104 | ||
eb1b66f0 P |
105 | size_t EVP_MAC_CTX_get_mac_size(EVP_MAC_CTX *ctx) |
106 | { | |
107 | return get_size_t_ctx_param(ctx, OSSL_MAC_PARAM_SIZE); | |
108 | } | |
109 | ||
110 | size_t EVP_MAC_CTX_get_block_size(EVP_MAC_CTX *ctx) | |
111 | { | |
112 | return get_size_t_ctx_param(ctx, OSSL_MAC_PARAM_BLOCK_SIZE); | |
113 | } | |
114 | ||
cc2314a9 P |
115 | int EVP_MAC_init(EVP_MAC_CTX *ctx, const unsigned char *key, size_t keylen, |
116 | const OSSL_PARAM params[]) | |
567db2c1 | 117 | { |
7c14d0c1 | 118 | return ctx->meth->init(ctx->algctx, key, keylen, params); |
567db2c1 RL |
119 | } |
120 | ||
121 | int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen) | |
122 | { | |
7c14d0c1 | 123 | return ctx->meth->update(ctx->algctx, data, datalen); |
567db2c1 RL |
124 | } |
125 | ||
b039c87a P |
126 | static int evp_mac_final(EVP_MAC_CTX *ctx, int xof, |
127 | unsigned char *out, size_t *outl, size_t outsize) | |
567db2c1 | 128 | { |
5f6a0b2f | 129 | size_t l; |
b039c87a P |
130 | int res; |
131 | OSSL_PARAM params[2]; | |
b97f4dd7 | 132 | size_t macsize; |
b039c87a P |
133 | |
134 | if (ctx == NULL || ctx->meth == NULL) { | |
135 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM); | |
136 | return 0; | |
137 | } | |
138 | if (ctx->meth->final == NULL) { | |
139 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); | |
140 | return 0; | |
141 | } | |
567db2c1 | 142 | |
b97f4dd7 | 143 | macsize = EVP_MAC_CTX_get_mac_size(ctx); |
b039c87a P |
144 | if (out == NULL) { |
145 | if (outl == NULL) { | |
146 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); | |
147 | return 0; | |
148 | } | |
b97f4dd7 | 149 | *outl = macsize; |
b039c87a P |
150 | return 1; |
151 | } | |
b97f4dd7 MC |
152 | if (outsize < macsize) { |
153 | ERR_raise(ERR_LIB_EVP, EVP_R_BUFFER_TOO_SMALL); | |
154 | return 0; | |
155 | } | |
b039c87a P |
156 | if (xof) { |
157 | params[0] = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_XOF, &xof); | |
158 | params[1] = OSSL_PARAM_construct_end(); | |
159 | ||
160 | if (EVP_MAC_CTX_set_params(ctx, params) <= 0) { | |
161 | ERR_raise(ERR_LIB_EVP, EVP_R_SETTING_XOF_FAILED); | |
162 | return 0; | |
163 | } | |
164 | } | |
7c14d0c1 | 165 | res = ctx->meth->final(ctx->algctx, out, &l, outsize); |
e74bd290 RL |
166 | if (outl != NULL) |
167 | *outl = l; | |
a85c9021 | 168 | return res; |
567db2c1 RL |
169 | } |
170 | ||
b039c87a P |
171 | int EVP_MAC_final(EVP_MAC_CTX *ctx, |
172 | unsigned char *out, size_t *outl, size_t outsize) | |
173 | { | |
174 | return evp_mac_final(ctx, 0, out, outl, outsize); | |
175 | } | |
176 | ||
177 | int EVP_MAC_finalXOF(EVP_MAC_CTX *ctx, unsigned char *out, size_t outsize) | |
178 | { | |
179 | return evp_mac_final(ctx, 1, out, NULL, outsize); | |
180 | } | |
181 | ||
e74bd290 RL |
182 | /* |
183 | * The {get,set}_params functions return 1 if there is no corresponding | |
184 | * function in the implementation. This is the same as if there was one, | |
185 | * but it didn't recognise any of the given params, i.e. nothing in the | |
186 | * bag of parameters was useful. | |
187 | */ | |
188 | int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[]) | |
567db2c1 | 189 | { |
e74bd290 RL |
190 | if (mac->get_params != NULL) |
191 | return mac->get_params(params); | |
192 | return 1; | |
567db2c1 RL |
193 | } |
194 | ||
865adf97 | 195 | int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[]) |
567db2c1 | 196 | { |
92d9d0ae | 197 | if (ctx->meth->get_ctx_params != NULL) |
7c14d0c1 | 198 | return ctx->meth->get_ctx_params(ctx->algctx, params); |
e74bd290 | 199 | return 1; |
567db2c1 RL |
200 | } |
201 | ||
865adf97 | 202 | int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[]) |
567db2c1 | 203 | { |
92d9d0ae | 204 | if (ctx->meth->set_ctx_params != NULL) |
7c14d0c1 | 205 | return ctx->meth->set_ctx_params(ctx->algctx, params); |
e74bd290 | 206 | return 1; |
567db2c1 | 207 | } |
251e610c | 208 | |
bcd5d3a2 | 209 | int evp_mac_get_number(const EVP_MAC *mac) |
506cb0f6 RL |
210 | { |
211 | return mac->name_id; | |
212 | } | |
213 | ||
ed576acd | 214 | const char *EVP_MAC_get0_name(const EVP_MAC *mac) |
c9452d74 | 215 | { |
6c9bc258 | 216 | return mac->type_name; |
c9452d74 P |
217 | } |
218 | ||
ed576acd | 219 | const char *EVP_MAC_get0_description(const EVP_MAC *mac) |
03888233 RL |
220 | { |
221 | return mac->description; | |
222 | } | |
223 | ||
251e610c RL |
224 | int EVP_MAC_is_a(const EVP_MAC *mac, const char *name) |
225 | { | |
ee8db8c5 | 226 | return mac != NULL && evp_is_a(mac->prov, mac->name_id, NULL, name); |
251e610c | 227 | } |
f651c727 | 228 | |
d84f5515 MC |
229 | int EVP_MAC_names_do_all(const EVP_MAC *mac, |
230 | void (*fn)(const char *name, void *data), | |
231 | void *data) | |
f651c727 RL |
232 | { |
233 | if (mac->prov != NULL) | |
d84f5515 MC |
234 | return evp_names_do_all(mac->prov, mac->name_id, fn, data); |
235 | ||
236 | return 1; | |
f651c727 | 237 | } |
0a8a6afd | 238 | |
006de767 RL |
239 | unsigned char *EVP_Q_mac(OSSL_LIB_CTX *libctx, |
240 | const char *name, const char *propq, | |
0a8a6afd DDO |
241 | const char *subalg, const OSSL_PARAM *params, |
242 | const void *key, size_t keylen, | |
243 | const unsigned char *data, size_t datalen, | |
006de767 | 244 | unsigned char *out, size_t outsize, size_t *outlen) |
0a8a6afd DDO |
245 | { |
246 | EVP_MAC *mac = EVP_MAC_fetch(libctx, name, propq); | |
247 | OSSL_PARAM subalg_param[] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
248 | EVP_MAC_CTX *ctx = NULL; | |
006de767 | 249 | size_t len = 0; |
0a8a6afd DDO |
250 | unsigned char *res = NULL; |
251 | ||
252 | if (outlen != NULL) | |
253 | *outlen = 0; | |
254 | if (mac == NULL) | |
255 | return NULL; | |
256 | if (subalg != NULL) { | |
257 | const OSSL_PARAM *defined_params = EVP_MAC_settable_ctx_params(mac); | |
258 | const char *param_name = OSSL_MAC_PARAM_DIGEST; | |
259 | ||
260 | /* | |
261 | * The underlying algorithm may be a cipher or a digest. | |
262 | * We don't know which it is, but we can ask the MAC what it | |
263 | * should be and bet on that. | |
264 | */ | |
265 | if (OSSL_PARAM_locate_const(defined_params, param_name) == NULL) { | |
266 | param_name = OSSL_MAC_PARAM_CIPHER; | |
267 | if (OSSL_PARAM_locate_const(defined_params, param_name) == NULL) { | |
268 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT); | |
269 | goto err; | |
270 | } | |
271 | } | |
272 | subalg_param[0] = | |
273 | OSSL_PARAM_construct_utf8_string(param_name, (char *)subalg, 0); | |
274 | } | |
275 | /* Single-shot - on NULL key input, set dummy key value for EVP_MAC_Init. */ | |
276 | if (key == NULL && keylen == 0) | |
277 | key = data; | |
278 | if ((ctx = EVP_MAC_CTX_new(mac)) != NULL | |
279 | && EVP_MAC_CTX_set_params(ctx, subalg_param) | |
280 | && EVP_MAC_CTX_set_params(ctx, params) | |
281 | && EVP_MAC_init(ctx, key, keylen, params) | |
282 | && EVP_MAC_update(ctx, data, datalen) | |
283 | && EVP_MAC_final(ctx, out, &len, outsize)) { | |
284 | if (out == NULL) { | |
285 | out = OPENSSL_malloc(len); | |
286 | if (out != NULL && !EVP_MAC_final(ctx, out, NULL, len)) { | |
287 | OPENSSL_free(out); | |
288 | out = NULL; | |
289 | } | |
290 | } | |
291 | res = out; | |
292 | if (res != NULL && outlen != NULL) | |
006de767 | 293 | *outlen = len; |
0a8a6afd DDO |
294 | } |
295 | ||
296 | err: | |
297 | EVP_MAC_CTX_free(ctx); | |
298 | EVP_MAC_free(mac); | |
299 | return res; | |
300 | } |