【问题标题】:How to write a generic function template for openssl bignum?如何为 openssl bignum 编写通用函数模板?
【发布时间】:2018-04-26 04:22:03
【问题描述】:

我希望为 openssl 创建一个简单的通用函数模板 bignum 库。我希望它包含所有的 biolerplate 和其他已经存在的东西,它应该遵循与其他 bignum 函数相同的模式。它应该类似于:

#include <openssl/bn.h>    

int Big_Func(BIGNUM* RESULT, BIGNUM *X, BIGNUM *Y){
     //biolerplate bignum stuff

    //your code goes here

   // more biolerplate
}

我要留下一个我根据 bn_gcd 找到的答案,但这似乎有点矫枉过正。

【问题讨论】:

    标签: c openssl cryptography bignum


    【解决方案1】:
    include <openssl/bn.h>
    include <stdio.h>
    

    /* 这个函数假设它在另一个函数中被调用,比如 main,它有它的 bn_ctx 变量传递给 func */

    int BN_func(BIGNUM *R, BIGNUM *in_a,BIGNUM *in_b, BN_CTX *ctx)
    {
     BIGNUM *a, *b, *t;//declare ptrs of type BIGNUM
     int ret = 0;//return code
    
    bn_check_top(in_a);//checktop does makes sure the
                     // internal array is nonempty
    bn_check_top(in_b);
    
    BN_CTX_start(ctx);//BigNum context usedto obtain temp BN vars from
    // a BN_CTX which have already been created by using BN_CTX_new
    a = BN_CTX_get(ctx);//gets ptr from ctx
    b = BN_CTX_get(ctx);//
    if (a == NULL || b == NULL) //checks if ptr is NULL; 
                        //if so, exits immediately.
        goto err;
    
    if (BN_copy(a, in_a) == NULL)//copy the func params to local ctx vars
      goto err;        //check if null as above
    if (BN_copy(b, in_b) == NULL)
     goto err;
    a->neg = 0;// sets neg to 0 indicating positive qty.
    b->neg = 0;
    /*after all that we can calculate with the 'a' and 'b' params safely*/
    
    //your code here
    
       /*on error go here*/
     err:
       BN_CTX_end(ctx);
       bn_check_top(R);
       return (ret);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-26
      相关资源
      最近更新 更多