diff options
author | tb <> | 2025-02-04 05:09:53 +0000 |
---|---|---|
committer | tb <> | 2025-02-04 05:09:53 +0000 |
commit | a8409a544cf836e1e561b3794aeafbe161f747ed (patch) | |
tree | e46d4e590b1aea867a0ea7957b7c9fabf55dbe2d | |
parent | 1601236cd0e1fdd34e7cee2ecb19392a92d2f514 (diff) | |
download | openbsd-a8409a544cf836e1e561b3794aeafbe161f747ed.tar.gz openbsd-a8409a544cf836e1e561b3794aeafbe161f747ed.tar.bz2 openbsd-a8409a544cf836e1e561b3794aeafbe161f747ed.zip |
Start cleaning up BN_div_reciprocal() a bit
The fast path where no division is performed can be dealt with without
BN_CTX, so do that up front so there's no need to clean up before return.
Error check BN_CTX_get() on each use asd simplify the logic for optional
input parameters. KNF for an ugly comment.
ok jsing
-rw-r--r-- | src/lib/libcrypto/bn/bn_recp.c | 47 |
1 files changed, 23 insertions, 24 deletions
diff --git a/src/lib/libcrypto/bn/bn_recp.c b/src/lib/libcrypto/bn/bn_recp.c index 8f917e95db..757ed0c3d2 100644 --- a/src/lib/libcrypto/bn/bn_recp.c +++ b/src/lib/libcrypto/bn/bn_recp.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bn_recp.c,v 1.30 2025/01/22 12:53:16 tb Exp $ */ | 1 | /* $OpenBSD: bn_recp.c,v 1.31 2025/02/04 05:09:53 tb Exp $ */ |
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | * All rights reserved. | 3 | * All rights reserved. |
4 | * | 4 | * |
@@ -139,34 +139,33 @@ BN_div_reciprocal(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp, | |||
139 | int i, j, ret = 0; | 139 | int i, j, ret = 0; |
140 | BIGNUM *a, *b, *d, *r; | 140 | BIGNUM *a, *b, *d, *r; |
141 | 141 | ||
142 | if (BN_ucmp(m, recp->N) < 0) { | ||
143 | if (dv != NULL) | ||
144 | BN_zero(dv); | ||
145 | if (rem != NULL) | ||
146 | return bn_copy(rem, m); | ||
147 | return 1; | ||
148 | } | ||
149 | |||
142 | BN_CTX_start(ctx); | 150 | BN_CTX_start(ctx); |
143 | a = BN_CTX_get(ctx); | 151 | if ((a = BN_CTX_get(ctx)) == NULL) |
144 | b = BN_CTX_get(ctx); | 152 | goto err; |
145 | if (dv != NULL) | 153 | if ((b = BN_CTX_get(ctx)) == NULL) |
146 | d = dv; | 154 | goto err; |
147 | else | 155 | |
156 | if ((d = dv) == NULL) | ||
148 | d = BN_CTX_get(ctx); | 157 | d = BN_CTX_get(ctx); |
149 | if (rem != NULL) | 158 | if (d == NULL) |
150 | r = rem; | ||
151 | else | ||
152 | r = BN_CTX_get(ctx); | ||
153 | if (a == NULL || b == NULL || d == NULL || r == NULL) | ||
154 | goto err; | 159 | goto err; |
155 | 160 | ||
156 | if (BN_ucmp(m, recp->N) < 0) { | 161 | if ((r = rem) == NULL) |
157 | BN_zero(d); | 162 | r = BN_CTX_get(ctx); |
158 | if (!bn_copy(r, m)) { | 163 | if (r == NULL) |
159 | BN_CTX_end(ctx); | 164 | goto err; |
160 | return 0; | ||
161 | } | ||
162 | BN_CTX_end(ctx); | ||
163 | return 1; | ||
164 | } | ||
165 | 165 | ||
166 | /* We want the remainder | 166 | /* |
167 | * Given input of ABCDEF / ab | 167 | * We want the remainder. Given input of ABCDEF / ab we need to |
168 | * we need multiply ABCDEF by 3 digests of the reciprocal of ab | 168 | * multiply ABCDEF by 3 digits of the reciprocal of ab. |
169 | * | ||
170 | */ | 169 | */ |
171 | 170 | ||
172 | /* i := max(BN_num_bits(m), 2*BN_num_bits(N)) */ | 171 | /* i := max(BN_num_bits(m), 2*BN_num_bits(N)) */ |