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 * 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 #include <sys/types.h> /* for uint_* */
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 #define SHA256_DIGEST_LENGTH 32 /* SHA256 digest length in bytes */
37 #define SHA384_DIGEST_LENGTH 48 /* SHA384 digest length in bytes */
38 #define SHA512_DIGEST_LENGTH 64 /* SHA512 digest length in bytes */
39
40 /* Truncated versions of SHA-512 according to FIPS-180-4, section 5.3.6 */
41 #define SHA512_224_DIGEST_LENGTH 28 /* SHA512/224 digest length */
42 #define SHA512_256_DIGEST_LENGTH 32 /* SHA512/256 digest length */
43
44 #define SHA256_HMAC_BLOCK_SIZE 64 /* SHA256-HMAC block size */
45 #define SHA512_HMAC_BLOCK_SIZE 128 /* SHA512-HMAC block size */
46
47 #define SHA256 0
48 #define SHA256_HMAC 1
49 #define SHA256_HMAC_GEN 2
50 #define SHA384 3
51 #define SHA384_HMAC 4
52 #define SHA384_HMAC_GEN 5
53 #define SHA512 6
54 #define SHA512_HMAC 7
55 #define SHA512_HMAC_GEN 8
56 #define SHA512_224 9
57 #define SHA512_256 10
58
59 /*
60 * SHA2 context.
61 * The contents of this structure are a private interface between the
62 * Init/Update/Final calls of the functions defined below.
63 * Callers must never attempt to read or write any of the fields
64 * in this structure directly.
65 */
66
67 #include <crypto/sha2/sha256.h>
68 #include <crypto/sha2/sha384.h>
69 #include <crypto/sha2/sha512.h>
70 #include <crypto/sha2/sha512t.h>
71 typedef struct {
72 uint32_t algotype; /* Algorithm Type */
73 union {
74 SHA256_CTX SHA256_ctx;
75 SHA384_CTX SHA384_ctx;
76 SHA512_CTX SHA512_ctx;
77 };
78 } SHA2_CTX;
79
80 extern void SHA256Init(SHA256_CTX *);
81
82 extern void SHA256Update(SHA256_CTX *, const void *, size_t);
83
84 extern void SHA256Final(void *, SHA256_CTX *);
85
86 extern void SHA384Init(SHA384_CTX *);
87
88 extern void SHA384Update(SHA384_CTX *, const void *, size_t);
89
90 extern void SHA384Final(void *, SHA384_CTX *);
91
92 extern void SHA512Init(SHA512_CTX *);
93
94 extern void SHA512Update(SHA512_CTX *, const void *, size_t);
95
96 extern void SHA512Final(void *, SHA512_CTX *);
97
98
99 static inline void
100 SHA2Init(uint64_t mech, SHA2_CTX *c)
101 {
102 switch (mech) {
103 case SHA256:
104 SHA256_Init(&c->SHA256_ctx);
105 break;
106 case SHA384:
107 SHA384_Init(&c->SHA384_ctx);
108 break;
109 case SHA512:
110 SHA512_Init(&c->SHA512_ctx);
111 break;
112 case SHA512_256:
113 SHA512_256_Init(&c->SHA512_ctx);
114 break;
115 default:
116 panic("unknown mechanism %ju", (uintmax_t)mech);
117 }
118 c->algotype = (uint32_t)mech;
119 }
120
121 static inline void
122 SHA2Update(SHA2_CTX *c, const void *p, size_t s)
123 {
124 switch (c->algotype) {
125 case SHA256:
126 SHA256_Update(&c->SHA256_ctx, p, s);
127 break;
128 case SHA384:
129 SHA384_Update(&c->SHA384_ctx, p, s);
130 break;
131 case SHA512:
132 SHA512_Update(&c->SHA512_ctx, p, s);
133 break;
134 case SHA512_256:
135 SHA512_256_Update(&c->SHA512_ctx, p, s);
136 break;
137 default:
138 panic("unknown mechanism %d", c->algotype);
139 }
140 }
141
142 static inline void
143 SHA2Final(void *p, SHA2_CTX *c)
144 {
145 switch (c->algotype) {
146 case SHA256:
147 SHA256_Final(p, &c->SHA256_ctx);
148 break;
149 case SHA384:
150 SHA384_Final(p, &c->SHA384_ctx);
151 break;
152 case SHA512:
153 SHA512_Final(p, &c->SHA512_ctx);
154 break;
155 case SHA512_256:
156 SHA512_256_Final(p, &c->SHA512_ctx);
157 break;
158 default:
159 panic("unknown mechanism %d", c->algotype);
160 }
161 }
162
163 #ifdef _SHA2_IMPL
164 /*
165 * The following types/functions are all private to the implementation
166 * of the SHA2 functions and must not be used by consumers of the interface
167 */
168
169 /*
170 * List of support mechanisms in this module.
171 *
172 * It is important to note that in the module, division or modulus calculations
173 * are used on the enumerated type to determine which mechanism is being used;
174 * therefore, changing the order or additional mechanisms should be done
175 * carefully
176 */
177 typedef enum sha2_mech_type {
178 SHA256_MECH_INFO_TYPE, /* SUN_CKM_SHA256 */
179 SHA256_HMAC_MECH_INFO_TYPE, /* SUN_CKM_SHA256_HMAC */
180 SHA256_HMAC_GEN_MECH_INFO_TYPE, /* SUN_CKM_SHA256_HMAC_GENERAL */
181 SHA384_MECH_INFO_TYPE, /* SUN_CKM_SHA384 */
182 SHA384_HMAC_MECH_INFO_TYPE, /* SUN_CKM_SHA384_HMAC */
183 SHA384_HMAC_GEN_MECH_INFO_TYPE, /* SUN_CKM_SHA384_HMAC_GENERAL */
184 SHA512_MECH_INFO_TYPE, /* SUN_CKM_SHA512 */
185 SHA512_HMAC_MECH_INFO_TYPE, /* SUN_CKM_SHA512_HMAC */
186 SHA512_HMAC_GEN_MECH_INFO_TYPE, /* SUN_CKM_SHA512_HMAC_GENERAL */
187 SHA512_224_MECH_INFO_TYPE, /* SUN_CKM_SHA512_224 */
188 SHA512_256_MECH_INFO_TYPE /* SUN_CKM_SHA512_256 */
189 } sha2_mech_type_t;
190
191 #endif /* _SHA2_IMPL */
192
193 #ifdef __cplusplus
194 }
195 #endif
196
197 #endif /* _SYS_SHA2_H */
Cache object: ae7d47071442b1e39e9b1bebc14b2950
|