1 /*
2  * FILE:    sha2.h
3  * AUTHOR:  Aaron D. Gifford
4  *          http://www.aarongifford.com/computers/sha.html
5  *
6  * Copyright (c) 2000-2003, Aaron D. Gifford
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the copyright holder nor the names of contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $Id: sha2.h,v 1.4 2004/01/07 19:06:18 adg Exp $
34  */
35 
36 #ifndef __SHA2_H__
37 #define __SHA2_H__
38 
39 /*[SK] refer to the Gauche's config */
40 #include <gauche/config.h>
41 #include "renaming.h"
42 
43 #include <stdint.h>
44 #include <inttypes.h>
45 #define SHA2_USE_INTTYPES_H
46 
47 /* gauche/config.h provides WORDS_BIGENDIAN. */
48 #undef BYTE_ORDER
49 #ifndef LITTLE_ENDIAN
50 #define LITTLE_ENDIAN 1234
51 #endif
52 #ifndef BIG_ENDIAN
53 #define BIG_ENDIAN 4321
54 #endif
55 #if WORDS_BIGENDIAN
56 #define BYTE_ORDER BIG_ENDIAN
57 #else
58 #define BYTE_ORDER LITTLE_ENDIAN
59 #endif
60 /*[/SK]*/
61 
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65 
66 
67 /*
68  * Import u_intXX_t size_t type definitions from system headers.  You
69  * may need to change this, or define these things yourself in this
70  * file.
71  */
72 #include <sys/types.h>
73 
74 #ifdef SHA2_USE_INTTYPES_H
75 
76 #include <inttypes.h>
77 
78 #endif /* SHA2_USE_INTTYPES_H */
79 
80 
81 /*** SHA-224/256/384/512 Various Length Definitions *******************/
82 
83 /* Digest lengths for SHA-1/224/256/384/512 */
84 #define   SHA1_DIGEST_LENGTH          20
85 #define   SHA1_DIGEST_STRING_LENGTH  (SHA1_DIGEST_LENGTH   * 2 + 1)
86 #define SHA224_DIGEST_LENGTH          28
87 #define SHA224_DIGEST_STRING_LENGTH  (SHA224_DIGEST_LENGTH * 2 + 1)
88 #define SHA256_DIGEST_LENGTH          32
89 #define SHA256_DIGEST_STRING_LENGTH  (SHA256_DIGEST_LENGTH * 2 + 1)
90 #define SHA384_DIGEST_LENGTH          48
91 #define SHA384_DIGEST_STRING_LENGTH  (SHA384_DIGEST_LENGTH * 2 + 1)
92 #define SHA512_DIGEST_LENGTH          64
93 #define SHA512_DIGEST_STRING_LENGTH  (SHA512_DIGEST_LENGTH * 2 + 1)
94 
95 
96 /*** SHA-224/256/384/512 Context Structures ***************************/
97 /* NOTE: If your architecture does not define either u_intXX_t types or
98  * uintXX_t (from inttypes.h), you may need to define things by hand
99  * for your system:
100  */
101 #if 0
102 typedef unsigned char u_int8_t;        /* 1-byte  (8-bits)  */
103 typedef unsigned int  u_int32_t;       /* 4-bytes (32-bits) */
104 typedef unsigned long long u_int64_t;  /* 8-bytes (64-bits) */
105 #endif
106 /*
107  * Most BSD systems already define u_intXX_t types, as does Linux.
108  * Some systems, however, like Compaq's Tru64 Unix instead can use
109  * uintXX_t types defined by very recent ANSI C standards and included
110  * in the file:
111  *
112  *   #include <inttypes.h>
113  *
114  * If you choose to use <inttypes.h> then please define:
115  *
116  *   #define SHA2_USE_INTTYPES_H
117  *
118  * Or on the command line during compile:
119  *
120  *   cc -DSHA2_USE_INTTYPES_H ...
121  */
122 #ifdef SHA2_USE_INTTYPES_H
123 
124 typedef union _SHA_CTX {
125     /* SHA-1 uses this part of the union: */
126     struct {
127         uint32_t state[5];
128         uint64_t bitcount;
129         uint8_t  buffer[64];
130     } s1;
131 
132     /* SHA-224 and SHA-256 use this part of the union: */
133     struct {
134         uint32_t state[8];
135         uint64_t bitcount;
136         uint8_t  buffer[64];
137     } s256;
138 
139     /* SHA-384 and SHA-512 use this part of the union: */
140     struct {
141         uint64_t state[8];
142         uint64_t bitcount[2];
143         uint8_t  buffer[128];
144     } s512;
145 } SHA_CTX;
146 
147 #else /* SHA2_USE_INTTYPES_H */
148 
149 typedef union _SHA_CTX {
150     /* SHA-1 uses this part of the union: */
151     struct {
152         u_int32_t state[5];
153         u_int64_t bitcount;
154         u_int8_t  buffer[64];
155     } s1;
156 
157     /* SHA-224 and SHA-256 use this part of the union: */
158     struct {
159         u_int32_t state[8];
160         u_int64_t bitcount;
161         u_int8_t  buffer[64];
162     } s256;
163 
164     /* SHA-384 and SHA-512 use this part of the union: */
165     struct {
166         u_int64_t state[8];
167         u_int64_t bitcount[2];
168         u_int8_t  buffer[128];
169     } s512;
170 } SHA_CTX;
171 
172 #endif /* SHA2_USE_INTTYPES_H */
173 
174 
175 /*** SHA-256/384/512 Function Prototypes ******************************/
176 #ifndef NOPROTO
177 #ifdef SHA2_USE_INTTYPES_H
178 
179 void SHA1_Init(SHA_CTX*);
180 void SHA1_Update(SHA_CTX*, const uint8_t*, size_t);
181 void SHA1_Final(uint8_t[SHA1_DIGEST_LENGTH], SHA_CTX*);
182 char* SHA1_End(SHA_CTX*, char[SHA1_DIGEST_STRING_LENGTH]);
183 char* SHA1_Data(const uint8_t*, size_t, char[SHA1_DIGEST_STRING_LENGTH]);
184 
185 void SHA224_Init(SHA_CTX*);
186 void SHA224_Update(SHA_CTX*, const uint8_t*, size_t);
187 void SHA224_Final(uint8_t[SHA224_DIGEST_LENGTH], SHA_CTX*);
188 char* SHA224_End(SHA_CTX*, char[SHA224_DIGEST_STRING_LENGTH]);
189 char* SHA224_Data(const uint8_t*, size_t, char[SHA224_DIGEST_STRING_LENGTH]);
190 
191 void SHA256_Init(SHA_CTX*);
192 void SHA256_Update(SHA_CTX*, const uint8_t*, size_t);
193 void SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA_CTX*);
194 char* SHA256_End(SHA_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
195 char* SHA256_Data(const uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
196 
197 void SHA384_Init(SHA_CTX*);
198 void SHA384_Update(SHA_CTX*, const uint8_t*, size_t);
199 void SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA_CTX*);
200 char* SHA384_End(SHA_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
201 char* SHA384_Data(const uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
202 
203 void SHA512_Init(SHA_CTX*);
204 void SHA512_Update(SHA_CTX*, const uint8_t*, size_t);
205 void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA_CTX*);
206 char* SHA512_End(SHA_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
207 char* SHA512_Data(const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
208 
209 #else /* SHA2_USE_INTTYPES_H */
210 
211 void SHA1_Init(SHA_CTX*);
212 void SHA1_Update(SHA_CTX*, const u_int8_t*, size_t);
213 void SHA1_Final(u_int8_t[SHA1_DIGEST_LENGTH], SHA_CTX*);
214 char* SHA1_End(SHA_CTX*, char[SHA1_DIGEST_STRING_LENGTH]);
215 char* SHA1_Data(const u_int8_t*, size_t, char[SHA1_DIGEST_STRING_LENGTH]);
216 
217 void SHA224_Init(SHA_CTX*);
218 void SHA224_Update(SHA_CTX*, const u_int8_t*, size_t);
219 void SHA224_Final(u_int8_t[SHA224_DIGEST_LENGTH], SHA_CTX*);
220 char* SHA224_End(SHA_CTX*, char[SHA224_DIGEST_STRING_LENGTH]);
221 char* SHA224_Data(const u_int8_t*, size_t, char[SHA224_DIGEST_STRING_LENGTH]);
222 
223 void SHA256_Init(SHA_CTX*);
224 void SHA256_Update(SHA_CTX*, const u_int8_t*, size_t);
225 void SHA256_Final(u_int8_t[SHA256_DIGEST_LENGTH], SHA_CTX*);
226 char* SHA256_End(SHA_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
227 char* SHA256_Data(const u_int8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
228 
229 void SHA384_Init(SHA_CTX*);
230 void SHA384_Update(SHA_CTX*, const u_int8_t*, size_t);
231 void SHA384_Final(u_int8_t[SHA384_DIGEST_LENGTH], SHA_CTX*);
232 char* SHA384_End(SHA_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
233 char* SHA384_Data(const u_int8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
234 
235 void SHA512_Init(SHA_CTX*);
236 void SHA512_Update(SHA_CTX*, const u_int8_t*, size_t);
237 void SHA512_Final(u_int8_t[SHA512_DIGEST_LENGTH], SHA_CTX*);
238 char* SHA512_End(SHA_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
239 char* SHA512_Data(const u_int8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
240 
241 #endif /* SHA2_USE_INTTYPES_H */
242 
243 #else /* NOPROTO */
244 
245 void SHA1_Init();
246 void SHA1_Update();
247 void SHA1_Final();
248 char* SHA1_End();
249 char* SHA1_Data();
250 
251 void SHA224_Init();
252 void SHA224_Update();
253 void SHA224_Final();
254 char* SHA224_End();
255 char* SHA224_Data();
256 
257 void SHA256_Init();
258 void SHA256_Update();
259 void SHA256_Final();
260 char* SHA256_End();
261 char* SHA256_Data();
262 
263 void SHA384_Init();
264 void SHA384_Update();
265 void SHA384_Final();
266 char* SHA384_End();
267 char* SHA384_Data();
268 
269 void SHA512_Init();
270 void SHA512_Update();
271 void SHA512_Final();
272 char* SHA512_End();
273 char* SHA512_Data();
274 
275 #endif /* NOPROTO */
276 
277 #ifdef    __cplusplus
278 }
279 #endif /* __cplusplus */
280 
281 #endif /* __SHA2_H__ */
282 
283