Enable PKEY MAC bridge signature algs to take ctx params
[openssl.git] / providers / implementations / signature / mac_legacy.c
1 /*
2  * Copyright 2019-2020 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 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
12
13 #include <openssl/crypto.h>
14 #include <openssl/evp.h>
15 #include <openssl/core_dispatch.h>
16 #include <openssl/core_names.h>
17 #include <openssl/params.h>
18 #include <openssl/err.h>
19 #include "prov/implementations.h"
20 #include "prov/provider_ctx.h"
21 #include "prov/macsignature.h"
22
23 static OSSL_FUNC_signature_newctx_fn mac_hmac_newctx;
24 static OSSL_FUNC_signature_newctx_fn mac_siphash_newctx;
25 static OSSL_FUNC_signature_newctx_fn mac_poly1305_newctx;
26 static OSSL_FUNC_signature_newctx_fn mac_cmac_newctx;
27 static OSSL_FUNC_signature_digest_sign_init_fn mac_digest_sign_init;
28 static OSSL_FUNC_signature_digest_sign_update_fn mac_digest_sign_update;
29 static OSSL_FUNC_signature_digest_sign_final_fn mac_digest_sign_final;
30 static OSSL_FUNC_signature_freectx_fn mac_freectx;
31 static OSSL_FUNC_signature_dupctx_fn mac_dupctx;
32 static OSSL_FUNC_signature_set_ctx_params_fn mac_set_ctx_params;
33 static OSSL_FUNC_signature_settable_ctx_params_fn mac_hmac_settable_ctx_params;
34 static OSSL_FUNC_signature_settable_ctx_params_fn mac_siphash_settable_ctx_params;
35 static OSSL_FUNC_signature_settable_ctx_params_fn mac_poly1305_settable_ctx_params;
36 static OSSL_FUNC_signature_settable_ctx_params_fn mac_cmac_settable_ctx_params;
37
38 typedef struct {
39     OPENSSL_CTX *libctx;
40     char *propq;
41     MAC_KEY *key;
42     EVP_MAC_CTX *macctx;
43 } PROV_MAC_CTX;
44
45 static void *mac_newctx(void *provctx, const char *propq, const char *macname)
46 {
47     PROV_MAC_CTX *pmacctx = OPENSSL_zalloc(sizeof(PROV_MAC_CTX));
48     EVP_MAC *mac = NULL;
49
50     if (pmacctx == NULL)
51         return NULL;
52
53     pmacctx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
54     if (propq != NULL && (pmacctx->propq = OPENSSL_strdup(propq)) == NULL) {
55         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
56         goto err;
57     }
58
59     mac = EVP_MAC_fetch(pmacctx->libctx, macname, propq);
60     if (mac == NULL)
61         goto err;
62
63     pmacctx->macctx = EVP_MAC_CTX_new(mac);
64     if (pmacctx->macctx == NULL)
65         goto err;
66
67     EVP_MAC_free(mac);
68
69     return pmacctx;
70
71  err:
72     OPENSSL_free(pmacctx);
73     EVP_MAC_free(mac);
74     return NULL;
75 }
76
77 #define MAC_NEWCTX(funcname, macname) \
78     static void *mac_##funcname##_newctx(void *provctx, const char *propq) \
79     { \
80         return mac_newctx(provctx, propq, macname); \
81     }
82
83 MAC_NEWCTX(hmac, "HMAC")
84 MAC_NEWCTX(siphash, "SIPHASH")
85 MAC_NEWCTX(poly1305, "POLY1305")
86 MAC_NEWCTX(cmac, "CMAC")
87
88 static int mac_digest_sign_init(void *vpmacctx, const char *mdname, void *vkey)
89 {
90     PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
91     const char *ciphername = NULL, *engine = NULL;
92
93     if (pmacctx == NULL || vkey == NULL || !mac_key_up_ref(vkey))
94         return 0;
95
96     mac_key_free(pmacctx->key);
97     pmacctx->key = vkey;
98
99     if (pmacctx->key->cipher.cipher != NULL)
100         ciphername = (char *)EVP_CIPHER_name(pmacctx->key->cipher.cipher);
101 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
102     if (pmacctx->key->cipher.engine != NULL)
103         engine = (char *)ENGINE_get_id(pmacctx->key->cipher.engine);
104 #endif
105
106     if (!ossl_prov_set_macctx(pmacctx->macctx, NULL,
107                               (char *)ciphername,
108                               (char *)mdname,
109                               (char *)engine,
110                               pmacctx->key->properties,
111                               pmacctx->key->priv_key,
112                               pmacctx->key->priv_key_len))
113         return 0;
114
115     if (!EVP_MAC_init(pmacctx->macctx))
116         return 0;
117
118     return 1;
119 }
120
121 int mac_digest_sign_update(void *vpmacctx, const unsigned char *data,
122                            size_t datalen)
123 {
124     PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
125
126     if (pmacctx == NULL || pmacctx->macctx == NULL)
127         return 0;
128
129     return EVP_MAC_update(pmacctx->macctx, data, datalen);
130 }
131
132 int mac_digest_sign_final(void *vpmacctx, unsigned char *mac, size_t *maclen,
133                           size_t macsize)
134 {
135     PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
136
137     if (pmacctx == NULL || pmacctx->macctx == NULL)
138         return 0;
139
140     return EVP_MAC_final(pmacctx->macctx, mac, maclen, macsize);
141 }
142
143 static void mac_freectx(void *vpmacctx)
144 {
145     PROV_MAC_CTX *ctx = (PROV_MAC_CTX *)vpmacctx;
146
147     OPENSSL_free(ctx->propq);
148     EVP_MAC_CTX_free(ctx->macctx);
149     mac_key_free(ctx->key);
150     OPENSSL_free(ctx);
151 }
152
153 static void *mac_dupctx(void *vpmacctx)
154 {
155     PROV_MAC_CTX *srcctx = (PROV_MAC_CTX *)vpmacctx;
156     PROV_MAC_CTX *dstctx;
157
158     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
159     if (dstctx == NULL)
160         return NULL;
161
162     *dstctx = *srcctx;
163     dstctx->key = NULL;
164     dstctx->macctx = NULL;
165
166     if (srcctx->key != NULL && !mac_key_up_ref(srcctx->key))
167         goto err;
168     dstctx->key = srcctx->key;
169
170     if (srcctx->macctx != NULL) {
171         dstctx->macctx = EVP_MAC_CTX_dup(srcctx->macctx);
172         if (dstctx->macctx == NULL)
173             goto err;
174     }
175
176     return dstctx;
177  err:
178     mac_freectx(dstctx);
179     return NULL;
180 }
181
182 static int mac_set_ctx_params(void *vpmacctx, const OSSL_PARAM params[])
183 {
184     PROV_MAC_CTX *ctx = (PROV_MAC_CTX *)vpmacctx;
185
186     return EVP_MAC_CTX_set_params(ctx->macctx, params);
187 }
188
189 static const OSSL_PARAM *mac_settable_ctx_params(void *provctx,
190                                                  const char *macname)
191 {
192     EVP_MAC *mac = EVP_MAC_fetch(PROV_LIBRARY_CONTEXT_OF(provctx), macname,
193                                  NULL);
194     const OSSL_PARAM *params;
195
196     if (mac == NULL)
197         return NULL;
198
199     params = EVP_MAC_settable_ctx_params(mac);
200     EVP_MAC_free(mac);
201
202     return params;
203 }
204
205 #define MAC_SETTABLE_CTX_PARAMS(funcname, macname) \
206     static const OSSL_PARAM *mac_##funcname##_settable_ctx_params(void *provctx) \
207     { \
208         return mac_settable_ctx_params(provctx, macname); \
209     }
210
211 MAC_SETTABLE_CTX_PARAMS(hmac, "HMAC")
212 MAC_SETTABLE_CTX_PARAMS(siphash, "SIPHASH")
213 MAC_SETTABLE_CTX_PARAMS(poly1305, "POLY1305")
214 MAC_SETTABLE_CTX_PARAMS(cmac, "CMAC")
215
216 #define MAC_SIGNATURE_FUNCTIONS(funcname) \
217     const OSSL_DISPATCH mac_legacy_##funcname##_signature_functions[] = { \
218         { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))mac_##funcname##_newctx }, \
219         { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, \
220         (void (*)(void))mac_digest_sign_init }, \
221         { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, \
222         (void (*)(void))mac_digest_sign_update }, \
223         { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, \
224         (void (*)(void))mac_digest_sign_final }, \
225         { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))mac_freectx }, \
226         { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))mac_dupctx }, \
227         { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, \
228           (void (*)(void))mac_set_ctx_params }, \
229         { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, \
230           (void (*)(void))mac_##funcname##_settable_ctx_params }, \
231         { 0, NULL } \
232     };
233
234 MAC_SIGNATURE_FUNCTIONS(hmac)
235 MAC_SIGNATURE_FUNCTIONS(siphash)
236 MAC_SIGNATURE_FUNCTIONS(poly1305)
237 MAC_SIGNATURE_FUNCTIONS(cmac)