skein.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /***********************************************************************
  2. **
  3. ** Implementation of the Skein hash function.
  4. **
  5. ** Source code author: Doug Whiting, 2008.
  6. **
  7. ** This algorithm and source code is released to the public domain.
  8. **
  9. ************************************************************************/
  10. #define SKEIN_PORT_CODE /* instantiate any code in skein_port.h */
  11. #include <linux/string.h> /* get the memcpy/memset functions */
  12. #include "skein.h" /* get the Skein API definitions */
  13. #include "skein_iv.h" /* get precomputed IVs */
  14. #include "skein_block.h"
  15. /*****************************************************************/
  16. /* 256-bit Skein */
  17. /*****************************************************************/
  18. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  19. /* init the context for a straight hashing operation */
  20. int skein_256_init(struct skein_256_ctx *ctx, size_t hash_bit_len)
  21. {
  22. union {
  23. u8 b[SKEIN_256_STATE_BYTES];
  24. u64 w[SKEIN_256_STATE_WORDS];
  25. } cfg; /* config block */
  26. skein_assert_ret(hash_bit_len > 0, SKEIN_BAD_HASHLEN);
  27. ctx->h.hash_bit_len = hash_bit_len; /* output hash bit count */
  28. switch (hash_bit_len) { /* use pre-computed values, where available */
  29. case 256:
  30. memcpy(ctx->x, SKEIN_256_IV_256, sizeof(ctx->x));
  31. break;
  32. case 224:
  33. memcpy(ctx->x, SKEIN_256_IV_224, sizeof(ctx->x));
  34. break;
  35. case 160:
  36. memcpy(ctx->x, SKEIN_256_IV_160, sizeof(ctx->x));
  37. break;
  38. case 128:
  39. memcpy(ctx->x, SKEIN_256_IV_128, sizeof(ctx->x));
  40. break;
  41. default:
  42. /* here if there is no precomputed IV value available */
  43. /*
  44. * build/process the config block, type == CONFIG (could be
  45. * precomputed)
  46. */
  47. /* set tweaks: T0=0; T1=CFG | FINAL */
  48. skein_start_new_type(ctx, CFG_FINAL);
  49. /* set the schema, version */
  50. cfg.w[0] = skein_swap64(SKEIN_SCHEMA_VER);
  51. /* hash result length in bits */
  52. cfg.w[1] = skein_swap64(hash_bit_len);
  53. cfg.w[2] = skein_swap64(SKEIN_CFG_TREE_INFO_SEQUENTIAL);
  54. /* zero pad config block */
  55. memset(&cfg.w[3], 0, sizeof(cfg) - 3*sizeof(cfg.w[0]));
  56. /* compute the initial chaining values from config block */
  57. /* zero the chaining variables */
  58. memset(ctx->x, 0, sizeof(ctx->x));
  59. skein_256_process_block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
  60. break;
  61. }
  62. /* The chaining vars ctx->x are now initialized for hash_bit_len. */
  63. /* Set up to process the data message portion of the hash (default) */
  64. skein_start_new_type(ctx, MSG); /* T0=0, T1= MSG type */
  65. return SKEIN_SUCCESS;
  66. }
  67. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  68. /* init the context for a MAC and/or tree hash operation */
  69. /* [identical to skein_256_init() when key_bytes == 0 && \
  70. * tree_info == SKEIN_CFG_TREE_INFO_SEQUENTIAL] */
  71. int skein_256_init_ext(struct skein_256_ctx *ctx, size_t hash_bit_len,
  72. u64 tree_info, const u8 *key, size_t key_bytes)
  73. {
  74. union {
  75. u8 b[SKEIN_256_STATE_BYTES];
  76. u64 w[SKEIN_256_STATE_WORDS];
  77. } cfg; /* config block */
  78. skein_assert_ret(hash_bit_len > 0, SKEIN_BAD_HASHLEN);
  79. skein_assert_ret(key_bytes == 0 || key != NULL, SKEIN_FAIL);
  80. /* compute the initial chaining values ctx->x[], based on key */
  81. if (key_bytes == 0) { /* is there a key? */
  82. /* no key: use all zeroes as key for config block */
  83. memset(ctx->x, 0, sizeof(ctx->x));
  84. } else { /* here to pre-process a key */
  85. skein_assert(sizeof(cfg.b) >= sizeof(ctx->x));
  86. /* do a mini-Init right here */
  87. /* set output hash bit count = state size */
  88. ctx->h.hash_bit_len = 8*sizeof(ctx->x);
  89. /* set tweaks: T0 = 0; T1 = KEY type */
  90. skein_start_new_type(ctx, KEY);
  91. /* zero the initial chaining variables */
  92. memset(ctx->x, 0, sizeof(ctx->x));
  93. /* hash the key */
  94. skein_256_update(ctx, key, key_bytes);
  95. /* put result into cfg.b[] */
  96. skein_256_final_pad(ctx, cfg.b);
  97. /* copy over into ctx->x[] */
  98. memcpy(ctx->x, cfg.b, sizeof(cfg.b));
  99. }
  100. /*
  101. * build/process the config block, type == CONFIG (could be
  102. * precomputed for each key)
  103. */
  104. /* output hash bit count */
  105. ctx->h.hash_bit_len = hash_bit_len;
  106. skein_start_new_type(ctx, CFG_FINAL);
  107. /* pre-pad cfg.w[] with zeroes */
  108. memset(&cfg.w, 0, sizeof(cfg.w));
  109. cfg.w[0] = skein_swap64(SKEIN_SCHEMA_VER);
  110. /* hash result length in bits */
  111. cfg.w[1] = skein_swap64(hash_bit_len);
  112. /* tree hash config info (or SKEIN_CFG_TREE_INFO_SEQUENTIAL) */
  113. cfg.w[2] = skein_swap64(tree_info);
  114. skein_show_key(256, &ctx->h, key, key_bytes);
  115. /* compute the initial chaining values from config block */
  116. skein_256_process_block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
  117. /* The chaining vars ctx->x are now initialized */
  118. /* Set up to process the data message portion of the hash (default) */
  119. skein_start_new_type(ctx, MSG);
  120. return SKEIN_SUCCESS;
  121. }
  122. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  123. /* process the input bytes */
  124. int skein_256_update(struct skein_256_ctx *ctx, const u8 *msg,
  125. size_t msg_byte_cnt)
  126. {
  127. size_t n;
  128. /* catch uninitialized context */
  129. skein_assert_ret(ctx->h.b_cnt <= SKEIN_256_BLOCK_BYTES, SKEIN_FAIL);
  130. /* process full blocks, if any */
  131. if (msg_byte_cnt + ctx->h.b_cnt > SKEIN_256_BLOCK_BYTES) {
  132. /* finish up any buffered message data */
  133. if (ctx->h.b_cnt) {
  134. /* # bytes free in buffer b[] */
  135. n = SKEIN_256_BLOCK_BYTES - ctx->h.b_cnt;
  136. if (n) {
  137. /* check on our logic here */
  138. skein_assert(n < msg_byte_cnt);
  139. memcpy(&ctx->b[ctx->h.b_cnt], msg, n);
  140. msg_byte_cnt -= n;
  141. msg += n;
  142. ctx->h.b_cnt += n;
  143. }
  144. skein_assert(ctx->h.b_cnt == SKEIN_256_BLOCK_BYTES);
  145. skein_256_process_block(ctx, ctx->b, 1,
  146. SKEIN_256_BLOCK_BYTES);
  147. ctx->h.b_cnt = 0;
  148. }
  149. /*
  150. * now process any remaining full blocks, directly from input
  151. * message data
  152. */
  153. if (msg_byte_cnt > SKEIN_256_BLOCK_BYTES) {
  154. /* number of full blocks to process */
  155. n = (msg_byte_cnt-1) / SKEIN_256_BLOCK_BYTES;
  156. skein_256_process_block(ctx, msg, n,
  157. SKEIN_256_BLOCK_BYTES);
  158. msg_byte_cnt -= n * SKEIN_256_BLOCK_BYTES;
  159. msg += n * SKEIN_256_BLOCK_BYTES;
  160. }
  161. skein_assert(ctx->h.b_cnt == 0);
  162. }
  163. /* copy any remaining source message data bytes into b[] */
  164. if (msg_byte_cnt) {
  165. skein_assert(msg_byte_cnt + ctx->h.b_cnt <=
  166. SKEIN_256_BLOCK_BYTES);
  167. memcpy(&ctx->b[ctx->h.b_cnt], msg, msg_byte_cnt);
  168. ctx->h.b_cnt += msg_byte_cnt;
  169. }
  170. return SKEIN_SUCCESS;
  171. }
  172. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  173. /* finalize the hash computation and output the result */
  174. int skein_256_final(struct skein_256_ctx *ctx, u8 *hash_val)
  175. {
  176. size_t i, n, byte_cnt;
  177. u64 x[SKEIN_256_STATE_WORDS];
  178. /* catch uninitialized context */
  179. skein_assert_ret(ctx->h.b_cnt <= SKEIN_256_BLOCK_BYTES, SKEIN_FAIL);
  180. /* tag as the final block */
  181. ctx->h.tweak[1] |= SKEIN_T1_FLAG_FINAL;
  182. /* zero pad b[] if necessary */
  183. if (ctx->h.b_cnt < SKEIN_256_BLOCK_BYTES)
  184. memset(&ctx->b[ctx->h.b_cnt], 0,
  185. SKEIN_256_BLOCK_BYTES - ctx->h.b_cnt);
  186. /* process the final block */
  187. skein_256_process_block(ctx, ctx->b, 1, ctx->h.b_cnt);
  188. /* now output the result */
  189. /* total number of output bytes */
  190. byte_cnt = (ctx->h.hash_bit_len + 7) >> 3;
  191. /* run Threefish in "counter mode" to generate output */
  192. /* zero out b[], so it can hold the counter */
  193. memset(ctx->b, 0, sizeof(ctx->b));
  194. /* keep a local copy of counter mode "key" */
  195. memcpy(x, ctx->x, sizeof(x));
  196. for (i = 0; i*SKEIN_256_BLOCK_BYTES < byte_cnt; i++) {
  197. /* build the counter block */
  198. ((u64 *)ctx->b)[0] = skein_swap64((u64) i);
  199. skein_start_new_type(ctx, OUT_FINAL);
  200. /* run "counter mode" */
  201. skein_256_process_block(ctx, ctx->b, 1, sizeof(u64));
  202. /* number of output bytes left to go */
  203. n = byte_cnt - i*SKEIN_256_BLOCK_BYTES;
  204. if (n >= SKEIN_256_BLOCK_BYTES)
  205. n = SKEIN_256_BLOCK_BYTES;
  206. /* "output" the ctr mode bytes */
  207. skein_put64_lsb_first(hash_val+i*SKEIN_256_BLOCK_BYTES, ctx->x,
  208. n);
  209. skein_show_final(256, &ctx->h, n,
  210. hash_val+i*SKEIN_256_BLOCK_BYTES);
  211. /* restore the counter mode key for next time */
  212. memcpy(ctx->x, x, sizeof(x));
  213. }
  214. return SKEIN_SUCCESS;
  215. }
  216. /*****************************************************************/
  217. /* 512-bit Skein */
  218. /*****************************************************************/
  219. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  220. /* init the context for a straight hashing operation */
  221. int skein_512_init(struct skein_512_ctx *ctx, size_t hash_bit_len)
  222. {
  223. union {
  224. u8 b[SKEIN_512_STATE_BYTES];
  225. u64 w[SKEIN_512_STATE_WORDS];
  226. } cfg; /* config block */
  227. skein_assert_ret(hash_bit_len > 0, SKEIN_BAD_HASHLEN);
  228. ctx->h.hash_bit_len = hash_bit_len; /* output hash bit count */
  229. switch (hash_bit_len) { /* use pre-computed values, where available */
  230. case 512:
  231. memcpy(ctx->x, SKEIN_512_IV_512, sizeof(ctx->x));
  232. break;
  233. case 384:
  234. memcpy(ctx->x, SKEIN_512_IV_384, sizeof(ctx->x));
  235. break;
  236. case 256:
  237. memcpy(ctx->x, SKEIN_512_IV_256, sizeof(ctx->x));
  238. break;
  239. case 224:
  240. memcpy(ctx->x, SKEIN_512_IV_224, sizeof(ctx->x));
  241. break;
  242. default:
  243. /* here if there is no precomputed IV value available */
  244. /*
  245. * build/process the config block, type == CONFIG (could be
  246. * precomputed)
  247. */
  248. /* set tweaks: T0=0; T1=CFG | FINAL */
  249. skein_start_new_type(ctx, CFG_FINAL);
  250. /* set the schema, version */
  251. cfg.w[0] = skein_swap64(SKEIN_SCHEMA_VER);
  252. /* hash result length in bits */
  253. cfg.w[1] = skein_swap64(hash_bit_len);
  254. cfg.w[2] = skein_swap64(SKEIN_CFG_TREE_INFO_SEQUENTIAL);
  255. /* zero pad config block */
  256. memset(&cfg.w[3], 0, sizeof(cfg) - 3*sizeof(cfg.w[0]));
  257. /* compute the initial chaining values from config block */
  258. /* zero the chaining variables */
  259. memset(ctx->x, 0, sizeof(ctx->x));
  260. skein_512_process_block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
  261. break;
  262. }
  263. /*
  264. * The chaining vars ctx->x are now initialized for the given
  265. * hash_bit_len.
  266. */
  267. /* Set up to process the data message portion of the hash (default) */
  268. skein_start_new_type(ctx, MSG); /* T0=0, T1= MSG type */
  269. return SKEIN_SUCCESS;
  270. }
  271. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  272. /* init the context for a MAC and/or tree hash operation */
  273. /* [identical to skein_512_init() when key_bytes == 0 && \
  274. * tree_info == SKEIN_CFG_TREE_INFO_SEQUENTIAL] */
  275. int skein_512_init_ext(struct skein_512_ctx *ctx, size_t hash_bit_len,
  276. u64 tree_info, const u8 *key, size_t key_bytes)
  277. {
  278. union {
  279. u8 b[SKEIN_512_STATE_BYTES];
  280. u64 w[SKEIN_512_STATE_WORDS];
  281. } cfg; /* config block */
  282. skein_assert_ret(hash_bit_len > 0, SKEIN_BAD_HASHLEN);
  283. skein_assert_ret(key_bytes == 0 || key != NULL, SKEIN_FAIL);
  284. /* compute the initial chaining values ctx->x[], based on key */
  285. if (key_bytes == 0) { /* is there a key? */
  286. /* no key: use all zeroes as key for config block */
  287. memset(ctx->x, 0, sizeof(ctx->x));
  288. } else { /* here to pre-process a key */
  289. skein_assert(sizeof(cfg.b) >= sizeof(ctx->x));
  290. /* do a mini-Init right here */
  291. /* set output hash bit count = state size */
  292. ctx->h.hash_bit_len = 8*sizeof(ctx->x);
  293. /* set tweaks: T0 = 0; T1 = KEY type */
  294. skein_start_new_type(ctx, KEY);
  295. /* zero the initial chaining variables */
  296. memset(ctx->x, 0, sizeof(ctx->x));
  297. /* hash the key */
  298. skein_512_update(ctx, key, key_bytes);
  299. /* put result into cfg.b[] */
  300. skein_512_final_pad(ctx, cfg.b);
  301. /* copy over into ctx->x[] */
  302. memcpy(ctx->x, cfg.b, sizeof(cfg.b));
  303. }
  304. /*
  305. * build/process the config block, type == CONFIG (could be
  306. * precomputed for each key)
  307. */
  308. ctx->h.hash_bit_len = hash_bit_len; /* output hash bit count */
  309. skein_start_new_type(ctx, CFG_FINAL);
  310. /* pre-pad cfg.w[] with zeroes */
  311. memset(&cfg.w, 0, sizeof(cfg.w));
  312. cfg.w[0] = skein_swap64(SKEIN_SCHEMA_VER);
  313. /* hash result length in bits */
  314. cfg.w[1] = skein_swap64(hash_bit_len);
  315. /* tree hash config info (or SKEIN_CFG_TREE_INFO_SEQUENTIAL) */
  316. cfg.w[2] = skein_swap64(tree_info);
  317. skein_show_key(512, &ctx->h, key, key_bytes);
  318. /* compute the initial chaining values from config block */
  319. skein_512_process_block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
  320. /* The chaining vars ctx->x are now initialized */
  321. /* Set up to process the data message portion of the hash (default) */
  322. skein_start_new_type(ctx, MSG);
  323. return SKEIN_SUCCESS;
  324. }
  325. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  326. /* process the input bytes */
  327. int skein_512_update(struct skein_512_ctx *ctx, const u8 *msg,
  328. size_t msg_byte_cnt)
  329. {
  330. size_t n;
  331. /* catch uninitialized context */
  332. skein_assert_ret(ctx->h.b_cnt <= SKEIN_512_BLOCK_BYTES, SKEIN_FAIL);
  333. /* process full blocks, if any */
  334. if (msg_byte_cnt + ctx->h.b_cnt > SKEIN_512_BLOCK_BYTES) {
  335. /* finish up any buffered message data */
  336. if (ctx->h.b_cnt) {
  337. /* # bytes free in buffer b[] */
  338. n = SKEIN_512_BLOCK_BYTES - ctx->h.b_cnt;
  339. if (n) {
  340. /* check on our logic here */
  341. skein_assert(n < msg_byte_cnt);
  342. memcpy(&ctx->b[ctx->h.b_cnt], msg, n);
  343. msg_byte_cnt -= n;
  344. msg += n;
  345. ctx->h.b_cnt += n;
  346. }
  347. skein_assert(ctx->h.b_cnt == SKEIN_512_BLOCK_BYTES);
  348. skein_512_process_block(ctx, ctx->b, 1,
  349. SKEIN_512_BLOCK_BYTES);
  350. ctx->h.b_cnt = 0;
  351. }
  352. /*
  353. * now process any remaining full blocks, directly from input
  354. * message data
  355. */
  356. if (msg_byte_cnt > SKEIN_512_BLOCK_BYTES) {
  357. /* number of full blocks to process */
  358. n = (msg_byte_cnt-1) / SKEIN_512_BLOCK_BYTES;
  359. skein_512_process_block(ctx, msg, n,
  360. SKEIN_512_BLOCK_BYTES);
  361. msg_byte_cnt -= n * SKEIN_512_BLOCK_BYTES;
  362. msg += n * SKEIN_512_BLOCK_BYTES;
  363. }
  364. skein_assert(ctx->h.b_cnt == 0);
  365. }
  366. /* copy any remaining source message data bytes into b[] */
  367. if (msg_byte_cnt) {
  368. skein_assert(msg_byte_cnt + ctx->h.b_cnt <=
  369. SKEIN_512_BLOCK_BYTES);
  370. memcpy(&ctx->b[ctx->h.b_cnt], msg, msg_byte_cnt);
  371. ctx->h.b_cnt += msg_byte_cnt;
  372. }
  373. return SKEIN_SUCCESS;
  374. }
  375. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  376. /* finalize the hash computation and output the result */
  377. int skein_512_final(struct skein_512_ctx *ctx, u8 *hash_val)
  378. {
  379. size_t i, n, byte_cnt;
  380. u64 x[SKEIN_512_STATE_WORDS];
  381. /* catch uninitialized context */
  382. skein_assert_ret(ctx->h.b_cnt <= SKEIN_512_BLOCK_BYTES, SKEIN_FAIL);
  383. /* tag as the final block */
  384. ctx->h.tweak[1] |= SKEIN_T1_FLAG_FINAL;
  385. /* zero pad b[] if necessary */
  386. if (ctx->h.b_cnt < SKEIN_512_BLOCK_BYTES)
  387. memset(&ctx->b[ctx->h.b_cnt], 0,
  388. SKEIN_512_BLOCK_BYTES - ctx->h.b_cnt);
  389. /* process the final block */
  390. skein_512_process_block(ctx, ctx->b, 1, ctx->h.b_cnt);
  391. /* now output the result */
  392. /* total number of output bytes */
  393. byte_cnt = (ctx->h.hash_bit_len + 7) >> 3;
  394. /* run Threefish in "counter mode" to generate output */
  395. /* zero out b[], so it can hold the counter */
  396. memset(ctx->b, 0, sizeof(ctx->b));
  397. /* keep a local copy of counter mode "key" */
  398. memcpy(x, ctx->x, sizeof(x));
  399. for (i = 0; i*SKEIN_512_BLOCK_BYTES < byte_cnt; i++) {
  400. /* build the counter block */
  401. ((u64 *)ctx->b)[0] = skein_swap64((u64) i);
  402. skein_start_new_type(ctx, OUT_FINAL);
  403. /* run "counter mode" */
  404. skein_512_process_block(ctx, ctx->b, 1, sizeof(u64));
  405. /* number of output bytes left to go */
  406. n = byte_cnt - i*SKEIN_512_BLOCK_BYTES;
  407. if (n >= SKEIN_512_BLOCK_BYTES)
  408. n = SKEIN_512_BLOCK_BYTES;
  409. /* "output" the ctr mode bytes */
  410. skein_put64_lsb_first(hash_val+i*SKEIN_512_BLOCK_BYTES, ctx->x,
  411. n);
  412. skein_show_final(512, &ctx->h, n,
  413. hash_val+i*SKEIN_512_BLOCK_BYTES);
  414. /* restore the counter mode key for next time */
  415. memcpy(ctx->x, x, sizeof(x));
  416. }
  417. return SKEIN_SUCCESS;
  418. }
  419. /*****************************************************************/
  420. /* 1024-bit Skein */
  421. /*****************************************************************/
  422. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  423. /* init the context for a straight hashing operation */
  424. int skein_1024_init(struct skein_1024_ctx *ctx, size_t hash_bit_len)
  425. {
  426. union {
  427. u8 b[SKEIN_1024_STATE_BYTES];
  428. u64 w[SKEIN_1024_STATE_WORDS];
  429. } cfg; /* config block */
  430. skein_assert_ret(hash_bit_len > 0, SKEIN_BAD_HASHLEN);
  431. ctx->h.hash_bit_len = hash_bit_len; /* output hash bit count */
  432. switch (hash_bit_len) { /* use pre-computed values, where available */
  433. case 512:
  434. memcpy(ctx->x, SKEIN_1024_IV_512, sizeof(ctx->x));
  435. break;
  436. case 384:
  437. memcpy(ctx->x, SKEIN_1024_IV_384, sizeof(ctx->x));
  438. break;
  439. case 1024:
  440. memcpy(ctx->x, SKEIN_1024_IV_1024, sizeof(ctx->x));
  441. break;
  442. default:
  443. /* here if there is no precomputed IV value available */
  444. /*
  445. * build/process the config block, type == CONFIG
  446. * (could be precomputed)
  447. */
  448. /* set tweaks: T0=0; T1=CFG | FINAL */
  449. skein_start_new_type(ctx, CFG_FINAL);
  450. /* set the schema, version */
  451. cfg.w[0] = skein_swap64(SKEIN_SCHEMA_VER);
  452. /* hash result length in bits */
  453. cfg.w[1] = skein_swap64(hash_bit_len);
  454. cfg.w[2] = skein_swap64(SKEIN_CFG_TREE_INFO_SEQUENTIAL);
  455. /* zero pad config block */
  456. memset(&cfg.w[3], 0, sizeof(cfg) - 3*sizeof(cfg.w[0]));
  457. /* compute the initial chaining values from config block */
  458. /* zero the chaining variables */
  459. memset(ctx->x, 0, sizeof(ctx->x));
  460. skein_1024_process_block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
  461. break;
  462. }
  463. /* The chaining vars ctx->x are now initialized for the hash_bit_len. */
  464. /* Set up to process the data message portion of the hash (default) */
  465. skein_start_new_type(ctx, MSG); /* T0=0, T1= MSG type */
  466. return SKEIN_SUCCESS;
  467. }
  468. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  469. /* init the context for a MAC and/or tree hash operation */
  470. /* [identical to skein_1024_init() when key_bytes == 0 && \
  471. * tree_info == SKEIN_CFG_TREE_INFO_SEQUENTIAL] */
  472. int skein_1024_init_ext(struct skein_1024_ctx *ctx, size_t hash_bit_len,
  473. u64 tree_info, const u8 *key, size_t key_bytes)
  474. {
  475. union {
  476. u8 b[SKEIN_1024_STATE_BYTES];
  477. u64 w[SKEIN_1024_STATE_WORDS];
  478. } cfg; /* config block */
  479. skein_assert_ret(hash_bit_len > 0, SKEIN_BAD_HASHLEN);
  480. skein_assert_ret(key_bytes == 0 || key != NULL, SKEIN_FAIL);
  481. /* compute the initial chaining values ctx->x[], based on key */
  482. if (key_bytes == 0) { /* is there a key? */
  483. /* no key: use all zeroes as key for config block */
  484. memset(ctx->x, 0, sizeof(ctx->x));
  485. } else { /* here to pre-process a key */
  486. skein_assert(sizeof(cfg.b) >= sizeof(ctx->x));
  487. /* do a mini-Init right here */
  488. /* set output hash bit count = state size */
  489. ctx->h.hash_bit_len = 8*sizeof(ctx->x);
  490. /* set tweaks: T0 = 0; T1 = KEY type */
  491. skein_start_new_type(ctx, KEY);
  492. /* zero the initial chaining variables */
  493. memset(ctx->x, 0, sizeof(ctx->x));
  494. /* hash the key */
  495. skein_1024_update(ctx, key, key_bytes);
  496. /* put result into cfg.b[] */
  497. skein_1024_final_pad(ctx, cfg.b);
  498. /* copy over into ctx->x[] */
  499. memcpy(ctx->x, cfg.b, sizeof(cfg.b));
  500. }
  501. /*
  502. * build/process the config block, type == CONFIG (could be
  503. * precomputed for each key)
  504. */
  505. /* output hash bit count */
  506. ctx->h.hash_bit_len = hash_bit_len;
  507. skein_start_new_type(ctx, CFG_FINAL);
  508. /* pre-pad cfg.w[] with zeroes */
  509. memset(&cfg.w, 0, sizeof(cfg.w));
  510. cfg.w[0] = skein_swap64(SKEIN_SCHEMA_VER);
  511. /* hash result length in bits */
  512. cfg.w[1] = skein_swap64(hash_bit_len);
  513. /* tree hash config info (or SKEIN_CFG_TREE_INFO_SEQUENTIAL) */
  514. cfg.w[2] = skein_swap64(tree_info);
  515. skein_show_key(1024, &ctx->h, key, key_bytes);
  516. /* compute the initial chaining values from config block */
  517. skein_1024_process_block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
  518. /* The chaining vars ctx->x are now initialized */
  519. /* Set up to process the data message portion of the hash (default) */
  520. skein_start_new_type(ctx, MSG);
  521. return SKEIN_SUCCESS;
  522. }
  523. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  524. /* process the input bytes */
  525. int skein_1024_update(struct skein_1024_ctx *ctx, const u8 *msg,
  526. size_t msg_byte_cnt)
  527. {
  528. size_t n;
  529. /* catch uninitialized context */
  530. skein_assert_ret(ctx->h.b_cnt <= SKEIN_1024_BLOCK_BYTES, SKEIN_FAIL);
  531. /* process full blocks, if any */
  532. if (msg_byte_cnt + ctx->h.b_cnt > SKEIN_1024_BLOCK_BYTES) {
  533. /* finish up any buffered message data */
  534. if (ctx->h.b_cnt) {
  535. /* # bytes free in buffer b[] */
  536. n = SKEIN_1024_BLOCK_BYTES - ctx->h.b_cnt;
  537. if (n) {
  538. /* check on our logic here */
  539. skein_assert(n < msg_byte_cnt);
  540. memcpy(&ctx->b[ctx->h.b_cnt], msg, n);
  541. msg_byte_cnt -= n;
  542. msg += n;
  543. ctx->h.b_cnt += n;
  544. }
  545. skein_assert(ctx->h.b_cnt == SKEIN_1024_BLOCK_BYTES);
  546. skein_1024_process_block(ctx, ctx->b, 1,
  547. SKEIN_1024_BLOCK_BYTES);
  548. ctx->h.b_cnt = 0;
  549. }
  550. /*
  551. * now process any remaining full blocks, directly from input
  552. * message data
  553. */
  554. if (msg_byte_cnt > SKEIN_1024_BLOCK_BYTES) {
  555. /* number of full blocks to process */
  556. n = (msg_byte_cnt-1) / SKEIN_1024_BLOCK_BYTES;
  557. skein_1024_process_block(ctx, msg, n,
  558. SKEIN_1024_BLOCK_BYTES);
  559. msg_byte_cnt -= n * SKEIN_1024_BLOCK_BYTES;
  560. msg += n * SKEIN_1024_BLOCK_BYTES;
  561. }
  562. skein_assert(ctx->h.b_cnt == 0);
  563. }
  564. /* copy any remaining source message data bytes into b[] */
  565. if (msg_byte_cnt) {
  566. skein_assert(msg_byte_cnt + ctx->h.b_cnt <=
  567. SKEIN_1024_BLOCK_BYTES);
  568. memcpy(&ctx->b[ctx->h.b_cnt], msg, msg_byte_cnt);
  569. ctx->h.b_cnt += msg_byte_cnt;
  570. }
  571. return SKEIN_SUCCESS;
  572. }
  573. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  574. /* finalize the hash computation and output the result */
  575. int skein_1024_final(struct skein_1024_ctx *ctx, u8 *hash_val)
  576. {
  577. size_t i, n, byte_cnt;
  578. u64 x[SKEIN_1024_STATE_WORDS];
  579. /* catch uninitialized context */
  580. skein_assert_ret(ctx->h.b_cnt <= SKEIN_1024_BLOCK_BYTES, SKEIN_FAIL);
  581. /* tag as the final block */
  582. ctx->h.tweak[1] |= SKEIN_T1_FLAG_FINAL;
  583. /* zero pad b[] if necessary */
  584. if (ctx->h.b_cnt < SKEIN_1024_BLOCK_BYTES)
  585. memset(&ctx->b[ctx->h.b_cnt], 0,
  586. SKEIN_1024_BLOCK_BYTES - ctx->h.b_cnt);
  587. /* process the final block */
  588. skein_1024_process_block(ctx, ctx->b, 1, ctx->h.b_cnt);
  589. /* now output the result */
  590. /* total number of output bytes */
  591. byte_cnt = (ctx->h.hash_bit_len + 7) >> 3;
  592. /* run Threefish in "counter mode" to generate output */
  593. /* zero out b[], so it can hold the counter */
  594. memset(ctx->b, 0, sizeof(ctx->b));
  595. /* keep a local copy of counter mode "key" */
  596. memcpy(x, ctx->x, sizeof(x));
  597. for (i = 0; i*SKEIN_1024_BLOCK_BYTES < byte_cnt; i++) {
  598. /* build the counter block */
  599. ((u64 *)ctx->b)[0] = skein_swap64((u64) i);
  600. skein_start_new_type(ctx, OUT_FINAL);
  601. /* run "counter mode" */
  602. skein_1024_process_block(ctx, ctx->b, 1, sizeof(u64));
  603. /* number of output bytes left to go */
  604. n = byte_cnt - i*SKEIN_1024_BLOCK_BYTES;
  605. if (n >= SKEIN_1024_BLOCK_BYTES)
  606. n = SKEIN_1024_BLOCK_BYTES;
  607. /* "output" the ctr mode bytes */
  608. skein_put64_lsb_first(hash_val+i*SKEIN_1024_BLOCK_BYTES, ctx->x,
  609. n);
  610. skein_show_final(1024, &ctx->h, n,
  611. hash_val+i*SKEIN_1024_BLOCK_BYTES);
  612. /* restore the counter mode key for next time */
  613. memcpy(ctx->x, x, sizeof(x));
  614. }
  615. return SKEIN_SUCCESS;
  616. }
  617. /**************** Functions to support MAC/tree hashing ***************/
  618. /* (this code is identical for Optimized and Reference versions) */
  619. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  620. /* finalize the hash computation and output the block, no OUTPUT stage */
  621. int skein_256_final_pad(struct skein_256_ctx *ctx, u8 *hash_val)
  622. {
  623. /* catch uninitialized context */
  624. skein_assert_ret(ctx->h.b_cnt <= SKEIN_256_BLOCK_BYTES, SKEIN_FAIL);
  625. /* tag as the final block */
  626. ctx->h.tweak[1] |= SKEIN_T1_FLAG_FINAL;
  627. /* zero pad b[] if necessary */
  628. if (ctx->h.b_cnt < SKEIN_256_BLOCK_BYTES)
  629. memset(&ctx->b[ctx->h.b_cnt], 0,
  630. SKEIN_256_BLOCK_BYTES - ctx->h.b_cnt);
  631. /* process the final block */
  632. skein_256_process_block(ctx, ctx->b, 1, ctx->h.b_cnt);
  633. /* "output" the state bytes */
  634. skein_put64_lsb_first(hash_val, ctx->x, SKEIN_256_BLOCK_BYTES);
  635. return SKEIN_SUCCESS;
  636. }
  637. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  638. /* finalize the hash computation and output the block, no OUTPUT stage */
  639. int skein_512_final_pad(struct skein_512_ctx *ctx, u8 *hash_val)
  640. {
  641. /* catch uninitialized context */
  642. skein_assert_ret(ctx->h.b_cnt <= SKEIN_512_BLOCK_BYTES, SKEIN_FAIL);
  643. /* tag as the final block */
  644. ctx->h.tweak[1] |= SKEIN_T1_FLAG_FINAL;
  645. /* zero pad b[] if necessary */
  646. if (ctx->h.b_cnt < SKEIN_512_BLOCK_BYTES)
  647. memset(&ctx->b[ctx->h.b_cnt], 0,
  648. SKEIN_512_BLOCK_BYTES - ctx->h.b_cnt);
  649. /* process the final block */
  650. skein_512_process_block(ctx, ctx->b, 1, ctx->h.b_cnt);
  651. /* "output" the state bytes */
  652. skein_put64_lsb_first(hash_val, ctx->x, SKEIN_512_BLOCK_BYTES);
  653. return SKEIN_SUCCESS;
  654. }
  655. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  656. /* finalize the hash computation and output the block, no OUTPUT stage */
  657. int skein_1024_final_pad(struct skein_1024_ctx *ctx, u8 *hash_val)
  658. {
  659. /* catch uninitialized context */
  660. skein_assert_ret(ctx->h.b_cnt <= SKEIN_1024_BLOCK_BYTES, SKEIN_FAIL);
  661. /* tag as the final block */
  662. ctx->h.tweak[1] |= SKEIN_T1_FLAG_FINAL;
  663. /* zero pad b[] if necessary */
  664. if (ctx->h.b_cnt < SKEIN_1024_BLOCK_BYTES)
  665. memset(&ctx->b[ctx->h.b_cnt], 0,
  666. SKEIN_1024_BLOCK_BYTES - ctx->h.b_cnt);
  667. /* process the final block */
  668. skein_1024_process_block(ctx, ctx->b, 1, ctx->h.b_cnt);
  669. /* "output" the state bytes */
  670. skein_put64_lsb_first(hash_val, ctx->x, SKEIN_1024_BLOCK_BYTES);
  671. return SKEIN_SUCCESS;
  672. }
  673. #if SKEIN_TREE_HASH
  674. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  675. /* just do the OUTPUT stage */
  676. int skein_256_output(struct skein_256_ctx *ctx, u8 *hash_val)
  677. {
  678. size_t i, n, byte_cnt;
  679. u64 x[SKEIN_256_STATE_WORDS];
  680. /* catch uninitialized context */
  681. skein_assert_ret(ctx->h.b_cnt <= SKEIN_256_BLOCK_BYTES, SKEIN_FAIL);
  682. /* now output the result */
  683. /* total number of output bytes */
  684. byte_cnt = (ctx->h.hash_bit_len + 7) >> 3;
  685. /* run Threefish in "counter mode" to generate output */
  686. /* zero out b[], so it can hold the counter */
  687. memset(ctx->b, 0, sizeof(ctx->b));
  688. /* keep a local copy of counter mode "key" */
  689. memcpy(x, ctx->x, sizeof(x));
  690. for (i = 0; i*SKEIN_256_BLOCK_BYTES < byte_cnt; i++) {
  691. /* build the counter block */
  692. ((u64 *)ctx->b)[0] = skein_swap64((u64) i);
  693. skein_start_new_type(ctx, OUT_FINAL);
  694. /* run "counter mode" */
  695. skein_256_process_block(ctx, ctx->b, 1, sizeof(u64));
  696. /* number of output bytes left to go */
  697. n = byte_cnt - i*SKEIN_256_BLOCK_BYTES;
  698. if (n >= SKEIN_256_BLOCK_BYTES)
  699. n = SKEIN_256_BLOCK_BYTES;
  700. /* "output" the ctr mode bytes */
  701. skein_put64_lsb_first(hash_val+i*SKEIN_256_BLOCK_BYTES, ctx->x,
  702. n);
  703. skein_show_final(256, &ctx->h, n,
  704. hash_val+i*SKEIN_256_BLOCK_BYTES);
  705. /* restore the counter mode key for next time */
  706. memcpy(ctx->x, x, sizeof(x));
  707. }
  708. return SKEIN_SUCCESS;
  709. }
  710. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  711. /* just do the OUTPUT stage */
  712. int skein_512_output(struct skein_512_ctx *ctx, u8 *hash_val)
  713. {
  714. size_t i, n, byte_cnt;
  715. u64 x[SKEIN_512_STATE_WORDS];
  716. /* catch uninitialized context */
  717. skein_assert_ret(ctx->h.b_cnt <= SKEIN_512_BLOCK_BYTES, SKEIN_FAIL);
  718. /* now output the result */
  719. /* total number of output bytes */
  720. byte_cnt = (ctx->h.hash_bit_len + 7) >> 3;
  721. /* run Threefish in "counter mode" to generate output */
  722. /* zero out b[], so it can hold the counter */
  723. memset(ctx->b, 0, sizeof(ctx->b));
  724. /* keep a local copy of counter mode "key" */
  725. memcpy(x, ctx->x, sizeof(x));
  726. for (i = 0; i*SKEIN_512_BLOCK_BYTES < byte_cnt; i++) {
  727. /* build the counter block */
  728. ((u64 *)ctx->b)[0] = skein_swap64((u64) i);
  729. skein_start_new_type(ctx, OUT_FINAL);
  730. /* run "counter mode" */
  731. skein_512_process_block(ctx, ctx->b, 1, sizeof(u64));
  732. /* number of output bytes left to go */
  733. n = byte_cnt - i*SKEIN_512_BLOCK_BYTES;
  734. if (n >= SKEIN_512_BLOCK_BYTES)
  735. n = SKEIN_512_BLOCK_BYTES;
  736. /* "output" the ctr mode bytes */
  737. skein_put64_lsb_first(hash_val+i*SKEIN_512_BLOCK_BYTES, ctx->x,
  738. n);
  739. skein_show_final(256, &ctx->h, n,
  740. hash_val+i*SKEIN_512_BLOCK_BYTES);
  741. /* restore the counter mode key for next time */
  742. memcpy(ctx->x, x, sizeof(x));
  743. }
  744. return SKEIN_SUCCESS;
  745. }
  746. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  747. /* just do the OUTPUT stage */
  748. int skein_1024_output(struct skein_1024_ctx *ctx, u8 *hash_val)
  749. {
  750. size_t i, n, byte_cnt;
  751. u64 x[SKEIN_1024_STATE_WORDS];
  752. /* catch uninitialized context */
  753. skein_assert_ret(ctx->h.b_cnt <= SKEIN_1024_BLOCK_BYTES, SKEIN_FAIL);
  754. /* now output the result */
  755. /* total number of output bytes */
  756. byte_cnt = (ctx->h.hash_bit_len + 7) >> 3;
  757. /* run Threefish in "counter mode" to generate output */
  758. /* zero out b[], so it can hold the counter */
  759. memset(ctx->b, 0, sizeof(ctx->b));
  760. /* keep a local copy of counter mode "key" */
  761. memcpy(x, ctx->x, sizeof(x));
  762. for (i = 0; i*SKEIN_1024_BLOCK_BYTES < byte_cnt; i++) {
  763. /* build the counter block */
  764. ((u64 *)ctx->b)[0] = skein_swap64((u64) i);
  765. skein_start_new_type(ctx, OUT_FINAL);
  766. /* run "counter mode" */
  767. skein_1024_process_block(ctx, ctx->b, 1, sizeof(u64));
  768. /* number of output bytes left to go */
  769. n = byte_cnt - i*SKEIN_1024_BLOCK_BYTES;
  770. if (n >= SKEIN_1024_BLOCK_BYTES)
  771. n = SKEIN_1024_BLOCK_BYTES;
  772. /* "output" the ctr mode bytes */
  773. skein_put64_lsb_first(hash_val+i*SKEIN_1024_BLOCK_BYTES, ctx->x,
  774. n);
  775. skein_show_final(256, &ctx->h, n,
  776. hash_val+i*SKEIN_1024_BLOCK_BYTES);
  777. /* restore the counter mode key for next time */
  778. memcpy(ctx->x, x, sizeof(x));
  779. }
  780. return SKEIN_SUCCESS;
  781. }
  782. #endif