1 /* $OpenBSD: deattack.c,v 1.14 2001/06/23 15:12:18 itojun Exp $ */
4 * Cryptographic attack detector for ssh - source code
6 * Copyright (c) 1998 CORE SDI S.A., Buenos Aires, Argentina.
8 * All rights reserved. Redistribution and use in source and binary
9 * forms, with or without modification, are permitted provided that
10 * this copyright notice is retained.
12 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
13 * WARRANTIES ARE DISCLAIMED. IN NO EVENT SHALL CORE SDI S.A. BE
14 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR
15 * CONSEQUENTIAL DAMAGES RESULTING FROM THE USE OR MISUSE OF THIS
18 * Ariel Futoransky <futo@core-sdi.com>
19 * <http://www.core-sdi.com>
21 * Modified for use in PuTTY by Simon Tatham
28 typedef unsigned char uchar;
29 typedef unsigned short uint16;
32 #define SSH_MAXBLOCKS (32 * 1024)
33 #define SSH_BLOCKSIZE (8)
35 /* Hashing constants */
36 #define HASH_MINSIZE (8 * 1024)
37 #define HASH_ENTRYSIZE (sizeof(uint16))
38 #define HASH_FACTOR(x) ((x)*3/2)
39 #define HASH_UNUSEDCHAR (0xff)
40 #define HASH_UNUSED (0xffff)
41 #define HASH_IV (0xfffe)
43 #define HASH_MINBLOCKS (7*SSH_BLOCKSIZE)
45 #define GET_32BIT_MSB_FIRST(cp) \
46 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
47 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
48 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
49 ((unsigned long)(unsigned char)(cp)[3]))
51 /* Hash function (Input keys are cipher results) */
52 #define HASH(x) GET_32BIT_MSB_FIRST(x)
54 #define CMP(a, b) (memcmp(a, b, SSH_BLOCKSIZE))
56 uchar ONE[4] = { 1, 0, 0, 0 };
57 uchar ZERO[4] = { 0, 0, 0, 0 };
64 void *crcda_make_context(void)
66 struct crcda_ctx *ret = snew(struct crcda_ctx);
68 ret->n = HASH_MINSIZE / HASH_ENTRYSIZE;
72 void crcda_free_context(void *handle)
74 struct crcda_ctx *ctx = (struct crcda_ctx *)handle;
82 static void crc_update(uint32 *a, void *b)
84 *a = crc32_update(*a, b, 4);
87 /* detect if a block is used in a particular pattern */
88 static int check_crc(uchar *S, uchar *buf, uint32 len, uchar *IV)
94 if (IV && !CMP(S, IV)) {
95 crc_update(&crc, ONE);
96 crc_update(&crc, ZERO);
98 for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {
100 crc_update(&crc, ONE);
101 crc_update(&crc, ZERO);
103 crc_update(&crc, ZERO);
104 crc_update(&crc, ZERO);
110 /* Detect a crc32 compensation attack on a packet */
111 int detect_attack(void *handle, uchar *buf, uint32 len, uchar *IV)
113 struct crcda_ctx *ctx = (struct crcda_ctx *)handle;
114 register uint32 i, j;
119 assert(!(len > (SSH_MAXBLOCKS * SSH_BLOCKSIZE) ||
120 len % SSH_BLOCKSIZE != 0));
121 for (l = ctx->n; l < HASH_FACTOR(len / SSH_BLOCKSIZE); l = l << 2)
124 if (ctx->h == NULL) {
126 ctx->h = snewn(ctx->n, uint16);
130 ctx->h = sresize(ctx->h, ctx->n, uint16);
134 if (len <= HASH_MINBLOCKS) {
135 for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {
136 if (IV && (!CMP(c, IV))) {
137 if ((check_crc(c, buf, len, IV)))
138 return 1; /* attack detected */
142 for (d = buf; d < c; d += SSH_BLOCKSIZE) {
144 if ((check_crc(c, buf, len, IV)))
145 return 1; /* attack detected */
153 memset(ctx->h, HASH_UNUSEDCHAR, ctx->n * HASH_ENTRYSIZE);
156 ctx->h[HASH(IV) & (ctx->n - 1)] = HASH_IV;
158 for (c = buf, j = 0; c < (buf + len); c += SSH_BLOCKSIZE, j++) {
159 for (i = HASH(c) & (ctx->n - 1); ctx->h[i] != HASH_UNUSED;
160 i = (i + 1) & (ctx->n - 1)) {
161 if (ctx->h[i] == HASH_IV) {
163 if (check_crc(c, buf, len, IV))
164 return 1; /* attack detected */
168 } else if (!CMP(c, buf + ctx->h[i] * SSH_BLOCKSIZE)) {
169 if (check_crc(c, buf, len, IV))
170 return 1; /* attack detected */