1*d3273b5bSchristos /* $NetBSD: tommath.h,v 1.2 2017/01/28 21:31:47 christos Exp $ */ 2ca1c9b0cSelric 3ca1c9b0cSelric /* LibTomMath, multiple-precision integer library -- Tom St Denis 4ca1c9b0cSelric * 5ca1c9b0cSelric * LibTomMath is a library that provides multiple-precision 6ca1c9b0cSelric * integer arithmetic as well as number theoretic functionality. 7ca1c9b0cSelric * 8ca1c9b0cSelric * The library was designed directly after the MPI library by 9ca1c9b0cSelric * Michael Fromberger but has been written from scratch with 10ca1c9b0cSelric * additional optimizations in place. 11ca1c9b0cSelric * 12ca1c9b0cSelric * The library is free for all purposes without any express 13ca1c9b0cSelric * guarantee it works. 14ca1c9b0cSelric * 15ca1c9b0cSelric * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com 16ca1c9b0cSelric */ 17ca1c9b0cSelric #ifndef BN_H_ 18ca1c9b0cSelric #define BN_H_ 19ca1c9b0cSelric 20ca1c9b0cSelric #include <stdio.h> 21ca1c9b0cSelric #include <string.h> 22ca1c9b0cSelric #include <stdlib.h> 23ca1c9b0cSelric #include <ctype.h> 24ca1c9b0cSelric #include <limits.h> 25ca1c9b0cSelric 26ca1c9b0cSelric #include <tommath_class.h> 27ca1c9b0cSelric 28ca1c9b0cSelric #ifndef MIN 29ca1c9b0cSelric #define MIN(x,y) ((x)<(y)?(x):(y)) 30ca1c9b0cSelric #endif 31ca1c9b0cSelric 32ca1c9b0cSelric #ifndef MAX 33ca1c9b0cSelric #define MAX(x,y) ((x)>(y)?(x):(y)) 34ca1c9b0cSelric #endif 35ca1c9b0cSelric 36ca1c9b0cSelric #ifdef __cplusplus 37ca1c9b0cSelric extern "C" { 38ca1c9b0cSelric 39ca1c9b0cSelric /* C++ compilers don't like assigning void * to mp_digit * */ 40ca1c9b0cSelric #define OPT_CAST(x) (x *) 41ca1c9b0cSelric 42ca1c9b0cSelric #else 43ca1c9b0cSelric 44ca1c9b0cSelric /* C on the other hand doesn't care */ 45ca1c9b0cSelric #define OPT_CAST(x) 46ca1c9b0cSelric 47ca1c9b0cSelric #endif 48ca1c9b0cSelric 49ca1c9b0cSelric 50ca1c9b0cSelric /* detect 64-bit mode if possible */ 51b9d004c6Schristos #if defined(__x86_64__) && !defined(__ILP32__) 52ca1c9b0cSelric #if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT)) 53ca1c9b0cSelric #define MP_64BIT 54ca1c9b0cSelric #endif 55ca1c9b0cSelric #endif 56ca1c9b0cSelric 57ca1c9b0cSelric /* some default configurations. 58ca1c9b0cSelric * 59ca1c9b0cSelric * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits 60ca1c9b0cSelric * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits 61ca1c9b0cSelric * 62ca1c9b0cSelric * At the very least a mp_digit must be able to hold 7 bits 63ca1c9b0cSelric * [any size beyond that is ok provided it doesn't overflow the data type] 64ca1c9b0cSelric */ 65ca1c9b0cSelric #ifdef MP_8BIT 66ca1c9b0cSelric typedef unsigned char mp_digit; 67ca1c9b0cSelric typedef unsigned short mp_word; 68ca1c9b0cSelric #elif defined(MP_16BIT) 69ca1c9b0cSelric typedef unsigned short mp_digit; 70ca1c9b0cSelric typedef unsigned long mp_word; 71ca1c9b0cSelric #elif defined(MP_64BIT) 72ca1c9b0cSelric /* for GCC only on supported platforms */ 73ca1c9b0cSelric #ifndef CRYPT 74ca1c9b0cSelric typedef unsigned long long ulong64; 75ca1c9b0cSelric typedef signed long long long64; 76ca1c9b0cSelric #endif 77ca1c9b0cSelric 78ca1c9b0cSelric typedef unsigned long mp_digit; 79ca1c9b0cSelric typedef unsigned long mp_word __attribute__ ((mode(TI))); 80ca1c9b0cSelric 81ca1c9b0cSelric #define DIGIT_BIT 60 82ca1c9b0cSelric #else 83ca1c9b0cSelric /* this is the default case, 28-bit digits */ 84ca1c9b0cSelric 85ca1c9b0cSelric /* this is to make porting into LibTomCrypt easier :-) */ 86ca1c9b0cSelric #ifndef CRYPT 87ca1c9b0cSelric #if defined(_MSC_VER) || defined(__BORLANDC__) 88ca1c9b0cSelric typedef unsigned __int64 ulong64; 89ca1c9b0cSelric typedef signed __int64 long64; 90ca1c9b0cSelric #else 91ca1c9b0cSelric typedef unsigned long long ulong64; 92ca1c9b0cSelric typedef signed long long long64; 93ca1c9b0cSelric #endif 94ca1c9b0cSelric #endif 95ca1c9b0cSelric 96ca1c9b0cSelric typedef unsigned long mp_digit; 97ca1c9b0cSelric typedef ulong64 mp_word; 98ca1c9b0cSelric 99ca1c9b0cSelric #ifdef MP_31BIT 100ca1c9b0cSelric /* this is an extension that uses 31-bit digits */ 101ca1c9b0cSelric #define DIGIT_BIT 31 102ca1c9b0cSelric #else 103ca1c9b0cSelric /* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */ 104ca1c9b0cSelric #define DIGIT_BIT 28 105ca1c9b0cSelric #define MP_28BIT 106ca1c9b0cSelric #endif 107ca1c9b0cSelric #endif 108ca1c9b0cSelric 109ca1c9b0cSelric /* define heap macros */ 110ca1c9b0cSelric #ifndef CRYPT 111ca1c9b0cSelric /* default to libc stuff */ 112ca1c9b0cSelric #ifndef XMALLOC 113ca1c9b0cSelric #define XMALLOC malloc 114ca1c9b0cSelric #define XFREE free 115ca1c9b0cSelric #define XREALLOC realloc 116ca1c9b0cSelric #define XCALLOC calloc 117ca1c9b0cSelric #else 118ca1c9b0cSelric /* prototypes for our heap functions */ 119ca1c9b0cSelric extern void *XMALLOC(size_t n); 120ca1c9b0cSelric extern void *XREALLOC(void *p, size_t n); 121ca1c9b0cSelric extern void *XCALLOC(size_t n, size_t s); 122ca1c9b0cSelric extern void XFREE(void *p); 123ca1c9b0cSelric #endif 124ca1c9b0cSelric #endif 125ca1c9b0cSelric 126ca1c9b0cSelric 127ca1c9b0cSelric /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */ 128ca1c9b0cSelric #ifndef DIGIT_BIT 129ca1c9b0cSelric #define DIGIT_BIT ((int)((CHAR_BIT * sizeof(mp_digit) - 1))) /* bits per digit */ 130ca1c9b0cSelric #endif 131ca1c9b0cSelric 132ca1c9b0cSelric #define MP_DIGIT_BIT DIGIT_BIT 133ca1c9b0cSelric #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1)) 134ca1c9b0cSelric #define MP_DIGIT_MAX MP_MASK 135ca1c9b0cSelric 136ca1c9b0cSelric /* equalities */ 137ca1c9b0cSelric #define MP_LT -1 /* less than */ 138ca1c9b0cSelric #define MP_EQ 0 /* equal to */ 139ca1c9b0cSelric #define MP_GT 1 /* greater than */ 140ca1c9b0cSelric 141ca1c9b0cSelric #define MP_ZPOS 0 /* positive integer */ 142ca1c9b0cSelric #define MP_NEG 1 /* negative */ 143ca1c9b0cSelric 144ca1c9b0cSelric #define MP_OKAY 0 /* ok result */ 145ca1c9b0cSelric #define MP_MEM -2 /* out of mem */ 146ca1c9b0cSelric #define MP_VAL -3 /* invalid input */ 147ca1c9b0cSelric #define MP_RANGE MP_VAL 148ca1c9b0cSelric 149ca1c9b0cSelric #define MP_YES 1 /* yes response */ 150ca1c9b0cSelric #define MP_NO 0 /* no response */ 151ca1c9b0cSelric 152ca1c9b0cSelric /* Primality generation flags */ 153ca1c9b0cSelric #define LTM_PRIME_BBS 0x0001 /* BBS style prime */ 154ca1c9b0cSelric #define LTM_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */ 155ca1c9b0cSelric #define LTM_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */ 156ca1c9b0cSelric 157ca1c9b0cSelric typedef int mp_err; 158ca1c9b0cSelric 159ca1c9b0cSelric /* you'll have to tune these... */ 160ca1c9b0cSelric extern int KARATSUBA_MUL_CUTOFF, 161ca1c9b0cSelric KARATSUBA_SQR_CUTOFF, 162ca1c9b0cSelric TOOM_MUL_CUTOFF, 163ca1c9b0cSelric TOOM_SQR_CUTOFF; 164ca1c9b0cSelric 165ca1c9b0cSelric /* define this to use lower memory usage routines (exptmods mostly) */ 166ca1c9b0cSelric /* #define MP_LOW_MEM */ 167ca1c9b0cSelric 168ca1c9b0cSelric /* default precision */ 169ca1c9b0cSelric #ifndef MP_PREC 170ca1c9b0cSelric #ifndef MP_LOW_MEM 171ca1c9b0cSelric #define MP_PREC 32 /* default digits of precision */ 172ca1c9b0cSelric #else 173ca1c9b0cSelric #define MP_PREC 8 /* default digits of precision */ 174ca1c9b0cSelric #endif 175ca1c9b0cSelric #endif 176ca1c9b0cSelric 177ca1c9b0cSelric /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */ 178ca1c9b0cSelric #define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1)) 179ca1c9b0cSelric 180ca1c9b0cSelric /* the infamous mp_int structure */ 181ca1c9b0cSelric typedef struct { 182ca1c9b0cSelric int used, alloc, sign; 183ca1c9b0cSelric mp_digit *dp; 184ca1c9b0cSelric } mp_int; 185ca1c9b0cSelric 186ca1c9b0cSelric /* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */ 187ca1c9b0cSelric typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat); 188ca1c9b0cSelric 189ca1c9b0cSelric 190ca1c9b0cSelric #define USED(m) ((m)->used) 191ca1c9b0cSelric #define DIGIT(m,k) ((m)->dp[(k)]) 192ca1c9b0cSelric #define SIGN(m) ((m)->sign) 193ca1c9b0cSelric 194b9d004c6Schristos /* error code to const char* string */ 195b9d004c6Schristos const char *mp_error_to_string(int code); 196ca1c9b0cSelric 197ca1c9b0cSelric /* ---> init and deinit bignum functions <--- */ 198ca1c9b0cSelric /* init a bignum */ 199ca1c9b0cSelric int mp_init(mp_int *a); 200ca1c9b0cSelric 201ca1c9b0cSelric /* free a bignum */ 202ca1c9b0cSelric void mp_clear(mp_int *a); 203ca1c9b0cSelric 204ca1c9b0cSelric /* init a null terminated series of arguments */ 205ca1c9b0cSelric int mp_init_multi(mp_int *mp, ...); 206ca1c9b0cSelric 207ca1c9b0cSelric /* clear a null terminated series of arguments */ 208ca1c9b0cSelric void mp_clear_multi(mp_int *mp, ...); 209ca1c9b0cSelric 210ca1c9b0cSelric /* exchange two ints */ 211ca1c9b0cSelric void mp_exch(mp_int *a, mp_int *b); 212ca1c9b0cSelric 213ca1c9b0cSelric /* shrink ram required for a bignum */ 214ca1c9b0cSelric int mp_shrink(mp_int *a); 215ca1c9b0cSelric 216ca1c9b0cSelric /* grow an int to a given size */ 217ca1c9b0cSelric int mp_grow(mp_int *a, int size); 218ca1c9b0cSelric 219ca1c9b0cSelric /* init to a given number of digits */ 220ca1c9b0cSelric int mp_init_size(mp_int *a, int size); 221ca1c9b0cSelric 222ca1c9b0cSelric /* ---> Basic Manipulations <--- */ 223ca1c9b0cSelric #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO) 224ca1c9b0cSelric #define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO) 225ca1c9b0cSelric #define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO) 226ca1c9b0cSelric #define mp_isneg(a) (((a)->sign) ? MP_YES : MP_NO) 227ca1c9b0cSelric 228ca1c9b0cSelric /* set to zero */ 229ca1c9b0cSelric void mp_zero(mp_int *a); 230ca1c9b0cSelric 231ca1c9b0cSelric /* set to zero, multi */ 232ca1c9b0cSelric void mp_zero_multi(mp_int *a, ...); 233ca1c9b0cSelric 234ca1c9b0cSelric /* set to a digit */ 235ca1c9b0cSelric void mp_set(mp_int *a, mp_digit b); 236ca1c9b0cSelric 237ca1c9b0cSelric /* set a 32-bit const */ 238ca1c9b0cSelric int mp_set_int(mp_int *a, unsigned long b); 239ca1c9b0cSelric 240ca1c9b0cSelric /* get a 32-bit value */ 241ca1c9b0cSelric unsigned long mp_get_int(mp_int * a); 242ca1c9b0cSelric 243ca1c9b0cSelric /* initialize and set a digit */ 244ca1c9b0cSelric int mp_init_set (mp_int * a, mp_digit b); 245ca1c9b0cSelric 246ca1c9b0cSelric /* initialize and set 32-bit value */ 247ca1c9b0cSelric int mp_init_set_int (mp_int * a, unsigned long b); 248ca1c9b0cSelric 249ca1c9b0cSelric /* copy, b = a */ 250ca1c9b0cSelric int mp_copy(mp_int *a, mp_int *b); 251ca1c9b0cSelric 252ca1c9b0cSelric /* inits and copies, a = b */ 253ca1c9b0cSelric int mp_init_copy(mp_int *a, mp_int *b); 254ca1c9b0cSelric 255ca1c9b0cSelric /* trim unused digits */ 256ca1c9b0cSelric void mp_clamp(mp_int *a); 257ca1c9b0cSelric 258ca1c9b0cSelric /* ---> digit manipulation <--- */ 259ca1c9b0cSelric 260ca1c9b0cSelric /* right shift by "b" digits */ 261ca1c9b0cSelric void mp_rshd(mp_int *a, int b); 262ca1c9b0cSelric 263ca1c9b0cSelric /* left shift by "b" digits */ 264ca1c9b0cSelric int mp_lshd(mp_int *a, int b); 265ca1c9b0cSelric 266ca1c9b0cSelric /* c = a / 2**b */ 267ca1c9b0cSelric int mp_div_2d(mp_int *a, int b, mp_int *c, mp_int *d); 268ca1c9b0cSelric 269ca1c9b0cSelric /* b = a/2 */ 270ca1c9b0cSelric int mp_div_2(mp_int *a, mp_int *b); 271ca1c9b0cSelric 272ca1c9b0cSelric /* c = a * 2**b */ 273ca1c9b0cSelric int mp_mul_2d(mp_int *a, int b, mp_int *c); 274ca1c9b0cSelric 275ca1c9b0cSelric /* b = a*2 */ 276ca1c9b0cSelric int mp_mul_2(mp_int *a, mp_int *b); 277ca1c9b0cSelric 278ca1c9b0cSelric /* c = a mod 2**d */ 279ca1c9b0cSelric int mp_mod_2d(mp_int *a, int b, mp_int *c); 280ca1c9b0cSelric 281ca1c9b0cSelric /* computes a = 2**b */ 282ca1c9b0cSelric int mp_2expt(mp_int *a, int b); 283ca1c9b0cSelric 284ca1c9b0cSelric /* Counts the number of lsbs which are zero before the first zero bit */ 285ca1c9b0cSelric int mp_cnt_lsb(mp_int *a); 286ca1c9b0cSelric 287ca1c9b0cSelric /* I Love Earth! */ 288ca1c9b0cSelric 289ca1c9b0cSelric /* makes a pseudo-random int of a given size */ 290ca1c9b0cSelric int mp_rand(mp_int *a, int digits); 291ca1c9b0cSelric 292ca1c9b0cSelric /* ---> binary operations <--- */ 293ca1c9b0cSelric /* c = a XOR b */ 294ca1c9b0cSelric int mp_xor(mp_int *a, mp_int *b, mp_int *c); 295ca1c9b0cSelric 296ca1c9b0cSelric /* c = a OR b */ 297ca1c9b0cSelric int mp_or(mp_int *a, mp_int *b, mp_int *c); 298ca1c9b0cSelric 299ca1c9b0cSelric /* c = a AND b */ 300ca1c9b0cSelric int mp_and(mp_int *a, mp_int *b, mp_int *c); 301ca1c9b0cSelric 302ca1c9b0cSelric /* ---> Basic arithmetic <--- */ 303ca1c9b0cSelric 304ca1c9b0cSelric /* b = -a */ 305ca1c9b0cSelric int mp_neg(mp_int *a, mp_int *b); 306ca1c9b0cSelric 307ca1c9b0cSelric /* b = |a| */ 308ca1c9b0cSelric int mp_abs(mp_int *a, mp_int *b); 309ca1c9b0cSelric 310ca1c9b0cSelric /* compare a to b */ 311ca1c9b0cSelric int mp_cmp(mp_int *a, mp_int *b); 312ca1c9b0cSelric 313ca1c9b0cSelric /* compare |a| to |b| */ 314ca1c9b0cSelric int mp_cmp_mag(mp_int *a, mp_int *b); 315ca1c9b0cSelric 316ca1c9b0cSelric /* c = a + b */ 317ca1c9b0cSelric int mp_add(mp_int *a, mp_int *b, mp_int *c); 318ca1c9b0cSelric 319ca1c9b0cSelric /* c = a - b */ 320ca1c9b0cSelric int mp_sub(mp_int *a, mp_int *b, mp_int *c); 321ca1c9b0cSelric 322ca1c9b0cSelric /* c = a * b */ 323ca1c9b0cSelric int mp_mul(mp_int *a, mp_int *b, mp_int *c); 324ca1c9b0cSelric 325ca1c9b0cSelric /* b = a*a */ 326ca1c9b0cSelric int mp_sqr(mp_int *a, mp_int *b); 327ca1c9b0cSelric 328ca1c9b0cSelric /* a/b => cb + d == a */ 329ca1c9b0cSelric int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d); 330ca1c9b0cSelric 331ca1c9b0cSelric /* c = a mod b, 0 <= c < b */ 332ca1c9b0cSelric int mp_mod(mp_int *a, mp_int *b, mp_int *c); 333ca1c9b0cSelric 334ca1c9b0cSelric /* ---> single digit functions <--- */ 335ca1c9b0cSelric 336ca1c9b0cSelric /* compare against a single digit */ 337ca1c9b0cSelric int mp_cmp_d(mp_int *a, mp_digit b); 338ca1c9b0cSelric 339ca1c9b0cSelric /* c = a + b */ 340ca1c9b0cSelric int mp_add_d(mp_int *a, mp_digit b, mp_int *c); 341ca1c9b0cSelric 342ca1c9b0cSelric /* c = a - b */ 343ca1c9b0cSelric int mp_sub_d(mp_int *a, mp_digit b, mp_int *c); 344ca1c9b0cSelric 345ca1c9b0cSelric /* c = a * b */ 346ca1c9b0cSelric int mp_mul_d(mp_int *a, mp_digit b, mp_int *c); 347ca1c9b0cSelric 348ca1c9b0cSelric /* a/b => cb + d == a */ 349ca1c9b0cSelric int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d); 350ca1c9b0cSelric 351ca1c9b0cSelric /* a/3 => 3c + d == a */ 352ca1c9b0cSelric int mp_div_3(mp_int *a, mp_int *c, mp_digit *d); 353ca1c9b0cSelric 354ca1c9b0cSelric /* c = a**b */ 355ca1c9b0cSelric int mp_expt_d(mp_int *a, mp_digit b, mp_int *c); 356ca1c9b0cSelric 357ca1c9b0cSelric /* c = a mod b, 0 <= c < b */ 358ca1c9b0cSelric int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c); 359ca1c9b0cSelric 360ca1c9b0cSelric /* ---> number theory <--- */ 361ca1c9b0cSelric 362ca1c9b0cSelric /* d = a + b (mod c) */ 363ca1c9b0cSelric int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); 364ca1c9b0cSelric 365ca1c9b0cSelric /* d = a - b (mod c) */ 366ca1c9b0cSelric int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); 367ca1c9b0cSelric 368ca1c9b0cSelric /* d = a * b (mod c) */ 369ca1c9b0cSelric int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); 370ca1c9b0cSelric 371ca1c9b0cSelric /* c = a * a (mod b) */ 372ca1c9b0cSelric int mp_sqrmod(mp_int *a, mp_int *b, mp_int *c); 373ca1c9b0cSelric 374ca1c9b0cSelric /* c = 1/a (mod b) */ 375ca1c9b0cSelric int mp_invmod(mp_int *a, mp_int *b, mp_int *c); 376ca1c9b0cSelric 377ca1c9b0cSelric /* c = (a, b) */ 378ca1c9b0cSelric int mp_gcd(mp_int *a, mp_int *b, mp_int *c); 379ca1c9b0cSelric 380ca1c9b0cSelric /* produces value such that U1*a + U2*b = U3 */ 381ca1c9b0cSelric int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3); 382ca1c9b0cSelric 383ca1c9b0cSelric /* c = [a, b] or (a*b)/(a, b) */ 384ca1c9b0cSelric int mp_lcm(mp_int *a, mp_int *b, mp_int *c); 385ca1c9b0cSelric 386ca1c9b0cSelric /* finds one of the b'th root of a, such that |c|**b <= |a| 387ca1c9b0cSelric * 388ca1c9b0cSelric * returns error if a < 0 and b is even 389ca1c9b0cSelric */ 390ca1c9b0cSelric int mp_n_root(mp_int *a, mp_digit b, mp_int *c); 391ca1c9b0cSelric 392ca1c9b0cSelric /* special sqrt algo */ 393ca1c9b0cSelric int mp_sqrt(mp_int *arg, mp_int *ret); 394ca1c9b0cSelric 395ca1c9b0cSelric /* is number a square? */ 396ca1c9b0cSelric int mp_is_square(mp_int *arg, int *ret); 397ca1c9b0cSelric 398ca1c9b0cSelric /* computes the jacobi c = (a | n) (or Legendre if b is prime) */ 399ca1c9b0cSelric int mp_jacobi(mp_int *a, mp_int *n, int *c); 400ca1c9b0cSelric 401ca1c9b0cSelric /* used to setup the Barrett reduction for a given modulus b */ 402ca1c9b0cSelric int mp_reduce_setup(mp_int *a, mp_int *b); 403ca1c9b0cSelric 404ca1c9b0cSelric /* Barrett Reduction, computes a (mod b) with a precomputed value c 405ca1c9b0cSelric * 406ca1c9b0cSelric * Assumes that 0 < a <= b*b, note if 0 > a > -(b*b) then you can merely 407ca1c9b0cSelric * compute the reduction as -1 * mp_reduce(mp_abs(a)) [pseudo code]. 408ca1c9b0cSelric */ 409ca1c9b0cSelric int mp_reduce(mp_int *a, mp_int *b, mp_int *c); 410ca1c9b0cSelric 411ca1c9b0cSelric /* setups the montgomery reduction */ 412ca1c9b0cSelric int mp_montgomery_setup(mp_int *a, mp_digit *mp); 413ca1c9b0cSelric 414ca1c9b0cSelric /* computes a = B**n mod b without division or multiplication useful for 415ca1c9b0cSelric * normalizing numbers in a Montgomery system. 416ca1c9b0cSelric */ 417ca1c9b0cSelric int mp_montgomery_calc_normalization(mp_int *a, mp_int *b); 418ca1c9b0cSelric 419ca1c9b0cSelric /* computes x/R == x (mod N) via Montgomery Reduction */ 420ca1c9b0cSelric int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp); 421ca1c9b0cSelric 422ca1c9b0cSelric /* returns 1 if a is a valid DR modulus */ 423ca1c9b0cSelric int mp_dr_is_modulus(mp_int *a); 424ca1c9b0cSelric 425ca1c9b0cSelric /* sets the value of "d" required for mp_dr_reduce */ 426ca1c9b0cSelric void mp_dr_setup(mp_int *a, mp_digit *d); 427ca1c9b0cSelric 428ca1c9b0cSelric /* reduces a modulo b using the Diminished Radix method */ 429ca1c9b0cSelric int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp); 430ca1c9b0cSelric 431ca1c9b0cSelric /* returns true if a can be reduced with mp_reduce_2k */ 432ca1c9b0cSelric int mp_reduce_is_2k(mp_int *a); 433ca1c9b0cSelric 434ca1c9b0cSelric /* determines k value for 2k reduction */ 435ca1c9b0cSelric int mp_reduce_2k_setup(mp_int *a, mp_digit *d); 436ca1c9b0cSelric 437ca1c9b0cSelric /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */ 438ca1c9b0cSelric int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d); 439ca1c9b0cSelric 440ca1c9b0cSelric /* returns true if a can be reduced with mp_reduce_2k_l */ 441ca1c9b0cSelric int mp_reduce_is_2k_l(mp_int *a); 442ca1c9b0cSelric 443ca1c9b0cSelric /* determines k value for 2k reduction */ 444ca1c9b0cSelric int mp_reduce_2k_setup_l(mp_int *a, mp_int *d); 445ca1c9b0cSelric 446ca1c9b0cSelric /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */ 447ca1c9b0cSelric int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d); 448ca1c9b0cSelric 449ca1c9b0cSelric /* d = a**b (mod c) */ 450ca1c9b0cSelric int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); 451ca1c9b0cSelric 452ca1c9b0cSelric /* ---> Primes <--- */ 453ca1c9b0cSelric 454ca1c9b0cSelric /* number of primes */ 455ca1c9b0cSelric #ifdef MP_8BIT 456ca1c9b0cSelric #define PRIME_SIZE 31 457ca1c9b0cSelric #else 458ca1c9b0cSelric #define PRIME_SIZE 256 459ca1c9b0cSelric #endif 460ca1c9b0cSelric 461ca1c9b0cSelric /* table of first PRIME_SIZE primes */ 462ca1c9b0cSelric extern const mp_digit ltm_prime_tab[]; 463ca1c9b0cSelric 464ca1c9b0cSelric /* result=1 if a is divisible by one of the first PRIME_SIZE primes */ 465ca1c9b0cSelric int mp_prime_is_divisible(mp_int *a, int *result); 466ca1c9b0cSelric 467ca1c9b0cSelric /* performs one Fermat test of "a" using base "b". 468ca1c9b0cSelric * Sets result to 0 if composite or 1 if probable prime 469ca1c9b0cSelric */ 470ca1c9b0cSelric int mp_prime_fermat(mp_int *a, mp_int *b, int *result); 471ca1c9b0cSelric 472ca1c9b0cSelric /* performs one Miller-Rabin test of "a" using base "b". 473ca1c9b0cSelric * Sets result to 0 if composite or 1 if probable prime 474ca1c9b0cSelric */ 475ca1c9b0cSelric int mp_prime_miller_rabin(mp_int *a, mp_int *b, int *result); 476ca1c9b0cSelric 477ca1c9b0cSelric /* This gives [for a given bit size] the number of trials required 478ca1c9b0cSelric * such that Miller-Rabin gives a prob of failure lower than 2^-96 479ca1c9b0cSelric */ 480ca1c9b0cSelric int mp_prime_rabin_miller_trials(int size); 481ca1c9b0cSelric 482ca1c9b0cSelric /* performs t rounds of Miller-Rabin on "a" using the first 483ca1c9b0cSelric * t prime bases. Also performs an initial sieve of trial 484ca1c9b0cSelric * division. Determines if "a" is prime with probability 485ca1c9b0cSelric * of error no more than (1/4)**t. 486ca1c9b0cSelric * 487ca1c9b0cSelric * Sets result to 1 if probably prime, 0 otherwise 488ca1c9b0cSelric */ 489ca1c9b0cSelric int mp_prime_is_prime(mp_int *a, int t, int *result); 490ca1c9b0cSelric 491ca1c9b0cSelric /* finds the next prime after the number "a" using "t" trials 492ca1c9b0cSelric * of Miller-Rabin. 493ca1c9b0cSelric * 494ca1c9b0cSelric * bbs_style = 1 means the prime must be congruent to 3 mod 4 495ca1c9b0cSelric */ 496ca1c9b0cSelric int mp_prime_next_prime(mp_int *a, int t, int bbs_style); 497ca1c9b0cSelric 498ca1c9b0cSelric /* makes a truly random prime of a given size (bytes), 499ca1c9b0cSelric * call with bbs = 1 if you want it to be congruent to 3 mod 4 500ca1c9b0cSelric * 501ca1c9b0cSelric * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can 502ca1c9b0cSelric * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself 503ca1c9b0cSelric * so it can be NULL 504ca1c9b0cSelric * 505ca1c9b0cSelric * The prime generated will be larger than 2^(8*size). 506ca1c9b0cSelric */ 507ca1c9b0cSelric #define mp_prime_random(a, t, size, bbs, cb, dat) mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?LTM_PRIME_BBS:0, cb, dat) 508ca1c9b0cSelric 509ca1c9b0cSelric /* makes a truly random prime of a given size (bits), 510ca1c9b0cSelric * 511ca1c9b0cSelric * Flags are as follows: 512ca1c9b0cSelric * 513ca1c9b0cSelric * LTM_PRIME_BBS - make prime congruent to 3 mod 4 514ca1c9b0cSelric * LTM_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS) 515ca1c9b0cSelric * LTM_PRIME_2MSB_OFF - make the 2nd highest bit zero 516ca1c9b0cSelric * LTM_PRIME_2MSB_ON - make the 2nd highest bit one 517ca1c9b0cSelric * 518ca1c9b0cSelric * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can 519ca1c9b0cSelric * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself 520ca1c9b0cSelric * so it can be NULL 521ca1c9b0cSelric * 522ca1c9b0cSelric */ 523ca1c9b0cSelric int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat); 524ca1c9b0cSelric 525b9d004c6Schristos int mp_find_prime(mp_int *a, int t); 526ca1c9b0cSelric 527ca1c9b0cSelric /* ---> radix conversion <--- */ 528ca1c9b0cSelric int mp_count_bits(mp_int *a); 529ca1c9b0cSelric 530ca1c9b0cSelric int mp_unsigned_bin_size(mp_int *a); 531ca1c9b0cSelric int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c); 532ca1c9b0cSelric int mp_to_unsigned_bin(mp_int *a, unsigned char *b); 533ca1c9b0cSelric int mp_to_unsigned_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen); 534ca1c9b0cSelric 535ca1c9b0cSelric int mp_signed_bin_size(mp_int *a); 536ca1c9b0cSelric int mp_read_signed_bin(mp_int *a, const unsigned char *b, int c); 537ca1c9b0cSelric int mp_to_signed_bin(mp_int *a, unsigned char *b); 538ca1c9b0cSelric int mp_to_signed_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen); 539ca1c9b0cSelric 540ca1c9b0cSelric int mp_read_radix(mp_int *a, const char *str, int radix); 541ca1c9b0cSelric int mp_toradix(mp_int *a, char *str, int radix); 542ca1c9b0cSelric int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen); 543ca1c9b0cSelric int mp_radix_size(mp_int *a, int radix, int *size); 544ca1c9b0cSelric 545ca1c9b0cSelric int mp_fread(mp_int *a, int radix, FILE *stream); 546ca1c9b0cSelric int mp_fwrite(mp_int *a, int radix, FILE *stream); 547ca1c9b0cSelric 548ca1c9b0cSelric #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len)) 549ca1c9b0cSelric #define mp_raw_size(mp) mp_signed_bin_size(mp) 550ca1c9b0cSelric #define mp_toraw(mp, str) mp_to_signed_bin((mp), (str)) 551ca1c9b0cSelric #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len)) 552ca1c9b0cSelric #define mp_mag_size(mp) mp_unsigned_bin_size(mp) 553ca1c9b0cSelric #define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str)) 554ca1c9b0cSelric 555ca1c9b0cSelric #define mp_tobinary(M, S) mp_toradix((M), (S), 2) 556ca1c9b0cSelric #define mp_tooctal(M, S) mp_toradix((M), (S), 8) 557ca1c9b0cSelric #define mp_todecimal(M, S) mp_toradix((M), (S), 10) 558ca1c9b0cSelric #define mp_tohex(M, S) mp_toradix((M), (S), 16) 559ca1c9b0cSelric 560ca1c9b0cSelric /* lowlevel functions, do not call! */ 561ca1c9b0cSelric int s_mp_add(mp_int *a, mp_int *b, mp_int *c); 562ca1c9b0cSelric int s_mp_sub(mp_int *a, mp_int *b, mp_int *c); 563ca1c9b0cSelric #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1) 564ca1c9b0cSelric int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs); 565ca1c9b0cSelric int s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs); 566ca1c9b0cSelric int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs); 567ca1c9b0cSelric int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs); 568ca1c9b0cSelric int fast_s_mp_sqr(mp_int *a, mp_int *b); 569ca1c9b0cSelric int s_mp_sqr(mp_int *a, mp_int *b); 570ca1c9b0cSelric int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c); 571ca1c9b0cSelric int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c); 572ca1c9b0cSelric int mp_karatsuba_sqr(mp_int *a, mp_int *b); 573ca1c9b0cSelric int mp_toom_sqr(mp_int *a, mp_int *b); 574ca1c9b0cSelric int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c); 575ca1c9b0cSelric int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c); 576ca1c9b0cSelric int fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp); 577ca1c9b0cSelric int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int mode); 578ca1c9b0cSelric int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int mode); 579ca1c9b0cSelric void bn_reverse(unsigned char *s, int len); 580ca1c9b0cSelric 581ca1c9b0cSelric extern const char *mp_s_rmap; 582ca1c9b0cSelric 583ca1c9b0cSelric #ifdef __cplusplus 584ca1c9b0cSelric } 585ca1c9b0cSelric #endif 586ca1c9b0cSelric 587ca1c9b0cSelric #endif 588ca1c9b0cSelric 589ca1c9b0cSelric 590ca1c9b0cSelric /* Source: /cvs/libtom/libtommath/tommath.h,v */ 591ca1c9b0cSelric /* Revision: 1.8 */ 592ca1c9b0cSelric /* Date: 2006/03/31 14:18:44 */ 593