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