crc-attack-detector.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* $OpenBSD: deattack.c,v 1.14 2001/06/23 15:12:18 itojun Exp $ */
  2. /*
  3. * 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>
  18. * <http://www.core-sdi.com>
  19. *
  20. * Modified for use in PuTTY by Simon Tatham
  21. */
  22. #include <assert.h>
  23. #include "misc.h"
  24. #include "ssh.h"
  25. /* SSH Constants */
  26. #define SSH_MAXBLOCKS (32 * 1024)
  27. #define SSH_BLOCKSIZE (8)
  28. /* Hashing constants */
  29. #define HASH_MINSIZE (8 * 1024)
  30. #define HASH_ENTRYSIZE (sizeof(uint16_t))
  31. #define HASH_FACTOR(x) ((x)*3/2)
  32. #define HASH_UNUSEDCHAR (0xff)
  33. #define HASH_UNUSED (0xffff)
  34. #define HASH_IV (0xfffe)
  35. #define HASH_MINBLOCKS (7*SSH_BLOCKSIZE)
  36. /* Hash function (Input keys are cipher results) */
  37. #define HASH(x) GET_32BIT_MSB_FIRST(x)
  38. #define CMP(a, b) (memcmp(a, b, SSH_BLOCKSIZE))
  39. static const uint8_t ONE[4] = { 1, 0, 0, 0 };
  40. static const uint8_t ZERO[4] = { 0, 0, 0, 0 };
  41. struct crcda_ctx {
  42. uint16_t *h;
  43. uint32_t n;
  44. };
  45. struct crcda_ctx *crcda_make_context(void)
  46. {
  47. struct crcda_ctx *ctx = snew(struct crcda_ctx);
  48. ctx->h = NULL;
  49. ctx->n = HASH_MINSIZE / HASH_ENTRYSIZE;
  50. return ctx;
  51. }
  52. void crcda_free_context(struct crcda_ctx *ctx)
  53. {
  54. if (ctx) {
  55. sfree(ctx->h);
  56. ctx->h = NULL;
  57. sfree(ctx);
  58. }
  59. }
  60. static void crc_update(uint32_t *a, const void *b)
  61. {
  62. *a = crc32_update(*a, make_ptrlen(b, 4));
  63. }
  64. /* detect if a block is used in a particular pattern */
  65. static bool check_crc(const uint8_t *S, const uint8_t *buf,
  66. uint32_t len, const uint8_t *IV)
  67. {
  68. uint32_t crc;
  69. const uint8_t *c;
  70. crc = 0;
  71. if (IV && !CMP(S, IV)) {
  72. crc_update(&crc, ONE);
  73. crc_update(&crc, ZERO);
  74. }
  75. for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {
  76. if (!CMP(S, c)) {
  77. crc_update(&crc, ONE);
  78. crc_update(&crc, ZERO);
  79. } else {
  80. crc_update(&crc, ZERO);
  81. crc_update(&crc, ZERO);
  82. }
  83. }
  84. return (crc == 0);
  85. }
  86. /* Detect a crc32 compensation attack on a packet */
  87. bool detect_attack(struct crcda_ctx *ctx,
  88. const unsigned char *buf, uint32_t len,
  89. const unsigned char *IV)
  90. {
  91. register uint32_t i, j;
  92. uint32_t l;
  93. register const uint8_t *c;
  94. const uint8_t *d;
  95. assert(!(len > (SSH_MAXBLOCKS * SSH_BLOCKSIZE) ||
  96. len % SSH_BLOCKSIZE != 0));
  97. for (l = ctx->n; l < HASH_FACTOR(len / SSH_BLOCKSIZE); l = l << 2)
  98. ;
  99. if (ctx->h == NULL) {
  100. ctx->n = l;
  101. ctx->h = snewn(ctx->n, uint16_t);
  102. } else {
  103. if (l > ctx->n) {
  104. ctx->n = l;
  105. ctx->h = sresize(ctx->h, ctx->n, uint16_t);
  106. }
  107. }
  108. if (len <= HASH_MINBLOCKS) {
  109. for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {
  110. if (IV && (!CMP(c, IV))) {
  111. if ((check_crc(c, buf, len, IV)))
  112. return true; /* attack detected */
  113. else
  114. break;
  115. }
  116. for (d = buf; d < c; d += SSH_BLOCKSIZE) {
  117. if (!CMP(c, d)) {
  118. if ((check_crc(c, buf, len, IV)))
  119. return true; /* attack detected */
  120. else
  121. break;
  122. }
  123. }
  124. }
  125. return false; /* ok */
  126. }
  127. memset(ctx->h, HASH_UNUSEDCHAR, ctx->n * HASH_ENTRYSIZE);
  128. if (IV)
  129. ctx->h[HASH(IV) & (ctx->n - 1)] = HASH_IV;
  130. for (c = buf, j = 0; c < (buf + len); c += SSH_BLOCKSIZE, j++) {
  131. for (i = HASH(c) & (ctx->n - 1); ctx->h[i] != HASH_UNUSED;
  132. i = (i + 1) & (ctx->n - 1)) {
  133. if (ctx->h[i] == HASH_IV) {
  134. assert(IV); /* or we wouldn't have stored HASH_IV above */
  135. if (!CMP(c, IV)) {
  136. if (check_crc(c, buf, len, IV))
  137. return true; /* attack detected */
  138. else
  139. break;
  140. }
  141. } else if (!CMP(c, buf + ctx->h[i] * SSH_BLOCKSIZE)) {
  142. if (check_crc(c, buf, len, IV))
  143. return true; /* attack detected */
  144. else
  145. break;
  146. }
  147. }
  148. ctx->h[i] = j;
  149. }
  150. return false; /* ok */
  151. }