diff src/win32/7zip/7z/CPP/7zip/Crypto/Sha1.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/Sha1.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,213 @@
     1.4 +// Crypto/Sha1.cpp
     1.5 +// This file is based on public domain
     1.6 +// Steve Reid and Wei Dai's code from Crypto++
     1.7 +
     1.8 +#include "StdAfx.h"
     1.9 +
    1.10 +#include "Sha1.h"
    1.11 +extern "C"
    1.12 +{
    1.13 +#include "../../../C/RotateDefs.h"
    1.14 +}
    1.15 +
    1.16 +namespace NCrypto {
    1.17 +namespace NSha1 {
    1.18 +
    1.19 +// define it for speed optimization
    1.20 +// #define _SHA1_UNROLL
    1.21 +
    1.22 +static const unsigned kNumW =
    1.23 +  #ifdef _SHA1_UNROLL
    1.24 +  16;
    1.25 +  #else
    1.26 +  80;
    1.27 +  #endif
    1.28 +  
    1.29 +
    1.30 +#define w0(i) (W[(i)] = data[(i)])
    1.31 +
    1.32 +#ifdef _SHA1_UNROLL
    1.33 +#define w1(i) (W[(i)&15] = rotlFixed(W[((i)-3)&15] ^ W[((i)-8)&15] ^ W[((i)-14)&15] ^ W[((i)-16)&15], 1))
    1.34 +#else
    1.35 +#define w1(i) (W[(i)] = rotlFixed(W[(i)-3] ^ W[(i)-8] ^ W[(i)-14] ^ W[(i)-16], 1))
    1.36 +#endif
    1.37 +
    1.38 +#define f1(x,y,z) (z^(x&(y^z)))
    1.39 +#define f2(x,y,z) (x^y^z)
    1.40 +#define f3(x,y,z) ((x&y)|(z&(x|y)))
    1.41 +#define f4(x,y,z) (x^y^z)
    1.42 +
    1.43 +#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);
    1.44 +
    1.45 +#define R0(a,b,c,d,e,i) RK1(a,b,c,d,e,i, f1, w0, 0x5A827999)
    1.46 +#define R1(a,b,c,d,e,i) RK1(a,b,c,d,e,i, f1, w1, 0x5A827999)
    1.47 +#define R2(a,b,c,d,e,i) RK1(a,b,c,d,e,i, f2, w1, 0x6ED9EBA1)
    1.48 +#define R3(a,b,c,d,e,i) RK1(a,b,c,d,e,i, f3, w1, 0x8F1BBCDC)
    1.49 +#define R4(a,b,c,d,e,i) RK1(a,b,c,d,e,i, f4, w1, 0xCA62C1D6)
    1.50 +
    1.51 +#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);
    1.52 +#define RX_5(rx, i) RX_1_4(rx, rx, i);
    1.53 +
    1.54 +void CContextBase::Init()
    1.55 +{
    1.56 +  _state[0] = 0x67452301;
    1.57 +  _state[1] = 0xEFCDAB89;
    1.58 +  _state[2] = 0x98BADCFE;
    1.59 +  _state[3] = 0x10325476;
    1.60 +  _state[4] = 0xC3D2E1F0;
    1.61 +  _count = 0;
    1.62 +}
    1.63 +
    1.64 +void CContextBase::GetBlockDigest(UInt32 *data, UInt32 *destDigest, bool returnRes)
    1.65 +{
    1.66 +  UInt32 a, b, c, d, e;
    1.67 +  UInt32 W[kNumW];
    1.68 +
    1.69 +  a = _state[0];
    1.70 +  b = _state[1];
    1.71 +  c = _state[2];
    1.72 +  d = _state[3];
    1.73 +  e = _state[4];
    1.74 +  #ifdef _SHA1_UNROLL
    1.75 +  RX_5(R0, 0); RX_5(R0, 5); RX_5(R0, 10);
    1.76 +  #else
    1.77 +  int i;
    1.78 +  for (i = 0; i < 15; i += 5) { RX_5(R0, i); }
    1.79 +  #endif
    1.80 +
    1.81 +  RX_1_4(R0, R1, 15);
    1.82 +
    1.83 +
    1.84 +  #ifdef _SHA1_UNROLL
    1.85 +  RX_5(R2, 20); RX_5(R2, 25); RX_5(R2, 30); RX_5(R2, 35);
    1.86 +  RX_5(R3, 40); RX_5(R3, 45); RX_5(R3, 50); RX_5(R3, 55);
    1.87 +  RX_5(R4, 60); RX_5(R4, 65); RX_5(R4, 70); RX_5(R4, 75);
    1.88 +  #else
    1.89 +  i = 20;
    1.90 +  for (; i < 40; i += 5) { RX_5(R2, i); }
    1.91 +  for (; i < 60; i += 5) { RX_5(R3, i); }
    1.92 +  for (; i < 80; i += 5) { RX_5(R4, i); }
    1.93 +  #endif
    1.94 +
    1.95 +  destDigest[0] = _state[0] + a;
    1.96 +  destDigest[1] = _state[1] + b;
    1.97 +  destDigest[2] = _state[2] + c;
    1.98 +  destDigest[3] = _state[3] + d;
    1.99 +  destDigest[4] = _state[4] + e;
   1.100 +
   1.101 +  if (returnRes)
   1.102 +    for (int i = 0 ; i < 16; i++)
   1.103 +      data[i] = W[kNumW - 16 + i];
   1.104 +  
   1.105 +  // Wipe variables
   1.106 +  // a = b = c = d = e = 0;
   1.107 +}
   1.108 +
   1.109 +void CContextBase::PrepareBlock(UInt32 *block, unsigned size) const
   1.110 +{
   1.111 +  unsigned curBufferPos = size & 0xF;
   1.112 +  block[curBufferPos++] = 0x80000000;
   1.113 +  while (curBufferPos != (16 - 2))
   1.114 +    block[curBufferPos++] = 0;
   1.115 +  const UInt64 lenInBits = (_count << 9) + ((UInt64)size << 5);
   1.116 +  block[curBufferPos++] = (UInt32)(lenInBits >> 32);
   1.117 +  block[curBufferPos++] = (UInt32)(lenInBits);
   1.118 +}
   1.119 +
   1.120 +void CContext::Update(Byte *data, size_t size, bool rar350Mode)
   1.121 +{
   1.122 +  bool returnRes = false;
   1.123 +  unsigned curBufferPos = _count2;
   1.124 +  while (size-- > 0)
   1.125 +  {
   1.126 +    int pos = (int)(curBufferPos & 3);
   1.127 +    if (pos == 0)
   1.128 +      _buffer[curBufferPos >> 2] = 0;
   1.129 +    _buffer[curBufferPos >> 2] |= ((UInt32)*data++) << (8 * (3 - pos));
   1.130 +    if (++curBufferPos == kBlockSize)
   1.131 +    {
   1.132 +      curBufferPos = 0;
   1.133 +      CContextBase::UpdateBlock(_buffer, returnRes);
   1.134 +      if (returnRes)
   1.135 +        for (int i = 0; i < kBlockSizeInWords; i++)
   1.136 +        {
   1.137 +          UInt32 d = _buffer[i];
   1.138 +          data[i * 4 + 0 - kBlockSize] = (Byte)(d);
   1.139 +          data[i * 4 + 1 - kBlockSize] = (Byte)(d >>  8);
   1.140 +          data[i * 4 + 2 - kBlockSize] = (Byte)(d >> 16);
   1.141 +          data[i * 4 + 3 - kBlockSize] = (Byte)(d >> 24);
   1.142 +        }
   1.143 +      returnRes = rar350Mode;
   1.144 +    }
   1.145 +  }
   1.146 +  _count2 = curBufferPos;
   1.147 +}
   1.148 +
   1.149 +void CContext::Final(Byte *digest)
   1.150 +{
   1.151 +  const UInt64 lenInBits = (_count << 9) + ((UInt64)_count2 << 3);
   1.152 +  unsigned curBufferPos = _count2;
   1.153 +  int pos = (int)(curBufferPos & 3);
   1.154 +  curBufferPos >>= 2;
   1.155 +  if (pos == 0)
   1.156 +    _buffer[curBufferPos] = 0;
   1.157 +  _buffer[curBufferPos++] |= ((UInt32)0x80) << (8 * (3 - pos));
   1.158 +
   1.159 +  while (curBufferPos != (16 - 2))
   1.160 +  {
   1.161 +    curBufferPos &= 0xF;
   1.162 +    if (curBufferPos == 0)
   1.163 +      UpdateBlock();
   1.164 +    _buffer[curBufferPos++] = 0;
   1.165 +  }
   1.166 +  _buffer[curBufferPos++] = (UInt32)(lenInBits >> 32);
   1.167 +  _buffer[curBufferPos++] = (UInt32)(lenInBits);
   1.168 +  UpdateBlock();
   1.169 +
   1.170 +  int i;
   1.171 +  for (i = 0; i < kDigestSizeInWords; i++)
   1.172 +  {
   1.173 +    UInt32 state = _state[i] & 0xFFFFFFFF;
   1.174 +    *digest++ = (Byte)(state >> 24);
   1.175 +    *digest++ = (Byte)(state >> 16);
   1.176 +    *digest++ = (Byte)(state >> 8);
   1.177 +    *digest++ = (Byte)(state);
   1.178 +  }
   1.179 +  Init();
   1.180 +}
   1.181 +
   1.182 +///////////////////////////
   1.183 +// Words version
   1.184 +
   1.185 +void CContext32::Update(const UInt32 *data, size_t size)
   1.186 +{
   1.187 +  while (size-- > 0)
   1.188 +  {
   1.189 +    _buffer[_count2++] = *data++;
   1.190 +    if (_count2 == kBlockSizeInWords)
   1.191 +    {
   1.192 +      _count2 = 0;
   1.193 +      UpdateBlock();
   1.194 +    }
   1.195 +  }
   1.196 +}
   1.197 +
   1.198 +void CContext32::Final(UInt32 *digest)
   1.199 +{
   1.200 +  const UInt64 lenInBits = (_count << 9) + ((UInt64)_count2 << 5);
   1.201 +  unsigned curBufferPos = _count2;
   1.202 +  _buffer[curBufferPos++] = 0x80000000;
   1.203 +  while (curBufferPos != (16 - 2))
   1.204 +  {
   1.205 +    curBufferPos &= 0xF;
   1.206 +    if (curBufferPos == 0)
   1.207 +      UpdateBlock();
   1.208 +    _buffer[curBufferPos++] = 0;
   1.209 +  }
   1.210 +  _buffer[curBufferPos++] = (UInt32)(lenInBits >> 32);
   1.211 +  _buffer[curBufferPos++] = (UInt32)(lenInBits);
   1.212 +  GetBlockDigest(_buffer, digest);
   1.213 +  Init();
   1.214 +}
   1.215 +
   1.216 +}}