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