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