Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
2.7 KiB

  1. // cmac.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #ifndef CRYPTOPP_IMPORTS
  4. #include "cmac.h"
  5. NAMESPACE_BEGIN(CryptoPP)
  6. static void MulU(byte *k, unsigned int length)
  7. {
  8. byte carry = 0;
  9. for (int i=length-1; i>=1; i-=2)
  10. {
  11. byte carry2 = k[i] >> 7;
  12. k[i] += k[i] + carry;
  13. carry = k[i-1] >> 7;
  14. k[i-1] += k[i-1] + carry2;
  15. }
  16. if (carry)
  17. {
  18. switch (length)
  19. {
  20. case 8:
  21. k[7] ^= 0x1b;
  22. break;
  23. case 16:
  24. k[15] ^= 0x87;
  25. break;
  26. case 32:
  27. k[30] ^= 4;
  28. k[31] ^= 0x23;
  29. break;
  30. default:
  31. throw InvalidArgument("CMAC: " + IntToString(length) + " is not a supported cipher block size");
  32. }
  33. }
  34. }
  35. void CMAC_Base::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
  36. {
  37. BlockCipher &cipher = AccessCipher();
  38. unsigned int blockSize = cipher.BlockSize();
  39. cipher.SetKey(key, length, params);
  40. m_reg.CleanNew(3*blockSize);
  41. m_counter = 0;
  42. cipher.ProcessBlock(m_reg, m_reg+blockSize);
  43. MulU(m_reg+blockSize, blockSize);
  44. memcpy(m_reg+2*blockSize, m_reg+blockSize, blockSize);
  45. MulU(m_reg+2*blockSize, blockSize);
  46. }
  47. void CMAC_Base::Update(const byte *input, size_t length)
  48. {
  49. assert((input && length) || !(input || length));
  50. if (!length)
  51. return;
  52. BlockCipher &cipher = AccessCipher();
  53. unsigned int blockSize = cipher.BlockSize();
  54. if (m_counter > 0)
  55. {
  56. const unsigned int len = UnsignedMin(blockSize - m_counter, length);
  57. if (len)
  58. {
  59. xorbuf(m_reg+m_counter, input, len);
  60. length -= len;
  61. input += len;
  62. m_counter += len;
  63. }
  64. if (m_counter == blockSize && length > 0)
  65. {
  66. cipher.ProcessBlock(m_reg);
  67. m_counter = 0;
  68. }
  69. }
  70. if (length > blockSize)
  71. {
  72. assert(m_counter == 0);
  73. size_t leftOver = 1 + cipher.AdvancedProcessBlocks(m_reg, input, m_reg, length-1, BlockTransformation::BT_DontIncrementInOutPointers|BlockTransformation::BT_XorInput);
  74. input += (length - leftOver);
  75. length = leftOver;
  76. }
  77. if (length > 0)
  78. {
  79. assert(m_counter + length <= blockSize);
  80. xorbuf(m_reg+m_counter, input, length);
  81. m_counter += (unsigned int)length;
  82. }
  83. assert(m_counter > 0);
  84. }
  85. void CMAC_Base::TruncatedFinal(byte *mac, size_t size)
  86. {
  87. ThrowIfInvalidTruncatedSize(size);
  88. BlockCipher &cipher = AccessCipher();
  89. unsigned int blockSize = cipher.BlockSize();
  90. if (m_counter < blockSize)
  91. {
  92. m_reg[m_counter] ^= 0x80;
  93. cipher.AdvancedProcessBlocks(m_reg, m_reg+2*blockSize, m_reg, blockSize, BlockTransformation::BT_DontIncrementInOutPointers|BlockTransformation::BT_XorInput);
  94. }
  95. else
  96. cipher.AdvancedProcessBlocks(m_reg, m_reg+blockSize, m_reg, blockSize, BlockTransformation::BT_DontIncrementInOutPointers|BlockTransformation::BT_XorInput);
  97. memcpy(mac, m_reg, size);
  98. m_counter = 0;
  99. memset(m_reg, 0, blockSize);
  100. }
  101. NAMESPACE_END
  102. #endif