sha2.h revision 1.1.1.1
1/*	$NetBSD: sha2.h,v 1.1.1.1 2010/03/08 02:14:20 lukem Exp $	*/
2
3/* OpenLDAP: pkg/ldap/contrib/slapd-modules/passwd/sha2/sha2.h,v 1.1.2.1 2009/01/26 21:07:06 quanah Exp */
4/*
5 * FILE:	sha2.h
6 * AUTHOR:	Aaron D. Gifford - http://www.aarongifford.com/
7 *
8 * Copyright (c) 2000-2001, Aaron D. Gifford
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the copyright holder nor the names of contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * Id: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg
36 */
37
38#ifndef __SHA2_H__
39#define __SHA2_H__
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45
46/*
47 * Import u_intXX_t size_t type definitions from system headers.  You
48 * may need to change this, or define these things yourself in this
49 * file.
50 */
51#include <sys/types.h>
52
53#ifdef SHA2_USE_INTTYPES_H
54
55#include <inttypes.h>
56
57#endif /* SHA2_USE_INTTYPES_H */
58
59
60/*** SHA-256/384/512 Various Length Definitions ***********************/
61#define SHA256_BLOCK_LENGTH		64
62#define SHA256_DIGEST_LENGTH		32
63#define SHA256_DIGEST_STRING_LENGTH	(SHA256_DIGEST_LENGTH * 2 + 1)
64#define SHA384_BLOCK_LENGTH		128
65#define SHA384_DIGEST_LENGTH		48
66#define SHA384_DIGEST_STRING_LENGTH	(SHA384_DIGEST_LENGTH * 2 + 1)
67#define SHA512_BLOCK_LENGTH		128
68#define SHA512_DIGEST_LENGTH		64
69#define SHA512_DIGEST_STRING_LENGTH	(SHA512_DIGEST_LENGTH * 2 + 1)
70
71
72/*** SHA-256/384/512 Context Structures *******************************/
73/* NOTE: If your architecture does not define either u_intXX_t types or
74 * uintXX_t (from inttypes.h), you may need to define things by hand
75 * for your system:
76 */
77#if 0
78typedef unsigned char u_int8_t;		/* 1-byte  (8-bits)  */
79typedef unsigned int u_int32_t;		/* 4-bytes (32-bits) */
80typedef unsigned long long u_int64_t;	/* 8-bytes (64-bits) */
81#endif
82/*
83 * Most BSD systems already define u_intXX_t types, as does Linux.
84 * Some systems, however, like Compaq's Tru64 Unix instead can use
85 * uintXX_t types defined by very recent ANSI C standards and included
86 * in the file:
87 *
88 *   #include <inttypes.h>
89 *
90 * If you choose to use <inttypes.h> then please define:
91 *
92 *   #define SHA2_USE_INTTYPES_H
93 *
94 * Or on the command line during compile:
95 *
96 *   cc -DSHA2_USE_INTTYPES_H ...
97 */
98#ifdef SHA2_USE_INTTYPES_H
99
100typedef struct _SHA256_CTX {
101	uint32_t	state[8];
102	uint64_t	bitcount;
103	uint8_t	buffer[SHA256_BLOCK_LENGTH];
104} SHA256_CTX;
105typedef struct _SHA512_CTX {
106	uint64_t	state[8];
107	uint64_t	bitcount[2];
108	uint8_t	buffer[SHA512_BLOCK_LENGTH];
109} SHA512_CTX;
110
111#else /* SHA2_USE_INTTYPES_H */
112
113typedef struct _SHA256_CTX {
114	u_int32_t	state[8];
115	u_int64_t	bitcount;
116	u_int8_t	buffer[SHA256_BLOCK_LENGTH];
117} SHA256_CTX;
118typedef struct _SHA512_CTX {
119	u_int64_t	state[8];
120	u_int64_t	bitcount[2];
121	u_int8_t	buffer[SHA512_BLOCK_LENGTH];
122} SHA512_CTX;
123
124#endif /* SHA2_USE_INTTYPES_H */
125
126typedef SHA512_CTX SHA384_CTX;
127
128
129/*** SHA-256/384/512 Function Prototypes ******************************/
130#ifndef NOPROTO
131#ifdef SHA2_USE_INTTYPES_H
132
133void SHA256_Init(SHA256_CTX *);
134void SHA256_Update(SHA256_CTX*, const uint8_t*, size_t);
135void SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*);
136char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
137char* SHA256_Data(const uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
138
139void SHA384_Init(SHA384_CTX*);
140void SHA384_Update(SHA384_CTX*, const uint8_t*, size_t);
141void SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*);
142char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
143char* SHA384_Data(const uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
144
145void SHA512_Init(SHA512_CTX*);
146void SHA512_Update(SHA512_CTX*, const uint8_t*, size_t);
147void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
148char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
149char* SHA512_Data(const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
150
151#else /* SHA2_USE_INTTYPES_H */
152
153void SHA256_Init(SHA256_CTX *);
154void SHA256_Update(SHA256_CTX*, const u_int8_t*, size_t);
155void SHA256_Final(u_int8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*);
156char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
157char* SHA256_Data(const u_int8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
158
159void SHA384_Init(SHA384_CTX*);
160void SHA384_Update(SHA384_CTX*, const u_int8_t*, size_t);
161void SHA384_Final(u_int8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*);
162char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
163char* SHA384_Data(const u_int8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
164
165void SHA512_Init(SHA512_CTX*);
166void SHA512_Update(SHA512_CTX*, const u_int8_t*, size_t);
167void SHA512_Final(u_int8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
168char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
169char* SHA512_Data(const u_int8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
170
171#endif /* SHA2_USE_INTTYPES_H */
172
173#else /* NOPROTO */
174
175void SHA256_Init();
176void SHA256_Update();
177void SHA256_Final();
178char* SHA256_End();
179char* SHA256_Data();
180
181void SHA384_Init();
182void SHA384_Update();
183void SHA384_Final();
184char* SHA384_End();
185char* SHA384_Data();
186
187void SHA512_Init();
188void SHA512_Update();
189void SHA512_Final();
190char* SHA512_End();
191char* SHA512_Data();
192
193#endif /* NOPROTO */
194
195#ifdef	__cplusplus
196}
197#endif /* __cplusplus */
198
199#endif /* __SHA2_H__ */
200
201