博客專(zhuān)欄

EEPW首頁(yè) > 博客 > SM2算法第二十七篇: openssl庫(kù)中的BIGNUM(超贊)

SM2算法第二十七篇: openssl庫(kù)中的BIGNUM(超贊)

發(fā)布人:電子禪石 時(shí)間:2023-09-05 來(lái)源:工程師 發(fā)布文章

openssl庫(kù)中的BIGNUM,處理大數(shù)比較好,可以用在很多方面。

BIGNUM是一個(gè)typedef的結(jié)構(gòu),可以直接使用。但一般來(lái)說(shuō),使用它的指針結(jié)構(gòu)。如:BIGNUM *p;

BIGNUM的創(chuàng)建與釋放
函數(shù)原型解釋示例
BIGNUM * BN_new (void);創(chuàng)建一個(gè)BIGNUM的結(jié)構(gòu),返回新BIGNUM結(jié)構(gòu)的指針BIGNUM *a = BN_new ();
void BN_free (BIGNUM *);釋放一個(gè)BIGNUMfree (a);
BIGNUM的值測(cè)試
函數(shù)原型解釋示例
int BN_cmp (BIGNUM *a, BIGNUM *b);| 判斷a與b是否相等if (BN_cmp (a, b) { printf ("a equ b/n"); }
<verbatim>int BN_ucmp (BIGNUM *a, BIGNUM *b);判斷a與b的絕對(duì)值是否相等if (BN_ucmp (a, b)
int BN_is_zero(BIGNUM *a);判斷a是不是為0if (BN_is_zero (a))
int BN_is_one(BIGNUM *a);判斷a是不是1if (BN_is_one (a))
int BN_is_word(BIGNUM *a, BN_ULONG w);判斷a是不是值wif (BN_is_word (a, 12))
int BN_is_odd(BIGNUM *a);判斷a是不是一個(gè)奇數(shù)if (BN_is_odd (a))
BIGNUM的賦值與取值
函數(shù)原型解釋示例
int BN_num_bytes(BIGNUM *a);返回a的字節(jié)數(shù)printf ("length: %d/n", BN_num_tytes (a));
int BN_num_bits(BIGNUM *a);返回a的二進(jìn)制位數(shù)printf ("bits: %d/n", BN_num_bits (a));
int BN_one(BIGNUM *a);設(shè)置a為1BN_one (a);
int BN_zero(BIGNUM *a);設(shè)置a為0BN_zero (a);
int BN_bn2bin(const BIGNUM *a, unsigned char *to);取a為二進(jìn)制到to中,返回字符串長(zhǎng)度char s[1024];
int length = BN_bn2bin (a, s);
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret);賦二進(jìn)制值s到ret中,返回retchar s[] = "1001001";
BN_bin2bn (s, strlen (s), a);
char *BN_bn2hex(const BIGNUM *a);取a的16進(jìn)制值,返回一個(gè)字符串的指針。此指針要使用完后,手動(dòng)使用OPENSSL_free釋放char *p = BN_bn2hex (a); 
if (p)
{
printf ("number is 0x%s/n", p);
OPENSSL_free (p);
}
char *BN_bn2dec(const BIGNUM *a);取a的10進(jìn)制值,返回一個(gè)字符串的指針。此指針要使用完后,手動(dòng)使用OPENSSL_free釋放p = BN_bn2dec (a);
int BN_hex2bn(BIGNUM **a, const char *str);賦16進(jìn)制值str到*a中,返回成功與否BN_hex2bn (&a, "0x123F23D12");
int BN_dec2bn(BIGNUM **a, const char *str);賦10進(jìn)制值str到*a中,返回成功與否BN_dec2bn (&a, "1999");
BIGNUM的計(jì)算
函數(shù)原型解釋示例
int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);計(jì)算a與b的和,值儲(chǔ)存在r中。如果成功,返回1;否則,返回0BIGNUM *a = BN_new (), *b = BN_new (), *r = BN_new ();
BN_dec2bn (&a, "1234");
BN_dec2bn (&b, "2345");
BN_add (r, a, b);
int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);計(jì)算a與b的差,值儲(chǔ)存在r中。返回1或者0BN_sum (r, a, b);
int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);計(jì)算a與b的積,值儲(chǔ)存在r中。返回1或者0。ctx為一個(gè)上下文相關(guān)的結(jié)構(gòu),可以用BN_CTX_new與BN_CTX_free來(lái)創(chuàng)建與釋放它BN_CTX *ctx = BN_CTX_new ();
BN_mul (r, a, b, ctx);
BN_CTX_free (ctx);
int BN_sqr(BIGNUM *r, BIGNUM *a, BN_CTX *ctx);計(jì)算a的平方,值儲(chǔ)存在r中。返回1或者0BN_sqr (r, a, ctx);
int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *a, const BIGNUM *d, BN_CTX *ctx);計(jì)算a與d的商,值儲(chǔ)存在dv中,余數(shù)儲(chǔ)存在rem中。返回1或者0BN_div (dv, r, a, b);
int BN_mod(BIGNUM *rem, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);計(jì)算a與m的模,值儲(chǔ)存在rem中。返回1或者0BN_mod (r, a, m, ctx);
int BN_nnmod(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);計(jì)算a與m的模,并且結(jié)果如果小于0,就加上m,值儲(chǔ)存在r中。返回1或者0BN_nnmod (r, a, m, ctx);
int BN_mod_add(BIGNUM *r, BIGNUM *a, BIGNUM *b, const BIGNUM *m, BN_CTX *ctx);計(jì)算a與b的和,再模m,值儲(chǔ)存在r中。返回1或者0BN_mod_add (r, a, b, m, ctx);
int BN_mod_sub(BIGNUM *r, BIGNUM *a, BIGNUM *b, const BIGNUM *m, BN_CTX *ctx);計(jì)算a與b的差,再模m,值儲(chǔ)存在r中。返回1或者0BN_mod_sub (r, a, b, m, ctx);
int BN_mod_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, const BIGNUM *m, BN_CTX *ctx);計(jì)算a與b的積,再模m,值儲(chǔ)存在r中。返回1或者0BN_mod_mul (r, a, b, m, ctx);
int BN_mod_sqr(BIGNUM *r, BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);計(jì)算a的平方根,再模m,值儲(chǔ)存在r中。返回1或者0BN_mod_sqr (r, a, m, ctx);
int BN_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BN_CTX *ctx);計(jì)算a的p次方,值儲(chǔ)存在r中。返回1或者0BN_exp (r, a, p, ctx);
int BN_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx);計(jì)算a的p次方,再模m,值儲(chǔ)存在r中。返回1或者0BN_mod_exp (r, a, p, m, ctx);
int BN_gcd(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);計(jì)算a與b的最大公約數(shù),值儲(chǔ)存在r中。返回1或者0BN_gcd (r, a, b, ctx);
int BN_add_word(BIGNUM *a, BN_ULONG w);大數(shù)a加上w,值儲(chǔ)存在a中,返回1或者0BN_dec2bn (a, "1234");
BN_add_word (a, 1);
int BN_sub_word(BIGNUM *a, BN_ULONG w);大數(shù)a減去w,值儲(chǔ)存在a中,返回1或者0BN_sub_word (a, 23);
int BN_mul_word(BIGNUM *a, BN_ULONG w);大數(shù)a乘以w,值儲(chǔ)存在a中,返回1或者0BN_mul_word (a, 2);
BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w);大數(shù)a除以w,值儲(chǔ)存在a中,返回余數(shù)BN_ULONG ru = BN_div_word (a, 256);
BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w);大數(shù)a模w,返回余數(shù)ru = BN_mod_word (a, 256);

轉(zhuǎn)自:http://blogold.chinaunix.net/u/19628/showart_2081074.html


*博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請(qǐng)聯(lián)系工作人員刪除。



關(guān)鍵詞: openssl

技術(shù)專(zhuān)區(qū)

關(guān)閉