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