diff options
author | weidai <weidai11@users.noreply.github.com> | 2003-07-04 00:17:37 +0000 |
---|---|---|
committer | weidai <weidai11@users.noreply.github.com> | 2003-07-04 00:17:37 +0000 |
commit | f278895908e663a6a5a2c1f63e5523c5004f5d20 (patch) | |
tree | 0536d87e504a82920156c239bc5ae6aa43e70ebc /cbcmac.h | |
parent | e43f74604744291d3a99b8bfe81d94af4ba6abbd (diff) | |
download | cryptopp-git-f278895908e663a6a5a2c1f63e5523c5004f5d20.tar.gz |
create DLL version, fix GetNextIV() bug in CTR and OFB modes
Diffstat (limited to 'cbcmac.h')
-rw-r--r-- | cbcmac.h | 73 |
1 files changed, 12 insertions, 61 deletions
@@ -6,22 +6,21 @@ NAMESPACE_BEGIN(CryptoPP) -template <class T> -class CRYPTOPP_NO_VTABLE CBC_MAC_Base : public SameKeyLengthAs<T>, 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<CBC_MAC_Base*>(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 T> -class CBC_MAC : public MessageAuthenticationCodeTemplate<CBC_MAC_Base<T> > +class CBC_MAC : public MessageAuthenticationCodeImpl<CBC_MAC_Base, CBC_MAC<T> >, public SameKeyLengthAs<T> { public: CBC_MAC() {} - CBC_MAC(const byte *key, unsigned int length=CBC_MAC_Base<T>::DEFAULT_KEYLENGTH) + CBC_MAC(const byte *key, unsigned int length=DEFAULT_KEYLENGTH) {SetKey(key, length);} -}; - -template <class T> -void CBC_MAC_Base<T>::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 <class T> -void CBC_MAC_Base<T>::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 <class T> -void CBC_MAC_Base<T>::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 <class T> -void CBC_MAC_Base<T>::ProcessBuf() -{ - m_cipher.ProcessBlock(m_reg); - m_counter = 0; -} +private: + BlockCipher & AccessCipher() {return m_cipher;} + typename T::Encryption m_cipher; +}; NAMESPACE_END |