xref: /dragonfly/sys/crypto/sha2/sha2.h (revision 1d1731fa)
1 /*	$FreeBSD: src/sys/crypto/sha2/sha2.h,v 1.1.2.1 2001/07/03 11:01:36 ume Exp $	*/
2 /*	$DragonFly: src/sys/crypto/sha2/sha2.h,v 1.3 2003/08/27 10:59:04 rob Exp $	*/
3 /*	$KAME: sha2.h,v 1.3 2001/03/12 08:27:48 itojun Exp $	*/
4 
5 /*
6  * sha2.h
7  *
8  * Version 1.0.0beta1
9  *
10  * Written by Aaron D. Gifford <me@aarongifford.com>
11  *
12  * Copyright 2000 Aaron D. Gifford.  All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the copyright holder nor the names of contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  */
39 
40 #ifndef __SHA2_H__
41 #define __SHA2_H__
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 
48 /*** SHA-256/384/512 Various Length Definitions ***********************/
49 #define SHA256_BLOCK_LENGTH		64
50 #define SHA256_DIGEST_LENGTH		32
51 #define SHA256_DIGEST_STRING_LENGTH	(SHA256_DIGEST_LENGTH * 2 + 1)
52 #define SHA384_BLOCK_LENGTH		128
53 #define SHA384_DIGEST_LENGTH		48
54 #define SHA384_DIGEST_STRING_LENGTH	(SHA384_DIGEST_LENGTH * 2 + 1)
55 #define SHA512_BLOCK_LENGTH		128
56 #define SHA512_DIGEST_LENGTH		64
57 #define SHA512_DIGEST_STRING_LENGTH	(SHA512_DIGEST_LENGTH * 2 + 1)
58 
59 
60 /*** SHA-256/384/512 Context Structures *******************************/
61 /* NOTE: If your architecture does not define either u_intXX_t types or
62  * uintXX_t (from inttypes.h), you may need to define things by hand
63  * for your system:
64  */
65 #if 0
66 typedef unsigned char u_int8_t;		/* 1-byte  (8-bits)  */
67 typedef unsigned int u_int32_t;		/* 4-bytes (32-bits) */
68 typedef unsigned long long u_int64_t;	/* 8-bytes (64-bits) */
69 #endif
70 /*
71  * Most BSD systems already define u_intXX_t types, as does Linux.
72  * Some systems, however, like Compaq's Tru64 Unix instead can use
73  * uintXX_t types defined by very recent ANSI C standards and included
74  * in the file:
75  *
76  *   #include <inttypes.h>
77  *
78  * If you choose to use <inttypes.h> then please define:
79  *
80  *   #define SHA2_USE_INTTYPES_H
81  *
82  * Or on the command line during compile:
83  *
84  *   cc -DSHA2_USE_INTTYPES_H ...
85  */
86 #if 0 /*def SHA2_USE_INTTYPES_H*/
87 
88 typedef struct _SHA256_CTX {
89 	uint32_t	state[8];
90 	uint64_t	bitcount;
91 	uint8_t	buffer[SHA256_BLOCK_LENGTH];
92 } SHA256_CTX;
93 typedef struct _SHA512_CTX {
94 	uint64_t	state[8];
95 	uint64_t	bitcount[2];
96 	uint8_t	buffer[SHA512_BLOCK_LENGTH];
97 } SHA512_CTX;
98 
99 #else /* SHA2_USE_INTTYPES_H */
100 
101 typedef struct _SHA256_CTX {
102 	u_int32_t	state[8];
103 	u_int64_t	bitcount;
104 	u_int8_t	buffer[SHA256_BLOCK_LENGTH];
105 } SHA256_CTX;
106 typedef struct _SHA512_CTX {
107 	u_int64_t	state[8];
108 	u_int64_t	bitcount[2];
109 	u_int8_t	buffer[SHA512_BLOCK_LENGTH];
110 } SHA512_CTX;
111 
112 #endif /* SHA2_USE_INTTYPES_H */
113 
114 typedef SHA512_CTX SHA384_CTX;
115 
116 
117 /*** SHA-256/384/512 Function Prototypes ******************************/
118 
119 void SHA256_Init (SHA256_CTX *);
120 void SHA256_Update (SHA256_CTX*, const u_int8_t*, size_t);
121 void SHA256_Final (u_int8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*);
122 char* SHA256_End (SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
123 char* SHA256_Data (const u_int8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
124 
125 void SHA384_Init (SHA384_CTX*);
126 void SHA384_Update (SHA384_CTX*, const u_int8_t*, size_t);
127 void SHA384_Final (u_int8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*);
128 char* SHA384_End (SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
129 char* SHA384_Data (const u_int8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
130 
131 void SHA512_Init (SHA512_CTX*);
132 void SHA512_Update (SHA512_CTX*, const u_int8_t*, size_t);
133 void SHA512_Final (u_int8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
134 char* SHA512_End (SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
135 char* SHA512_Data (const u_int8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
136 
137 #ifdef	__cplusplus
138 }
139 #endif /* __cplusplus */
140 
141 #endif /* __SHA2_H__ */
142 
143