3 /**************************************************************************
5 ** Interface declarations and internal definitions for Skein hashing.
7 ** Source code author: Doug Whiting, 2008.
9 ** This algorithm and source code is released to the public domain.
11 ***************************************************************************
13 ** The following compile-time switches may be defined to control some
14 ** tradeoffs between speed, code size, error checking, and security.
16 ** The "default" note explains what happens when the switch is not defined.
18 ** SKEIN_DEBUG -- make callouts from inside Skein code
19 ** to examine/display intermediate values.
20 ** [default: no callouts (no overhead)]
22 ** SKEIN_ERR_CHECK -- how error checking is handled inside Skein
23 ** code. If not defined, most error checking
24 ** is disabled (for performance). Otherwise,
25 ** the switch value is interpreted as:
26 ** 0: use assert() to flag errors
27 ** 1: return SKEIN_FAIL to flag errors
29 ***************************************************************************/
32 #define RotL_64(x, N) (((x) << (N)) | ((x) >> (64-(N))))
35 /* below two prototype assume we are handed aligned data */
36 #define Skein_Put64_LSB_First(dst08, src64, bCnt) memcpy(dst08, src64, bCnt)
37 #define Skein_Get64_LSB_First(dst64, src08, wCnt) memcpy(dst64, src08, 8*(wCnt))
38 #define Skein_Swap64(w64) (w64)
41 SKEIN_SUCCESS = 0, /* return codes from Skein calls */
46 #define SKEIN_MODIFIER_WORDS (2) /* number of modifier (tweak) words */
48 #define SKEIN_256_STATE_WORDS (4)
49 #define SKEIN_512_STATE_WORDS (8)
50 #define SKEIN1024_STATE_WORDS (16)
51 #define SKEIN_MAX_STATE_WORDS (16)
53 #define SKEIN_256_STATE_BYTES (8*SKEIN_256_STATE_WORDS)
54 #define SKEIN_512_STATE_BYTES (8*SKEIN_512_STATE_WORDS)
55 #define SKEIN1024_STATE_BYTES (8*SKEIN1024_STATE_WORDS)
57 #define SKEIN_256_STATE_BITS (64*SKEIN_256_STATE_WORDS)
58 #define SKEIN_512_STATE_BITS (64*SKEIN_512_STATE_WORDS)
59 #define SKEIN1024_STATE_BITS (64*SKEIN1024_STATE_WORDS)
61 #define SKEIN_256_BLOCK_BYTES (8*SKEIN_256_STATE_WORDS)
62 #define SKEIN_512_BLOCK_BYTES (8*SKEIN_512_STATE_WORDS)
63 #define SKEIN1024_BLOCK_BYTES (8*SKEIN1024_STATE_WORDS)
65 struct skein_ctx_hdr {
66 size_t hashBitLen; /* size of hash result, in bits */
67 size_t bCnt; /* current byte count in buffer b[] */
68 u64 T[SKEIN_MODIFIER_WORDS]; /* tweak: T[0]=byte cnt, T[1]=flags */
71 struct skein_256_ctx { /* 256-bit Skein hash context structure */
72 struct skein_ctx_hdr h; /* common header context variables */
73 u64 X[SKEIN_256_STATE_WORDS]; /* chaining variables */
74 u8 b[SKEIN_256_BLOCK_BYTES]; /* partial block buf (8-byte aligned) */
77 struct skein_512_ctx { /* 512-bit Skein hash context structure */
78 struct skein_ctx_hdr h; /* common header context variables */
79 u64 X[SKEIN_512_STATE_WORDS]; /* chaining variables */
80 u8 b[SKEIN_512_BLOCK_BYTES]; /* partial block buf (8-byte aligned) */
83 struct skein1024_ctx { /* 1024-bit Skein hash context structure */
84 struct skein_ctx_hdr h; /* common header context variables */
85 u64 X[SKEIN1024_STATE_WORDS]; /* chaining variables */
86 u8 b[SKEIN1024_BLOCK_BYTES]; /* partial block buf (8-byte aligned) */
89 /* Skein APIs for (incremental) "straight hashing" */
90 int Skein_256_Init(struct skein_256_ctx *ctx, size_t hashBitLen);
91 int Skein_512_Init(struct skein_512_ctx *ctx, size_t hashBitLen);
92 int Skein1024_Init(struct skein1024_ctx *ctx, size_t hashBitLen);
94 int Skein_256_Update(struct skein_256_ctx *ctx, const u8 *msg,
96 int Skein_512_Update(struct skein_512_ctx *ctx, const u8 *msg,
98 int Skein1024_Update(struct skein1024_ctx *ctx, const u8 *msg,
101 int Skein_256_Final(struct skein_256_ctx *ctx, u8 *hashVal);
102 int Skein_512_Final(struct skein_512_ctx *ctx, u8 *hashVal);
103 int Skein1024_Final(struct skein1024_ctx *ctx, u8 *hashVal);
106 ** Skein APIs for "extended" initialization: MAC keys, tree hashing.
107 ** After an InitExt() call, just use Update/Final calls as with Init().
109 ** Notes: Same parameters as _Init() calls, plus treeInfo/key/keyBytes.
110 ** When keyBytes == 0 and treeInfo == SKEIN_SEQUENTIAL,
111 ** the results of InitExt() are identical to calling Init().
112 ** The function Init() may be called once to "precompute" the IV for
113 ** a given hashBitLen value, then by saving a copy of the context
114 ** the IV computation may be avoided in later calls.
115 ** Similarly, the function InitExt() may be called once per MAC key
116 ** to precompute the MAC IV, then a copy of the context saved and
117 ** reused for each new MAC computation.
119 int Skein_256_InitExt(struct skein_256_ctx *ctx, size_t hashBitLen,
120 u64 treeInfo, const u8 *key, size_t keyBytes);
121 int Skein_512_InitExt(struct skein_512_ctx *ctx, size_t hashBitLen,
122 u64 treeInfo, const u8 *key, size_t keyBytes);
123 int Skein1024_InitExt(struct skein1024_ctx *ctx, size_t hashBitLen,
124 u64 treeInfo, const u8 *key, size_t keyBytes);
127 ** Skein APIs for MAC and tree hash:
128 ** Final_Pad: pad, do final block, but no OUTPUT type
129 ** Output: do just the output stage
131 int Skein_256_Final_Pad(struct skein_256_ctx *ctx, u8 *hashVal);
132 int Skein_512_Final_Pad(struct skein_512_ctx *ctx, u8 *hashVal);
133 int Skein1024_Final_Pad(struct skein1024_ctx *ctx, u8 *hashVal);
135 #ifndef SKEIN_TREE_HASH
136 #define SKEIN_TREE_HASH (1)
139 int Skein_256_Output(struct skein_256_ctx *ctx, u8 *hashVal);
140 int Skein_512_Output(struct skein_512_ctx *ctx, u8 *hashVal);
141 int Skein1024_Output(struct skein1024_ctx *ctx, u8 *hashVal);
144 /*****************************************************************
145 ** "Internal" Skein definitions
146 ** -- not needed for sequential hashing API, but will be
147 ** helpful for other uses of Skein (e.g., tree hash mode).
148 ** -- included here so that they can be shared between
149 ** reference and optimized code.
150 ******************************************************************/
152 /* tweak word T[1]: bit field starting positions */
153 #define SKEIN_T1_BIT(BIT) ((BIT) - 64) /* second word */
155 #define SKEIN_T1_POS_TREE_LVL SKEIN_T1_BIT(112) /* 112..118 hash tree level */
156 #define SKEIN_T1_POS_BIT_PAD SKEIN_T1_BIT(119) /* 119 part. final in byte */
157 #define SKEIN_T1_POS_BLK_TYPE SKEIN_T1_BIT(120) /* 120..125 type field `*/
158 #define SKEIN_T1_POS_FIRST SKEIN_T1_BIT(126) /* 126 first blk flag */
159 #define SKEIN_T1_POS_FINAL SKEIN_T1_BIT(127) /* 127 final blk flag */
161 /* tweak word T[1]: flag bit definition(s) */
162 #define SKEIN_T1_FLAG_FIRST (((u64) 1) << SKEIN_T1_POS_FIRST)
163 #define SKEIN_T1_FLAG_FINAL (((u64) 1) << SKEIN_T1_POS_FINAL)
164 #define SKEIN_T1_FLAG_BIT_PAD (((u64) 1) << SKEIN_T1_POS_BIT_PAD)
166 /* tweak word T[1]: tree level bit field mask */
167 #define SKEIN_T1_TREE_LVL_MASK (((u64)0x7F) << SKEIN_T1_POS_TREE_LVL)
168 #define SKEIN_T1_TREE_LEVEL(n) (((u64) (n)) << SKEIN_T1_POS_TREE_LVL)
170 /* tweak word T[1]: block type field */
171 #define SKEIN_BLK_TYPE_KEY (0) /* key, for MAC and KDF */
172 #define SKEIN_BLK_TYPE_CFG (4) /* configuration block */
173 #define SKEIN_BLK_TYPE_PERS (8) /* personalization string */
174 #define SKEIN_BLK_TYPE_PK (12) /* pubkey (for digital sigs) */
175 #define SKEIN_BLK_TYPE_KDF (16) /* key identifier for KDF */
176 #define SKEIN_BLK_TYPE_NONCE (20) /* nonce for PRNG */
177 #define SKEIN_BLK_TYPE_MSG (48) /* message processing */
178 #define SKEIN_BLK_TYPE_OUT (63) /* output stage */
179 #define SKEIN_BLK_TYPE_MASK (63) /* bit field mask */
181 #define SKEIN_T1_BLK_TYPE(T) (((u64) (SKEIN_BLK_TYPE_##T)) << \
182 SKEIN_T1_POS_BLK_TYPE)
183 #define SKEIN_T1_BLK_TYPE_KEY SKEIN_T1_BLK_TYPE(KEY) /* for MAC and KDF */
184 #define SKEIN_T1_BLK_TYPE_CFG SKEIN_T1_BLK_TYPE(CFG) /* config block */
185 #define SKEIN_T1_BLK_TYPE_PERS SKEIN_T1_BLK_TYPE(PERS) /* personalization */
186 #define SKEIN_T1_BLK_TYPE_PK SKEIN_T1_BLK_TYPE(PK) /* pubkey (for sigs) */
187 #define SKEIN_T1_BLK_TYPE_KDF SKEIN_T1_BLK_TYPE(KDF) /* key ident for KDF */
188 #define SKEIN_T1_BLK_TYPE_NONCE SKEIN_T1_BLK_TYPE(NONCE)/* nonce for PRNG */
189 #define SKEIN_T1_BLK_TYPE_MSG SKEIN_T1_BLK_TYPE(MSG) /* message processing */
190 #define SKEIN_T1_BLK_TYPE_OUT SKEIN_T1_BLK_TYPE(OUT) /* output stage */
191 #define SKEIN_T1_BLK_TYPE_MASK SKEIN_T1_BLK_TYPE(MASK) /* field bit mask */
193 #define SKEIN_T1_BLK_TYPE_CFG_FINAL (SKEIN_T1_BLK_TYPE_CFG | \
195 #define SKEIN_T1_BLK_TYPE_OUT_FINAL (SKEIN_T1_BLK_TYPE_OUT | \
198 #define SKEIN_VERSION (1)
200 #ifndef SKEIN_ID_STRING_LE /* allow compile-time personalization */
201 #define SKEIN_ID_STRING_LE (0x33414853) /* "SHA3" (little-endian)*/
204 #define SKEIN_MK_64(hi32, lo32) ((lo32) + (((u64) (hi32)) << 32))
205 #define SKEIN_SCHEMA_VER SKEIN_MK_64(SKEIN_VERSION, SKEIN_ID_STRING_LE)
206 #define SKEIN_KS_PARITY SKEIN_MK_64(0x1BD11BDA, 0xA9FC1A22)
208 #define SKEIN_CFG_STR_LEN (4*8)
210 /* bit field definitions in config block treeInfo word */
211 #define SKEIN_CFG_TREE_LEAF_SIZE_POS (0)
212 #define SKEIN_CFG_TREE_NODE_SIZE_POS (8)
213 #define SKEIN_CFG_TREE_MAX_LEVEL_POS (16)
215 #define SKEIN_CFG_TREE_LEAF_SIZE_MSK (((u64)0xFF) << \
216 SKEIN_CFG_TREE_LEAF_SIZE_POS)
217 #define SKEIN_CFG_TREE_NODE_SIZE_MSK (((u64)0xFF) << \
218 SKEIN_CFG_TREE_NODE_SIZE_POS)
219 #define SKEIN_CFG_TREE_MAX_LEVEL_MSK (((u64)0xFF) << \
220 SKEIN_CFG_TREE_MAX_LEVEL_POS)
222 #define SKEIN_CFG_TREE_INFO(leaf, node, maxLvl) \
223 ((((u64)(leaf)) << SKEIN_CFG_TREE_LEAF_SIZE_POS) | \
224 (((u64)(node)) << SKEIN_CFG_TREE_NODE_SIZE_POS) | \
225 (((u64)(maxLvl)) << SKEIN_CFG_TREE_MAX_LEVEL_POS))
227 /* use as treeInfo in InitExt() call for sequential processing */
228 #define SKEIN_CFG_TREE_INFO_SEQUENTIAL SKEIN_CFG_TREE_INFO(0, 0, 0)
231 ** Skein macros for getting/setting tweak words, etc.
232 ** These are useful for partial input bytes, hash tree init/update, etc.
234 #define Skein_Get_Tweak(ctxPtr, TWK_NUM) ((ctxPtr)->h.T[TWK_NUM])
235 #define Skein_Set_Tweak(ctxPtr, TWK_NUM, tVal) { \
236 (ctxPtr)->h.T[TWK_NUM] = (tVal); \
239 #define Skein_Get_T0(ctxPtr) Skein_Get_Tweak(ctxPtr, 0)
240 #define Skein_Get_T1(ctxPtr) Skein_Get_Tweak(ctxPtr, 1)
241 #define Skein_Set_T0(ctxPtr, T0) Skein_Set_Tweak(ctxPtr, 0, T0)
242 #define Skein_Set_T1(ctxPtr, T1) Skein_Set_Tweak(ctxPtr, 1, T1)
244 /* set both tweak words at once */
245 #define Skein_Set_T0_T1(ctxPtr, T0, T1) \
247 Skein_Set_T0(ctxPtr, (T0)); \
248 Skein_Set_T1(ctxPtr, (T1)); \
251 #define Skein_Set_Type(ctxPtr, BLK_TYPE) \
252 Skein_Set_T1(ctxPtr, SKEIN_T1_BLK_TYPE_##BLK_TYPE)
255 * setup for starting with a new type:
256 * h.T[0]=0; h.T[1] = NEW_TYPE; h.bCnt=0;
258 #define Skein_Start_New_Type(ctxPtr, BLK_TYPE) { \
259 Skein_Set_T0_T1(ctxPtr, 0, SKEIN_T1_FLAG_FIRST | \
260 SKEIN_T1_BLK_TYPE_##BLK_TYPE); \
261 (ctxPtr)->h.bCnt = 0; \
264 #define Skein_Clear_First_Flag(hdr) { \
265 (hdr).T[1] &= ~SKEIN_T1_FLAG_FIRST; \
267 #define Skein_Set_Bit_Pad_Flag(hdr) { \
268 (hdr).T[1] |= SKEIN_T1_FLAG_BIT_PAD; \
271 #define Skein_Set_Tree_Level(hdr, height) { \
272 (hdr).T[1] |= SKEIN_T1_TREE_LEVEL(height); \
275 /*****************************************************************
276 ** "Internal" Skein definitions for debugging and error checking
277 ******************************************************************/
278 #ifdef SKEIN_DEBUG /* examine/display intermediate values? */
279 #include "skein_debug.h"
280 #else /* default is no callouts */
281 #define Skein_Show_Block(bits, ctx, X, blkPtr, wPtr, ksEvenPtr, ksOddPtr)
282 #define Skein_Show_Round(bits, ctx, r, X)
283 #define Skein_Show_R_Ptr(bits, ctx, r, X_ptr)
284 #define Skein_Show_Final(bits, ctx, cnt, outPtr)
285 #define Skein_Show_Key(bits, ctx, key, keyBytes)
288 #define Skein_Assert(x, retCode)/* ignore all Asserts, for performance */
289 #define Skein_assert(x)
291 /*****************************************************************
292 ** Skein block function constants (shared across Ref and Opt code)
293 ******************************************************************/
295 /* Skein_256 round rotation constants */
296 R_256_0_0 = 14, R_256_0_1 = 16,
297 R_256_1_0 = 52, R_256_1_1 = 57,
298 R_256_2_0 = 23, R_256_2_1 = 40,
299 R_256_3_0 = 5, R_256_3_1 = 37,
300 R_256_4_0 = 25, R_256_4_1 = 33,
301 R_256_5_0 = 46, R_256_5_1 = 12,
302 R_256_6_0 = 58, R_256_6_1 = 22,
303 R_256_7_0 = 32, R_256_7_1 = 32,
305 /* Skein_512 round rotation constants */
306 R_512_0_0 = 46, R_512_0_1 = 36, R_512_0_2 = 19, R_512_0_3 = 37,
307 R_512_1_0 = 33, R_512_1_1 = 27, R_512_1_2 = 14, R_512_1_3 = 42,
308 R_512_2_0 = 17, R_512_2_1 = 49, R_512_2_2 = 36, R_512_2_3 = 39,
309 R_512_3_0 = 44, R_512_3_1 = 9, R_512_3_2 = 54, R_512_3_3 = 56,
310 R_512_4_0 = 39, R_512_4_1 = 30, R_512_4_2 = 34, R_512_4_3 = 24,
311 R_512_5_0 = 13, R_512_5_1 = 50, R_512_5_2 = 10, R_512_5_3 = 17,
312 R_512_6_0 = 25, R_512_6_1 = 29, R_512_6_2 = 39, R_512_6_3 = 43,
313 R_512_7_0 = 8, R_512_7_1 = 35, R_512_7_2 = 56, R_512_7_3 = 22,
315 /* Skein1024 round rotation constants */
316 R1024_0_0 = 24, R1024_0_1 = 13, R1024_0_2 = 8, R1024_0_3 = 47,
317 R1024_0_4 = 8, R1024_0_5 = 17, R1024_0_6 = 22, R1024_0_7 = 37,
318 R1024_1_0 = 38, R1024_1_1 = 19, R1024_1_2 = 10, R1024_1_3 = 55,
319 R1024_1_4 = 49, R1024_1_5 = 18, R1024_1_6 = 23, R1024_1_7 = 52,
320 R1024_2_0 = 33, R1024_2_1 = 4, R1024_2_2 = 51, R1024_2_3 = 13,
321 R1024_2_4 = 34, R1024_2_5 = 41, R1024_2_6 = 59, R1024_2_7 = 17,
322 R1024_3_0 = 5, R1024_3_1 = 20, R1024_3_2 = 48, R1024_3_3 = 41,
323 R1024_3_4 = 47, R1024_3_5 = 28, R1024_3_6 = 16, R1024_3_7 = 25,
324 R1024_4_0 = 41, R1024_4_1 = 9, R1024_4_2 = 37, R1024_4_3 = 31,
325 R1024_4_4 = 12, R1024_4_5 = 47, R1024_4_6 = 44, R1024_4_7 = 30,
326 R1024_5_0 = 16, R1024_5_1 = 34, R1024_5_2 = 56, R1024_5_3 = 51,
327 R1024_5_4 = 4, R1024_5_5 = 53, R1024_5_6 = 42, R1024_5_7 = 41,
328 R1024_6_0 = 31, R1024_6_1 = 44, R1024_6_2 = 47, R1024_6_3 = 46,
329 R1024_6_4 = 19, R1024_6_5 = 42, R1024_6_6 = 44, R1024_6_7 = 25,
330 R1024_7_0 = 9, R1024_7_1 = 48, R1024_7_2 = 35, R1024_7_3 = 52,
331 R1024_7_4 = 23, R1024_7_5 = 31, R1024_7_6 = 37, R1024_7_7 = 20
335 #define SKEIN_256_ROUNDS_TOTAL (72) /* # rounds for diff block sizes */
336 #define SKEIN_512_ROUNDS_TOTAL (72)
337 #define SKEIN1024_ROUNDS_TOTAL (80)
338 #else /* allow command-line define in range 8*(5..14) */
339 #define SKEIN_256_ROUNDS_TOTAL (8*((((SKEIN_ROUNDS/100) + 5) % 10) + 5))
340 #define SKEIN_512_ROUNDS_TOTAL (8*((((SKEIN_ROUNDS/10) + 5) % 10) + 5))
341 #define SKEIN1024_ROUNDS_TOTAL (8*((((SKEIN_ROUNDS) + 5) % 10) + 5))
344 #endif /* ifndef _SKEIN_H_ */