1. 程式人生 > >SM2演算法第二十七篇: openssl庫中的BIGNUM(超讚)

SM2演算法第二十七篇: openssl庫中的BIGNUM(超讚)

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

BIGNUM是一個typedef的結構,可以直接使用。但一般來說,使用它的指標結構。如:BIGNUM *p;

BIGNUM的建立與釋放

函式原型 解釋 示例
BIGNUM * BN_new (void); 建立一個BIGNUM的結構,返回新BIGNUM結構的指標 BIGNUM *a = BN_new ();
void BN_free (BIGNUM *); 釋放一個BIGNUM free (a);

BIGNUM的值測試

函式原型 解釋 示例
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的絕對值是否相等 if (BN_ucmp (a, b)
int BN_is_zero(BIGNUM *a); 判斷a是不是為0 if (BN_is_zero (a))
int BN_is_one(BIGNUM *a); 判斷a是不是1 if (BN_is_one (a))
int BN_is_word(BIGNUM *a, BN_ULONG w); 判斷a是不是值w if (BN_is_word (a, 12))
int BN_is_odd(BIGNUM *a); 判斷a是不是一個奇數 if (BN_is_odd (a))

BIGNUM的賦值與取值

函式原型 解釋 示例
int BN_num_bytes(BIGNUM *a); 返回a的位元組數 printf ("length: %d/n", BN_num_tytes (a));
int BN_num_bits(BIGNUM *a); 返回a的二進位制位數 printf ("bits: %d/n", BN_num_bits (a));
int BN_one(BIGNUM *a); 設定a為1 BN_one (a);
int BN_zero(BIGNUM *a); 設定a為0 BN_zero (a);
int BN_bn2bin(const BIGNUM *a, unsigned char *to); 取a為二進位制到to中,返回字串長度 char s[1024];
int length = BN_bn2bin (a, s);
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret); 賦二進位制值s到ret中,返回ret char s[] = "1001001";
BN_bin2bn (s, strlen (s), a);
char *BN_bn2hex(const BIGNUM *a); 取a的16進位制值,返回一個字串的指標。此指標要使用完後,手動使用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進位制值,返回一個字串的指標。此指標要使用完後,手動使用OPENSSL_free釋放 p = BN_bn2dec (a);
int BN_hex2bn(BIGNUM **a, const char *str); 賦16進位制值str到*a中,返回成功與否 BN_hex2bn (&a, "0x123F23D12");
int BN_dec2bn(BIGNUM **a, const char *str); 賦10進位制值str到*a中,返回成功與否 BN_dec2bn (&a, "1999");

BIGNUM的計算

函式原型 解釋 示例
int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 計算a與b的和,值儲存在r中。如果成功,返回1;否則,返回0 BIGNUM *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); 計算a與b的差,值儲存在r中。返回1或者0 BN_sum (r, a, b);
int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx); 計算a與b的積,值儲存在r中。返回1或者0。ctx為一個上下文相關的結構,可以用BN_CTX_new與BN_CTX_free來建立與釋放它 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); 計算a的平方,值儲存在r中。返回1或者0 BN_sqr (r, a, ctx);
int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *a, const BIGNUM *d, BN_CTX *ctx); 計算a與d的商,值儲存在dv中,餘數儲存在rem中。返回1或者0 BN_div (dv, r, a, b);
int BN_mod(BIGNUM *rem, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); 計算a與m的模,值儲存在rem中。返回1或者0 BN_mod (r, a, m, ctx);
int BN_nnmod(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); 計算a與m的模,並且結果如果小於0,就加上m,值儲存在r中。返回1或者0 BN_nnmod (r, a, m, ctx);
int BN_mod_add(BIGNUM *r, BIGNUM *a, BIGNUM *b, const BIGNUM *m, BN_CTX *ctx); 計算a與b的和,再模m,值儲存在r中。返回1或者0 BN_mod_add (r, a, b, m, ctx);
int BN_mod_sub(BIGNUM *r, BIGNUM *a, BIGNUM *b, const BIGNUM *m, BN_CTX *ctx); 計算a與b的差,再模m,值儲存在r中。返回1或者0 BN_mod_sub (r, a, b, m, ctx);
int BN_mod_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, const BIGNUM *m, BN_CTX *ctx); 計算a與b的積,再模m,值儲存在r中。返回1或者0 BN_mod_mul (r, a, b, m, ctx);
int BN_mod_sqr(BIGNUM *r, BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); 計算a的平方根,再模m,值儲存在r中。返回1或者0 BN_mod_sqr (r, a, m, ctx);
int BN_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BN_CTX *ctx); 計算a的p次方,值儲存在r中。返回1或者0 BN_exp (r, a, p, ctx);
int BN_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx); 計算a的p次方,再模m,值儲存在r中。返回1或者0 BN_mod_exp (r, a, p, m, ctx);
int BN_gcd(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx); 計算a與b的最大公約數,值儲存在r中。返回1或者0 BN_gcd (r, a, b, ctx);
int BN_add_word(BIGNUM *a, BN_ULONG w); 大數a加上w,值儲存在a中,返回1或者0 BN_dec2bn (a, "1234");
BN_add_word (a, 1);
int BN_sub_word(BIGNUM *a, BN_ULONG w); 大數a減去w,值儲存在a中,返回1或者0 BN_sub_word (a, 23);
int BN_mul_word(BIGNUM *a, BN_ULONG w); 大數a乘以w,值儲存在a中,返回1或者0 BN_mul_word (a, 2);
BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w); 大數a除以w,值儲存在a中,返回餘數 BN_ULONG ru = BN_div_word (a, 256);
BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w); 大數a模w,返回餘數 ru = BN_mod_word (a, 256);