使用OpenSSL的RSA解密原语(SP800-56B)

时间:2019-07-11 17:31:00

标签: encryption openssl rsa

我正在尝试使用OpenSSL实现RSA解密原语(请参阅https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Br2.pdf;第7.1.2.1节)。

这是我的代码:

BIGNUM *m = NULL, *n = NULL, *d = NULL, *c = NULL;
RSA    *rsa = NULL;
int isValid = -1;

n = BN_bin2bn(N, 256, n);
d = BN_bin2bn(D, 256, d);
c = BN_bin2bn(ciphertext, 256, d);

rsa = RSA_new();

if (c == NULL || n == NULL || d == NULL)
    printf("\n\nC,N,D is NULL, BN_bin2bn() failed!!\n\n");
isValid = BN_mod_exp(m, c, d, n, rsa);

RSA_free(rsa);
BN_free(n); BN_free(d); BN_free(m);

我的输入如下所示:

N : bff722714050aebb23a9bd018c3e9ba26a47f53816eeac7e10543958702d9265c8d67784fe03c07bfceac05e7f2a434971dfa2a5ea461893450ced52fcb3f143a85fb3a9194417ff220258840a3359a104079ccd201afec091bab6587d4cfe0b95bba34ef74a70b392a92a93f026c9bed41eb4ec80452492a2ad524e6b0333c5787b34ee941829020bb75ee5dd216b3734823ddd547d50f8a7e711f8a24fd7dbc0bd2f062ccaba98cdbf62c15d2521b39ce44c53125604493e482ae35f945c4efff1d01414b0aad33de77b020ea4aedf3d88171fe51b22881bc70c639f8b6f1b5a70ed39aa121a8f44887dcbbfce29e1e508d1b0f0666693b476d81faa6a18bd

D : a13aec8eba3a09c7dc18404b0083c52c10a00771e8b0e5e7abc751b2d9e52cc4987ea93be62d3889eacf306b2ddb4d506e782a9fb7b8d0034147ae3cb94a59253e51c3100fcc856b2021603ee66262b13e3536998291a9ce0b980a7720267e693485b890265b3b75578505e1e31e70ebfa3520385333bf97f9522183039658efd9b09fc0bd67a7d3c32e23adada71320ada2135f1d06a9144033ff9e0037a3b7ed1f5729b6db5f02470ecdde9eb2d97c759c73d13889bae550ab97205b67ce2f91eefb487f18c19bc6dd8831a43b0d699c771e1a9c55a1d5d2ae975691789b5c0a814c4f5e3d6a8e9e5f75419194b2d7dfe06700f6891cae8b712b3af1f9ec71

C : 534d1f57d948cac580b88b922bc47bc3d64c8cd1262bbf0944b99833ec94d072c1a1496be44d47a9c419dc403855a4b1cb2bb30e56e0cc5fd557d34373d785dbe70d67e30355fc228a353b05432a40874ba84253af5cc52d3ab4118e8ca1e28e6c9c610760e753f87a15912774ccb80b00ca21e85926143c1ed8385a607c4e55fa531f1f208bb3f23bc0c4eff4c272068f9939157bc61f5427cc32f017ef31f6363c8a736ec984da763ebea5eb94d83fa31d70223ec5503cfd97e598d883f43aca5e884b702a2f76d298659181cb5180e25faf56c9aa0ebe49413b9acbbefde95ec102ee4e351a8ff8d5a3fbdcee448ff466dffb45fdc0a0b3d31b3d192bb5cb

从文档中,可以通过以下方式得出m:

m = (c^d) mod n

我使用OpenSSL函数BN_mod_exp()来做到这一点。 (请参阅:https://www.openssl.org/docs/man1.1.0/man3/BN_mod_exp.html)。我从https://github.com/openssl/openssl/blob/master/test/bntest.c开始关注此函数的用法示例,但仍然看到以下错误。

enter image description here

有人可以告诉我他们做的事情有什么问题吗? 我确保我正确复制了值并在使用后释放了它们。 预先感谢!

更新:我将NULL arg作为第五个arg传递给BN_mod_exp函数:

BN_mod_exp(m, c, d, n, NULL);

我仍然看到一个错误,与前一个略有不同。最初,我将第五个参数设为NULL,但这给了我这个错误,这就是为什么我传入NULL的原因。 在下面传递NULL后显示输出:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff792fd39 in fips_bn_ctx_start (ctx=0x0) at bn_ctx.c:261
261             if(ctx->err_stack || ctx->too_many)
(gdb) bt
#0  0x00007ffff792fd39 in fips_bn_ctx_start (ctx=0x0) at bn_ctx.c:261
#1  0x00007ffff7932a55 in fips_bn_mod_exp_mont (rr=0x0, a=0x6a9b30, p=0x6a9b30, m=0x6a99c0, ctx=0x0, in_mont=0x0) at bn_exp.c:417
#2  0x00007ffff79320f0 in fips_bn_mod_exp (r=0x0, a=0x6a9b30, p=0x6a9b30, m=0x6a99c0, ctx=0x0) at bn_exp.c:237

N,D和密文的数据类型:

unsigned char N[512];  
unsigned char D[512];   
unsigned char ciphertext[512];

0 个答案:

没有答案