1/*
2 * FILE:	sha2.h
3 * AUTHOR:	Aaron D. Gifford - http://www.aarongifford.com/
4 *
5 * Copyright (c) 2000-2001, Aaron D. Gifford
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the copyright holder nor the names of contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $OrigId: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $
33 * $RoughId: sha2.h,v 1.3 2002/02/24 08:14:32 knu Exp $
34 * $Id: sha2.h 27437 2010-04-22 08:04:13Z nobu $
35 */
36
37#ifndef __SHA2_H__
38#define __SHA2_H__
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44
45/*
46 * Import u_intXX_t size_t type definitions from system headers.  You
47 * may need to change this, or define these things yourself in this
48 * file.
49 */
50#include <sys/types.h>
51
52#ifdef RUBY
53# ifdef HAVE_PROTOTYPES
54#  undef NOPROTO
55# else
56#  define NOPROTO
57# endif /* HAVE_PROTOTYPES */
58# ifndef BYTE_ORDER
59#  define LITTLE_ENDIAN	1234
60#  define BIG_ENDIAN	4321
61#  ifdef WORDS_BIGENDIAN
62#   define BYTE_ORDER	BIG_ENDIAN
63#  else
64#   define BYTE_ORDER	LITTLE_ENDIAN
65#  endif
66# endif /* BYTE_ORDER */
67# define SHA2_USE_INTTYPES_H
68#else /* RUBY */
69#ifdef SHA2_USE_INTTYPES_H
70
71#include <inttypes.h>
72
73#endif /* SHA2_USE_INTTYPES_H */
74#endif /* RUBY */
75
76
77/*** SHA-256/384/512 Various Length Definitions ***********************/
78#define SHA256_BLOCK_LENGTH		64
79#define SHA256_DIGEST_LENGTH		32
80#define SHA256_DIGEST_STRING_LENGTH	(SHA256_DIGEST_LENGTH * 2 + 1)
81#define SHA384_BLOCK_LENGTH		128
82#define SHA384_DIGEST_LENGTH		48
83#define SHA384_DIGEST_STRING_LENGTH	(SHA384_DIGEST_LENGTH * 2 + 1)
84#define SHA512_BLOCK_LENGTH		128
85#define SHA512_DIGEST_LENGTH		64
86#define SHA512_DIGEST_STRING_LENGTH	(SHA512_DIGEST_LENGTH * 2 + 1)
87
88
89/*** SHA-256/384/512 Context Structures *******************************/
90/* NOTE: If your architecture does not define either u_intXX_t types or
91 * uintXX_t (from inttypes.h), you may need to define things by hand
92 * for your system:
93 */
94#ifndef SHA2_USE_INTTYPES_H
95# ifdef HAVE_U_INT8_T
96typedef u_int8_t uint8_t;		/* 1-byte  (8-bits)  */
97typedef u_int32_t uint32_t;		/* 4-bytes (32-bits) */
98typedef u_int64_t uint64_t;		/* 8-bytes (64-bits) */
99# else
100typedef unsigned char uint8_t;		/* 1-byte  (8-bits)  */
101typedef unsigned int uint32_t;		/* 4-bytes (32-bits) */
102typedef unsigned long long uint64_t;	/* 8-bytes (64-bits) */
103# endif
104#endif
105
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 */
122typedef struct _SHA256_CTX {
123	uint32_t	state[8];
124	uint64_t	bitcount;
125	uint8_t	buffer[SHA256_BLOCK_LENGTH];
126} SHA256_CTX;
127typedef struct _SHA512_CTX {
128	uint64_t	state[8];
129	uint64_t	bitcount[2];
130	uint8_t	buffer[SHA512_BLOCK_LENGTH];
131} SHA512_CTX;
132
133typedef SHA512_CTX SHA384_CTX;
134
135
136/*** SHA-256/384/512 Function Prototypes ******************************/
137#ifdef RUBY
138#define SHA256_Init		rb_Digest_SHA256_Init
139#define SHA256_Update		rb_Digest_SHA256_Update
140#define SHA256_Finish		rb_Digest_SHA256_Finish
141#define SHA256_Data		rb_Digest_SHA256_Data
142#define SHA256_End		rb_Digest_SHA256_End
143#define SHA256_Last		rb_Digest_SHA256_Last
144#define SHA256_Transform	rb_Digest_SHA256_Transform
145#define SHA256_Final(d, c)	SHA256_Finish(c, d)
146
147#define SHA384_Init		rb_Digest_SHA384_Init
148#define SHA384_Update		rb_Digest_SHA384_Update
149#define SHA384_Finish		rb_Digest_SHA384_Finish
150#define SHA384_Data		rb_Digest_SHA384_Data
151#define SHA384_End		rb_Digest_SHA384_End
152#define SHA384_Last		rb_Digest_SHA384_Last
153#define SHA384_Transform	rb_Digest_SHA384_Transform
154#define SHA384_Final(d, c)	SHA384_Finish(c, d)
155
156#define SHA512_Init		rb_Digest_SHA512_Init
157#define SHA512_Update		rb_Digest_SHA512_Update
158#define SHA512_Finish		rb_Digest_SHA512_Finish
159#define SHA512_Data		rb_Digest_SHA512_Data
160#define SHA512_End		rb_Digest_SHA512_End
161#define SHA512_Last		rb_Digest_SHA512_Last
162#define SHA512_Transform	rb_Digest_SHA512_Transform
163#define SHA512_Final(d, c)	SHA512_Finish(c, d)
164#endif /* RUBY */
165
166#ifndef NOPROTO
167
168void SHA256_Init(SHA256_CTX *);
169void SHA256_Update(SHA256_CTX*, const uint8_t*, size_t);
170void SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*);
171char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
172char* SHA256_Data(const uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
173
174void SHA384_Init(SHA384_CTX*);
175void SHA384_Update(SHA384_CTX*, const uint8_t*, size_t);
176void SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*);
177char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
178char* SHA384_Data(const uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
179
180void SHA512_Init(SHA512_CTX*);
181void SHA512_Update(SHA512_CTX*, const uint8_t*, size_t);
182void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
183char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
184char* SHA512_Data(const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
185
186#else /* NOPROTO */
187
188void SHA256_Init();
189void SHA256_Update();
190#ifdef RUBY
191void SHA256_Finish();
192#else
193void SHA256_Final();
194#endif /* RUBY */
195char* SHA256_End();
196char* SHA256_Data();
197
198void SHA384_Init();
199void SHA384_Update();
200#ifdef RUBY
201void SHA384_Finish();
202#else
203void SHA384_Final();
204#endif /* RUBY */
205char* SHA384_End();
206char* SHA384_Data();
207
208void SHA512_Init();
209void SHA512_Update();
210#ifdef RUBY
211void SHA512_Finish();
212#else
213void SHA512_Final();
214#endif /* RUBY */
215char* SHA512_End();
216char* SHA512_Data();
217
218#endif /* NOPROTO */
219
220#ifdef	__cplusplus
221}
222#endif /* __cplusplus */
223
224#endif /* __SHA2_H__ */
225
226