OSDN Git Service

Compile executable files.
[ffftp/ffftp.git] / contrib / putty / SSHCRCDA.C
1 /*      $OpenBSD: deattack.c,v 1.14 2001/06/23 15:12:18 itojun Exp $    */\r
2 \r
3 /*\r
4  * Cryptographic attack detector for ssh - source code\r
5  *\r
6  * Copyright (c) 1998 CORE SDI S.A., Buenos Aires, Argentina.\r
7  *\r
8  * All rights reserved. Redistribution and use in source and binary\r
9  * forms, with or without modification, are permitted provided that\r
10  * this copyright notice is retained.\r
11  *\r
12  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED\r
13  * WARRANTIES ARE DISCLAIMED. IN NO EVENT SHALL CORE SDI S.A. BE\r
14  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR\r
15  * CONSEQUENTIAL DAMAGES RESULTING FROM THE USE OR MISUSE OF THIS\r
16  * SOFTWARE.\r
17  *\r
18  * Ariel Futoransky <futo@core-sdi.com>\r
19  * <http://www.core-sdi.com>\r
20  * \r
21  * Modified for use in PuTTY by Simon Tatham\r
22  */\r
23 \r
24 #include <assert.h>\r
25 #include "misc.h"\r
26 #include "ssh.h"\r
27 \r
28 typedef unsigned char uchar;\r
29 typedef unsigned short uint16;\r
30 \r
31 /* SSH Constants */\r
32 #define SSH_MAXBLOCKS   (32 * 1024)\r
33 #define SSH_BLOCKSIZE   (8)\r
34 \r
35 /* Hashing constants */\r
36 #define HASH_MINSIZE    (8 * 1024)\r
37 #define HASH_ENTRYSIZE  (sizeof(uint16))\r
38 #define HASH_FACTOR(x)  ((x)*3/2)\r
39 #define HASH_UNUSEDCHAR (0xff)\r
40 #define HASH_UNUSED     (0xffff)\r
41 #define HASH_IV         (0xfffe)\r
42 \r
43 #define HASH_MINBLOCKS  (7*SSH_BLOCKSIZE)\r
44 \r
45 /* Hash function (Input keys are cipher results) */\r
46 #define HASH(x)         GET_32BIT_MSB_FIRST(x)\r
47 \r
48 #define CMP(a, b)       (memcmp(a, b, SSH_BLOCKSIZE))\r
49 \r
50 uchar ONE[4] = { 1, 0, 0, 0 };\r
51 uchar ZERO[4] = { 0, 0, 0, 0 };\r
52 \r
53 struct crcda_ctx {\r
54     uint16 *h;\r
55     uint32 n;\r
56 };\r
57 \r
58 void *crcda_make_context(void)\r
59 {\r
60     struct crcda_ctx *ret = snew(struct crcda_ctx);\r
61     ret->h = NULL;\r
62     ret->n = HASH_MINSIZE / HASH_ENTRYSIZE;\r
63     return ret;\r
64 }\r
65 \r
66 void crcda_free_context(void *handle)\r
67 {\r
68     struct crcda_ctx *ctx = (struct crcda_ctx *)handle;\r
69     if (ctx) {\r
70         sfree(ctx->h);\r
71         ctx->h = NULL;\r
72         sfree(ctx);\r
73     }\r
74 }\r
75 \r
76 static void crc_update(uint32 *a, void *b)\r
77 {\r
78     *a = crc32_update(*a, b, 4);\r
79 }\r
80 \r
81 /* detect if a block is used in a particular pattern */\r
82 static int check_crc(uchar *S, uchar *buf, uint32 len, uchar *IV)\r
83 {\r
84     uint32 crc;\r
85     uchar *c;\r
86 \r
87     crc = 0;\r
88     if (IV && !CMP(S, IV)) {\r
89         crc_update(&crc, ONE);\r
90         crc_update(&crc, ZERO);\r
91     }\r
92     for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {\r
93         if (!CMP(S, c)) {\r
94             crc_update(&crc, ONE);\r
95             crc_update(&crc, ZERO);\r
96         } else {\r
97             crc_update(&crc, ZERO);\r
98             crc_update(&crc, ZERO);\r
99         }\r
100     }\r
101     return (crc == 0);\r
102 }\r
103 \r
104 /* Detect a crc32 compensation attack on a packet */\r
105 int detect_attack(void *handle, uchar *buf, uint32 len, uchar *IV)\r
106 {\r
107     struct crcda_ctx *ctx = (struct crcda_ctx *)handle;\r
108     register uint32 i, j;\r
109     uint32 l;\r
110     register uchar *c;\r
111     uchar *d;\r
112 \r
113     assert(!(len > (SSH_MAXBLOCKS * SSH_BLOCKSIZE) ||\r
114              len % SSH_BLOCKSIZE != 0));\r
115     for (l = ctx->n; l < HASH_FACTOR(len / SSH_BLOCKSIZE); l = l << 2)\r
116         ;\r
117 \r
118     if (ctx->h == NULL) {\r
119         ctx->n = l;\r
120         ctx->h = snewn(ctx->n, uint16);\r
121     } else {\r
122         if (l > ctx->n) {\r
123             ctx->n = l;\r
124             ctx->h = sresize(ctx->h, ctx->n, uint16);\r
125         }\r
126     }\r
127 \r
128     if (len <= HASH_MINBLOCKS) {\r
129         for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {\r
130             if (IV && (!CMP(c, IV))) {\r
131                 if ((check_crc(c, buf, len, IV)))\r
132                     return 1;          /* attack detected */\r
133                 else\r
134                     break;\r
135             }\r
136             for (d = buf; d < c; d += SSH_BLOCKSIZE) {\r
137                 if (!CMP(c, d)) {\r
138                     if ((check_crc(c, buf, len, IV)))\r
139                         return 1;      /* attack detected */\r
140                     else\r
141                         break;\r
142                 }\r
143             }\r
144         }\r
145         return 0;                      /* ok */\r
146     }\r
147     memset(ctx->h, HASH_UNUSEDCHAR, ctx->n * HASH_ENTRYSIZE);\r
148 \r
149     if (IV)\r
150         ctx->h[HASH(IV) & (ctx->n - 1)] = HASH_IV;\r
151 \r
152     for (c = buf, j = 0; c < (buf + len); c += SSH_BLOCKSIZE, j++) {\r
153         for (i = HASH(c) & (ctx->n - 1); ctx->h[i] != HASH_UNUSED;\r
154              i = (i + 1) & (ctx->n - 1)) {\r
155             if (ctx->h[i] == HASH_IV) {\r
156                 if (!CMP(c, IV)) {\r
157                     if (check_crc(c, buf, len, IV))\r
158                         return 1;      /* attack detected */\r
159                     else\r
160                         break;\r
161                 }\r
162             } else if (!CMP(c, buf + ctx->h[i] * SSH_BLOCKSIZE)) {\r
163                 if (check_crc(c, buf, len, IV))\r
164                     return 1;          /* attack detected */\r
165                 else\r
166                     break;\r
167             }\r
168         }\r
169         ctx->h[i] = j;\r
170     }\r
171     return 0;                          /* ok */\r
172 }\r