No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / external / bsd / openssh / dist / deattack.c
blob8e368376b615eded459e53aea8c60fb130a2614c
1 /* $NetBSD$ */
2 /* $OpenBSD: deattack.c,v 1.30 2006/09/16 19:53:37 djm Exp $ */
3 /*
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
16 * SOFTWARE.
18 * Ariel Futoransky <futo@core-sdi.com>
19 * <http://www.core-sdi.com>
22 #include "includes.h"
23 __RCSID("$NetBSD: deattack.c,v 1.17 2009/02/16 20:53:54 christos Exp $");
24 #include <sys/types.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <time.h>
31 #include "xmalloc.h"
32 #include "deattack.h"
33 #include "log.h"
34 #include "crc32.h"
35 #include "misc.h"
38 * CRC attack detection has a worst-case behaviour that is O(N^3) over
39 * the number of identical blocks in a packet. This behaviour can be
40 * exploited to create a limited denial of service attack.
42 * However, because we are dealing with encrypted data, identical
43 * blocks should only occur every 2^35 maximally-sized packets or so.
44 * Consequently, we can detect this DoS by looking for identical blocks
45 * in a packet.
47 * The parameter below determines how many identical blocks we will
48 * accept in a single packet, trading off between attack detection and
49 * likelihood of terminating a legitimate connection. A value of 32
50 * corresponds to an average of 2^40 messages before an attack is
51 * misdetected
53 #define MAX_IDENTICAL 32
55 /* SSH Constants */
56 #define SSH_MAXBLOCKS (32 * 1024)
57 #define SSH_BLOCKSIZE (8)
59 /* Hashing constants */
60 #define HASH_MINSIZE (8 * 1024)
61 #define HASH_ENTRYSIZE (2)
62 #define HASH_FACTOR(x) ((x)*3/2)
63 #define HASH_UNUSEDCHAR (0xff)
64 #define HASH_UNUSED (0xffff)
65 #define HASH_IV (0xfffe)
67 #define HASH_MINBLOCKS (7*SSH_BLOCKSIZE)
70 /* Hash function (Input keys are cipher results) */
71 #define HASH(x) get_u32(x)
73 #define CMP(a, b) (memcmp(a, b, SSH_BLOCKSIZE))
75 static void
76 crc_update(u_int32_t *a, u_int32_t b)
78 b ^= *a;
79 *a = ssh_crc32((u_char *)&b, sizeof(b));
82 /* detect if a block is used in a particular pattern */
83 static int
84 check_crc(u_char *S, u_char *buf, u_int32_t len)
86 u_int32_t crc;
87 u_char *c;
89 crc = 0;
90 for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {
91 if (!CMP(S, c)) {
92 crc_update(&crc, 1);
93 crc_update(&crc, 0);
94 } else {
95 crc_update(&crc, 0);
96 crc_update(&crc, 0);
99 return (crc == 0);
103 /* Detect a crc32 compensation attack on a packet */
105 detect_attack(u_char *buf, u_int32_t len)
107 static u_int16_t *h = (u_int16_t *) NULL;
108 static u_int32_t n = HASH_MINSIZE / HASH_ENTRYSIZE;
109 u_int32_t i, j;
110 u_int32_t l, same;
111 u_char *c;
112 u_char *d;
114 if (len > (SSH_MAXBLOCKS * SSH_BLOCKSIZE) ||
115 len % SSH_BLOCKSIZE != 0) {
116 fatal("detect_attack: bad length %d", len);
118 for (l = n; l < HASH_FACTOR(len / SSH_BLOCKSIZE); l = l << 2)
121 if (h == NULL) {
122 debug("Installing crc compensation attack detector.");
123 h = (u_int16_t *) xcalloc(l, HASH_ENTRYSIZE);
124 n = l;
125 } else {
126 if (l > n) {
127 h = (u_int16_t *)xrealloc(h, l, HASH_ENTRYSIZE);
128 n = l;
132 if (len <= HASH_MINBLOCKS) {
133 for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {
134 for (d = buf; d < c; d += SSH_BLOCKSIZE) {
135 if (!CMP(c, d)) {
136 if ((check_crc(c, buf, len)))
137 return (DEATTACK_DETECTED);
138 else
139 break;
143 return (DEATTACK_OK);
145 memset(h, HASH_UNUSEDCHAR, n * HASH_ENTRYSIZE);
147 for (c = buf, same = j = 0; c < (buf + len); c += SSH_BLOCKSIZE, j++) {
148 for (i = HASH(c) & (n - 1); h[i] != HASH_UNUSED;
149 i = (i + 1) & (n - 1)) {
150 if (!CMP(c, buf + h[i] * SSH_BLOCKSIZE)) {
151 if (++same > MAX_IDENTICAL)
152 return (DEATTACK_DOS_DETECTED);
153 if (check_crc(c, buf, len))
154 return (DEATTACK_DETECTED);
155 else
156 break;
159 h[i] = j;
161 return (DEATTACK_OK);