From e680fe5b2098d1406fab3bb3994254f026651090 Mon Sep 17 00:00:00 2001 From: tb <> Date: Thu, 3 Aug 2023 18:53:56 +0000 Subject: Make the bn_rand_interval() API a bit more ergonomic Provide bn_rand_in_range() which is a slightly tweaked version of what was previously called bn_rand_range(). The way bn_rand_range() is called in libcrypto, the lower bound is always expressible as a word. In fact, most of the time it is 1, the DH code uses a 2, the MR tests in BPSW use 3 and an exceptinally high number appears in the Tonelli-Shanks implementation where we use 32. Converting these lower bounds to BIGNUMs on the call site is annoying so let bn_rand_interval() do that internally and route that through bn_rand_in_range(). This way we can avoid using BN_sub_word(). Adjust the bn_isqrt() test to use bn_rand_in_range() since that's the only caller that uses actual BIGNUMs as lower bounds. ok jsing --- src/lib/libcrypto/dsa/dsa_ossl.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/lib/libcrypto/dsa/dsa_ossl.c') diff --git a/src/lib/libcrypto/dsa/dsa_ossl.c b/src/lib/libcrypto/dsa/dsa_ossl.c index 36b2a63462..b92d0b8cee 100644 --- a/src/lib/libcrypto/dsa/dsa_ossl.c +++ b/src/lib/libcrypto/dsa/dsa_ossl.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dsa_ossl.c,v 1.52 2023/07/08 14:28:15 beck Exp $ */ +/* $OpenBSD: dsa_ossl.c,v 1.53 2023/08/03 18:53:55 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -172,7 +172,7 @@ dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) * * Where b is a random value in the range [1, q). */ - if (!bn_rand_interval(b, BN_value_one(), dsa->q)) + if (!bn_rand_interval(b, 1, dsa->q)) goto err; if (BN_mod_inverse_ct(binv, b, dsa->q, ctx) == NULL) goto err; @@ -261,7 +261,7 @@ dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) !BN_set_bit(m, q_bits)) goto err; - if (!bn_rand_interval(k, BN_value_one(), dsa->q)) + if (!bn_rand_interval(k, 1, dsa->q)) goto err; BN_set_flags(k, BN_FLG_CONSTTIME); -- cgit v1.2.3-55-g6feb