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 --- cbcmac.h | 73 +++++++++++----------------------------------------------------- 1 file changed, 12 insertions(+), 61 deletions(-) (limited to 'cbcmac.h') diff --git a/cbcmac.h b/cbcmac.h index 7f88e91..b15c728 100644 --- a/cbcmac.h +++ b/cbcmac.h @@ -6,22 +6,21 @@ NAMESPACE_BEGIN(CryptoPP) -template -class CRYPTOPP_NO_VTABLE CBC_MAC_Base : public SameKeyLengthAs, public MessageAuthenticationCode +class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_MAC_Base : public MessageAuthenticationCode { public: - static std::string StaticAlgorithmName() {return std::string("CBC-MAC(") + T::StaticAlgorithmName() + ")";} - CBC_MAC_Base() {} void CheckedSetKey(void *, Empty empty, const byte *key, unsigned int length, const NameValuePairs ¶ms); void Update(const byte *input, unsigned int length); void TruncatedFinal(byte *mac, unsigned int size); - unsigned int DigestSize() const {return m_cipher.BlockSize();} + unsigned int DigestSize() const {return const_cast(this)->AccessCipher().BlockSize();} + +protected: + virtual BlockCipher & AccessCipher() =0; private: void ProcessBuf(); - typename T::Encryption m_cipher; SecByteBlock m_reg; unsigned int m_counter; }; @@ -32,67 +31,19 @@ private: messages use DMAC. */ template -class CBC_MAC : public MessageAuthenticationCodeTemplate > +class CBC_MAC : public MessageAuthenticationCodeImpl >, public SameKeyLengthAs { public: CBC_MAC() {} - CBC_MAC(const byte *key, unsigned int length=CBC_MAC_Base::DEFAULT_KEYLENGTH) + CBC_MAC(const byte *key, unsigned int length=DEFAULT_KEYLENGTH) {SetKey(key, length);} -}; - -template -void CBC_MAC_Base::CheckedSetKey(void *, Empty empty, const byte *key, unsigned int length, const NameValuePairs ¶ms) -{ - m_cipher.SetKey(key, length, params); - m_reg.CleanNew(m_cipher.BlockSize()); - m_counter = 0; -} - -template -void CBC_MAC_Base::Update(const byte *input, unsigned int length) -{ - while (m_counter && length) - { - m_reg[m_counter++] ^= *input++; - if (m_counter == T::BLOCKSIZE) - ProcessBuf(); - length--; - } - while (length >= T::BLOCKSIZE) - { - xorbuf(m_reg, input, T::BLOCKSIZE); - ProcessBuf(); - input += T::BLOCKSIZE; - length -= T::BLOCKSIZE; - } - - while (length--) - { - m_reg[m_counter++] ^= *input++; - if (m_counter == T::BLOCKSIZE) - ProcessBuf(); - } -} - -template -void CBC_MAC_Base::TruncatedFinal(byte *mac, unsigned int size) -{ - ThrowIfInvalidTruncatedSize(size); - - if (m_counter) - ProcessBuf(); - - memcpy(mac, m_reg, size); - memset(m_reg, 0, T::BLOCKSIZE); -} + static std::string StaticAlgorithmName() {return std::string("CBC-MAC(") + T::StaticAlgorithmName() + ")";} -template -void CBC_MAC_Base::ProcessBuf() -{ - m_cipher.ProcessBlock(m_reg); - m_counter = 0; -} +private: + BlockCipher & AccessCipher() {return m_cipher;} + typename T::Encryption m_cipher; +}; NAMESPACE_END -- cgit v1.2.1