/src/openssl/providers/implementations/signature/ecdsa_sig.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2020-2021 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 | 156 | { |
112 | 156 | PROV_ECDSA_CTX *ctx; |
113 | | |
114 | 156 | if (!ossl_prov_is_running()) |
115 | 0 | return NULL; |
116 | | |
117 | 156 | ctx = OPENSSL_zalloc(sizeof(PROV_ECDSA_CTX)); |
118 | 156 | if (ctx == NULL) |
119 | 0 | return NULL; |
120 | | |
121 | 156 | ctx->flag_allow_md = 1; |
122 | 156 | ctx->libctx = PROV_LIBCTX_OF(provctx); |
123 | 156 | if (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL) { |
124 | 0 | OPENSSL_free(ctx); |
125 | 0 | ctx = NULL; |
126 | 0 | } |
127 | 156 | return ctx; |
128 | 156 | } |
129 | | |
130 | | static int ecdsa_signverify_init(void *vctx, void *ec, |
131 | | const OSSL_PARAM params[], int operation) |
132 | 156 | { |
133 | 156 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
134 | | |
135 | 156 | if (!ossl_prov_is_running() |
136 | 156 | || ctx == NULL) |
137 | 0 | return 0; |
138 | | |
139 | 156 | 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 | 156 | if (ec != NULL) { |
145 | 156 | if (!ossl_ec_check_key(ctx->libctx, ec, operation == EVP_PKEY_OP_SIGN)) |
146 | 0 | return 0; |
147 | 156 | if (!EC_KEY_up_ref(ec)) |
148 | 0 | return 0; |
149 | 156 | EC_KEY_free(ctx->ec); |
150 | 156 | ctx->ec = ec; |
151 | 156 | } |
152 | | |
153 | 156 | ctx->operation = operation; |
154 | | |
155 | 156 | if (!ecdsa_set_ctx_params(ctx, params)) |
156 | 0 | return 0; |
157 | | |
158 | 156 | return 1; |
159 | 156 | } |
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 | 156 | { |
217 | 156 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
218 | | |
219 | 156 | if (!ossl_prov_is_running() || (ctx->mdsize != 0 && tbslen != ctx->mdsize)) |
220 | 0 | return 0; |
221 | | |
222 | 156 | return ECDSA_verify(0, tbs, tbslen, sig, siglen, ctx->ec); |
223 | 156 | } |
224 | | |
225 | | static int ecdsa_setup_md(PROV_ECDSA_CTX *ctx, const char *mdname, |
226 | | const char *mdprops) |
227 | 156 | { |
228 | 156 | EVP_MD *md = NULL; |
229 | 156 | size_t mdname_len; |
230 | 156 | int md_nid, sha1_allowed; |
231 | 156 | WPACKET pkt; |
232 | | |
233 | 156 | if (mdname == NULL) |
234 | 0 | return 1; |
235 | | |
236 | 156 | mdname_len = strlen(mdname); |
237 | 156 | 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 | 156 | if (mdprops == NULL) |
243 | 156 | mdprops = ctx->propq; |
244 | 156 | md = EVP_MD_fetch(ctx->libctx, mdname, mdprops); |
245 | 156 | 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 | 156 | sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN); |
251 | 156 | md_nid = ossl_digest_get_approved_nid_with_sha1(ctx->libctx, md, |
252 | 156 | sha1_allowed); |
253 | 156 | if (md_nid < 0) { |
254 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED, |
255 | 0 | "digest=%s", mdname); |
256 | 0 | EVP_MD_free(md); |
257 | 0 | return 0; |
258 | 0 | } |
259 | | |
260 | 156 | if (!ctx->flag_allow_md) { |
261 | 0 | if (ctx->mdname[0] != '\0' && !EVP_MD_is_a(md, ctx->mdname)) { |
262 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED, |
263 | 0 | "digest %s != %s", mdname, ctx->mdname); |
264 | 0 | EVP_MD_free(md); |
265 | 0 | return 0; |
266 | 0 | } |
267 | 0 | EVP_MD_free(md); |
268 | 0 | return 1; |
269 | 0 | } |
270 | | |
271 | 156 | EVP_MD_CTX_free(ctx->mdctx); |
272 | 156 | EVP_MD_free(ctx->md); |
273 | | |
274 | 156 | ctx->aid_len = 0; |
275 | 156 | if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf)) |
276 | 156 | && ossl_DER_w_algorithmIdentifier_ECDSA_with_MD(&pkt, -1, ctx->ec, |
277 | 156 | md_nid) |
278 | 156 | && WPACKET_finish(&pkt)) { |
279 | 156 | WPACKET_get_total_written(&pkt, &ctx->aid_len); |
280 | 156 | ctx->aid = WPACKET_get_curr(&pkt); |
281 | 156 | } |
282 | 156 | WPACKET_cleanup(&pkt); |
283 | 156 | ctx->mdctx = NULL; |
284 | 156 | ctx->md = md; |
285 | 156 | ctx->mdsize = EVP_MD_get_size(ctx->md); |
286 | 156 | OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname)); |
287 | | |
288 | 156 | return 1; |
289 | 156 | } |
290 | | |
291 | | static int ecdsa_digest_signverify_init(void *vctx, const char *mdname, |
292 | | void *ec, const OSSL_PARAM params[], |
293 | | int operation) |
294 | 156 | { |
295 | 156 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
296 | | |
297 | 156 | if (!ossl_prov_is_running()) |
298 | 0 | return 0; |
299 | | |
300 | 156 | if (!ecdsa_signverify_init(vctx, ec, params, operation) |
301 | 156 | || !ecdsa_setup_md(ctx, mdname, NULL)) |
302 | 0 | return 0; |
303 | | |
304 | 156 | ctx->flag_allow_md = 0; |
305 | | |
306 | 156 | if (ctx->mdctx == NULL) { |
307 | 156 | ctx->mdctx = EVP_MD_CTX_new(); |
308 | 156 | if (ctx->mdctx == NULL) |
309 | 0 | goto error; |
310 | 156 | } |
311 | | |
312 | 156 | if (!EVP_DigestInit_ex2(ctx->mdctx, ctx->md, params)) |
313 | 0 | goto error; |
314 | 156 | return 1; |
315 | 0 | error: |
316 | 0 | EVP_MD_CTX_free(ctx->mdctx); |
317 | 0 | ctx->mdctx = NULL; |
318 | 0 | return 0; |
319 | 156 | } |
320 | | |
321 | | static int ecdsa_digest_sign_init(void *vctx, const char *mdname, void *ec, |
322 | | const OSSL_PARAM params[]) |
323 | 0 | { |
324 | 0 | return ecdsa_digest_signverify_init(vctx, mdname, ec, params, |
325 | 0 | EVP_PKEY_OP_SIGN); |
326 | 0 | } |
327 | | |
328 | | static int ecdsa_digest_verify_init(void *vctx, const char *mdname, void *ec, |
329 | | const OSSL_PARAM params[]) |
330 | 156 | { |
331 | 156 | return ecdsa_digest_signverify_init(vctx, mdname, ec, params, |
332 | 156 | EVP_PKEY_OP_VERIFY); |
333 | 156 | } |
334 | | |
335 | | int ecdsa_digest_signverify_update(void *vctx, const unsigned char *data, |
336 | | size_t datalen) |
337 | 156 | { |
338 | 156 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
339 | | |
340 | 156 | if (ctx == NULL || ctx->mdctx == NULL) |
341 | 0 | return 0; |
342 | | |
343 | 156 | return EVP_DigestUpdate(ctx->mdctx, data, datalen); |
344 | 156 | } |
345 | | |
346 | | int ecdsa_digest_sign_final(void *vctx, unsigned char *sig, size_t *siglen, |
347 | | size_t sigsize) |
348 | 0 | { |
349 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
350 | 0 | unsigned char digest[EVP_MAX_MD_SIZE]; |
351 | 0 | unsigned int dlen = 0; |
352 | |
|
353 | 0 | if (!ossl_prov_is_running() || ctx == NULL || ctx->mdctx == NULL) |
354 | 0 | return 0; |
355 | | |
356 | | /* |
357 | | * If sig is NULL then we're just finding out the sig size. Other fields |
358 | | * are ignored. Defer to ecdsa_sign. |
359 | | */ |
360 | 0 | if (sig != NULL |
361 | 0 | && !EVP_DigestFinal_ex(ctx->mdctx, digest, &dlen)) |
362 | 0 | return 0; |
363 | 0 | ctx->flag_allow_md = 1; |
364 | 0 | return ecdsa_sign(vctx, sig, siglen, sigsize, digest, (size_t)dlen); |
365 | 0 | } |
366 | | |
367 | | int ecdsa_digest_verify_final(void *vctx, const unsigned char *sig, |
368 | | size_t siglen) |
369 | 156 | { |
370 | 156 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
371 | 156 | unsigned char digest[EVP_MAX_MD_SIZE]; |
372 | 156 | unsigned int dlen = 0; |
373 | | |
374 | 156 | if (!ossl_prov_is_running() || ctx == NULL || ctx->mdctx == NULL) |
375 | 0 | return 0; |
376 | | |
377 | 156 | if (!EVP_DigestFinal_ex(ctx->mdctx, digest, &dlen)) |
378 | 0 | return 0; |
379 | 156 | ctx->flag_allow_md = 1; |
380 | 156 | return ecdsa_verify(ctx, sig, siglen, digest, (size_t)dlen); |
381 | 156 | } |
382 | | |
383 | | static void ecdsa_freectx(void *vctx) |
384 | 312 | { |
385 | 312 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
386 | | |
387 | 312 | OPENSSL_free(ctx->propq); |
388 | 312 | EVP_MD_CTX_free(ctx->mdctx); |
389 | 312 | EVP_MD_free(ctx->md); |
390 | 312 | ctx->propq = NULL; |
391 | 312 | ctx->mdctx = NULL; |
392 | 312 | ctx->md = NULL; |
393 | 312 | ctx->mdsize = 0; |
394 | 312 | EC_KEY_free(ctx->ec); |
395 | 312 | BN_clear_free(ctx->kinv); |
396 | 312 | BN_clear_free(ctx->r); |
397 | 312 | OPENSSL_free(ctx); |
398 | 312 | } |
399 | | |
400 | | static void *ecdsa_dupctx(void *vctx) |
401 | 156 | { |
402 | 156 | PROV_ECDSA_CTX *srcctx = (PROV_ECDSA_CTX *)vctx; |
403 | 156 | PROV_ECDSA_CTX *dstctx; |
404 | | |
405 | 156 | if (!ossl_prov_is_running()) |
406 | 0 | return NULL; |
407 | | |
408 | 156 | dstctx = OPENSSL_zalloc(sizeof(*srcctx)); |
409 | 156 | if (dstctx == NULL) |
410 | 0 | return NULL; |
411 | | |
412 | 156 | *dstctx = *srcctx; |
413 | 156 | dstctx->ec = NULL; |
414 | 156 | dstctx->md = NULL; |
415 | 156 | dstctx->mdctx = NULL; |
416 | 156 | dstctx->propq = NULL; |
417 | | |
418 | 156 | if (srcctx->ec != NULL && !EC_KEY_up_ref(srcctx->ec)) |
419 | 0 | goto err; |
420 | | /* Test KATS should not need to be supported */ |
421 | 156 | if (srcctx->kinv != NULL || srcctx->r != NULL) |
422 | 0 | goto err; |
423 | 156 | dstctx->ec = srcctx->ec; |
424 | | |
425 | 156 | if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md)) |
426 | 0 | goto err; |
427 | 156 | dstctx->md = srcctx->md; |
428 | | |
429 | 156 | if (srcctx->mdctx != NULL) { |
430 | 156 | dstctx->mdctx = EVP_MD_CTX_new(); |
431 | 156 | if (dstctx->mdctx == NULL |
432 | 156 | || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx)) |
433 | 0 | goto err; |
434 | 156 | } |
435 | | |
436 | 156 | if (srcctx->propq != NULL) { |
437 | 0 | dstctx->propq = OPENSSL_strdup(srcctx->propq); |
438 | 0 | if (dstctx->propq == NULL) |
439 | 0 | goto err; |
440 | 0 | } |
441 | | |
442 | 156 | return dstctx; |
443 | 0 | err: |
444 | 0 | ecdsa_freectx(dstctx); |
445 | 0 | return NULL; |
446 | 156 | } |
447 | | |
448 | | static int ecdsa_get_ctx_params(void *vctx, OSSL_PARAM *params) |
449 | 0 | { |
450 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
451 | 0 | OSSL_PARAM *p; |
452 | |
|
453 | 0 | if (ctx == NULL) |
454 | 0 | return 0; |
455 | | |
456 | 0 | p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID); |
457 | 0 | if (p != NULL && !OSSL_PARAM_set_octet_string(p, ctx->aid, ctx->aid_len)) |
458 | 0 | return 0; |
459 | | |
460 | 0 | p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE); |
461 | 0 | if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->mdsize)) |
462 | 0 | return 0; |
463 | | |
464 | 0 | p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST); |
465 | 0 | if (p != NULL && !OSSL_PARAM_set_utf8_string(p, ctx->md == NULL |
466 | 0 | ? ctx->mdname |
467 | 0 | : EVP_MD_get0_name(ctx->md))) |
468 | 0 | return 0; |
469 | | |
470 | 0 | p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_NONCE_TYPE); |
471 | 0 | if (p != NULL && !OSSL_PARAM_set_uint(p, ctx->nonce_type)) |
472 | 0 | return 0; |
473 | | |
474 | 0 | return 1; |
475 | 0 | } |
476 | | |
477 | | static const OSSL_PARAM known_gettable_ctx_params[] = { |
478 | | OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0), |
479 | | OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL), |
480 | | OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0), |
481 | | OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_NONCE_TYPE, NULL), |
482 | | OSSL_PARAM_END |
483 | | }; |
484 | | |
485 | | static const OSSL_PARAM *ecdsa_gettable_ctx_params(ossl_unused void *vctx, |
486 | | ossl_unused void *provctx) |
487 | 0 | { |
488 | 0 | return known_gettable_ctx_params; |
489 | 0 | } |
490 | | |
491 | | static int ecdsa_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
492 | 156 | { |
493 | 156 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
494 | 156 | const OSSL_PARAM *p; |
495 | 156 | size_t mdsize = 0; |
496 | | |
497 | 156 | if (ctx == NULL) |
498 | 0 | return 0; |
499 | 156 | if (params == NULL) |
500 | 156 | return 1; |
501 | | |
502 | | #if !defined(OPENSSL_NO_ACVP_TESTS) |
503 | | p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_KAT); |
504 | | if (p != NULL && !OSSL_PARAM_get_uint(p, &ctx->kattest)) |
505 | | return 0; |
506 | | #endif |
507 | | |
508 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST); |
509 | 0 | if (p != NULL) { |
510 | 0 | char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname; |
511 | 0 | char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops; |
512 | 0 | const OSSL_PARAM *propsp = |
513 | 0 | OSSL_PARAM_locate_const(params, |
514 | 0 | OSSL_SIGNATURE_PARAM_PROPERTIES); |
515 | |
|
516 | 0 | if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname))) |
517 | 0 | return 0; |
518 | 0 | if (propsp != NULL |
519 | 0 | && !OSSL_PARAM_get_utf8_string(propsp, &pmdprops, sizeof(mdprops))) |
520 | 0 | return 0; |
521 | 0 | if (!ecdsa_setup_md(ctx, mdname, mdprops)) |
522 | 0 | return 0; |
523 | 0 | } |
524 | | |
525 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE); |
526 | 0 | if (p != NULL) { |
527 | 0 | if (!OSSL_PARAM_get_size_t(p, &mdsize) |
528 | 0 | || (!ctx->flag_allow_md && mdsize != ctx->mdsize)) |
529 | 0 | return 0; |
530 | 0 | ctx->mdsize = mdsize; |
531 | 0 | } |
532 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_NONCE_TYPE); |
533 | 0 | if (p != NULL |
534 | 0 | && !OSSL_PARAM_get_uint(p, &ctx->nonce_type)) |
535 | 0 | return 0; |
536 | | |
537 | 0 | return 1; |
538 | 0 | } |
539 | | |
540 | | static const OSSL_PARAM settable_ctx_params[] = { |
541 | | OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0), |
542 | | OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL), |
543 | | OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0), |
544 | | OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_KAT, NULL), |
545 | | OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_NONCE_TYPE, NULL), |
546 | | OSSL_PARAM_END |
547 | | }; |
548 | | |
549 | | static const OSSL_PARAM settable_ctx_params_no_digest[] = { |
550 | | OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_KAT, NULL), |
551 | | OSSL_PARAM_END |
552 | | }; |
553 | | |
554 | | static const OSSL_PARAM *ecdsa_settable_ctx_params(void *vctx, |
555 | | ossl_unused void *provctx) |
556 | 0 | { |
557 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
558 | |
|
559 | 0 | if (ctx != NULL && !ctx->flag_allow_md) |
560 | 0 | return settable_ctx_params_no_digest; |
561 | 0 | return settable_ctx_params; |
562 | 0 | } |
563 | | |
564 | | static int ecdsa_get_ctx_md_params(void *vctx, OSSL_PARAM *params) |
565 | 0 | { |
566 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
567 | |
|
568 | 0 | if (ctx->mdctx == NULL) |
569 | 0 | return 0; |
570 | | |
571 | 0 | return EVP_MD_CTX_get_params(ctx->mdctx, params); |
572 | 0 | } |
573 | | |
574 | | static const OSSL_PARAM *ecdsa_gettable_ctx_md_params(void *vctx) |
575 | 0 | { |
576 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
577 | |
|
578 | 0 | if (ctx->md == NULL) |
579 | 0 | return 0; |
580 | | |
581 | 0 | return EVP_MD_gettable_ctx_params(ctx->md); |
582 | 0 | } |
583 | | |
584 | | static int ecdsa_set_ctx_md_params(void *vctx, const OSSL_PARAM params[]) |
585 | 0 | { |
586 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
587 | |
|
588 | 0 | if (ctx->mdctx == NULL) |
589 | 0 | return 0; |
590 | | |
591 | 0 | return EVP_MD_CTX_set_params(ctx->mdctx, params); |
592 | 0 | } |
593 | | |
594 | | static const OSSL_PARAM *ecdsa_settable_ctx_md_params(void *vctx) |
595 | 0 | { |
596 | 0 | PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; |
597 | |
|
598 | 0 | if (ctx->md == NULL) |
599 | 0 | return 0; |
600 | | |
601 | 0 | return EVP_MD_settable_ctx_params(ctx->md); |
602 | 0 | } |
603 | | |
604 | | const OSSL_DISPATCH ossl_ecdsa_signature_functions[] = { |
605 | | { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))ecdsa_newctx }, |
606 | | { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))ecdsa_sign_init }, |
607 | | { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))ecdsa_sign }, |
608 | | { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))ecdsa_verify_init }, |
609 | | { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))ecdsa_verify }, |
610 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, |
611 | | (void (*)(void))ecdsa_digest_sign_init }, |
612 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, |
613 | | (void (*)(void))ecdsa_digest_signverify_update }, |
614 | | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, |
615 | | (void (*)(void))ecdsa_digest_sign_final }, |
616 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, |
617 | | (void (*)(void))ecdsa_digest_verify_init }, |
618 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE, |
619 | | (void (*)(void))ecdsa_digest_signverify_update }, |
620 | | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL, |
621 | | (void (*)(void))ecdsa_digest_verify_final }, |
622 | | { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))ecdsa_freectx }, |
623 | | { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))ecdsa_dupctx }, |
624 | | { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))ecdsa_get_ctx_params }, |
625 | | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, |
626 | | (void (*)(void))ecdsa_gettable_ctx_params }, |
627 | | { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))ecdsa_set_ctx_params }, |
628 | | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, |
629 | | (void (*)(void))ecdsa_settable_ctx_params }, |
630 | | { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS, |
631 | | (void (*)(void))ecdsa_get_ctx_md_params }, |
632 | | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS, |
633 | | (void (*)(void))ecdsa_gettable_ctx_md_params }, |
634 | | { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS, |
635 | | (void (*)(void))ecdsa_set_ctx_md_params }, |
636 | | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS, |
637 | | (void (*)(void))ecdsa_settable_ctx_md_params }, |
638 | | OSSL_DISPATCH_END |
639 | | }; |