1193323Sed/*
2193323Sed * CDDL HEADER START
3193323Sed *
4193323Sed * The contents of this file are subject to the terms of the
5193323Sed * Common Development and Distribution License (the "License").
6193323Sed * You may not use this file except in compliance with the License.
7193323Sed *
8193323Sed * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9193323Sed * or https://opensource.org/licenses/CDDL-1.0.
10193323Sed * See the License for the specific language governing permissions
11193323Sed * and limitations under the License.
12193323Sed *
13210299Sed * When distributing Covered Code, include this CDDL HEADER in each
14210299Sed * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15210299Sed * If applicable, add the following below this CDDL HEADER, with the
16210299Sed * fields enclosed by brackets "[]" replaced with your own identifying
17193323Sed * information: Portions Copyright [yyyy] [name of copyright owner]
18193323Sed *
19193323Sed * CDDL HEADER END
20193323Sed */
21193323Sed
22193323Sed/*
23261991Sdim * Copyright (c) 2022 Tino Reichardt <milky-zfs@mcmilk.de>
24218893Sdim */
25193323Sed
26193323Sed#ifndef	_SYS_SHA2_H
27193323Sed#define	_SYS_SHA2_H
28193323Sed
29193323Sed#ifdef  _KERNEL
30193323Sed#include <sys/types.h>
31193323Sed#else
32226633Sdim#include <stdint.h>
33193323Sed#include <stdlib.h>
34226633Sdim#endif
35193323Sed
36193323Sed#ifdef __cplusplus
37193323Sedextern "C" {
38193323Sed#endif
39226633Sdim
40226633Sdim#define	SHA224_BLOCK_LENGTH		64
41193323Sed#define	SHA256_BLOCK_LENGTH		64
42226633Sdim#define	SHA384_BLOCK_LENGTH		128
43193323Sed#define	SHA512_BLOCK_LENGTH		128
44193323Sed
45193323Sed#define	SHA224_DIGEST_LENGTH		28
46193323Sed#define	SHA256_DIGEST_LENGTH		32
47193323Sed#define	SHA384_DIGEST_LENGTH		48
48210299Sed#define	SHA512_DIGEST_LENGTH		64
49210299Sed
50210299Sed#define	SHA512_224_DIGEST_LENGTH	28
51210299Sed#define	SHA512_256_DIGEST_LENGTH	32
52210299Sed
53210299Sed#define	SHA256_HMAC_BLOCK_SIZE		64
54210299Sed#define	SHA512_HMAC_BLOCK_SIZE		128
55210299Sed
56210299Sed/* sha256 context */
57210299Sedtypedef struct {
58210299Sed	uint32_t state[8];
59210299Sed	uint64_t count[2];
60210299Sed	uint8_t wbuf[64];
61210299Sed
62210299Sed	/* const sha256_ops_t *ops */
63193323Sed	const void *ops;
64210299Sed} sha256_ctx;
65193323Sed
66193323Sed/* sha512 context */
67193323Sedtypedef struct {
68193323Sed	uint64_t state[8];
69193323Sed	uint64_t count[2];
70193323Sed	uint8_t wbuf[128];
71193323Sed
72193323Sed	/* const sha256_ops_t *ops */
73193323Sed	const void *ops;
74193323Sed} sha512_ctx;
75193323Sed
76193323Sed/* SHA2 context */
77193323Sedtypedef struct {
78210299Sed	union {
79210299Sed		sha256_ctx sha256;
80193323Sed		sha512_ctx sha512;
81210299Sed	};
82210299Sed
83193323Sed	/* algorithm type */
84193323Sed	int algotype;
85193323Sed} SHA2_CTX;
86193323Sed
87193323Sed/* SHA2 algorithm types */
88210299Sedtypedef enum sha2_mech_type {
89226633Sdim	SHA256_MECH_INFO_TYPE,		/* SUN_CKM_SHA256 */
90226633Sdim	SHA256_HMAC_MECH_INFO_TYPE,	/* SUN_CKM_SHA256_HMAC */
91226633Sdim	SHA256_HMAC_GEN_MECH_INFO_TYPE,	/* SUN_CKM_SHA256_HMAC_GENERAL */
92193323Sed	SHA384_MECH_INFO_TYPE,		/* SUN_CKM_SHA384 */
93193323Sed	SHA384_HMAC_MECH_INFO_TYPE,	/* SUN_CKM_SHA384_HMAC */
94193323Sed	SHA384_HMAC_GEN_MECH_INFO_TYPE,	/* SUN_CKM_SHA384_HMAC_GENERAL */
95226633Sdim	SHA512_MECH_INFO_TYPE,		/* SUN_CKM_SHA512 */
96226633Sdim	SHA512_HMAC_MECH_INFO_TYPE,	/* SUN_CKM_SHA512_HMAC */
97226633Sdim	SHA512_HMAC_GEN_MECH_INFO_TYPE,	/* SUN_CKM_SHA512_HMAC_GENERAL */
98226633Sdim	SHA512_224_MECH_INFO_TYPE,	/* SUN_CKM_SHA512_224 */
99226633Sdim	SHA512_256_MECH_INFO_TYPE	/* SUN_CKM_SHA512_256 */
100226633Sdim} sha2_mech_type_t;
101226633Sdim
102210299Sed#define	SHA256			0
103210299Sed#define	SHA256_HMAC		1
104210299Sed#define	SHA256_HMAC_GEN		2
105210299Sed#define	SHA384			3
106210299Sed#define	SHA384_HMAC		4
107193323Sed#define	SHA384_HMAC_GEN		5
108193323Sed#define	SHA512			6
109193323Sed#define	SHA512_HMAC		7
110193323Sed#define	SHA512_HMAC_GEN		8
111193323Sed#define	SHA512_224		9
112193323Sed#define	SHA512_256		10
113193323Sed
114193323Sed/* SHA2 Init function */
115193323Sedextern void SHA2Init(int algotype, SHA2_CTX *ctx);
116193323Sed
117193323Sed/* SHA2 Update function */
118193323Sedextern void SHA2Update(SHA2_CTX *ctx, const void *data, size_t len);
119193323Sed
120207618Srdivacky/* SHA2 Final function */
121207618Srdivackyextern void SHA2Final(void *digest, SHA2_CTX *ctx);
122207618Srdivacky
123207618Srdivacky#ifdef __cplusplus
124207618Srdivacky}
125207618Srdivacky#endif
126193323Sed
127276479Sdim#endif	/* SYS_SHA2_H */
128276479Sdim