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
);
220 * Thin abstraction for things where hashes are pluggable.
223 static void *sha1_init(void)
232 static void sha1_bytes(void *handle
, void *p
, int len
)
234 SHA_State
*s
= handle
;
236 SHA_Bytes(s
, p
, len
);
239 static void sha1_final(void *handle
, unsigned char *output
)
241 SHA_State
*s
= handle
;
243 SHA_Final(s
, output
);
247 const struct ssh_hash ssh_sha1
= {
248 sha1_init
, sha1_bytes
, sha1_final
, 20, "SHA-1"
251 /* ----------------------------------------------------------------------
252 * The above is the SHA-1 algorithm itself. Now we implement the
253 * HMAC wrapper on it.
256 static void *sha1_make_context(void)
258 return snewn(3, SHA_State
);
261 static void sha1_free_context(void *handle
)
266 static void sha1_key_internal(void *handle
, unsigned char *key
, int len
)
268 SHA_State
*keys
= (SHA_State
*)handle
;
269 unsigned char foo
[64];
272 memset(foo
, 0x36, 64);
273 for (i
= 0; i
< len
&& i
< 64; i
++)
276 SHA_Bytes(&keys
[0], foo
, 64);
278 memset(foo
, 0x5C, 64);
279 for (i
= 0; i
< len
&& i
< 64; i
++)
282 SHA_Bytes(&keys
[1], foo
, 64);
284 smemclr(foo
, 64); /* burn the evidence */
287 static void sha1_key(void *handle
, unsigned char *key
)
289 sha1_key_internal(handle
, key
, 20);
292 static void sha1_key_buggy(void *handle
, unsigned char *key
)
294 sha1_key_internal(handle
, key
, 16);
297 static void hmacsha1_start(void *handle
)
299 SHA_State
*keys
= (SHA_State
*)handle
;
301 keys
[2] = keys
[0]; /* structure copy */
304 static void hmacsha1_bytes(void *handle
, unsigned char const *blk
, int len
)
306 SHA_State
*keys
= (SHA_State
*)handle
;
307 SHA_Bytes(&keys
[2], (void *)blk
, len
);
310 static void hmacsha1_genresult(void *handle
, unsigned char *hmac
)
312 SHA_State
*keys
= (SHA_State
*)handle
;
314 unsigned char intermediate
[20];
316 s
= keys
[2]; /* structure copy */
317 SHA_Final(&s
, intermediate
);
318 s
= keys
[1]; /* structure copy */
319 SHA_Bytes(&s
, intermediate
, 20);
323 static void sha1_do_hmac(void *handle
, unsigned char *blk
, int len
,
324 unsigned long seq
, unsigned char *hmac
)
326 unsigned char seqbuf
[4];
328 PUT_32BIT_MSB_FIRST(seqbuf
, seq
);
329 hmacsha1_start(handle
);
330 hmacsha1_bytes(handle
, seqbuf
, 4);
331 hmacsha1_bytes(handle
, blk
, len
);
332 hmacsha1_genresult(handle
, hmac
);
335 static void sha1_generate(void *handle
, unsigned char *blk
, int len
,
338 sha1_do_hmac(handle
, blk
, len
, seq
, blk
+ len
);
341 static int hmacsha1_verresult(void *handle
, unsigned char const *hmac
)
343 unsigned char correct
[20];
344 hmacsha1_genresult(handle
, correct
);
345 return !memcmp(correct
, hmac
, 20);
348 static int sha1_verify(void *handle
, unsigned char *blk
, int len
,
351 unsigned char correct
[20];
352 sha1_do_hmac(handle
, blk
, len
, seq
, correct
);
353 return !memcmp(correct
, blk
+ len
, 20);
356 static void hmacsha1_96_genresult(void *handle
, unsigned char *hmac
)
358 unsigned char full
[20];
359 hmacsha1_genresult(handle
, full
);
360 memcpy(hmac
, full
, 12);
363 static void sha1_96_generate(void *handle
, unsigned char *blk
, int len
,
366 unsigned char full
[20];
367 sha1_do_hmac(handle
, blk
, len
, seq
, full
);
368 memcpy(blk
+ len
, full
, 12);
371 static int hmacsha1_96_verresult(void *handle
, unsigned char const *hmac
)
373 unsigned char correct
[20];
374 hmacsha1_genresult(handle
, correct
);
375 return !memcmp(correct
, hmac
, 12);
378 static int sha1_96_verify(void *handle
, unsigned char *blk
, int len
,
381 unsigned char correct
[20];
382 sha1_do_hmac(handle
, blk
, len
, seq
, correct
);
383 return !memcmp(correct
, blk
+ len
, 12);
386 void hmac_sha1_simple(void *key
, int keylen
, void *data
, int datalen
,
387 unsigned char *output
) {
389 unsigned char intermediate
[20];
391 sha1_key_internal(states
, key
, keylen
);
392 SHA_Bytes(&states
[0], data
, datalen
);
393 SHA_Final(&states
[0], intermediate
);
395 SHA_Bytes(&states
[1], intermediate
, 20);
396 SHA_Final(&states
[1], output
);
399 const struct ssh_mac ssh_hmac_sha1
= {
400 sha1_make_context
, sha1_free_context
, sha1_key
,
401 sha1_generate
, sha1_verify
,
402 hmacsha1_start
, hmacsha1_bytes
, hmacsha1_genresult
, hmacsha1_verresult
,
408 const struct ssh_mac ssh_hmac_sha1_96
= {
409 sha1_make_context
, sha1_free_context
, sha1_key
,
410 sha1_96_generate
, sha1_96_verify
,
411 hmacsha1_start
, hmacsha1_bytes
,
412 hmacsha1_96_genresult
, hmacsha1_96_verresult
,
418 const struct ssh_mac ssh_hmac_sha1_buggy
= {
419 sha1_make_context
, sha1_free_context
, sha1_key_buggy
,
420 sha1_generate
, sha1_verify
,
421 hmacsha1_start
, hmacsha1_bytes
, hmacsha1_genresult
, hmacsha1_verresult
,
424 "bug-compatible HMAC-SHA1"
427 const struct ssh_mac ssh_hmac_sha1_96_buggy
= {
428 sha1_make_context
, sha1_free_context
, sha1_key_buggy
,
429 sha1_96_generate
, sha1_96_verify
,
430 hmacsha1_start
, hmacsha1_bytes
,
431 hmacsha1_96_genresult
, hmacsha1_96_verresult
,
434 "bug-compatible HMAC-SHA1-96"