From 572fe07633123ce38abf28c6426356e37aef3a99 Mon Sep 17 00:00:00 2001 From: weidai Date: Fri, 4 Jul 2003 00:17:37 +0000 Subject: create DLL version, fix GetNextIV() bug in CTR and OFB modes git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@87 57ff6487-cd31-0410-9ec3-f628ee90f5f0 --- modes.h | 105 +++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 68 insertions(+), 37 deletions(-) (limited to 'modes.h') diff --git a/modes.h b/modes.h index 4dd1437..e6b30e0 100644 --- a/modes.h +++ b/modes.h @@ -28,7 +28,7 @@ struct CipherModeDocumentation : public SymmetricCipherDocumentation { }; -class CRYPTOPP_NO_VTABLE CipherModeBase : public SymmetricCipher +class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CipherModeBase : public SymmetricCipher { public: unsigned int MinKeyLength() const {return m_cipher->MinKeyLength();} @@ -66,16 +66,19 @@ template class CRYPTOPP_NO_VTABLE ModePolicyCommonTemplate : public CipherModeBase, public POLICY_INTERFACE { unsigned int GetAlignment() const {return m_cipher->BlockAlignment();} - void CipherSetKey(const NameValuePairs ¶ms, const byte *key, unsigned int length) - { - m_cipher->SetKey(key, length, params); - ResizeBuffers(); - int feedbackSize = params.GetIntValueWithDefault(Name::FeedbackSize(), 0); - SetFeedbackSize(feedbackSize); - } + void CipherSetKey(const NameValuePairs ¶ms, const byte *key, unsigned int length); }; -class CRYPTOPP_NO_VTABLE CFB_ModePolicy : public ModePolicyCommonTemplate +template +void ModePolicyCommonTemplate::CipherSetKey(const NameValuePairs ¶ms, const byte *key, unsigned int length) +{ + m_cipher->SetKey(key, length, params); + ResizeBuffers(); + int feedbackSize = params.GetIntValueWithDefault(Name::FeedbackSize(), 0); + SetFeedbackSize(feedbackSize); +} + +class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CFB_ModePolicy : public ModePolicyCommonTemplate { public: IV_Requirement IVRequirement() const {return RANDOM_IV;} @@ -118,25 +121,35 @@ inline void CopyOrZero(void *dest, const void *src, size_t s) memset(dest, 0, s); } -class CRYPTOPP_NO_VTABLE OFB_ModePolicy : public ModePolicyCommonTemplate +class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE OFB_ModePolicy : public ModePolicyCommonTemplate { +public: + bool IsRandomAccess() const {return false;} + IV_Requirement IVRequirement() const {return STRUCTURED_IV;} + +private: unsigned int GetBytesPerIteration() const {return BlockSize();} unsigned int GetIterationsToBuffer() const {return 1;} void WriteKeystream(byte *keystreamBuffer, unsigned int iterationCount) { assert(iterationCount == 1); m_cipher->ProcessBlock(keystreamBuffer); + memcpy(m_register, keystreamBuffer, BlockSize()); } void CipherResynchronize(byte *keystreamBuffer, const byte *iv) { CopyOrZero(keystreamBuffer, iv, BlockSize()); } - bool IsRandomAccess() const {return false;} - IV_Requirement IVRequirement() const {return STRUCTURED_IV;} }; -class CRYPTOPP_NO_VTABLE CTR_ModePolicy : public ModePolicyCommonTemplate +class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CTR_ModePolicy : public ModePolicyCommonTemplate { +public: + bool IsRandomAccess() const {return true;} + IV_Requirement IVRequirement() const {return STRUCTURED_IV;} + void GetNextIV(byte *IV); + +private: unsigned int GetBytesPerIteration() const {return BlockSize();} unsigned int GetIterationsToBuffer() const {return m_cipher->OptimalNumberOfParallelBlocks();} void WriteKeystream(byte *buffer, unsigned int iterationCount) @@ -144,16 +157,14 @@ class CRYPTOPP_NO_VTABLE CTR_ModePolicy : public ModePolicyCommonTemplateProcessAndXorMultipleBlocks(inString, NULL, outString, numberOfBlocks);} }; -class CRYPTOPP_NO_VTABLE CBC_ModeBase : public BlockOrientedCipherModeBase +class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_ModeBase : public BlockOrientedCipherModeBase { public: IV_Requirement IVRequirement() const {return UNPREDICTABLE_RANDOM_IV;} @@ -193,13 +204,13 @@ public: unsigned int MinLastBlockSize() const {return 0;} }; -class CRYPTOPP_NO_VTABLE CBC_Encryption : public CBC_ModeBase +class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_Encryption : public CBC_ModeBase { public: void ProcessBlocks(byte *outString, const byte *inString, unsigned int numberOfBlocks); }; -class CRYPTOPP_NO_VTABLE CBC_CTS_Encryption : public CBC_Encryption +class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_CTS_Encryption : public CBC_Encryption { public: void SetStolenIV(byte *iv) {m_stolenIV = iv;} @@ -216,7 +227,7 @@ protected: byte *m_stolenIV; }; -class CRYPTOPP_NO_VTABLE CBC_Decryption : public CBC_ModeBase +class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_Decryption : public CBC_ModeBase { public: void ProcessBlocks(byte *outString, const byte *inString, unsigned int numberOfBlocks); @@ -230,7 +241,7 @@ protected: SecByteBlock m_temp; }; -class CRYPTOPP_NO_VTABLE CBC_CTS_Decryption : public CBC_Decryption +class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_CTS_Decryption : public CBC_Decryption { public: unsigned int MinLastBlockSize() const {return BlockSize()+1;} @@ -264,24 +275,32 @@ template class CipherModeFinalTemplate_ExternalCipher : public BASE { public: - CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher) - { - ThrowIfResynchronizable(); - m_cipher = &cipher; - ResizeBuffers(); - } + CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher); - CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher, const byte *iv, int feedbackSize = 0) - { - ThrowIfInvalidIV(iv); - m_cipher = &cipher; - ResizeBuffers(); - SetFeedbackSize(feedbackSize); - if (IsResynchronizable()) - Resynchronize(iv); - } + CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher, const byte *iv, int feedbackSize = 0); }; +template CipherModeFinalTemplate_ExternalCipher::CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher) +{ + ThrowIfResynchronizable(); + m_cipher = &cipher; + ResizeBuffers(); +} + +template CipherModeFinalTemplate_ExternalCipher::CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher, const byte *iv, int feedbackSize) +{ + ThrowIfInvalidIV(iv); + m_cipher = &cipher; + ResizeBuffers(); + SetFeedbackSize(feedbackSize); + if (IsResynchronizable()) + Resynchronize(iv); +} + +CRYPTOPP_DLL_TEMPLATE_CLASS CFB_CipherTemplate >; +CRYPTOPP_DLL_TEMPLATE_CLASS CFB_EncryptionTemplate >; +CRYPTOPP_DLL_TEMPLATE_CLASS CFB_DecryptionTemplate >; + //! CFB mode template struct CFB_Mode : public CipherModeDocumentation @@ -297,6 +316,8 @@ struct CFB_Mode_ExternalCipher : public CipherModeDocumentation typedef CipherModeFinalTemplate_ExternalCipher > > > Decryption; }; +CRYPTOPP_DLL_TEMPLATE_CLASS AdditiveCipherTemplate >; + //! OFB mode template struct OFB_Mode : public CipherModeDocumentation @@ -312,6 +333,8 @@ struct OFB_Mode_ExternalCipher : public CipherModeDocumentation typedef Encryption Decryption; }; +CRYPTOPP_DLL_TEMPLATE_CLASS AdditiveCipherTemplate >; + //! CTR mode template struct CTR_Mode : public CipherModeDocumentation @@ -335,6 +358,8 @@ struct ECB_Mode : public CipherModeDocumentation typedef CipherModeFinalTemplate_CipherHolder Decryption; }; +CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher; + //! ECB mode, external cipher struct ECB_Mode_ExternalCipher : public CipherModeDocumentation { @@ -350,6 +375,9 @@ struct CBC_Mode : public CipherModeDocumentation typedef CipherModeFinalTemplate_CipherHolder Decryption; }; +CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher; +CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher; + //! CBC mode, external cipher struct CBC_Mode_ExternalCipher : public CipherModeDocumentation { @@ -365,6 +393,9 @@ struct CBC_CTS_Mode : public CipherModeDocumentation typedef CipherModeFinalTemplate_CipherHolder Decryption; }; +CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher; +CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher; + //! CBC mode with ciphertext stealing, external cipher struct CBC_CTS_Mode_ExternalCipher : public CipherModeDocumentation { -- cgit v1.2.1