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