Coverage Report

Created: 2024-11-21 07:03

/src/cryptofuzz/modules/veracrypt/Rmd160.c
Line
Count
Source
1
// RIPEMD-160 written and placed in the public domain by Wei Dai
2
3
/*
4
 * This code implements the MD4 message-digest algorithm.
5
 * The algorithm is due to Ron Rivest.  This code was
6
 * written by Colin Plumb in 1993, no copyright is claimed.
7
 * This code is in the public domain; do with it what you wish.
8
 */
9
10
/* Adapted for TrueCrypt */
11
/* Adapted for VeraCrypt */
12
#if !defined(_UEFI)
13
#include <memory.h>
14
#endif // !defined(_UEFI)
15
16
#include "Tcdefs.h"
17
#include "Endian.h"
18
#include "Rmd160.h"
19
20
1.75M
#define F(x, y, z)    (x ^ y ^ z) 
21
1.75M
#define G(x, y, z)    (z ^ (x & (y^z)))
22
1.75M
#define H(x, y, z)    (z ^ (x | ~y))
23
1.75M
#define I(x, y, z)    (y ^ (z & (x^y)))
24
1.75M
#define J(x, y, z)    (x ^ (y | ~z))
25
26
138
#define PUT_64BIT_LE(cp, value) do {                                    \
27
138
  (cp)[7] = (byte) ((value) >> 56);                                        \
28
138
  (cp)[6] = (byte) ((value) >> 48);                                        \
29
138
  (cp)[5] = (byte) ((value) >> 40);                                        \
30
138
  (cp)[4] = (byte) ((value) >> 32);                                        \
31
138
  (cp)[3] = (byte) ((value) >> 24);                                        \
32
138
  (cp)[2] = (byte) ((value) >> 16);                                        \
33
138
  (cp)[1] = (byte) ((value) >> 8);                                         \
34
138
  (cp)[0] = (byte) (value); } while (0)
35
36
690
#define PUT_32BIT_LE(cp, value) do {                                    \
37
690
  (cp)[3] = (byte) ((value) >> 24);                                        \
38
690
  (cp)[2] = (byte) ((value) >> 16);                                        \
39
690
  (cp)[1] = (byte) ((value) >> 8);                                         \
40
690
  (cp)[0] = (byte) (value); } while (0)
41
42
#ifndef TC_MINIMIZE_CODE_SIZE
43
44
static byte PADDING[64] = {
45
  0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
48
};
49
50
#else
51
52
static byte PADDING[64];
53
54
#endif
55
56
void RMD160Init (RMD160_CTX *ctx)
57
138
{
58
138
  ctx->count = 0;
59
138
  ctx->state[0] = 0x67452301;
60
138
  ctx->state[1] = 0xefcdab89;
61
138
  ctx->state[2] = 0x98badcfe;
62
138
  ctx->state[3] = 0x10325476;
63
138
  ctx->state[4] = 0xc3d2e1f0;
64
138
  PADDING[0] = 0x80;
65
138
}
66
67
/*
68
* Update context to reflect the concatenation of another buffer full
69
* of bytes.
70
*/
71
void RMD160Update (RMD160_CTX *ctx, const unsigned char *input, unsigned __int32 lenArg)
72
20.9k
{
73
20.9k
#ifndef TC_WINDOWS_BOOT
74
20.9k
  uint64 len = lenArg;
75
#else
76
  uint32 len = lenArg;
77
#endif
78
20.9k
  unsigned int have, need;
79
80
  /* Check how many bytes we already have and how many more we need. */
81
20.9k
  have = (unsigned int) ((ctx->count) & (RIPEMD160_BLOCK_LENGTH - 1));
82
20.9k
  need = RIPEMD160_BLOCK_LENGTH - have;
83
84
  /* Update bitcount */
85
20.9k
  ctx->count += len;
86
87
20.9k
  if (len >= need) {
88
674
    if (have != 0) {
89
555
      memcpy (ctx->buffer + have, input, (size_t) need);
90
555
      RMD160Transform ((uint32 *) ctx->state, (const uint32 *) ctx->buffer);
91
555
      input += need;
92
555
      len -= need;
93
555
      have = 0;
94
555
    }
95
96
    /* Process data in RIPEMD160_BLOCK_LENGTH-byte chunks. */
97
54.9k
    while (len >= RIPEMD160_BLOCK_LENGTH) {
98
54.3k
      RMD160Transform ((uint32 *) ctx->state, (const uint32 *) input);
99
54.3k
      input += RIPEMD160_BLOCK_LENGTH;
100
54.3k
      len -= RIPEMD160_BLOCK_LENGTH;
101
54.3k
    }
102
674
  }
103
104
  /* Handle any remaining bytes of data. */
105
20.9k
  if (len != 0)
106
1.08k
    memcpy (ctx->buffer + have, input, (size_t) len);
107
20.9k
}
108
109
/*
110
* Pad pad to 64-byte boundary with the bit pattern
111
* 1 0* (64-bit count of bits processed, MSB-first)
112
*/
113
static void RMD160Pad(RMD160_CTX *ctx)
114
138
{
115
138
  byte count[8];
116
138
  uint32 padlen;
117
118
  /* Convert count to 8 bytes in little endian order. */
119
120
138
#ifndef TC_WINDOWS_BOOT
121
138
  uint64 bitcount = ctx->count << 3;
122
138
  PUT_64BIT_LE(count, bitcount);
123
#else
124
  *(uint32 *) (count + 4) = 0;
125
  *(uint32 *) (count + 0) = ctx->count << 3;
126
#endif
127
128
  /* Pad out to 56 mod 64. */
129
138
  padlen = RIPEMD160_BLOCK_LENGTH -
130
138
    (uint32)((ctx->count) & (RIPEMD160_BLOCK_LENGTH - 1));
131
138
  if (padlen < 1 + 8)
132
38
    padlen += RIPEMD160_BLOCK_LENGTH;
133
138
  RMD160Update(ctx, PADDING, padlen - 8);            /* padlen - 8 <= 64 */
134
138
  RMD160Update(ctx, count, 8);
135
138
}
136
137
/*
138
* Final wrapup--call RMD160Pad, fill in digest and zero out ctx.
139
*/
140
void RMD160Final(unsigned char *digest, RMD160_CTX *ctx)
141
138
{
142
138
  int i;
143
144
138
  RMD160Pad(ctx);
145
138
  if (digest) {
146
828
    for (i = 0; i < 5; i++)
147
690
      PUT_32BIT_LE(digest + i * 4, ctx->state[i]);
148
138
#ifndef TC_WINDOWS_BOOT
149
138
    burn (ctx, sizeof(*ctx));
150
138
#endif
151
138
  }
152
138
}
153
154
155
#ifndef TC_MINIMIZE_CODE_SIZE
156
157
54.8k
#define word32 unsigned __int32
158
159
#define k0 0
160
#define k1 0x5a827999UL
161
#define k2 0x6ed9eba1UL
162
#define k3 0x8f1bbcdcUL
163
#define k4 0xa953fd4eUL
164
#define k5 0x50a28be6UL
165
#define k6 0x5c4dd124UL
166
#define k7 0x6d703ef3UL
167
#define k8 0x7a6d76e9UL
168
#define k9 0
169
170
static word32 rotlFixed (word32 x, unsigned int y)
171
17.5M
{
172
17.5M
  return (word32)((x<<y) | (x>>(sizeof(word32)*8-y)));
173
17.5M
}
174
175
#define Subround(f, a, b, c, d, e, x, s, k)        \
176
8.77M
  a += f(b, c, d) + x + k;\
177
8.77M
  a = rotlFixed((word32)a, s) + e;\
178
8.77M
  c = rotlFixed((word32)c, 10U)
179
180
void RMD160Transform (unsigned __int32 *digest, const unsigned __int32 *data)
181
54.8k
{
182
54.8k
#if BYTE_ORDER == LITTLE_ENDIAN
183
54.8k
  const word32 *X = data;
184
#else
185
  word32 X[16];
186
  int i;
187
#endif
188
189
54.8k
  word32 a1, b1, c1, d1, e1, a2, b2, c2, d2, e2;
190
54.8k
  a1 = a2 = digest[0];
191
54.8k
  b1 = b2 = digest[1];
192
54.8k
  c1 = c2 = digest[2];
193
54.8k
  d1 = d2 = digest[3];
194
54.8k
  e1 = e2 = digest[4];
195
196
#if BYTE_ORDER == BIG_ENDIAN
197
  for (i = 0; i < 16; i++)
198
  {
199
    X[i] = LE32 (data[i]);
200
  }
201
#endif
202
203
54.8k
  Subround(F, a1, b1, c1, d1, e1, X[ 0], 11, k0);
204
54.8k
  Subround(F, e1, a1, b1, c1, d1, X[ 1], 14, k0);
205
54.8k
  Subround(F, d1, e1, a1, b1, c1, X[ 2], 15, k0);
206
54.8k
  Subround(F, c1, d1, e1, a1, b1, X[ 3], 12, k0);
207
54.8k
  Subround(F, b1, c1, d1, e1, a1, X[ 4],  5, k0);
208
54.8k
  Subround(F, a1, b1, c1, d1, e1, X[ 5],  8, k0);
209
54.8k
  Subround(F, e1, a1, b1, c1, d1, X[ 6],  7, k0);
210
54.8k
  Subround(F, d1, e1, a1, b1, c1, X[ 7],  9, k0);
211
54.8k
  Subround(F, c1, d1, e1, a1, b1, X[ 8], 11, k0);
212
54.8k
  Subround(F, b1, c1, d1, e1, a1, X[ 9], 13, k0);
213
54.8k
  Subround(F, a1, b1, c1, d1, e1, X[10], 14, k0);
214
54.8k
  Subround(F, e1, a1, b1, c1, d1, X[11], 15, k0);
215
54.8k
  Subround(F, d1, e1, a1, b1, c1, X[12],  6, k0);
216
54.8k
  Subround(F, c1, d1, e1, a1, b1, X[13],  7, k0);
217
54.8k
  Subround(F, b1, c1, d1, e1, a1, X[14],  9, k0);
218
54.8k
  Subround(F, a1, b1, c1, d1, e1, X[15],  8, k0);
219
220
54.8k
  Subround(G, e1, a1, b1, c1, d1, X[ 7],  7, k1);
221
54.8k
  Subround(G, d1, e1, a1, b1, c1, X[ 4],  6, k1);
222
54.8k
  Subround(G, c1, d1, e1, a1, b1, X[13],  8, k1);
223
54.8k
  Subround(G, b1, c1, d1, e1, a1, X[ 1], 13, k1);
224
54.8k
  Subround(G, a1, b1, c1, d1, e1, X[10], 11, k1);
225
54.8k
  Subround(G, e1, a1, b1, c1, d1, X[ 6],  9, k1);
226
54.8k
  Subround(G, d1, e1, a1, b1, c1, X[15],  7, k1);
227
54.8k
  Subround(G, c1, d1, e1, a1, b1, X[ 3], 15, k1);
228
54.8k
  Subround(G, b1, c1, d1, e1, a1, X[12],  7, k1);
229
54.8k
  Subround(G, a1, b1, c1, d1, e1, X[ 0], 12, k1);
230
54.8k
  Subround(G, e1, a1, b1, c1, d1, X[ 9], 15, k1);
231
54.8k
  Subround(G, d1, e1, a1, b1, c1, X[ 5],  9, k1);
232
54.8k
  Subround(G, c1, d1, e1, a1, b1, X[ 2], 11, k1);
233
54.8k
  Subround(G, b1, c1, d1, e1, a1, X[14],  7, k1);
234
54.8k
  Subround(G, a1, b1, c1, d1, e1, X[11], 13, k1);
235
54.8k
  Subround(G, e1, a1, b1, c1, d1, X[ 8], 12, k1);
236
237
54.8k
  Subround(H, d1, e1, a1, b1, c1, X[ 3], 11, k2);
238
54.8k
  Subround(H, c1, d1, e1, a1, b1, X[10], 13, k2);
239
54.8k
  Subround(H, b1, c1, d1, e1, a1, X[14],  6, k2);
240
54.8k
  Subround(H, a1, b1, c1, d1, e1, X[ 4],  7, k2);
241
54.8k
  Subround(H, e1, a1, b1, c1, d1, X[ 9], 14, k2);
242
54.8k
  Subround(H, d1, e1, a1, b1, c1, X[15],  9, k2);
243
54.8k
  Subround(H, c1, d1, e1, a1, b1, X[ 8], 13, k2);
244
54.8k
  Subround(H, b1, c1, d1, e1, a1, X[ 1], 15, k2);
245
54.8k
  Subround(H, a1, b1, c1, d1, e1, X[ 2], 14, k2);
246
54.8k
  Subround(H, e1, a1, b1, c1, d1, X[ 7],  8, k2);
247
54.8k
  Subround(H, d1, e1, a1, b1, c1, X[ 0], 13, k2);
248
54.8k
  Subround(H, c1, d1, e1, a1, b1, X[ 6],  6, k2);
249
54.8k
  Subround(H, b1, c1, d1, e1, a1, X[13],  5, k2);
250
54.8k
  Subround(H, a1, b1, c1, d1, e1, X[11], 12, k2);
251
54.8k
  Subround(H, e1, a1, b1, c1, d1, X[ 5],  7, k2);
252
54.8k
  Subround(H, d1, e1, a1, b1, c1, X[12],  5, k2);
253
254
54.8k
  Subround(I, c1, d1, e1, a1, b1, X[ 1], 11, k3);
255
54.8k
  Subround(I, b1, c1, d1, e1, a1, X[ 9], 12, k3);
256
54.8k
  Subround(I, a1, b1, c1, d1, e1, X[11], 14, k3);
257
54.8k
  Subround(I, e1, a1, b1, c1, d1, X[10], 15, k3);
258
54.8k
  Subround(I, d1, e1, a1, b1, c1, X[ 0], 14, k3);
259
54.8k
  Subround(I, c1, d1, e1, a1, b1, X[ 8], 15, k3);
260
54.8k
  Subround(I, b1, c1, d1, e1, a1, X[12],  9, k3);
261
54.8k
  Subround(I, a1, b1, c1, d1, e1, X[ 4],  8, k3);
262
54.8k
  Subround(I, e1, a1, b1, c1, d1, X[13],  9, k3);
263
54.8k
  Subround(I, d1, e1, a1, b1, c1, X[ 3], 14, k3);
264
54.8k
  Subround(I, c1, d1, e1, a1, b1, X[ 7],  5, k3);
265
54.8k
  Subround(I, b1, c1, d1, e1, a1, X[15],  6, k3);
266
54.8k
  Subround(I, a1, b1, c1, d1, e1, X[14],  8, k3);
267
54.8k
  Subround(I, e1, a1, b1, c1, d1, X[ 5],  6, k3);
268
54.8k
  Subround(I, d1, e1, a1, b1, c1, X[ 6],  5, k3);
269
54.8k
  Subround(I, c1, d1, e1, a1, b1, X[ 2], 12, k3);
270
271
54.8k
  Subround(J, b1, c1, d1, e1, a1, X[ 4],  9, k4);
272
54.8k
  Subround(J, a1, b1, c1, d1, e1, X[ 0], 15, k4);
273
54.8k
  Subround(J, e1, a1, b1, c1, d1, X[ 5],  5, k4);
274
54.8k
  Subround(J, d1, e1, a1, b1, c1, X[ 9], 11, k4);
275
54.8k
  Subround(J, c1, d1, e1, a1, b1, X[ 7],  6, k4);
276
54.8k
  Subround(J, b1, c1, d1, e1, a1, X[12],  8, k4);
277
54.8k
  Subround(J, a1, b1, c1, d1, e1, X[ 2], 13, k4);
278
54.8k
  Subround(J, e1, a1, b1, c1, d1, X[10], 12, k4);
279
54.8k
  Subround(J, d1, e1, a1, b1, c1, X[14],  5, k4);
280
54.8k
  Subround(J, c1, d1, e1, a1, b1, X[ 1], 12, k4);
281
54.8k
  Subround(J, b1, c1, d1, e1, a1, X[ 3], 13, k4);
282
54.8k
  Subround(J, a1, b1, c1, d1, e1, X[ 8], 14, k4);
283
54.8k
  Subround(J, e1, a1, b1, c1, d1, X[11], 11, k4);
284
54.8k
  Subround(J, d1, e1, a1, b1, c1, X[ 6],  8, k4);
285
54.8k
  Subround(J, c1, d1, e1, a1, b1, X[15],  5, k4);
286
54.8k
  Subround(J, b1, c1, d1, e1, a1, X[13],  6, k4);
287
288
54.8k
  Subround(J, a2, b2, c2, d2, e2, X[ 5],  8, k5);
289
54.8k
  Subround(J, e2, a2, b2, c2, d2, X[14],  9, k5);
290
54.8k
  Subround(J, d2, e2, a2, b2, c2, X[ 7],  9, k5);
291
54.8k
  Subround(J, c2, d2, e2, a2, b2, X[ 0], 11, k5);
292
54.8k
  Subround(J, b2, c2, d2, e2, a2, X[ 9], 13, k5);
293
54.8k
  Subround(J, a2, b2, c2, d2, e2, X[ 2], 15, k5);
294
54.8k
  Subround(J, e2, a2, b2, c2, d2, X[11], 15, k5);
295
54.8k
  Subround(J, d2, e2, a2, b2, c2, X[ 4],  5, k5);
296
54.8k
  Subround(J, c2, d2, e2, a2, b2, X[13],  7, k5);
297
54.8k
  Subround(J, b2, c2, d2, e2, a2, X[ 6],  7, k5);
298
54.8k
  Subround(J, a2, b2, c2, d2, e2, X[15],  8, k5);
299
54.8k
  Subround(J, e2, a2, b2, c2, d2, X[ 8], 11, k5);
300
54.8k
  Subround(J, d2, e2, a2, b2, c2, X[ 1], 14, k5);
301
54.8k
  Subround(J, c2, d2, e2, a2, b2, X[10], 14, k5);
302
54.8k
  Subround(J, b2, c2, d2, e2, a2, X[ 3], 12, k5);
303
54.8k
  Subround(J, a2, b2, c2, d2, e2, X[12],  6, k5);
304
305
54.8k
  Subround(I, e2, a2, b2, c2, d2, X[ 6],  9, k6); 
306
54.8k
  Subround(I, d2, e2, a2, b2, c2, X[11], 13, k6);
307
54.8k
  Subround(I, c2, d2, e2, a2, b2, X[ 3], 15, k6);
308
54.8k
  Subround(I, b2, c2, d2, e2, a2, X[ 7],  7, k6);
309
54.8k
  Subround(I, a2, b2, c2, d2, e2, X[ 0], 12, k6);
310
54.8k
  Subround(I, e2, a2, b2, c2, d2, X[13],  8, k6);
311
54.8k
  Subround(I, d2, e2, a2, b2, c2, X[ 5],  9, k6);
312
54.8k
  Subround(I, c2, d2, e2, a2, b2, X[10], 11, k6);
313
54.8k
  Subround(I, b2, c2, d2, e2, a2, X[14],  7, k6);
314
54.8k
  Subround(I, a2, b2, c2, d2, e2, X[15],  7, k6);
315
54.8k
  Subround(I, e2, a2, b2, c2, d2, X[ 8], 12, k6);
316
54.8k
  Subround(I, d2, e2, a2, b2, c2, X[12],  7, k6);
317
54.8k
  Subround(I, c2, d2, e2, a2, b2, X[ 4],  6, k6);
318
54.8k
  Subround(I, b2, c2, d2, e2, a2, X[ 9], 15, k6);
319
54.8k
  Subround(I, a2, b2, c2, d2, e2, X[ 1], 13, k6);
320
54.8k
  Subround(I, e2, a2, b2, c2, d2, X[ 2], 11, k6);
321
322
54.8k
  Subround(H, d2, e2, a2, b2, c2, X[15],  9, k7);
323
54.8k
  Subround(H, c2, d2, e2, a2, b2, X[ 5],  7, k7);
324
54.8k
  Subround(H, b2, c2, d2, e2, a2, X[ 1], 15, k7);
325
54.8k
  Subround(H, a2, b2, c2, d2, e2, X[ 3], 11, k7);
326
54.8k
  Subround(H, e2, a2, b2, c2, d2, X[ 7],  8, k7);
327
54.8k
  Subround(H, d2, e2, a2, b2, c2, X[14],  6, k7);
328
54.8k
  Subround(H, c2, d2, e2, a2, b2, X[ 6],  6, k7);
329
54.8k
  Subround(H, b2, c2, d2, e2, a2, X[ 9], 14, k7);
330
54.8k
  Subround(H, a2, b2, c2, d2, e2, X[11], 12, k7);
331
54.8k
  Subround(H, e2, a2, b2, c2, d2, X[ 8], 13, k7);
332
54.8k
  Subround(H, d2, e2, a2, b2, c2, X[12],  5, k7);
333
54.8k
  Subround(H, c2, d2, e2, a2, b2, X[ 2], 14, k7);
334
54.8k
  Subround(H, b2, c2, d2, e2, a2, X[10], 13, k7);
335
54.8k
  Subround(H, a2, b2, c2, d2, e2, X[ 0], 13, k7);
336
54.8k
  Subround(H, e2, a2, b2, c2, d2, X[ 4],  7, k7);
337
54.8k
  Subround(H, d2, e2, a2, b2, c2, X[13],  5, k7);
338
339
54.8k
  Subround(G, c2, d2, e2, a2, b2, X[ 8], 15, k8);
340
54.8k
  Subround(G, b2, c2, d2, e2, a2, X[ 6],  5, k8);
341
54.8k
  Subround(G, a2, b2, c2, d2, e2, X[ 4],  8, k8);
342
54.8k
  Subround(G, e2, a2, b2, c2, d2, X[ 1], 11, k8);
343
54.8k
  Subround(G, d2, e2, a2, b2, c2, X[ 3], 14, k8);
344
54.8k
  Subround(G, c2, d2, e2, a2, b2, X[11], 14, k8);
345
54.8k
  Subround(G, b2, c2, d2, e2, a2, X[15],  6, k8);
346
54.8k
  Subround(G, a2, b2, c2, d2, e2, X[ 0], 14, k8);
347
54.8k
  Subround(G, e2, a2, b2, c2, d2, X[ 5],  6, k8);
348
54.8k
  Subround(G, d2, e2, a2, b2, c2, X[12],  9, k8);
349
54.8k
  Subround(G, c2, d2, e2, a2, b2, X[ 2], 12, k8);
350
54.8k
  Subround(G, b2, c2, d2, e2, a2, X[13],  9, k8);
351
54.8k
  Subround(G, a2, b2, c2, d2, e2, X[ 9], 12, k8);
352
54.8k
  Subround(G, e2, a2, b2, c2, d2, X[ 7],  5, k8);
353
54.8k
  Subround(G, d2, e2, a2, b2, c2, X[10], 15, k8);
354
54.8k
  Subround(G, c2, d2, e2, a2, b2, X[14],  8, k8);
355
356
54.8k
  Subround(F, b2, c2, d2, e2, a2, X[12],  8, k9);
357
54.8k
  Subround(F, a2, b2, c2, d2, e2, X[15],  5, k9);
358
54.8k
  Subround(F, e2, a2, b2, c2, d2, X[10], 12, k9);
359
54.8k
  Subround(F, d2, e2, a2, b2, c2, X[ 4],  9, k9);
360
54.8k
  Subround(F, c2, d2, e2, a2, b2, X[ 1], 12, k9);
361
54.8k
  Subround(F, b2, c2, d2, e2, a2, X[ 5],  5, k9);
362
54.8k
  Subround(F, a2, b2, c2, d2, e2, X[ 8], 14, k9);
363
54.8k
  Subround(F, e2, a2, b2, c2, d2, X[ 7],  6, k9);
364
54.8k
  Subround(F, d2, e2, a2, b2, c2, X[ 6],  8, k9);
365
54.8k
  Subround(F, c2, d2, e2, a2, b2, X[ 2], 13, k9);
366
54.8k
  Subround(F, b2, c2, d2, e2, a2, X[13],  6, k9);
367
54.8k
  Subround(F, a2, b2, c2, d2, e2, X[14],  5, k9);
368
54.8k
  Subround(F, e2, a2, b2, c2, d2, X[ 0], 15, k9);
369
54.8k
  Subround(F, d2, e2, a2, b2, c2, X[ 3], 13, k9);
370
54.8k
  Subround(F, c2, d2, e2, a2, b2, X[ 9], 11, k9);
371
54.8k
  Subround(F, b2, c2, d2, e2, a2, X[11], 11, k9);
372
373
54.8k
  c1        = digest[1] + c1 + d2;
374
54.8k
  digest[1] = digest[2] + d1 + e2;
375
54.8k
  digest[2] = digest[3] + e1 + a2;
376
54.8k
  digest[3] = digest[4] + a1 + b2;
377
54.8k
  digest[4] = digest[0] + b1 + c2;
378
54.8k
  digest[0] = c1;
379
54.8k
}
380
381
#else // TC_MINIMIZE_CODE_SIZE
382
383
/*
384
 Derived from source code of TrueCrypt 7.1a, which is
385
 Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
386
 by the TrueCrypt License 3.0.
387
388
 Modifications and additions to the original source code (contained in this file) 
389
 and all other portions of this file are Copyright (c) 2013-2017 IDRIX
390
 and are governed by the Apache License 2.0 the full text of which is
391
 contained in the file License.txt included in VeraCrypt binary and source
392
 code distribution packages.
393
*/
394
395
#pragma optimize ("tl", on)
396
397
typedef unsigned __int32 uint32;
398
typedef unsigned __int8 byte;
399
400
#include <stdlib.h>
401
#pragma intrinsic (_lrotl)
402
403
static const byte OrderTab[] = {
404
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
405
  7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
406
  3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
407
  1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
408
  4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13,
409
  5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
410
  6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
411
  15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
412
  8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
413
  12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
414
};
415
416
static const byte RolTab[] = {
417
  11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
418
  7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
419
  11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
420
  11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
421
  9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6,
422
  8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
423
  9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
424
  9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
425
  15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
426
  8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
427
};
428
429
static const uint32 KTab[] = {
430
  0x00000000UL,
431
  0x5A827999UL,
432
  0x6ED9EBA1UL,
433
  0x8F1BBCDCUL,
434
  0xA953FD4EUL,
435
  0x50A28BE6UL,
436
  0x5C4DD124UL,
437
  0x6D703EF3UL,
438
  0x7A6D76E9UL,
439
  0x00000000UL
440
};
441
442
443
void RMD160Transform (unsigned __int32 *state, const unsigned __int32 *data)
444
{
445
  uint32 a, b, c, d, e;
446
  uint32 a2, b2, c2, d2, e2;
447
  byte pos;
448
  uint32 tmp;
449
450
  a = state[0];
451
  b = state[1];
452
  c = state[2];
453
  d = state[3];
454
  e = state[4];
455
456
  for (pos = 0; pos < 160; ++pos)
457
  {
458
    tmp = a + data[OrderTab[pos]] + KTab[pos >> 4];
459
    
460
    switch (pos >> 4)
461
    {
462
    case 0: case 9: tmp += F (b, c, d); break;
463
    case 1: case 8: tmp += G (b, c, d); break;
464
    case 2: case 7: tmp += H (b, c, d); break;
465
    case 3: case 6: tmp += I (b, c, d); break;
466
    case 4: case 5: tmp += J (b, c, d); break;
467
    }
468
469
    tmp = _lrotl (tmp, RolTab[pos]) + e;
470
    a = e;
471
    e = d;
472
    d = _lrotl (c, 10);
473
    c = b;
474
    b = tmp;
475
476
    if (pos == 79)
477
    {
478
      a2 = a;
479
      b2 = b;
480
      c2 = c;
481
      d2 = d;
482
      e2 = e;
483
484
      a = state[0];
485
      b = state[1];
486
      c = state[2];
487
      d = state[3];
488
      e = state[4];
489
    }
490
  }
491
492
  tmp = state[1] + c2 + d;
493
  state[1] = state[2] + d2 + e;
494
  state[2] = state[3] + e2 + a;
495
  state[3] = state[4] + a2 + b;
496
  state[4] = state[0] + b2 + c;
497
  state[0] = tmp;
498
}
499
500
#endif // TC_MINIMIZE_CODE_SIZE