sha256.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file sha256.c
  4. /// \brief SHA-256
  5. ///
  6. /// \todo Crypto++ has x86 ASM optimizations. They use SSE so if they
  7. /// are imported to liblzma, SSE instructions need to be used
  8. /// conditionally to keep the code working on older boxes.
  9. //
  10. // This code is based on the code found from 7-Zip, which has a modified
  11. // version of the SHA-256 found from Crypto++ <http://www.cryptopp.com/>.
  12. // The code was modified a little to fit into liblzma.
  13. //
  14. // Authors: Kevin Springle
  15. // Wei Dai
  16. // Igor Pavlov
  17. // Lasse Collin
  18. //
  19. // This file has been put into the public domain.
  20. // You can do whatever you want with this file.
  21. //
  22. ///////////////////////////////////////////////////////////////////////////////
  23. #include "check.h"
  24. // Rotate a uint32_t. GCC can optimize this to a rotate instruction
  25. // at least on x86.
  26. static inline uint32_t
  27. rotr_32(uint32_t num, unsigned amount)
  28. {
  29. return (num >> amount) | (num << (32 - amount));
  30. }
  31. #define blk0(i) (W[i] = conv32be(data[i]))
  32. #define blk2(i) (W[i & 15] += s1(W[(i - 2) & 15]) + W[(i - 7) & 15] \
  33. + s0(W[(i - 15) & 15]))
  34. #define Ch(x, y, z) (z ^ (x & (y ^ z)))
  35. #define Maj(x, y, z) ((x & (y ^ z)) + (y & z))
  36. #define a(i) T[(0 - i) & 7]
  37. #define b(i) T[(1 - i) & 7]
  38. #define c(i) T[(2 - i) & 7]
  39. #define d(i) T[(3 - i) & 7]
  40. #define e(i) T[(4 - i) & 7]
  41. #define f(i) T[(5 - i) & 7]
  42. #define g(i) T[(6 - i) & 7]
  43. #define h(i) T[(7 - i) & 7]
  44. #define R(i, j, blk) \
  45. h(i) += S1(e(i)) + Ch(e(i), f(i), g(i)) + SHA256_K[i + j] + blk; \
  46. d(i) += h(i); \
  47. h(i) += S0(a(i)) + Maj(a(i), b(i), c(i))
  48. #define R0(i) R(i, 0, blk0(i))
  49. #define R2(i) R(i, j, blk2(i))
  50. #define S0(x) rotr_32(x ^ rotr_32(x ^ rotr_32(x, 9), 11), 2)
  51. #define S1(x) rotr_32(x ^ rotr_32(x ^ rotr_32(x, 14), 5), 6)
  52. #define s0(x) (rotr_32(x ^ rotr_32(x, 11), 7) ^ (x >> 3))
  53. #define s1(x) (rotr_32(x ^ rotr_32(x, 2), 17) ^ (x >> 10))
  54. static const uint32_t SHA256_K[64] = {
  55. 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
  56. 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
  57. 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
  58. 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
  59. 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
  60. 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
  61. 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
  62. 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
  63. 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
  64. 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
  65. 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
  66. 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
  67. 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
  68. 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
  69. 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
  70. 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
  71. };
  72. static void
  73. transform(uint32_t state[8], const uint32_t data[16])
  74. {
  75. uint32_t W[16];
  76. uint32_t T[8];
  77. // Copy state[] to working vars.
  78. memcpy(T, state, sizeof(T));
  79. // The first 16 operations unrolled
  80. R0( 0); R0( 1); R0( 2); R0( 3);
  81. R0( 4); R0( 5); R0( 6); R0( 7);
  82. R0( 8); R0( 9); R0(10); R0(11);
  83. R0(12); R0(13); R0(14); R0(15);
  84. // The remaining 48 operations partially unrolled
  85. for (unsigned int j = 16; j < 64; j += 16) {
  86. R2( 0); R2( 1); R2( 2); R2( 3);
  87. R2( 4); R2( 5); R2( 6); R2( 7);
  88. R2( 8); R2( 9); R2(10); R2(11);
  89. R2(12); R2(13); R2(14); R2(15);
  90. }
  91. // Add the working vars back into state[].
  92. state[0] += a(0);
  93. state[1] += b(0);
  94. state[2] += c(0);
  95. state[3] += d(0);
  96. state[4] += e(0);
  97. state[5] += f(0);
  98. state[6] += g(0);
  99. state[7] += h(0);
  100. }
  101. static void
  102. process(lzma_check_state *check)
  103. {
  104. transform(check->state.sha256.state, check->buffer.u32);
  105. return;
  106. }
  107. extern void
  108. lzma_sha256_init(lzma_check_state *check)
  109. {
  110. static const uint32_t s[8] = {
  111. 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
  112. 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
  113. };
  114. memcpy(check->state.sha256.state, s, sizeof(s));
  115. check->state.sha256.size = 0;
  116. return;
  117. }
  118. extern void
  119. lzma_sha256_update(const uint8_t *buf, size_t size, lzma_check_state *check)
  120. {
  121. // Copy the input data into a properly aligned temporary buffer.
  122. // This way we can be called with arbitrarily sized buffers
  123. // (no need to be multiple of 64 bytes), and the code works also
  124. // on architectures that don't allow unaligned memory access.
  125. while (size > 0) {
  126. const size_t copy_start = check->state.sha256.size & 0x3F;
  127. size_t copy_size = 64 - copy_start;
  128. if (copy_size > size)
  129. copy_size = size;
  130. memcpy(check->buffer.u8 + copy_start, buf, copy_size);
  131. buf += copy_size;
  132. size -= copy_size;
  133. check->state.sha256.size += copy_size;
  134. if ((check->state.sha256.size & 0x3F) == 0)
  135. process(check);
  136. }
  137. return;
  138. }
  139. extern void
  140. lzma_sha256_finish(lzma_check_state *check)
  141. {
  142. // Add padding as described in RFC 3174 (it describes SHA-1 but
  143. // the same padding style is used for SHA-256 too).
  144. size_t pos = check->state.sha256.size & 0x3F;
  145. check->buffer.u8[pos++] = 0x80;
  146. while (pos != 64 - 8) {
  147. if (pos == 64) {
  148. process(check);
  149. pos = 0;
  150. }
  151. check->buffer.u8[pos++] = 0x00;
  152. }
  153. // Convert the message size from bytes to bits.
  154. check->state.sha256.size *= 8;
  155. check->buffer.u64[(64 - 8) / 8] = conv64be(check->state.sha256.size);
  156. process(check);
  157. for (size_t i = 0; i < 8; ++i)
  158. check->buffer.u32[i] = conv32be(check->state.sha256.state[i]);
  159. return;
  160. }