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.h399
1 files changed, 69 insertions, 330 deletions
diff --git a/src/lib/libcrypto/bn/bn.h b/src/lib/libcrypto/bn/bn.h
index 6d754d5547..1251521c54 100644
--- a/src/lib/libcrypto/bn/bn.h
+++ b/src/lib/libcrypto/bn/bn.h
@@ -55,19 +55,6 @@
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 */
71 58
72#ifndef HEADER_BN_H 59#ifndef HEADER_BN_H
73#define HEADER_BN_H 60#define HEADER_BN_H
@@ -76,23 +63,14 @@
76#ifndef OPENSSL_NO_FP_API 63#ifndef OPENSSL_NO_FP_API
77#include <stdio.h> /* FILE */ 64#include <stdio.h> /* FILE */
78#endif 65#endif
79#include <openssl/ossl_typ.h>
80 66
81#ifdef __cplusplus 67#ifdef __cplusplus
82extern "C" { 68extern "C" {
83#endif 69#endif
84 70
85/* These preprocessor symbols control various aspects of the bignum headers and 71#ifdef OPENSSL_SYS_VMS
86 * library code. They're not defined by any "normal" configuration, as they are 72#undef BN_LLONG /* experimental, so far... */
87 * intended for development and testing purposes. NB: defining all three can be 73#endif
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 */
96 74
97#define BN_MUL_COMBA 75#define BN_MUL_COMBA
98#define BN_SQR_COMBA 76#define BN_SQR_COMBA
@@ -165,12 +143,10 @@ extern "C" {
165#endif 143#endif
166 144
167#ifdef THIRTY_TWO_BIT 145#ifdef THIRTY_TWO_BIT
168#ifdef BN_LLONG 146#if defined(OPENSSL_SYS_WIN32) && !defined(__GNUC__)
169# if defined(OPENSSL_SYS_WIN32) && !defined(__GNUC__) 147#define BN_ULLONG unsigned _int64
170# define BN_ULLONG unsigned __int64 148#else
171# else 149#define BN_ULLONG unsigned long long
172# define BN_ULLONG unsigned long long
173# endif
174#endif 150#endif
175#define BN_ULONG unsigned long 151#define BN_ULONG unsigned long
176#define BN_LONG long 152#define BN_LONG long
@@ -243,23 +219,15 @@ extern "C" {
243 219
244#define BN_DEFAULT_BITS 1280 220#define BN_DEFAULT_BITS 1280
245 221
246#define BN_FLG_MALLOCED 0x01 222#ifdef BIGNUM
247#define BN_FLG_STATIC_DATA 0x02 223#undef BIGNUM
248#define BN_FLG_CONSTTIME 0x04 /* avoid leaking exponent information through timing,
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 224#endif
259 225
260#ifndef OPENSSL_NO_DEPRECATED 226#define BN_FLG_MALLOCED 0x01
227#define BN_FLG_STATIC_DATA 0x02
228#define BN_FLG_EXP_CONSTTIME 0x04 /* avoid leaking exponent information through timings
229 * (BN_mod_exp_mont() will call BN_mod_exp_mont_consttime) */
261#define BN_FLG_FREE 0x8000 /* used for debuging */ 230#define BN_FLG_FREE 0x8000 /* used for debuging */
262#endif
263#define BN_set_flags(b,n) ((b)->flags|=(n)) 231#define BN_set_flags(b,n) ((b)->flags|=(n))
264#define BN_get_flags(b,n) ((b)->flags&(n)) 232#define BN_get_flags(b,n) ((b)->flags&(n))
265 233
@@ -274,18 +242,7 @@ extern "C" {
274 | BN_FLG_STATIC_DATA \ 242 | BN_FLG_STATIC_DATA \
275 | (n))) 243 | (n)))
276 244
277/* Already declared in ossl_typ.h */ 245typedef struct bignum_st
278#if 0
279typedef struct bignum_st BIGNUM;
280/* Used for temp variables (declaration hidden in bn_lcl.h) */
281typedef struct bignum_ctx BN_CTX;
282typedef struct bn_blinding_st BN_BLINDING;
283typedef struct bn_mont_ctx_st BN_MONT_CTX;
284typedef struct bn_recp_ctx_st BN_RECP_CTX;
285typedef struct bn_gencb_st BN_GENCB;
286#endif
287
288struct bignum_st
289 { 246 {
290 BN_ULONG *d; /* Pointer to an array of 'BN_BITS2' bit chunks. */ 247 BN_ULONG *d; /* Pointer to an array of 'BN_BITS2' bit chunks. */
291 int top; /* Index of last used d +1. */ 248 int top; /* Index of last used d +1. */
@@ -293,64 +250,44 @@ struct bignum_st
293 int dmax; /* Size of the d array. */ 250 int dmax; /* Size of the d array. */
294 int neg; /* one if the number is negative */ 251 int neg; /* one if the number is negative */
295 int flags; 252 int flags;
296 }; 253 } BIGNUM;
254
255/* Used for temp variables (declaration hidden in bn_lcl.h) */
256typedef struct bignum_ctx BN_CTX;
257
258typedef 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;
297 267
298/* Used for montgomery multiplication */ 268/* Used for montgomery multiplication */
299struct bn_mont_ctx_st 269typedef struct bn_mont_ctx_st
300 { 270 {
301 int ri; /* number of bits in R */ 271 int ri; /* number of bits in R */
302 BIGNUM RR; /* used to convert to montgomery form */ 272 BIGNUM RR; /* used to convert to montgomery form */
303 BIGNUM N; /* The modulus */ 273 BIGNUM N; /* The modulus */
304 BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1 274 BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1
305 * (Ni is only stored for bignum algorithm) */ 275 * (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
310 BN_ULONG n0; /* least significant word of Ni */ 276 BN_ULONG n0; /* least significant word of Ni */
311#endif
312 int flags; 277 int flags;
313 }; 278 } BN_MONT_CTX;
314 279
315/* Used for reciprocal division/mod functions 280/* Used for reciprocal division/mod functions
316 * It cannot be shared between threads 281 * It cannot be shared between threads
317 */ 282 */
318struct bn_recp_ctx_st 283typedef struct bn_recp_ctx_st
319 { 284 {
320 BIGNUM N; /* the divisor */ 285 BIGNUM N; /* the divisor */
321 BIGNUM Nr; /* the reciprocal */ 286 BIGNUM Nr; /* the reciprocal */
322 int num_bits; 287 int num_bits;
323 int shift; 288 int shift;
324 int flags; 289 int flags;
325 }; 290 } BN_RECP_CTX;
326
327/* Used for slow "generation" functions. */
328struct 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, */
341int 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); }
354 291
355#define BN_prime_checks 0 /* default: select number of iterations 292#define BN_prime_checks 0 /* default: select number of iterations
356 based on the size of the number */ 293 based on the size of the number */
@@ -375,33 +312,24 @@ int BN_GENCB_call(BN_GENCB *cb, int a, int b);
375 312
376#define BN_num_bytes(a) ((BN_num_bits(a)+7)/8) 313#define BN_num_bytes(a) ((BN_num_bits(a)+7)/8)
377 314
378/* Note that BN_abs_is_word didn't work reliably for w == 0 until 0.9.8 */ 315/* Note that BN_abs_is_word does not work reliably for w == 0 */
379#define BN_abs_is_word(a,w) ((((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w))) || \ 316#define BN_abs_is_word(a,w) (((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w)))
380 (((w) == 0) && ((a)->top == 0))) 317#define BN_is_zero(a) (((a)->top == 0) || BN_abs_is_word(a,0))
381#define BN_is_zero(a) ((a)->top == 0)
382#define BN_is_one(a) (BN_abs_is_word((a),1) && !(a)->neg) 318#define BN_is_one(a) (BN_abs_is_word((a),1) && !(a)->neg)
383#define BN_is_word(a,w) (BN_abs_is_word((a),(w)) && (!(w) || !(a)->neg)) 319#define BN_is_word(a,w) ((w) ? BN_abs_is_word((a),(w)) && !(a)->neg : \
320 BN_is_zero((a)))
384#define BN_is_odd(a) (((a)->top > 0) && ((a)->d[0] & 1)) 321#define BN_is_odd(a) (((a)->top > 0) && ((a)->d[0] & 1))
385 322
386#define BN_one(a) (BN_set_word((a),1)) 323#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
396#define BN_zero(a) (BN_set_word((a),0)) 324#define BN_zero(a) (BN_set_word((a),0))
397#endif 325
326/*#define BN_ascii2bn(a) BN_hex2bn(a) */
327/*#define BN_bn2ascii(a) BN_bn2hex(a) */
398 328
399const BIGNUM *BN_value_one(void); 329const BIGNUM *BN_value_one(void);
400char * BN_options(void); 330char * BN_options(void);
401BN_CTX *BN_CTX_new(void); 331BN_CTX *BN_CTX_new(void);
402#ifndef OPENSSL_NO_DEPRECATED
403void BN_CTX_init(BN_CTX *c); 332void BN_CTX_init(BN_CTX *c);
404#endif
405void BN_CTX_free(BN_CTX *c); 333void BN_CTX_free(BN_CTX *c);
406void BN_CTX_start(BN_CTX *ctx); 334void BN_CTX_start(BN_CTX *ctx);
407BIGNUM *BN_CTX_get(BN_CTX *ctx); 335BIGNUM *BN_CTX_get(BN_CTX *ctx);
@@ -427,16 +355,6 @@ int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
427int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 355int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
428int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); 356int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
429int BN_sqr(BIGNUM *r, const BIGNUM *a,BN_CTX *ctx); 357int 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 */
434void 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)
440 358
441int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, 359int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
442 BN_CTX *ctx); 360 BN_CTX *ctx);
@@ -510,9 +428,6 @@ BIGNUM *BN_mod_inverse(BIGNUM *ret,
510 const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx); 428 const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx);
511BIGNUM *BN_mod_sqrt(BIGNUM *ret, 429BIGNUM *BN_mod_sqrt(BIGNUM *ret,
512 const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx); 430 const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx);
513
514/* Deprecated versions */
515#ifndef OPENSSL_NO_DEPRECATED
516BIGNUM *BN_generate_prime(BIGNUM *ret,int bits,int safe, 431BIGNUM *BN_generate_prime(BIGNUM *ret,int bits,int safe,
517 const BIGNUM *add, const BIGNUM *rem, 432 const BIGNUM *add, const BIGNUM *rem,
518 void (*callback)(int,int,void *),void *cb_arg); 433 void (*callback)(int,int,void *),void *cb_arg);
@@ -522,14 +437,19 @@ int BN_is_prime(const BIGNUM *p,int nchecks,
522int BN_is_prime_fasttest(const BIGNUM *p,int nchecks, 437int BN_is_prime_fasttest(const BIGNUM *p,int nchecks,
523 void (*callback)(int,int,void *),BN_CTX *ctx,void *cb_arg, 438 void (*callback)(int,int,void *),BN_CTX *ctx,void *cb_arg,
524 int do_trial_division); 439 int do_trial_division);
525#endif /* !defined(OPENSSL_NO_DEPRECATED) */
526 440
527/* Newer versions */ 441#ifdef OPENSSL_FIPS
528int BN_generate_prime_ex(BIGNUM *ret,int bits,int safe, const BIGNUM *add, 442int BN_X931_derive_prime(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
529 const BIGNUM *rem, BN_GENCB *cb); 443 void (*cb)(int, int, void *), void *cb_arg,
530int BN_is_prime_ex(const BIGNUM *p,int nchecks, BN_CTX *ctx, BN_GENCB *cb); 444 const BIGNUM *Xp, const BIGNUM *Xp1, const BIGNUM *Xp2,
531int BN_is_prime_fasttest_ex(const BIGNUM *p,int nchecks, BN_CTX *ctx, 445 const BIGNUM *e, BN_CTX *ctx);
532 int do_trial_division, BN_GENCB *cb); 446int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx);
447int 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
533 453
534BN_MONT_CTX *BN_MONT_CTX_new(void ); 454BN_MONT_CTX *BN_MONT_CTX_new(void );
535void BN_MONT_CTX_init(BN_MONT_CTX *ctx); 455void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
@@ -545,31 +465,14 @@ BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to,BN_MONT_CTX *from);
545BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock, 465BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,
546 const BIGNUM *mod, BN_CTX *ctx); 466 const BIGNUM *mod, BN_CTX *ctx);
547 467
548/* BN_BLINDING flags */ 468BN_BLINDING *BN_BLINDING_new(BIGNUM *A,BIGNUM *Ai,BIGNUM *mod);
549#define BN_BLINDING_NO_UPDATE 0x00000001
550#define BN_BLINDING_NO_RECREATE 0x00000002
551
552BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, /* const */ BIGNUM *mod);
553void BN_BLINDING_free(BN_BLINDING *b); 469void BN_BLINDING_free(BN_BLINDING *b);
554int BN_BLINDING_update(BN_BLINDING *b,BN_CTX *ctx); 470int BN_BLINDING_update(BN_BLINDING *b,BN_CTX *ctx);
555int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx); 471int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *r, BN_CTX *ctx);
556int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx); 472int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
557int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *); 473
558int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *);
559unsigned long BN_BLINDING_get_thread_id(const BN_BLINDING *);
560void BN_BLINDING_set_thread_id(BN_BLINDING *, unsigned long);
561unsigned long BN_BLINDING_get_flags(const BN_BLINDING *);
562void BN_BLINDING_set_flags(BN_BLINDING *, unsigned long);
563BN_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
570void BN_set_params(int mul,int high,int low,int mont); 474void BN_set_params(int mul,int high,int low,int mont);
571int BN_get_params(int which); /* 0, mul, 1 high, 2 low, 3 mont */ 475int BN_get_params(int which); /* 0, mul, 1 high, 2 low, 3 mont */
572#endif
573 476
574void BN_RECP_CTX_init(BN_RECP_CTX *recp); 477void BN_RECP_CTX_init(BN_RECP_CTX *recp);
575BN_RECP_CTX *BN_RECP_CTX_new(void); 478BN_RECP_CTX *BN_RECP_CTX_new(void);
@@ -582,162 +485,15 @@ int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
582int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, 485int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
583 BN_RECP_CTX *recp, BN_CTX *ctx); 486 BN_RECP_CTX *recp, BN_CTX *ctx);
584 487
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
594int 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)
596int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p); /*r=a mod p*/
597int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
598 const BIGNUM *p, BN_CTX *ctx); /* r = (a * b) mod p */
599int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
600 BN_CTX *ctx); /* r = (a * a) mod p */
601int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *b, const BIGNUM *p,
602 BN_CTX *ctx); /* r = (1 / b) mod p */
603int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
604 const BIGNUM *p, BN_CTX *ctx); /* r = (a / b) mod p */
605int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
606 const BIGNUM *p, BN_CTX *ctx); /* r = (a ^ b) mod p */
607int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
608 BN_CTX *ctx); /* r = sqrt(a) mod p */
609int 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 */
617int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[]);
618 /* r = a mod p */
619int 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 */
621int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[],
622 BN_CTX *ctx); /* r = (a * a) mod p */
623int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *b, const unsigned int p[],
624 BN_CTX *ctx); /* r = (1 / b) mod p */
625int 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 */
627int 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 */
629int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a,
630 const unsigned int p[], BN_CTX *ctx); /* r = sqrt(a) mod p */
631int 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 */
633int BN_GF2m_poly2arr(const BIGNUM *a, unsigned int p[], int max);
634int BN_GF2m_arr2poly(const unsigned int p[], BIGNUM *a);
635
636/* faster mod functions for the 'NIST primes'
637 * 0 <= a < p^2 */
638int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
639int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
640int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
641int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
642int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
643
644const BIGNUM *BN_get0_nist_prime_192(void);
645const BIGNUM *BN_get0_nist_prime_224(void);
646const BIGNUM *BN_get0_nist_prime_256(void);
647const BIGNUM *BN_get0_nist_prime_384(void);
648const BIGNUM *BN_get0_nist_prime_521(void);
649
650/* library internal functions */ 488/* library internal functions */
651 489
652#define bn_expand(a,bits) ((((((bits+BN_BITS2-1))/BN_BITS2)) <= (a)->dmax)?\ 490#define bn_expand(a,bits) ((((((bits+BN_BITS2-1))/BN_BITS2)) <= (a)->dmax)?\
653 (a):bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2)) 491 (a):bn_expand2((a),(bits)/BN_BITS2+1))
654#define bn_wexpand(a,words) (((words) <= (a)->dmax)?(a):bn_expand2((a),(words))) 492#define bn_wexpand(a,words) (((words) <= (a)->dmax)?(a):bn_expand2((a),(words)))
655BIGNUM *bn_expand2(BIGNUM *a, int words); 493BIGNUM *bn_expand2(BIGNUM *a, int words);
656#ifndef OPENSSL_NO_DEPRECATED 494BIGNUM *bn_dup_expand(const BIGNUM *a, int words);
657BIGNUM *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
689 495
690/* We only need assert() when debugging */ 496#define bn_fix_top(a) \
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
696int 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) \
741 { \ 497 { \
742 BN_ULONG *ftl; \ 498 BN_ULONG *ftl; \
743 if ((a)->top > 0) \ 499 if ((a)->top > 0) \
@@ -745,7 +501,6 @@ int RAND_pseudo_bytes(unsigned char *buf,int num);
745 for (ftl= &((a)->d[(a)->top-1]); (a)->top > 0; (a)->top--) \ 501 for (ftl= &((a)->d[(a)->top-1]); (a)->top > 0; (a)->top--) \
746 if (*(ftl--)) break; \ 502 if (*(ftl--)) break; \
747 } \ 503 } \
748 bn_pollute(a); \
749 } 504 }
750 505
751BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w); 506BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
@@ -755,17 +510,15 @@ BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
755BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,int num); 510BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,int num);
756BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,int num); 511BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,int num);
757 512
758/* Primes from RFC 2409 */ 513#ifdef BN_DEBUG
759BIGNUM *get_rfc2409_prime_768(BIGNUM *bn); 514void bn_dump1(FILE *o, const char *a, const BN_ULONG *b,int n);
760BIGNUM *get_rfc2409_prime_1024(BIGNUM *bn); 515# define bn_print(a) {fprintf(stderr, #a "="); BN_print_fp(stderr,a); \
761 516 fprintf(stderr,"\n");}
762/* Primes from RFC 3526 */ 517# define bn_dump(a,n) bn_dump1(stderr,#a,a,n);
763BIGNUM *get_rfc3526_prime_1536(BIGNUM *bn); 518#else
764BIGNUM *get_rfc3526_prime_2048(BIGNUM *bn); 519# define bn_print(a)
765BIGNUM *get_rfc3526_prime_3072(BIGNUM *bn); 520# define bn_dump(a,b)
766BIGNUM *get_rfc3526_prime_4096(BIGNUM *bn); 521#endif
767BIGNUM *get_rfc3526_prime_6144(BIGNUM *bn);
768BIGNUM *get_rfc3526_prime_8192(BIGNUM *bn);
769 522
770int BN_bntest_rand(BIGNUM *rnd, int bits, int top,int bottom); 523int BN_bntest_rand(BIGNUM *rnd, int bits, int top,int bottom);
771 524
@@ -778,30 +531,18 @@ void ERR_load_BN_strings(void);
778/* Error codes for the BN functions. */ 531/* Error codes for the BN functions. */
779 532
780/* Function codes. */ 533/* Function codes. */
781#define BN_F_BNRAND 127 534#define BN_F_BN_BLINDING_CONVERT 100
782#define BN_F_BN_BLINDING_CONVERT_EX 100 535#define BN_F_BN_BLINDING_INVERT 101
783#define BN_F_BN_BLINDING_CREATE_PARAM 128
784#define BN_F_BN_BLINDING_INVERT_EX 101
785#define BN_F_BN_BLINDING_NEW 102 536#define BN_F_BN_BLINDING_NEW 102
786#define BN_F_BN_BLINDING_UPDATE 103 537#define BN_F_BN_BLINDING_UPDATE 103
787#define BN_F_BN_BN2DEC 104 538#define BN_F_BN_BN2DEC 104
788#define BN_F_BN_BN2HEX 105 539#define BN_F_BN_BN2HEX 105
789#define BN_F_BN_CTX_GET 116 540#define BN_F_BN_CTX_GET 116
790#define BN_F_BN_CTX_NEW 106 541#define BN_F_BN_CTX_NEW 106
791#define BN_F_BN_CTX_START 129
792#define BN_F_BN_DIV 107 542#define BN_F_BN_DIV 107
793#define BN_F_BN_DIV_NO_BRANCH 138
794#define BN_F_BN_DIV_RECP 130
795#define BN_F_BN_EXP 123 543#define BN_F_BN_EXP 123
796#define BN_F_BN_EXPAND2 108 544#define BN_F_BN_EXPAND2 108
797#define BN_F_BN_EXPAND_INTERNAL 120 545#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
805#define BN_F_BN_MOD_EXP2_MONT 118 546#define BN_F_BN_MOD_EXP2_MONT 118
806#define BN_F_BN_MOD_EXP_MONT 109 547#define BN_F_BN_MOD_EXP_MONT 109
807#define BN_F_BN_MOD_EXP_MONT_CONSTTIME 124 548#define BN_F_BN_MOD_EXP_MONT_CONSTTIME 124
@@ -809,7 +550,6 @@ void ERR_load_BN_strings(void);
809#define BN_F_BN_MOD_EXP_RECP 125 550#define BN_F_BN_MOD_EXP_RECP 125
810#define BN_F_BN_MOD_EXP_SIMPLE 126 551#define BN_F_BN_MOD_EXP_SIMPLE 126
811#define BN_F_BN_MOD_INVERSE 110 552#define BN_F_BN_MOD_INVERSE 110
812#define BN_F_BN_MOD_INVERSE_NO_BRANCH 139
813#define BN_F_BN_MOD_LSHIFT_QUICK 119 553#define BN_F_BN_MOD_LSHIFT_QUICK 119
814#define BN_F_BN_MOD_MUL_RECIPROCAL 111 554#define BN_F_BN_MOD_MUL_RECIPROCAL 111
815#define BN_F_BN_MOD_SQRT 121 555#define BN_F_BN_MOD_SQRT 121
@@ -833,7 +573,6 @@ void ERR_load_BN_strings(void);
833#define BN_R_NOT_A_SQUARE 111 573#define BN_R_NOT_A_SQUARE 111
834#define BN_R_NOT_INITIALIZED 107 574#define BN_R_NOT_INITIALIZED 107
835#define BN_R_NO_INVERSE 108 575#define BN_R_NO_INVERSE 108
836#define BN_R_NO_SOLUTION 116
837#define BN_R_P_IS_NOT_PRIME 112 576#define BN_R_P_IS_NOT_PRIME 112
838#define BN_R_TOO_MANY_ITERATIONS 113 577#define BN_R_TOO_MANY_ITERATIONS 113
839#define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109 578#define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109