From d23a489940499bd6c634a1cb0a9875f094f8a850 Mon Sep 17 00:00:00 2001 From: weidai Date: Thu, 20 Mar 2003 01:24:12 +0000 Subject: various changes for 5.1 git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@38 57ff6487-cd31-0410-9ec3-f628ee90f5f0 --- gfpcrypt.h | 55 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 16 deletions(-) (limited to 'gfpcrypt.h') diff --git a/gfpcrypt.h b/gfpcrypt.h index 31db5a1..24c8168 100644 --- a/gfpcrypt.h +++ b/gfpcrypt.h @@ -149,18 +149,13 @@ class DL_Algorithm_GDSA : public DL_ElgamalLikeSignatureAlgorithm public: static const char * StaticAlgorithmName() {return "DSA-1363";} - Integer EncodeDigest(unsigned int modulusBits, const byte *digest, unsigned int digestLen) const - { - return DSA_EncodeDigest(modulusBits, digest, digestLen); - } - - bool Sign(const DL_GroupParameters ¶ms, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const + void Sign(const DL_GroupParameters ¶ms, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const { const Integer &q = params.GetSubgroupOrder(); - r = params.ConvertElementToInteger(params.ExponentiateBase(k)) % q; + r %= q; Integer kInv = k.InverseMod(q); s = (kInv * (x*r + e)) % q; - return (!!r && !!s); + assert(!!r && !!s); } bool Verify(const DL_GroupParameters ¶ms, const DL_PublicKey &publicKey, const Integer &e, const Integer &r, const Integer &s) const @@ -189,12 +184,12 @@ public: return NR_EncodeDigest(modulusBits, digest, digestLen); } - bool Sign(const DL_GroupParameters ¶ms, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const + void Sign(const DL_GroupParameters ¶ms, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const { const Integer &q = params.GetSubgroupOrder(); - r = (params.ConvertElementToInteger(params.ExponentiateBase(k)) + e) % q; + r = (r + e) % q; s = (k - x*r) % q; - return !!r; + assert(!!r); } bool Verify(const DL_GroupParameters ¶ms, const DL_PublicKey &publicKey, const Integer &e, const Integer &r, const Integer &s) const @@ -345,13 +340,21 @@ public: //! DSA-1363 template -struct GDSA : public DL_SSA, H> +struct GDSA : public DL_SS< + DL_SignatureKeys_GFP, + DL_Algorithm_GDSA, + DL_SignatureMessageEncodingMethod_DSA, + H> { }; //! NR template -struct NR : public DL_SSA, H> +struct NR : public DL_SS< + DL_SignatureKeys_GFP, + DL_Algorithm_NR, + DL_SignatureMessageEncodingMethod_NR, + H> { }; @@ -376,7 +379,12 @@ struct DL_Keys_DSA }; //! DSA -struct DSA : public DL_SSA, SHA, DSA> +struct DSA : public DL_SS< + DL_Keys_DSA, + DL_Algorithm_GDSA, + DL_SignatureMessageEncodingMethod_DSA, + SHA, + DSA> { static std::string StaticAlgorithmName() {return std::string("DSA");} @@ -426,7 +434,14 @@ public: } xorbuf(cipherText, plainText, cipherKey, plainTextLength); - MAC(macKey).CalculateDigest(cipherText + plainTextLength, cipherText, plainTextLength); + MAC mac(macKey); + mac.Update(cipherText, plainTextLength); + if (DHAES_MODE) + { + const byte L[8] = {0,0,0,0,0,0,0,0}; + mac.Update(L, 8); + } + mac.Final(cipherText + plainTextLength); } DecodingResult SymmetricDecrypt(const byte *key, const byte *cipherText, unsigned int cipherTextLength, byte *plainText) const { @@ -443,8 +458,16 @@ public: macKey = key + plainTextLength; } - if (!MAC(macKey).VerifyDigest(cipherText + plainTextLength, cipherText, plainTextLength)) + MAC mac(macKey); + mac.Update(cipherText, plainTextLength); + if (DHAES_MODE) + { + const byte L[8] = {0,0,0,0,0,0,0,0}; + mac.Update(L, 8); + } + if (!mac.Verify(cipherText + plainTextLength)) return DecodingResult(); + xorbuf(plainText, cipherText, cipherKey, plainTextLength); return DecodingResult(plainTextLength); } -- cgit v1.2.1