summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/bn/bn.h')
-rw-r--r--src/lib/libcrypto/bn/bn.h135
1 files changed, 89 insertions, 46 deletions
diff --git a/src/lib/libcrypto/bn/bn.h b/src/lib/libcrypto/bn/bn.h
index f935e1ca79..d8822610df 100644
--- a/src/lib/libcrypto/bn/bn.h
+++ b/src/lib/libcrypto/bn/bn.h
@@ -83,12 +83,12 @@ extern "C" {
83 * The reason for this flag is that when the particular C compiler 83 * The reason for this flag is that when the particular C compiler
84 * library routine is used, and the library is linked with a different 84 * library routine is used, and the library is linked with a different
85 * compiler, the library is missing. This mostly happens when the 85 * compiler, the library is missing. This mostly happens when the
86 * library is built with gcc and then linked using nornal cc. This would 86 * library is built with gcc and then linked using normal cc. This would
87 * be a common occurance because gcc normally produces code that is 87 * be a common occurrence because gcc normally produces code that is
88 * 2 times faster than system compilers for the big number stuff. 88 * 2 times faster than system compilers for the big number stuff.
89 * For machines with only one compiler (or shared libraries), this should 89 * For machines with only one compiler (or shared libraries), this should
90 * be on. Again this in only really a problem on machines 90 * be on. Again this in only really a problem on machines
91 * using "long long's", are 32bit, and are not using my assember code. */ 91 * using "long long's", are 32bit, and are not using my assembler code. */
92#if defined(MSDOS) || defined(WINDOWS) || defined(linux) 92#if defined(MSDOS) || defined(WINDOWS) || defined(linux)
93#define BN_DIV2W 93#define BN_DIV2W
94#endif 94#endif
@@ -118,8 +118,8 @@ extern "C" {
118 118
119/* This is where the long long data type is 64 bits, but long is 32. 119/* This is where the long long data type is 64 bits, but long is 32.
120 * For machines where there are 64bit registers, this is the mode to use. 120 * For machines where there are 64bit registers, this is the mode to use.
121 * IRIX, on R4000 and above should use this mode, along with the relevent 121 * IRIX, on R4000 and above should use this mode, along with the relevant
122 * assember code :-). Do NOT define BN_LLONG. 122 * assembler code :-). Do NOT define BN_LLONG.
123 */ 123 */
124#ifdef SIXTY_FOUR_BIT 124#ifdef SIXTY_FOUR_BIT
125#undef BN_LLONG 125#undef BN_LLONG
@@ -240,11 +240,15 @@ typedef struct bignum_st
240 240
241/* Used for temp variables */ 241/* Used for temp variables */
242#define BN_CTX_NUM 12 242#define BN_CTX_NUM 12
243#define BN_CTX_NUM_POS 12
243typedef struct bignum_ctx 244typedef struct bignum_ctx
244 { 245 {
245 int tos; 246 int tos;
246 BIGNUM bn[BN_CTX_NUM+1]; 247 BIGNUM bn[BN_CTX_NUM];
247 int flags; 248 int flags;
249 int depth;
250 int pos[BN_CTX_NUM_POS];
251 int too_many;
248 } BN_CTX; 252 } BN_CTX;
249 253
250typedef struct bn_blinding_st 254typedef struct bn_blinding_st
@@ -257,16 +261,15 @@ typedef struct bn_blinding_st
257 261
258/* Used for montgomery multiplication */ 262/* Used for montgomery multiplication */
259typedef struct bn_mont_ctx_st 263typedef struct bn_mont_ctx_st
260 { 264 {
261 int use_word; /* 0 for word form, 1 for long form */ 265 int ri; /* number of bits in R */
262 int ri; /* number of bits in R */ 266 BIGNUM RR; /* used to convert to montgomery form */
263 BIGNUM RR; /* used to convert to montgomery form */ 267 BIGNUM N; /* The modulus */
264 BIGNUM N; /* The modulus */ 268 BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1
265 BIGNUM Ni; /* The inverse of N */ 269 * (Ni is only stored for bignum algorithm) */
266 BN_ULONG n0; /* word form of inverse, normally only one of 270 BN_ULONG n0; /* least significant word of Ni */
267 * Ni or n0 is defined */
268 int flags; 271 int flags;
269 } BN_MONT_CTX; 272 } BN_MONT_CTX;
270 273
271/* Used for reciprocal division/mod functions 274/* Used for reciprocal division/mod functions
272 * It cannot be shared between threads 275 * It cannot be shared between threads
@@ -283,7 +286,26 @@ typedef struct bn_recp_ctx_st
283#define BN_to_montgomery(r,a,mont,ctx) BN_mod_mul_montgomery(\ 286#define BN_to_montgomery(r,a,mont,ctx) BN_mod_mul_montgomery(\
284 r,a,&((mont)->RR),(mont),ctx) 287 r,a,&((mont)->RR),(mont),ctx)
285 288
286#define BN_prime_checks (5) 289#define BN_prime_checks 0 /* default: select number of iterations
290 based on the size of the number */
291
292/* number of Miller-Rabin iterations for an error rate of less than 2^-80
293 * for random 'b'-bit input, b >= 100 (taken from table 4.4 in the Handbook
294 * of Applied Cryptography [Menezes, van Oorschot, Vanstone; CRC Press 1996];
295 * original paper: Damgaard, Landrock, Pomerance: Average case error estimates
296 * for the strong probable prime test. -- Math. Comp. 61 (1993) 177-194) */
297#define BN_prime_checks_for_size(b) ((b) >= 1300 ? 2 : \
298 (b) >= 850 ? 3 : \
299 (b) >= 650 ? 4 : \
300 (b) >= 550 ? 5 : \
301 (b) >= 450 ? 6 : \
302 (b) >= 400 ? 7 : \
303 (b) >= 350 ? 8 : \
304 (b) >= 300 ? 9 : \
305 (b) >= 250 ? 12 : \
306 (b) >= 200 ? 15 : \
307 (b) >= 150 ? 18 : \
308 /* b >= 100 */ 27)
287 309
288#define BN_num_bytes(a) ((BN_num_bits(a)+7)/8) 310#define BN_num_bytes(a) ((BN_num_bits(a)+7)/8)
289#define BN_is_word(a,w) (((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w))) 311#define BN_is_word(a,w) (((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w)))
@@ -296,26 +318,16 @@ typedef struct bn_recp_ctx_st
296/*#define BN_ascii2bn(a) BN_hex2bn(a) */ 318/*#define BN_ascii2bn(a) BN_hex2bn(a) */
297/*#define BN_bn2ascii(a) BN_bn2hex(a) */ 319/*#define BN_bn2ascii(a) BN_bn2hex(a) */
298 320
299#define bn_expand(n,b) ((((((b+BN_BITS2-1))/BN_BITS2)) <= (n)->max)?\
300 (n):bn_expand2((n),(b)/BN_BITS2+1))
301#define bn_wexpand(n,b) (((b) <= (n)->max)?(n):bn_expand2((n),(b)))
302
303#define bn_fix_top(a) \
304 { \
305 BN_ULONG *ftl; \
306 if ((a)->top > 0) \
307 { \
308 for (ftl= &((a)->d[(a)->top-1]); (a)->top > 0; (a)->top--) \
309 if (*(ftl--)) break; \
310 } \
311 }
312
313BIGNUM *BN_value_one(void); 321BIGNUM *BN_value_one(void);
314char * BN_options(void); 322char * BN_options(void);
315BN_CTX *BN_CTX_new(void); 323BN_CTX *BN_CTX_new(void);
316void BN_CTX_init(BN_CTX *c); 324void BN_CTX_init(BN_CTX *c);
317void BN_CTX_free(BN_CTX *c); 325void BN_CTX_free(BN_CTX *c);
326void BN_CTX_start(BN_CTX *ctx);
327BIGNUM *BN_CTX_get(BN_CTX *ctx);
328void BN_CTX_end(BN_CTX *ctx);
318int BN_rand(BIGNUM *rnd, int bits, int top,int bottom); 329int BN_rand(BIGNUM *rnd, int bits, int top,int bottom);
330int BN_pseudo_rand(BIGNUM *rnd, int bits, int top,int bottom);
319int BN_num_bits(const BIGNUM *a); 331int BN_num_bits(const BIGNUM *a);
320int BN_num_bits_word(BN_ULONG); 332int BN_num_bits_word(BN_ULONG);
321BIGNUM *BN_new(void); 333BIGNUM *BN_new(void);
@@ -329,13 +341,13 @@ int BN_bn2mpi(const BIGNUM *a, unsigned char *to);
329int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 341int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
330int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 342int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
331int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 343int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
332int BN_add(BIGNUM *r, BIGNUM *a, BIGNUM *b); 344int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
333int BN_mod(BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx); 345int BN_mod(BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx);
334int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, 346int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
335 BN_CTX *ctx); 347 BN_CTX *ctx);
336int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b,BN_CTX *ctx); 348int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);
337int BN_sqr(BIGNUM *r, BIGNUM *a,BN_CTX *ctx); 349int BN_sqr(BIGNUM *r, BIGNUM *a,BN_CTX *ctx);
338BN_ULONG BN_mod_word(BIGNUM *a, BN_ULONG w); 350BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w);
339BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w); 351BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w);
340int BN_mul_word(BIGNUM *a, BN_ULONG w); 352int BN_mul_word(BIGNUM *a, BN_ULONG w);
341int BN_add_word(BIGNUM *a, BN_ULONG w); 353int BN_add_word(BIGNUM *a, BN_ULONG w);
@@ -358,19 +370,18 @@ int BN_mod_exp_simple(BIGNUM *r, BIGNUM *a, BIGNUM *p,
358 BIGNUM *m,BN_CTX *ctx); 370 BIGNUM *m,BN_CTX *ctx);
359int BN_mask_bits(BIGNUM *a,int n); 371int BN_mask_bits(BIGNUM *a,int n);
360int BN_mod_mul(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m, BN_CTX *ctx); 372int BN_mod_mul(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m, BN_CTX *ctx);
361#ifndef WIN16 373#ifndef NO_FP_API
362int BN_print_fp(FILE *fp, BIGNUM *a); 374int BN_print_fp(FILE *fp, const BIGNUM *a);
363#endif 375#endif
364#ifdef HEADER_BIO_H 376#ifdef HEADER_BIO_H
365int BN_print(BIO *fp, const BIGNUM *a); 377int BN_print(BIO *fp, const BIGNUM *a);
366#else 378#else
367int BN_print(char *fp, const BIGNUM *a); 379int BN_print(void *fp, const BIGNUM *a);
368#endif 380#endif
369int BN_reciprocal(BIGNUM *r, BIGNUM *m, int len, BN_CTX *ctx); 381int BN_reciprocal(BIGNUM *r, BIGNUM *m, int len, BN_CTX *ctx);
370int BN_rshift(BIGNUM *r, BIGNUM *a, int n); 382int BN_rshift(BIGNUM *r, BIGNUM *a, int n);
371int BN_rshift1(BIGNUM *r, BIGNUM *a); 383int BN_rshift1(BIGNUM *r, BIGNUM *a);
372void BN_clear(BIGNUM *a); 384void BN_clear(BIGNUM *a);
373BIGNUM *bn_expand2(BIGNUM *b, int bits);
374BIGNUM *BN_dup(const BIGNUM *a); 385BIGNUM *BN_dup(const BIGNUM *a);
375int BN_ucmp(const BIGNUM *a, const BIGNUM *b); 386int BN_ucmp(const BIGNUM *a, const BIGNUM *b);
376int BN_set_bit(BIGNUM *a, int n); 387int BN_set_bit(BIGNUM *a, int n);
@@ -381,19 +392,16 @@ int BN_hex2bn(BIGNUM **a, const char *str);
381int BN_dec2bn(BIGNUM **a, const char *str); 392int BN_dec2bn(BIGNUM **a, const char *str);
382int BN_gcd(BIGNUM *r,BIGNUM *in_a,BIGNUM *in_b,BN_CTX *ctx); 393int BN_gcd(BIGNUM *r,BIGNUM *in_a,BIGNUM *in_b,BN_CTX *ctx);
383BIGNUM *BN_mod_inverse(BIGNUM *ret,BIGNUM *a, const BIGNUM *n,BN_CTX *ctx); 394BIGNUM *BN_mod_inverse(BIGNUM *ret,BIGNUM *a, const BIGNUM *n,BN_CTX *ctx);
384BIGNUM *BN_generate_prime(BIGNUM *ret,int bits,int strong,BIGNUM *add, 395BIGNUM *BN_generate_prime(BIGNUM *ret,int bits,int safe,BIGNUM *add,
385 BIGNUM *rem,void (*callback)(int,int,void *),void *cb_arg); 396 BIGNUM *rem,void (*callback)(int,int,void *),void *cb_arg);
386int BN_is_prime(BIGNUM *p,int nchecks,void (*callback)(int,int,void *), 397int BN_is_prime(const BIGNUM *p,int nchecks,
398 void (*callback)(int,int,void *),
387 BN_CTX *ctx,void *cb_arg); 399 BN_CTX *ctx,void *cb_arg);
400int BN_is_prime_fasttest(const BIGNUM *p,int nchecks,
401 void (*callback)(int,int,void *),BN_CTX *ctx,void *cb_arg,
402 int do_trial_division);
388void ERR_load_BN_strings(void ); 403void ERR_load_BN_strings(void );
389 404
390BN_ULONG bn_mul_add_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w);
391BN_ULONG bn_mul_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w);
392void bn_sqr_words(BN_ULONG *rp, BN_ULONG *ap, int num);
393BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
394BN_ULONG bn_add_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,int num);
395BN_ULONG bn_sub_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,int num);
396
397BN_MONT_CTX *BN_MONT_CTX_new(void ); 405BN_MONT_CTX *BN_MONT_CTX_new(void );
398void BN_MONT_CTX_init(BN_MONT_CTX *ctx); 406void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
399int BN_mod_mul_montgomery(BIGNUM *r,BIGNUM *a,BIGNUM *b,BN_MONT_CTX *mont, 407int BN_mod_mul_montgomery(BIGNUM *r,BIGNUM *a,BIGNUM *b,BN_MONT_CTX *mont,
@@ -423,6 +431,39 @@ int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
423int BN_div_recp(BIGNUM *dv, BIGNUM *rem, BIGNUM *m, 431int BN_div_recp(BIGNUM *dv, BIGNUM *rem, BIGNUM *m,
424 BN_RECP_CTX *recp, BN_CTX *ctx); 432 BN_RECP_CTX *recp, BN_CTX *ctx);
425 433
434/* library internal functions */
435
436#define bn_expand(a,bits) ((((((bits+BN_BITS2-1))/BN_BITS2)) <= (a)->max)?\
437 (a):bn_expand2((a),(bits)/BN_BITS2+1))
438#define bn_wexpand(a,words) (((words) <= (a)->max)?(a):bn_expand2((a),(words)))
439BIGNUM *bn_expand2(BIGNUM *a, int words);
440
441#define bn_fix_top(a) \
442 { \
443 BN_ULONG *ftl; \
444 if ((a)->top > 0) \
445 { \
446 for (ftl= &((a)->d[(a)->top-1]); (a)->top > 0; (a)->top--) \
447 if (*(ftl--)) break; \
448 } \
449 }
450
451BN_ULONG bn_mul_add_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w);
452BN_ULONG bn_mul_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w);
453void bn_sqr_words(BN_ULONG *rp, BN_ULONG *ap, int num);
454BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
455BN_ULONG bn_add_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,int num);
456BN_ULONG bn_sub_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,int num);
457
458#ifdef BN_DEBUG
459 void bn_dump1(FILE *o, const char *a, BN_ULONG *b,int n);
460# define bn_print(a) {fprintf(stderr, #a "="); BN_print_fp(stderr,a); \
461 fprintf(stderr,"\n");}
462# define bn_dump(a,n) bn_dump1(stderr,#a,a,n);
463#else
464# define bn_print(a)
465# define bn_dump(a,b)
466#endif
426 467
427/* BEGIN ERROR CODES */ 468/* BEGIN ERROR CODES */
428/* The following lines are auto generated by the script mkerr.pl. Any changes 469/* The following lines are auto generated by the script mkerr.pl. Any changes
@@ -438,6 +479,7 @@ int BN_div_recp(BIGNUM *dv, BIGNUM *rem, BIGNUM *m,
438#define BN_F_BN_BLINDING_UPDATE 103 479#define BN_F_BN_BLINDING_UPDATE 103
439#define BN_F_BN_BN2DEC 104 480#define BN_F_BN_BN2DEC 104
440#define BN_F_BN_BN2HEX 105 481#define BN_F_BN_BN2HEX 105
482#define BN_F_BN_CTX_GET 116
441#define BN_F_BN_CTX_NEW 106 483#define BN_F_BN_CTX_NEW 106
442#define BN_F_BN_DIV 107 484#define BN_F_BN_DIV 107
443#define BN_F_BN_EXPAND2 108 485#define BN_F_BN_EXPAND2 108
@@ -459,6 +501,7 @@ int BN_div_recp(BIGNUM *dv, BIGNUM *rem, BIGNUM *m,
459#define BN_R_INVALID_LENGTH 106 501#define BN_R_INVALID_LENGTH 106
460#define BN_R_NOT_INITIALIZED 107 502#define BN_R_NOT_INITIALIZED 107
461#define BN_R_NO_INVERSE 108 503#define BN_R_NO_INVERSE 108
504#define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109
462 505
463#ifdef __cplusplus 506#ifdef __cplusplus
464} 507}