ccRTP 2.1.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
skein.h
Go to the documentation of this file.
1 #ifndef _SKEIN_H_
2 #define _SKEIN_H_ 1
3 /**************************************************************************
4 **
5 ** Interface declarations and internal definitions for Skein hashing.
6 **
7 ** Source code author: Doug Whiting, 2008.
8 **
9 ** This algorithm and source code is released to the public domain.
10 **
11 ***************************************************************************
12 **
13 ** The following compile-time switches may be defined to control some
14 ** tradeoffs between speed, code size, error checking, and security.
15 **
16 ** The "default" note explains what happens when the switch is not defined.
17 **
18 ** SKEIN_DEBUG -- make callouts from inside Skein code
19 ** to examine/display intermediate values.
20 ** [default: no callouts (no overhead)]
21 **
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
28 **
29 ***************************************************************************/
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #endif
34 
35 #include <stddef.h> /* get size_t definition */
36 #include <crypto/skein_port.h> /* get platform-specific definitions */
37 
38 enum
39  {
40  SKEIN_SUCCESS = 0, /* return codes from Skein calls */
43  };
44 
45 #define SKEIN_MODIFIER_WORDS ( 2) /* number of modifier (tweak) words */
46 
47 #define SKEIN_256_STATE_WORDS ( 4)
48 #define SKEIN_512_STATE_WORDS ( 8)
49 #define SKEIN1024_STATE_WORDS (16)
50 #define SKEIN_MAX_STATE_WORDS (16)
51 
52 #define SKEIN_256_STATE_BYTES ( 8*SKEIN_256_STATE_WORDS)
53 #define SKEIN_512_STATE_BYTES ( 8*SKEIN_512_STATE_WORDS)
54 #define SKEIN1024_STATE_BYTES ( 8*SKEIN1024_STATE_WORDS)
55 
56 #define SKEIN_256_STATE_BITS (64*SKEIN_256_STATE_WORDS)
57 #define SKEIN_512_STATE_BITS (64*SKEIN_512_STATE_WORDS)
58 #define SKEIN1024_STATE_BITS (64*SKEIN1024_STATE_WORDS)
59 
60 #define SKEIN_256_BLOCK_BYTES ( 8*SKEIN_256_STATE_WORDS)
61 #define SKEIN_512_BLOCK_BYTES ( 8*SKEIN_512_STATE_WORDS)
62 #define SKEIN1024_BLOCK_BYTES ( 8*SKEIN1024_STATE_WORDS)
63 
64 typedef struct
65  {
66  size_t hashBitLen; /* size of hash result, in bits */
67  size_t bCnt; /* current byte count in buffer b[] */
68  u64b_t T[SKEIN_MODIFIER_WORDS]; /* tweak words: T[0]=byte cnt, T[1]=flags */
70 
71 typedef struct /* 256-bit Skein hash context structure */
72  {
73  Skein_Ctxt_Hdr_t h; /* common header context variables */
74  u64b_t X[SKEIN_256_STATE_WORDS]; /* chaining variables */
75  u08b_t b[SKEIN_256_BLOCK_BYTES]; /* partial block buffer (8-byte aligned) */
77 
78 typedef struct /* 512-bit Skein hash context structure */
79  {
80  Skein_Ctxt_Hdr_t h; /* common header context variables */
81  u64b_t X[SKEIN_512_STATE_WORDS]; /* chaining variables */
82  u08b_t b[SKEIN_512_BLOCK_BYTES]; /* partial block buffer (8-byte aligned) */
84 
85 typedef struct /* 1024-bit Skein hash context structure */
86  {
87  Skein_Ctxt_Hdr_t h; /* common header context variables */
88  u64b_t X[SKEIN1024_STATE_WORDS]; /* chaining variables */
89  u08b_t b[SKEIN1024_BLOCK_BYTES]; /* partial block buffer (8-byte aligned) */
91 
92 /* Skein APIs for (incremental) "straight hashing" */
93 int Skein_256_Init (Skein_256_Ctxt_t *ctx, size_t hashBitLen);
94 int Skein_512_Init (Skein_512_Ctxt_t *ctx, size_t hashBitLen);
95 int Skein1024_Init (Skein1024_Ctxt_t *ctx, size_t hashBitLen);
96 
97 int Skein_256_Update(Skein_256_Ctxt_t *ctx, const u08b_t *msg, size_t msgByteCnt);
98 int Skein_512_Update(Skein_512_Ctxt_t *ctx, const u08b_t *msg, size_t msgByteCnt);
99 int Skein1024_Update(Skein1024_Ctxt_t *ctx, const u08b_t *msg, size_t msgByteCnt);
100 
101 int Skein_256_Final (Skein_256_Ctxt_t *ctx, u08b_t * hashVal);
102 int Skein_512_Final (Skein_512_Ctxt_t *ctx, u08b_t * hashVal);
103 int Skein1024_Final (Skein1024_Ctxt_t *ctx, u08b_t * hashVal);
104 
105 /*
106 ** Skein APIs for "extended" initialization: MAC keys, tree hashing.
107 ** After an InitExt() call, just use Update/Final calls as with Init().
108 **
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.
118 **/
119 int Skein_256_InitExt(Skein_256_Ctxt_t *ctx, size_t hashBitLen, u64b_t treeInfo, const u08b_t *key, size_t keyBytes);
120 int Skein_512_InitExt(Skein_512_Ctxt_t *ctx, size_t hashBitLen, u64b_t treeInfo, const u08b_t *key, size_t keyBytes);
121 int Skein1024_InitExt(Skein1024_Ctxt_t *ctx, size_t hashBitLen, u64b_t treeInfo, const u08b_t *key, size_t keyBytes);
122 
123 /*
124 ** Skein APIs for MAC and tree hash:
125 ** Final_Pad: pad, do final block, but no OUTPUT type
126 ** Output: do just the output stage
127 */
128 int Skein_256_Final_Pad(Skein_256_Ctxt_t *ctx, u08b_t * hashVal);
129 int Skein_512_Final_Pad(Skein_512_Ctxt_t *ctx, u08b_t * hashVal);
130 int Skein1024_Final_Pad(Skein1024_Ctxt_t *ctx, u08b_t * hashVal);
131 
132 #ifndef SKEIN_TREE_HASH
133 #define SKEIN_TREE_HASH (1)
134 #endif
135 #if SKEIN_TREE_HASH
136 int Skein_256_Output (Skein_256_Ctxt_t *ctx, u08b_t * hashVal);
137 int Skein_512_Output (Skein_512_Ctxt_t *ctx, u08b_t * hashVal);
138 int Skein1024_Output (Skein1024_Ctxt_t *ctx, u08b_t * hashVal);
139 #endif
140 
141 /*****************************************************************
142 ** "Internal" Skein definitions
143 ** -- not needed for sequential hashing API, but will be
144 ** helpful for other uses of Skein (e.g., tree hash mode).
145 ** -- included here so that they can be shared between
146 ** reference and optimized code.
147 ******************************************************************/
148 
149 /* tweak word T[1]: bit field starting positions */
150 #define SKEIN_T1_BIT(BIT) ((BIT) - 64) /* offset 64 because it's the second word */
151 
152 #define SKEIN_T1_POS_TREE_LVL SKEIN_T1_BIT(112) /* bits 112..118: level in hash tree */
153 #define SKEIN_T1_POS_BIT_PAD SKEIN_T1_BIT(119) /* bit 119 : partial final input byte */
154 #define SKEIN_T1_POS_BLK_TYPE SKEIN_T1_BIT(120) /* bits 120..125: type field */
155 #define SKEIN_T1_POS_FIRST SKEIN_T1_BIT(126) /* bits 126 : first block flag */
156 #define SKEIN_T1_POS_FINAL SKEIN_T1_BIT(127) /* bit 127 : final block flag */
157 
158 /* tweak word T[1]: flag bit definition(s) */
159 #define SKEIN_T1_FLAG_FIRST (((u64b_t) 1 ) << SKEIN_T1_POS_FIRST)
160 #define SKEIN_T1_FLAG_FINAL (((u64b_t) 1 ) << SKEIN_T1_POS_FINAL)
161 #define SKEIN_T1_FLAG_BIT_PAD (((u64b_t) 1 ) << SKEIN_T1_POS_BIT_PAD)
162 
163 /* tweak word T[1]: tree level bit field mask */
164 #define SKEIN_T1_TREE_LVL_MASK (((u64b_t)0x7F) << SKEIN_T1_POS_TREE_LVL)
165 #define SKEIN_T1_TREE_LEVEL(n) (((u64b_t) (n)) << SKEIN_T1_POS_TREE_LVL)
166 
167 /* tweak word T[1]: block type field */
168 #define SKEIN_BLK_TYPE_KEY ( 0) /* key, for MAC and KDF */
169 #define SKEIN_BLK_TYPE_CFG ( 4) /* configuration block */
170 #define SKEIN_BLK_TYPE_PERS ( 8) /* personalization string */
171 #define SKEIN_BLK_TYPE_PK (12) /* public key (for digital signature hashing) */
172 #define SKEIN_BLK_TYPE_KDF (16) /* key identifier for KDF */
173 #define SKEIN_BLK_TYPE_NONCE (20) /* nonce for PRNG */
174 #define SKEIN_BLK_TYPE_MSG (48) /* message processing */
175 #define SKEIN_BLK_TYPE_OUT (63) /* output stage */
176 #define SKEIN_BLK_TYPE_MASK (63) /* bit field mask */
177 
178 #define SKEIN_T1_BLK_TYPE(T) (((u64b_t) (SKEIN_BLK_TYPE_##T)) << SKEIN_T1_POS_BLK_TYPE)
179 #define SKEIN_T1_BLK_TYPE_KEY SKEIN_T1_BLK_TYPE(KEY) /* key, for MAC and KDF */
180 #define SKEIN_T1_BLK_TYPE_CFG SKEIN_T1_BLK_TYPE(CFG) /* configuration block */
181 #define SKEIN_T1_BLK_TYPE_PERS SKEIN_T1_BLK_TYPE(PERS) /* personalization string */
182 #define SKEIN_T1_BLK_TYPE_PK SKEIN_T1_BLK_TYPE(PK) /* public key (for digital signature hashing) */
183 #define SKEIN_T1_BLK_TYPE_KDF SKEIN_T1_BLK_TYPE(KDF) /* key identifier for KDF */
184 #define SKEIN_T1_BLK_TYPE_NONCE SKEIN_T1_BLK_TYPE(NONCE)/* nonce for PRNG */
185 #define SKEIN_T1_BLK_TYPE_MSG SKEIN_T1_BLK_TYPE(MSG) /* message processing */
186 #define SKEIN_T1_BLK_TYPE_OUT SKEIN_T1_BLK_TYPE(OUT) /* output stage */
187 #define SKEIN_T1_BLK_TYPE_MASK SKEIN_T1_BLK_TYPE(MASK) /* field bit mask */
188 
189 #define SKEIN_T1_BLK_TYPE_CFG_FINAL (SKEIN_T1_BLK_TYPE_CFG | SKEIN_T1_FLAG_FINAL)
190 #define SKEIN_T1_BLK_TYPE_OUT_FINAL (SKEIN_T1_BLK_TYPE_OUT | SKEIN_T1_FLAG_FINAL)
191 
192 #define SKEIN_VERSION (1)
193 
194 #ifndef SKEIN_ID_STRING_LE /* allow compile-time personalization */
195 #define SKEIN_ID_STRING_LE (0x33414853) /* "SHA3" (little-endian)*/
196 #endif
197 
198 #define SKEIN_MK_64(hi32,lo32) ((lo32) + (((u64b_t) (hi32)) << 32))
199 #define SKEIN_SCHEMA_VER SKEIN_MK_64(SKEIN_VERSION,SKEIN_ID_STRING_LE)
200 #define SKEIN_KS_PARITY SKEIN_MK_64(0x1BD11BDA,0xA9FC1A22)
201 
202 #define SKEIN_CFG_STR_LEN (4*8)
203 
204 /* bit field definitions in config block treeInfo word */
205 #define SKEIN_CFG_TREE_LEAF_SIZE_POS ( 0)
206 #define SKEIN_CFG_TREE_NODE_SIZE_POS ( 8)
207 #define SKEIN_CFG_TREE_MAX_LEVEL_POS (16)
208 
209 #define SKEIN_CFG_TREE_LEAF_SIZE_MSK (((u64b_t) 0xFF) << SKEIN_CFG_TREE_LEAF_SIZE_POS)
210 #define SKEIN_CFG_TREE_NODE_SIZE_MSK (((u64b_t) 0xFF) << SKEIN_CFG_TREE_NODE_SIZE_POS)
211 #define SKEIN_CFG_TREE_MAX_LEVEL_MSK (((u64b_t) 0xFF) << SKEIN_CFG_TREE_MAX_LEVEL_POS)
212 
213 #define SKEIN_CFG_TREE_INFO(leaf,node,maxLvl) \
214  ( (((u64b_t)(leaf )) << SKEIN_CFG_TREE_LEAF_SIZE_POS) | \
215  (((u64b_t)(node )) << SKEIN_CFG_TREE_NODE_SIZE_POS) | \
216  (((u64b_t)(maxLvl)) << SKEIN_CFG_TREE_MAX_LEVEL_POS) )
217 
218 #define SKEIN_CFG_TREE_INFO_SEQUENTIAL SKEIN_CFG_TREE_INFO(0,0,0) /* use as treeInfo in InitExt() call for sequential processing */
219 
220 /*
221 ** Skein macros for getting/setting tweak words, etc.
222 ** These are useful for partial input bytes, hash tree init/update, etc.
223 **/
224 #define Skein_Get_Tweak(ctxPtr,TWK_NUM) ((ctxPtr)->h.T[TWK_NUM])
225 #define Skein_Set_Tweak(ctxPtr,TWK_NUM,tVal) {(ctxPtr)->h.T[TWK_NUM] = (tVal);}
226 
227 #define Skein_Get_T0(ctxPtr) Skein_Get_Tweak(ctxPtr,0)
228 #define Skein_Get_T1(ctxPtr) Skein_Get_Tweak(ctxPtr,1)
229 #define Skein_Set_T0(ctxPtr,T0) Skein_Set_Tweak(ctxPtr,0,T0)
230 #define Skein_Set_T1(ctxPtr,T1) Skein_Set_Tweak(ctxPtr,1,T1)
231 
232 /* set both tweak words at once */
233 #define Skein_Set_T0_T1(ctxPtr,T0,T1) \
234  { \
235  Skein_Set_T0(ctxPtr,(T0)); \
236  Skein_Set_T1(ctxPtr,(T1)); \
237  }
238 
239 #define Skein_Set_Type(ctxPtr,BLK_TYPE) \
240  Skein_Set_T1(ctxPtr,SKEIN_T1_BLK_TYPE_##BLK_TYPE)
241 
242 /* set up for starting with a new type: h.T[0]=0; h.T[1] = NEW_TYPE; h.bCnt=0; */
243 #define Skein_Start_New_Type(ctxPtr,BLK_TYPE) \
244  { Skein_Set_T0_T1(ctxPtr,0,SKEIN_T1_FLAG_FIRST | SKEIN_T1_BLK_TYPE_##BLK_TYPE); (ctxPtr)->h.bCnt=0; }
245 
246 #define Skein_Clear_First_Flag(hdr) { (hdr).T[1] &= ~SKEIN_T1_FLAG_FIRST; }
247 #define Skein_Set_Bit_Pad_Flag(hdr) { (hdr).T[1] |= SKEIN_T1_FLAG_BIT_PAD; }
248 
249 #define Skein_Set_Tree_Level(hdr,height) { (hdr).T[1] |= SKEIN_T1_TREE_LEVEL(height);}
250 
251 /*****************************************************************
252 ** "Internal" Skein definitions for debugging and error checking
253 ******************************************************************/
254 #ifdef SKEIN_DEBUG /* examine/display intermediate values? */
255 #include "skein_debug.h"
256 #else /* default is no callouts */
257 #define Skein_Show_Block(bits,ctx,X,blkPtr,wPtr,ksEvenPtr,ksOddPtr)
258 #define Skein_Show_Round(bits,ctx,r,X)
259 #define Skein_Show_R_Ptr(bits,ctx,r,X_ptr)
260 #define Skein_Show_Final(bits,ctx,cnt,outPtr)
261 #define Skein_Show_Key(bits,ctx,key,keyBytes)
262 #endif
263 
264 #ifndef SKEIN_ERR_CHECK /* run-time checks (e.g., bad params, uninitialized context)? */
265 #define Skein_Assert(x,retCode)/* default: ignore all Asserts, for performance */
266 #define Skein_assert(x)
267 #elif defined(SKEIN_ASSERT)
268 #include <assert.h>
269 #define Skein_Assert(x,retCode) assert(x)
270 #define Skein_assert(x) assert(x)
271 #else
272 #include <assert.h>
273 #define Skein_Assert(x,retCode) { if (!(x)) return retCode; } /* caller error */
274 #define Skein_assert(x) assert(x) /* internal error */
275 #endif
276 
277 /*****************************************************************
278 ** Skein block function constants (shared across Ref and Opt code)
279 ******************************************************************/
280 enum
281  {
282  /* Skein_256 round rotation constants */
291 
292  /* Skein_512 round rotation constants */
301 
302  /* Skein1024 round rotation constants */
311  };
312 
313 #ifndef SKEIN_ROUNDS
314 #define SKEIN_256_ROUNDS_TOTAL (72) /* number of rounds for the different block sizes */
315 #define SKEIN_512_ROUNDS_TOTAL (72)
316 #define SKEIN1024_ROUNDS_TOTAL (80)
317 #else /* allow command-line define in range 8*(5..14) */
318 #define SKEIN_256_ROUNDS_TOTAL (8*((((SKEIN_ROUNDS/100) + 5) % 10) + 5))
319 #define SKEIN_512_ROUNDS_TOTAL (8*((((SKEIN_ROUNDS/ 10) + 5) % 10) + 5))
320 #define SKEIN1024_ROUNDS_TOTAL (8*((((SKEIN_ROUNDS ) + 5) % 10) + 5))
321 #endif
322 
323 #ifdef __cplusplus
324 }
325 #endif
326 
327 #endif /* ifndef _SKEIN_H_ */
int Skein_256_InitExt(Skein_256_Ctxt_t *ctx, size_t hashBitLen, u64b_t treeInfo, const u08b_t *key, size_t keyBytes)
uint_64t u64b_t
Definition: skein_port.h:25
Skein_Ctxt_Hdr_t h
Definition: skein.h:80
int Skein_512_InitExt(Skein_512_Ctxt_t *ctx, size_t hashBitLen, u64b_t treeInfo, const u08b_t *key, size_t keyBytes)
int Skein_256_Final_Pad(Skein_256_Ctxt_t *ctx, u08b_t *hashVal)
int Skein_256_Output(Skein_256_Ctxt_t *ctx, u08b_t *hashVal)
int Skein_512_Final_Pad(Skein_512_Ctxt_t *ctx, u08b_t *hashVal)
#define SKEIN1024_BLOCK_BYTES
Definition: skein.h:62
#define SKEIN_MODIFIER_WORDS
Definition: skein.h:45
int Skein_512_Init(Skein_512_Ctxt_t *ctx, size_t hashBitLen)
#define SKEIN1024_STATE_WORDS
Definition: skein.h:49
size_t hashBitLen
Definition: skein.h:66
int Skein_256_Final(Skein_256_Ctxt_t *ctx, u08b_t *hashVal)
int Skein1024_Init(Skein1024_Ctxt_t *ctx, size_t hashBitLen)
int Skein1024_Output(Skein1024_Ctxt_t *ctx, u08b_t *hashVal)
int Skein1024_Final_Pad(Skein1024_Ctxt_t *ctx, u08b_t *hashVal)
int Skein_256_Init(Skein_256_Ctxt_t *ctx, size_t hashBitLen)
#define SKEIN_512_STATE_WORDS
Definition: skein.h:48
#define SKEIN_512_BLOCK_BYTES
Definition: skein.h:61
int Skein_512_Update(Skein_512_Ctxt_t *ctx, const u08b_t *msg, size_t msgByteCnt)
int Skein_256_Update(Skein_256_Ctxt_t *ctx, const u08b_t *msg, size_t msgByteCnt)
#define SKEIN_256_STATE_WORDS
Definition: skein.h:47
Skein_Ctxt_Hdr_t h
Definition: skein.h:87
uint_8t u08b_t
Definition: skein_port.h:24
int Skein1024_Update(Skein1024_Ctxt_t *ctx, const u08b_t *msg, size_t msgByteCnt)
int Skein1024_Final(Skein1024_Ctxt_t *ctx, u08b_t *hashVal)
size_t bCnt
Definition: skein.h:67
int Skein_512_Output(Skein_512_Ctxt_t *ctx, u08b_t *hashVal)
int Skein_512_Final(Skein_512_Ctxt_t *ctx, u08b_t *hashVal)
#define SKEIN_256_BLOCK_BYTES
Definition: skein.h:60
Skein_Ctxt_Hdr_t h
Definition: skein.h:73
int Skein1024_InitExt(Skein1024_Ctxt_t *ctx, size_t hashBitLen, u64b_t treeInfo, const u08b_t *key, size_t keyBytes)