2014-01-12 94 views

回答

1

如何在沒有驗證功能的情況下使用Crypto ++驗證ECDSA簽名?

我不知道如何驗證沒有驗證功能的代碼。我可能不瞭解這個問題。

爲了以防萬一,以下是Crypto ++如何實現其驗證碼。

首先,ECDSADL_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> 
{ 
}; 

接下來,這裏是在gfcrypt.h驗證從DL_Algorithm_GDSA功能:

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_Verifiercryptolib.h中聲明:

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

PK_Verifier是「主」基類對象像ECDSANRRSASS使用以暴露一致的接口。

對象,如ECDSANRRSASS通過DL_SS連接到PK_Verifier

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(從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,請參閱Elliptic Curve Digital Signature Algorithm。下面是一個簽名從維基採取的驗證樣本。

註冊

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; 
+0

問題是我想簽署pe文件並用exe保存密鑰,而在另一個exe文件中,我想加載公鑰並檢查簽名 – user3188135

+0

Wei在Crypto ++的test.cpp中做了類似的操作。查看選項「mac_dll」的代碼。簡而言之,'mac_dll'打開可執行文件,用'HMAC'對代碼進行校驗,然後將預期的指紋嵌入到可執行文件中以檢測篡改。在運行時,可執行文件驗證校驗和的代碼上的簽名。由於[FIPS 140-2](http://csrc.nist.gov/publications/fips/fips140-2/fips1402.pdf)的要求,「mac_dll」存在。另請參閱[動態文本部分圖像驗證](http://www.codeproject.com/Articles/17636/Dynamic-TEXT-Section-Image-Verification)。 – jww

+0

@jww請結帳這個問題http://stackoverflow.com/questions/30950856/ecdsa-signing-not-working-using-cryptopp我想簽署和驗證使用ECDSA,但隨機生成的密鑰它甚至不簽署 – SandeepAggarwal