]>
Commit | Line | Data |
---|---|---|
409910be MC |
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 | ||
2ef9a7ac MC |
10 | /* We need to use some engine deprecated APIs */ |
11 | #define OPENSSL_SUPPRESS_DEPRECATED | |
12 | ||
409910be MC |
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" | |
f590a5ea | 22 | #include "prov/providercommon.h" |
409910be MC |
23 | |
24 | static OSSL_FUNC_signature_newctx_fn mac_hmac_newctx; | |
f2713893 MC |
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; | |
409910be MC |
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; | |
f2713893 MC |
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; | |
409910be MC |
38 | |
39 | typedef struct { | |
40 | OPENSSL_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 | { | |
f590a5ea | 48 | PROV_MAC_CTX *pmacctx; |
409910be MC |
49 | EVP_MAC *mac = NULL; |
50 | ||
f590a5ea P |
51 | if (!ossl_prov_is_running()) |
52 | return NULL; | |
53 | ||
54 | pmacctx = OPENSSL_zalloc(sizeof(PROV_MAC_CTX)); | |
409910be MC |
55 | if (pmacctx == NULL) |
56 | return NULL; | |
57 | ||
58 | pmacctx->libctx = PROV_LIBRARY_CONTEXT_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); | |
78 | EVP_MAC_free(mac); | |
79 | return NULL; | |
80 | } | |
81 | ||
82 | #define MAC_NEWCTX(funcname, macname) \ | |
83 | static void *mac_##funcname##_newctx(void *provctx, const char *propq) \ | |
84 | { \ | |
85 | return mac_newctx(provctx, propq, macname); \ | |
86 | } | |
87 | ||
88 | MAC_NEWCTX(hmac, "HMAC") | |
b27b31b6 | 89 | MAC_NEWCTX(siphash, "SIPHASH") |
4db71d01 | 90 | MAC_NEWCTX(poly1305, "POLY1305") |
a540ef90 | 91 | MAC_NEWCTX(cmac, "CMAC") |
409910be MC |
92 | |
93 | static int mac_digest_sign_init(void *vpmacctx, const char *mdname, void *vkey) | |
94 | { | |
95 | PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx; | |
2ef9a7ac | 96 | const char *ciphername = NULL, *engine = NULL; |
409910be | 97 | |
f590a5ea P |
98 | if (!ossl_prov_is_running() |
99 | || pmacctx == NULL | |
100 | || vkey == NULL | |
101 | || !mac_key_up_ref(vkey)) | |
409910be MC |
102 | return 0; |
103 | ||
409910be MC |
104 | mac_key_free(pmacctx->key); |
105 | pmacctx->key = vkey; | |
106 | ||
2ef9a7ac MC |
107 | if (pmacctx->key->cipher.cipher != NULL) |
108 | ciphername = (char *)EVP_CIPHER_name(pmacctx->key->cipher.cipher); | |
109 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) | |
110 | if (pmacctx->key->cipher.engine != NULL) | |
111 | engine = (char *)ENGINE_get_id(pmacctx->key->cipher.engine); | |
112 | #endif | |
113 | ||
114 | if (!ossl_prov_set_macctx(pmacctx->macctx, NULL, | |
115 | (char *)ciphername, | |
116 | (char *)mdname, | |
117 | (char *)engine, | |
118 | pmacctx->key->properties, | |
119 | pmacctx->key->priv_key, | |
120 | pmacctx->key->priv_key_len)) | |
409910be MC |
121 | return 0; |
122 | ||
123 | if (!EVP_MAC_init(pmacctx->macctx)) | |
124 | return 0; | |
125 | ||
126 | return 1; | |
127 | } | |
128 | ||
129 | int mac_digest_sign_update(void *vpmacctx, const unsigned char *data, | |
130 | size_t datalen) | |
131 | { | |
132 | PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx; | |
133 | ||
134 | if (pmacctx == NULL || pmacctx->macctx == NULL) | |
135 | return 0; | |
136 | ||
137 | return EVP_MAC_update(pmacctx->macctx, data, datalen); | |
138 | } | |
139 | ||
140 | int mac_digest_sign_final(void *vpmacctx, unsigned char *mac, size_t *maclen, | |
141 | size_t macsize) | |
142 | { | |
143 | PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx; | |
144 | ||
f590a5ea | 145 | if (!ossl_prov_is_running() || pmacctx == NULL || pmacctx->macctx == NULL) |
409910be MC |
146 | return 0; |
147 | ||
148 | return EVP_MAC_final(pmacctx->macctx, mac, maclen, macsize); | |
149 | } | |
150 | ||
151 | static void mac_freectx(void *vpmacctx) | |
152 | { | |
153 | PROV_MAC_CTX *ctx = (PROV_MAC_CTX *)vpmacctx; | |
154 | ||
155 | OPENSSL_free(ctx->propq); | |
156 | EVP_MAC_CTX_free(ctx->macctx); | |
157 | mac_key_free(ctx->key); | |
158 | OPENSSL_free(ctx); | |
159 | } | |
160 | ||
161 | static void *mac_dupctx(void *vpmacctx) | |
162 | { | |
163 | PROV_MAC_CTX *srcctx = (PROV_MAC_CTX *)vpmacctx; | |
164 | PROV_MAC_CTX *dstctx; | |
165 | ||
f590a5ea P |
166 | if (!ossl_prov_is_running()) |
167 | return NULL; | |
168 | ||
409910be MC |
169 | dstctx = OPENSSL_zalloc(sizeof(*srcctx)); |
170 | if (dstctx == NULL) | |
171 | return NULL; | |
172 | ||
173 | *dstctx = *srcctx; | |
174 | dstctx->key = NULL; | |
175 | dstctx->macctx = NULL; | |
176 | ||
177 | if (srcctx->key != NULL && !mac_key_up_ref(srcctx->key)) | |
178 | goto err; | |
179 | dstctx->key = srcctx->key; | |
180 | ||
181 | if (srcctx->macctx != NULL) { | |
182 | dstctx->macctx = EVP_MAC_CTX_dup(srcctx->macctx); | |
183 | if (dstctx->macctx == NULL) | |
184 | goto err; | |
185 | } | |
186 | ||
187 | return dstctx; | |
188 | err: | |
189 | mac_freectx(dstctx); | |
190 | return NULL; | |
191 | } | |
192 | ||
f2713893 MC |
193 | static int mac_set_ctx_params(void *vpmacctx, const OSSL_PARAM params[]) |
194 | { | |
195 | PROV_MAC_CTX *ctx = (PROV_MAC_CTX *)vpmacctx; | |
196 | ||
197 | return EVP_MAC_CTX_set_params(ctx->macctx, params); | |
198 | } | |
199 | ||
200 | static const OSSL_PARAM *mac_settable_ctx_params(void *provctx, | |
201 | const char *macname) | |
202 | { | |
203 | EVP_MAC *mac = EVP_MAC_fetch(PROV_LIBRARY_CONTEXT_OF(provctx), macname, | |
204 | NULL); | |
205 | const OSSL_PARAM *params; | |
206 | ||
207 | if (mac == NULL) | |
208 | return NULL; | |
209 | ||
210 | params = EVP_MAC_settable_ctx_params(mac); | |
211 | EVP_MAC_free(mac); | |
212 | ||
213 | return params; | |
214 | } | |
215 | ||
216 | #define MAC_SETTABLE_CTX_PARAMS(funcname, macname) \ | |
217 | static const OSSL_PARAM *mac_##funcname##_settable_ctx_params(void *provctx) \ | |
218 | { \ | |
219 | return mac_settable_ctx_params(provctx, macname); \ | |
220 | } | |
221 | ||
222 | MAC_SETTABLE_CTX_PARAMS(hmac, "HMAC") | |
223 | MAC_SETTABLE_CTX_PARAMS(siphash, "SIPHASH") | |
224 | MAC_SETTABLE_CTX_PARAMS(poly1305, "POLY1305") | |
225 | MAC_SETTABLE_CTX_PARAMS(cmac, "CMAC") | |
226 | ||
409910be | 227 | #define MAC_SIGNATURE_FUNCTIONS(funcname) \ |
1be63951 | 228 | const OSSL_DISPATCH ossl_mac_legacy_##funcname##_signature_functions[] = { \ |
409910be MC |
229 | { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))mac_##funcname##_newctx }, \ |
230 | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, \ | |
231 | (void (*)(void))mac_digest_sign_init }, \ | |
232 | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, \ | |
233 | (void (*)(void))mac_digest_sign_update }, \ | |
234 | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, \ | |
235 | (void (*)(void))mac_digest_sign_final }, \ | |
236 | { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))mac_freectx }, \ | |
237 | { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))mac_dupctx }, \ | |
f2713893 MC |
238 | { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, \ |
239 | (void (*)(void))mac_set_ctx_params }, \ | |
240 | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, \ | |
241 | (void (*)(void))mac_##funcname##_settable_ctx_params }, \ | |
409910be MC |
242 | { 0, NULL } \ |
243 | }; | |
244 | ||
b27b31b6 MC |
245 | MAC_SIGNATURE_FUNCTIONS(hmac) |
246 | MAC_SIGNATURE_FUNCTIONS(siphash) | |
4db71d01 | 247 | MAC_SIGNATURE_FUNCTIONS(poly1305) |
a540ef90 | 248 | MAC_SIGNATURE_FUNCTIONS(cmac) |