xref: /freebsd/sys/contrib/openzfs/include/sys/skein.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: LicenseRef-OpenZFS-ThirdParty-PublicDomain
2 /*
3  * Interface declarations for Skein hashing.
4  * Source code author: Doug Whiting, 2008.
5  * This algorithm and source code is released to the public domain.
6  *
7  * The following compile-time switches may be defined to control some
8  * tradeoffs between speed, code size, error checking, and security.
9  *
10  * The "default" note explains what happens when the switch is not defined.
11  *
12  *  SKEIN_DEBUG            -- make callouts from inside Skein code
13  *                            to examine/display intermediate values.
14  *                            [default: no callouts (no overhead)]
15  *
16  *  SKEIN_ERR_CHECK        -- how error checking is handled inside Skein
17  *                            code. If not defined, most error checking
18  *                            is disabled (for performance). Otherwise,
19  *                            the switch value is interpreted as:
20  *                                0: use assert()      to flag errors
21  *                                1: return SKEIN_FAIL to flag errors
22  */
23 /* Copyright 2013 Doug Whiting. This code is released to the public domain. */
24 #ifndef	_SYS_SKEIN_H_
25 #define	_SYS_SKEIN_H_
26 
27 #ifdef  _KERNEL
28 #include <sys/types.h>		/* get size_t definition */
29 #else
30 #include <stdint.h>
31 #include <stdlib.h>
32 #endif
33 
34 #ifdef	__cplusplus
35 extern "C" {
36 #endif
37 
38 enum {
39 	SKEIN_SUCCESS = 0,	/* return codes from Skein calls */
40 	SKEIN_FAIL = 1,
41 	SKEIN_BAD_HASHLEN = 2
42 };
43 
44 #define	SKEIN_MODIFIER_WORDS	(2)	/* number of modifier (tweak) words */
45 
46 #define	SKEIN_256_STATE_WORDS	(4)
47 #define	SKEIN_512_STATE_WORDS	(8)
48 #define	SKEIN1024_STATE_WORDS	(16)
49 #define	SKEIN_MAX_STATE_WORDS	(16)
50 
51 #define	SKEIN_256_STATE_BYTES	(8 * SKEIN_256_STATE_WORDS)
52 #define	SKEIN_512_STATE_BYTES	(8 * SKEIN_512_STATE_WORDS)
53 #define	SKEIN1024_STATE_BYTES	(8 * SKEIN1024_STATE_WORDS)
54 
55 #define	SKEIN_256_STATE_BITS	(64 * SKEIN_256_STATE_WORDS)
56 #define	SKEIN_512_STATE_BITS	(64 * SKEIN_512_STATE_WORDS)
57 #define	SKEIN1024_STATE_BITS	(64 * SKEIN1024_STATE_WORDS)
58 
59 #define	SKEIN_256_BLOCK_BYTES	(8 * SKEIN_256_STATE_WORDS)
60 #define	SKEIN_512_BLOCK_BYTES	(8 * SKEIN_512_STATE_WORDS)
61 #define	SKEIN1024_BLOCK_BYTES	(8 * SKEIN1024_STATE_WORDS)
62 
63 typedef struct {
64 	size_t hashBitLen;	/* size of hash result, in bits */
65 	size_t bCnt;		/* current byte count in buffer b[] */
66 	/* tweak words: T[0]=byte cnt, T[1]=flags */
67 	uint64_t T[SKEIN_MODIFIER_WORDS];
68 } Skein_Ctxt_Hdr_t;
69 
70 typedef struct {		/*  256-bit Skein hash context structure */
71 	Skein_Ctxt_Hdr_t h;	/* common header context variables */
72 	uint64_t X[SKEIN_256_STATE_WORDS];	/* chaining variables */
73 	/* partial block buffer (8-byte aligned) */
74 	uint8_t b[SKEIN_256_BLOCK_BYTES];
75 } Skein_256_Ctxt_t;
76 
77 typedef struct {		/*  512-bit Skein hash context structure */
78 	Skein_Ctxt_Hdr_t h;	/* common header context variables */
79 	uint64_t X[SKEIN_512_STATE_WORDS];	/* chaining variables */
80 	/* partial block buffer (8-byte aligned) */
81 	uint8_t b[SKEIN_512_BLOCK_BYTES];
82 } Skein_512_Ctxt_t;
83 
84 typedef struct {		/* 1024-bit Skein hash context structure */
85 	Skein_Ctxt_Hdr_t h;	/* common header context variables */
86 	uint64_t X[SKEIN1024_STATE_WORDS];	/* chaining variables */
87 	/* partial block buffer (8-byte aligned) */
88 	uint8_t b[SKEIN1024_BLOCK_BYTES];
89 } Skein1024_Ctxt_t;
90 
91 /*   Skein APIs for (incremental) "straight hashing" */
92 int Skein_256_Init(Skein_256_Ctxt_t *ctx, size_t hashBitLen);
93 int Skein_512_Init(Skein_512_Ctxt_t *ctx, size_t hashBitLen);
94 int Skein1024_Init(Skein1024_Ctxt_t *ctx, size_t hashBitLen);
95 
96 int Skein_256_Update(Skein_256_Ctxt_t *ctx, const uint8_t *msg,
97     size_t msgByteCnt);
98 int Skein_512_Update(Skein_512_Ctxt_t *ctx, const uint8_t *msg,
99     size_t msgByteCnt);
100 int Skein1024_Update(Skein1024_Ctxt_t *ctx, const uint8_t *msg,
101     size_t msgByteCnt);
102 
103 int Skein_256_Final(Skein_256_Ctxt_t *ctx, uint8_t *hashVal);
104 int Skein_512_Final(Skein_512_Ctxt_t *ctx, uint8_t *hashVal);
105 int Skein1024_Final(Skein1024_Ctxt_t *ctx, uint8_t *hashVal);
106 
107 /*
108  * Skein APIs for "extended" initialization: MAC keys, tree hashing.
109  * After an InitExt() call, just use Update/Final calls as with Init().
110  *
111  * Notes: Same parameters as _Init() calls, plus treeInfo/key/keyBytes.
112  *          When keyBytes == 0 and treeInfo == SKEIN_SEQUENTIAL,
113  *              the results of InitExt() are identical to calling Init().
114  *          The function Init() may be called once to "precompute" the IV for
115  *              a given hashBitLen value, then by saving a copy of the context
116  *              the IV computation may be avoided in later calls.
117  *          Similarly, the function InitExt() may be called once per MAC key
118  *              to precompute the MAC IV, then a copy of the context saved and
119  *              reused for each new MAC computation.
120  */
121 int Skein_256_InitExt(Skein_256_Ctxt_t *ctx, size_t hashBitLen,
122     uint64_t treeInfo, const uint8_t *key, size_t keyBytes);
123 int Skein_512_InitExt(Skein_512_Ctxt_t *ctx, size_t hashBitLen,
124     uint64_t treeInfo, const uint8_t *key, size_t keyBytes);
125 int Skein1024_InitExt(Skein1024_Ctxt_t *ctx, size_t hashBitLen,
126     uint64_t treeInfo, const uint8_t *key, size_t keyBytes);
127 
128 /*
129  * Skein APIs for MAC and tree hash:
130  *	Final_Pad: pad, do final block, but no OUTPUT type
131  *	Output:    do just the output stage
132  */
133 int Skein_256_Final_Pad(Skein_256_Ctxt_t *ctx, uint8_t *hashVal);
134 int Skein_512_Final_Pad(Skein_512_Ctxt_t *ctx, uint8_t *hashVal);
135 int Skein1024_Final_Pad(Skein1024_Ctxt_t *ctx, uint8_t *hashVal);
136 
137 #ifndef	SKEIN_TREE_HASH
138 #define	SKEIN_TREE_HASH (1)
139 #endif
140 #if	SKEIN_TREE_HASH
141 int Skein_256_Output(Skein_256_Ctxt_t *ctx, uint8_t *hashVal);
142 int Skein_512_Output(Skein_512_Ctxt_t *ctx, uint8_t *hashVal);
143 int Skein1024_Output(Skein1024_Ctxt_t *ctx, uint8_t *hashVal);
144 #endif
145 
146 /*
147  * When you initialize a Skein KCF hashing method you can pass this param
148  * structure in cm_param to fine-tune the algorithm's defaults.
149  */
150 typedef struct skein_param {
151 	size_t	sp_digest_bitlen;		/* length of digest in bits */
152 } skein_param_t;
153 
154 /* Module definitions */
155 #ifdef	SKEIN_MODULE_IMPL
156 #define	CKM_SKEIN_256_MAC			"CKM_SKEIN_256_MAC"
157 #define	CKM_SKEIN_512_MAC			"CKM_SKEIN_512_MAC"
158 #define	CKM_SKEIN1024_MAC			"CKM_SKEIN1024_MAC"
159 
160 typedef enum skein_mech_type {
161 	SKEIN_256_MAC_MECH_INFO_TYPE,
162 	SKEIN_512_MAC_MECH_INFO_TYPE,
163 	SKEIN1024_MAC_MECH_INFO_TYPE
164 } skein_mech_type_t;
165 
166 #define	VALID_SKEIN_MAC_MECH(__mech)				\
167 	((int)(__mech) >= SKEIN_256_MAC_MECH_INFO_TYPE &&	\
168 	(__mech) <= SKEIN1024_MAC_MECH_INFO_TYPE)
169 #endif	/* SKEIN_MODULE_IMPL */
170 
171 #ifdef	__cplusplus
172 }
173 #endif
174 
175 #endif	/* _SYS_SKEIN_H_ */
176