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