2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 * ECDSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
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 "internal/nelem.h"
25 #include "internal/sizes.h"
26 #include "internal/cryptlib.h"
27 #include "prov/providercommon.h"
28 #include "prov/providercommonerr.h"
29 #include "prov/implementations.h"
30 #include "prov/provider_ctx.h"
31 #include "prov/securitycheck.h"
32 #include "crypto/ec.h"
33 #include "prov/der_ec.h"
35 static OSSL_FUNC_signature_newctx_fn ecdsa_newctx;
36 static OSSL_FUNC_signature_sign_init_fn ecdsa_sign_init;
37 static OSSL_FUNC_signature_verify_init_fn ecdsa_verify_init;
38 static OSSL_FUNC_signature_sign_fn ecdsa_sign;
39 static OSSL_FUNC_signature_verify_fn ecdsa_verify;
40 static OSSL_FUNC_signature_digest_sign_init_fn ecdsa_digest_sign_init;
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;
43 static OSSL_FUNC_signature_digest_verify_init_fn ecdsa_digest_verify_init;
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;
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.
67 char mdname[OSSL_MAX_NAME_SIZE];
69 /* The Algorithm Identifier of the combined signature algorithm */
70 unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE];
79 * Internally used to cache the results of calling the EC group
80 * sign_setup() methods which are then passed to the sign operation.
81 * This is used by CAVS failure tests to terminate a loop if the signature
83 * This could of also been done with a simple flag.
87 #if !defined(OPENSSL_NO_ACVP_TESTS)
89 * This indicates that KAT (CAVS) test is running. Externally an app will
90 * override the random callback such that the generated private key and k
92 * Normal operation will loop to choose a new k if the signature is not
93 * valid - but for this mode of operation it forces a failure instead.
99 static void *ecdsa_newctx(void *provctx, const char *propq)
103 if (!ossl_prov_is_running())
106 ctx = OPENSSL_zalloc(sizeof(PROV_ECDSA_CTX));
110 ctx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
111 if (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL) {
114 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
119 static int ecdsa_signverify_init(void *vctx, void *ec, int operation)
121 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
123 if (!ossl_prov_is_running()
126 || !EC_KEY_up_ref(ec))
128 EC_KEY_free(ctx->ec);
130 ctx->operation = operation;
131 return ec_check_key(ec, operation == EVP_PKEY_OP_SIGN);
134 static int ecdsa_sign_init(void *vctx, void *ec)
136 return ecdsa_signverify_init(vctx, ec, EVP_PKEY_OP_SIGN);
139 static int ecdsa_verify_init(void *vctx, void *ec)
141 return ecdsa_signverify_init(vctx, ec, EVP_PKEY_OP_VERIFY);
144 static int ecdsa_sign(void *vctx, unsigned char *sig, size_t *siglen,
145 size_t sigsize, const unsigned char *tbs, size_t tbslen)
147 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
150 size_t ecsize = ECDSA_size(ctx->ec);
152 if (!ossl_prov_is_running())
160 #if !defined(OPENSSL_NO_ACVP_TESTS)
161 if (ctx->kattest && !ECDSA_sign_setup(ctx->ec, NULL, &ctx->kinv, &ctx->r))
165 if (sigsize < (size_t)ecsize)
168 if (ctx->mdsize != 0 && tbslen != ctx->mdsize)
171 ret = ECDSA_sign_ex(0, tbs, tbslen, sig, &sltmp, ctx->kinv, ctx->r, ctx->ec);
179 static int ecdsa_verify(void *vctx, const unsigned char *sig, size_t siglen,
180 const unsigned char *tbs, size_t tbslen)
182 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
184 if (!ossl_prov_is_running() || (ctx->mdsize != 0 && tbslen != ctx->mdsize))
187 return ECDSA_verify(0, tbs, tbslen, sig, siglen, ctx->ec);
190 static void free_md(PROV_ECDSA_CTX *ctx)
192 OPENSSL_free(ctx->propq);
193 EVP_MD_CTX_free(ctx->mdctx);
194 EVP_MD_free(ctx->md);
201 static int ecdsa_digest_signverify_init(void *vctx, const char *mdname,
202 void *ec, int operation)
204 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
205 int md_nid = NID_undef;
207 int sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN);
209 if (!ossl_prov_is_running())
214 if (!ecdsa_signverify_init(vctx, ec, operation))
217 ctx->md = EVP_MD_fetch(ctx->libctx, mdname, ctx->propq);
218 md_nid = digest_get_approved_nid_with_sha1(ctx->md, sha1_allowed);
219 if (md_nid == NID_undef)
222 ctx->mdsize = EVP_MD_size(ctx->md);
223 ctx->mdctx = EVP_MD_CTX_new();
224 if (ctx->mdctx == NULL)
228 * TODO(3.0) Should we care about DER writing errors?
229 * All it really means is that for some reason, there's no
230 * AlgorithmIdentifier to be had, but the operation itself is
231 * still valid, just as long as it's not used to construct
232 * anything that needs an AlgorithmIdentifier.
235 if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf))
236 && ossl_DER_w_algorithmIdentifier_ECDSA_with_MD(&pkt, -1, ctx->ec,
238 && WPACKET_finish(&pkt)) {
239 WPACKET_get_total_written(&pkt, &ctx->aid_len);
240 ctx->aid = WPACKET_get_curr(&pkt);
242 WPACKET_cleanup(&pkt);
244 if (!EVP_DigestInit_ex(ctx->mdctx, ctx->md, NULL))
252 static int ecdsa_digest_sign_init(void *vctx, const char *mdname, void *ec)
254 return ecdsa_digest_signverify_init(vctx, mdname, ec, EVP_PKEY_OP_SIGN);
257 static int ecdsa_digest_verify_init(void *vctx, const char *mdname, void *ec)
259 return ecdsa_digest_signverify_init(vctx, mdname, ec, EVP_PKEY_OP_VERIFY);
262 int ecdsa_digest_signverify_update(void *vctx, const unsigned char *data,
265 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
267 if (ctx == NULL || ctx->mdctx == NULL)
270 return EVP_DigestUpdate(ctx->mdctx, data, datalen);
273 int ecdsa_digest_sign_final(void *vctx, unsigned char *sig, size_t *siglen,
276 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
277 unsigned char digest[EVP_MAX_MD_SIZE];
278 unsigned int dlen = 0;
280 if (!ossl_prov_is_running() || ctx == NULL || ctx->mdctx == NULL)
284 * If sig is NULL then we're just finding out the sig size. Other fields
285 * are ignored. Defer to ecdsa_sign.
289 * TODO(3.0): There is the possibility that some externally provided
290 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
291 * but that problem is much larger than just in DSA.
293 if (!EVP_DigestFinal_ex(ctx->mdctx, digest, &dlen))
297 return ecdsa_sign(vctx, sig, siglen, sigsize, digest, (size_t)dlen);
300 int ecdsa_digest_verify_final(void *vctx, const unsigned char *sig,
303 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
304 unsigned char digest[EVP_MAX_MD_SIZE];
305 unsigned int dlen = 0;
307 if (!ossl_prov_is_running() || ctx == NULL || ctx->mdctx == NULL)
311 * TODO(3.0): There is the possibility that some externally provided
312 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
313 * but that problem is much larger than just in DSA.
315 if (!EVP_DigestFinal_ex(ctx->mdctx, digest, &dlen))
318 return ecdsa_verify(ctx, sig, siglen, digest, (size_t)dlen);
321 static void ecdsa_freectx(void *vctx)
323 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
326 EC_KEY_free(ctx->ec);
327 BN_clear_free(ctx->kinv);
328 BN_clear_free(ctx->r);
332 static void *ecdsa_dupctx(void *vctx)
334 PROV_ECDSA_CTX *srcctx = (PROV_ECDSA_CTX *)vctx;
335 PROV_ECDSA_CTX *dstctx;
337 if (!ossl_prov_is_running())
340 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
347 dstctx->mdctx = NULL;
349 if (srcctx->ec != NULL && !EC_KEY_up_ref(srcctx->ec))
351 /* Test KATS should not need to be supported */
352 if (srcctx->kinv != NULL || srcctx->r != NULL)
354 dstctx->ec = srcctx->ec;
356 if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
358 dstctx->md = srcctx->md;
360 if (srcctx->mdctx != NULL) {
361 dstctx->mdctx = EVP_MD_CTX_new();
362 if (dstctx->mdctx == NULL
363 || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
369 ecdsa_freectx(dstctx);
373 static int ecdsa_get_ctx_params(void *vctx, OSSL_PARAM *params)
375 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
378 if (ctx == NULL || params == NULL)
381 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
382 if (p != NULL && !OSSL_PARAM_set_octet_string(p, ctx->aid, ctx->aid_len))
385 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
386 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->mdsize))
389 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
390 if (p != NULL && !OSSL_PARAM_set_utf8_string(p, ctx->md == NULL
392 : EVP_MD_name(ctx->md)))
398 static const OSSL_PARAM known_gettable_ctx_params[] = {
399 OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
400 OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
401 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
405 static const OSSL_PARAM *ecdsa_gettable_ctx_params(ossl_unused void *provctx)
407 return known_gettable_ctx_params;
410 static int ecdsa_set_ctx_params(void *vctx, const OSSL_PARAM params[])
412 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
416 if (ctx == NULL || params == NULL)
419 if (ctx->md != NULL) {
421 * You cannot set the digest name/size when doing a DigestSign or
426 #if !defined(OPENSSL_NO_ACVP_TESTS)
427 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_KAT);
428 if (p != NULL && !OSSL_PARAM_get_uint(p, &ctx->kattest))
432 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
433 if (p != NULL && !OSSL_PARAM_get_size_t(p, &ctx->mdsize))
437 * We never actually use the mdname, but we do support getting it later.
438 * This can be useful for applications that want to know the MD that they
441 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
442 mdname = ctx->mdname;
444 && !OSSL_PARAM_get_utf8_string(p, &mdname, sizeof(ctx->mdname)))
450 static const OSSL_PARAM known_settable_ctx_params[] = {
451 OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
452 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
453 OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_KAT, NULL),
457 static const OSSL_PARAM *ecdsa_settable_ctx_params(ossl_unused void *provctx)
460 * TODO(3.0): Should this function return a different set of settable ctx
461 * params if the ctx is being used for a DigestSign/DigestVerify? In that
462 * case it is not allowed to set the digest size/digest name because the
463 * digest is explicitly set as part of the init.
465 return known_settable_ctx_params;
468 static int ecdsa_get_ctx_md_params(void *vctx, OSSL_PARAM *params)
470 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
472 if (ctx->mdctx == NULL)
475 return EVP_MD_CTX_get_params(ctx->mdctx, params);
478 static const OSSL_PARAM *ecdsa_gettable_ctx_md_params(void *vctx)
480 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
485 return EVP_MD_gettable_ctx_params(ctx->md);
488 static int ecdsa_set_ctx_md_params(void *vctx, const OSSL_PARAM params[])
490 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
492 if (ctx->mdctx == NULL)
495 return EVP_MD_CTX_set_params(ctx->mdctx, params);
498 static const OSSL_PARAM *ecdsa_settable_ctx_md_params(void *vctx)
500 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
505 return EVP_MD_settable_ctx_params(ctx->md);
508 const OSSL_DISPATCH ecossl_dsa_signature_functions[] = {
509 { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))ecdsa_newctx },
510 { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))ecdsa_sign_init },
511 { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))ecdsa_sign },
512 { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))ecdsa_verify_init },
513 { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))ecdsa_verify },
514 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
515 (void (*)(void))ecdsa_digest_sign_init },
516 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
517 (void (*)(void))ecdsa_digest_signverify_update },
518 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
519 (void (*)(void))ecdsa_digest_sign_final },
520 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
521 (void (*)(void))ecdsa_digest_verify_init },
522 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
523 (void (*)(void))ecdsa_digest_signverify_update },
524 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
525 (void (*)(void))ecdsa_digest_verify_final },
526 { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))ecdsa_freectx },
527 { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))ecdsa_dupctx },
528 { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))ecdsa_get_ctx_params },
529 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
530 (void (*)(void))ecdsa_gettable_ctx_params },
531 { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))ecdsa_set_ctx_params },
532 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
533 (void (*)(void))ecdsa_settable_ctx_params },
534 { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
535 (void (*)(void))ecdsa_get_ctx_md_params },
536 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
537 (void (*)(void))ecdsa_gettable_ctx_md_params },
538 { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
539 (void (*)(void))ecdsa_set_ctx_md_params },
540 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
541 (void (*)(void))ecdsa_settable_ctx_md_params },