xref: /minix3/external/bsd/bind/dist/lib/isc/include/isc/sha2.h (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1 /*	$NetBSD: sha2.h,v 1.5 2014/12/10 04:38:00 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2005-2007, 2009, 2014  Internet Systems Consortium, Inc. ("ISC")
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /* Id: sha2.h,v 1.12 2009/10/22 02:21:31 each Exp  */
20 
21 /*	$FreeBSD: src/sys/crypto/sha2/sha2.h,v 1.1.2.1 2001/07/03 11:01:36 ume Exp $	*/
22 /*	$KAME: sha2.h,v 1.3 2001/03/12 08:27:48 itojun Exp $	*/
23 
24 /*
25  * sha2.h
26  *
27  * Version 1.0.0beta1
28  *
29  * Written by Aaron D. Gifford <me@aarongifford.com>
30  *
31  * Copyright 2000 Aaron D. Gifford.  All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  * 3. Neither the name of the copyright holder nor the names of contributors
42  *    may be used to endorse or promote products derived from this software
43  *    without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  *
57  */
58 
59 #ifndef ISC_SHA2_H
60 #define ISC_SHA2_H
61 
62 #include <isc/lang.h>
63 #include <isc/platform.h>
64 #include <isc/types.h>
65 
66 /*** SHA-224/256/384/512 Various Length Definitions ***********************/
67 
68 #define ISC_SHA224_BLOCK_LENGTH		64U
69 #define ISC_SHA224_DIGESTLENGTH	28U
70 #define ISC_SHA224_DIGESTSTRINGLENGTH	(ISC_SHA224_DIGESTLENGTH * 2 + 1)
71 #define ISC_SHA256_BLOCK_LENGTH		64U
72 #define ISC_SHA256_DIGESTLENGTH	32U
73 #define ISC_SHA256_DIGESTSTRINGLENGTH	(ISC_SHA256_DIGESTLENGTH * 2 + 1)
74 #define ISC_SHA384_BLOCK_LENGTH		128
75 #define ISC_SHA384_DIGESTLENGTH	48U
76 #define ISC_SHA384_DIGESTSTRINGLENGTH	(ISC_SHA384_DIGESTLENGTH * 2 + 1)
77 #define ISC_SHA512_BLOCK_LENGTH		128U
78 #define ISC_SHA512_DIGESTLENGTH	64U
79 #define ISC_SHA512_DIGESTSTRINGLENGTH	(ISC_SHA512_DIGESTLENGTH * 2 + 1)
80 
81 /*** SHA-256/384/512 Context Structures *******************************/
82 
83 #ifdef ISC_PLATFORM_OPENSSLHASH
84 #include <openssl/evp.h>
85 
86 typedef EVP_MD_CTX isc_sha256_t;
87 typedef EVP_MD_CTX isc_sha512_t;
88 
89 #elif PKCS11CRYPTO
90 #include <pk11/pk11.h>
91 
92 typedef pk11_context_t isc_sha256_t;
93 typedef pk11_context_t isc_sha512_t;
94 
95 #else
96 
97 /*
98  * Keep buffer immediately after bitcount to preserve alignment.
99  */
100 typedef struct {
101 	isc_uint32_t	state[8];
102 	isc_uint64_t	bitcount;
103 	isc_uint8_t	buffer[ISC_SHA256_BLOCK_LENGTH];
104 } isc_sha256_t;
105 
106 /*
107  * Keep buffer immediately after bitcount to preserve alignment.
108  */
109 typedef struct {
110 	isc_uint64_t	state[8];
111 	isc_uint64_t	bitcount[2];
112 	isc_uint8_t	buffer[ISC_SHA512_BLOCK_LENGTH];
113 } isc_sha512_t;
114 #endif
115 
116 typedef isc_sha256_t isc_sha224_t;
117 typedef isc_sha512_t isc_sha384_t;
118 
119 ISC_LANG_BEGINDECLS
120 
121 /*** SHA-224/256/384/512 Function Prototypes ******************************/
122 
123 void isc_sha224_init (isc_sha224_t *);
124 void isc_sha224_invalidate (isc_sha224_t *);
125 void isc_sha224_update (isc_sha224_t *, const isc_uint8_t *, size_t);
126 void isc_sha224_final (isc_uint8_t[ISC_SHA224_DIGESTLENGTH], isc_sha224_t *);
127 char *isc_sha224_end (isc_sha224_t *, char[ISC_SHA224_DIGESTSTRINGLENGTH]);
128 char *isc_sha224_data (const isc_uint8_t *, size_t, char[ISC_SHA224_DIGESTSTRINGLENGTH]);
129 
130 void isc_sha256_init (isc_sha256_t *);
131 void isc_sha256_invalidate (isc_sha256_t *);
132 void isc_sha256_update (isc_sha256_t *, const isc_uint8_t *, size_t);
133 void isc_sha256_final (isc_uint8_t[ISC_SHA256_DIGESTLENGTH], isc_sha256_t *);
134 char *isc_sha256_end (isc_sha256_t *, char[ISC_SHA256_DIGESTSTRINGLENGTH]);
135 char *isc_sha256_data (const isc_uint8_t *, size_t, char[ISC_SHA256_DIGESTSTRINGLENGTH]);
136 
137 void isc_sha384_init (isc_sha384_t *);
138 void isc_sha384_invalidate (isc_sha384_t *);
139 void isc_sha384_update (isc_sha384_t *, const isc_uint8_t *, size_t);
140 void isc_sha384_final (isc_uint8_t[ISC_SHA384_DIGESTLENGTH], isc_sha384_t *);
141 char *isc_sha384_end (isc_sha384_t *, char[ISC_SHA384_DIGESTSTRINGLENGTH]);
142 char *isc_sha384_data (const isc_uint8_t *, size_t, char[ISC_SHA384_DIGESTSTRINGLENGTH]);
143 
144 void isc_sha512_init (isc_sha512_t *);
145 void isc_sha512_invalidate (isc_sha512_t *);
146 void isc_sha512_update (isc_sha512_t *, const isc_uint8_t *, size_t);
147 void isc_sha512_final (isc_uint8_t[ISC_SHA512_DIGESTLENGTH], isc_sha512_t *);
148 char *isc_sha512_end (isc_sha512_t *, char[ISC_SHA512_DIGESTSTRINGLENGTH]);
149 char *isc_sha512_data (const isc_uint8_t *, size_t, char[ISC_SHA512_DIGESTSTRINGLENGTH]);
150 
151 ISC_LANG_ENDDECLS
152 
153 #endif /* ISC_SHA2_H */
154