5 #ifndef CRYPTOPP_IMPORTS
9 NAMESPACE_BEGIN(CryptoPP)
11 static
void MulU(byte *k,
unsigned int length)
15 for (
int i=length-1; i>=1; i-=2)
17 byte carry2 = k[i] >> 7;
20 k[i-1] += k[i-1] + carry2;
38 throw InvalidArgument(
"CMAC: " + IntToString(length) +
" is not a supported cipher block size");
43 void CMAC_Base::UncheckedSetKey(
const byte *key,
unsigned int length,
const NameValuePairs ¶ms)
46 unsigned int blockSize = cipher.
BlockSize();
48 cipher.
SetKey(key, length, params);
53 MulU(m_reg+blockSize, blockSize);
54 memcpy(m_reg+2*blockSize, m_reg+blockSize, blockSize);
55 MulU(m_reg+2*blockSize, blockSize);
64 unsigned int blockSize = cipher.
BlockSize();
68 unsigned int len = UnsignedMin(blockSize - m_counter, length);
69 xorbuf(m_reg+m_counter, input, len);
74 if (m_counter == blockSize && length > 0)
81 if (length > blockSize)
83 assert(m_counter == 0);
84 size_t leftOver = 1 + cipher.
AdvancedProcessBlocks(m_reg, input, m_reg, length-1, BlockTransformation::BT_DontIncrementInOutPointers|BlockTransformation::BT_XorInput);
85 input += (length - leftOver);
91 assert(m_counter + length <= blockSize);
92 xorbuf(m_reg+m_counter, input, length);
93 m_counter += (
unsigned int)length;
96 assert(m_counter > 0);
101 ThrowIfInvalidTruncatedSize(size);
104 unsigned int blockSize = cipher.
BlockSize();
106 if (m_counter < blockSize)
108 m_reg[m_counter] ^= 0x80;
109 cipher.
AdvancedProcessBlocks(m_reg, m_reg+2*blockSize, m_reg, blockSize, BlockTransformation::BT_DontIncrementInOutPointers|BlockTransformation::BT_XorInput);
112 cipher.
AdvancedProcessBlocks(m_reg, m_reg+blockSize, m_reg, blockSize, BlockTransformation::BT_DontIncrementInOutPointers|BlockTransformation::BT_XorInput);
114 memcpy(mac, m_reg, size);
117 memset(m_reg, 0, blockSize);