2 * SHA1 hash algorithm. Used in SSH-2 as a MAC, and the transform is
3 * also used as a `stirring' function for the PuTTY random number
4 * pool. Implemented directly from the specification by Simon
10 /* ----------------------------------------------------------------------
11 * Core SHA algorithm: processes 16-word blocks into a message digest.
14 #define rol(x,y) ( ((x) << (y)) | (((uint32)x) >> (32-y)) )
16 static void SHA_Core_Init(uint32 h[5])
25 void SHATransform(word32 * digest, word32 * block)
31 #ifdef RANDOM_DIAGNOSTICS
33 extern int random_diagnostics;
34 if (random_diagnostics) {
36 printf("SHATransform:");
37 for (i = 0; i < 5; i++)
38 printf(" %08x", digest[i]);
40 for (i = 0; i < 16; i++)
41 printf(" %08x", block[i]);
46 for (t = 0; t < 16; t++)
49 for (t = 16; t < 80; t++) {
50 word32 tmp = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16];
60 for (t = 0; t < 20; t++) {
62 rol(a, 5) + ((b & c) | (d & ~b)) + e + w[t] + 0x5a827999;
69 for (t = 20; t < 40; t++) {
70 word32 tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0x6ed9eba1;
77 for (t = 40; t < 60; t++) {
79 5) + ((b & c) | (b & d) | (c & d)) + e + w[t] +
87 for (t = 60; t < 80; t++) {
88 word32 tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0xca62c1d6;
102 #ifdef RANDOM_DIAGNOSTICS
104 extern int random_diagnostics;
105 if (random_diagnostics) {
108 for (i = 0; i < 5; i++)
109 printf(" %08x", digest[i]);
116 /* ----------------------------------------------------------------------
117 * Outer SHA algorithm: take an arbitrary length byte string,
118 * convert it into 16-word blocks with the prescribed padding at
119 * the end, and pass those blocks to the core SHA algorithm.
122 void SHA_Init(SHA_State * s)
126 s->lenhi = s->lenlo = 0;
129 void SHA_Bytes(SHA_State * s, const void *p, int len)
131 const unsigned char *q = (const unsigned char *) p;
132 uint32 wordblock[16];
137 * Update the length field.
140 s->lenhi += (s->lenlo < lenw);
142 if (s->blkused && s->blkused + len < 64) {
144 * Trivial case: just add to the block.
146 memcpy(s->block + s->blkused, q, len);
150 * We must complete and process at least one block.
152 while (s->blkused + len >= 64) {
153 memcpy(s->block + s->blkused, q, 64 - s->blkused);
154 q += 64 - s->blkused;
155 len -= 64 - s->blkused;
156 /* Now process the block. Gather bytes big-endian into words */
157 for (i = 0; i < 16; i++) {
159 (((uint32) s->block[i * 4 + 0]) << 24) |
160 (((uint32) s->block[i * 4 + 1]) << 16) |
161 (((uint32) s->block[i * 4 + 2]) << 8) |
162 (((uint32) s->block[i * 4 + 3]) << 0);
164 SHATransform(s->h, wordblock);
167 memcpy(s->block, q, len);
172 void SHA_Final(SHA_State * s, unsigned char *output)
179 if (s->blkused >= 56)
180 pad = 56 + 64 - s->blkused;
182 pad = 56 - s->blkused;
184 lenhi = (s->lenhi << 3) | (s->lenlo >> (32 - 3));
185 lenlo = (s->lenlo << 3);
189 SHA_Bytes(s, &c, pad);
191 c[0] = (lenhi >> 24) & 0xFF;
192 c[1] = (lenhi >> 16) & 0xFF;
193 c[2] = (lenhi >> 8) & 0xFF;
194 c[3] = (lenhi >> 0) & 0xFF;
195 c[4] = (lenlo >> 24) & 0xFF;
196 c[5] = (lenlo >> 16) & 0xFF;
197 c[6] = (lenlo >> 8) & 0xFF;
198 c[7] = (lenlo >> 0) & 0xFF;
202 for (i = 0; i < 5; i++) {
203 output[i * 4] = (s->h[i] >> 24) & 0xFF;
204 output[i * 4 + 1] = (s->h[i] >> 16) & 0xFF;
205 output[i * 4 + 2] = (s->h[i] >> 8) & 0xFF;
206 output[i * 4 + 3] = (s->h[i]) & 0xFF;
210 void SHA_Simple(const void *p, int len, unsigned char *output)
215 SHA_Bytes(&s, p, len);
216 SHA_Final(&s, output);
217 smemclr(&s, sizeof(s));
221 * Thin abstraction for things where hashes are pluggable.
224 static void *sha1_init(void)
233 static void sha1_bytes(void *handle, void *p, int len)
235 SHA_State *s = handle;
237 SHA_Bytes(s, p, len);
240 static void sha1_final(void *handle, unsigned char *output)
242 SHA_State *s = handle;
244 SHA_Final(s, output);
245 smemclr(s, sizeof(*s));
249 const struct ssh_hash ssh_sha1 = {
250 sha1_init, sha1_bytes, sha1_final, 20, "SHA-1"
253 /* ----------------------------------------------------------------------
254 * The above is the SHA-1 algorithm itself. Now we implement the
255 * HMAC wrapper on it.
258 static void *sha1_make_context(void)
260 return snewn(3, SHA_State);
263 static void sha1_free_context(void *handle)
265 smemclr(handle, 3 * sizeof(SHA_State));
269 static void sha1_key_internal(void *handle, unsigned char *key, int len)
271 SHA_State *keys = (SHA_State *)handle;
272 unsigned char foo[64];
275 memset(foo, 0x36, 64);
276 for (i = 0; i < len && i < 64; i++)
279 SHA_Bytes(&keys[0], foo, 64);
281 memset(foo, 0x5C, 64);
282 for (i = 0; i < len && i < 64; i++)
285 SHA_Bytes(&keys[1], foo, 64);
287 smemclr(foo, 64); /* burn the evidence */
290 static void sha1_key(void *handle, unsigned char *key)
292 sha1_key_internal(handle, key, 20);
295 static void sha1_key_buggy(void *handle, unsigned char *key)
297 sha1_key_internal(handle, key, 16);
300 static void hmacsha1_start(void *handle)
302 SHA_State *keys = (SHA_State *)handle;
304 keys[2] = keys[0]; /* structure copy */
307 static void hmacsha1_bytes(void *handle, unsigned char const *blk, int len)
309 SHA_State *keys = (SHA_State *)handle;
310 SHA_Bytes(&keys[2], (void *)blk, len);
313 static void hmacsha1_genresult(void *handle, unsigned char *hmac)
315 SHA_State *keys = (SHA_State *)handle;
317 unsigned char intermediate[20];
319 s = keys[2]; /* structure copy */
320 SHA_Final(&s, intermediate);
321 s = keys[1]; /* structure copy */
322 SHA_Bytes(&s, intermediate, 20);
326 static void sha1_do_hmac(void *handle, unsigned char *blk, int len,
327 unsigned long seq, unsigned char *hmac)
329 unsigned char seqbuf[4];
331 PUT_32BIT_MSB_FIRST(seqbuf, seq);
332 hmacsha1_start(handle);
333 hmacsha1_bytes(handle, seqbuf, 4);
334 hmacsha1_bytes(handle, blk, len);
335 hmacsha1_genresult(handle, hmac);
338 static void sha1_generate(void *handle, unsigned char *blk, int len,
341 sha1_do_hmac(handle, blk, len, seq, blk + len);
344 static int hmacsha1_verresult(void *handle, unsigned char const *hmac)
346 unsigned char correct[20];
347 hmacsha1_genresult(handle, correct);
348 return smemeq(correct, hmac, 20);
351 static int sha1_verify(void *handle, unsigned char *blk, int len,
354 unsigned char correct[20];
355 sha1_do_hmac(handle, blk, len, seq, correct);
356 return smemeq(correct, blk + len, 20);
359 static void hmacsha1_96_genresult(void *handle, unsigned char *hmac)
361 unsigned char full[20];
362 hmacsha1_genresult(handle, full);
363 memcpy(hmac, full, 12);
366 static void sha1_96_generate(void *handle, unsigned char *blk, int len,
369 unsigned char full[20];
370 sha1_do_hmac(handle, blk, len, seq, full);
371 memcpy(blk + len, full, 12);
374 static int hmacsha1_96_verresult(void *handle, unsigned char const *hmac)
376 unsigned char correct[20];
377 hmacsha1_genresult(handle, correct);
378 return smemeq(correct, hmac, 12);
381 static int sha1_96_verify(void *handle, unsigned char *blk, int len,
384 unsigned char correct[20];
385 sha1_do_hmac(handle, blk, len, seq, correct);
386 return smemeq(correct, blk + len, 12);
389 void hmac_sha1_simple(void *key, int keylen, void *data, int datalen,
390 unsigned char *output) {
392 unsigned char intermediate[20];
394 sha1_key_internal(states, key, keylen);
395 SHA_Bytes(&states[0], data, datalen);
396 SHA_Final(&states[0], intermediate);
398 SHA_Bytes(&states[1], intermediate, 20);
399 SHA_Final(&states[1], output);
402 const struct ssh_mac ssh_hmac_sha1 = {
403 sha1_make_context, sha1_free_context, sha1_key,
404 sha1_generate, sha1_verify,
405 hmacsha1_start, hmacsha1_bytes, hmacsha1_genresult, hmacsha1_verresult,
406 "hmac-sha1", "hmac-sha1-etm@openssh.com",
411 const struct ssh_mac ssh_hmac_sha1_96 = {
412 sha1_make_context, sha1_free_context, sha1_key,
413 sha1_96_generate, sha1_96_verify,
414 hmacsha1_start, hmacsha1_bytes,
415 hmacsha1_96_genresult, hmacsha1_96_verresult,
416 "hmac-sha1-96", "hmac-sha1-96-etm@openssh.com",
421 const struct ssh_mac ssh_hmac_sha1_buggy = {
422 sha1_make_context, sha1_free_context, sha1_key_buggy,
423 sha1_generate, sha1_verify,
424 hmacsha1_start, hmacsha1_bytes, hmacsha1_genresult, hmacsha1_verresult,
427 "bug-compatible HMAC-SHA1"
430 const struct ssh_mac ssh_hmac_sha1_96_buggy = {
431 sha1_make_context, sha1_free_context, sha1_key_buggy,
432 sha1_96_generate, sha1_96_verify,
433 hmacsha1_start, hmacsha1_bytes,
434 hmacsha1_96_genresult, hmacsha1_96_verresult,
435 "hmac-sha1-96", NULL,
437 "bug-compatible HMAC-SHA1-96"