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