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