Openssl 之大数运算函数 BN
Openssl 之大數(shù)運算函數(shù) BN
主要介紹Openssl中的有關(guān)大數(shù)運算函數(shù),這個對于RSA研究和實現(xiàn)比較有價值
?
1.初始化函數(shù)
?
BIGNUM *BN_new(void);??? 新生成一個BIGNUM結(jié)構(gòu)?
?
void BN_free(BIGNUM *a);?? 釋放一個BIGNUM結(jié)構(gòu),釋放完后a=NULL;?
?
void BN_init(BIGNUM *);??? 初始化所有項均為0,一般為BN_ init(&c)?
?
void BN_clear(BIGNUM *a);? 將a中所有項均賦值為0,但是內(nèi)存并沒有釋放?
?
void BN_clear_free(BIGNUM *a); 相當與將BN_free和BN_clear綜合,要不就賦值0,要不就釋放空間。?
?
2.上下文情景函數(shù),存儲計算中的中間過程
BN_CTX *BN_CTX_new(void);申請一個新的上下文結(jié)構(gòu)?
?
void BN_CTX_init(BN_CTX *c);將所有的項賦值為0,一般BN_CTX_init(&c)?
?
? void BN_CTX_free(BN_CTX *c);釋放上下文結(jié)構(gòu),釋放完后c=NULL;
?
3.復制以及交換函數(shù)
? BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b);將b復制給a,正確返回a,錯誤返回NULL
?
? BIGNUM *BN_dup(const BIGNUM *a);新建一個BIGNUM結(jié)構(gòu),將a復制給新建結(jié)構(gòu)返回,錯誤返回NULL
?
? BIGNUM *BN_swap(BIGNUM *a, BIGNUM *b);交換a,b
?
4.取位函數(shù)
?int BN_num_bytes(const BIGNUM *a);返回a的位數(shù),大量使用
?
?int BN_num_bits(const BIGNUM *a);
?
?int BN_num_bits_word(BN_ULONG w);他返回有意義比特的位數(shù),例如0x00000432 為11。
?
5.基本計算函數(shù)
?
?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,效率高于bn_mul(r,a,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最大公約數(shù)
?
?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);模逆,((a*r)%n==1).
?
?
?
6.比較函數(shù)
?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);? 比較a,b覺得值,返回值和上同。
?
?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);??????? 上面四個返回1,假如條件成立,否則將返回0
?
7.設置函數(shù)
?int BN_zero(BIGNUM *a);? 設置a為0
?
?int BN_one(BIGNUM *a);?? 設置a為1
?
?const BIGNUM *BN_value_one(void); 返回一個為1的大數(shù)
?
?int BN_set_word(BIGNUM *a, unsigned long w); 設置a為w
?
?unsigned long BN_get_word(BIGNUM *a); 假如a能表示為long型,那么返回一個long型數(shù)
?
8.隨機數(shù)函數(shù)
?int BN_rand(BIGNUM *rnd, int bits, int top, int bottom);產(chǎn)生一個加密用的強bits的偽隨機數(shù),若top=-1,最高位為0,top=0,最高位為1,top=1,最高位和次高位為1,bottom為真,隨機數(shù)為偶數(shù)?
?
?int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom);產(chǎn)生一個偽隨機數(shù),應用于某些目的。
?
int BN_rand_range(BIGNUM *rnd, BIGNUM *range);產(chǎn)生的0<rnd<range
?
?int BN_pseudo_rand_range(BIGNUM *rnd, BIGNUM *range);同上面道理
?
9.產(chǎn)生素數(shù)函數(shù)
BIGNUM *BN_generate_prime(BIGNUM *ret, int bits,int safe, BIGNUM *add,
?
???????? BIGNUM *rem, void (*callback)(int, int, void *), void *cb_arg);產(chǎn)生一個bits位的素數(shù),后面幾個參數(shù)都可以為NULL
?
?int BN_is_prime(const BIGNUM *p, int nchecks,
?
???????? void (*callback)(int, int, void *), BN_CTX *ctx, void *cb_arg);
?
判斷是否為素數(shù),返回0表示成功,1表示錯誤概率小于0。25,-1表示錯誤
?
10.位數(shù)函數(shù)
?int BN_set_bit(BIGNUM *a, int n);將a中的第n位設置為1,假如a小于n位將擴展
?
?int BN_clear_bit(BIGNUM *a, int n);將a中的第n為設置為0,假如a小于n位將出錯
?
?int BN_is_bit_set(const BIGNUM *a, int n);測試是否已經(jīng)設置,1表示已設置
?
?int BN_mask_bits(BIGNUM *a, int n);將a截斷至n位,假如a小于n位將出錯
?
?int BN_lshift(BIGNUM *r, const BIGNUM *a, int n);a左移n位,結(jié)果存于r
?
?int BN_lshift1(BIGNUM *r, BIGNUM *a); a左移1位,結(jié)果存于r
?
?int BN_rshift(BIGNUM *r, BIGNUM *a, int n); a右移n位,結(jié)果存于r
?
?int BN_rshift1(BIGNUM *r, BIGNUM *a); a左移1位,結(jié)果存于r
?
11.與字符串的轉(zhuǎn)換函數(shù)
int BN_bn2bin(const BIGNUM *a, unsigned char *to);將abs(a)轉(zhuǎn)化為字符串存入to,to的空間必須大于BN_num_bytes(a)
?
?BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret);將s中的len位的正整數(shù)轉(zhuǎn)化為大數(shù)
?
?char *BN_bn2hex(const BIGNUM *a);轉(zhuǎn)化為16進制字符串
?
?char *BN_bn2dec(const BIGNUM *a);轉(zhuǎn)化為10進制字符串
?
?int BN_hex2bn(BIGNUM **a, const char *str);同上理
?
?int BN_dec2bn(BIGNUM **a, const char *str);同上理
?
?int BN_print(BIO *fp, const BIGNUM *a);將大數(shù)16進制形式寫入內(nèi)存中
?
?int BN_print_fp(FILE *fp, const BIGNUM *a); 將大數(shù)16進制形式寫入文件
?
?int BN_bn2mpi(const BIGNUM *a, unsigned char *to);
?
?BIGNUM *BN_mpi2bn(unsigned char *s, int len, BIGNUM *ret);
?
12.其他函數(shù)
下面函數(shù)可以進行更有效率的模乘和模除,假如在重復在同一模下重復進行模乘和模除計算,計算r=(a*b)%m 利用了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);
?
下面函數(shù)采用蒙哥馬利算法進行模冪計算,可以提高效率,他也主要應用于在同一模下進行多次冪運算
?
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);
轉(zhuǎn)載于:https://www.cnblogs.com/yestreen/p/3205804.html
總結(jié)
以上是生活随笔為你收集整理的Openssl 之大数运算函数 BN的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4. XHTML语法
- 下一篇: php转义和去掉html、php标签函数