]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - sshsha.c
Use a timing-safe memory compare to verify MACs.
[PuTTY.git] / sshsha.c
1 /*
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
5  * Tatham.
6  */
7
8 #include "ssh.h"
9
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
16 static void SHA_Core_Init(uint32 h[5])
17 {
18     h[0] = 0x67452301;
19     h[1] = 0xefcdab89;
20     h[2] = 0x98badcfe;
21     h[3] = 0x10325476;
22     h[4] = 0xc3d2e1f0;
23 }
24
25 void SHATransform(word32 * digest, word32 * block)
26 {
27     word32 w[80];
28     word32 a, b, c, d, e;
29     int t;
30
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
46     for (t = 0; t < 16; t++)
47         w[t] = block[t];
48
49     for (t = 16; t < 80; t++) {
50         word32 tmp = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16];
51         w[t] = rol(tmp, 1);
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++) {
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;
68     }
69     for (t = 20; t < 40; t++) {
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;
76     }
77     for (t = 40; t < 60; t++) {
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;
86     }
87     for (t = 60; t < 80; t++) {
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;
94     }
95
96     digest[0] += a;
97     digest[1] += b;
98     digest[2] += c;
99     digest[3] += d;
100     digest[4] += e;
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
114 }
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
122 void SHA_Init(SHA_State * s)
123 {
124     SHA_Core_Init(s->h);
125     s->blkused = 0;
126     s->lenhi = s->lenlo = 0;
127 }
128
129 void SHA_Bytes(SHA_State * s, const void *p, int len)
130 {
131     const unsigned char *q = (const unsigned char *) p;
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
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;
148     } else {
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;
169     }
170 }
171
172 void SHA_Final(SHA_State * s, unsigned char *output)
173 {
174     int i;
175     int pad;
176     unsigned char c[64];
177     uint32 lenhi, lenlo;
178
179     if (s->blkused >= 56)
180         pad = 56 + 64 - s->blkused;
181     else
182         pad = 56 - s->blkused;
183
184     lenhi = (s->lenhi << 3) | (s->lenlo >> (32 - 3));
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;
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;
199
200     SHA_Bytes(s, &c, 8);
201
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;
207     }
208 }
209
210 void SHA_Simple(const void *p, int len, unsigned char *output)
211 {
212     SHA_State s;
213
214     SHA_Init(&s);
215     SHA_Bytes(&s, p, len);
216     SHA_Final(&s, output);
217 }
218
219 /*
220  * Thin abstraction for things where hashes are pluggable.
221  */
222
223 static void *sha1_init(void)
224 {
225     SHA_State *s;
226
227     s = snew(SHA_State);
228     SHA_Init(s);
229     return s;
230 }
231
232 static void sha1_bytes(void *handle, void *p, int len)
233 {
234     SHA_State *s = handle;
235
236     SHA_Bytes(s, p, len);
237 }
238
239 static 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
247 const struct ssh_hash ssh_sha1 = {
248     sha1_init, sha1_bytes, sha1_final, 20, "SHA-1"
249 };
250
251 /* ----------------------------------------------------------------------
252  * The above is the SHA-1 algorithm itself. Now we implement the
253  * HMAC wrapper on it.
254  */
255
256 static void *sha1_make_context(void)
257 {
258     return snewn(3, SHA_State);
259 }
260
261 static void sha1_free_context(void *handle)
262 {
263     sfree(handle);
264 }
265
266 static void sha1_key_internal(void *handle, unsigned char *key, int len)
267 {
268     SHA_State *keys = (SHA_State *)handle;
269     unsigned char foo[64];
270     int i;
271
272     memset(foo, 0x36, 64);
273     for (i = 0; i < len && i < 64; i++)
274         foo[i] ^= key[i];
275     SHA_Init(&keys[0]);
276     SHA_Bytes(&keys[0], foo, 64);
277
278     memset(foo, 0x5C, 64);
279     for (i = 0; i < len && i < 64; i++)
280         foo[i] ^= key[i];
281     SHA_Init(&keys[1]);
282     SHA_Bytes(&keys[1], foo, 64);
283
284     smemclr(foo, 64);                  /* burn the evidence */
285 }
286
287 static void sha1_key(void *handle, unsigned char *key)
288 {
289     sha1_key_internal(handle, key, 20);
290 }
291
292 static void sha1_key_buggy(void *handle, unsigned char *key)
293 {
294     sha1_key_internal(handle, key, 16);
295 }
296
297 static void hmacsha1_start(void *handle)
298 {
299     SHA_State *keys = (SHA_State *)handle;
300
301     keys[2] = keys[0];                /* structure copy */
302 }
303
304 static 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
310 static void hmacsha1_genresult(void *handle, unsigned char *hmac)
311 {
312     SHA_State *keys = (SHA_State *)handle;
313     SHA_State s;
314     unsigned char intermediate[20];
315
316     s = keys[2];                       /* structure copy */
317     SHA_Final(&s, intermediate);
318     s = keys[1];                       /* structure copy */
319     SHA_Bytes(&s, intermediate, 20);
320     SHA_Final(&s, hmac);
321 }
322
323 static 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
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);
333 }
334
335 static void sha1_generate(void *handle, unsigned char *blk, int len,
336                           unsigned long seq)
337 {
338     sha1_do_hmac(handle, blk, len, seq, blk + len);
339 }
340
341 static int hmacsha1_verresult(void *handle, unsigned char const *hmac)
342 {
343     unsigned char correct[20];
344     hmacsha1_genresult(handle, correct);
345     return smemeq(correct, hmac, 20);
346 }
347
348 static int sha1_verify(void *handle, unsigned char *blk, int len,
349                        unsigned long seq)
350 {
351     unsigned char correct[20];
352     sha1_do_hmac(handle, blk, len, seq, correct);
353     return smemeq(correct, blk + len, 20);
354 }
355
356 static 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
363 static 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
371 static int hmacsha1_96_verresult(void *handle, unsigned char const *hmac)
372 {
373     unsigned char correct[20];
374     hmacsha1_genresult(handle, correct);
375     return smemeq(correct, hmac, 12);
376 }
377
378 static 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 smemeq(correct, blk + len, 12);
384 }
385
386 void hmac_sha1_simple(void *key, int keylen, void *data, int datalen,
387                       unsigned char *output) {
388     SHA_State states[2];
389     unsigned char intermediate[20];
390
391     sha1_key_internal(states, key, keylen);
392     SHA_Bytes(&states[0], data, datalen);
393     SHA_Final(&states[0], intermediate);
394
395     SHA_Bytes(&states[1], intermediate, 20);
396     SHA_Final(&states[1], output);
397 }
398
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,
403     "hmac-sha1", "hmac-sha1-etm@openssh.com",
404     20,
405     "HMAC-SHA1"
406 };
407
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,
413     "hmac-sha1-96", "hmac-sha1-96-etm@openssh.com",
414     12,
415     "HMAC-SHA1-96"
416 };
417
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,
422     "hmac-sha1", NULL,
423     20,
424     "bug-compatible HMAC-SHA1"
425 };
426
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,
432     "hmac-sha1-96", NULL,
433     12,
434     "bug-compatible HMAC-SHA1-96"
435 };