3 * $Id: sha.c,v 1.3 2000/06/17 11:31:43 mdw Exp $
5 * Implementation of the SHA-1 hash function
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.3 2000/06/17 11:31:43 mdw
34 * Portability fix for broken compilers.
36 * Revision 1.2 1999/12/10 23:20:03 mdw
37 * New hash interface requirements.
39 * Revision 1.1 1999/09/03 08:41:12 mdw
44 /*----- Header files ------------------------------------------------------*/
46 #include <mLib/bits.h>
49 #include "ghash-def.h"
53 /*----- Main code ---------------------------------------------------------*/
55 /* --- @sha_compress@ --- *
57 * Arguments: @sha_ctx *ctx@ = pointer to context block
58 * @const void *sbuf@ = pointer to buffer of appropriate size
62 * Use: SHA-1 compression function.
65 void sha_compress(sha_ctx *ctx, const void *sbuf)
70 /* --- Fetch the chaining variables --- */
78 /* --- Fetch and expand the buffer contents --- */
84 for (i = 0, p = sbuf; i < 16; i++, p += 4)
86 for (i = 16; i < 80; i++) {
87 uint32 x = buf[i - 3] ^ buf[i - 8] ^ buf[i - 14] ^ buf[i - 16];
92 /* --- Definitions for round functions --- */
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)))
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; \
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)
108 /* --- The main compression function --- */
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);
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);
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);
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);
194 /* --- Update the chaining variables --- */
203 /* --- @sha_init@ --- *
205 * Arguments: @sha_ctx *ctx@ = pointer to context block to initialize
209 * Use: Initializes a context block ready for hashing.
212 void sha_init(sha_ctx *ctx)
220 ctx->nl = ctx->nh = 0;
223 /* --- @sha_set@ --- *
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
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.
236 void sha_set(sha_ctx *ctx, const void *buf, unsigned long count)
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);
245 ctx->nl = U32(count);
246 ctx->nh = U32(((count & ~MASK32) >> 16) >> 16);
249 /* --- @sha_hash@ --- *
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
257 * Use: Hashes a buffer of data. The buffer may be of any size and
261 void sha_hash(sha_ctx *ctx, const void *buf, size_t sz)
263 HASH_BUFFER(SHA, sha, ctx, buf, sz);
266 /* --- @sha_done@ --- *
268 * Arguments: @sha_ctx *ctx@ = pointer to context block
269 * @void *hash@ = pointer to output buffer
273 * Use: Returns the hash of the data read so far.
276 void sha_done(sha_ctx *ctx, void *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);
290 /* --- @sha_state@ --- *
292 * Arguments: @sha_ctx *ctx@ = pointer to context
293 * @void *state@ = pointer to buffer for current state
295 * Returns: Number of bytes written to the hash function so far.
297 * Use: Returns the current state of the hash function such that
298 * it can be passed to @sha_set@.
301 unsigned long sha_state(sha_ctx *ctx, void *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));
312 /* --- Generic interface --- */
316 /* --- Test code --- */
320 /*----- That's all, folks -------------------------------------------------*/