1101099Srwatson/*	$OpenBSD: sha2.h,v 1.10 2016/09/03 17:00:29 tedu Exp $	*/
2184467Srwatson
3145412Strhodes/*
4171253Srwatson * FILE:	sha2.h
5172930Srwatson * AUTHOR:	Aaron D. Gifford <me@aarongifford.com>
6101099Srwatson *
7101099Srwatson * Copyright (c) 2000-2001, Aaron D. Gifford
8101099Srwatson * All rights reserved.
9145412Strhodes *
10101099Srwatson * Redistribution and use in source and binary forms, with or without
11106393Srwatson * modification, are permitted provided that the following conditions
12106393Srwatson * are met:
13106393Srwatson * 1. Redistributions of source code must retain the above copyright
14106393Srwatson *    notice, this list of conditions and the following disclaimer.
15101099Srwatson * 2. Redistributions in binary form must reproduce the above copyright
16172930Srwatson *    notice, this list of conditions and the following disclaimer in the
17172930Srwatson *    documentation and/or other materials provided with the distribution.
18172930Srwatson * 3. Neither the name of the copyright holder nor the names of contributors
19101099Srwatson *    may be used to endorse or promote products derived from this software
20101099Srwatson *    without specific prior written permission.
21101099Srwatson *
22101099Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
23101099Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24101099Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25101099Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
26101099Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27101099Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28101099Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29101099Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30101099Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31101099Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32101099Srwatson * SUCH DAMAGE.
33101099Srwatson *
34101099Srwatson * $From: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $
35101099Srwatson */
36101099Srwatson
37101099Srwatson#ifndef _SHA2_H
38101099Srwatson#define _SHA2_H
39101099Srwatson
40101099Srwatson
41101099Srwatson/*** SHA-256/384/512 Various Length Definitions ***********************/
42136774Srwatson#define SHA224_BLOCK_LENGTH		64
43101099Srwatson#define SHA224_DIGEST_LENGTH		28
44101099Srwatson#define SHA224_DIGEST_STRING_LENGTH	(SHA224_DIGEST_LENGTH * 2 + 1)
45171253Srwatson#define SHA256_BLOCK_LENGTH		64
46171253Srwatson#define SHA256_DIGEST_LENGTH		32
47171253Srwatson#define SHA256_DIGEST_STRING_LENGTH	(SHA256_DIGEST_LENGTH * 2 + 1)
48101099Srwatson#define SHA384_BLOCK_LENGTH		128
49101099Srwatson#define SHA384_DIGEST_LENGTH		48
50101099Srwatson#define SHA384_DIGEST_STRING_LENGTH	(SHA384_DIGEST_LENGTH * 2 + 1)
51101099Srwatson#define SHA512_BLOCK_LENGTH		128
52101099Srwatson#define SHA512_DIGEST_LENGTH		64
53157986Sdwmalone#define SHA512_DIGEST_STRING_LENGTH	(SHA512_DIGEST_LENGTH * 2 + 1)
54145412Strhodes#define SHA512_256_BLOCK_LENGTH		128
55101099Srwatson#define SHA512_256_DIGEST_LENGTH	32
56166905Srwatson#define SHA512_256_DIGEST_STRING_LENGTH	(SHA512_256_DIGEST_LENGTH * 2 + 1)
57101099Srwatson
58145412Strhodes
59170689Srwatson/*** SHA-224/256/384/512 Context Structure *******************************/
60185435Sbztypedef struct _SHA2_CTX {
61101099Srwatson	union {
62101099Srwatson		u_int32_t	st32[8];
63101099Srwatson		u_int64_t	st64[8];
64134132Strhodes	} state;
65182905Strasz	u_int64_t	bitcount[2];
66101099Srwatson	u_int8_t	buffer[SHA512_BLOCK_LENGTH];
67165469Srwatson} SHA2_CTX;
68101099Srwatson
69184331Srwatson__BEGIN_DECLS
70101099Srwatsonvoid SHA224Init(SHA2_CTX *);
71172955Srwatsonvoid SHA224Transform(u_int32_t state[8], const u_int8_t [SHA224_BLOCK_LENGTH]);
72145412Strhodesvoid SHA224Update(SHA2_CTX *, const u_int8_t *, size_t)
73101099Srwatson	__attribute__((__bounded__(__string__,2,3)));
74101099Srwatsonvoid SHA224Pad(SHA2_CTX *);
75227309Sedvoid SHA224Final(u_int8_t [SHA224_DIGEST_LENGTH], SHA2_CTX *)
76101099Srwatson	__attribute__((__bounded__(__minbytes__,1,SHA224_DIGEST_LENGTH)));
77101099Srwatsonchar *SHA224End(SHA2_CTX *, char *)
78172955Srwatson	__attribute__((__bounded__(__minbytes__,2,SHA224_DIGEST_STRING_LENGTH)));
79267992Shselaskychar *SHA224File(const char *, char *)
80172955Srwatson	__attribute__((__bounded__(__minbytes__,2,SHA224_DIGEST_STRING_LENGTH)));
81101099Srwatsonchar *SHA224FileChunk(const char *, char *, off_t, off_t)
82227293Sed	__attribute__((__bounded__(__minbytes__,2,SHA224_DIGEST_STRING_LENGTH)));
83227293Sedchar *SHA224Data(const u_int8_t *, size_t, char *)
84101099Srwatson	__attribute__((__bounded__(__string__,1,2)))
85101099Srwatson	__attribute__((__bounded__(__minbytes__,3,SHA224_DIGEST_STRING_LENGTH)));
86101099Srwatson
87101099Srwatsonvoid SHA256Init(SHA2_CTX *);
88101099Srwatsonvoid SHA256Transform(u_int32_t state[8], const u_int8_t [SHA256_BLOCK_LENGTH]);
89157986Sdwmalonevoid SHA256Update(SHA2_CTX *, const u_int8_t *, size_t)
90101099Srwatson	__attribute__((__bounded__(__string__,2,3)));
91101099Srwatsonvoid SHA256Pad(SHA2_CTX *);
92101099Srwatsonvoid SHA256Final(u_int8_t [SHA256_DIGEST_LENGTH], SHA2_CTX *)
93101099Srwatson	__attribute__((__bounded__(__minbytes__,1,SHA256_DIGEST_LENGTH)));
94101099Srwatsonchar *SHA256End(SHA2_CTX *, char *)
95157986Sdwmalone	__attribute__((__bounded__(__minbytes__,2,SHA256_DIGEST_STRING_LENGTH)));
96157986Sdwmalonechar *SHA256File(const char *, char *)
97101099Srwatson	__attribute__((__bounded__(__minbytes__,2,SHA256_DIGEST_STRING_LENGTH)));
98134132Strhodeschar *SHA256FileChunk(const char *, char *, off_t, off_t)
99171253Srwatson	__attribute__((__bounded__(__minbytes__,2,SHA256_DIGEST_STRING_LENGTH)));
100171253Srwatsonchar *SHA256Data(const u_int8_t *, size_t, char *)
101134132Strhodes	__attribute__((__bounded__(__string__,1,2)))
102172955Srwatson	__attribute__((__bounded__(__minbytes__,3,SHA256_DIGEST_STRING_LENGTH)));
103134132Strhodes
104172955Srwatsonvoid SHA384Init(SHA2_CTX *);
105134132Strhodesvoid SHA384Transform(u_int64_t state[8], const u_int8_t [SHA384_BLOCK_LENGTH]);
106134132Strhodesvoid SHA384Update(SHA2_CTX *, const u_int8_t *, size_t)
107171253Srwatson	__attribute__((__bounded__(__string__,2,3)));
108171253Srwatsonvoid SHA384Pad(SHA2_CTX *);
109171253Srwatsonvoid SHA384Final(u_int8_t [SHA384_DIGEST_LENGTH], SHA2_CTX *)
110134131Strhodes	__attribute__((__bounded__(__minbytes__,1,SHA384_DIGEST_LENGTH)));
111172955Srwatsonchar *SHA384End(SHA2_CTX *, char *)
112134131Strhodes	__attribute__((__bounded__(__minbytes__,2,SHA384_DIGEST_STRING_LENGTH)));
113172955Srwatsonchar *SHA384File(const char *, char *)
114171253Srwatson	__attribute__((__bounded__(__minbytes__,2,SHA384_DIGEST_STRING_LENGTH)));
115134131Strhodeschar *SHA384FileChunk(const char *, char *, off_t, off_t)
116134131Strhodes	__attribute__((__bounded__(__minbytes__,2,SHA384_DIGEST_STRING_LENGTH)));
117172955Srwatsonchar *SHA384Data(const u_int8_t *, size_t, char *)
118101099Srwatson	__attribute__((__bounded__(__string__,1,2)))
119101099Srwatson	__attribute__((__bounded__(__minbytes__,3,SHA384_DIGEST_STRING_LENGTH)));
120157986Sdwmalone
121101099Srwatsonvoid SHA512Init(SHA2_CTX *);
122157986Sdwmalonevoid SHA512Transform(u_int64_t state[8], const u_int8_t [SHA512_BLOCK_LENGTH]);
123101099Srwatsonvoid SHA512Update(SHA2_CTX *, const u_int8_t *, size_t)
124157986Sdwmalone	__attribute__((__bounded__(__string__,2,3)));
125157986Sdwmalonevoid SHA512Pad(SHA2_CTX *);
126157986Sdwmalonevoid SHA512Final(u_int8_t [SHA512_DIGEST_LENGTH], SHA2_CTX *)
127157986Sdwmalone	__attribute__((__bounded__(__minbytes__,1,SHA512_DIGEST_LENGTH)));
128321055Semastechar *SHA512End(SHA2_CTX *, char *)
129157986Sdwmalone	__attribute__((__bounded__(__minbytes__,2,SHA512_DIGEST_STRING_LENGTH)));
130157986Sdwmalonechar *SHA512File(const char *, char *)
131136739Srwatson	__attribute__((__bounded__(__minbytes__,2,SHA512_DIGEST_STRING_LENGTH)));
132101099Srwatsonchar *SHA512FileChunk(const char *, char *, off_t, off_t)
133101099Srwatson	__attribute__((__bounded__(__minbytes__,2,SHA512_DIGEST_STRING_LENGTH)));
134101099Srwatsonchar *SHA512Data(const u_int8_t *, size_t, char *)
135101099Srwatson	__attribute__((__bounded__(__string__,1,2)))
136101099Srwatson	__attribute__((__bounded__(__minbytes__,3,SHA512_DIGEST_STRING_LENGTH)));
137101099Srwatson
138101099Srwatsonvoid SHA512_256Init(SHA2_CTX *);
139101099Srwatsonvoid SHA512_256Transform(u_int64_t state[8], const u_int8_t [SHA512_256_BLOCK_LENGTH]);
140101099Srwatsonvoid SHA512_256Update(SHA2_CTX *, const u_int8_t *, size_t)
141101099Srwatson	__attribute__((__bounded__(__string__,2,3)));
142101099Srwatsonvoid SHA512_256Pad(SHA2_CTX *);
143145412Strhodesvoid SHA512_256Final(u_int8_t [SHA512_256_DIGEST_LENGTH], SHA2_CTX *)
144101099Srwatson	__attribute__((__bounded__(__minbytes__,1,SHA512_256_DIGEST_LENGTH)));
145101099Srwatsonchar *SHA512_256End(SHA2_CTX *, char *)
146101099Srwatson	__attribute__((__bounded__(__minbytes__,2,SHA512_256_DIGEST_STRING_LENGTH)));
147101099Srwatsonchar *SHA512_256File(const char *, char *)
148101099Srwatson	__attribute__((__bounded__(__minbytes__,2,SHA512_256_DIGEST_STRING_LENGTH)));
149154386Scsjpchar *SHA512_256FileChunk(const char *, char *, off_t, off_t)
150101099Srwatson	__attribute__((__bounded__(__minbytes__,2,SHA512_256_DIGEST_STRING_LENGTH)));
151101099Srwatsonchar *SHA512_256Data(const u_int8_t *, size_t, char *)
152145412Strhodes	__attribute__((__bounded__(__string__,1,2)))
153145412Strhodes	__attribute__((__bounded__(__minbytes__,3,SHA512_256_DIGEST_STRING_LENGTH)));
154145412Strhodes__END_DECLS
155101099Srwatson
156101099Srwatson#endif /* _SHA2_H */
157184214Sdes