crc32.c 4.3 KB

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