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