/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)) == 0); |
238 | | |
239 | | if (!ossl_fips_ind_digest_sign_check(OSSL_FIPS_IND_GET(ctx), |
240 | | OSSL_FIPS_IND_SETTABLE1, |
241 | | ctx->libctx, |
242 | | md_nid, sha1_allowed, 0, desc, |
243 | | ossl_fips_config_signature_digest_check)) |
244 | | goto err; |
245 | | } |
246 | | #endif |
247 | | |
248 | 0 | if (!ctx->flag_allow_md) { |
249 | 0 | if (ctx->mdname[0] != '\0' && !EVP_MD_is_a(md, ctx->mdname)) { |
250 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED, |
251 | 0 | "digest %s != %s", mdname, ctx->mdname); |
252 | 0 | goto err; |
253 | 0 | } |
254 | 0 | EVP_MD_free(md); |
255 | 0 | return 1; |
256 | 0 | } |
257 | | |
258 | 0 | EVP_MD_CTX_free(ctx->mdctx); |
259 | 0 | EVP_MD_free(ctx->md); |
260 | |
|
261 | 0 | ctx->aid_len = 0; |
262 | 0 | #ifndef FIPS_MODULE |
263 | 0 | if (md_nid != NID_undef) { |
264 | | #else |
265 | | { |
266 | | #endif |
267 | 0 | if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf)) |
268 | 0 | && ossl_DER_w_algorithmIdentifier_ECDSA_with_MD(&pkt, -1, ctx->ec, |
269 | 0 | md_nid) |
270 | 0 | && WPACKET_finish(&pkt)) { |
271 | 0 | WPACKET_get_total_written(&pkt, &ctx->aid_len); |
272 | 0 | aid = WPACKET_get_curr(&pkt); |
273 | 0 | } |
274 | 0 | WPACKET_cleanup(&pkt); |
275 | 0 | if (aid != NULL && ctx->aid_len != 0) |
276 | 0 | memmove(ctx->aid_buf, aid, ctx->aid_len); |
277 | 0 | } |
278 | |
|
279 | 0 | ctx->mdctx = NULL; |
280 | 0 | ctx->md = md; |
281 | 0 | ctx->mdsize = (size_t)md_size; |
282 | 0 | OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname)); |
283 | |
|
284 | 0 | return 1; |
285 | 0 | err: |
286 | 0 | EVP_MD_free(md); |
287 | 0 | return 0; |
288 | 0 | } |
289 | | |
290 | | static int |
291 | | ecdsa_signverify_init(PROV_ECDSA_CTX *ctx, void *ec, |
292 | | OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params, |
293 | | const OSSL_PARAM params[], int operation, |
294 | | const char *desc) |
295 | 0 | { |
296 | 0 | if (!ossl_prov_is_running() |
297 | 0 | || ctx == NULL) |
298 | 0 | return 0; |
299 | | |
300 | 0 | if (ec == NULL && ctx->ec == NULL) { |
301 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
302 | 0 | return 0; |
303 | 0 | } |
304 | | |
305 | 0 | if (ec != NULL) { |
306 | 0 | if (!EC_KEY_up_ref(ec)) |
307 | 0 | return 0; |
308 | 0 | EC_KEY_free(ctx->ec); |
309 | 0 | ctx->ec = ec; |
310 | 0 | } |
311 | | |
312 | 0 | ctx->operation = operation; |
313 | |
|
314 | 0 | OSSL_FIPS_IND_SET_APPROVED(ctx) |
315 | 0 | if (!set_ctx_params(ctx, params)) |
316 | 0 | return 0; |
317 | | #ifdef FIPS_MODULE |
318 | | if (!ossl_fips_ind_ec_key_check(OSSL_FIPS_IND_GET(ctx), |
319 | | OSSL_FIPS_IND_SETTABLE0, ctx->libctx, |
320 | | EC_KEY_get0_group(ctx->ec), desc, |
321 | | (operation & (EVP_PKEY_OP_SIGN |
322 | | | 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] = |
478 | 0 | OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_SIGNATURE, |
479 | 0 | (unsigned char *)sig, siglen); |
480 | 0 | params[1] = OSSL_PARAM_construct_end(); |
481 | 0 | return ecdsa_sigalg_set_ctx_params(ctx, params); |
482 | 0 | } |
483 | | |
484 | | static int ecdsa_verify_message_final(void *vctx) |
485 | 0 | { |
486 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
487 | 0 | unsigned char digest[EVP_MAX_MD_SIZE]; |
488 | 0 | unsigned int dlen = 0; |
489 | |
|
490 | 0 | if (!ossl_prov_is_running() || ctx == NULL || ctx->mdctx == NULL) |
491 | 0 | return 0; |
492 | | |
493 | | /* |
494 | | * The digests used here are all known (see ecdsa_get_md_nid()), so they |
495 | | * should not exceed the internal buffer size of EVP_MAX_MD_SIZE. |
496 | | */ |
497 | 0 | if (!EVP_DigestFinal_ex(ctx->mdctx, digest, &dlen)) |
498 | 0 | return 0; |
499 | | |
500 | 0 | return ecdsa_verify_directly(vctx, ctx->sig, ctx->siglen, |
501 | 0 | digest, dlen); |
502 | 0 | } |
503 | | |
504 | | /* |
505 | | * If verifying a message, digest tbs and verify the result. |
506 | | * Otherwise, verify tbs directly. |
507 | | */ |
508 | | static int ecdsa_verify(void *vctx, |
509 | | const unsigned char *sig, size_t siglen, |
510 | | const unsigned char *tbs, size_t tbslen) |
511 | 0 | { |
512 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
513 | |
|
514 | 0 | if (ctx->operation == EVP_PKEY_OP_VERIFYMSG) { |
515 | 0 | if (ecdsa_verify_set_sig(ctx, sig, siglen) <= 0) |
516 | 0 | return 0; |
517 | 0 | if (ecdsa_signverify_message_update(ctx, tbs, tbslen) <= 0) |
518 | 0 | return 0; |
519 | 0 | return ecdsa_verify_message_final(ctx); |
520 | 0 | } |
521 | 0 | return ecdsa_verify_directly(ctx, sig, siglen, tbs, tbslen); |
522 | 0 | } |
523 | | |
524 | | /* DigestSign/DigestVerify wrappers */ |
525 | | |
526 | | static int ecdsa_digest_signverify_init(void *vctx, const char *mdname, |
527 | | void *ec, const OSSL_PARAM params[], |
528 | | int operation, const char *desc) |
529 | 0 | { |
530 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
531 | |
|
532 | 0 | if (!ossl_prov_is_running()) |
533 | 0 | return 0; |
534 | | |
535 | | #ifdef FIPS_MODULE |
536 | | ctx->verify_message = 1; |
537 | | #endif |
538 | 0 | if (!ecdsa_signverify_init(vctx, ec, ecdsa_set_ctx_params, params, |
539 | 0 | operation, desc)) |
540 | 0 | return 0; |
541 | | |
542 | 0 | if (mdname != NULL |
543 | | /* was ecdsa_setup_md already called in ecdsa_signverify_init()? */ |
544 | 0 | && (mdname[0] == '\0' || OPENSSL_strcasecmp(ctx->mdname, mdname) != 0) |
545 | 0 | && !ecdsa_setup_md(ctx, mdname, NULL, desc)) |
546 | 0 | return 0; |
547 | | |
548 | 0 | ctx->flag_allow_md = 0; |
549 | |
|
550 | 0 | if (ctx->mdctx == NULL) { |
551 | 0 | ctx->mdctx = EVP_MD_CTX_new(); |
552 | 0 | if (ctx->mdctx == NULL) |
553 | 0 | goto error; |
554 | 0 | } |
555 | | |
556 | 0 | if (!EVP_DigestInit_ex2(ctx->mdctx, ctx->md, params)) |
557 | 0 | goto error; |
558 | 0 | return 1; |
559 | 0 | error: |
560 | 0 | EVP_MD_CTX_free(ctx->mdctx); |
561 | 0 | ctx->mdctx = NULL; |
562 | 0 | return 0; |
563 | 0 | } |
564 | | |
565 | | static int ecdsa_digest_sign_init(void *vctx, const char *mdname, void *ec, |
566 | | const OSSL_PARAM params[]) |
567 | 0 | { |
568 | 0 | return ecdsa_digest_signverify_init(vctx, mdname, ec, params, |
569 | 0 | EVP_PKEY_OP_SIGNMSG, |
570 | 0 | "ECDSA Digest Sign Init"); |
571 | 0 | } |
572 | | |
573 | | static int ecdsa_digest_signverify_update(void *vctx, const unsigned char *data, |
574 | | size_t datalen) |
575 | 0 | { |
576 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
577 | |
|
578 | 0 | if (ctx == NULL || ctx->mdctx == NULL) |
579 | 0 | return 0; |
580 | | /* Sigalg implementations shouldn't do digest_sign */ |
581 | 0 | if (ctx->flag_sigalg) |
582 | 0 | return 0; |
583 | | |
584 | 0 | return ecdsa_signverify_message_update(vctx, data, datalen); |
585 | 0 | } |
586 | | |
587 | | int ecdsa_digest_sign_final(void *vctx, unsigned char *sig, size_t *siglen, |
588 | | size_t sigsize) |
589 | 0 | { |
590 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
591 | 0 | int ok = 0; |
592 | |
|
593 | 0 | if (ctx == NULL) |
594 | 0 | return 0; |
595 | | /* Sigalg implementations shouldn't do digest_sign */ |
596 | 0 | if (ctx->flag_sigalg) |
597 | 0 | return 0; |
598 | | |
599 | 0 | ok = ecdsa_sign_message_final(ctx, sig, siglen, sigsize); |
600 | |
|
601 | 0 | ctx->flag_allow_md = 1; |
602 | |
|
603 | 0 | return ok; |
604 | 0 | } |
605 | | |
606 | | static int ecdsa_digest_verify_init(void *vctx, const char *mdname, void *ec, |
607 | | const OSSL_PARAM params[]) |
608 | 0 | { |
609 | 0 | return ecdsa_digest_signverify_init(vctx, mdname, ec, params, |
610 | 0 | EVP_PKEY_OP_VERIFYMSG, |
611 | 0 | "ECDSA Digest Verify Init"); |
612 | 0 | } |
613 | | |
614 | | int ecdsa_digest_verify_final(void *vctx, const unsigned char *sig, |
615 | | size_t siglen) |
616 | 0 | { |
617 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
618 | 0 | int ok = 0; |
619 | |
|
620 | 0 | if (!ossl_prov_is_running() || ctx == NULL || ctx->mdctx == NULL) |
621 | 0 | return 0; |
622 | | |
623 | | /* Sigalg implementations shouldn't do digest_verify */ |
624 | 0 | if (ctx->flag_sigalg) |
625 | 0 | return 0; |
626 | | |
627 | 0 | if (ecdsa_verify_set_sig(ctx, sig, siglen)) |
628 | 0 | ok = ecdsa_verify_message_final(ctx); |
629 | |
|
630 | 0 | ctx->flag_allow_md = 1; |
631 | |
|
632 | 0 | return ok; |
633 | 0 | } |
634 | | |
635 | | static void ecdsa_freectx(void *vctx) |
636 | 0 | { |
637 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
638 | |
|
639 | 0 | EVP_MD_CTX_free(ctx->mdctx); |
640 | 0 | EVP_MD_free(ctx->md); |
641 | 0 | OPENSSL_free(ctx->propq); |
642 | 0 | OPENSSL_free(ctx->sig); |
643 | 0 | EC_KEY_free(ctx->ec); |
644 | 0 | BN_clear_free(ctx->kinv); |
645 | 0 | BN_clear_free(ctx->r); |
646 | 0 | OPENSSL_free(ctx); |
647 | 0 | } |
648 | | |
649 | | static void *ecdsa_dupctx(void *vctx) |
650 | 0 | { |
651 | 0 | PROV_ECDSA_CTX *srcctx = (PROV_ECDSA_CTX *)vctx; |
652 | 0 | PROV_ECDSA_CTX *dstctx; |
653 | |
|
654 | 0 | if (!ossl_prov_is_running()) |
655 | 0 | return NULL; |
656 | | |
657 | 0 | dstctx = OPENSSL_zalloc(sizeof(*srcctx)); |
658 | 0 | if (dstctx == NULL) |
659 | 0 | return NULL; |
660 | | |
661 | 0 | *dstctx = *srcctx; |
662 | 0 | dstctx->ec = NULL; |
663 | 0 | dstctx->propq = NULL; |
664 | |
|
665 | 0 | if (srcctx->ec != NULL && !EC_KEY_up_ref(srcctx->ec)) |
666 | 0 | goto err; |
667 | | /* Test KATS should not need to be supported */ |
668 | 0 | if (srcctx->kinv != NULL || srcctx->r != NULL) |
669 | 0 | goto err; |
670 | 0 | dstctx->ec = srcctx->ec; |
671 | |
|
672 | 0 | if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md)) |
673 | 0 | goto err; |
674 | 0 | dstctx->md = srcctx->md; |
675 | |
|
676 | 0 | if (srcctx->mdctx != NULL) { |
677 | 0 | dstctx->mdctx = EVP_MD_CTX_new(); |
678 | 0 | if (dstctx->mdctx == NULL |
679 | 0 | || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx)) |
680 | 0 | goto err; |
681 | 0 | } |
682 | | |
683 | 0 | if (srcctx->propq != NULL) { |
684 | 0 | dstctx->propq = OPENSSL_strdup(srcctx->propq); |
685 | 0 | if (dstctx->propq == NULL) |
686 | 0 | goto err; |
687 | 0 | } |
688 | | |
689 | 0 | return dstctx; |
690 | 0 | err: |
691 | 0 | ecdsa_freectx(dstctx); |
692 | 0 | return NULL; |
693 | 0 | } |
694 | | |
695 | | static int ecdsa_get_ctx_params(void *vctx, OSSL_PARAM *params) |
696 | 0 | { |
697 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
698 | 0 | struct ecdsa_get_ctx_params_st p; |
699 | |
|
700 | 0 | if (ctx == NULL || !ecdsa_get_ctx_params_decoder(params, &p)) |
701 | 0 | return 0; |
702 | | |
703 | 0 | if (p.algid != NULL |
704 | 0 | && !OSSL_PARAM_set_octet_string(p.algid, |
705 | 0 | ctx->aid_len == 0 ? NULL : ctx->aid_buf, |
706 | 0 | ctx->aid_len)) |
707 | 0 | return 0; |
708 | | |
709 | 0 | if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, ctx->mdsize)) |
710 | 0 | return 0; |
711 | | |
712 | 0 | if (p.digest != NULL |
713 | 0 | && !OSSL_PARAM_set_utf8_string(p.digest, ctx->md == NULL |
714 | 0 | ? ctx->mdname |
715 | 0 | : EVP_MD_get0_name(ctx->md))) |
716 | 0 | return 0; |
717 | | |
718 | 0 | if (p.nonce != NULL && !OSSL_PARAM_set_uint(p.nonce, ctx->nonce_type)) |
719 | 0 | return 0; |
720 | | |
721 | | #ifdef FIPS_MODULE |
722 | | if (p.verify != NULL && !OSSL_PARAM_set_uint(p.verify, ctx->verify_message)) |
723 | | return 0; |
724 | | #endif |
725 | | |
726 | 0 | if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind)) |
727 | 0 | return 0; |
728 | 0 | return 1; |
729 | 0 | } |
730 | | |
731 | | static const OSSL_PARAM *ecdsa_gettable_ctx_params(ossl_unused void *vctx, |
732 | | ossl_unused void *provctx) |
733 | 0 | { |
734 | 0 | return ecdsa_get_ctx_params_list; |
735 | 0 | } |
736 | | |
737 | | /** |
738 | | * @brief Set up common params for ecdsa_set_ctx_params and |
739 | | * ecdsa_sigalg_set_ctx_params. The caller is responsible for checking |vctx| is |
740 | | * not NULL and |params| is not empty. |
741 | | */ |
742 | | static int ecdsa_common_set_ctx_params(PROV_ECDSA_CTX *ctx, |
743 | | const struct ecdsa_all_set_ctx_params_st *p) |
744 | 0 | { |
745 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, |
746 | 0 | p->ind_k)) |
747 | 0 | return 0; |
748 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE1, |
749 | 0 | p->ind_d)) |
750 | 0 | return 0; |
751 | | |
752 | | #if !defined(OPENSSL_NO_ACVP_TESTS) |
753 | | if (p->kat != NULL && !OSSL_PARAM_get_uint(p->kat, &ctx->kattest)) |
754 | | return 0; |
755 | | #endif |
756 | | |
757 | 0 | if (p->nonce != NULL && !OSSL_PARAM_get_uint(p->nonce, &ctx->nonce_type)) |
758 | 0 | return 0; |
759 | 0 | return 1; |
760 | 0 | } |
761 | | |
762 | | static int ecdsa_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
763 | 0 | { |
764 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
765 | 0 | struct ecdsa_all_set_ctx_params_st p; |
766 | 0 | size_t mdsize = 0; |
767 | 0 | int ret; |
768 | |
|
769 | 0 | if (ctx == NULL || !ecdsa_set_ctx_params_decoder(params, &p)) |
770 | 0 | return 0; |
771 | | |
772 | 0 | if ((ret = ecdsa_common_set_ctx_params(ctx, &p)) <= 0) |
773 | 0 | return ret; |
774 | | |
775 | 0 | if (p.digest != NULL) { |
776 | 0 | char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname; |
777 | 0 | char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops; |
778 | |
|
779 | 0 | if (!OSSL_PARAM_get_utf8_string(p.digest, &pmdname, sizeof(mdname))) |
780 | 0 | return 0; |
781 | 0 | if (p.propq != NULL |
782 | 0 | && !OSSL_PARAM_get_utf8_string(p.propq, &pmdprops, sizeof(mdprops))) |
783 | 0 | return 0; |
784 | 0 | if (!ecdsa_setup_md(ctx, mdname, mdprops, "ECDSA Set Ctx")) |
785 | 0 | return 0; |
786 | 0 | } |
787 | | |
788 | 0 | if (p.size != NULL) { |
789 | 0 | if (!OSSL_PARAM_get_size_t(p.size, &mdsize) |
790 | 0 | || (!ctx->flag_allow_md && mdsize != ctx->mdsize)) |
791 | 0 | return 0; |
792 | 0 | ctx->mdsize = mdsize; |
793 | 0 | } |
794 | 0 | return 1; |
795 | 0 | } |
796 | | |
797 | | static const OSSL_PARAM *ecdsa_settable_ctx_params(void *vctx, |
798 | | ossl_unused void *provctx) |
799 | 0 | { |
800 | 0 | return ecdsa_set_ctx_params_list; |
801 | 0 | } |
802 | | |
803 | | static int ecdsa_get_ctx_md_params(void *vctx, OSSL_PARAM *params) |
804 | 0 | { |
805 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
806 | |
|
807 | 0 | if (ctx->mdctx == NULL) |
808 | 0 | return 0; |
809 | | |
810 | 0 | return EVP_MD_CTX_get_params(ctx->mdctx, params); |
811 | 0 | } |
812 | | |
813 | | static const OSSL_PARAM *ecdsa_gettable_ctx_md_params(void *vctx) |
814 | 0 | { |
815 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
816 | |
|
817 | 0 | if (ctx->md == NULL) |
818 | 0 | return 0; |
819 | | |
820 | 0 | return EVP_MD_gettable_ctx_params(ctx->md); |
821 | 0 | } |
822 | | |
823 | | static int ecdsa_set_ctx_md_params(void *vctx, const 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_set_params(ctx->mdctx, params); |
831 | 0 | } |
832 | | |
833 | | static const OSSL_PARAM *ecdsa_settable_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_settable_ctx_params(ctx->md); |
841 | 0 | } |
842 | | |
843 | | const OSSL_DISPATCH ossl_ecdsa_signature_functions[] = { |
844 | | { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))ecdsa_newctx }, |
845 | | { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))ecdsa_sign_init }, |
846 | | { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))ecdsa_sign }, |
847 | | { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))ecdsa_verify_init }, |
848 | | { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))ecdsa_verify }, |
849 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, |
850 | | (void (*)(void))ecdsa_digest_sign_init }, |
851 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, |
852 | | (void (*)(void))ecdsa_digest_signverify_update }, |
853 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, |
854 | | (void (*)(void))ecdsa_digest_sign_final }, |
855 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, |
856 | | (void (*)(void))ecdsa_digest_verify_init }, |
857 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE, |
858 | | (void (*)(void))ecdsa_digest_signverify_update }, |
859 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL, |
860 | | (void (*)(void))ecdsa_digest_verify_final }, |
861 | | { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))ecdsa_freectx }, |
862 | | { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))ecdsa_dupctx }, |
863 | | { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))ecdsa_get_ctx_params }, |
864 | | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, |
865 | | (void (*)(void))ecdsa_gettable_ctx_params }, |
866 | | { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))ecdsa_set_ctx_params }, |
867 | | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, |
868 | | (void (*)(void))ecdsa_settable_ctx_params }, |
869 | | { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS, |
870 | | (void (*)(void))ecdsa_get_ctx_md_params }, |
871 | | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS, |
872 | | (void (*)(void))ecdsa_gettable_ctx_md_params }, |
873 | | { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS, |
874 | | (void (*)(void))ecdsa_set_ctx_md_params }, |
875 | | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS, |
876 | | (void (*)(void))ecdsa_settable_ctx_md_params }, |
877 | | OSSL_DISPATCH_END |
878 | | }; |
879 | | |
880 | | /* ------------------------------------------------------------------ */ |
881 | | |
882 | | /* |
883 | | * So called sigalgs (composite ECDSA+hash) implemented below. They |
884 | | * are pretty much hard coded. |
885 | | */ |
886 | | |
887 | | static OSSL_FUNC_signature_query_key_types_fn ecdsa_sigalg_query_key_types; |
888 | | static OSSL_FUNC_signature_settable_ctx_params_fn ecdsa_sigalg_settable_ctx_params; |
889 | | static OSSL_FUNC_signature_set_ctx_params_fn ecdsa_sigalg_set_ctx_params; |
890 | | |
891 | | /* |
892 | | * ecdsa_sigalg_signverify_init() is almost like ecdsa_digest_signverify_init(), |
893 | | * just doesn't allow fetching an MD from whatever the user chooses. |
894 | | */ |
895 | | static int ecdsa_sigalg_signverify_init(void *vctx, void *vec, |
896 | | OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params, |
897 | | const OSSL_PARAM params[], |
898 | | const char *mdname, |
899 | | int operation, const char *desc) |
900 | 0 | { |
901 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
902 | |
|
903 | 0 | if (!ossl_prov_is_running()) |
904 | 0 | return 0; |
905 | | |
906 | 0 | if (!ecdsa_signverify_init(vctx, vec, set_ctx_params, params, operation, |
907 | 0 | desc)) |
908 | 0 | return 0; |
909 | | |
910 | 0 | if (!ecdsa_setup_md(ctx, mdname, NULL, desc)) |
911 | 0 | return 0; |
912 | | |
913 | 0 | ctx->flag_sigalg = 1; |
914 | 0 | ctx->flag_allow_md = 0; |
915 | |
|
916 | 0 | if (ctx->mdctx == NULL) { |
917 | 0 | ctx->mdctx = EVP_MD_CTX_new(); |
918 | 0 | if (ctx->mdctx == NULL) |
919 | 0 | goto error; |
920 | 0 | } |
921 | | |
922 | 0 | if (!EVP_DigestInit_ex2(ctx->mdctx, ctx->md, params)) |
923 | 0 | goto error; |
924 | | |
925 | 0 | return 1; |
926 | | |
927 | 0 | error: |
928 | 0 | EVP_MD_CTX_free(ctx->mdctx); |
929 | 0 | ctx->mdctx = NULL; |
930 | 0 | return 0; |
931 | 0 | } |
932 | | |
933 | | static const char **ecdsa_sigalg_query_key_types(void) |
934 | 0 | { |
935 | 0 | static const char *keytypes[] = { "EC", NULL }; |
936 | |
|
937 | 0 | return keytypes; |
938 | 0 | } |
939 | | |
940 | | static const OSSL_PARAM *ecdsa_sigalg_settable_ctx_params(void *vctx, |
941 | | ossl_unused void *provctx) |
942 | 0 | { |
943 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
944 | |
|
945 | 0 | if (ctx != NULL && ctx->operation == EVP_PKEY_OP_VERIFYMSG) |
946 | 0 | return ecdsa_sigalg_set_ctx_params_list; |
947 | 0 | return NULL; |
948 | 0 | } |
949 | | |
950 | | static int ecdsa_sigalg_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
951 | 0 | { |
952 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
953 | 0 | struct ecdsa_all_set_ctx_params_st p; |
954 | 0 | int ret; |
955 | |
|
956 | 0 | if (ctx == NULL || !ecdsa_sigalg_set_ctx_params_decoder(params, &p)) |
957 | 0 | return 0; |
958 | | |
959 | 0 | if ((ret = ecdsa_common_set_ctx_params(ctx, &p)) <= 0) |
960 | 0 | return ret; |
961 | | |
962 | 0 | if (ctx->operation == EVP_PKEY_OP_VERIFYMSG) { |
963 | 0 | if (p.sig != NULL) { |
964 | 0 | OPENSSL_free(ctx->sig); |
965 | 0 | ctx->sig = NULL; |
966 | 0 | ctx->siglen = 0; |
967 | 0 | if (!OSSL_PARAM_get_octet_string(p.sig, (void **)&ctx->sig, |
968 | 0 | 0, &ctx->siglen)) |
969 | 0 | return 0; |
970 | 0 | } |
971 | 0 | } |
972 | 0 | return 1; |
973 | 0 | } |
974 | | |
975 | | #define IMPL_ECDSA_SIGALG(md, MD) \ |
976 | | static OSSL_FUNC_signature_sign_init_fn ecdsa_##md##_sign_init; \ |
977 | | static OSSL_FUNC_signature_sign_message_init_fn \ |
978 | | ecdsa_##md##_sign_message_init; \ |
979 | | static OSSL_FUNC_signature_verify_init_fn ecdsa_##md##_verify_init; \ |
980 | | static OSSL_FUNC_signature_verify_message_init_fn \ |
981 | | ecdsa_##md##_verify_message_init; \ |
982 | | \ |
983 | | static int \ |
984 | | ecdsa_##md##_sign_init(void *vctx, void *vec, \ |
985 | | const OSSL_PARAM params[]) \ |
986 | 0 | { \ |
987 | 0 | static const char desc[] = "ECDSA-" MD " Sign Init"; \ |
988 | 0 | \ |
989 | 0 | return ecdsa_sigalg_signverify_init(vctx, vec, \ |
990 | 0 | ecdsa_sigalg_set_ctx_params, \ |
991 | 0 | params, MD, \ |
992 | 0 | EVP_PKEY_OP_SIGN, \ |
993 | 0 | desc); \ |
994 | 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 |
995 | | \ |
996 | | static int \ |
997 | | ecdsa_##md##_sign_message_init(void *vctx, void *vec, \ |
998 | | const OSSL_PARAM params[]) \ |
999 | 0 | { \ |
1000 | 0 | static const char desc[] = "ECDSA-" MD " Sign Message Init"; \ |
1001 | 0 | \ |
1002 | 0 | return ecdsa_sigalg_signverify_init(vctx, vec, \ |
1003 | 0 | ecdsa_sigalg_set_ctx_params, \ |
1004 | 0 | params, MD, \ |
1005 | 0 | EVP_PKEY_OP_SIGNMSG, \ |
1006 | 0 | desc); \ |
1007 | 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 |
1008 | | \ |
1009 | | static int \ |
1010 | | ecdsa_##md##_verify_init(void *vctx, void *vec, \ |
1011 | | const OSSL_PARAM params[]) \ |
1012 | 0 | { \ |
1013 | 0 | static const char desc[] = "ECDSA-" MD " Verify 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_VERIFY, \ |
1019 | 0 | desc); \ |
1020 | 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 |
1021 | | \ |
1022 | | static int \ |
1023 | | ecdsa_##md##_verify_message_init(void *vctx, void *vec, \ |
1024 | | const OSSL_PARAM params[]) \ |
1025 | 0 | { \ |
1026 | 0 | static const char desc[] = "ECDSA-" MD " Verify 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_VERIFYMSG, \ |
1032 | 0 | desc); \ |
1033 | 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 |
1034 | | \ |
1035 | | const OSSL_DISPATCH ossl_ecdsa_##md##_signature_functions[] = { \ |
1036 | | { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))ecdsa_newctx }, \ |
1037 | | { OSSL_FUNC_SIGNATURE_SIGN_INIT, \ |
1038 | | (void (*)(void))ecdsa_##md##_sign_init }, \ |
1039 | | { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))ecdsa_sign }, \ |
1040 | | { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_INIT, \ |
1041 | | (void (*)(void))ecdsa_##md##_sign_message_init }, \ |
1042 | | { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_UPDATE, \ |
1043 | | (void (*)(void))ecdsa_signverify_message_update }, \ |
1044 | | { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_FINAL, \ |
1045 | | (void (*)(void))ecdsa_sign_message_final }, \ |
1046 | | { OSSL_FUNC_SIGNATURE_VERIFY_INIT, \ |
1047 | | (void (*)(void))ecdsa_##md##_verify_init }, \ |
1048 | | { OSSL_FUNC_SIGNATURE_VERIFY, \ |
1049 | | (void (*)(void))ecdsa_verify }, \ |
1050 | | { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_INIT, \ |
1051 | | (void (*)(void))ecdsa_##md##_verify_message_init }, \ |
1052 | | { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_UPDATE, \ |
1053 | | (void (*)(void))ecdsa_signverify_message_update }, \ |
1054 | | { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_FINAL, \ |
1055 | | (void (*)(void))ecdsa_verify_message_final }, \ |
1056 | | { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))ecdsa_freectx }, \ |
1057 | | { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))ecdsa_dupctx }, \ |
1058 | | { OSSL_FUNC_SIGNATURE_QUERY_KEY_TYPES, \ |
1059 | | (void (*)(void))ecdsa_sigalg_query_key_types }, \ |
1060 | | { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, \ |
1061 | | (void (*)(void))ecdsa_get_ctx_params }, \ |
1062 | | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, \ |
1063 | | (void (*)(void))ecdsa_gettable_ctx_params }, \ |
1064 | | { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, \ |
1065 | | (void (*)(void))ecdsa_sigalg_set_ctx_params }, \ |
1066 | | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, \ |
1067 | | (void (*)(void))ecdsa_sigalg_settable_ctx_params }, \ |
1068 | | OSSL_DISPATCH_END \ |
1069 | | } |
1070 | | |
1071 | | IMPL_ECDSA_SIGALG(sha1, "SHA1"); |
1072 | | IMPL_ECDSA_SIGALG(sha224, "SHA2-224"); |
1073 | | IMPL_ECDSA_SIGALG(sha256, "SHA2-256"); |
1074 | | IMPL_ECDSA_SIGALG(sha384, "SHA2-384"); |
1075 | | IMPL_ECDSA_SIGALG(sha512, "SHA2-512"); |
1076 | | IMPL_ECDSA_SIGALG(sha3_224, "SHA3-224"); |
1077 | | IMPL_ECDSA_SIGALG(sha3_256, "SHA3-256"); |
1078 | | IMPL_ECDSA_SIGALG(sha3_384, "SHA3-384"); |
1079 | | IMPL_ECDSA_SIGALG(sha3_512, "SHA3-512"); |