首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在OpenSSL中定义常量BIGNUM

在OpenSSL中定义常量BIGNUM
EN

Stack Overflow用户
提问于 2016-06-29 22:50:22
回答 1查看 1K关注 0票数 1

是否有一种方法可以在C中定义一个类型为BIGNUM*并值2^512的常量,其等价于Java:

代码语言:javascript
复制
private static final BigInteger THRESHOLD = BigInteger.valueOf(2).pow(512);

在本地范围内,我通过执行以下操作来实现这一点:

代码语言:javascript
复制
BN_CTX *ctx = BN_CTX_new();

BIGNUM *two = BN_new();
BN_set_word(two, 2);

BIGNUM *pow = BN_new();
BN_set_word(pow, 512);

BIGNUM * threshold = BN_new();
BN_exp(threshold, two, pow, ctx);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-30 09:20:54

是也不是。

在OpenSSL 1.0.2/1.0.1中,这是可能的(警告:代码完全未经测试!)

代码语言:javascript
复制
/*
 * I made up the numbers...too lazy to figure out what the real ones are!
 * Note that, the BN_ULONG values here are in little endian form,
 * so this represents:
 * D3FBF564FEB008A3
 */
#if BN_BITS2 == 64
static const BN_ULONG data[] = {
    0xA308B0FE64F5FBD3ULL
};
#else
static const BN_ULONG data[] = {
    0x64F5FBD3, 0xA308B0FE
};
#endif

static const BIGNUM threshold = {
    (BN_ULONG *) data,
    sizeof(data)/sizeof(BN_ULONG),
    sizeof(data)/sizeof(BN_ULONG),
    0,
    BN_FLG_STATIC_DATA
};

在OpenSSL 1.1.0 (尚未发布)中,事情就不那么容易了。由于非常好的原因,BIGNUM结构已经变得不透明,所以您不能再静态地初始化数据。

你可以这样做:

代码语言:javascript
复制
static CRYPTO_ONCE threshold_once = CRYPTO_ONCE_STATIC_INIT;
static BIGNUM *threshold = NULL;

static void thresholdcleanup(void)
{
    BN_free(threshold);
}
static void thresholdinit(void)
{
    threshold = BN_new();
    /* Do stuff to set threshold to the right value */
    OPENSSL_atexit(thresholdcleanup);
}

static void my_func(void)
{
    CRYPTO_THREAD_run_once(&threshold_once, threshholdinit);

    /* Use threshold here */
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38111293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档