Skip to content

Commit 988c9bc

Browse files
author
tb
committed
Avoid infinite loop for custom curves of order 1
If a private key encoded with EC parameters happens to have order 1 and is used for ECDSA signatures, this causes an infinite loop since a random integer x in the interval [0,1) will be 0, so do ... while (x == 0); will loop indefinitely. Found and reported with a reproducer by Hanno Boeck. Helpful comments and analysis from David Benjamin. ok beck jsing
1 parent 6948bf3 commit 988c9bc

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/lib/libcrypto/ec/ec_lib.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: ec_lib.c,v 1.44 2022/03/29 14:03:12 tb Exp $ */
1+
/* $OpenBSD: ec_lib.c,v 1.45 2022/04/07 17:37:25 tb Exp $ */
22
/*
33
* Originally written by Bodo Moeller for the OpenSSL project.
44
*/
@@ -348,10 +348,10 @@ EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
348348
}
349349

350350
/*
351-
* Require order >= 1 and enforce an upper bound of at most one bit more
351+
* Require order > 1 and enforce an upper bound of at most one bit more
352352
* than the field cardinality due to Hasse's theorem.
353353
*/
354-
if (order == NULL || BN_is_zero(order) || BN_is_negative(order) ||
354+
if (order == NULL || BN_cmp(order, BN_value_one()) <= 0 ||
355355
BN_num_bits(order) > BN_num_bits(&group->field) + 1) {
356356
ECerror(EC_R_INVALID_GROUP_ORDER);
357357
return 0;

src/lib/libcrypto/ecdsa/ecs_ossl.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: ecs_ossl.c,v 1.23 2022/01/20 11:03:48 inoguchi Exp $ */
1+
/* $OpenBSD: ecs_ossl.c,v 1.24 2022/04/07 17:37:25 tb Exp $ */
22
/*
33
* Written by Nils Larsch for the OpenSSL project
44
*/
@@ -163,6 +163,11 @@ ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
163163
goto err;
164164
}
165165

166+
if (BN_cmp(order, BN_value_one()) <= 0) {
167+
ECDSAerror(EC_R_INVALID_GROUP_ORDER);
168+
goto err;
169+
}
170+
166171
/* Preallocate space. */
167172
order_bits = BN_num_bits(order);
168173
if (!BN_set_bit(k, order_bits) ||

0 commit comments

Comments
 (0)