0

我使用 Crypto++ 使用 ECDSA 制作了签名者/检查者机制。

问题是当我想检查签名时,它不适用于验证功能。

你能建议我一种更手动的方式来验证签名吗?

4

1 回答 1

2

如何在没有验证功能的情况下使用 Crypto++ 验证 ECDSA 签名?

我不确定在没有验证功能的情况下如何验证代码。我可能不明白这个问题。

以防万一,以下是 Crypto++ 实现其验证码的方式。

首先,ECDSA是一个DL_Algorithm_ECDSA(来自eccrypto.h):

//! ECDSA algorithm
template <class EC>
class DL_Algorithm_ECDSA : public DL_Algorithm_GDSA<typename EC::Point>
{
public:
    static const char * CRYPTOPP_API StaticAlgorithmName() {return "ECDSA";}
};
...

template <class EC, class H>
struct ECDSA :
    public DL_SS<DL_Keys_ECDSA<EC>, DL_Algorithm_ECDSA<EC>,
               DL_SignatureMessageEncodingMethod_DSA, H>
{
};

接下来,这里是来自DL_Algorithm_GDSAin的验证函数gfcrypt.h

bool Verify(const DL_GroupParameters<T> &params, const DL_PublicKey<T> &publicKey,
                const Integer &e, const Integer &r, const Integer &s) const
{
    const Integer &q = params.GetSubgroupOrder();
    if (r>=q || r<1 || s>=q || s<1)
        return false;

    Integer w = s.InverseMod(q);
    Integer u1 = (e * w) % q;
    Integer u2 = (r * w) % q;

    return r == params.ConvertElementToInteger(
                    publicKey.CascadeExponentiateBaseAndPublicElement(u1, u2)) % q;
}

下面的代码使用VerifyMessage, 及其在 中PK_Verifier声明的部分cryptolib.h

virtual bool VerifyMessage(const byte *message, size_t messageLen, 
                 const byte *signature, size_t signatureLength) const;

PK_Verifier是对象喜欢的“主”基类,用于ECDSA公开一致的接口。NRRSASS

ECDSA,NR和通过RSASS连接到的对象:PK_VerifierDL_SS

template <class EC, class H>
struct ECDSA :
    public DL_SS<DL_Keys_ECDSA<EC>, DL_Algorithm_ECDSA<EC>, DL_SignatureMessageEncodingMethod_DSA, H>
{
};

最后,以下DL_SS是与PK_Verifier(from pubkey.h) 的关系:

//! Discrete Log Based Signature Scheme
template <class KEYS, class SA, class MEM, class H, class ALG_INFO = DL_SS<KEYS, SA, MEM, H, int> >
class DL_SS : public KEYS
{
    typedef DL_SignatureSchemeOptions<ALG_INFO, KEYS, SA, MEM, H> SchemeOptions;
    ...

    //! implements PK_Signer interface
    typedef PK_FinalTemplate<DL_SignerImpl<SchemeOptions> > Signer;
    //! implements PK_Verifier interface
    typedef PK_FinalTemplate<DL_VerifierImpl<SchemeOptions> > Verifier;
};

问题是当我想检查签名时,它不适用于验证功能。

Crypto++ wiki 上有很多可用的代码。对于 ECDSA,请参阅椭圆曲线数字签名算法。下面是一个签名验证样本,取自 wiki。

符号

AutoSeededRandomPool prng;

ECDSA<ECP, SHA1>::PrivateKey privateKey;
privateKey.Load(...);
privateKey.Validate(prng, 3);

ECDSA<ECP, SHA1>::Signer signer(privateKey);

string message = "Do or do not. There is no try.";

// Determine maximum size, allocate a string with the maximum size
size_t siglen = signer.MaxSignatureLength();
string signature(siglen, 0x00);

// Sign, and trim signature to actual size
siglen = signer.SignMessage( prng, message.data(), message.size(), signature.data() );
signature.resize(siglen);

核实

AutoSeededRandomPool prng;

ECDSA<ECP, SHA1>::PublicKey publicKey;
publicKey.Load(...);
publicKey.Validate(prng, 3);

ECDSA<ECP, SHA1>::Verifier verifier(publicKey);

bool result = verifier.VerifyMessage( message.data(), message.size(), signature.data(), signature.size() );
if(result)
  cout << "Verified signature on message" << endl;
else
  cerr << "Failed to verify signature on message" << endl;
于 2014-01-13T05:25:16.907 回答