summaryrefslogtreecommitdiffstats
path: root/crypto/bn/bn_nist.c
diff options
context:
space:
mode:
authorBodo Möller <bodo@openssl.org>2002-10-28 14:02:19 +0000
committerBodo Möller <bodo@openssl.org>2002-10-28 14:02:19 +0000
commit19b8d06a7911d41ce8b3e347b4f58878e12d92ff (patch)
treedeb9c6b9a4d34e5fb9dac030f672c17e17ca4cbb /crypto/bn/bn_nist.c
parent5c6bf03117a26942327f43d02e9113e9870f7aba (diff)
clean up new code for NIST primes
create new lock CRYPTO_LOCK_BN to avoid race condition
Diffstat (limited to 'crypto/bn/bn_nist.c')
-rw-r--r--crypto/bn/bn_nist.c41
1 files changed, 28 insertions, 13 deletions
diff --git a/crypto/bn/bn_nist.c b/crypto/bn/bn_nist.c
index 4e21a05456..19bd540725 100644
--- a/crypto/bn/bn_nist.c
+++ b/crypto/bn/bn_nist.c
@@ -54,11 +54,12 @@
*/
#include "bn_lcl.h"
+#include "cryptlib.h"
#define BN_NIST_192_TOP (192+BN_BITS2-1)/BN_BITS2
#define BN_NIST_224_TOP (224+BN_BITS2-1)/BN_BITS2
#define BN_NIST_256_TOP (256+BN_BITS2-1)/BN_BITS2
-#define BN_NIST_384_TOP (384+BN_BITS2-1)/BN_BITS2
+#define BN_NIST_384_TOP (384+BN_BITS2-1)/BN_BITS2
#define BN_NIST_521_TOP (521+BN_BITS2-1)/BN_BITS2
#if BN_BITS2 == 64
@@ -314,7 +315,7 @@ int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
if (top == 0)
return BN_zero(r);
else if (top > 0)
- return (r == a)? 1 : !!BN_copy(r ,a);
+ return (r == a)? 1 : (BN_copy(r ,a) != NULL);
if (r != a)
if (!BN_ncopy(r, a, BN_NIST_192_TOP))
@@ -353,7 +354,7 @@ int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
}
r->top = BN_NIST_192_TOP;
-#if 0
+#if 1
bn_clear_top2max(r);
#endif
bn_fix_top(r);
@@ -393,7 +394,7 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
if (tmp_int == 0)
return BN_zero(r);
else if (tmp_int > 0)
- return (r == a)? 1 : !!BN_copy(r ,a);
+ return (r == a)? 1 : (BN_copy(r ,a) != NULL);
if (r != a)
if (!BN_ncopy(r, a, BN_NIST_224_TOP))
@@ -445,7 +446,7 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
}
r->top = BN_NIST_224_TOP;
-#if 0
+#if 1
bn_clear_top2max(r);
#endif
bn_fix_top(r);
@@ -503,13 +504,20 @@ int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
BN_32_BIT_BUF(14) BN_32_BIT_BUF(15)
if (!_is_set_256_data)
- _init_256_data();
-
+ {
+ CRYPTO_w_lock(CRYPTO_LOCK_BN);
+
+ if (!_is_set_256_data)
+ _init_256_data();
+
+ CRYPTO_w_unlock(CRYPTO_LOCK_BN);
+ }
+
tmp_int = BN_ucmp(field, a);
if (tmp_int == 0)
return BN_zero(r);
else if (tmp_int > 0)
- return (r == a)? 1 : !!BN_copy(r ,a);
+ return (r == a)? 1 : (BN_copy(r ,a) != NULL);
if (r != a)
if (!BN_ncopy(r, a, BN_NIST_256_TOP))
@@ -596,7 +604,7 @@ int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
}
r->top = BN_NIST_256_TOP;
-#if 0
+#if 1
bn_clear_top2max(r);
#endif
bn_fix_top(r);
@@ -657,13 +665,20 @@ int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
BN_32_BIT_BUF(22) BN_32_BIT_BUF(23)
if (!_is_set_384_data)
- _init_384_data();
+ {
+ CRYPTO_w_lock(CRYPTO_LOCK_BN);
+
+ if (!_is_set_384_data)
+ _init_384_data();
+
+ CRYPTO_w_unlock(CRYPTO_LOCK_BN);
+ }
tmp_int = BN_ucmp(field, a);
if (tmp_int == 0)
return BN_zero(r);
else if (tmp_int > 0)
- return (r == a)? 1 : !!BN_copy(r ,a);
+ return (r == a)? 1 : (BN_copy(r ,a) != NULL);
if (r != a)
if (!BN_ncopy(r, a, BN_NIST_384_TOP))
@@ -757,7 +772,7 @@ int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
}
r->top = BN_NIST_384_TOP;
-#if 0
+#if 1
bn_clear_top2max(r);
#endif
bn_fix_top(r);
@@ -793,7 +808,7 @@ int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
top = a->top;
if (top < BN_NIST_521_TOP || ( top == BN_NIST_521_TOP &&
(!(a->d[BN_NIST_521_TOP-1] & ~(BN_NIST_521_TOP_MASK)))))
- return (r == a)? 1 : !!BN_copy(r ,a);
+ return (r == a)? 1 : (BN_copy(r ,a) != NULL);
BN_CTX_start(ctx);
tmp = BN_CTX_get(ctx);