我应该在 rsa_pub_enc 和 rsa_pub_dec OpenSSL 函数中使用什么?

What should I use into rsa_pub_enc and rsa_pub_dec OpenSSL functions?

我编写了一些 OpenSSL 引擎。 它通过帮助硬件实现了其他RSA实现。

OpenSSL 具有函数 ENGINE_set_RSA(ENGINE *e, const RSA_METHOD *rsa_meth);。在 help one 的帮助下,我可以设置我的新实现。 类型 RSA_METHOD 包含指向实现的指针。

struct rsa_meth_st {
    const char *name;
    int (*rsa_pub_enc) (int flen, const unsigned char *from,
                        unsigned char *to, RSA *rsa, int padding);
    int (*rsa_pub_dec) (int flen, const unsigned char *from,
                        unsigned char *to, RSA *rsa, int padding);
    int (*rsa_priv_enc) (int flen, const unsigned char *from,
                         unsigned char *to, RSA *rsa, int padding);
    int (*rsa_priv_dec) (int flen, const unsigned char *from,
                         unsigned char *to, RSA *rsa, int padding);
    /* Can be null */
    int (*rsa_mod_exp) (BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
    /* Can be null */
    int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                       const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
    /* called at new */
    int (*init) (RSA *rsa);
    /* called at free */
    int (*finish) (RSA *rsa);
    /* RSA_METHOD_FLAG_* things */
    int flags;
    /* may be needed! */
    char *app_data;
    /*
     * New sign and verify functions: some libraries don't allow arbitrary
     * data to be signed/verified: this allows them to be used. Note: for
     * this to work the RSA_public_decrypt() and RSA_private_encrypt() should
     * *NOT* be used RSA_sign(), RSA_verify() should be used instead. Note:
     * for backwards compatibility this functionality is only enabled if the
     * RSA_FLAG_SIGN_VER option is set in 'flags'.
     */
    int (*rsa_sign) (int type,
                     const unsigned char *m, unsigned int m_length,
                     unsigned char *sigret, unsigned int *siglen,
                     const RSA *rsa);
    int (*rsa_verify) (int dtype, const unsigned char *m,
                       unsigned int m_length, const unsigned char *sigbuf,
                       unsigned int siglen, const RSA *rsa);
    /*
     * If this callback is NULL, the builtin software RSA key-gen will be
     * used. This is for behavioural compatibility whilst the code gets
     * rewired, but one day it would be nice to assume there are no such
     * things as "builtin software" implementations.
     */
    int (*rsa_keygen) (RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
};

我不明白 rsa_pub_encrsa_pub_dec 应该做什么。 它应该只用 PUBlic 密钥加密和解密吗?

关于 rsa_priv_enc/rsa_priv_dec 我也有同样的问题。 是否应该仅借助私钥进行加密和解密?

我已经看过了https://www.openssl.org/docs/manmaster/man3/RSA_public_encrypt.html and https://www.openssl.org/docs/man1.1.0/man3/RSA_public_decrypt.html,但是没看懂

谁能给我解释一下,好吗?

encryption/decryption是这样发生的:

  1. 使用 public 密钥加密 - 使用私钥 解密(建议和标准方式) 或
  2. 使用私钥加密 - 使用 public 密钥解密

设置 1:您的 rsa_pub_enc 可以指向 RSA_public_encrypt 的实现,而 rsa_priv_dec 可以指向 RSA_private_decrypt

Set 2 : 你的 rsa_priv_enc 可以指向 RSA_private_encrypt 的实现并且 rsa_pub_dec 可以指向 RSA_public_decrypt

用户设置1因为保持私钥安全总是好的