xref: /freebsd/crypto/openssl/providers/implementations/signature/mac_legacy_sig.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery  * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6*b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery  */
9*b077aed3SPierre Pronchery 
10*b077aed3SPierre Pronchery /* We need to use some engine deprecated APIs */
11*b077aed3SPierre Pronchery #define OPENSSL_SUPPRESS_DEPRECATED
12*b077aed3SPierre Pronchery 
13*b077aed3SPierre Pronchery #include <openssl/crypto.h>
14*b077aed3SPierre Pronchery #include <openssl/evp.h>
15*b077aed3SPierre Pronchery #include <openssl/core_dispatch.h>
16*b077aed3SPierre Pronchery #include <openssl/core_names.h>
17*b077aed3SPierre Pronchery #include <openssl/params.h>
18*b077aed3SPierre Pronchery #include <openssl/err.h>
19*b077aed3SPierre Pronchery #include <openssl/proverr.h>
20*b077aed3SPierre Pronchery #ifndef FIPS_MODULE
21*b077aed3SPierre Pronchery # include <openssl/engine.h>
22*b077aed3SPierre Pronchery #endif
23*b077aed3SPierre Pronchery #include "prov/implementations.h"
24*b077aed3SPierre Pronchery #include "prov/provider_ctx.h"
25*b077aed3SPierre Pronchery #include "prov/macsignature.h"
26*b077aed3SPierre Pronchery #include "prov/providercommon.h"
27*b077aed3SPierre Pronchery 
28*b077aed3SPierre Pronchery static OSSL_FUNC_signature_newctx_fn mac_hmac_newctx;
29*b077aed3SPierre Pronchery static OSSL_FUNC_signature_newctx_fn mac_siphash_newctx;
30*b077aed3SPierre Pronchery static OSSL_FUNC_signature_newctx_fn mac_poly1305_newctx;
31*b077aed3SPierre Pronchery static OSSL_FUNC_signature_newctx_fn mac_cmac_newctx;
32*b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_sign_init_fn mac_digest_sign_init;
33*b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_sign_update_fn mac_digest_sign_update;
34*b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_sign_final_fn mac_digest_sign_final;
35*b077aed3SPierre Pronchery static OSSL_FUNC_signature_freectx_fn mac_freectx;
36*b077aed3SPierre Pronchery static OSSL_FUNC_signature_dupctx_fn mac_dupctx;
37*b077aed3SPierre Pronchery static OSSL_FUNC_signature_set_ctx_params_fn mac_set_ctx_params;
38*b077aed3SPierre Pronchery static OSSL_FUNC_signature_settable_ctx_params_fn mac_hmac_settable_ctx_params;
39*b077aed3SPierre Pronchery static OSSL_FUNC_signature_settable_ctx_params_fn mac_siphash_settable_ctx_params;
40*b077aed3SPierre Pronchery static OSSL_FUNC_signature_settable_ctx_params_fn mac_poly1305_settable_ctx_params;
41*b077aed3SPierre Pronchery static OSSL_FUNC_signature_settable_ctx_params_fn mac_cmac_settable_ctx_params;
42*b077aed3SPierre Pronchery 
43*b077aed3SPierre Pronchery typedef struct {
44*b077aed3SPierre Pronchery     OSSL_LIB_CTX *libctx;
45*b077aed3SPierre Pronchery     char *propq;
46*b077aed3SPierre Pronchery     MAC_KEY *key;
47*b077aed3SPierre Pronchery     EVP_MAC_CTX *macctx;
48*b077aed3SPierre Pronchery } PROV_MAC_CTX;
49*b077aed3SPierre Pronchery 
mac_newctx(void * provctx,const char * propq,const char * macname)50*b077aed3SPierre Pronchery static void *mac_newctx(void *provctx, const char *propq, const char *macname)
51*b077aed3SPierre Pronchery {
52*b077aed3SPierre Pronchery     PROV_MAC_CTX *pmacctx;
53*b077aed3SPierre Pronchery     EVP_MAC *mac = NULL;
54*b077aed3SPierre Pronchery 
55*b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
56*b077aed3SPierre Pronchery         return NULL;
57*b077aed3SPierre Pronchery 
58*b077aed3SPierre Pronchery     pmacctx = OPENSSL_zalloc(sizeof(PROV_MAC_CTX));
59*b077aed3SPierre Pronchery     if (pmacctx == NULL)
60*b077aed3SPierre Pronchery         return NULL;
61*b077aed3SPierre Pronchery 
62*b077aed3SPierre Pronchery     pmacctx->libctx = PROV_LIBCTX_OF(provctx);
63*b077aed3SPierre Pronchery     if (propq != NULL && (pmacctx->propq = OPENSSL_strdup(propq)) == NULL) {
64*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
65*b077aed3SPierre Pronchery         goto err;
66*b077aed3SPierre Pronchery     }
67*b077aed3SPierre Pronchery 
68*b077aed3SPierre Pronchery     mac = EVP_MAC_fetch(pmacctx->libctx, macname, propq);
69*b077aed3SPierre Pronchery     if (mac == NULL)
70*b077aed3SPierre Pronchery         goto err;
71*b077aed3SPierre Pronchery 
72*b077aed3SPierre Pronchery     pmacctx->macctx = EVP_MAC_CTX_new(mac);
73*b077aed3SPierre Pronchery     if (pmacctx->macctx == NULL)
74*b077aed3SPierre Pronchery         goto err;
75*b077aed3SPierre Pronchery 
76*b077aed3SPierre Pronchery     EVP_MAC_free(mac);
77*b077aed3SPierre Pronchery 
78*b077aed3SPierre Pronchery     return pmacctx;
79*b077aed3SPierre Pronchery 
80*b077aed3SPierre Pronchery  err:
81*b077aed3SPierre Pronchery     OPENSSL_free(pmacctx->propq);
82*b077aed3SPierre Pronchery     OPENSSL_free(pmacctx);
83*b077aed3SPierre Pronchery     EVP_MAC_free(mac);
84*b077aed3SPierre Pronchery     return NULL;
85*b077aed3SPierre Pronchery }
86*b077aed3SPierre Pronchery 
87*b077aed3SPierre Pronchery #define MAC_NEWCTX(funcname, macname) \
88*b077aed3SPierre Pronchery     static void *mac_##funcname##_newctx(void *provctx, const char *propq) \
89*b077aed3SPierre Pronchery     { \
90*b077aed3SPierre Pronchery         return mac_newctx(provctx, propq, macname); \
91*b077aed3SPierre Pronchery     }
92*b077aed3SPierre Pronchery 
93*b077aed3SPierre Pronchery MAC_NEWCTX(hmac, "HMAC")
94*b077aed3SPierre Pronchery MAC_NEWCTX(siphash, "SIPHASH")
95*b077aed3SPierre Pronchery MAC_NEWCTX(poly1305, "POLY1305")
96*b077aed3SPierre Pronchery MAC_NEWCTX(cmac, "CMAC")
97*b077aed3SPierre Pronchery 
mac_digest_sign_init(void * vpmacctx,const char * mdname,void * vkey,const OSSL_PARAM params[])98*b077aed3SPierre Pronchery static int mac_digest_sign_init(void *vpmacctx, const char *mdname, void *vkey,
99*b077aed3SPierre Pronchery                                 const OSSL_PARAM params[])
100*b077aed3SPierre Pronchery {
101*b077aed3SPierre Pronchery     PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
102*b077aed3SPierre Pronchery     const char *ciphername = NULL, *engine = NULL;
103*b077aed3SPierre Pronchery 
104*b077aed3SPierre Pronchery     if (!ossl_prov_is_running()
105*b077aed3SPierre Pronchery         || pmacctx == NULL)
106*b077aed3SPierre Pronchery         return 0;
107*b077aed3SPierre Pronchery 
108*b077aed3SPierre Pronchery     if (pmacctx->key == NULL && vkey == NULL) {
109*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
110*b077aed3SPierre Pronchery         return 0;
111*b077aed3SPierre Pronchery     }
112*b077aed3SPierre Pronchery 
113*b077aed3SPierre Pronchery     if (vkey != NULL) {
114*b077aed3SPierre Pronchery         if (!ossl_mac_key_up_ref(vkey))
115*b077aed3SPierre Pronchery             return 0;
116*b077aed3SPierre Pronchery         ossl_mac_key_free(pmacctx->key);
117*b077aed3SPierre Pronchery         pmacctx->key = vkey;
118*b077aed3SPierre Pronchery     }
119*b077aed3SPierre Pronchery 
120*b077aed3SPierre Pronchery     if (pmacctx->key->cipher.cipher != NULL)
121*b077aed3SPierre Pronchery         ciphername = (char *)EVP_CIPHER_get0_name(pmacctx->key->cipher.cipher);
122*b077aed3SPierre Pronchery #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
123*b077aed3SPierre Pronchery     if (pmacctx->key->cipher.engine != NULL)
124*b077aed3SPierre Pronchery         engine = (char *)ENGINE_get_id(pmacctx->key->cipher.engine);
125*b077aed3SPierre Pronchery #endif
126*b077aed3SPierre Pronchery 
127*b077aed3SPierre Pronchery     if (!ossl_prov_set_macctx(pmacctx->macctx, NULL,
128*b077aed3SPierre Pronchery                               (char *)ciphername,
129*b077aed3SPierre Pronchery                               (char *)mdname,
130*b077aed3SPierre Pronchery                               (char *)engine,
131*b077aed3SPierre Pronchery                               pmacctx->key->properties,
132*b077aed3SPierre Pronchery                               NULL, 0))
133*b077aed3SPierre Pronchery         return 0;
134*b077aed3SPierre Pronchery 
135*b077aed3SPierre Pronchery     if (!EVP_MAC_init(pmacctx->macctx, pmacctx->key->priv_key,
136*b077aed3SPierre Pronchery                       pmacctx->key->priv_key_len, params))
137*b077aed3SPierre Pronchery         return 0;
138*b077aed3SPierre Pronchery 
139*b077aed3SPierre Pronchery     return 1;
140*b077aed3SPierre Pronchery }
141*b077aed3SPierre Pronchery 
mac_digest_sign_update(void * vpmacctx,const unsigned char * data,size_t datalen)142*b077aed3SPierre Pronchery int mac_digest_sign_update(void *vpmacctx, const unsigned char *data,
143*b077aed3SPierre Pronchery                            size_t datalen)
144*b077aed3SPierre Pronchery {
145*b077aed3SPierre Pronchery     PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
146*b077aed3SPierre Pronchery 
147*b077aed3SPierre Pronchery     if (pmacctx == NULL || pmacctx->macctx == NULL)
148*b077aed3SPierre Pronchery         return 0;
149*b077aed3SPierre Pronchery 
150*b077aed3SPierre Pronchery     return EVP_MAC_update(pmacctx->macctx, data, datalen);
151*b077aed3SPierre Pronchery }
152*b077aed3SPierre Pronchery 
mac_digest_sign_final(void * vpmacctx,unsigned char * mac,size_t * maclen,size_t macsize)153*b077aed3SPierre Pronchery int mac_digest_sign_final(void *vpmacctx, unsigned char *mac, size_t *maclen,
154*b077aed3SPierre Pronchery                           size_t macsize)
155*b077aed3SPierre Pronchery {
156*b077aed3SPierre Pronchery     PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
157*b077aed3SPierre Pronchery 
158*b077aed3SPierre Pronchery     if (!ossl_prov_is_running() || pmacctx == NULL || pmacctx->macctx == NULL)
159*b077aed3SPierre Pronchery         return 0;
160*b077aed3SPierre Pronchery 
161*b077aed3SPierre Pronchery     return EVP_MAC_final(pmacctx->macctx, mac, maclen, macsize);
162*b077aed3SPierre Pronchery }
163*b077aed3SPierre Pronchery 
mac_freectx(void * vpmacctx)164*b077aed3SPierre Pronchery static void mac_freectx(void *vpmacctx)
165*b077aed3SPierre Pronchery {
166*b077aed3SPierre Pronchery     PROV_MAC_CTX *ctx = (PROV_MAC_CTX *)vpmacctx;
167*b077aed3SPierre Pronchery 
168*b077aed3SPierre Pronchery     OPENSSL_free(ctx->propq);
169*b077aed3SPierre Pronchery     EVP_MAC_CTX_free(ctx->macctx);
170*b077aed3SPierre Pronchery     ossl_mac_key_free(ctx->key);
171*b077aed3SPierre Pronchery     OPENSSL_free(ctx);
172*b077aed3SPierre Pronchery }
173*b077aed3SPierre Pronchery 
mac_dupctx(void * vpmacctx)174*b077aed3SPierre Pronchery static void *mac_dupctx(void *vpmacctx)
175*b077aed3SPierre Pronchery {
176*b077aed3SPierre Pronchery     PROV_MAC_CTX *srcctx = (PROV_MAC_CTX *)vpmacctx;
177*b077aed3SPierre Pronchery     PROV_MAC_CTX *dstctx;
178*b077aed3SPierre Pronchery 
179*b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
180*b077aed3SPierre Pronchery         return NULL;
181*b077aed3SPierre Pronchery 
182*b077aed3SPierre Pronchery     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
183*b077aed3SPierre Pronchery     if (dstctx == NULL)
184*b077aed3SPierre Pronchery         return NULL;
185*b077aed3SPierre Pronchery 
186*b077aed3SPierre Pronchery     *dstctx = *srcctx;
187*b077aed3SPierre Pronchery     dstctx->propq = NULL;
188*b077aed3SPierre Pronchery     dstctx->key = NULL;
189*b077aed3SPierre Pronchery     dstctx->macctx = NULL;
190*b077aed3SPierre Pronchery 
191*b077aed3SPierre Pronchery     if (srcctx->propq != NULL && (dstctx->propq = OPENSSL_strdup(srcctx->propq)) == NULL)
192*b077aed3SPierre Pronchery         goto err;
193*b077aed3SPierre Pronchery 
194*b077aed3SPierre Pronchery     if (srcctx->key != NULL && !ossl_mac_key_up_ref(srcctx->key))
195*b077aed3SPierre Pronchery         goto err;
196*b077aed3SPierre Pronchery     dstctx->key = srcctx->key;
197*b077aed3SPierre Pronchery 
198*b077aed3SPierre Pronchery     if (srcctx->macctx != NULL) {
199*b077aed3SPierre Pronchery         dstctx->macctx = EVP_MAC_CTX_dup(srcctx->macctx);
200*b077aed3SPierre Pronchery         if (dstctx->macctx == NULL)
201*b077aed3SPierre Pronchery             goto err;
202*b077aed3SPierre Pronchery     }
203*b077aed3SPierre Pronchery 
204*b077aed3SPierre Pronchery     return dstctx;
205*b077aed3SPierre Pronchery  err:
206*b077aed3SPierre Pronchery     mac_freectx(dstctx);
207*b077aed3SPierre Pronchery     return NULL;
208*b077aed3SPierre Pronchery }
209*b077aed3SPierre Pronchery 
mac_set_ctx_params(void * vpmacctx,const OSSL_PARAM params[])210*b077aed3SPierre Pronchery static int mac_set_ctx_params(void *vpmacctx, const OSSL_PARAM params[])
211*b077aed3SPierre Pronchery {
212*b077aed3SPierre Pronchery     PROV_MAC_CTX *ctx = (PROV_MAC_CTX *)vpmacctx;
213*b077aed3SPierre Pronchery 
214*b077aed3SPierre Pronchery     return EVP_MAC_CTX_set_params(ctx->macctx, params);
215*b077aed3SPierre Pronchery }
216*b077aed3SPierre Pronchery 
mac_settable_ctx_params(ossl_unused void * ctx,void * provctx,const char * macname)217*b077aed3SPierre Pronchery static const OSSL_PARAM *mac_settable_ctx_params(ossl_unused void *ctx,
218*b077aed3SPierre Pronchery                                                  void *provctx,
219*b077aed3SPierre Pronchery                                                  const char *macname)
220*b077aed3SPierre Pronchery {
221*b077aed3SPierre Pronchery     EVP_MAC *mac = EVP_MAC_fetch(PROV_LIBCTX_OF(provctx), macname,
222*b077aed3SPierre Pronchery                                  NULL);
223*b077aed3SPierre Pronchery     const OSSL_PARAM *params;
224*b077aed3SPierre Pronchery 
225*b077aed3SPierre Pronchery     if (mac == NULL)
226*b077aed3SPierre Pronchery         return NULL;
227*b077aed3SPierre Pronchery 
228*b077aed3SPierre Pronchery     params = EVP_MAC_settable_ctx_params(mac);
229*b077aed3SPierre Pronchery     EVP_MAC_free(mac);
230*b077aed3SPierre Pronchery 
231*b077aed3SPierre Pronchery     return params;
232*b077aed3SPierre Pronchery }
233*b077aed3SPierre Pronchery 
234*b077aed3SPierre Pronchery #define MAC_SETTABLE_CTX_PARAMS(funcname, macname) \
235*b077aed3SPierre Pronchery     static const OSSL_PARAM *mac_##funcname##_settable_ctx_params(void *ctx, \
236*b077aed3SPierre Pronchery                                                                   void *provctx) \
237*b077aed3SPierre Pronchery     { \
238*b077aed3SPierre Pronchery         return mac_settable_ctx_params(ctx, provctx, macname); \
239*b077aed3SPierre Pronchery     }
240*b077aed3SPierre Pronchery 
241*b077aed3SPierre Pronchery MAC_SETTABLE_CTX_PARAMS(hmac, "HMAC")
242*b077aed3SPierre Pronchery MAC_SETTABLE_CTX_PARAMS(siphash, "SIPHASH")
243*b077aed3SPierre Pronchery MAC_SETTABLE_CTX_PARAMS(poly1305, "POLY1305")
244*b077aed3SPierre Pronchery MAC_SETTABLE_CTX_PARAMS(cmac, "CMAC")
245*b077aed3SPierre Pronchery 
246*b077aed3SPierre Pronchery #define MAC_SIGNATURE_FUNCTIONS(funcname) \
247*b077aed3SPierre Pronchery     const OSSL_DISPATCH ossl_mac_legacy_##funcname##_signature_functions[] = { \
248*b077aed3SPierre Pronchery         { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))mac_##funcname##_newctx }, \
249*b077aed3SPierre Pronchery         { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, \
250*b077aed3SPierre Pronchery         (void (*)(void))mac_digest_sign_init }, \
251*b077aed3SPierre Pronchery         { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, \
252*b077aed3SPierre Pronchery         (void (*)(void))mac_digest_sign_update }, \
253*b077aed3SPierre Pronchery         { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, \
254*b077aed3SPierre Pronchery         (void (*)(void))mac_digest_sign_final }, \
255*b077aed3SPierre Pronchery         { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))mac_freectx }, \
256*b077aed3SPierre Pronchery         { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))mac_dupctx }, \
257*b077aed3SPierre Pronchery         { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, \
258*b077aed3SPierre Pronchery           (void (*)(void))mac_set_ctx_params }, \
259*b077aed3SPierre Pronchery         { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, \
260*b077aed3SPierre Pronchery           (void (*)(void))mac_##funcname##_settable_ctx_params }, \
261*b077aed3SPierre Pronchery         { 0, NULL } \
262*b077aed3SPierre Pronchery     };
263*b077aed3SPierre Pronchery 
264*b077aed3SPierre Pronchery MAC_SIGNATURE_FUNCTIONS(hmac)
265*b077aed3SPierre Pronchery MAC_SIGNATURE_FUNCTIONS(siphash)
266*b077aed3SPierre Pronchery MAC_SIGNATURE_FUNCTIONS(poly1305)
267*b077aed3SPierre Pronchery MAC_SIGNATURE_FUNCTIONS(cmac)
268