The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
8.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. struct SHA256Processor
  16. {
  17. // expects 64 bytes of data
  18. void processFullBlock (const void* data) noexcept
  19. {
  20. const uint32_t constants[] =
  21. {
  22. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  23. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  24. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  25. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  26. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  27. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  28. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  29. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  30. };
  31. uint32_t block[16], s[8];
  32. memcpy (s, state, sizeof (s));
  33. auto d = static_cast<const uint8_t*> (data);
  34. for (auto& b : block)
  35. {
  36. b = (uint32_t (d[0]) << 24) | (uint32_t (d[1]) << 16) | (uint32_t (d[2]) << 8) | d[3];
  37. d += 4;
  38. }
  39. auto convolve = [&] (uint32_t i, uint32_t j)
  40. {
  41. s[(7 - i) & 7] += S1 (s[(4 - i) & 7]) + ch (s[(4 - i) & 7], s[(5 - i) & 7], s[(6 - i) & 7]) + constants[i + j]
  42. + (j != 0 ? (block[i & 15] += s1 (block[(i - 2) & 15]) + block[(i - 7) & 15] + s0 (block[(i - 15) & 15]))
  43. : block[i]);
  44. s[(3 - i) & 7] += s[(7 - i) & 7];
  45. s[(7 - i) & 7] += S0 (s[(0 - i) & 7]) + maj (s[(0 - i) & 7], s[(1 - i) & 7], s[(2 - i) & 7]);
  46. };
  47. for (uint32_t j = 0; j < 64; j += 16)
  48. for (uint32_t i = 0; i < 16; ++i)
  49. convolve (i, j);
  50. for (int i = 0; i < 8; ++i)
  51. state[i] += s[i];
  52. length += 64;
  53. }
  54. void processFinalBlock (const void* data, uint32_t numBytes) noexcept
  55. {
  56. jassert (numBytes < 64);
  57. length += numBytes;
  58. length *= 8; // (the length is stored as a count of bits, not bytes)
  59. uint8_t finalBlocks[128];
  60. memcpy (finalBlocks, data, numBytes);
  61. finalBlocks[numBytes++] = 128; // append a '1' bit
  62. while (numBytes != 56 && numBytes < 64 + 56)
  63. finalBlocks[numBytes++] = 0; // pad with zeros..
  64. for (int i = 8; --i >= 0;)
  65. finalBlocks[numBytes++] = (uint8_t) (length >> (i * 8)); // append the length.
  66. jassert (numBytes == 64 || numBytes == 128);
  67. processFullBlock (finalBlocks);
  68. if (numBytes > 64)
  69. processFullBlock (finalBlocks + 64);
  70. }
  71. void copyResult (uint8_t* result) const noexcept
  72. {
  73. for (auto s : state)
  74. {
  75. *result++ = (uint8_t) (s >> 24);
  76. *result++ = (uint8_t) (s >> 16);
  77. *result++ = (uint8_t) (s >> 8);
  78. *result++ = (uint8_t) s;
  79. }
  80. }
  81. void processStream (InputStream& input, int64_t numBytesToRead, uint8_t* result)
  82. {
  83. if (numBytesToRead < 0)
  84. numBytesToRead = std::numeric_limits<int64_t>::max();
  85. for (;;)
  86. {
  87. uint8_t buffer[64];
  88. auto bytesRead = input.read (buffer, (int) jmin (numBytesToRead, (int64_t) sizeof (buffer)));
  89. if (bytesRead < (int) sizeof (buffer))
  90. {
  91. processFinalBlock (buffer, (unsigned int) bytesRead);
  92. break;
  93. }
  94. numBytesToRead -= (int64_t) sizeof (buffer);
  95. processFullBlock (buffer);
  96. }
  97. copyResult (result);
  98. }
  99. private:
  100. uint32_t state[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  101. 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
  102. uint64_t length = 0;
  103. static inline uint32_t rotate (uint32_t x, uint32_t y) noexcept { return (x >> y) | (x << (32 - y)); }
  104. static inline uint32_t ch (uint32_t x, uint32_t y, uint32_t z) noexcept { return z ^ ((y ^ z) & x); }
  105. static inline uint32_t maj (uint32_t x, uint32_t y, uint32_t z) noexcept { return y ^ ((y ^ z) & (x ^ y)); }
  106. static inline uint32_t s0 (uint32_t x) noexcept { return rotate (x, 7) ^ rotate (x, 18) ^ (x >> 3); }
  107. static inline uint32_t s1 (uint32_t x) noexcept { return rotate (x, 17) ^ rotate (x, 19) ^ (x >> 10); }
  108. static inline uint32_t S0 (uint32_t x) noexcept { return rotate (x, 2) ^ rotate (x, 13) ^ rotate (x, 22); }
  109. static inline uint32_t S1 (uint32_t x) noexcept { return rotate (x, 6) ^ rotate (x, 11) ^ rotate (x, 25); }
  110. };
  111. //==============================================================================
  112. SHA256::SHA256() = default;
  113. SHA256::~SHA256() = default;
  114. SHA256::SHA256 (const SHA256&) = default;
  115. SHA256& SHA256::operator= (const SHA256&) = default;
  116. SHA256::SHA256 (const MemoryBlock& data)
  117. {
  118. process (data.getData(), data.getSize());
  119. }
  120. SHA256::SHA256 (const void* data, size_t numBytes)
  121. {
  122. process (data, numBytes);
  123. }
  124. SHA256::SHA256 (InputStream& input, int64 numBytesToRead)
  125. {
  126. SHA256Processor processor;
  127. processor.processStream (input, numBytesToRead, result);
  128. }
  129. SHA256::SHA256 (const File& file)
  130. {
  131. FileInputStream fin (file);
  132. if (fin.getStatus().wasOk())
  133. {
  134. SHA256Processor processor;
  135. processor.processStream (fin, -1, result);
  136. }
  137. else
  138. {
  139. zerostruct (result);
  140. }
  141. }
  142. SHA256::SHA256 (CharPointer_UTF8 utf8) noexcept
  143. {
  144. jassert (utf8.getAddress() != nullptr);
  145. process (utf8.getAddress(), utf8.sizeInBytes() - 1);
  146. }
  147. void SHA256::process (const void* data, size_t numBytes)
  148. {
  149. MemoryInputStream m (data, numBytes, false);
  150. SHA256Processor processor;
  151. processor.processStream (m, -1, result);
  152. }
  153. MemoryBlock SHA256::getRawData() const
  154. {
  155. return MemoryBlock (result, sizeof (result));
  156. }
  157. String SHA256::toHexString() const
  158. {
  159. return String::toHexString (result, sizeof (result), 0);
  160. }
  161. bool SHA256::operator== (const SHA256& other) const noexcept { return memcmp (result, other.result, sizeof (result)) == 0; }
  162. bool SHA256::operator!= (const SHA256& other) const noexcept { return ! operator== (other); }
  163. //==============================================================================
  164. #if JUCE_UNIT_TESTS
  165. class SHA256Tests : public UnitTest
  166. {
  167. public:
  168. SHA256Tests()
  169. : UnitTest ("SHA-256", UnitTestCategories::cryptography)
  170. {}
  171. void test (const char* input, const char* expected)
  172. {
  173. {
  174. SHA256 hash (input, strlen (input));
  175. expectEquals (hash.toHexString(), String (expected));
  176. }
  177. {
  178. CharPointer_UTF8 utf8 (input);
  179. SHA256 hash (utf8);
  180. expectEquals (hash.toHexString(), String (expected));
  181. }
  182. {
  183. MemoryInputStream m (input, strlen (input), false);
  184. SHA256 hash (m);
  185. expectEquals (hash.toHexString(), String (expected));
  186. }
  187. }
  188. void runTest() override
  189. {
  190. beginTest ("SHA256");
  191. test ("", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
  192. test ("The quick brown fox jumps over the lazy dog", "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592");
  193. test ("The quick brown fox jumps over the lazy dog.", "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c");
  194. }
  195. };
  196. static SHA256Tests sha256UnitTests;
  197. #endif
  198. } // namespace juce