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