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.

120 lines
2.9 KiB

  1. // md2.cpp - modified by Wei Dai from Andrew M. Kuchling's md2.c
  2. // The original code and all modifications are in the public domain.
  3. // This is the original introductory comment:
  4. /*
  5. * md2.c : MD2 hash algorithm.
  6. *
  7. * Part of the Python Cryptography Toolkit, version 1.1
  8. *
  9. * Distribute and use freely; there are no restrictions on further
  10. * dissemination and usage except those imposed by the laws of your
  11. * country of residence.
  12. *
  13. */
  14. #include "pch.h"
  15. #define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
  16. #include "md2.h"
  17. NAMESPACE_BEGIN(CryptoPP)
  18. namespace Weak1 {
  19. MD2::MD2()
  20. : m_X(48), m_C(16), m_buf(16)
  21. {
  22. Init();
  23. }
  24. void MD2::Init()
  25. {
  26. memset(m_X, 0, 48);
  27. memset(m_C, 0, 16);
  28. memset(m_buf, 0, 16);
  29. m_count = 0;
  30. }
  31. void MD2::Update(const byte *buf, size_t len)
  32. {
  33. static const byte S[256] = {
  34. 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
  35. 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
  36. 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
  37. 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
  38. 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
  39. 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
  40. 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
  41. 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
  42. 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
  43. 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
  44. 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
  45. 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
  46. 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
  47. 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
  48. 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
  49. 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
  50. 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
  51. 31, 26, 219, 153, 141, 51, 159, 17, 131, 20
  52. };
  53. while (len)
  54. {
  55. unsigned int L = UnsignedMin(16U-m_count, len);
  56. memcpy(m_buf+m_count, buf, L);
  57. m_count+=L;
  58. buf+=L;
  59. len-=L;
  60. if (m_count==16)
  61. {
  62. byte t;
  63. int i,j;
  64. m_count=0;
  65. memcpy(m_X+16, m_buf, 16);
  66. t=m_C[15];
  67. for(i=0; i<16; i++)
  68. {
  69. m_X[32+i]=m_X[16+i]^m_X[i];
  70. t=m_C[i]^=S[m_buf[i]^t];
  71. }
  72. t=0;
  73. for(i=0; i<18; i++)
  74. {
  75. for(j=0; j<48; j+=8)
  76. {
  77. t=m_X[j+0]^=S[t];
  78. t=m_X[j+1]^=S[t];
  79. t=m_X[j+2]^=S[t];
  80. t=m_X[j+3]^=S[t];
  81. t=m_X[j+4]^=S[t];
  82. t=m_X[j+5]^=S[t];
  83. t=m_X[j+6]^=S[t];
  84. t=m_X[j+7]^=S[t];
  85. }
  86. t=(t+i) & 0xFF;
  87. }
  88. }
  89. }
  90. }
  91. void MD2::TruncatedFinal(byte *hash, size_t size)
  92. {
  93. ThrowIfInvalidTruncatedSize(size);
  94. byte padding[16];
  95. word32 padlen;
  96. unsigned int i;
  97. padlen= 16-m_count;
  98. for(i=0; i<padlen; i++) padding[i]=(byte)padlen;
  99. Update(padding, padlen);
  100. Update(m_C, 16);
  101. memcpy(hash, m_X, size);
  102. Init();
  103. }
  104. }
  105. NAMESPACE_END