-
Notifications
You must be signed in to change notification settings - Fork 2
/
quark.c
615 lines (487 loc) · 17.1 KB
/
quark.c
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*
Quark reference C implementation
Copyright (c) 2010-2014 Jean-Philippe Aumasson <[email protected]>
To the extent possible under law, the author(s) have dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with
this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#if defined(UQUARK)
#define CAPACITY 16
#define RATE 1
#define WIDTH 17
#elif defined(DQUARK)
#define CAPACITY 20
#define RATE 2
#define WIDTH 22
#elif defined(SQUARK)
#define CAPACITY 28
#define RATE 4
#define WIDTH 32
#elif defined(CQUARK)
#define CAPACITY 40
#define RATE 8
#define WIDTH 48
#endif
#define DIGEST WIDTH
typedef uint64_t u64;
typedef uint32_t u32;
typedef uint8_t u8;
typedef struct
{
int pos; /* number of bytes read into x from current block */
u32 x[ WIDTH*8 ]; /* one bit stored in each word */
} hashState;
#if defined(UQUARK)
/* 17 bytes */
u8 iv[] = {0xd8,0xda,0xca,0x44,0x41,0x4a,0x09,0x97,
0x19,0xc8,0x0a,0xa3,0xaf,0x06,0x56,0x44,0xdb
};
#elif defined(DQUARK)
/* 22 bytes */
u8 iv[] = {0xcc,0x6c,0x4a,0xb7,0xd1,0x1f,0xa9,0xbd,
0xf6,0xee,0xde,0x03,0xd8,0x7b,0x68,0xf9,
0x1b,0xaa,0x70,0x6c,0x20,0xe9
};
#elif defined(SQUARK)
/* 32 bytes */
u8 iv[] = {0x39,0x72,0x51,0xce,0xe1,0xde,0x8a,0xa7,
0x3e,0xa2,0x62,0x50,0xc6,0xd7,0xbe,0x12,
0x8c,0xd3,0xe7,0x9d,0xd7,0x18,0xc2,0x4b,
0x8a,0x19,0xd0,0x9c,0x24,0x92,0xda,0x5d
};
#elif defined(CQUARK)
/* 48 bytes */
u8 iv[] = {0x3b,0x45,0x03,0xec,0x76,0x62,0xc3,0xcb,
0x30,0xe0,0x08,0x37,0xec,0x8d,0x38,0xbb,
0xe5,0xff,0x5a,0xcd,0x69,0x01,0xa2,0x49,
0x57,0x50,0xf9,0x19,0x8e,0x2e,0x3b,0x58,
0x52,0xdc,0xaa,0x16,0x62,0xb7,0xda,0xd6,
0x5f,0xcb,0x5a,0x8a,0x1f,0x0d,0x5f,0xcc
};
#endif
void showstate( u32 *x )
{
int i;
u8 buf=0;
for( i=0; i<8*WIDTH; ++i )
{
buf ^= ( 1&x[i] )<<( 7-( i%8 ) );
if ( ( ( i%8 )==7 ) && ( i ) )
{
printf( "%02x",buf );
buf=0;
}
}
printf( "\n" );
}
int permute_u( u32 *x )
{
/* state of 136=2x68 bits */
#define ROUNDS_U 4*136
#define N_LEN_U 68
#define L_LEN_U 10
u32 *X, *Y, *L;
u32 h;
int i;
X = ( u32 * )malloc( ( N_LEN_U+ROUNDS_U )*sizeof( u32 ) );
Y = ( u32 * )malloc( ( N_LEN_U+ROUNDS_U )*sizeof( u32 ) );
L = ( u32 * )malloc( ( L_LEN_U+ROUNDS_U )*sizeof( u32 ) );
/* local copy of the state in the registers*/
for( i=0; i< N_LEN_U; ++i )
{
X[i]=x[i];
Y[i]=x[i+N_LEN_U];
}
/* initialize the LFSR to 11..11 */
for( i=0; i< L_LEN_U; ++i )
L[i]=0xFFFFFFFF;
/* iterate rounds */
for( i=0; i< ROUNDS_U; ++i )
{
/* indices up to i+59, for 8x parallelizibility*/
/* need X[i] as linear term only, for invertibility */
X[N_LEN_U+i] = X[i] ^ Y[i];
X[N_LEN_U+i] ^= X[i+9] ^ X[i+14] ^ X[i+21] ^ X[i+28] ^
X[i+33] ^ X[i+37] ^ X[i+45] ^ X[i+52] ^ X[i+55] ^ X[i+50] ^
( X[i+59] & X[i+55] ) ^ ( X[i+37] & X[i+33] ) ^ ( X[i+15] & X[i+9] ) ^
( X[i+55] & X[i+52] & X[i+45] ) ^ ( X[i+33] & X[i+28] & X[i+21] ) ^
( X[i+59] & X[i+45] & X[i+28] & X[i+9] ) ^
( X[i+55] & X[i+52] & X[i+37] & X[i+33] ) ^
( X[i+59] & X[i+55] & X[i+21] & X[i+15] ) ^
( X[i+59] & X[i+55] & X[i+52] & X[i+45] & X[i+37] ) ^
( X[i+33] & X[i+28] & X[i+21] & X[i+15] & X[i+9] ) ^
( X[i+52] & X[i+45] & X[i+37] & X[i+33] & X[i+28] & X[i+21] );
/* need Y[i] as linear term only, for invertibility */
Y[N_LEN_U+i] = Y[i];
Y[N_LEN_U+i] ^= Y[i+7] ^ Y[i+16] ^ Y[i+20] ^ Y[i+30] ^
Y[i+35] ^ Y[i+37] ^ Y[i+42] ^ Y[i+51] ^ Y[i+54] ^ Y[i+49] ^
( Y[i+58] & Y[i+54] ) ^ ( Y[i+37] & Y[i+35] ) ^ ( Y[i+15] & Y[i+7] ) ^
( Y[i+54] & Y[i+51] & Y[i+42] ) ^ ( Y[i+35] & Y[i+30] & Y[i+20] ) ^
( Y[i+58] & Y[i+42] & Y[i+30] & Y[i+7] ) ^
( Y[i+54] & Y[i+51] & Y[i+37] & Y[i+35] ) ^
( Y[i+58] & Y[i+54] & Y[i+20] & Y[i+15] ) ^
( Y[i+58] & Y[i+54] & Y[i+51] & Y[i+42] & Y[i+37] ) ^
( Y[i+35] & Y[i+30] & Y[i+20] & Y[i+15] & Y[i+7] ) ^
( Y[i+51] & Y[i+42] & Y[i+37] & Y[i+35] & Y[i+30] & Y[i+20] );
/* need L[i] as linear term only, for invertibility */
L[L_LEN_U+i] = L[i];
L[L_LEN_U+i] ^= L[i+3];
/* compute output of the h function */
h = X[i+25] ^ Y[i+59] ^ ( Y[i+3] & X[i+55] ) ^ ( X[i+46] & X[i+55] ) ^ ( X[i+55] & Y[i+59] ) ^
( Y[i+3] & X[i+25] & X[i+46] ) ^ ( Y[i+3] & X[i+46] & X[i+55] ) ^ ( Y[i+3] & X[i+46] & Y[i+59] ) ^
( X[i+25] & X[i+46] & Y[i+59] & L[i] ) ^ ( X[i+25] & L[i] );
h ^= X[i+1] ^ Y[i+2] ^ X[i+4] ^ Y[i+10] ^ X[i+31] ^ Y[i+43] ^ X[i+56] ^ L[i];
/* feedback of h into the registers */
X[N_LEN_U+i] ^= h;
Y[N_LEN_U+i] ^= h;
}
/* copy final state into hashState */
for( i=0; i< N_LEN_U; ++i )
{
x[i] = X[ROUNDS_U+i];
x[i+N_LEN_U] = Y[ROUNDS_U+i];
}
free( X );
free( Y );
free( L );
return 0;
}
int permute_d( u32 *x )
{
/* state of 176=2x88 bits */
#define ROUNDS_D 4*176
#define N_LEN_D 88
#define L_LEN_D 10
u32 *X, *Y, *L;
u32 h;
int i;
X = ( u32 * )malloc( ( N_LEN_D+ROUNDS_D )*sizeof( u32 ) );
Y = ( u32 * )malloc( ( N_LEN_D+ROUNDS_D )*sizeof( u32 ) );
L = ( u32 * )malloc( ( L_LEN_D+ROUNDS_D )*sizeof( u32 ) );
/* local copy of the state in the registers*/
for( i=0; i< N_LEN_D; ++i )
{
X[i]=x[i];
Y[i]=x[i+N_LEN_D];
}
/* initialize the LFSR to 11..11 */
for( i=0; i< L_LEN_D; ++i )
L[i]=0xFFFFFFFF;
/* iterate rounds */
for( i=0; i< ROUNDS_D; ++i )
{
/* need X[i] as linear term only, for invertibility */
X[N_LEN_D+i] = X[i] ^ Y[i];
X[N_LEN_D+i] ^= X[i+11] ^ X[i+18] ^ X[i+27] ^ X[i+36] ^
X[i+42] ^ X[i+47] ^ X[i+58] ^ X[i+67] ^ X[i+71] ^ X[i+64] ^
( X[i+79] & X[i+71] ) ^ ( X[i+47] & X[i+42] ) ^ ( X[i+19] & X[i+11] ) ^
( X[i+71] & X[i+67] & X[i+58] ) ^ ( X[i+42] & X[i+36] & X[i+27] ) ^
( X[i+79] & X[i+58] & X[i+36] & X[i+11] ) ^
( X[i+71] & X[i+67] & X[i+47] & X[i+42] ) ^
( X[i+79] & X[i+71] & X[i+27] & X[i+19] ) ^
( X[i+79] & X[i+71] & X[i+67] & X[i+58] & X[i+47] ) ^
( X[i+42] & X[i+36] & X[i+27] & X[i+19] & X[i+11] ) ^
( X[i+67] & X[i+58] & X[i+47] & X[i+42] & X[i+36] & X[i+27] );
/* need Y[i] as linear term only, for invertibility */
Y[N_LEN_D+i] = Y[i];
Y[N_LEN_D+i] ^= Y[i+9] ^ Y[i+20] ^ Y[i+25] ^ Y[i+38] ^
Y[i+44] ^ Y[i+47] ^ Y[i+54] ^ Y[i+67] ^ Y[i+69] ^ Y[i+63] ^
( Y[i+78] & Y[i+69] ) ^ ( Y[i+47] & Y[i+44] ) ^ ( Y[i+19] & Y[i+9] ) ^
( Y[i+69] & Y[i+67] & Y[i+54] ) ^ ( Y[i+44] & Y[i+38] & Y[i+25] ) ^
( Y[i+78] & Y[i+54] & Y[i+38] & Y[i+9] ) ^
( Y[i+69] & Y[i+67] & Y[i+47] & Y[i+44] ) ^
( Y[i+78] & Y[i+69] & Y[i+25] & Y[i+19] ) ^
( Y[i+78] & Y[i+69] & Y[i+67] & Y[i+54] & Y[i+47] ) ^
( Y[i+44] & Y[i+38] & Y[i+25] & Y[i+19] & Y[i+9] ) ^
( Y[i+67] & Y[i+54] & Y[i+47] & Y[i+44] & Y[i+38] & Y[i+25] );
/* need L[i] as linear term only, for invertibility */
L[L_LEN_D+i] = L[i];
L[L_LEN_D+i] ^= L[i+3]; // linear feedback here
/* compute output of the h function */
h = X[i+35] ^ Y[i+79] ^ ( Y[i+4] & X[i+68] ) ^ ( X[i+57] & X[i+68] ) ^ ( X[i+68] & Y[i+79] ) ^
( Y[i+4] & X[i+35] & X[i+57] ) ^ ( Y[i+4] & X[i+57] & X[i+68] ) ^ ( Y[i+4] & X[i+57] & Y[i+79] ) ^
( X[i+35] & X[i+57] & Y[i+79] & L[i] ) ^ ( X[i+35] & L[i] );
h ^= X[i+1] ^ Y[i+2] ^ X[i+5] ^ Y[i+12] ^ X[i+40] ^ Y[i+55] ^ X[i+72] ^ L[i];
h ^= Y[i+24] ^ X[i+48] ^ Y[i+61];
/* feedback of h into the registers */
X[N_LEN_D+i] ^= h;
Y[N_LEN_D+i] ^= h;
}
/* copy final state into hashState */
for( i=0; i< N_LEN_D; ++i )
{
x[i] = X[ROUNDS_D+i];
x[i+N_LEN_D] = Y[ROUNDS_D+i];
}
free( X );
free( Y );
free( L );
return 0;
}
int permute_s( u32 *x )
{
/* state of 256=2x128 bits */
#define ROUNDS_S 4*256
#define N_LEN_S 128
#define L_LEN_S 10
u32 *X, *Y, *L;
u32 h;
int i;
X = ( u32 * )malloc( ( N_LEN_S+ROUNDS_S )*sizeof( u32 ) );
Y = ( u32 * )malloc( ( N_LEN_S+ROUNDS_S )*sizeof( u32 ) );
L = ( u32 * )malloc( ( L_LEN_S+ROUNDS_S )*sizeof( u32 ) );
/* local copy of the state in the registers*/
for( i=0; i< N_LEN_S; ++i )
{
X[i]=x[i];
Y[i]=x[i+N_LEN_S];
}
/* initialize the LFSR to 11..11 */
for( i=0; i< L_LEN_S; ++i )
L[i]=0xFFFFFFFF;
/* iterate rounds */
for( i=0; i< ROUNDS_S; ++i )
{
/* need X[i] as linear term only, for invertibility */
X[N_LEN_S+i] = X[i] ^ Y[i];
X[N_LEN_S+i] ^= X[i+16] ^ X[i+26] ^ X[i+39] ^ X[i+52] ^
X[i+61] ^ X[i+69] ^ X[i+84] ^ X[i+97] ^ X[i+103] ^ X[i+94] ^
( X[i+111] & X[i+103] ) ^ ( X[i+69] & X[i+61] ) ^ ( X[i+28] & X[i+16] ) ^
( X[i+103] & X[i+97] & X[i+84] ) ^ ( X[i+61] & X[i+52] & X[i+39] ) ^
( X[i+111] & X[i+84] & X[i+52] & X[i+16] ) ^
( X[i+103] & X[i+97] & X[i+69] & X[i+61] ) ^
( X[i+111] & X[i+103] & X[i+39] & X[i+28] ) ^
( X[i+111] & X[i+103] & X[i+97] & X[i+84] & X[i+69] ) ^
( X[i+61] & X[i+52] & X[i+39] & X[i+28] & X[i+16] ) ^
( X[i+97] & X[i+84] & X[i+69] & X[i+61] & X[i+52] & X[i+39] );
/* need Y[i] as linear term only, for invertibility */
Y[N_LEN_S+i] = Y[i];
Y[N_LEN_S+i] ^= Y[i+13] ^ Y[i+30] ^ Y[i+37] ^ Y[i+56] ^
Y[i+65] ^ Y[i+69] ^ Y[i+79] ^ Y[i+96] ^ Y[i+101] ^ Y[i+92] ^
( Y[i+109] & Y[i+101] ) ^ ( Y[i+69] & Y[i+65] ) ^ ( Y[i+28] & Y[i+13] ) ^
( Y[i+101] & Y[i+96] & Y[i+79] ) ^ ( Y[i+65] & Y[i+56] & Y[i+37] ) ^
( Y[i+109] & Y[i+79] & Y[i+56] & Y[i+13] ) ^
( Y[i+101] & Y[i+96] & Y[i+69] & Y[i+65] ) ^
( Y[i+109] & Y[i+101] & Y[i+37] & Y[i+28] ) ^
( Y[i+109] & Y[i+101] & Y[i+96] & Y[i+79] & Y[i+69] ) ^
( Y[i+65] & Y[i+56] & Y[i+37] & Y[i+28] & Y[i+13] ) ^
( Y[i+96] & Y[i+79] & Y[i+69] & Y[i+65] & Y[i+56] & Y[i+37] );
/* need L[i] as linear term only, for invertibility */
L[L_LEN_S+i] = L[i];
L[L_LEN_S+i] ^= L[i+3]; // linear feedback here
/* compute output of the h function */
h = X[i+47] ^ Y[i+111] ^ ( Y[i+8] & X[i+100] ) ^ ( X[i+72] & X[i+100] ) ^ ( X[i+100] & Y[i+111] ) ^
( Y[i+8] & X[i+47] & X[i+72] ) ^ ( Y[i+8] & X[i+72] & X[i+100] ) ^ ( Y[i+8] & X[i+72] & Y[i+111] ) ^
( X[i+47] & X[i+72] & Y[i+111] & L[i] ) ^ ( X[i+47] & L[i] );
h ^= X[i+1] ^ Y[i+3] ^ X[i+7] ^ Y[i+18] ^ X[i+58] ^ Y[i+80] ^ X[i+105] ^ L[i];
h ^= Y[i+34] ^ Y[i+71] ^ X[i+90] ^ Y[i+91];
/* feedback of h into the registers */
X[N_LEN_S+i] ^= h;
Y[N_LEN_S+i] ^= h;
}
/* copy final state into hashState */
for( i=0; i< N_LEN_S; ++i )
{
x[i] = X[ROUNDS_S+i];
x[i+N_LEN_S] = Y[ROUNDS_S+i];
}
free( X );
free( Y );
free( L );
return 0;
}
int permute_c( u32 *x )
{
/* state of 384=2x192 bits */
#define ROUNDS_C 2*384
#define N_LEN_C 192
#define L_LEN_C 16
u32 *X, *Y, *L;
u32 h;
int i;
X = ( u32 * )malloc( ( N_LEN_C+ROUNDS_C )*sizeof( u32 ) );
Y = ( u32 * )malloc( ( N_LEN_C+ROUNDS_C )*sizeof( u32 ) );
L = ( u32 * )malloc( ( L_LEN_C+ROUNDS_C )*sizeof( u32 ) );
/* local copy of the state in the registers*/
for( i=0; i< N_LEN_C; ++i )
{
X[i]=x[i];
Y[i]=x[i+N_LEN_C];
}
/* initialize the LFSR to 11..11 */
for( i=0; i< L_LEN_C; ++i )
L[i]=0xFFFFFFFF;
/* iterate rounds */
for( i=0; i< ROUNDS_C; ++i )
{
X[N_LEN_C+i] = X[i] ^ Y[i];
X[N_LEN_C+i] ^= X[i+13] ^ X[i+34] ^ X[i+65] ^ X[i+77] ^
X[i+94] ^ X[i+109] ^ X[i+127] ^ X[i+145] ^ X[i+157] ^ X[i+140] ^
( X[i+159] & X[i+157] ) ^ ( X[i+109] & X[i+94] ) ^ ( X[i+47] & X[i+13] ) ^
( X[i+157] & X[i+145] & X[i+127] ) ^ ( X[i+94] & X[i+77] & X[i+65] ) ^
( X[i+159] & X[i+127] & X[i+77] & X[i+13] ) ^
( X[i+157] & X[i+145] & X[i+109] & X[i+94] ) ^
( X[i+159] & X[i+157] & X[i+65] & X[i+47] ) ^
( X[i+159] & X[i+157] & X[i+145] & X[i+127] & X[i+109] ) ^
( X[i+94] & X[i+77] & X[i+65] & X[i+47] & X[i+13] ) ^
( X[i+145] & X[i+127] & X[i+109] & X[i+94] & X[i+77] & X[i+65] );
Y[N_LEN_C+i] = Y[i];
Y[N_LEN_C+i] ^= Y[i+21] ^ Y[i+57] ^ Y[i+60] ^ Y[i+94] ^
Y[i+112] ^ Y[i+125] ^ Y[i+133] ^ Y[i+152] ^ Y[i+157] ^ Y[i+146] ^
( Y[i+159] & Y[i+157] ) ^ ( Y[i+125] & Y[i+112] ) ^ ( Y[i+36] & Y[i+21] ) ^
( Y[i+157] & Y[i+152] & Y[i+133] ) ^ ( Y[i+112] & Y[i+94] & Y[i+60] ) ^
( Y[i+159] & Y[i+133] & Y[i+94] & Y[i+21] ) ^
( Y[i+157] & Y[i+152] & Y[i+125] & Y[i+112] ) ^
( Y[i+159] & Y[i+157] & Y[i+60] & Y[i+36] ) ^
( Y[i+159] & Y[i+157] & Y[i+152] & Y[i+133] & Y[i+125] ) ^
( Y[i+112] & Y[i+94] & Y[i+60] & Y[i+36] & Y[i+21] ) ^
( Y[i+152] & Y[i+133] & Y[i+125] & Y[i+112] & Y[i+94] & Y[i+60] );
L[L_LEN_C+i] = L[i] ^ L[i+2] ^ L[i+3] ^ L[i+5];
h = X[i+25] ^ Y[i+59] ^ ( Y[i+3] & X[i+55] ) ^ ( X[i+46] & X[i+55] ) ^ ( X[i+55] & Y[i+59] ) ^
( Y[i+3] & X[i+25] & X[i+46] ) ^ ( Y[i+3] & X[i+46] & X[i+55] ) ^ ( Y[i+3] & X[i+46] & Y[i+59] ) ^
( X[i+25] & X[i+46] & Y[i+59] & L[i] ) ^ ( X[i+25] & L[i] );
h ^= L[i];
h ^= X[i+4] ^ X[i+28] ^ X[i+40] ^ X[i+85] ^ X[i+112] ^ X[i+141] ^ X[i+146] ^ X[i+152];
h ^= Y[i+2] ^ Y[i+33] ^ Y[i+60] ^ Y[i+62] ^ Y[i+ 87] ^ Y[i+ 99] ^ Y[i+138] ^ Y[i+148];
X[N_LEN_C+i] ^= h;
Y[N_LEN_C+i] ^= h;
}
/* copy final state into hashState */
for( i=0; i< N_LEN_C; ++i )
{
x[i] = X[ROUNDS_C+i];
x[i+N_LEN_C] = Y[ROUNDS_C+i];
}
free( X );
free( Y );
free( L );
return 0;
}
/* permutation of the state */
static void permute( u32 *x )
{
#ifdef DEBUG
printf( "enter permute\n" );
showstate( x );
#endif
#if defined(UQUARK)
permute_u( x );
#elif defined(DQUARK)
permute_d( x );
#elif defined(SQUARK)
permute_s( x );
#elif defined(CQUARK)
permute_c( x );
#endif
#ifdef DEBUG
printf( "permute done\n" );
showstate( x );
#endif
}
/* initialization of the IV */
int init( hashState *state )
{
int i;
#ifdef DEBUG
printf( "enter init\n" );
#endif
/* initialize state */
for ( i = 0; i < 8*WIDTH; ++i )
state->x[i] = ( iv[i/8]>>( 7-( i%8 ) ) )&1;
state->pos = 0;
#ifdef DEBUG
printf( "init done\n" );
showstate( state->x );
#endif
return 0;
}
int update( hashState *state, const u8 *data, int databytelen )
{
/* caller promises us that previous data had integral number of bytes */
/* so state->pos is a multiple of 8 */
int i;
#ifdef DEBUG
printf( "enter update\n" );
#endif
while ( databytelen > 0 )
{
/* get next byte */
u8 u = *data;
#ifdef DEBUG
printf( "get byte %02x at pos %d\n", u, state->pos );
#endif
/* xor state with each bit */
for( i=8*state->pos; i<8*state->pos+8; ++i )
{
state->x[( 8*( WIDTH-RATE ) )+i] ^= ( u>>( i%8 ) )&1;
}
data += 1;
databytelen -= 1;
state->pos += 1;
if ( state->pos == RATE )
{
permute( state->x );
state->pos = 0;
}
}
#ifdef DEBUG
printf( "update done\n" );
#endif
return 0;
}
/* finalize (padding) and return digest */
int final( hashState *state, u8 *out )
{
int i;
int outbytes=0;
u8 u;
#ifdef DEBUG
printf( "enter final\n" );
#endif
/* append '1' bit */
state->x[8*( WIDTH-RATE )+state->pos*8] ^= 1;
/* permute to obtain first final state*/
permute( state->x );
/* zeroize output buffer */
for( i=0; i<DIGEST; ++i )
out[i]=0;
/* while output requested, extract RATE bytes and permute */
while ( outbytes < DIGEST )
{
/* extract one byte */
for( i=0; i<8; ++i )
{
u = state->x[8*( WIDTH-RATE )+i+8*( outbytes%RATE )] &1;
out[outbytes] ^= ( u << ( 7-i ) );
}
#ifdef DEBUG
printf( "extracted byte %02x (%d)\n",out[outbytes],outbytes );
#endif
outbytes += 1;
if ( outbytes == DIGEST )
break;
/* if RATE bytes extracted, permute again */
if ( ! ( outbytes % RATE ) )
{
permute( state->x );
}
}
#ifdef DEBUG
printf( "final done\n" );
#endif
return 0;
}
int quark( u8 *out, const u8 *in, u64 inlen )
{
/* inlen in bytes */
hashState state;
init( &state );
update( &state, in, inlen );
final( &state, out );
return 0;
}