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 if (sig
== NULL
&& (kinv
== NULL
|| r
== NULL
)) {
81 *siglen
= ECDSA_size(eckey
);
85 s
= ECDSA_do_sign_ex(dgst
, dlen
, kinv
, r
, eckey
);
90 *siglen
= i2d_ECDSA_SIG(s
, sig
!= NULL
? &sig
: NULL
);
95 int ossl_ecdsa_deterministic_sign(const unsigned char *dgst
, int dlen
,
96 unsigned char *sig
, unsigned int *siglen
,
97 EC_KEY
*eckey
, unsigned int nonce_type
,
98 const char *digestname
,
99 OSSL_LIB_CTX
*libctx
, const char *propq
)
102 BIGNUM
*kinv
= NULL
, *r
= NULL
;
106 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
111 if (!ecdsa_sign_setup(eckey
, NULL
, &kinv
, &r
, dgst
, dlen
,
112 nonce_type
, digestname
, libctx
, propq
))
115 s
= ECDSA_do_sign_ex(dgst
, dlen
, kinv
, r
, eckey
);
119 *siglen
= i2d_ECDSA_SIG(s
, &sig
);
128 static int ecdsa_sign_setup(EC_KEY
*eckey
, BN_CTX
*ctx_in
,
129 BIGNUM
**kinvp
, BIGNUM
**rp
,
130 const unsigned char *dgst
, int dlen
,
131 unsigned int nonce_type
, const char *digestname
,
132 OSSL_LIB_CTX
*libctx
, const char *propq
)
135 BIGNUM
*k
= NULL
, *r
= NULL
, *X
= NULL
;
137 EC_POINT
*tmp_point
= NULL
;
138 const EC_GROUP
*group
;
141 const BIGNUM
*priv_key
;
143 if (eckey
== NULL
|| (group
= EC_KEY_get0_group(eckey
)) == NULL
) {
144 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
147 if ((priv_key
= EC_KEY_get0_private_key(eckey
)) == NULL
) {
148 ERR_raise(ERR_LIB_EC
, EC_R_MISSING_PRIVATE_KEY
);
152 if (!EC_KEY_can_sign(eckey
)) {
153 ERR_raise(ERR_LIB_EC
, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING
);
157 if ((ctx
= ctx_in
) == NULL
) {
158 if ((ctx
= BN_CTX_new_ex(eckey
->libctx
)) == NULL
) {
159 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
164 k
= BN_secure_new(); /* this value is later returned in *kinvp */
165 r
= BN_new(); /* this value is later returned in *rp */
167 if (k
== NULL
|| r
== NULL
|| X
== NULL
) {
168 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
171 if ((tmp_point
= EC_POINT_new(group
)) == NULL
) {
172 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
175 order
= EC_GROUP_get0_order(group
);
177 /* Preallocate space */
178 order_bits
= BN_num_bits(order
);
179 /* Check the number of bits here so that an infinite loop is not possible */
180 if (order_bits
< MIN_ECDSA_SIGN_ORDERBITS
181 || !BN_set_bit(k
, order_bits
)
182 || !BN_set_bit(r
, order_bits
)
183 || !BN_set_bit(X
, order_bits
))
187 /* get random or deterministic value of k */
192 if (nonce_type
== 1) {
194 res
= ossl_gen_deterministic_nonce_rfc6979(k
, order
,
201 res
= ossl_bn_gen_dsa_nonce_fixed_top(k
, order
, priv_key
,
205 res
= ossl_bn_priv_rand_range_fixed_top(k
, order
, 0, ctx
);
208 ERR_raise(ERR_LIB_EC
, EC_R_RANDOM_NUMBER_GENERATION_FAILED
);
211 } while (ossl_bn_is_word_fixed_top(k
, 0));
213 /* compute r the x-coordinate of generator * k */
214 if (!EC_POINT_mul(group
, tmp_point
, k
, NULL
, NULL
, ctx
)) {
215 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
219 if (!EC_POINT_get_affine_coordinates(group
, tmp_point
, X
, NULL
, ctx
)) {
220 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
224 if (!BN_nnmod(r
, X
, order
, ctx
)) {
225 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
228 } while (BN_is_zero(r
));
230 /* compute the inverse of k */
231 if (!ossl_ec_group_do_inverse_ord(group
, k
, k
, ctx
)) {
232 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
236 /* clear old values if necessary */
238 BN_clear_free(*kinvp
);
239 /* save the pre-computed values */
250 EC_POINT_free(tmp_point
);
255 int ossl_ecdsa_simple_sign_setup(EC_KEY
*eckey
, BN_CTX
*ctx_in
, BIGNUM
**kinvp
,
258 return ecdsa_sign_setup(eckey
, ctx_in
, kinvp
, rp
, NULL
, 0,
259 0, NULL
, NULL
, NULL
);
262 ECDSA_SIG
*ossl_ecdsa_simple_sign_sig(const unsigned char *dgst
, int dgst_len
,
263 const BIGNUM
*in_kinv
, const BIGNUM
*in_r
,
268 BIGNUM
*kinv
= NULL
, *s
, *m
= NULL
;
269 const BIGNUM
*order
, *ckinv
;
271 const EC_GROUP
*group
;
273 const BIGNUM
*priv_key
;
275 group
= EC_KEY_get0_group(eckey
);
276 priv_key
= EC_KEY_get0_private_key(eckey
);
279 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
282 if (priv_key
== NULL
) {
283 ERR_raise(ERR_LIB_EC
, EC_R_MISSING_PRIVATE_KEY
);
287 if (!EC_KEY_can_sign(eckey
)) {
288 ERR_raise(ERR_LIB_EC
, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING
);
292 ret
= ECDSA_SIG_new();
294 ERR_raise(ERR_LIB_EC
, ERR_R_ECDSA_LIB
);
299 if (ret
->r
== NULL
|| ret
->s
== NULL
) {
300 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
305 if ((ctx
= BN_CTX_new_ex(eckey
->libctx
)) == NULL
306 || (m
= BN_new()) == NULL
) {
307 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
311 order
= EC_GROUP_get0_order(group
);
312 i
= BN_num_bits(order
);
314 * Need to truncate digest if it is too long: first truncate whole bytes.
316 if (8 * dgst_len
> i
)
317 dgst_len
= (i
+ 7) / 8;
318 if (!BN_bin2bn(dgst
, dgst_len
, m
)) {
319 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
322 /* If still too long, truncate remaining bits with a shift */
323 if ((8 * dgst_len
> i
) && !BN_rshift(m
, m
, 8 - (i
& 0x7))) {
324 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
328 if (in_kinv
== NULL
|| in_r
== NULL
) {
329 if (!ecdsa_sign_setup(eckey
, ctx
, &kinv
, &ret
->r
, dgst
, dgst_len
,
330 0, NULL
, NULL
, NULL
)) {
331 ERR_raise(ERR_LIB_EC
, ERR_R_ECDSA_LIB
);
337 if (BN_copy(ret
->r
, in_r
) == NULL
) {
338 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
344 * With only one multiplicant being in Montgomery domain
345 * multiplication yields real result without post-conversion.
346 * Also note that all operations but last are performed with
347 * zero-padded vectors. Last operation, BN_mod_mul_montgomery
348 * below, returns user-visible value with removed zero padding.
350 if (!bn_to_mont_fixed_top(s
, ret
->r
, group
->mont_data
, ctx
)
351 || !bn_mul_mont_fixed_top(s
, s
, priv_key
, group
->mont_data
, ctx
)) {
352 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
355 if (!bn_mod_add_fixed_top(s
, s
, m
, order
)) {
356 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
360 * |s| can still be larger than modulus, because |m| can be. In
361 * such case we count on Montgomery reduction to tie it up.
363 if (!bn_to_mont_fixed_top(s
, s
, group
->mont_data
, ctx
)
364 || !BN_mod_mul_montgomery(s
, s
, ckinv
, group
->mont_data
, ctx
)) {
365 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
371 * if kinv and r have been supplied by the caller, don't
372 * generate new kinv and r values
374 if (in_kinv
!= NULL
&& in_r
!= NULL
) {
375 ERR_raise(ERR_LIB_EC
, EC_R_NEED_NEW_SETUP_VALUES
);
378 /* Avoid infinite loops cause by invalid group parameters */
379 if (retries
++ > MAX_ECDSA_SIGN_RETRIES
) {
380 ERR_raise(ERR_LIB_EC
, EC_R_TOO_MANY_RETRIES
);
384 /* s != 0 => we have a valid signature */
403 * 1: correct signature
404 * 0: incorrect signature
407 int ossl_ecdsa_verify(int type
, const unsigned char *dgst
, int dgst_len
,
408 const unsigned char *sigbuf
, int sig_len
, EC_KEY
*eckey
)
411 const unsigned char *p
= sigbuf
;
412 unsigned char *der
= NULL
;
419 if (d2i_ECDSA_SIG(&s
, &p
, sig_len
) == NULL
)
421 /* Ensure signature uses DER and doesn't have trailing garbage */
422 derlen
= i2d_ECDSA_SIG(s
, &der
);
423 if (derlen
!= sig_len
|| memcmp(sigbuf
, der
, derlen
) != 0)
425 ret
= ECDSA_do_verify(dgst
, dgst_len
, s
, eckey
);
432 int ossl_ecdsa_simple_verify_sig(const unsigned char *dgst
, int dgst_len
,
433 const ECDSA_SIG
*sig
, EC_KEY
*eckey
)
438 BIGNUM
*u1
, *u2
, *m
, *X
;
439 EC_POINT
*point
= NULL
;
440 const EC_GROUP
*group
;
441 const EC_POINT
*pub_key
;
443 /* check input values */
444 if (eckey
== NULL
|| (group
= EC_KEY_get0_group(eckey
)) == NULL
||
445 (pub_key
= EC_KEY_get0_public_key(eckey
)) == NULL
|| sig
== NULL
) {
446 ERR_raise(ERR_LIB_EC
, EC_R_MISSING_PARAMETERS
);
450 if (!EC_KEY_can_sign(eckey
)) {
451 ERR_raise(ERR_LIB_EC
, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING
);
455 ctx
= BN_CTX_new_ex(eckey
->libctx
);
457 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
461 u1
= BN_CTX_get(ctx
);
462 u2
= BN_CTX_get(ctx
);
466 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
470 order
= EC_GROUP_get0_order(group
);
472 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
476 if (BN_is_zero(sig
->r
) || BN_is_negative(sig
->r
) ||
477 BN_ucmp(sig
->r
, order
) >= 0 || BN_is_zero(sig
->s
) ||
478 BN_is_negative(sig
->s
) || BN_ucmp(sig
->s
, order
) >= 0) {
479 ERR_raise(ERR_LIB_EC
, EC_R_BAD_SIGNATURE
);
480 ret
= 0; /* signature is invalid */
483 /* calculate tmp1 = inv(S) mod order */
484 if (!ossl_ec_group_do_inverse_ord(group
, u2
, sig
->s
, ctx
)) {
485 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
489 i
= BN_num_bits(order
);
491 * Need to truncate digest if it is too long: first truncate whole bytes.
493 if (8 * dgst_len
> i
)
494 dgst_len
= (i
+ 7) / 8;
495 if (!BN_bin2bn(dgst
, dgst_len
, m
)) {
496 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
499 /* If still too long truncate remaining bits with a shift */
500 if ((8 * dgst_len
> i
) && !BN_rshift(m
, m
, 8 - (i
& 0x7))) {
501 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
504 /* u1 = m * tmp mod order */
505 if (!BN_mod_mul(u1
, m
, u2
, order
, ctx
)) {
506 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
509 /* u2 = r * w mod q */
510 if (!BN_mod_mul(u2
, sig
->r
, u2
, order
, ctx
)) {
511 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
515 if ((point
= EC_POINT_new(group
)) == NULL
) {
516 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
519 if (!EC_POINT_mul(group
, point
, u1
, pub_key
, u2
, ctx
)) {
520 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
524 if (!EC_POINT_get_affine_coordinates(group
, point
, X
, NULL
, ctx
)) {
525 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
529 if (!BN_nnmod(u1
, X
, order
, ctx
)) {
530 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
533 /* if the signature is correct u1 is equal to sig->r */
534 ret
= (BN_ucmp(u1
, sig
->r
) == 0);
538 EC_POINT_free(point
);