summaryrefslogtreecommitdiff
path: root/crypto/bn/bn_exp.c
diff options
context:
space:
mode:
authorBodo Möller <bodo@openssl.org>2000-12-07 22:06:09 +0000
committerBodo Möller <bodo@openssl.org>2000-12-07 22:06:09 +0000
commit8dea52fa4270a71535b2677953662499946f02e3 (patch)
tree6c419fda8d18eac4d092e595ed5a087d6a89f1d0 /crypto/bn/bn_exp.c
parentf7356b677b35ad58ea2db85cfd22af83b0267978 (diff)
downloadopenssl-new-8dea52fa4270a71535b2677953662499946f02e3.tar.gz
Fix some things that look like bugs.
One problem that looked like a problem in bn_recp.c at first turned out to be a BN_mul bug. An example is given in bn_recp.c; finding the bug responsible for this is left as an exercise.
Diffstat (limited to 'crypto/bn/bn_exp.c')
-rw-r--r--crypto/bn/bn_exp.c22
1 files changed, 18 insertions, 4 deletions
diff --git a/crypto/bn/bn_exp.c b/crypto/bn/bn_exp.c
index 1739be6864..afdfd580fb 100644
--- a/crypto/bn/bn_exp.c
+++ b/crypto/bn/bn_exp.c
@@ -246,7 +246,17 @@ int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
if ((aa = BN_CTX_get(ctx)) == NULL) goto err;
BN_RECP_CTX_init(&recp);
- if (BN_RECP_CTX_set(&recp,m,ctx) <= 0) goto err;
+ if (m->neg)
+ {
+ /* ignore sign of 'm' */
+ if (!BN_copy(aa, m)) goto err;
+ aa->neg = 0;
+ if (BN_RECP_CTX_set(&recp,aa,ctx) <= 0) goto err;
+ }
+ else
+ {
+ if (BN_RECP_CTX_set(&recp,m,ctx) <= 0) goto err;
+ }
BN_init(&(val[0]));
ts=1;
@@ -497,9 +507,13 @@ int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,
(/* BN_ucmp(r, (m)) < 0 ? 1 :*/ \
(BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))
/* BN_MOD_MUL_WORD is only used with 'w' large,
- * so the BN_ucmp test is probably more overhead
- * than always using BN_mod (which uses BN_copy if
- * a similar test returns true). */
+ * so the BN_ucmp test is probably more overhead
+ * than always using BN_mod (which uses BN_copy if
+ * a similar test returns true). */
+ /* We can use BN_mod and do not need BN_nnmod because our
+ * accumulator is never negative (the result of BN_mod does
+ * not depend on the sign of the modulus).
+ */
#define BN_TO_MONTGOMERY_WORD(r, w, mont) \
(BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))