blob: 57a747da50388508ba060da5d80193657ac85830 [file] [log] [blame]
Damien Millere3476ed2006-07-24 14:13:33 +10001/* $OpenBSD: deattack.c,v 1.28 2006/07/22 20:48:23 stevesk Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003 * Cryptographic attack detector for ssh - source code
4 *
5 * Copyright (c) 1998 CORE SDI S.A., Buenos Aires, Argentina.
6 *
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.
10 *
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
15 * SOFTWARE.
16 *
17 * Ariel Futoransky <futo@core-sdi.com>
Damien Miller95def091999-11-25 00:26:21 +110018 * <http://www.core-sdi.com>
19 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020
21#include "includes.h"
Ben Lindstrom05764b92002-03-05 01:53:02 +000022
Damien Millere3476ed2006-07-24 14:13:33 +100023#include <string.h>
24
Damien Millerd4a8b7e1999-10-27 13:42:43 +100025#include "deattack.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000026#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027#include "crc32.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100028#include "xmalloc.h"
Damien Miller3f941882006-03-31 23:13:02 +110029#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100030
31/* SSH Constants */
Damien Miller95def091999-11-25 00:26:21 +110032#define SSH_MAXBLOCKS (32 * 1024)
33#define SSH_BLOCKSIZE (8)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034
35/* Hashing constants */
Damien Miller95def091999-11-25 00:26:21 +110036#define HASH_MINSIZE (8 * 1024)
37#define HASH_ENTRYSIZE (2)
38#define HASH_FACTOR(x) ((x)*3/2)
39#define HASH_UNUSEDCHAR (0xff)
40#define HASH_UNUSED (0xffff)
Damien Miller9f0f5c62001-12-21 14:45:46 +110041#define HASH_IV (0xfffe)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
Damien Miller95def091999-11-25 00:26:21 +110043#define HASH_MINBLOCKS (7*SSH_BLOCKSIZE)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
45
46/* Hash function (Input keys are cipher results) */
Damien Miller3f941882006-03-31 23:13:02 +110047#define HASH(x) get_u32(x)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048
Ben Lindstrom204e4882001-03-05 06:47:00 +000049#define CMP(a, b) (memcmp(a, b, SSH_BLOCKSIZE))
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050
Ben Lindstrombba81212001-06-25 05:01:22 +000051static void
Damien Miller95def091999-11-25 00:26:21 +110052crc_update(u_int32_t *a, u_int32_t b)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053{
Damien Miller95def091999-11-25 00:26:21 +110054 b ^= *a;
Damien Miller2dbbf8e2006-03-26 00:11:46 +110055 *a = ssh_crc32((u_char *)&b, sizeof(b));
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056}
57
Damien Miller95def091999-11-25 00:26:21 +110058/* detect if a block is used in a particular pattern */
Ben Lindstrombba81212001-06-25 05:01:22 +000059static int
Damien Miller2dbbf8e2006-03-26 00:11:46 +110060check_crc(u_char *S, u_char *buf, u_int32_t len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061{
Damien Miller95def091999-11-25 00:26:21 +110062 u_int32_t crc;
Ben Lindstrom46c16222000-12-22 01:43:59 +000063 u_char *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064
Damien Miller95def091999-11-25 00:26:21 +110065 crc = 0;
Damien Miller95def091999-11-25 00:26:21 +110066 for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {
67 if (!CMP(S, c)) {
68 crc_update(&crc, 1);
69 crc_update(&crc, 0);
70 } else {
71 crc_update(&crc, 0);
72 crc_update(&crc, 0);
73 }
74 }
75 return (crc == 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076}
77
78
Damien Miller95def091999-11-25 00:26:21 +110079/* Detect a crc32 compensation attack on a packet */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080int
Damien Miller2dbbf8e2006-03-26 00:11:46 +110081detect_attack(u_char *buf, u_int32_t len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082{
Damien Miller95def091999-11-25 00:26:21 +110083 static u_int16_t *h = (u_int16_t *) NULL;
Ben Lindstromf2de06b2000-11-05 05:42:36 +000084 static u_int32_t n = HASH_MINSIZE / HASH_ENTRYSIZE;
Ben Lindstrom70a290c2001-12-06 16:39:56 +000085 u_int32_t i, j;
Damien Miller95def091999-11-25 00:26:21 +110086 u_int32_t l;
Ben Lindstrom70a290c2001-12-06 16:39:56 +000087 u_char *c;
Ben Lindstrom46c16222000-12-22 01:43:59 +000088 u_char *d;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Damien Miller95def091999-11-25 00:26:21 +110090 if (len > (SSH_MAXBLOCKS * SSH_BLOCKSIZE) ||
91 len % SSH_BLOCKSIZE != 0) {
92 fatal("detect_attack: bad length %d", len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093 }
Damien Miller95def091999-11-25 00:26:21 +110094 for (l = n; l < HASH_FACTOR(len / SSH_BLOCKSIZE); l = l << 2)
95 ;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
Damien Miller95def091999-11-25 00:26:21 +110097 if (h == NULL) {
98 debug("Installing crc compensation attack detector.");
Damien Miller07d86be2006-03-26 14:19:21 +110099 h = (u_int16_t *) xcalloc(l, HASH_ENTRYSIZE);
Damien Miller95def091999-11-25 00:26:21 +1100100 n = l;
Damien Miller95def091999-11-25 00:26:21 +1100101 } else {
102 if (l > n) {
Damien Miller36812092006-03-26 14:22:47 +1100103 h = (u_int16_t *)xrealloc(h, l, HASH_ENTRYSIZE);
Damien Miller95def091999-11-25 00:26:21 +1100104 n = l;
Damien Miller95def091999-11-25 00:26:21 +1100105 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
Damien Miller95def091999-11-25 00:26:21 +1100108 if (len <= HASH_MINBLOCKS) {
109 for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {
Damien Miller95def091999-11-25 00:26:21 +1100110 for (d = buf; d < c; d += SSH_BLOCKSIZE) {
111 if (!CMP(c, d)) {
Damien Miller2dbbf8e2006-03-26 00:11:46 +1100112 if ((check_crc(c, buf, len)))
Damien Miller95def091999-11-25 00:26:21 +1100113 return (DEATTACK_DETECTED);
114 else
115 break;
116 }
117 }
118 }
119 return (DEATTACK_OK);
120 }
121 memset(h, HASH_UNUSEDCHAR, n * HASH_ENTRYSIZE);
122
Damien Miller95def091999-11-25 00:26:21 +1100123 for (c = buf, j = 0; c < (buf + len); c += SSH_BLOCKSIZE, j++) {
124 for (i = HASH(c) & (n - 1); h[i] != HASH_UNUSED;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100125 i = (i + 1) & (n - 1)) {
Damien Miller2dbbf8e2006-03-26 00:11:46 +1100126 if (!CMP(c, buf + h[i] * SSH_BLOCKSIZE)) {
127 if (check_crc(c, buf, len))
Damien Miller95def091999-11-25 00:26:21 +1100128 return (DEATTACK_DETECTED);
129 else
130 break;
131 }
132 }
133 h[i] = j;
134 }
135 return (DEATTACK_OK);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136}