Sebastian Kuschel reports that pfd_closing can be called for a socket
[u/mdw/putty] / sshsha.c
CommitLineData
374330e2 1/*
2e85c969 2 * SHA1 hash algorithm. Used in SSH-2 as a MAC, and the transform is
e5574168 3 * also used as a `stirring' function for the PuTTY random number
4 * pool. Implemented directly from the specification by Simon
5 * Tatham.
374330e2 6 */
7
8#include "ssh.h"
9
e5574168 10/* ----------------------------------------------------------------------
11 * Core SHA algorithm: processes 16-word blocks into a message digest.
12 */
13
14#define rol(x,y) ( ((x) << (y)) | (((uint32)x) >> (32-y)) )
15
d5696ae5 16static void SHA_Core_Init(uint32 h[5])
32874aea 17{
e5574168 18 h[0] = 0x67452301;
19 h[1] = 0xefcdab89;
20 h[2] = 0x98badcfe;
21 h[3] = 0x10325476;
22 h[4] = 0xc3d2e1f0;
23}
e9483e66 24
32874aea 25void SHATransform(word32 * digest, word32 * block)
26{
e9483e66 27 word32 w[80];
32874aea 28 word32 a, b, c, d, e;
e9483e66 29 int t;
30
d2be8a43 31#ifdef RANDOM_DIAGNOSTICS
32 {
33 extern int random_diagnostics;
34 if (random_diagnostics) {
35 int i;
36 printf("SHATransform:");
37 for (i = 0; i < 5; i++)
38 printf(" %08x", digest[i]);
39 printf(" +");
40 for (i = 0; i < 16; i++)
41 printf(" %08x", block[i]);
42 }
43 }
44#endif
45
e9483e66 46 for (t = 0; t < 16; t++)
32874aea 47 w[t] = block[t];
e9483e66 48
49 for (t = 16; t < 80; t++) {
32874aea 50 word32 tmp = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16];
51 w[t] = rol(tmp, 1);
e9483e66 52 }
53
54 a = digest[0];
55 b = digest[1];
56 c = digest[2];
57 d = digest[3];
58 e = digest[4];
59
60 for (t = 0; t < 20; t++) {
32874aea 61 word32 tmp =
62 rol(a, 5) + ((b & c) | (d & ~b)) + e + w[t] + 0x5a827999;
63 e = d;
64 d = c;
65 c = rol(b, 30);
66 b = a;
67 a = tmp;
e9483e66 68 }
69 for (t = 20; t < 40; t++) {
32874aea 70 word32 tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0x6ed9eba1;
71 e = d;
72 d = c;
73 c = rol(b, 30);
74 b = a;
75 a = tmp;
e9483e66 76 }
77 for (t = 40; t < 60; t++) {
32874aea 78 word32 tmp = rol(a,
79 5) + ((b & c) | (b & d) | (c & d)) + e + w[t] +
80 0x8f1bbcdc;
81 e = d;
82 d = c;
83 c = rol(b, 30);
84 b = a;
85 a = tmp;
e9483e66 86 }
87 for (t = 60; t < 80; t++) {
32874aea 88 word32 tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0xca62c1d6;
89 e = d;
90 d = c;
91 c = rol(b, 30);
92 b = a;
93 a = tmp;
e9483e66 94 }
95
96 digest[0] += a;
97 digest[1] += b;
98 digest[2] += c;
99 digest[3] += d;
100 digest[4] += e;
d2be8a43 101
102#ifdef RANDOM_DIAGNOSTICS
103 {
104 extern int random_diagnostics;
105 if (random_diagnostics) {
106 int i;
107 printf(" =");
108 for (i = 0; i < 5; i++)
109 printf(" %08x", digest[i]);
110 printf("\n");
111 }
112 }
113#endif
374330e2 114}
e5574168 115
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.
120 */
121
32874aea 122void SHA_Init(SHA_State * s)
123{
e5574168 124 SHA_Core_Init(s->h);
125 s->blkused = 0;
126 s->lenhi = s->lenlo = 0;
127}
128
42466758 129void SHA_Bytes(SHA_State * s, const void *p, int len)
32874aea 130{
42466758 131 const unsigned char *q = (const unsigned char *) p;
e5574168 132 uint32 wordblock[16];
133 uint32 lenw = len;
134 int i;
135
136 /*
137 * Update the length field.
138 */
139 s->lenlo += lenw;
140 s->lenhi += (s->lenlo < lenw);
141
32874aea 142 if (s->blkused && s->blkused + len < 64) {
143 /*
144 * Trivial case: just add to the block.
145 */
146 memcpy(s->block + s->blkused, q, len);
147 s->blkused += len;
e5574168 148 } else {
32874aea 149 /*
150 * We must complete and process at least one block.
151 */
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++) {
158 wordblock[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);
163 }
164 SHATransform(s->h, wordblock);
165 s->blkused = 0;
166 }
167 memcpy(s->block, q, len);
168 s->blkused = len;
e5574168 169 }
170}
171
32874aea 172void SHA_Final(SHA_State * s, unsigned char *output)
173{
e5574168 174 int i;
175 int pad;
176 unsigned char c[64];
177 uint32 lenhi, lenlo;
178
179 if (s->blkused >= 56)
32874aea 180 pad = 56 + 64 - s->blkused;
e5574168 181 else
32874aea 182 pad = 56 - s->blkused;
e5574168 183
32874aea 184 lenhi = (s->lenhi << 3) | (s->lenlo >> (32 - 3));
e5574168 185 lenlo = (s->lenlo << 3);
186
187 memset(c, 0, pad);
188 c[0] = 0x80;
189 SHA_Bytes(s, &c, pad);
190
191 c[0] = (lenhi >> 24) & 0xFF;
192 c[1] = (lenhi >> 16) & 0xFF;
32874aea 193 c[2] = (lenhi >> 8) & 0xFF;
194 c[3] = (lenhi >> 0) & 0xFF;
e5574168 195 c[4] = (lenlo >> 24) & 0xFF;
196 c[5] = (lenlo >> 16) & 0xFF;
32874aea 197 c[6] = (lenlo >> 8) & 0xFF;
198 c[7] = (lenlo >> 0) & 0xFF;
e5574168 199
200 SHA_Bytes(s, &c, 8);
201
202 for (i = 0; i < 5; i++) {
32874aea 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;
e5574168 207 }
208}
209
42466758 210void SHA_Simple(const void *p, int len, unsigned char *output)
32874aea 211{
e5574168 212 SHA_State s;
213
214 SHA_Init(&s);
215 SHA_Bytes(&s, p, len);
216 SHA_Final(&s, output);
217}
218
b672f405 219/*
220 * Thin abstraction for things where hashes are pluggable.
221 */
222
223static void *sha1_init(void)
224{
225 SHA_State *s;
226
227 s = snew(SHA_State);
228 SHA_Init(s);
229 return s;
230}
231
232static void sha1_bytes(void *handle, void *p, int len)
233{
234 SHA_State *s = handle;
235
236 SHA_Bytes(s, p, len);
237}
238
239static void sha1_final(void *handle, unsigned char *output)
240{
241 SHA_State *s = handle;
242
243 SHA_Final(s, output);
244 sfree(s);
245}
246
247const struct ssh_hash ssh_sha1 = {
c6daaa1a 248 sha1_init, sha1_bytes, sha1_final, 20, "SHA-1"
b672f405 249};
250
e5574168 251/* ----------------------------------------------------------------------
252 * The above is the SHA-1 algorithm itself. Now we implement the
253 * HMAC wrapper on it.
254 */
255
e0e1a00d 256static void *sha1_make_context(void)
257{
9916cc1e 258 return snewn(3, SHA_State);
e0e1a00d 259}
260
261static void sha1_free_context(void *handle)
262{
263 sfree(handle);
264}
e5574168 265
e0e1a00d 266static void sha1_key_internal(void *handle, unsigned char *key, int len)
32874aea 267{
e0e1a00d 268 SHA_State *keys = (SHA_State *)handle;
e5574168 269 unsigned char foo[64];
270 int i;
271
272 memset(foo, 0x36, 64);
273 for (i = 0; i < len && i < 64; i++)
32874aea 274 foo[i] ^= key[i];
e0e1a00d 275 SHA_Init(&keys[0]);
276 SHA_Bytes(&keys[0], foo, 64);
e5574168 277
278 memset(foo, 0x5C, 64);
279 for (i = 0; i < len && i < 64; i++)
32874aea 280 foo[i] ^= key[i];
e0e1a00d 281 SHA_Init(&keys[1]);
282 SHA_Bytes(&keys[1], foo, 64);
e5574168 283
dfb88efd 284 smemclr(foo, 64); /* burn the evidence */
e5574168 285}
286
e0e1a00d 287static void sha1_key(void *handle, unsigned char *key)
32874aea 288{
e0e1a00d 289 sha1_key_internal(handle, key, 20);
7591b9ff 290}
291
e0e1a00d 292static void sha1_key_buggy(void *handle, unsigned char *key)
32874aea 293{
e0e1a00d 294 sha1_key_internal(handle, key, 16);
7591b9ff 295}
296
9916cc1e 297static void hmacsha1_start(void *handle)
298{
299 SHA_State *keys = (SHA_State *)handle;
300
301 keys[2] = keys[0]; /* structure copy */
302}
303
304static void hmacsha1_bytes(void *handle, unsigned char const *blk, int len)
305{
306 SHA_State *keys = (SHA_State *)handle;
307 SHA_Bytes(&keys[2], (void *)blk, len);
308}
309
310static void hmacsha1_genresult(void *handle, unsigned char *hmac)
32874aea 311{
e0e1a00d 312 SHA_State *keys = (SHA_State *)handle;
e5574168 313 SHA_State s;
314 unsigned char intermediate[20];
315
9916cc1e 316 s = keys[2]; /* structure copy */
e5574168 317 SHA_Final(&s, intermediate);
e0e1a00d 318 s = keys[1]; /* structure copy */
e5574168 319 SHA_Bytes(&s, intermediate, 20);
320 SHA_Final(&s, hmac);
321}
322
9916cc1e 323static void sha1_do_hmac(void *handle, unsigned char *blk, int len,
324 unsigned long seq, unsigned char *hmac)
325{
326 unsigned char seqbuf[4];
327
bb4a3bb6 328 PUT_32BIT_MSB_FIRST(seqbuf, seq);
9916cc1e 329 hmacsha1_start(handle);
330 hmacsha1_bytes(handle, seqbuf, 4);
331 hmacsha1_bytes(handle, blk, len);
332 hmacsha1_genresult(handle, hmac);
333}
334
e0e1a00d 335static void sha1_generate(void *handle, unsigned char *blk, int len,
336 unsigned long seq)
32874aea 337{
e0e1a00d 338 sha1_do_hmac(handle, blk, len, seq, blk + len);
e5574168 339}
340
9916cc1e 341static int hmacsha1_verresult(void *handle, unsigned char const *hmac)
342{
343 unsigned char correct[20];
344 hmacsha1_genresult(handle, correct);
345 return !memcmp(correct, hmac, 20);
346}
347
e0e1a00d 348static int sha1_verify(void *handle, unsigned char *blk, int len,
349 unsigned long seq)
32874aea 350{
e5574168 351 unsigned char correct[20];
e0e1a00d 352 sha1_do_hmac(handle, blk, len, seq, correct);
32874aea 353 return !memcmp(correct, blk + len, 20);
e5574168 354}
355
9916cc1e 356static void hmacsha1_96_genresult(void *handle, unsigned char *hmac)
357{
358 unsigned char full[20];
359 hmacsha1_genresult(handle, full);
360 memcpy(hmac, full, 12);
361}
362
6668a75e 363static void sha1_96_generate(void *handle, unsigned char *blk, int len,
364 unsigned long seq)
365{
366 unsigned char full[20];
367 sha1_do_hmac(handle, blk, len, seq, full);
368 memcpy(blk + len, full, 12);
369}
370
9916cc1e 371static int hmacsha1_96_verresult(void *handle, unsigned char const *hmac)
372{
373 unsigned char correct[20];
374 hmacsha1_genresult(handle, correct);
375 return !memcmp(correct, hmac, 12);
376}
377
6668a75e 378static int sha1_96_verify(void *handle, unsigned char *blk, int len,
379 unsigned long seq)
380{
381 unsigned char correct[20];
382 sha1_do_hmac(handle, blk, len, seq, correct);
383 return !memcmp(correct, blk + len, 12);
384}
385
5c72ca61 386void hmac_sha1_simple(void *key, int keylen, void *data, int datalen,
387 unsigned char *output) {
e0e1a00d 388 SHA_State states[2];
5c72ca61 389 unsigned char intermediate[20];
390
e0e1a00d 391 sha1_key_internal(states, key, keylen);
392 SHA_Bytes(&states[0], data, datalen);
393 SHA_Final(&states[0], intermediate);
5c72ca61 394
e0e1a00d 395 SHA_Bytes(&states[1], intermediate, 20);
396 SHA_Final(&states[1], output);
5c72ca61 397}
398
1993f323 399const struct ssh_mac ssh_hmac_sha1 = {
e0e1a00d 400 sha1_make_context, sha1_free_context, sha1_key,
401 sha1_generate, sha1_verify,
9916cc1e 402 hmacsha1_start, hmacsha1_bytes, hmacsha1_genresult, hmacsha1_verresult,
e5574168 403 "hmac-sha1",
6c135243 404 20,
405 "HMAC-SHA1"
e5574168 406};
7591b9ff 407
6668a75e 408const struct ssh_mac ssh_hmac_sha1_96 = {
409 sha1_make_context, sha1_free_context, sha1_key,
410 sha1_96_generate, sha1_96_verify,
9916cc1e 411 hmacsha1_start, hmacsha1_bytes,
412 hmacsha1_96_genresult, hmacsha1_96_verresult,
6668a75e 413 "hmac-sha1-96",
414 12,
415 "HMAC-SHA1-96"
416};
417
1993f323 418const struct ssh_mac ssh_hmac_sha1_buggy = {
e0e1a00d 419 sha1_make_context, sha1_free_context, sha1_key_buggy,
420 sha1_generate, sha1_verify,
9916cc1e 421 hmacsha1_start, hmacsha1_bytes, hmacsha1_genresult, hmacsha1_verresult,
7591b9ff 422 "hmac-sha1",
6c135243 423 20,
424 "bug-compatible HMAC-SHA1"
7591b9ff 425};
6668a75e 426
427const 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,
9916cc1e 430 hmacsha1_start, hmacsha1_bytes,
431 hmacsha1_96_genresult, hmacsha1_96_verresult,
6668a75e 432 "hmac-sha1-96",
433 12,
434 "bug-compatible HMAC-SHA1-96"
435};