Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Sha1.c « C - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/C/Sha1.c
blob: 040cfe50a94fdce902799612e8ec4e78a6e594b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* Sha1.c -- SHA-1 Hash
2016-02-09 : Igor Pavlov : Public domain
This code is based on public domain code of Steve Reid from Wei Dai's Crypto++ library. */

#include "Precomp.h"

#include <string.h>

#include "CpuArch.h"
#include "RotateDefs.h"
#include "Sha1.h"

// define it for speed optimization
// #define _SHA1_UNROLL

#ifdef _SHA1_UNROLL
  #define kNumW 16
  #define WW(i) W[(i)&15]
#else
  #define kNumW 80
  #define WW(i) W[i]
#endif

#define w0(i) (W[i] = data[i])

#define w1(i) (WW(i) = rotlFixed(WW((i)-3) ^ WW((i)-8) ^ WW((i)-14) ^ WW((i)-16), 1))

#define f1(x,y,z)  (z^(x&(y^z)))
#define f2(x,y,z)  (x^y^z)
#define f3(x,y,z)  ((x&y)|(z&(x|y)))
#define f4(x,y,z)  (x^y^z)

#define RK(a,b,c,d,e, fx, w, k)  e += fx(b,c,d) + w + k + rotlFixed(a,5); b = rotlFixed(b,30);

#define R0(a,b,c,d,e, i)  RK(a,b,c,d,e, f1, w0(i), 0x5A827999)
#define R1(a,b,c,d,e, i)  RK(a,b,c,d,e, f1, w1(i), 0x5A827999)
#define R2(a,b,c,d,e, i)  RK(a,b,c,d,e, f2, w1(i), 0x6ED9EBA1)
#define R3(a,b,c,d,e, i)  RK(a,b,c,d,e, f3, w1(i), 0x8F1BBCDC)
#define R4(a,b,c,d,e, i)  RK(a,b,c,d,e, f4, w1(i), 0xCA62C1D6)

#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); \

#define RX_5(rx, i)  RX_1_4(rx, rx, i);

#ifdef _SHA1_UNROLL

  #define RX_15 \
      RX_5(R0, 0); \
      RX_5(R0, 5); \
      RX_5(R0, 10);
  
  #define RX_20(rx, i) \
      RX_5(rx, i); \
      RX_5(rx, i + 5); \
      RX_5(rx, i + 10); \
      RX_5(rx, i + 15);

#else
  
#define RX_15  { unsigned i; for (i = 0; i < 15; i += 5) { RX_5(R0, i); } }
#define RX_20(rx, ii)  { unsigned i; i = ii; for (; i < ii + 20; i += 5) { RX_5(rx, i); } }

#endif


void Sha1_Init(CSha1 *p)
{
  p->state[0] = 0x67452301;
  p->state[1] = 0xEFCDAB89;
  p->state[2] = 0x98BADCFE;
  p->state[3] = 0x10325476;
  p->state[4] = 0xC3D2E1F0;
  p->count = 0;
}

void Sha1_GetBlockDigest(CSha1 *p, const UInt32 *data, UInt32 *destDigest)
{
  UInt32 a, b, c, d, e;
  UInt32 W[kNumW];

  a = p->state[0];
  b = p->state[1];
  c = p->state[2];
  d = p->state[3];
  e = p->state[4];
  
  RX_15

  RX_1_4(R0, R1, 15);

  RX_20(R2, 20);
  RX_20(R3, 40);
  RX_20(R4, 60);

  destDigest[0] = p->state[0] + a;
  destDigest[1] = p->state[1] + b;
  destDigest[2] = p->state[2] + c;
  destDigest[3] = p->state[3] + d;
  destDigest[4] = p->state[4] + e;
}

void Sha1_UpdateBlock_Rar(CSha1 *p, UInt32 *data, int returnRes)
{
  UInt32 a, b, c, d, e;
  UInt32 W[kNumW];

  a = p->state[0];
  b = p->state[1];
  c = p->state[2];
  d = p->state[3];
  e = p->state[4];
  
  RX_15

  RX_1_4(R0, R1, 15);

  RX_20(R2, 20);
  RX_20(R3, 40);
  RX_20(R4, 60);

  p->state[0] += a;
  p->state[1] += b;
  p->state[2] += c;
  p->state[3] += d;
  p->state[4] += e;

  if (returnRes)
  {
    unsigned i;
    for (i = 0 ; i < SHA1_NUM_BLOCK_WORDS; i++)
      data[i] = W[kNumW - SHA1_NUM_BLOCK_WORDS + i];
  }
}

#define Sha1_UpdateBlock(p) Sha1_GetBlockDigest(p, p->buffer, p->state)

void Sha1_Update(CSha1 *p, const Byte *data, size_t size)
{
  unsigned pos, pos2;
  if (size == 0)
    return;
  pos = (unsigned)p->count & 0x3F;
  p->count += size;
  pos2 = pos & 3;
  pos >>= 2;
  
  if (pos2 != 0)
  {
    UInt32 w;
    pos2 = (3 - pos2) * 8;
    w = ((UInt32)*data++) << pos2;
    if (--size && pos2)
    {
      pos2 -= 8;
      w |= ((UInt32)*data++) << pos2;
      if (--size && pos2)
      {
        pos2 -= 8;
        w |= ((UInt32)*data++) << pos2;
        size--;
      }
    }
    p->buffer[pos] |= w;
    if (pos2 == 0)
      pos++;
  }

  for (;;)
  {
    if (pos == SHA1_NUM_BLOCK_WORDS)
    {
      for (;;)
      {
        unsigned i;
        Sha1_UpdateBlock(p);
        if (size < SHA1_BLOCK_SIZE)
          break;
        size -= SHA1_BLOCK_SIZE;
        for (i = 0; i < SHA1_NUM_BLOCK_WORDS; i += 2)
        {
          p->buffer[i    ] = GetBe32(data);
          p->buffer[i + 1] = GetBe32(data + 4);
          data += 8;
        }
      }
      pos = 0;
    }
    if (size < 4)
      break;

    p->buffer[pos] = GetBe32(data);
    data += 4;
    size -= 4;
    pos++;
  }

  if (size != 0)
  {
    UInt32 w = ((UInt32)data[0]) << 24;
    if (size > 1)
    {
      w |= ((UInt32)data[1]) << 16;
      if (size > 2)
        w |= ((UInt32)data[2]) << 8;
    }
    p->buffer[pos] = w;
  }
}

void Sha1_Update_Rar(CSha1 *p, Byte *data, size_t size, int rar350Mode)
{
  int returnRes = False;
  
  unsigned pos = (unsigned)p->count & 0x3F;
  p->count += size;

  while (size--)
  {
    unsigned pos2 = (pos & 3);
    UInt32 v = ((UInt32)*data++) << (8 * (3 - pos2));
    UInt32 *ref = &(p->buffer[pos >> 2]);
    pos++;
    if (pos2 == 0)
    {
      *ref = v;
      continue;
    }
    *ref |= v;
    
    if (pos == SHA1_BLOCK_SIZE)
    {
      pos = 0;
      Sha1_UpdateBlock_Rar(p, p->buffer, returnRes);
      if (returnRes)
      {
        unsigned i;
        for (i = 0; i < SHA1_NUM_BLOCK_WORDS; i++)
        {
          UInt32 d = p->buffer[i];
          Byte *prev = data + i * 4 - SHA1_BLOCK_SIZE;
          SetUi32(prev, d);
        }
      }
      returnRes = rar350Mode;
    }
  }
}

void Sha1_Final(CSha1 *p, Byte *digest)
{
  unsigned pos = (unsigned)p->count & 0x3F;
  unsigned pos2 = (pos & 3);
  UInt64 numBits;
  UInt32 w;
  unsigned i;
  
  pos >>= 2;
  
  w = 0;
  if (pos2 != 0)
    w = p->buffer[pos];
  p->buffer[pos++] = w | (((UInt32)0x80000000) >> (8 * pos2));

  while (pos != (SHA1_NUM_BLOCK_WORDS - 2))
  {
    pos &= 0xF;
    if (pos == 0)
      Sha1_UpdateBlock(p);
    p->buffer[pos++] = 0;
  }
  
  numBits = (p->count << 3);
  p->buffer[SHA1_NUM_BLOCK_WORDS - 2] = (UInt32)(numBits >> 32);
  p->buffer[SHA1_NUM_BLOCK_WORDS - 1] = (UInt32)(numBits);
  Sha1_UpdateBlock(p);

  for (i = 0; i < SHA1_NUM_DIGEST_WORDS; i++)
  {
    UInt32 v = p->state[i];
    SetBe32(digest, v);
    digest += 4;
  }

  Sha1_Init(p);
}


void Sha1_32_PrepareBlock(const CSha1 *p, UInt32 *block, unsigned size)
{
  const UInt64 numBits = (p->count + size) << 5;
  block[SHA1_NUM_BLOCK_WORDS - 2] = (UInt32)(numBits >> 32);
  block[SHA1_NUM_BLOCK_WORDS - 1] = (UInt32)(numBits);
  block[size++] = 0x80000000;
  while (size != (SHA1_NUM_BLOCK_WORDS - 2))
    block[size++] = 0;
}

void Sha1_32_Update(CSha1 *p, const UInt32 *data, size_t size)
{
  unsigned pos = (unsigned)p->count & 0xF;
  p->count += size;
  while (size--)
  {
    p->buffer[pos++] = *data++;
    if (pos == SHA1_NUM_BLOCK_WORDS)
    {
      pos = 0;
      Sha1_UpdateBlock(p);
    }
  }
}

void Sha1_32_Final(CSha1 *p, UInt32 *digest)
{
  UInt64 numBits;
  unsigned pos = (unsigned)p->count & 0xF;
  p->buffer[pos++] = 0x80000000;

  while (pos != (SHA1_NUM_BLOCK_WORDS - 2))
  {
    pos &= 0xF;
    if (pos == 0)
      Sha1_UpdateBlock(p);
    p->buffer[pos++] = 0;
  }
  
  numBits = (p->count << 5);
  p->buffer[SHA1_NUM_BLOCK_WORDS - 2] = (UInt32)(numBits >> 32);
  p->buffer[SHA1_NUM_BLOCK_WORDS - 1] = (UInt32)(numBits);

  Sha1_GetBlockDigest(p, p->buffer, digest);
  
  Sha1_Init(p);
}