diff options
author | tb <> | 2025-02-04 12:47:58 +0000 |
---|---|---|
committer | tb <> | 2025-02-04 12:47:58 +0000 |
commit | 587eb12f2939f2de7ec3b59d80ce14dfa6c9e436 (patch) | |
tree | d33ee17c950899cea94f810f605dd3ce55ee8f7b | |
parent | a8409a544cf836e1e561b3794aeafbe161f747ed (diff) | |
download | openbsd-587eb12f2939f2de7ec3b59d80ce14dfa6c9e436.tar.gz openbsd-587eb12f2939f2de7ec3b59d80ce14dfa6c9e436.tar.bz2 openbsd-587eb12f2939f2de7ec3b59d80ce14dfa6c9e436.zip |
Inline BN_reciprocal() in its only caller
This is simpler, doesn't need an auxiliary function of dubious value,
avouds an auxiliary variable and gets rid of a bunch of comments that
are hard to make sense of.
This doesn't bother to invalidate recp->shift since on error you should
not be reusing the RECP_CTX without reinitializing it.
ok jsing
-rw-r--r-- | src/lib/libcrypto/bn/bn_recp.c | 46 |
1 files changed, 10 insertions, 36 deletions
diff --git a/src/lib/libcrypto/bn/bn_recp.c b/src/lib/libcrypto/bn/bn_recp.c index 757ed0c3d2..d5070bc003 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.31 2025/02/04 05:09:53 tb Exp $ */ | 1 | /* $OpenBSD: bn_recp.c,v 1.32 2025/02/04 12:47:58 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 | * |
@@ -104,34 +104,6 @@ BN_RECP_CTX_free(BN_RECP_CTX *recp) | |||
104 | freezero(recp, sizeof(*recp)); | 104 | freezero(recp, sizeof(*recp)); |
105 | } | 105 | } |
106 | 106 | ||
107 | /* len is the expected size of the result | ||
108 | * We actually calculate with an extra word of precision, so | ||
109 | * we can do faster division if the remainder is not required. | ||
110 | */ | ||
111 | /* r := 2^len / m */ | ||
112 | static int | ||
113 | BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx) | ||
114 | { | ||
115 | int ret = -1; | ||
116 | BIGNUM *t; | ||
117 | |||
118 | BN_CTX_start(ctx); | ||
119 | if ((t = BN_CTX_get(ctx)) == NULL) | ||
120 | goto err; | ||
121 | |||
122 | if (!BN_set_bit(t, len)) | ||
123 | goto err; | ||
124 | |||
125 | if (!BN_div_ct(r, NULL, t, m, ctx)) | ||
126 | goto err; | ||
127 | |||
128 | ret = len; | ||
129 | |||
130 | err: | ||
131 | BN_CTX_end(ctx); | ||
132 | return ret; | ||
133 | } | ||
134 | |||
135 | int | 107 | int |
136 | BN_div_reciprocal(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp, | 108 | BN_div_reciprocal(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp, |
137 | BN_CTX *ctx) | 109 | BN_CTX *ctx) |
@@ -174,13 +146,15 @@ BN_div_reciprocal(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp, | |||
174 | if (j > i) | 146 | if (j > i) |
175 | i = j; | 147 | i = j; |
176 | 148 | ||
177 | /* Nr := round(2^i / N) */ | 149 | /* Compute Nr := (1 << i) / N if necessary. */ |
178 | if (i != recp->shift) | 150 | if (i != recp->shift) { |
179 | recp->shift = BN_reciprocal(recp->Nr, recp->N, i, ctx); | 151 | BN_zero(recp->Nr); |
180 | 152 | if (!BN_set_bit(recp->Nr, i)) | |
181 | /* BN_reciprocal returns i, or -1 for an error */ | 153 | goto err; |
182 | if (recp->shift == -1) | 154 | if (!BN_div_ct(recp->Nr, NULL, recp->Nr, recp->N, ctx)) |
183 | goto err; | 155 | goto err; |
156 | recp->shift = i; | ||
157 | } | ||
184 | 158 | ||
185 | /* d := |round(round(m / 2^BN_num_bits(N)) * recp->Nr / 2^(i - BN_num_bits(N)))| | 159 | /* d := |round(round(m / 2^BN_num_bits(N)) * recp->Nr / 2^(i - BN_num_bits(N)))| |
186 | * = |round(round(m / 2^BN_num_bits(N)) * round(2^i / N) / 2^(i - BN_num_bits(N)))| | 160 | * = |round(round(m / 2^BN_num_bits(N)) * round(2^i / N) / 2^(i - BN_num_bits(N)))| |