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 && DER_w_algorithmIdentifier_ECDSA_with_MD(&pkt, -1, ctx->ec, md_nid)
237 && WPACKET_finish(&pkt)) {
238 WPACKET_get_total_written(&pkt, &ctx->aid_len);
239 ctx->aid = WPACKET_get_curr(&pkt);
241 WPACKET_cleanup(&pkt);
243 if (!EVP_DigestInit_ex(ctx->mdctx, ctx->md, NULL))
251 static int ecdsa_digest_sign_init(void *vctx, const char *mdname, void *ec)
253 return ecdsa_digest_signverify_init(vctx, mdname, ec, EVP_PKEY_OP_SIGN);
256 static int ecdsa_digest_verify_init(void *vctx, const char *mdname, void *ec)
258 return ecdsa_digest_signverify_init(vctx, mdname, ec, EVP_PKEY_OP_VERIFY);
261 int ecdsa_digest_signverify_update(void *vctx, const unsigned char *data,
264 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
266 if (ctx == NULL || ctx->mdctx == NULL)
269 return EVP_DigestUpdate(ctx->mdctx, data, datalen);
272 int ecdsa_digest_sign_final(void *vctx, unsigned char *sig, size_t *siglen,
275 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
276 unsigned char digest[EVP_MAX_MD_SIZE];
277 unsigned int dlen = 0;
279 if (!ossl_prov_is_running() || ctx == NULL || ctx->mdctx == NULL)
283 * If sig is NULL then we're just finding out the sig size. Other fields
284 * are ignored. Defer to ecdsa_sign.
288 * TODO(3.0): There is the possibility that some externally provided
289 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
290 * but that problem is much larger than just in DSA.
292 if (!EVP_DigestFinal_ex(ctx->mdctx, digest, &dlen))
296 return ecdsa_sign(vctx, sig, siglen, sigsize, digest, (size_t)dlen);
299 int ecdsa_digest_verify_final(void *vctx, const unsigned char *sig,
302 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
303 unsigned char digest[EVP_MAX_MD_SIZE];
304 unsigned int dlen = 0;
306 if (!ossl_prov_is_running() || ctx == NULL || ctx->mdctx == NULL)
310 * TODO(3.0): There is the possibility that some externally provided
311 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
312 * but that problem is much larger than just in DSA.
314 if (!EVP_DigestFinal_ex(ctx->mdctx, digest, &dlen))
317 return ecdsa_verify(ctx, sig, siglen, digest, (size_t)dlen);
320 static void ecdsa_freectx(void *vctx)
322 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
325 EC_KEY_free(ctx->ec);
326 BN_clear_free(ctx->kinv);
327 BN_clear_free(ctx->r);
331 static void *ecdsa_dupctx(void *vctx)
333 PROV_ECDSA_CTX *srcctx = (PROV_ECDSA_CTX *)vctx;
334 PROV_ECDSA_CTX *dstctx;
336 if (!ossl_prov_is_running())
339 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
346 dstctx->mdctx = NULL;
348 if (srcctx->ec != NULL && !EC_KEY_up_ref(srcctx->ec))
350 /* Test KATS should not need to be supported */
351 if (srcctx->kinv != NULL || srcctx->r != NULL)
353 dstctx->ec = srcctx->ec;
355 if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
357 dstctx->md = srcctx->md;
359 if (srcctx->mdctx != NULL) {
360 dstctx->mdctx = EVP_MD_CTX_new();
361 if (dstctx->mdctx == NULL
362 || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
368 ecdsa_freectx(dstctx);
372 static int ecdsa_get_ctx_params(void *vctx, OSSL_PARAM *params)
374 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
377 if (ctx == NULL || params == NULL)
380 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
381 if (p != NULL && !OSSL_PARAM_set_octet_string(p, ctx->aid, ctx->aid_len))
384 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
385 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->mdsize))
388 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
389 if (p != NULL && !OSSL_PARAM_set_utf8_string(p, ctx->md == NULL
391 : EVP_MD_name(ctx->md)))
397 static const OSSL_PARAM known_gettable_ctx_params[] = {
398 OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
399 OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
400 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
404 static const OSSL_PARAM *ecdsa_gettable_ctx_params(ossl_unused void *provctx)
406 return known_gettable_ctx_params;
409 static int ecdsa_set_ctx_params(void *vctx, const OSSL_PARAM params[])
411 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
415 if (ctx == NULL || params == NULL)
418 if (ctx->md != NULL) {
420 * You cannot set the digest name/size when doing a DigestSign or
425 #if !defined(OPENSSL_NO_ACVP_TESTS)
426 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_KAT);
427 if (p != NULL && !OSSL_PARAM_get_uint(p, &ctx->kattest))
431 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
432 if (p != NULL && !OSSL_PARAM_get_size_t(p, &ctx->mdsize))
436 * We never actually use the mdname, but we do support getting it later.
437 * This can be useful for applications that want to know the MD that they
440 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
441 mdname = ctx->mdname;
443 && !OSSL_PARAM_get_utf8_string(p, &mdname, sizeof(ctx->mdname)))
449 static const OSSL_PARAM known_settable_ctx_params[] = {
450 OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
451 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
452 OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_KAT, NULL),
456 static const OSSL_PARAM *ecdsa_settable_ctx_params(ossl_unused void *provctx)
459 * TODO(3.0): Should this function return a different set of settable ctx
460 * params if the ctx is being used for a DigestSign/DigestVerify? In that
461 * case it is not allowed to set the digest size/digest name because the
462 * digest is explicitly set as part of the init.
464 return known_settable_ctx_params;
467 static int ecdsa_get_ctx_md_params(void *vctx, OSSL_PARAM *params)
469 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
471 if (ctx->mdctx == NULL)
474 return EVP_MD_CTX_get_params(ctx->mdctx, params);
477 static const OSSL_PARAM *ecdsa_gettable_ctx_md_params(void *vctx)
479 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
484 return EVP_MD_gettable_ctx_params(ctx->md);
487 static int ecdsa_set_ctx_md_params(void *vctx, const OSSL_PARAM params[])
489 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
491 if (ctx->mdctx == NULL)
494 return EVP_MD_CTX_set_params(ctx->mdctx, params);
497 static const OSSL_PARAM *ecdsa_settable_ctx_md_params(void *vctx)
499 PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
504 return EVP_MD_settable_ctx_params(ctx->md);
507 const OSSL_DISPATCH ecossl_dsa_signature_functions[] = {
508 { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))ecdsa_newctx },
509 { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))ecdsa_sign_init },
510 { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))ecdsa_sign },
511 { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))ecdsa_verify_init },
512 { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))ecdsa_verify },
513 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
514 (void (*)(void))ecdsa_digest_sign_init },
515 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
516 (void (*)(void))ecdsa_digest_signverify_update },
517 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
518 (void (*)(void))ecdsa_digest_sign_final },
519 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
520 (void (*)(void))ecdsa_digest_verify_init },
521 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
522 (void (*)(void))ecdsa_digest_signverify_update },
523 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
524 (void (*)(void))ecdsa_digest_verify_final },
525 { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))ecdsa_freectx },
526 { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))ecdsa_dupctx },
527 { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))ecdsa_get_ctx_params },
528 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
529 (void (*)(void))ecdsa_gettable_ctx_params },
530 { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))ecdsa_set_ctx_params },
531 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
532 (void (*)(void))ecdsa_settable_ctx_params },
533 { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
534 (void (*)(void))ecdsa_get_ctx_md_params },
535 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
536 (void (*)(void))ecdsa_gettable_ctx_md_params },
537 { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
538 (void (*)(void))ecdsa_set_ctx_md_params },
539 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
540 (void (*)(void))ecdsa_settable_ctx_md_params },