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