sshcrc.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * CRC32 implementation, as used in SSH-1.
  3. *
  4. * This particular form of the CRC uses the polynomial
  5. * P(x) = x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x^1+1
  6. * and represents polynomials in bit-reversed form, so that the x^0
  7. * coefficient (constant term) appears in the bit with place value
  8. * 2^31, and the x^31 coefficient in the bit with place value 2^0. In
  9. * this representation, (x^32 mod P) = 0xEDB88320, so multiplying the
  10. * current state by x is done by shifting right by one bit, and XORing
  11. * that constant into the result if the bit shifted out was 1.
  12. *
  13. * There's a bewildering array of subtly different variants of CRC out
  14. * there, using different polynomials, both bit orders, and varying
  15. * the start and end conditions. There are catalogue websites such as
  16. * http://reveng.sourceforge.net/crc-catalogue/ , which generally seem
  17. * to have the convention of indexing CRCs by their 'check value',
  18. * defined as whatever you get if you hash the 9-byte test string
  19. * "123456789".
  20. *
  21. * The crc32_rfc1662() function below, which starts off the CRC state
  22. * at 0xFFFFFFFF and complements it after feeding all the data, gives
  23. * the check value 0xCBF43926, and matches the hash function that the
  24. * above catalogue refers to as "CRC-32/ISO-HDLC"; among other things,
  25. * it's also the "FCS-32" checksum described in RFC 1662 section C.3
  26. * (hence the name I've given it here).
  27. *
  28. * The crc32_ssh1() function implements the variant form used by
  29. * SSH-1, which uses the same update function, but starts the state at
  30. * zero and doesn't complement it at the end of the computation. The
  31. * check value for that version is 0x2DFD2D88, which that CRC
  32. * catalogue doesn't list at all.
  33. */
  34. #include <stdint.h>
  35. #include <stdlib.h>
  36. #include "ssh.h"
  37. /*
  38. * Multiply a CRC value by x^4. This implementation strategy avoids
  39. * using a lookup table (which would be a side-channel hazard, since
  40. * SSH-1 applies this CRC to decrypted session data).
  41. *
  42. * The basic idea is that you'd like to "multiply" the shifted-out 4
  43. * bits by the CRC polynomial value 0xEDB88320, or rather by that
  44. * value shifted right 3 bits (since you want the _last_ bit shifted
  45. * out, i.e. the one originally at the 2^3 position, to generate
  46. * 0xEDB88320 itself). But the scare-quoted "multiply" would have to
  47. * be a multiplication of polynomials over GF(2), which differs from
  48. * integer multiplication in that you don't have any carries. In other
  49. * words, you make a copy of one input shifted left by the index of
  50. * each set bit in the other, so that adding them all together would
  51. * give you the ordinary integer product, and then you XOR them
  52. * together instead.
  53. *
  54. * With a 4-bit multiplier, the two kinds of multiplication coincide
  55. * provided the multiplicand has no two set bits at positions
  56. * differing by less than 4, because then no two copies of the
  57. * multiplier can overlap to generate a carry. So I break up the
  58. * intended multiplicand K = 0xEDB88320 >> 3 into three sub-constants
  59. * a,b,c with that property, such that a^b^c = K. Then I can multiply
  60. * m by each of them separately, and XOR together the results.
  61. */
  62. static inline uint32_t crc32_shift_4(uint32_t v)
  63. {
  64. const uint32_t a = 0x11111044, b = 0x08840020, c = 0x04220000;
  65. uint32_t m = v & 0xF;
  66. return (v >> 4) ^ (a*m) ^ (b*m) ^ (c*m);
  67. }
  68. /*
  69. * The 8-bit shift you need every time you absorb an input byte,
  70. * implemented simply by iterating the 4-bit shift twice.
  71. */
  72. static inline uint32_t crc32_shift_8(uint32_t v)
  73. {
  74. return crc32_shift_4(crc32_shift_4(v));
  75. }
  76. /*
  77. * Update an existing hash value with extra bytes of data.
  78. */
  79. uint32_t crc32_update(uint32_t crc, ptrlen data)
  80. {
  81. const uint8_t *p = (const uint8_t *)data.ptr;
  82. for (size_t len = data.len; len-- > 0 ;)
  83. crc = crc32_shift_8(crc ^ *p++);
  84. return crc;
  85. }
  86. /*
  87. * The SSH-1 variant of CRC-32.
  88. */
  89. uint32_t crc32_ssh1(ptrlen data)
  90. {
  91. return crc32_update(0, data);
  92. }
  93. /*
  94. * The official version of CRC-32. Nothing in PuTTY proper uses this,
  95. * but it's useful to expose it to testcrypt so that we can implement
  96. * standard test vectors.
  97. */
  98. uint32_t crc32_rfc1662(ptrlen data)
  99. {
  100. return crc32_update(0xFFFFFFFF, data) ^ 0xFFFFFFFF;
  101. }