xref: /freebsd/sys/contrib/openzfs/include/sys/sha2.h (revision f126890a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or https://opensource.org/licenses/CDDL-1.0.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2022 Tino Reichardt <milky-zfs@mcmilk.de>
24  */
25 
26 #ifndef	_SYS_SHA2_H
27 #define	_SYS_SHA2_H
28 
29 #ifdef  _KERNEL
30 #include <sys/types.h>
31 #else
32 #include <stdint.h>
33 #include <stdlib.h>
34 #endif
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 #define	SHA224_BLOCK_LENGTH		64
41 #define	SHA256_BLOCK_LENGTH		64
42 #define	SHA384_BLOCK_LENGTH		128
43 #define	SHA512_BLOCK_LENGTH		128
44 
45 #define	SHA224_DIGEST_LENGTH		28
46 #define	SHA256_DIGEST_LENGTH		32
47 #define	SHA384_DIGEST_LENGTH		48
48 #define	SHA512_DIGEST_LENGTH		64
49 
50 #define	SHA512_224_DIGEST_LENGTH	28
51 #define	SHA512_256_DIGEST_LENGTH	32
52 
53 #define	SHA256_HMAC_BLOCK_SIZE		64
54 #define	SHA512_HMAC_BLOCK_SIZE		128
55 
56 /* sha256 context */
57 typedef struct {
58 	uint32_t state[8];
59 	uint64_t count[2];
60 	uint8_t wbuf[64];
61 
62 	/* const sha256_ops_t *ops */
63 	const void *ops;
64 } sha256_ctx;
65 
66 /* sha512 context */
67 typedef struct {
68 	uint64_t state[8];
69 	uint64_t count[2];
70 	uint8_t wbuf[128];
71 
72 	/* const sha256_ops_t *ops */
73 	const void *ops;
74 } sha512_ctx;
75 
76 /* SHA2 context */
77 typedef struct {
78 	union {
79 		sha256_ctx sha256;
80 		sha512_ctx sha512;
81 	};
82 
83 	/* algorithm type */
84 	int algotype;
85 } SHA2_CTX;
86 
87 /* SHA2 algorithm types */
88 typedef enum sha2_mech_type {
89 	SHA256_MECH_INFO_TYPE,		/* SUN_CKM_SHA256 */
90 	SHA256_HMAC_MECH_INFO_TYPE,	/* SUN_CKM_SHA256_HMAC */
91 	SHA256_HMAC_GEN_MECH_INFO_TYPE,	/* SUN_CKM_SHA256_HMAC_GENERAL */
92 	SHA384_MECH_INFO_TYPE,		/* SUN_CKM_SHA384 */
93 	SHA384_HMAC_MECH_INFO_TYPE,	/* SUN_CKM_SHA384_HMAC */
94 	SHA384_HMAC_GEN_MECH_INFO_TYPE,	/* SUN_CKM_SHA384_HMAC_GENERAL */
95 	SHA512_MECH_INFO_TYPE,		/* SUN_CKM_SHA512 */
96 	SHA512_HMAC_MECH_INFO_TYPE,	/* SUN_CKM_SHA512_HMAC */
97 	SHA512_HMAC_GEN_MECH_INFO_TYPE,	/* SUN_CKM_SHA512_HMAC_GENERAL */
98 	SHA512_224_MECH_INFO_TYPE,	/* SUN_CKM_SHA512_224 */
99 	SHA512_256_MECH_INFO_TYPE	/* SUN_CKM_SHA512_256 */
100 } sha2_mech_type_t;
101 
102 #define	SHA256			0
103 #define	SHA256_HMAC		1
104 #define	SHA256_HMAC_GEN		2
105 #define	SHA384			3
106 #define	SHA384_HMAC		4
107 #define	SHA384_HMAC_GEN		5
108 #define	SHA512			6
109 #define	SHA512_HMAC		7
110 #define	SHA512_HMAC_GEN		8
111 #define	SHA512_224		9
112 #define	SHA512_256		10
113 
114 /* SHA2 Init function */
115 extern void SHA2Init(int algotype, SHA2_CTX *ctx);
116 
117 /* SHA2 Update function */
118 extern void SHA2Update(SHA2_CTX *ctx, const void *data, size_t len);
119 
120 /* SHA2 Final function */
121 extern void SHA2Final(void *digest, SHA2_CTX *ctx);
122 
123 #ifdef __cplusplus
124 }
125 #endif
126 
127 #endif	/* SYS_SHA2_H */
128