为 RSA OpenSSL 设置特定密钥

Set up a specific key for RSA OpenSSL

我目前正在尝试为 RSA 实现实现一些测试向量,我想通过 OpenSSL v1.1.0f 实现来测试它们。但是,当我尝试设置密钥(e、n p、q 或 d)时,出现以下错误:

erreur : dereferencing pointer to incomplete type « RSA {alias struct rsa_st} »

我的代码如下:

int rsa_encrypt(byte *in, size_t in_len, byte *out, const char *n, const char *e, char padding){
    int err = -1;

    RSA *keys = NULL;
    keys = RSA_new();

    BN_hex2bn(&keys->n, n); // error here
    BN_hex2bn(&keys->e, e); // and here

    out = malloc(RSA_size(keys));

    if (padding == OAEP ) {
        err = RSA_public_encrypt(in_len, in, out, keys, RSA_PKCS1_OAEP_PADDING);
    }
    else if (padding == v1_5) {
        err = RSA_public_encrypt(in_len, in, out, keys, RSA_PKCS1_PADDING);
    }

    RSA_free(keys);

    return err;
}

其中 n 和 e 是代表我的十六进制参数的字符串。

我查找了RSA类型对应的结构,找到了this。 我不明白为什么我不能设置 n 和 e...有什么想法吗?

OpenSSL 结构在 1.1.x 中是不透明的,您不能谈论它们的字段。除其他外,它允许在服务发布期间添加新的结构字段,因为调用者代码无法知道字段偏移量。

对于 1.1.x 你需要做一些类似于

BN* bnN = NULL;
BN* bnE = NULL;
RSA* keys = RSA_new();

BN_hex2bn(&bnN, n);
BN_hex2bn(&bnE, e);
RSA_set0_key(keys, bnN, bnE, NULL);

...
RSA_free(keys);
// do not free bnN or bnE, they were freed by RSA_free.

请注意,我省略了必要的错误检查。