sshcrcda.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. typedef unsigned char uchar;
  26. typedef unsigned short uint16;
  27. /* SSH Constants */
  28. #define SSH_MAXBLOCKS (32 * 1024)
  29. #define SSH_BLOCKSIZE (8)
  30. /* Hashing constants */
  31. #define HASH_MINSIZE (8 * 1024)
  32. #define HASH_ENTRYSIZE (sizeof(uint16))
  33. #define HASH_FACTOR(x) ((x)*3/2)
  34. #define HASH_UNUSEDCHAR (0xff)
  35. #define HASH_UNUSED (0xffff)
  36. #define HASH_IV (0xfffe)
  37. #define HASH_MINBLOCKS (7*SSH_BLOCKSIZE)
  38. /* Hash function (Input keys are cipher results) */
  39. #define HASH(x) GET_32BIT_MSB_FIRST(x)
  40. #define CMP(a, b) (memcmp(a, b, SSH_BLOCKSIZE))
  41. #ifdef MPEXT
  42. static
  43. #endif
  44. uchar ONE[4] = { 1, 0, 0, 0 };
  45. #ifdef MPEXT
  46. static
  47. #endif
  48. uchar ZERO[4] = { 0, 0, 0, 0 };
  49. struct crcda_ctx {
  50. uint16 *h;
  51. uint32 n;
  52. };
  53. void *crcda_make_context(void)
  54. {
  55. struct crcda_ctx *ret = snew(struct crcda_ctx);
  56. ret->h = NULL;
  57. ret->n = HASH_MINSIZE / HASH_ENTRYSIZE;
  58. return ret;
  59. }
  60. void crcda_free_context(void *handle)
  61. {
  62. struct crcda_ctx *ctx = (struct crcda_ctx *)handle;
  63. if (ctx) {
  64. sfree(ctx->h);
  65. ctx->h = NULL;
  66. sfree(ctx);
  67. }
  68. }
  69. static void crc_update(uint32 *a, void *b)
  70. {
  71. *a = crc32_update(*a, b, 4);
  72. }
  73. /* detect if a block is used in a particular pattern */
  74. static int check_crc(uchar *S, uchar *buf, uint32 len, uchar *IV)
  75. {
  76. uint32 crc;
  77. uchar *c;
  78. crc = 0;
  79. if (IV && !CMP(S, IV)) {
  80. crc_update(&crc, ONE);
  81. crc_update(&crc, ZERO);
  82. }
  83. for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {
  84. if (!CMP(S, c)) {
  85. crc_update(&crc, ONE);
  86. crc_update(&crc, ZERO);
  87. } else {
  88. crc_update(&crc, ZERO);
  89. crc_update(&crc, ZERO);
  90. }
  91. }
  92. return (crc == 0);
  93. }
  94. /* Detect a crc32 compensation attack on a packet */
  95. int detect_attack(void *handle, uchar *buf, uint32 len, uchar *IV)
  96. {
  97. struct crcda_ctx *ctx = (struct crcda_ctx *)handle;
  98. register uint32 i, j;
  99. uint32 l;
  100. register uchar *c;
  101. uchar *d;
  102. assert(!(len > (SSH_MAXBLOCKS * SSH_BLOCKSIZE) ||
  103. len % SSH_BLOCKSIZE != 0));
  104. for (l = ctx->n; l < HASH_FACTOR(len / SSH_BLOCKSIZE); l = l << 2)
  105. ;
  106. if (ctx->h == NULL) {
  107. ctx->n = l;
  108. ctx->h = snewn(ctx->n, uint16);
  109. } else {
  110. if (l > ctx->n) {
  111. ctx->n = l;
  112. ctx->h = sresize(ctx->h, ctx->n, uint16);
  113. }
  114. }
  115. if (len <= HASH_MINBLOCKS) {
  116. for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {
  117. if (IV && (!CMP(c, IV))) {
  118. if ((check_crc(c, buf, len, IV)))
  119. return 1; /* attack detected */
  120. else
  121. break;
  122. }
  123. for (d = buf; d < c; d += SSH_BLOCKSIZE) {
  124. if (!CMP(c, d)) {
  125. if ((check_crc(c, buf, len, IV)))
  126. return 1; /* attack detected */
  127. else
  128. break;
  129. }
  130. }
  131. }
  132. return 0; /* ok */
  133. }
  134. memset(ctx->h, HASH_UNUSEDCHAR, ctx->n * HASH_ENTRYSIZE);
  135. if (IV)
  136. ctx->h[HASH(IV) & (ctx->n - 1)] = HASH_IV;
  137. for (c = buf, j = 0; c < (buf + len); c += SSH_BLOCKSIZE, j++) {
  138. for (i = HASH(c) & (ctx->n - 1); ctx->h[i] != HASH_UNUSED;
  139. i = (i + 1) & (ctx->n - 1)) {
  140. if (ctx->h[i] == HASH_IV) {
  141. if (!CMP(c, IV)) {
  142. if (check_crc(c, buf, len, IV))
  143. return 1; /* attack detected */
  144. else
  145. break;
  146. }
  147. } else if (!CMP(c, buf + ctx->h[i] * SSH_BLOCKSIZE)) {
  148. if (check_crc(c, buf, len, IV))
  149. return 1; /* attack detected */
  150. else
  151. break;
  152. }
  153. }
  154. ctx->h[i] = j;
  155. }
  156. return 0; /* ok */
  157. }