45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Implementation of the Skein hash function.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Source code author: Doug Whiting, 2008.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * This algorithm and source code is released to the public domain.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* Copyright 2013 Doug Whiting. This code is released to the public domain. */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#define SKEIN_PORT_CODE /* instantiate any code in skein_port.h */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include <sys/types.h>
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include <sys/note.h>
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include <sys/skein.h> /* get the Skein API definitions */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include "skein_impl.h" /* get internal definitions */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* External function to process blkCnt (nonzero) full block(s) of data. */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensvoid Skein_256_Process_Block(Skein_256_Ctxt_t *ctx, const uint8_t *blkPtr,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens size_t blkCnt, size_t byteCntAdd);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensvoid Skein_512_Process_Block(Skein_512_Ctxt_t *ctx, const uint8_t *blkPtr,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens size_t blkCnt, size_t byteCntAdd);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensvoid Skein1024_Process_Block(Skein1024_Ctxt_t *ctx, const uint8_t *blkPtr,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens size_t blkCnt, size_t byteCntAdd);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* 256-bit Skein */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* init the context for a straight hashing operation */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew AhrensSkein_256_Init(Skein_256_Ctxt_t *ctx, size_t hashBitLen)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens union {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint8_t b[SKEIN_256_STATE_BYTES];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint64_t w[SKEIN_256_STATE_WORDS];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens } cfg; /* config block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(hashBitLen > 0, SKEIN_BAD_HASHLEN);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.hashBitLen = hashBitLen; /* output hash bit count */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens switch (hashBitLen) { /* use pre-computed values, where available */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#ifndef SKEIN_NO_PRECOMP
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens case 256:
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(SKEIN_256_IV_256, ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens break;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens case 224:
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(SKEIN_256_IV_224, ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens break;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens case 160:
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(SKEIN_256_IV_160, ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens break;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens case 128:
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(SKEIN_256_IV_128, ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens break;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens default:
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* here if there is no precomputed IV value available */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * build/process the config block, type == CONFIG (could be
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * precomputed)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* set tweaks: T0=0; T1=CFG | FINAL */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, CFG_FINAL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* set the schema, version */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens cfg.w[0] = Skein_Swap64(SKEIN_SCHEMA_VER);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* hash result length in bits */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens cfg.w[1] = Skein_Swap64(hashBitLen);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens cfg.w[2] = Skein_Swap64(SKEIN_CFG_TREE_INFO_SEQUENTIAL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero pad config block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(&cfg.w[3], sizeof (cfg) - 3 * sizeof (cfg.w[0]));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* compute the initial chaining values from config block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero the chaining variables */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_256_Process_Block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens break;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * The chaining vars ctx->X are now initialized for the given
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * hashBitLen.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Set up to process the data message portion of the hash (default)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, MSG); /* T0=0, T1= MSG type */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (SKEIN_SUCCESS);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* init the context for a MAC and/or tree hash operation */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * [identical to Skein_256_Init() when keyBytes == 0 &&
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * treeInfo == SKEIN_CFG_TREE_INFO_SEQUENTIAL]
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew AhrensSkein_256_InitExt(Skein_256_Ctxt_t *ctx, size_t hashBitLen, uint64_t treeInfo,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens const uint8_t *key, size_t keyBytes)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens union {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint8_t b[SKEIN_256_STATE_BYTES];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint64_t w[SKEIN_256_STATE_WORDS];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens } cfg; /* config block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(hashBitLen > 0, SKEIN_BAD_HASHLEN);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(keyBytes == 0 || key != NULL, SKEIN_FAIL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* compute the initial chaining values ctx->X[], based on key */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (keyBytes == 0) { /* is there a key? */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* no key: use all zeroes as key for config block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens } else { /* here to pre-process a key */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_assert(sizeof (cfg.b) >= sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* do a mini-Init right here */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* set output hash bit count = state size */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.hashBitLen = 8 * sizeof (ctx->X);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* set tweaks: T0 = 0; T1 = KEY type */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, KEY);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero the initial chaining variables */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* hash the key */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens (void) Skein_256_Update(ctx, key, keyBytes);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* put result into cfg.b[] */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens (void) Skein_256_Final_Pad(ctx, cfg.b);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* copy over into ctx->X[] */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(cfg.b, ctx->X, sizeof (cfg.b));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#if SKEIN_NEED_SWAP
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint_t i;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* convert key bytes to context words */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens for (i = 0; i < SKEIN_256_STATE_WORDS; i++)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->X[i] = Skein_Swap64(ctx->X[i]);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * build/process the config block, type == CONFIG (could be
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * precomputed for each key)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.hashBitLen = hashBitLen; /* output hash bit count */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, CFG_FINAL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(&cfg.w, sizeof (cfg.w)); /* pre-pad cfg.w[] with zeroes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens cfg.w[0] = Skein_Swap64(SKEIN_SCHEMA_VER);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens cfg.w[1] = Skein_Swap64(hashBitLen); /* hash result length in bits */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* tree hash config info (or SKEIN_CFG_TREE_INFO_SEQUENTIAL) */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens cfg.w[2] = Skein_Swap64(treeInfo);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Show_Key(256, &ctx->h, key, keyBytes);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* compute the initial chaining values from config block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_256_Process_Block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* The chaining vars ctx->X are now initialized */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* Set up to process the data message portion of the hash (default) */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.bCnt = 0; /* buffer b[] starts out empty */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, MSG);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (SKEIN_SUCCESS);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* process the input bytes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew AhrensSkein_256_Update(Skein_256_Ctxt_t *ctx, const uint8_t *msg, size_t msgByteCnt)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens size_t n;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* catch uninitialized context */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(ctx->h.bCnt <= SKEIN_256_BLOCK_BYTES, SKEIN_FAIL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* process full blocks, if any */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (msgByteCnt + ctx->h.bCnt > SKEIN_256_BLOCK_BYTES) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* finish up any buffered message data */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (ctx->h.bCnt) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* # bytes free in buffer b[] */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens n = SKEIN_256_BLOCK_BYTES - ctx->h.bCnt;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (n) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* check on our logic here */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_assert(n < msgByteCnt);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(msg, &ctx->b[ctx->h.bCnt], n);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens msgByteCnt -= n;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens msg += n;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.bCnt += n;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_assert(ctx->h.bCnt == SKEIN_256_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_256_Process_Block(ctx, ctx->b, 1,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens SKEIN_256_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.bCnt = 0;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * now process any remaining full blocks, directly from input
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * message data
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (msgByteCnt > SKEIN_256_BLOCK_BYTES) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* number of full blocks to process */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens n = (msgByteCnt - 1) / SKEIN_256_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_256_Process_Block(ctx, msg, n,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens SKEIN_256_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens msgByteCnt -= n * SKEIN_256_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens msg += n * SKEIN_256_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_assert(ctx->h.bCnt == 0);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* copy any remaining source message data bytes into b[] */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (msgByteCnt) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_assert(msgByteCnt + ctx->h.bCnt <= SKEIN_256_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(msg, &ctx->b[ctx->h.bCnt], msgByteCnt);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.bCnt += msgByteCnt;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (SKEIN_SUCCESS);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* finalize the hash computation and output the result */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew AhrensSkein_256_Final(Skein_256_Ctxt_t *ctx, uint8_t *hashVal)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens size_t i, n, byteCnt;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint64_t X[SKEIN_256_STATE_WORDS];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* catch uninitialized context */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(ctx->h.bCnt <= SKEIN_256_BLOCK_BYTES, SKEIN_FAIL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.T[1] |= SKEIN_T1_FLAG_FINAL; /* tag as the final block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero pad b[] if necessary */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (ctx->h.bCnt < SKEIN_256_BLOCK_BYTES)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(&ctx->b[ctx->h.bCnt],
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens SKEIN_256_BLOCK_BYTES - ctx->h.bCnt);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* process the final block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_256_Process_Block(ctx, ctx->b, 1, ctx->h.bCnt);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* now output the result */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* total number of output bytes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens byteCnt = (ctx->h.hashBitLen + 7) >> 3;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* run Threefish in "counter mode" to generate output */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero out b[], so it can hold the counter */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(ctx->b, sizeof (ctx->b));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* keep a local copy of counter mode "key" */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(ctx->X, X, sizeof (X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens for (i = 0; i * SKEIN_256_BLOCK_BYTES < byteCnt; i++) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* build the counter block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint64_t tmp = Skein_Swap64((uint64_t)i);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(&tmp, ctx->b, sizeof (tmp));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, OUT_FINAL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* run "counter mode" */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_256_Process_Block(ctx, ctx->b, 1, sizeof (uint64_t));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* number of output bytes left to go */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens n = byteCnt - i * SKEIN_256_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (n >= SKEIN_256_BLOCK_BYTES)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens n = SKEIN_256_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Put64_LSB_First(hashVal + i * SKEIN_256_BLOCK_BYTES,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->X, n); /* "output" the ctr mode bytes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Show_Final(256, &ctx->h, n,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens hashVal + i * SKEIN_256_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* restore the counter mode key for next time */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(X, ctx->X, sizeof (X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (SKEIN_SUCCESS);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* 512-bit Skein */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* init the context for a straight hashing operation */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew AhrensSkein_512_Init(Skein_512_Ctxt_t *ctx, size_t hashBitLen)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens union {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint8_t b[SKEIN_512_STATE_BYTES];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint64_t w[SKEIN_512_STATE_WORDS];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens } cfg; /* config block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(hashBitLen > 0, SKEIN_BAD_HASHLEN);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.hashBitLen = hashBitLen; /* output hash bit count */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens switch (hashBitLen) { /* use pre-computed values, where available */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#ifndef SKEIN_NO_PRECOMP
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens case 512:
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(SKEIN_512_IV_512, ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens break;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens case 384:
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(SKEIN_512_IV_384, ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens break;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens case 256:
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(SKEIN_512_IV_256, ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens break;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens case 224:
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(SKEIN_512_IV_224, ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens break;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens default:
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * here if there is no precomputed IV value available
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * build/process the config block, type == CONFIG (could be
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * precomputed)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* set tweaks: T0=0; T1=CFG | FINAL */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, CFG_FINAL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* set the schema, version */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens cfg.w[0] = Skein_Swap64(SKEIN_SCHEMA_VER);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* hash result length in bits */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens cfg.w[1] = Skein_Swap64(hashBitLen);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens cfg.w[2] = Skein_Swap64(SKEIN_CFG_TREE_INFO_SEQUENTIAL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero pad config block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(&cfg.w[3], sizeof (cfg) - 3 * sizeof (cfg.w[0]));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* compute the initial chaining values from config block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero the chaining variables */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_512_Process_Block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens break;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * The chaining vars ctx->X are now initialized for the given
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * hashBitLen. Set up to process the data message portion of the
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * hash (default)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, MSG); /* T0=0, T1= MSG type */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (SKEIN_SUCCESS);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* init the context for a MAC and/or tree hash operation */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * [identical to Skein_512_Init() when keyBytes == 0 &&
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * treeInfo == SKEIN_CFG_TREE_INFO_SEQUENTIAL]
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew AhrensSkein_512_InitExt(Skein_512_Ctxt_t *ctx, size_t hashBitLen, uint64_t treeInfo,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens const uint8_t *key, size_t keyBytes)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens union {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint8_t b[SKEIN_512_STATE_BYTES];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint64_t w[SKEIN_512_STATE_WORDS];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens } cfg; /* config block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(hashBitLen > 0, SKEIN_BAD_HASHLEN);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(keyBytes == 0 || key != NULL, SKEIN_FAIL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* compute the initial chaining values ctx->X[], based on key */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (keyBytes == 0) { /* is there a key? */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* no key: use all zeroes as key for config block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens } else { /* here to pre-process a key */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_assert(sizeof (cfg.b) >= sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* do a mini-Init right here */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* set output hash bit count = state size */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.hashBitLen = 8 * sizeof (ctx->X);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* set tweaks: T0 = 0; T1 = KEY type */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, KEY);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero the initial chaining variables */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens (void) Skein_512_Update(ctx, key, keyBytes); /* hash the key */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* put result into cfg.b[] */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens (void) Skein_512_Final_Pad(ctx, cfg.b);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* copy over into ctx->X[] */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(cfg.b, ctx->X, sizeof (cfg.b));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#if SKEIN_NEED_SWAP
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint_t i;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* convert key bytes to context words */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens for (i = 0; i < SKEIN_512_STATE_WORDS; i++)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->X[i] = Skein_Swap64(ctx->X[i]);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * build/process the config block, type == CONFIG (could be
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * precomputed for each key)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.hashBitLen = hashBitLen; /* output hash bit count */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, CFG_FINAL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(&cfg.w, sizeof (cfg.w)); /* pre-pad cfg.w[] with zeroes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens cfg.w[0] = Skein_Swap64(SKEIN_SCHEMA_VER);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens cfg.w[1] = Skein_Swap64(hashBitLen); /* hash result length in bits */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* tree hash config info (or SKEIN_CFG_TREE_INFO_SEQUENTIAL) */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens cfg.w[2] = Skein_Swap64(treeInfo);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Show_Key(512, &ctx->h, key, keyBytes);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* compute the initial chaining values from config block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_512_Process_Block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* The chaining vars ctx->X are now initialized */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* Set up to process the data message portion of the hash (default) */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.bCnt = 0; /* buffer b[] starts out empty */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, MSG);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (SKEIN_SUCCESS);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* process the input bytes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew AhrensSkein_512_Update(Skein_512_Ctxt_t *ctx, const uint8_t *msg, size_t msgByteCnt)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens size_t n;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* catch uninitialized context */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(ctx->h.bCnt <= SKEIN_512_BLOCK_BYTES, SKEIN_FAIL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* process full blocks, if any */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (msgByteCnt + ctx->h.bCnt > SKEIN_512_BLOCK_BYTES) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* finish up any buffered message data */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (ctx->h.bCnt) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* # bytes free in buffer b[] */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens n = SKEIN_512_BLOCK_BYTES - ctx->h.bCnt;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (n) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* check on our logic here */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_assert(n < msgByteCnt);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(msg, &ctx->b[ctx->h.bCnt], n);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens msgByteCnt -= n;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens msg += n;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.bCnt += n;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_assert(ctx->h.bCnt == SKEIN_512_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_512_Process_Block(ctx, ctx->b, 1,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens SKEIN_512_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.bCnt = 0;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * now process any remaining full blocks, directly from input
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * message data
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (msgByteCnt > SKEIN_512_BLOCK_BYTES) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* number of full blocks to process */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens n = (msgByteCnt - 1) / SKEIN_512_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_512_Process_Block(ctx, msg, n,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens SKEIN_512_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens msgByteCnt -= n * SKEIN_512_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens msg += n * SKEIN_512_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_assert(ctx->h.bCnt == 0);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* copy any remaining source message data bytes into b[] */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (msgByteCnt) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_assert(msgByteCnt + ctx->h.bCnt <= SKEIN_512_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(msg, &ctx->b[ctx->h.bCnt], msgByteCnt);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.bCnt += msgByteCnt;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (SKEIN_SUCCESS);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* finalize the hash computation and output the result */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew AhrensSkein_512_Final(Skein_512_Ctxt_t *ctx, uint8_t *hashVal)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens size_t i, n, byteCnt;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint64_t X[SKEIN_512_STATE_WORDS];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* catch uninitialized context */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(ctx->h.bCnt <= SKEIN_512_BLOCK_BYTES, SKEIN_FAIL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.T[1] |= SKEIN_T1_FLAG_FINAL; /* tag as the final block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero pad b[] if necessary */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (ctx->h.bCnt < SKEIN_512_BLOCK_BYTES)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(&ctx->b[ctx->h.bCnt],
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens SKEIN_512_BLOCK_BYTES - ctx->h.bCnt);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* process the final block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_512_Process_Block(ctx, ctx->b, 1, ctx->h.bCnt);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* now output the result */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* total number of output bytes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens byteCnt = (ctx->h.hashBitLen + 7) >> 3;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* run Threefish in "counter mode" to generate output */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero out b[], so it can hold the counter */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(ctx->b, sizeof (ctx->b));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* keep a local copy of counter mode "key" */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(ctx->X, X, sizeof (X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens for (i = 0; i * SKEIN_512_BLOCK_BYTES < byteCnt; i++) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* build the counter block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint64_t tmp = Skein_Swap64((uint64_t)i);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(&tmp, ctx->b, sizeof (tmp));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, OUT_FINAL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* run "counter mode" */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_512_Process_Block(ctx, ctx->b, 1, sizeof (uint64_t));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* number of output bytes left to go */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens n = byteCnt - i * SKEIN_512_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (n >= SKEIN_512_BLOCK_BYTES)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens n = SKEIN_512_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Put64_LSB_First(hashVal + i * SKEIN_512_BLOCK_BYTES,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->X, n); /* "output" the ctr mode bytes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Show_Final(512, &ctx->h, n,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens hashVal + i * SKEIN_512_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* restore the counter mode key for next time */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(X, ctx->X, sizeof (X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (SKEIN_SUCCESS);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* 1024-bit Skein */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* init the context for a straight hashing operation */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew AhrensSkein1024_Init(Skein1024_Ctxt_t *ctx, size_t hashBitLen)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens union {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint8_t b[SKEIN1024_STATE_BYTES];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint64_t w[SKEIN1024_STATE_WORDS];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens } cfg; /* config block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(hashBitLen > 0, SKEIN_BAD_HASHLEN);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.hashBitLen = hashBitLen; /* output hash bit count */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens switch (hashBitLen) { /* use pre-computed values, where available */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#ifndef SKEIN_NO_PRECOMP
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens case 512:
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(SKEIN1024_IV_512, ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens break;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens case 384:
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(SKEIN1024_IV_384, ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens break;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens case 1024:
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(SKEIN1024_IV_1024, ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens break;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens default:
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* here if there is no precomputed IV value available */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * build/process the config block, type == CONFIG (could be
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * precomputed)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* set tweaks: T0=0; T1=CFG | FINAL */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, CFG_FINAL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* set the schema, version */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens cfg.w[0] = Skein_Swap64(SKEIN_SCHEMA_VER);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* hash result length in bits */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens cfg.w[1] = Skein_Swap64(hashBitLen);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens cfg.w[2] = Skein_Swap64(SKEIN_CFG_TREE_INFO_SEQUENTIAL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero pad config block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(&cfg.w[3], sizeof (cfg) - 3 * sizeof (cfg.w[0]));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* compute the initial chaining values from config block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero the chaining variables */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein1024_Process_Block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens break;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * The chaining vars ctx->X are now initialized for the given
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * hashBitLen. Set up to process the data message portion of the hash
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * (default)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, MSG); /* T0=0, T1= MSG type */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (SKEIN_SUCCESS);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* init the context for a MAC and/or tree hash operation */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * [identical to Skein1024_Init() when keyBytes == 0 &&
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * treeInfo == SKEIN_CFG_TREE_INFO_SEQUENTIAL]
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew AhrensSkein1024_InitExt(Skein1024_Ctxt_t *ctx, size_t hashBitLen, uint64_t treeInfo,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens const uint8_t *key, size_t keyBytes)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens union {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint8_t b[SKEIN1024_STATE_BYTES];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint64_t w[SKEIN1024_STATE_WORDS];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens } cfg; /* config block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(hashBitLen > 0, SKEIN_BAD_HASHLEN);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(keyBytes == 0 || key != NULL, SKEIN_FAIL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* compute the initial chaining values ctx->X[], based on key */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (keyBytes == 0) { /* is there a key? */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* no key: use all zeroes as key for config block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens } else { /* here to pre-process a key */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_assert(sizeof (cfg.b) >= sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* do a mini-Init right here */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* set output hash bit count = state size */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.hashBitLen = 8 * sizeof (ctx->X);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* set tweaks: T0 = 0; T1 = KEY type */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, KEY);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero the initial chaining variables */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(ctx->X, sizeof (ctx->X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens (void) Skein1024_Update(ctx, key, keyBytes); /* hash the key */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* put result into cfg.b[] */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens (void) Skein1024_Final_Pad(ctx, cfg.b);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* copy over into ctx->X[] */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(cfg.b, ctx->X, sizeof (cfg.b));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#if SKEIN_NEED_SWAP
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint_t i;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* convert key bytes to context words */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens for (i = 0; i < SKEIN1024_STATE_WORDS; i++)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->X[i] = Skein_Swap64(ctx->X[i]);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * build/process the config block, type == CONFIG (could be
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * precomputed for each key)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.hashBitLen = hashBitLen; /* output hash bit count */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, CFG_FINAL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(&cfg.w, sizeof (cfg.w)); /* pre-pad cfg.w[] with zeroes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens cfg.w[0] = Skein_Swap64(SKEIN_SCHEMA_VER);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* hash result length in bits */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens cfg.w[1] = Skein_Swap64(hashBitLen);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* tree hash config info (or SKEIN_CFG_TREE_INFO_SEQUENTIAL) */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens cfg.w[2] = Skein_Swap64(treeInfo);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Show_Key(1024, &ctx->h, key, keyBytes);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* compute the initial chaining values from config block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein1024_Process_Block(ctx, cfg.b, 1, SKEIN_CFG_STR_LEN);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* The chaining vars ctx->X are now initialized */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* Set up to process the data message portion of the hash (default) */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.bCnt = 0; /* buffer b[] starts out empty */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, MSG);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (SKEIN_SUCCESS);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* process the input bytes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew AhrensSkein1024_Update(Skein1024_Ctxt_t *ctx, const uint8_t *msg, size_t msgByteCnt)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens size_t n;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* catch uninitialized context */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(ctx->h.bCnt <= SKEIN1024_BLOCK_BYTES, SKEIN_FAIL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* process full blocks, if any */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (msgByteCnt + ctx->h.bCnt > SKEIN1024_BLOCK_BYTES) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* finish up any buffered message data */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (ctx->h.bCnt) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* # bytes free in buffer b[] */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens n = SKEIN1024_BLOCK_BYTES - ctx->h.bCnt;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (n) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* check on our logic here */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_assert(n < msgByteCnt);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(msg, &ctx->b[ctx->h.bCnt], n);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens msgByteCnt -= n;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens msg += n;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.bCnt += n;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_assert(ctx->h.bCnt == SKEIN1024_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein1024_Process_Block(ctx, ctx->b, 1,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens SKEIN1024_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.bCnt = 0;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * now process any remaining full blocks, directly from
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * input message data
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (msgByteCnt > SKEIN1024_BLOCK_BYTES) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* number of full blocks to process */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens n = (msgByteCnt - 1) / SKEIN1024_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein1024_Process_Block(ctx, msg, n,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens SKEIN1024_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens msgByteCnt -= n * SKEIN1024_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens msg += n * SKEIN1024_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_assert(ctx->h.bCnt == 0);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* copy any remaining source message data bytes into b[] */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (msgByteCnt) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_assert(msgByteCnt + ctx->h.bCnt <= SKEIN1024_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(msg, &ctx->b[ctx->h.bCnt], msgByteCnt);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.bCnt += msgByteCnt;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (SKEIN_SUCCESS);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* finalize the hash computation and output the result */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew AhrensSkein1024_Final(Skein1024_Ctxt_t *ctx, uint8_t *hashVal)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens size_t i, n, byteCnt;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint64_t X[SKEIN1024_STATE_WORDS];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* catch uninitialized context */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(ctx->h.bCnt <= SKEIN1024_BLOCK_BYTES, SKEIN_FAIL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.T[1] |= SKEIN_T1_FLAG_FINAL; /* tag as the final block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero pad b[] if necessary */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (ctx->h.bCnt < SKEIN1024_BLOCK_BYTES)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(&ctx->b[ctx->h.bCnt],
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens SKEIN1024_BLOCK_BYTES - ctx->h.bCnt);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* process the final block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein1024_Process_Block(ctx, ctx->b, 1, ctx->h.bCnt);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* now output the result */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* total number of output bytes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens byteCnt = (ctx->h.hashBitLen + 7) >> 3;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* run Threefish in "counter mode" to generate output */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero out b[], so it can hold the counter */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(ctx->b, sizeof (ctx->b));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* keep a local copy of counter mode "key" */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(ctx->X, X, sizeof (X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens for (i = 0; i * SKEIN1024_BLOCK_BYTES < byteCnt; i++) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* build the counter block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint64_t tmp = Skein_Swap64((uint64_t)i);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(&tmp, ctx->b, sizeof (tmp));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, OUT_FINAL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* run "counter mode" */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein1024_Process_Block(ctx, ctx->b, 1, sizeof (uint64_t));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* number of output bytes left to go */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens n = byteCnt - i * SKEIN1024_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (n >= SKEIN1024_BLOCK_BYTES)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens n = SKEIN1024_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Put64_LSB_First(hashVal + i * SKEIN1024_BLOCK_BYTES,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->X, n); /* "output" the ctr mode bytes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Show_Final(1024, &ctx->h, n,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens hashVal + i * SKEIN1024_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* restore the counter mode key for next time */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(X, ctx->X, sizeof (X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (SKEIN_SUCCESS);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* Functions to support MAC/tree hashing */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* (this code is identical for Optimized and Reference versions) */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* finalize the hash computation and output the block, no OUTPUT stage */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew AhrensSkein_256_Final_Pad(Skein_256_Ctxt_t *ctx, uint8_t *hashVal)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* catch uninitialized context */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(ctx->h.bCnt <= SKEIN_256_BLOCK_BYTES, SKEIN_FAIL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.T[1] |= SKEIN_T1_FLAG_FINAL; /* tag as the final block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero pad b[] if necessary */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (ctx->h.bCnt < SKEIN_256_BLOCK_BYTES)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(&ctx->b[ctx->h.bCnt],
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens SKEIN_256_BLOCK_BYTES - ctx->h.bCnt);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* process the final block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_256_Process_Block(ctx, ctx->b, 1, ctx->h.bCnt);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* "output" the state bytes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Put64_LSB_First(hashVal, ctx->X, SKEIN_256_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (SKEIN_SUCCESS);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* finalize the hash computation and output the block, no OUTPUT stage */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew AhrensSkein_512_Final_Pad(Skein_512_Ctxt_t *ctx, uint8_t *hashVal)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* catch uninitialized context */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(ctx->h.bCnt <= SKEIN_512_BLOCK_BYTES, SKEIN_FAIL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.T[1] |= SKEIN_T1_FLAG_FINAL; /* tag as the final block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero pad b[] if necessary */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (ctx->h.bCnt < SKEIN_512_BLOCK_BYTES)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(&ctx->b[ctx->h.bCnt],
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens SKEIN_512_BLOCK_BYTES - ctx->h.bCnt);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* process the final block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_512_Process_Block(ctx, ctx->b, 1, ctx->h.bCnt);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* "output" the state bytes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Put64_LSB_First(hashVal, ctx->X, SKEIN_512_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (SKEIN_SUCCESS);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* finalize the hash computation and output the block, no OUTPUT stage */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew AhrensSkein1024_Final_Pad(Skein1024_Ctxt_t *ctx, uint8_t *hashVal)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* catch uninitialized context */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(ctx->h.bCnt <= SKEIN1024_BLOCK_BYTES, SKEIN_FAIL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* tag as the final block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->h.T[1] |= SKEIN_T1_FLAG_FINAL;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero pad b[] if necessary */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (ctx->h.bCnt < SKEIN1024_BLOCK_BYTES)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(&ctx->b[ctx->h.bCnt],
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens SKEIN1024_BLOCK_BYTES - ctx->h.bCnt);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* process the final block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein1024_Process_Block(ctx, ctx->b, 1, ctx->h.bCnt);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* "output" the state bytes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Put64_LSB_First(hashVal, ctx->X, SKEIN1024_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (SKEIN_SUCCESS);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#if SKEIN_TREE_HASH
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* just do the OUTPUT stage */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew AhrensSkein_256_Output(Skein_256_Ctxt_t *ctx, uint8_t *hashVal)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens size_t i, n, byteCnt;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint64_t X[SKEIN_256_STATE_WORDS];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* catch uninitialized context */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(ctx->h.bCnt <= SKEIN_256_BLOCK_BYTES, SKEIN_FAIL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* now output the result */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* total number of output bytes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens byteCnt = (ctx->h.hashBitLen + 7) >> 3;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* run Threefish in "counter mode" to generate output */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero out b[], so it can hold the counter */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(ctx->b, sizeof (ctx->b));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* keep a local copy of counter mode "key" */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(ctx->X, X, sizeof (X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens for (i = 0; i * SKEIN_256_BLOCK_BYTES < byteCnt; i++) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* build the counter block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint64_t tmp = Skein_Swap64((uint64_t)i);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(&tmp, ctx->b, sizeof (tmp));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, OUT_FINAL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* run "counter mode" */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_256_Process_Block(ctx, ctx->b, 1, sizeof (uint64_t));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* number of output bytes left to go */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens n = byteCnt - i * SKEIN_256_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (n >= SKEIN_256_BLOCK_BYTES)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens n = SKEIN_256_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Put64_LSB_First(hashVal + i * SKEIN_256_BLOCK_BYTES,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->X, n); /* "output" the ctr mode bytes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Show_Final(256, &ctx->h, n,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens hashVal + i * SKEIN_256_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* restore the counter mode key for next time */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(X, ctx->X, sizeof (X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (SKEIN_SUCCESS);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* just do the OUTPUT stage */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew AhrensSkein_512_Output(Skein_512_Ctxt_t *ctx, uint8_t *hashVal)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens size_t i, n, byteCnt;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint64_t X[SKEIN_512_STATE_WORDS];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* catch uninitialized context */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(ctx->h.bCnt <= SKEIN_512_BLOCK_BYTES, SKEIN_FAIL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* now output the result */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* total number of output bytes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens byteCnt = (ctx->h.hashBitLen + 7) >> 3;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* run Threefish in "counter mode" to generate output */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero out b[], so it can hold the counter */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(ctx->b, sizeof (ctx->b));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* keep a local copy of counter mode "key" */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(ctx->X, X, sizeof (X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens for (i = 0; i * SKEIN_512_BLOCK_BYTES < byteCnt; i++) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* build the counter block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint64_t tmp = Skein_Swap64((uint64_t)i);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(&tmp, ctx->b, sizeof (tmp));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, OUT_FINAL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* run "counter mode" */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_512_Process_Block(ctx, ctx->b, 1, sizeof (uint64_t));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* number of output bytes left to go */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens n = byteCnt - i * SKEIN_512_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (n >= SKEIN_512_BLOCK_BYTES)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens n = SKEIN_512_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Put64_LSB_First(hashVal + i * SKEIN_512_BLOCK_BYTES,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->X, n); /* "output" the ctr mode bytes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Show_Final(256, &ctx->h, n,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens hashVal + i * SKEIN_512_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* restore the counter mode key for next time */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(X, ctx->X, sizeof (X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (SKEIN_SUCCESS);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/* just do the OUTPUT stage */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew AhrensSkein1024_Output(Skein1024_Ctxt_t *ctx, uint8_t *hashVal)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens size_t i, n, byteCnt;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint64_t X[SKEIN1024_STATE_WORDS];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* catch uninitialized context */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Assert(ctx->h.bCnt <= SKEIN1024_BLOCK_BYTES, SKEIN_FAIL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* now output the result */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* total number of output bytes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens byteCnt = (ctx->h.hashBitLen + 7) >> 3;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* run Threefish in "counter mode" to generate output */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* zero out b[], so it can hold the counter */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(ctx->b, sizeof (ctx->b));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* keep a local copy of counter mode "key" */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(ctx->X, X, sizeof (X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens for (i = 0; i * SKEIN1024_BLOCK_BYTES < byteCnt; i++) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* build the counter block */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint64_t tmp = Skein_Swap64((uint64_t)i);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(&tmp, ctx->b, sizeof (tmp));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Start_New_Type(ctx, OUT_FINAL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* run "counter mode" */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein1024_Process_Block(ctx, ctx->b, 1, sizeof (uint64_t));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* number of output bytes left to go */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens n = byteCnt - i * SKEIN1024_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (n >= SKEIN1024_BLOCK_BYTES)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens n = SKEIN1024_BLOCK_BYTES;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Put64_LSB_First(hashVal + i * SKEIN1024_BLOCK_BYTES,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx->X, n); /* "output" the ctr mode bytes */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_Show_Final(256, &ctx->h, n,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens hashVal + i * SKEIN1024_BLOCK_BYTES);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /* restore the counter mode key for next time */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(X, ctx->X, sizeof (X));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (SKEIN_SUCCESS);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif