Introduction to OpenSSL-related big data computing functions
1. initialize the Function
Bignum * bn_new (void); generates a bignum structure.
Void bn_free (bignum * A); release a bignum structure. After the release, a = NULL;
Void bn_init (bignum *); Initialization of all items is 0, generally bn _ Init (& C)
Void bn_clear (bignum * A); all items in a are assigned 0, but the memory is not released.
Void bn_clear_free (bignum * A); It is equivalent to combining bn_free and bn_clear. Do not assign 0 values, or release space.
2. Context context functions are used to store intermediate processes in computing.
Bn_ctx * bn_ctx_new (void); apply for a new context structure
Void bn_ctx_init (bn_ctx * C); assign all items to 0. Generally, bn_ctx_init (& C)
Void bn_ctx_free (bn_ctx * C); release the context structure. After the release, c = NULL;
3. Copy and exchange functions
Bignum * bn_copy (bignum * a, const bignum * B); copy B to A, return a correctly, and return NULL if an error occurs.
Bignum * bn_dup (const bignum * A); creates a bignum structure, copies a to the new structure, and returns a null error.
Bignum * bn_swap (bignum * a, bignum * B); exchange a, B
4. bitwise Functions
Int bn_num_bytes (const bignum * A); returns the number of digits of A, which is widely used.
int BN_num_bits(const BIGNUM *a);
Int bn_num_bits_word (bn_ulong W); it returns the number of meaningful bits, for example, 0x00000432 is 11.
5. basic computing functions
int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);r=a+b
int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);r=a-b
int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);r=a*b
Int bn_sqr (bignum * r, bignum * a, bn_ctx * CTX); r = A * a, the efficiency is higher than bn_mul (R, A,)
int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *a, const BIGNUM *d,
BN_CTX *ctx);d=a/b,r=a%b
int BN_mod(BIGNUM *rem, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);r=a%b
int BN_nnmod(BIGNUM *rem, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);r=abs(a%b)
int BN_mod_add(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m,
BN_CTX *ctx);r=abs((a+b)%m))
int BN_mod_sub(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m,
BN_CTX *ctx); r=abs((a-b)%m))
int BN_mod_mul(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m,
BN_CTX *ctx); r=abs((a*b)%m))
int BN_mod_sqr(BIGNUM *ret, BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); r=abs((a*a)%m))
int BN_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BN_CTX *ctx);r=pow(a,p)
int BN_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
const BIGNUM *m, BN_CTX *ctx); r=pow(a,p)%M
Int bn_gcd (bignum * r, bignum * a, bignum * B, bn_ctx * CTX); r = A, B max common approx.
int BN_add_word(BIGNUM *a, BN_ULONG w);
int BN_sub_word(BIGNUM *a, BN_ULONG w);
int BN_mul_word(BIGNUM *a, BN_ULONG w);
BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w);
BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w);
BIGNUM *BN_mod_inverse(BIGNUM *r, BIGNUM *a, const BIGNUM *n,
Bn_ctx * CTX); modulo inverse, (A * r) % N = 1 ).
6. comparison functions
int BN_cmp(BIGNUM *a, BIGNUM *b); -1 if a < b, 0 if a == b and 1 if a > b.
Int bn_ucmp (bignum * a, bignum * B); compare the values of A and B, and the returned values are the same as those of the above.
int BN_is_zero(BIGNUM *a);
int BN_is_one(BIGNUM *a);
int BN_is_word(BIGNUM *a, BN_ULONG w);
Int bn_is_odd (bignum * A); the preceding four return 1. If the condition is true, 0 is returned.
7. Set Functions
Int bn_zero (bignum * A); set a to 0
Int bn_one (bignum * A); set a to 1
Const bignum * bn_value_one (void); returns a large value of 1.
Int bn_set_word (bignum * a, unsigned long w); set a to W
Unsigned long bn_get_word (bignum * A); If a can be expressed as long, a long number is returned.
8. Random Number Function
Int bn_rand (bignum * RND, int bits, int top, int bottom); generate a pseudo random number of strong bits for encryption. If Top =-1, the maximum bit is 0, top = 0, the highest bit is 1,Top= 1, the highest bit and secondary high are 1, bottom is true, and the random number is an even number
Int bn_pseudo do_rand (bignum * RND, int bits, int top, int bottom); generates a pseudo-random number for some purposes.
Int bn_rand_range (bignum * RND, bignum * Range); 0 <RND <range
Int bn_pseudo do_rand_range (bignum * RND, bignum * Range); Same as above
9. Generate the prime number Function
BIGNUM *BN_generate_prime(BIGNUM *ret, int bits,int safe, BIGNUM *add,
Bignum * REM, void (* callback) (INT, Int, void *), void * cb_arg); generate a bits-bit prime number. All the following parameters can be null.
int BN_is_prime(const BIGNUM *p, int nchecks,
void (*callback)(int, int, void *), BN_CTX *ctx, void *cb_arg);
Returns 0 to determine if it is a prime number. 1 to indicate that the error probability is less than 0. 25,-1 indicates an error
10. Number of digits Function
Int bn_set_bit (bignum * a, int N); set the nth bit in a to 1. If a is less than N bits
Int bn_clear_bit (bignum * a, int N); Set nth in a to 0. If a is less than N bits, an error occurs.
Int bn_is_bit_set (const bignum * a, int N); test whether it has been set; 1 indicates that it has been set
Int bn_mask_bits (bignum * a, int N); truncates A to N bits. If a is less than N bits, an error occurs.
Int bn_lshift (bignum * r, const bignum * a, int N); A shifts left n places, and the result is stored in R
Int bn_lshift1 (bignum * r, bignum * A); A shifts 1 bit left, and the result is stored in R.
Int bn_rshift (bignum * r, bignum * a, int N); A shifts n places to the right, and the result is stored in R.
Int bn_rshift1 (bignum * r, bignum * A); A shifts 1 bit left, and the result is stored in R.
11. conversion functions with strings
Int bn_bn2bin (const bignum * a, unsigned char * to); Convert ABS (a) to a string and store it to. The space to must be greater than bn_num_bytes (A)
Bignum * bn_bin2bn (const unsigned char * s, int Len, bignum * RET); converts a positive integer of the Len bit in s to a large number.
Char * bn_bn2hex (const bignum * A); convert to hexadecimal string
Char * bn_bn2dec (const bignum * A); convert to a 10-digit string
Int bn_hex2bn (bignum ** A, const char * Str); Same as above
Int bn_dec2bn (bignum ** A, const char * Str); Same as above
Int bn_print (Bio * FP, const bignum * A); write large numbers in hexadecimal format into memory
Int bn_print_fp (File * FP, const bignum * A); write large numbers into files in hexadecimal format
int BN_bn2mpi(const BIGNUM *a, unsigned char *to);
BIGNUM *BN_mpi2bn(unsigned char *s, int len, BIGNUM *ret);
12. Other functions
The following functions can perform more efficient Modulo Multiplication and modulo division. If we duplicate the Modulo Multiplication and modulo division computing in the same modulo, calculate R = (A * B) % m used RECP = 1/m
BN_RECP_CTX *BN_RECP_CTX_new(void);
void BN_RECP_CTX_init(BN_RECP_CTX *recp);
void BN_RECP_CTX_free(BN_RECP_CTX *recp);
int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *m, BN_CTX *ctx);
int BN_mod_mul_reciprocal(BIGNUM *r, BIGNUM *a, BIGNUM *b,
BN_RECP_CTX *recp, BN_CTX *ctx);
The following functions use the Montgomery Algorithm for modulo power calculation, which can improve efficiency. They are also mainly used for multiple power operations under the same model.
BN_MONT_CTX *BN_MONT_CTX_new(void);
void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
void BN_MONT_CTX_free(BN_MONT_CTX *mont);
int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *m, BN_CTX *ctx);
BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from);
int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b,
BN_MONT_CTX *mont, BN_CTX *ctx);
int BN_from_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont,
BN_CTX *ctx);
int BN_to_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont,
BN_CTX *ctx);