]> git.proxmox.com Git - mirror_zfs.git/blob - include/sys/sha2.h
Serialize ZTHR operations to eliminate races
[mirror_zfs.git] / include / sys / sha2.h
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 http://www.opensolaris.org/os/licensing.
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 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25 /* Copyright 2013 Saso Kiselkov. All rights reserved. */
26
27 #ifndef _SYS_SHA2_H
28 #define _SYS_SHA2_H
29
30 #ifdef _KERNEL
31 #include <sys/types.h> /* for uint_* */
32 #else
33 #include <stdint.h>
34 #endif
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 #define SHA2_HMAC_MIN_KEY_LEN 1 /* SHA2-HMAC min key length in bytes */
41 #define SHA2_HMAC_MAX_KEY_LEN INT_MAX /* SHA2-HMAC max key length in bytes */
42
43 #define SHA256_DIGEST_LENGTH 32 /* SHA256 digest length in bytes */
44 #define SHA384_DIGEST_LENGTH 48 /* SHA384 digest length in bytes */
45 #define SHA512_DIGEST_LENGTH 64 /* SHA512 digest length in bytes */
46
47 /* Truncated versions of SHA-512 according to FIPS-180-4, section 5.3.6 */
48 #define SHA512_224_DIGEST_LENGTH 28 /* SHA512/224 digest length */
49 #define SHA512_256_DIGEST_LENGTH 32 /* SHA512/256 digest length */
50
51 #define SHA256_HMAC_BLOCK_SIZE 64 /* SHA256-HMAC block size */
52 #define SHA512_HMAC_BLOCK_SIZE 128 /* SHA512-HMAC block size */
53
54 #define SHA256 0
55 #define SHA256_HMAC 1
56 #define SHA256_HMAC_GEN 2
57 #define SHA384 3
58 #define SHA384_HMAC 4
59 #define SHA384_HMAC_GEN 5
60 #define SHA512 6
61 #define SHA512_HMAC 7
62 #define SHA512_HMAC_GEN 8
63 #define SHA512_224 9
64 #define SHA512_256 10
65
66 /*
67 * SHA2 context.
68 * The contents of this structure are a private interface between the
69 * Init/Update/Final calls of the functions defined below.
70 * Callers must never attempt to read or write any of the fields
71 * in this structure directly.
72 */
73 typedef struct {
74 uint32_t algotype; /* Algorithm Type */
75
76 /* state (ABCDEFGH) */
77 union {
78 uint32_t s32[8]; /* for SHA256 */
79 uint64_t s64[8]; /* for SHA384/512 */
80 } state;
81 /* number of bits */
82 union {
83 uint32_t c32[2]; /* for SHA256 , modulo 2^64 */
84 uint64_t c64[2]; /* for SHA384/512, modulo 2^128 */
85 } count;
86 union {
87 uint8_t buf8[128]; /* undigested input */
88 uint32_t buf32[32]; /* realigned input */
89 uint64_t buf64[16]; /* realigned input */
90 } buf_un;
91 } SHA2_CTX;
92
93 typedef SHA2_CTX SHA256_CTX;
94 typedef SHA2_CTX SHA384_CTX;
95 typedef SHA2_CTX SHA512_CTX;
96
97 extern void SHA2Init(uint64_t mech, SHA2_CTX *);
98
99 extern void SHA2Update(SHA2_CTX *, const void *, size_t);
100
101 extern void SHA2Final(void *, SHA2_CTX *);
102
103 extern void SHA256Init(SHA256_CTX *);
104
105 extern void SHA256Update(SHA256_CTX *, const void *, size_t);
106
107 extern void SHA256Final(void *, SHA256_CTX *);
108
109 extern void SHA384Init(SHA384_CTX *);
110
111 extern void SHA384Update(SHA384_CTX *, const void *, size_t);
112
113 extern void SHA384Final(void *, SHA384_CTX *);
114
115 extern void SHA512Init(SHA512_CTX *);
116
117 extern void SHA512Update(SHA512_CTX *, const void *, size_t);
118
119 extern void SHA512Final(void *, SHA512_CTX *);
120
121 #ifdef _SHA2_IMPL
122 /*
123 * The following types/functions are all private to the implementation
124 * of the SHA2 functions and must not be used by consumers of the interface
125 */
126
127 /*
128 * List of support mechanisms in this module.
129 *
130 * It is important to note that in the module, division or modulus calculations
131 * are used on the enumerated type to determine which mechanism is being used;
132 * therefore, changing the order or additional mechanisms should be done
133 * carefully
134 */
135 typedef enum sha2_mech_type {
136 SHA256_MECH_INFO_TYPE, /* SUN_CKM_SHA256 */
137 SHA256_HMAC_MECH_INFO_TYPE, /* SUN_CKM_SHA256_HMAC */
138 SHA256_HMAC_GEN_MECH_INFO_TYPE, /* SUN_CKM_SHA256_HMAC_GENERAL */
139 SHA384_MECH_INFO_TYPE, /* SUN_CKM_SHA384 */
140 SHA384_HMAC_MECH_INFO_TYPE, /* SUN_CKM_SHA384_HMAC */
141 SHA384_HMAC_GEN_MECH_INFO_TYPE, /* SUN_CKM_SHA384_HMAC_GENERAL */
142 SHA512_MECH_INFO_TYPE, /* SUN_CKM_SHA512 */
143 SHA512_HMAC_MECH_INFO_TYPE, /* SUN_CKM_SHA512_HMAC */
144 SHA512_HMAC_GEN_MECH_INFO_TYPE, /* SUN_CKM_SHA512_HMAC_GENERAL */
145 SHA512_224_MECH_INFO_TYPE, /* SUN_CKM_SHA512_224 */
146 SHA512_256_MECH_INFO_TYPE /* SUN_CKM_SHA512_256 */
147 } sha2_mech_type_t;
148
149 #endif /* _SHA2_IMPL */
150
151 #ifdef __cplusplus
152 }
153 #endif
154
155 #endif /* _SYS_SHA2_H */