博客專欄

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

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

        發布人:電子禪石 時間:2023-09-05 來源:工程師 發布文章

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

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

        BIGNUM的創建與釋放
        函數原型解釋示例
        BIGNUM * BN_new (void);創建一個BIGNUM的結構,返回新BIGNUM結構的指針BIGNUM *a = BN_new ();
        void BN_free (BIGNUM *);釋放一個BIGNUMfree (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是不是為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是不是一個奇數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為1BN_one (a);
        int BN_zero(BIGNUM *a);設置a為0BN_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中,返回retchar 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;否則,返回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);計算a與b的差,值儲存在r中。返回1或者0BN_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或者0BN_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或者0BN_div (dv, r, a, b);
        int BN_mod(BIGNUM *rem, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);計算a與m的模,值儲存在rem中。返回1或者0BN_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或者0BN_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或者0BN_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或者0BN_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或者0BN_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或者0BN_mod_sqr (r, a, m, ctx);
        int BN_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BN_CTX *ctx);計算a的p次方,值儲存在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);計算a的p次方,再模m,值儲存在r中。返回1或者0BN_mod_exp (r, a, p, m, ctx);
        int BN_gcd(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);計算a與b的最大公約數,值儲存在r中。返回1或者0BN_gcd (r, a, b, ctx);
        int BN_add_word(BIGNUM *a, BN_ULONG w);大數a加上w,值儲存在a中,返回1或者0BN_dec2bn (a, "1234");
        BN_add_word (a, 1);
        int BN_sub_word(BIGNUM *a, BN_ULONG w);大數a減去w,值儲存在a中,返回1或者0BN_sub_word (a, 23);
        int BN_mul_word(BIGNUM *a, BN_ULONG w);大數a乘以w,值儲存在a中,返回1或者0BN_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);

        轉自:http://blogold.chinaunix.net/u/19628/showart_2081074.html


        *博客內容為網友個人發布,僅代表博主個人觀點,如有侵權請聯系工作人員刪除。



        關鍵詞: openssl

        技術專區

        關閉
        主站蜘蛛池模板: 九台市| 安顺市| 聂荣县| 肃南| 七台河市| 宁城县| 临夏县| 南木林县| 冷水江市| 枞阳县| 驻马店市| 汾西县| 竹山县| 且末县| 奉化市| 深水埗区| 成都市| 永济市| 平湖市| 夏津县| 泸溪县| 阿图什市| 海淀区| 岫岩| 兰州市| 天水市| 湘西| 绵阳市| 江达县| 谷城县| 建瓯市| 太和县| 五大连池市| 扶沟县| 滨海县| 宁陕县| 乌拉特中旗| 思茅市| 泰和县| 芷江| 射阳县|