1170222Sdougb/*
2224092Sdougb * Copyright (C) 2005-2007, 2009  Internet Systems Consortium, Inc. ("ISC")
3170222Sdougb *
4193149Sdougb * Permission to use, copy, modify, and/or distribute this software for any
5170222Sdougb * purpose with or without fee is hereby granted, provided that the above
6170222Sdougb * copyright notice and this permission notice appear in all copies.
7170222Sdougb *
8170222Sdougb * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9170222Sdougb * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10170222Sdougb * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11170222Sdougb * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12170222Sdougb * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13170222Sdougb * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14170222Sdougb * PERFORMANCE OF THIS SOFTWARE.
15170222Sdougb */
16170222Sdougb
17234010Sdougb/* $Id: sha2.h,v 1.12 2009/10/22 02:21:31 each Exp $ */
18170222Sdougb
19170222Sdougb/*	$FreeBSD$	*/
20170222Sdougb/*	$KAME: sha2.h,v 1.3 2001/03/12 08:27:48 itojun Exp $	*/
21170222Sdougb
22170222Sdougb/*
23170222Sdougb * sha2.h
24170222Sdougb *
25170222Sdougb * Version 1.0.0beta1
26170222Sdougb *
27170222Sdougb * Written by Aaron D. Gifford <me@aarongifford.com>
28170222Sdougb *
29170222Sdougb * Copyright 2000 Aaron D. Gifford.  All rights reserved.
30170222Sdougb *
31170222Sdougb * Redistribution and use in source and binary forms, with or without
32170222Sdougb * modification, are permitted provided that the following conditions
33170222Sdougb * are met:
34170222Sdougb * 1. Redistributions of source code must retain the above copyright
35170222Sdougb *    notice, this list of conditions and the following disclaimer.
36170222Sdougb * 2. Redistributions in binary form must reproduce the above copyright
37170222Sdougb *    notice, this list of conditions and the following disclaimer in the
38170222Sdougb *    documentation and/or other materials provided with the distribution.
39170222Sdougb * 3. Neither the name of the copyright holder nor the names of contributors
40170222Sdougb *    may be used to endorse or promote products derived from this software
41170222Sdougb *    without specific prior written permission.
42204619Sdougb *
43170222Sdougb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``AS IS'' AND
44170222Sdougb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45170222Sdougb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46170222Sdougb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE
47170222Sdougb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48170222Sdougb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49170222Sdougb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50170222Sdougb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51170222Sdougb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52170222Sdougb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53170222Sdougb * SUCH DAMAGE.
54170222Sdougb *
55170222Sdougb */
56170222Sdougb
57170222Sdougb#ifndef ISC_SHA2_H
58170222Sdougb#define ISC_SHA2_H
59170222Sdougb
60170222Sdougb#include <isc/lang.h>
61224092Sdougb#include <isc/platform.h>
62170222Sdougb#include <isc/types.h>
63170222Sdougb
64170222Sdougb/*** SHA-224/256/384/512 Various Length Definitions ***********************/
65170222Sdougb
66170222Sdougb#define ISC_SHA224_BLOCK_LENGTH		64U
67170222Sdougb#define ISC_SHA224_DIGESTLENGTH	28U
68170222Sdougb#define ISC_SHA224_DIGESTSTRINGLENGTH	(ISC_SHA224_DIGESTLENGTH * 2 + 1)
69170222Sdougb#define ISC_SHA256_BLOCK_LENGTH		64U
70170222Sdougb#define ISC_SHA256_DIGESTLENGTH	32U
71170222Sdougb#define ISC_SHA256_DIGESTSTRINGLENGTH	(ISC_SHA256_DIGESTLENGTH * 2 + 1)
72170222Sdougb#define ISC_SHA384_BLOCK_LENGTH		128
73170222Sdougb#define ISC_SHA384_DIGESTLENGTH	48U
74170222Sdougb#define ISC_SHA384_DIGESTSTRINGLENGTH	(ISC_SHA384_DIGESTLENGTH * 2 + 1)
75170222Sdougb#define ISC_SHA512_BLOCK_LENGTH		128U
76170222Sdougb#define ISC_SHA512_DIGESTLENGTH	64U
77170222Sdougb#define ISC_SHA512_DIGESTSTRINGLENGTH	(ISC_SHA512_DIGESTLENGTH * 2 + 1)
78170222Sdougb
79224092Sdougb/*** SHA-256/384/512 Context Structures *******************************/
80170222Sdougb
81224092Sdougb#ifdef ISC_PLATFORM_OPENSSLHASH
82224092Sdougb#include <openssl/evp.h>
83170222Sdougb
84224092Sdougbtypedef EVP_MD_CTX isc_sha256_t;
85224092Sdougbtypedef EVP_MD_CTX isc_sha512_t;
86170222Sdougb
87224092Sdougb#else
88224092Sdougb
89170222Sdougb/*
90170222Sdougb * Keep buffer immediately after bitcount to preserve alignment.
91170222Sdougb */
92170222Sdougbtypedef struct {
93170222Sdougb	isc_uint32_t	state[8];
94170222Sdougb	isc_uint64_t	bitcount;
95170222Sdougb	isc_uint8_t	buffer[ISC_SHA256_BLOCK_LENGTH];
96170222Sdougb} isc_sha256_t;
97170222Sdougb
98170222Sdougb/*
99170222Sdougb * Keep buffer immediately after bitcount to preserve alignment.
100170222Sdougb */
101170222Sdougbtypedef struct {
102170222Sdougb	isc_uint64_t	state[8];
103170222Sdougb	isc_uint64_t	bitcount[2];
104170222Sdougb	isc_uint8_t	buffer[ISC_SHA512_BLOCK_LENGTH];
105170222Sdougb} isc_sha512_t;
106224092Sdougb#endif
107170222Sdougb
108170222Sdougbtypedef isc_sha256_t isc_sha224_t;
109170222Sdougbtypedef isc_sha512_t isc_sha384_t;
110170222Sdougb
111224092SdougbISC_LANG_BEGINDECLS
112224092Sdougb
113170222Sdougb/*** SHA-224/256/384/512 Function Prototypes ******************************/
114170222Sdougb
115170222Sdougbvoid isc_sha224_init (isc_sha224_t *);
116204619Sdougbvoid isc_sha224_invalidate (isc_sha224_t *);
117170222Sdougbvoid isc_sha224_update (isc_sha224_t *, const isc_uint8_t *, size_t);
118170222Sdougbvoid isc_sha224_final (isc_uint8_t[ISC_SHA224_DIGESTLENGTH], isc_sha224_t *);
119170222Sdougbchar *isc_sha224_end (isc_sha224_t *, char[ISC_SHA224_DIGESTSTRINGLENGTH]);
120170222Sdougbchar *isc_sha224_data (const isc_uint8_t *, size_t, char[ISC_SHA224_DIGESTSTRINGLENGTH]);
121170222Sdougb
122170222Sdougbvoid isc_sha256_init (isc_sha256_t *);
123204619Sdougbvoid isc_sha256_invalidate (isc_sha256_t *);
124170222Sdougbvoid isc_sha256_update (isc_sha256_t *, const isc_uint8_t *, size_t);
125170222Sdougbvoid isc_sha256_final (isc_uint8_t[ISC_SHA256_DIGESTLENGTH], isc_sha256_t *);
126170222Sdougbchar *isc_sha256_end (isc_sha256_t *, char[ISC_SHA256_DIGESTSTRINGLENGTH]);
127170222Sdougbchar *isc_sha256_data (const isc_uint8_t *, size_t, char[ISC_SHA256_DIGESTSTRINGLENGTH]);
128170222Sdougb
129170222Sdougbvoid isc_sha384_init (isc_sha384_t *);
130204619Sdougbvoid isc_sha384_invalidate (isc_sha384_t *);
131170222Sdougbvoid isc_sha384_update (isc_sha384_t *, const isc_uint8_t *, size_t);
132170222Sdougbvoid isc_sha384_final (isc_uint8_t[ISC_SHA384_DIGESTLENGTH], isc_sha384_t *);
133170222Sdougbchar *isc_sha384_end (isc_sha384_t *, char[ISC_SHA384_DIGESTSTRINGLENGTH]);
134170222Sdougbchar *isc_sha384_data (const isc_uint8_t *, size_t, char[ISC_SHA384_DIGESTSTRINGLENGTH]);
135170222Sdougb
136170222Sdougbvoid isc_sha512_init (isc_sha512_t *);
137204619Sdougbvoid isc_sha512_invalidate (isc_sha512_t *);
138170222Sdougbvoid isc_sha512_update (isc_sha512_t *, const isc_uint8_t *, size_t);
139170222Sdougbvoid isc_sha512_final (isc_uint8_t[ISC_SHA512_DIGESTLENGTH], isc_sha512_t *);
140170222Sdougbchar *isc_sha512_end (isc_sha512_t *, char[ISC_SHA512_DIGESTSTRINGLENGTH]);
141170222Sdougbchar *isc_sha512_data (const isc_uint8_t *, size_t, char[ISC_SHA512_DIGESTSTRINGLENGTH]);
142170222Sdougb
143170222SdougbISC_LANG_ENDDECLS
144170222Sdougb
145170222Sdougb#endif /* ISC_SHA2_H */
146