New hash variant SHA224.
[u/mdw/catacomb] / sha256.c
1 /* -*-c-*-
2 *
3 * $Id: sha256.c,v 1.2 2004/03/21 22:43:34 mdw Exp $
4 *
5 * Implementation of the SHA-256 hash function
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: sha256.c,v $
33 * Revision 1.2 2004/03/21 22:43:34 mdw
34 * New hash variant SHA224.
35 *
36 * Revision 1.1 2000/10/15 17:48:14 mdw
37 * New SHA variants with longer outputs.
38 *
39 */
40
41 /*----- Header files ------------------------------------------------------*/
42
43 #include <mLib/bits.h>
44
45 #include "ghash.h"
46 #include "ghash-def.h"
47 #include "hash.h"
48 #include "sha256.h"
49
50 /*----- Main code ---------------------------------------------------------*/
51
52 /* --- @sha256_compress@, @sha224_compress@ --- *
53 *
54 * Arguments: @sha256_ctx *ctx@ = pointer to context block
55 * @const void *sbuf@ = pointer to buffer of appropriate size
56 *
57 * Returns: ---
58 *
59 * Use: SHA-256 compression function.
60 */
61
62 void sha256_compress(sha256_ctx *ctx, const void *sbuf)
63 {
64 uint32 a, b, c, d, e, f, g, h;
65 uint32 buf[64];
66
67 /* --- Fetch the chaining variables --- */
68
69 a = ctx->a;
70 b = ctx->b;
71 c = ctx->c;
72 d = ctx->d;
73 e = ctx->e;
74 f = ctx->f;
75 g = ctx->g;
76 h = ctx->h;
77
78 /* --- Definitions for round functions --- */
79
80 #define CH(x, y, z) (((x) & (y)) | (~(x) & (z)))
81 #define MAJ(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
82
83 #define S0(x) (ROR32((x), 2) ^ ROR32((x), 13) ^ ROR32((x), 22))
84 #define S1(x) (ROR32((x), 6) ^ ROR32((x), 11) ^ ROR32((x), 25))
85 #define s0(x) (ROR32((x), 7) ^ ROR32((x), 18) ^ LSR32((x), 3))
86 #define s1(x) (ROR32((x), 17) ^ ROR32((x), 19) ^ LSR32((x), 10))
87
88 #define T(a, b, c, d, e, f, g, h, i, k) do { \
89 uint32 t1 = h + S1(e) + CH(e, f, g) + k + buf[i]; \
90 uint32 t2 = S0(a) + MAJ(a, b, c); \
91 d += t1; h = t1 + t2; \
92 } while (0)
93
94 /* --- Fetch and expand the buffer contents --- */
95
96 {
97 int i;
98 const octet *p;
99
100 for (i = 0, p = sbuf; i < 16; i++, p += 4)
101 buf[i] = LOAD32(p);
102 for (i = 16; i < 64; i++)
103 buf[i] = s1(buf[i - 2]) + buf[i - 7] + s0(buf[i - 15]) + buf[i - 16];
104 }
105
106 /* --- The main compression function --- */
107
108 T(a, b, c, d, e, f, g, h, 0, 0x428a2f98);
109 T(h, a, b, c, d, e, f, g, 1, 0x71374491);
110 T(g, h, a, b, c, d, e, f, 2, 0xb5c0fbcf);
111 T(f, g, h, a, b, c, d, e, 3, 0xe9b5dba5);
112 T(e, f, g, h, a, b, c, d, 4, 0x3956c25b);
113 T(d, e, f, g, h, a, b, c, 5, 0x59f111f1);
114 T(c, d, e, f, g, h, a, b, 6, 0x923f82a4);
115 T(b, c, d, e, f, g, h, a, 7, 0xab1c5ed5);
116 T(a, b, c, d, e, f, g, h, 8, 0xd807aa98);
117 T(h, a, b, c, d, e, f, g, 9, 0x12835b01);
118 T(g, h, a, b, c, d, e, f, 10, 0x243185be);
119 T(f, g, h, a, b, c, d, e, 11, 0x550c7dc3);
120 T(e, f, g, h, a, b, c, d, 12, 0x72be5d74);
121 T(d, e, f, g, h, a, b, c, 13, 0x80deb1fe);
122 T(c, d, e, f, g, h, a, b, 14, 0x9bdc06a7);
123 T(b, c, d, e, f, g, h, a, 15, 0xc19bf174);
124 T(a, b, c, d, e, f, g, h, 16, 0xe49b69c1);
125 T(h, a, b, c, d, e, f, g, 17, 0xefbe4786);
126 T(g, h, a, b, c, d, e, f, 18, 0x0fc19dc6);
127 T(f, g, h, a, b, c, d, e, 19, 0x240ca1cc);
128 T(e, f, g, h, a, b, c, d, 20, 0x2de92c6f);
129 T(d, e, f, g, h, a, b, c, 21, 0x4a7484aa);
130 T(c, d, e, f, g, h, a, b, 22, 0x5cb0a9dc);
131 T(b, c, d, e, f, g, h, a, 23, 0x76f988da);
132 T(a, b, c, d, e, f, g, h, 24, 0x983e5152);
133 T(h, a, b, c, d, e, f, g, 25, 0xa831c66d);
134 T(g, h, a, b, c, d, e, f, 26, 0xb00327c8);
135 T(f, g, h, a, b, c, d, e, 27, 0xbf597fc7);
136 T(e, f, g, h, a, b, c, d, 28, 0xc6e00bf3);
137 T(d, e, f, g, h, a, b, c, 29, 0xd5a79147);
138 T(c, d, e, f, g, h, a, b, 30, 0x06ca6351);
139 T(b, c, d, e, f, g, h, a, 31, 0x14292967);
140 T(a, b, c, d, e, f, g, h, 32, 0x27b70a85);
141 T(h, a, b, c, d, e, f, g, 33, 0x2e1b2138);
142 T(g, h, a, b, c, d, e, f, 34, 0x4d2c6dfc);
143 T(f, g, h, a, b, c, d, e, 35, 0x53380d13);
144 T(e, f, g, h, a, b, c, d, 36, 0x650a7354);
145 T(d, e, f, g, h, a, b, c, 37, 0x766a0abb);
146 T(c, d, e, f, g, h, a, b, 38, 0x81c2c92e);
147 T(b, c, d, e, f, g, h, a, 39, 0x92722c85);
148 T(a, b, c, d, e, f, g, h, 40, 0xa2bfe8a1);
149 T(h, a, b, c, d, e, f, g, 41, 0xa81a664b);
150 T(g, h, a, b, c, d, e, f, 42, 0xc24b8b70);
151 T(f, g, h, a, b, c, d, e, 43, 0xc76c51a3);
152 T(e, f, g, h, a, b, c, d, 44, 0xd192e819);
153 T(d, e, f, g, h, a, b, c, 45, 0xd6990624);
154 T(c, d, e, f, g, h, a, b, 46, 0xf40e3585);
155 T(b, c, d, e, f, g, h, a, 47, 0x106aa070);
156 T(a, b, c, d, e, f, g, h, 48, 0x19a4c116);
157 T(h, a, b, c, d, e, f, g, 49, 0x1e376c08);
158 T(g, h, a, b, c, d, e, f, 50, 0x2748774c);
159 T(f, g, h, a, b, c, d, e, 51, 0x34b0bcb5);
160 T(e, f, g, h, a, b, c, d, 52, 0x391c0cb3);
161 T(d, e, f, g, h, a, b, c, 53, 0x4ed8aa4a);
162 T(c, d, e, f, g, h, a, b, 54, 0x5b9cca4f);
163 T(b, c, d, e, f, g, h, a, 55, 0x682e6ff3);
164 T(a, b, c, d, e, f, g, h, 56, 0x748f82ee);
165 T(h, a, b, c, d, e, f, g, 57, 0x78a5636f);
166 T(g, h, a, b, c, d, e, f, 58, 0x84c87814);
167 T(f, g, h, a, b, c, d, e, 59, 0x8cc70208);
168 T(e, f, g, h, a, b, c, d, 60, 0x90befffa);
169 T(d, e, f, g, h, a, b, c, 61, 0xa4506ceb);
170 T(c, d, e, f, g, h, a, b, 62, 0xbef9a3f7);
171 T(b, c, d, e, f, g, h, a, 63, 0xc67178f2);
172
173 /* --- Update the chaining variables --- */
174
175 ctx->a += a;
176 ctx->b += b;
177 ctx->c += c;
178 ctx->d += d;
179 ctx->e += e;
180 ctx->f += f;
181 ctx->g += g;
182 ctx->h += h;
183 }
184
185 /* --- @sha256_init@, @sha224_init@ --- *
186 *
187 * Arguments: @sha256_ctx *ctx@ = pointer to context block to initialize
188 *
189 * Returns: ---
190 *
191 * Use: Initializes a context block ready for hashing.
192 */
193
194 void sha256_init(sha256_ctx *ctx)
195 {
196 ctx->a = 0x6a09e667;
197 ctx->b = 0xbb67ae85;
198 ctx->c = 0x3c6ef372;
199 ctx->d = 0xa54ff53a;
200 ctx->e = 0x510e527f;
201 ctx->f = 0x9b05688c;
202 ctx->g = 0x1f83d9ab;
203 ctx->h = 0x5be0cd19;
204 ctx->off = 0;
205 ctx->nl = ctx->nh = 0;
206 }
207
208 void sha224_init(sha256_ctx *ctx)
209 {
210 ctx->a = 0xc1059ed8;
211 ctx->b = 0x367cd507;
212 ctx->c = 0x3070dd17;
213 ctx->d = 0xf70e5939;
214 ctx->e = 0xffc00b31;
215 ctx->f = 0x68581511;
216 ctx->g = 0x64f98fa7;
217 ctx->h = 0xbefa4fa4;
218 ctx->off = 0;
219 ctx->nl = ctx->nh = 0;
220 }
221
222 /* --- @sha256_set@, @sha224_set@ --- *
223 *
224 * Arguments: @sha256_ctx *ctx@ = pointer to context block
225 * @const void *buf@ = pointer to state buffer
226 * @unsigned long count@ = current count of bytes processed
227 *
228 * Returns: ---
229 *
230 * Use: Initializes a context block from a given state. This is
231 * useful in cases where the initial hash state is meant to be
232 * secret, e.g., for NMAC and HMAC support.
233 */
234
235 void sha256_set(sha256_ctx *ctx, const void *buf, unsigned long count)
236 {
237 const octet *p = buf;
238 ctx->a = LOAD32(p + 0);
239 ctx->b = LOAD32(p + 4);
240 ctx->c = LOAD32(p + 8);
241 ctx->d = LOAD32(p + 12);
242 ctx->e = LOAD32(p + 16);
243 ctx->f = LOAD32(p + 20);
244 ctx->g = LOAD32(p + 24);
245 ctx->h = LOAD32(p + 28);
246 ctx->off = 0;
247 ctx->nl = U32(count);
248 ctx->nh = U32(((count & ~MASK32) >> 16) >> 16);
249 }
250
251 /* --- @sha256_hash@, @sha224_hash@ --- *
252 *
253 * Arguments: @sha256_ctx *ctx@ = pointer to context block
254 * @const void *buf@ = buffer of data to hash
255 * @size_t sz@ = size of buffer to hash
256 *
257 * Returns: ---
258 *
259 * Use: Hashes a buffer of data. The buffer may be of any size and
260 * alignment.
261 */
262
263 void sha256_hash(sha256_ctx *ctx, const void *buf, size_t sz)
264 {
265 HASH_BUFFER(SHA256, sha256, ctx, buf, sz);
266 }
267
268 /* --- @sha256_done, @sha224_done@ --- *
269 *
270 * Arguments: @sha256_ctx *ctx@ = pointer to context block
271 * @void *hash@ = pointer to output buffer
272 *
273 * Returns: ---
274 *
275 * Use: Returns the hash of the data read so far.
276 */
277
278 static void final(sha256_ctx *ctx)
279 {
280 HASH_PAD(SHA256, sha256, ctx, 0x80, 0, 8);
281 STORE32(ctx->buf + SHA256_BUFSZ - 8, (ctx->nl >> 29) | (ctx->nh << 3));
282 STORE32(ctx->buf + SHA256_BUFSZ - 4, ctx->nl << 3);
283 sha256_compress(ctx, ctx->buf);
284 }
285
286 void sha256_done(sha256_ctx *ctx, void *hash)
287 {
288 octet *p = hash;
289 final(ctx);
290 STORE32(p + 0, ctx->a);
291 STORE32(p + 4, ctx->b);
292 STORE32(p + 8, ctx->c);
293 STORE32(p + 12, ctx->d);
294 STORE32(p + 16, ctx->e);
295 STORE32(p + 20, ctx->f);
296 STORE32(p + 24, ctx->g);
297 STORE32(p + 28, ctx->h);
298 }
299
300 void sha224_done(sha224_ctx *ctx, void *hash)
301 {
302 octet *p = hash;
303 final(ctx);
304 STORE32(p + 0, ctx->a);
305 STORE32(p + 4, ctx->b);
306 STORE32(p + 8, ctx->c);
307 STORE32(p + 12, ctx->d);
308 STORE32(p + 16, ctx->e);
309 STORE32(p + 20, ctx->f);
310 STORE32(p + 24, ctx->g);
311 }
312
313 /* --- @sha256_state@, @sha224_state@ --- *
314 *
315 * Arguments: @sha256_ctx *ctx@ = pointer to context
316 * @void *state@ = pointer to buffer for current state
317 *
318 * Returns: Number of bytes written to the hash function so far.
319 *
320 * Use: Returns the current state of the hash function such that
321 * it can be passed to @sha256_set@.
322 */
323
324 unsigned long sha256_state(sha256_ctx *ctx, void *state)
325 {
326 octet *p = state;
327 STORE32(p + 0, ctx->a);
328 STORE32(p + 4, ctx->b);
329 STORE32(p + 8, ctx->c);
330 STORE32(p + 12, ctx->d);
331 STORE32(p + 16, ctx->e);
332 STORE32(p + 20, ctx->f);
333 STORE32(p + 24, ctx->g);
334 STORE32(p + 28, ctx->h);
335 return (ctx->nl | ((ctx->nh << 16) << 16));
336 }
337
338 /* --- Generic interface --- */
339
340 GHASH_DEF(SHA256, sha256)
341
342 /* --- Test code --- */
343
344 HASH_TEST(SHA256, sha256)
345
346 /*----- That's all, folks -------------------------------------------------*/