1 /***********************************************************************
3 ** Implementation of the Skein hash function.
5 ** Source code author: Doug Whiting, 2008.
7 ** This algorithm and source code is released to the public domain.
9 ************************************************************************/
11 #include <linux/string.h> /* get the memcpy/memset functions */
12 #include <linux/export.h>
13 #include "skein_base.h" /* get the Skein API definitions */
14 #include "skein_iv.h" /* get precomputed IVs */
15 #include "skein_block.h"
17 /*****************************************************************/
19 /*****************************************************************/
21 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
22 /* init the context for a straight hashing operation */
23 int skein_256_init(struct skein_256_ctx *ctx, size_t hash_bit_len)
26 u8 b[SKEIN_256_STATE_BYTES];
27 u64 w[SKEIN_256_STATE_WORDS];
28 } cfg; /* config block */
30 skein_assert_ret(hash_bit_len > 0, SKEIN_BAD_HASHLEN);
31 ctx->h.hash_bit_len = hash_bit_len; /* output hash bit count */
33 switch (hash_bit_len) { /* use pre-computed values, where available */
35 memcpy(ctx->x, SKEIN_256_IV_256, sizeof(ctx->x));
38 memcpy(ctx->x, SKEIN_256_IV_224, sizeof(ctx->x));
41 memcpy(ctx->x, SKEIN_256_IV_160, sizeof(ctx->x));
44 memcpy(ctx->x, SKEIN_256_IV_128, sizeof(ctx->x));
47 /* here if there is no precomputed IV value available */
49 * build/process the config block, type == CONFIG (could be
52 /* set tweaks: T0=0; T1=CFG | FINAL */
53 skein_start_new_type(ctx, CFG_FINAL);
55 /* set the schema, version */
56 cfg.w[0] = skein_swap64(SKEIN_SCHEMA_VER);
57 /* hash result length in bits */
58 cfg.w[1] = skein_swap64(hash_bit_len);
59 cfg.w[2] = skein_swap64(SKEIN_CFG_TREE_INFO_SEQUENTIAL);
60 /* zero pad config block */
61 memset(&cfg.w[3], 0, sizeof(cfg) - 3*sizeof(cfg.w[0]));
63 /* compute the initial chaining values from config block */
64 /* zero the chaining variables */
65 memset(ctx->x, 0, sizeof(ctx->x));
66 skein_256_process_block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
69 /* The chaining vars ctx->x are now initialized for hash_bit_len. */
70 /* Set up to process the data message portion of the hash (default) */
71 skein_start_new_type(ctx, MSG); /* T0=0, T1= MSG type */
76 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
77 /* init the context for a MAC and/or tree hash operation */
79 * [identical to skein_256_init() when key_bytes == 0 && \
80 * tree_info == SKEIN_CFG_TREE_INFO_SEQUENTIAL]
82 int skein_256_init_ext(struct skein_256_ctx *ctx, size_t hash_bit_len,
83 u64 tree_info, const u8 *key, size_t key_bytes)
86 u8 b[SKEIN_256_STATE_BYTES];
87 u64 w[SKEIN_256_STATE_WORDS];
88 } cfg; /* config block */
90 skein_assert_ret(hash_bit_len > 0, SKEIN_BAD_HASHLEN);
91 skein_assert_ret(key_bytes == 0 || key, SKEIN_FAIL);
93 /* compute the initial chaining values ctx->x[], based on key */
94 if (key_bytes == 0) { /* is there a key? */
95 /* no key: use all zeroes as key for config block */
96 memset(ctx->x, 0, sizeof(ctx->x));
97 } else { /* here to pre-process a key */
98 skein_assert(sizeof(cfg.b) >= sizeof(ctx->x));
99 /* do a mini-Init right here */
100 /* set output hash bit count = state size */
101 ctx->h.hash_bit_len = 8*sizeof(ctx->x);
102 /* set tweaks: T0 = 0; T1 = KEY type */
103 skein_start_new_type(ctx, KEY);
104 /* zero the initial chaining variables */
105 memset(ctx->x, 0, sizeof(ctx->x));
107 skein_256_update(ctx, key, key_bytes);
108 /* put result into cfg.b[] */
109 skein_256_final_pad(ctx, cfg.b);
110 /* copy over into ctx->x[] */
111 memcpy(ctx->x, cfg.b, sizeof(cfg.b));
114 * build/process the config block, type == CONFIG (could be
115 * precomputed for each key)
117 /* output hash bit count */
118 ctx->h.hash_bit_len = hash_bit_len;
119 skein_start_new_type(ctx, CFG_FINAL);
121 /* pre-pad cfg.w[] with zeroes */
122 memset(&cfg.w, 0, sizeof(cfg.w));
123 cfg.w[0] = skein_swap64(SKEIN_SCHEMA_VER);
124 /* hash result length in bits */
125 cfg.w[1] = skein_swap64(hash_bit_len);
126 /* tree hash config info (or SKEIN_CFG_TREE_INFO_SEQUENTIAL) */
127 cfg.w[2] = skein_swap64(tree_info);
129 /* compute the initial chaining values from config block */
130 skein_256_process_block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
132 /* The chaining vars ctx->x are now initialized */
133 /* Set up to process the data message portion of the hash (default) */
134 skein_start_new_type(ctx, MSG);
136 return SKEIN_SUCCESS;
139 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
140 /* process the input bytes */
141 int skein_256_update(struct skein_256_ctx *ctx, const u8 *msg,
146 /* catch uninitialized context */
147 skein_assert_ret(ctx->h.b_cnt <= SKEIN_256_BLOCK_BYTES, SKEIN_FAIL);
149 /* process full blocks, if any */
150 if (msg_byte_cnt + ctx->h.b_cnt > SKEIN_256_BLOCK_BYTES) {
151 /* finish up any buffered message data */
153 /* # bytes free in buffer b[] */
154 n = SKEIN_256_BLOCK_BYTES - ctx->h.b_cnt;
156 /* check on our logic here */
157 skein_assert(n < msg_byte_cnt);
158 memcpy(&ctx->b[ctx->h.b_cnt], msg, n);
163 skein_assert(ctx->h.b_cnt == SKEIN_256_BLOCK_BYTES);
164 skein_256_process_block(ctx, ctx->b, 1,
165 SKEIN_256_BLOCK_BYTES);
169 * now process any remaining full blocks, directly from input
172 if (msg_byte_cnt > SKEIN_256_BLOCK_BYTES) {
173 /* number of full blocks to process */
174 n = (msg_byte_cnt-1) / SKEIN_256_BLOCK_BYTES;
175 skein_256_process_block(ctx, msg, n,
176 SKEIN_256_BLOCK_BYTES);
177 msg_byte_cnt -= n * SKEIN_256_BLOCK_BYTES;
178 msg += n * SKEIN_256_BLOCK_BYTES;
180 skein_assert(ctx->h.b_cnt == 0);
183 /* copy any remaining source message data bytes into b[] */
185 skein_assert(msg_byte_cnt + ctx->h.b_cnt <=
186 SKEIN_256_BLOCK_BYTES);
187 memcpy(&ctx->b[ctx->h.b_cnt], msg, msg_byte_cnt);
188 ctx->h.b_cnt += msg_byte_cnt;
191 return SKEIN_SUCCESS;
194 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
195 /* finalize the hash computation and output the result */
196 int skein_256_final(struct skein_256_ctx *ctx, u8 *hash_val)
198 size_t i, n, byte_cnt;
199 u64 x[SKEIN_256_STATE_WORDS];
200 /* catch uninitialized context */
201 skein_assert_ret(ctx->h.b_cnt <= SKEIN_256_BLOCK_BYTES, SKEIN_FAIL);
203 /* tag as the final block */
204 ctx->h.tweak[1] |= SKEIN_T1_FLAG_FINAL;
205 /* zero pad b[] if necessary */
206 if (ctx->h.b_cnt < SKEIN_256_BLOCK_BYTES)
207 memset(&ctx->b[ctx->h.b_cnt], 0,
208 SKEIN_256_BLOCK_BYTES - ctx->h.b_cnt);
210 /* process the final block */
211 skein_256_process_block(ctx, ctx->b, 1, ctx->h.b_cnt);
213 /* now output the result */
214 /* total number of output bytes */
215 byte_cnt = (ctx->h.hash_bit_len + 7) >> 3;
217 /* run Threefish in "counter mode" to generate output */
218 /* zero out b[], so it can hold the counter */
219 memset(ctx->b, 0, sizeof(ctx->b));
220 /* keep a local copy of counter mode "key" */
221 memcpy(x, ctx->x, sizeof(x));
222 for (i = 0; i*SKEIN_256_BLOCK_BYTES < byte_cnt; i++) {
223 /* build the counter block */
224 ((u64 *)ctx->b)[0] = skein_swap64((u64) i);
225 skein_start_new_type(ctx, OUT_FINAL);
226 /* run "counter mode" */
227 skein_256_process_block(ctx, ctx->b, 1, sizeof(u64));
228 /* number of output bytes left to go */
229 n = byte_cnt - i*SKEIN_256_BLOCK_BYTES;
230 if (n >= SKEIN_256_BLOCK_BYTES)
231 n = SKEIN_256_BLOCK_BYTES;
232 /* "output" the ctr mode bytes */
233 skein_put64_lsb_first(hash_val+i*SKEIN_256_BLOCK_BYTES, ctx->x,
235 /* restore the counter mode key for next time */
236 memcpy(ctx->x, x, sizeof(x));
238 return SKEIN_SUCCESS;
241 /*****************************************************************/
243 /*****************************************************************/
245 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
246 /* init the context for a straight hashing operation */
247 int skein_512_init(struct skein_512_ctx *ctx, size_t hash_bit_len)
250 u8 b[SKEIN_512_STATE_BYTES];
251 u64 w[SKEIN_512_STATE_WORDS];
252 } cfg; /* config block */
254 skein_assert_ret(hash_bit_len > 0, SKEIN_BAD_HASHLEN);
255 ctx->h.hash_bit_len = hash_bit_len; /* output hash bit count */
257 switch (hash_bit_len) { /* use pre-computed values, where available */
259 memcpy(ctx->x, SKEIN_512_IV_512, sizeof(ctx->x));
262 memcpy(ctx->x, SKEIN_512_IV_384, sizeof(ctx->x));
265 memcpy(ctx->x, SKEIN_512_IV_256, sizeof(ctx->x));
268 memcpy(ctx->x, SKEIN_512_IV_224, sizeof(ctx->x));
271 /* here if there is no precomputed IV value available */
273 * build/process the config block, type == CONFIG (could be
276 /* set tweaks: T0=0; T1=CFG | FINAL */
277 skein_start_new_type(ctx, CFG_FINAL);
279 /* set the schema, version */
280 cfg.w[0] = skein_swap64(SKEIN_SCHEMA_VER);
281 /* hash result length in bits */
282 cfg.w[1] = skein_swap64(hash_bit_len);
283 cfg.w[2] = skein_swap64(SKEIN_CFG_TREE_INFO_SEQUENTIAL);
284 /* zero pad config block */
285 memset(&cfg.w[3], 0, sizeof(cfg) - 3*sizeof(cfg.w[0]));
287 /* compute the initial chaining values from config block */
288 /* zero the chaining variables */
289 memset(ctx->x, 0, sizeof(ctx->x));
290 skein_512_process_block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
295 * The chaining vars ctx->x are now initialized for the given
298 /* Set up to process the data message portion of the hash (default) */
299 skein_start_new_type(ctx, MSG); /* T0=0, T1= MSG type */
301 return SKEIN_SUCCESS;
304 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
305 /* init the context for a MAC and/or tree hash operation */
307 * [identical to skein_512_init() when key_bytes == 0 && \
308 * tree_info == SKEIN_CFG_TREE_INFO_SEQUENTIAL]
310 int skein_512_init_ext(struct skein_512_ctx *ctx, size_t hash_bit_len,
311 u64 tree_info, const u8 *key, size_t key_bytes)
314 u8 b[SKEIN_512_STATE_BYTES];
315 u64 w[SKEIN_512_STATE_WORDS];
316 } cfg; /* config block */
318 skein_assert_ret(hash_bit_len > 0, SKEIN_BAD_HASHLEN);
319 skein_assert_ret(key_bytes == 0 || key, SKEIN_FAIL);
321 /* compute the initial chaining values ctx->x[], based on key */
322 if (key_bytes == 0) { /* is there a key? */
323 /* no key: use all zeroes as key for config block */
324 memset(ctx->x, 0, sizeof(ctx->x));
325 } else { /* here to pre-process a key */
326 skein_assert(sizeof(cfg.b) >= sizeof(ctx->x));
327 /* do a mini-Init right here */
328 /* set output hash bit count = state size */
329 ctx->h.hash_bit_len = 8*sizeof(ctx->x);
330 /* set tweaks: T0 = 0; T1 = KEY type */
331 skein_start_new_type(ctx, KEY);
332 /* zero the initial chaining variables */
333 memset(ctx->x, 0, sizeof(ctx->x));
335 skein_512_update(ctx, key, key_bytes);
336 /* put result into cfg.b[] */
337 skein_512_final_pad(ctx, cfg.b);
338 /* copy over into ctx->x[] */
339 memcpy(ctx->x, cfg.b, sizeof(cfg.b));
342 * build/process the config block, type == CONFIG (could be
343 * precomputed for each key)
345 ctx->h.hash_bit_len = hash_bit_len; /* output hash bit count */
346 skein_start_new_type(ctx, CFG_FINAL);
348 /* pre-pad cfg.w[] with zeroes */
349 memset(&cfg.w, 0, sizeof(cfg.w));
350 cfg.w[0] = skein_swap64(SKEIN_SCHEMA_VER);
351 /* hash result length in bits */
352 cfg.w[1] = skein_swap64(hash_bit_len);
353 /* tree hash config info (or SKEIN_CFG_TREE_INFO_SEQUENTIAL) */
354 cfg.w[2] = skein_swap64(tree_info);
356 /* compute the initial chaining values from config block */
357 skein_512_process_block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
359 /* The chaining vars ctx->x are now initialized */
360 /* Set up to process the data message portion of the hash (default) */
361 skein_start_new_type(ctx, MSG);
363 return SKEIN_SUCCESS;
366 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
367 /* process the input bytes */
368 int skein_512_update(struct skein_512_ctx *ctx, const u8 *msg,
373 /* catch uninitialized context */
374 skein_assert_ret(ctx->h.b_cnt <= SKEIN_512_BLOCK_BYTES, SKEIN_FAIL);
376 /* process full blocks, if any */
377 if (msg_byte_cnt + ctx->h.b_cnt > SKEIN_512_BLOCK_BYTES) {
378 /* finish up any buffered message data */
380 /* # bytes free in buffer b[] */
381 n = SKEIN_512_BLOCK_BYTES - ctx->h.b_cnt;
383 /* check on our logic here */
384 skein_assert(n < msg_byte_cnt);
385 memcpy(&ctx->b[ctx->h.b_cnt], msg, n);
390 skein_assert(ctx->h.b_cnt == SKEIN_512_BLOCK_BYTES);
391 skein_512_process_block(ctx, ctx->b, 1,
392 SKEIN_512_BLOCK_BYTES);
396 * now process any remaining full blocks, directly from input
399 if (msg_byte_cnt > SKEIN_512_BLOCK_BYTES) {
400 /* number of full blocks to process */
401 n = (msg_byte_cnt-1) / SKEIN_512_BLOCK_BYTES;
402 skein_512_process_block(ctx, msg, n,
403 SKEIN_512_BLOCK_BYTES);
404 msg_byte_cnt -= n * SKEIN_512_BLOCK_BYTES;
405 msg += n * SKEIN_512_BLOCK_BYTES;
407 skein_assert(ctx->h.b_cnt == 0);
410 /* copy any remaining source message data bytes into b[] */
412 skein_assert(msg_byte_cnt + ctx->h.b_cnt <=
413 SKEIN_512_BLOCK_BYTES);
414 memcpy(&ctx->b[ctx->h.b_cnt], msg, msg_byte_cnt);
415 ctx->h.b_cnt += msg_byte_cnt;
418 return SKEIN_SUCCESS;
421 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
422 /* finalize the hash computation and output the result */
423 int skein_512_final(struct skein_512_ctx *ctx, u8 *hash_val)
425 size_t i, n, byte_cnt;
426 u64 x[SKEIN_512_STATE_WORDS];
427 /* catch uninitialized context */
428 skein_assert_ret(ctx->h.b_cnt <= SKEIN_512_BLOCK_BYTES, SKEIN_FAIL);
430 /* tag as the final block */
431 ctx->h.tweak[1] |= SKEIN_T1_FLAG_FINAL;
432 /* zero pad b[] if necessary */
433 if (ctx->h.b_cnt < SKEIN_512_BLOCK_BYTES)
434 memset(&ctx->b[ctx->h.b_cnt], 0,
435 SKEIN_512_BLOCK_BYTES - ctx->h.b_cnt);
437 /* process the final block */
438 skein_512_process_block(ctx, ctx->b, 1, ctx->h.b_cnt);
440 /* now output the result */
441 /* total number of output bytes */
442 byte_cnt = (ctx->h.hash_bit_len + 7) >> 3;
444 /* run Threefish in "counter mode" to generate output */
445 /* zero out b[], so it can hold the counter */
446 memset(ctx->b, 0, sizeof(ctx->b));
447 /* keep a local copy of counter mode "key" */
448 memcpy(x, ctx->x, sizeof(x));
449 for (i = 0; i*SKEIN_512_BLOCK_BYTES < byte_cnt; i++) {
450 /* build the counter block */
451 ((u64 *)ctx->b)[0] = skein_swap64((u64) i);
452 skein_start_new_type(ctx, OUT_FINAL);
453 /* run "counter mode" */
454 skein_512_process_block(ctx, ctx->b, 1, sizeof(u64));
455 /* number of output bytes left to go */
456 n = byte_cnt - i*SKEIN_512_BLOCK_BYTES;
457 if (n >= SKEIN_512_BLOCK_BYTES)
458 n = SKEIN_512_BLOCK_BYTES;
459 /* "output" the ctr mode bytes */
460 skein_put64_lsb_first(hash_val+i*SKEIN_512_BLOCK_BYTES, ctx->x,
462 /* restore the counter mode key for next time */
463 memcpy(ctx->x, x, sizeof(x));
465 return SKEIN_SUCCESS;
468 /*****************************************************************/
470 /*****************************************************************/
472 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
473 /* init the context for a straight hashing operation */
474 int skein_1024_init(struct skein_1024_ctx *ctx, size_t hash_bit_len)
477 u8 b[SKEIN_1024_STATE_BYTES];
478 u64 w[SKEIN_1024_STATE_WORDS];
479 } cfg; /* config block */
481 skein_assert_ret(hash_bit_len > 0, SKEIN_BAD_HASHLEN);
482 ctx->h.hash_bit_len = hash_bit_len; /* output hash bit count */
484 switch (hash_bit_len) { /* use pre-computed values, where available */
486 memcpy(ctx->x, SKEIN_1024_IV_512, sizeof(ctx->x));
489 memcpy(ctx->x, SKEIN_1024_IV_384, sizeof(ctx->x));
492 memcpy(ctx->x, SKEIN_1024_IV_1024, sizeof(ctx->x));
495 /* here if there is no precomputed IV value available */
497 * build/process the config block, type == CONFIG
498 * (could be precomputed)
500 /* set tweaks: T0=0; T1=CFG | FINAL */
501 skein_start_new_type(ctx, CFG_FINAL);
503 /* set the schema, version */
504 cfg.w[0] = skein_swap64(SKEIN_SCHEMA_VER);
505 /* hash result length in bits */
506 cfg.w[1] = skein_swap64(hash_bit_len);
507 cfg.w[2] = skein_swap64(SKEIN_CFG_TREE_INFO_SEQUENTIAL);
508 /* zero pad config block */
509 memset(&cfg.w[3], 0, sizeof(cfg) - 3*sizeof(cfg.w[0]));
511 /* compute the initial chaining values from config block */
512 /* zero the chaining variables */
513 memset(ctx->x, 0, sizeof(ctx->x));
514 skein_1024_process_block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
518 /* The chaining vars ctx->x are now initialized for the hash_bit_len. */
519 /* Set up to process the data message portion of the hash (default) */
520 skein_start_new_type(ctx, MSG); /* T0=0, T1= MSG type */
522 return SKEIN_SUCCESS;
525 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
526 /* init the context for a MAC and/or tree hash operation */
528 * [identical to skein_1024_init() when key_bytes == 0 && \
529 * tree_info == SKEIN_CFG_TREE_INFO_SEQUENTIAL]
531 int skein_1024_init_ext(struct skein_1024_ctx *ctx, size_t hash_bit_len,
532 u64 tree_info, const u8 *key, size_t key_bytes)
535 u8 b[SKEIN_1024_STATE_BYTES];
536 u64 w[SKEIN_1024_STATE_WORDS];
537 } cfg; /* config block */
539 skein_assert_ret(hash_bit_len > 0, SKEIN_BAD_HASHLEN);
540 skein_assert_ret(key_bytes == 0 || key, SKEIN_FAIL);
542 /* compute the initial chaining values ctx->x[], based on key */
543 if (key_bytes == 0) { /* is there a key? */
544 /* no key: use all zeroes as key for config block */
545 memset(ctx->x, 0, sizeof(ctx->x));
546 } else { /* here to pre-process a key */
547 skein_assert(sizeof(cfg.b) >= sizeof(ctx->x));
548 /* do a mini-Init right here */
549 /* set output hash bit count = state size */
550 ctx->h.hash_bit_len = 8*sizeof(ctx->x);
551 /* set tweaks: T0 = 0; T1 = KEY type */
552 skein_start_new_type(ctx, KEY);
553 /* zero the initial chaining variables */
554 memset(ctx->x, 0, sizeof(ctx->x));
556 skein_1024_update(ctx, key, key_bytes);
557 /* put result into cfg.b[] */
558 skein_1024_final_pad(ctx, cfg.b);
559 /* copy over into ctx->x[] */
560 memcpy(ctx->x, cfg.b, sizeof(cfg.b));
563 * build/process the config block, type == CONFIG (could be
564 * precomputed for each key)
566 /* output hash bit count */
567 ctx->h.hash_bit_len = hash_bit_len;
568 skein_start_new_type(ctx, CFG_FINAL);
570 /* pre-pad cfg.w[] with zeroes */
571 memset(&cfg.w, 0, sizeof(cfg.w));
572 cfg.w[0] = skein_swap64(SKEIN_SCHEMA_VER);
573 /* hash result length in bits */
574 cfg.w[1] = skein_swap64(hash_bit_len);
575 /* tree hash config info (or SKEIN_CFG_TREE_INFO_SEQUENTIAL) */
576 cfg.w[2] = skein_swap64(tree_info);
578 /* compute the initial chaining values from config block */
579 skein_1024_process_block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
581 /* The chaining vars ctx->x are now initialized */
582 /* Set up to process the data message portion of the hash (default) */
583 skein_start_new_type(ctx, MSG);
585 return SKEIN_SUCCESS;
588 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
589 /* process the input bytes */
590 int skein_1024_update(struct skein_1024_ctx *ctx, const u8 *msg,
595 /* catch uninitialized context */
596 skein_assert_ret(ctx->h.b_cnt <= SKEIN_1024_BLOCK_BYTES, SKEIN_FAIL);
598 /* process full blocks, if any */
599 if (msg_byte_cnt + ctx->h.b_cnt > SKEIN_1024_BLOCK_BYTES) {
600 /* finish up any buffered message data */
602 /* # bytes free in buffer b[] */
603 n = SKEIN_1024_BLOCK_BYTES - ctx->h.b_cnt;
605 /* check on our logic here */
606 skein_assert(n < msg_byte_cnt);
607 memcpy(&ctx->b[ctx->h.b_cnt], msg, n);
612 skein_assert(ctx->h.b_cnt == SKEIN_1024_BLOCK_BYTES);
613 skein_1024_process_block(ctx, ctx->b, 1,
614 SKEIN_1024_BLOCK_BYTES);
618 * now process any remaining full blocks, directly from input
621 if (msg_byte_cnt > SKEIN_1024_BLOCK_BYTES) {
622 /* number of full blocks to process */
623 n = (msg_byte_cnt-1) / SKEIN_1024_BLOCK_BYTES;
624 skein_1024_process_block(ctx, msg, n,
625 SKEIN_1024_BLOCK_BYTES);
626 msg_byte_cnt -= n * SKEIN_1024_BLOCK_BYTES;
627 msg += n * SKEIN_1024_BLOCK_BYTES;
629 skein_assert(ctx->h.b_cnt == 0);
632 /* copy any remaining source message data bytes into b[] */
634 skein_assert(msg_byte_cnt + ctx->h.b_cnt <=
635 SKEIN_1024_BLOCK_BYTES);
636 memcpy(&ctx->b[ctx->h.b_cnt], msg, msg_byte_cnt);
637 ctx->h.b_cnt += msg_byte_cnt;
640 return SKEIN_SUCCESS;
643 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
644 /* finalize the hash computation and output the result */
645 int skein_1024_final(struct skein_1024_ctx *ctx, u8 *hash_val)
647 size_t i, n, byte_cnt;
648 u64 x[SKEIN_1024_STATE_WORDS];
649 /* catch uninitialized context */
650 skein_assert_ret(ctx->h.b_cnt <= SKEIN_1024_BLOCK_BYTES, SKEIN_FAIL);
652 /* tag as the final block */
653 ctx->h.tweak[1] |= SKEIN_T1_FLAG_FINAL;
654 /* zero pad b[] if necessary */
655 if (ctx->h.b_cnt < SKEIN_1024_BLOCK_BYTES)
656 memset(&ctx->b[ctx->h.b_cnt], 0,
657 SKEIN_1024_BLOCK_BYTES - ctx->h.b_cnt);
659 /* process the final block */
660 skein_1024_process_block(ctx, ctx->b, 1, ctx->h.b_cnt);
662 /* now output the result */
663 /* total number of output bytes */
664 byte_cnt = (ctx->h.hash_bit_len + 7) >> 3;
666 /* run Threefish in "counter mode" to generate output */
667 /* zero out b[], so it can hold the counter */
668 memset(ctx->b, 0, sizeof(ctx->b));
669 /* keep a local copy of counter mode "key" */
670 memcpy(x, ctx->x, sizeof(x));
671 for (i = 0; i*SKEIN_1024_BLOCK_BYTES < byte_cnt; i++) {
672 /* build the counter block */
673 ((u64 *)ctx->b)[0] = skein_swap64((u64) i);
674 skein_start_new_type(ctx, OUT_FINAL);
675 /* run "counter mode" */
676 skein_1024_process_block(ctx, ctx->b, 1, sizeof(u64));
677 /* number of output bytes left to go */
678 n = byte_cnt - i*SKEIN_1024_BLOCK_BYTES;
679 if (n >= SKEIN_1024_BLOCK_BYTES)
680 n = SKEIN_1024_BLOCK_BYTES;
681 /* "output" the ctr mode bytes */
682 skein_put64_lsb_first(hash_val+i*SKEIN_1024_BLOCK_BYTES, ctx->x,
684 /* restore the counter mode key for next time */
685 memcpy(ctx->x, x, sizeof(x));
687 return SKEIN_SUCCESS;
690 /**************** Functions to support MAC/tree hashing ***************/
691 /* (this code is identical for Optimized and Reference versions) */
693 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
694 /* finalize the hash computation and output the block, no OUTPUT stage */
695 int skein_256_final_pad(struct skein_256_ctx *ctx, u8 *hash_val)
697 /* catch uninitialized context */
698 skein_assert_ret(ctx->h.b_cnt <= SKEIN_256_BLOCK_BYTES, SKEIN_FAIL);
700 /* tag as the final block */
701 ctx->h.tweak[1] |= SKEIN_T1_FLAG_FINAL;
702 /* zero pad b[] if necessary */
703 if (ctx->h.b_cnt < SKEIN_256_BLOCK_BYTES)
704 memset(&ctx->b[ctx->h.b_cnt], 0,
705 SKEIN_256_BLOCK_BYTES - ctx->h.b_cnt);
706 /* process the final block */
707 skein_256_process_block(ctx, ctx->b, 1, ctx->h.b_cnt);
709 /* "output" the state bytes */
710 skein_put64_lsb_first(hash_val, ctx->x, SKEIN_256_BLOCK_BYTES);
712 return SKEIN_SUCCESS;
715 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
716 /* finalize the hash computation and output the block, no OUTPUT stage */
717 int skein_512_final_pad(struct skein_512_ctx *ctx, u8 *hash_val)
719 /* catch uninitialized context */
720 skein_assert_ret(ctx->h.b_cnt <= SKEIN_512_BLOCK_BYTES, SKEIN_FAIL);
722 /* tag as the final block */
723 ctx->h.tweak[1] |= SKEIN_T1_FLAG_FINAL;
724 /* zero pad b[] if necessary */
725 if (ctx->h.b_cnt < SKEIN_512_BLOCK_BYTES)
726 memset(&ctx->b[ctx->h.b_cnt], 0,
727 SKEIN_512_BLOCK_BYTES - ctx->h.b_cnt);
728 /* process the final block */
729 skein_512_process_block(ctx, ctx->b, 1, ctx->h.b_cnt);
731 /* "output" the state bytes */
732 skein_put64_lsb_first(hash_val, ctx->x, SKEIN_512_BLOCK_BYTES);
734 return SKEIN_SUCCESS;
737 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
738 /* finalize the hash computation and output the block, no OUTPUT stage */
739 int skein_1024_final_pad(struct skein_1024_ctx *ctx, u8 *hash_val)
741 /* catch uninitialized context */
742 skein_assert_ret(ctx->h.b_cnt <= SKEIN_1024_BLOCK_BYTES, SKEIN_FAIL);
744 /* tag as the final block */
745 ctx->h.tweak[1] |= SKEIN_T1_FLAG_FINAL;
746 /* zero pad b[] if necessary */
747 if (ctx->h.b_cnt < SKEIN_1024_BLOCK_BYTES)
748 memset(&ctx->b[ctx->h.b_cnt], 0,
749 SKEIN_1024_BLOCK_BYTES - ctx->h.b_cnt);
750 /* process the final block */
751 skein_1024_process_block(ctx, ctx->b, 1, ctx->h.b_cnt);
753 /* "output" the state bytes */
754 skein_put64_lsb_first(hash_val, ctx->x, SKEIN_1024_BLOCK_BYTES);
756 return SKEIN_SUCCESS;
760 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
761 /* just do the OUTPUT stage */
762 int skein_256_output(struct skein_256_ctx *ctx, u8 *hash_val)
764 size_t i, n, byte_cnt;
765 u64 x[SKEIN_256_STATE_WORDS];
766 /* catch uninitialized context */
767 skein_assert_ret(ctx->h.b_cnt <= SKEIN_256_BLOCK_BYTES, SKEIN_FAIL);
769 /* now output the result */
770 /* total number of output bytes */
771 byte_cnt = (ctx->h.hash_bit_len + 7) >> 3;
773 /* run Threefish in "counter mode" to generate output */
774 /* zero out b[], so it can hold the counter */
775 memset(ctx->b, 0, sizeof(ctx->b));
776 /* keep a local copy of counter mode "key" */
777 memcpy(x, ctx->x, sizeof(x));
778 for (i = 0; i*SKEIN_256_BLOCK_BYTES < byte_cnt; i++) {
779 /* build the counter block */
780 ((u64 *)ctx->b)[0] = skein_swap64((u64) i);
781 skein_start_new_type(ctx, OUT_FINAL);
782 /* run "counter mode" */
783 skein_256_process_block(ctx, ctx->b, 1, sizeof(u64));
784 /* number of output bytes left to go */
785 n = byte_cnt - i*SKEIN_256_BLOCK_BYTES;
786 if (n >= SKEIN_256_BLOCK_BYTES)
787 n = SKEIN_256_BLOCK_BYTES;
788 /* "output" the ctr mode bytes */
789 skein_put64_lsb_first(hash_val+i*SKEIN_256_BLOCK_BYTES, ctx->x,
791 /* restore the counter mode key for next time */
792 memcpy(ctx->x, x, sizeof(x));
794 return SKEIN_SUCCESS;
797 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
798 /* just do the OUTPUT stage */
799 int skein_512_output(struct skein_512_ctx *ctx, u8 *hash_val)
801 size_t i, n, byte_cnt;
802 u64 x[SKEIN_512_STATE_WORDS];
803 /* catch uninitialized context */
804 skein_assert_ret(ctx->h.b_cnt <= SKEIN_512_BLOCK_BYTES, SKEIN_FAIL);
806 /* now output the result */
807 /* total number of output bytes */
808 byte_cnt = (ctx->h.hash_bit_len + 7) >> 3;
810 /* run Threefish in "counter mode" to generate output */
811 /* zero out b[], so it can hold the counter */
812 memset(ctx->b, 0, sizeof(ctx->b));
813 /* keep a local copy of counter mode "key" */
814 memcpy(x, ctx->x, sizeof(x));
815 for (i = 0; i*SKEIN_512_BLOCK_BYTES < byte_cnt; i++) {
816 /* build the counter block */
817 ((u64 *)ctx->b)[0] = skein_swap64((u64) i);
818 skein_start_new_type(ctx, OUT_FINAL);
819 /* run "counter mode" */
820 skein_512_process_block(ctx, ctx->b, 1, sizeof(u64));
821 /* number of output bytes left to go */
822 n = byte_cnt - i*SKEIN_512_BLOCK_BYTES;
823 if (n >= SKEIN_512_BLOCK_BYTES)
824 n = SKEIN_512_BLOCK_BYTES;
825 /* "output" the ctr mode bytes */
826 skein_put64_lsb_first(hash_val+i*SKEIN_512_BLOCK_BYTES, ctx->x,
828 /* restore the counter mode key for next time */
829 memcpy(ctx->x, x, sizeof(x));
831 return SKEIN_SUCCESS;
834 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
835 /* just do the OUTPUT stage */
836 int skein_1024_output(struct skein_1024_ctx *ctx, u8 *hash_val)
838 size_t i, n, byte_cnt;
839 u64 x[SKEIN_1024_STATE_WORDS];
840 /* catch uninitialized context */
841 skein_assert_ret(ctx->h.b_cnt <= SKEIN_1024_BLOCK_BYTES, SKEIN_FAIL);
843 /* now output the result */
844 /* total number of output bytes */
845 byte_cnt = (ctx->h.hash_bit_len + 7) >> 3;
847 /* run Threefish in "counter mode" to generate output */
848 /* zero out b[], so it can hold the counter */
849 memset(ctx->b, 0, sizeof(ctx->b));
850 /* keep a local copy of counter mode "key" */
851 memcpy(x, ctx->x, sizeof(x));
852 for (i = 0; i*SKEIN_1024_BLOCK_BYTES < byte_cnt; i++) {
853 /* build the counter block */
854 ((u64 *)ctx->b)[0] = skein_swap64((u64) i);
855 skein_start_new_type(ctx, OUT_FINAL);
856 /* run "counter mode" */
857 skein_1024_process_block(ctx, ctx->b, 1, sizeof(u64));
858 /* number of output bytes left to go */
859 n = byte_cnt - i*SKEIN_1024_BLOCK_BYTES;
860 if (n >= SKEIN_1024_BLOCK_BYTES)
861 n = SKEIN_1024_BLOCK_BYTES;
862 /* "output" the ctr mode bytes */
863 skein_put64_lsb_first(hash_val+i*SKEIN_1024_BLOCK_BYTES, ctx->x,
865 /* restore the counter mode key for next time */
866 memcpy(ctx->x, x, sizeof(x));
868 return SKEIN_SUCCESS;