-
Notifications
You must be signed in to change notification settings - Fork 0
/
tf1024.c
683 lines (560 loc) · 20.1 KB
/
tf1024.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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
#include "tf1024.h"
#include <string.h>
#define THREEFISH_PARITY 0x1bd11bdaa9fc1a22ULL
static void tf1024_init_for_skein(tfc1024_ctx *ctx) {
size_t i;
ctx->K[TF_ARRAY_SZ(ctx->K) - 1] = THREEFISH_PARITY;
for (i = 0; i < TF_ARRAY_SZ(ctx->K) - 1; i++)
ctx->K[TF_ARRAY_SZ(ctx->K) - 1] ^= ctx->K[i];
ctx->T[2] = ctx->T[0] ^ ctx->T[1];
}
#ifdef TF_NEED_CORE
void tfc1024_init(tfc1024_ctx *ctx) { memset(ctx, 0, sizeof(tfc1024_ctx)); }
void tfc1024_done(tfc1024_ctx *ctx) { memset(ctx, 0, sizeof(tfc1024_ctx)); }
void tfc1024_set_key(tfc1024_ctx *ctx, const void *key, size_t klen) {
TF_UNIT_TYPE parity = THREEFISH_PARITY;
size_t i;
if (klen > TF_KEY_SIZE)
return;
memcpy(ctx->K, key, klen);
memset((TF_BYTE_TYPE *)ctx->K + klen, 0, TF_KEY_SIZE - klen);
data_to_little(ctx->K, sizeof(ctx->K));
for (i = 0; i < TF_NR_UNITS; i++)
parity ^= ctx->K[i];
ctx->K[i] = parity;
}
void tfc1024_set_tweak(tfc1024_ctx *ctx, const void *tweak) {
const TF_UNIT_TYPE *tw = tweak;
ctx->T[0] = tw[0];
ctx->T[1] = tw[1];
data_to_little(ctx->T, sizeof(ctx->T));
ctx->T[2] = ctx->T[0] ^ ctx->T[1];
}
#endif
#ifdef TF_NEED_MODES
void tf1024_init(tf1024_ctx *ctx) { memset(ctx, 0, sizeof(tf1024_ctx)); }
void tf1024_done(tf1024_ctx *ctx) { memset(ctx, 0, sizeof(tf1024_ctx)); }
void tf1024_start_counter(tf1024_ctx *ctx, const void *ctr) {
#ifdef TF_NEED_CTR_BACKUP
memcpy(ctx->ictr, ctr, sizeof(ctx->ictr));
#endif
memcpy(ctx->ctr, ctr, sizeof(ctx->ctr));
#ifdef TF_NEED_CTR_BACKUP
data_to_little(ctx->ictr, sizeof(ctx->ictr));
#endif
data_to_little(ctx->ctr, sizeof(ctx->ctr));
}
void tf1024_rewind_counter(tf1024_ctx *ctx, const void *newctr, size_t ctrsz) {
#ifdef TF_NEED_CTR_BACKUP
memcpy(ctx->ctr, ctx->ictr, sizeof(ctx->ctr));
#endif
if (newctr && ctrsz)
ctr_add(ctx->ctr, newctr,
ctrsz > TF_ARRAY_SZ(ctx->ctr) ? TF_ARRAY_SZ(ctx->ctr) : ctrsz);
}
#ifdef TF_NEED_TCTR_MODE
void tf1024_start_counter_tctr(tfc1024_ctx *ctx, const void *ctr) {
tfc1024_set_tweak(ctx, ctr);
#ifdef TF_NEED_CTR_BACKUP
memcpy(ctx->iT, ctx->T, sizeof(ctx->iT));
#endif
}
void tf1024_rewind_counter_tctr(tfc1024_ctx *ctx, const void *newctr,
size_t ctrsz) {
#ifdef TF_NEED_CTR_BACKUP
memcpy(ctx->T, ctx->iT, sizeof(ctx->T));
#endif
if (newctr && ctrsz)
ctr_add(ctx->T, newctr,
ctrsz > TF_ARRAY_SZ(ctx->T) ? TF_ARRAY_SZ(ctx->T) : ctrsz);
}
#endif
#ifdef TF_NEED_CTR_MODE
/* CTR mode threefish */
void tf1024_crypt(tf1024_ctx *ctx, const void *src, size_t slen, void *dst) {
const TF_BYTE_TYPE *usrc = src;
TF_BYTE_TYPE *udst = dst;
TF_UNIT_TYPE x[TF_BLOCK_UNITS], y[TF_BLOCK_UNITS];
size_t sl = slen, i;
if (sl > TF_BLOCK_SIZE) {
do {
/* Load src data, convert to LE if necessary */
memcpy(x, usrc, TF_BLOCK_SIZE);
usrc += TF_BLOCK_SIZE;
data_to_little(x, TF_BLOCK_SIZE);
/* Adjust counter, process data */
ctr_inc(ctx->ctr, TF_BLOCK_UNITS);
tfc1024_encrypt_blk(&ctx->tfc, ctx->ctr, y);
for (i = 0; i < TF_BLOCK_UNITS; i++)
y[i] ^= x[i];
/* Convert from LE if necessary, store result data */
data_to_little(y, TF_BLOCK_SIZE);
memcpy(udst, y, TF_BLOCK_SIZE);
udst += TF_BLOCK_SIZE;
} while ((sl -= TF_BLOCK_SIZE) >= TF_BLOCK_SIZE);
}
/* If there is remaining, then process it partially */
if (sl) {
memset(x, 0, TF_BLOCK_SIZE);
memcpy(x, usrc, sl);
data_to_little(x, TF_BLOCK_SIZE);
ctr_inc(ctx->ctr, TF_BLOCK_UNITS);
tfc1024_encrypt_blk(&ctx->tfc, ctx->ctr, y);
for (i = 0; i < TF_BLOCK_UNITS; i++)
y[i] ^= x[i];
data_to_little(y, TF_BLOCK_SIZE);
memcpy(udst, y, sl);
}
}
#endif
#ifdef TF_NEED_TCTR_MODE
/* Tweak CTR mode threefish. */
void tf1024_tctr_encrypt(tfc1024_ctx *ctx, const void *src, size_t slen,
void *dst) {
const TF_BYTE_TYPE *usrc = src;
TF_BYTE_TYPE *udst = dst;
TF_UNIT_TYPE x[TF_BLOCK_UNITS], y[TF_BLOCK_UNITS];
size_t sl = slen, i;
if (sl >= TF_BLOCK_SIZE) {
do {
/* Load src data, convert to LE if necessary */
memcpy(x, usrc, TF_BLOCK_SIZE);
usrc += TF_BLOCK_SIZE;
data_to_little(x, TF_BLOCK_SIZE);
/*
* Increment tweak, up to it's full size,
* 192 bits wide counter.
*/
ctr_inc(ctx->T, TF_ARRAY_SZ(ctx->T));
/* Encrypt plaintext. It's always going to be different. */
tfc1024_encrypt_blk(ctx, x, y);
/* Convert from LE if necessary, store result data */
data_to_little(y, TF_BLOCK_SIZE);
memcpy(udst, y, TF_BLOCK_SIZE);
udst += TF_BLOCK_SIZE;
} while ((sl -= TF_BLOCK_SIZE) >= TF_BLOCK_SIZE);
}
/*
* If there is remaining, then process it partially using plain CTR mode.
* This will automatically become TCTR again if block will be padded by
* caller.
*/
if (sl) {
memset(x, 0, TF_BLOCK_SIZE);
memcpy(x, usrc, sl);
data_to_little(x, TF_BLOCK_SIZE);
memset(y, 0, TF_BLOCK_SIZE);
memcpy(y, ctx->T, sizeof(ctx->T));
ctr_inc(y, TF_BLOCK_UNITS);
tfc1024_encrypt_blk(ctx, y, y);
for (i = 0; i < TF_BLOCK_UNITS; i++)
y[i] ^= x[i];
data_to_little(y, TF_BLOCK_SIZE);
memcpy(udst, y, sl);
}
}
void tf1024_tctr_decrypt(tfc1024_ctx *ctx, const void *src, size_t slen,
void *dst) {
const TF_BYTE_TYPE *usrc = src;
TF_BYTE_TYPE *udst = dst;
TF_UNIT_TYPE x[TF_BLOCK_UNITS], y[TF_BLOCK_UNITS];
size_t sl = slen, i;
if (sl >= TF_BLOCK_SIZE) {
do {
/* Load src data, convert to LE if necessary */
memcpy(x, usrc, TF_BLOCK_SIZE);
usrc += TF_BLOCK_SIZE;
data_to_little(x, TF_BLOCK_SIZE);
/* Prepare known tweak */
ctr_inc(ctx->T, TF_ARRAY_SZ(ctx->T));
/* Decrypt ciphertext */
tfc1024_decrypt_blk(ctx, x, y);
/* Convert from LE if necessary, store result data */
data_to_little(y, TF_BLOCK_SIZE);
memcpy(udst, y, TF_BLOCK_SIZE);
udst += TF_BLOCK_SIZE;
} while ((sl -= TF_BLOCK_SIZE) >= TF_BLOCK_SIZE);
}
/*
* If there is remaining, then process it partially using plain CTR mode.
* This will automatically become TCTR again if block will be padded by
* caller.
*/
if (sl) {
memset(x, 0, TF_BLOCK_SIZE);
memcpy(x, usrc, sl);
data_to_little(x, TF_BLOCK_SIZE);
memset(y, 0, TF_BLOCK_SIZE);
memcpy(y, ctx->T, sizeof(ctx->T));
ctr_inc(y, TF_BLOCK_UNITS);
tfc1024_encrypt_blk(ctx, y, y);
for (i = 0; i < TF_BLOCK_UNITS; i++)
y[i] ^= x[i];
data_to_little(y, TF_BLOCK_SIZE);
memcpy(udst, y, sl);
}
}
#endif /* TF_NEED_TCTR_MODE */
#ifdef TF_NEED_CBC_MODE
/* CBC mode threefish. */
void tf1024_cbc_encrypt(tf1024_ctx *ctx, const void *src, size_t slen,
void *dst) {
const TF_BYTE_TYPE *usrc = src;
TF_BYTE_TYPE *udst = dst;
TF_UNIT_TYPE x[TF_BLOCK_UNITS], y[TF_BLOCK_UNITS];
size_t sl = slen, i;
if (sl >= TF_BLOCK_SIZE) {
do {
/* Load src data, convert to LE if necessary */
memcpy(x, usrc, TF_BLOCK_SIZE);
usrc += TF_BLOCK_SIZE;
data_to_little(x, TF_BLOCK_SIZE);
/* Add plaintext to IV or previoud block */
for (i = 0; i < TF_BLOCK_UNITS; i++)
y[i] = x[i] ^ ctx->ctr[i];
/* Encrypt result */
tfc1024_encrypt_blk(&ctx->tfc, y, x);
/* Save result for next block */
memcpy(ctx->ctr, x, TF_BLOCK_SIZE);
/* Convert from LE if necessary, store result data */
data_to_little(x, TF_BLOCK_SIZE);
memcpy(udst, x, TF_BLOCK_SIZE);
udst += TF_BLOCK_SIZE;
} while ((sl -= TF_BLOCK_SIZE) >= TF_BLOCK_SIZE);
}
/*
* If there is remaining, then process it partially using plain CTR mode.
* This will automatically become CBC again if block will be padded by
* caller.
*/
if (sl) {
memset(x, 0, TF_BLOCK_SIZE);
memcpy(x, usrc, sl);
data_to_little(x, TF_BLOCK_SIZE);
ctr_inc(ctx->ctr, TF_BLOCK_UNITS);
tfc1024_encrypt_blk(&ctx->tfc, ctx->ctr, y);
for (i = 0; i < TF_BLOCK_UNITS; i++)
y[i] ^= x[i];
data_to_little(y, TF_BLOCK_SIZE);
memcpy(udst, y, sl);
}
}
void tf1024_cbc_decrypt(tf1024_ctx *ctx, const void *src, size_t slen,
void *dst) {
const TF_BYTE_TYPE *usrc = src;
TF_BYTE_TYPE *udst = dst;
TF_UNIT_TYPE x[TF_BLOCK_UNITS], y[TF_BLOCK_UNITS], t[TF_BLOCK_UNITS];
size_t sl = slen, i;
if (sl >= TF_BLOCK_SIZE) {
do {
/* Load src data, convert to LE if necessary */
memcpy(x, usrc, TF_BLOCK_SIZE);
usrc += TF_BLOCK_SIZE;
data_to_little(x, TF_BLOCK_SIZE);
/* Save temporary */
memcpy(t, x, TF_BLOCK_SIZE);
/* Decrypt block */
tfc1024_decrypt_blk(&ctx->tfc, x, y);
/* Remove IV */
for (i = 0; i < TF_BLOCK_UNITS; i++)
y[i] ^= ctx->ctr[i];
/* Save IV for next block */
memcpy(ctx->ctr, t, TF_BLOCK_SIZE);
/* Convert from LE if necessary, store result data */
data_to_little(y, TF_BLOCK_SIZE);
memcpy(udst, y, TF_BLOCK_SIZE);
udst += TF_BLOCK_SIZE;
} while ((sl -= TF_BLOCK_SIZE) >= TF_BLOCK_SIZE);
}
/*
* If there is remaining, then process it partially using plain CTR mode.
* This will automatically become CBC again if block will be padded by
* caller.
*/
if (sl) {
memset(x, 0, TF_BLOCK_SIZE);
memcpy(x, usrc, sl);
data_to_little(x, TF_BLOCK_SIZE);
ctr_inc(ctx->ctr, TF_BLOCK_UNITS);
tfc1024_encrypt_blk(&ctx->tfc, ctx->ctr, y);
for (i = 0; i < TF_BLOCK_UNITS; i++)
y[i] ^= x[i];
data_to_little(y, TF_BLOCK_SIZE);
memcpy(udst, y, sl);
}
}
#endif /* TF_NEED_CBC_MODE */
#ifdef TF_NEED_ECB_MODE
/* ECB mode threefish. */
void tf1024_ecb_encrypt(tfc1024_ctx *ctx, const void *src, size_t slen,
void *dst) {
const TF_BYTE_TYPE *usrc = src;
TF_BYTE_TYPE *udst = dst;
TF_UNIT_TYPE x[TF_BLOCK_UNITS], y[TF_BLOCK_UNITS];
size_t sl = slen, i;
if (sl >= TF_BLOCK_SIZE) {
do {
memcpy(x, usrc, TF_BLOCK_SIZE);
usrc += TF_BLOCK_SIZE;
data_to_little(x, TF_BLOCK_SIZE);
tfc1024_encrypt_blk(ctx, x, y);
data_to_little(y, TF_BLOCK_SIZE);
memcpy(udst, y, TF_BLOCK_SIZE);
udst += TF_BLOCK_SIZE;
} while ((sl -= TF_BLOCK_SIZE) >= TF_BLOCK_SIZE);
}
if (sl) {
memset(x, 0, TF_BLOCK_SIZE);
memcpy(x, usrc, sl);
data_to_little(x, TF_BLOCK_SIZE);
memset(y, 0, TF_BLOCK_SIZE);
tfc1024_encrypt_blk(ctx, y, y);
for (i = 0; i < TF_BLOCK_UNITS; i++)
y[i] ^= x[i];
data_to_little(y, TF_BLOCK_SIZE);
memcpy(udst, y, sl);
}
}
void tf1024_ecb_decrypt(tfc1024_ctx *ctx, const void *src, size_t slen,
void *dst) {
const TF_BYTE_TYPE *usrc = src;
TF_BYTE_TYPE *udst = dst;
TF_UNIT_TYPE x[TF_BLOCK_UNITS], y[TF_BLOCK_UNITS];
size_t sl = slen, i;
if (sl >= TF_BLOCK_SIZE) {
do {
memcpy(x, usrc, TF_BLOCK_SIZE);
usrc += TF_BLOCK_SIZE;
data_to_little(x, TF_BLOCK_SIZE);
tfc1024_decrypt_blk(ctx, x, y);
data_to_little(y, TF_BLOCK_SIZE);
memcpy(udst, y, TF_BLOCK_SIZE);
udst += TF_BLOCK_SIZE;
} while ((sl -= TF_BLOCK_SIZE) >= TF_BLOCK_SIZE);
}
if (sl) {
memset(x, 0, TF_BLOCK_SIZE);
memcpy(x, usrc, sl);
data_to_little(x, TF_BLOCK_SIZE);
memset(y, 0, TF_BLOCK_SIZE);
tfc1024_encrypt_blk(ctx, y, y);
for (i = 0; i < TF_BLOCK_UNITS; i++)
y[i] ^= x[i];
data_to_little(y, TF_BLOCK_SIZE);
memcpy(udst, y, sl);
}
}
#endif /* TF_NEED_ECB_MODE */
#ifdef TF_NEED_EME_MODE
/* EME (Encrypt, Mask, Encrypt) mode. 2x slower. Has both CBC and CTR
* properties. */
void tf1024_eme_encrypt(tf1024_ctx *ctx, const void *src, size_t slen,
void *dst) {
const TF_BYTE_TYPE *usrc = src;
TF_BYTE_TYPE *udst = dst;
TF_UNIT_TYPE x[TF_BLOCK_UNITS], y[TF_BLOCK_UNITS];
size_t sl = slen, i;
if (sl >= TF_BLOCK_SIZE) {
do {
memcpy(x, usrc, TF_BLOCK_SIZE);
usrc += TF_BLOCK_SIZE;
data_to_little(x, TF_BLOCK_SIZE);
ctr_inc(ctx->ctr, TF_BLOCK_UNITS);
tfc1024_encrypt_blk(&ctx->tfc, ctx->ctr, y);
for (i = 0; i < TF_BLOCK_UNITS; i++)
y[i] ^= x[i];
tfc1024_encrypt_blk(&ctx->tfc, y, x);
data_to_little(x, TF_BLOCK_SIZE);
memcpy(udst, x, TF_BLOCK_SIZE);
udst += TF_BLOCK_SIZE;
} while ((sl -= TF_BLOCK_SIZE) >= TF_BLOCK_SIZE);
}
if (sl) {
memset(x, 0, TF_BLOCK_SIZE);
memcpy(x, usrc, sl);
data_to_little(x, TF_BLOCK_SIZE);
ctr_inc(ctx->ctr, TF_BLOCK_UNITS);
tfc1024_encrypt_blk(&ctx->tfc, ctx->ctr, y);
for (i = 0; i < TF_BLOCK_UNITS; i++)
y[i] ^= x[i];
data_to_little(y, TF_BLOCK_SIZE);
memcpy(udst, y, sl);
}
}
void tf1024_eme_decrypt(tf1024_ctx *ctx, const void *src, size_t slen,
void *dst) {
const TF_BYTE_TYPE *usrc = src;
TF_BYTE_TYPE *udst = dst;
TF_UNIT_TYPE x[TF_BLOCK_UNITS], y[TF_BLOCK_UNITS];
size_t sl = slen, i;
if (sl >= TF_BLOCK_SIZE) {
do {
memcpy(x, usrc, TF_BLOCK_SIZE);
usrc += TF_BLOCK_SIZE;
data_to_little(x, TF_BLOCK_SIZE);
tfc1024_decrypt_blk(&ctx->tfc, x, y);
ctr_inc(ctx->ctr, TF_BLOCK_UNITS);
tfc1024_encrypt_blk(&ctx->tfc, ctx->ctr, x);
for (i = 0; i < TF_BLOCK_UNITS; i++)
y[i] ^= x[i];
data_to_little(y, TF_BLOCK_SIZE);
memcpy(udst, y, TF_BLOCK_SIZE);
udst += TF_BLOCK_SIZE;
} while ((sl -= TF_BLOCK_SIZE) >= TF_BLOCK_SIZE);
}
if (sl) {
memset(x, 0, TF_BLOCK_SIZE);
memcpy(x, usrc, sl);
data_to_little(x, TF_BLOCK_SIZE);
ctr_inc(ctx->ctr, TF_BLOCK_UNITS);
tfc1024_encrypt_blk(&ctx->tfc, ctx->ctr, y);
for (i = 0; i < TF_BLOCK_UNITS; i++)
y[i] ^= x[i];
data_to_little(y, TF_BLOCK_SIZE);
memcpy(udst, y, sl);
}
}
#endif /* TF_NEED_EME_MODE */
#endif /* TF_NEED_MODES */
#define SKEIN_VERSION 1
#define SKEIN_ID 0x33414853 /* LE: "SHA3" */
#define SKEIN_BLOCK_KEY (0)
#define SKEIN_BLOCK_CFG ((TF_UNIT_TYPE)4 << 56)
#define SKEIN_BLOCK_MSG ((TF_UNIT_TYPE)48 << 56)
#define SKEIN_BLOCK_OUT ((TF_UNIT_TYPE)63 << 56)
#define SKEIN_FLAG_FIRST ((TF_UNIT_TYPE)1 << 62)
#define SKEIN_FLAG_LAST ((TF_UNIT_TYPE)1 << 63)
static inline void skput64lsb(TF_BYTE_TYPE *dst, const TF_UNIT_TYPE *src,
size_t l) {
size_t n;
for (n = 0; n < l; n++)
dst[n] = (TF_BYTE_TYPE)(src[n >> 3] >> (TF_SIZE_UNIT * (n & 7)));
}
static inline void skget64lsb(TF_UNIT_TYPE *dst, const TF_BYTE_TYPE *src,
size_t l) {
size_t n;
for (n = 0; n < TF_SIZE_UNIT * l; n += TF_SIZE_UNIT)
dst[n / TF_SIZE_UNIT] = (((TF_UNIT_TYPE)src[n])) +
(((TF_UNIT_TYPE)src[n + 1]) << 8) +
(((TF_UNIT_TYPE)src[n + 2]) << 16) +
(((TF_UNIT_TYPE)src[n + 3]) << 24) +
(((TF_UNIT_TYPE)src[n + 4]) << 32) +
(((TF_UNIT_TYPE)src[n + 5]) << 40) +
(((TF_UNIT_TYPE)src[n + 6]) << 48) +
(((TF_UNIT_TYPE)src[n + 7]) << 56);
}
static void sk1024_process_blk(sk1024_ctx *ctx, const TF_BYTE_TYPE *in,
size_t bnum, size_t l) {
TF_UNIT_TYPE x[TF_BLOCK_UNITS], y[TF_BLOCK_UNITS];
size_t i;
do {
ctx->tfc.T[0] += l;
skget64lsb(x, in, TF_ARRAY_SZ(x));
in += sizeof(x);
tf1024_init_for_skein(&ctx->tfc);
tfc1024_encrypt_blk(&ctx->tfc, x, y);
for (i = 0; i < TF_BLOCK_UNITS; i++)
ctx->tfc.K[i] = y[i] ^ x[i];
ctx->tfc.T[1] &= ~SKEIN_FLAG_FIRST;
} while (--bnum);
}
void sk1024_init_key(sk1024_ctx *ctx) {
memset(ctx, 0, sizeof(sk1024_ctx));
ctx->hl = TF_MAX_BITS;
ctx->bl = 0;
ctx->tfc.T[0] = 0;
ctx->tfc.T[1] = SKEIN_BLOCK_KEY | SKEIN_FLAG_FIRST;
memset(ctx->tfc.K, 0, sizeof(ctx->tfc.K));
}
void sk1024_update_key(sk1024_ctx *ctx, const void *key, size_t klen) {
sk1024_update(ctx, key, klen);
}
void sk1024_final_key(sk1024_ctx *ctx) {
TF_UNIT_TYPE cfg[TF_BLOCK_UNITS];
memset(cfg, 0, sizeof(cfg));
sk1024_final_pad(ctx, (void *)cfg, 1);
memcpy(ctx->tfc.K, cfg, sizeof(cfg));
data_to_little(ctx->tfc.K, TF_KEY_SIZE);
}
void sk1024_init(sk1024_ctx *ctx, size_t bits, int with_key) {
TF_UNIT_TYPE cfg[TF_BLOCK_UNITS];
if (!with_key)
memset(ctx, 0, sizeof(sk1024_ctx));
ctx->hl = bits;
ctx->bl = 0;
memset(cfg, 0, sizeof(cfg));
cfg[0] = TF_SWAP_FUNC(((TF_UNIT_TYPE)SKEIN_VERSION << 32) + SKEIN_ID);
cfg[1] = TF_SWAP_FUNC(bits);
ctx->tfc.T[0] = 0;
ctx->tfc.T[1] = SKEIN_BLOCK_CFG | SKEIN_FLAG_FIRST | SKEIN_FLAG_LAST;
sk1024_process_blk(ctx, (TF_BYTE_TYPE *)cfg, 1, 32);
ctx->tfc.T[0] = 0;
ctx->tfc.T[1] = SKEIN_BLOCK_MSG | SKEIN_FLAG_FIRST;
}
void sk1024_update(sk1024_ctx *ctx, const void *msg, size_t l) {
const TF_BYTE_TYPE *umsg = msg;
size_t n;
if (l + ctx->bl > TF_BLOCK_SIZE) {
if (ctx->bl) {
n = TF_BLOCK_SIZE - ctx->bl;
if (n) {
memcpy(&ctx->B[ctx->bl], umsg, n);
l -= n;
umsg += n;
ctx->bl += n;
}
sk1024_process_blk(ctx, ctx->B, 1, TF_BLOCK_SIZE);
ctx->bl = 0;
}
if (l > TF_BLOCK_SIZE) {
n = (l - 1) / TF_BLOCK_SIZE;
sk1024_process_blk(ctx, umsg, n, TF_BLOCK_SIZE);
l -= n * TF_BLOCK_SIZE;
umsg += n * TF_BLOCK_SIZE;
}
}
if (l) {
memcpy(&ctx->B[ctx->bl], umsg, l);
ctx->bl += l;
}
}
void sk1024_final_pad(sk1024_ctx *ctx, void *outhash, short do_pad) {
TF_BYTE_TYPE *hash = outhash;
TF_UNIT_TYPE key[TF_BLOCK_UNITS], *X;
size_t i, b, n;
if (ctx->bl < TF_BLOCK_SIZE)
memset(ctx->B + ctx->bl, 0, TF_BLOCK_SIZE - ctx->bl);
ctx->tfc.T[1] |= SKEIN_FLAG_LAST;
sk1024_process_blk(ctx, ctx->B, 1, ctx->bl);
if (do_pad) {
skput64lsb(outhash, ctx->tfc.K, TF_BLOCK_SIZE);
return;
}
b = (ctx->hl + 7) / 8;
memset(ctx->B, 0, sizeof(ctx->B));
memcpy(key, ctx->tfc.K, sizeof(key));
for (i = 0; i * TF_BLOCK_SIZE < b; i++) {
X = (TF_UNIT_TYPE *)ctx->B;
X[0] = TF_SWAP_FUNC((TF_UNIT_TYPE)i);
ctx->tfc.T[0] = 0;
ctx->tfc.T[1] = SKEIN_BLOCK_OUT | SKEIN_FLAG_FIRST | SKEIN_FLAG_LAST;
ctx->bl = 0;
sk1024_process_blk(ctx, ctx->B, 1, sizeof(TF_UNIT_TYPE));
n = b - i * TF_BLOCK_SIZE;
if (n >= TF_BLOCK_SIZE)
n = TF_BLOCK_SIZE;
skput64lsb(hash + i * TF_BLOCK_SIZE, ctx->tfc.K, n);
memcpy(ctx->tfc.K, key, sizeof(key));
}
}
void sk1024_final(sk1024_ctx *ctx, void *outhash) {
sk1024_final_pad(ctx, outhash, 0);
}
void sk1024(const void *src, size_t slen, void *dst, size_t bits) {
sk1024_ctx ctx;
memset(&ctx, 0, sizeof(sk1024_ctx));
sk1024_init(&ctx, bits, 0);
sk1024_update(&ctx, src, slen);
sk1024_final(&ctx, dst);
memset(&ctx, 0, sizeof(sk1024_ctx));
}