1/* sha2.h
2
3 The sha2 family of hash functions.
4
5 Copyright (C) 2001, 2012 Niels Möller
6
7 This file is part of GNU Nettle.
8
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
11
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
15
16 or
17
18 * the GNU General Public License as published by the Free
19 Software Foundation; either version 2 of the License, or (at your
20 option) any later version.
21
22 or both in parallel, as here.
23
24 GNU Nettle is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
28
29 You should have received copies of the GNU General Public License and
30 the GNU Lesser General Public License along with this program. If
31 not, see http://www.gnu.org/licenses/.
32*/
33
34#ifndef NETTLE_SHA2_H_INCLUDED
35#define NETTLE_SHA2_H_INCLUDED
36
37#include "nettle-types.h"
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43/* Name mangling */
44#define sha224_init nettle_sha224_init
45#define sha224_digest nettle_sha224_digest
46#define sha256_init nettle_sha256_init
47#define sha256_update nettle_sha256_update
48#define sha256_digest nettle_sha256_digest
49#define sha384_init nettle_sha384_init
50#define sha384_digest nettle_sha384_digest
51#define sha512_init nettle_sha512_init
52#define sha512_update nettle_sha512_update
53#define sha512_digest nettle_sha512_digest
54#define sha512_224_init nettle_sha512_224_init
55#define sha512_224_digest nettle_sha512_224_digest
56#define sha512_256_init nettle_sha512_256_init
57#define sha512_256_digest nettle_sha512_256_digest
58
59/* For backwards compatibility */
60#define SHA224_DATA_SIZE SHA256_BLOCK_SIZE
61#define SHA256_DATA_SIZE SHA256_BLOCK_SIZE
62#define SHA512_DATA_SIZE SHA512_BLOCK_SIZE
63#define SHA384_DATA_SIZE SHA512_BLOCK_SIZE
64
65/* SHA256 */
66
67#define SHA256_DIGEST_SIZE 32
68#define SHA256_BLOCK_SIZE 64
69
70/* Digest is kept internally as 8 32-bit words. */
71#define _SHA256_DIGEST_LENGTH 8
72
73struct sha256_ctx
74{
75 uint32_t state[_SHA256_DIGEST_LENGTH]; /* State variables */
76 uint64_t count; /* 64-bit block count */
77 uint8_t block[SHA256_BLOCK_SIZE]; /* SHA256 data buffer */
78 unsigned int index; /* index into buffer */
79};
80
81void
82sha256_init(struct sha256_ctx *ctx);
83
84void
85sha256_update(struct sha256_ctx *ctx,
86 size_t length,
87 const uint8_t *data);
88
89void
90sha256_digest(struct sha256_ctx *ctx,
91 size_t length,
92 uint8_t *digest);
93
94/* Internal compression function. STATE points to 8 uint32_t words,
95 DATA points to 64 bytes of input data, possibly unaligned, and K
96 points to the table of constants. */
97void
98_nettle_sha256_compress(uint32_t *state, const uint8_t *data, const uint32_t *k);
99
100
101/* SHA224, a truncated SHA256 with different initial state. */
102
103#define SHA224_DIGEST_SIZE 28
104#define SHA224_BLOCK_SIZE SHA256_BLOCK_SIZE
105#define sha224_ctx sha256_ctx
106
107void
108sha224_init(struct sha256_ctx *ctx);
109
110#define sha224_update nettle_sha256_update
111
112void
113sha224_digest(struct sha256_ctx *ctx,
114 size_t length,
115 uint8_t *digest);
116
117
118/* SHA512 */
119
120#define SHA512_DIGEST_SIZE 64
121#define SHA512_BLOCK_SIZE 128
122
123/* Digest is kept internally as 8 64-bit words. */
124#define _SHA512_DIGEST_LENGTH 8
125
126struct sha512_ctx
127{
128 uint64_t state[_SHA512_DIGEST_LENGTH]; /* State variables */
129 uint64_t count_low, count_high; /* 128-bit block count */
130 uint8_t block[SHA512_BLOCK_SIZE]; /* SHA512 data buffer */
131 unsigned int index; /* index into buffer */
132};
133
134void
135sha512_init(struct sha512_ctx *ctx);
136
137void
138sha512_update(struct sha512_ctx *ctx,
139 size_t length,
140 const uint8_t *data);
141
142void
143sha512_digest(struct sha512_ctx *ctx,
144 size_t length,
145 uint8_t *digest);
146
147/* Internal compression function. STATE points to 8 uint64_t words,
148 DATA points to 128 bytes of input data, possibly unaligned, and K
149 points to the table of constants. */
150void
151_nettle_sha512_compress(uint64_t *state, const uint8_t *data, const uint64_t *k);
152
153
154/* SHA384, a truncated SHA512 with different initial state. */
155
156#define SHA384_DIGEST_SIZE 48
157#define SHA384_BLOCK_SIZE SHA512_BLOCK_SIZE
158#define sha384_ctx sha512_ctx
159
160void
161sha384_init(struct sha512_ctx *ctx);
162
163#define sha384_update nettle_sha512_update
164
165void
166sha384_digest(struct sha512_ctx *ctx,
167 size_t length,
168 uint8_t *digest);
169
170
171/* SHA512_224 and SHA512_256, two truncated versions of SHA512
172 with different initial states. */
173
174#define SHA512_224_DIGEST_SIZE 28
175#define SHA512_224_BLOCK_SIZE SHA512_BLOCK_SIZE
176#define sha512_224_ctx sha512_ctx
177
178void
179sha512_224_init(struct sha512_224_ctx *ctx);
180
181#define sha512_224_update nettle_sha512_update
182
183void
184sha512_224_digest(struct sha512_224_ctx *ctx,
185 size_t length,
186 uint8_t *digest);
187
188#define SHA512_256_DIGEST_SIZE 32
189#define SHA512_256_BLOCK_SIZE SHA512_BLOCK_SIZE
190#define sha512_256_ctx sha512_ctx
191
192void
193sha512_256_init(struct sha512_256_ctx *ctx);
194
195#define sha512_256_update nettle_sha512_update
196
197void
198sha512_256_digest(struct sha512_256_ctx *ctx,
199 size_t length,
200 uint8_t *digest);
201
202#ifdef __cplusplus
203}
204#endif
205
206#endif /* NETTLE_SHA2_H_INCLUDED */
207