Path: blob/main/sys/contrib/openzfs/module/icp/algs/skein/skein_impl.h
48775 views
// SPDX-License-Identifier: LicenseRef-OpenZFS-ThirdParty-PublicDomain1/*2* Internal definitions for Skein hashing.3* Source code author: Doug Whiting, 2008.4* This algorithm and source code is released to the public domain.5*6* The following compile-time switches may be defined to control some7* tradeoffs between speed, code size, error checking, and security.8*9* The "default" note explains what happens when the switch is not defined.10*11* SKEIN_DEBUG -- make callouts from inside Skein code12* to examine/display intermediate values.13* [default: no callouts (no overhead)]14*15* SKEIN_ERR_CHECK -- how error checking is handled inside Skein16* code. If not defined, most error checking17* is disabled (for performance). Otherwise,18* the switch value is interpreted as:19* 0: use assert() to flag errors20* 1: return SKEIN_FAIL to flag errors21*/22/* Copyright 2013 Doug Whiting. This code is released to the public domain. */2324#ifndef _SKEIN_IMPL_H_25#define _SKEIN_IMPL_H_2627#include <sys/skein.h>28#include <sys/string.h>29#include "skein_impl.h"30#include "skein_port.h"3132/*33* "Internal" Skein definitions34* -- not needed for sequential hashing API, but will be35* helpful for other uses of Skein (e.g., tree hash mode).36* -- included here so that they can be shared between37* reference and optimized code.38*/3940/* tweak word T[1]: bit field starting positions */41/* offset 64 because it's the second word */42#define SKEIN_T1_BIT(BIT) ((BIT) - 64)4344/* bits 112..118: level in hash tree */45#define SKEIN_T1_POS_TREE_LVL SKEIN_T1_BIT(112)46/* bit 119: partial final input byte */47#define SKEIN_T1_POS_BIT_PAD SKEIN_T1_BIT(119)48/* bits 120..125: type field */49#define SKEIN_T1_POS_BLK_TYPE SKEIN_T1_BIT(120)50/* bits 126: first block flag */51#define SKEIN_T1_POS_FIRST SKEIN_T1_BIT(126)52/* bit 127: final block flag */53#define SKEIN_T1_POS_FINAL SKEIN_T1_BIT(127)5455/* tweak word T[1]: flag bit definition(s) */56#define SKEIN_T1_FLAG_FIRST (((uint64_t)1) << SKEIN_T1_POS_FIRST)57#define SKEIN_T1_FLAG_FINAL (((uint64_t)1) << SKEIN_T1_POS_FINAL)58#define SKEIN_T1_FLAG_BIT_PAD (((uint64_t)1) << SKEIN_T1_POS_BIT_PAD)5960/* tweak word T[1]: tree level bit field mask */61#define SKEIN_T1_TREE_LVL_MASK (((uint64_t)0x7F) << SKEIN_T1_POS_TREE_LVL)62#define SKEIN_T1_TREE_LEVEL(n) (((uint64_t)(n)) << SKEIN_T1_POS_TREE_LVL)6364/* tweak word T[1]: block type field */65#define SKEIN_BLK_TYPE_KEY (0) /* key, for MAC and KDF */66#define SKEIN_BLK_TYPE_CFG (4) /* configuration block */67#define SKEIN_BLK_TYPE_PERS (8) /* personalization string */68#define SKEIN_BLK_TYPE_PK (12) /* public key (for signature hashing) */69#define SKEIN_BLK_TYPE_KDF (16) /* key identifier for KDF */70#define SKEIN_BLK_TYPE_NONCE (20) /* nonce for PRNG */71#define SKEIN_BLK_TYPE_MSG (48) /* message processing */72#define SKEIN_BLK_TYPE_OUT (63) /* output stage */73#define SKEIN_BLK_TYPE_MASK (63) /* bit field mask */7475#define SKEIN_T1_BLK_TYPE(T) \76(((uint64_t)(SKEIN_BLK_TYPE_##T)) << SKEIN_T1_POS_BLK_TYPE)77/* key, for MAC and KDF */78#define SKEIN_T1_BLK_TYPE_KEY SKEIN_T1_BLK_TYPE(KEY)79/* configuration block */80#define SKEIN_T1_BLK_TYPE_CFG SKEIN_T1_BLK_TYPE(CFG)81/* personalization string */82#define SKEIN_T1_BLK_TYPE_PERS SKEIN_T1_BLK_TYPE(PERS)83/* public key (for digital signature hashing) */84#define SKEIN_T1_BLK_TYPE_PK SKEIN_T1_BLK_TYPE(PK)85/* key identifier for KDF */86#define SKEIN_T1_BLK_TYPE_KDF SKEIN_T1_BLK_TYPE(KDF)87/* nonce for PRNG */88#define SKEIN_T1_BLK_TYPE_NONCE SKEIN_T1_BLK_TYPE(NONCE)89/* message processing */90#define SKEIN_T1_BLK_TYPE_MSG SKEIN_T1_BLK_TYPE(MSG)91/* output stage */92#define SKEIN_T1_BLK_TYPE_OUT SKEIN_T1_BLK_TYPE(OUT)93/* field bit mask */94#define SKEIN_T1_BLK_TYPE_MASK SKEIN_T1_BLK_TYPE(MASK)9596#define SKEIN_T1_BLK_TYPE_CFG_FINAL \97(SKEIN_T1_BLK_TYPE_CFG | SKEIN_T1_FLAG_FINAL)98#define SKEIN_T1_BLK_TYPE_OUT_FINAL \99(SKEIN_T1_BLK_TYPE_OUT | SKEIN_T1_FLAG_FINAL)100101#define SKEIN_VERSION (1)102103#ifndef SKEIN_ID_STRING_LE /* allow compile-time personalization */104#define SKEIN_ID_STRING_LE (0x33414853) /* "SHA3" (little-endian) */105#endif106107#define SKEIN_MK_64(hi32, lo32) ((lo32) + (((uint64_t)(hi32)) << 32))108#define SKEIN_SCHEMA_VER SKEIN_MK_64(SKEIN_VERSION, SKEIN_ID_STRING_LE)109#define SKEIN_KS_PARITY SKEIN_MK_64(0x1BD11BDA, 0xA9FC1A22)110111#define SKEIN_CFG_STR_LEN (4*8)112113/* bit field definitions in config block treeInfo word */114#define SKEIN_CFG_TREE_LEAF_SIZE_POS (0)115#define SKEIN_CFG_TREE_NODE_SIZE_POS (8)116#define SKEIN_CFG_TREE_MAX_LEVEL_POS (16)117118#define SKEIN_CFG_TREE_LEAF_SIZE_MSK \119(((uint64_t)0xFF) << SKEIN_CFG_TREE_LEAF_SIZE_POS)120#define SKEIN_CFG_TREE_NODE_SIZE_MSK \121(((uint64_t)0xFF) << SKEIN_CFG_TREE_NODE_SIZE_POS)122#define SKEIN_CFG_TREE_MAX_LEVEL_MSK \123(((uint64_t)0xFF) << SKEIN_CFG_TREE_MAX_LEVEL_POS)124125#define SKEIN_CFG_TREE_INFO(leaf, node, maxLvl) \126((((uint64_t)(leaf)) << SKEIN_CFG_TREE_LEAF_SIZE_POS) | \127(((uint64_t)(node)) << SKEIN_CFG_TREE_NODE_SIZE_POS) | \128(((uint64_t)(maxLvl)) << SKEIN_CFG_TREE_MAX_LEVEL_POS))129130/* use as treeInfo in InitExt() call for sequential processing */131#define SKEIN_CFG_TREE_INFO_SEQUENTIAL SKEIN_CFG_TREE_INFO(0, 0, 0)132133/*134* Skein macros for getting/setting tweak words, etc.135* These are useful for partial input bytes, hash tree init/update, etc.136*/137#define Skein_Get_Tweak(ctxPtr, TWK_NUM) ((ctxPtr)->h.T[TWK_NUM])138#define Skein_Set_Tweak(ctxPtr, TWK_NUM, tVal) \139do { \140(ctxPtr)->h.T[TWK_NUM] = (tVal); \141} while (0)142143#define Skein_Get_T0(ctxPtr) Skein_Get_Tweak(ctxPtr, 0)144#define Skein_Get_T1(ctxPtr) Skein_Get_Tweak(ctxPtr, 1)145#define Skein_Set_T0(ctxPtr, T0) Skein_Set_Tweak(ctxPtr, 0, T0)146#define Skein_Set_T1(ctxPtr, T1) Skein_Set_Tweak(ctxPtr, 1, T1)147148/* set both tweak words at once */149#define Skein_Set_T0_T1(ctxPtr, T0, T1) \150do { \151Skein_Set_T0(ctxPtr, (T0)); \152Skein_Set_T1(ctxPtr, (T1)); \153} while (0)154155#define Skein_Set_Type(ctxPtr, BLK_TYPE) \156Skein_Set_T1(ctxPtr, SKEIN_T1_BLK_TYPE_##BLK_TYPE)157158/*159* set up for starting with a new type: h.T[0]=0; h.T[1] = NEW_TYPE; h.bCnt=0;160*/161#define Skein_Start_New_Type(ctxPtr, BLK_TYPE) \162do { \163Skein_Set_T0_T1(ctxPtr, 0, SKEIN_T1_FLAG_FIRST | \164SKEIN_T1_BLK_TYPE_ ## BLK_TYPE); \165(ctxPtr)->h.bCnt = 0; \166} while (0)167168#define Skein_Clear_First_Flag(hdr) \169do { \170(hdr).T[1] &= ~SKEIN_T1_FLAG_FIRST; \171} while (0)172#define Skein_Set_Bit_Pad_Flag(hdr) \173do { \174(hdr).T[1] |= SKEIN_T1_FLAG_BIT_PAD; \175} while (0)176177#define Skein_Set_Tree_Level(hdr, height) \178do { \179(hdr).T[1] |= SKEIN_T1_TREE_LEVEL(height); \180} while (0)181182/*183* "Internal" Skein definitions for debugging and error checking184* Note: in Illumos we always disable debugging features.185*/186#define Skein_Show_Block(bits, ctx, X, blkPtr, wPtr, ksEvenPtr, ksOddPtr)187#define Skein_Show_Round(bits, ctx, r, X)188#define Skein_Show_R_Ptr(bits, ctx, r, X_ptr)189#define Skein_Show_Final(bits, ctx, cnt, outPtr)190#define Skein_Show_Key(bits, ctx, key, keyBytes)191192/* run-time checks (e.g., bad params, uninitialized context)? */193#ifndef SKEIN_ERR_CHECK194/* default: ignore all Asserts, for performance */195#define Skein_Assert(x, retCode)196#define Skein_assert(x)197#elif defined(SKEIN_ASSERT)198#include <sys/debug.h>199#define Skein_Assert(x, retCode) ASSERT(x)200#define Skein_assert(x) ASSERT(x)201#else202#include <sys/debug.h>203/* caller error */204#define Skein_Assert(x, retCode) \205do { \206if (!(x)) \207return (retCode); \208} while (0)209/* internal error */210#define Skein_assert(x) ASSERT(x)211#endif212213/*214* Skein block function constants (shared across Ref and Opt code)215*/216enum {217/* Skein_256 round rotation constants */218R_256_0_0 = 14, R_256_0_1 = 16,219R_256_1_0 = 52, R_256_1_1 = 57,220R_256_2_0 = 23, R_256_2_1 = 40,221R_256_3_0 = 5, R_256_3_1 = 37,222R_256_4_0 = 25, R_256_4_1 = 33,223R_256_5_0 = 46, R_256_5_1 = 12,224R_256_6_0 = 58, R_256_6_1 = 22,225R_256_7_0 = 32, R_256_7_1 = 32,226227/* Skein_512 round rotation constants */228R_512_0_0 = 46, R_512_0_1 = 36, R_512_0_2 = 19, R_512_0_3 = 37,229R_512_1_0 = 33, R_512_1_1 = 27, R_512_1_2 = 14, R_512_1_3 = 42,230R_512_2_0 = 17, R_512_2_1 = 49, R_512_2_2 = 36, R_512_2_3 = 39,231R_512_3_0 = 44, R_512_3_1 = 9, R_512_3_2 = 54, R_512_3_3 = 56,232R_512_4_0 = 39, R_512_4_1 = 30, R_512_4_2 = 34, R_512_4_3 = 24,233R_512_5_0 = 13, R_512_5_1 = 50, R_512_5_2 = 10, R_512_5_3 = 17,234R_512_6_0 = 25, R_512_6_1 = 29, R_512_6_2 = 39, R_512_6_3 = 43,235R_512_7_0 = 8, R_512_7_1 = 35, R_512_7_2 = 56, R_512_7_3 = 22,236237/* Skein1024 round rotation constants */238R1024_0_0 = 24, R1024_0_1 = 13, R1024_0_2 = 8, R1024_0_3 =23947, R1024_0_4 = 8, R1024_0_5 = 17, R1024_0_6 = 22, R1024_0_7 = 37,240R1024_1_0 = 38, R1024_1_1 = 19, R1024_1_2 = 10, R1024_1_3 =24155, R1024_1_4 = 49, R1024_1_5 = 18, R1024_1_6 = 23, R1024_1_7 = 52,242R1024_2_0 = 33, R1024_2_1 = 4, R1024_2_2 = 51, R1024_2_3 =24313, R1024_2_4 = 34, R1024_2_5 = 41, R1024_2_6 = 59, R1024_2_7 = 17,244R1024_3_0 = 5, R1024_3_1 = 20, R1024_3_2 = 48, R1024_3_3 =24541, R1024_3_4 = 47, R1024_3_5 = 28, R1024_3_6 = 16, R1024_3_7 = 25,246R1024_4_0 = 41, R1024_4_1 = 9, R1024_4_2 = 37, R1024_4_3 =24731, R1024_4_4 = 12, R1024_4_5 = 47, R1024_4_6 = 44, R1024_4_7 = 30,248R1024_5_0 = 16, R1024_5_1 = 34, R1024_5_2 = 56, R1024_5_3 =24951, R1024_5_4 = 4, R1024_5_5 = 53, R1024_5_6 = 42, R1024_5_7 = 41,250R1024_6_0 = 31, R1024_6_1 = 44, R1024_6_2 = 47, R1024_6_3 =25146, R1024_6_4 = 19, R1024_6_5 = 42, R1024_6_6 = 44, R1024_6_7 = 25,252R1024_7_0 = 9, R1024_7_1 = 48, R1024_7_2 = 35, R1024_7_3 =25352, R1024_7_4 = 23, R1024_7_5 = 31, R1024_7_6 = 37, R1024_7_7 = 20254};255256/* number of rounds for the different block sizes */257#define SKEIN_256_ROUNDS_TOTAL (72)258#define SKEIN_512_ROUNDS_TOTAL (72)259#define SKEIN1024_ROUNDS_TOTAL (80)260261262extern const uint64_t SKEIN_256_IV_128[];263extern const uint64_t SKEIN_256_IV_160[];264extern const uint64_t SKEIN_256_IV_224[];265extern const uint64_t SKEIN_256_IV_256[];266extern const uint64_t SKEIN_512_IV_224[];267extern const uint64_t SKEIN_512_IV_256[];268extern const uint64_t SKEIN_512_IV_384[];269extern const uint64_t SKEIN_512_IV_512[];270extern const uint64_t SKEIN1024_IV_384[];271extern const uint64_t SKEIN1024_IV_512[];272extern const uint64_t SKEIN1024_IV_1024[];273274/* Functions to process blkCnt (nonzero) full block(s) of data. */275void Skein_256_Process_Block(Skein_256_Ctxt_t *ctx, const uint8_t *blkPtr,276size_t blkCnt, size_t byteCntAdd);277void Skein_512_Process_Block(Skein_512_Ctxt_t *ctx, const uint8_t *blkPtr,278size_t blkCnt, size_t byteCntAdd);279void Skein1024_Process_Block(Skein1024_Ctxt_t *ctx, const uint8_t *blkPtr,280size_t blkCnt, size_t byteCntAdd);281282#endif /* _SKEIN_IMPL_H_ */283284285