c++ - 覆盖智能卡的 RSA 私钥操作?

标签 c++ cryptography rsa smartcard crypto++

我正在实现智能卡中间件,我需要向其中添加 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/

相关文章:

java - 充气城堡 PQC XMSS 签名 : NullPointerException after retrieving SecretKey from KeyStore

java - 将密码存储在文件 : RSA implementation 中

android - Java(Android) 中的 RSA key 签名和验证

c++ - InitializeCriticalSectionEx 不是 atlwinverapi.h 中全局命名空间的成员

java - 如何使用keytool设置Usage属性

java - RSA/ECB/OAEPWithSHA-256AndMGF1Padding 但 MGF1 使用 SHA-256?

objective-c - 从 AnyObject 转换时出错?到 SecKeyRef?在 swift

C++ 需要一些关于 Pig Latin 字符串的建议

c++ - 简单的 C++ 声明问题

c++ - 为什么 "(!v.empty())"比 "(v.size() >0)"更好?