3 * $Id: sha.c,v 1.2 1999/12/10 23:20:03 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.2 1999/12/10 23:20:03 mdw
34 * New hash interface requirements.
36 * Revision 1.1 1999/09/03 08:41:12 mdw
41 /*----- Header files ------------------------------------------------------*/
43 #include <mLib/bits.h>
46 #include "ghash-def.h"
50 /*----- Main code ---------------------------------------------------------*/
52 /* --- @sha_compress@ --- *
54 * Arguments: @sha_ctx *ctx@ = pointer to context block
55 * @const void *sbuf@ = pointer to buffer of appropriate size
59 * Use: SHA-1 compression function.
62 void sha_compress(sha_ctx
*ctx
, const void *sbuf
)
67 /* --- Fetch the chaining variables --- */
75 /* --- Fetch and expand the buffer contents --- */
81 for (i
= 0, p
= sbuf
; i
< 16; i
++, p
+= 4)
83 for (i
= 16; i
< 80; i
++) {
84 uint32 x
= buf
[i
- 3] ^ buf
[i
- 8] ^ buf
[i
- 14] ^ buf
[i
- 16];
89 /* --- Definitions for round functions --- */
91 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
92 #define G(x, y, z) ((x) ^ (y) ^ (z))
93 #define H(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
95 #define T(v, w, x, y, z, i, f, k) do { \
96 z = ROL32(v, 5) + f(w, x, y) + z + buf[i] + k; \
100 #define FF(v, w, x, y, z, i) T(v, w, x, y, z, i, F, 0x5a827999)
101 #define GG(v, w, x, y, z, i) T(v, w, x, y, z, i, G, 0x6ed9eba1)
102 #define HH(v, w, x, y, z, i) T(v, w, x, y, z, i, H, 0x8f1bbcdc)
103 #define II(v, w, x, y, z, i) T(v, w, x, y, z, i, G, 0xca62c1d6)
105 /* --- The main compression function --- */
107 FF(a
, b
, c
, d
, e
, 0);
108 FF(e
, a
, b
, c
, d
, 1);
109 FF(d
, e
, a
, b
, c
, 2);
110 FF(c
, d
, e
, a
, b
, 3);
111 FF(b
, c
, d
, e
, a
, 4);
112 FF(a
, b
, c
, d
, e
, 5);
113 FF(e
, a
, b
, c
, d
, 6);
114 FF(d
, e
, a
, b
, c
, 7);
115 FF(c
, d
, e
, a
, b
, 8);
116 FF(b
, c
, d
, e
, a
, 9);
117 FF(a
, b
, c
, d
, e
, 10);
118 FF(e
, a
, b
, c
, d
, 11);
119 FF(d
, e
, a
, b
, c
, 12);
120 FF(c
, d
, e
, a
, b
, 13);
121 FF(b
, c
, d
, e
, a
, 14);
122 FF(a
, b
, c
, d
, e
, 15);
123 FF(e
, a
, b
, c
, d
, 16);
124 FF(d
, e
, a
, b
, c
, 17);
125 FF(c
, d
, e
, a
, b
, 18);
126 FF(b
, c
, d
, e
, a
, 19);
128 GG(a
, b
, c
, d
, e
, 20);
129 GG(e
, a
, b
, c
, d
, 21);
130 GG(d
, e
, a
, b
, c
, 22);
131 GG(c
, d
, e
, a
, b
, 23);
132 GG(b
, c
, d
, e
, a
, 24);
133 GG(a
, b
, c
, d
, e
, 25);
134 GG(e
, a
, b
, c
, d
, 26);
135 GG(d
, e
, a
, b
, c
, 27);
136 GG(c
, d
, e
, a
, b
, 28);
137 GG(b
, c
, d
, e
, a
, 29);
138 GG(a
, b
, c
, d
, e
, 30);
139 GG(e
, a
, b
, c
, d
, 31);
140 GG(d
, e
, a
, b
, c
, 32);
141 GG(c
, d
, e
, a
, b
, 33);
142 GG(b
, c
, d
, e
, a
, 34);
143 GG(a
, b
, c
, d
, e
, 35);
144 GG(e
, a
, b
, c
, d
, 36);
145 GG(d
, e
, a
, b
, c
, 37);
146 GG(c
, d
, e
, a
, b
, 38);
147 GG(b
, c
, d
, e
, a
, 39);
149 HH(a
, b
, c
, d
, e
, 40);
150 HH(e
, a
, b
, c
, d
, 41);
151 HH(d
, e
, a
, b
, c
, 42);
152 HH(c
, d
, e
, a
, b
, 43);
153 HH(b
, c
, d
, e
, a
, 44);
154 HH(a
, b
, c
, d
, e
, 45);
155 HH(e
, a
, b
, c
, d
, 46);
156 HH(d
, e
, a
, b
, c
, 47);
157 HH(c
, d
, e
, a
, b
, 48);
158 HH(b
, c
, d
, e
, a
, 49);
159 HH(a
, b
, c
, d
, e
, 50);
160 HH(e
, a
, b
, c
, d
, 51);
161 HH(d
, e
, a
, b
, c
, 52);
162 HH(c
, d
, e
, a
, b
, 53);
163 HH(b
, c
, d
, e
, a
, 54);
164 HH(a
, b
, c
, d
, e
, 55);
165 HH(e
, a
, b
, c
, d
, 56);
166 HH(d
, e
, a
, b
, c
, 57);
167 HH(c
, d
, e
, a
, b
, 58);
168 HH(b
, c
, d
, e
, a
, 59);
170 II(a
, b
, c
, d
, e
, 60);
171 II(e
, a
, b
, c
, d
, 61);
172 II(d
, e
, a
, b
, c
, 62);
173 II(c
, d
, e
, a
, b
, 63);
174 II(b
, c
, d
, e
, a
, 64);
175 II(a
, b
, c
, d
, e
, 65);
176 II(e
, a
, b
, c
, d
, 66);
177 II(d
, e
, a
, b
, c
, 67);
178 II(c
, d
, e
, a
, b
, 68);
179 II(b
, c
, d
, e
, a
, 69);
180 II(a
, b
, c
, d
, e
, 70);
181 II(e
, a
, b
, c
, d
, 71);
182 II(d
, e
, a
, b
, c
, 72);
183 II(c
, d
, e
, a
, b
, 73);
184 II(b
, c
, d
, e
, a
, 74);
185 II(a
, b
, c
, d
, e
, 75);
186 II(e
, a
, b
, c
, d
, 76);
187 II(d
, e
, a
, b
, c
, 77);
188 II(c
, d
, e
, a
, b
, 78);
189 II(b
, c
, d
, e
, a
, 79);
191 /* --- Update the chaining variables --- */
200 /* --- @sha_init@ --- *
202 * Arguments: @sha_ctx *ctx@ = pointer to context block to initialize
206 * Use: Initializes a context block ready for hashing.
209 void sha_init(sha_ctx
*ctx
)
217 ctx
->nl
= ctx
->nh
= 0;
220 /* --- @sha_set@ --- *
222 * Arguments: @sha_ctx *ctx@ = pointer to context block
223 * @const void *buf@ = pointer to state buffer
224 * @unsigned long count@ = current count of bytes processed
228 * Use: Initializes a context block from a given state. This is
229 * useful in cases where the initial hash state is meant to be
230 * secret, e.g., for NMAC and HMAC support.
233 void sha_set(sha_ctx
*ctx
, const void *buf
, unsigned long count
)
235 const octet
*p
= buf
;
236 ctx
->a
= LOAD32(p
+ 0);
237 ctx
->b
= LOAD32(p
+ 4);
238 ctx
->c
= LOAD32(p
+ 8);
239 ctx
->d
= LOAD32(p
+ 12);
240 ctx
->e
= LOAD32(p
+ 16);
242 ctx
->nl
= U32(count
);
243 ctx
->nh
= U32((count
>> 16) >> 16);
246 /* --- @sha_hash@ --- *
248 * Arguments: @sha_ctx *ctx@ = pointer to context block
249 * @const void *buf@ = buffer of data to hash
250 * @size_t sz@ = size of buffer to hash
254 * Use: Hashes a buffer of data. The buffer may be of any size and
258 void sha_hash(sha_ctx
*ctx
, const void *buf
, size_t sz
)
260 HASH_BUFFER(SHA
, sha
, ctx
, buf
, sz
);
263 /* --- @sha_done@ --- *
265 * Arguments: @sha_ctx *ctx@ = pointer to context block
266 * @void *hash@ = pointer to output buffer
270 * Use: Returns the hash of the data read so far.
273 void sha_done(sha_ctx
*ctx
, void *hash
)
276 HASH_PAD(SHA
, sha
, ctx
, 0x80, 0, 8);
277 STORE32(ctx
->buf
+ SHA_BUFSZ
- 8, (ctx
->nl
>> 29) | (ctx
->nh
<< 3));
278 STORE32(ctx
->buf
+ SHA_BUFSZ
- 4, ctx
->nl
<< 3);
279 sha_compress(ctx
, ctx
->buf
);
280 STORE32(p
+ 0, ctx
->a
);
281 STORE32(p
+ 4, ctx
->b
);
282 STORE32(p
+ 8, ctx
->c
);
283 STORE32(p
+ 12, ctx
->d
);
284 STORE32(p
+ 16, ctx
->e
);
287 /* --- @sha_state@ --- *
289 * Arguments: @sha_ctx *ctx@ = pointer to context
290 * @void *state@ = pointer to buffer for current state
292 * Returns: Number of bytes written to the hash function so far.
294 * Use: Returns the current state of the hash function such that
295 * it can be passed to @sha_set@.
298 unsigned long sha_state(sha_ctx
*ctx
, void *state
)
301 STORE32(p
+ 0, ctx
->a
);
302 STORE32(p
+ 4, ctx
->b
);
303 STORE32(p
+ 8, ctx
->c
);
304 STORE32(p
+ 12, ctx
->d
);
305 STORE32(p
+ 16, ctx
->e
);
306 return (ctx
->nl
| ((ctx
->nh
<< 16) << 16));
309 /* --- Generic interface --- */
313 /* --- Test code --- */
317 /*----- That's all, folks -------------------------------------------------*/