diff options
Diffstat (limited to 'src/lib/libssl/src/crypto/bn/bn.h')
-rw-r--r-- | src/lib/libssl/src/crypto/bn/bn.h | 399 |
1 files changed, 330 insertions, 69 deletions
diff --git a/src/lib/libssl/src/crypto/bn/bn.h b/src/lib/libssl/src/crypto/bn/bn.h index 1251521c54..6d754d5547 100644 --- a/src/lib/libssl/src/crypto/bn/bn.h +++ b/src/lib/libssl/src/crypto/bn/bn.h | |||
@@ -55,6 +55,19 @@ | |||
55 | * copied and put under another distribution licence | 55 | * copied and put under another distribution licence |
56 | * [including the GNU Public Licence.] | 56 | * [including the GNU Public Licence.] |
57 | */ | 57 | */ |
58 | /* ==================================================================== | ||
59 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | ||
60 | * | ||
61 | * Portions of the attached software ("Contribution") are developed by | ||
62 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. | ||
63 | * | ||
64 | * The Contribution is licensed pursuant to the Eric Young open source | ||
65 | * license provided above. | ||
66 | * | ||
67 | * The binary polynomial arithmetic software is originally written by | ||
68 | * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories. | ||
69 | * | ||
70 | */ | ||
58 | 71 | ||
59 | #ifndef HEADER_BN_H | 72 | #ifndef HEADER_BN_H |
60 | #define HEADER_BN_H | 73 | #define HEADER_BN_H |
@@ -63,14 +76,23 @@ | |||
63 | #ifndef OPENSSL_NO_FP_API | 76 | #ifndef OPENSSL_NO_FP_API |
64 | #include <stdio.h> /* FILE */ | 77 | #include <stdio.h> /* FILE */ |
65 | #endif | 78 | #endif |
79 | #include <openssl/ossl_typ.h> | ||
66 | 80 | ||
67 | #ifdef __cplusplus | 81 | #ifdef __cplusplus |
68 | extern "C" { | 82 | extern "C" { |
69 | #endif | 83 | #endif |
70 | 84 | ||
71 | #ifdef OPENSSL_SYS_VMS | 85 | /* These preprocessor symbols control various aspects of the bignum headers and |
72 | #undef BN_LLONG /* experimental, so far... */ | 86 | * library code. They're not defined by any "normal" configuration, as they are |
73 | #endif | 87 | * intended for development and testing purposes. NB: defining all three can be |
88 | * useful for debugging application code as well as openssl itself. | ||
89 | * | ||
90 | * BN_DEBUG - turn on various debugging alterations to the bignum code | ||
91 | * BN_DEBUG_RAND - uses random poisoning of unused words to trip up | ||
92 | * mismanagement of bignum internals. You must also define BN_DEBUG. | ||
93 | */ | ||
94 | /* #define BN_DEBUG */ | ||
95 | /* #define BN_DEBUG_RAND */ | ||
74 | 96 | ||
75 | #define BN_MUL_COMBA | 97 | #define BN_MUL_COMBA |
76 | #define BN_SQR_COMBA | 98 | #define BN_SQR_COMBA |
@@ -143,10 +165,12 @@ extern "C" { | |||
143 | #endif | 165 | #endif |
144 | 166 | ||
145 | #ifdef THIRTY_TWO_BIT | 167 | #ifdef THIRTY_TWO_BIT |
146 | #if defined(OPENSSL_SYS_WIN32) && !defined(__GNUC__) | 168 | #ifdef BN_LLONG |
147 | #define BN_ULLONG unsigned _int64 | 169 | # if defined(OPENSSL_SYS_WIN32) && !defined(__GNUC__) |
148 | #else | 170 | # define BN_ULLONG unsigned __int64 |
149 | #define BN_ULLONG unsigned long long | 171 | # else |
172 | # define BN_ULLONG unsigned long long | ||
173 | # endif | ||
150 | #endif | 174 | #endif |
151 | #define BN_ULONG unsigned long | 175 | #define BN_ULONG unsigned long |
152 | #define BN_LONG long | 176 | #define BN_LONG long |
@@ -219,15 +243,23 @@ extern "C" { | |||
219 | 243 | ||
220 | #define BN_DEFAULT_BITS 1280 | 244 | #define BN_DEFAULT_BITS 1280 |
221 | 245 | ||
222 | #ifdef BIGNUM | ||
223 | #undef BIGNUM | ||
224 | #endif | ||
225 | |||
226 | #define BN_FLG_MALLOCED 0x01 | 246 | #define BN_FLG_MALLOCED 0x01 |
227 | #define BN_FLG_STATIC_DATA 0x02 | 247 | #define BN_FLG_STATIC_DATA 0x02 |
228 | #define BN_FLG_EXP_CONSTTIME 0x04 /* avoid leaking exponent information through timings | 248 | #define BN_FLG_CONSTTIME 0x04 /* avoid leaking exponent information through timing, |
229 | * (BN_mod_exp_mont() will call BN_mod_exp_mont_consttime) */ | 249 | * BN_mod_exp_mont() will call BN_mod_exp_mont_consttime, |
250 | * BN_div() will call BN_div_no_branch, | ||
251 | * BN_mod_inverse() will call BN_mod_inverse_no_branch. | ||
252 | */ | ||
253 | |||
254 | #ifndef OPENSSL_NO_DEPRECATED | ||
255 | #define BN_FLG_EXP_CONSTTIME BN_FLG_CONSTTIME /* deprecated name for the flag */ | ||
256 | /* avoid leaking exponent information through timings | ||
257 | * (BN_mod_exp_mont() will call BN_mod_exp_mont_consttime) */ | ||
258 | #endif | ||
259 | |||
260 | #ifndef OPENSSL_NO_DEPRECATED | ||
230 | #define BN_FLG_FREE 0x8000 /* used for debuging */ | 261 | #define BN_FLG_FREE 0x8000 /* used for debuging */ |
262 | #endif | ||
231 | #define BN_set_flags(b,n) ((b)->flags|=(n)) | 263 | #define BN_set_flags(b,n) ((b)->flags|=(n)) |
232 | #define BN_get_flags(b,n) ((b)->flags&(n)) | 264 | #define BN_get_flags(b,n) ((b)->flags&(n)) |
233 | 265 | ||
@@ -242,7 +274,18 @@ extern "C" { | |||
242 | | BN_FLG_STATIC_DATA \ | 274 | | BN_FLG_STATIC_DATA \ |
243 | | (n))) | 275 | | (n))) |
244 | 276 | ||
245 | typedef struct bignum_st | 277 | /* Already declared in ossl_typ.h */ |
278 | #if 0 | ||
279 | typedef struct bignum_st BIGNUM; | ||
280 | /* Used for temp variables (declaration hidden in bn_lcl.h) */ | ||
281 | typedef struct bignum_ctx BN_CTX; | ||
282 | typedef struct bn_blinding_st BN_BLINDING; | ||
283 | typedef struct bn_mont_ctx_st BN_MONT_CTX; | ||
284 | typedef struct bn_recp_ctx_st BN_RECP_CTX; | ||
285 | typedef struct bn_gencb_st BN_GENCB; | ||
286 | #endif | ||
287 | |||
288 | struct bignum_st | ||
246 | { | 289 | { |
247 | BN_ULONG *d; /* Pointer to an array of 'BN_BITS2' bit chunks. */ | 290 | BN_ULONG *d; /* Pointer to an array of 'BN_BITS2' bit chunks. */ |
248 | int top; /* Index of last used d +1. */ | 291 | int top; /* Index of last used d +1. */ |
@@ -250,44 +293,64 @@ typedef struct bignum_st | |||
250 | int dmax; /* Size of the d array. */ | 293 | int dmax; /* Size of the d array. */ |
251 | int neg; /* one if the number is negative */ | 294 | int neg; /* one if the number is negative */ |
252 | int flags; | 295 | int flags; |
253 | } BIGNUM; | 296 | }; |
254 | |||
255 | /* Used for temp variables (declaration hidden in bn_lcl.h) */ | ||
256 | typedef struct bignum_ctx BN_CTX; | ||
257 | |||
258 | typedef struct bn_blinding_st | ||
259 | { | ||
260 | int init; | ||
261 | BIGNUM *A; | ||
262 | BIGNUM *Ai; | ||
263 | BIGNUM *mod; /* just a reference */ | ||
264 | unsigned long thread_id; /* added in OpenSSL 0.9.6j and 0.9.7b; | ||
265 | * used only by crypto/rsa/rsa_eay.c, rsa_lib.c */ | ||
266 | } BN_BLINDING; | ||
267 | 297 | ||
268 | /* Used for montgomery multiplication */ | 298 | /* Used for montgomery multiplication */ |
269 | typedef struct bn_mont_ctx_st | 299 | struct bn_mont_ctx_st |
270 | { | 300 | { |
271 | int ri; /* number of bits in R */ | 301 | int ri; /* number of bits in R */ |
272 | BIGNUM RR; /* used to convert to montgomery form */ | 302 | BIGNUM RR; /* used to convert to montgomery form */ |
273 | BIGNUM N; /* The modulus */ | 303 | BIGNUM N; /* The modulus */ |
274 | BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1 | 304 | BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1 |
275 | * (Ni is only stored for bignum algorithm) */ | 305 | * (Ni is only stored for bignum algorithm) */ |
306 | #if 0 | ||
307 | /* OpenSSL 0.9.9 preview: */ | ||
308 | BN_ULONG n0[2];/* least significant word(s) of Ni */ | ||
309 | #else | ||
276 | BN_ULONG n0; /* least significant word of Ni */ | 310 | BN_ULONG n0; /* least significant word of Ni */ |
311 | #endif | ||
277 | int flags; | 312 | int flags; |
278 | } BN_MONT_CTX; | 313 | }; |
279 | 314 | ||
280 | /* Used for reciprocal division/mod functions | 315 | /* Used for reciprocal division/mod functions |
281 | * It cannot be shared between threads | 316 | * It cannot be shared between threads |
282 | */ | 317 | */ |
283 | typedef struct bn_recp_ctx_st | 318 | struct bn_recp_ctx_st |
284 | { | 319 | { |
285 | BIGNUM N; /* the divisor */ | 320 | BIGNUM N; /* the divisor */ |
286 | BIGNUM Nr; /* the reciprocal */ | 321 | BIGNUM Nr; /* the reciprocal */ |
287 | int num_bits; | 322 | int num_bits; |
288 | int shift; | 323 | int shift; |
289 | int flags; | 324 | int flags; |
290 | } BN_RECP_CTX; | 325 | }; |
326 | |||
327 | /* Used for slow "generation" functions. */ | ||
328 | struct bn_gencb_st | ||
329 | { | ||
330 | unsigned int ver; /* To handle binary (in)compatibility */ | ||
331 | void *arg; /* callback-specific data */ | ||
332 | union | ||
333 | { | ||
334 | /* if(ver==1) - handles old style callbacks */ | ||
335 | void (*cb_1)(int, int, void *); | ||
336 | /* if(ver==2) - new callback style */ | ||
337 | int (*cb_2)(int, int, BN_GENCB *); | ||
338 | } cb; | ||
339 | }; | ||
340 | /* Wrapper function to make using BN_GENCB easier, */ | ||
341 | int BN_GENCB_call(BN_GENCB *cb, int a, int b); | ||
342 | /* Macro to populate a BN_GENCB structure with an "old"-style callback */ | ||
343 | #define BN_GENCB_set_old(gencb, callback, cb_arg) { \ | ||
344 | BN_GENCB *tmp_gencb = (gencb); \ | ||
345 | tmp_gencb->ver = 1; \ | ||
346 | tmp_gencb->arg = (cb_arg); \ | ||
347 | tmp_gencb->cb.cb_1 = (callback); } | ||
348 | /* Macro to populate a BN_GENCB structure with a "new"-style callback */ | ||
349 | #define BN_GENCB_set(gencb, callback, cb_arg) { \ | ||
350 | BN_GENCB *tmp_gencb = (gencb); \ | ||
351 | tmp_gencb->ver = 2; \ | ||
352 | tmp_gencb->arg = (cb_arg); \ | ||
353 | tmp_gencb->cb.cb_2 = (callback); } | ||
291 | 354 | ||
292 | #define BN_prime_checks 0 /* default: select number of iterations | 355 | #define BN_prime_checks 0 /* default: select number of iterations |
293 | based on the size of the number */ | 356 | based on the size of the number */ |
@@ -312,24 +375,33 @@ typedef struct bn_recp_ctx_st | |||
312 | 375 | ||
313 | #define BN_num_bytes(a) ((BN_num_bits(a)+7)/8) | 376 | #define BN_num_bytes(a) ((BN_num_bits(a)+7)/8) |
314 | 377 | ||
315 | /* Note that BN_abs_is_word does not work reliably for w == 0 */ | 378 | /* Note that BN_abs_is_word didn't work reliably for w == 0 until 0.9.8 */ |
316 | #define BN_abs_is_word(a,w) (((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w))) | 379 | #define BN_abs_is_word(a,w) ((((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w))) || \ |
317 | #define BN_is_zero(a) (((a)->top == 0) || BN_abs_is_word(a,0)) | 380 | (((w) == 0) && ((a)->top == 0))) |
381 | #define BN_is_zero(a) ((a)->top == 0) | ||
318 | #define BN_is_one(a) (BN_abs_is_word((a),1) && !(a)->neg) | 382 | #define BN_is_one(a) (BN_abs_is_word((a),1) && !(a)->neg) |
319 | #define BN_is_word(a,w) ((w) ? BN_abs_is_word((a),(w)) && !(a)->neg : \ | 383 | #define BN_is_word(a,w) (BN_abs_is_word((a),(w)) && (!(w) || !(a)->neg)) |
320 | BN_is_zero((a))) | ||
321 | #define BN_is_odd(a) (((a)->top > 0) && ((a)->d[0] & 1)) | 384 | #define BN_is_odd(a) (((a)->top > 0) && ((a)->d[0] & 1)) |
322 | 385 | ||
323 | #define BN_one(a) (BN_set_word((a),1)) | 386 | #define BN_one(a) (BN_set_word((a),1)) |
387 | #define BN_zero_ex(a) \ | ||
388 | do { \ | ||
389 | BIGNUM *_tmp_bn = (a); \ | ||
390 | _tmp_bn->top = 0; \ | ||
391 | _tmp_bn->neg = 0; \ | ||
392 | } while(0) | ||
393 | #ifdef OPENSSL_NO_DEPRECATED | ||
394 | #define BN_zero(a) BN_zero_ex(a) | ||
395 | #else | ||
324 | #define BN_zero(a) (BN_set_word((a),0)) | 396 | #define BN_zero(a) (BN_set_word((a),0)) |
325 | 397 | #endif | |
326 | /*#define BN_ascii2bn(a) BN_hex2bn(a) */ | ||
327 | /*#define BN_bn2ascii(a) BN_bn2hex(a) */ | ||
328 | 398 | ||
329 | const BIGNUM *BN_value_one(void); | 399 | const BIGNUM *BN_value_one(void); |
330 | char * BN_options(void); | 400 | char * BN_options(void); |
331 | BN_CTX *BN_CTX_new(void); | 401 | BN_CTX *BN_CTX_new(void); |
402 | #ifndef OPENSSL_NO_DEPRECATED | ||
332 | void BN_CTX_init(BN_CTX *c); | 403 | void BN_CTX_init(BN_CTX *c); |
404 | #endif | ||
333 | void BN_CTX_free(BN_CTX *c); | 405 | void BN_CTX_free(BN_CTX *c); |
334 | void BN_CTX_start(BN_CTX *ctx); | 406 | void BN_CTX_start(BN_CTX *ctx); |
335 | BIGNUM *BN_CTX_get(BN_CTX *ctx); | 407 | BIGNUM *BN_CTX_get(BN_CTX *ctx); |
@@ -355,6 +427,16 @@ int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); | |||
355 | int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); | 427 | int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); |
356 | int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); | 428 | int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); |
357 | int BN_sqr(BIGNUM *r, const BIGNUM *a,BN_CTX *ctx); | 429 | int BN_sqr(BIGNUM *r, const BIGNUM *a,BN_CTX *ctx); |
430 | /** BN_set_negative sets sign of a BIGNUM | ||
431 | * \param b pointer to the BIGNUM object | ||
432 | * \param n 0 if the BIGNUM b should be positive and a value != 0 otherwise | ||
433 | */ | ||
434 | void BN_set_negative(BIGNUM *b, int n); | ||
435 | /** BN_is_negative returns 1 if the BIGNUM is negative | ||
436 | * \param a pointer to the BIGNUM object | ||
437 | * \return 1 if a < 0 and 0 otherwise | ||
438 | */ | ||
439 | #define BN_is_negative(a) ((a)->neg != 0) | ||
358 | 440 | ||
359 | int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, | 441 | int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, |
360 | BN_CTX *ctx); | 442 | BN_CTX *ctx); |
@@ -428,6 +510,9 @@ BIGNUM *BN_mod_inverse(BIGNUM *ret, | |||
428 | const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx); | 510 | const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx); |
429 | BIGNUM *BN_mod_sqrt(BIGNUM *ret, | 511 | BIGNUM *BN_mod_sqrt(BIGNUM *ret, |
430 | const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx); | 512 | const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx); |
513 | |||
514 | /* Deprecated versions */ | ||
515 | #ifndef OPENSSL_NO_DEPRECATED | ||
431 | BIGNUM *BN_generate_prime(BIGNUM *ret,int bits,int safe, | 516 | BIGNUM *BN_generate_prime(BIGNUM *ret,int bits,int safe, |
432 | const BIGNUM *add, const BIGNUM *rem, | 517 | const BIGNUM *add, const BIGNUM *rem, |
433 | void (*callback)(int,int,void *),void *cb_arg); | 518 | void (*callback)(int,int,void *),void *cb_arg); |
@@ -437,19 +522,14 @@ int BN_is_prime(const BIGNUM *p,int nchecks, | |||
437 | int BN_is_prime_fasttest(const BIGNUM *p,int nchecks, | 522 | int BN_is_prime_fasttest(const BIGNUM *p,int nchecks, |
438 | void (*callback)(int,int,void *),BN_CTX *ctx,void *cb_arg, | 523 | void (*callback)(int,int,void *),BN_CTX *ctx,void *cb_arg, |
439 | int do_trial_division); | 524 | int do_trial_division); |
525 | #endif /* !defined(OPENSSL_NO_DEPRECATED) */ | ||
440 | 526 | ||
441 | #ifdef OPENSSL_FIPS | 527 | /* Newer versions */ |
442 | int BN_X931_derive_prime(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, | 528 | int BN_generate_prime_ex(BIGNUM *ret,int bits,int safe, const BIGNUM *add, |
443 | void (*cb)(int, int, void *), void *cb_arg, | 529 | const BIGNUM *rem, BN_GENCB *cb); |
444 | const BIGNUM *Xp, const BIGNUM *Xp1, const BIGNUM *Xp2, | 530 | int BN_is_prime_ex(const BIGNUM *p,int nchecks, BN_CTX *ctx, BN_GENCB *cb); |
445 | const BIGNUM *e, BN_CTX *ctx); | 531 | int BN_is_prime_fasttest_ex(const BIGNUM *p,int nchecks, BN_CTX *ctx, |
446 | int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx); | 532 | int do_trial_division, BN_GENCB *cb); |
447 | int BN_X931_generate_prime(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, | ||
448 | BIGNUM *Xp1, BIGNUM *Xp2, | ||
449 | const BIGNUM *Xp, | ||
450 | const BIGNUM *e, BN_CTX *ctx, | ||
451 | void (*cb)(int, int, void *), void *cb_arg); | ||
452 | #endif | ||
453 | 533 | ||
454 | BN_MONT_CTX *BN_MONT_CTX_new(void ); | 534 | BN_MONT_CTX *BN_MONT_CTX_new(void ); |
455 | void BN_MONT_CTX_init(BN_MONT_CTX *ctx); | 535 | void BN_MONT_CTX_init(BN_MONT_CTX *ctx); |
@@ -465,14 +545,31 @@ BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to,BN_MONT_CTX *from); | |||
465 | BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock, | 545 | BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock, |
466 | const BIGNUM *mod, BN_CTX *ctx); | 546 | const BIGNUM *mod, BN_CTX *ctx); |
467 | 547 | ||
468 | BN_BLINDING *BN_BLINDING_new(BIGNUM *A,BIGNUM *Ai,BIGNUM *mod); | 548 | /* BN_BLINDING flags */ |
549 | #define BN_BLINDING_NO_UPDATE 0x00000001 | ||
550 | #define BN_BLINDING_NO_RECREATE 0x00000002 | ||
551 | |||
552 | BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, /* const */ BIGNUM *mod); | ||
469 | void BN_BLINDING_free(BN_BLINDING *b); | 553 | void BN_BLINDING_free(BN_BLINDING *b); |
470 | int BN_BLINDING_update(BN_BLINDING *b,BN_CTX *ctx); | 554 | int BN_BLINDING_update(BN_BLINDING *b,BN_CTX *ctx); |
471 | int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *r, BN_CTX *ctx); | 555 | int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx); |
472 | int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx); | 556 | int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx); |
473 | 557 | int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *); | |
558 | int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *); | ||
559 | unsigned long BN_BLINDING_get_thread_id(const BN_BLINDING *); | ||
560 | void BN_BLINDING_set_thread_id(BN_BLINDING *, unsigned long); | ||
561 | unsigned long BN_BLINDING_get_flags(const BN_BLINDING *); | ||
562 | void BN_BLINDING_set_flags(BN_BLINDING *, unsigned long); | ||
563 | BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b, | ||
564 | const BIGNUM *e, /* const */ BIGNUM *m, BN_CTX *ctx, | ||
565 | int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
566 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx), | ||
567 | BN_MONT_CTX *m_ctx); | ||
568 | |||
569 | #ifndef OPENSSL_NO_DEPRECATED | ||
474 | void BN_set_params(int mul,int high,int low,int mont); | 570 | void BN_set_params(int mul,int high,int low,int mont); |
475 | int BN_get_params(int which); /* 0, mul, 1 high, 2 low, 3 mont */ | 571 | int BN_get_params(int which); /* 0, mul, 1 high, 2 low, 3 mont */ |
572 | #endif | ||
476 | 573 | ||
477 | void BN_RECP_CTX_init(BN_RECP_CTX *recp); | 574 | void BN_RECP_CTX_init(BN_RECP_CTX *recp); |
478 | BN_RECP_CTX *BN_RECP_CTX_new(void); | 575 | BN_RECP_CTX *BN_RECP_CTX_new(void); |
@@ -485,15 +582,162 @@ int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | |||
485 | int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, | 582 | int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, |
486 | BN_RECP_CTX *recp, BN_CTX *ctx); | 583 | BN_RECP_CTX *recp, BN_CTX *ctx); |
487 | 584 | ||
585 | /* Functions for arithmetic over binary polynomials represented by BIGNUMs. | ||
586 | * | ||
587 | * The BIGNUM::neg property of BIGNUMs representing binary polynomials is | ||
588 | * ignored. | ||
589 | * | ||
590 | * Note that input arguments are not const so that their bit arrays can | ||
591 | * be expanded to the appropriate size if needed. | ||
592 | */ | ||
593 | |||
594 | int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); /*r = a + b*/ | ||
595 | #define BN_GF2m_sub(r, a, b) BN_GF2m_add(r, a, b) | ||
596 | int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p); /*r=a mod p*/ | ||
597 | int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, | ||
598 | const BIGNUM *p, BN_CTX *ctx); /* r = (a * b) mod p */ | ||
599 | int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
600 | BN_CTX *ctx); /* r = (a * a) mod p */ | ||
601 | int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *b, const BIGNUM *p, | ||
602 | BN_CTX *ctx); /* r = (1 / b) mod p */ | ||
603 | int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, | ||
604 | const BIGNUM *p, BN_CTX *ctx); /* r = (a / b) mod p */ | ||
605 | int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, | ||
606 | const BIGNUM *p, BN_CTX *ctx); /* r = (a ^ b) mod p */ | ||
607 | int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
608 | BN_CTX *ctx); /* r = sqrt(a) mod p */ | ||
609 | int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
610 | BN_CTX *ctx); /* r^2 + r = a mod p */ | ||
611 | #define BN_GF2m_cmp(a, b) BN_ucmp((a), (b)) | ||
612 | /* Some functions allow for representation of the irreducible polynomials | ||
613 | * as an unsigned int[], say p. The irreducible f(t) is then of the form: | ||
614 | * t^p[0] + t^p[1] + ... + t^p[k] | ||
615 | * where m = p[0] > p[1] > ... > p[k] = 0. | ||
616 | */ | ||
617 | int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[]); | ||
618 | /* r = a mod p */ | ||
619 | int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, | ||
620 | const unsigned int p[], BN_CTX *ctx); /* r = (a * b) mod p */ | ||
621 | int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[], | ||
622 | BN_CTX *ctx); /* r = (a * a) mod p */ | ||
623 | int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *b, const unsigned int p[], | ||
624 | BN_CTX *ctx); /* r = (1 / b) mod p */ | ||
625 | int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, | ||
626 | const unsigned int p[], BN_CTX *ctx); /* r = (a / b) mod p */ | ||
627 | int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, | ||
628 | const unsigned int p[], BN_CTX *ctx); /* r = (a ^ b) mod p */ | ||
629 | int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, | ||
630 | const unsigned int p[], BN_CTX *ctx); /* r = sqrt(a) mod p */ | ||
631 | int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a, | ||
632 | const unsigned int p[], BN_CTX *ctx); /* r^2 + r = a mod p */ | ||
633 | int BN_GF2m_poly2arr(const BIGNUM *a, unsigned int p[], int max); | ||
634 | int BN_GF2m_arr2poly(const unsigned int p[], BIGNUM *a); | ||
635 | |||
636 | /* faster mod functions for the 'NIST primes' | ||
637 | * 0 <= a < p^2 */ | ||
638 | int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); | ||
639 | int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); | ||
640 | int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); | ||
641 | int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); | ||
642 | int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); | ||
643 | |||
644 | const BIGNUM *BN_get0_nist_prime_192(void); | ||
645 | const BIGNUM *BN_get0_nist_prime_224(void); | ||
646 | const BIGNUM *BN_get0_nist_prime_256(void); | ||
647 | const BIGNUM *BN_get0_nist_prime_384(void); | ||
648 | const BIGNUM *BN_get0_nist_prime_521(void); | ||
649 | |||
488 | /* library internal functions */ | 650 | /* library internal functions */ |
489 | 651 | ||
490 | #define bn_expand(a,bits) ((((((bits+BN_BITS2-1))/BN_BITS2)) <= (a)->dmax)?\ | 652 | #define bn_expand(a,bits) ((((((bits+BN_BITS2-1))/BN_BITS2)) <= (a)->dmax)?\ |
491 | (a):bn_expand2((a),(bits)/BN_BITS2+1)) | 653 | (a):bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2)) |
492 | #define bn_wexpand(a,words) (((words) <= (a)->dmax)?(a):bn_expand2((a),(words))) | 654 | #define bn_wexpand(a,words) (((words) <= (a)->dmax)?(a):bn_expand2((a),(words))) |
493 | BIGNUM *bn_expand2(BIGNUM *a, int words); | 655 | BIGNUM *bn_expand2(BIGNUM *a, int words); |
494 | BIGNUM *bn_dup_expand(const BIGNUM *a, int words); | 656 | #ifndef OPENSSL_NO_DEPRECATED |
657 | BIGNUM *bn_dup_expand(const BIGNUM *a, int words); /* unused */ | ||
658 | #endif | ||
659 | |||
660 | /* Bignum consistency macros | ||
661 | * There is one "API" macro, bn_fix_top(), for stripping leading zeroes from | ||
662 | * bignum data after direct manipulations on the data. There is also an | ||
663 | * "internal" macro, bn_check_top(), for verifying that there are no leading | ||
664 | * zeroes. Unfortunately, some auditing is required due to the fact that | ||
665 | * bn_fix_top() has become an overabused duct-tape because bignum data is | ||
666 | * occasionally passed around in an inconsistent state. So the following | ||
667 | * changes have been made to sort this out; | ||
668 | * - bn_fix_top()s implementation has been moved to bn_correct_top() | ||
669 | * - if BN_DEBUG isn't defined, bn_fix_top() maps to bn_correct_top(), and | ||
670 | * bn_check_top() is as before. | ||
671 | * - if BN_DEBUG *is* defined; | ||
672 | * - bn_check_top() tries to pollute unused words even if the bignum 'top' is | ||
673 | * consistent. (ed: only if BN_DEBUG_RAND is defined) | ||
674 | * - bn_fix_top() maps to bn_check_top() rather than "fixing" anything. | ||
675 | * The idea is to have debug builds flag up inconsistent bignums when they | ||
676 | * occur. If that occurs in a bn_fix_top(), we examine the code in question; if | ||
677 | * the use of bn_fix_top() was appropriate (ie. it follows directly after code | ||
678 | * that manipulates the bignum) it is converted to bn_correct_top(), and if it | ||
679 | * was not appropriate, we convert it permanently to bn_check_top() and track | ||
680 | * down the cause of the bug. Eventually, no internal code should be using the | ||
681 | * bn_fix_top() macro. External applications and libraries should try this with | ||
682 | * their own code too, both in terms of building against the openssl headers | ||
683 | * with BN_DEBUG defined *and* linking with a version of OpenSSL built with it | ||
684 | * defined. This not only improves external code, it provides more test | ||
685 | * coverage for openssl's own code. | ||
686 | */ | ||
687 | |||
688 | #ifdef BN_DEBUG | ||
495 | 689 | ||
496 | #define bn_fix_top(a) \ | 690 | /* We only need assert() when debugging */ |
691 | #include <assert.h> | ||
692 | |||
693 | #ifdef BN_DEBUG_RAND | ||
694 | /* To avoid "make update" cvs wars due to BN_DEBUG, use some tricks */ | ||
695 | #ifndef RAND_pseudo_bytes | ||
696 | int RAND_pseudo_bytes(unsigned char *buf,int num); | ||
697 | #define BN_DEBUG_TRIX | ||
698 | #endif | ||
699 | #define bn_pollute(a) \ | ||
700 | do { \ | ||
701 | const BIGNUM *_bnum1 = (a); \ | ||
702 | if(_bnum1->top < _bnum1->dmax) { \ | ||
703 | unsigned char _tmp_char; \ | ||
704 | /* We cast away const without the compiler knowing, any \ | ||
705 | * *genuinely* constant variables that aren't mutable \ | ||
706 | * wouldn't be constructed with top!=dmax. */ \ | ||
707 | BN_ULONG *_not_const; \ | ||
708 | memcpy(&_not_const, &_bnum1->d, sizeof(BN_ULONG*)); \ | ||
709 | RAND_pseudo_bytes(&_tmp_char, 1); \ | ||
710 | memset((unsigned char *)(_not_const + _bnum1->top), _tmp_char, \ | ||
711 | (_bnum1->dmax - _bnum1->top) * sizeof(BN_ULONG)); \ | ||
712 | } \ | ||
713 | } while(0) | ||
714 | #ifdef BN_DEBUG_TRIX | ||
715 | #undef RAND_pseudo_bytes | ||
716 | #endif | ||
717 | #else | ||
718 | #define bn_pollute(a) | ||
719 | #endif | ||
720 | #define bn_check_top(a) \ | ||
721 | do { \ | ||
722 | const BIGNUM *_bnum2 = (a); \ | ||
723 | if (_bnum2 != NULL) { \ | ||
724 | assert((_bnum2->top == 0) || \ | ||
725 | (_bnum2->d[_bnum2->top - 1] != 0)); \ | ||
726 | bn_pollute(_bnum2); \ | ||
727 | } \ | ||
728 | } while(0) | ||
729 | |||
730 | #define bn_fix_top(a) bn_check_top(a) | ||
731 | |||
732 | #else /* !BN_DEBUG */ | ||
733 | |||
734 | #define bn_pollute(a) | ||
735 | #define bn_check_top(a) | ||
736 | #define bn_fix_top(a) bn_correct_top(a) | ||
737 | |||
738 | #endif | ||
739 | |||
740 | #define bn_correct_top(a) \ | ||
497 | { \ | 741 | { \ |
498 | BN_ULONG *ftl; \ | 742 | BN_ULONG *ftl; \ |
499 | if ((a)->top > 0) \ | 743 | if ((a)->top > 0) \ |
@@ -501,6 +745,7 @@ BIGNUM *bn_dup_expand(const BIGNUM *a, int words); | |||
501 | for (ftl= &((a)->d[(a)->top-1]); (a)->top > 0; (a)->top--) \ | 745 | for (ftl= &((a)->d[(a)->top-1]); (a)->top > 0; (a)->top--) \ |
502 | if (*(ftl--)) break; \ | 746 | if (*(ftl--)) break; \ |
503 | } \ | 747 | } \ |
748 | bn_pollute(a); \ | ||
504 | } | 749 | } |
505 | 750 | ||
506 | BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w); | 751 | BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w); |
@@ -510,15 +755,17 @@ BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d); | |||
510 | BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,int num); | 755 | BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,int num); |
511 | BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,int num); | 756 | BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,int num); |
512 | 757 | ||
513 | #ifdef BN_DEBUG | 758 | /* Primes from RFC 2409 */ |
514 | void bn_dump1(FILE *o, const char *a, const BN_ULONG *b,int n); | 759 | BIGNUM *get_rfc2409_prime_768(BIGNUM *bn); |
515 | # define bn_print(a) {fprintf(stderr, #a "="); BN_print_fp(stderr,a); \ | 760 | BIGNUM *get_rfc2409_prime_1024(BIGNUM *bn); |
516 | fprintf(stderr,"\n");} | 761 | |
517 | # define bn_dump(a,n) bn_dump1(stderr,#a,a,n); | 762 | /* Primes from RFC 3526 */ |
518 | #else | 763 | BIGNUM *get_rfc3526_prime_1536(BIGNUM *bn); |
519 | # define bn_print(a) | 764 | BIGNUM *get_rfc3526_prime_2048(BIGNUM *bn); |
520 | # define bn_dump(a,b) | 765 | BIGNUM *get_rfc3526_prime_3072(BIGNUM *bn); |
521 | #endif | 766 | BIGNUM *get_rfc3526_prime_4096(BIGNUM *bn); |
767 | BIGNUM *get_rfc3526_prime_6144(BIGNUM *bn); | ||
768 | BIGNUM *get_rfc3526_prime_8192(BIGNUM *bn); | ||
522 | 769 | ||
523 | int BN_bntest_rand(BIGNUM *rnd, int bits, int top,int bottom); | 770 | int BN_bntest_rand(BIGNUM *rnd, int bits, int top,int bottom); |
524 | 771 | ||
@@ -531,18 +778,30 @@ void ERR_load_BN_strings(void); | |||
531 | /* Error codes for the BN functions. */ | 778 | /* Error codes for the BN functions. */ |
532 | 779 | ||
533 | /* Function codes. */ | 780 | /* Function codes. */ |
534 | #define BN_F_BN_BLINDING_CONVERT 100 | 781 | #define BN_F_BNRAND 127 |
535 | #define BN_F_BN_BLINDING_INVERT 101 | 782 | #define BN_F_BN_BLINDING_CONVERT_EX 100 |
783 | #define BN_F_BN_BLINDING_CREATE_PARAM 128 | ||
784 | #define BN_F_BN_BLINDING_INVERT_EX 101 | ||
536 | #define BN_F_BN_BLINDING_NEW 102 | 785 | #define BN_F_BN_BLINDING_NEW 102 |
537 | #define BN_F_BN_BLINDING_UPDATE 103 | 786 | #define BN_F_BN_BLINDING_UPDATE 103 |
538 | #define BN_F_BN_BN2DEC 104 | 787 | #define BN_F_BN_BN2DEC 104 |
539 | #define BN_F_BN_BN2HEX 105 | 788 | #define BN_F_BN_BN2HEX 105 |
540 | #define BN_F_BN_CTX_GET 116 | 789 | #define BN_F_BN_CTX_GET 116 |
541 | #define BN_F_BN_CTX_NEW 106 | 790 | #define BN_F_BN_CTX_NEW 106 |
791 | #define BN_F_BN_CTX_START 129 | ||
542 | #define BN_F_BN_DIV 107 | 792 | #define BN_F_BN_DIV 107 |
793 | #define BN_F_BN_DIV_NO_BRANCH 138 | ||
794 | #define BN_F_BN_DIV_RECP 130 | ||
543 | #define BN_F_BN_EXP 123 | 795 | #define BN_F_BN_EXP 123 |
544 | #define BN_F_BN_EXPAND2 108 | 796 | #define BN_F_BN_EXPAND2 108 |
545 | #define BN_F_BN_EXPAND_INTERNAL 120 | 797 | #define BN_F_BN_EXPAND_INTERNAL 120 |
798 | #define BN_F_BN_GF2M_MOD 131 | ||
799 | #define BN_F_BN_GF2M_MOD_EXP 132 | ||
800 | #define BN_F_BN_GF2M_MOD_MUL 133 | ||
801 | #define BN_F_BN_GF2M_MOD_SOLVE_QUAD 134 | ||
802 | #define BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR 135 | ||
803 | #define BN_F_BN_GF2M_MOD_SQR 136 | ||
804 | #define BN_F_BN_GF2M_MOD_SQRT 137 | ||
546 | #define BN_F_BN_MOD_EXP2_MONT 118 | 805 | #define BN_F_BN_MOD_EXP2_MONT 118 |
547 | #define BN_F_BN_MOD_EXP_MONT 109 | 806 | #define BN_F_BN_MOD_EXP_MONT 109 |
548 | #define BN_F_BN_MOD_EXP_MONT_CONSTTIME 124 | 807 | #define BN_F_BN_MOD_EXP_MONT_CONSTTIME 124 |
@@ -550,6 +809,7 @@ void ERR_load_BN_strings(void); | |||
550 | #define BN_F_BN_MOD_EXP_RECP 125 | 809 | #define BN_F_BN_MOD_EXP_RECP 125 |
551 | #define BN_F_BN_MOD_EXP_SIMPLE 126 | 810 | #define BN_F_BN_MOD_EXP_SIMPLE 126 |
552 | #define BN_F_BN_MOD_INVERSE 110 | 811 | #define BN_F_BN_MOD_INVERSE 110 |
812 | #define BN_F_BN_MOD_INVERSE_NO_BRANCH 139 | ||
553 | #define BN_F_BN_MOD_LSHIFT_QUICK 119 | 813 | #define BN_F_BN_MOD_LSHIFT_QUICK 119 |
554 | #define BN_F_BN_MOD_MUL_RECIPROCAL 111 | 814 | #define BN_F_BN_MOD_MUL_RECIPROCAL 111 |
555 | #define BN_F_BN_MOD_SQRT 121 | 815 | #define BN_F_BN_MOD_SQRT 121 |
@@ -573,6 +833,7 @@ void ERR_load_BN_strings(void); | |||
573 | #define BN_R_NOT_A_SQUARE 111 | 833 | #define BN_R_NOT_A_SQUARE 111 |
574 | #define BN_R_NOT_INITIALIZED 107 | 834 | #define BN_R_NOT_INITIALIZED 107 |
575 | #define BN_R_NO_INVERSE 108 | 835 | #define BN_R_NO_INVERSE 108 |
836 | #define BN_R_NO_SOLUTION 116 | ||
576 | #define BN_R_P_IS_NOT_PRIME 112 | 837 | #define BN_R_P_IS_NOT_PRIME 112 |
577 | #define BN_R_TOO_MANY_ITERATIONS 113 | 838 | #define BN_R_TOO_MANY_ITERATIONS 113 |
578 | #define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109 | 839 | #define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109 |