/src/openssl/providers/implementations/signature/ecdsa_sig.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2020-2023 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 "crypto/ec.h" |
34 | | #include "prov/der_ec.h" |
35 | | |
36 | | static OSSL_FUNC_signature_newctx_fn ecdsa_newctx; |
37 | | static OSSL_FUNC_signature_sign_init_fn ecdsa_sign_init; |
38 | | static OSSL_FUNC_signature_verify_init_fn ecdsa_verify_init; |
39 | | static OSSL_FUNC_signature_sign_fn ecdsa_sign; |
40 | | static OSSL_FUNC_signature_verify_fn ecdsa_verify; |
41 | | static OSSL_FUNC_signature_digest_sign_init_fn ecdsa_digest_sign_init; |
42 | | static OSSL_FUNC_signature_digest_sign_update_fn ecdsa_digest_signverify_update; |
43 | | static OSSL_FUNC_signature_digest_sign_final_fn ecdsa_digest_sign_final; |
44 | | static OSSL_FUNC_signature_digest_verify_init_fn ecdsa_digest_verify_init; |
45 | | static OSSL_FUNC_signature_digest_verify_update_fn ecdsa_digest_signverify_update; |
46 | | static OSSL_FUNC_signature_digest_verify_final_fn ecdsa_digest_verify_final; |
47 | | static OSSL_FUNC_signature_freectx_fn ecdsa_freectx; |
48 | | static OSSL_FUNC_signature_dupctx_fn ecdsa_dupctx; |
49 | | static OSSL_FUNC_signature_get_ctx_params_fn ecdsa_get_ctx_params; |
50 | | static OSSL_FUNC_signature_gettable_ctx_params_fn ecdsa_gettable_ctx_params; |
51 | | static OSSL_FUNC_signature_set_ctx_params_fn ecdsa_set_ctx_params; |
52 | | static OSSL_FUNC_signature_settable_ctx_params_fn ecdsa_settable_ctx_params; |
53 | | static OSSL_FUNC_signature_get_ctx_md_params_fn ecdsa_get_ctx_md_params; |
54 | | static OSSL_FUNC_signature_gettable_ctx_md_params_fn ecdsa_gettable_ctx_md_params; |
55 | | static OSSL_FUNC_signature_set_ctx_md_params_fn ecdsa_set_ctx_md_params; |
56 | | static OSSL_FUNC_signature_settable_ctx_md_params_fn ecdsa_settable_ctx_md_params; |
57 | | |
58 | | /* |
59 | | * What's passed as an actual key is defined by the KEYMGMT interface. |
60 | | * We happen to know that our KEYMGMT simply passes DSA structures, so |
61 | | * we use that here too. |
62 | | */ |
63 | | |
64 | | typedef struct { |
65 | | OSSL_LIB_CTX *libctx; |
66 | | char *propq; |
67 | | EC_KEY *ec; |
68 | | char mdname[OSSL_MAX_NAME_SIZE]; |
69 | | |
70 | | /* |
71 | | * Flag to determine if the hash function can be changed (1) or not (0) |
72 | | * Because it's dangerous to change during a DigestSign or DigestVerify |
73 | | * operation, this flag is cleared by their Init function, and set again |
74 | | * by their Final function. |
75 | | */ |
76 | | unsigned int flag_allow_md : 1; |
77 | | |
78 | | /* The Algorithm Identifier of the combined signature algorithm */ |
79 | | unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE]; |
80 | | unsigned char *aid; |
81 | | size_t aid_len; |
82 | | size_t mdsize; |
83 | | int operation; |
84 | | |
85 | | EVP_MD *md; |
86 | | EVP_MD_CTX *mdctx; |
87 | | /* |
88 | | * Internally used to cache the results of calling the EC group |
89 | | * sign_setup() methods which are then passed to the sign operation. |
90 | | * This is used by CAVS failure tests to terminate a loop if the signature |
91 | | * is not valid. |
92 | | * This could of also been done with a simple flag. |
93 | | */ |
94 | | BIGNUM *kinv; |
95 | | BIGNUM *r; |
96 | | #if !defined(OPENSSL_NO_ACVP_TESTS) |
97 | | /* |
98 | | * This indicates that KAT (CAVS) test is running. Externally an app will |
99 | | * override the random callback such that the generated private key and k |
100 | | * are known. |
101 | | * Normal operation will loop to choose a new k if the signature is not |
102 | | * valid - but for this mode of operation it forces a failure instead. |
103 | | */ |
104 | | unsigned int kattest; |
105 | | #endif |
106 | | /* If this is set then the generated k is not random */ |
107 | | unsigned int nonce_type; |
108 | | } PROV_ECDSA_CTX; |
109 | | |
110 | | static void *ecdsa_newctx(void *provctx, const char *propq) |
111 | 0 | { |
112 | 0 | PROV_ECDSA_CTX *ctx; |
113 | |
|
114 | 0 | if (!ossl_prov_is_running()) |
115 | 0 | return NULL; |
116 | | |
117 | 0 | ctx = OPENSSL_zalloc(sizeof(PROV_ECDSA_CTX)); |
118 | 0 | if (ctx == NULL) |
119 | 0 | return NULL; |
120 | | |
121 | 0 | ctx->flag_allow_md = 1; |
122 | 0 | ctx->libctx = PROV_LIBCTX_OF(provctx); |
123 | 0 | if (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL) { |
124 | 0 | OPENSSL_free(ctx); |
125 | 0 | ctx = NULL; |
126 | 0 | } |
127 | 0 | return ctx; |
128 | 0 | } |
129 | | |
130 | | static int ecdsa_signverify_init(void *vctx, void *ec, |
131 | | const OSSL_PARAM params[], int operation) |
132 | 0 | { |
133 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
134 | |
|
135 | 0 | if (!ossl_prov_is_running() |
136 | 0 | || ctx == NULL) |
137 | 0 | return 0; |
138 | | |
139 | 0 | if (ec == NULL && ctx->ec == NULL) { |
140 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
141 | 0 | return 0; |
142 | 0 | } |
143 | | |
144 | 0 | if (ec != NULL) { |
145 | 0 | if (!ossl_ec_check_key(ctx->libctx, ec, operation == EVP_PKEY_OP_SIGN)) |
146 | 0 | return 0; |
147 | 0 | if (!EC_KEY_up_ref(ec)) |
148 | 0 | return 0; |
149 | 0 | EC_KEY_free(ctx->ec); |
150 | 0 | ctx->ec = ec; |
151 | 0 | } |
152 | | |
153 | 0 | ctx->operation = operation; |
154 | |
|
155 | 0 | if (!ecdsa_set_ctx_params(ctx, params)) |
156 | 0 | return 0; |
157 | | |
158 | 0 | return 1; |
159 | 0 | } |
160 | | |
161 | | static int ecdsa_sign_init(void *vctx, void *ec, const OSSL_PARAM params[]) |
162 | 0 | { |
163 | 0 | return ecdsa_signverify_init(vctx, ec, params, EVP_PKEY_OP_SIGN); |
164 | 0 | } |
165 | | |
166 | | static int ecdsa_verify_init(void *vctx, void *ec, const OSSL_PARAM params[]) |
167 | 0 | { |
168 | 0 | return ecdsa_signverify_init(vctx, ec, params, EVP_PKEY_OP_VERIFY); |
169 | 0 | } |
170 | | |
171 | | static int ecdsa_sign(void *vctx, unsigned char *sig, size_t *siglen, |
172 | | size_t sigsize, const unsigned char *tbs, size_t tbslen) |
173 | 0 | { |
174 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
175 | 0 | int ret; |
176 | 0 | unsigned int sltmp; |
177 | 0 | size_t ecsize = ECDSA_size(ctx->ec); |
178 | |
|
179 | 0 | if (!ossl_prov_is_running()) |
180 | 0 | return 0; |
181 | | |
182 | 0 | if (sig == NULL) { |
183 | 0 | *siglen = ecsize; |
184 | 0 | return 1; |
185 | 0 | } |
186 | | |
187 | | #if !defined(OPENSSL_NO_ACVP_TESTS) |
188 | | if (ctx->kattest && !ECDSA_sign_setup(ctx->ec, NULL, &ctx->kinv, &ctx->r)) |
189 | | return 0; |
190 | | #endif |
191 | | |
192 | 0 | if (sigsize < (size_t)ecsize) |
193 | 0 | return 0; |
194 | | |
195 | 0 | if (ctx->mdsize != 0 && tbslen != ctx->mdsize) |
196 | 0 | return 0; |
197 | | |
198 | 0 | if (ctx->nonce_type != 0) { |
199 | 0 | ret = ossl_ecdsa_deterministic_sign(tbs, tbslen, sig, &sltmp, |
200 | 0 | ctx->ec, ctx->nonce_type, |
201 | 0 | ctx->mdname, |
202 | 0 | ctx->libctx, ctx->propq); |
203 | 0 | } else { |
204 | 0 | ret = ECDSA_sign_ex(0, tbs, tbslen, sig, &sltmp, ctx->kinv, ctx->r, |
205 | 0 | ctx->ec); |
206 | 0 | } |
207 | 0 | if (ret <= 0) |
208 | 0 | return 0; |
209 | | |
210 | 0 | *siglen = sltmp; |
211 | 0 | return 1; |
212 | 0 | } |
213 | | |
214 | | static int ecdsa_verify(void *vctx, const unsigned char *sig, size_t siglen, |
215 | | const unsigned char *tbs, size_t tbslen) |
216 | 0 | { |
217 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
218 | |
|
219 | 0 | if (!ossl_prov_is_running() || (ctx->mdsize != 0 && tbslen != ctx->mdsize)) |
220 | 0 | return 0; |
221 | | |
222 | 0 | return ECDSA_verify(0, tbs, tbslen, sig, siglen, ctx->ec); |
223 | 0 | } |
224 | | |
225 | | static int ecdsa_setup_md(PROV_ECDSA_CTX *ctx, const char *mdname, |
226 | | const char *mdprops) |
227 | 0 | { |
228 | 0 | EVP_MD *md = NULL; |
229 | 0 | size_t mdname_len; |
230 | 0 | int md_nid, sha1_allowed, md_size; |
231 | 0 | WPACKET pkt; |
232 | |
|
233 | 0 | if (mdname == NULL) |
234 | 0 | return 1; |
235 | | |
236 | 0 | mdname_len = strlen(mdname); |
237 | 0 | if (mdname_len >= sizeof(ctx->mdname)) { |
238 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, |
239 | 0 | "%s exceeds name buffer length", mdname); |
240 | 0 | return 0; |
241 | 0 | } |
242 | 0 | if (mdprops == NULL) |
243 | 0 | mdprops = ctx->propq; |
244 | 0 | md = EVP_MD_fetch(ctx->libctx, mdname, mdprops); |
245 | 0 | if (md == NULL) { |
246 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, |
247 | 0 | "%s could not be fetched", mdname); |
248 | 0 | return 0; |
249 | 0 | } |
250 | 0 | md_size = EVP_MD_get_size(md); |
251 | 0 | if (md_size <= 0) { |
252 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, |
253 | 0 | "%s has invalid md size %d", mdname, md_size); |
254 | 0 | EVP_MD_free(md); |
255 | 0 | return 0; |
256 | 0 | } |
257 | 0 | sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN); |
258 | 0 | md_nid = ossl_digest_get_approved_nid_with_sha1(ctx->libctx, md, |
259 | 0 | sha1_allowed); |
260 | 0 | if (md_nid < 0) { |
261 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED, |
262 | 0 | "digest=%s", mdname); |
263 | 0 | EVP_MD_free(md); |
264 | 0 | return 0; |
265 | 0 | } |
266 | | |
267 | 0 | if (!ctx->flag_allow_md) { |
268 | 0 | if (ctx->mdname[0] != '\0' && !EVP_MD_is_a(md, ctx->mdname)) { |
269 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED, |
270 | 0 | "digest %s != %s", mdname, ctx->mdname); |
271 | 0 | EVP_MD_free(md); |
272 | 0 | return 0; |
273 | 0 | } |
274 | 0 | EVP_MD_free(md); |
275 | 0 | return 1; |
276 | 0 | } |
277 | | |
278 | 0 | EVP_MD_CTX_free(ctx->mdctx); |
279 | 0 | EVP_MD_free(ctx->md); |
280 | |
|
281 | 0 | ctx->aid_len = 0; |
282 | 0 | if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf)) |
283 | 0 | && ossl_DER_w_algorithmIdentifier_ECDSA_with_MD(&pkt, -1, ctx->ec, |
284 | 0 | md_nid) |
285 | 0 | && WPACKET_finish(&pkt)) { |
286 | 0 | WPACKET_get_total_written(&pkt, &ctx->aid_len); |
287 | 0 | ctx->aid = WPACKET_get_curr(&pkt); |
288 | 0 | } |
289 | 0 | WPACKET_cleanup(&pkt); |
290 | 0 | ctx->mdctx = NULL; |
291 | 0 | ctx->md = md; |
292 | 0 | ctx->mdsize = (size_t)md_size; |
293 | 0 | OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname)); |
294 | |
|
295 | 0 | return 1; |
296 | 0 | } |
297 | | |
298 | | static int ecdsa_digest_signverify_init(void *vctx, const char *mdname, |
299 | | void *ec, const OSSL_PARAM params[], |
300 | | int operation) |
301 | 0 | { |
302 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
303 | |
|
304 | 0 | if (!ossl_prov_is_running()) |
305 | 0 | return 0; |
306 | | |
307 | 0 | if (!ecdsa_signverify_init(vctx, ec, params, operation) |
308 | 0 | || !ecdsa_setup_md(ctx, mdname, NULL)) |
309 | 0 | return 0; |
310 | | |
311 | 0 | ctx->flag_allow_md = 0; |
312 | |
|
313 | 0 | if (ctx->mdctx == NULL) { |
314 | 0 | ctx->mdctx = EVP_MD_CTX_new(); |
315 | 0 | if (ctx->mdctx == NULL) |
316 | 0 | goto error; |
317 | 0 | } |
318 | | |
319 | 0 | if (!EVP_DigestInit_ex2(ctx->mdctx, ctx->md, params)) |
320 | 0 | goto error; |
321 | 0 | return 1; |
322 | 0 | error: |
323 | 0 | EVP_MD_CTX_free(ctx->mdctx); |
324 | 0 | ctx->mdctx = NULL; |
325 | 0 | return 0; |
326 | 0 | } |
327 | | |
328 | | static int ecdsa_digest_sign_init(void *vctx, const char *mdname, void *ec, |
329 | | const OSSL_PARAM params[]) |
330 | 0 | { |
331 | 0 | return ecdsa_digest_signverify_init(vctx, mdname, ec, params, |
332 | 0 | EVP_PKEY_OP_SIGN); |
333 | 0 | } |
334 | | |
335 | | static int ecdsa_digest_verify_init(void *vctx, const char *mdname, void *ec, |
336 | | const OSSL_PARAM params[]) |
337 | 0 | { |
338 | 0 | return ecdsa_digest_signverify_init(vctx, mdname, ec, params, |
339 | 0 | EVP_PKEY_OP_VERIFY); |
340 | 0 | } |
341 | | |
342 | | int ecdsa_digest_signverify_update(void *vctx, const unsigned char *data, |
343 | | size_t datalen) |
344 | 0 | { |
345 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
346 | |
|
347 | 0 | if (ctx == NULL || ctx->mdctx == NULL) |
348 | 0 | return 0; |
349 | | |
350 | 0 | return EVP_DigestUpdate(ctx->mdctx, data, datalen); |
351 | 0 | } |
352 | | |
353 | | int ecdsa_digest_sign_final(void *vctx, unsigned char *sig, size_t *siglen, |
354 | | size_t sigsize) |
355 | 0 | { |
356 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
357 | 0 | unsigned char digest[EVP_MAX_MD_SIZE]; |
358 | 0 | unsigned int dlen = 0; |
359 | |
|
360 | 0 | if (!ossl_prov_is_running() || ctx == NULL || ctx->mdctx == NULL) |
361 | 0 | return 0; |
362 | | |
363 | | /* |
364 | | * If sig is NULL then we're just finding out the sig size. Other fields |
365 | | * are ignored. Defer to ecdsa_sign. |
366 | | */ |
367 | 0 | if (sig != NULL |
368 | 0 | && !EVP_DigestFinal_ex(ctx->mdctx, digest, &dlen)) |
369 | 0 | return 0; |
370 | 0 | ctx->flag_allow_md = 1; |
371 | 0 | return ecdsa_sign(vctx, sig, siglen, sigsize, digest, (size_t)dlen); |
372 | 0 | } |
373 | | |
374 | | int ecdsa_digest_verify_final(void *vctx, const unsigned char *sig, |
375 | | size_t siglen) |
376 | 0 | { |
377 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
378 | 0 | unsigned char digest[EVP_MAX_MD_SIZE]; |
379 | 0 | unsigned int dlen = 0; |
380 | |
|
381 | 0 | if (!ossl_prov_is_running() || ctx == NULL || ctx->mdctx == NULL) |
382 | 0 | return 0; |
383 | | |
384 | 0 | if (!EVP_DigestFinal_ex(ctx->mdctx, digest, &dlen)) |
385 | 0 | return 0; |
386 | 0 | ctx->flag_allow_md = 1; |
387 | 0 | return ecdsa_verify(ctx, sig, siglen, digest, (size_t)dlen); |
388 | 0 | } |
389 | | |
390 | | static void ecdsa_freectx(void *vctx) |
391 | 0 | { |
392 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
393 | |
|
394 | 0 | OPENSSL_free(ctx->propq); |
395 | 0 | EVP_MD_CTX_free(ctx->mdctx); |
396 | 0 | EVP_MD_free(ctx->md); |
397 | 0 | ctx->propq = NULL; |
398 | 0 | ctx->mdctx = NULL; |
399 | 0 | ctx->md = NULL; |
400 | 0 | ctx->mdsize = 0; |
401 | 0 | EC_KEY_free(ctx->ec); |
402 | 0 | BN_clear_free(ctx->kinv); |
403 | 0 | BN_clear_free(ctx->r); |
404 | 0 | OPENSSL_free(ctx); |
405 | 0 | } |
406 | | |
407 | | static void *ecdsa_dupctx(void *vctx) |
408 | 0 | { |
409 | 0 | PROV_ECDSA_CTX *srcctx = (PROV_ECDSA_CTX *)vctx; |
410 | 0 | PROV_ECDSA_CTX *dstctx; |
411 | |
|
412 | 0 | if (!ossl_prov_is_running()) |
413 | 0 | return NULL; |
414 | | |
415 | 0 | dstctx = OPENSSL_zalloc(sizeof(*srcctx)); |
416 | 0 | if (dstctx == NULL) |
417 | 0 | return NULL; |
418 | | |
419 | 0 | *dstctx = *srcctx; |
420 | 0 | dstctx->ec = NULL; |
421 | 0 | dstctx->md = NULL; |
422 | 0 | dstctx->mdctx = NULL; |
423 | 0 | dstctx->propq = NULL; |
424 | |
|
425 | 0 | if (srcctx->ec != NULL && !EC_KEY_up_ref(srcctx->ec)) |
426 | 0 | goto err; |
427 | | /* Test KATS should not need to be supported */ |
428 | 0 | if (srcctx->kinv != NULL || srcctx->r != NULL) |
429 | 0 | goto err; |
430 | 0 | dstctx->ec = srcctx->ec; |
431 | |
|
432 | 0 | if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md)) |
433 | 0 | goto err; |
434 | 0 | dstctx->md = srcctx->md; |
435 | |
|
436 | 0 | if (srcctx->mdctx != NULL) { |
437 | 0 | dstctx->mdctx = EVP_MD_CTX_new(); |
438 | 0 | if (dstctx->mdctx == NULL |
439 | 0 | || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx)) |
440 | 0 | goto err; |
441 | 0 | } |
442 | | |
443 | 0 | if (srcctx->propq != NULL) { |
444 | 0 | dstctx->propq = OPENSSL_strdup(srcctx->propq); |
445 | 0 | if (dstctx->propq == NULL) |
446 | 0 | goto err; |
447 | 0 | } |
448 | | |
449 | 0 | return dstctx; |
450 | 0 | err: |
451 | 0 | ecdsa_freectx(dstctx); |
452 | 0 | return NULL; |
453 | 0 | } |
454 | | |
455 | | static int ecdsa_get_ctx_params(void *vctx, OSSL_PARAM *params) |
456 | 0 | { |
457 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
458 | 0 | OSSL_PARAM *p; |
459 | |
|
460 | 0 | if (ctx == NULL) |
461 | 0 | return 0; |
462 | | |
463 | 0 | p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID); |
464 | 0 | if (p != NULL && !OSSL_PARAM_set_octet_string(p, ctx->aid, ctx->aid_len)) |
465 | 0 | return 0; |
466 | | |
467 | 0 | p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE); |
468 | 0 | if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->mdsize)) |
469 | 0 | return 0; |
470 | | |
471 | 0 | p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST); |
472 | 0 | if (p != NULL && !OSSL_PARAM_set_utf8_string(p, ctx->md == NULL |
473 | 0 | ? ctx->mdname |
474 | 0 | : EVP_MD_get0_name(ctx->md))) |
475 | 0 | return 0; |
476 | | |
477 | 0 | p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_NONCE_TYPE); |
478 | 0 | if (p != NULL && !OSSL_PARAM_set_uint(p, ctx->nonce_type)) |
479 | 0 | return 0; |
480 | | |
481 | 0 | return 1; |
482 | 0 | } |
483 | | |
484 | | static const OSSL_PARAM known_gettable_ctx_params[] = { |
485 | | OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0), |
486 | | OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL), |
487 | | OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0), |
488 | | OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_NONCE_TYPE, NULL), |
489 | | OSSL_PARAM_END |
490 | | }; |
491 | | |
492 | | static const OSSL_PARAM *ecdsa_gettable_ctx_params(ossl_unused void *vctx, |
493 | | ossl_unused void *provctx) |
494 | 0 | { |
495 | 0 | return known_gettable_ctx_params; |
496 | 0 | } |
497 | | |
498 | | static int ecdsa_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
499 | 0 | { |
500 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
501 | 0 | const OSSL_PARAM *p; |
502 | 0 | size_t mdsize = 0; |
503 | |
|
504 | 0 | if (ctx == NULL) |
505 | 0 | return 0; |
506 | 0 | if (params == NULL) |
507 | 0 | return 1; |
508 | | |
509 | | #if !defined(OPENSSL_NO_ACVP_TESTS) |
510 | | p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_KAT); |
511 | | if (p != NULL && !OSSL_PARAM_get_uint(p, &ctx->kattest)) |
512 | | return 0; |
513 | | #endif |
514 | | |
515 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST); |
516 | 0 | if (p != NULL) { |
517 | 0 | char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname; |
518 | 0 | char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops; |
519 | 0 | const OSSL_PARAM *propsp = |
520 | 0 | OSSL_PARAM_locate_const(params, |
521 | 0 | OSSL_SIGNATURE_PARAM_PROPERTIES); |
522 | |
|
523 | 0 | if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname))) |
524 | 0 | return 0; |
525 | 0 | if (propsp != NULL |
526 | 0 | && !OSSL_PARAM_get_utf8_string(propsp, &pmdprops, sizeof(mdprops))) |
527 | 0 | return 0; |
528 | 0 | if (!ecdsa_setup_md(ctx, mdname, mdprops)) |
529 | 0 | return 0; |
530 | 0 | } |
531 | | |
532 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE); |
533 | 0 | if (p != NULL) { |
534 | 0 | if (!OSSL_PARAM_get_size_t(p, &mdsize) |
535 | 0 | || (!ctx->flag_allow_md && mdsize != ctx->mdsize)) |
536 | 0 | return 0; |
537 | 0 | ctx->mdsize = mdsize; |
538 | 0 | } |
539 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_NONCE_TYPE); |
540 | 0 | if (p != NULL |
541 | 0 | && !OSSL_PARAM_get_uint(p, &ctx->nonce_type)) |
542 | 0 | return 0; |
543 | | |
544 | 0 | return 1; |
545 | 0 | } |
546 | | |
547 | | static const OSSL_PARAM settable_ctx_params[] = { |
548 | | OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0), |
549 | | OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL), |
550 | | OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0), |
551 | | OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_KAT, NULL), |
552 | | OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_NONCE_TYPE, NULL), |
553 | | OSSL_PARAM_END |
554 | | }; |
555 | | |
556 | | static const OSSL_PARAM settable_ctx_params_no_digest[] = { |
557 | | OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_KAT, NULL), |
558 | | OSSL_PARAM_END |
559 | | }; |
560 | | |
561 | | static const OSSL_PARAM *ecdsa_settable_ctx_params(void *vctx, |
562 | | ossl_unused void *provctx) |
563 | 0 | { |
564 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
565 | |
|
566 | 0 | if (ctx != NULL && !ctx->flag_allow_md) |
567 | 0 | return settable_ctx_params_no_digest; |
568 | 0 | return settable_ctx_params; |
569 | 0 | } |
570 | | |
571 | | static int ecdsa_get_ctx_md_params(void *vctx, OSSL_PARAM *params) |
572 | 0 | { |
573 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
574 | |
|
575 | 0 | if (ctx->mdctx == NULL) |
576 | 0 | return 0; |
577 | | |
578 | 0 | return EVP_MD_CTX_get_params(ctx->mdctx, params); |
579 | 0 | } |
580 | | |
581 | | static const OSSL_PARAM *ecdsa_gettable_ctx_md_params(void *vctx) |
582 | 0 | { |
583 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
584 | |
|
585 | 0 | if (ctx->md == NULL) |
586 | 0 | return 0; |
587 | | |
588 | 0 | return EVP_MD_gettable_ctx_params(ctx->md); |
589 | 0 | } |
590 | | |
591 | | static int ecdsa_set_ctx_md_params(void *vctx, const OSSL_PARAM params[]) |
592 | 0 | { |
593 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
594 | |
|
595 | 0 | if (ctx->mdctx == NULL) |
596 | 0 | return 0; |
597 | | |
598 | 0 | return EVP_MD_CTX_set_params(ctx->mdctx, params); |
599 | 0 | } |
600 | | |
601 | | static const OSSL_PARAM *ecdsa_settable_ctx_md_params(void *vctx) |
602 | 0 | { |
603 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
604 | |
|
605 | 0 | if (ctx->md == NULL) |
606 | 0 | return 0; |
607 | | |
608 | 0 | return EVP_MD_settable_ctx_params(ctx->md); |
609 | 0 | } |
610 | | |
611 | | const OSSL_DISPATCH ossl_ecdsa_signature_functions[] = { |
612 | | { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))ecdsa_newctx }, |
613 | | { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))ecdsa_sign_init }, |
614 | | { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))ecdsa_sign }, |
615 | | { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))ecdsa_verify_init }, |
616 | | { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))ecdsa_verify }, |
617 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, |
618 | | (void (*)(void))ecdsa_digest_sign_init }, |
619 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, |
620 | | (void (*)(void))ecdsa_digest_signverify_update }, |
621 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, |
622 | | (void (*)(void))ecdsa_digest_sign_final }, |
623 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, |
624 | | (void (*)(void))ecdsa_digest_verify_init }, |
625 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE, |
626 | | (void (*)(void))ecdsa_digest_signverify_update }, |
627 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL, |
628 | | (void (*)(void))ecdsa_digest_verify_final }, |
629 | | { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))ecdsa_freectx }, |
630 | | { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))ecdsa_dupctx }, |
631 | | { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))ecdsa_get_ctx_params }, |
632 | | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, |
633 | | (void (*)(void))ecdsa_gettable_ctx_params }, |
634 | | { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))ecdsa_set_ctx_params }, |
635 | | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, |
636 | | (void (*)(void))ecdsa_settable_ctx_params }, |
637 | | { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS, |
638 | | (void (*)(void))ecdsa_get_ctx_md_params }, |
639 | | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS, |
640 | | (void (*)(void))ecdsa_gettable_ctx_md_params }, |
641 | | { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS, |
642 | | (void (*)(void))ecdsa_set_ctx_md_params }, |
643 | | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS, |
644 | | (void (*)(void))ecdsa_settable_ctx_md_params }, |
645 | | OSSL_DISPATCH_END |
646 | | }; |