【发布时间】:2020-07-10 05:59:46
【问题描述】:
我正在尝试实现Elgamal 操作。常见的第一个是两个 BIGNUM 之间的乘法。第二个是两个 BIGNUM 的幂(例如h:=g^x, c_1:=g^y)。当我执行BN_exp() 时,C 程序卡住了。为什么?另外,有什么解决问题的建议吗?
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <openssl/obj_mac.h>
#include <openssl/ec.h>
#include <openssl/rand.h>
#include <openssl/bn.h>
int main(int argc, char *argv[])
{
BN_CTX *ctx;
BIGNUM *bn1 = BN_new();
BIGNUM *bn2 = BN_new();
BIGNUM *result = BN_new();
BIGNUM *r = BN_new();
BN_CTX *bn_ctx = BN_CTX_new();
static const char rnd_seed[] = "string to make the random number generator think it has entropy";
RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime_ex may fail */
BN_generate_prime_ex(r, 1024, 0, NULL, NULL, NULL);
BN_rand_range(bn1, r);
BN_rand_range(bn2, r);
BN_mul(result, bn2, bn1, bn_ctx);
BN_exp(result, bn2, bn1, bn_ctx); // here get stuck!
return 0;
}
【问题讨论】:
-
ElGamal 需要(性能更好的)模运算,即
BN_mod_mul()、BN_mod_exp()和BN_mod_inverse()。标准的幂运算 (BN_exp()) 会很快产生大量的数字并花费大量时间(测试 16、17、...位)。 -
您显示的代码不是 ElGamal。
-
@President James K. Polk 当然是 xD。这是 Elgamal 密码系统中使用的主要操作。顺便说一句,感谢 Topaco 的建议,我解决了。
标签: c openssl cryptography elgamal