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
|
// haval.cpp - written and placed in the public domain by Wei Dai
#include "pch.h"
#include "haval.h"
#include "misc.h"
NAMESPACE_BEGIN(CryptoPP)
HAVAL::HAVAL(unsigned int digestSize, unsigned int pass)
: digestSize(digestSize), pass(pass)
{
SetStateSize(DIGESTSIZE);
if (!(digestSize >= 16 && digestSize <= 32 && digestSize%4==0))
throw InvalidArgument("HAVAL: invalid digest size");
if (!(pass >= 3 && pass <= 5))
throw InvalidArgument("HAVAL: invalid number of passes");
Init();
}
void HAVAL::Init()
{
m_digest[0] = 0x243F6A88;
m_digest[1] = 0x85A308D3;
m_digest[2] = 0x13198A2E;
m_digest[3] = 0x03707344;
m_digest[4] = 0xA4093822;
m_digest[5] = 0x299F31D0;
m_digest[6] = 0x082EFA98;
m_digest[7] = 0xEC4E6C89;
}
void HAVAL::HashEndianCorrectedBlock(const word32 *in)
{
if (pass==3)
HAVAL3::Transform(m_digest, in);
else if (pass==4)
HAVAL4::Transform(m_digest, in);
else
HAVAL5::Transform(m_digest, in);
}
void HAVAL::TruncatedFinal(byte *hash, size_t size)
{
ThrowIfInvalidTruncatedSize(size);
PadLastBlock(118, 1); // first byte of padding for HAVAL is 1 instead of 0x80
CorrectEndianess(m_data, m_data, 120);
m_data[29] &= 0xffff;
m_data[29] |= ((word32)digestSize<<25) | ((word32)pass<<19) | ((word32)HAVAL_VERSION<<16);
m_data[30] = GetBitCountLo();
m_data[31] = GetBitCountHi();
HashEndianCorrectedBlock(m_data);
Tailor(digestSize*8);
CorrectEndianess(m_digest, m_digest, digestSize);
memcpy(hash, m_digest, size);
Restart(); // reinit for next use
}
#define ROTR(x, y) rotrFixed(x, y##u)
// fold digest down to desired size
void HAVAL::Tailor(unsigned int bitlen)
{
#define EB(a, b, c) (m_digest[a] & (((~(word32)0) << b) & ((~(word32)0) >> (8*sizeof(word32)-b-c))))
#define S(a, b) (a > b ? a - b : 32 + a - b)
#define T128(a, b, c, d, e) ROTR(EB(7, b, S(a,b)) | EB(6, c, S(b,c)) | EB(5, d, S(c,d)) | EB(4, e, S(d,e)), e)
#define T160(a, b, c, d) ROTR(EB(7, b, S(a,b)) | EB(6, c, S(b,c)) | EB(5, d, S(c,d)), d)
#define T192(a, b, c) ROTR(EB(7, b, S(a,b)) | EB(6, c, S(b,c)), c)
#define T224(a, b) ROTR(EB(7, b, S(a,b)), b)
switch (bitlen)
{
case 128:
m_digest[0] += T128(8, 0, 24, 16, 8);
m_digest[1] += T128(16, 8, 0, 24, 16);
m_digest[2] += T128(24, 16, 8, 0, 24);
m_digest[3] += T128(0, 24, 16, 8, 0);
break;
case 160:
m_digest[0] += T160(6, 0, 25, 19);
m_digest[1] += T160(12, 6, 0, 25);
m_digest[2] += T160(19, 12, 6, 0);
m_digest[3] += T160(25, 19, 12, 6);
m_digest[4] += T160(0, 25, 19, 12);
break;
case 192:
m_digest[0] += T192(5, 0, 26);
m_digest[1] += T192(10, 5, 0);
m_digest[2] += T192(16, 10, 5);
m_digest[3] += T192(21, 16, 10);
m_digest[4] += T192(26, 21, 16);
m_digest[5] += T192(0, 26, 21);
break;
case 224:
m_digest[0] += T224(0, 27);
m_digest[1] += T224(27, 22);
m_digest[2] += T224(22, 18);
m_digest[3] += T224(18, 13);
m_digest[4] += T224(13, 9);
m_digest[5] += T224(9, 4);
m_digest[6] += T224(4, 0);
break;
case 256:
break;
default:
assert(false);
}
}
/* Nonlinear F functions */
/* #define F1(X6, X5, X4, X3, X2, X1, X0) \
((X1) & (X4) ^ (X2) & (X5) ^ (X3) & (X6) ^ (X0) & (X1) ^ (X0))*/
#define F1(X6, X5, X4, X3, X2, X1, X0) \
(((X1) & ((X4) ^ (X0))) ^ ((X2) & (X5)) ^ ((X3) & (X6)) ^ (X0))
/* #define F2(X6, X5, X4, X3, X2, X1, X0) \
((X1) & (X2) & (X3) ^ (X2) & (X4) & (X5) ^ \
(X1) & (X2) ^ (X1) & (X4) ^ (X2) & (X6) ^ (X3) & (X5) ^ \
(X4) & (X5) ^ (X0) & (X2) ^ (X0))*/
#define F2(X6, X5, X4, X3, X2, X1, X0) \
(((X2) & (((X1) & (~(X3))) ^ ((X4) & (X5)) ^ (X6) ^ (X0))) ^ \
(((X4) & ((X1) ^ (X5))) ^ ((X3) & (X5)) ^ (X0)))
/* #define F3(X6, X5, X4, X3, X2, X1, X0) \
((X1) & (X2) & (X3) ^ (X1) & (X4) ^ (X2) & (X5) ^ (X3) & (X6) ^ (X0) &
(X3) ^ (X0))*/
#define F3(X6, X5, X4, X3, X2, X1, X0) \
(((X3) & (((X1) & (X2)) ^ (X6) ^ (X0))) ^ ((X1) & (X4)) ^ \
((X2) & (X5)) ^ (X0))
/* #define F4(X6, X5, X4, X3, X2, X1, X0) \
((X1) & (X2) & (X3) ^ (X2) & (X4) & (X5) ^ (X3) & (X4) & (X6) ^ \
(X1) & (X4) ^ (X2) & (X6) ^ (X3) & (X4) ^ (X3) & (X5) ^ \
(X3) & (X6) ^ (X4) & (X5) ^ (X4) & (X6) ^ (X0) & (X4) ^(X0))*/
#define F4(X6, X5, X4, X3, X2, X1, X0) \
(((X4) & (((~(X2)) & (X5)) ^ ((X3) | (X6)) ^ (X1) ^ (X0))) ^ \
((X3) & (((X1) & (X2)) ^ (X5) ^ (X6))) ^ ((X2) & (X6)) ^ (X0))
/* #define F5(X6, X5, X4, X3, X2, X1, X0) \
((X1) & (X4) ^ (X2) & (X5) ^ (X3) & (X6) ^ \
(X0) & (X1) & (X2) & (X3) ^ (X0) & (X5) ^ (X0))*/
#define F5(X6, X5, X4, X3, X2, X1, X0) \
(((X1) & ((X4) ^ ((X0) & (X2) & (X3)))) ^ \
(((X2) ^ (X0)) & (X5)) ^ ((X3) & (X6)) ^ (X0))
#define p31(x) (x==0 ? 1 : (x==1 ? 0 : (x==2 ? 3 : (x==3 ? 5 : (x==4 ? 6 : (x==5 ? 2 : (x==6 ? 4 : 7)))))))
#define p41(x) (x==0 ? 2 : (x==1 ? 6 : (x==2 ? 1 : (x==3 ? 4 : (x==4 ? 5 : (x==5 ? 3 : (x==6 ? 0 : 7)))))))
#define p51(x) (x==0 ? 3 : (x==1 ? 4 : (x==2 ? 1 : (x==3 ? 0 : (x==4 ? 5 : (x==5 ? 2 : (x==6 ? 6 : 7)))))))
#define p32(x) (x==0 ? 4 : (x==1 ? 2 : (x==2 ? 1 : (x==3 ? 0 : (x==4 ? 5 : (x==5 ? 3 : (x==6 ? 6 : 7)))))))
#define p42(x) (x==0 ? 3 : (x==1 ? 5 : (x==2 ? 2 : (x==3 ? 0 : (x==4 ? 1 : (x==5 ? 6 : (x==6 ? 4 : 7)))))))
#define p52(x) (x==0 ? 6 : (x==1 ? 2 : (x==2 ? 1 : (x==3 ? 0 : (x==4 ? 3 : (x==5 ? 4 : (x==6 ? 5 : 7)))))))
#define p33(x) (x==0 ? 6 : (x==1 ? 1 : (x==2 ? 2 : (x==3 ? 3 : (x==4 ? 4 : (x==5 ? 5 : (x==6 ? 0 : 7)))))))
#define p43(x) (x==0 ? 1 : (x==1 ? 4 : (x==2 ? 3 : (x==3 ? 6 : (x==4 ? 0 : (x==5 ? 2 : (x==6 ? 5 : 7)))))))
#define p53(x) (x==0 ? 2 : (x==1 ? 6 : (x==2 ? 0 : (x==3 ? 4 : (x==4 ? 3 : (x==5 ? 1 : (x==6 ? 5 : 7)))))))
#define p44(x) (x==0 ? 6 : (x==1 ? 4 : (x==2 ? 0 : (x==3 ? 5 : (x==4 ? 2 : (x==5 ? 1 : (x==6 ? 3 : 7)))))))
#define p54(x) (x==0 ? 1 : (x==1 ? 5 : (x==2 ? 3 : (x==3 ? 2 : (x==4 ? 0 : (x==5 ? 4 : (x==6 ? 6 : 7)))))))
#define p55(x) (x==0 ? 2 : (x==1 ? 5 : (x==2 ? 0 : (x==3 ? 6 : (x==4 ? 4 : (x==5 ? 3 : (x==6 ? 1 : 7)))))))
#define t(b,p,x,j) ((b&&((p(x)+8-j)%8<(8-j)))?E:T)[(p(x)+8-j)%8]
#define FF(b, e, F, p, j, w, c) \
T[7-j] = rotrFixed(F(t(b,p,0,j), t(b,p,1,j), t(b,p,2,j), t(b,p,3,j), t(b,p,4,j), t(b,p,5,j), t(b,p,6,j)), 7U) + rotrFixed(t(b,p,7,j), 11U) + w + c; \
if (e) E[7-j] += T[7-j];
#ifdef CRYPTOPP_DOXYGEN_PROCESSING
// Doxygen can't handle these macros
#define Round1(t)
#define Round(t, n)
#else
#define Round1(t) \
for (i=0; i<4; i++) \
{ \
FF(i==0, 0, F1, p##t##1, 0, W[8*i+0], 0); \
FF(i==0, 0, F1, p##t##1, 1, W[8*i+1], 0); \
FF(i==0, 0, F1, p##t##1, 2, W[8*i+2], 0); \
FF(i==0, 0, F1, p##t##1, 3, W[8*i+3], 0); \
FF(i==0, 0, F1, p##t##1, 4, W[8*i+4], 0); \
FF(i==0, 0, F1, p##t##1, 5, W[8*i+5], 0); \
FF(i==0, 0, F1, p##t##1, 6, W[8*i+6], 0); \
FF(i==0, 0, F1, p##t##1, 7, W[8*i+7], 0); \
}
#define Round(t, n) \
for (i=0; i<4; i++) \
{ \
FF(0, t==n && i==3, F##n, p##t##n, 0, W[wi##n[8*i+0]], mc##n[8*i+0]); \
FF(0, t==n && i==3, F##n, p##t##n, 1, W[wi##n[8*i+1]], mc##n[8*i+1]); \
FF(0, t==n && i==3, F##n, p##t##n, 2, W[wi##n[8*i+2]], mc##n[8*i+2]); \
FF(0, t==n && i==3, F##n, p##t##n, 3, W[wi##n[8*i+3]], mc##n[8*i+3]); \
FF(0, t==n && i==3, F##n, p##t##n, 4, W[wi##n[8*i+4]], mc##n[8*i+4]); \
FF(0, t==n && i==3, F##n, p##t##n, 5, W[wi##n[8*i+5]], mc##n[8*i+5]); \
FF(0, t==n && i==3, F##n, p##t##n, 6, W[wi##n[8*i+6]], mc##n[8*i+6]); \
FF(0, t==n && i==3, F##n, p##t##n, 7, W[wi##n[8*i+7]], mc##n[8*i+7]); \
}
#endif
const unsigned int HAVAL::wi2[32] = { 5,14,26,18,11,28, 7,16, 0,23,20,22, 1,10, 4, 8,30, 3,21, 9,17,24,29, 6,19,12,15,13, 2,25,31,27};
const unsigned int HAVAL::wi3[32] = {19, 9, 4,20,28,17, 8,22,29,14,25,12,24,30,16,26,31,15, 7, 3, 1, 0,18,27,13, 6,21,10,23,11, 5, 2};
const unsigned int HAVAL::wi4[32] = {24, 4, 0,14, 2, 7,28,23,26, 6,30,20,18,25,19, 3,22,11,31,21, 8,27,12, 9, 1,29, 5,15,17,10,16,13};
const unsigned int HAVAL::wi5[32] = {27, 3,21,26,17,11,20,29,19, 0,12, 7,13, 8,31,10, 5, 9,14,30,18, 6,28,24, 2,23,16,22, 4, 1,25,15};
const word32 HAVAL::mc2[32] = {
0x452821E6, 0x38D01377, 0xBE5466CF, 0x34E90C6C, 0xC0AC29B7, 0xC97C50DD, 0x3F84D5B5, 0xB5470917
, 0x9216D5D9, 0x8979FB1B, 0xD1310BA6, 0x98DFB5AC, 0x2FFD72DB, 0xD01ADFB7, 0xB8E1AFED, 0x6A267E96
, 0xBA7C9045, 0xF12C7F99, 0x24A19947, 0xB3916CF7, 0x0801F2E2, 0x858EFC16, 0x636920D8, 0x71574E69
, 0xA458FEA3, 0xF4933D7E, 0x0D95748F, 0x728EB658, 0x718BCD58, 0x82154AEE, 0x7B54A41D, 0xC25A59B5};
const word32 HAVAL::mc3[32] = {
0x9C30D539,0x2AF26013,0xC5D1B023,0x286085F0,0xCA417918,0xB8DB38EF,0x8E79DCB0,0x603A180E,
0x6C9E0E8B,0xB01E8A3E,0xD71577C1,0xBD314B27,0x78AF2FDA,0x55605C60,0xE65525F3,0xAA55AB94,
0x57489862,0x63E81440,0x55CA396A,0x2AAB10B6,0xB4CC5C34,0x1141E8CE,0xA15486AF,0x7C72E993,
0xB3EE1411,0x636FBC2A,0x2BA9C55D,0x741831F6,0xCE5C3E16,0x9B87931E,0xAFD6BA33,0x6C24CF5C};
const word32 HAVAL::mc4[32] = {
0x7A325381,0x28958677,0x3B8F4898,0x6B4BB9AF,0xC4BFE81B,0x66282193,0x61D809CC,0xFB21A991,
0x487CAC60,0x5DEC8032,0xEF845D5D,0xE98575B1,0xDC262302,0xEB651B88,0x23893E81,0xD396ACC5,
0x0F6D6FF3,0x83F44239,0x2E0B4482,0xA4842004,0x69C8F04A,0x9E1F9B5E,0x21C66842,0xF6E96C9A,
0x670C9C61,0xABD388F0,0x6A51A0D2,0xD8542F68,0x960FA728,0xAB5133A3,0x6EEF0B6C,0x137A3BE4};
const word32 HAVAL::mc5[32] = {
0xBA3BF050,0x7EFB2A98,0xA1F1651D,0x39AF0176,0x66CA593E,0x82430E88,0x8CEE8619,0x456F9FB4,
0x7D84A5C3,0x3B8B5EBE,0xE06F75D8,0x85C12073,0x401A449F,0x56C16AA6,0x4ED3AA62,0x363F7706,
0x1BFEDF72,0x429B023D,0x37D0D724,0xD00A1248,0xDB0FEAD3,0x49F1C09B,0x075372C9,0x80991B7B,
0x25D479D8,0xF6E8DEF7,0xE3FE501A,0xB6794C3B,0x976CE0BD,0x04C006BA,0xC1A94FB6,0x409F60C4};
void HAVAL3::Transform(word32 *E, const word32 *W)
{
word32 T[8];
unsigned int i;
Round1(3);
Round(3, 2);
Round(3, 3);
memset(T, 0, sizeof(T));
}
void HAVAL4::Transform(word32 *E, const word32 *W)
{
word32 T[8];
unsigned int i;
Round1(4);
Round(4, 2);
Round(4, 3);
Round(4, 4);
memset(T, 0, sizeof(T));
}
void HAVAL5::Transform(word32 *E, const word32 *W)
{
word32 T[8];
unsigned int i;
Round1(5);
Round(5, 2);
Round(5, 3);
Round(5, 4);
Round(5, 5);
memset(T, 0, sizeof(T));
}
NAMESPACE_END
|