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