skein_base.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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. #include <linux/string.h> /* get the memcpy/memset functions */
  11. #include <linux/export.h>
  12. #include "skein_base.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. /*
  70. * [identical to skein_256_init() when key_bytes == 0 && \
  71. * tree_info == SKEIN_CFG_TREE_INFO_SEQUENTIAL]
  72. */
  73. int skein_256_init_ext(struct skein_256_ctx *ctx, size_t hash_bit_len,
  74. u64 tree_info, const u8 *key, size_t key_bytes)
  75. {
  76. union {
  77. u8 b[SKEIN_256_STATE_BYTES];
  78. u64 w[SKEIN_256_STATE_WORDS];
  79. } cfg; /* config block */
  80. skein_assert_ret(hash_bit_len > 0, SKEIN_BAD_HASHLEN);
  81. skein_assert_ret(key_bytes == 0 || key, SKEIN_FAIL);
  82. /* compute the initial chaining values ctx->x[], based on key */
  83. if (key_bytes == 0) { /* is there a key? */
  84. /* no key: use all zeroes as key for config block */
  85. memset(ctx->x, 0, sizeof(ctx->x));
  86. } else { /* here to pre-process a key */
  87. skein_assert(sizeof(cfg.b) >= sizeof(ctx->x));
  88. /* do a mini-Init right here */
  89. /* set output hash bit count = state size */
  90. ctx->h.hash_bit_len = 8*sizeof(ctx->x);
  91. /* set tweaks: T0 = 0; T1 = KEY type */
  92. skein_start_new_type(ctx, KEY);
  93. /* zero the initial chaining variables */
  94. memset(ctx->x, 0, sizeof(ctx->x));
  95. /* hash the key */
  96. skein_256_update(ctx, key, key_bytes);
  97. /* put result into cfg.b[] */
  98. skein_256_final_pad(ctx, cfg.b);
  99. /* copy over into ctx->x[] */
  100. memcpy(ctx->x, cfg.b, sizeof(cfg.b));
  101. }
  102. /*
  103. * build/process the config block, type == CONFIG (could be
  104. * precomputed for each key)
  105. */
  106. /* output hash bit count */
  107. ctx->h.hash_bit_len = hash_bit_len;
  108. skein_start_new_type(ctx, CFG_FINAL);
  109. /* pre-pad cfg.w[] with zeroes */
  110. memset(&cfg.w, 0, sizeof(cfg.w));
  111. cfg.w[0] = skein_swap64(SKEIN_SCHEMA_VER);
  112. /* hash result length in bits */
  113. cfg.w[1] = skein_swap64(hash_bit_len);
  114. /* tree hash config info (or SKEIN_CFG_TREE_INFO_SEQUENTIAL) */
  115. cfg.w[2] = skein_swap64(tree_info);
  116. /* compute the initial chaining values from config block */
  117. skein_256_process_block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
  118. /* The chaining vars ctx->x are now initialized */
  119. /* Set up to process the data message portion of the hash (default) */
  120. skein_start_new_type(ctx, MSG);
  121. return SKEIN_SUCCESS;
  122. }
  123. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  124. /* process the input bytes */
  125. int skein_256_update(struct skein_256_ctx *ctx, const u8 *msg,
  126. size_t msg_byte_cnt)
  127. {
  128. size_t n;
  129. /* catch uninitialized context */
  130. skein_assert_ret(ctx->h.b_cnt <= SKEIN_256_BLOCK_BYTES, SKEIN_FAIL);
  131. /* process full blocks, if any */
  132. if (msg_byte_cnt + ctx->h.b_cnt > SKEIN_256_BLOCK_BYTES) {
  133. /* finish up any buffered message data */
  134. if (ctx->h.b_cnt) {
  135. /* # bytes free in buffer b[] */
  136. n = SKEIN_256_BLOCK_BYTES - ctx->h.b_cnt;
  137. if (n) {
  138. /* check on our logic here */
  139. skein_assert(n < msg_byte_cnt);
  140. memcpy(&ctx->b[ctx->h.b_cnt], msg, n);
  141. msg_byte_cnt -= n;
  142. msg += n;
  143. ctx->h.b_cnt += n;
  144. }
  145. skein_assert(ctx->h.b_cnt == SKEIN_256_BLOCK_BYTES);
  146. skein_256_process_block(ctx, ctx->b, 1,
  147. SKEIN_256_BLOCK_BYTES);
  148. ctx->h.b_cnt = 0;
  149. }
  150. /*
  151. * now process any remaining full blocks, directly from input
  152. * message data
  153. */
  154. if (msg_byte_cnt > SKEIN_256_BLOCK_BYTES) {
  155. /* number of full blocks to process */
  156. n = (msg_byte_cnt-1) / SKEIN_256_BLOCK_BYTES;
  157. skein_256_process_block(ctx, msg, n,
  158. SKEIN_256_BLOCK_BYTES);
  159. msg_byte_cnt -= n * SKEIN_256_BLOCK_BYTES;
  160. msg += n * SKEIN_256_BLOCK_BYTES;
  161. }
  162. skein_assert(ctx->h.b_cnt == 0);
  163. }
  164. /* copy any remaining source message data bytes into b[] */
  165. if (msg_byte_cnt) {
  166. skein_assert(msg_byte_cnt + ctx->h.b_cnt <=
  167. SKEIN_256_BLOCK_BYTES);
  168. memcpy(&ctx->b[ctx->h.b_cnt], msg, msg_byte_cnt);
  169. ctx->h.b_cnt += msg_byte_cnt;
  170. }
  171. return SKEIN_SUCCESS;
  172. }
  173. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  174. /* finalize the hash computation and output the result */
  175. int skein_256_final(struct skein_256_ctx *ctx, u8 *hash_val)
  176. {
  177. size_t i, n, byte_cnt;
  178. u64 x[SKEIN_256_STATE_WORDS];
  179. /* catch uninitialized context */
  180. skein_assert_ret(ctx->h.b_cnt <= SKEIN_256_BLOCK_BYTES, SKEIN_FAIL);
  181. /* tag as the final block */
  182. ctx->h.tweak[1] |= SKEIN_T1_FLAG_FINAL;
  183. /* zero pad b[] if necessary */
  184. if (ctx->h.b_cnt < SKEIN_256_BLOCK_BYTES)
  185. memset(&ctx->b[ctx->h.b_cnt], 0,
  186. SKEIN_256_BLOCK_BYTES - ctx->h.b_cnt);
  187. /* process the final block */
  188. skein_256_process_block(ctx, ctx->b, 1, ctx->h.b_cnt);
  189. /* now output the result */
  190. /* total number of output bytes */
  191. byte_cnt = (ctx->h.hash_bit_len + 7) >> 3;
  192. /* run Threefish in "counter mode" to generate output */
  193. /* zero out b[], so it can hold the counter */
  194. memset(ctx->b, 0, sizeof(ctx->b));
  195. /* keep a local copy of counter mode "key" */
  196. memcpy(x, ctx->x, sizeof(x));
  197. for (i = 0; i*SKEIN_256_BLOCK_BYTES < byte_cnt; i++) {
  198. /* build the counter block */
  199. ((u64 *)ctx->b)[0] = skein_swap64((u64) i);
  200. skein_start_new_type(ctx, OUT_FINAL);
  201. /* run "counter mode" */
  202. skein_256_process_block(ctx, ctx->b, 1, sizeof(u64));
  203. /* number of output bytes left to go */
  204. n = byte_cnt - i*SKEIN_256_BLOCK_BYTES;
  205. if (n >= SKEIN_256_BLOCK_BYTES)
  206. n = SKEIN_256_BLOCK_BYTES;
  207. /* "output" the ctr mode bytes */
  208. skein_put64_lsb_first(hash_val+i*SKEIN_256_BLOCK_BYTES, ctx->x,
  209. n);
  210. /* restore the counter mode key for next time */
  211. memcpy(ctx->x, x, sizeof(x));
  212. }
  213. return SKEIN_SUCCESS;
  214. }
  215. /*****************************************************************/
  216. /* 512-bit Skein */
  217. /*****************************************************************/
  218. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  219. /* init the context for a straight hashing operation */
  220. int skein_512_init(struct skein_512_ctx *ctx, size_t hash_bit_len)
  221. {
  222. union {
  223. u8 b[SKEIN_512_STATE_BYTES];
  224. u64 w[SKEIN_512_STATE_WORDS];
  225. } cfg; /* config block */
  226. skein_assert_ret(hash_bit_len > 0, SKEIN_BAD_HASHLEN);
  227. ctx->h.hash_bit_len = hash_bit_len; /* output hash bit count */
  228. switch (hash_bit_len) { /* use pre-computed values, where available */
  229. case 512:
  230. memcpy(ctx->x, SKEIN_512_IV_512, sizeof(ctx->x));
  231. break;
  232. case 384:
  233. memcpy(ctx->x, SKEIN_512_IV_384, sizeof(ctx->x));
  234. break;
  235. case 256:
  236. memcpy(ctx->x, SKEIN_512_IV_256, sizeof(ctx->x));
  237. break;
  238. case 224:
  239. memcpy(ctx->x, SKEIN_512_IV_224, sizeof(ctx->x));
  240. break;
  241. default:
  242. /* here if there is no precomputed IV value available */
  243. /*
  244. * build/process the config block, type == CONFIG (could be
  245. * precomputed)
  246. */
  247. /* set tweaks: T0=0; T1=CFG | FINAL */
  248. skein_start_new_type(ctx, CFG_FINAL);
  249. /* set the schema, version */
  250. cfg.w[0] = skein_swap64(SKEIN_SCHEMA_VER);
  251. /* hash result length in bits */
  252. cfg.w[1] = skein_swap64(hash_bit_len);
  253. cfg.w[2] = skein_swap64(SKEIN_CFG_TREE_INFO_SEQUENTIAL);
  254. /* zero pad config block */
  255. memset(&cfg.w[3], 0, sizeof(cfg) - 3*sizeof(cfg.w[0]));
  256. /* compute the initial chaining values from config block */
  257. /* zero the chaining variables */
  258. memset(ctx->x, 0, sizeof(ctx->x));
  259. skein_512_process_block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
  260. break;
  261. }
  262. /*
  263. * The chaining vars ctx->x are now initialized for the given
  264. * hash_bit_len.
  265. */
  266. /* Set up to process the data message portion of the hash (default) */
  267. skein_start_new_type(ctx, MSG); /* T0=0, T1= MSG type */
  268. return SKEIN_SUCCESS;
  269. }
  270. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  271. /* init the context for a MAC and/or tree hash operation */
  272. /*
  273. * [identical to skein_512_init() when key_bytes == 0 && \
  274. * tree_info == SKEIN_CFG_TREE_INFO_SEQUENTIAL]
  275. */
  276. int skein_512_init_ext(struct skein_512_ctx *ctx, size_t hash_bit_len,
  277. u64 tree_info, const u8 *key, size_t key_bytes)
  278. {
  279. union {
  280. u8 b[SKEIN_512_STATE_BYTES];
  281. u64 w[SKEIN_512_STATE_WORDS];
  282. } cfg; /* config block */
  283. skein_assert_ret(hash_bit_len > 0, SKEIN_BAD_HASHLEN);
  284. skein_assert_ret(key_bytes == 0 || key, SKEIN_FAIL);
  285. /* compute the initial chaining values ctx->x[], based on key */
  286. if (key_bytes == 0) { /* is there a key? */
  287. /* no key: use all zeroes as key for config block */
  288. memset(ctx->x, 0, sizeof(ctx->x));
  289. } else { /* here to pre-process a key */
  290. skein_assert(sizeof(cfg.b) >= sizeof(ctx->x));
  291. /* do a mini-Init right here */
  292. /* set output hash bit count = state size */
  293. ctx->h.hash_bit_len = 8*sizeof(ctx->x);
  294. /* set tweaks: T0 = 0; T1 = KEY type */
  295. skein_start_new_type(ctx, KEY);
  296. /* zero the initial chaining variables */
  297. memset(ctx->x, 0, sizeof(ctx->x));
  298. /* hash the key */
  299. skein_512_update(ctx, key, key_bytes);
  300. /* put result into cfg.b[] */
  301. skein_512_final_pad(ctx, cfg.b);
  302. /* copy over into ctx->x[] */
  303. memcpy(ctx->x, cfg.b, sizeof(cfg.b));
  304. }
  305. /*
  306. * build/process the config block, type == CONFIG (could be
  307. * precomputed for each key)
  308. */
  309. ctx->h.hash_bit_len = hash_bit_len; /* output hash bit count */
  310. skein_start_new_type(ctx, CFG_FINAL);
  311. /* pre-pad cfg.w[] with zeroes */
  312. memset(&cfg.w, 0, sizeof(cfg.w));
  313. cfg.w[0] = skein_swap64(SKEIN_SCHEMA_VER);
  314. /* hash result length in bits */
  315. cfg.w[1] = skein_swap64(hash_bit_len);
  316. /* tree hash config info (or SKEIN_CFG_TREE_INFO_SEQUENTIAL) */
  317. cfg.w[2] = skein_swap64(tree_info);
  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. /* restore the counter mode key for next time */
  413. memcpy(ctx->x, x, sizeof(x));
  414. }
  415. return SKEIN_SUCCESS;
  416. }
  417. /*****************************************************************/
  418. /* 1024-bit Skein */
  419. /*****************************************************************/
  420. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  421. /* init the context for a straight hashing operation */
  422. int skein_1024_init(struct skein_1024_ctx *ctx, size_t hash_bit_len)
  423. {
  424. union {
  425. u8 b[SKEIN_1024_STATE_BYTES];
  426. u64 w[SKEIN_1024_STATE_WORDS];
  427. } cfg; /* config block */
  428. skein_assert_ret(hash_bit_len > 0, SKEIN_BAD_HASHLEN);
  429. ctx->h.hash_bit_len = hash_bit_len; /* output hash bit count */
  430. switch (hash_bit_len) { /* use pre-computed values, where available */
  431. case 512:
  432. memcpy(ctx->x, SKEIN_1024_IV_512, sizeof(ctx->x));
  433. break;
  434. case 384:
  435. memcpy(ctx->x, SKEIN_1024_IV_384, sizeof(ctx->x));
  436. break;
  437. case 1024:
  438. memcpy(ctx->x, SKEIN_1024_IV_1024, sizeof(ctx->x));
  439. break;
  440. default:
  441. /* here if there is no precomputed IV value available */
  442. /*
  443. * build/process the config block, type == CONFIG
  444. * (could be precomputed)
  445. */
  446. /* set tweaks: T0=0; T1=CFG | FINAL */
  447. skein_start_new_type(ctx, CFG_FINAL);
  448. /* set the schema, version */
  449. cfg.w[0] = skein_swap64(SKEIN_SCHEMA_VER);
  450. /* hash result length in bits */
  451. cfg.w[1] = skein_swap64(hash_bit_len);
  452. cfg.w[2] = skein_swap64(SKEIN_CFG_TREE_INFO_SEQUENTIAL);
  453. /* zero pad config block */
  454. memset(&cfg.w[3], 0, sizeof(cfg) - 3*sizeof(cfg.w[0]));
  455. /* compute the initial chaining values from config block */
  456. /* zero the chaining variables */
  457. memset(ctx->x, 0, sizeof(ctx->x));
  458. skein_1024_process_block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
  459. break;
  460. }
  461. /* The chaining vars ctx->x are now initialized for the hash_bit_len. */
  462. /* Set up to process the data message portion of the hash (default) */
  463. skein_start_new_type(ctx, MSG); /* T0=0, T1= MSG type */
  464. return SKEIN_SUCCESS;
  465. }
  466. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  467. /* init the context for a MAC and/or tree hash operation */
  468. /*
  469. * [identical to skein_1024_init() when key_bytes == 0 && \
  470. * tree_info == SKEIN_CFG_TREE_INFO_SEQUENTIAL]
  471. */
  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, 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. /* compute the initial chaining values from config block */
  516. skein_1024_process_block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
  517. /* The chaining vars ctx->x are now initialized */
  518. /* Set up to process the data message portion of the hash (default) */
  519. skein_start_new_type(ctx, MSG);
  520. return SKEIN_SUCCESS;
  521. }
  522. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  523. /* process the input bytes */
  524. int skein_1024_update(struct skein_1024_ctx *ctx, const u8 *msg,
  525. size_t msg_byte_cnt)
  526. {
  527. size_t n;
  528. /* catch uninitialized context */
  529. skein_assert_ret(ctx->h.b_cnt <= SKEIN_1024_BLOCK_BYTES, SKEIN_FAIL);
  530. /* process full blocks, if any */
  531. if (msg_byte_cnt + ctx->h.b_cnt > SKEIN_1024_BLOCK_BYTES) {
  532. /* finish up any buffered message data */
  533. if (ctx->h.b_cnt) {
  534. /* # bytes free in buffer b[] */
  535. n = SKEIN_1024_BLOCK_BYTES - ctx->h.b_cnt;
  536. if (n) {
  537. /* check on our logic here */
  538. skein_assert(n < msg_byte_cnt);
  539. memcpy(&ctx->b[ctx->h.b_cnt], msg, n);
  540. msg_byte_cnt -= n;
  541. msg += n;
  542. ctx->h.b_cnt += n;
  543. }
  544. skein_assert(ctx->h.b_cnt == SKEIN_1024_BLOCK_BYTES);
  545. skein_1024_process_block(ctx, ctx->b, 1,
  546. SKEIN_1024_BLOCK_BYTES);
  547. ctx->h.b_cnt = 0;
  548. }
  549. /*
  550. * now process any remaining full blocks, directly from input
  551. * message data
  552. */
  553. if (msg_byte_cnt > SKEIN_1024_BLOCK_BYTES) {
  554. /* number of full blocks to process */
  555. n = (msg_byte_cnt-1) / SKEIN_1024_BLOCK_BYTES;
  556. skein_1024_process_block(ctx, msg, n,
  557. SKEIN_1024_BLOCK_BYTES);
  558. msg_byte_cnt -= n * SKEIN_1024_BLOCK_BYTES;
  559. msg += n * SKEIN_1024_BLOCK_BYTES;
  560. }
  561. skein_assert(ctx->h.b_cnt == 0);
  562. }
  563. /* copy any remaining source message data bytes into b[] */
  564. if (msg_byte_cnt) {
  565. skein_assert(msg_byte_cnt + ctx->h.b_cnt <=
  566. SKEIN_1024_BLOCK_BYTES);
  567. memcpy(&ctx->b[ctx->h.b_cnt], msg, msg_byte_cnt);
  568. ctx->h.b_cnt += msg_byte_cnt;
  569. }
  570. return SKEIN_SUCCESS;
  571. }
  572. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  573. /* finalize the hash computation and output the result */
  574. int skein_1024_final(struct skein_1024_ctx *ctx, u8 *hash_val)
  575. {
  576. size_t i, n, byte_cnt;
  577. u64 x[SKEIN_1024_STATE_WORDS];
  578. /* catch uninitialized context */
  579. skein_assert_ret(ctx->h.b_cnt <= SKEIN_1024_BLOCK_BYTES, SKEIN_FAIL);
  580. /* tag as the final block */
  581. ctx->h.tweak[1] |= SKEIN_T1_FLAG_FINAL;
  582. /* zero pad b[] if necessary */
  583. if (ctx->h.b_cnt < SKEIN_1024_BLOCK_BYTES)
  584. memset(&ctx->b[ctx->h.b_cnt], 0,
  585. SKEIN_1024_BLOCK_BYTES - ctx->h.b_cnt);
  586. /* process the final block */
  587. skein_1024_process_block(ctx, ctx->b, 1, ctx->h.b_cnt);
  588. /* now output the result */
  589. /* total number of output bytes */
  590. byte_cnt = (ctx->h.hash_bit_len + 7) >> 3;
  591. /* run Threefish in "counter mode" to generate output */
  592. /* zero out b[], so it can hold the counter */
  593. memset(ctx->b, 0, sizeof(ctx->b));
  594. /* keep a local copy of counter mode "key" */
  595. memcpy(x, ctx->x, sizeof(x));
  596. for (i = 0; i*SKEIN_1024_BLOCK_BYTES < byte_cnt; i++) {
  597. /* build the counter block */
  598. ((u64 *)ctx->b)[0] = skein_swap64((u64) i);
  599. skein_start_new_type(ctx, OUT_FINAL);
  600. /* run "counter mode" */
  601. skein_1024_process_block(ctx, ctx->b, 1, sizeof(u64));
  602. /* number of output bytes left to go */
  603. n = byte_cnt - i*SKEIN_1024_BLOCK_BYTES;
  604. if (n >= SKEIN_1024_BLOCK_BYTES)
  605. n = SKEIN_1024_BLOCK_BYTES;
  606. /* "output" the ctr mode bytes */
  607. skein_put64_lsb_first(hash_val+i*SKEIN_1024_BLOCK_BYTES, ctx->x,
  608. n);
  609. /* restore the counter mode key for next time */
  610. memcpy(ctx->x, x, sizeof(x));
  611. }
  612. return SKEIN_SUCCESS;
  613. }
  614. /**************** Functions to support MAC/tree hashing ***************/
  615. /* (this code is identical for Optimized and Reference versions) */
  616. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  617. /* finalize the hash computation and output the block, no OUTPUT stage */
  618. int skein_256_final_pad(struct skein_256_ctx *ctx, u8 *hash_val)
  619. {
  620. /* catch uninitialized context */
  621. skein_assert_ret(ctx->h.b_cnt <= SKEIN_256_BLOCK_BYTES, SKEIN_FAIL);
  622. /* tag as the final block */
  623. ctx->h.tweak[1] |= SKEIN_T1_FLAG_FINAL;
  624. /* zero pad b[] if necessary */
  625. if (ctx->h.b_cnt < SKEIN_256_BLOCK_BYTES)
  626. memset(&ctx->b[ctx->h.b_cnt], 0,
  627. SKEIN_256_BLOCK_BYTES - ctx->h.b_cnt);
  628. /* process the final block */
  629. skein_256_process_block(ctx, ctx->b, 1, ctx->h.b_cnt);
  630. /* "output" the state bytes */
  631. skein_put64_lsb_first(hash_val, ctx->x, SKEIN_256_BLOCK_BYTES);
  632. return SKEIN_SUCCESS;
  633. }
  634. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  635. /* finalize the hash computation and output the block, no OUTPUT stage */
  636. int skein_512_final_pad(struct skein_512_ctx *ctx, u8 *hash_val)
  637. {
  638. /* catch uninitialized context */
  639. skein_assert_ret(ctx->h.b_cnt <= SKEIN_512_BLOCK_BYTES, SKEIN_FAIL);
  640. /* tag as the final block */
  641. ctx->h.tweak[1] |= SKEIN_T1_FLAG_FINAL;
  642. /* zero pad b[] if necessary */
  643. if (ctx->h.b_cnt < SKEIN_512_BLOCK_BYTES)
  644. memset(&ctx->b[ctx->h.b_cnt], 0,
  645. SKEIN_512_BLOCK_BYTES - ctx->h.b_cnt);
  646. /* process the final block */
  647. skein_512_process_block(ctx, ctx->b, 1, ctx->h.b_cnt);
  648. /* "output" the state bytes */
  649. skein_put64_lsb_first(hash_val, ctx->x, SKEIN_512_BLOCK_BYTES);
  650. return SKEIN_SUCCESS;
  651. }
  652. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  653. /* finalize the hash computation and output the block, no OUTPUT stage */
  654. int skein_1024_final_pad(struct skein_1024_ctx *ctx, u8 *hash_val)
  655. {
  656. /* catch uninitialized context */
  657. skein_assert_ret(ctx->h.b_cnt <= SKEIN_1024_BLOCK_BYTES, SKEIN_FAIL);
  658. /* tag as the final block */
  659. ctx->h.tweak[1] |= SKEIN_T1_FLAG_FINAL;
  660. /* zero pad b[] if necessary */
  661. if (ctx->h.b_cnt < SKEIN_1024_BLOCK_BYTES)
  662. memset(&ctx->b[ctx->h.b_cnt], 0,
  663. SKEIN_1024_BLOCK_BYTES - ctx->h.b_cnt);
  664. /* process the final block */
  665. skein_1024_process_block(ctx, ctx->b, 1, ctx->h.b_cnt);
  666. /* "output" the state bytes */
  667. skein_put64_lsb_first(hash_val, ctx->x, SKEIN_1024_BLOCK_BYTES);
  668. return SKEIN_SUCCESS;
  669. }
  670. #if SKEIN_TREE_HASH
  671. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  672. /* just do the OUTPUT stage */
  673. int skein_256_output(struct skein_256_ctx *ctx, u8 *hash_val)
  674. {
  675. size_t i, n, byte_cnt;
  676. u64 x[SKEIN_256_STATE_WORDS];
  677. /* catch uninitialized context */
  678. skein_assert_ret(ctx->h.b_cnt <= SKEIN_256_BLOCK_BYTES, SKEIN_FAIL);
  679. /* now output the result */
  680. /* total number of output bytes */
  681. byte_cnt = (ctx->h.hash_bit_len + 7) >> 3;
  682. /* run Threefish in "counter mode" to generate output */
  683. /* zero out b[], so it can hold the counter */
  684. memset(ctx->b, 0, sizeof(ctx->b));
  685. /* keep a local copy of counter mode "key" */
  686. memcpy(x, ctx->x, sizeof(x));
  687. for (i = 0; i*SKEIN_256_BLOCK_BYTES < byte_cnt; i++) {
  688. /* build the counter block */
  689. ((u64 *)ctx->b)[0] = skein_swap64((u64) i);
  690. skein_start_new_type(ctx, OUT_FINAL);
  691. /* run "counter mode" */
  692. skein_256_process_block(ctx, ctx->b, 1, sizeof(u64));
  693. /* number of output bytes left to go */
  694. n = byte_cnt - i*SKEIN_256_BLOCK_BYTES;
  695. if (n >= SKEIN_256_BLOCK_BYTES)
  696. n = SKEIN_256_BLOCK_BYTES;
  697. /* "output" the ctr mode bytes */
  698. skein_put64_lsb_first(hash_val+i*SKEIN_256_BLOCK_BYTES, ctx->x,
  699. n);
  700. /* restore the counter mode key for next time */
  701. memcpy(ctx->x, x, sizeof(x));
  702. }
  703. return SKEIN_SUCCESS;
  704. }
  705. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  706. /* just do the OUTPUT stage */
  707. int skein_512_output(struct skein_512_ctx *ctx, u8 *hash_val)
  708. {
  709. size_t i, n, byte_cnt;
  710. u64 x[SKEIN_512_STATE_WORDS];
  711. /* catch uninitialized context */
  712. skein_assert_ret(ctx->h.b_cnt <= SKEIN_512_BLOCK_BYTES, SKEIN_FAIL);
  713. /* now output the result */
  714. /* total number of output bytes */
  715. byte_cnt = (ctx->h.hash_bit_len + 7) >> 3;
  716. /* run Threefish in "counter mode" to generate output */
  717. /* zero out b[], so it can hold the counter */
  718. memset(ctx->b, 0, sizeof(ctx->b));
  719. /* keep a local copy of counter mode "key" */
  720. memcpy(x, ctx->x, sizeof(x));
  721. for (i = 0; i*SKEIN_512_BLOCK_BYTES < byte_cnt; i++) {
  722. /* build the counter block */
  723. ((u64 *)ctx->b)[0] = skein_swap64((u64) i);
  724. skein_start_new_type(ctx, OUT_FINAL);
  725. /* run "counter mode" */
  726. skein_512_process_block(ctx, ctx->b, 1, sizeof(u64));
  727. /* number of output bytes left to go */
  728. n = byte_cnt - i*SKEIN_512_BLOCK_BYTES;
  729. if (n >= SKEIN_512_BLOCK_BYTES)
  730. n = SKEIN_512_BLOCK_BYTES;
  731. /* "output" the ctr mode bytes */
  732. skein_put64_lsb_first(hash_val+i*SKEIN_512_BLOCK_BYTES, ctx->x,
  733. n);
  734. /* restore the counter mode key for next time */
  735. memcpy(ctx->x, x, sizeof(x));
  736. }
  737. return SKEIN_SUCCESS;
  738. }
  739. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  740. /* just do the OUTPUT stage */
  741. int skein_1024_output(struct skein_1024_ctx *ctx, u8 *hash_val)
  742. {
  743. size_t i, n, byte_cnt;
  744. u64 x[SKEIN_1024_STATE_WORDS];
  745. /* catch uninitialized context */
  746. skein_assert_ret(ctx->h.b_cnt <= SKEIN_1024_BLOCK_BYTES, SKEIN_FAIL);
  747. /* now output the result */
  748. /* total number of output bytes */
  749. byte_cnt = (ctx->h.hash_bit_len + 7) >> 3;
  750. /* run Threefish in "counter mode" to generate output */
  751. /* zero out b[], so it can hold the counter */
  752. memset(ctx->b, 0, sizeof(ctx->b));
  753. /* keep a local copy of counter mode "key" */
  754. memcpy(x, ctx->x, sizeof(x));
  755. for (i = 0; i*SKEIN_1024_BLOCK_BYTES < byte_cnt; i++) {
  756. /* build the counter block */
  757. ((u64 *)ctx->b)[0] = skein_swap64((u64) i);
  758. skein_start_new_type(ctx, OUT_FINAL);
  759. /* run "counter mode" */
  760. skein_1024_process_block(ctx, ctx->b, 1, sizeof(u64));
  761. /* number of output bytes left to go */
  762. n = byte_cnt - i*SKEIN_1024_BLOCK_BYTES;
  763. if (n >= SKEIN_1024_BLOCK_BYTES)
  764. n = SKEIN_1024_BLOCK_BYTES;
  765. /* "output" the ctr mode bytes */
  766. skein_put64_lsb_first(hash_val+i*SKEIN_1024_BLOCK_BYTES, ctx->x,
  767. n);
  768. /* restore the counter mode key for next time */
  769. memcpy(ctx->x, x, sizeof(x));
  770. }
  771. return SKEIN_SUCCESS;
  772. }
  773. #endif