3 * $Id: sha.c,v 1.4 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
32 #include <mLib/bits.h>
35 #include "ghash-def.h"
39 /*----- Main code ---------------------------------------------------------*/
41 /* --- @sha_compress@ --- *
43 * Arguments: @sha_ctx *ctx@ = pointer to context block
44 * @const void *sbuf@ = pointer to buffer of appropriate size
48 * Use: SHA-1 compression function.
51 void sha_compress(sha_ctx
*ctx
, const void *sbuf
)
56 /* --- Fetch the chaining variables --- */
64 /* --- Fetch and expand the buffer contents --- */
70 for (i
= 0, p
= sbuf
; i
< 16; i
++, p
+= 4)
72 for (i
= 16; i
< 80; i
++) {
73 uint32 x
= buf
[i
- 3] ^ buf
[i
- 8] ^ buf
[i
- 14] ^ buf
[i
- 16];
78 /* --- Definitions for round functions --- */
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)))
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; \
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)
94 /* --- The main compression function --- */
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);
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);
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);
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);
180 /* --- Update the chaining variables --- */
189 /* --- @sha_init@ --- *
191 * Arguments: @sha_ctx *ctx@ = pointer to context block to initialize
195 * Use: Initializes a context block ready for hashing.
198 void sha_init(sha_ctx
*ctx
)
206 ctx
->nl
= ctx
->nh
= 0;
209 /* --- @sha_set@ --- *
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
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.
222 void sha_set(sha_ctx
*ctx
, const void *buf
, unsigned long count
)
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);
231 ctx
->nl
= U32(count
);
232 ctx
->nh
= U32(((count
& ~MASK32
) >> 16) >> 16);
235 /* --- @sha_hash@ --- *
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
243 * Use: Hashes a buffer of data. The buffer may be of any size and
247 void sha_hash(sha_ctx
*ctx
, const void *buf
, size_t sz
)
249 HASH_BUFFER(SHA
, sha
, ctx
, buf
, sz
);
252 /* --- @sha_done@ --- *
254 * Arguments: @sha_ctx *ctx@ = pointer to context block
255 * @void *hash@ = pointer to output buffer
259 * Use: Returns the hash of the data read so far.
262 void sha_done(sha_ctx
*ctx
, void *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
);
276 /* --- @sha_state@ --- *
278 * Arguments: @sha_ctx *ctx@ = pointer to context
279 * @void *state@ = pointer to buffer for current state
281 * Returns: Number of bytes written to the hash function so far.
283 * Use: Returns the current state of the hash function such that
284 * it can be passed to @sha_set@.
287 unsigned long sha_state(sha_ctx
*ctx
, void *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));
298 /* --- Generic interface --- */
302 /* --- Test code --- */
306 /*----- That's all, folks -------------------------------------------------*/