/src/openssl/providers/implementations/signature/dsa_sig.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2019-2025 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 | | /* |
11 | | * DSA low level APIs are deprecated for public use, but still ok for |
12 | | * internal use. |
13 | | */ |
14 | | #include "internal/deprecated.h" |
15 | | |
16 | | #include <string.h> |
17 | | |
18 | | #include <openssl/crypto.h> |
19 | | #include <openssl/core_dispatch.h> |
20 | | #include <openssl/core_names.h> |
21 | | #include <openssl/err.h> |
22 | | #include <openssl/dsa.h> |
23 | | #include <openssl/params.h> |
24 | | #include <openssl/evp.h> |
25 | | #include <openssl/proverr.h> |
26 | | #include "internal/fips.h" |
27 | | #include "internal/nelem.h" |
28 | | #include "internal/sizes.h" |
29 | | #include "internal/cryptlib.h" |
30 | | #include "prov/providercommon.h" |
31 | | #include "prov/implementations.h" |
32 | | #include "prov/provider_ctx.h" |
33 | | #include "prov/securitycheck.h" |
34 | | #include "prov/der_dsa.h" |
35 | | #include "crypto/dsa.h" |
36 | | |
37 | | static OSSL_FUNC_signature_newctx_fn dsa_newctx; |
38 | | static OSSL_FUNC_signature_sign_init_fn dsa_sign_init; |
39 | | static OSSL_FUNC_signature_verify_init_fn dsa_verify_init; |
40 | | static OSSL_FUNC_signature_sign_fn dsa_sign; |
41 | | static OSSL_FUNC_signature_sign_message_update_fn dsa_signverify_message_update; |
42 | | static OSSL_FUNC_signature_sign_message_final_fn dsa_sign_message_final; |
43 | | static OSSL_FUNC_signature_verify_fn dsa_verify; |
44 | | static OSSL_FUNC_signature_verify_message_update_fn dsa_signverify_message_update; |
45 | | static OSSL_FUNC_signature_verify_message_final_fn dsa_verify_message_final; |
46 | | static OSSL_FUNC_signature_digest_sign_init_fn dsa_digest_sign_init; |
47 | | static OSSL_FUNC_signature_digest_sign_update_fn dsa_digest_signverify_update; |
48 | | static OSSL_FUNC_signature_digest_sign_final_fn dsa_digest_sign_final; |
49 | | static OSSL_FUNC_signature_digest_verify_init_fn dsa_digest_verify_init; |
50 | | static OSSL_FUNC_signature_digest_verify_update_fn dsa_digest_signverify_update; |
51 | | static OSSL_FUNC_signature_digest_verify_final_fn dsa_digest_verify_final; |
52 | | static OSSL_FUNC_signature_freectx_fn dsa_freectx; |
53 | | static OSSL_FUNC_signature_dupctx_fn dsa_dupctx; |
54 | | static OSSL_FUNC_signature_query_key_types_fn dsa_sigalg_query_key_types; |
55 | | static OSSL_FUNC_signature_get_ctx_params_fn dsa_get_ctx_params; |
56 | | static OSSL_FUNC_signature_gettable_ctx_params_fn dsa_gettable_ctx_params; |
57 | | static OSSL_FUNC_signature_set_ctx_params_fn dsa_set_ctx_params; |
58 | | static OSSL_FUNC_signature_settable_ctx_params_fn dsa_settable_ctx_params; |
59 | | static OSSL_FUNC_signature_get_ctx_md_params_fn dsa_get_ctx_md_params; |
60 | | static OSSL_FUNC_signature_gettable_ctx_md_params_fn dsa_gettable_ctx_md_params; |
61 | | static OSSL_FUNC_signature_set_ctx_md_params_fn dsa_set_ctx_md_params; |
62 | | static OSSL_FUNC_signature_settable_ctx_md_params_fn dsa_settable_ctx_md_params; |
63 | | static OSSL_FUNC_signature_set_ctx_params_fn dsa_sigalg_set_ctx_params; |
64 | | static OSSL_FUNC_signature_settable_ctx_params_fn dsa_sigalg_settable_ctx_params; |
65 | | |
66 | | /* |
67 | | * What's passed as an actual key is defined by the KEYMGMT interface. |
68 | | * We happen to know that our KEYMGMT simply passes DSA structures, so |
69 | | * we use that here too. |
70 | | */ |
71 | | |
72 | | typedef struct { |
73 | | OSSL_LIB_CTX *libctx; |
74 | | char *propq; |
75 | | DSA *dsa; |
76 | | /* |operation| reuses EVP's operation bitfield */ |
77 | | int operation; |
78 | | |
79 | | /* |
80 | | * Flag to determine if a full sigalg is run (1) or if a composable |
81 | | * signature algorithm is run (0). |
82 | | * |
83 | | * When a full sigalg is run (1), this currently affects the following |
84 | | * other flags, which are to remain untouched after their initialization: |
85 | | * |
86 | | * - flag_allow_md (initialized to 0) |
87 | | */ |
88 | | unsigned int flag_sigalg : 1; |
89 | | /* |
90 | | * Flag to determine if the hash function can be changed (1) or not (0) |
91 | | * Because it's dangerous to change during a DigestSign or DigestVerify |
92 | | * operation, this flag is cleared by their Init function, and set again |
93 | | * by their Final function. |
94 | | */ |
95 | | unsigned int flag_allow_md : 1; |
96 | | |
97 | | /* If this is set to 1 then the generated k is not random */ |
98 | | unsigned int nonce_type; |
99 | | |
100 | | /* The Algorithm Identifier of the combined signature algorithm */ |
101 | | unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE]; |
102 | | size_t aid_len; |
103 | | |
104 | | /* main digest */ |
105 | | char mdname[OSSL_MAX_NAME_SIZE]; |
106 | | EVP_MD *md; |
107 | | EVP_MD_CTX *mdctx; |
108 | | |
109 | | /* Signature, for verification */ |
110 | | unsigned char *sig; |
111 | | size_t siglen; |
112 | | |
113 | | OSSL_FIPS_IND_DECLARE |
114 | | } PROV_DSA_CTX; |
115 | | |
116 | | static size_t dsa_get_md_size(const PROV_DSA_CTX *pdsactx) |
117 | 0 | { |
118 | 0 | int md_size; |
119 | |
|
120 | 0 | if (pdsactx->md != NULL) { |
121 | 0 | md_size = EVP_MD_get_size(pdsactx->md); |
122 | 0 | if (md_size <= 0) |
123 | 0 | return 0; |
124 | 0 | return (size_t)md_size; |
125 | 0 | } |
126 | 0 | return 0; |
127 | 0 | } |
128 | | |
129 | | static void *dsa_newctx(void *provctx, const char *propq) |
130 | 0 | { |
131 | 0 | PROV_DSA_CTX *pdsactx; |
132 | |
|
133 | 0 | if (!ossl_prov_is_running()) |
134 | 0 | return NULL; |
135 | | |
136 | | #ifdef FIPS_MODULE |
137 | | if (!ossl_deferred_self_test(PROV_LIBCTX_OF(provctx), |
138 | | ST_ID_SIG_DSA_SHA256)) |
139 | | return NULL; |
140 | | #endif |
141 | | |
142 | 0 | pdsactx = OPENSSL_zalloc(sizeof(PROV_DSA_CTX)); |
143 | 0 | if (pdsactx == NULL) |
144 | 0 | return NULL; |
145 | | |
146 | 0 | pdsactx->libctx = PROV_LIBCTX_OF(provctx); |
147 | 0 | pdsactx->flag_allow_md = 1; |
148 | 0 | OSSL_FIPS_IND_INIT(pdsactx) |
149 | 0 | if (propq != NULL && (pdsactx->propq = OPENSSL_strdup(propq)) == NULL) { |
150 | 0 | OPENSSL_free(pdsactx); |
151 | 0 | pdsactx = NULL; |
152 | 0 | } |
153 | 0 | return pdsactx; |
154 | 0 | } |
155 | | |
156 | | static int dsa_setup_md(PROV_DSA_CTX *ctx, |
157 | | const char *mdname, const char *mdprops, |
158 | | const char *desc) |
159 | 0 | { |
160 | 0 | EVP_MD *md = NULL; |
161 | |
|
162 | 0 | if (mdprops == NULL) |
163 | 0 | mdprops = ctx->propq; |
164 | |
|
165 | 0 | if (mdname != NULL) { |
166 | 0 | WPACKET pkt; |
167 | 0 | int md_nid; |
168 | 0 | size_t mdname_len = strlen(mdname); |
169 | 0 | unsigned char *aid = NULL; |
170 | |
|
171 | 0 | md = EVP_MD_fetch(ctx->libctx, mdname, mdprops); |
172 | 0 | md_nid = ossl_digest_get_approved_nid(md); |
173 | |
|
174 | 0 | if (md == NULL) { |
175 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, |
176 | 0 | "%s could not be fetched", mdname); |
177 | 0 | goto err; |
178 | 0 | } |
179 | 0 | if (md_nid == NID_undef) { |
180 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED, |
181 | 0 | "digest=%s", mdname); |
182 | 0 | goto err; |
183 | 0 | } |
184 | 0 | if (mdname_len >= sizeof(ctx->mdname)) { |
185 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, |
186 | 0 | "%s exceeds name buffer length", mdname); |
187 | 0 | goto err; |
188 | 0 | } |
189 | | /* XOF digests don't work */ |
190 | 0 | if (EVP_MD_xof(md)) { |
191 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); |
192 | 0 | goto err; |
193 | 0 | } |
194 | | #ifdef FIPS_MODULE |
195 | | { |
196 | | int sha1_allowed |
197 | | = ((ctx->operation |
198 | | & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_SIGNMSG)) |
199 | | == 0); |
200 | | |
201 | | if (!ossl_fips_ind_digest_sign_check(OSSL_FIPS_IND_GET(ctx), |
202 | | OSSL_FIPS_IND_SETTABLE1, |
203 | | ctx->libctx, |
204 | | md_nid, sha1_allowed, 0, desc, |
205 | | ossl_fips_config_signature_digest_check)) |
206 | | goto err; |
207 | | } |
208 | | #endif |
209 | | |
210 | 0 | if (!ctx->flag_allow_md) { |
211 | 0 | if (ctx->mdname[0] != '\0' |
212 | 0 | && !EVP_MD_is_a(md, ctx->mdname)) { |
213 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED, |
214 | 0 | "digest %s != %s", mdname, ctx->mdname); |
215 | 0 | goto err; |
216 | 0 | } |
217 | 0 | EVP_MD_free(md); |
218 | 0 | return 1; |
219 | 0 | } |
220 | | |
221 | 0 | EVP_MD_CTX_free(ctx->mdctx); |
222 | 0 | EVP_MD_free(ctx->md); |
223 | | |
224 | | /* |
225 | | * We do not care about DER writing errors. |
226 | | * All it really means is that for some reason, there's no |
227 | | * AlgorithmIdentifier to be had, but the operation itself is |
228 | | * still valid, just as long as it's not used to construct |
229 | | * anything that needs an AlgorithmIdentifier. |
230 | | */ |
231 | 0 | ctx->aid_len = 0; |
232 | 0 | if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf)) |
233 | 0 | && ossl_DER_w_algorithmIdentifier_DSA_with_MD(&pkt, -1, ctx->dsa, |
234 | 0 | md_nid) |
235 | 0 | && WPACKET_finish(&pkt)) { |
236 | 0 | WPACKET_get_total_written(&pkt, &ctx->aid_len); |
237 | 0 | aid = WPACKET_get_curr(&pkt); |
238 | 0 | } |
239 | 0 | WPACKET_cleanup(&pkt); |
240 | 0 | if (aid != NULL && ctx->aid_len != 0) |
241 | 0 | memmove(ctx->aid_buf, aid, ctx->aid_len); |
242 | |
|
243 | 0 | ctx->mdctx = NULL; |
244 | 0 | ctx->md = md; |
245 | 0 | OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname)); |
246 | 0 | } |
247 | | |
248 | 0 | return 1; |
249 | 0 | err: |
250 | 0 | EVP_MD_free(md); |
251 | 0 | return 0; |
252 | 0 | } |
253 | | |
254 | | #ifdef FIPS_MODULE |
255 | | |
256 | | static int dsa_sign_check_approved(PROV_DSA_CTX *ctx, int signing, |
257 | | const char *desc) |
258 | | { |
259 | | /* DSA Signing is not approved in FIPS 140-3 */ |
260 | | if (signing |
261 | | && !OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE2, |
262 | | ctx->libctx, desc, "DSA", |
263 | | ossl_fips_config_dsa_sign_disallowed)) |
264 | | return 0; |
265 | | return 1; |
266 | | } |
267 | | |
268 | | static int dsa_check_key(PROV_DSA_CTX *ctx, int sign, const char *desc) |
269 | | { |
270 | | int approved = ossl_dsa_check_key(ctx->dsa, sign); |
271 | | |
272 | | if (!approved) { |
273 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0, |
274 | | ctx->libctx, desc, "DSA Key", |
275 | | ossl_fips_config_signature_digest_check)) { |
276 | | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
277 | | return 0; |
278 | | } |
279 | | } |
280 | | return 1; |
281 | | } |
282 | | #endif |
283 | | |
284 | | static int |
285 | | dsa_signverify_init(void *vpdsactx, void *vdsa, |
286 | | OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params, |
287 | | const OSSL_PARAM params[], int operation, |
288 | | const char *desc) |
289 | 0 | { |
290 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
291 | |
|
292 | 0 | if (!ossl_prov_is_running() |
293 | 0 | || pdsactx == NULL) |
294 | 0 | return 0; |
295 | | |
296 | 0 | if (vdsa == NULL && pdsactx->dsa == NULL) { |
297 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
298 | 0 | return 0; |
299 | 0 | } |
300 | | |
301 | 0 | if (vdsa != NULL) { |
302 | 0 | if (!DSA_up_ref(vdsa)) |
303 | 0 | return 0; |
304 | 0 | DSA_free(pdsactx->dsa); |
305 | 0 | pdsactx->dsa = vdsa; |
306 | 0 | } |
307 | | |
308 | 0 | pdsactx->operation = operation; |
309 | |
|
310 | 0 | OSSL_FIPS_IND_SET_APPROVED(pdsactx) |
311 | 0 | if (!set_ctx_params(pdsactx, params)) |
312 | 0 | return 0; |
313 | | #ifdef FIPS_MODULE |
314 | | { |
315 | | int operation_is_sign |
316 | | = (operation & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_SIGNMSG)) != 0; |
317 | | |
318 | | if (!dsa_sign_check_approved(pdsactx, operation_is_sign, desc)) |
319 | | return 0; |
320 | | if (!dsa_check_key(pdsactx, operation_is_sign, desc)) |
321 | | return 0; |
322 | | } |
323 | | #endif |
324 | 0 | return 1; |
325 | 0 | } |
326 | | |
327 | | static int dsa_sign_init(void *vpdsactx, void *vdsa, const OSSL_PARAM params[]) |
328 | 0 | { |
329 | 0 | return dsa_signverify_init(vpdsactx, vdsa, dsa_set_ctx_params, params, |
330 | 0 | EVP_PKEY_OP_SIGN, "DSA Sign Init"); |
331 | 0 | } |
332 | | |
333 | | /* |
334 | | * Sign tbs without digesting it first. This is suitable for "primitive" |
335 | | * signing and signing the digest of a message, i.e. should be used with |
336 | | * implementations of the keytype related algorithms. |
337 | | */ |
338 | | static int dsa_sign_directly(void *vpdsactx, |
339 | | unsigned char *sig, size_t *siglen, size_t sigsize, |
340 | | const unsigned char *tbs, size_t tbslen) |
341 | 0 | { |
342 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
343 | 0 | int ret; |
344 | 0 | unsigned int sltmp; |
345 | 0 | size_t dsasize = DSA_size(pdsactx->dsa); |
346 | 0 | size_t mdsize = dsa_get_md_size(pdsactx); |
347 | |
|
348 | 0 | if (!ossl_prov_is_running()) |
349 | 0 | return 0; |
350 | | |
351 | | #ifdef FIPS_MODULE |
352 | | if (!dsa_sign_check_approved(pdsactx, 1, "Sign")) |
353 | | return 0; |
354 | | #endif |
355 | | |
356 | 0 | if (sig == NULL) { |
357 | 0 | *siglen = dsasize; |
358 | 0 | return 1; |
359 | 0 | } |
360 | | |
361 | 0 | if (sigsize < dsasize) |
362 | 0 | return 0; |
363 | | |
364 | 0 | if (mdsize != 0 && tbslen != mdsize) |
365 | 0 | return 0; |
366 | | |
367 | 0 | ret = ossl_dsa_sign_int(0, tbs, (int)tbslen, sig, &sltmp, pdsactx->dsa, |
368 | 0 | pdsactx->nonce_type, pdsactx->mdname, |
369 | 0 | pdsactx->libctx, pdsactx->propq); |
370 | 0 | if (ret <= 0) |
371 | 0 | return 0; |
372 | | |
373 | 0 | *siglen = sltmp; |
374 | 0 | return 1; |
375 | 0 | } |
376 | | |
377 | | static int dsa_signverify_message_update(void *vpdsactx, |
378 | | const unsigned char *data, |
379 | | size_t datalen) |
380 | 0 | { |
381 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
382 | |
|
383 | 0 | if (pdsactx == NULL) |
384 | 0 | return 0; |
385 | | |
386 | 0 | return EVP_DigestUpdate(pdsactx->mdctx, data, datalen); |
387 | 0 | } |
388 | | |
389 | | static int dsa_sign_message_final(void *vpdsactx, unsigned char *sig, |
390 | | size_t *siglen, size_t sigsize) |
391 | 0 | { |
392 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
393 | 0 | unsigned char digest[EVP_MAX_MD_SIZE]; |
394 | 0 | unsigned int dlen = 0; |
395 | |
|
396 | 0 | if (!ossl_prov_is_running() || pdsactx == NULL || pdsactx->mdctx == NULL) |
397 | 0 | return 0; |
398 | | /* |
399 | | * If sig is NULL then we're just finding out the sig size. Other fields |
400 | | * are ignored. Defer to dsa_sign. |
401 | | */ |
402 | 0 | if (sig != NULL) { |
403 | | /* |
404 | | * When this function is used through dsa_digest_sign_final(), |
405 | | * there is the possibility that some externally provided digests |
406 | | * exceed EVP_MAX_MD_SIZE. We should probably handle that |
407 | | * somehow but that problem is much larger than just in DSA. |
408 | | */ |
409 | 0 | if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen)) |
410 | 0 | return 0; |
411 | 0 | } |
412 | | |
413 | 0 | return dsa_sign_directly(vpdsactx, sig, siglen, sigsize, digest, dlen); |
414 | 0 | } |
415 | | |
416 | | /* |
417 | | * If signing a message, digest tbs and sign the result. |
418 | | * Otherwise, sign tbs directly. |
419 | | */ |
420 | | static int dsa_sign(void *vpdsactx, unsigned char *sig, size_t *siglen, |
421 | | size_t sigsize, const unsigned char *tbs, size_t tbslen) |
422 | 0 | { |
423 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
424 | |
|
425 | 0 | if (pdsactx->operation == EVP_PKEY_OP_SIGNMSG) { |
426 | | /* |
427 | | * If |sig| is NULL, the caller is only looking for the sig length. |
428 | | * DO NOT update the input in this case. |
429 | | */ |
430 | 0 | if (sig == NULL) |
431 | 0 | return dsa_sign_message_final(pdsactx, sig, siglen, sigsize); |
432 | | |
433 | 0 | if (dsa_signverify_message_update(pdsactx, tbs, tbslen) <= 0) |
434 | 0 | return 0; |
435 | 0 | return dsa_sign_message_final(pdsactx, sig, siglen, sigsize); |
436 | 0 | } |
437 | 0 | return dsa_sign_directly(pdsactx, sig, siglen, sigsize, tbs, tbslen); |
438 | 0 | } |
439 | | |
440 | | static int dsa_verify_init(void *vpdsactx, void *vdsa, |
441 | | const OSSL_PARAM params[]) |
442 | 0 | { |
443 | 0 | return dsa_signverify_init(vpdsactx, vdsa, dsa_set_ctx_params, params, |
444 | 0 | EVP_PKEY_OP_VERIFY, "DSA Verify Init"); |
445 | 0 | } |
446 | | |
447 | | static int dsa_verify_directly(void *vpdsactx, |
448 | | const unsigned char *sig, size_t siglen, |
449 | | const unsigned char *tbs, size_t tbslen) |
450 | 0 | { |
451 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
452 | 0 | size_t mdsize = dsa_get_md_size(pdsactx); |
453 | |
|
454 | 0 | if (!ossl_prov_is_running() || (mdsize != 0 && tbslen != mdsize)) |
455 | 0 | return 0; |
456 | | |
457 | 0 | return DSA_verify(0, tbs, (int)tbslen, sig, (int)siglen, pdsactx->dsa); |
458 | 0 | } |
459 | | |
460 | | static int dsa_verify_set_sig(void *vpdsactx, |
461 | | const unsigned char *sig, size_t siglen) |
462 | 0 | { |
463 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
464 | 0 | OSSL_PARAM params[2]; |
465 | |
|
466 | 0 | params[0] = OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_SIGNATURE, |
467 | 0 | (unsigned char *)sig, siglen); |
468 | 0 | params[1] = OSSL_PARAM_construct_end(); |
469 | 0 | return dsa_sigalg_set_ctx_params(pdsactx, params); |
470 | 0 | } |
471 | | |
472 | | static int dsa_verify_message_final(void *vpdsactx) |
473 | 0 | { |
474 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
475 | 0 | unsigned char digest[EVP_MAX_MD_SIZE]; |
476 | 0 | unsigned int dlen = 0; |
477 | |
|
478 | 0 | if (!ossl_prov_is_running()) |
479 | 0 | return 0; |
480 | | |
481 | 0 | if (pdsactx == NULL || pdsactx->mdctx == NULL) |
482 | 0 | return 0; |
483 | | |
484 | | /* |
485 | | * The digests used here are all known (see dsa_get_md_nid()), so they |
486 | | * should not exceed the internal buffer size of EVP_MAX_MD_SIZE. |
487 | | */ |
488 | 0 | if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen)) |
489 | 0 | return 0; |
490 | | |
491 | 0 | return dsa_verify_directly(vpdsactx, pdsactx->sig, pdsactx->siglen, |
492 | 0 | digest, dlen); |
493 | 0 | } |
494 | | |
495 | | /* |
496 | | * If verifying a message, digest tbs and verify the result. |
497 | | * Otherwise, verify tbs directly. |
498 | | */ |
499 | | static int dsa_verify(void *vpdsactx, |
500 | | const unsigned char *sig, size_t siglen, |
501 | | const unsigned char *tbs, size_t tbslen) |
502 | 0 | { |
503 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
504 | |
|
505 | 0 | if (pdsactx->operation == EVP_PKEY_OP_VERIFYMSG) { |
506 | 0 | if (dsa_verify_set_sig(pdsactx, sig, siglen) <= 0) |
507 | 0 | return 0; |
508 | 0 | if (dsa_signverify_message_update(pdsactx, tbs, tbslen) <= 0) |
509 | 0 | return 0; |
510 | 0 | return dsa_verify_message_final(pdsactx); |
511 | 0 | } |
512 | 0 | return dsa_verify_directly(pdsactx, sig, siglen, tbs, tbslen); |
513 | 0 | } |
514 | | |
515 | | /* DigestSign/DigestVerify wrappers */ |
516 | | |
517 | | static int dsa_digest_signverify_init(void *vpdsactx, const char *mdname, |
518 | | void *vdsa, const OSSL_PARAM params[], |
519 | | int operation, const char *desc) |
520 | 0 | { |
521 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
522 | |
|
523 | 0 | if (!ossl_prov_is_running()) |
524 | 0 | return 0; |
525 | | |
526 | 0 | if (!dsa_signverify_init(vpdsactx, vdsa, dsa_set_ctx_params, params, |
527 | 0 | operation, desc)) |
528 | 0 | return 0; |
529 | | |
530 | 0 | if (mdname != NULL |
531 | | /* was dsa_setup_md already called in dsa_signverify_init()? */ |
532 | 0 | && (mdname[0] == '\0' || OPENSSL_strcasecmp(pdsactx->mdname, mdname) != 0) |
533 | 0 | && !dsa_setup_md(pdsactx, mdname, NULL, desc)) |
534 | 0 | return 0; |
535 | | |
536 | 0 | pdsactx->flag_allow_md = 0; |
537 | |
|
538 | 0 | if (pdsactx->mdctx == NULL) { |
539 | 0 | pdsactx->mdctx = EVP_MD_CTX_new(); |
540 | 0 | if (pdsactx->mdctx == NULL) |
541 | 0 | goto error; |
542 | 0 | } |
543 | | |
544 | 0 | if (!EVP_DigestInit_ex2(pdsactx->mdctx, pdsactx->md, params)) |
545 | 0 | goto error; |
546 | | |
547 | 0 | return 1; |
548 | | |
549 | 0 | error: |
550 | 0 | EVP_MD_CTX_free(pdsactx->mdctx); |
551 | 0 | pdsactx->mdctx = NULL; |
552 | 0 | return 0; |
553 | 0 | } |
554 | | |
555 | | static int dsa_digest_sign_init(void *vpdsactx, const char *mdname, |
556 | | void *vdsa, const OSSL_PARAM params[]) |
557 | 0 | { |
558 | 0 | return dsa_digest_signverify_init(vpdsactx, mdname, vdsa, params, |
559 | 0 | EVP_PKEY_OP_SIGNMSG, |
560 | 0 | "DSA Digest Sign Init"); |
561 | 0 | } |
562 | | |
563 | | static int dsa_digest_signverify_update(void *vpdsactx, const unsigned char *data, |
564 | | size_t datalen) |
565 | 0 | { |
566 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
567 | |
|
568 | 0 | if (pdsactx == NULL) |
569 | 0 | return 0; |
570 | | /* Sigalg implementations shouldn't do digest_sign */ |
571 | 0 | if (pdsactx->flag_sigalg) |
572 | 0 | return 0; |
573 | | |
574 | 0 | return dsa_signverify_message_update(vpdsactx, data, datalen); |
575 | 0 | } |
576 | | |
577 | | static int dsa_digest_sign_final(void *vpdsactx, unsigned char *sig, |
578 | | size_t *siglen, size_t sigsize) |
579 | 0 | { |
580 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
581 | 0 | int ok = 0; |
582 | |
|
583 | 0 | if (pdsactx == NULL) |
584 | 0 | return 0; |
585 | | /* Sigalg implementations shouldn't do digest_sign */ |
586 | 0 | if (pdsactx->flag_sigalg) |
587 | 0 | return 0; |
588 | | |
589 | 0 | ok = dsa_sign_message_final(pdsactx, sig, siglen, sigsize); |
590 | |
|
591 | 0 | pdsactx->flag_allow_md = 1; |
592 | |
|
593 | 0 | return ok; |
594 | 0 | } |
595 | | |
596 | | static int dsa_digest_verify_init(void *vpdsactx, const char *mdname, |
597 | | void *vdsa, const OSSL_PARAM params[]) |
598 | 0 | { |
599 | 0 | return dsa_digest_signverify_init(vpdsactx, mdname, vdsa, params, |
600 | 0 | EVP_PKEY_OP_VERIFYMSG, |
601 | 0 | "DSA Digest Verify Init"); |
602 | 0 | } |
603 | | |
604 | | int dsa_digest_verify_final(void *vpdsactx, const unsigned char *sig, |
605 | | size_t siglen) |
606 | 0 | { |
607 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
608 | 0 | int ok = 0; |
609 | |
|
610 | 0 | if (pdsactx == NULL) |
611 | 0 | return 0; |
612 | | /* Sigalg implementations shouldn't do digest_verify */ |
613 | 0 | if (pdsactx->flag_sigalg) |
614 | 0 | return 0; |
615 | | |
616 | 0 | if (dsa_verify_set_sig(pdsactx, sig, siglen)) |
617 | 0 | ok = dsa_verify_message_final(vpdsactx); |
618 | |
|
619 | 0 | pdsactx->flag_allow_md = 1; |
620 | |
|
621 | 0 | return ok; |
622 | 0 | } |
623 | | |
624 | | static void dsa_freectx(void *vpdsactx) |
625 | 0 | { |
626 | 0 | PROV_DSA_CTX *ctx = (PROV_DSA_CTX *)vpdsactx; |
627 | |
|
628 | 0 | EVP_MD_CTX_free(ctx->mdctx); |
629 | 0 | EVP_MD_free(ctx->md); |
630 | 0 | OPENSSL_free(ctx->sig); |
631 | 0 | OPENSSL_free(ctx->propq); |
632 | 0 | DSA_free(ctx->dsa); |
633 | 0 | OPENSSL_free(ctx); |
634 | 0 | } |
635 | | |
636 | | static void *dsa_dupctx(void *vpdsactx) |
637 | 0 | { |
638 | 0 | PROV_DSA_CTX *srcctx = (PROV_DSA_CTX *)vpdsactx; |
639 | 0 | PROV_DSA_CTX *dstctx; |
640 | |
|
641 | 0 | if (!ossl_prov_is_running()) |
642 | 0 | return NULL; |
643 | | |
644 | 0 | dstctx = OPENSSL_zalloc(sizeof(*srcctx)); |
645 | 0 | if (dstctx == NULL) |
646 | 0 | return NULL; |
647 | | |
648 | 0 | *dstctx = *srcctx; |
649 | 0 | dstctx->dsa = NULL; |
650 | 0 | dstctx->propq = NULL; |
651 | |
|
652 | 0 | if (srcctx->dsa != NULL && !DSA_up_ref(srcctx->dsa)) |
653 | 0 | goto err; |
654 | 0 | dstctx->dsa = srcctx->dsa; |
655 | |
|
656 | 0 | if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md)) |
657 | 0 | goto err; |
658 | 0 | dstctx->md = srcctx->md; |
659 | |
|
660 | 0 | if (srcctx->mdctx != NULL) { |
661 | 0 | dstctx->mdctx = EVP_MD_CTX_new(); |
662 | 0 | if (dstctx->mdctx == NULL |
663 | 0 | || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx)) |
664 | 0 | goto err; |
665 | 0 | } |
666 | | |
667 | 0 | if (srcctx->propq != NULL) { |
668 | 0 | dstctx->propq = OPENSSL_strdup(srcctx->propq); |
669 | 0 | if (dstctx->propq == NULL) |
670 | 0 | goto err; |
671 | 0 | } |
672 | | |
673 | 0 | return dstctx; |
674 | 0 | err: |
675 | 0 | dsa_freectx(dstctx); |
676 | 0 | return NULL; |
677 | 0 | } |
678 | | |
679 | | struct dsa_all_set_ctx_params_st { |
680 | | OSSL_PARAM *digest; /* dsa_set_ctx_params */ |
681 | | OSSL_PARAM *propq; /* dsa_set_ctx_params */ |
682 | | #ifdef FIPS_MODULE |
683 | | OSSL_PARAM *ind_d; |
684 | | OSSL_PARAM *ind_k; |
685 | | OSSL_PARAM *ind_sign; |
686 | | #endif |
687 | | OSSL_PARAM *nonce; |
688 | | OSSL_PARAM *sig; /* dsa_sigalg_set_ctx_params */ |
689 | | }; |
690 | | |
691 | | #define dsa_set_ctx_params_st dsa_all_set_ctx_params_st |
692 | | #define dsa_sigalg_set_ctx_params_st dsa_all_set_ctx_params_st |
693 | | |
694 | | #include "providers/implementations/signature/dsa_sig.inc" |
695 | | |
696 | | static int dsa_get_ctx_params(void *vpdsactx, OSSL_PARAM *params) |
697 | 0 | { |
698 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
699 | 0 | struct dsa_get_ctx_params_st p; |
700 | |
|
701 | 0 | if (pdsactx == NULL || !dsa_get_ctx_params_decoder(params, &p)) |
702 | 0 | return 0; |
703 | | |
704 | 0 | if (p.algid != NULL |
705 | 0 | && !OSSL_PARAM_set_octet_string(p.algid, |
706 | 0 | pdsactx->aid_len == 0 ? NULL : pdsactx->aid_buf, |
707 | 0 | pdsactx->aid_len)) |
708 | 0 | return 0; |
709 | | |
710 | 0 | if (p.digest != NULL && !OSSL_PARAM_set_utf8_string(p.digest, pdsactx->mdname)) |
711 | 0 | return 0; |
712 | | |
713 | 0 | if (p.nonce != NULL && !OSSL_PARAM_set_uint(p.nonce, pdsactx->nonce_type)) |
714 | 0 | return 0; |
715 | | |
716 | 0 | if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(pdsactx, p.ind)) |
717 | 0 | return 0; |
718 | | |
719 | 0 | return 1; |
720 | 0 | } |
721 | | |
722 | | static const OSSL_PARAM *dsa_gettable_ctx_params(ossl_unused void *ctx, |
723 | | ossl_unused void *provctx) |
724 | 0 | { |
725 | 0 | return dsa_get_ctx_params_list; |
726 | 0 | } |
727 | | |
728 | | /** |
729 | | * @brief Setup common params for dsa_set_ctx_params and dsa_sigalg_set_ctx_params |
730 | | * The caller is responsible for checking |vpdsactx| is not NULL and |params| |
731 | | * is not empty. |
732 | | */ |
733 | | static int dsa_common_set_ctx_params(PROV_DSA_CTX *pdsactx, |
734 | | const struct dsa_all_set_ctx_params_st *p) |
735 | 0 | { |
736 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(pdsactx, OSSL_FIPS_IND_SETTABLE0, |
737 | 0 | p->ind_k)) |
738 | 0 | return 0; |
739 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(pdsactx, OSSL_FIPS_IND_SETTABLE1, |
740 | 0 | p->ind_d)) |
741 | 0 | return 0; |
742 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(pdsactx, OSSL_FIPS_IND_SETTABLE2, |
743 | 0 | p->ind_sign)) |
744 | 0 | return 0; |
745 | | |
746 | 0 | if (p->nonce != NULL |
747 | 0 | && !OSSL_PARAM_get_uint(p->nonce, &pdsactx->nonce_type)) |
748 | 0 | return 0; |
749 | 0 | return 1; |
750 | 0 | } |
751 | | |
752 | | static int dsa_set_ctx_params(void *vpdsactx, const OSSL_PARAM params[]) |
753 | 0 | { |
754 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
755 | 0 | struct dsa_all_set_ctx_params_st p; |
756 | 0 | int ret; |
757 | |
|
758 | 0 | if (pdsactx == NULL || !dsa_set_ctx_params_decoder(params, &p)) |
759 | 0 | return 0; |
760 | | |
761 | 0 | if ((ret = dsa_common_set_ctx_params(pdsactx, &p)) <= 0) |
762 | 0 | return ret; |
763 | | |
764 | 0 | if (p.digest != NULL) { |
765 | 0 | char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname; |
766 | 0 | char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops; |
767 | |
|
768 | 0 | if (!OSSL_PARAM_get_utf8_string(p.digest, &pmdname, sizeof(mdname))) |
769 | 0 | return 0; |
770 | 0 | if (p.propq != NULL |
771 | 0 | && !OSSL_PARAM_get_utf8_string(p.propq, &pmdprops, sizeof(mdprops))) |
772 | 0 | return 0; |
773 | 0 | if (!dsa_setup_md(pdsactx, mdname, mdprops, "DSA Set Ctx")) |
774 | 0 | return 0; |
775 | 0 | } |
776 | 0 | return 1; |
777 | 0 | } |
778 | | |
779 | | static const OSSL_PARAM settable_ctx_params_no_digest[] = { |
780 | | OSSL_PARAM_END |
781 | | }; |
782 | | |
783 | | static const OSSL_PARAM *dsa_settable_ctx_params(void *vpdsactx, |
784 | | ossl_unused void *provctx) |
785 | 0 | { |
786 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
787 | |
|
788 | 0 | if (pdsactx != NULL && !pdsactx->flag_allow_md) |
789 | 0 | return settable_ctx_params_no_digest; |
790 | 0 | return dsa_set_ctx_params_list; |
791 | 0 | } |
792 | | |
793 | | static int dsa_get_ctx_md_params(void *vpdsactx, OSSL_PARAM *params) |
794 | 0 | { |
795 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
796 | |
|
797 | 0 | if (pdsactx->mdctx == NULL) |
798 | 0 | return 0; |
799 | | |
800 | 0 | return EVP_MD_CTX_get_params(pdsactx->mdctx, params); |
801 | 0 | } |
802 | | |
803 | | static const OSSL_PARAM *dsa_gettable_ctx_md_params(void *vpdsactx) |
804 | 0 | { |
805 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
806 | |
|
807 | 0 | if (pdsactx->md == NULL) |
808 | 0 | return 0; |
809 | | |
810 | 0 | return EVP_MD_gettable_ctx_params(pdsactx->md); |
811 | 0 | } |
812 | | |
813 | | static int dsa_set_ctx_md_params(void *vpdsactx, const OSSL_PARAM params[]) |
814 | 0 | { |
815 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
816 | |
|
817 | 0 | if (pdsactx->mdctx == NULL) |
818 | 0 | return 0; |
819 | | |
820 | 0 | return EVP_MD_CTX_set_params(pdsactx->mdctx, params); |
821 | 0 | } |
822 | | |
823 | | static const OSSL_PARAM *dsa_settable_ctx_md_params(void *vpdsactx) |
824 | 0 | { |
825 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
826 | |
|
827 | 0 | if (pdsactx->md == NULL) |
828 | 0 | return 0; |
829 | | |
830 | 0 | return EVP_MD_settable_ctx_params(pdsactx->md); |
831 | 0 | } |
832 | | |
833 | | const OSSL_DISPATCH ossl_dsa_signature_functions[] = { |
834 | | { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))dsa_newctx }, |
835 | | { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))dsa_sign_init }, |
836 | | { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))dsa_sign }, |
837 | | { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))dsa_verify_init }, |
838 | | { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))dsa_verify }, |
839 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, |
840 | | (void (*)(void))dsa_digest_sign_init }, |
841 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, |
842 | | (void (*)(void))dsa_digest_signverify_update }, |
843 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, |
844 | | (void (*)(void))dsa_digest_sign_final }, |
845 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, |
846 | | (void (*)(void))dsa_digest_verify_init }, |
847 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE, |
848 | | (void (*)(void))dsa_digest_signverify_update }, |
849 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL, |
850 | | (void (*)(void))dsa_digest_verify_final }, |
851 | | { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))dsa_freectx }, |
852 | | { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))dsa_dupctx }, |
853 | | { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))dsa_get_ctx_params }, |
854 | | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, |
855 | | (void (*)(void))dsa_gettable_ctx_params }, |
856 | | { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))dsa_set_ctx_params }, |
857 | | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, |
858 | | (void (*)(void))dsa_settable_ctx_params }, |
859 | | { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS, |
860 | | (void (*)(void))dsa_get_ctx_md_params }, |
861 | | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS, |
862 | | (void (*)(void))dsa_gettable_ctx_md_params }, |
863 | | { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS, |
864 | | (void (*)(void))dsa_set_ctx_md_params }, |
865 | | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS, |
866 | | (void (*)(void))dsa_settable_ctx_md_params }, |
867 | | OSSL_DISPATCH_END |
868 | | }; |
869 | | |
870 | | /* ------------------------------------------------------------------ */ |
871 | | |
872 | | /* |
873 | | * So called sigalgs (composite DSA+hash) implemented below. They |
874 | | * are pretty much hard coded. |
875 | | */ |
876 | | |
877 | | static OSSL_FUNC_signature_query_key_types_fn dsa_sigalg_query_key_types; |
878 | | static OSSL_FUNC_signature_settable_ctx_params_fn dsa_sigalg_settable_ctx_params; |
879 | | static OSSL_FUNC_signature_set_ctx_params_fn dsa_sigalg_set_ctx_params; |
880 | | |
881 | | /* |
882 | | * dsa_sigalg_signverify_init() is almost like dsa_digest_signverify_init(), |
883 | | * just doesn't allow fetching an MD from whatever the user chooses. |
884 | | */ |
885 | | static int dsa_sigalg_signverify_init(void *vpdsactx, void *vdsa, |
886 | | OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params, |
887 | | const OSSL_PARAM params[], |
888 | | const char *mdname, |
889 | | int operation, const char *desc) |
890 | 0 | { |
891 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
892 | |
|
893 | 0 | if (!ossl_prov_is_running()) |
894 | 0 | return 0; |
895 | | |
896 | 0 | if (!dsa_signverify_init(vpdsactx, vdsa, set_ctx_params, params, operation, |
897 | 0 | desc)) |
898 | 0 | return 0; |
899 | | |
900 | 0 | if (!dsa_setup_md(pdsactx, mdname, NULL, desc)) |
901 | 0 | return 0; |
902 | | |
903 | 0 | pdsactx->flag_sigalg = 1; |
904 | 0 | pdsactx->flag_allow_md = 0; |
905 | |
|
906 | 0 | if (pdsactx->mdctx == NULL) { |
907 | 0 | pdsactx->mdctx = EVP_MD_CTX_new(); |
908 | 0 | if (pdsactx->mdctx == NULL) |
909 | 0 | goto error; |
910 | 0 | } |
911 | | |
912 | 0 | if (!EVP_DigestInit_ex2(pdsactx->mdctx, pdsactx->md, params)) |
913 | 0 | goto error; |
914 | | |
915 | 0 | return 1; |
916 | | |
917 | 0 | error: |
918 | 0 | EVP_MD_CTX_free(pdsactx->mdctx); |
919 | 0 | pdsactx->mdctx = NULL; |
920 | 0 | return 0; |
921 | 0 | } |
922 | | |
923 | | static const char **dsa_sigalg_query_key_types(void) |
924 | 0 | { |
925 | 0 | static const char *keytypes[] = { "DSA", NULL }; |
926 | |
|
927 | 0 | return keytypes; |
928 | 0 | } |
929 | | |
930 | | static const OSSL_PARAM *dsa_sigalg_settable_ctx_params(void *vpdsactx, |
931 | | ossl_unused void *provctx) |
932 | 0 | { |
933 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
934 | |
|
935 | 0 | if (pdsactx != NULL && pdsactx->operation == EVP_PKEY_OP_VERIFYMSG) |
936 | 0 | return dsa_sigalg_set_ctx_params_list; |
937 | 0 | return NULL; |
938 | 0 | } |
939 | | |
940 | | static int dsa_sigalg_set_ctx_params(void *vpdsactx, const OSSL_PARAM params[]) |
941 | 0 | { |
942 | 0 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; |
943 | 0 | struct dsa_all_set_ctx_params_st p; |
944 | 0 | int ret; |
945 | |
|
946 | 0 | if (pdsactx == NULL || !dsa_sigalg_set_ctx_params_decoder(params, &p)) |
947 | 0 | return 0; |
948 | | |
949 | 0 | if ((ret = dsa_common_set_ctx_params(pdsactx, &p)) <= 0) |
950 | 0 | return ret; |
951 | | |
952 | 0 | if (pdsactx->operation == EVP_PKEY_OP_VERIFYMSG) { |
953 | 0 | if (p.sig != NULL) { |
954 | 0 | OPENSSL_free(pdsactx->sig); |
955 | 0 | pdsactx->sig = NULL; |
956 | 0 | pdsactx->siglen = 0; |
957 | 0 | if (!OSSL_PARAM_get_octet_string(p.sig, (void **)&pdsactx->sig, |
958 | 0 | 0, &pdsactx->siglen)) |
959 | 0 | return 0; |
960 | 0 | } |
961 | 0 | } |
962 | 0 | return 1; |
963 | 0 | } |
964 | | |
965 | | #define IMPL_DSA_SIGALG(md, MD) \ |
966 | | static OSSL_FUNC_signature_sign_init_fn dsa_##md##_sign_init; \ |
967 | | static OSSL_FUNC_signature_sign_message_init_fn \ |
968 | | dsa_##md##_sign_message_init; \ |
969 | | static OSSL_FUNC_signature_verify_init_fn dsa_##md##_verify_init; \ |
970 | | static OSSL_FUNC_signature_verify_message_init_fn \ |
971 | | dsa_##md##_verify_message_init; \ |
972 | | \ |
973 | | static int \ |
974 | | dsa_##md##_sign_init(void *vpdsactx, void *vdsa, \ |
975 | | const OSSL_PARAM params[]) \ |
976 | 0 | { \ |
977 | 0 | static const char desc[] = "DSA-" MD " Sign Init"; \ |
978 | 0 | \ |
979 | 0 | return dsa_sigalg_signverify_init(vpdsactx, vdsa, \ |
980 | 0 | dsa_sigalg_set_ctx_params, \ |
981 | 0 | params, MD, \ |
982 | 0 | EVP_PKEY_OP_SIGN, \ |
983 | 0 | desc); \ |
984 | 0 | } \ Unexecuted instantiation: dsa_sig.c:dsa_sha1_sign_init Unexecuted instantiation: dsa_sig.c:dsa_sha224_sign_init Unexecuted instantiation: dsa_sig.c:dsa_sha256_sign_init Unexecuted instantiation: dsa_sig.c:dsa_sha384_sign_init Unexecuted instantiation: dsa_sig.c:dsa_sha512_sign_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_224_sign_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_256_sign_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_384_sign_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_512_sign_init |
985 | | \ |
986 | | static int \ |
987 | | dsa_##md##_sign_message_init(void *vpdsactx, void *vdsa, \ |
988 | | const OSSL_PARAM params[]) \ |
989 | 0 | { \ |
990 | 0 | static const char desc[] = "DSA-" MD " Sign Message Init"; \ |
991 | 0 | \ |
992 | 0 | return dsa_sigalg_signverify_init(vpdsactx, vdsa, \ |
993 | 0 | dsa_sigalg_set_ctx_params, \ |
994 | 0 | params, MD, \ |
995 | 0 | EVP_PKEY_OP_SIGNMSG, \ |
996 | 0 | desc); \ |
997 | 0 | } \ Unexecuted instantiation: dsa_sig.c:dsa_sha1_sign_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha224_sign_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha256_sign_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha384_sign_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha512_sign_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_224_sign_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_256_sign_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_384_sign_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_512_sign_message_init |
998 | | \ |
999 | | static int \ |
1000 | | dsa_##md##_verify_init(void *vpdsactx, void *vdsa, \ |
1001 | | const OSSL_PARAM params[]) \ |
1002 | 0 | { \ |
1003 | 0 | static const char desc[] = "DSA-" MD " Verify Init"; \ |
1004 | 0 | \ |
1005 | 0 | return dsa_sigalg_signverify_init(vpdsactx, vdsa, \ |
1006 | 0 | dsa_sigalg_set_ctx_params, \ |
1007 | 0 | params, MD, \ |
1008 | 0 | EVP_PKEY_OP_VERIFY, \ |
1009 | 0 | desc); \ |
1010 | 0 | } \ Unexecuted instantiation: dsa_sig.c:dsa_sha1_verify_init Unexecuted instantiation: dsa_sig.c:dsa_sha224_verify_init Unexecuted instantiation: dsa_sig.c:dsa_sha256_verify_init Unexecuted instantiation: dsa_sig.c:dsa_sha384_verify_init Unexecuted instantiation: dsa_sig.c:dsa_sha512_verify_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_224_verify_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_256_verify_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_384_verify_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_512_verify_init |
1011 | | \ |
1012 | | static int \ |
1013 | | dsa_##md##_verify_message_init(void *vpdsactx, void *vdsa, \ |
1014 | | const OSSL_PARAM params[]) \ |
1015 | 0 | { \ |
1016 | 0 | static const char desc[] = "DSA-" MD " Verify Message Init"; \ |
1017 | 0 | \ |
1018 | 0 | return dsa_sigalg_signverify_init(vpdsactx, vdsa, \ |
1019 | 0 | dsa_sigalg_set_ctx_params, \ |
1020 | 0 | params, MD, \ |
1021 | 0 | EVP_PKEY_OP_VERIFYMSG, \ |
1022 | 0 | desc); \ |
1023 | 0 | } \ Unexecuted instantiation: dsa_sig.c:dsa_sha1_verify_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha224_verify_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha256_verify_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha384_verify_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha512_verify_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_224_verify_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_256_verify_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_384_verify_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_512_verify_message_init |
1024 | | \ |
1025 | | const OSSL_DISPATCH ossl_dsa_##md##_signature_functions[] = { \ |
1026 | | { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))dsa_newctx }, \ |
1027 | | { OSSL_FUNC_SIGNATURE_SIGN_INIT, \ |
1028 | | (void (*)(void))dsa_##md##_sign_init }, \ |
1029 | | { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))dsa_sign }, \ |
1030 | | { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_INIT, \ |
1031 | | (void (*)(void))dsa_##md##_sign_message_init }, \ |
1032 | | { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_UPDATE, \ |
1033 | | (void (*)(void))dsa_signverify_message_update }, \ |
1034 | | { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_FINAL, \ |
1035 | | (void (*)(void))dsa_sign_message_final }, \ |
1036 | | { OSSL_FUNC_SIGNATURE_VERIFY_INIT, \ |
1037 | | (void (*)(void))dsa_##md##_verify_init }, \ |
1038 | | { OSSL_FUNC_SIGNATURE_VERIFY, \ |
1039 | | (void (*)(void))dsa_verify }, \ |
1040 | | { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_INIT, \ |
1041 | | (void (*)(void))dsa_##md##_verify_message_init }, \ |
1042 | | { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_UPDATE, \ |
1043 | | (void (*)(void))dsa_signverify_message_update }, \ |
1044 | | { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_FINAL, \ |
1045 | | (void (*)(void))dsa_verify_message_final }, \ |
1046 | | { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))dsa_freectx }, \ |
1047 | | { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))dsa_dupctx }, \ |
1048 | | { OSSL_FUNC_SIGNATURE_QUERY_KEY_TYPES, \ |
1049 | | (void (*)(void))dsa_sigalg_query_key_types }, \ |
1050 | | { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, \ |
1051 | | (void (*)(void))dsa_get_ctx_params }, \ |
1052 | | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, \ |
1053 | | (void (*)(void))dsa_gettable_ctx_params }, \ |
1054 | | { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, \ |
1055 | | (void (*)(void))dsa_sigalg_set_ctx_params }, \ |
1056 | | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, \ |
1057 | | (void (*)(void))dsa_sigalg_settable_ctx_params }, \ |
1058 | | OSSL_DISPATCH_END \ |
1059 | | } |
1060 | | |
1061 | | IMPL_DSA_SIGALG(sha1, "SHA1"); |
1062 | | IMPL_DSA_SIGALG(sha224, "SHA2-224"); |
1063 | | IMPL_DSA_SIGALG(sha256, "SHA2-256"); |
1064 | | IMPL_DSA_SIGALG(sha384, "SHA2-384"); |
1065 | | IMPL_DSA_SIGALG(sha512, "SHA2-512"); |
1066 | | IMPL_DSA_SIGALG(sha3_224, "SHA3-224"); |
1067 | | IMPL_DSA_SIGALG(sha3_256, "SHA3-256"); |
1068 | | IMPL_DSA_SIGALG(sha3_384, "SHA3-384"); |
1069 | | IMPL_DSA_SIGALG(sha3_512, "SHA3-512"); |