diff options
Diffstat (limited to 'lib/hcrypto/libtommath')
-rw-r--r-- | lib/hcrypto/libtommath/bn_error.c | 4 | ||||
-rw-r--r-- | lib/hcrypto/libtommath/bn_mp_find_prime.c | 18 | ||||
-rw-r--r-- | lib/hcrypto/libtommath/bn_mp_isprime.c | 75 | ||||
-rw-r--r-- | lib/hcrypto/libtommath/bn_mp_mod.c | 8 | ||||
-rw-r--r-- | lib/hcrypto/libtommath/bn_mp_prime_next_prime.c | 2 | ||||
-rw-r--r-- | lib/hcrypto/libtommath/bn_mp_rand.c | 4 | ||||
-rw-r--r-- | lib/hcrypto/libtommath/bn_mp_shrink.c | 11 | ||||
-rw-r--r-- | lib/hcrypto/libtommath/tommath.h | 10 | ||||
-rw-r--r-- | lib/hcrypto/libtommath/tommath_class.h | 2 |
9 files changed, 37 insertions, 97 deletions
diff --git a/lib/hcrypto/libtommath/bn_error.c b/lib/hcrypto/libtommath/bn_error.c index b1b7177e61f0..fbba7aa1fb14 100644 --- a/lib/hcrypto/libtommath/bn_error.c +++ b/lib/hcrypto/libtommath/bn_error.c @@ -17,7 +17,7 @@ static const struct { int code; - char *msg; + const char *msg; } msgs[] = { { MP_OKAY, "Successful" }, { MP_MEM, "Out of heap" }, @@ -25,7 +25,7 @@ static const struct { }; /* return a char * string for a given code */ -char *mp_error_to_string(int code) +const char *mp_error_to_string(int code) { int x; diff --git a/lib/hcrypto/libtommath/bn_mp_find_prime.c b/lib/hcrypto/libtommath/bn_mp_find_prime.c index ef7b6532c5cd..29ef8747d623 100644 --- a/lib/hcrypto/libtommath/bn_mp_find_prime.c +++ b/lib/hcrypto/libtommath/bn_mp_find_prime.c @@ -5,17 +5,26 @@ * Love Hornquist Astrand <lha@h5l.org> */ #include <tommath.h> - -int mp_find_prime(mp_int *a) +#ifdef BN_MP_FIND_PRIME_C +int mp_find_prime(mp_int *a, int t) { - int res; + int res = MP_NO; + + /* valid value of t? */ + if (t <= 0 || t > PRIME_SIZE) { + return MP_VAL; + } if (mp_iseven(a)) mp_add_d(a, 1, a); do { + if (mp_prime_is_prime(a, t, &res) != 0) { + res = MP_VAL; + break; + } - if ((res = mp_isprime(a)) == MP_NO) { + if (res == MP_NO) { mp_add_d(a, 2, a); continue; } @@ -24,3 +33,4 @@ int mp_find_prime(mp_int *a) return res; } +#endif diff --git a/lib/hcrypto/libtommath/bn_mp_isprime.c b/lib/hcrypto/libtommath/bn_mp_isprime.c deleted file mode 100644 index d3678d5dc11a..000000000000 --- a/lib/hcrypto/libtommath/bn_mp_isprime.c +++ /dev/null @@ -1,75 +0,0 @@ -/* TomsFastMath, a fast ISO C bignum library. - * - * This project is meant to fill in where LibTomMath - * falls short. That is speed ;-) - * - * This project is public domain and free for all purposes. - * - * Tom St Denis, tomstdenis@gmail.com - */ -#include <tommath.h> - -/* a few primes */ -static const mp_digit primes[256] = { - 0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013, - 0x0017, 0x001D, 0x001F, 0x0025, 0x0029, 0x002B, 0x002F, 0x0035, - 0x003B, 0x003D, 0x0043, 0x0047, 0x0049, 0x004F, 0x0053, 0x0059, - 0x0061, 0x0065, 0x0067, 0x006B, 0x006D, 0x0071, 0x007F, 0x0083, - 0x0089, 0x008B, 0x0095, 0x0097, 0x009D, 0x00A3, 0x00A7, 0x00AD, - 0x00B3, 0x00B5, 0x00BF, 0x00C1, 0x00C5, 0x00C7, 0x00D3, 0x00DF, - 0x00E3, 0x00E5, 0x00E9, 0x00EF, 0x00F1, 0x00FB, 0x0101, 0x0107, - 0x010D, 0x010F, 0x0115, 0x0119, 0x011B, 0x0125, 0x0133, 0x0137, - - 0x0139, 0x013D, 0x014B, 0x0151, 0x015B, 0x015D, 0x0161, 0x0167, - 0x016F, 0x0175, 0x017B, 0x017F, 0x0185, 0x018D, 0x0191, 0x0199, - 0x01A3, 0x01A5, 0x01AF, 0x01B1, 0x01B7, 0x01BB, 0x01C1, 0x01C9, - 0x01CD, 0x01CF, 0x01D3, 0x01DF, 0x01E7, 0x01EB, 0x01F3, 0x01F7, - 0x01FD, 0x0209, 0x020B, 0x021D, 0x0223, 0x022D, 0x0233, 0x0239, - 0x023B, 0x0241, 0x024B, 0x0251, 0x0257, 0x0259, 0x025F, 0x0265, - 0x0269, 0x026B, 0x0277, 0x0281, 0x0283, 0x0287, 0x028D, 0x0293, - 0x0295, 0x02A1, 0x02A5, 0x02AB, 0x02B3, 0x02BD, 0x02C5, 0x02CF, - - 0x02D7, 0x02DD, 0x02E3, 0x02E7, 0x02EF, 0x02F5, 0x02F9, 0x0301, - 0x0305, 0x0313, 0x031D, 0x0329, 0x032B, 0x0335, 0x0337, 0x033B, - 0x033D, 0x0347, 0x0355, 0x0359, 0x035B, 0x035F, 0x036D, 0x0371, - 0x0373, 0x0377, 0x038B, 0x038F, 0x0397, 0x03A1, 0x03A9, 0x03AD, - 0x03B3, 0x03B9, 0x03C7, 0x03CB, 0x03D1, 0x03D7, 0x03DF, 0x03E5, - 0x03F1, 0x03F5, 0x03FB, 0x03FD, 0x0407, 0x0409, 0x040F, 0x0419, - 0x041B, 0x0425, 0x0427, 0x042D, 0x043F, 0x0443, 0x0445, 0x0449, - 0x044F, 0x0455, 0x045D, 0x0463, 0x0469, 0x047F, 0x0481, 0x048B, - - 0x0493, 0x049D, 0x04A3, 0x04A9, 0x04B1, 0x04BD, 0x04C1, 0x04C7, - 0x04CD, 0x04CF, 0x04D5, 0x04E1, 0x04EB, 0x04FD, 0x04FF, 0x0503, - 0x0509, 0x050B, 0x0511, 0x0515, 0x0517, 0x051B, 0x0527, 0x0529, - 0x052F, 0x0551, 0x0557, 0x055D, 0x0565, 0x0577, 0x0581, 0x058F, - 0x0593, 0x0595, 0x0599, 0x059F, 0x05A7, 0x05AB, 0x05AD, 0x05B3, - 0x05BF, 0x05C9, 0x05CB, 0x05CF, 0x05D1, 0x05D5, 0x05DB, 0x05E7, - 0x05F3, 0x05FB, 0x0607, 0x060D, 0x0611, 0x0617, 0x061F, 0x0623, - 0x062B, 0x062F, 0x063D, 0x0641, 0x0647, 0x0649, 0x064D, 0x0653 -}; - -int mp_isprime(mp_int *a) -{ - mp_int b; - mp_digit d; - int r, res; - - /* do trial division */ - for (r = 0; r < 256; r++) { - mp_mod_d(a, primes[r], &d); - if (d == 0) { - return MP_NO; - } - } - - /* now do 8 miller rabins */ - mp_init(&b); - for (r = 0; r < 128; r++) { - mp_set(&b, primes[r]); - mp_prime_miller_rabin(a, &b, &res); - if (res == MP_NO) { - return MP_NO; - } - } - return MP_YES; -} diff --git a/lib/hcrypto/libtommath/bn_mp_mod.c b/lib/hcrypto/libtommath/bn_mp_mod.c index f5cf8d09f2dc..757335aebc5b 100644 --- a/lib/hcrypto/libtommath/bn_mp_mod.c +++ b/lib/hcrypto/libtommath/bn_mp_mod.c @@ -15,7 +15,7 @@ * Tom St Denis, tomstdenis@gmail.com, http://libtom.org */ -/* c = a mod b, 0 <= c < b */ +/* c = a mod b, 0 <= c < b if b > 0, b < c <= 0 if b < 0 */ int mp_mod (mp_int * a, mp_int * b, mp_int * c) { @@ -31,11 +31,11 @@ mp_mod (mp_int * a, mp_int * b, mp_int * c) return res; } - if (t.sign != b->sign) { - res = mp_add (b, &t, c); - } else { + if (mp_iszero(&t) || t.sign == b->sign) { res = MP_OKAY; mp_exch (&t, c); + } else { + res = mp_add (b, &t, c); } mp_clear (&t); diff --git a/lib/hcrypto/libtommath/bn_mp_prime_next_prime.c b/lib/hcrypto/libtommath/bn_mp_prime_next_prime.c index db2f46ddbf9b..8cd3ec2a5270 100644 --- a/lib/hcrypto/libtommath/bn_mp_prime_next_prime.c +++ b/lib/hcrypto/libtommath/bn_mp_prime_next_prime.c @@ -142,7 +142,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style) } /* is this prime? */ - for (x = 0; x < t && x < PRIME_SIZE; x++) { + for (x = 0; x < t; x++) { mp_set(&b, ltm_prime_tab[x]); if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) { goto LBL_ERR; diff --git a/lib/hcrypto/libtommath/bn_mp_rand.c b/lib/hcrypto/libtommath/bn_mp_rand.c index af66a67eea14..14f5b7503bc7 100644 --- a/lib/hcrypto/libtommath/bn_mp_rand.c +++ b/lib/hcrypto/libtommath/bn_mp_rand.c @@ -29,7 +29,7 @@ mp_rand (mp_int * a, int digits) /* first place a random non-zero digit */ do { - d = ((mp_digit) abs (rand ())) & MP_MASK; + d = ((mp_digit) labs (rand ())) & MP_MASK; } while (d == 0); if ((res = mp_add_d (a, d, a)) != MP_OKAY) { @@ -41,7 +41,7 @@ mp_rand (mp_int * a, int digits) return res; } - if ((res = mp_add_d (a, ((mp_digit) abs (rand ())), a)) != MP_OKAY) { + if ((res = mp_add_d (a, ((mp_digit) labs (rand ())), a)) != MP_OKAY) { return res; } } diff --git a/lib/hcrypto/libtommath/bn_mp_shrink.c b/lib/hcrypto/libtommath/bn_mp_shrink.c index 4b8c5ef11adb..c600efc58164 100644 --- a/lib/hcrypto/libtommath/bn_mp_shrink.c +++ b/lib/hcrypto/libtommath/bn_mp_shrink.c @@ -19,12 +19,17 @@ int mp_shrink (mp_int * a) { mp_digit *tmp; - if (a->alloc != a->used && a->used > 0) { - if ((tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * a->used)) == NULL) { + int used = 1; + + if(a->used > 0) + used = a->used; + + if (a->alloc != used) { + if ((tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * used)) == NULL) { return MP_MEM; } a->dp = tmp; - a->alloc = a->used; + a->alloc = used; } return MP_OKAY; } diff --git a/lib/hcrypto/libtommath/tommath.h b/lib/hcrypto/libtommath/tommath.h index 67d3b06af66d..e5d8d5367a82 100644 --- a/lib/hcrypto/libtommath/tommath.h +++ b/lib/hcrypto/libtommath/tommath.h @@ -46,7 +46,7 @@ extern "C" { /* detect 64-bit mode if possible */ -#if defined(__x86_64__) +#if defined(__x86_64__) && !defined(__ILP32__) #if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT)) #define MP_64BIT #endif @@ -189,8 +189,8 @@ typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat); #define DIGIT(m,k) ((m)->dp[(k)]) #define SIGN(m) ((m)->sign) -/* error code to char* string */ -char *mp_error_to_string(int code); +/* error code to const char* string */ +const char *mp_error_to_string(int code); /* ---> init and deinit bignum functions <--- */ /* init a bignum */ @@ -520,9 +520,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style); */ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat); -int mp_find_prime(mp_int *a); - -int mp_isprime(mp_int *a); +int mp_find_prime(mp_int *a, int t); /* ---> radix conversion <--- */ int mp_count_bits(mp_int *a); diff --git a/lib/hcrypto/libtommath/tommath_class.h b/lib/hcrypto/libtommath/tommath_class.h index fa95a0277a1e..8fd0f52f49bb 100644 --- a/lib/hcrypto/libtommath/tommath_class.h +++ b/lib/hcrypto/libtommath/tommath_class.h @@ -42,6 +42,7 @@ #define BN_MP_EXPTMOD_C #define BN_MP_EXPTMOD_FAST_C #define BN_MP_EXTEUCLID_C +#define BN_MP_FIND_PRIME_C #define BN_MP_FREAD_C #define BN_MP_FWRITE_C #define BN_MP_GCD_C @@ -55,6 +56,7 @@ #define BN_MP_INIT_SIZE_C #define BN_MP_INVMOD_C #define BN_MP_INVMOD_SLOW_C +#define BN_MP_ISPRIME_C #define BN_MP_IS_SQUARE_C #define BN_MP_JACOBI_C #define BN_MP_KARATSUBA_MUL_C |