使用OpenSSL的SHA -256密钥大小为2048位的RSA-OAEP

Mar*_*per 8 c openssl rsa public-key-encryption

我试图解决与如何使用RSA加密数据完全相同的用例,SHA-256作为哈希函数,MGF1作为掩码生成函数?,但我需要更清楚一点.

上述查询是在2013年提出的.当时OpenSSL仅支持SHAEP填充的SHA1哈希(硬编码).在最新的OpenSSL(1.0.2k)中,我可以看到使用以下API解决了这个问题:

int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
                                    const unsigned char *from, int flen,
                                    const unsigned char *param, int plen,
                                    const EVP_MD *md, const EVP_MD mgf1md)
Run Code Online (Sandbox Code Playgroud)

RSA_public_encrypt()不将EVP_MD结构作为参数我不知道如何指定它.

如何RSA_public_encrypt()使用掩码生成功能调用SHA-256模式?

Ant*_*ton 1

RSA_public_encrypt(...)已弃用;应改用EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, ...) 。

填充、掩码生成函数和其他参数是为作为第一个参数传递给 EVP_PKEY_encrypt 的上下文配置的:

    EVP_PKEY* evp_key = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL);
    if (evp_key == NULL) {
        // handle error
    }

    EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new(evp_key, NULL);
    if (ctx == NULL) {
        // handle error
    }

    if (EVP_PKEY_encrypt_init(ctx) <= 0) {
        // handle error
    }

    if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0) {
        // handle error
    }
    if (EVP_PKEY_CTX_set_rsa_oaep_md(ctx, EVP_sha256()) <= 0) {
        // handle error
    }
    if (EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, EVP_sha256()) <= 0) {
        // handle error
    }

    if (EVP_PKEY_encrypt(ctx, encrypted, &outlen, data, len) <= 0) {
        // handle error
    }
Run Code Online (Sandbox Code Playgroud)