From 3108d934898ab00444295055e8c7c3c68188e227 Mon Sep 17 00:00:00 2001 From: Love Hornquist Astrand Date: Wed, 26 May 2010 10:45:15 -0500 Subject: [PATCH] add and use fp_isneg --- lib/hcrypto/dh-tfm.c | 2 +- lib/hcrypto/rsa-tfm.c | 10 ++++------ lib/hcrypto/tomsfastmath/src/headers/tfm.h | 3 +++ 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/hcrypto/dh-tfm.c b/lib/hcrypto/dh-tfm.c index f0fee9f14..d98f9c51c 100644 --- a/lib/hcrypto/dh-tfm.c +++ b/lib/hcrypto/dh-tfm.c @@ -168,7 +168,7 @@ tfm_dh_compute_key(unsigned char *shared, const BIGNUM * pub, DH *dh) BN2mpz(&peer_pub, pub); /* check if peers pubkey is reasonable */ - if (peer_pub.sign == FP_NEG + if (fp_isneg(&peer_pub) || fp_cmp(&peer_pub, &p) >= 0 || fp_cmp_d(&peer_pub, 1) <= 0) { diff --git a/lib/hcrypto/rsa-tfm.c b/lib/hcrypto/rsa-tfm.c index cc69df534..dc57de1e8 100644 --- a/lib/hcrypto/rsa-tfm.c +++ b/lib/hcrypto/rsa-tfm.c @@ -78,7 +78,7 @@ tfm_rsa_private_calculate(fp_int * in, fp_int * p, fp_int * q, /* C2 = 1/q mod p (iqmp) */ /* u = (vp - vq)C2 mod p. */ fp_sub(&vp, &vq, &u); - if (u.sign == FP_NEG) + if (fp_isneg(&u)) fp_add(&u, p, &u); fp_mul(&u, iqmp, &u); fp_mod(&u, p, &u); @@ -265,7 +265,7 @@ tfm_rsa_private_encrypt(int flen, const unsigned char* from, fp_read_unsigned_bin(&in, p0, size); free(p0); - if(in.sign == FP_NEG || + if(fp_isneg(&in) || fp_cmp(&in, &n) >= 0) { size = -3; goto out; @@ -337,8 +337,7 @@ tfm_rsa_private_decrypt(int flen, const unsigned char* from, fp_read_unsigned_bin(&in, rk_UNCONST(from), flen); - if(in.sign == FP_NEG || - fp_cmp(&in, &n) >= 0) { + if(fp_isneg(&in) || fp_cmp(&in, &n) >= 0) { size = -2; goto out; } @@ -364,8 +363,7 @@ tfm_rsa_private_decrypt(int flen, const unsigned char* from, } else { fp_int d; - if(in.sign == FP_NEG || - fp_cmp(&in, &n) >= 0) + if(fp_isneg(&in) || fp_cmp(&in, &n) >= 0) return -4; BN2mpz(&d, rsa->d); diff --git a/lib/hcrypto/tomsfastmath/src/headers/tfm.h b/lib/hcrypto/tomsfastmath/src/headers/tfm.h index 6bd259f52..7144d4910 100644 --- a/lib/hcrypto/tomsfastmath/src/headers/tfm.h +++ b/lib/hcrypto/tomsfastmath/src/headers/tfm.h @@ -310,6 +310,9 @@ void fp_init_multi(fp_int *a, ...); #define fp_iseven(a) (((a)->used >= 0 && (((a)->dp[0] & 1) == 0)) ? FP_YES : FP_NO) #define fp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? FP_YES : FP_NO) +/* is negative ? */ +#define fp_isneg(a) (((a)->sign) == FP_NEG) + /* set to a small digit */ void fp_set(fp_int *a, fp_digit b);