--- /dev/null
+/* $OpenBSD: deattack.c,v 1.14 2001/06/23 15:12:18 itojun Exp $ */\r
+\r
+/*\r
+ * Cryptographic attack detector for ssh - source code\r
+ *\r
+ * Copyright (c) 1998 CORE SDI S.A., Buenos Aires, Argentina.\r
+ *\r
+ * All rights reserved. Redistribution and use in source and binary\r
+ * forms, with or without modification, are permitted provided that\r
+ * this copyright notice is retained.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED\r
+ * WARRANTIES ARE DISCLAIMED. IN NO EVENT SHALL CORE SDI S.A. BE\r
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR\r
+ * CONSEQUENTIAL DAMAGES RESULTING FROM THE USE OR MISUSE OF THIS\r
+ * SOFTWARE.\r
+ *\r
+ * Ariel Futoransky <futo@core-sdi.com>\r
+ * <http://www.core-sdi.com>\r
+ * \r
+ * Modified for use in PuTTY by Simon Tatham\r
+ */\r
+\r
+#include <assert.h>\r
+#include "misc.h"\r
+#include "ssh.h"\r
+\r
+typedef unsigned char uchar;\r
+typedef unsigned short uint16;\r
+\r
+/* SSH Constants */\r
+#define SSH_MAXBLOCKS (32 * 1024)\r
+#define SSH_BLOCKSIZE (8)\r
+\r
+/* Hashing constants */\r
+#define HASH_MINSIZE (8 * 1024)\r
+#define HASH_ENTRYSIZE (sizeof(uint16))\r
+#define HASH_FACTOR(x) ((x)*3/2)\r
+#define HASH_UNUSEDCHAR (0xff)\r
+#define HASH_UNUSED (0xffff)\r
+#define HASH_IV (0xfffe)\r
+\r
+#define HASH_MINBLOCKS (7*SSH_BLOCKSIZE)\r
+\r
+/* Hash function (Input keys are cipher results) */\r
+#define HASH(x) GET_32BIT_MSB_FIRST(x)\r
+\r
+#define CMP(a, b) (memcmp(a, b, SSH_BLOCKSIZE))\r
+\r
+uchar ONE[4] = { 1, 0, 0, 0 };\r
+uchar ZERO[4] = { 0, 0, 0, 0 };\r
+\r
+struct crcda_ctx {\r
+ uint16 *h;\r
+ uint32 n;\r
+};\r
+\r
+void *crcda_make_context(void)\r
+{\r
+ struct crcda_ctx *ret = snew(struct crcda_ctx);\r
+ ret->h = NULL;\r
+ ret->n = HASH_MINSIZE / HASH_ENTRYSIZE;\r
+ return ret;\r
+}\r
+\r
+void crcda_free_context(void *handle)\r
+{\r
+ struct crcda_ctx *ctx = (struct crcda_ctx *)handle;\r
+ if (ctx) {\r
+ sfree(ctx->h);\r
+ ctx->h = NULL;\r
+ sfree(ctx);\r
+ }\r
+}\r
+\r
+static void crc_update(uint32 *a, void *b)\r
+{\r
+ *a = crc32_update(*a, b, 4);\r
+}\r
+\r
+/* detect if a block is used in a particular pattern */\r
+static int check_crc(uchar *S, uchar *buf, uint32 len, uchar *IV)\r
+{\r
+ uint32 crc;\r
+ uchar *c;\r
+\r
+ crc = 0;\r
+ if (IV && !CMP(S, IV)) {\r
+ crc_update(&crc, ONE);\r
+ crc_update(&crc, ZERO);\r
+ }\r
+ for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {\r
+ if (!CMP(S, c)) {\r
+ crc_update(&crc, ONE);\r
+ crc_update(&crc, ZERO);\r
+ } else {\r
+ crc_update(&crc, ZERO);\r
+ crc_update(&crc, ZERO);\r
+ }\r
+ }\r
+ return (crc == 0);\r
+}\r
+\r
+/* Detect a crc32 compensation attack on a packet */\r
+int detect_attack(void *handle, uchar *buf, uint32 len, uchar *IV)\r
+{\r
+ struct crcda_ctx *ctx = (struct crcda_ctx *)handle;\r
+ register uint32 i, j;\r
+ uint32 l;\r
+ register uchar *c;\r
+ uchar *d;\r
+\r
+ assert(!(len > (SSH_MAXBLOCKS * SSH_BLOCKSIZE) ||\r
+ len % SSH_BLOCKSIZE != 0));\r
+ for (l = ctx->n; l < HASH_FACTOR(len / SSH_BLOCKSIZE); l = l << 2)\r
+ ;\r
+\r
+ if (ctx->h == NULL) {\r
+ ctx->n = l;\r
+ ctx->h = snewn(ctx->n, uint16);\r
+ } else {\r
+ if (l > ctx->n) {\r
+ ctx->n = l;\r
+ ctx->h = sresize(ctx->h, ctx->n, uint16);\r
+ }\r
+ }\r
+\r
+ if (len <= HASH_MINBLOCKS) {\r
+ for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {\r
+ if (IV && (!CMP(c, IV))) {\r
+ if ((check_crc(c, buf, len, IV)))\r
+ return 1; /* attack detected */\r
+ else\r
+ break;\r
+ }\r
+ for (d = buf; d < c; d += SSH_BLOCKSIZE) {\r
+ if (!CMP(c, d)) {\r
+ if ((check_crc(c, buf, len, IV)))\r
+ return 1; /* attack detected */\r
+ else\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ return 0; /* ok */\r
+ }\r
+ memset(ctx->h, HASH_UNUSEDCHAR, ctx->n * HASH_ENTRYSIZE);\r
+\r
+ if (IV)\r
+ ctx->h[HASH(IV) & (ctx->n - 1)] = HASH_IV;\r
+\r
+ for (c = buf, j = 0; c < (buf + len); c += SSH_BLOCKSIZE, j++) {\r
+ for (i = HASH(c) & (ctx->n - 1); ctx->h[i] != HASH_UNUSED;\r
+ i = (i + 1) & (ctx->n - 1)) {\r
+ if (ctx->h[i] == HASH_IV) {\r
+ if (!CMP(c, IV)) {\r
+ if (check_crc(c, buf, len, IV))\r
+ return 1; /* attack detected */\r
+ else\r
+ break;\r
+ }\r
+ } else if (!CMP(c, buf + ctx->h[i] * SSH_BLOCKSIZE)) {\r
+ if (check_crc(c, buf, len, IV))\r
+ return 1; /* attack detected */\r
+ else\r
+ break;\r
+ }\r
+ }\r
+ ctx->h[i] = j;\r
+ }\r
+ return 0; /* ok */\r
+}\r