skein.h 15 KB

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