File indexing completed on 2025-01-18 09:54:56
0001
0002
0003
0004
0005
0006 #ifndef CRYPTOPP_DMAC_H
0007 #define CRYPTOPP_DMAC_H
0008
0009 #include "cbcmac.h"
0010
0011 NAMESPACE_BEGIN(CryptoPP)
0012
0013
0014
0015
0016 template <class T>
0017 class CRYPTOPP_NO_VTABLE DMAC_Base : public SameKeyLengthAs<T>, public MessageAuthenticationCode
0018 {
0019 public:
0020 CRYPTOPP_CONSTANT(DIGESTSIZE=T::BLOCKSIZE);
0021 static std::string StaticAlgorithmName() {return std::string("DMAC(") + T::StaticAlgorithmName() + ")";}
0022
0023 virtual~DMAC_Base() {}
0024 DMAC_Base() : m_subkeylength(0), m_counter(0) {}
0025
0026 void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms);
0027 void Update(const byte *input, size_t length);
0028 void TruncatedFinal(byte *mac, size_t size);
0029 unsigned int DigestSize() const {return DIGESTSIZE;}
0030
0031 std::string AlgorithmProvider() const;
0032
0033 private:
0034 byte *GenerateSubKeys(const byte *key, size_t keylength);
0035
0036 size_t m_subkeylength;
0037 SecByteBlock m_subkeys;
0038 CBC_MAC<T> m_mac1;
0039 typename T::Encryption m_f2;
0040 unsigned int m_counter;
0041 };
0042
0043 template <class T>
0044 std::string DMAC_Base<T>::AlgorithmProvider() const
0045 {
0046 return m_f2.AlgorithmProvider();
0047 }
0048
0049
0050
0051
0052
0053
0054 template <class T>
0055 class DMAC : public MessageAuthenticationCodeFinal<DMAC_Base<T> >
0056 {
0057 public:
0058
0059 DMAC() {}
0060
0061
0062
0063
0064 DMAC(const byte *key, size_t length=DMAC_Base<T>::DEFAULT_KEYLENGTH)
0065 {this->SetKey(key, length);}
0066 };
0067
0068 template <class T>
0069 void DMAC_Base<T>::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms)
0070 {
0071 m_subkeylength = T::StaticGetValidKeyLength(T::BLOCKSIZE);
0072 m_subkeys.resize(2*UnsignedMin((unsigned int)T::BLOCKSIZE, m_subkeylength));
0073 m_mac1.SetKey(GenerateSubKeys(key, length), m_subkeylength, params);
0074 m_f2.SetKey(m_subkeys+m_subkeys.size()/2, m_subkeylength, params);
0075 m_counter = 0;
0076 m_subkeys.resize(0);
0077 }
0078
0079 template <class T>
0080 void DMAC_Base<T>::Update(const byte *input, size_t length)
0081 {
0082 m_mac1.Update(input, length);
0083 m_counter = (unsigned int)((m_counter + length) % T::BLOCKSIZE);
0084 }
0085
0086 template <class T>
0087 void DMAC_Base<T>::TruncatedFinal(byte *mac, size_t size)
0088 {
0089 ThrowIfInvalidTruncatedSize(size);
0090
0091 byte pad[T::BLOCKSIZE];
0092 byte padByte = byte(T::BLOCKSIZE-m_counter);
0093 std::memset(pad, padByte, padByte);
0094 m_mac1.Update(pad, padByte);
0095 m_mac1.TruncatedFinal(mac, size);
0096 m_f2.ProcessBlock(mac);
0097
0098 m_counter = 0;
0099 }
0100
0101 template <class T>
0102 byte *DMAC_Base<T>::GenerateSubKeys(const byte *key, size_t keylength)
0103 {
0104 typename T::Encryption cipher(key, keylength);
0105 std::memset(m_subkeys, 0, m_subkeys.size());
0106 cipher.ProcessBlock(m_subkeys);
0107 m_subkeys[m_subkeys.size()/2 + T::BLOCKSIZE - 1] = 1;
0108 cipher.ProcessBlock(m_subkeys+m_subkeys.size()/2);
0109 return m_subkeys;
0110 }
0111
0112 NAMESPACE_END
0113
0114 #endif