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 
31 #include <stddef.h>                          /* get size_t definition */
32 #include <dieharder/skein_port.h>            /* get platform-specific definitions */
33 
34 enum
35     {
36     SKEIN_SUCCESS         =      0,          /* return codes from Skein calls */
37     SKEIN_FAIL            =      1,
38     SKEIN_BAD_HASHLEN     =      2
39     };
40 
41 #define  SKEIN_MODIFIER_WORDS  ( 2)          /* number of modifier (tweak) words */
42 
43 #define  SKEIN_512_STATE_WORDS ( 8)
44 #define  SKEIN_MAX_STATE_WORDS (16)
45 
46 #define  SKEIN_512_STATE_BYTES ( 8*SKEIN_512_STATE_WORDS)
47 
48 #define  SKEIN_512_STATE_BITS  (64*SKEIN_512_STATE_WORDS)
49 
50 #define  SKEIN_512_BLOCK_BYTES ( 8*SKEIN_512_STATE_WORDS)
51 
52 typedef struct
53     {
54     size_t  hashBitLen;                      /* size of hash result, in bits */
55     size_t  bCnt;                            /* current byte count in buffer b[] */
56     u64b_t  T[SKEIN_MODIFIER_WORDS];         /* tweak words: T[0]=byte cnt, T[1]=flags */
57     } Skein_Ctxt_Hdr_t;
58 
59 typedef struct                               /*  512-bit Skein hash context structure */
60     {
61     Skein_Ctxt_Hdr_t h;                      /* common header context variables */
62     u64b_t  X[SKEIN_512_STATE_WORDS];        /* chaining variables */
63     u08b_t  b[SKEIN_512_BLOCK_BYTES];        /* partial block buffer (8-byte aligned) */
64     } Skein_512_Ctxt_t;
65 
66 typedef struct {
67 	u64b_t T[SKEIN_MODIFIER_WORDS];		// Tweak
68 	u64b_t Key[SKEIN_512_STATE_WORDS];  // Key
69 } Threefish_512_Ctxt_t;
70 
71 void Threefish_512_Process_Blocks(Threefish_512_Ctxt_t *ctx, const u08b_t *input,
72 		void *output, size_t blkCnt);
73 
74 void Threefish_512_Process_Blocks64(Threefish_512_Ctxt_t *ctx, const u08b_t *input,
75 		void *output, size_t blkCnt);
76 
77 /*   Skein APIs for (incremental) "straight hashing" */
78 int  Skein_512_Init  (Skein_512_Ctxt_t *ctx, size_t hashBitLen);
79 
80 int  Skein_512_Update(Skein_512_Ctxt_t *ctx, const u08b_t *msg, size_t msgByteCnt);
81 
82 int  Skein_512_Final (Skein_512_Ctxt_t *ctx, u08b_t * hashVal);
83 
84 /*
85 **   Skein APIs for "extended" initialization: MAC keys, tree hashing.
86 **   After an InitExt() call, just use Update/Final calls as with Init().
87 **
88 **   Notes: Same parameters as _Init() calls, plus treeInfo/key/keyBytes.
89 **          When keyBytes == 0 and treeInfo == SKEIN_SEQUENTIAL,
90 **              the results of InitExt() are identical to calling Init().
91 **          The function Init() may be called once to "precompute" the IV for
92 **              a given hashBitLen value, then by saving a copy of the context
93 **              the IV computation may be avoided in later calls.
94 **          Similarly, the function InitExt() may be called once per MAC key
95 **              to precompute the MAC IV, then a copy of the context saved and
96 **              reused for each new MAC computation.
97 **/
98 int  Skein_512_InitExt(Skein_512_Ctxt_t *ctx, size_t hashBitLen, u64b_t treeInfo, const u08b_t *key, size_t keyBytes);
99 
100 /*
101 **   Skein APIs for tree hash:
102 **		Final_Pad:  pad, do final block, but no OUTPUT type
103 **		Output:     do just the output stage
104 */
105 #ifndef SKEIN_TREE_HASH
106 #define SKEIN_TREE_HASH (1)
107 #endif
108 #if  SKEIN_TREE_HASH
109 int  Skein_512_Final_Pad(Skein_512_Ctxt_t *ctx, u08b_t * hashVal);
110 
111 int  Skein_512_Output   (Skein_512_Ctxt_t *ctx, u08b_t * hashVal);
112 #endif
113 
114 /*****************************************************************
115 ** "Internal" Skein definitions
116 **    -- not needed for sequential hashing API, but will be
117 **           helpful for other uses of Skein (e.g., tree hash mode).
118 **    -- included here so that they can be shared between
119 **           reference and optimized code.
120 ******************************************************************/
121 
122 /* tweak word T[1]: bit field starting positions */
123 #define SKEIN_T1_BIT(BIT)       ((BIT) - 64)            /* offset 64 because it's the second word  */
124 
125 #define SKEIN_T1_POS_TREE_LVL   SKEIN_T1_BIT(112)       /* bits 112..118: level in hash tree       */
126 #define SKEIN_T1_POS_BIT_PAD    SKEIN_T1_BIT(119)       /* bit  119     : partial final input byte */
127 #define SKEIN_T1_POS_BLK_TYPE   SKEIN_T1_BIT(120)       /* bits 120..125: type field               */
128 #define SKEIN_T1_POS_FIRST      SKEIN_T1_BIT(126)       /* bits 126     : first block flag         */
129 #define SKEIN_T1_POS_FINAL      SKEIN_T1_BIT(127)       /* bit  127     : final block flag         */
130 
131 /* tweak word T[1]: flag bit definition(s) */
132 #define SKEIN_T1_FLAG_FIRST     (((u64b_t)  1 ) << SKEIN_T1_POS_FIRST)
133 #define SKEIN_T1_FLAG_FINAL     (((u64b_t)  1 ) << SKEIN_T1_POS_FINAL)
134 #define SKEIN_T1_FLAG_BIT_PAD   (((u64b_t)  1 ) << SKEIN_T1_POS_BIT_PAD)
135 
136 /* tweak word T[1]: tree level bit field mask */
137 #define SKEIN_T1_TREE_LVL_MASK  (((u64b_t)0x7F) << SKEIN_T1_POS_TREE_LVL)
138 #define	SKEIN_T1_TREE_LEVEL(n)  (((u64b_t) (n)) << SKEIN_T1_POS_TREE_LVL)
139 
140 /* tweak word T[1]: block type field */
141 #define SKEIN_BLK_TYPE_KEY      ( 0)                    /* key, for MAC and KDF */
142 #define SKEIN_BLK_TYPE_CFG      ( 4)                    /* configuration block */
143 #define SKEIN_BLK_TYPE_PERS     ( 8)                    /* personalization string */
144 #define SKEIN_BLK_TYPE_PK       (12)                    /* public key (for digital signature hashing) */
145 #define SKEIN_BLK_TYPE_KDF      (16)                    /* key identifier for KDF */
146 #define SKEIN_BLK_TYPE_NONCE    (20)                    /* nonce for PRNG */
147 #define SKEIN_BLK_TYPE_MSG      (48)                    /* message processing */
148 #define SKEIN_BLK_TYPE_OUT      (63)                    /* output stage */
149 #define SKEIN_BLK_TYPE_MASK     (63)                    /* bit field mask */
150 
151 #define SKEIN_T1_BLK_TYPE(T)   (((u64b_t) (SKEIN_BLK_TYPE_##T)) << SKEIN_T1_POS_BLK_TYPE)
152 #define SKEIN_T1_BLK_TYPE_KEY   SKEIN_T1_BLK_TYPE(KEY)  /* key, for MAC and KDF */
153 #define SKEIN_T1_BLK_TYPE_CFG   SKEIN_T1_BLK_TYPE(CFG)  /* configuration block */
154 #define SKEIN_T1_BLK_TYPE_PERS  SKEIN_T1_BLK_TYPE(PERS) /* personalization string */
155 #define SKEIN_T1_BLK_TYPE_PK    SKEIN_T1_BLK_TYPE(PK)   /* public key (for digital signature hashing) */
156 #define SKEIN_T1_BLK_TYPE_KDF   SKEIN_T1_BLK_TYPE(KDF)  /* key identifier for KDF */
157 #define SKEIN_T1_BLK_TYPE_NONCE SKEIN_T1_BLK_TYPE(NONCE)/* nonce for PRNG */
158 #define SKEIN_T1_BLK_TYPE_MSG   SKEIN_T1_BLK_TYPE(MSG)  /* message processing */
159 #define SKEIN_T1_BLK_TYPE_OUT   SKEIN_T1_BLK_TYPE(OUT)  /* output stage */
160 #define SKEIN_T1_BLK_TYPE_MASK  SKEIN_T1_BLK_TYPE(MASK) /* field bit mask */
161 
162 #define SKEIN_T1_BLK_TYPE_CFG_FINAL       (SKEIN_T1_BLK_TYPE_CFG | SKEIN_T1_FLAG_FINAL)
163 #define SKEIN_T1_BLK_TYPE_OUT_FINAL       (SKEIN_T1_BLK_TYPE_OUT | SKEIN_T1_FLAG_FINAL)
164 
165 #define SKEIN_VERSION           (1)
166 
167 #ifndef SKEIN_ID_STRING_LE      /* allow compile-time personalization */
168 #define SKEIN_ID_STRING_LE      (0x33414853)            /* "SHA3" (little-endian)*/
169 #endif
170 
171 #define SKEIN_MK_64(hi32,lo32)  ((lo32) + (((u64b_t) (hi32)) << 32))
172 #define SKEIN_SCHEMA_VER        SKEIN_MK_64(SKEIN_VERSION,SKEIN_ID_STRING_LE)
173 #define SKEIN_KS_PARITY         SKEIN_MK_64(0x55555555,0x55555555)
174 
175 /* bit field definitions in config block treeInfo word */
176 #define SKEIN_CFG_TREE_LEAF_SIZE_POS  ( 0)
177 #define SKEIN_CFG_TREE_NODE_SIZE_POS  ( 8)
178 #define SKEIN_CFG_TREE_MAX_LEVEL_POS  (16)
179 
180 #define SKEIN_CFG_TREE_LEAF_SIZE_MSK  ((u64b_t) 0xFF) << SKEIN_CFG_TREE_LEAF_SIZE_POS)
181 #define SKEIN_CFG_TREE_NODE_SIZE_MSK  ((u64b_t) 0xFF) << SKEIN_CFG_TREE_NODE_SIZE_POS)
182 #define SKEIN_CFG_TREE_MAX_LEVEL_MSK  ((u64b_t) 0xFF) << SKEIN_CFG_TREE_MAX_LEVEL_POS)
183 
184 #define SKEIN_CFG_TREE_INFO_SEQUENTIAL (0) /* use as treeInfo in InitExt() call for sequential processing */
185 #define SKEIN_CFG_TREE_INFO(leaf,node,maxLevel) ((u64b_t) ((leaf) | ((node) << 8) | ((maxLevel) << 16)))
186 
187 /*
188 **   Skein macros for getting/setting tweak words, etc.
189 **   These are useful for partial input bytes, hash tree init/update, etc.
190 **/
191 #define Skein_Get_Tweak(ctxPtr,TWK_NUM)         ((ctxPtr)->h.T[TWK_NUM])
192 #define Skein_Set_Tweak(ctxPtr,TWK_NUM,tVal)    {(ctxPtr)->h.T[TWK_NUM] = (tVal);}
193 
194 #define Skein_Get_T0(ctxPtr)    Skein_Get_Tweak(ctxPtr,0)
195 #define Skein_Get_T1(ctxPtr)    Skein_Get_Tweak(ctxPtr,1)
196 #define Skein_Set_T0(ctxPtr,T0) Skein_Set_Tweak(ctxPtr,0,T0)
197 #define Skein_Set_T1(ctxPtr,T1) Skein_Set_Tweak(ctxPtr,1,T1)
198 
199 /* set both tweak words at once */
200 #define Skein_Set_T0_T1(ctxPtr,T0,T1)           \
201     {                                           \
202     Skein_Set_T0(ctxPtr,(T0));                  \
203     Skein_Set_T1(ctxPtr,(T1));                  \
204     }
205 
206 #define Skein_Set_Type(ctxPtr,BLK_TYPE)         \
207     Skein_Set_T1(ctxPtr,SKEIN_T1_BLK_TYPE_##BLK_TYPE)
208 
209 /* set up for starting with a new type: h.T[0]=0; h.T[1] = NEW_TYPE; h.bCnt=0; */
210 #define Skein_Start_New_Type(ctxPtr,BLK_TYPE)   \
211     { Skein_Set_T0_T1(ctxPtr,0,SKEIN_T1_FLAG_FIRST | SKEIN_T1_BLK_TYPE_##BLK_TYPE); (ctxPtr)->h.bCnt=0; }
212 
213 #define Skein_Clear_First_Flag(hdr)	     { (hdr).T[1] &= ~SKEIN_T1_FLAG_FIRST;       }
214 #define Skein_Set_Bit_Pad_Flag(hdr)      { (hdr).T[1] |=  SKEIN_T1_FLAG_BIT_PAD;     }
215 
216 #define Skein_Set_Tree_Level(hdr,height) { (hdr).T[1] |= SKEIN_T1_TREE_LEVEL(height);}
217 
218 /*****************************************************************
219 ** "Internal" Skein definitions for debugging and error checking
220 ******************************************************************/
221 #ifdef  SKEIN_DEBUG             /* examine/display intermediate values? */
222 #include "skein_debug.h"
223 #else                           /* default is no callouts */
224 #define Skein_Show_Block(bits,ctx,X,blkPtr,wPtr,ksEvenPtr,ksOddPtr)
225 #define Skein_Show_Round(bits,ctx,r,X)
226 #define Skein_Show_R_Ptr(bits,ctx,r,X_ptr)
227 #define Skein_Show_Final(bits,ctx,cnt,outPtr)
228 #define Skein_Show_Key(bits,ctx,key,keyBytes)
229 #endif
230 
231 #ifndef SKEIN_ERR_CHECK        /* run-time checks (e.g., bad params, uninitialized context)? */
232 #define Skein_Assert(x,retCode)/* default: ignore all Asserts, for performance */
233 #define Skein_assert(x)
234 #elif   defined(SKEIN_ASSERT)
235 #include <assert.h>
236 #define Skein_Assert(x,retCode) assert(x)
237 #define Skein_assert(x)         assert(x)
238 #else
239 #include <assert.h>
240 #define Skein_Assert(x,retCode) { if (!(x)) return retCode; } /*  caller  error */
241 #define Skein_assert(x)         assert(x)                     /* internal error */
242 #endif
243 
244 /*****************************************************************
245 ** Skein block function constants (shared across Ref and Opt code)
246 ******************************************************************/
247 enum
248     {
249       /* Skein_512 round rotation constants */
250     R_512_0_0=38, R_512_0_1=30, R_512_0_2=50, R_512_0_3=53,
251     R_512_1_0=48, R_512_1_1=20, R_512_1_2=43, R_512_1_3=31,
252     R_512_2_0=34, R_512_2_1=14, R_512_2_2=15, R_512_2_3=27,
253     R_512_3_0=26, R_512_3_1=12, R_512_3_2=58, R_512_3_3= 7,
254     R_512_4_0=33, R_512_4_1=49, R_512_4_2= 8, R_512_4_3=42,
255     R_512_5_0=39, R_512_5_1=27, R_512_5_2=41, R_512_5_3=14,
256     R_512_6_0=29, R_512_6_1=26, R_512_6_2=11, R_512_6_3= 9,
257     R_512_7_0=33, R_512_7_1=51, R_512_7_2=39, R_512_7_3=35
258     };
259 
260 #ifndef SKEIN_ROUNDS
261 #define SKEIN_512_ROUNDS_TOTAL (72)
262 #else                                        /* allow command-line define in range 8*(5..14)   */
263 #define SKEIN_512_ROUNDS_TOTAL (8*((((SKEIN_ROUNDS/ 10) + 5) % 10) + 5))
264 #endif
265 
266 #endif  /* ifndef _SKEIN_H_ */
267