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