xref: /freebsd/crypto/openssh/openbsd-compat/sha2.h (revision 19261079b74319502c6ffa1249920079f0f69a72)
1*19261079SEd Maste /*	$OpenBSD: sha2.h,v 1.10 2016/09/03 17:00:29 tedu Exp $	*/
2761efaa7SDag-Erling Smørgrav 
3761efaa7SDag-Erling Smørgrav /*
4761efaa7SDag-Erling Smørgrav  * FILE:	sha2.h
5761efaa7SDag-Erling Smørgrav  * AUTHOR:	Aaron D. Gifford <me@aarongifford.com>
6761efaa7SDag-Erling Smørgrav  *
7761efaa7SDag-Erling Smørgrav  * Copyright (c) 2000-2001, Aaron D. Gifford
8761efaa7SDag-Erling Smørgrav  * All rights reserved.
9761efaa7SDag-Erling Smørgrav  *
10761efaa7SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
11761efaa7SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
12761efaa7SDag-Erling Smørgrav  * are met:
13761efaa7SDag-Erling Smørgrav  * 1. Redistributions of source code must retain the above copyright
14761efaa7SDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer.
15761efaa7SDag-Erling Smørgrav  * 2. Redistributions in binary form must reproduce the above copyright
16761efaa7SDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer in the
17761efaa7SDag-Erling Smørgrav  *    documentation and/or other materials provided with the distribution.
18761efaa7SDag-Erling Smørgrav  * 3. Neither the name of the copyright holder nor the names of contributors
19761efaa7SDag-Erling Smørgrav  *    may be used to endorse or promote products derived from this software
20761efaa7SDag-Erling Smørgrav  *    without specific prior written permission.
21761efaa7SDag-Erling Smørgrav  *
22761efaa7SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
23761efaa7SDag-Erling Smørgrav  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24761efaa7SDag-Erling Smørgrav  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25761efaa7SDag-Erling Smørgrav  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
26761efaa7SDag-Erling Smørgrav  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27761efaa7SDag-Erling Smørgrav  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28761efaa7SDag-Erling Smørgrav  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29761efaa7SDag-Erling Smørgrav  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30761efaa7SDag-Erling Smørgrav  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31761efaa7SDag-Erling Smørgrav  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32761efaa7SDag-Erling Smørgrav  * SUCH DAMAGE.
33761efaa7SDag-Erling Smørgrav  *
34761efaa7SDag-Erling Smørgrav  * $From: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $
35761efaa7SDag-Erling Smørgrav  */
36761efaa7SDag-Erling Smørgrav 
37761efaa7SDag-Erling Smørgrav /* OPENBSD ORIGINAL: include/sha2.h */
38761efaa7SDag-Erling Smørgrav 
39761efaa7SDag-Erling Smørgrav #ifndef _SSHSHA2_H
40761efaa7SDag-Erling Smørgrav #define _SSHSHA2_H
41761efaa7SDag-Erling Smørgrav 
42761efaa7SDag-Erling Smørgrav #include "includes.h"
43761efaa7SDag-Erling Smørgrav 
44*19261079SEd Maste #if !defined(HAVE_SHA256UPDATE) || !defined(HAVE_SHA384UPDATE) || \
45*19261079SEd Maste     !defined(HAVE_SHA512UPDATE)
46761efaa7SDag-Erling Smørgrav 
47761efaa7SDag-Erling Smørgrav /*** SHA-256/384/512 Various Length Definitions ***********************/
48*19261079SEd Maste #define SHA224_BLOCK_LENGTH		64
49*19261079SEd Maste #define SHA224_DIGEST_LENGTH		28
50*19261079SEd Maste #define SHA224_DIGEST_STRING_LENGTH	(SHA224_DIGEST_LENGTH * 2 + 1)
51761efaa7SDag-Erling Smørgrav #define SHA256_BLOCK_LENGTH		64
52761efaa7SDag-Erling Smørgrav #define SHA256_DIGEST_LENGTH		32
53761efaa7SDag-Erling Smørgrav #define SHA256_DIGEST_STRING_LENGTH	(SHA256_DIGEST_LENGTH * 2 + 1)
54761efaa7SDag-Erling Smørgrav #define SHA384_BLOCK_LENGTH		128
55761efaa7SDag-Erling Smørgrav #define SHA384_DIGEST_LENGTH		48
56761efaa7SDag-Erling Smørgrav #define SHA384_DIGEST_STRING_LENGTH	(SHA384_DIGEST_LENGTH * 2 + 1)
57761efaa7SDag-Erling Smørgrav #define SHA512_BLOCK_LENGTH		128
58761efaa7SDag-Erling Smørgrav #define SHA512_DIGEST_LENGTH		64
59761efaa7SDag-Erling Smørgrav #define SHA512_DIGEST_STRING_LENGTH	(SHA512_DIGEST_LENGTH * 2 + 1)
60*19261079SEd Maste #define SHA512_256_BLOCK_LENGTH		128
61*19261079SEd Maste #define SHA512_256_DIGEST_LENGTH	32
62*19261079SEd Maste #define SHA512_256_DIGEST_STRING_LENGTH	(SHA512_256_DIGEST_LENGTH * 2 + 1)
63761efaa7SDag-Erling Smørgrav 
64761efaa7SDag-Erling Smørgrav 
65*19261079SEd Maste /*** SHA-224/256/384/512 Context Structure *******************************/
66*19261079SEd Maste typedef struct _SHA2_CTX {
67*19261079SEd Maste 	union {
68*19261079SEd Maste 		u_int32_t	st32[8];
69*19261079SEd Maste 		u_int64_t	st64[8];
70*19261079SEd Maste 	} state;
71761efaa7SDag-Erling Smørgrav 	u_int64_t	bitcount[2];
72761efaa7SDag-Erling Smørgrav 	u_int8_t	buffer[SHA512_BLOCK_LENGTH];
73*19261079SEd Maste } SHA2_CTX;
74761efaa7SDag-Erling Smørgrav 
75*19261079SEd Maste #if 0
76*19261079SEd Maste __BEGIN_DECLS
77*19261079SEd Maste void SHA224Init(SHA2_CTX *);
78*19261079SEd Maste void SHA224Transform(u_int32_t state[8], const u_int8_t [SHA224_BLOCK_LENGTH]);
79*19261079SEd Maste void SHA224Update(SHA2_CTX *, const u_int8_t *, size_t)
80761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__string__,2,3)));
81*19261079SEd Maste void SHA224Pad(SHA2_CTX *);
82*19261079SEd Maste void SHA224Final(u_int8_t [SHA224_DIGEST_LENGTH], SHA2_CTX *)
83*19261079SEd Maste 	__attribute__((__bounded__(__minbytes__,1,SHA224_DIGEST_LENGTH)));
84*19261079SEd Maste char *SHA224End(SHA2_CTX *, char *)
85*19261079SEd Maste 	__attribute__((__bounded__(__minbytes__,2,SHA224_DIGEST_STRING_LENGTH)));
86*19261079SEd Maste char *SHA224File(const char *, char *)
87*19261079SEd Maste 	__attribute__((__bounded__(__minbytes__,2,SHA224_DIGEST_STRING_LENGTH)));
88*19261079SEd Maste char *SHA224FileChunk(const char *, char *, off_t, off_t)
89*19261079SEd Maste 	__attribute__((__bounded__(__minbytes__,2,SHA224_DIGEST_STRING_LENGTH)));
90*19261079SEd Maste char *SHA224Data(const u_int8_t *, size_t, char *)
91*19261079SEd Maste 	__attribute__((__bounded__(__string__,1,2)))
92*19261079SEd Maste 	__attribute__((__bounded__(__minbytes__,3,SHA224_DIGEST_STRING_LENGTH)));
93*19261079SEd Maste #endif /* 0 */
94*19261079SEd Maste 
95*19261079SEd Maste #ifndef HAVE_SHA256UPDATE
96*19261079SEd Maste void SHA256Init(SHA2_CTX *);
97*19261079SEd Maste void SHA256Transform(u_int32_t state[8], const u_int8_t [SHA256_BLOCK_LENGTH]);
98*19261079SEd Maste void SHA256Update(SHA2_CTX *, const u_int8_t *, size_t)
99*19261079SEd Maste 	__attribute__((__bounded__(__string__,2,3)));
100*19261079SEd Maste void SHA256Pad(SHA2_CTX *);
101*19261079SEd Maste void SHA256Final(u_int8_t [SHA256_DIGEST_LENGTH], SHA2_CTX *)
102761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__minbytes__,1,SHA256_DIGEST_LENGTH)));
103*19261079SEd Maste char *SHA256End(SHA2_CTX *, char *)
104761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__minbytes__,2,SHA256_DIGEST_STRING_LENGTH)));
105*19261079SEd Maste char *SHA256File(const char *, char *)
106761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__minbytes__,2,SHA256_DIGEST_STRING_LENGTH)));
107*19261079SEd Maste char *SHA256FileChunk(const char *, char *, off_t, off_t)
108761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__minbytes__,2,SHA256_DIGEST_STRING_LENGTH)));
109*19261079SEd Maste char *SHA256Data(const u_int8_t *, size_t, char *)
110761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__string__,1,2)))
111761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__minbytes__,3,SHA256_DIGEST_STRING_LENGTH)));
112*19261079SEd Maste #endif /* HAVE_SHA256UPDATE */
113761efaa7SDag-Erling Smørgrav 
114*19261079SEd Maste #ifndef HAVE_SHA384UPDATE
115*19261079SEd Maste void SHA384Init(SHA2_CTX *);
116*19261079SEd Maste void SHA384Transform(u_int64_t state[8], const u_int8_t [SHA384_BLOCK_LENGTH]);
117*19261079SEd Maste void SHA384Update(SHA2_CTX *, const u_int8_t *, size_t)
118761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__string__,2,3)));
119*19261079SEd Maste void SHA384Pad(SHA2_CTX *);
120*19261079SEd Maste void SHA384Final(u_int8_t [SHA384_DIGEST_LENGTH], SHA2_CTX *)
121761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__minbytes__,1,SHA384_DIGEST_LENGTH)));
122*19261079SEd Maste char *SHA384End(SHA2_CTX *, char *)
123761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__minbytes__,2,SHA384_DIGEST_STRING_LENGTH)));
124*19261079SEd Maste char *SHA384File(const char *, char *)
125761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__minbytes__,2,SHA384_DIGEST_STRING_LENGTH)));
126*19261079SEd Maste char *SHA384FileChunk(const char *, char *, off_t, off_t)
127761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__minbytes__,2,SHA384_DIGEST_STRING_LENGTH)));
128*19261079SEd Maste char *SHA384Data(const u_int8_t *, size_t, char *)
129761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__string__,1,2)))
130761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__minbytes__,3,SHA384_DIGEST_STRING_LENGTH)));
131*19261079SEd Maste #endif /* HAVE_SHA384UPDATE */
132761efaa7SDag-Erling Smørgrav 
133*19261079SEd Maste #ifndef HAVE_SHA512UPDATE
134*19261079SEd Maste void SHA512Init(SHA2_CTX *);
135*19261079SEd Maste void SHA512Transform(u_int64_t state[8], const u_int8_t [SHA512_BLOCK_LENGTH]);
136*19261079SEd Maste void SHA512Update(SHA2_CTX *, const u_int8_t *, size_t)
137761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__string__,2,3)));
138*19261079SEd Maste void SHA512Pad(SHA2_CTX *);
139*19261079SEd Maste void SHA512Final(u_int8_t [SHA512_DIGEST_LENGTH], SHA2_CTX *)
140761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__minbytes__,1,SHA512_DIGEST_LENGTH)));
141*19261079SEd Maste char *SHA512End(SHA2_CTX *, char *)
142761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__minbytes__,2,SHA512_DIGEST_STRING_LENGTH)));
143*19261079SEd Maste char *SHA512File(const char *, char *)
144761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__minbytes__,2,SHA512_DIGEST_STRING_LENGTH)));
145*19261079SEd Maste char *SHA512FileChunk(const char *, char *, off_t, off_t)
146761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__minbytes__,2,SHA512_DIGEST_STRING_LENGTH)));
147*19261079SEd Maste char *SHA512Data(const u_int8_t *, size_t, char *)
148761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__string__,1,2)))
149761efaa7SDag-Erling Smørgrav 	__attribute__((__bounded__(__minbytes__,3,SHA512_DIGEST_STRING_LENGTH)));
150*19261079SEd Maste #endif /* HAVE_SHA512UPDATE */
151761efaa7SDag-Erling Smørgrav 
152*19261079SEd Maste #if 0
153*19261079SEd Maste void SHA512_256Init(SHA2_CTX *);
154*19261079SEd Maste void SHA512_256Transform(u_int64_t state[8], const u_int8_t [SHA512_256_BLOCK_LENGTH]);
155*19261079SEd Maste void SHA512_256Update(SHA2_CTX *, const u_int8_t *, size_t)
156*19261079SEd Maste 	__attribute__((__bounded__(__string__,2,3)));
157*19261079SEd Maste void SHA512_256Pad(SHA2_CTX *);
158*19261079SEd Maste void SHA512_256Final(u_int8_t [SHA512_256_DIGEST_LENGTH], SHA2_CTX *)
159*19261079SEd Maste 	__attribute__((__bounded__(__minbytes__,1,SHA512_256_DIGEST_LENGTH)));
160*19261079SEd Maste char *SHA512_256End(SHA2_CTX *, char *)
161*19261079SEd Maste 	__attribute__((__bounded__(__minbytes__,2,SHA512_256_DIGEST_STRING_LENGTH)));
162*19261079SEd Maste char *SHA512_256File(const char *, char *)
163*19261079SEd Maste 	__attribute__((__bounded__(__minbytes__,2,SHA512_256_DIGEST_STRING_LENGTH)));
164*19261079SEd Maste char *SHA512_256FileChunk(const char *, char *, off_t, off_t)
165*19261079SEd Maste 	__attribute__((__bounded__(__minbytes__,2,SHA512_256_DIGEST_STRING_LENGTH)));
166*19261079SEd Maste char *SHA512_256Data(const u_int8_t *, size_t, char *)
167*19261079SEd Maste 	__attribute__((__bounded__(__string__,1,2)))
168*19261079SEd Maste 	__attribute__((__bounded__(__minbytes__,3,SHA512_256_DIGEST_STRING_LENGTH)));
169*19261079SEd Maste __END_DECLS
170*19261079SEd Maste #endif /* 0 */
171*19261079SEd Maste 
172*19261079SEd Maste #endif /* HAVE_SHA{256,384,512}UPDATE */
173761efaa7SDag-Erling Smørgrav 
174761efaa7SDag-Erling Smørgrav #endif /* _SSHSHA2_H */
175