2 * SHA1 hash algorithm. Used in SSH-2 as a MAC, and the transform is
\r
3 * also used as a `stirring' function for the PuTTY random number
\r
4 * pool. Implemented directly from the specification by Simon
\r
10 /* ----------------------------------------------------------------------
\r
11 * Core SHA algorithm: processes 16-word blocks into a message digest.
\r
14 #define rol(x,y) ( ((x) << (y)) | (((uint32)x) >> (32-y)) )
\r
16 static void SHA_Core_Init(uint32 h[5])
\r
25 void SHATransform(word32 * digest, word32 * block)
\r
28 word32 a, b, c, d, e;
\r
31 for (t = 0; t < 16; t++)
\r
34 for (t = 16; t < 80; t++) {
\r
35 word32 tmp = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16];
\r
45 for (t = 0; t < 20; t++) {
\r
47 rol(a, 5) + ((b & c) | (d & ~b)) + e + w[t] + 0x5a827999;
\r
54 for (t = 20; t < 40; t++) {
\r
55 word32 tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0x6ed9eba1;
\r
62 for (t = 40; t < 60; t++) {
\r
64 5) + ((b & c) | (b & d) | (c & d)) + e + w[t] +
\r
72 for (t = 60; t < 80; t++) {
\r
73 word32 tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0xca62c1d6;
\r
88 /* ----------------------------------------------------------------------
\r
89 * Outer SHA algorithm: take an arbitrary length byte string,
\r
90 * convert it into 16-word blocks with the prescribed padding at
\r
91 * the end, and pass those blocks to the core SHA algorithm.
\r
94 void SHA_Init(SHA_State * s)
\r
96 SHA_Core_Init(s->h);
\r
98 s->lenhi = s->lenlo = 0;
\r
101 void SHA_Bytes(SHA_State * s, void *p, int len)
\r
103 unsigned char *q = (unsigned char *) p;
\r
104 uint32 wordblock[16];
\r
109 * Update the length field.
\r
112 s->lenhi += (s->lenlo < lenw);
\r
114 if (s->blkused && s->blkused + len < 64) {
\r
116 * Trivial case: just add to the block.
\r
118 memcpy(s->block + s->blkused, q, len);
\r
122 * We must complete and process at least one block.
\r
124 while (s->blkused + len >= 64) {
\r
125 memcpy(s->block + s->blkused, q, 64 - s->blkused);
\r
126 q += 64 - s->blkused;
\r
127 len -= 64 - s->blkused;
\r
128 /* Now process the block. Gather bytes big-endian into words */
\r
129 for (i = 0; i < 16; i++) {
\r
131 (((uint32) s->block[i * 4 + 0]) << 24) |
\r
132 (((uint32) s->block[i * 4 + 1]) << 16) |
\r
133 (((uint32) s->block[i * 4 + 2]) << 8) |
\r
134 (((uint32) s->block[i * 4 + 3]) << 0);
\r
136 SHATransform(s->h, wordblock);
\r
139 memcpy(s->block, q, len);
\r
144 void SHA_Final(SHA_State * s, unsigned char *output)
\r
148 unsigned char c[64];
\r
149 uint32 lenhi, lenlo;
\r
151 if (s->blkused >= 56)
\r
152 pad = 56 + 64 - s->blkused;
\r
154 pad = 56 - s->blkused;
\r
156 lenhi = (s->lenhi << 3) | (s->lenlo >> (32 - 3));
\r
157 lenlo = (s->lenlo << 3);
\r
161 SHA_Bytes(s, &c, pad);
\r
163 c[0] = (lenhi >> 24) & 0xFF;
\r
164 c[1] = (lenhi >> 16) & 0xFF;
\r
165 c[2] = (lenhi >> 8) & 0xFF;
\r
166 c[3] = (lenhi >> 0) & 0xFF;
\r
167 c[4] = (lenlo >> 24) & 0xFF;
\r
168 c[5] = (lenlo >> 16) & 0xFF;
\r
169 c[6] = (lenlo >> 8) & 0xFF;
\r
170 c[7] = (lenlo >> 0) & 0xFF;
\r
172 SHA_Bytes(s, &c, 8);
\r
174 for (i = 0; i < 5; i++) {
\r
175 output[i * 4] = (s->h[i] >> 24) & 0xFF;
\r
176 output[i * 4 + 1] = (s->h[i] >> 16) & 0xFF;
\r
177 output[i * 4 + 2] = (s->h[i] >> 8) & 0xFF;
\r
178 output[i * 4 + 3] = (s->h[i]) & 0xFF;
\r
182 void SHA_Simple(void *p, int len, unsigned char *output)
\r
187 SHA_Bytes(&s, p, len);
\r
188 SHA_Final(&s, output);
\r
192 * Thin abstraction for things where hashes are pluggable.
\r
195 static void *sha1_init(void)
\r
199 s = snew(SHA_State);
\r
204 static void sha1_bytes(void *handle, void *p, int len)
\r
206 SHA_State *s = handle;
\r
208 SHA_Bytes(s, p, len);
\r
211 static void sha1_final(void *handle, unsigned char *output)
\r
213 SHA_State *s = handle;
\r
215 SHA_Final(s, output);
\r
219 const struct ssh_hash ssh_sha1 = {
\r
220 sha1_init, sha1_bytes, sha1_final, 20, "SHA-1"
\r
223 /* ----------------------------------------------------------------------
\r
224 * The above is the SHA-1 algorithm itself. Now we implement the
\r
225 * HMAC wrapper on it.
\r
228 static void *sha1_make_context(void)
\r
230 return snewn(3, SHA_State);
\r
233 static void sha1_free_context(void *handle)
\r
238 static void sha1_key_internal(void *handle, unsigned char *key, int len)
\r
240 SHA_State *keys = (SHA_State *)handle;
\r
241 unsigned char foo[64];
\r
244 memset(foo, 0x36, 64);
\r
245 for (i = 0; i < len && i < 64; i++)
\r
247 SHA_Init(&keys[0]);
\r
248 SHA_Bytes(&keys[0], foo, 64);
\r
250 memset(foo, 0x5C, 64);
\r
251 for (i = 0; i < len && i < 64; i++)
\r
253 SHA_Init(&keys[1]);
\r
254 SHA_Bytes(&keys[1], foo, 64);
\r
256 memset(foo, 0, 64); /* burn the evidence */
\r
259 static void sha1_key(void *handle, unsigned char *key)
\r
261 sha1_key_internal(handle, key, 20);
\r
264 static void sha1_key_buggy(void *handle, unsigned char *key)
\r
266 sha1_key_internal(handle, key, 16);
\r
269 static void hmacsha1_start(void *handle)
\r
271 SHA_State *keys = (SHA_State *)handle;
\r
273 keys[2] = keys[0]; /* structure copy */
\r
276 static void hmacsha1_bytes(void *handle, unsigned char const *blk, int len)
\r
278 SHA_State *keys = (SHA_State *)handle;
\r
279 SHA_Bytes(&keys[2], (void *)blk, len);
\r
282 static void hmacsha1_genresult(void *handle, unsigned char *hmac)
\r
284 SHA_State *keys = (SHA_State *)handle;
\r
286 unsigned char intermediate[20];
\r
288 s = keys[2]; /* structure copy */
\r
289 SHA_Final(&s, intermediate);
\r
290 s = keys[1]; /* structure copy */
\r
291 SHA_Bytes(&s, intermediate, 20);
\r
292 SHA_Final(&s, hmac);
\r
295 static void sha1_do_hmac(void *handle, unsigned char *blk, int len,
\r
296 unsigned long seq, unsigned char *hmac)
\r
298 unsigned char seqbuf[4];
\r
300 seqbuf[0] = (unsigned char) ((seq >> 24) & 0xFF);
\r
301 seqbuf[1] = (unsigned char) ((seq >> 16) & 0xFF);
\r
302 seqbuf[2] = (unsigned char) ((seq >> 8) & 0xFF);
\r
303 seqbuf[3] = (unsigned char) ((seq) & 0xFF);
\r
305 hmacsha1_start(handle);
\r
306 hmacsha1_bytes(handle, seqbuf, 4);
\r
307 hmacsha1_bytes(handle, blk, len);
\r
308 hmacsha1_genresult(handle, hmac);
\r
311 static void sha1_generate(void *handle, unsigned char *blk, int len,
\r
314 sha1_do_hmac(handle, blk, len, seq, blk + len);
\r
317 static int hmacsha1_verresult(void *handle, unsigned char const *hmac)
\r
319 unsigned char correct[20];
\r
320 hmacsha1_genresult(handle, correct);
\r
321 return !memcmp(correct, hmac, 20);
\r
324 static int sha1_verify(void *handle, unsigned char *blk, int len,
\r
327 unsigned char correct[20];
\r
328 sha1_do_hmac(handle, blk, len, seq, correct);
\r
329 return !memcmp(correct, blk + len, 20);
\r
332 static void hmacsha1_96_genresult(void *handle, unsigned char *hmac)
\r
334 unsigned char full[20];
\r
335 hmacsha1_genresult(handle, full);
\r
336 memcpy(hmac, full, 12);
\r
339 static void sha1_96_generate(void *handle, unsigned char *blk, int len,
\r
342 unsigned char full[20];
\r
343 sha1_do_hmac(handle, blk, len, seq, full);
\r
344 memcpy(blk + len, full, 12);
\r
347 static int hmacsha1_96_verresult(void *handle, unsigned char const *hmac)
\r
349 unsigned char correct[20];
\r
350 hmacsha1_genresult(handle, correct);
\r
351 return !memcmp(correct, hmac, 12);
\r
354 static int sha1_96_verify(void *handle, unsigned char *blk, int len,
\r
357 unsigned char correct[20];
\r
358 sha1_do_hmac(handle, blk, len, seq, correct);
\r
359 return !memcmp(correct, blk + len, 12);
\r
362 void hmac_sha1_simple(void *key, int keylen, void *data, int datalen,
\r
363 unsigned char *output) {
\r
364 SHA_State states[2];
\r
365 unsigned char intermediate[20];
\r
367 sha1_key_internal(states, key, keylen);
\r
368 SHA_Bytes(&states[0], data, datalen);
\r
369 SHA_Final(&states[0], intermediate);
\r
371 SHA_Bytes(&states[1], intermediate, 20);
\r
372 SHA_Final(&states[1], output);
\r
375 const struct ssh_mac ssh_hmac_sha1 = {
\r
376 sha1_make_context, sha1_free_context, sha1_key,
\r
377 sha1_generate, sha1_verify,
\r
378 hmacsha1_start, hmacsha1_bytes, hmacsha1_genresult, hmacsha1_verresult,
\r
384 const struct ssh_mac ssh_hmac_sha1_96 = {
\r
385 sha1_make_context, sha1_free_context, sha1_key,
\r
386 sha1_96_generate, sha1_96_verify,
\r
387 hmacsha1_start, hmacsha1_bytes,
\r
388 hmacsha1_96_genresult, hmacsha1_96_verresult,
\r
394 const struct ssh_mac ssh_hmac_sha1_buggy = {
\r
395 sha1_make_context, sha1_free_context, sha1_key_buggy,
\r
396 sha1_generate, sha1_verify,
\r
397 hmacsha1_start, hmacsha1_bytes, hmacsha1_genresult, hmacsha1_verresult,
\r
400 "bug-compatible HMAC-SHA1"
\r
403 const struct ssh_mac ssh_hmac_sha1_96_buggy = {
\r
404 sha1_make_context, sha1_free_context, sha1_key_buggy,
\r
405 sha1_96_generate, sha1_96_verify,
\r
406 hmacsha1_start, hmacsha1_bytes,
\r
407 hmacsha1_96_genresult, hmacsha1_96_verresult,
\r
410 "bug-compatible HMAC-SHA1-96"
\r