chiark / gitweb /
Version bump.
[catacomb] / sha.c
1 /* -*-c-*-
2  *
3  * $Id: sha.c,v 1.3 2000/06/17 11:31:43 mdw Exp $
4  *
5  * Implementation of the SHA-1 hash function
6  *
7  * (c) 1999 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: sha.c,v $
33  * Revision 1.3  2000/06/17 11:31:43  mdw
34  * Portability fix for broken compilers.
35  *
36  * Revision 1.2  1999/12/10 23:20:03  mdw
37  * New hash interface requirements.
38  *
39  * Revision 1.1  1999/09/03 08:41:12  mdw
40  * Initial import.
41  *
42  */
43
44 /*----- Header files ------------------------------------------------------*/
45
46 #include <mLib/bits.h>
47
48 #include "ghash.h"
49 #include "ghash-def.h"
50 #include "hash.h"
51 #include "sha.h"
52
53 /*----- Main code ---------------------------------------------------------*/
54
55 /* --- @sha_compress@ --- *
56  *
57  * Arguments:   @sha_ctx *ctx@ = pointer to context block
58  *              @const void *sbuf@ = pointer to buffer of appropriate size
59  *
60  * Returns:     ---
61  *
62  * Use:         SHA-1 compression function.
63  */
64
65 void sha_compress(sha_ctx *ctx, const void *sbuf)
66 {
67   uint32 a, b, c, d, e;
68   uint32 buf[80];
69
70   /* --- Fetch the chaining variables --- */
71
72   a = ctx->a;
73   b = ctx->b;
74   c = ctx->c;
75   d = ctx->d;
76   e = ctx->e;
77
78   /* --- Fetch and expand the buffer contents --- */
79
80   {
81     int i;
82     const octet *p;
83
84     for (i = 0, p = sbuf; i < 16; i++, p += 4)
85       buf[i] = LOAD32(p);
86     for (i = 16; i < 80; i++) {
87       uint32 x = buf[i - 3] ^ buf[i - 8] ^ buf[i - 14] ^ buf[i - 16];
88       buf[i] = ROL32(x, 1);
89     }
90   }
91
92   /* --- Definitions for round functions --- */
93
94 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
95 #define G(x, y, z) ((x) ^ (y) ^ (z))
96 #define H(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
97
98 #define T(v, w, x, y, z, i, f, k) do {                                  \
99   z = ROL32(v, 5) + f(w, x, y) + z + buf[i] + k;                        \
100   w = ROR32(w, 2);                                                      \
101 } while (0)
102
103 #define FF(v, w, x, y, z, i) T(v, w, x, y, z, i, F, 0x5a827999)
104 #define GG(v, w, x, y, z, i) T(v, w, x, y, z, i, G, 0x6ed9eba1)
105 #define HH(v, w, x, y, z, i) T(v, w, x, y, z, i, H, 0x8f1bbcdc)
106 #define II(v, w, x, y, z, i) T(v, w, x, y, z, i, G, 0xca62c1d6)
107
108   /* --- The main compression function --- */
109
110   FF(a, b, c, d, e,  0);
111   FF(e, a, b, c, d,  1);
112   FF(d, e, a, b, c,  2);
113   FF(c, d, e, a, b,  3);
114   FF(b, c, d, e, a,  4);
115   FF(a, b, c, d, e,  5);
116   FF(e, a, b, c, d,  6);
117   FF(d, e, a, b, c,  7);
118   FF(c, d, e, a, b,  8);
119   FF(b, c, d, e, a,  9);
120   FF(a, b, c, d, e, 10);
121   FF(e, a, b, c, d, 11);
122   FF(d, e, a, b, c, 12);
123   FF(c, d, e, a, b, 13);
124   FF(b, c, d, e, a, 14);
125   FF(a, b, c, d, e, 15);
126   FF(e, a, b, c, d, 16);
127   FF(d, e, a, b, c, 17);
128   FF(c, d, e, a, b, 18);
129   FF(b, c, d, e, a, 19);
130
131   GG(a, b, c, d, e, 20);
132   GG(e, a, b, c, d, 21);
133   GG(d, e, a, b, c, 22);
134   GG(c, d, e, a, b, 23);
135   GG(b, c, d, e, a, 24);
136   GG(a, b, c, d, e, 25);
137   GG(e, a, b, c, d, 26);
138   GG(d, e, a, b, c, 27);
139   GG(c, d, e, a, b, 28);
140   GG(b, c, d, e, a, 29);
141   GG(a, b, c, d, e, 30);
142   GG(e, a, b, c, d, 31);
143   GG(d, e, a, b, c, 32);
144   GG(c, d, e, a, b, 33);
145   GG(b, c, d, e, a, 34);
146   GG(a, b, c, d, e, 35);
147   GG(e, a, b, c, d, 36);
148   GG(d, e, a, b, c, 37);
149   GG(c, d, e, a, b, 38);
150   GG(b, c, d, e, a, 39);
151
152   HH(a, b, c, d, e, 40);
153   HH(e, a, b, c, d, 41);
154   HH(d, e, a, b, c, 42);
155   HH(c, d, e, a, b, 43);
156   HH(b, c, d, e, a, 44);
157   HH(a, b, c, d, e, 45);
158   HH(e, a, b, c, d, 46);
159   HH(d, e, a, b, c, 47);
160   HH(c, d, e, a, b, 48);
161   HH(b, c, d, e, a, 49);
162   HH(a, b, c, d, e, 50);
163   HH(e, a, b, c, d, 51);
164   HH(d, e, a, b, c, 52);
165   HH(c, d, e, a, b, 53);
166   HH(b, c, d, e, a, 54);
167   HH(a, b, c, d, e, 55);
168   HH(e, a, b, c, d, 56);
169   HH(d, e, a, b, c, 57);
170   HH(c, d, e, a, b, 58);
171   HH(b, c, d, e, a, 59);
172
173   II(a, b, c, d, e, 60);
174   II(e, a, b, c, d, 61);
175   II(d, e, a, b, c, 62);
176   II(c, d, e, a, b, 63);
177   II(b, c, d, e, a, 64);
178   II(a, b, c, d, e, 65);
179   II(e, a, b, c, d, 66);
180   II(d, e, a, b, c, 67);
181   II(c, d, e, a, b, 68);
182   II(b, c, d, e, a, 69);
183   II(a, b, c, d, e, 70);
184   II(e, a, b, c, d, 71);
185   II(d, e, a, b, c, 72);
186   II(c, d, e, a, b, 73);
187   II(b, c, d, e, a, 74);
188   II(a, b, c, d, e, 75);
189   II(e, a, b, c, d, 76);
190   II(d, e, a, b, c, 77);
191   II(c, d, e, a, b, 78);
192   II(b, c, d, e, a, 79);
193
194   /* --- Update the chaining variables --- */
195
196   ctx->a += a;
197   ctx->b += b;
198   ctx->c += c;
199   ctx->d += d;
200   ctx->e += e;
201 }
202
203 /* --- @sha_init@ --- *
204  *
205  * Arguments:   @sha_ctx *ctx@ = pointer to context block to initialize
206  *
207  * Returns:     ---
208  *
209  * Use:         Initializes a context block ready for hashing.
210  */
211
212 void sha_init(sha_ctx *ctx)
213 {
214   ctx->a = 0x67452301;
215   ctx->b = 0xefcdab89;
216   ctx->c = 0x98badcfe;
217   ctx->d = 0x10325476;
218   ctx->e = 0xc3d2e1f0;
219   ctx->off = 0;
220   ctx->nl = ctx->nh = 0;
221 }
222
223 /* --- @sha_set@ --- *
224  *
225  * Arguments:   @sha_ctx *ctx@ = pointer to context block
226  *              @const void *buf@ = pointer to state buffer
227  *              @unsigned long count@ = current count of bytes processed
228  *
229  * Returns:     ---
230  *
231  * Use:         Initializes a context block from a given state.  This is
232  *              useful in cases where the initial hash state is meant to be
233  *              secret, e.g., for NMAC and HMAC support.
234  */
235
236 void sha_set(sha_ctx *ctx, const void *buf, unsigned long count)
237 {
238   const octet *p = buf;
239   ctx->a = LOAD32(p +  0);
240   ctx->b = LOAD32(p +  4);
241   ctx->c = LOAD32(p +  8);
242   ctx->d = LOAD32(p + 12);
243   ctx->e = LOAD32(p + 16);
244   ctx->off = 0;
245   ctx->nl = U32(count);
246   ctx->nh = U32(((count & ~MASK32) >> 16) >> 16);
247 }
248
249 /* --- @sha_hash@ --- *
250  *
251  * Arguments:   @sha_ctx *ctx@ = pointer to context block
252  *              @const void *buf@ = buffer of data to hash
253  *              @size_t sz@ = size of buffer to hash
254  *
255  * Returns:     ---
256  *
257  * Use:         Hashes a buffer of data.  The buffer may be of any size and
258  *              alignment.
259  */
260
261 void sha_hash(sha_ctx *ctx, const void *buf, size_t sz)
262 {
263   HASH_BUFFER(SHA, sha, ctx, buf, sz);
264 }
265
266 /* --- @sha_done@ --- *
267  *
268  * Arguments:   @sha_ctx *ctx@ = pointer to context block
269  *              @void *hash@ = pointer to output buffer
270  *
271  * Returns:     ---
272  *
273  * Use:         Returns the hash of the data read so far.
274  */
275
276 void sha_done(sha_ctx *ctx, void *hash)
277 {
278   octet *p = hash;
279   HASH_PAD(SHA, sha, ctx, 0x80, 0, 8);
280   STORE32(ctx->buf + SHA_BUFSZ - 8, (ctx->nl >> 29) | (ctx->nh << 3));
281   STORE32(ctx->buf + SHA_BUFSZ - 4, ctx->nl << 3);
282   sha_compress(ctx, ctx->buf);
283   STORE32(p +  0, ctx->a);
284   STORE32(p +  4, ctx->b);
285   STORE32(p +  8, ctx->c);
286   STORE32(p + 12, ctx->d);
287   STORE32(p + 16, ctx->e);
288 }
289
290 /* --- @sha_state@ --- *
291  *
292  * Arguments:   @sha_ctx *ctx@ = pointer to context
293  *              @void *state@ = pointer to buffer for current state
294  *
295  * Returns:     Number of bytes written to the hash function so far.
296  *
297  * Use:         Returns the current state of the hash function such that
298  *              it can be passed to @sha_set@.
299  */
300
301 unsigned long sha_state(sha_ctx *ctx, void *state)
302 {
303   octet *p = state;
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   return (ctx->nl | ((ctx->nh << 16) << 16));
310 }
311
312 /* --- Generic interface --- */
313
314 GHASH_DEF(SHA, sha)
315
316 /* --- Test code --- */
317
318 HASH_TEST(SHA, sha)
319
320 /*----- That's all, folks -------------------------------------------------*/