sshcrcda.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <[email protected]>
  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. #ifdef MPEXT
  40. static
  41. #endif
  42. uint8_t ONE[4] = { 1, 0, 0, 0 };
  43. #ifdef MPEXT
  44. static
  45. #endif
  46. uint8_t ZERO[4] = { 0, 0, 0, 0 };
  47. struct crcda_ctx {
  48. uint16_t *h;
  49. uint32_t n;
  50. };
  51. struct crcda_ctx *crcda_make_context(void)
  52. {
  53. struct crcda_ctx *ret = snew(struct crcda_ctx);
  54. ret->h = NULL;
  55. ret->n = HASH_MINSIZE / HASH_ENTRYSIZE;
  56. return ret;
  57. }
  58. void crcda_free_context(struct crcda_ctx *ctx)
  59. {
  60. if (ctx) {
  61. sfree(ctx->h);
  62. ctx->h = NULL;
  63. sfree(ctx);
  64. }
  65. }
  66. static void crc_update(uint32_t *a, void *b)
  67. {
  68. *a = crc32_update(*a, b, 4);
  69. }
  70. /* detect if a block is used in a particular pattern */
  71. static bool check_crc(uint8_t *S, uint8_t *buf, uint32_t len, uint8_t *IV)
  72. {
  73. uint32_t crc;
  74. uint8_t *c;
  75. crc = 0;
  76. if (IV && !CMP(S, IV)) {
  77. crc_update(&crc, ONE);
  78. crc_update(&crc, ZERO);
  79. }
  80. for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {
  81. if (!CMP(S, c)) {
  82. crc_update(&crc, ONE);
  83. crc_update(&crc, ZERO);
  84. } else {
  85. crc_update(&crc, ZERO);
  86. crc_update(&crc, ZERO);
  87. }
  88. }
  89. return (crc == 0);
  90. }
  91. /* Detect a crc32 compensation attack on a packet */
  92. bool detect_attack(
  93. struct crcda_ctx *ctx, uint8_t *buf, uint32_t len, uint8_t *IV)
  94. {
  95. register uint32_t i, j;
  96. uint32_t l;
  97. register uint8_t *c;
  98. uint8_t *d;
  99. assert(!(len > (SSH_MAXBLOCKS * SSH_BLOCKSIZE) ||
  100. len % SSH_BLOCKSIZE != 0));
  101. for (l = ctx->n; l < HASH_FACTOR(len / SSH_BLOCKSIZE); l = l << 2)
  102. ;
  103. if (ctx->h == NULL) {
  104. ctx->n = l;
  105. ctx->h = snewn(ctx->n, uint16_t);
  106. } else {
  107. if (l > ctx->n) {
  108. ctx->n = l;
  109. ctx->h = sresize(ctx->h, ctx->n, uint16_t);
  110. }
  111. }
  112. if (len <= HASH_MINBLOCKS) {
  113. for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {
  114. if (IV && (!CMP(c, IV))) {
  115. if ((check_crc(c, buf, len, IV)))
  116. return true; /* attack detected */
  117. else
  118. break;
  119. }
  120. for (d = buf; d < c; d += SSH_BLOCKSIZE) {
  121. if (!CMP(c, d)) {
  122. if ((check_crc(c, buf, len, IV)))
  123. return true; /* attack detected */
  124. else
  125. break;
  126. }
  127. }
  128. }
  129. return false; /* ok */
  130. }
  131. memset(ctx->h, HASH_UNUSEDCHAR, ctx->n * HASH_ENTRYSIZE);
  132. if (IV)
  133. ctx->h[HASH(IV) & (ctx->n - 1)] = HASH_IV;
  134. for (c = buf, j = 0; c < (buf + len); c += SSH_BLOCKSIZE, j++) {
  135. for (i = HASH(c) & (ctx->n - 1); ctx->h[i] != HASH_UNUSED;
  136. i = (i + 1) & (ctx->n - 1)) {
  137. if (ctx->h[i] == HASH_IV) {
  138. if (!CMP(c, IV)) {
  139. if (check_crc(c, buf, len, IV))
  140. return true; /* attack detected */
  141. else
  142. break;
  143. }
  144. } else if (!CMP(c, buf + ctx->h[i] * SSH_BLOCKSIZE)) {
  145. if (check_crc(c, buf, len, IV))
  146. return true; /* attack detected */
  147. else
  148. break;
  149. }
  150. }
  151. ctx->h[i] = j;
  152. }
  153. return false; /* ok */
  154. }