Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/crypto/skein/skein_block.c
39478 views
1
/***********************************************************************
2
**
3
** Implementation of the Skein block functions.
4
**
5
** Source code author: Doug Whiting, 2008.
6
**
7
** This algorithm and source code is released to the public domain.
8
**
9
** Compile-time switches:
10
**
11
** SKEIN_USE_ASM -- set bits (256/512/1024) to select which
12
** versions use ASM code for block processing
13
** [default: use C for all block sizes]
14
**
15
************************************************************************/
16
17
#include <sys/cdefs.h>
18
#include <sys/endian.h>
19
#include <sys/types.h>
20
21
#ifdef _KERNEL
22
#include <sys/systm.h>
23
#else
24
#include <string.h>
25
#endif
26
27
#include "skein.h"
28
29
#ifndef SKEIN_USE_ASM
30
#define SKEIN_USE_ASM (0) /* default is all C code (no ASM) */
31
#endif
32
33
#ifndef SKEIN_LOOP
34
#define SKEIN_LOOP 001 /* default: unroll 256 and 512, but not 1024 */
35
#endif
36
37
#define BLK_BITS (WCNT*64) /* some useful definitions for code here */
38
#define KW_TWK_BASE (0)
39
#define KW_KEY_BASE (3)
40
#define ks (kw + KW_KEY_BASE)
41
#define ts (kw + KW_TWK_BASE)
42
43
#ifdef SKEIN_DEBUG
44
#define DebugSaveTweak(ctx) { ctx->h.T[0] = ts[0]; ctx->h.T[1] = ts[1]; }
45
#else
46
#define DebugSaveTweak(ctx)
47
#endif
48
49
/*****************************************************************/
50
/* functions to process blkCnt (nonzero) full block(s) of data. */
51
void Skein_256_Process_Block(Skein_256_Ctxt_t *ctx,const u08b_t *blkPtr,size_t blkCnt,size_t byteCntAdd);
52
void Skein_512_Process_Block(Skein_512_Ctxt_t *ctx,const u08b_t *blkPtr,size_t blkCnt,size_t byteCntAdd);
53
void Skein1024_Process_Block(Skein1024_Ctxt_t *ctx,const u08b_t *blkPtr,size_t blkCnt,size_t byteCntAdd);
54
55
/***************************** Skein_256 ******************************/
56
#if !(SKEIN_USE_ASM & 256)
57
void Skein_256_Process_Block(Skein_256_Ctxt_t *ctx,const u08b_t *blkPtr,size_t blkCnt,size_t byteCntAdd)
58
{ /* do it in C */
59
enum
60
{
61
WCNT = SKEIN_256_STATE_WORDS
62
};
63
#undef RCNT
64
#define RCNT (SKEIN_256_ROUNDS_TOTAL/8)
65
66
#ifdef SKEIN_LOOP /* configure how much to unroll the loop */
67
#define SKEIN_UNROLL_256 (((SKEIN_LOOP)/100)%10)
68
#else
69
#define SKEIN_UNROLL_256 (0)
70
#endif
71
72
#if SKEIN_UNROLL_256
73
#if (RCNT % SKEIN_UNROLL_256)
74
#error "Invalid SKEIN_UNROLL_256" /* sanity check on unroll count */
75
#endif
76
size_t r;
77
u64b_t kw[WCNT+4+RCNT*2]; /* key schedule words : chaining vars + tweak + "rotation"*/
78
#else
79
u64b_t kw[WCNT+4]; /* key schedule words : chaining vars + tweak */
80
#endif
81
u64b_t X0,X1,X2,X3; /* local copy of context vars, for speed */
82
u64b_t w [WCNT]; /* local copy of input block */
83
#ifdef SKEIN_DEBUG
84
const u64b_t *Xptr[4]; /* use for debugging (help compiler put Xn in registers) */
85
Xptr[0] = &X0; Xptr[1] = &X1; Xptr[2] = &X2; Xptr[3] = &X3;
86
#endif
87
Skein_assert(blkCnt != 0); /* never call with blkCnt == 0! */
88
ts[0] = ctx->h.T[0];
89
ts[1] = ctx->h.T[1];
90
do {
91
/* this implementation only supports 2**64 input bytes (no carry out here) */
92
ts[0] += byteCntAdd; /* update processed length */
93
94
/* precompute the key schedule for this block */
95
ks[0] = ctx->X[0];
96
ks[1] = ctx->X[1];
97
ks[2] = ctx->X[2];
98
ks[3] = ctx->X[3];
99
ks[4] = ks[0] ^ ks[1] ^ ks[2] ^ ks[3] ^ SKEIN_KS_PARITY;
100
101
ts[2] = ts[0] ^ ts[1];
102
103
Skein_Get64_LSB_First(w,blkPtr,WCNT); /* get input block in little-endian format */
104
DebugSaveTweak(ctx);
105
Skein_Show_Block(BLK_BITS,&ctx->h,ctx->X,blkPtr,w,ks,ts);
106
107
X0 = w[0] + ks[0]; /* do the first full key injection */
108
X1 = w[1] + ks[1] + ts[0];
109
X2 = w[2] + ks[2] + ts[1];
110
X3 = w[3] + ks[3];
111
112
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INITIAL,Xptr); /* show starting state values */
113
114
blkPtr += SKEIN_256_BLOCK_BYTES;
115
116
/* run the rounds */
117
118
#define Round256(p0,p1,p2,p3,ROT,rNum) \
119
X##p0 += X##p1; X##p1 = RotL_64(X##p1,ROT##_0); X##p1 ^= X##p0; \
120
X##p2 += X##p3; X##p3 = RotL_64(X##p3,ROT##_1); X##p3 ^= X##p2; \
121
122
#if SKEIN_UNROLL_256 == 0
123
#define R256(p0,p1,p2,p3,ROT,rNum) /* fully unrolled */ \
124
Round256(p0,p1,p2,p3,ROT,rNum) \
125
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,rNum,Xptr);
126
127
#define I256(R) \
128
X0 += ks[((R)+1) % 5]; /* inject the key schedule value */ \
129
X1 += ks[((R)+2) % 5] + ts[((R)+1) % 3]; \
130
X2 += ks[((R)+3) % 5] + ts[((R)+2) % 3]; \
131
X3 += ks[((R)+4) % 5] + (R)+1; \
132
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr);
133
#else /* looping version */
134
#define R256(p0,p1,p2,p3,ROT,rNum) \
135
Round256(p0,p1,p2,p3,ROT,rNum) \
136
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,4*(r-1)+rNum,Xptr);
137
138
#define I256(R) \
139
X0 += ks[r+(R)+0]; /* inject the key schedule value */ \
140
X1 += ks[r+(R)+1] + ts[r+(R)+0]; \
141
X2 += ks[r+(R)+2] + ts[r+(R)+1]; \
142
X3 += ks[r+(R)+3] + r+(R) ; \
143
ks[r + (R)+4 ] = ks[r+(R)-1]; /* rotate key schedule */\
144
ts[r + (R)+2 ] = ts[r+(R)-1]; \
145
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr);
146
147
for (r=1;r < 2*RCNT;r+=2*SKEIN_UNROLL_256) /* loop thru it */
148
#endif
149
{
150
#define R256_8_rounds(R) \
151
R256(0,1,2,3,R_256_0,8*(R) + 1); \
152
R256(0,3,2,1,R_256_1,8*(R) + 2); \
153
R256(0,1,2,3,R_256_2,8*(R) + 3); \
154
R256(0,3,2,1,R_256_3,8*(R) + 4); \
155
I256(2*(R)); \
156
R256(0,1,2,3,R_256_4,8*(R) + 5); \
157
R256(0,3,2,1,R_256_5,8*(R) + 6); \
158
R256(0,1,2,3,R_256_6,8*(R) + 7); \
159
R256(0,3,2,1,R_256_7,8*(R) + 8); \
160
I256(2*(R)+1);
161
162
R256_8_rounds( 0);
163
164
#define R256_Unroll_R(NN) ((SKEIN_UNROLL_256 == 0 && SKEIN_256_ROUNDS_TOTAL/8 > (NN)) || (SKEIN_UNROLL_256 > (NN)))
165
166
#if R256_Unroll_R( 1)
167
R256_8_rounds( 1);
168
#endif
169
#if R256_Unroll_R( 2)
170
R256_8_rounds( 2);
171
#endif
172
#if R256_Unroll_R( 3)
173
R256_8_rounds( 3);
174
#endif
175
#if R256_Unroll_R( 4)
176
R256_8_rounds( 4);
177
#endif
178
#if R256_Unroll_R( 5)
179
R256_8_rounds( 5);
180
#endif
181
#if R256_Unroll_R( 6)
182
R256_8_rounds( 6);
183
#endif
184
#if R256_Unroll_R( 7)
185
R256_8_rounds( 7);
186
#endif
187
#if R256_Unroll_R( 8)
188
R256_8_rounds( 8);
189
#endif
190
#if R256_Unroll_R( 9)
191
R256_8_rounds( 9);
192
#endif
193
#if R256_Unroll_R(10)
194
R256_8_rounds(10);
195
#endif
196
#if R256_Unroll_R(11)
197
R256_8_rounds(11);
198
#endif
199
#if R256_Unroll_R(12)
200
R256_8_rounds(12);
201
#endif
202
#if R256_Unroll_R(13)
203
R256_8_rounds(13);
204
#endif
205
#if R256_Unroll_R(14)
206
R256_8_rounds(14);
207
#endif
208
#if (SKEIN_UNROLL_256 > 14)
209
#error "need more unrolling in Skein_256_Process_Block"
210
#endif
211
}
212
/* do the final "feedforward" xor, update context chaining vars */
213
ctx->X[0] = X0 ^ w[0];
214
ctx->X[1] = X1 ^ w[1];
215
ctx->X[2] = X2 ^ w[2];
216
ctx->X[3] = X3 ^ w[3];
217
218
Skein_Show_Round(BLK_BITS,&ctx->h,SKEIN_RND_FEED_FWD,ctx->X);
219
220
ts[1] &= ~SKEIN_T1_FLAG_FIRST;
221
}
222
while (--blkCnt);
223
ctx->h.T[0] = ts[0];
224
ctx->h.T[1] = ts[1];
225
}
226
227
#if defined(SKEIN_CODE_SIZE) || defined(SKEIN_PERF)
228
size_t Skein_256_Process_Block_CodeSize(void)
229
{
230
return ((u08b_t *) Skein_256_Process_Block_CodeSize) -
231
((u08b_t *) Skein_256_Process_Block);
232
}
233
uint_t Skein_256_Unroll_Cnt(void)
234
{
235
return SKEIN_UNROLL_256;
236
}
237
#endif
238
#endif
239
240
/***************************** Skein_512 ******************************/
241
#if !(SKEIN_USE_ASM & 512)
242
void Skein_512_Process_Block(Skein_512_Ctxt_t *ctx,const u08b_t *blkPtr,size_t blkCnt,size_t byteCntAdd)
243
{ /* do it in C */
244
enum
245
{
246
WCNT = SKEIN_512_STATE_WORDS
247
};
248
#undef RCNT
249
#define RCNT (SKEIN_512_ROUNDS_TOTAL/8)
250
251
#ifdef SKEIN_LOOP /* configure how much to unroll the loop */
252
#define SKEIN_UNROLL_512 (((SKEIN_LOOP)/10)%10)
253
#else
254
#define SKEIN_UNROLL_512 (0)
255
#endif
256
257
#if SKEIN_UNROLL_512
258
#if (RCNT % SKEIN_UNROLL_512)
259
#error "Invalid SKEIN_UNROLL_512" /* sanity check on unroll count */
260
#endif
261
size_t r;
262
u64b_t kw[WCNT+4+RCNT*2]; /* key schedule words : chaining vars + tweak + "rotation"*/
263
#else
264
u64b_t kw[WCNT+4]; /* key schedule words : chaining vars + tweak */
265
#endif
266
u64b_t X0,X1,X2,X3,X4,X5,X6,X7; /* local copy of vars, for speed */
267
u64b_t w [WCNT]; /* local copy of input block */
268
#ifdef SKEIN_DEBUG
269
const u64b_t *Xptr[8]; /* use for debugging (help compiler put Xn in registers) */
270
Xptr[0] = &X0; Xptr[1] = &X1; Xptr[2] = &X2; Xptr[3] = &X3;
271
Xptr[4] = &X4; Xptr[5] = &X5; Xptr[6] = &X6; Xptr[7] = &X7;
272
#endif
273
274
Skein_assert(blkCnt != 0); /* never call with blkCnt == 0! */
275
ts[0] = ctx->h.T[0];
276
ts[1] = ctx->h.T[1];
277
do {
278
/* this implementation only supports 2**64 input bytes (no carry out here) */
279
ts[0] += byteCntAdd; /* update processed length */
280
281
/* precompute the key schedule for this block */
282
ks[0] = ctx->X[0];
283
ks[1] = ctx->X[1];
284
ks[2] = ctx->X[2];
285
ks[3] = ctx->X[3];
286
ks[4] = ctx->X[4];
287
ks[5] = ctx->X[5];
288
ks[6] = ctx->X[6];
289
ks[7] = ctx->X[7];
290
ks[8] = ks[0] ^ ks[1] ^ ks[2] ^ ks[3] ^
291
ks[4] ^ ks[5] ^ ks[6] ^ ks[7] ^ SKEIN_KS_PARITY;
292
293
ts[2] = ts[0] ^ ts[1];
294
295
Skein_Get64_LSB_First(w,blkPtr,WCNT); /* get input block in little-endian format */
296
DebugSaveTweak(ctx);
297
Skein_Show_Block(BLK_BITS,&ctx->h,ctx->X,blkPtr,w,ks,ts);
298
299
X0 = w[0] + ks[0]; /* do the first full key injection */
300
X1 = w[1] + ks[1];
301
X2 = w[2] + ks[2];
302
X3 = w[3] + ks[3];
303
X4 = w[4] + ks[4];
304
X5 = w[5] + ks[5] + ts[0];
305
X6 = w[6] + ks[6] + ts[1];
306
X7 = w[7] + ks[7];
307
308
blkPtr += SKEIN_512_BLOCK_BYTES;
309
310
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INITIAL,Xptr);
311
/* run the rounds */
312
#define Round512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) \
313
X##p0 += X##p1; X##p1 = RotL_64(X##p1,ROT##_0); X##p1 ^= X##p0; \
314
X##p2 += X##p3; X##p3 = RotL_64(X##p3,ROT##_1); X##p3 ^= X##p2; \
315
X##p4 += X##p5; X##p5 = RotL_64(X##p5,ROT##_2); X##p5 ^= X##p4; \
316
X##p6 += X##p7; X##p7 = RotL_64(X##p7,ROT##_3); X##p7 ^= X##p6; \
317
318
#if SKEIN_UNROLL_512 == 0
319
#define R512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) /* unrolled */ \
320
Round512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) \
321
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,rNum,Xptr);
322
323
#define I512(R) \
324
X0 += ks[((R)+1) % 9]; /* inject the key schedule value */ \
325
X1 += ks[((R)+2) % 9]; \
326
X2 += ks[((R)+3) % 9]; \
327
X3 += ks[((R)+4) % 9]; \
328
X4 += ks[((R)+5) % 9]; \
329
X5 += ks[((R)+6) % 9] + ts[((R)+1) % 3]; \
330
X6 += ks[((R)+7) % 9] + ts[((R)+2) % 3]; \
331
X7 += ks[((R)+8) % 9] + (R)+1; \
332
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr);
333
#else /* looping version */
334
#define R512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) \
335
Round512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) \
336
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,4*(r-1)+rNum,Xptr);
337
338
#define I512(R) \
339
X0 += ks[r+(R)+0]; /* inject the key schedule value */ \
340
X1 += ks[r+(R)+1]; \
341
X2 += ks[r+(R)+2]; \
342
X3 += ks[r+(R)+3]; \
343
X4 += ks[r+(R)+4]; \
344
X5 += ks[r+(R)+5] + ts[r+(R)+0]; \
345
X6 += ks[r+(R)+6] + ts[r+(R)+1]; \
346
X7 += ks[r+(R)+7] + r+(R) ; \
347
ks[r + (R)+8] = ks[r+(R)-1]; /* rotate key schedule */ \
348
ts[r + (R)+2] = ts[r+(R)-1]; \
349
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr);
350
351
for (r=1;r < 2*RCNT;r+=2*SKEIN_UNROLL_512) /* loop thru it */
352
#endif /* end of looped code definitions */
353
{
354
#define R512_8_rounds(R) /* do 8 full rounds */ \
355
R512(0,1,2,3,4,5,6,7,R_512_0,8*(R)+ 1); \
356
R512(2,1,4,7,6,5,0,3,R_512_1,8*(R)+ 2); \
357
R512(4,1,6,3,0,5,2,7,R_512_2,8*(R)+ 3); \
358
R512(6,1,0,7,2,5,4,3,R_512_3,8*(R)+ 4); \
359
I512(2*(R)); \
360
R512(0,1,2,3,4,5,6,7,R_512_4,8*(R)+ 5); \
361
R512(2,1,4,7,6,5,0,3,R_512_5,8*(R)+ 6); \
362
R512(4,1,6,3,0,5,2,7,R_512_6,8*(R)+ 7); \
363
R512(6,1,0,7,2,5,4,3,R_512_7,8*(R)+ 8); \
364
I512(2*(R)+1); /* and key injection */
365
366
R512_8_rounds( 0);
367
368
#define R512_Unroll_R(NN) ((SKEIN_UNROLL_512 == 0 && SKEIN_512_ROUNDS_TOTAL/8 > (NN)) || (SKEIN_UNROLL_512 > (NN)))
369
370
#if R512_Unroll_R( 1)
371
R512_8_rounds( 1);
372
#endif
373
#if R512_Unroll_R( 2)
374
R512_8_rounds( 2);
375
#endif
376
#if R512_Unroll_R( 3)
377
R512_8_rounds( 3);
378
#endif
379
#if R512_Unroll_R( 4)
380
R512_8_rounds( 4);
381
#endif
382
#if R512_Unroll_R( 5)
383
R512_8_rounds( 5);
384
#endif
385
#if R512_Unroll_R( 6)
386
R512_8_rounds( 6);
387
#endif
388
#if R512_Unroll_R( 7)
389
R512_8_rounds( 7);
390
#endif
391
#if R512_Unroll_R( 8)
392
R512_8_rounds( 8);
393
#endif
394
#if R512_Unroll_R( 9)
395
R512_8_rounds( 9);
396
#endif
397
#if R512_Unroll_R(10)
398
R512_8_rounds(10);
399
#endif
400
#if R512_Unroll_R(11)
401
R512_8_rounds(11);
402
#endif
403
#if R512_Unroll_R(12)
404
R512_8_rounds(12);
405
#endif
406
#if R512_Unroll_R(13)
407
R512_8_rounds(13);
408
#endif
409
#if R512_Unroll_R(14)
410
R512_8_rounds(14);
411
#endif
412
#if (SKEIN_UNROLL_512 > 14)
413
#error "need more unrolling in Skein_512_Process_Block"
414
#endif
415
}
416
417
/* do the final "feedforward" xor, update context chaining vars */
418
ctx->X[0] = X0 ^ w[0];
419
ctx->X[1] = X1 ^ w[1];
420
ctx->X[2] = X2 ^ w[2];
421
ctx->X[3] = X3 ^ w[3];
422
ctx->X[4] = X4 ^ w[4];
423
ctx->X[5] = X5 ^ w[5];
424
ctx->X[6] = X6 ^ w[6];
425
ctx->X[7] = X7 ^ w[7];
426
Skein_Show_Round(BLK_BITS,&ctx->h,SKEIN_RND_FEED_FWD,ctx->X);
427
428
ts[1] &= ~SKEIN_T1_FLAG_FIRST;
429
}
430
while (--blkCnt);
431
ctx->h.T[0] = ts[0];
432
ctx->h.T[1] = ts[1];
433
}
434
435
#if defined(SKEIN_CODE_SIZE) || defined(SKEIN_PERF)
436
size_t Skein_512_Process_Block_CodeSize(void)
437
{
438
return ((u08b_t *) Skein_512_Process_Block_CodeSize) -
439
((u08b_t *) Skein_512_Process_Block);
440
}
441
uint_t Skein_512_Unroll_Cnt(void)
442
{
443
return SKEIN_UNROLL_512;
444
}
445
#endif
446
#endif
447
448
/***************************** Skein1024 ******************************/
449
#if !(SKEIN_USE_ASM & 1024)
450
void Skein1024_Process_Block(Skein1024_Ctxt_t *ctx,const u08b_t *blkPtr,size_t blkCnt,size_t byteCntAdd)
451
{ /* do it in C, always looping (unrolled is bigger AND slower!) */
452
enum
453
{
454
WCNT = SKEIN1024_STATE_WORDS
455
};
456
#undef RCNT
457
#define RCNT (SKEIN1024_ROUNDS_TOTAL/8)
458
459
#ifdef SKEIN_LOOP /* configure how much to unroll the loop */
460
#define SKEIN_UNROLL_1024 ((SKEIN_LOOP)%10)
461
#else
462
#define SKEIN_UNROLL_1024 (0)
463
#endif
464
465
#if (SKEIN_UNROLL_1024 != 0)
466
#if (RCNT % SKEIN_UNROLL_1024)
467
#error "Invalid SKEIN_UNROLL_1024" /* sanity check on unroll count */
468
#endif
469
size_t r;
470
u64b_t kw[WCNT+4+RCNT*2]; /* key schedule words : chaining vars + tweak + "rotation"*/
471
#else
472
u64b_t kw[WCNT+4]; /* key schedule words : chaining vars + tweak */
473
#endif
474
475
u64b_t X00,X01,X02,X03,X04,X05,X06,X07, /* local copy of vars, for speed */
476
X08,X09,X10,X11,X12,X13,X14,X15;
477
u64b_t w [WCNT]; /* local copy of input block */
478
#ifdef SKEIN_DEBUG
479
const u64b_t *Xptr[16]; /* use for debugging (help compiler put Xn in registers) */
480
Xptr[ 0] = &X00; Xptr[ 1] = &X01; Xptr[ 2] = &X02; Xptr[ 3] = &X03;
481
Xptr[ 4] = &X04; Xptr[ 5] = &X05; Xptr[ 6] = &X06; Xptr[ 7] = &X07;
482
Xptr[ 8] = &X08; Xptr[ 9] = &X09; Xptr[10] = &X10; Xptr[11] = &X11;
483
Xptr[12] = &X12; Xptr[13] = &X13; Xptr[14] = &X14; Xptr[15] = &X15;
484
#endif
485
486
Skein_assert(blkCnt != 0); /* never call with blkCnt == 0! */
487
ts[0] = ctx->h.T[0];
488
ts[1] = ctx->h.T[1];
489
do {
490
/* this implementation only supports 2**64 input bytes (no carry out here) */
491
ts[0] += byteCntAdd; /* update processed length */
492
493
/* precompute the key schedule for this block */
494
ks[ 0] = ctx->X[ 0];
495
ks[ 1] = ctx->X[ 1];
496
ks[ 2] = ctx->X[ 2];
497
ks[ 3] = ctx->X[ 3];
498
ks[ 4] = ctx->X[ 4];
499
ks[ 5] = ctx->X[ 5];
500
ks[ 6] = ctx->X[ 6];
501
ks[ 7] = ctx->X[ 7];
502
ks[ 8] = ctx->X[ 8];
503
ks[ 9] = ctx->X[ 9];
504
ks[10] = ctx->X[10];
505
ks[11] = ctx->X[11];
506
ks[12] = ctx->X[12];
507
ks[13] = ctx->X[13];
508
ks[14] = ctx->X[14];
509
ks[15] = ctx->X[15];
510
ks[16] = ks[ 0] ^ ks[ 1] ^ ks[ 2] ^ ks[ 3] ^
511
ks[ 4] ^ ks[ 5] ^ ks[ 6] ^ ks[ 7] ^
512
ks[ 8] ^ ks[ 9] ^ ks[10] ^ ks[11] ^
513
ks[12] ^ ks[13] ^ ks[14] ^ ks[15] ^ SKEIN_KS_PARITY;
514
515
ts[2] = ts[0] ^ ts[1];
516
517
Skein_Get64_LSB_First(w,blkPtr,WCNT); /* get input block in little-endian format */
518
DebugSaveTweak(ctx);
519
Skein_Show_Block(BLK_BITS,&ctx->h,ctx->X,blkPtr,w,ks,ts);
520
521
X00 = w[ 0] + ks[ 0]; /* do the first full key injection */
522
X01 = w[ 1] + ks[ 1];
523
X02 = w[ 2] + ks[ 2];
524
X03 = w[ 3] + ks[ 3];
525
X04 = w[ 4] + ks[ 4];
526
X05 = w[ 5] + ks[ 5];
527
X06 = w[ 6] + ks[ 6];
528
X07 = w[ 7] + ks[ 7];
529
X08 = w[ 8] + ks[ 8];
530
X09 = w[ 9] + ks[ 9];
531
X10 = w[10] + ks[10];
532
X11 = w[11] + ks[11];
533
X12 = w[12] + ks[12];
534
X13 = w[13] + ks[13] + ts[0];
535
X14 = w[14] + ks[14] + ts[1];
536
X15 = w[15] + ks[15];
537
538
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INITIAL,Xptr);
539
540
#define Round1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rNum) \
541
X##p0 += X##p1; X##p1 = RotL_64(X##p1,ROT##_0); X##p1 ^= X##p0; \
542
X##p2 += X##p3; X##p3 = RotL_64(X##p3,ROT##_1); X##p3 ^= X##p2; \
543
X##p4 += X##p5; X##p5 = RotL_64(X##p5,ROT##_2); X##p5 ^= X##p4; \
544
X##p6 += X##p7; X##p7 = RotL_64(X##p7,ROT##_3); X##p7 ^= X##p6; \
545
X##p8 += X##p9; X##p9 = RotL_64(X##p9,ROT##_4); X##p9 ^= X##p8; \
546
X##pA += X##pB; X##pB = RotL_64(X##pB,ROT##_5); X##pB ^= X##pA; \
547
X##pC += X##pD; X##pD = RotL_64(X##pD,ROT##_6); X##pD ^= X##pC; \
548
X##pE += X##pF; X##pF = RotL_64(X##pF,ROT##_7); X##pF ^= X##pE; \
549
550
#if SKEIN_UNROLL_1024 == 0
551
#define R1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rn) \
552
Round1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rn) \
553
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,rn,Xptr);
554
555
#define I1024(R) \
556
X00 += ks[((R)+ 1) % 17]; /* inject the key schedule value */ \
557
X01 += ks[((R)+ 2) % 17]; \
558
X02 += ks[((R)+ 3) % 17]; \
559
X03 += ks[((R)+ 4) % 17]; \
560
X04 += ks[((R)+ 5) % 17]; \
561
X05 += ks[((R)+ 6) % 17]; \
562
X06 += ks[((R)+ 7) % 17]; \
563
X07 += ks[((R)+ 8) % 17]; \
564
X08 += ks[((R)+ 9) % 17]; \
565
X09 += ks[((R)+10) % 17]; \
566
X10 += ks[((R)+11) % 17]; \
567
X11 += ks[((R)+12) % 17]; \
568
X12 += ks[((R)+13) % 17]; \
569
X13 += ks[((R)+14) % 17] + ts[((R)+1) % 3]; \
570
X14 += ks[((R)+15) % 17] + ts[((R)+2) % 3]; \
571
X15 += ks[((R)+16) % 17] + (R)+1; \
572
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr);
573
#else /* looping version */
574
#define R1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rn) \
575
Round1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rn) \
576
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,4*(r-1)+rn,Xptr);
577
578
#define I1024(R) \
579
X00 += ks[r+(R)+ 0]; /* inject the key schedule value */ \
580
X01 += ks[r+(R)+ 1]; \
581
X02 += ks[r+(R)+ 2]; \
582
X03 += ks[r+(R)+ 3]; \
583
X04 += ks[r+(R)+ 4]; \
584
X05 += ks[r+(R)+ 5]; \
585
X06 += ks[r+(R)+ 6]; \
586
X07 += ks[r+(R)+ 7]; \
587
X08 += ks[r+(R)+ 8]; \
588
X09 += ks[r+(R)+ 9]; \
589
X10 += ks[r+(R)+10]; \
590
X11 += ks[r+(R)+11]; \
591
X12 += ks[r+(R)+12]; \
592
X13 += ks[r+(R)+13] + ts[r+(R)+0]; \
593
X14 += ks[r+(R)+14] + ts[r+(R)+1]; \
594
X15 += ks[r+(R)+15] + r+(R) ; \
595
ks[r + (R)+16] = ks[r+(R)-1]; /* rotate key schedule */ \
596
ts[r + (R)+ 2] = ts[r+(R)-1]; \
597
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr);
598
599
for (r=1;r <= 2*RCNT;r+=2*SKEIN_UNROLL_1024) /* loop thru it */
600
#endif
601
{
602
#define R1024_8_rounds(R) /* do 8 full rounds */ \
603
R1024(00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,R1024_0,8*(R) + 1); \
604
R1024(00,09,02,13,06,11,04,15,10,07,12,03,14,05,08,01,R1024_1,8*(R) + 2); \
605
R1024(00,07,02,05,04,03,06,01,12,15,14,13,08,11,10,09,R1024_2,8*(R) + 3); \
606
R1024(00,15,02,11,06,13,04,09,14,01,08,05,10,03,12,07,R1024_3,8*(R) + 4); \
607
I1024(2*(R)); \
608
R1024(00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,R1024_4,8*(R) + 5); \
609
R1024(00,09,02,13,06,11,04,15,10,07,12,03,14,05,08,01,R1024_5,8*(R) + 6); \
610
R1024(00,07,02,05,04,03,06,01,12,15,14,13,08,11,10,09,R1024_6,8*(R) + 7); \
611
R1024(00,15,02,11,06,13,04,09,14,01,08,05,10,03,12,07,R1024_7,8*(R) + 8); \
612
I1024(2*(R)+1);
613
614
R1024_8_rounds( 0);
615
616
#define R1024_Unroll_R(NN) ((SKEIN_UNROLL_1024 == 0 && SKEIN1024_ROUNDS_TOTAL/8 > (NN)) || (SKEIN_UNROLL_1024 > (NN)))
617
618
#if R1024_Unroll_R( 1)
619
R1024_8_rounds( 1);
620
#endif
621
#if R1024_Unroll_R( 2)
622
R1024_8_rounds( 2);
623
#endif
624
#if R1024_Unroll_R( 3)
625
R1024_8_rounds( 3);
626
#endif
627
#if R1024_Unroll_R( 4)
628
R1024_8_rounds( 4);
629
#endif
630
#if R1024_Unroll_R( 5)
631
R1024_8_rounds( 5);
632
#endif
633
#if R1024_Unroll_R( 6)
634
R1024_8_rounds( 6);
635
#endif
636
#if R1024_Unroll_R( 7)
637
R1024_8_rounds( 7);
638
#endif
639
#if R1024_Unroll_R( 8)
640
R1024_8_rounds( 8);
641
#endif
642
#if R1024_Unroll_R( 9)
643
R1024_8_rounds( 9);
644
#endif
645
#if R1024_Unroll_R(10)
646
R1024_8_rounds(10);
647
#endif
648
#if R1024_Unroll_R(11)
649
R1024_8_rounds(11);
650
#endif
651
#if R1024_Unroll_R(12)
652
R1024_8_rounds(12);
653
#endif
654
#if R1024_Unroll_R(13)
655
R1024_8_rounds(13);
656
#endif
657
#if R1024_Unroll_R(14)
658
R1024_8_rounds(14);
659
#endif
660
#if (SKEIN_UNROLL_1024 > 14)
661
#error "need more unrolling in Skein_1024_Process_Block"
662
#endif
663
}
664
/* do the final "feedforward" xor, update context chaining vars */
665
666
ctx->X[ 0] = X00 ^ w[ 0];
667
ctx->X[ 1] = X01 ^ w[ 1];
668
ctx->X[ 2] = X02 ^ w[ 2];
669
ctx->X[ 3] = X03 ^ w[ 3];
670
ctx->X[ 4] = X04 ^ w[ 4];
671
ctx->X[ 5] = X05 ^ w[ 5];
672
ctx->X[ 6] = X06 ^ w[ 6];
673
ctx->X[ 7] = X07 ^ w[ 7];
674
ctx->X[ 8] = X08 ^ w[ 8];
675
ctx->X[ 9] = X09 ^ w[ 9];
676
ctx->X[10] = X10 ^ w[10];
677
ctx->X[11] = X11 ^ w[11];
678
ctx->X[12] = X12 ^ w[12];
679
ctx->X[13] = X13 ^ w[13];
680
ctx->X[14] = X14 ^ w[14];
681
ctx->X[15] = X15 ^ w[15];
682
683
Skein_Show_Round(BLK_BITS,&ctx->h,SKEIN_RND_FEED_FWD,ctx->X);
684
685
ts[1] &= ~SKEIN_T1_FLAG_FIRST;
686
blkPtr += SKEIN1024_BLOCK_BYTES;
687
}
688
while (--blkCnt);
689
ctx->h.T[0] = ts[0];
690
ctx->h.T[1] = ts[1];
691
}
692
693
#if defined(SKEIN_CODE_SIZE) || defined(SKEIN_PERF)
694
size_t Skein1024_Process_Block_CodeSize(void)
695
{
696
return ((u08b_t *) Skein1024_Process_Block_CodeSize) -
697
((u08b_t *) Skein1024_Process_Block);
698
}
699
uint_t Skein1024_Unroll_Cnt(void)
700
{
701
return SKEIN_UNROLL_1024;
702
}
703
#endif
704
#endif
705
706