/src/openssl34/providers/implementations/signature/ecdsa_sig.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2020-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 | | * ECDSA 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> /* memcpy */ |
17 | | #include <openssl/crypto.h> |
18 | | #include <openssl/core_dispatch.h> |
19 | | #include <openssl/core_names.h> |
20 | | #include <openssl/dsa.h> |
21 | | #include <openssl/params.h> |
22 | | #include <openssl/evp.h> |
23 | | #include <openssl/err.h> |
24 | | #include <openssl/proverr.h> |
25 | | #include "internal/nelem.h" |
26 | | #include "internal/sizes.h" |
27 | | #include "internal/cryptlib.h" |
28 | | #include "internal/deterministic_nonce.h" |
29 | | #include "prov/providercommon.h" |
30 | | #include "prov/implementations.h" |
31 | | #include "prov/provider_ctx.h" |
32 | | #include "prov/securitycheck.h" |
33 | | #include "prov/der_ec.h" |
34 | | #include "crypto/ec.h" |
35 | | |
36 | | static OSSL_FUNC_signature_newctx_fn ecdsa_newctx; |
37 | | static OSSL_FUNC_signature_sign_init_fn ecdsa_sign_init; |
38 | | static OSSL_FUNC_signature_verify_init_fn ecdsa_verify_init; |
39 | | static OSSL_FUNC_signature_sign_fn ecdsa_sign; |
40 | | static OSSL_FUNC_signature_sign_message_update_fn ecdsa_signverify_message_update; |
41 | | static OSSL_FUNC_signature_sign_message_final_fn ecdsa_sign_message_final; |
42 | | static OSSL_FUNC_signature_verify_fn ecdsa_verify; |
43 | | static OSSL_FUNC_signature_verify_message_update_fn ecdsa_signverify_message_update; |
44 | | static OSSL_FUNC_signature_verify_message_final_fn ecdsa_verify_message_final; |
45 | | static OSSL_FUNC_signature_digest_sign_init_fn ecdsa_digest_sign_init; |
46 | | static OSSL_FUNC_signature_digest_sign_update_fn ecdsa_digest_signverify_update; |
47 | | static OSSL_FUNC_signature_digest_sign_final_fn ecdsa_digest_sign_final; |
48 | | static OSSL_FUNC_signature_digest_verify_init_fn ecdsa_digest_verify_init; |
49 | | static OSSL_FUNC_signature_digest_verify_update_fn ecdsa_digest_signverify_update; |
50 | | static OSSL_FUNC_signature_digest_verify_final_fn ecdsa_digest_verify_final; |
51 | | static OSSL_FUNC_signature_freectx_fn ecdsa_freectx; |
52 | | static OSSL_FUNC_signature_dupctx_fn ecdsa_dupctx; |
53 | | static OSSL_FUNC_signature_query_key_types_fn ecdsa_sigalg_query_key_types; |
54 | | static OSSL_FUNC_signature_get_ctx_params_fn ecdsa_get_ctx_params; |
55 | | static OSSL_FUNC_signature_gettable_ctx_params_fn ecdsa_gettable_ctx_params; |
56 | | static OSSL_FUNC_signature_set_ctx_params_fn ecdsa_set_ctx_params; |
57 | | static OSSL_FUNC_signature_settable_ctx_params_fn ecdsa_settable_ctx_params; |
58 | | static OSSL_FUNC_signature_get_ctx_md_params_fn ecdsa_get_ctx_md_params; |
59 | | static OSSL_FUNC_signature_gettable_ctx_md_params_fn ecdsa_gettable_ctx_md_params; |
60 | | static OSSL_FUNC_signature_set_ctx_md_params_fn ecdsa_set_ctx_md_params; |
61 | | static OSSL_FUNC_signature_settable_ctx_md_params_fn ecdsa_settable_ctx_md_params; |
62 | | static OSSL_FUNC_signature_set_ctx_params_fn ecdsa_sigalg_set_ctx_params; |
63 | | static OSSL_FUNC_signature_settable_ctx_params_fn ecdsa_sigalg_settable_ctx_params; |
64 | | |
65 | | /* |
66 | | * What's passed as an actual key is defined by the KEYMGMT interface. |
67 | | * We happen to know that our KEYMGMT simply passes DSA structures, so |
68 | | * we use that here too. |
69 | | */ |
70 | | |
71 | | typedef struct { |
72 | | OSSL_LIB_CTX *libctx; |
73 | | char *propq; |
74 | | EC_KEY *ec; |
75 | | /* |operation| reuses EVP's operation bitfield */ |
76 | | int operation; |
77 | | |
78 | | /* |
79 | | * Flag to determine if a full sigalg is run (1) or if a composable |
80 | | * signature algorithm is run (0). |
81 | | * |
82 | | * When a full sigalg is run (1), this currently affects the following |
83 | | * other flags, which are to remain untouched after their initialization: |
84 | | * |
85 | | * - flag_allow_md (initialized to 0) |
86 | | */ |
87 | | unsigned int flag_sigalg : 1; |
88 | | /* |
89 | | * Flag to determine if the hash function can be changed (1) or not (0) |
90 | | * Because it's dangerous to change during a DigestSign or DigestVerify |
91 | | * operation, this flag is cleared by their Init function, and set again |
92 | | * by their Final function. |
93 | | */ |
94 | | unsigned int flag_allow_md : 1; |
95 | | |
96 | | /* The Algorithm Identifier of the combined signature algorithm */ |
97 | | unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE]; |
98 | | size_t aid_len; |
99 | | |
100 | | /* main digest */ |
101 | | char mdname[OSSL_MAX_NAME_SIZE]; |
102 | | EVP_MD *md; |
103 | | EVP_MD_CTX *mdctx; |
104 | | size_t mdsize; |
105 | | |
106 | | /* Signature, for verification */ |
107 | | unsigned char *sig; |
108 | | size_t siglen; |
109 | | |
110 | | /* |
111 | | * Internally used to cache the results of calling the EC group |
112 | | * sign_setup() methods which are then passed to the sign operation. |
113 | | * This is used by CAVS failure tests to terminate a loop if the signature |
114 | | * is not valid. |
115 | | * This could of also been done with a simple flag. |
116 | | */ |
117 | | BIGNUM *kinv; |
118 | | BIGNUM *r; |
119 | | #if !defined(OPENSSL_NO_ACVP_TESTS) |
120 | | /* |
121 | | * This indicates that KAT (CAVS) test is running. Externally an app will |
122 | | * override the random callback such that the generated private key and k |
123 | | * are known. |
124 | | * Normal operation will loop to choose a new k if the signature is not |
125 | | * valid - but for this mode of operation it forces a failure instead. |
126 | | */ |
127 | | unsigned int kattest; |
128 | | #endif |
129 | | #ifdef FIPS_MODULE |
130 | | /* |
131 | | * FIPS 140-3 IG 2.4.B mandates that verification based on a digest of a |
132 | | * message is not permitted. However, signing based on a digest is still |
133 | | * permitted. |
134 | | */ |
135 | | int verify_message; |
136 | | #endif |
137 | | /* If this is set then the generated k is not random */ |
138 | | unsigned int nonce_type; |
139 | | OSSL_FIPS_IND_DECLARE |
140 | | } PROV_ECDSA_CTX; |
141 | | |
142 | | static void *ecdsa_newctx(void *provctx, const char *propq) |
143 | 14.0k | { |
144 | 14.0k | PROV_ECDSA_CTX *ctx; |
145 | | |
146 | 14.0k | if (!ossl_prov_is_running()) |
147 | 0 | return NULL; |
148 | | |
149 | 14.0k | ctx = OPENSSL_zalloc(sizeof(PROV_ECDSA_CTX)); |
150 | 14.0k | if (ctx == NULL) |
151 | 0 | return NULL; |
152 | | |
153 | 14.0k | OSSL_FIPS_IND_INIT(ctx) |
154 | 14.0k | ctx->flag_allow_md = 1; |
155 | | #ifdef FIPS_MODULE |
156 | | ctx->verify_message = 1; |
157 | | #endif |
158 | 14.0k | ctx->libctx = PROV_LIBCTX_OF(provctx); |
159 | 14.0k | if (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL) { |
160 | 0 | OPENSSL_free(ctx); |
161 | 0 | ctx = NULL; |
162 | 0 | } |
163 | 14.0k | return ctx; |
164 | 14.0k | } |
165 | | |
166 | | static int ecdsa_setup_md(PROV_ECDSA_CTX *ctx, |
167 | | const char *mdname, const char *mdprops, |
168 | | const char *desc) |
169 | 6.81k | { |
170 | 6.81k | EVP_MD *md = NULL; |
171 | 6.81k | size_t mdname_len; |
172 | 6.81k | int md_nid, md_size; |
173 | 6.81k | WPACKET pkt; |
174 | 6.81k | unsigned char *aid = NULL; |
175 | | |
176 | 6.81k | if (mdname == NULL) |
177 | 0 | return 1; |
178 | | |
179 | 6.81k | mdname_len = strlen(mdname); |
180 | 6.81k | if (mdname_len >= sizeof(ctx->mdname)) { |
181 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, |
182 | 0 | "%s exceeds name buffer length", mdname); |
183 | 0 | return 0; |
184 | 0 | } |
185 | 6.81k | if (mdprops == NULL) |
186 | 6.81k | mdprops = ctx->propq; |
187 | 6.81k | md = EVP_MD_fetch(ctx->libctx, mdname, mdprops); |
188 | 6.81k | if (md == NULL) { |
189 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, |
190 | 0 | "%s could not be fetched", mdname); |
191 | 0 | return 0; |
192 | 0 | } |
193 | 6.81k | md_size = EVP_MD_get_size(md); |
194 | 6.81k | if (md_size <= 0) { |
195 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, |
196 | 0 | "%s has invalid md size %d", mdname, md_size); |
197 | 0 | goto err; |
198 | 0 | } |
199 | 6.81k | md_nid = ossl_digest_get_approved_nid(md); |
200 | | #ifdef FIPS_MODULE |
201 | | if (md_nid == NID_undef) { |
202 | | ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED, |
203 | | "digest=%s", mdname); |
204 | | goto err; |
205 | | } |
206 | | #endif |
207 | | /* XOF digests don't work */ |
208 | 6.81k | if (EVP_MD_xof(md)) { |
209 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); |
210 | 0 | goto err; |
211 | 0 | } |
212 | | |
213 | | #ifdef FIPS_MODULE |
214 | | { |
215 | | int sha1_allowed |
216 | | = ((ctx->operation |
217 | | & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_SIGNMSG)) == 0); |
218 | | |
219 | | if (!ossl_fips_ind_digest_sign_check(OSSL_FIPS_IND_GET(ctx), |
220 | | OSSL_FIPS_IND_SETTABLE1, |
221 | | ctx->libctx, |
222 | | md_nid, sha1_allowed, desc, |
223 | | ossl_fips_config_signature_digest_check)) |
224 | | goto err; |
225 | | } |
226 | | #endif |
227 | | |
228 | 6.81k | if (!ctx->flag_allow_md) { |
229 | 0 | if (ctx->mdname[0] != '\0' && !EVP_MD_is_a(md, ctx->mdname)) { |
230 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED, |
231 | 0 | "digest %s != %s", mdname, ctx->mdname); |
232 | 0 | goto err; |
233 | 0 | } |
234 | 0 | EVP_MD_free(md); |
235 | 0 | return 1; |
236 | 0 | } |
237 | | |
238 | 6.81k | EVP_MD_CTX_free(ctx->mdctx); |
239 | 6.81k | EVP_MD_free(ctx->md); |
240 | | |
241 | 6.81k | ctx->aid_len = 0; |
242 | 6.81k | #ifndef FIPS_MODULE |
243 | 6.81k | if (md_nid != NID_undef) { |
244 | | #else |
245 | | { |
246 | | #endif |
247 | 6.81k | if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf)) |
248 | 6.81k | && ossl_DER_w_algorithmIdentifier_ECDSA_with_MD(&pkt, -1, ctx->ec, |
249 | 6.81k | md_nid) |
250 | 6.81k | && WPACKET_finish(&pkt)) { |
251 | 6.81k | WPACKET_get_total_written(&pkt, &ctx->aid_len); |
252 | 6.81k | aid = WPACKET_get_curr(&pkt); |
253 | 6.81k | } |
254 | 6.81k | WPACKET_cleanup(&pkt); |
255 | 6.81k | if (aid != NULL && ctx->aid_len != 0) |
256 | 6.81k | memmove(ctx->aid_buf, aid, ctx->aid_len); |
257 | 6.81k | } |
258 | | |
259 | 6.81k | ctx->mdctx = NULL; |
260 | 6.81k | ctx->md = md; |
261 | 6.81k | ctx->mdsize = (size_t)md_size; |
262 | 6.81k | OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname)); |
263 | | |
264 | 6.81k | return 1; |
265 | 0 | err: |
266 | 0 | EVP_MD_free(md); |
267 | 0 | return 0; |
268 | 6.81k | } |
269 | | |
270 | | static int |
271 | | ecdsa_signverify_init(PROV_ECDSA_CTX *ctx, void *ec, |
272 | | OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params, |
273 | | const OSSL_PARAM params[], int operation, |
274 | | const char *desc) |
275 | 6.81k | { |
276 | 6.81k | if (!ossl_prov_is_running() |
277 | 6.81k | || ctx == NULL) |
278 | 0 | return 0; |
279 | | |
280 | 6.81k | if (ec == NULL && ctx->ec == NULL) { |
281 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
282 | 0 | return 0; |
283 | 0 | } |
284 | | |
285 | 6.81k | if (ec != NULL) { |
286 | 6.81k | if (!EC_KEY_up_ref(ec)) |
287 | 0 | return 0; |
288 | 6.81k | EC_KEY_free(ctx->ec); |
289 | 6.81k | ctx->ec = ec; |
290 | 6.81k | } |
291 | | |
292 | 6.81k | ctx->operation = operation; |
293 | | |
294 | 6.81k | OSSL_FIPS_IND_SET_APPROVED(ctx) |
295 | 6.81k | if (!set_ctx_params(ctx, params)) |
296 | 0 | return 0; |
297 | | #ifdef FIPS_MODULE |
298 | | if (!ossl_fips_ind_ec_key_check(OSSL_FIPS_IND_GET(ctx), |
299 | | OSSL_FIPS_IND_SETTABLE0, ctx->libctx, |
300 | | EC_KEY_get0_group(ctx->ec), desc, |
301 | | (operation & (EVP_PKEY_OP_SIGN |
302 | | | EVP_PKEY_OP_SIGNMSG)) != 0)) |
303 | | return 0; |
304 | | #endif |
305 | 6.81k | return 1; |
306 | 6.81k | } |
307 | | |
308 | | static int ecdsa_sign_init(void *vctx, void *ec, const OSSL_PARAM params[]) |
309 | 0 | { |
310 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
311 | |
|
312 | | #ifdef FIPS_MODULE |
313 | | ctx->verify_message = 1; |
314 | | #endif |
315 | 0 | return ecdsa_signverify_init(ctx, ec, ecdsa_set_ctx_params, params, |
316 | 0 | EVP_PKEY_OP_SIGN, "ECDSA Sign Init"); |
317 | 0 | } |
318 | | |
319 | | /* |
320 | | * Sign tbs without digesting it first. This is suitable for "primitive" |
321 | | * signing and signing the digest of a message. |
322 | | */ |
323 | | static int ecdsa_sign_directly(void *vctx, |
324 | | unsigned char *sig, size_t *siglen, size_t sigsize, |
325 | | const unsigned char *tbs, size_t tbslen) |
326 | 4.76k | { |
327 | 4.76k | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
328 | 4.76k | int ret; |
329 | 4.76k | unsigned int sltmp; |
330 | 4.76k | size_t ecsize = ECDSA_size(ctx->ec); |
331 | | |
332 | 4.76k | if (!ossl_prov_is_running()) |
333 | 0 | return 0; |
334 | | |
335 | 4.76k | if (sig == NULL) { |
336 | 2.38k | *siglen = ecsize; |
337 | 2.38k | return 1; |
338 | 2.38k | } |
339 | | |
340 | | #if !defined(OPENSSL_NO_ACVP_TESTS) |
341 | | if (ctx->kattest && !ECDSA_sign_setup(ctx->ec, NULL, &ctx->kinv, &ctx->r)) |
342 | | return 0; |
343 | | #endif |
344 | | |
345 | 2.38k | if (sigsize < (size_t)ecsize) |
346 | 0 | return 0; |
347 | | |
348 | 2.38k | if (ctx->mdsize != 0 && tbslen != ctx->mdsize) |
349 | 0 | return 0; |
350 | | |
351 | 2.38k | if (ctx->nonce_type != 0) { |
352 | 0 | const char *mdname = NULL; |
353 | |
|
354 | 0 | if (ctx->mdname[0] != '\0') |
355 | 0 | mdname = ctx->mdname; |
356 | 0 | ret = ossl_ecdsa_deterministic_sign(tbs, tbslen, sig, &sltmp, |
357 | 0 | ctx->ec, ctx->nonce_type, |
358 | 0 | mdname, |
359 | 0 | ctx->libctx, ctx->propq); |
360 | 2.38k | } else { |
361 | 2.38k | ret = ECDSA_sign_ex(0, tbs, tbslen, sig, &sltmp, ctx->kinv, ctx->r, |
362 | 2.38k | ctx->ec); |
363 | 2.38k | } |
364 | 2.38k | if (ret <= 0) |
365 | 0 | return 0; |
366 | | |
367 | 2.38k | *siglen = sltmp; |
368 | 2.38k | return 1; |
369 | 2.38k | } |
370 | | |
371 | | static int ecdsa_signverify_message_update(void *vctx, |
372 | | const unsigned char *data, |
373 | | size_t datalen) |
374 | 3.10k | { |
375 | 3.10k | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
376 | | |
377 | 3.10k | if (ctx == NULL) |
378 | 0 | return 0; |
379 | | |
380 | 3.10k | return EVP_DigestUpdate(ctx->mdctx, data, datalen); |
381 | 3.10k | } |
382 | | |
383 | | static int ecdsa_sign_message_final(void *vctx, unsigned char *sig, |
384 | | size_t *siglen, size_t sigsize) |
385 | 4.76k | { |
386 | 4.76k | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
387 | 4.76k | unsigned char digest[EVP_MAX_MD_SIZE]; |
388 | 4.76k | unsigned int dlen = 0; |
389 | | |
390 | 4.76k | if (!ossl_prov_is_running() || ctx == NULL) |
391 | 0 | return 0; |
392 | 4.76k | if (ctx->mdctx == NULL) |
393 | 0 | return 0; |
394 | | /* |
395 | | * If sig is NULL then we're just finding out the sig size. Other fields |
396 | | * are ignored. Defer to ecdsa_sign. |
397 | | */ |
398 | 4.76k | if (sig != NULL |
399 | 4.76k | && !EVP_DigestFinal_ex(ctx->mdctx, digest, &dlen)) |
400 | 0 | return 0; |
401 | 4.76k | return ecdsa_sign_directly(vctx, sig, siglen, sigsize, digest, dlen); |
402 | 4.76k | } |
403 | | |
404 | | /* |
405 | | * If signing a message, digest tbs and sign the result. |
406 | | * Otherwise, sign tbs directly. |
407 | | */ |
408 | | static int ecdsa_sign(void *vctx, unsigned char *sig, size_t *siglen, |
409 | | size_t sigsize, const unsigned char *tbs, size_t tbslen) |
410 | | { |
411 | | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
412 | | |
413 | | if (ctx->operation == EVP_PKEY_OP_SIGNMSG) { |
414 | | /* |
415 | | * If |sig| is NULL, the caller is only looking for the sig length. |
416 | | * DO NOT update the input in this case. |
417 | | */ |
418 | | if (sig == NULL) |
419 | | return ecdsa_sign_message_final(ctx, sig, siglen, sigsize); |
420 | | |
421 | | if (ecdsa_signverify_message_update(ctx, tbs, tbslen) <= 0) |
422 | | return 0; |
423 | | return ecdsa_sign_message_final(ctx, sig, siglen, sigsize); |
424 | | } |
425 | | return ecdsa_sign_directly(ctx, sig, siglen, sigsize, tbs, tbslen); |
426 | | } |
427 | | |
428 | | static int ecdsa_verify_init(void *vctx, void *ec, const OSSL_PARAM params[]) |
429 | 0 | { |
430 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
431 | |
|
432 | | #ifdef FIPS_MODULE |
433 | | ctx->verify_message = 0; |
434 | | #endif |
435 | 0 | return ecdsa_signverify_init(ctx, ec, ecdsa_set_ctx_params, params, |
436 | 0 | EVP_PKEY_OP_VERIFY, "ECDSA Verify Init"); |
437 | 0 | } |
438 | | |
439 | | static int ecdsa_verify_directly(void *vctx, |
440 | | const unsigned char *sig, size_t siglen, |
441 | | const unsigned char *tbs, size_t tbslen) |
442 | 718 | { |
443 | 718 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
444 | | |
445 | 718 | if (!ossl_prov_is_running() || (ctx->mdsize != 0 && tbslen != ctx->mdsize)) |
446 | 0 | return 0; |
447 | | |
448 | 718 | return ECDSA_verify(0, tbs, tbslen, sig, siglen, ctx->ec); |
449 | 718 | } |
450 | | |
451 | | static int ecdsa_verify_set_sig(void *vctx, |
452 | | const unsigned char *sig, size_t siglen) |
453 | 720 | { |
454 | 720 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
455 | 720 | OSSL_PARAM params[2]; |
456 | | |
457 | 720 | params[0] = |
458 | 720 | OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_SIGNATURE, |
459 | 720 | (unsigned char *)sig, siglen); |
460 | 720 | params[1] = OSSL_PARAM_construct_end(); |
461 | 720 | return ecdsa_sigalg_set_ctx_params(ctx, params); |
462 | 720 | } |
463 | | |
464 | | static int ecdsa_verify_message_final(void *vctx) |
465 | 718 | { |
466 | 718 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
467 | 718 | unsigned char digest[EVP_MAX_MD_SIZE]; |
468 | 718 | unsigned int dlen = 0; |
469 | | |
470 | 718 | if (!ossl_prov_is_running() || ctx == NULL || ctx->mdctx == NULL) |
471 | 0 | return 0; |
472 | | |
473 | | /* |
474 | | * The digests used here are all known (see ecdsa_get_md_nid()), so they |
475 | | * should not exceed the internal buffer size of EVP_MAX_MD_SIZE. |
476 | | */ |
477 | 718 | if (!EVP_DigestFinal_ex(ctx->mdctx, digest, &dlen)) |
478 | 0 | return 0; |
479 | | |
480 | 718 | return ecdsa_verify_directly(vctx, ctx->sig, ctx->siglen, |
481 | 718 | digest, dlen); |
482 | 718 | } |
483 | | |
484 | | /* |
485 | | * If verifying a message, digest tbs and verify the result. |
486 | | * Otherwise, verify tbs directly. |
487 | | */ |
488 | | static int ecdsa_verify(void *vctx, |
489 | | const unsigned char *sig, size_t siglen, |
490 | | const unsigned char *tbs, size_t tbslen) |
491 | 0 | { |
492 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
493 | |
|
494 | 0 | if (ctx->operation == EVP_PKEY_OP_VERIFYMSG) { |
495 | 0 | if (ecdsa_verify_set_sig(ctx, sig, siglen) <= 0) |
496 | 0 | return 0; |
497 | 0 | if (ecdsa_signverify_message_update(ctx, tbs, tbslen) <= 0) |
498 | 0 | return 0; |
499 | 0 | return ecdsa_verify_message_final(ctx); |
500 | 0 | } |
501 | 0 | return ecdsa_verify_directly(ctx, sig, siglen, tbs, tbslen); |
502 | 0 | } |
503 | | |
504 | | /* DigestSign/DigestVerify wrappers */ |
505 | | |
506 | | static int ecdsa_digest_signverify_init(void *vctx, const char *mdname, |
507 | | void *ec, const OSSL_PARAM params[], |
508 | | int operation, const char *desc) |
509 | 6.81k | { |
510 | 6.81k | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
511 | | |
512 | 6.81k | if (!ossl_prov_is_running()) |
513 | 0 | return 0; |
514 | | |
515 | | #ifdef FIPS_MODULE |
516 | | ctx->verify_message = 1; |
517 | | #endif |
518 | 6.81k | if (!ecdsa_signverify_init(vctx, ec, ecdsa_set_ctx_params, params, |
519 | 6.81k | operation, desc)) |
520 | 0 | return 0; |
521 | | |
522 | 6.81k | if (mdname != NULL |
523 | | /* was ecdsa_setup_md already called in ecdsa_signverify_init()? */ |
524 | 6.81k | && (mdname[0] == '\0' || OPENSSL_strcasecmp(ctx->mdname, mdname) != 0) |
525 | 6.81k | && !ecdsa_setup_md(ctx, mdname, NULL, desc)) |
526 | 0 | return 0; |
527 | | |
528 | 6.81k | ctx->flag_allow_md = 0; |
529 | | |
530 | 6.81k | if (ctx->mdctx == NULL) { |
531 | 6.81k | ctx->mdctx = EVP_MD_CTX_new(); |
532 | 6.81k | if (ctx->mdctx == NULL) |
533 | 0 | goto error; |
534 | 6.81k | } |
535 | | |
536 | 6.81k | if (!EVP_DigestInit_ex2(ctx->mdctx, ctx->md, params)) |
537 | 0 | goto error; |
538 | 6.81k | return 1; |
539 | 0 | error: |
540 | 0 | EVP_MD_CTX_free(ctx->mdctx); |
541 | 0 | ctx->mdctx = NULL; |
542 | 0 | return 0; |
543 | 6.81k | } |
544 | | |
545 | | static int ecdsa_digest_sign_init(void *vctx, const char *mdname, void *ec, |
546 | | const OSSL_PARAM params[]) |
547 | 12.4k | { |
548 | 12.4k | return ecdsa_digest_signverify_init(vctx, mdname, ec, params, |
549 | 12.4k | EVP_PKEY_OP_SIGNMSG, |
550 | 12.4k | "ECDSA Digest Sign Init"); |
551 | 12.4k | } |
552 | | |
553 | | static int ecdsa_digest_signverify_update(void *vctx, const unsigned char *data, |
554 | | size_t datalen) |
555 | 3.10k | { |
556 | 3.10k | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
557 | | |
558 | 3.10k | if (ctx == NULL || ctx->mdctx == NULL) |
559 | 0 | return 0; |
560 | | /* Sigalg implementations shouldn't do digest_sign */ |
561 | 3.10k | if (ctx->flag_sigalg) |
562 | 0 | return 0; |
563 | | |
564 | 3.10k | return ecdsa_signverify_message_update(vctx, data, datalen); |
565 | 3.10k | } |
566 | | |
567 | | int ecdsa_digest_sign_final(void *vctx, unsigned char *sig, size_t *siglen, |
568 | | size_t sigsize) |
569 | 4.76k | { |
570 | 4.76k | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
571 | 4.76k | int ok = 0; |
572 | | |
573 | 4.76k | if (ctx == NULL) |
574 | 0 | return 0; |
575 | | /* Sigalg implementations shouldn't do digest_sign */ |
576 | 4.76k | if (ctx->flag_sigalg) |
577 | 0 | return 0; |
578 | | |
579 | 4.76k | ok = ecdsa_sign_message_final(ctx, sig, siglen, sigsize); |
580 | | |
581 | 4.76k | ctx->flag_allow_md = 1; |
582 | | |
583 | 4.76k | return ok; |
584 | 4.76k | } |
585 | | |
586 | | static int ecdsa_digest_verify_init(void *vctx, const char *mdname, void *ec, |
587 | | const OSSL_PARAM params[]) |
588 | 1.53k | { |
589 | 1.53k | return ecdsa_digest_signverify_init(vctx, mdname, ec, params, |
590 | 1.53k | EVP_PKEY_OP_VERIFYMSG, |
591 | 1.53k | "ECDSA Digest Verify Init"); |
592 | 1.53k | } |
593 | | |
594 | | int ecdsa_digest_verify_final(void *vctx, const unsigned char *sig, |
595 | | size_t siglen) |
596 | 720 | { |
597 | 720 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
598 | 720 | int ok = 0; |
599 | | |
600 | 720 | if (!ossl_prov_is_running() || ctx == NULL || ctx->mdctx == NULL) |
601 | 0 | return 0; |
602 | | |
603 | | /* Sigalg implementations shouldn't do digest_verify */ |
604 | 720 | if (ctx->flag_sigalg) |
605 | 0 | return 0; |
606 | | |
607 | 720 | if (ecdsa_verify_set_sig(ctx, sig, siglen)) |
608 | 718 | ok = ecdsa_verify_message_final(ctx); |
609 | | |
610 | 720 | ctx->flag_allow_md = 1; |
611 | | |
612 | 720 | return ok; |
613 | 720 | } |
614 | | |
615 | | static void ecdsa_freectx(void *vctx) |
616 | 19.9k | { |
617 | 19.9k | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
618 | | |
619 | 19.9k | EVP_MD_CTX_free(ctx->mdctx); |
620 | 19.9k | EVP_MD_free(ctx->md); |
621 | 19.9k | OPENSSL_free(ctx->propq); |
622 | 19.9k | OPENSSL_free(ctx->sig); |
623 | 19.9k | EC_KEY_free(ctx->ec); |
624 | 19.9k | BN_clear_free(ctx->kinv); |
625 | 19.9k | BN_clear_free(ctx->r); |
626 | 19.9k | OPENSSL_free(ctx); |
627 | 19.9k | } |
628 | | |
629 | | static void *ecdsa_dupctx(void *vctx) |
630 | 5.95k | { |
631 | 5.95k | PROV_ECDSA_CTX *srcctx = (PROV_ECDSA_CTX *)vctx; |
632 | 5.95k | PROV_ECDSA_CTX *dstctx; |
633 | | |
634 | 5.95k | if (!ossl_prov_is_running()) |
635 | 0 | return NULL; |
636 | | |
637 | 5.95k | dstctx = OPENSSL_zalloc(sizeof(*srcctx)); |
638 | 5.95k | if (dstctx == NULL) |
639 | 0 | return NULL; |
640 | | |
641 | 5.95k | *dstctx = *srcctx; |
642 | 5.95k | dstctx->ec = NULL; |
643 | 5.95k | dstctx->propq = NULL; |
644 | | |
645 | 5.95k | if (srcctx->ec != NULL && !EC_KEY_up_ref(srcctx->ec)) |
646 | 0 | goto err; |
647 | | /* Test KATS should not need to be supported */ |
648 | 5.95k | if (srcctx->kinv != NULL || srcctx->r != NULL) |
649 | 0 | goto err; |
650 | 5.95k | dstctx->ec = srcctx->ec; |
651 | | |
652 | 5.95k | if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md)) |
653 | 0 | goto err; |
654 | 5.95k | dstctx->md = srcctx->md; |
655 | | |
656 | 5.95k | if (srcctx->mdctx != NULL) { |
657 | 5.95k | dstctx->mdctx = EVP_MD_CTX_new(); |
658 | 5.95k | if (dstctx->mdctx == NULL |
659 | 5.95k | || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx)) |
660 | 0 | goto err; |
661 | 5.95k | } |
662 | | |
663 | 5.95k | if (srcctx->propq != NULL) { |
664 | 0 | dstctx->propq = OPENSSL_strdup(srcctx->propq); |
665 | 0 | if (dstctx->propq == NULL) |
666 | 0 | goto err; |
667 | 0 | } |
668 | | |
669 | 5.95k | return dstctx; |
670 | 0 | err: |
671 | 0 | ecdsa_freectx(dstctx); |
672 | 0 | return NULL; |
673 | 5.95k | } |
674 | | |
675 | | static int ecdsa_get_ctx_params(void *vctx, OSSL_PARAM *params) |
676 | 0 | { |
677 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
678 | 0 | OSSL_PARAM *p; |
679 | |
|
680 | 0 | if (ctx == NULL) |
681 | 0 | return 0; |
682 | | |
683 | 0 | p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID); |
684 | 0 | if (p != NULL && !OSSL_PARAM_set_octet_string(p, |
685 | 0 | ctx->aid_len == 0 ? NULL : ctx->aid_buf, |
686 | 0 | ctx->aid_len)) |
687 | 0 | return 0; |
688 | | |
689 | 0 | p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE); |
690 | 0 | if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->mdsize)) |
691 | 0 | return 0; |
692 | | |
693 | 0 | p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST); |
694 | 0 | if (p != NULL && !OSSL_PARAM_set_utf8_string(p, ctx->md == NULL |
695 | 0 | ? ctx->mdname |
696 | 0 | : EVP_MD_get0_name(ctx->md))) |
697 | 0 | return 0; |
698 | | |
699 | 0 | p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_NONCE_TYPE); |
700 | 0 | if (p != NULL && !OSSL_PARAM_set_uint(p, ctx->nonce_type)) |
701 | 0 | return 0; |
702 | | |
703 | | #ifdef FIPS_MODULE |
704 | | p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_FIPS_VERIFY_MESSAGE); |
705 | | if (p != NULL && !OSSL_PARAM_set_uint(p, ctx->verify_message)) |
706 | | return 0; |
707 | | #endif |
708 | | |
709 | 0 | if (!OSSL_FIPS_IND_GET_CTX_PARAM(ctx, params)) |
710 | 0 | return 0; |
711 | 0 | return 1; |
712 | 0 | } |
713 | | |
714 | | static const OSSL_PARAM known_gettable_ctx_params[] = { |
715 | | OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0), |
716 | | OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL), |
717 | | OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0), |
718 | | OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_NONCE_TYPE, NULL), |
719 | | #ifdef FIPS_MODULE |
720 | | OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_FIPS_VERIFY_MESSAGE, NULL), |
721 | | #endif |
722 | | OSSL_FIPS_IND_GETTABLE_CTX_PARAM() |
723 | | OSSL_PARAM_END |
724 | | }; |
725 | | |
726 | | static const OSSL_PARAM *ecdsa_gettable_ctx_params(ossl_unused void *vctx, |
727 | | ossl_unused void *provctx) |
728 | 0 | { |
729 | 0 | return known_gettable_ctx_params; |
730 | 0 | } |
731 | | |
732 | | /* The common params for ecdsa_set_ctx_params and ecdsa_sigalg_set_ctx_params */ |
733 | | static int ecdsa_common_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
734 | 2.08k | { |
735 | 2.08k | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
736 | 2.08k | const OSSL_PARAM *p; |
737 | | |
738 | 2.08k | if (ctx == NULL) |
739 | 0 | return 0; |
740 | 2.08k | if (params == NULL) |
741 | 1.86k | return 1; |
742 | | |
743 | 213 | if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, params, |
744 | 213 | OSSL_SIGNATURE_PARAM_FIPS_KEY_CHECK)) |
745 | 0 | return 0; |
746 | 213 | if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE1, params, |
747 | 213 | OSSL_SIGNATURE_PARAM_FIPS_DIGEST_CHECK)) |
748 | 0 | return 0; |
749 | | |
750 | | #if !defined(OPENSSL_NO_ACVP_TESTS) |
751 | | p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_KAT); |
752 | | if (p != NULL && !OSSL_PARAM_get_uint(p, &ctx->kattest)) |
753 | | return 0; |
754 | | #endif |
755 | | |
756 | 213 | p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_NONCE_TYPE); |
757 | 213 | if (p != NULL |
758 | 213 | && !OSSL_PARAM_get_uint(p, &ctx->nonce_type)) |
759 | 0 | return 0; |
760 | 213 | return 1; |
761 | 213 | } |
762 | | |
763 | | #define ECDSA_COMMON_SETTABLE_CTX_PARAMS \ |
764 | | OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_KAT, NULL), \ |
765 | | OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_NONCE_TYPE, NULL), \ |
766 | | OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_SIGNATURE_PARAM_FIPS_KEY_CHECK) \ |
767 | | OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_SIGNATURE_PARAM_FIPS_DIGEST_CHECK) \ |
768 | | OSSL_PARAM_END |
769 | | |
770 | | static int ecdsa_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
771 | 1.86k | { |
772 | 1.86k | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
773 | 1.86k | const OSSL_PARAM *p; |
774 | 1.86k | size_t mdsize = 0; |
775 | 1.86k | int ret; |
776 | | |
777 | 1.86k | if ((ret = ecdsa_common_set_ctx_params(ctx, params)) <= 0) |
778 | 0 | return ret; |
779 | | |
780 | 1.86k | if (params == NULL) |
781 | 1.86k | return 1; |
782 | | |
783 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST); |
784 | 0 | if (p != NULL) { |
785 | 0 | char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname; |
786 | 0 | char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops; |
787 | 0 | const OSSL_PARAM *propsp = |
788 | 0 | OSSL_PARAM_locate_const(params, |
789 | 0 | OSSL_SIGNATURE_PARAM_PROPERTIES); |
790 | |
|
791 | 0 | if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname))) |
792 | 0 | return 0; |
793 | 0 | if (propsp != NULL |
794 | 0 | && !OSSL_PARAM_get_utf8_string(propsp, &pmdprops, sizeof(mdprops))) |
795 | 0 | return 0; |
796 | 0 | if (!ecdsa_setup_md(ctx, mdname, mdprops, "ECDSA Set Ctx")) |
797 | 0 | return 0; |
798 | 0 | } |
799 | | |
800 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE); |
801 | 0 | if (p != NULL) { |
802 | 0 | if (!OSSL_PARAM_get_size_t(p, &mdsize) |
803 | 0 | || (!ctx->flag_allow_md && mdsize != ctx->mdsize)) |
804 | 0 | return 0; |
805 | 0 | ctx->mdsize = mdsize; |
806 | 0 | } |
807 | 0 | return 1; |
808 | 0 | } |
809 | | |
810 | | static const OSSL_PARAM settable_ctx_params[] = { |
811 | | OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0), |
812 | | OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL), |
813 | | OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0), |
814 | | ECDSA_COMMON_SETTABLE_CTX_PARAMS |
815 | | }; |
816 | | |
817 | | static const OSSL_PARAM *ecdsa_settable_ctx_params(void *vctx, |
818 | | ossl_unused void *provctx) |
819 | 3 | { |
820 | 3 | return settable_ctx_params; |
821 | 3 | } |
822 | | |
823 | | static int ecdsa_get_ctx_md_params(void *vctx, OSSL_PARAM *params) |
824 | 0 | { |
825 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
826 | |
|
827 | 0 | if (ctx->mdctx == NULL) |
828 | 0 | return 0; |
829 | | |
830 | 0 | return EVP_MD_CTX_get_params(ctx->mdctx, params); |
831 | 0 | } |
832 | | |
833 | | static const OSSL_PARAM *ecdsa_gettable_ctx_md_params(void *vctx) |
834 | 0 | { |
835 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
836 | |
|
837 | 0 | if (ctx->md == NULL) |
838 | 0 | return 0; |
839 | | |
840 | 0 | return EVP_MD_gettable_ctx_params(ctx->md); |
841 | 0 | } |
842 | | |
843 | | static int ecdsa_set_ctx_md_params(void *vctx, const OSSL_PARAM params[]) |
844 | 0 | { |
845 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
846 | |
|
847 | 0 | if (ctx->mdctx == NULL) |
848 | 0 | return 0; |
849 | | |
850 | 0 | return EVP_MD_CTX_set_params(ctx->mdctx, params); |
851 | 0 | } |
852 | | |
853 | | static const OSSL_PARAM *ecdsa_settable_ctx_md_params(void *vctx) |
854 | 0 | { |
855 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
856 | |
|
857 | 0 | if (ctx->md == NULL) |
858 | 0 | return 0; |
859 | | |
860 | 0 | return EVP_MD_settable_ctx_params(ctx->md); |
861 | 0 | } |
862 | | |
863 | | const OSSL_DISPATCH ossl_ecdsa_signature_functions[] = { |
864 | | { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))ecdsa_newctx }, |
865 | | { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))ecdsa_sign_init }, |
866 | | { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))ecdsa_sign }, |
867 | | { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))ecdsa_verify_init }, |
868 | | { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))ecdsa_verify }, |
869 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, |
870 | | (void (*)(void))ecdsa_digest_sign_init }, |
871 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, |
872 | | (void (*)(void))ecdsa_digest_signverify_update }, |
873 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, |
874 | | (void (*)(void))ecdsa_digest_sign_final }, |
875 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, |
876 | | (void (*)(void))ecdsa_digest_verify_init }, |
877 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE, |
878 | | (void (*)(void))ecdsa_digest_signverify_update }, |
879 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL, |
880 | | (void (*)(void))ecdsa_digest_verify_final }, |
881 | | { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))ecdsa_freectx }, |
882 | | { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))ecdsa_dupctx }, |
883 | | { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))ecdsa_get_ctx_params }, |
884 | | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, |
885 | | (void (*)(void))ecdsa_gettable_ctx_params }, |
886 | | { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))ecdsa_set_ctx_params }, |
887 | | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, |
888 | | (void (*)(void))ecdsa_settable_ctx_params }, |
889 | | { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS, |
890 | | (void (*)(void))ecdsa_get_ctx_md_params }, |
891 | | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS, |
892 | | (void (*)(void))ecdsa_gettable_ctx_md_params }, |
893 | | { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS, |
894 | | (void (*)(void))ecdsa_set_ctx_md_params }, |
895 | | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS, |
896 | | (void (*)(void))ecdsa_settable_ctx_md_params }, |
897 | | OSSL_DISPATCH_END |
898 | | }; |
899 | | |
900 | | /* ------------------------------------------------------------------ */ |
901 | | |
902 | | /* |
903 | | * So called sigalgs (composite ECDSA+hash) implemented below. They |
904 | | * are pretty much hard coded. |
905 | | */ |
906 | | |
907 | | static OSSL_FUNC_signature_query_key_types_fn ecdsa_sigalg_query_key_types; |
908 | | static OSSL_FUNC_signature_settable_ctx_params_fn ecdsa_sigalg_settable_ctx_params; |
909 | | static OSSL_FUNC_signature_set_ctx_params_fn ecdsa_sigalg_set_ctx_params; |
910 | | |
911 | | /* |
912 | | * ecdsa_sigalg_signverify_init() is almost like ecdsa_digest_signverify_init(), |
913 | | * just doesn't allow fetching an MD from whatever the user chooses. |
914 | | */ |
915 | | static int ecdsa_sigalg_signverify_init(void *vctx, void *vec, |
916 | | OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params, |
917 | | const OSSL_PARAM params[], |
918 | | const char *mdname, |
919 | | int operation, const char *desc) |
920 | 0 | { |
921 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
922 | |
|
923 | 0 | if (!ossl_prov_is_running()) |
924 | 0 | return 0; |
925 | | |
926 | 0 | if (!ecdsa_signverify_init(vctx, vec, set_ctx_params, params, operation, |
927 | 0 | desc)) |
928 | 0 | return 0; |
929 | | |
930 | 0 | if (!ecdsa_setup_md(ctx, mdname, NULL, desc)) |
931 | 0 | return 0; |
932 | | |
933 | 0 | ctx->flag_sigalg = 1; |
934 | 0 | ctx->flag_allow_md = 0; |
935 | |
|
936 | 0 | if (ctx->mdctx == NULL) { |
937 | 0 | ctx->mdctx = EVP_MD_CTX_new(); |
938 | 0 | if (ctx->mdctx == NULL) |
939 | 0 | goto error; |
940 | 0 | } |
941 | | |
942 | 0 | if (!EVP_DigestInit_ex2(ctx->mdctx, ctx->md, params)) |
943 | 0 | goto error; |
944 | | |
945 | 0 | return 1; |
946 | | |
947 | 0 | error: |
948 | 0 | EVP_MD_CTX_free(ctx->mdctx); |
949 | 0 | ctx->mdctx = NULL; |
950 | 0 | return 0; |
951 | 0 | } |
952 | | |
953 | | static const char **ecdsa_sigalg_query_key_types(void) |
954 | 0 | { |
955 | 0 | static const char *keytypes[] = { "EC", NULL }; |
956 | |
|
957 | 0 | return keytypes; |
958 | 0 | } |
959 | | |
960 | | static const OSSL_PARAM settable_sigalg_ctx_params[] = { |
961 | | OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_SIGNATURE, NULL, 0), |
962 | | ECDSA_COMMON_SETTABLE_CTX_PARAMS |
963 | | }; |
964 | | |
965 | | static const OSSL_PARAM *ecdsa_sigalg_settable_ctx_params(void *vctx, |
966 | | ossl_unused void *provctx) |
967 | 3 | { |
968 | 3 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
969 | | |
970 | 3 | if (ctx != NULL && ctx->operation == EVP_PKEY_OP_VERIFYMSG) |
971 | 0 | return settable_sigalg_ctx_params; |
972 | 3 | return NULL; |
973 | 3 | } |
974 | | |
975 | | static int ecdsa_sigalg_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
976 | 213 | { |
977 | 213 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
978 | 213 | const OSSL_PARAM *p; |
979 | 213 | int ret; |
980 | | |
981 | 213 | if ((ret = ecdsa_common_set_ctx_params(ctx, params)) <= 0) |
982 | 0 | return ret; |
983 | | |
984 | 213 | if (params == NULL) |
985 | 0 | return 1; |
986 | | |
987 | 213 | if (ctx->operation == EVP_PKEY_OP_VERIFYMSG) { |
988 | 213 | p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_SIGNATURE); |
989 | 213 | if (p != NULL) { |
990 | 213 | OPENSSL_free(ctx->sig); |
991 | 213 | ctx->sig = NULL; |
992 | 213 | ctx->siglen = 0; |
993 | 213 | if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->sig, |
994 | 213 | 0, &ctx->siglen)) |
995 | 2 | return 0; |
996 | 213 | } |
997 | 213 | } |
998 | 211 | return 1; |
999 | 213 | } |
1000 | | |
1001 | | #define IMPL_ECDSA_SIGALG(md, MD) \ |
1002 | | static OSSL_FUNC_signature_sign_init_fn ecdsa_##md##_sign_init; \ |
1003 | | static OSSL_FUNC_signature_sign_message_init_fn \ |
1004 | | ecdsa_##md##_sign_message_init; \ |
1005 | | static OSSL_FUNC_signature_verify_init_fn ecdsa_##md##_verify_init; \ |
1006 | | static OSSL_FUNC_signature_verify_message_init_fn \ |
1007 | | ecdsa_##md##_verify_message_init; \ |
1008 | | \ |
1009 | | static int \ |
1010 | | ecdsa_##md##_sign_init(void *vctx, void *vec, \ |
1011 | | const OSSL_PARAM params[]) \ |
1012 | 0 | { \ |
1013 | 0 | static const char desc[] = "ECDSA-" #MD " Sign Init"; \ |
1014 | 0 | \ |
1015 | 0 | return ecdsa_sigalg_signverify_init(vctx, vec, \ |
1016 | 0 | ecdsa_sigalg_set_ctx_params, \ |
1017 | 0 | params, #MD, \ |
1018 | 0 | EVP_PKEY_OP_SIGN, \ |
1019 | 0 | desc); \ |
1020 | 0 | } \ Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha1_sign_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha224_sign_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha256_sign_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha384_sign_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha512_sign_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha3_224_sign_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha3_256_sign_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha3_384_sign_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha3_512_sign_init |
1021 | | \ |
1022 | | static int \ |
1023 | | ecdsa_##md##_sign_message_init(void *vctx, void *vec, \ |
1024 | | const OSSL_PARAM params[]) \ |
1025 | 0 | { \ |
1026 | 0 | static const char desc[] = "ECDSA-" #MD " Sign Message Init"; \ |
1027 | 0 | \ |
1028 | 0 | return ecdsa_sigalg_signverify_init(vctx, vec, \ |
1029 | 0 | ecdsa_sigalg_set_ctx_params, \ |
1030 | 0 | params, #MD, \ |
1031 | 0 | EVP_PKEY_OP_SIGNMSG, \ |
1032 | 0 | desc); \ |
1033 | 0 | } \ Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha1_sign_message_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha224_sign_message_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha256_sign_message_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha384_sign_message_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha512_sign_message_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha3_224_sign_message_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha3_256_sign_message_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha3_384_sign_message_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha3_512_sign_message_init |
1034 | | \ |
1035 | | static int \ |
1036 | | ecdsa_##md##_verify_init(void *vctx, void *vec, \ |
1037 | | const OSSL_PARAM params[]) \ |
1038 | 0 | { \ |
1039 | 0 | static const char desc[] = "ECDSA-" #MD " Verify Init"; \ |
1040 | 0 | \ |
1041 | 0 | return ecdsa_sigalg_signverify_init(vctx, vec, \ |
1042 | 0 | ecdsa_sigalg_set_ctx_params, \ |
1043 | 0 | params, #MD, \ |
1044 | 0 | EVP_PKEY_OP_VERIFY, \ |
1045 | 0 | desc); \ |
1046 | 0 | } \ Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha1_verify_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha224_verify_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha256_verify_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha384_verify_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha512_verify_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha3_224_verify_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha3_256_verify_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha3_384_verify_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha3_512_verify_init |
1047 | | \ |
1048 | | static int \ |
1049 | | ecdsa_##md##_verify_message_init(void *vctx, void *vec, \ |
1050 | | const OSSL_PARAM params[]) \ |
1051 | 0 | { \ |
1052 | 0 | static const char desc[] = "ECDSA-" #MD " Verify Message Init"; \ |
1053 | 0 | \ |
1054 | 0 | return ecdsa_sigalg_signverify_init(vctx, vec, \ |
1055 | 0 | ecdsa_sigalg_set_ctx_params, \ |
1056 | 0 | params, #MD, \ |
1057 | 0 | EVP_PKEY_OP_VERIFYMSG, \ |
1058 | 0 | desc); \ |
1059 | 0 | } \ Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha1_verify_message_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha224_verify_message_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha256_verify_message_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha384_verify_message_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha512_verify_message_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha3_224_verify_message_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha3_256_verify_message_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha3_384_verify_message_init Unexecuted instantiation: ecdsa_sig.c:ecdsa_sha3_512_verify_message_init |
1060 | | \ |
1061 | | const OSSL_DISPATCH ossl_ecdsa_##md##_signature_functions[] = { \ |
1062 | | { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))ecdsa_newctx }, \ |
1063 | | { OSSL_FUNC_SIGNATURE_SIGN_INIT, \ |
1064 | | (void (*)(void))ecdsa_##md##_sign_init }, \ |
1065 | | { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))ecdsa_sign }, \ |
1066 | | { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_INIT, \ |
1067 | | (void (*)(void))ecdsa_##md##_sign_message_init }, \ |
1068 | | { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_UPDATE, \ |
1069 | | (void (*)(void))ecdsa_signverify_message_update }, \ |
1070 | | { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_FINAL, \ |
1071 | | (void (*)(void))ecdsa_sign_message_final }, \ |
1072 | | { OSSL_FUNC_SIGNATURE_VERIFY_INIT, \ |
1073 | | (void (*)(void))ecdsa_##md##_verify_init }, \ |
1074 | | { OSSL_FUNC_SIGNATURE_VERIFY, \ |
1075 | | (void (*)(void))ecdsa_verify }, \ |
1076 | | { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_INIT, \ |
1077 | | (void (*)(void))ecdsa_##md##_verify_message_init }, \ |
1078 | | { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_UPDATE, \ |
1079 | | (void (*)(void))ecdsa_signverify_message_update }, \ |
1080 | | { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_FINAL, \ |
1081 | | (void (*)(void))ecdsa_verify_message_final }, \ |
1082 | | { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))ecdsa_freectx }, \ |
1083 | | { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))ecdsa_dupctx }, \ |
1084 | | { OSSL_FUNC_SIGNATURE_QUERY_KEY_TYPES, \ |
1085 | | (void (*)(void))ecdsa_sigalg_query_key_types }, \ |
1086 | | { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, \ |
1087 | | (void (*)(void))ecdsa_get_ctx_params }, \ |
1088 | | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, \ |
1089 | | (void (*)(void))ecdsa_gettable_ctx_params }, \ |
1090 | | { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, \ |
1091 | | (void (*)(void))ecdsa_sigalg_set_ctx_params }, \ |
1092 | | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, \ |
1093 | | (void (*)(void))ecdsa_sigalg_settable_ctx_params }, \ |
1094 | | OSSL_DISPATCH_END \ |
1095 | | } |
1096 | | |
1097 | | IMPL_ECDSA_SIGALG(sha1, SHA1); |
1098 | | IMPL_ECDSA_SIGALG(sha224, SHA2-224); |
1099 | | IMPL_ECDSA_SIGALG(sha256, SHA2-256); |
1100 | | IMPL_ECDSA_SIGALG(sha384, SHA2-384); |
1101 | | IMPL_ECDSA_SIGALG(sha512, SHA2-512); |
1102 | | IMPL_ECDSA_SIGALG(sha3_224, SHA3-224); |
1103 | | IMPL_ECDSA_SIGALG(sha3_256, SHA3-256); |
1104 | | IMPL_ECDSA_SIGALG(sha3_384, SHA3-384); |
1105 | | IMPL_ECDSA_SIGALG(sha3_512, SHA3-512); |