SHA256.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //====- SHA256.cpp - SHA256 implementation ---*- C++ -* ======//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. /*
  9. * The SHA-256 Secure Hash Standard was published by NIST in 2002.
  10. *
  11. * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
  12. *
  13. * The implementation is based on nacl's sha256 implementation [0] and LLVM's
  14. * pre-exsiting SHA1 code [1].
  15. *
  16. * [0] https://hyperelliptic.org/nacl/nacl-20110221.tar.bz2 (public domain
  17. * code)
  18. * [1] llvm/lib/Support/SHA1.{h,cpp}
  19. */
  20. //===----------------------------------------------------------------------===//
  21. #include "llvm/Support/SHA256.h"
  22. #include "llvm/ADT/ArrayRef.h"
  23. #include "llvm/ADT/StringRef.h"
  24. #include "llvm/Support/Endian.h"
  25. #include "llvm/Support/SwapByteOrder.h"
  26. #include <string.h>
  27. namespace llvm {
  28. #define SHR(x, c) ((x) >> (c))
  29. #define ROTR(x, n) (((x) >> n) | ((x) << (32 - (n))))
  30. #define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
  31. #define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  32. #define SIGMA_0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
  33. #define SIGMA_1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
  34. #define SIGMA_2(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
  35. #define SIGMA_3(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
  36. #define F_EXPAND(A, B, C, D, E, F, G, H, M1, M2, M3, M4, k) \
  37. do { \
  38. H += SIGMA_1(E) + CH(E, F, G) + M1 + k; \
  39. D += H; \
  40. H += SIGMA_0(A) + MAJ(A, B, C); \
  41. M1 += SIGMA_2(M2) + M3 + SIGMA_3(M4); \
  42. } while (0);
  43. void SHA256::init() {
  44. InternalState.State[0] = 0x6A09E667;
  45. InternalState.State[1] = 0xBB67AE85;
  46. InternalState.State[2] = 0x3C6EF372;
  47. InternalState.State[3] = 0xA54FF53A;
  48. InternalState.State[4] = 0x510E527F;
  49. InternalState.State[5] = 0x9B05688C;
  50. InternalState.State[6] = 0x1F83D9AB;
  51. InternalState.State[7] = 0x5BE0CD19;
  52. InternalState.ByteCount = 0;
  53. InternalState.BufferOffset = 0;
  54. }
  55. void SHA256::hashBlock() {
  56. uint32_t A = InternalState.State[0];
  57. uint32_t B = InternalState.State[1];
  58. uint32_t C = InternalState.State[2];
  59. uint32_t D = InternalState.State[3];
  60. uint32_t E = InternalState.State[4];
  61. uint32_t F = InternalState.State[5];
  62. uint32_t G = InternalState.State[6];
  63. uint32_t H = InternalState.State[7];
  64. uint32_t W00 = InternalState.Buffer.L[0];
  65. uint32_t W01 = InternalState.Buffer.L[1];
  66. uint32_t W02 = InternalState.Buffer.L[2];
  67. uint32_t W03 = InternalState.Buffer.L[3];
  68. uint32_t W04 = InternalState.Buffer.L[4];
  69. uint32_t W05 = InternalState.Buffer.L[5];
  70. uint32_t W06 = InternalState.Buffer.L[6];
  71. uint32_t W07 = InternalState.Buffer.L[7];
  72. uint32_t W08 = InternalState.Buffer.L[8];
  73. uint32_t W09 = InternalState.Buffer.L[9];
  74. uint32_t W10 = InternalState.Buffer.L[10];
  75. uint32_t W11 = InternalState.Buffer.L[11];
  76. uint32_t W12 = InternalState.Buffer.L[12];
  77. uint32_t W13 = InternalState.Buffer.L[13];
  78. uint32_t W14 = InternalState.Buffer.L[14];
  79. uint32_t W15 = InternalState.Buffer.L[15];
  80. F_EXPAND(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x428A2F98);
  81. F_EXPAND(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x71374491);
  82. F_EXPAND(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0xB5C0FBCF);
  83. F_EXPAND(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0xE9B5DBA5);
  84. F_EXPAND(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x3956C25B);
  85. F_EXPAND(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x59F111F1);
  86. F_EXPAND(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x923F82A4);
  87. F_EXPAND(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0xAB1C5ED5);
  88. F_EXPAND(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xD807AA98);
  89. F_EXPAND(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x12835B01);
  90. F_EXPAND(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x243185BE);
  91. F_EXPAND(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x550C7DC3);
  92. F_EXPAND(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x72BE5D74);
  93. F_EXPAND(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0x80DEB1FE);
  94. F_EXPAND(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x9BDC06A7);
  95. F_EXPAND(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC19BF174);
  96. F_EXPAND(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0xE49B69C1);
  97. F_EXPAND(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0xEFBE4786);
  98. F_EXPAND(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x0FC19DC6);
  99. F_EXPAND(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x240CA1CC);
  100. F_EXPAND(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x2DE92C6F);
  101. F_EXPAND(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4A7484AA);
  102. F_EXPAND(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5CB0A9DC);
  103. F_EXPAND(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x76F988DA);
  104. F_EXPAND(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x983E5152);
  105. F_EXPAND(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA831C66D);
  106. F_EXPAND(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xB00327C8);
  107. F_EXPAND(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xBF597FC7);
  108. F_EXPAND(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xC6E00BF3);
  109. F_EXPAND(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD5A79147);
  110. F_EXPAND(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x06CA6351);
  111. F_EXPAND(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x14292967);
  112. F_EXPAND(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x27B70A85);
  113. F_EXPAND(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x2E1B2138);
  114. F_EXPAND(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x4D2C6DFC);
  115. F_EXPAND(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x53380D13);
  116. F_EXPAND(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x650A7354);
  117. F_EXPAND(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x766A0ABB);
  118. F_EXPAND(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x81C2C92E);
  119. F_EXPAND(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x92722C85);
  120. F_EXPAND(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xA2BFE8A1);
  121. F_EXPAND(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA81A664B);
  122. F_EXPAND(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xC24B8B70);
  123. F_EXPAND(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xC76C51A3);
  124. F_EXPAND(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xD192E819);
  125. F_EXPAND(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD6990624);
  126. F_EXPAND(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xF40E3585);
  127. F_EXPAND(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x106AA070);
  128. F_EXPAND(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x19A4C116);
  129. F_EXPAND(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x1E376C08);
  130. F_EXPAND(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x2748774C);
  131. F_EXPAND(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x34B0BCB5);
  132. F_EXPAND(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x391C0CB3);
  133. F_EXPAND(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4ED8AA4A);
  134. F_EXPAND(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5B9CCA4F);
  135. F_EXPAND(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x682E6FF3);
  136. F_EXPAND(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x748F82EE);
  137. F_EXPAND(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x78A5636F);
  138. F_EXPAND(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x84C87814);
  139. F_EXPAND(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x8CC70208);
  140. F_EXPAND(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x90BEFFFA);
  141. F_EXPAND(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xA4506CEB);
  142. F_EXPAND(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xBEF9A3F7);
  143. F_EXPAND(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC67178F2);
  144. InternalState.State[0] += A;
  145. InternalState.State[1] += B;
  146. InternalState.State[2] += C;
  147. InternalState.State[3] += D;
  148. InternalState.State[4] += E;
  149. InternalState.State[5] += F;
  150. InternalState.State[6] += G;
  151. InternalState.State[7] += H;
  152. }
  153. void SHA256::addUncounted(uint8_t Data) {
  154. if constexpr (sys::IsBigEndianHost)
  155. InternalState.Buffer.C[InternalState.BufferOffset] = Data;
  156. else
  157. InternalState.Buffer.C[InternalState.BufferOffset ^ 3] = Data;
  158. InternalState.BufferOffset++;
  159. if (InternalState.BufferOffset == BLOCK_LENGTH) {
  160. hashBlock();
  161. InternalState.BufferOffset = 0;
  162. }
  163. }
  164. void SHA256::writebyte(uint8_t Data) {
  165. ++InternalState.ByteCount;
  166. addUncounted(Data);
  167. }
  168. void SHA256::update(ArrayRef<uint8_t> Data) {
  169. InternalState.ByteCount += Data.size();
  170. // Finish the current block.
  171. if (InternalState.BufferOffset > 0) {
  172. const size_t Remainder = std::min<size_t>(
  173. Data.size(), BLOCK_LENGTH - InternalState.BufferOffset);
  174. for (size_t I = 0; I < Remainder; ++I)
  175. addUncounted(Data[I]);
  176. Data = Data.drop_front(Remainder);
  177. }
  178. // Fast buffer filling for large inputs.
  179. while (Data.size() >= BLOCK_LENGTH) {
  180. assert(InternalState.BufferOffset == 0);
  181. static_assert(BLOCK_LENGTH % 4 == 0);
  182. constexpr size_t BLOCK_LENGTH_32 = BLOCK_LENGTH / 4;
  183. for (size_t I = 0; I < BLOCK_LENGTH_32; ++I)
  184. InternalState.Buffer.L[I] = support::endian::read32be(&Data[I * 4]);
  185. hashBlock();
  186. Data = Data.drop_front(BLOCK_LENGTH);
  187. }
  188. // Finish the remainder.
  189. for (uint8_t C : Data)
  190. addUncounted(C);
  191. }
  192. void SHA256::update(StringRef Str) {
  193. update(
  194. ArrayRef<uint8_t>((uint8_t *)const_cast<char *>(Str.data()), Str.size()));
  195. }
  196. void SHA256::pad() {
  197. // Implement SHA-2 padding (fips180-2 5.1.1)
  198. // Pad with 0x80 followed by 0x00 until the end of the block
  199. addUncounted(0x80);
  200. while (InternalState.BufferOffset != 56)
  201. addUncounted(0x00);
  202. uint64_t len = InternalState.ByteCount << 3; // bit size
  203. // Append length in the last 8 bytes big edian encoded
  204. addUncounted(len >> 56);
  205. addUncounted(len >> 48);
  206. addUncounted(len >> 40);
  207. addUncounted(len >> 32);
  208. addUncounted(len >> 24);
  209. addUncounted(len >> 16);
  210. addUncounted(len >> 8);
  211. addUncounted(len);
  212. }
  213. void SHA256::final(std::array<uint32_t, HASH_LENGTH / 4> &HashResult) {
  214. // Pad to complete the last block
  215. pad();
  216. if constexpr (sys::IsBigEndianHost) {
  217. // Just copy the current state
  218. for (int i = 0; i < 8; i++) {
  219. HashResult[i] = InternalState.State[i];
  220. }
  221. } else {
  222. // Swap byte order back
  223. for (int i = 0; i < 8; i++) {
  224. HashResult[i] = sys::getSwappedBytes(InternalState.State[i]);
  225. }
  226. }
  227. }
  228. std::array<uint8_t, 32> SHA256::final() {
  229. union {
  230. std::array<uint32_t, HASH_LENGTH / 4> HashResult;
  231. std::array<uint8_t, HASH_LENGTH> ReturnResult;
  232. };
  233. static_assert(sizeof(HashResult) == sizeof(ReturnResult));
  234. final(HashResult);
  235. return ReturnResult;
  236. }
  237. std::array<uint8_t, 32> SHA256::result() {
  238. auto StateToRestore = InternalState;
  239. auto Hash = final();
  240. // Restore the state
  241. InternalState = StateToRestore;
  242. // Return pointer to hash (32 characters)
  243. return Hash;
  244. }
  245. std::array<uint8_t, 32> SHA256::hash(ArrayRef<uint8_t> Data) {
  246. SHA256 Hash;
  247. Hash.update(Data);
  248. return Hash.final();
  249. }
  250. } // namespace llvm