1 #ifndef _SKEIN_H_ 2 #define _SKEIN_H_ 1 3 /* 4 ************************************************************************** 5 * 6 * Interface declarations and internal definitions for Skein hashing. 7 * 8 * Source code author: Doug Whiting, 2008. 9 * 10 * This algorithm and source code is released to the public domain. 11 * 12 ************************************************************************** 13 * 14 * The following compile-time switches may be defined to control some 15 * tradeoffs between speed, code size, error checking, and security. 16 * 17 * The "default" note explains what happens when the switch is not defined. 18 * 19 * SKEIN_ERR_CHECK -- how error checking is handled inside Skein 20 * code. If not defined, most error checking 21 * is disabled (for performance). Otherwise, 22 * the switch value is interpreted as: 23 * 0: use assert() to flag errors 24 * 1: return SKEIN_FAIL to flag errors 25 * 26 ************************************************************************** 27 */ 28 29 /*Skein digest sizes for crypto api*/ 30 #define SKEIN256_DIGEST_BIT_SIZE 256 31 #define SKEIN512_DIGEST_BIT_SIZE 512 32 #define SKEIN1024_DIGEST_BIT_SIZE 1024 33 34 /* below two prototype assume we are handed aligned data */ 35 #define skein_put64_lsb_first(dst08, src64, b_cnt) memcpy(dst08, src64, b_cnt) 36 #define skein_get64_lsb_first(dst64, src08, w_cnt) \ 37 memcpy(dst64, src08, 8 * (w_cnt)) 38 #define skein_swap64(w64) (w64) 39 40 enum { 41 SKEIN_SUCCESS = 0, /* return codes from Skein calls */ 42 SKEIN_FAIL = 1, 43 SKEIN_BAD_HASHLEN = 2 44 }; 45 46 #define SKEIN_MODIFIER_WORDS 2 /* number of modifier (tweak) words */ 47 48 #define SKEIN_256_STATE_WORDS 4 49 #define SKEIN_512_STATE_WORDS 8 50 #define SKEIN_1024_STATE_WORDS 16 51 #define SKEIN_MAX_STATE_WORDS 16 52 53 #define SKEIN_256_STATE_BYTES (8 * SKEIN_256_STATE_WORDS) 54 #define SKEIN_512_STATE_BYTES (8 * SKEIN_512_STATE_WORDS) 55 #define SKEIN_1024_STATE_BYTES (8 * SKEIN_1024_STATE_WORDS) 56 57 #define SKEIN_256_STATE_BITS (64 * SKEIN_256_STATE_WORDS) 58 #define SKEIN_512_STATE_BITS (64 * SKEIN_512_STATE_WORDS) 59 #define SKEIN_1024_STATE_BITS (64 * SKEIN_1024_STATE_WORDS) 60 61 #define SKEIN_256_BLOCK_BYTES (8 * SKEIN_256_STATE_WORDS) 62 #define SKEIN_512_BLOCK_BYTES (8 * SKEIN_512_STATE_WORDS) 63 #define SKEIN_1024_BLOCK_BYTES (8 * SKEIN_1024_STATE_WORDS) 64 65 struct skein_ctx_hdr { 66 size_t hash_bit_len; /* size of hash result, in bits */ 67 size_t b_cnt; /* current byte count in buffer b[] */ 68 u64 tweak[SKEIN_MODIFIER_WORDS]; /* tweak[0]=byte cnt, tweak[1]=flags */ 69 }; 70 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) */ 75 }; 76 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) */ 81 }; 82 83 struct skein_1024_ctx { /* 1024-bit Skein hash context structure */ 84 struct skein_ctx_hdr h; /* common header context variables */ 85 u64 x[SKEIN_1024_STATE_WORDS]; /* chaining variables */ 86 u8 b[SKEIN_1024_BLOCK_BYTES]; /* partial block buf (8-byte aligned) */ 87 }; 88 89 /* Skein APIs for (incremental) "straight hashing" */ 90 int skein_256_init(struct skein_256_ctx *ctx, size_t hash_bit_len); 91 int skein_512_init(struct skein_512_ctx *ctx, size_t hash_bit_len); 92 int skein_1024_init(struct skein_1024_ctx *ctx, size_t hash_bit_len); 93 94 int skein_256_update(struct skein_256_ctx *ctx, const u8 *msg, 95 size_t msg_byte_cnt); 96 int skein_512_update(struct skein_512_ctx *ctx, const u8 *msg, 97 size_t msg_byte_cnt); 98 int skein_1024_update(struct skein_1024_ctx *ctx, const u8 *msg, 99 size_t msg_byte_cnt); 100 101 int skein_256_final(struct skein_256_ctx *ctx, u8 *hash_val); 102 int skein_512_final(struct skein_512_ctx *ctx, u8 *hash_val); 103 int skein_1024_final(struct skein_1024_ctx *ctx, u8 *hash_val); 104 105 /* 106 * Skein APIs for "extended" initialization: MAC keys, tree hashing. 107 * After an init_ext() call, just use update/final calls as with init(). 108 * 109 * Notes: Same parameters as _init() calls, plus tree_info/key/key_bytes. 110 * When key_bytes == 0 and tree_info == SKEIN_SEQUENTIAL, 111 * the results of init_ext() are identical to calling init(). 112 * The function init() may be called once to "precompute" the IV for 113 * a given hash_bit_len value, then by saving a copy of the context 114 * the IV computation may be avoided in later calls. 115 * Similarly, the function init_ext() 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_init_ext(struct skein_256_ctx *ctx, size_t hash_bit_len, 120 u64 tree_info, const u8 *key, size_t key_bytes); 121 int skein_512_init_ext(struct skein_512_ctx *ctx, size_t hash_bit_len, 122 u64 tree_info, const u8 *key, size_t key_bytes); 123 int skein_1024_init_ext(struct skein_1024_ctx *ctx, size_t hash_bit_len, 124 u64 tree_info, const u8 *key, size_t key_bytes); 125 126 /* 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 130 */ 131 int skein_256_final_pad(struct skein_256_ctx *ctx, u8 *hash_val); 132 int skein_512_final_pad(struct skein_512_ctx *ctx, u8 *hash_val); 133 int skein_1024_final_pad(struct skein_1024_ctx *ctx, u8 *hash_val); 134 135 #ifndef SKEIN_TREE_HASH 136 #define SKEIN_TREE_HASH (1) 137 #endif 138 #if SKEIN_TREE_HASH 139 int skein_256_output(struct skein_256_ctx *ctx, u8 *hash_val); 140 int skein_512_output(struct skein_512_ctx *ctx, u8 *hash_val); 141 int skein_1024_output(struct skein_1024_ctx *ctx, u8 *hash_val); 142 #endif 143 144 /* 145 ***************************************************************** 146 * "Internal" Skein definitions 147 * -- not needed for sequential hashing API, but will be 148 * helpful for other uses of Skein (e.g., tree hash mode). 149 * -- included here so that they can be shared between 150 * reference and optimized code. 151 ***************************************************************** 152 */ 153 154 /* tweak word tweak[1]: bit field starting positions */ 155 #define SKEIN_T1_BIT(BIT) ((BIT) - 64) /* second word */ 156 157 #define SKEIN_T1_POS_TREE_LVL SKEIN_T1_BIT(112) /* 112..118 hash tree level */ 158 #define SKEIN_T1_POS_BIT_PAD SKEIN_T1_BIT(119) /* 119 part. final in byte */ 159 #define SKEIN_T1_POS_BLK_TYPE SKEIN_T1_BIT(120) /* 120..125 type field `*/ 160 #define SKEIN_T1_POS_FIRST SKEIN_T1_BIT(126) /* 126 first blk flag */ 161 #define SKEIN_T1_POS_FINAL SKEIN_T1_BIT(127) /* 127 final blk flag */ 162 163 /* tweak word tweak[1]: flag bit definition(s) */ 164 #define SKEIN_T1_FLAG_FIRST (((u64)1) << SKEIN_T1_POS_FIRST) 165 #define SKEIN_T1_FLAG_FINAL (((u64)1) << SKEIN_T1_POS_FINAL) 166 #define SKEIN_T1_FLAG_BIT_PAD (((u64)1) << SKEIN_T1_POS_BIT_PAD) 167 168 /* tweak word tweak[1]: tree level bit field mask */ 169 #define SKEIN_T1_TREE_LVL_MASK (((u64)0x7F) << SKEIN_T1_POS_TREE_LVL) 170 #define SKEIN_T1_TREE_LEVEL(n) (((u64)(n)) << SKEIN_T1_POS_TREE_LVL) 171 172 /* tweak word tweak[1]: block type field */ 173 #define SKEIN_BLK_TYPE_KEY (0) /* key, for MAC and KDF */ 174 #define SKEIN_BLK_TYPE_CFG (4) /* configuration block */ 175 #define SKEIN_BLK_TYPE_PERS (8) /* personalization string */ 176 #define SKEIN_BLK_TYPE_PK (12) /* pubkey (for digital sigs) */ 177 #define SKEIN_BLK_TYPE_KDF (16) /* key identifier for KDF */ 178 #define SKEIN_BLK_TYPE_NONCE (20) /* nonce for PRNG */ 179 #define SKEIN_BLK_TYPE_MSG (48) /* message processing */ 180 #define SKEIN_BLK_TYPE_OUT (63) /* output stage */ 181 #define SKEIN_BLK_TYPE_MASK (63) /* bit field mask */ 182 183 #define SKEIN_T1_BLK_TYPE(T) (((u64)(SKEIN_BLK_TYPE_##T)) << \ 184 SKEIN_T1_POS_BLK_TYPE) 185 #define SKEIN_T1_BLK_TYPE_KEY SKEIN_T1_BLK_TYPE(KEY) /* for MAC and KDF */ 186 #define SKEIN_T1_BLK_TYPE_CFG SKEIN_T1_BLK_TYPE(CFG) /* config block */ 187 #define SKEIN_T1_BLK_TYPE_PERS SKEIN_T1_BLK_TYPE(PERS) /* personalization */ 188 #define SKEIN_T1_BLK_TYPE_PK SKEIN_T1_BLK_TYPE(PK) /* pubkey (for sigs) */ 189 #define SKEIN_T1_BLK_TYPE_KDF SKEIN_T1_BLK_TYPE(KDF) /* key ident for KDF */ 190 #define SKEIN_T1_BLK_TYPE_NONCE SKEIN_T1_BLK_TYPE(NONCE)/* nonce for PRNG */ 191 #define SKEIN_T1_BLK_TYPE_MSG SKEIN_T1_BLK_TYPE(MSG) /* message processing */ 192 #define SKEIN_T1_BLK_TYPE_OUT SKEIN_T1_BLK_TYPE(OUT) /* output stage */ 193 #define SKEIN_T1_BLK_TYPE_MASK SKEIN_T1_BLK_TYPE(MASK) /* field bit mask */ 194 195 #define SKEIN_T1_BLK_TYPE_CFG_FINAL (SKEIN_T1_BLK_TYPE_CFG | \ 196 SKEIN_T1_FLAG_FINAL) 197 #define SKEIN_T1_BLK_TYPE_OUT_FINAL (SKEIN_T1_BLK_TYPE_OUT | \ 198 SKEIN_T1_FLAG_FINAL) 199 200 #define SKEIN_VERSION (1) 201 202 #ifndef SKEIN_ID_STRING_LE /* allow compile-time personalization */ 203 #define SKEIN_ID_STRING_LE (0x33414853) /* "SHA3" (little-endian)*/ 204 #endif 205 206 #define SKEIN_MK_64(hi32, lo32) ((lo32) + (((u64)(hi32)) << 32)) 207 #define SKEIN_SCHEMA_VER SKEIN_MK_64(SKEIN_VERSION, SKEIN_ID_STRING_LE) 208 #define SKEIN_KS_PARITY SKEIN_MK_64(0x1BD11BDA, 0xA9FC1A22) 209 210 #define SKEIN_CFG_STR_LEN (4 * 8) 211 212 /* bit field definitions in config block tree_info word */ 213 #define SKEIN_CFG_TREE_LEAF_SIZE_POS (0) 214 #define SKEIN_CFG_TREE_NODE_SIZE_POS (8) 215 #define SKEIN_CFG_TREE_MAX_LEVEL_POS (16) 216 217 #define SKEIN_CFG_TREE_LEAF_SIZE_MSK (((u64)0xFF) << \ 218 SKEIN_CFG_TREE_LEAF_SIZE_POS) 219 #define SKEIN_CFG_TREE_NODE_SIZE_MSK (((u64)0xFF) << \ 220 SKEIN_CFG_TREE_NODE_SIZE_POS) 221 #define SKEIN_CFG_TREE_MAX_LEVEL_MSK (((u64)0xFF) << \ 222 SKEIN_CFG_TREE_MAX_LEVEL_POS) 223 224 #define SKEIN_CFG_TREE_INFO(leaf, node, max_lvl) \ 225 ((((u64)(leaf)) << SKEIN_CFG_TREE_LEAF_SIZE_POS) | \ 226 (((u64)(node)) << SKEIN_CFG_TREE_NODE_SIZE_POS) | \ 227 (((u64)(max_lvl)) << SKEIN_CFG_TREE_MAX_LEVEL_POS)) 228 229 /* use as tree_info in InitExt() call for sequential processing */ 230 #define SKEIN_CFG_TREE_INFO_SEQUENTIAL SKEIN_CFG_TREE_INFO(0, 0, 0) 231 232 /* 233 * Skein macros for getting/setting tweak words, etc. 234 * These are useful for partial input bytes, hash tree init/update, etc. 235 */ 236 #define skein_get_tweak(ctx_ptr, TWK_NUM) ((ctx_ptr)->h.tweak[TWK_NUM]) 237 #define skein_set_tweak(ctx_ptr, TWK_NUM, t_val) { \ 238 (ctx_ptr)->h.tweak[TWK_NUM] = (t_val); \ 239 } 240 241 #define skein_get_T0(ctx_ptr) skein_get_tweak(ctx_ptr, 0) 242 #define skein_get_T1(ctx_ptr) skein_get_tweak(ctx_ptr, 1) 243 #define skein_set_T0(ctx_ptr, T0) skein_set_tweak(ctx_ptr, 0, T0) 244 #define skein_set_T1(ctx_ptr, T1) skein_set_tweak(ctx_ptr, 1, T1) 245 246 /* set both tweak words at once */ 247 #define skein_set_T0_T1(ctx_ptr, T0, T1) \ 248 { \ 249 skein_set_T0(ctx_ptr, (T0)); \ 250 skein_set_T1(ctx_ptr, (T1)); \ 251 } 252 253 #define skein_set_type(ctx_ptr, BLK_TYPE) \ 254 skein_set_T1(ctx_ptr, SKEIN_T1_BLK_TYPE_##BLK_TYPE) 255 256 /* 257 * setup for starting with a new type: 258 * h.tweak[0]=0; h.tweak[1] = NEW_TYPE; h.b_cnt=0; 259 */ 260 #define skein_start_new_type(ctx_ptr, BLK_TYPE) { \ 261 skein_set_T0_T1(ctx_ptr, 0, SKEIN_T1_FLAG_FIRST | \ 262 SKEIN_T1_BLK_TYPE_##BLK_TYPE); \ 263 (ctx_ptr)->h.b_cnt = 0; \ 264 } 265 266 #define skein_clear_first_flag(hdr) { \ 267 (hdr).tweak[1] &= ~SKEIN_T1_FLAG_FIRST; \ 268 } 269 #define skein_set_bit_pad_flag(hdr) { \ 270 (hdr).tweak[1] |= SKEIN_T1_FLAG_BIT_PAD; \ 271 } 272 273 #define skein_set_tree_level(hdr, height) { \ 274 (hdr).tweak[1] |= SKEIN_T1_TREE_LEVEL(height); \ 275 } 276 277 /* ignore all asserts, for performance */ 278 #define skein_assert_ret(x, ret_code) 279 #define skein_assert(x) 280 281 /* 282 ***************************************************************** 283 * Skein block function constants (shared across Ref and Opt code) 284 ***************************************************************** 285 */ 286 enum { 287 /* SKEIN_256 round rotation constants */ 288 R_256_0_0 = 14, R_256_0_1 = 16, 289 R_256_1_0 = 52, R_256_1_1 = 57, 290 R_256_2_0 = 23, R_256_2_1 = 40, 291 R_256_3_0 = 5, R_256_3_1 = 37, 292 R_256_4_0 = 25, R_256_4_1 = 33, 293 R_256_5_0 = 46, R_256_5_1 = 12, 294 R_256_6_0 = 58, R_256_6_1 = 22, 295 R_256_7_0 = 32, R_256_7_1 = 32, 296 297 /* SKEIN_512 round rotation constants */ 298 R_512_0_0 = 46, R_512_0_1 = 36, R_512_0_2 = 19, R_512_0_3 = 37, 299 R_512_1_0 = 33, R_512_1_1 = 27, R_512_1_2 = 14, R_512_1_3 = 42, 300 R_512_2_0 = 17, R_512_2_1 = 49, R_512_2_2 = 36, R_512_2_3 = 39, 301 R_512_3_0 = 44, R_512_3_1 = 9, R_512_3_2 = 54, R_512_3_3 = 56, 302 R_512_4_0 = 39, R_512_4_1 = 30, R_512_4_2 = 34, R_512_4_3 = 24, 303 R_512_5_0 = 13, R_512_5_1 = 50, R_512_5_2 = 10, R_512_5_3 = 17, 304 R_512_6_0 = 25, R_512_6_1 = 29, R_512_6_2 = 39, R_512_6_3 = 43, 305 R_512_7_0 = 8, R_512_7_1 = 35, R_512_7_2 = 56, R_512_7_3 = 22, 306 307 /* SKEIN_1024 round rotation constants */ 308 R1024_0_0 = 24, R1024_0_1 = 13, R1024_0_2 = 8, R1024_0_3 = 47, 309 R1024_0_4 = 8, R1024_0_5 = 17, R1024_0_6 = 22, R1024_0_7 = 37, 310 R1024_1_0 = 38, R1024_1_1 = 19, R1024_1_2 = 10, R1024_1_3 = 55, 311 R1024_1_4 = 49, R1024_1_5 = 18, R1024_1_6 = 23, R1024_1_7 = 52, 312 R1024_2_0 = 33, R1024_2_1 = 4, R1024_2_2 = 51, R1024_2_3 = 13, 313 R1024_2_4 = 34, R1024_2_5 = 41, R1024_2_6 = 59, R1024_2_7 = 17, 314 R1024_3_0 = 5, R1024_3_1 = 20, R1024_3_2 = 48, R1024_3_3 = 41, 315 R1024_3_4 = 47, R1024_3_5 = 28, R1024_3_6 = 16, R1024_3_7 = 25, 316 R1024_4_0 = 41, R1024_4_1 = 9, R1024_4_2 = 37, R1024_4_3 = 31, 317 R1024_4_4 = 12, R1024_4_5 = 47, R1024_4_6 = 44, R1024_4_7 = 30, 318 R1024_5_0 = 16, R1024_5_1 = 34, R1024_5_2 = 56, R1024_5_3 = 51, 319 R1024_5_4 = 4, R1024_5_5 = 53, R1024_5_6 = 42, R1024_5_7 = 41, 320 R1024_6_0 = 31, R1024_6_1 = 44, R1024_6_2 = 47, R1024_6_3 = 46, 321 R1024_6_4 = 19, R1024_6_5 = 42, R1024_6_6 = 44, R1024_6_7 = 25, 322 R1024_7_0 = 9, R1024_7_1 = 48, R1024_7_2 = 35, R1024_7_3 = 52, 323 R1024_7_4 = 23, R1024_7_5 = 31, R1024_7_6 = 37, R1024_7_7 = 20 324 }; 325 326 #ifndef SKEIN_ROUNDS 327 #define SKEIN_256_ROUNDS_TOTAL (72) /* # rounds for diff block sizes */ 328 #define SKEIN_512_ROUNDS_TOTAL (72) 329 #define SKEIN_1024_ROUNDS_TOTAL (80) 330 #else /* allow command-line define in range 8*(5..14) */ 331 #define SKEIN_256_ROUNDS_TOTAL (8 * ((((SKEIN_ROUNDS / 100) + 5) % 10) + 5)) 332 #define SKEIN_512_ROUNDS_TOTAL (8 * ((((SKEIN_ROUNDS / 10) + 5) % 10) + 5)) 333 #define SKEIN_1024_ROUNDS_TOTAL (8 * ((((SKEIN_ROUNDS) + 5) % 10) + 5)) 334 #endif 335 336 #endif /* ifndef _SKEIN_H_ */ 337