我正在实现智能卡中间件,我需要向其中添加 RSA-PSS 签名方案。我希望我可以覆盖 Crypto++ 中负责私钥操作的代码并免费获得其他所有内容(PSS 和 PKCS 填充)。我认为 rsa.cpp
中的这段代码是我想为智能卡重写的不同内容:
Integer InvertibleRSAFunction::CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
{
DoQuickSanityCheck();
ModularArithmetic modn(m_n);
Integer r, rInv;
do { // do this in a loop for people using small numbers for testing
r.Randomize(rng, Integer::One(), m_n - Integer::One());
rInv = modn.MultiplicativeInverse(r);
} while (rInv.IsZero());
...
首先我认为这可以通过子类化 RSA::PrivateKey
class MyPrivKey : public RSA::PrivateKey {
public:
template<typename... Args>
MyPrivKey(Args&&... args) : RSA::PrivateKey(std::forward<Args>(args)...) {}
Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const override {
return ... // do some smart card magic
}
};
然后将这个 key 传递给signer
对象
MyPrivateKey privateKey(params);
// Signer object
RSASS<PSS, SHA1>::Signer signer(privateKey);
但很快我发现 privateKey
的所有组件(模数、私有(private)和公共(public)指数等)都只是复制到 signer
内部。所以覆盖 RSA::PrivateKey::CalculateInverse
并没有真正的帮助。
然后我迷失在模板中并哭了起来。我想有人可以帮助我的可能性为 0.0001%。我来试试运气。
我的示例的完整代码 https://pastebin.com/Nwk4jX0j
最佳答案
好吧,我发现我需要继承更多的东西
class SmartCardPrivateKey : public InvertibleRSAFunction {
public:
Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const override {
return ... // do smart card magic here
}
};
struct SmartCardRSA : public RSA {
typedef SmartCardPrivateKey PrivateKey;
};
template <class STANDARD, class H>
struct SmartCardRSASS : public TF_SS<SmartCardRSA, STANDARD, H> {
};
然后可以构造自定义签名者对象
SmartCardRSASS<PSS, SHA1>::Signer signer
关于c++ - 覆盖智能卡的 RSA 私钥操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44266480/