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