3 * Implementation of the SHA-1 hash function
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
30 #include <mLib/bits.h>
33 #include "ghash-def.h"
37 /*----- Main code ---------------------------------------------------------*/
39 /* --- @sha_compress@ --- *
41 * Arguments: @sha_ctx *ctx@ = pointer to context block
42 * @const void *sbuf@ = pointer to buffer of appropriate size
46 * Use: SHA-1 compression function.
49 void sha_compress(sha_ctx
*ctx
, const void *sbuf
)
54 /* --- Fetch the chaining variables --- */
62 /* --- Fetch and expand the buffer contents --- */
68 for (i
= 0, p
= sbuf
; i
< 16; i
++, p
+= 4)
70 for (i
= 16; i
< 80; i
++) {
71 uint32 x
= buf
[i
- 3] ^ buf
[i
- 8] ^ buf
[i
- 14] ^ buf
[i
- 16];
76 /* --- Definitions for round functions --- */
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)))
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; \
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)
92 /* --- The main compression function --- */
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);
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);
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);
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);
178 /* --- Update the chaining variables --- */
187 /* --- @sha_init@ --- *
189 * Arguments: @sha_ctx *ctx@ = pointer to context block to initialize
193 * Use: Initializes a context block ready for hashing.
196 void sha_init(sha_ctx
*ctx
)
204 ctx
->nl
= ctx
->nh
= 0;
207 /* --- @sha_set@ --- *
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
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.
220 void sha_set(sha_ctx
*ctx
, const void *buf
, unsigned long count
)
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);
229 ctx
->nl
= U32(count
);
230 ctx
->nh
= U32(((count
& ~MASK32
) >> 16) >> 16);
233 /* --- @sha_hash@ --- *
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
241 * Use: Hashes a buffer of data. The buffer may be of any size and
245 void sha_hash(sha_ctx
*ctx
, const void *buf
, size_t sz
)
247 HASH_BUFFER(SHA
, sha
, ctx
, buf
, sz
);
250 /* --- @sha_done@ --- *
252 * Arguments: @sha_ctx *ctx@ = pointer to context block
253 * @void *hash@ = pointer to output buffer
257 * Use: Returns the hash of the data read so far.
260 void sha_done(sha_ctx
*ctx
, void *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
);
274 /* --- @sha_state@ --- *
276 * Arguments: @sha_ctx *ctx@ = pointer to context
277 * @void *state@ = pointer to buffer for current state
279 * Returns: Number of bytes written to the hash function so far.
281 * Use: Returns the current state of the hash function such that
282 * it can be passed to @sha_set@.
285 unsigned long sha_state(sha_ctx
*ctx
, void *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));
296 /* --- Generic interface --- */
300 /* --- Test code --- */
304 /*----- That's all, folks -------------------------------------------------*/