SHA1.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //====- SHA1.cpp - Private copy of the SHA1 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. // This code is taken from public domain
  10. // (http://oauth.googlecode.com/svn/code/c/liboauth/src/sha1.c and
  11. // http://cvsweb.netbsd.org/bsdweb.cgi/src/common/lib/libc/hash/sha1/sha1.c?rev=1.6)
  12. // and modified by wrapping it in a C++ interface for LLVM,
  13. // and removing unnecessary code.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/Support/SHA1.h"
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/ADT/StringRef.h"
  19. #include "llvm/Support/Endian.h"
  20. #include "llvm/Support/Host.h"
  21. #include <string.h>
  22. using namespace llvm;
  23. #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
  24. #define SHA_BIG_ENDIAN
  25. #endif
  26. static inline uint32_t rol(uint32_t Number, int Bits) {
  27. return (Number << Bits) | (Number >> (32 - Bits));
  28. }
  29. static inline uint32_t blk0(uint32_t *Buf, int I) { return Buf[I]; }
  30. static inline uint32_t blk(uint32_t *Buf, int I) {
  31. Buf[I & 15] = rol(Buf[(I + 13) & 15] ^ Buf[(I + 8) & 15] ^ Buf[(I + 2) & 15] ^
  32. Buf[I & 15],
  33. 1);
  34. return Buf[I & 15];
  35. }
  36. static inline void r0(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D,
  37. uint32_t &E, int I, uint32_t *Buf) {
  38. E += ((B & (C ^ D)) ^ D) + blk0(Buf, I) + 0x5A827999 + rol(A, 5);
  39. B = rol(B, 30);
  40. }
  41. static inline void r1(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D,
  42. uint32_t &E, int I, uint32_t *Buf) {
  43. E += ((B & (C ^ D)) ^ D) + blk(Buf, I) + 0x5A827999 + rol(A, 5);
  44. B = rol(B, 30);
  45. }
  46. static inline void r2(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D,
  47. uint32_t &E, int I, uint32_t *Buf) {
  48. E += (B ^ C ^ D) + blk(Buf, I) + 0x6ED9EBA1 + rol(A, 5);
  49. B = rol(B, 30);
  50. }
  51. static inline void r3(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D,
  52. uint32_t &E, int I, uint32_t *Buf) {
  53. E += (((B | C) & D) | (B & C)) + blk(Buf, I) + 0x8F1BBCDC + rol(A, 5);
  54. B = rol(B, 30);
  55. }
  56. static inline void r4(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D,
  57. uint32_t &E, int I, uint32_t *Buf) {
  58. E += (B ^ C ^ D) + blk(Buf, I) + 0xCA62C1D6 + rol(A, 5);
  59. B = rol(B, 30);
  60. }
  61. /* code */
  62. #define SHA1_K0 0x5a827999
  63. #define SHA1_K20 0x6ed9eba1
  64. #define SHA1_K40 0x8f1bbcdc
  65. #define SHA1_K60 0xca62c1d6
  66. #define SEED_0 0x67452301
  67. #define SEED_1 0xefcdab89
  68. #define SEED_2 0x98badcfe
  69. #define SEED_3 0x10325476
  70. #define SEED_4 0xc3d2e1f0
  71. void SHA1::init() {
  72. InternalState.State[0] = SEED_0;
  73. InternalState.State[1] = SEED_1;
  74. InternalState.State[2] = SEED_2;
  75. InternalState.State[3] = SEED_3;
  76. InternalState.State[4] = SEED_4;
  77. InternalState.ByteCount = 0;
  78. InternalState.BufferOffset = 0;
  79. }
  80. void SHA1::hashBlock() {
  81. uint32_t A = InternalState.State[0];
  82. uint32_t B = InternalState.State[1];
  83. uint32_t C = InternalState.State[2];
  84. uint32_t D = InternalState.State[3];
  85. uint32_t E = InternalState.State[4];
  86. // 4 rounds of 20 operations each. Loop unrolled.
  87. r0(A, B, C, D, E, 0, InternalState.Buffer.L);
  88. r0(E, A, B, C, D, 1, InternalState.Buffer.L);
  89. r0(D, E, A, B, C, 2, InternalState.Buffer.L);
  90. r0(C, D, E, A, B, 3, InternalState.Buffer.L);
  91. r0(B, C, D, E, A, 4, InternalState.Buffer.L);
  92. r0(A, B, C, D, E, 5, InternalState.Buffer.L);
  93. r0(E, A, B, C, D, 6, InternalState.Buffer.L);
  94. r0(D, E, A, B, C, 7, InternalState.Buffer.L);
  95. r0(C, D, E, A, B, 8, InternalState.Buffer.L);
  96. r0(B, C, D, E, A, 9, InternalState.Buffer.L);
  97. r0(A, B, C, D, E, 10, InternalState.Buffer.L);
  98. r0(E, A, B, C, D, 11, InternalState.Buffer.L);
  99. r0(D, E, A, B, C, 12, InternalState.Buffer.L);
  100. r0(C, D, E, A, B, 13, InternalState.Buffer.L);
  101. r0(B, C, D, E, A, 14, InternalState.Buffer.L);
  102. r0(A, B, C, D, E, 15, InternalState.Buffer.L);
  103. r1(E, A, B, C, D, 16, InternalState.Buffer.L);
  104. r1(D, E, A, B, C, 17, InternalState.Buffer.L);
  105. r1(C, D, E, A, B, 18, InternalState.Buffer.L);
  106. r1(B, C, D, E, A, 19, InternalState.Buffer.L);
  107. r2(A, B, C, D, E, 20, InternalState.Buffer.L);
  108. r2(E, A, B, C, D, 21, InternalState.Buffer.L);
  109. r2(D, E, A, B, C, 22, InternalState.Buffer.L);
  110. r2(C, D, E, A, B, 23, InternalState.Buffer.L);
  111. r2(B, C, D, E, A, 24, InternalState.Buffer.L);
  112. r2(A, B, C, D, E, 25, InternalState.Buffer.L);
  113. r2(E, A, B, C, D, 26, InternalState.Buffer.L);
  114. r2(D, E, A, B, C, 27, InternalState.Buffer.L);
  115. r2(C, D, E, A, B, 28, InternalState.Buffer.L);
  116. r2(B, C, D, E, A, 29, InternalState.Buffer.L);
  117. r2(A, B, C, D, E, 30, InternalState.Buffer.L);
  118. r2(E, A, B, C, D, 31, InternalState.Buffer.L);
  119. r2(D, E, A, B, C, 32, InternalState.Buffer.L);
  120. r2(C, D, E, A, B, 33, InternalState.Buffer.L);
  121. r2(B, C, D, E, A, 34, InternalState.Buffer.L);
  122. r2(A, B, C, D, E, 35, InternalState.Buffer.L);
  123. r2(E, A, B, C, D, 36, InternalState.Buffer.L);
  124. r2(D, E, A, B, C, 37, InternalState.Buffer.L);
  125. r2(C, D, E, A, B, 38, InternalState.Buffer.L);
  126. r2(B, C, D, E, A, 39, InternalState.Buffer.L);
  127. r3(A, B, C, D, E, 40, InternalState.Buffer.L);
  128. r3(E, A, B, C, D, 41, InternalState.Buffer.L);
  129. r3(D, E, A, B, C, 42, InternalState.Buffer.L);
  130. r3(C, D, E, A, B, 43, InternalState.Buffer.L);
  131. r3(B, C, D, E, A, 44, InternalState.Buffer.L);
  132. r3(A, B, C, D, E, 45, InternalState.Buffer.L);
  133. r3(E, A, B, C, D, 46, InternalState.Buffer.L);
  134. r3(D, E, A, B, C, 47, InternalState.Buffer.L);
  135. r3(C, D, E, A, B, 48, InternalState.Buffer.L);
  136. r3(B, C, D, E, A, 49, InternalState.Buffer.L);
  137. r3(A, B, C, D, E, 50, InternalState.Buffer.L);
  138. r3(E, A, B, C, D, 51, InternalState.Buffer.L);
  139. r3(D, E, A, B, C, 52, InternalState.Buffer.L);
  140. r3(C, D, E, A, B, 53, InternalState.Buffer.L);
  141. r3(B, C, D, E, A, 54, InternalState.Buffer.L);
  142. r3(A, B, C, D, E, 55, InternalState.Buffer.L);
  143. r3(E, A, B, C, D, 56, InternalState.Buffer.L);
  144. r3(D, E, A, B, C, 57, InternalState.Buffer.L);
  145. r3(C, D, E, A, B, 58, InternalState.Buffer.L);
  146. r3(B, C, D, E, A, 59, InternalState.Buffer.L);
  147. r4(A, B, C, D, E, 60, InternalState.Buffer.L);
  148. r4(E, A, B, C, D, 61, InternalState.Buffer.L);
  149. r4(D, E, A, B, C, 62, InternalState.Buffer.L);
  150. r4(C, D, E, A, B, 63, InternalState.Buffer.L);
  151. r4(B, C, D, E, A, 64, InternalState.Buffer.L);
  152. r4(A, B, C, D, E, 65, InternalState.Buffer.L);
  153. r4(E, A, B, C, D, 66, InternalState.Buffer.L);
  154. r4(D, E, A, B, C, 67, InternalState.Buffer.L);
  155. r4(C, D, E, A, B, 68, InternalState.Buffer.L);
  156. r4(B, C, D, E, A, 69, InternalState.Buffer.L);
  157. r4(A, B, C, D, E, 70, InternalState.Buffer.L);
  158. r4(E, A, B, C, D, 71, InternalState.Buffer.L);
  159. r4(D, E, A, B, C, 72, InternalState.Buffer.L);
  160. r4(C, D, E, A, B, 73, InternalState.Buffer.L);
  161. r4(B, C, D, E, A, 74, InternalState.Buffer.L);
  162. r4(A, B, C, D, E, 75, InternalState.Buffer.L);
  163. r4(E, A, B, C, D, 76, InternalState.Buffer.L);
  164. r4(D, E, A, B, C, 77, InternalState.Buffer.L);
  165. r4(C, D, E, A, B, 78, InternalState.Buffer.L);
  166. r4(B, C, D, E, A, 79, InternalState.Buffer.L);
  167. InternalState.State[0] += A;
  168. InternalState.State[1] += B;
  169. InternalState.State[2] += C;
  170. InternalState.State[3] += D;
  171. InternalState.State[4] += E;
  172. }
  173. void SHA1::addUncounted(uint8_t Data) {
  174. #ifdef SHA_BIG_ENDIAN
  175. InternalState.Buffer.C[InternalState.BufferOffset] = Data;
  176. #else
  177. InternalState.Buffer.C[InternalState.BufferOffset ^ 3] = Data;
  178. #endif
  179. InternalState.BufferOffset++;
  180. if (InternalState.BufferOffset == BLOCK_LENGTH) {
  181. hashBlock();
  182. InternalState.BufferOffset = 0;
  183. }
  184. }
  185. void SHA1::writebyte(uint8_t Data) {
  186. ++InternalState.ByteCount;
  187. addUncounted(Data);
  188. }
  189. void SHA1::update(ArrayRef<uint8_t> Data) {
  190. InternalState.ByteCount += Data.size();
  191. // Finish the current block.
  192. if (InternalState.BufferOffset > 0) {
  193. const size_t Remainder = std::min<size_t>(
  194. Data.size(), BLOCK_LENGTH - InternalState.BufferOffset);
  195. for (size_t I = 0; I < Remainder; ++I)
  196. addUncounted(Data[I]);
  197. Data = Data.drop_front(Remainder);
  198. }
  199. // Fast buffer filling for large inputs.
  200. while (Data.size() >= BLOCK_LENGTH) {
  201. assert(InternalState.BufferOffset == 0);
  202. static_assert(BLOCK_LENGTH % 4 == 0, "");
  203. constexpr size_t BLOCK_LENGTH_32 = BLOCK_LENGTH / 4;
  204. for (size_t I = 0; I < BLOCK_LENGTH_32; ++I)
  205. InternalState.Buffer.L[I] = support::endian::read32be(&Data[I * 4]);
  206. hashBlock();
  207. Data = Data.drop_front(BLOCK_LENGTH);
  208. }
  209. // Finish the remainder.
  210. for (uint8_t C : Data)
  211. addUncounted(C);
  212. }
  213. void SHA1::update(StringRef Str) {
  214. update(
  215. ArrayRef<uint8_t>((uint8_t *)const_cast<char *>(Str.data()), Str.size()));
  216. }
  217. void SHA1::pad() {
  218. // Implement SHA-1 padding (fips180-2 5.1.1)
  219. // Pad with 0x80 followed by 0x00 until the end of the block
  220. addUncounted(0x80);
  221. while (InternalState.BufferOffset != 56)
  222. addUncounted(0x00);
  223. // Append length in the last 8 bytes
  224. addUncounted(0); // We're only using 32 bit lengths
  225. addUncounted(0); // But SHA-1 supports 64 bit lengths
  226. addUncounted(0); // So zero pad the top bits
  227. addUncounted(InternalState.ByteCount >> 29); // Shifting to multiply by 8
  228. addUncounted(InternalState.ByteCount >>
  229. 21); // as SHA-1 supports bitstreams as well as
  230. addUncounted(InternalState.ByteCount >> 13); // byte.
  231. addUncounted(InternalState.ByteCount >> 5);
  232. addUncounted(InternalState.ByteCount << 3);
  233. }
  234. StringRef SHA1::final() {
  235. // Pad to complete the last block
  236. pad();
  237. #ifdef SHA_BIG_ENDIAN
  238. // Just copy the current state
  239. for (int i = 0; i < 5; i++) {
  240. HashResult[i] = InternalState.State[i];
  241. }
  242. #else
  243. // Swap byte order back
  244. for (int i = 0; i < 5; i++) {
  245. HashResult[i] = (((InternalState.State[i]) << 24) & 0xff000000) |
  246. (((InternalState.State[i]) << 8) & 0x00ff0000) |
  247. (((InternalState.State[i]) >> 8) & 0x0000ff00) |
  248. (((InternalState.State[i]) >> 24) & 0x000000ff);
  249. }
  250. #endif
  251. // Return pointer to hash (20 characters)
  252. return StringRef((char *)HashResult, HASH_LENGTH);
  253. }
  254. StringRef SHA1::result() {
  255. auto StateToRestore = InternalState;
  256. auto Hash = final();
  257. // Restore the state
  258. InternalState = StateToRestore;
  259. // Return pointer to hash (20 characters)
  260. return Hash;
  261. }
  262. std::array<uint8_t, 20> SHA1::hash(ArrayRef<uint8_t> Data) {
  263. SHA1 Hash;
  264. Hash.update(Data);
  265. StringRef S = Hash.final();
  266. std::array<uint8_t, 20> Arr;
  267. memcpy(Arr.data(), S.data(), S.size());
  268. return Arr;
  269. }