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