1 /* $OpenBSD: deattack.c,v 1.32 2015/01/20 23:14:00 deraadt Exp $ */
3 * Cryptographic attack detector for ssh - source code
5 * Copyright (c) 1998 CORE SDI S.A., Buenos Aires, Argentina.
7 * All rights reserved. Redistribution and use in source and binary
8 * forms, with or without modification, are permitted provided that
9 * this copyright notice is retained.
11 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
12 * WARRANTIES ARE DISCLAIMED. IN NO EVENT SHALL CORE SDI S.A. BE
13 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR
14 * CONSEQUENTIAL DAMAGES RESULTING FROM THE USE OR MISUSE OF THIS
17 * Ariel Futoransky <futo@core-sdi.com>
18 * <http://www.core-sdi.com>
33 * CRC attack detection has a worst-case behaviour that is O(N^3) over
34 * the number of identical blocks in a packet. This behaviour can be
35 * exploited to create a limited denial of service attack.
37 * However, because we are dealing with encrypted data, identical
38 * blocks should only occur every 2^35 maximally-sized packets or so.
39 * Consequently, we can detect this DoS by looking for identical blocks
42 * The parameter below determines how many identical blocks we will
43 * accept in a single packet, trading off between attack detection and
44 * likelihood of terminating a legitimate connection. A value of 32
45 * corresponds to an average of 2^40 messages before an attack is
48 #define MAX_IDENTICAL 32
51 #define SSH_MAXBLOCKS (32 * 1024)
52 #define SSH_BLOCKSIZE (8)
54 /* Hashing constants */
55 #define HASH_MINSIZE (8 * 1024)
56 #define HASH_ENTRYSIZE (2)
57 #define HASH_FACTOR(x) ((x)*3/2)
58 #define HASH_UNUSEDCHAR (0xff)
59 #define HASH_UNUSED (0xffff)
60 #define HASH_IV (0xfffe)
62 #define HASH_MINBLOCKS (7*SSH_BLOCKSIZE)
65 /* Hash function (Input keys are cipher results) */
66 #define HASH(x) PEEK_U32(x)
68 #define CMP(a, b) (memcmp(a, b, SSH_BLOCKSIZE))
71 crc_update(u_int32_t *a, u_int32_t b)
74 *a = ssh_crc32((u_char *)&b, sizeof(b));
77 /* detect if a block is used in a particular pattern */
79 check_crc(const u_char *S, const u_char *buf, u_int32_t len)
85 for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {
98 deattack_init(struct deattack_ctx *dctx)
100 bzero(dctx, sizeof(*dctx));
101 dctx->n = HASH_MINSIZE / HASH_ENTRYSIZE;
104 /* Detect a crc32 compensation attack on a packet */
106 detect_attack(struct deattack_ctx *dctx, const u_char *buf, u_int32_t len)
108 u_int32_t i, j, l, same;
112 if (len > (SSH_MAXBLOCKS * SSH_BLOCKSIZE) ||
113 len % SSH_BLOCKSIZE != 0)
114 return DEATTACK_ERROR;
115 for (l = dctx->n; l < HASH_FACTOR(len / SSH_BLOCKSIZE); l = l << 2)
118 if (dctx->h == NULL) {
119 if ((dctx->h = calloc(l, HASH_ENTRYSIZE)) == NULL)
120 return DEATTACK_ERROR;
124 if ((tmp = reallocarray(dctx->h, l, HASH_ENTRYSIZE))
128 return DEATTACK_ERROR;
135 if (len <= HASH_MINBLOCKS) {
136 for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {
137 for (d = buf; d < c; d += SSH_BLOCKSIZE) {
139 if ((check_crc(c, buf, len)))
140 return DEATTACK_DETECTED;
148 memset(dctx->h, HASH_UNUSEDCHAR, dctx->n * HASH_ENTRYSIZE);
150 for (c = buf, same = j = 0; c < (buf + len); c += SSH_BLOCKSIZE, j++) {
151 for (i = HASH(c) & (dctx->n - 1); dctx->h[i] != HASH_UNUSED;
152 i = (i + 1) & (dctx->n - 1)) {
153 if (!CMP(c, buf + dctx->h[i] * SSH_BLOCKSIZE)) {
154 if (++same > MAX_IDENTICAL)
155 return DEATTACK_DOS_DETECTED;
156 if (check_crc(c, buf, len))
157 return DEATTACK_DETECTED;