Counter Strike : Global Offensive Source Code
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.

122 lines
2.6 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. if (!length)
  50. return;
  51. BlockCipher &cipher = AccessCipher();
  52. unsigned int blockSize = cipher.BlockSize();
  53. if (m_counter > 0)
  54. {
  55. unsigned int len = UnsignedMin(blockSize - m_counter, length);
  56. xorbuf(m_reg+m_counter, input, len);
  57. length -= len;
  58. input += len;
  59. m_counter += len;
  60. if (m_counter == blockSize && length > 0)
  61. {
  62. cipher.ProcessBlock(m_reg);
  63. m_counter = 0;
  64. }
  65. }
  66. if (length > blockSize)
  67. {
  68. assert(m_counter == 0);
  69. size_t leftOver = 1 + cipher.AdvancedProcessBlocks(m_reg, input, m_reg, length-1, BlockTransformation::BT_DontIncrementInOutPointers|BlockTransformation::BT_XorInput);
  70. input += (length - leftOver);
  71. length = leftOver;
  72. }
  73. if (length > 0)
  74. {
  75. assert(m_counter + length <= blockSize);
  76. xorbuf(m_reg+m_counter, input, length);
  77. m_counter += (unsigned int)length;
  78. }
  79. assert(m_counter > 0);
  80. }
  81. void CMAC_Base::TruncatedFinal(byte *mac, size_t size)
  82. {
  83. ThrowIfInvalidTruncatedSize(size);
  84. BlockCipher &cipher = AccessCipher();
  85. unsigned int blockSize = cipher.BlockSize();
  86. if (m_counter < blockSize)
  87. {
  88. m_reg[m_counter] ^= 0x80;
  89. cipher.AdvancedProcessBlocks(m_reg, m_reg+2*blockSize, m_reg, blockSize, BlockTransformation::BT_DontIncrementInOutPointers|BlockTransformation::BT_XorInput);
  90. }
  91. else
  92. cipher.AdvancedProcessBlocks(m_reg, m_reg+blockSize, m_reg, blockSize, BlockTransformation::BT_DontIncrementInOutPointers|BlockTransformation::BT_XorInput);
  93. memcpy(mac, m_reg, size);
  94. m_counter = 0;
  95. memset(m_reg, 0, blockSize);
  96. }
  97. NAMESPACE_END
  98. #endif