Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tpruvot
GitHub Repository: tpruvot/cpuminer-multi
Path: blob/linux/crypto/c_skein.c
1201 views
1
/***********************************************************************
2
**
3
** Implementation of the Skein hash function.
4
**
5
** Source code author: Doug Whiting, 2008.
6
**
7
** This algorithm and source code is released to the public domain.
8
**
9
************************************************************************/
10
11
#define SKEIN_PORT_CODE /* instantiate any code in skein_port.h */
12
13
#include <stddef.h> /* get size_t definition */
14
#include <string.h> /* get the memcpy/memset functions */
15
#include "c_skein.h" /* get the Skein API definitions */
16
17
#define DISABLE_UNUSED 0
18
19
#ifndef SKEIN_256_NIST_MAX_HASHBITS
20
#define SKEIN_256_NIST_MAX_HASHBITS (0)
21
#endif
22
23
#ifndef SKEIN_512_NIST_MAX_HASHBITS
24
#define SKEIN_512_NIST_MAX_HASHBITS (512)
25
#endif
26
27
#define SKEIN_MODIFIER_WORDS ( 2) /* number of modifier (tweak) words */
28
29
#define SKEIN_256_STATE_WORDS ( 4)
30
#define SKEIN_512_STATE_WORDS ( 8)
31
#define SKEIN1024_STATE_WORDS (16)
32
#define SKEIN_MAX_STATE_WORDS (16)
33
34
#define SKEIN_256_STATE_BYTES ( 8*SKEIN_256_STATE_WORDS)
35
#define SKEIN_512_STATE_BYTES ( 8*SKEIN_512_STATE_WORDS)
36
#define SKEIN1024_STATE_BYTES ( 8*SKEIN1024_STATE_WORDS)
37
38
#define SKEIN_256_STATE_BITS (64*SKEIN_256_STATE_WORDS)
39
#define SKEIN_512_STATE_BITS (64*SKEIN_512_STATE_WORDS)
40
#define SKEIN1024_STATE_BITS (64*SKEIN1024_STATE_WORDS)
41
42
#define SKEIN_256_BLOCK_BYTES ( 8*SKEIN_256_STATE_WORDS)
43
#define SKEIN_512_BLOCK_BYTES ( 8*SKEIN_512_STATE_WORDS)
44
#define SKEIN1024_BLOCK_BYTES ( 8*SKEIN1024_STATE_WORDS)
45
46
#define SKEIN_RND_SPECIAL (1000u)
47
#define SKEIN_RND_KEY_INITIAL (SKEIN_RND_SPECIAL+0u)
48
#define SKEIN_RND_KEY_INJECT (SKEIN_RND_SPECIAL+1u)
49
#define SKEIN_RND_FEED_FWD (SKEIN_RND_SPECIAL+2u)
50
51
typedef struct
52
{
53
size_t hashBitLen; /* size of hash result, in bits */
54
size_t bCnt; /* current byte count in buffer b[] */
55
u64b_t T[SKEIN_MODIFIER_WORDS]; /* tweak words: T[0]=byte cnt, T[1]=flags */
56
} Skein_Ctxt_Hdr_t;
57
58
typedef struct /* 256-bit Skein hash context structure */
59
{
60
Skein_Ctxt_Hdr_t h; /* common header context variables */
61
u64b_t X[SKEIN_256_STATE_WORDS]; /* chaining variables */
62
u08b_t b[SKEIN_256_BLOCK_BYTES]; /* partial block buffer (8-byte aligned) */
63
} Skein_256_Ctxt_t;
64
65
typedef struct /* 512-bit Skein hash context structure */
66
{
67
Skein_Ctxt_Hdr_t h; /* common header context variables */
68
u64b_t X[SKEIN_512_STATE_WORDS]; /* chaining variables */
69
u08b_t b[SKEIN_512_BLOCK_BYTES]; /* partial block buffer (8-byte aligned) */
70
} Skein_512_Ctxt_t;
71
72
typedef struct /* 1024-bit Skein hash context structure */
73
{
74
Skein_Ctxt_Hdr_t h; /* common header context variables */
75
u64b_t X[SKEIN1024_STATE_WORDS]; /* chaining variables */
76
u08b_t b[SKEIN1024_BLOCK_BYTES]; /* partial block buffer (8-byte aligned) */
77
} Skein1024_Ctxt_t;
78
79
/* Skein APIs for (incremental) "straight hashing" */
80
#if SKEIN_256_NIST_MAX_HASH_BITS
81
static int Skein_256_Init (Skein_256_Ctxt_t *ctx, size_t hashBitLen);
82
#endif
83
static int Skein_512_Init (Skein_512_Ctxt_t *ctx, size_t hashBitLen);
84
static int Skein1024_Init (Skein1024_Ctxt_t *ctx, size_t hashBitLen);
85
86
static int Skein_256_Update(Skein_256_Ctxt_t *ctx, const u08b_t *msg, size_t msgByteCnt);
87
static int Skein_512_Update(Skein_512_Ctxt_t *ctx, const u08b_t *msg, size_t msgByteCnt);
88
static int Skein1024_Update(Skein1024_Ctxt_t *ctx, const u08b_t *msg, size_t msgByteCnt);
89
90
static int Skein_256_Final (Skein_256_Ctxt_t *ctx, u08b_t * hashVal);
91
static int Skein_512_Final (Skein_512_Ctxt_t *ctx, u08b_t * hashVal);
92
static int Skein1024_Final (Skein1024_Ctxt_t *ctx, u08b_t * hashVal);
93
94
/*
95
** Skein APIs for "extended" initialization: MAC keys, tree hashing.
96
** After an InitExt() call, just use Update/Final calls as with Init().
97
**
98
** Notes: Same parameters as _Init() calls, plus treeInfo/key/keyBytes.
99
** When keyBytes == 0 and treeInfo == SKEIN_SEQUENTIAL,
100
** the results of InitExt() are identical to calling Init().
101
** The function Init() may be called once to "precompute" the IV for
102
** a given hashBitLen value, then by saving a copy of the context
103
** the IV computation may be avoided in later calls.
104
** Similarly, the function InitExt() may be called once per MAC key
105
** to precompute the MAC IV, then a copy of the context saved and
106
** reused for each new MAC computation.
107
**/
108
#if 0
109
static int Skein_256_InitExt(Skein_256_Ctxt_t *ctx, size_t hashBitLen, u64b_t treeInfo, const u08b_t *key, size_t keyBytes);
110
static int Skein_512_InitExt(Skein_512_Ctxt_t *ctx, size_t hashBitLen, u64b_t treeInfo, const u08b_t *key, size_t keyBytes);
111
static int Skein1024_InitExt(Skein1024_Ctxt_t *ctx, size_t hashBitLen, u64b_t treeInfo, const u08b_t *key, size_t keyBytes);
112
#endif
113
114
/*
115
** Skein APIs for MAC and tree hash:
116
** Final_Pad: pad, do final block, but no OUTPUT type
117
** Output: do just the output stage
118
*/
119
#if 0
120
static int Skein_256_Final_Pad(Skein_256_Ctxt_t *ctx, u08b_t * hashVal);
121
static int Skein_512_Final_Pad(Skein_512_Ctxt_t *ctx, u08b_t * hashVal);
122
static int Skein1024_Final_Pad(Skein1024_Ctxt_t *ctx, u08b_t * hashVal);
123
#endif
124
125
#ifndef SKEIN_TREE_HASH
126
#define SKEIN_TREE_HASH (1)
127
#endif
128
#if 0
129
#if SKEIN_TREE_HASH
130
static int Skein_256_Output (Skein_256_Ctxt_t *ctx, u08b_t * hashVal);
131
static int Skein_512_Output (Skein_512_Ctxt_t *ctx, u08b_t * hashVal);
132
static int Skein1024_Output (Skein1024_Ctxt_t *ctx, u08b_t * hashVal);
133
#endif
134
#endif
135
136
/*****************************************************************
137
** "Internal" Skein definitions
138
** -- not needed for sequential hashing API, but will be
139
** helpful for other uses of Skein (e.g., tree hash mode).
140
** -- included here so that they can be shared between
141
** reference and optimized code.
142
******************************************************************/
143
144
/* tweak word T[1]: bit field starting positions */
145
#define SKEIN_T1_BIT(BIT) ((BIT) - 64) /* offset 64 because it's the second word */
146
147
#define SKEIN_T1_POS_TREE_LVL SKEIN_T1_BIT(112) /* bits 112..118: level in hash tree */
148
#define SKEIN_T1_POS_BIT_PAD SKEIN_T1_BIT(119) /* bit 119 : partial final input byte */
149
#define SKEIN_T1_POS_BLK_TYPE SKEIN_T1_BIT(120) /* bits 120..125: type field */
150
#define SKEIN_T1_POS_FIRST SKEIN_T1_BIT(126) /* bits 126 : first block flag */
151
#define SKEIN_T1_POS_FINAL SKEIN_T1_BIT(127) /* bit 127 : final block flag */
152
153
/* tweak word T[1]: flag bit definition(s) */
154
#define SKEIN_T1_FLAG_FIRST (((u64b_t) 1 ) << SKEIN_T1_POS_FIRST)
155
#define SKEIN_T1_FLAG_FINAL (((u64b_t) 1 ) << SKEIN_T1_POS_FINAL)
156
#define SKEIN_T1_FLAG_BIT_PAD (((u64b_t) 1 ) << SKEIN_T1_POS_BIT_PAD)
157
158
/* tweak word T[1]: tree level bit field mask */
159
#define SKEIN_T1_TREE_LVL_MASK (((u64b_t)0x7F) << SKEIN_T1_POS_TREE_LVL)
160
#define SKEIN_T1_TREE_LEVEL(n) (((u64b_t) (n)) << SKEIN_T1_POS_TREE_LVL)
161
162
/* tweak word T[1]: block type field */
163
#define SKEIN_BLK_TYPE_KEY ( 0) /* key, for MAC and KDF */
164
#define SKEIN_BLK_TYPE_CFG ( 4) /* configuration block */
165
#define SKEIN_BLK_TYPE_PERS ( 8) /* personalization string */
166
#define SKEIN_BLK_TYPE_PK (12) /* public key (for digital signature hashing) */
167
#define SKEIN_BLK_TYPE_KDF (16) /* key identifier for KDF */
168
#define SKEIN_BLK_TYPE_NONCE (20) /* nonce for PRNG */
169
#define SKEIN_BLK_TYPE_MSG (48) /* message processing */
170
#define SKEIN_BLK_TYPE_OUT (63) /* output stage */
171
#define SKEIN_BLK_TYPE_MASK (63) /* bit field mask */
172
173
#define SKEIN_T1_BLK_TYPE(T) (((u64b_t) (SKEIN_BLK_TYPE_##T)) << SKEIN_T1_POS_BLK_TYPE)
174
#define SKEIN_T1_BLK_TYPE_KEY SKEIN_T1_BLK_TYPE(KEY) /* key, for MAC and KDF */
175
#define SKEIN_T1_BLK_TYPE_CFG SKEIN_T1_BLK_TYPE(CFG) /* configuration block */
176
#define SKEIN_T1_BLK_TYPE_PERS SKEIN_T1_BLK_TYPE(PERS) /* personalization string */
177
#define SKEIN_T1_BLK_TYPE_PK SKEIN_T1_BLK_TYPE(PK) /* public key (for digital signature hashing) */
178
#define SKEIN_T1_BLK_TYPE_KDF SKEIN_T1_BLK_TYPE(KDF) /* key identifier for KDF */
179
#define SKEIN_T1_BLK_TYPE_NONCE SKEIN_T1_BLK_TYPE(NONCE)/* nonce for PRNG */
180
#define SKEIN_T1_BLK_TYPE_MSG SKEIN_T1_BLK_TYPE(MSG) /* message processing */
181
#define SKEIN_T1_BLK_TYPE_OUT SKEIN_T1_BLK_TYPE(OUT) /* output stage */
182
#define SKEIN_T1_BLK_TYPE_MASK SKEIN_T1_BLK_TYPE(MASK) /* field bit mask */
183
184
#define SKEIN_T1_BLK_TYPE_CFG_FINAL (SKEIN_T1_BLK_TYPE_CFG | SKEIN_T1_FLAG_FINAL)
185
#define SKEIN_T1_BLK_TYPE_OUT_FINAL (SKEIN_T1_BLK_TYPE_OUT | SKEIN_T1_FLAG_FINAL)
186
187
#define SKEIN_VERSION (1)
188
189
#ifndef SKEIN_ID_STRING_LE /* allow compile-time personalization */
190
#define SKEIN_ID_STRING_LE (0x33414853) /* "SHA3" (little-endian)*/
191
#endif
192
193
#define SKEIN_MK_64(hi32,lo32) ((lo32) + (((u64b_t) (hi32)) << 32))
194
#define SKEIN_SCHEMA_VER SKEIN_MK_64(SKEIN_VERSION,SKEIN_ID_STRING_LE)
195
#define SKEIN_KS_PARITY SKEIN_MK_64(0x1BD11BDA,0xA9FC1A22)
196
197
#define SKEIN_CFG_STR_LEN (4*8)
198
199
/* bit field definitions in config block treeInfo word */
200
#define SKEIN_CFG_TREE_LEAF_SIZE_POS ( 0)
201
#define SKEIN_CFG_TREE_NODE_SIZE_POS ( 8)
202
#define SKEIN_CFG_TREE_MAX_LEVEL_POS (16)
203
204
#define SKEIN_CFG_TREE_LEAF_SIZE_MSK (((u64b_t) 0xFF) << SKEIN_CFG_TREE_LEAF_SIZE_POS)
205
#define SKEIN_CFG_TREE_NODE_SIZE_MSK (((u64b_t) 0xFF) << SKEIN_CFG_TREE_NODE_SIZE_POS)
206
#define SKEIN_CFG_TREE_MAX_LEVEL_MSK (((u64b_t) 0xFF) << SKEIN_CFG_TREE_MAX_LEVEL_POS)
207
208
#define SKEIN_CFG_TREE_INFO(leaf,node,maxLvl) \
209
( (((u64b_t)(leaf )) << SKEIN_CFG_TREE_LEAF_SIZE_POS) | \
210
(((u64b_t)(node )) << SKEIN_CFG_TREE_NODE_SIZE_POS) | \
211
(((u64b_t)(maxLvl)) << SKEIN_CFG_TREE_MAX_LEVEL_POS) )
212
213
#define SKEIN_CFG_TREE_INFO_SEQUENTIAL SKEIN_CFG_TREE_INFO(0,0,0) /* use as treeInfo in InitExt() call for sequential processing */
214
215
/*
216
** Skein macros for getting/setting tweak words, etc.
217
** These are useful for partial input bytes, hash tree init/update, etc.
218
**/
219
#define Skein_Get_Tweak(ctxPtr,TWK_NUM) ((ctxPtr)->h.T[TWK_NUM])
220
#define Skein_Set_Tweak(ctxPtr,TWK_NUM,tVal) {(ctxPtr)->h.T[TWK_NUM] = (tVal);}
221
222
#define Skein_Get_T0(ctxPtr) Skein_Get_Tweak(ctxPtr,0)
223
#define Skein_Get_T1(ctxPtr) Skein_Get_Tweak(ctxPtr,1)
224
#define Skein_Set_T0(ctxPtr,T0) Skein_Set_Tweak(ctxPtr,0,T0)
225
#define Skein_Set_T1(ctxPtr,T1) Skein_Set_Tweak(ctxPtr,1,T1)
226
227
/* set both tweak words at once */
228
#define Skein_Set_T0_T1(ctxPtr,T0,T1) \
229
{ \
230
Skein_Set_T0(ctxPtr,(T0)); \
231
Skein_Set_T1(ctxPtr,(T1)); \
232
}
233
234
#define Skein_Set_Type(ctxPtr,BLK_TYPE) \
235
Skein_Set_T1(ctxPtr,SKEIN_T1_BLK_TYPE_##BLK_TYPE)
236
237
/* set up for starting with a new type: h.T[0]=0; h.T[1] = NEW_TYPE; h.bCnt=0; */
238
#define Skein_Start_New_Type(ctxPtr,BLK_TYPE) \
239
{ Skein_Set_T0_T1(ctxPtr,0,SKEIN_T1_FLAG_FIRST | SKEIN_T1_BLK_TYPE_##BLK_TYPE); (ctxPtr)->h.bCnt=0; }
240
241
#define Skein_Clear_First_Flag(hdr) { (hdr).T[1] &= ~SKEIN_T1_FLAG_FIRST; }
242
#define Skein_Set_Bit_Pad_Flag(hdr) { (hdr).T[1] |= SKEIN_T1_FLAG_BIT_PAD; }
243
244
#define Skein_Set_Tree_Level(hdr,height) { (hdr).T[1] |= SKEIN_T1_TREE_LEVEL(height);}
245
246
/*****************************************************************
247
** "Internal" Skein definitions for debugging and error checking
248
******************************************************************/
249
#define Skein_Show_Block(bits,ctx,X,blkPtr,wPtr,ksEvenPtr,ksOddPtr)
250
#define Skein_Show_Round(bits,ctx,r,X)
251
#define Skein_Show_R_Ptr(bits,ctx,r,X_ptr)
252
#define Skein_Show_Final(bits,ctx,cnt,outPtr)
253
#define Skein_Show_Key(bits,ctx,key,keyBytes)
254
255
256
#ifndef SKEIN_ERR_CHECK /* run-time checks (e.g., bad params, uninitialized context)? */
257
#define Skein_Assert(x,retCode)/* default: ignore all Asserts, for performance */
258
#define Skein_assert(x)
259
#elif defined(SKEIN_ASSERT)
260
#include <assert.h>
261
#define Skein_Assert(x,retCode) assert(x)
262
#define Skein_assert(x) assert(x)
263
#else
264
#include <assert.h>
265
#define Skein_Assert(x,retCode) { if (!(x)) return retCode; } /* caller error */
266
#define Skein_assert(x) assert(x) /* internal error */
267
#endif
268
269
/*****************************************************************
270
** Skein block function constants (shared across Ref and Opt code)
271
******************************************************************/
272
enum
273
{
274
/* Skein_256 round rotation constants */
275
R_256_0_0=14, R_256_0_1=16,
276
R_256_1_0=52, R_256_1_1=57,
277
R_256_2_0=23, R_256_2_1=40,
278
R_256_3_0= 5, R_256_3_1=37,
279
R_256_4_0=25, R_256_4_1=33,
280
R_256_5_0=46, R_256_5_1=12,
281
R_256_6_0=58, R_256_6_1=22,
282
R_256_7_0=32, R_256_7_1=32,
283
284
/* Skein_512 round rotation constants */
285
R_512_0_0=46, R_512_0_1=36, R_512_0_2=19, R_512_0_3=37,
286
R_512_1_0=33, R_512_1_1=27, R_512_1_2=14, R_512_1_3=42,
287
R_512_2_0=17, R_512_2_1=49, R_512_2_2=36, R_512_2_3=39,
288
R_512_3_0=44, R_512_3_1= 9, R_512_3_2=54, R_512_3_3=56,
289
R_512_4_0=39, R_512_4_1=30, R_512_4_2=34, R_512_4_3=24,
290
R_512_5_0=13, R_512_5_1=50, R_512_5_2=10, R_512_5_3=17,
291
R_512_6_0=25, R_512_6_1=29, R_512_6_2=39, R_512_6_3=43,
292
R_512_7_0= 8, R_512_7_1=35, R_512_7_2=56, R_512_7_3=22,
293
294
/* Skein1024 round rotation constants */
295
R1024_0_0=24, R1024_0_1=13, R1024_0_2= 8, R1024_0_3=47, R1024_0_4= 8, R1024_0_5=17, R1024_0_6=22, R1024_0_7=37,
296
R1024_1_0=38, R1024_1_1=19, R1024_1_2=10, R1024_1_3=55, R1024_1_4=49, R1024_1_5=18, R1024_1_6=23, R1024_1_7=52,
297
R1024_2_0=33, R1024_2_1= 4, R1024_2_2=51, R1024_2_3=13, R1024_2_4=34, R1024_2_5=41, R1024_2_6=59, R1024_2_7=17,
298
R1024_3_0= 5, R1024_3_1=20, R1024_3_2=48, R1024_3_3=41, R1024_3_4=47, R1024_3_5=28, R1024_3_6=16, R1024_3_7=25,
299
R1024_4_0=41, R1024_4_1= 9, R1024_4_2=37, R1024_4_3=31, R1024_4_4=12, R1024_4_5=47, R1024_4_6=44, R1024_4_7=30,
300
R1024_5_0=16, R1024_5_1=34, R1024_5_2=56, R1024_5_3=51, R1024_5_4= 4, R1024_5_5=53, R1024_5_6=42, R1024_5_7=41,
301
R1024_6_0=31, R1024_6_1=44, R1024_6_2=47, R1024_6_3=46, R1024_6_4=19, R1024_6_5=42, R1024_6_6=44, R1024_6_7=25,
302
R1024_7_0= 9, R1024_7_1=48, R1024_7_2=35, R1024_7_3=52, R1024_7_4=23, R1024_7_5=31, R1024_7_6=37, R1024_7_7=20
303
};
304
305
#ifndef SKEIN_ROUNDS
306
#define SKEIN_256_ROUNDS_TOTAL (72) /* number of rounds for the different block sizes */
307
#define SKEIN_512_ROUNDS_TOTAL (72)
308
#define SKEIN1024_ROUNDS_TOTAL (80)
309
#else /* allow command-line define in range 8*(5..14) */
310
#define SKEIN_256_ROUNDS_TOTAL (8*((((SKEIN_ROUNDS/100) + 5) % 10) + 5))
311
#define SKEIN_512_ROUNDS_TOTAL (8*((((SKEIN_ROUNDS/ 10) + 5) % 10) + 5))
312
#define SKEIN1024_ROUNDS_TOTAL (8*((((SKEIN_ROUNDS ) + 5) % 10) + 5))
313
#endif
314
315
316
/*
317
***************** Pre-computed Skein IVs *******************
318
**
319
** NOTE: these values are not "magic" constants, but
320
** are generated using the Threefish block function.
321
** They are pre-computed here only for speed; i.e., to
322
** avoid the need for a Threefish call during Init().
323
**
324
** The IV for any fixed hash length may be pre-computed.
325
** Only the most common values are included here.
326
**
327
************************************************************
328
**/
329
330
#define MK_64 SKEIN_MK_64
331
332
/* blkSize = 256 bits. hashSize = 128 bits */
333
const u64b_t SKEIN_256_IV_128[] =
334
{
335
MK_64(0xE1111906,0x964D7260),
336
MK_64(0x883DAAA7,0x7C8D811C),
337
MK_64(0x10080DF4,0x91960F7A),
338
MK_64(0xCCF7DDE5,0xB45BC1C2)
339
};
340
341
/* blkSize = 256 bits. hashSize = 160 bits */
342
const u64b_t SKEIN_256_IV_160[] =
343
{
344
MK_64(0x14202314,0x72825E98),
345
MK_64(0x2AC4E9A2,0x5A77E590),
346
MK_64(0xD47A5856,0x8838D63E),
347
MK_64(0x2DD2E496,0x8586AB7D)
348
};
349
350
/* blkSize = 256 bits. hashSize = 224 bits */
351
const u64b_t SKEIN_256_IV_224[] =
352
{
353
MK_64(0xC6098A8C,0x9AE5EA0B),
354
MK_64(0x876D5686,0x08C5191C),
355
MK_64(0x99CB88D7,0xD7F53884),
356
MK_64(0x384BDDB1,0xAEDDB5DE)
357
};
358
359
/* blkSize = 256 bits. hashSize = 256 bits */
360
const u64b_t SKEIN_256_IV_256[] =
361
{
362
MK_64(0xFC9DA860,0xD048B449),
363
MK_64(0x2FCA6647,0x9FA7D833),
364
MK_64(0xB33BC389,0x6656840F),
365
MK_64(0x6A54E920,0xFDE8DA69)
366
};
367
368
/* blkSize = 512 bits. hashSize = 128 bits */
369
const u64b_t SKEIN_512_IV_128[] =
370
{
371
MK_64(0xA8BC7BF3,0x6FBF9F52),
372
MK_64(0x1E9872CE,0xBD1AF0AA),
373
MK_64(0x309B1790,0xB32190D3),
374
MK_64(0xBCFBB854,0x3F94805C),
375
MK_64(0x0DA61BCD,0x6E31B11B),
376
MK_64(0x1A18EBEA,0xD46A32E3),
377
MK_64(0xA2CC5B18,0xCE84AA82),
378
MK_64(0x6982AB28,0x9D46982D)
379
};
380
381
/* blkSize = 512 bits. hashSize = 160 bits */
382
const u64b_t SKEIN_512_IV_160[] =
383
{
384
MK_64(0x28B81A2A,0xE013BD91),
385
MK_64(0xC2F11668,0xB5BDF78F),
386
MK_64(0x1760D8F3,0xF6A56F12),
387
MK_64(0x4FB74758,0x8239904F),
388
MK_64(0x21EDE07F,0x7EAF5056),
389
MK_64(0xD908922E,0x63ED70B8),
390
MK_64(0xB8EC76FF,0xECCB52FA),
391
MK_64(0x01A47BB8,0xA3F27A6E)
392
};
393
394
/* blkSize = 512 bits. hashSize = 224 bits */
395
const u64b_t SKEIN_512_IV_224[] =
396
{
397
MK_64(0xCCD06162,0x48677224),
398
MK_64(0xCBA65CF3,0xA92339EF),
399
MK_64(0x8CCD69D6,0x52FF4B64),
400
MK_64(0x398AED7B,0x3AB890B4),
401
MK_64(0x0F59D1B1,0x457D2BD0),
402
MK_64(0x6776FE65,0x75D4EB3D),
403
MK_64(0x99FBC70E,0x997413E9),
404
MK_64(0x9E2CFCCF,0xE1C41EF7)
405
};
406
407
/* blkSize = 512 bits. hashSize = 256 bits */
408
const u64b_t SKEIN_512_IV_256[] =
409
{
410
MK_64(0xCCD044A1,0x2FDB3E13),
411
MK_64(0xE8359030,0x1A79A9EB),
412
MK_64(0x55AEA061,0x4F816E6F),
413
MK_64(0x2A2767A4,0xAE9B94DB),
414
MK_64(0xEC06025E,0x74DD7683),
415
MK_64(0xE7A436CD,0xC4746251),
416
MK_64(0xC36FBAF9,0x393AD185),
417
MK_64(0x3EEDBA18,0x33EDFC13)
418
};
419
420
/* blkSize = 512 bits. hashSize = 384 bits */
421
const u64b_t SKEIN_512_IV_384[] =
422
{
423
MK_64(0xA3F6C6BF,0x3A75EF5F),
424
MK_64(0xB0FEF9CC,0xFD84FAA4),
425
MK_64(0x9D77DD66,0x3D770CFE),
426
MK_64(0xD798CBF3,0xB468FDDA),
427
MK_64(0x1BC4A666,0x8A0E4465),
428
MK_64(0x7ED7D434,0xE5807407),
429
MK_64(0x548FC1AC,0xD4EC44D6),
430
MK_64(0x266E1754,0x6AA18FF8)
431
};
432
433
/* blkSize = 512 bits. hashSize = 512 bits */
434
const u64b_t SKEIN_512_IV_512[] =
435
{
436
MK_64(0x4903ADFF,0x749C51CE),
437
MK_64(0x0D95DE39,0x9746DF03),
438
MK_64(0x8FD19341,0x27C79BCE),
439
MK_64(0x9A255629,0xFF352CB1),
440
MK_64(0x5DB62599,0xDF6CA7B0),
441
MK_64(0xEABE394C,0xA9D5C3F4),
442
MK_64(0x991112C7,0x1A75B523),
443
MK_64(0xAE18A40B,0x660FCC33)
444
};
445
446
/* blkSize = 1024 bits. hashSize = 384 bits */
447
const u64b_t SKEIN1024_IV_384[] =
448
{
449
MK_64(0x5102B6B8,0xC1894A35),
450
MK_64(0xFEEBC9E3,0xFE8AF11A),
451
MK_64(0x0C807F06,0xE32BED71),
452
MK_64(0x60C13A52,0xB41A91F6),
453
MK_64(0x9716D35D,0xD4917C38),
454
MK_64(0xE780DF12,0x6FD31D3A),
455
MK_64(0x797846B6,0xC898303A),
456
MK_64(0xB172C2A8,0xB3572A3B),
457
MK_64(0xC9BC8203,0xA6104A6C),
458
MK_64(0x65909338,0xD75624F4),
459
MK_64(0x94BCC568,0x4B3F81A0),
460
MK_64(0x3EBBF51E,0x10ECFD46),
461
MK_64(0x2DF50F0B,0xEEB08542),
462
MK_64(0x3B5A6530,0x0DBC6516),
463
MK_64(0x484B9CD2,0x167BBCE1),
464
MK_64(0x2D136947,0xD4CBAFEA)
465
};
466
467
/* blkSize = 1024 bits. hashSize = 512 bits */
468
const u64b_t SKEIN1024_IV_512[] =
469
{
470
MK_64(0xCAEC0E5D,0x7C1B1B18),
471
MK_64(0xA01B0E04,0x5F03E802),
472
MK_64(0x33840451,0xED912885),
473
MK_64(0x374AFB04,0xEAEC2E1C),
474
MK_64(0xDF25A0E2,0x813581F7),
475
MK_64(0xE4004093,0x8B12F9D2),
476
MK_64(0xA662D539,0xC2ED39B6),
477
MK_64(0xFA8B85CF,0x45D8C75A),
478
MK_64(0x8316ED8E,0x29EDE796),
479
MK_64(0x053289C0,0x2E9F91B8),
480
MK_64(0xC3F8EF1D,0x6D518B73),
481
MK_64(0xBDCEC3C4,0xD5EF332E),
482
MK_64(0x549A7E52,0x22974487),
483
MK_64(0x67070872,0x5B749816),
484
MK_64(0xB9CD28FB,0xF0581BD1),
485
MK_64(0x0E2940B8,0x15804974)
486
};
487
488
/* blkSize = 1024 bits. hashSize = 1024 bits */
489
const u64b_t SKEIN1024_IV_1024[] =
490
{
491
MK_64(0xD593DA07,0x41E72355),
492
MK_64(0x15B5E511,0xAC73E00C),
493
MK_64(0x5180E5AE,0xBAF2C4F0),
494
MK_64(0x03BD41D3,0xFCBCAFAF),
495
MK_64(0x1CAEC6FD,0x1983A898),
496
MK_64(0x6E510B8B,0xCDD0589F),
497
MK_64(0x77E2BDFD,0xC6394ADA),
498
MK_64(0xC11E1DB5,0x24DCB0A3),
499
MK_64(0xD6D14AF9,0xC6329AB5),
500
MK_64(0x6A9B0BFC,0x6EB67E0D),
501
MK_64(0x9243C60D,0xCCFF1332),
502
MK_64(0x1A1F1DDE,0x743F02D4),
503
MK_64(0x0996753C,0x10ED0BB8),
504
MK_64(0x6572DD22,0xF2B4969A),
505
MK_64(0x61FD3062,0xD00A579A),
506
MK_64(0x1DE0536E,0x8682E539)
507
};
508
509
510
#ifndef SKEIN_USE_ASM
511
#define SKEIN_USE_ASM (0) /* default is all C code (no ASM) */
512
#endif
513
514
#ifndef SKEIN_LOOP
515
#define SKEIN_LOOP 001 /* default: unroll 256 and 512, but not 1024 */
516
#endif
517
518
#define BLK_BITS (WCNT*64) /* some useful definitions for code here */
519
#define KW_TWK_BASE (0)
520
#define KW_KEY_BASE (3)
521
#define ks (kw + KW_KEY_BASE)
522
#define ts (kw + KW_TWK_BASE)
523
524
#ifdef SKEIN_DEBUG
525
#define DebugSaveTweak(ctx) { ctx->h.T[0] = ts[0]; ctx->h.T[1] = ts[1]; }
526
#else
527
#define DebugSaveTweak(ctx)
528
#endif
529
530
/***************************** Skein_256 ******************************/
531
#if !(SKEIN_USE_ASM & 256)
532
static void Skein_256_Process_Block(Skein_256_Ctxt_t *ctx,const u08b_t *blkPtr,size_t blkCnt,size_t byteCntAdd)
533
{ /* do it in C */
534
enum
535
{
536
WCNT = SKEIN_256_STATE_WORDS
537
};
538
#undef RCNT
539
#define RCNT (SKEIN_256_ROUNDS_TOTAL/8)
540
541
#ifdef SKEIN_LOOP /* configure how much to unroll the loop */
542
#define SKEIN_UNROLL_256 (((SKEIN_LOOP)/100)%10)
543
#else
544
#define SKEIN_UNROLL_256 (0)
545
#endif
546
547
#if SKEIN_UNROLL_256
548
#if (RCNT % SKEIN_UNROLL_256)
549
#error "Invalid SKEIN_UNROLL_256" /* sanity check on unroll count */
550
#endif
551
size_t r;
552
u64b_t kw[WCNT+4+RCNT*2]; /* key schedule words : chaining vars + tweak + "rotation"*/
553
#else
554
u64b_t kw[WCNT+4]; /* key schedule words : chaining vars + tweak */
555
#endif
556
u64b_t X0,X1,X2,X3; /* local copy of context vars, for speed */
557
u64b_t w [WCNT]; /* local copy of input block */
558
#ifdef SKEIN_DEBUG
559
const u64b_t *Xptr[4]; /* use for debugging (help compiler put Xn in registers) */
560
Xptr[0] = &X0; Xptr[1] = &X1; Xptr[2] = &X2; Xptr[3] = &X3;
561
#endif
562
Skein_assert(blkCnt != 0); /* never call with blkCnt == 0! */
563
ts[0] = ctx->h.T[0];
564
ts[1] = ctx->h.T[1];
565
do {
566
/* this implementation only supports 2**64 input bytes (no carry out here) */
567
ts[0] += byteCntAdd; /* update processed length */
568
569
/* precompute the key schedule for this block */
570
ks[0] = ctx->X[0];
571
ks[1] = ctx->X[1];
572
ks[2] = ctx->X[2];
573
ks[3] = ctx->X[3];
574
ks[4] = ks[0] ^ ks[1] ^ ks[2] ^ ks[3] ^ SKEIN_KS_PARITY;
575
576
ts[2] = ts[0] ^ ts[1];
577
578
Skein_Get64_LSB_First(w,blkPtr,WCNT); /* get input block in little-endian format */
579
DebugSaveTweak(ctx);
580
Skein_Show_Block(BLK_BITS,&ctx->h,ctx->X,blkPtr,w,ks,ts);
581
582
X0 = w[0] + ks[0]; /* do the first full key injection */
583
X1 = w[1] + ks[1] + ts[0];
584
X2 = w[2] + ks[2] + ts[1];
585
X3 = w[3] + ks[3];
586
587
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INITIAL,Xptr); /* show starting state values */
588
589
blkPtr += SKEIN_256_BLOCK_BYTES;
590
591
/* run the rounds */
592
593
#define Round256(p0,p1,p2,p3,ROT,rNum) \
594
X##p0 += X##p1; X##p1 = RotL_64(X##p1,ROT##_0); X##p1 ^= X##p0; \
595
X##p2 += X##p3; X##p3 = RotL_64(X##p3,ROT##_1); X##p3 ^= X##p2; \
596
597
#if SKEIN_UNROLL_256 == 0
598
#define R256(p0,p1,p2,p3,ROT,rNum) /* fully unrolled */ \
599
Round256(p0,p1,p2,p3,ROT,rNum) \
600
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,rNum,Xptr);
601
602
#define I256(R) \
603
X0 += ks[((R)+1) % 5]; /* inject the key schedule value */ \
604
X1 += ks[((R)+2) % 5] + ts[((R)+1) % 3]; \
605
X2 += ks[((R)+3) % 5] + ts[((R)+2) % 3]; \
606
X3 += ks[((R)+4) % 5] + (R)+1; \
607
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr);
608
#else /* looping version */
609
#define R256(p0,p1,p2,p3,ROT,rNum) \
610
Round256(p0,p1,p2,p3,ROT,rNum) \
611
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,4*(r-1)+rNum,Xptr);
612
613
#define I256(R) \
614
X0 += ks[r+(R)+0]; /* inject the key schedule value */ \
615
X1 += ks[r+(R)+1] + ts[r+(R)+0]; \
616
X2 += ks[r+(R)+2] + ts[r+(R)+1]; \
617
X3 += ks[r+(R)+3] + r+(R) ; \
618
ks[r + (R)+4 ] = ks[r+(R)-1]; /* rotate key schedule */\
619
ts[r + (R)+2 ] = ts[r+(R)-1]; \
620
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr);
621
622
for (r=1;r < 2*RCNT;r+=2*SKEIN_UNROLL_256) /* loop thru it */
623
#endif
624
{
625
#define R256_8_rounds(R) \
626
R256(0,1,2,3,R_256_0,8*(R) + 1); \
627
R256(0,3,2,1,R_256_1,8*(R) + 2); \
628
R256(0,1,2,3,R_256_2,8*(R) + 3); \
629
R256(0,3,2,1,R_256_3,8*(R) + 4); \
630
I256(2*(R)); \
631
R256(0,1,2,3,R_256_4,8*(R) + 5); \
632
R256(0,3,2,1,R_256_5,8*(R) + 6); \
633
R256(0,1,2,3,R_256_6,8*(R) + 7); \
634
R256(0,3,2,1,R_256_7,8*(R) + 8); \
635
I256(2*(R)+1);
636
637
R256_8_rounds( 0);
638
639
#define R256_Unroll_R(NN) ((SKEIN_UNROLL_256 == 0 && SKEIN_256_ROUNDS_TOTAL/8 > (NN)) || (SKEIN_UNROLL_256 > (NN)))
640
641
#if R256_Unroll_R( 1)
642
R256_8_rounds( 1);
643
#endif
644
#if R256_Unroll_R( 2)
645
R256_8_rounds( 2);
646
#endif
647
#if R256_Unroll_R( 3)
648
R256_8_rounds( 3);
649
#endif
650
#if R256_Unroll_R( 4)
651
R256_8_rounds( 4);
652
#endif
653
#if R256_Unroll_R( 5)
654
R256_8_rounds( 5);
655
#endif
656
#if R256_Unroll_R( 6)
657
R256_8_rounds( 6);
658
#endif
659
#if R256_Unroll_R( 7)
660
R256_8_rounds( 7);
661
#endif
662
#if R256_Unroll_R( 8)
663
R256_8_rounds( 8);
664
#endif
665
#if R256_Unroll_R( 9)
666
R256_8_rounds( 9);
667
#endif
668
#if R256_Unroll_R(10)
669
R256_8_rounds(10);
670
#endif
671
#if R256_Unroll_R(11)
672
R256_8_rounds(11);
673
#endif
674
#if R256_Unroll_R(12)
675
R256_8_rounds(12);
676
#endif
677
#if R256_Unroll_R(13)
678
R256_8_rounds(13);
679
#endif
680
#if R256_Unroll_R(14)
681
R256_8_rounds(14);
682
#endif
683
#if (SKEIN_UNROLL_256 > 14)
684
#error "need more unrolling in Skein_256_Process_Block"
685
#endif
686
}
687
/* do the final "feedforward" xor, update context chaining vars */
688
ctx->X[0] = X0 ^ w[0];
689
ctx->X[1] = X1 ^ w[1];
690
ctx->X[2] = X2 ^ w[2];
691
ctx->X[3] = X3 ^ w[3];
692
693
Skein_Show_Round(BLK_BITS,&ctx->h,SKEIN_RND_FEED_FWD,ctx->X);
694
695
ts[1] &= ~SKEIN_T1_FLAG_FIRST;
696
}
697
while (--blkCnt);
698
ctx->h.T[0] = ts[0];
699
ctx->h.T[1] = ts[1];
700
}
701
702
#if defined(SKEIN_CODE_SIZE) || defined(SKEIN_PERF)
703
static size_t Skein_256_Process_Block_CodeSize(void)
704
{
705
return ((u08b_t *) Skein_256_Process_Block_CodeSize) -
706
((u08b_t *) Skein_256_Process_Block);
707
}
708
static uint_t Skein_256_Unroll_Cnt(void)
709
{
710
return SKEIN_UNROLL_256;
711
}
712
#endif
713
#endif
714
715
/***************************** Skein_512 ******************************/
716
#if !(SKEIN_USE_ASM & 512)
717
static void Skein_512_Process_Block(Skein_512_Ctxt_t *ctx,const u08b_t *blkPtr,size_t blkCnt,size_t byteCntAdd)
718
{ /* do it in C */
719
enum
720
{
721
WCNT = SKEIN_512_STATE_WORDS
722
};
723
#undef RCNT
724
#define RCNT (SKEIN_512_ROUNDS_TOTAL/8)
725
726
#ifdef SKEIN_LOOP /* configure how much to unroll the loop */
727
#define SKEIN_UNROLL_512 (((SKEIN_LOOP)/10)%10)
728
#else
729
#define SKEIN_UNROLL_512 (0)
730
#endif
731
732
#if SKEIN_UNROLL_512
733
#if (RCNT % SKEIN_UNROLL_512)
734
#error "Invalid SKEIN_UNROLL_512" /* sanity check on unroll count */
735
#endif
736
size_t r;
737
u64b_t kw[WCNT+4+RCNT*2]; /* key schedule words : chaining vars + tweak + "rotation"*/
738
#else
739
u64b_t kw[WCNT+4]; /* key schedule words : chaining vars + tweak */
740
#endif
741
u64b_t X0,X1,X2,X3,X4,X5,X6,X7; /* local copy of vars, for speed */
742
u64b_t w [WCNT]; /* local copy of input block */
743
#ifdef SKEIN_DEBUG
744
const u64b_t *Xptr[8]; /* use for debugging (help compiler put Xn in registers) */
745
Xptr[0] = &X0; Xptr[1] = &X1; Xptr[2] = &X2; Xptr[3] = &X3;
746
Xptr[4] = &X4; Xptr[5] = &X5; Xptr[6] = &X6; Xptr[7] = &X7;
747
#endif
748
749
Skein_assert(blkCnt != 0); /* never call with blkCnt == 0! */
750
ts[0] = ctx->h.T[0];
751
ts[1] = ctx->h.T[1];
752
do {
753
/* this implementation only supports 2**64 input bytes (no carry out here) */
754
ts[0] += byteCntAdd; /* update processed length */
755
756
/* precompute the key schedule for this block */
757
ks[0] = ctx->X[0];
758
ks[1] = ctx->X[1];
759
ks[2] = ctx->X[2];
760
ks[3] = ctx->X[3];
761
ks[4] = ctx->X[4];
762
ks[5] = ctx->X[5];
763
ks[6] = ctx->X[6];
764
ks[7] = ctx->X[7];
765
ks[8] = ks[0] ^ ks[1] ^ ks[2] ^ ks[3] ^
766
ks[4] ^ ks[5] ^ ks[6] ^ ks[7] ^ SKEIN_KS_PARITY;
767
768
ts[2] = ts[0] ^ ts[1];
769
770
Skein_Get64_LSB_First(w,blkPtr,WCNT); /* get input block in little-endian format */
771
DebugSaveTweak(ctx);
772
Skein_Show_Block(BLK_BITS,&ctx->h,ctx->X,blkPtr,w,ks,ts);
773
774
X0 = w[0] + ks[0]; /* do the first full key injection */
775
X1 = w[1] + ks[1];
776
X2 = w[2] + ks[2];
777
X3 = w[3] + ks[3];
778
X4 = w[4] + ks[4];
779
X5 = w[5] + ks[5] + ts[0];
780
X6 = w[6] + ks[6] + ts[1];
781
X7 = w[7] + ks[7];
782
783
blkPtr += SKEIN_512_BLOCK_BYTES;
784
785
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INITIAL,Xptr);
786
/* run the rounds */
787
#define Round512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) \
788
X##p0 += X##p1; X##p1 = RotL_64(X##p1,ROT##_0); X##p1 ^= X##p0; \
789
X##p2 += X##p3; X##p3 = RotL_64(X##p3,ROT##_1); X##p3 ^= X##p2; \
790
X##p4 += X##p5; X##p5 = RotL_64(X##p5,ROT##_2); X##p5 ^= X##p4; \
791
X##p6 += X##p7; X##p7 = RotL_64(X##p7,ROT##_3); X##p7 ^= X##p6; \
792
793
#if SKEIN_UNROLL_512 == 0
794
#define R512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) /* unrolled */ \
795
Round512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) \
796
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,rNum,Xptr);
797
798
#define I512(R) \
799
X0 += ks[((R)+1) % 9]; /* inject the key schedule value */ \
800
X1 += ks[((R)+2) % 9]; \
801
X2 += ks[((R)+3) % 9]; \
802
X3 += ks[((R)+4) % 9]; \
803
X4 += ks[((R)+5) % 9]; \
804
X5 += ks[((R)+6) % 9] + ts[((R)+1) % 3]; \
805
X6 += ks[((R)+7) % 9] + ts[((R)+2) % 3]; \
806
X7 += ks[((R)+8) % 9] + (R)+1; \
807
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr);
808
#else /* looping version */
809
#define R512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) \
810
Round512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) \
811
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,4*(r-1)+rNum,Xptr);
812
813
#define I512(R) \
814
X0 += ks[r+(R)+0]; /* inject the key schedule value */ \
815
X1 += ks[r+(R)+1]; \
816
X2 += ks[r+(R)+2]; \
817
X3 += ks[r+(R)+3]; \
818
X4 += ks[r+(R)+4]; \
819
X5 += ks[r+(R)+5] + ts[r+(R)+0]; \
820
X6 += ks[r+(R)+6] + ts[r+(R)+1]; \
821
X7 += ks[r+(R)+7] + r+(R) ; \
822
ks[r + (R)+8] = ks[r+(R)-1]; /* rotate key schedule */ \
823
ts[r + (R)+2] = ts[r+(R)-1]; \
824
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr);
825
826
for (r=1;r < 2*RCNT;r+=2*SKEIN_UNROLL_512) /* loop thru it */
827
#endif /* end of looped code definitions */
828
{
829
#define R512_8_rounds(R) /* do 8 full rounds */ \
830
R512(0,1,2,3,4,5,6,7,R_512_0,8*(R)+ 1); \
831
R512(2,1,4,7,6,5,0,3,R_512_1,8*(R)+ 2); \
832
R512(4,1,6,3,0,5,2,7,R_512_2,8*(R)+ 3); \
833
R512(6,1,0,7,2,5,4,3,R_512_3,8*(R)+ 4); \
834
I512(2*(R)); \
835
R512(0,1,2,3,4,5,6,7,R_512_4,8*(R)+ 5); \
836
R512(2,1,4,7,6,5,0,3,R_512_5,8*(R)+ 6); \
837
R512(4,1,6,3,0,5,2,7,R_512_6,8*(R)+ 7); \
838
R512(6,1,0,7,2,5,4,3,R_512_7,8*(R)+ 8); \
839
I512(2*(R)+1); /* and key injection */
840
841
R512_8_rounds( 0);
842
843
#define R512_Unroll_R(NN) ((SKEIN_UNROLL_512 == 0 && SKEIN_512_ROUNDS_TOTAL/8 > (NN)) || (SKEIN_UNROLL_512 > (NN)))
844
845
#if R512_Unroll_R( 1)
846
R512_8_rounds( 1);
847
#endif
848
#if R512_Unroll_R( 2)
849
R512_8_rounds( 2);
850
#endif
851
#if R512_Unroll_R( 3)
852
R512_8_rounds( 3);
853
#endif
854
#if R512_Unroll_R( 4)
855
R512_8_rounds( 4);
856
#endif
857
#if R512_Unroll_R( 5)
858
R512_8_rounds( 5);
859
#endif
860
#if R512_Unroll_R( 6)
861
R512_8_rounds( 6);
862
#endif
863
#if R512_Unroll_R( 7)
864
R512_8_rounds( 7);
865
#endif
866
#if R512_Unroll_R( 8)
867
R512_8_rounds( 8);
868
#endif
869
#if R512_Unroll_R( 9)
870
R512_8_rounds( 9);
871
#endif
872
#if R512_Unroll_R(10)
873
R512_8_rounds(10);
874
#endif
875
#if R512_Unroll_R(11)
876
R512_8_rounds(11);
877
#endif
878
#if R512_Unroll_R(12)
879
R512_8_rounds(12);
880
#endif
881
#if R512_Unroll_R(13)
882
R512_8_rounds(13);
883
#endif
884
#if R512_Unroll_R(14)
885
R512_8_rounds(14);
886
#endif
887
#if (SKEIN_UNROLL_512 > 14)
888
#error "need more unrolling in Skein_512_Process_Block"
889
#endif
890
}
891
892
/* do the final "feedforward" xor, update context chaining vars */
893
ctx->X[0] = X0 ^ w[0];
894
ctx->X[1] = X1 ^ w[1];
895
ctx->X[2] = X2 ^ w[2];
896
ctx->X[3] = X3 ^ w[3];
897
ctx->X[4] = X4 ^ w[4];
898
ctx->X[5] = X5 ^ w[5];
899
ctx->X[6] = X6 ^ w[6];
900
ctx->X[7] = X7 ^ w[7];
901
Skein_Show_Round(BLK_BITS,&ctx->h,SKEIN_RND_FEED_FWD,ctx->X);
902
903
ts[1] &= ~SKEIN_T1_FLAG_FIRST;
904
}
905
while (--blkCnt);
906
ctx->h.T[0] = ts[0];
907
ctx->h.T[1] = ts[1];
908
}
909
910
#if defined(SKEIN_CODE_SIZE) || defined(SKEIN_PERF)
911
static size_t Skein_512_Process_Block_CodeSize(void)
912
{
913
return ((u08b_t *) Skein_512_Process_Block_CodeSize) -
914
((u08b_t *) Skein_512_Process_Block);
915
}
916
static uint_t Skein_512_Unroll_Cnt(void)
917
{
918
return SKEIN_UNROLL_512;
919
}
920
#endif
921
#endif
922
923
/***************************** Skein1024 ******************************/
924
#if !(SKEIN_USE_ASM & 1024)
925
static void Skein1024_Process_Block(Skein1024_Ctxt_t *ctx,const u08b_t *blkPtr,size_t blkCnt,size_t byteCntAdd)
926
{ /* do it in C, always looping (unrolled is bigger AND slower!) */
927
enum
928
{
929
WCNT = SKEIN1024_STATE_WORDS
930
};
931
#undef RCNT
932
#define RCNT (SKEIN1024_ROUNDS_TOTAL/8)
933
934
#ifdef SKEIN_LOOP /* configure how much to unroll the loop */
935
#define SKEIN_UNROLL_1024 ((SKEIN_LOOP)%10)
936
#else
937
#define SKEIN_UNROLL_1024 (0)
938
#endif
939
940
#if (SKEIN_UNROLL_1024 != 0)
941
#if (RCNT % SKEIN_UNROLL_1024)
942
#error "Invalid SKEIN_UNROLL_1024" /* sanity check on unroll count */
943
#endif
944
size_t r;
945
u64b_t kw[WCNT+4+RCNT*2]; /* key schedule words : chaining vars + tweak + "rotation"*/
946
#else
947
u64b_t kw[WCNT+4]; /* key schedule words : chaining vars + tweak */
948
#endif
949
950
u64b_t X00,X01,X02,X03,X04,X05,X06,X07, /* local copy of vars, for speed */
951
X08,X09,X10,X11,X12,X13,X14,X15;
952
u64b_t w [WCNT]; /* local copy of input block */
953
#ifdef SKEIN_DEBUG
954
const u64b_t *Xptr[16]; /* use for debugging (help compiler put Xn in registers) */
955
Xptr[ 0] = &X00; Xptr[ 1] = &X01; Xptr[ 2] = &X02; Xptr[ 3] = &X03;
956
Xptr[ 4] = &X04; Xptr[ 5] = &X05; Xptr[ 6] = &X06; Xptr[ 7] = &X07;
957
Xptr[ 8] = &X08; Xptr[ 9] = &X09; Xptr[10] = &X10; Xptr[11] = &X11;
958
Xptr[12] = &X12; Xptr[13] = &X13; Xptr[14] = &X14; Xptr[15] = &X15;
959
#endif
960
961
Skein_assert(blkCnt != 0); /* never call with blkCnt == 0! */
962
ts[0] = ctx->h.T[0];
963
ts[1] = ctx->h.T[1];
964
do {
965
/* this implementation only supports 2**64 input bytes (no carry out here) */
966
ts[0] += byteCntAdd; /* update processed length */
967
968
/* precompute the key schedule for this block */
969
ks[ 0] = ctx->X[ 0];
970
ks[ 1] = ctx->X[ 1];
971
ks[ 2] = ctx->X[ 2];
972
ks[ 3] = ctx->X[ 3];
973
ks[ 4] = ctx->X[ 4];
974
ks[ 5] = ctx->X[ 5];
975
ks[ 6] = ctx->X[ 6];
976
ks[ 7] = ctx->X[ 7];
977
ks[ 8] = ctx->X[ 8];
978
ks[ 9] = ctx->X[ 9];
979
ks[10] = ctx->X[10];
980
ks[11] = ctx->X[11];
981
ks[12] = ctx->X[12];
982
ks[13] = ctx->X[13];
983
ks[14] = ctx->X[14];
984
ks[15] = ctx->X[15];
985
ks[16] = ks[ 0] ^ ks[ 1] ^ ks[ 2] ^ ks[ 3] ^
986
ks[ 4] ^ ks[ 5] ^ ks[ 6] ^ ks[ 7] ^
987
ks[ 8] ^ ks[ 9] ^ ks[10] ^ ks[11] ^
988
ks[12] ^ ks[13] ^ ks[14] ^ ks[15] ^ SKEIN_KS_PARITY;
989
990
ts[2] = ts[0] ^ ts[1];
991
992
Skein_Get64_LSB_First(w,blkPtr,WCNT); /* get input block in little-endian format */
993
DebugSaveTweak(ctx);
994
Skein_Show_Block(BLK_BITS,&ctx->h,ctx->X,blkPtr,w,ks,ts);
995
996
X00 = w[ 0] + ks[ 0]; /* do the first full key injection */
997
X01 = w[ 1] + ks[ 1];
998
X02 = w[ 2] + ks[ 2];
999
X03 = w[ 3] + ks[ 3];
1000
X04 = w[ 4] + ks[ 4];
1001
X05 = w[ 5] + ks[ 5];
1002
X06 = w[ 6] + ks[ 6];
1003
X07 = w[ 7] + ks[ 7];
1004
X08 = w[ 8] + ks[ 8];
1005
X09 = w[ 9] + ks[ 9];
1006
X10 = w[10] + ks[10];
1007
X11 = w[11] + ks[11];
1008
X12 = w[12] + ks[12];
1009
X13 = w[13] + ks[13] + ts[0];
1010
X14 = w[14] + ks[14] + ts[1];
1011
X15 = w[15] + ks[15];
1012
1013
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INITIAL,Xptr);
1014
1015
#define Round1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rNum) \
1016
X##p0 += X##p1; X##p1 = RotL_64(X##p1,ROT##_0); X##p1 ^= X##p0; \
1017
X##p2 += X##p3; X##p3 = RotL_64(X##p3,ROT##_1); X##p3 ^= X##p2; \
1018
X##p4 += X##p5; X##p5 = RotL_64(X##p5,ROT##_2); X##p5 ^= X##p4; \
1019
X##p6 += X##p7; X##p7 = RotL_64(X##p7,ROT##_3); X##p7 ^= X##p6; \
1020
X##p8 += X##p9; X##p9 = RotL_64(X##p9,ROT##_4); X##p9 ^= X##p8; \
1021
X##pA += X##pB; X##pB = RotL_64(X##pB,ROT##_5); X##pB ^= X##pA; \
1022
X##pC += X##pD; X##pD = RotL_64(X##pD,ROT##_6); X##pD ^= X##pC; \
1023
X##pE += X##pF; X##pF = RotL_64(X##pF,ROT##_7); X##pF ^= X##pE; \
1024
1025
#if SKEIN_UNROLL_1024 == 0
1026
#define R1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rn) \
1027
Round1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rn) \
1028
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,rn,Xptr);
1029
1030
#define I1024(R) \
1031
X00 += ks[((R)+ 1) % 17]; /* inject the key schedule value */ \
1032
X01 += ks[((R)+ 2) % 17]; \
1033
X02 += ks[((R)+ 3) % 17]; \
1034
X03 += ks[((R)+ 4) % 17]; \
1035
X04 += ks[((R)+ 5) % 17]; \
1036
X05 += ks[((R)+ 6) % 17]; \
1037
X06 += ks[((R)+ 7) % 17]; \
1038
X07 += ks[((R)+ 8) % 17]; \
1039
X08 += ks[((R)+ 9) % 17]; \
1040
X09 += ks[((R)+10) % 17]; \
1041
X10 += ks[((R)+11) % 17]; \
1042
X11 += ks[((R)+12) % 17]; \
1043
X12 += ks[((R)+13) % 17]; \
1044
X13 += ks[((R)+14) % 17] + ts[((R)+1) % 3]; \
1045
X14 += ks[((R)+15) % 17] + ts[((R)+2) % 3]; \
1046
X15 += ks[((R)+16) % 17] + (R)+1; \
1047
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr);
1048
#else /* looping version */
1049
#define R1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rn) \
1050
Round1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rn) \
1051
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,4*(r-1)+rn,Xptr);
1052
1053
#define I1024(R) \
1054
X00 += ks[r+(R)+ 0]; /* inject the key schedule value */ \
1055
X01 += ks[r+(R)+ 1]; \
1056
X02 += ks[r+(R)+ 2]; \
1057
X03 += ks[r+(R)+ 3]; \
1058
X04 += ks[r+(R)+ 4]; \
1059
X05 += ks[r+(R)+ 5]; \
1060
X06 += ks[r+(R)+ 6]; \
1061
X07 += ks[r+(R)+ 7]; \
1062
X08 += ks[r+(R)+ 8]; \
1063
X09 += ks[r+(R)+ 9]; \
1064
X10 += ks[r+(R)+10]; \
1065
X11 += ks[r+(R)+11]; \
1066
X12 += ks[r+(R)+12]; \
1067
X13 += ks[r+(R)+13] + ts[r+(R)+0]; \
1068
X14 += ks[r+(R)+14] + ts[r+(R)+1]; \
1069
X15 += ks[r+(R)+15] + r+(R) ; \
1070
ks[r + (R)+16] = ks[r+(R)-1]; /* rotate key schedule */ \
1071
ts[r + (R)+ 2] = ts[r+(R)-1]; \
1072
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr);
1073
1074
for (r=1;r <= 2*RCNT;r+=2*SKEIN_UNROLL_1024) /* loop thru it */
1075
#endif
1076
{
1077
#define R1024_8_rounds(R) /* do 8 full rounds */ \
1078
R1024(00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,R1024_0,8*(R) + 1); \
1079
R1024(00,09,02,13,06,11,04,15,10,07,12,03,14,05,08,01,R1024_1,8*(R) + 2); \
1080
R1024(00,07,02,05,04,03,06,01,12,15,14,13,08,11,10,09,R1024_2,8*(R) + 3); \
1081
R1024(00,15,02,11,06,13,04,09,14,01,08,05,10,03,12,07,R1024_3,8*(R) + 4); \
1082
I1024(2*(R)); \
1083
R1024(00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,R1024_4,8*(R) + 5); \
1084
R1024(00,09,02,13,06,11,04,15,10,07,12,03,14,05,08,01,R1024_5,8*(R) + 6); \
1085
R1024(00,07,02,05,04,03,06,01,12,15,14,13,08,11,10,09,R1024_6,8*(R) + 7); \
1086
R1024(00,15,02,11,06,13,04,09,14,01,08,05,10,03,12,07,R1024_7,8*(R) + 8); \
1087
I1024(2*(R)+1);
1088
1089
R1024_8_rounds( 0);
1090
1091
#define R1024_Unroll_R(NN) ((SKEIN_UNROLL_1024 == 0 && SKEIN1024_ROUNDS_TOTAL/8 > (NN)) || (SKEIN_UNROLL_1024 > (NN)))
1092
1093
#if R1024_Unroll_R( 1)
1094
R1024_8_rounds( 1);
1095
#endif
1096
#if R1024_Unroll_R( 2)
1097
R1024_8_rounds( 2);
1098
#endif
1099
#if R1024_Unroll_R( 3)
1100
R1024_8_rounds( 3);
1101
#endif
1102
#if R1024_Unroll_R( 4)
1103
R1024_8_rounds( 4);
1104
#endif
1105
#if R1024_Unroll_R( 5)
1106
R1024_8_rounds( 5);
1107
#endif
1108
#if R1024_Unroll_R( 6)
1109
R1024_8_rounds( 6);
1110
#endif
1111
#if R1024_Unroll_R( 7)
1112
R1024_8_rounds( 7);
1113
#endif
1114
#if R1024_Unroll_R( 8)
1115
R1024_8_rounds( 8);
1116
#endif
1117
#if R1024_Unroll_R( 9)
1118
R1024_8_rounds( 9);
1119
#endif
1120
#if R1024_Unroll_R(10)
1121
R1024_8_rounds(10);
1122
#endif
1123
#if R1024_Unroll_R(11)
1124
R1024_8_rounds(11);
1125
#endif
1126
#if R1024_Unroll_R(12)
1127
R1024_8_rounds(12);
1128
#endif
1129
#if R1024_Unroll_R(13)
1130
R1024_8_rounds(13);
1131
#endif
1132
#if R1024_Unroll_R(14)
1133
R1024_8_rounds(14);
1134
#endif
1135
#if (SKEIN_UNROLL_1024 > 14)
1136
#error "need more unrolling in Skein_1024_Process_Block"
1137
#endif
1138
}
1139
/* do the final "feedforward" xor, update context chaining vars */
1140
1141
ctx->X[ 0] = X00 ^ w[ 0];
1142
ctx->X[ 1] = X01 ^ w[ 1];
1143
ctx->X[ 2] = X02 ^ w[ 2];
1144
ctx->X[ 3] = X03 ^ w[ 3];
1145
ctx->X[ 4] = X04 ^ w[ 4];
1146
ctx->X[ 5] = X05 ^ w[ 5];
1147
ctx->X[ 6] = X06 ^ w[ 6];
1148
ctx->X[ 7] = X07 ^ w[ 7];
1149
ctx->X[ 8] = X08 ^ w[ 8];
1150
ctx->X[ 9] = X09 ^ w[ 9];
1151
ctx->X[10] = X10 ^ w[10];
1152
ctx->X[11] = X11 ^ w[11];
1153
ctx->X[12] = X12 ^ w[12];
1154
ctx->X[13] = X13 ^ w[13];
1155
ctx->X[14] = X14 ^ w[14];
1156
ctx->X[15] = X15 ^ w[15];
1157
1158
Skein_Show_Round(BLK_BITS,&ctx->h,SKEIN_RND_FEED_FWD,ctx->X);
1159
1160
ts[1] &= ~SKEIN_T1_FLAG_FIRST;
1161
blkPtr += SKEIN1024_BLOCK_BYTES;
1162
}
1163
while (--blkCnt);
1164
ctx->h.T[0] = ts[0];
1165
ctx->h.T[1] = ts[1];
1166
}
1167
1168
#if defined(SKEIN_CODE_SIZE) || defined(SKEIN_PERF)
1169
static size_t Skein1024_Process_Block_CodeSize(void)
1170
{
1171
return ((u08b_t *) Skein1024_Process_Block_CodeSize) -
1172
((u08b_t *) Skein1024_Process_Block);
1173
}
1174
static uint_t Skein1024_Unroll_Cnt(void)
1175
{
1176
return SKEIN_UNROLL_1024;
1177
}
1178
#endif
1179
#endif
1180
1181
1182
#if 0
1183
/*****************************************************************/
1184
/* 256-bit Skein */
1185
/*****************************************************************/
1186
1187
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1188
/* init the context for a straight hashing operation */
1189
static int Skein_256_Init(Skein_256_Ctxt_t *ctx, size_t hashBitLen)
1190
{
1191
union
1192
{
1193
u08b_t b[SKEIN_256_STATE_BYTES];
1194
u64b_t w[SKEIN_256_STATE_WORDS];
1195
} cfg; /* config block */
1196
1197
Skein_Assert(hashBitLen > 0,SKEIN_BAD_HASHLEN);
1198
ctx->h.hashBitLen = hashBitLen; /* output hash bit count */
1199
1200
switch (hashBitLen)
1201
{ /* use pre-computed values, where available */
1202
#ifndef SKEIN_NO_PRECOMP
1203
case 256: memcpy(ctx->X,SKEIN_256_IV_256,sizeof(ctx->X)); break;
1204
case 224: memcpy(ctx->X,SKEIN_256_IV_224,sizeof(ctx->X)); break;
1205
case 160: memcpy(ctx->X,SKEIN_256_IV_160,sizeof(ctx->X)); break;
1206
case 128: memcpy(ctx->X,SKEIN_256_IV_128,sizeof(ctx->X)); break;
1207
#endif
1208
default:
1209
/* here if there is no precomputed IV value available */
1210
/* build/process the config block, type == CONFIG (could be precomputed) */
1211
Skein_Start_New_Type(ctx,CFG_FINAL); /* set tweaks: T0=0; T1=CFG | FINAL */
1212
1213
cfg.w[0] = Skein_Swap64(SKEIN_SCHEMA_VER); /* set the schema, version */
1214
cfg.w[1] = Skein_Swap64(hashBitLen); /* hash result length in bits */
1215
cfg.w[2] = Skein_Swap64(SKEIN_CFG_TREE_INFO_SEQUENTIAL);
1216
memset(&cfg.w[3],0,sizeof(cfg) - 3*sizeof(cfg.w[0])); /* zero pad config block */
1217
1218
/* compute the initial chaining values from config block */
1219
memset(ctx->X,0,sizeof(ctx->X)); /* zero the chaining variables */
1220
Skein_256_Process_Block(ctx,cfg.b,1,SKEIN_CFG_STR_LEN);
1221
break;
1222
}
1223
/* The chaining vars ctx->X are now initialized for the given hashBitLen. */
1224
/* Set up to process the data message portion of the hash (default) */
1225
Skein_Start_New_Type(ctx,MSG); /* T0=0, T1= MSG type */
1226
1227
return SKEIN_SUCCESS;
1228
}
1229
1230
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1231
/* init the context for a MAC and/or tree hash operation */
1232
/* [identical to Skein_256_Init() when keyBytes == 0 && treeInfo == SKEIN_CFG_TREE_INFO_SEQUENTIAL] */
1233
static int Skein_256_InitExt(Skein_256_Ctxt_t *ctx,size_t hashBitLen,u64b_t treeInfo, const u08b_t *key, size_t keyBytes)
1234
{
1235
union
1236
{
1237
u08b_t b[SKEIN_256_STATE_BYTES];
1238
u64b_t w[SKEIN_256_STATE_WORDS];
1239
} cfg; /* config block */
1240
1241
Skein_Assert(hashBitLen > 0,SKEIN_BAD_HASHLEN);
1242
Skein_Assert(keyBytes == 0 || key != NULL,SKEIN_FAIL);
1243
1244
/* compute the initial chaining values ctx->X[], based on key */
1245
if (keyBytes == 0) /* is there a key? */
1246
{
1247
memset(ctx->X,0,sizeof(ctx->X)); /* no key: use all zeroes as key for config block */
1248
}
1249
else /* here to pre-process a key */
1250
{
1251
Skein_assert(sizeof(cfg.b) >= sizeof(ctx->X));
1252
/* do a mini-Init right here */
1253
ctx->h.hashBitLen=8*sizeof(ctx->X); /* set output hash bit count = state size */
1254
Skein_Start_New_Type(ctx,KEY); /* set tweaks: T0 = 0; T1 = KEY type */
1255
memset(ctx->X,0,sizeof(ctx->X)); /* zero the initial chaining variables */
1256
Skein_256_Update(ctx,key,keyBytes); /* hash the key */
1257
Skein_256_Final_Pad(ctx,cfg.b); /* put result into cfg.b[] */
1258
memcpy(ctx->X,cfg.b,sizeof(cfg.b)); /* copy over into ctx->X[] */
1259
#if SKEIN_NEED_SWAP
1260
{
1261
uint_t i;
1262
for (i=0;i<SKEIN_256_STATE_WORDS;i++) /* convert key bytes to context words */
1263
ctx->X[i] = Skein_Swap64(ctx->X[i]);
1264
}
1265
#endif
1266
}
1267
/* build/process the config block, type == CONFIG (could be precomputed for each key) */
1268
ctx->h.hashBitLen = hashBitLen; /* output hash bit count */
1269
Skein_Start_New_Type(ctx,CFG_FINAL);
1270
1271
memset(&cfg.w,0,sizeof(cfg.w)); /* pre-pad cfg.w[] with zeroes */
1272
cfg.w[0] = Skein_Swap64(SKEIN_SCHEMA_VER);
1273
cfg.w[1] = Skein_Swap64(hashBitLen); /* hash result length in bits */
1274
cfg.w[2] = Skein_Swap64(treeInfo); /* tree hash config info (or SKEIN_CFG_TREE_INFO_SEQUENTIAL) */
1275
1276
Skein_Show_Key(256,&ctx->h,key,keyBytes);
1277
1278
/* compute the initial chaining values from config block */
1279
Skein_256_Process_Block(ctx,cfg.b,1,SKEIN_CFG_STR_LEN);
1280
1281
/* The chaining vars ctx->X are now initialized */
1282
/* Set up to process the data message portion of the hash (default) */
1283
ctx->h.bCnt = 0; /* buffer b[] starts out empty */
1284
Skein_Start_New_Type(ctx,MSG);
1285
1286
return SKEIN_SUCCESS;
1287
}
1288
#endif
1289
1290
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1291
/* process the input bytes */
1292
static int Skein_256_Update(Skein_256_Ctxt_t *ctx, const u08b_t *msg, size_t msgByteCnt)
1293
{
1294
size_t n;
1295
1296
Skein_Assert(ctx->h.bCnt <= SKEIN_256_BLOCK_BYTES,SKEIN_FAIL); /* catch uninitialized context */
1297
1298
/* process full blocks, if any */
1299
if (msgByteCnt + ctx->h.bCnt > SKEIN_256_BLOCK_BYTES)
1300
{
1301
if (ctx->h.bCnt) /* finish up any buffered message data */
1302
{
1303
n = SKEIN_256_BLOCK_BYTES - ctx->h.bCnt; /* # bytes free in buffer b[] */
1304
if (n)
1305
{
1306
Skein_assert(n < msgByteCnt); /* check on our logic here */
1307
memcpy(&ctx->b[ctx->h.bCnt],msg,n);
1308
msgByteCnt -= n;
1309
msg += n;
1310
ctx->h.bCnt += n;
1311
}
1312
Skein_assert(ctx->h.bCnt == SKEIN_256_BLOCK_BYTES);
1313
Skein_256_Process_Block(ctx,ctx->b,1,SKEIN_256_BLOCK_BYTES);
1314
ctx->h.bCnt = 0;
1315
}
1316
/* now process any remaining full blocks, directly from input message data */
1317
if (msgByteCnt > SKEIN_256_BLOCK_BYTES)
1318
{
1319
n = (msgByteCnt-1) / SKEIN_256_BLOCK_BYTES; /* number of full blocks to process */
1320
Skein_256_Process_Block(ctx,msg,n,SKEIN_256_BLOCK_BYTES);
1321
msgByteCnt -= n * SKEIN_256_BLOCK_BYTES;
1322
msg += n * SKEIN_256_BLOCK_BYTES;
1323
}
1324
Skein_assert(ctx->h.bCnt == 0);
1325
}
1326
1327
/* copy any remaining source message data bytes into b[] */
1328
if (msgByteCnt)
1329
{
1330
Skein_assert(msgByteCnt + ctx->h.bCnt <= SKEIN_256_BLOCK_BYTES);
1331
memcpy(&ctx->b[ctx->h.bCnt],msg,msgByteCnt);
1332
ctx->h.bCnt += msgByteCnt;
1333
}
1334
1335
return SKEIN_SUCCESS;
1336
}
1337
1338
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1339
/* finalize the hash computation and output the result */
1340
static int Skein_256_Final(Skein_256_Ctxt_t *ctx, u08b_t *hashVal)
1341
{
1342
size_t i,n,byteCnt;
1343
u64b_t X[SKEIN_256_STATE_WORDS];
1344
Skein_Assert(ctx->h.bCnt <= SKEIN_256_BLOCK_BYTES,SKEIN_FAIL); /* catch uninitialized context */
1345
1346
ctx->h.T[1] |= SKEIN_T1_FLAG_FINAL; /* tag as the final block */
1347
if (ctx->h.bCnt < SKEIN_256_BLOCK_BYTES) /* zero pad b[] if necessary */
1348
memset(&ctx->b[ctx->h.bCnt],0,SKEIN_256_BLOCK_BYTES - ctx->h.bCnt);
1349
1350
Skein_256_Process_Block(ctx,ctx->b,1,ctx->h.bCnt); /* process the final block */
1351
1352
/* now output the result */
1353
byteCnt = (ctx->h.hashBitLen + 7) >> 3; /* total number of output bytes */
1354
1355
/* run Threefish in "counter mode" to generate output */
1356
memset(ctx->b,0,sizeof(ctx->b)); /* zero out b[], so it can hold the counter */
1357
memcpy(X,ctx->X,sizeof(X)); /* keep a local copy of counter mode "key" */
1358
for (i=0;i < byteCnt;i += SKEIN_256_BLOCK_BYTES)
1359
{
1360
((u64b_t *)ctx->b)[0]= Skein_Swap64((u64b_t) i); /* build the counter block */
1361
Skein_Start_New_Type(ctx,OUT_FINAL);
1362
Skein_256_Process_Block(ctx,ctx->b,1,sizeof(u64b_t)); /* run "counter mode" */
1363
n = byteCnt - i; /* number of output bytes left to go */
1364
if (n >= SKEIN_256_BLOCK_BYTES)
1365
n = SKEIN_256_BLOCK_BYTES;
1366
Skein_Put64_LSB_First(hashVal+i,ctx->X,n); /* "output" the ctr mode bytes */
1367
Skein_Show_Final(256,&ctx->h,n,hashVal+i*SKEIN_256_BLOCK_BYTES);
1368
memcpy(ctx->X,X,sizeof(X)); /* restore the counter mode key for next time */
1369
}
1370
return SKEIN_SUCCESS;
1371
}
1372
1373
#if defined(SKEIN_CODE_SIZE) || defined(SKEIN_PERF)
1374
static size_t Skein_256_API_CodeSize(void)
1375
{
1376
return ((u08b_t *) Skein_256_API_CodeSize) -
1377
((u08b_t *) Skein_256_Init);
1378
}
1379
#endif
1380
1381
/*****************************************************************/
1382
/* 512-bit Skein */
1383
/*****************************************************************/
1384
1385
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1386
/* init the context for a straight hashing operation */
1387
static int Skein_512_Init(Skein_512_Ctxt_t *ctx, size_t hashBitLen)
1388
{
1389
union
1390
{
1391
u08b_t b[SKEIN_512_STATE_BYTES];
1392
u64b_t w[SKEIN_512_STATE_WORDS];
1393
} cfg; /* config block */
1394
1395
Skein_Assert(hashBitLen > 0,SKEIN_BAD_HASHLEN);
1396
ctx->h.hashBitLen = hashBitLen; /* output hash bit count */
1397
1398
switch (hashBitLen)
1399
{ /* use pre-computed values, where available */
1400
#ifndef SKEIN_NO_PRECOMP
1401
case 512: memcpy(ctx->X,SKEIN_512_IV_512,sizeof(ctx->X)); break;
1402
case 384: memcpy(ctx->X,SKEIN_512_IV_384,sizeof(ctx->X)); break;
1403
case 256: memcpy(ctx->X,SKEIN_512_IV_256,sizeof(ctx->X)); break;
1404
case 224: memcpy(ctx->X,SKEIN_512_IV_224,sizeof(ctx->X)); break;
1405
#endif
1406
default:
1407
/* here if there is no precomputed IV value available */
1408
/* build/process the config block, type == CONFIG (could be precomputed) */
1409
Skein_Start_New_Type(ctx,CFG_FINAL); /* set tweaks: T0=0; T1=CFG | FINAL */
1410
1411
cfg.w[0] = Skein_Swap64(SKEIN_SCHEMA_VER); /* set the schema, version */
1412
cfg.w[1] = Skein_Swap64(hashBitLen); /* hash result length in bits */
1413
cfg.w[2] = Skein_Swap64(SKEIN_CFG_TREE_INFO_SEQUENTIAL);
1414
memset(&cfg.w[3],0,sizeof(cfg) - 3*sizeof(cfg.w[0])); /* zero pad config block */
1415
1416
/* compute the initial chaining values from config block */
1417
memset(ctx->X,0,sizeof(ctx->X)); /* zero the chaining variables */
1418
Skein_512_Process_Block(ctx,cfg.b,1,SKEIN_CFG_STR_LEN);
1419
break;
1420
}
1421
1422
/* The chaining vars ctx->X are now initialized for the given hashBitLen. */
1423
/* Set up to process the data message portion of the hash (default) */
1424
Skein_Start_New_Type(ctx,MSG); /* T0=0, T1= MSG type */
1425
1426
return SKEIN_SUCCESS;
1427
}
1428
1429
#if 0
1430
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1431
/* init the context for a MAC and/or tree hash operation */
1432
/* [identical to Skein_512_Init() when keyBytes == 0 && treeInfo == SKEIN_CFG_TREE_INFO_SEQUENTIAL] */
1433
static int Skein_512_InitExt(Skein_512_Ctxt_t *ctx,size_t hashBitLen,u64b_t treeInfo, const u08b_t *key, size_t keyBytes)
1434
{
1435
union
1436
{
1437
u08b_t b[SKEIN_512_STATE_BYTES];
1438
u64b_t w[SKEIN_512_STATE_WORDS];
1439
} cfg; /* config block */
1440
1441
Skein_Assert(hashBitLen > 0,SKEIN_BAD_HASHLEN);
1442
Skein_Assert(keyBytes == 0 || key != NULL,SKEIN_FAIL);
1443
1444
/* compute the initial chaining values ctx->X[], based on key */
1445
if (keyBytes == 0) /* is there a key? */
1446
{
1447
memset(ctx->X,0,sizeof(ctx->X)); /* no key: use all zeroes as key for config block */
1448
}
1449
else /* here to pre-process a key */
1450
{
1451
Skein_assert(sizeof(cfg.b) >= sizeof(ctx->X));
1452
/* do a mini-Init right here */
1453
ctx->h.hashBitLen=8*sizeof(ctx->X); /* set output hash bit count = state size */
1454
Skein_Start_New_Type(ctx,KEY); /* set tweaks: T0 = 0; T1 = KEY type */
1455
memset(ctx->X,0,sizeof(ctx->X)); /* zero the initial chaining variables */
1456
Skein_512_Update(ctx,key,keyBytes); /* hash the key */
1457
Skein_512_Final_Pad(ctx,cfg.b); /* put result into cfg.b[] */
1458
memcpy(ctx->X,cfg.b,sizeof(cfg.b)); /* copy over into ctx->X[] */
1459
#if SKEIN_NEED_SWAP
1460
{
1461
uint_t i;
1462
for (i=0;i<SKEIN_512_STATE_WORDS;i++) /* convert key bytes to context words */
1463
ctx->X[i] = Skein_Swap64(ctx->X[i]);
1464
}
1465
#endif
1466
}
1467
/* build/process the config block, type == CONFIG (could be precomputed for each key) */
1468
ctx->h.hashBitLen = hashBitLen; /* output hash bit count */
1469
Skein_Start_New_Type(ctx,CFG_FINAL);
1470
1471
memset(&cfg.w,0,sizeof(cfg.w)); /* pre-pad cfg.w[] with zeroes */
1472
cfg.w[0] = Skein_Swap64(SKEIN_SCHEMA_VER);
1473
cfg.w[1] = Skein_Swap64(hashBitLen); /* hash result length in bits */
1474
cfg.w[2] = Skein_Swap64(treeInfo); /* tree hash config info (or SKEIN_CFG_TREE_INFO_SEQUENTIAL) */
1475
1476
Skein_Show_Key(512,&ctx->h,key,keyBytes);
1477
1478
/* compute the initial chaining values from config block */
1479
Skein_512_Process_Block(ctx,cfg.b,1,SKEIN_CFG_STR_LEN);
1480
1481
/* The chaining vars ctx->X are now initialized */
1482
/* Set up to process the data message portion of the hash (default) */
1483
ctx->h.bCnt = 0; /* buffer b[] starts out empty */
1484
Skein_Start_New_Type(ctx,MSG);
1485
1486
return SKEIN_SUCCESS;
1487
}
1488
#endif
1489
1490
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1491
/* process the input bytes */
1492
static int Skein_512_Update(Skein_512_Ctxt_t *ctx, const u08b_t *msg, size_t msgByteCnt)
1493
{
1494
size_t n;
1495
1496
Skein_Assert(ctx->h.bCnt <= SKEIN_512_BLOCK_BYTES,SKEIN_FAIL); /* catch uninitialized context */
1497
1498
/* process full blocks, if any */
1499
if (msgByteCnt + ctx->h.bCnt > SKEIN_512_BLOCK_BYTES)
1500
{
1501
if (ctx->h.bCnt) /* finish up any buffered message data */
1502
{
1503
n = SKEIN_512_BLOCK_BYTES - ctx->h.bCnt; /* # bytes free in buffer b[] */
1504
if (n)
1505
{
1506
Skein_assert(n < msgByteCnt); /* check on our logic here */
1507
memcpy(&ctx->b[ctx->h.bCnt],msg,n);
1508
msgByteCnt -= n;
1509
msg += n;
1510
ctx->h.bCnt += n;
1511
}
1512
Skein_assert(ctx->h.bCnt == SKEIN_512_BLOCK_BYTES);
1513
Skein_512_Process_Block(ctx,ctx->b,1,SKEIN_512_BLOCK_BYTES);
1514
ctx->h.bCnt = 0;
1515
}
1516
/* now process any remaining full blocks, directly from input message data */
1517
if (msgByteCnt > SKEIN_512_BLOCK_BYTES)
1518
{
1519
n = (msgByteCnt-1) / SKEIN_512_BLOCK_BYTES; /* number of full blocks to process */
1520
Skein_512_Process_Block(ctx,msg,n,SKEIN_512_BLOCK_BYTES);
1521
msgByteCnt -= n * SKEIN_512_BLOCK_BYTES;
1522
msg += n * SKEIN_512_BLOCK_BYTES;
1523
}
1524
Skein_assert(ctx->h.bCnt == 0);
1525
}
1526
1527
/* copy any remaining source message data bytes into b[] */
1528
if (msgByteCnt)
1529
{
1530
Skein_assert(msgByteCnt + ctx->h.bCnt <= SKEIN_512_BLOCK_BYTES);
1531
memcpy(&ctx->b[ctx->h.bCnt],msg,msgByteCnt);
1532
ctx->h.bCnt += msgByteCnt;
1533
}
1534
1535
return SKEIN_SUCCESS;
1536
}
1537
1538
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1539
/* finalize the hash computation and output the result */
1540
static int Skein_512_Final(Skein_512_Ctxt_t *ctx, u08b_t *hashVal)
1541
{
1542
size_t i,n,byteCnt;
1543
u64b_t X[SKEIN_512_STATE_WORDS];
1544
Skein_Assert(ctx->h.bCnt <= SKEIN_512_BLOCK_BYTES,SKEIN_FAIL); /* catch uninitialized context */
1545
1546
ctx->h.T[1] |= SKEIN_T1_FLAG_FINAL; /* tag as the final block */
1547
if (ctx->h.bCnt < SKEIN_512_BLOCK_BYTES) /* zero pad b[] if necessary */
1548
memset(&ctx->b[ctx->h.bCnt],0,SKEIN_512_BLOCK_BYTES - ctx->h.bCnt);
1549
1550
Skein_512_Process_Block(ctx,ctx->b,1,ctx->h.bCnt); /* process the final block */
1551
1552
/* now output the result */
1553
byteCnt = (ctx->h.hashBitLen + 7) >> 3; /* total number of output bytes */
1554
1555
/* run Threefish in "counter mode" to generate output */
1556
memset(ctx->b,0,sizeof(ctx->b)); /* zero out b[], so it can hold the counter */
1557
memcpy(X,ctx->X,sizeof(X)); /* keep a local copy of counter mode "key" */
1558
for (i=0;i*SKEIN_512_BLOCK_BYTES < byteCnt;i++)
1559
{
1560
((u64b_t *)ctx->b)[0]= Skein_Swap64((u64b_t) i); /* build the counter block */
1561
Skein_Start_New_Type(ctx,OUT_FINAL);
1562
Skein_512_Process_Block(ctx,ctx->b,1,sizeof(u64b_t)); /* run "counter mode" */
1563
n = byteCnt - i*SKEIN_512_BLOCK_BYTES; /* number of output bytes left to go */
1564
if (n >= SKEIN_512_BLOCK_BYTES)
1565
n = SKEIN_512_BLOCK_BYTES;
1566
Skein_Put64_LSB_First(hashVal+i*SKEIN_512_BLOCK_BYTES,ctx->X,n); /* "output" the ctr mode bytes */
1567
Skein_Show_Final(512,&ctx->h,n,hashVal+i*SKEIN_512_BLOCK_BYTES);
1568
memcpy(ctx->X,X,sizeof(X)); /* restore the counter mode key for next time */
1569
}
1570
return SKEIN_SUCCESS;
1571
}
1572
1573
#if defined(SKEIN_CODE_SIZE) || defined(SKEIN_PERF)
1574
static size_t Skein_512_API_CodeSize(void)
1575
{
1576
return ((u08b_t *) Skein_512_API_CodeSize) -
1577
((u08b_t *) Skein_512_Init);
1578
}
1579
#endif
1580
1581
/*****************************************************************/
1582
/* 1024-bit Skein */
1583
/*****************************************************************/
1584
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1585
/* init the context for a straight hashing operation */
1586
static int Skein1024_Init(Skein1024_Ctxt_t *ctx, size_t hashBitLen)
1587
{
1588
union
1589
{
1590
u08b_t b[SKEIN1024_STATE_BYTES];
1591
u64b_t w[SKEIN1024_STATE_WORDS];
1592
} cfg; /* config block */
1593
1594
Skein_Assert(hashBitLen > 0,SKEIN_BAD_HASHLEN);
1595
ctx->h.hashBitLen = hashBitLen; /* output hash bit count */
1596
1597
switch (hashBitLen)
1598
{ /* use pre-computed values, where available */
1599
#ifndef SKEIN_NO_PRECOMP
1600
case 512: memcpy(ctx->X,SKEIN1024_IV_512 ,sizeof(ctx->X)); break;
1601
case 384: memcpy(ctx->X,SKEIN1024_IV_384 ,sizeof(ctx->X)); break;
1602
case 1024: memcpy(ctx->X,SKEIN1024_IV_1024,sizeof(ctx->X)); break;
1603
#endif
1604
default:
1605
/* here if there is no precomputed IV value available */
1606
/* build/process the config block, type == CONFIG (could be precomputed) */
1607
Skein_Start_New_Type(ctx,CFG_FINAL); /* set tweaks: T0=0; T1=CFG | FINAL */
1608
1609
cfg.w[0] = Skein_Swap64(SKEIN_SCHEMA_VER); /* set the schema, version */
1610
cfg.w[1] = Skein_Swap64(hashBitLen); /* hash result length in bits */
1611
cfg.w[2] = Skein_Swap64(SKEIN_CFG_TREE_INFO_SEQUENTIAL);
1612
memset(&cfg.w[3],0,sizeof(cfg) - 3*sizeof(cfg.w[0])); /* zero pad config block */
1613
1614
/* compute the initial chaining values from config block */
1615
memset(ctx->X,0,sizeof(ctx->X)); /* zero the chaining variables */
1616
Skein1024_Process_Block(ctx,cfg.b,1,SKEIN_CFG_STR_LEN);
1617
break;
1618
}
1619
1620
/* The chaining vars ctx->X are now initialized for the given hashBitLen. */
1621
/* Set up to process the data message portion of the hash (default) */
1622
Skein_Start_New_Type(ctx,MSG); /* T0=0, T1= MSG type */
1623
1624
return SKEIN_SUCCESS;
1625
}
1626
1627
#if 0
1628
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1629
/* init the context for a MAC and/or tree hash operation */
1630
/* [identical to Skein1024_Init() when keyBytes == 0 && treeInfo == SKEIN_CFG_TREE_INFO_SEQUENTIAL] */
1631
static int Skein1024_InitExt(Skein1024_Ctxt_t *ctx,size_t hashBitLen,u64b_t treeInfo, const u08b_t *key, size_t keyBytes)
1632
{
1633
union
1634
{
1635
u08b_t b[SKEIN1024_STATE_BYTES];
1636
u64b_t w[SKEIN1024_STATE_WORDS];
1637
} cfg; /* config block */
1638
1639
Skein_Assert(hashBitLen > 0,SKEIN_BAD_HASHLEN);
1640
Skein_Assert(keyBytes == 0 || key != NULL,SKEIN_FAIL);
1641
1642
/* compute the initial chaining values ctx->X[], based on key */
1643
if (keyBytes == 0) /* is there a key? */
1644
{
1645
memset(ctx->X,0,sizeof(ctx->X)); /* no key: use all zeroes as key for config block */
1646
}
1647
else /* here to pre-process a key */
1648
{
1649
Skein_assert(sizeof(cfg.b) >= sizeof(ctx->X));
1650
/* do a mini-Init right here */
1651
ctx->h.hashBitLen=8*sizeof(ctx->X); /* set output hash bit count = state size */
1652
Skein_Start_New_Type(ctx,KEY); /* set tweaks: T0 = 0; T1 = KEY type */
1653
memset(ctx->X,0,sizeof(ctx->X)); /* zero the initial chaining variables */
1654
Skein1024_Update(ctx,key,keyBytes); /* hash the key */
1655
Skein1024_Final_Pad(ctx,cfg.b); /* put result into cfg.b[] */
1656
memcpy(ctx->X,cfg.b,sizeof(cfg.b)); /* copy over into ctx->X[] */
1657
#if SKEIN_NEED_SWAP
1658
{
1659
uint_t i;
1660
for (i=0;i<SKEIN1024_STATE_WORDS;i++) /* convert key bytes to context words */
1661
ctx->X[i] = Skein_Swap64(ctx->X[i]);
1662
}
1663
#endif
1664
}
1665
/* build/process the config block, type == CONFIG (could be precomputed for each key) */
1666
ctx->h.hashBitLen = hashBitLen; /* output hash bit count */
1667
Skein_Start_New_Type(ctx,CFG_FINAL);
1668
1669
memset(&cfg.w,0,sizeof(cfg.w)); /* pre-pad cfg.w[] with zeroes */
1670
cfg.w[0] = Skein_Swap64(SKEIN_SCHEMA_VER);
1671
cfg.w[1] = Skein_Swap64(hashBitLen); /* hash result length in bits */
1672
cfg.w[2] = Skein_Swap64(treeInfo); /* tree hash config info (or SKEIN_CFG_TREE_INFO_SEQUENTIAL) */
1673
1674
Skein_Show_Key(1024,&ctx->h,key,keyBytes);
1675
1676
/* compute the initial chaining values from config block */
1677
Skein1024_Process_Block(ctx,cfg.b,1,SKEIN_CFG_STR_LEN);
1678
1679
/* The chaining vars ctx->X are now initialized */
1680
/* Set up to process the data message portion of the hash (default) */
1681
ctx->h.bCnt = 0; /* buffer b[] starts out empty */
1682
Skein_Start_New_Type(ctx,MSG);
1683
1684
return SKEIN_SUCCESS;
1685
}
1686
#endif
1687
1688
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1689
/* process the input bytes */
1690
static int Skein1024_Update(Skein1024_Ctxt_t *ctx, const u08b_t *msg, size_t msgByteCnt)
1691
{
1692
size_t n;
1693
1694
Skein_Assert(ctx->h.bCnt <= SKEIN1024_BLOCK_BYTES,SKEIN_FAIL); /* catch uninitialized context */
1695
1696
/* process full blocks, if any */
1697
if (msgByteCnt + ctx->h.bCnt > SKEIN1024_BLOCK_BYTES)
1698
{
1699
if (ctx->h.bCnt) /* finish up any buffered message data */
1700
{
1701
n = SKEIN1024_BLOCK_BYTES - ctx->h.bCnt; /* # bytes free in buffer b[] */
1702
if (n)
1703
{
1704
Skein_assert(n < msgByteCnt); /* check on our logic here */
1705
memcpy(&ctx->b[ctx->h.bCnt],msg,n);
1706
msgByteCnt -= n;
1707
msg += n;
1708
ctx->h.bCnt += n;
1709
}
1710
Skein_assert(ctx->h.bCnt == SKEIN1024_BLOCK_BYTES);
1711
Skein1024_Process_Block(ctx,ctx->b,1,SKEIN1024_BLOCK_BYTES);
1712
ctx->h.bCnt = 0;
1713
}
1714
/* now process any remaining full blocks, directly from input message data */
1715
if (msgByteCnt > SKEIN1024_BLOCK_BYTES)
1716
{
1717
n = (msgByteCnt-1) / SKEIN1024_BLOCK_BYTES; /* number of full blocks to process */
1718
Skein1024_Process_Block(ctx,msg,n,SKEIN1024_BLOCK_BYTES);
1719
msgByteCnt -= n * SKEIN1024_BLOCK_BYTES;
1720
msg += n * SKEIN1024_BLOCK_BYTES;
1721
}
1722
Skein_assert(ctx->h.bCnt == 0);
1723
}
1724
1725
/* copy any remaining source message data bytes into b[] */
1726
if (msgByteCnt)
1727
{
1728
Skein_assert(msgByteCnt + ctx->h.bCnt <= SKEIN1024_BLOCK_BYTES);
1729
memcpy(&ctx->b[ctx->h.bCnt],msg,msgByteCnt);
1730
ctx->h.bCnt += msgByteCnt;
1731
}
1732
1733
return SKEIN_SUCCESS;
1734
}
1735
1736
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1737
/* finalize the hash computation and output the result */
1738
static int Skein1024_Final(Skein1024_Ctxt_t *ctx, u08b_t *hashVal)
1739
{
1740
size_t i,n,byteCnt;
1741
u64b_t X[SKEIN1024_STATE_WORDS];
1742
Skein_Assert(ctx->h.bCnt <= SKEIN1024_BLOCK_BYTES,SKEIN_FAIL); /* catch uninitialized context */
1743
1744
ctx->h.T[1] |= SKEIN_T1_FLAG_FINAL; /* tag as the final block */
1745
if (ctx->h.bCnt < SKEIN1024_BLOCK_BYTES) /* zero pad b[] if necessary */
1746
memset(&ctx->b[ctx->h.bCnt],0,SKEIN1024_BLOCK_BYTES - ctx->h.bCnt);
1747
1748
Skein1024_Process_Block(ctx,ctx->b,1,ctx->h.bCnt); /* process the final block */
1749
1750
/* now output the result */
1751
byteCnt = (ctx->h.hashBitLen + 7) >> 3; /* total number of output bytes */
1752
1753
/* run Threefish in "counter mode" to generate output */
1754
memset(ctx->b,0,sizeof(ctx->b)); /* zero out b[], so it can hold the counter */
1755
memcpy(X,ctx->X,sizeof(X)); /* keep a local copy of counter mode "key" */
1756
for (i=0;i*SKEIN1024_BLOCK_BYTES < byteCnt;i++)
1757
{
1758
((u64b_t *)ctx->b)[0]= Skein_Swap64((u64b_t) i); /* build the counter block */
1759
Skein_Start_New_Type(ctx,OUT_FINAL);
1760
Skein1024_Process_Block(ctx,ctx->b,1,sizeof(u64b_t)); /* run "counter mode" */
1761
n = byteCnt - i*SKEIN1024_BLOCK_BYTES; /* number of output bytes left to go */
1762
if (n >= SKEIN1024_BLOCK_BYTES)
1763
n = SKEIN1024_BLOCK_BYTES;
1764
Skein_Put64_LSB_First(hashVal+i*SKEIN1024_BLOCK_BYTES,ctx->X,n); /* "output" the ctr mode bytes */
1765
Skein_Show_Final(1024,&ctx->h,n,hashVal+i*SKEIN1024_BLOCK_BYTES);
1766
memcpy(ctx->X,X,sizeof(X)); /* restore the counter mode key for next time */
1767
}
1768
return SKEIN_SUCCESS;
1769
}
1770
1771
#if defined(SKEIN_CODE_SIZE) || defined(SKEIN_PERF)
1772
static size_t Skein1024_API_CodeSize(void)
1773
{
1774
return ((u08b_t *) Skein1024_API_CodeSize) -
1775
((u08b_t *) Skein1024_Init);
1776
}
1777
#endif
1778
1779
/**************** Functions to support MAC/tree hashing ***************/
1780
/* (this code is identical for Optimized and Reference versions) */
1781
1782
#if 0
1783
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1784
/* finalize the hash computation and output the block, no OUTPUT stage */
1785
static int Skein_256_Final_Pad(Skein_256_Ctxt_t *ctx, u08b_t *hashVal)
1786
{
1787
Skein_Assert(ctx->h.bCnt <= SKEIN_256_BLOCK_BYTES,SKEIN_FAIL); /* catch uninitialized context */
1788
1789
ctx->h.T[1] |= SKEIN_T1_FLAG_FINAL; /* tag as the final block */
1790
if (ctx->h.bCnt < SKEIN_256_BLOCK_BYTES) /* zero pad b[] if necessary */
1791
memset(&ctx->b[ctx->h.bCnt],0,SKEIN_256_BLOCK_BYTES - ctx->h.bCnt);
1792
Skein_256_Process_Block(ctx,ctx->b,1,ctx->h.bCnt); /* process the final block */
1793
1794
Skein_Put64_LSB_First(hashVal,ctx->X,SKEIN_256_BLOCK_BYTES); /* "output" the state bytes */
1795
1796
return SKEIN_SUCCESS;
1797
}
1798
1799
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1800
/* finalize the hash computation and output the block, no OUTPUT stage */
1801
static int Skein_512_Final_Pad(Skein_512_Ctxt_t *ctx, u08b_t *hashVal)
1802
{
1803
Skein_Assert(ctx->h.bCnt <= SKEIN_512_BLOCK_BYTES,SKEIN_FAIL); /* catch uninitialized context */
1804
1805
ctx->h.T[1] |= SKEIN_T1_FLAG_FINAL; /* tag as the final block */
1806
if (ctx->h.bCnt < SKEIN_512_BLOCK_BYTES) /* zero pad b[] if necessary */
1807
memset(&ctx->b[ctx->h.bCnt],0,SKEIN_512_BLOCK_BYTES - ctx->h.bCnt);
1808
Skein_512_Process_Block(ctx,ctx->b,1,ctx->h.bCnt); /* process the final block */
1809
1810
Skein_Put64_LSB_First(hashVal,ctx->X,SKEIN_512_BLOCK_BYTES); /* "output" the state bytes */
1811
1812
return SKEIN_SUCCESS;
1813
}
1814
1815
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1816
/* finalize the hash computation and output the block, no OUTPUT stage */
1817
static int Skein1024_Final_Pad(Skein1024_Ctxt_t *ctx, u08b_t *hashVal)
1818
{
1819
Skein_Assert(ctx->h.bCnt <= SKEIN1024_BLOCK_BYTES,SKEIN_FAIL); /* catch uninitialized context */
1820
1821
ctx->h.T[1] |= SKEIN_T1_FLAG_FINAL; /* tag as the final block */
1822
if (ctx->h.bCnt < SKEIN1024_BLOCK_BYTES) /* zero pad b[] if necessary */
1823
memset(&ctx->b[ctx->h.bCnt],0,SKEIN1024_BLOCK_BYTES - ctx->h.bCnt);
1824
Skein1024_Process_Block(ctx,ctx->b,1,ctx->h.bCnt); /* process the final block */
1825
1826
Skein_Put64_LSB_First(hashVal,ctx->X,SKEIN1024_BLOCK_BYTES); /* "output" the state bytes */
1827
1828
return SKEIN_SUCCESS;
1829
}
1830
1831
1832
#if SKEIN_TREE_HASH
1833
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1834
/* just do the OUTPUT stage */
1835
static int Skein_256_Output(Skein_256_Ctxt_t *ctx, u08b_t *hashVal)
1836
{
1837
size_t i,n,byteCnt;
1838
u64b_t X[SKEIN_256_STATE_WORDS];
1839
Skein_Assert(ctx->h.bCnt <= SKEIN_256_BLOCK_BYTES,SKEIN_FAIL); /* catch uninitialized context */
1840
1841
/* now output the result */
1842
byteCnt = (ctx->h.hashBitLen + 7) >> 3; /* total number of output bytes */
1843
1844
/* run Threefish in "counter mode" to generate output */
1845
memset(ctx->b,0,sizeof(ctx->b)); /* zero out b[], so it can hold the counter */
1846
memcpy(X,ctx->X,sizeof(X)); /* keep a local copy of counter mode "key" */
1847
for (i=0;i*SKEIN_256_BLOCK_BYTES < byteCnt;i++)
1848
{
1849
((u64b_t *)ctx->b)[0]= Skein_Swap64((u64b_t) i); /* build the counter block */
1850
Skein_Start_New_Type(ctx,OUT_FINAL);
1851
Skein_256_Process_Block(ctx,ctx->b,1,sizeof(u64b_t)); /* run "counter mode" */
1852
n = byteCnt - i*SKEIN_256_BLOCK_BYTES; /* number of output bytes left to go */
1853
if (n >= SKEIN_256_BLOCK_BYTES)
1854
n = SKEIN_256_BLOCK_BYTES;
1855
Skein_Put64_LSB_First(hashVal+i*SKEIN_256_BLOCK_BYTES,ctx->X,n); /* "output" the ctr mode bytes */
1856
Skein_Show_Final(256,&ctx->h,n,hashVal+i*SKEIN_256_BLOCK_BYTES);
1857
memcpy(ctx->X,X,sizeof(X)); /* restore the counter mode key for next time */
1858
}
1859
return SKEIN_SUCCESS;
1860
}
1861
1862
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1863
/* just do the OUTPUT stage */
1864
static int Skein_512_Output(Skein_512_Ctxt_t *ctx, u08b_t *hashVal)
1865
{
1866
size_t i,n,byteCnt;
1867
u64b_t X[SKEIN_512_STATE_WORDS];
1868
Skein_Assert(ctx->h.bCnt <= SKEIN_512_BLOCK_BYTES,SKEIN_FAIL); /* catch uninitialized context */
1869
1870
/* now output the result */
1871
byteCnt = (ctx->h.hashBitLen + 7) >> 3; /* total number of output bytes */
1872
1873
/* run Threefish in "counter mode" to generate output */
1874
memset(ctx->b,0,sizeof(ctx->b)); /* zero out b[], so it can hold the counter */
1875
memcpy(X,ctx->X,sizeof(X)); /* keep a local copy of counter mode "key" */
1876
for (i=0;i*SKEIN_512_BLOCK_BYTES < byteCnt;i++)
1877
{
1878
((u64b_t *)ctx->b)[0]= Skein_Swap64((u64b_t) i); /* build the counter block */
1879
Skein_Start_New_Type(ctx,OUT_FINAL);
1880
Skein_512_Process_Block(ctx,ctx->b,1,sizeof(u64b_t)); /* run "counter mode" */
1881
n = byteCnt - i*SKEIN_512_BLOCK_BYTES; /* number of output bytes left to go */
1882
if (n >= SKEIN_512_BLOCK_BYTES)
1883
n = SKEIN_512_BLOCK_BYTES;
1884
Skein_Put64_LSB_First(hashVal+i*SKEIN_512_BLOCK_BYTES,ctx->X,n); /* "output" the ctr mode bytes */
1885
Skein_Show_Final(256,&ctx->h,n,hashVal+i*SKEIN_512_BLOCK_BYTES);
1886
memcpy(ctx->X,X,sizeof(X)); /* restore the counter mode key for next time */
1887
}
1888
return SKEIN_SUCCESS;
1889
}
1890
1891
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1892
/* just do the OUTPUT stage */
1893
static int Skein1024_Output(Skein1024_Ctxt_t *ctx, u08b_t *hashVal)
1894
{
1895
size_t i,n,byteCnt;
1896
u64b_t X[SKEIN1024_STATE_WORDS];
1897
Skein_Assert(ctx->h.bCnt <= SKEIN1024_BLOCK_BYTES,SKEIN_FAIL); /* catch uninitialized context */
1898
1899
/* now output the result */
1900
byteCnt = (ctx->h.hashBitLen + 7) >> 3; /* total number of output bytes */
1901
1902
/* run Threefish in "counter mode" to generate output */
1903
memset(ctx->b,0,sizeof(ctx->b)); /* zero out b[], so it can hold the counter */
1904
memcpy(X,ctx->X,sizeof(X)); /* keep a local copy of counter mode "key" */
1905
for (i=0;i*SKEIN1024_BLOCK_BYTES < byteCnt;i++)
1906
{
1907
((u64b_t *)ctx->b)[0]= Skein_Swap64((u64b_t) i); /* build the counter block */
1908
Skein_Start_New_Type(ctx,OUT_FINAL);
1909
Skein1024_Process_Block(ctx,ctx->b,1,sizeof(u64b_t)); /* run "counter mode" */
1910
n = byteCnt - i*SKEIN1024_BLOCK_BYTES; /* number of output bytes left to go */
1911
if (n >= SKEIN1024_BLOCK_BYTES)
1912
n = SKEIN1024_BLOCK_BYTES;
1913
Skein_Put64_LSB_First(hashVal+i*SKEIN1024_BLOCK_BYTES,ctx->X,n); /* "output" the ctr mode bytes */
1914
Skein_Show_Final(256,&ctx->h,n,hashVal+i*SKEIN1024_BLOCK_BYTES);
1915
memcpy(ctx->X,X,sizeof(X)); /* restore the counter mode key for next time */
1916
}
1917
return SKEIN_SUCCESS;
1918
}
1919
#endif
1920
#endif
1921
1922
typedef struct
1923
{
1924
uint_t statebits; /* 256, 512, or 1024 */
1925
union
1926
{
1927
Skein_Ctxt_Hdr_t h; /* common header "overlay" */
1928
Skein_256_Ctxt_t ctx_256;
1929
Skein_512_Ctxt_t ctx_512;
1930
Skein1024_Ctxt_t ctx1024;
1931
} u;
1932
}
1933
hashState;
1934
1935
/* "incremental" hashing API */
1936
static SkeinHashReturn Init (hashState *state, int hashbitlen);
1937
static SkeinHashReturn Update(hashState *state, const SkeinBitSequence *data, SkeinDataLength databitlen);
1938
static SkeinHashReturn Final (hashState *state, SkeinBitSequence *hashval);
1939
1940
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1941
/* select the context size and init the context */
1942
static SkeinHashReturn Init(hashState *state, int hashbitlen)
1943
{
1944
#if SKEIN_256_NIST_MAX_HASH_BITS
1945
if (hashbitlen <= SKEIN_256_NIST_MAX_HASHBITS)
1946
{
1947
Skein_Assert(hashbitlen > 0,BAD_HASHLEN);
1948
state->statebits = 64*SKEIN_256_STATE_WORDS;
1949
return Skein_256_Init(&state->u.ctx_256,(size_t) hashbitlen);
1950
}
1951
#endif
1952
if (hashbitlen <= SKEIN_512_NIST_MAX_HASHBITS)
1953
{
1954
state->statebits = 64*SKEIN_512_STATE_WORDS;
1955
return Skein_512_Init(&state->u.ctx_512,(size_t) hashbitlen);
1956
}
1957
else
1958
{
1959
state->statebits = 64*SKEIN1024_STATE_WORDS;
1960
return Skein1024_Init(&state->u.ctx1024,(size_t) hashbitlen);
1961
}
1962
}
1963
1964
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
1965
/* process data to be hashed */
1966
static SkeinHashReturn Update(hashState *state, const SkeinBitSequence *data, SkeinDataLength databitlen)
1967
{
1968
/* only the final Update() call is allowed do partial bytes, else assert an error */
1969
Skein_Assert((state->u.h.T[1] & SKEIN_T1_FLAG_BIT_PAD) == 0 || databitlen == 0, SKEIN_FAIL);
1970
1971
Skein_Assert(state->statebits % 256 == 0 && (state->statebits-256) < 1024,SKEIN_FAIL);
1972
if ((databitlen & 7) == 0) /* partial bytes? */
1973
{
1974
switch ((state->statebits >> 8) & 3)
1975
{
1976
case 2: return Skein_512_Update(&state->u.ctx_512,data,databitlen >> 3);
1977
case 1: return Skein_256_Update(&state->u.ctx_256,data,databitlen >> 3);
1978
case 0: return Skein1024_Update(&state->u.ctx1024,data,databitlen >> 3);
1979
default: return SKEIN_FAIL;
1980
}
1981
}
1982
else
1983
{ /* handle partial final byte */
1984
size_t bCnt = (databitlen >> 3) + 1; /* number of bytes to handle (nonzero here!) */
1985
u08b_t b,mask;
1986
1987
mask = (u08b_t) (1u << (7 - (databitlen & 7))); /* partial byte bit mask */
1988
b = (u08b_t) ((data[bCnt-1] & (0-mask)) | mask); /* apply bit padding on final byte */
1989
1990
switch ((state->statebits >> 8) & 3)
1991
{
1992
case 2: Skein_512_Update(&state->u.ctx_512,data,bCnt-1); /* process all but the final byte */
1993
Skein_512_Update(&state->u.ctx_512,&b , 1 ); /* process the (masked) partial byte */
1994
break;
1995
case 1: Skein_256_Update(&state->u.ctx_256,data,bCnt-1); /* process all but the final byte */
1996
Skein_256_Update(&state->u.ctx_256,&b , 1 ); /* process the (masked) partial byte */
1997
break;
1998
case 0: Skein1024_Update(&state->u.ctx1024,data,bCnt-1); /* process all but the final byte */
1999
Skein1024_Update(&state->u.ctx1024,&b , 1 ); /* process the (masked) partial byte */
2000
break;
2001
default: return SKEIN_FAIL;
2002
}
2003
Skein_Set_Bit_Pad_Flag(state->u.h); /* set tweak flag for the final call */
2004
2005
return SKEIN_SUCCESS;
2006
}
2007
}
2008
2009
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
2010
/* finalize hash computation and output the result (hashbitlen bits) */
2011
static SkeinHashReturn Final(hashState *state, SkeinBitSequence *hashval)
2012
{
2013
Skein_Assert(state->statebits % 256 == 0 && (state->statebits-256) < 1024,FAIL);
2014
switch ((state->statebits >> 8) & 3)
2015
{
2016
case 2: return Skein_512_Final(&state->u.ctx_512,hashval);
2017
case 1: return Skein_256_Final(&state->u.ctx_256,hashval);
2018
case 0: return Skein1024_Final(&state->u.ctx1024,hashval);
2019
default: return SKEIN_FAIL;
2020
}
2021
}
2022
2023
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
2024
/* all-in-one hash function */
2025
SkeinHashReturn skein_hash(int hashbitlen, const SkeinBitSequence *data, /* all-in-one call */
2026
SkeinDataLength databitlen,SkeinBitSequence *hashval)
2027
{
2028
hashState state;
2029
SkeinHashReturn r = Init(&state,hashbitlen);
2030
if (r == SKEIN_SUCCESS)
2031
{ /* these calls do not fail when called properly */
2032
r = Update(&state,data,databitlen);
2033
Final(&state,hashval);
2034
}
2035
return r;
2036
}
2037
2038