Guest User

Untitled

a guest
Jul 17th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.89 KB | None | 0 0
  1. // Crypto/Sha1.cpp
  2. // This file is based on public domain
  3. // Steve Reid and Wei Dai's code from Crypto++
  4.  
  5. #include "StdAfx.h"
  6.  
  7. #include "../../../C/RotateDefs.h"
  8.  
  9. #include "Sha1.h"
  10.  
  11. namespace NCrypto {
  12. namespace NSha1 {
  13.  
  14. // define it for speed optimization
  15. // #define _SHA1_UNROLL
  16.  
  17. static const unsigned kNumW =
  18.   #ifdef _SHA1_UNROLL
  19.   16;
  20.   #else
  21.   80;
  22.   #endif
  23.  
  24.  
  25. #define w0(i) (W[(i)] = data[(i)])
  26.  
  27. #ifdef _SHA1_UNROLL
  28. #define w1(i) (W[(i)&15] = rotlFixed(W[((i)-3)&15] ^ W[((i)-8)&15] ^ W[((i)-14)&15] ^ W[((i)-16)&15], 1))
  29. #else
  30. #define w1(i) (W[(i)] = rotlFixed(W[(i)-3] ^ W[(i)-8] ^ W[(i)-14] ^ W[(i)-16], 1))
  31. #endif
  32.  
  33. #define f1(x,y,z) (z^(x&(y^z)))
  34. #define f2(x,y,z) (x^y^z)
  35. #define f3(x,y,z) ((x&y)|(z&(x|y)))
  36. #define f4(x,y,z) (x^y^z)
  37.  
  38. #define RK1(a,b,c,d,e,i, f, w, k) e += f(b,c,d) + w(i) + k + rotlFixed(a,5); b = rotlFixed(b,30);
  39.  
  40. #define R0(a,b,c,d,e,i) RK1(a,b,c,d,e,i, f1, w0, 0x5A827999)
  41. #define R1(a,b,c,d,e,i) RK1(a,b,c,d,e,i, f1, w1, 0x5A827999)
  42. #define R2(a,b,c,d,e,i) RK1(a,b,c,d,e,i, f2, w1, 0x6ED9EBA1)
  43. #define R3(a,b,c,d,e,i) RK1(a,b,c,d,e,i, f3, w1, 0x8F1BBCDC)
  44. #define R4(a,b,c,d,e,i) RK1(a,b,c,d,e,i, f4, w1, 0xCA62C1D6)
  45.  
  46. #define RX_1_4(rx1, rx4, i) rx1(a,b,c,d,e,i); rx4(e,a,b,c,d,i+1); rx4(d,e,a,b,c,i+2); rx4(c,d,e,a,b,i+3); rx4(b,c,d,e,a,i+4);
  47. #define RX_5(rx, i) RX_1_4(rx, rx, i);
  48.  
  49. void CContextBase::Init()
  50. {
  51.   _state[0] = 0x67452301;
  52.   _state[1] = 0xEFCDAB89;
  53.   _state[2] = 0x98BADCFE;
  54.   _state[3] = 0x10325476;
  55.   _state[4] = 0xC3D2E1F0;
  56.   _count = 0;
  57. }
  58.  
  59. void CContextBase::GetBlockDigest(UInt32 *data, UInt32 *destDigest, bool returnRes)
  60. {
  61.   UInt32 a, b, c, d, e;
  62.   UInt32 W[kNumW];
  63.  
  64.   a = _state[0];
  65.   b = _state[1];
  66.   c = _state[2];
  67.   d = _state[3];
  68.   e = _state[4];
  69.   #ifdef _SHA1_UNROLL
  70.   RX_5(R0, 0); RX_5(R0, 5); RX_5(R0, 10);
  71.   #else
  72.   int i;
  73.   for (i = 0; i < 15; i += 5) { RX_5(R0, i); }
  74.   #endif
  75.  
  76.   RX_1_4(R0, R1, 15);
  77.  
  78.  
  79.   #ifdef _SHA1_UNROLL
  80.   RX_5(R2, 20); RX_5(R2, 25); RX_5(R2, 30); RX_5(R2, 35);
  81.   RX_5(R3, 40); RX_5(R3, 45); RX_5(R3, 50); RX_5(R3, 55);
  82.   RX_5(R4, 60); RX_5(R4, 65); RX_5(R4, 70); RX_5(R4, 75);
  83.   #else
  84.   i = 20;
  85.   for (; i < 40; i += 5) { RX_5(R2, i); }
  86.   for (; i < 60; i += 5) { RX_5(R3, i); }
  87.   for (; i < 80; i += 5) { RX_5(R4, i); }
  88.   #endif
  89.  
  90.   destDigest[0] = _state[0] + a;
  91.   destDigest[1] = _state[1] + b;
  92.   destDigest[2] = _state[2] + c;
  93.   destDigest[3] = _state[3] + d;
  94.   destDigest[4] = _state[4] + e;
  95.  
  96.   if (returnRes)
  97.     for (int i = 0 ; i < 16; i++)
  98.       data[i] = W[kNumW - 16 + i];
  99.  
  100.   // Wipe variables
  101.   // a = b = c = d = e = 0;
  102. }
  103.  
  104. void CContextBase::PrepareBlock(UInt32 *block, unsigned size) const
  105. {
  106.   unsigned curBufferPos = size & 0xF;
  107.   block[curBufferPos++] = 0x80000000;
  108.   while (curBufferPos != (16 - 2))
  109.     block[curBufferPos++] = 0;
  110.   const UInt64 lenInBits = (_count << 9) + ((UInt64)size << 5);
  111.   block[curBufferPos++] = (UInt32)(lenInBits >> 32);
  112.   block[curBufferPos++] = (UInt32)(lenInBits);
  113. }
  114.  
  115. void CContext::Update(const Byte *data, size_t size)
  116. {
  117.   unsigned curBufferPos = _count2;
  118.   while (size--)
  119.   {
  120.     int pos = (int)(curBufferPos & 3);
  121.     if (pos == 0)
  122.       _buffer[curBufferPos >> 2] = 0;
  123.     _buffer[curBufferPos >> 2] |= ((UInt32)*data++) << (8 * (3 - pos));
  124.     if (++curBufferPos == kBlockSize)
  125.     {
  126.       curBufferPos = 0;
  127.       CContextBase::UpdateBlock(_buffer, false);
  128.     }
  129.   }
  130.   _count2 = curBufferPos;
  131. }
  132.  
  133. void CContext::UpdateRar(Byte *data, size_t size, bool rar350Mode)
  134. {
  135.  
  136.   bool returnRes = false;  
  137.   fflush(stdout);
  138.   unsigned curBufferPos = _count2;
  139.   while (size--)
  140.   {
  141.  
  142.  
  143.     int pos = (int)(curBufferPos & 3);
  144.        
  145.  
  146.     fflush(stdout);
  147.     if (pos == 0)
  148.       _buffer[curBufferPos >> 2] = 0;
  149.     _buffer[curBufferPos >> 2] |= ((UInt32)*data++) << (8 * (3 - pos));
  150.     if (++curBufferPos == kBlockSize)
  151.     {
  152.        
  153.       curBufferPos = 0;
  154. CContextBase::UpdateBlock(_buffer, returnRes);
  155. if (returnRes)
  156. for (unsigned i = 0; i < kBlockSizeInWords; i++)
  157. {
  158. UInt32 d = _buffer[i];
  159. data[(int)i * 4 + 0 - (int)kBlockSize] = (Byte)(d);
  160. data[(int)i * 4 + 1 - (int)kBlockSize] = (Byte)(d >> 8);
  161. data[(int)i * 4 + 2 - (int)kBlockSize] = (Byte)(d >> 16);
  162. data[(int)i * 4 + 3 - (int)kBlockSize] = (Byte)(d >> 24);
  163. }
  164.       returnRes = rar350Mode;
  165.     }
  166.   }
  167.   _count2 = curBufferPos;
  168. }
  169.  
  170. void CContext::Final(Byte *digest)
  171. {
  172.   const UInt64 lenInBits = (_count << 9) + ((UInt64)_count2 << 3);
  173.   unsigned curBufferPos = _count2;
  174.   int pos = (int)(curBufferPos & 3);
  175.   curBufferPos >>= 2;
  176.   if (pos == 0)
  177.     _buffer[curBufferPos] = 0;
  178.   _buffer[curBufferPos++] |= ((UInt32)0x80) << (8 * (3 - pos));
  179.  
  180.   while (curBufferPos != (16 - 2))
  181.   {
  182.     curBufferPos &= 0xF;
  183.     if (curBufferPos == 0)
  184.       UpdateBlock();
  185.     _buffer[curBufferPos++] = 0;
  186.   }
  187.   _buffer[curBufferPos++] = (UInt32)(lenInBits >> 32);
  188.   _buffer[curBufferPos++] = (UInt32)(lenInBits);
  189.   UpdateBlock();
  190.  
  191.   int i;
  192.   for (i = 0; i < kDigestSizeInWords; i++)
  193.   {
  194.     UInt32 state = _state[i] & 0xFFFFFFFF;
  195.     *digest++ = (Byte)(state >> 24);
  196.     *digest++ = (Byte)(state >> 16);
  197.     *digest++ = (Byte)(state >> 8);
  198.     *digest++ = (Byte)(state);
  199.   }
  200.   Init();
  201. }
  202.  
  203. ///////////////////////////
  204. // Words version
  205.  
  206. void CContext32::Update(const UInt32 *data, size_t size)
  207. {
  208.   while (size--)
  209.   {
  210.     _buffer[_count2++] = *data++;
  211.     if (_count2 == kBlockSizeInWords)
  212.     {
  213.       _count2 = 0;
  214.       UpdateBlock();
  215.     }
  216.   }
  217. }
  218.  
  219. void CContext32::Final(UInt32 *digest)
  220. {
  221.   const UInt64 lenInBits = (_count << 9) + ((UInt64)_count2 << 5);
  222.   unsigned curBufferPos = _count2;
  223.   _buffer[curBufferPos++] = 0x80000000;
  224.   while (curBufferPos != (16 - 2))
  225.   {
  226.     curBufferPos &= 0xF;
  227.     if (curBufferPos == 0)
  228.       UpdateBlock();
  229.     _buffer[curBufferPos++] = 0;
  230.   }
  231.   _buffer[curBufferPos++] = (UInt32)(lenInBits >> 32);
  232.   _buffer[curBufferPos++] = (UInt32)(lenInBits);
  233.   GetBlockDigest(_buffer, digest);
  234.   Init();
  235. }
  236.  
  237. }}
Add Comment
Please, Sign In to add comment