[Title] Extract nettle-2.1.tar.gz from Nettle's official repository.
[external/nettle.git] / sha.h
1 /* sha.h
2  *
3  * The sha1 and sha256 hash functions.
4  */
5
6 /* nettle, low-level cryptographics library
7  *
8  * Copyright (C) 2001 Niels Möller
9  *  
10  * The nettle library is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or (at your
13  * option) any later version.
14  * 
15  * The nettle library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
18  * License for more details.
19  * 
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with the nettle library; see the file COPYING.LIB.  If not, write to
22  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23  * MA 02111-1307, USA.
24  */
25  
26 #ifndef NETTLE_SHA_H_INCLUDED
27 #define NETTLE_SHA_H_INCLUDED
28
29 #include "nettle-types.h"
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /* Name mangling */
36 #define sha1_init nettle_sha1_init
37 #define sha1_update nettle_sha1_update
38 #define sha1_digest nettle_sha1_digest
39 #define sha224_init nettle_sha224_init
40 #define sha224_digest nettle_sha224_digest
41 #define sha256_init nettle_sha256_init
42 #define sha256_update nettle_sha256_update
43 #define sha256_digest nettle_sha256_digest
44 #define sha384_init nettle_sha384_init
45 #define sha384_digest nettle_sha384_digest
46 #define sha512_init nettle_sha512_init
47 #define sha512_update nettle_sha512_update
48 #define sha512_digest nettle_sha512_digest
49
50 /* SHA1 */
51
52 #define SHA1_DIGEST_SIZE 20
53 #define SHA1_DATA_SIZE 64
54
55 /* Digest is kept internally as 5 32-bit words. */
56 #define _SHA1_DIGEST_LENGTH 5
57
58 struct sha1_ctx
59 {
60   uint32_t digest[_SHA1_DIGEST_LENGTH];   /* Message digest */
61   uint32_t count_low, count_high;         /* 64-bit block count */
62   uint8_t block[SHA1_DATA_SIZE];          /* SHA1 data buffer */
63   unsigned int index;                     /* index into buffer */
64 };
65
66 void
67 sha1_init(struct sha1_ctx *ctx);
68
69 void
70 sha1_update(struct sha1_ctx *ctx,
71             unsigned length,
72             const uint8_t *data);
73
74 void
75 sha1_digest(struct sha1_ctx *ctx,
76             unsigned length,
77             uint8_t *digest);
78
79 /* Internal compression function. STATE points to 5 uint32_t words,
80    and DATA points to 64 bytes of input data, possibly unaligned. */
81 void
82 _nettle_sha1_compress(uint32_t *state, const uint8_t *data);
83
84 /* SHA256 */
85
86 #define SHA256_DIGEST_SIZE 32
87 #define SHA256_DATA_SIZE 64
88
89 /* Digest is kept internally as 8 32-bit words. */
90 #define _SHA256_DIGEST_LENGTH 8
91
92 struct sha256_ctx
93 {
94   uint32_t state[_SHA256_DIGEST_LENGTH];    /* State variables */
95   uint32_t count_low, count_high;           /* 64-bit block count */
96   uint8_t block[SHA256_DATA_SIZE];          /* SHA256 data buffer */
97   unsigned int index;                       /* index into buffer */
98 };
99
100 void
101 sha256_init(struct sha256_ctx *ctx);
102
103 void
104 sha256_update(struct sha256_ctx *ctx,
105               unsigned length,
106               const uint8_t *data);
107
108 void
109 sha256_digest(struct sha256_ctx *ctx,
110               unsigned length,
111               uint8_t *digest);
112
113 /* Internal compression function. STATE points to 8 uint32_t words,
114    DATA points to 64 bytes of input data, possibly unaligned, and K
115    points to the table of constants. */
116 void
117 _nettle_sha256_compress(uint32_t *state, const uint8_t *data, const uint32_t *k);
118
119
120 /* SHA224, a truncated SHA256 with different initial state. */
121
122 #define SHA224_DIGEST_SIZE 28
123 #define SHA224_DATA_SIZE SHA256_DATA_SIZE
124 #define sha224_ctx sha256_ctx
125
126 void
127 sha224_init(struct sha256_ctx *ctx);
128
129 #define sha224_update nettle_sha256_update
130
131 void
132 sha224_digest(struct sha256_ctx *ctx,
133               unsigned length,
134               uint8_t *digest);
135
136
137 /* SHA512 */
138
139 #define SHA512_DIGEST_SIZE 64
140 #define SHA512_DATA_SIZE 128
141
142 /* Digest is kept internally as 8 64-bit words. */
143 #define _SHA512_DIGEST_LENGTH 8
144
145 struct sha512_ctx
146 {
147   uint64_t state[_SHA512_DIGEST_LENGTH];    /* State variables */
148   uint64_t count_low, count_high;           /* 128-bit block count */
149   uint8_t block[SHA512_DATA_SIZE];          /* SHA512 data buffer */
150   unsigned int index;                       /* index into buffer */
151 };
152
153 void
154 sha512_init(struct sha512_ctx *ctx);
155
156 void
157 sha512_update(struct sha512_ctx *ctx,
158               unsigned length,
159               const uint8_t *data);
160
161 void
162 sha512_digest(struct sha512_ctx *ctx,
163               unsigned length,
164               uint8_t *digest);
165
166 /* Internal compression function. STATE points to 8 uint64_t words,
167    DATA points to 128 bytes of input data, possibly unaligned, and K
168    points to the table of constants. */
169 void
170 _nettle_sha512_compress(uint64_t *state, const uint8_t *data, const uint64_t *k);
171
172
173 /* SHA384, a truncated SHA512 with different initial state. */
174
175 #define SHA384_DIGEST_SIZE 48
176 #define SHA384_DATA_SIZE SHA512_DATA_SIZE
177 #define sha384_ctx sha512_ctx
178
179 void
180 sha384_init(struct sha512_ctx *ctx);
181
182 #define sha384_update nettle_sha512_update
183
184 void
185 sha384_digest(struct sha512_ctx *ctx,
186               unsigned length,
187               uint8_t *digest);
188
189 #ifdef __cplusplus
190 }
191 #endif
192
193 #endif /* NETTLE_SHA_H_INCLUDED */