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