概述
我有一些关于在 openSSL 中实现rsa_meth_st
(又名 RSA_METHOD)的具体问题,但是在执行标准 RSA 操作时,我通常也对我的引擎在(或应该)被用户“钩住”的确切位置感到困惑。
背景
我正在尝试为自定义 RSA 硬件加速器开发引擎,并且对 RSA_METHOD 结构实现有一些疑问。
一些上下文:对于加密,我的加速器将基数、公共指数和模数作为输入,并返回结果密文。对于解密,它将基数和模数作为输入。它不需要私钥,因为它存储在硬件中,并且只能通过带外通道进行配置。我已经有一个内核模块,它向用户空间程序公开 API 以使用加速器。现在我只需要将它集成到 openSSL 中。
我已经为 AES 和 SHA256 创建了一个类似的引擎,但是我在 RSA 上苦苦挣扎。理想情况下,除了对预先填充和准备好的数据块执行模幂运算之外,我不必担心其他任何事情。对于 SHA 和 AES,这很简单:所有这些都由 EVP 接口处理,所以我只需要担心将数据传入和传出加速器。但是对于 RSA 来说似乎并不那么简单(如果我错了,请纠正我)。
我的具体问题
我对我的引擎需要实现哪些 RSA_METHOD 函数指针感到困惑。我显示下面的结构以供参考:
struct rsa_meth_st {
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);
int (*rsa_mod_exp) (BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
/* ....stuff.... */
int flags;
/* .... stuff ... */
}; // TYPEDEF'ED TO RSA_METHOD in include/ossl_typ.h
所以,三个问题:
- 标准 OpenSSL RSA 实现是否可以使用我的引擎的“模幂”函数,而不必重写
RSA_[public|private]_[encrypt|decrypt]
/include/openssl/rsa.h 中的函数系列? - 如果是这样,只实现 rsa_mod_exp 函数就足够了吗?还是我必须同时实现 public_enc/dec 和 private_enc/dec 函数?我问,因为旧英特尔 RSAX 引擎的源代码执行此操作,但我无法弄清楚在“RSA 流”中如何以及何时调用引擎的函数。
在 /include/openssl/rsa.h 中,我看到了 RSA_METHOD 标志字段的以下宏(第 61 行):
/*
* This flag means the private key operations will be handled by rsa_mod_exp
* and that they do not depend on the private key components being present:
* for example a key stored in external hardware. Without this flag
* bn_mod_exp gets called when private key components are absent.
*/
# define RSA_FLAG_EXT_PKEY 0x0020
- 这是否意味着如果我在 RSA_METHOD 的“标志”字段中使用此标志,我不需要实现 rsa_pub_enc/dec 和朋友?我想我只是对在 RSA 加密/解密过程中应该在什么时候调用我的引擎感到困惑。
任何智慧之言将不胜感激。