diff options
author | weidai <weidai@57ff6487-cd31-0410-9ec3-f628ee90f5f0> | 2003-07-16 01:53:45 +0000 |
---|---|---|
committer | weidai <weidai@57ff6487-cd31-0410-9ec3-f628ee90f5f0> | 2003-07-16 01:53:45 +0000 |
commit | 42c3d8f3aa593c224174558fd6f3d2709e08f7d0 (patch) | |
tree | 1f90c9ea7a31679b5c416408a3ffeba23e87d165 /gfpcrypt.h | |
parent | 09326fa9f564c09ebecff7c56d0e33555dec65b6 (diff) | |
download | cryptopp-42c3d8f3aa593c224174558fd6f3d2709e08f7d0.tar.gz |
added support for using encoding parameters and key derivation parameters
git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@98 57ff6487-cd31-0410-9ec3-f628ee90f5f0
Diffstat (limited to 'gfpcrypt.h')
-rw-r--r-- | gfpcrypt.h | 58 |
1 files changed, 36 insertions, 22 deletions
@@ -418,13 +418,14 @@ template <class MAC, bool DHAES_MODE> class DL_EncryptionAlgorithm_Xor : public DL_SymmetricEncryptionAlgorithm { public: - unsigned int GetSymmetricKeyLength(unsigned int plainTextLength) const - {return plainTextLength + MAC::DEFAULT_KEYLENGTH;} - unsigned int GetSymmetricCiphertextLength(unsigned int plainTextLength) const - {return plainTextLength + MAC::DIGESTSIZE;} - unsigned int GetMaxSymmetricPlaintextLength(unsigned int cipherTextLength) const - {return SaturatingSubtract(cipherTextLength, (unsigned int)MAC::DIGESTSIZE);} - void SymmetricEncrypt(RandomNumberGenerator &rng, const byte *key, const byte *plainText, unsigned int plainTextLength, byte *cipherText) const + bool ParameterSupported(const char *name) const {return strcmp(name, Name::EncodingParameters()) == 0;} + unsigned int GetSymmetricKeyLength(unsigned int plaintextLength) const + {return plaintextLength + MAC::DEFAULT_KEYLENGTH;} + unsigned int GetSymmetricCiphertextLength(unsigned int plaintextLength) const + {return plaintextLength + MAC::DIGESTSIZE;} + unsigned int GetMaxSymmetricPlaintextLength(unsigned int ciphertextLength) const + {return SaturatingSubtract(ciphertextLength, (unsigned int)MAC::DIGESTSIZE);} + void SymmetricEncrypt(RandomNumberGenerator &rng, const byte *key, const byte *plaintext, unsigned int plaintextLength, byte *ciphertext, const NameValuePairs ¶meters) const { const byte *cipherKey, *macKey; if (DHAES_MODE) @@ -435,22 +436,27 @@ public: else { cipherKey = key; - macKey = key + plainTextLength; + macKey = key + plaintextLength; } - xorbuf(cipherText, plainText, cipherKey, plainTextLength); + ConstByteArrayParameter encodingParameters; + parameters.GetValue(Name::EncodingParameters(), encodingParameters); + + xorbuf(ciphertext, plaintext, cipherKey, plaintextLength); MAC mac(macKey); - mac.Update(cipherText, plainTextLength); + mac.Update(ciphertext, plaintextLength); + mac.Update(encodingParameters.begin(), encodingParameters.size()); if (DHAES_MODE) { - const byte L[8] = {0,0,0,0,0,0,0,0}; + byte L[8] = {0,0,0,0}; + UnalignedPutWord(BIG_ENDIAN_ORDER, L+4, word32(encodingParameters.size())); mac.Update(L, 8); } - mac.Final(cipherText + plainTextLength); + mac.Final(ciphertext + plaintextLength); } - DecodingResult SymmetricDecrypt(const byte *key, const byte *cipherText, unsigned int cipherTextLength, byte *plainText) const + DecodingResult SymmetricDecrypt(const byte *key, const byte *ciphertext, unsigned int ciphertextLength, byte *plaintext, const NameValuePairs ¶meters) const { - unsigned int plainTextLength = GetMaxSymmetricPlaintextLength(cipherTextLength); + unsigned int plaintextLength = GetMaxSymmetricPlaintextLength(ciphertextLength); const byte *cipherKey, *macKey; if (DHAES_MODE) { @@ -460,21 +466,26 @@ public: else { cipherKey = key; - macKey = key + plainTextLength; + macKey = key + plaintextLength; } + ConstByteArrayParameter encodingParameters; + parameters.GetValue(Name::EncodingParameters(), encodingParameters); + MAC mac(macKey); - mac.Update(cipherText, plainTextLength); + mac.Update(ciphertext, plaintextLength); + mac.Update(encodingParameters.begin(), encodingParameters.size()); if (DHAES_MODE) { - const byte L[8] = {0,0,0,0,0,0,0,0}; + byte L[8] = {0,0,0,0}; + UnalignedPutWord(BIG_ENDIAN_ORDER, L+4, word32(encodingParameters.size())); mac.Update(L, 8); } - if (!mac.Verify(cipherText + plainTextLength)) + if (!mac.Verify(ciphertext + plaintextLength)) return DecodingResult(); - xorbuf(plainText, cipherText, cipherKey, plainTextLength); - return DecodingResult(plainTextLength); + xorbuf(plaintext, ciphertext, cipherKey, plaintextLength); + return DecodingResult(plaintextLength); } }; @@ -483,7 +494,8 @@ template <class T, bool DHAES_MODE, class KDF> class DL_KeyDerivationAlgorithm_P1363 : public DL_KeyDerivationAlgorithm<T> { public: - void Derive(const DL_GroupParameters<T> ¶ms, byte *derivedKey, unsigned int derivedLength, const T &agreedElement, const T &ephemeralPublicKey) const + bool ParameterSupported(const char *name) const {return strcmp(name, Name::KeyDerivationParameters()) == 0;} + void Derive(const DL_GroupParameters<T> ¶ms, byte *derivedKey, unsigned int derivedLength, const T &agreedElement, const T &ephemeralPublicKey, const NameValuePairs ¶meters) const { SecByteBlock agreedSecret; if (DHAES_MODE) @@ -498,7 +510,9 @@ public: params.EncodeElement(false, agreedElement, agreedSecret); } - KDF::DeriveKey(derivedKey, derivedLength, agreedSecret, agreedSecret.size()); + ConstByteArrayParameter derivationParameters; + parameters.GetValue(Name::KeyDerivationParameters(), derivationParameters); + KDF::DeriveKey(derivedKey, derivedLength, agreedSecret, agreedSecret.size(), derivationParameters.begin(), derivationParameters.size()); } }; |