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.

140 lines
3.7 KiB

  1. // shacal2.cpp - by Kevin Springle, 2003
  2. //
  3. // Portions of this code were derived from
  4. // Wei Dai's implementation of SHA-2
  5. //
  6. // The original code and all modifications are in the public domain.
  7. #include "pch.h"
  8. #include "shacal2.h"
  9. #include "misc.h"
  10. NAMESPACE_BEGIN(CryptoPP)
  11. // SHACAL-2 function and round definitions
  12. #define S0(x) (rotrFixed(x,2)^rotrFixed(x,13)^rotrFixed(x,22))
  13. #define S1(x) (rotrFixed(x,6)^rotrFixed(x,11)^rotrFixed(x,25))
  14. #define s0(x) (rotrFixed(x,7)^rotrFixed(x,18)^(x>>3))
  15. #define s1(x) (rotrFixed(x,17)^rotrFixed(x,19)^(x>>10))
  16. #define Ch(x,y,z) (z^(x&(y^z)))
  17. #define Maj(x,y,z) ((x&y)|(z&(x|y)))
  18. /* R is the SHA-256 round function. */
  19. /* This macro increments the k argument as a side effect. */
  20. #define R(a,b,c,d,e,f,g,h,k) \
  21. h+=S1(e)+Ch(e,f,g)+*k++;d+=h;h+=S0(a)+Maj(a,b,c);
  22. /* P is the inverse of the SHA-256 round function. */
  23. /* This macro decrements the k argument as a side effect. */
  24. #define P(a,b,c,d,e,f,g,h,k) \
  25. h-=S0(a)+Maj(a,b,c);d-=h;h-=S1(e)+Ch(e,f,g)+*--k;
  26. void SHACAL2::Base::UncheckedSetKey(const byte *userKey, unsigned int keylen, const NameValuePairs &)
  27. {
  28. AssertValidKeyLength(keylen);
  29. word32 *rk = m_key;
  30. unsigned int i;
  31. GetUserKey(BIG_ENDIAN_ORDER, rk, m_key.size(), userKey, keylen);
  32. for (i = 0; i < 48; i++, rk++)
  33. {
  34. rk[16] = rk[0] + s0(rk[1]) + rk[9] + s1(rk[14]);
  35. rk[0] += K[i];
  36. }
  37. for (i = 48; i < 64; i++, rk++)
  38. {
  39. rk[0] += K[i];
  40. }
  41. }
  42. typedef BlockGetAndPut<word32, BigEndian> Block;
  43. void SHACAL2::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  44. {
  45. word32 a, b, c, d, e, f, g, h;
  46. const word32 *rk = m_key;
  47. /*
  48. * map byte array block to cipher state:
  49. */
  50. Block::Get(inBlock)(a)(b)(c)(d)(e)(f)(g)(h);
  51. // Perform SHA-256 transformation.
  52. /* 64 operations, partially loop unrolled */
  53. for (unsigned int j=0; j<64; j+=8)
  54. {
  55. R(a,b,c,d,e,f,g,h,rk);
  56. R(h,a,b,c,d,e,f,g,rk);
  57. R(g,h,a,b,c,d,e,f,rk);
  58. R(f,g,h,a,b,c,d,e,rk);
  59. R(e,f,g,h,a,b,c,d,rk);
  60. R(d,e,f,g,h,a,b,c,rk);
  61. R(c,d,e,f,g,h,a,b,rk);
  62. R(b,c,d,e,f,g,h,a,rk);
  63. }
  64. /*
  65. * map cipher state to byte array block:
  66. */
  67. Block::Put(xorBlock, outBlock)(a)(b)(c)(d)(e)(f)(g)(h);
  68. }
  69. void SHACAL2::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  70. {
  71. word32 a, b, c, d, e, f, g, h;
  72. const word32 *rk = m_key + 64;
  73. /*
  74. * map byte array block to cipher state:
  75. */
  76. Block::Get(inBlock)(a)(b)(c)(d)(e)(f)(g)(h);
  77. // Perform inverse SHA-256 transformation.
  78. /* 64 operations, partially loop unrolled */
  79. for (unsigned int j=0; j<64; j+=8)
  80. {
  81. P(b,c,d,e,f,g,h,a,rk);
  82. P(c,d,e,f,g,h,a,b,rk);
  83. P(d,e,f,g,h,a,b,c,rk);
  84. P(e,f,g,h,a,b,c,d,rk);
  85. P(f,g,h,a,b,c,d,e,rk);
  86. P(g,h,a,b,c,d,e,f,rk);
  87. P(h,a,b,c,d,e,f,g,rk);
  88. P(a,b,c,d,e,f,g,h,rk);
  89. }
  90. /*
  91. * map cipher state to byte array block:
  92. */
  93. Block::Put(xorBlock, outBlock)(a)(b)(c)(d)(e)(f)(g)(h);
  94. }
  95. // The SHACAL-2 round constants are identical to the SHA-256 round constants.
  96. const word32 SHACAL2::Base::K[64] =
  97. {
  98. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  99. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  100. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  101. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  102. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  103. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  104. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  105. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  106. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  107. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  108. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  109. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  110. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  111. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  112. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  113. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  114. };
  115. NAMESPACE_END