2 * Copyright 2002-2023 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"
17 #include <openssl/err.h>
18 #include <openssl/obj_mac.h>
19 #include <openssl/rand.h>
20 #include "crypto/bn.h"
22 #include "internal/deterministic_nonce.h"
24 #define MIN_ECDSA_SIGN_ORDERBITS 64
26 * It is highly unlikely that a retry will happen,
27 * Multiple retries would indicate that something is wrong
28 * with the group parameters (which would normally only happen
29 * with a bad custom group).
31 #define MAX_ECDSA_SIGN_RETRIES 8
33 static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
34 BIGNUM **kinvp, BIGNUM **rp,
35 const unsigned char *dgst, int dlen,
36 unsigned int nonce_type, const char *digestname,
37 OSSL_LIB_CTX *libctx, const char *propq);
39 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
42 if (eckey->group->meth->ecdsa_sign_setup == NULL) {
43 ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
47 return eckey->group->meth->ecdsa_sign_setup(eckey, ctx_in, kinvp, rp);
50 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
51 const BIGNUM *in_kinv, const BIGNUM *in_r,
54 if (eckey->group->meth->ecdsa_sign_sig == NULL) {
55 ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
59 return eckey->group->meth->ecdsa_sign_sig(dgst, dgst_len,
60 in_kinv, in_r, eckey);
63 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
64 const ECDSA_SIG *sig, EC_KEY *eckey)
66 if (eckey->group->meth->ecdsa_verify_sig == NULL) {
67 ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
71 return eckey->group->meth->ecdsa_verify_sig(dgst, dgst_len, sig, eckey);
74 int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
75 unsigned char *sig, unsigned int *siglen,
76 const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey)
80 s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
85 *siglen = i2d_ECDSA_SIG(s, sig != NULL ? &sig : NULL);
90 int ossl_ecdsa_deterministic_sign(const unsigned char *dgst, int dlen,
91 unsigned char *sig, unsigned int *siglen,
92 EC_KEY *eckey, unsigned int nonce_type,
93 const char *digestname,
94 OSSL_LIB_CTX *libctx, const char *propq)
97 BIGNUM *kinv = NULL, *r = NULL;
101 if (!ecdsa_sign_setup(eckey, NULL, &kinv, &r, dgst, dlen,
102 nonce_type, digestname, libctx, propq))
105 s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
109 *siglen = i2d_ECDSA_SIG(s, sig != NULL ? &sig : NULL);
118 static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
119 BIGNUM **kinvp, BIGNUM **rp,
120 const unsigned char *dgst, int dlen,
121 unsigned int nonce_type, const char *digestname,
122 OSSL_LIB_CTX *libctx, const char *propq)
125 BIGNUM *k = NULL, *r = NULL, *X = NULL;
127 EC_POINT *tmp_point = NULL;
128 const EC_GROUP *group;
131 const BIGNUM *priv_key;
133 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
134 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
137 if ((priv_key = EC_KEY_get0_private_key(eckey)) == NULL) {
138 ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
142 if (!EC_KEY_can_sign(eckey)) {
143 ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
147 if ((ctx = ctx_in) == NULL) {
148 if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL) {
149 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
154 k = BN_secure_new(); /* this value is later returned in *kinvp */
155 r = BN_new(); /* this value is later returned in *rp */
157 if (k == NULL || r == NULL || X == NULL) {
158 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
161 if ((tmp_point = EC_POINT_new(group)) == NULL) {
162 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
165 order = EC_GROUP_get0_order(group);
167 /* Preallocate space */
168 order_bits = BN_num_bits(order);
169 /* Check the number of bits here so that an infinite loop is not possible */
170 if (order_bits < MIN_ECDSA_SIGN_ORDERBITS
171 || !BN_set_bit(k, order_bits)
172 || !BN_set_bit(r, order_bits)
173 || !BN_set_bit(X, order_bits))
177 /* get random or deterministic value of k */
182 if (nonce_type == 1) {
184 res = ossl_gen_deterministic_nonce_rfc6979(k, order,
191 res = BN_generate_dsa_nonce(k, order, priv_key, dgst, dlen,
195 res = BN_priv_rand_range_ex(k, order, 0, ctx);
198 ERR_raise(ERR_LIB_EC, EC_R_RANDOM_NUMBER_GENERATION_FAILED);
201 } while (BN_is_zero(k));
203 /* compute r the x-coordinate of generator * k */
204 if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
205 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
209 if (!EC_POINT_get_affine_coordinates(group, tmp_point, X, NULL, ctx)) {
210 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
214 if (!BN_nnmod(r, X, order, ctx)) {
215 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
218 } while (BN_is_zero(r));
220 /* compute the inverse of k */
221 if (!ossl_ec_group_do_inverse_ord(group, k, k, ctx)) {
222 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
226 /* clear old values if necessary */
228 BN_clear_free(*kinvp);
229 /* save the pre-computed values */
240 EC_POINT_free(tmp_point);
245 int ossl_ecdsa_simple_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
248 return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0,
249 0, NULL, NULL, NULL);
252 ECDSA_SIG *ossl_ecdsa_simple_sign_sig(const unsigned char *dgst, int dgst_len,
253 const BIGNUM *in_kinv, const BIGNUM *in_r,
258 BIGNUM *kinv = NULL, *s, *m = NULL;
259 const BIGNUM *order, *ckinv;
261 const EC_GROUP *group;
263 const BIGNUM *priv_key;
265 group = EC_KEY_get0_group(eckey);
266 priv_key = EC_KEY_get0_private_key(eckey);
269 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
272 if (priv_key == NULL) {
273 ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
277 if (!EC_KEY_can_sign(eckey)) {
278 ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
282 ret = ECDSA_SIG_new();
284 ERR_raise(ERR_LIB_EC, ERR_R_ECDSA_LIB);
289 if (ret->r == NULL || ret->s == NULL) {
290 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
295 if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL
296 || (m = BN_new()) == NULL) {
297 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
301 order = EC_GROUP_get0_order(group);
302 i = BN_num_bits(order);
304 * Need to truncate digest if it is too long: first truncate whole bytes.
306 if (8 * dgst_len > i)
307 dgst_len = (i + 7) / 8;
308 if (!BN_bin2bn(dgst, dgst_len, m)) {
309 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
312 /* If still too long, truncate remaining bits with a shift */
313 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
314 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
318 if (in_kinv == NULL || in_r == NULL) {
319 if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len,
320 0, NULL, NULL, NULL)) {
321 ERR_raise(ERR_LIB_EC, ERR_R_ECDSA_LIB);
327 if (BN_copy(ret->r, in_r) == NULL) {
328 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
334 * With only one multiplicant being in Montgomery domain
335 * multiplication yields real result without post-conversion.
336 * Also note that all operations but last are performed with
337 * zero-padded vectors. Last operation, BN_mod_mul_montgomery
338 * below, returns user-visible value with removed zero padding.
340 if (!bn_to_mont_fixed_top(s, ret->r, group->mont_data, ctx)
341 || !bn_mul_mont_fixed_top(s, s, priv_key, group->mont_data, ctx)) {
342 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
345 if (!bn_mod_add_fixed_top(s, s, m, order)) {
346 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
350 * |s| can still be larger than modulus, because |m| can be. In
351 * such case we count on Montgomery reduction to tie it up.
353 if (!bn_to_mont_fixed_top(s, s, group->mont_data, ctx)
354 || !BN_mod_mul_montgomery(s, s, ckinv, group->mont_data, ctx)) {
355 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
361 * if kinv and r have been supplied by the caller, don't
362 * generate new kinv and r values
364 if (in_kinv != NULL && in_r != NULL) {
365 ERR_raise(ERR_LIB_EC, EC_R_NEED_NEW_SETUP_VALUES);
368 /* Avoid infinite loops cause by invalid group parameters */
369 if (retries++ > MAX_ECDSA_SIGN_RETRIES) {
370 ERR_raise(ERR_LIB_EC, EC_R_TOO_MANY_RETRIES);
374 /* s != 0 => we have a valid signature */
393 * 1: correct signature
394 * 0: incorrect signature
397 int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
398 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
401 const unsigned char *p = sigbuf;
402 unsigned char *der = NULL;
409 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
411 /* Ensure signature uses DER and doesn't have trailing garbage */
412 derlen = i2d_ECDSA_SIG(s, &der);
413 if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
415 ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
422 int ossl_ecdsa_simple_verify_sig(const unsigned char *dgst, int dgst_len,
423 const ECDSA_SIG *sig, EC_KEY *eckey)
428 BIGNUM *u1, *u2, *m, *X;
429 EC_POINT *point = NULL;
430 const EC_GROUP *group;
431 const EC_POINT *pub_key;
433 /* check input values */
434 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
435 (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
436 ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
440 if (!EC_KEY_can_sign(eckey)) {
441 ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
445 ctx = BN_CTX_new_ex(eckey->libctx);
447 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
451 u1 = BN_CTX_get(ctx);
452 u2 = BN_CTX_get(ctx);
456 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
460 order = EC_GROUP_get0_order(group);
462 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
466 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
467 BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
468 BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
469 ERR_raise(ERR_LIB_EC, EC_R_BAD_SIGNATURE);
470 ret = 0; /* signature is invalid */
473 /* calculate tmp1 = inv(S) mod order */
474 if (!ossl_ec_group_do_inverse_ord(group, u2, sig->s, ctx)) {
475 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
479 i = BN_num_bits(order);
481 * Need to truncate digest if it is too long: first truncate whole bytes.
483 if (8 * dgst_len > i)
484 dgst_len = (i + 7) / 8;
485 if (!BN_bin2bn(dgst, dgst_len, m)) {
486 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
489 /* If still too long truncate remaining bits with a shift */
490 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
491 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
494 /* u1 = m * tmp mod order */
495 if (!BN_mod_mul(u1, m, u2, order, ctx)) {
496 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
499 /* u2 = r * w mod q */
500 if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
501 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
505 if ((point = EC_POINT_new(group)) == NULL) {
506 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
509 if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
510 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
514 if (!EC_POINT_get_affine_coordinates(group, point, X, NULL, ctx)) {
515 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
519 if (!BN_nnmod(u1, X, order, ctx)) {
520 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
523 /* if the signature is correct u1 is equal to sig->r */
524 ret = (BN_ucmp(u1, sig->r) == 0);
528 EC_POINT_free(point);