Hide BN_CTX structure details.
[openssl.git] / crypto / bn / bn.h
index 9e3bbf400a13b043e9af523dbdb79be36c54ae90..7e4234339bf5c2407777584a7cc7a4e8838de0e1 100644 (file)
 #ifndef HEADER_BN_H
 #define HEADER_BN_H
 
-#ifndef NO_FP_API
+#include <openssl/e_os2.h>
+#ifndef OPENSSL_NO_FP_API
 #include <stdio.h> /* FILE */
 #endif
-#include <openssl/opensslconf.h>
 
 #ifdef  __cplusplus
 extern "C" {
 #endif
 
-#ifdef VMS
+#ifdef OPENSSL_SYS_VMS
 #undef BN_LLONG /* experimental, so far... */
 #endif
 
@@ -87,7 +87,8 @@ extern "C" {
  * For machines with only one compiler (or shared libraries), this should
  * be on.  Again this in only really a problem on machines
  * using "long long's", are 32bit, and are not using my assembler code. */
-#if defined(MSDOS) || defined(WINDOWS) || defined(WIN32) || defined(linux)
+#if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || \
+    defined(OPENSSL_SYS_WIN32) || defined(linux)
 #undef BN_DIV2W
 #define BN_DIV2W
 #endif
@@ -141,7 +142,7 @@ extern "C" {
 #endif
 
 #ifdef THIRTY_TWO_BIT
-#if defined(WIN32) && !defined(__GNUC__)
+#if defined(OPENSSL_SYS_WIN32) && !defined(__GNUC__)
 #define BN_ULLONG      unsigned _int64
 #else
 #define BN_ULLONG      unsigned long long
@@ -152,7 +153,7 @@ extern "C" {
 #define BN_BYTES       4
 #define BN_BITS2       32
 #define BN_BITS4       16
-#ifdef WIN32
+#ifdef OPENSSL_SYS_WIN32
 /* VC++ doesn't like the LL suffix */
 #define BN_MASK                (0xffffffffffffffffL)
 #else
@@ -237,18 +238,8 @@ typedef struct bignum_st
        int flags;
        } BIGNUM;
 
-/* Used for temp variables */
-#define BN_CTX_NUM     16
-#define BN_CTX_NUM_POS 12
-typedef struct bignum_ctx
-       {
-       int tos;
-       BIGNUM bn[BN_CTX_NUM];
-       int flags;
-       int depth;
-       int pos[BN_CTX_NUM_POS];
-       int too_many;
-       } BN_CTX;
+/* Used for temp variables (declaration hidden in bn_lcl.h) */
+typedef struct bignum_ctx BN_CTX;
 
 typedef struct bn_blinding_st
        {
@@ -304,17 +295,22 @@ typedef struct bn_recp_ctx_st
                                 /* b >= 100 */ 27)
 
 #define BN_num_bytes(a)        ((BN_num_bits(a)+7)/8)
-#define BN_is_word(a,w)        (((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w)))
-#define BN_is_zero(a)  (((a)->top == 0) || BN_is_word(a,0))
-#define BN_is_one(a)   (BN_is_word((a),1))
-#define BN_is_odd(a)   (((a)->top > 0) && ((a)->d[0] & 1))
+
+/* Note that BN_abs_is_word does not work reliably for w == 0 */
+#define BN_abs_is_word(a,w) (((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w)))
+#define BN_is_zero(a)       (((a)->top == 0) || BN_abs_is_word(a,0))
+#define BN_is_one(a)        (BN_abs_is_word((a),1) && !(a)->neg)
+#define BN_is_word(a,w)     ((w) ? BN_abs_is_word((a),(w)) && !(a)->neg : \
+                                   BN_is_zero((a)))
+#define BN_is_odd(a)       (((a)->top > 0) && ((a)->d[0] & 1))
+
 #define BN_one(a)      (BN_set_word((a),1))
 #define BN_zero(a)     (BN_set_word((a),0))
 
 /*#define BN_ascii2bn(a)       BN_hex2bn(a) */
 /*#define BN_bn2ascii(a)       BN_bn2hex(a) */
 
-BIGNUM *BN_value_one(void);
+const BIGNUM *BN_value_one(void);
 char * BN_options(void);
 BN_CTX *BN_CTX_new(void);
 void   BN_CTX_init(BN_CTX *c);
@@ -324,6 +320,7 @@ BIGNUM *BN_CTX_get(BN_CTX *ctx);
 void   BN_CTX_end(BN_CTX *ctx);
 int     BN_rand(BIGNUM *rnd, int bits, int top,int bottom);
 int     BN_pseudo_rand(BIGNUM *rnd, int bits, int top,int bottom);
+int    BN_rand_range(BIGNUM *rnd, BIGNUM *range);
 int    BN_num_bits(const BIGNUM *a);
 int    BN_num_bits_word(BN_ULONG);
 BIGNUM *BN_new(void);
@@ -352,6 +349,7 @@ int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_
 int    BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m);
 int    BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
        const BIGNUM *m, BN_CTX *ctx);
+int    BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
 int    BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
 int    BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m);
 int    BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m, BN_CTX *ctx);
@@ -385,7 +383,7 @@ int BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
        const BIGNUM *m,BN_CTX *ctx);
 
 int    BN_mask_bits(BIGNUM *a,int n);
-#ifndef NO_FP_API
+#ifndef OPENSSL_NO_FP_API
 int    BN_print_fp(FILE *fp, const BIGNUM *a);
 #endif
 #ifdef HEADER_BIO_H
@@ -405,9 +403,12 @@ char *     BN_bn2hex(const BIGNUM *a);
 char * BN_bn2dec(const BIGNUM *a);
 int    BN_hex2bn(BIGNUM **a, const char *str);
 int    BN_dec2bn(BIGNUM **a, const char *str);
-int    BN_gcd(BIGNUM *r,const BIGNUM *in_a,const BIGNUM *in_b,BN_CTX *ctx);
+int    BN_gcd(BIGNUM *r,const BIGNUM *a,const BIGNUM *b,BN_CTX *ctx);
+int    BN_kronecker(const BIGNUM *a,const BIGNUM *b,BN_CTX *ctx); /* returns -2 for error */
 BIGNUM *BN_mod_inverse(BIGNUM *ret,
        const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx);
+BIGNUM *BN_mod_sqrt(BIGNUM *ret,
+       const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx);
 BIGNUM *BN_generate_prime(BIGNUM *ret,int bits,int safe,
        const BIGNUM *add, const BIGNUM *rem,
        void (*callback)(int,int,void *),void *cb_arg);
@@ -486,6 +487,8 @@ void bn_dump1(FILE *o, const char *a, const BN_ULONG *b,int n);
 # define bn_dump(a,b)
 #endif
 
+int BN_bntest_rand(BIGNUM *rnd, int bits, int top,int bottom);
+
 /* BEGIN ERROR CODES */
 /* The following lines are auto generated by the script mkerr.pl. Any changes
  * made after this point may be overwritten when the script is next run.
@@ -504,32 +507,39 @@ void bn_dump1(FILE *o, const char *a, const BN_ULONG *b,int n);
 #define BN_F_BN_CTX_NEW                                         106
 #define BN_F_BN_DIV                                     107
 #define BN_F_BN_EXPAND2                                         108
+#define BN_F_BN_EXPAND_INTERNAL                                 120
 #define BN_F_BN_MOD_EXP2_MONT                           118
 #define BN_F_BN_MOD_EXP_MONT                            109
 #define BN_F_BN_MOD_EXP_MONT_WORD                       117
 #define BN_F_BN_MOD_INVERSE                             110
 #define BN_F_BN_MOD_LSHIFT_QUICK                        119
 #define BN_F_BN_MOD_MUL_RECIPROCAL                      111
+#define BN_F_BN_MOD_SQRT                                121
 #define BN_F_BN_MPI2BN                                  112
 #define BN_F_BN_NEW                                     113
 #define BN_F_BN_RAND                                    114
+#define BN_F_BN_RAND_RANGE                              122
 #define BN_F_BN_USUB                                    115
 
 /* Reason codes. */
 #define BN_R_ARG2_LT_ARG3                               100
 #define BN_R_BAD_RECIPROCAL                             101
+#define BN_R_BIGNUM_TOO_LONG                            114
 #define BN_R_CALLED_WITH_EVEN_MODULUS                   102
 #define BN_R_DIV_BY_ZERO                                103
 #define BN_R_ENCODING_ERROR                             104
 #define BN_R_EXPAND_ON_STATIC_BIGNUM_DATA               105
 #define BN_R_INPUT_NOT_REDUCED                          110
 #define BN_R_INVALID_LENGTH                             106
+#define BN_R_INVALID_RANGE                              115
+#define BN_R_NOT_A_SQUARE                               111
 #define BN_R_NOT_INITIALIZED                            107
 #define BN_R_NO_INVERSE                                         108
+#define BN_R_P_IS_NOT_PRIME                             112
+#define BN_R_TOO_MANY_ITERATIONS                        113
 #define BN_R_TOO_MANY_TEMPORARY_VARIABLES               109
 
 #ifdef  __cplusplus
 }
 #endif
 #endif
-