FuzzerSHA1.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //===- FuzzerSHA1.h - 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. // This code is taken from public domain
  9. // (http://oauth.googlecode.com/svn/code/c/liboauth/src/sha1.c)
  10. // and modified by adding anonymous namespace, adding an interface
  11. // function fuzzer::ComputeSHA1() and removing unnecessary code.
  12. //
  13. // lib/Fuzzer can not use SHA1 implementation from openssl because
  14. // openssl may not be available and because we may be fuzzing openssl itself.
  15. // For the same reason we do not want to depend on SHA1 from LLVM tree.
  16. //===----------------------------------------------------------------------===//
  17. #include "FuzzerSHA1.h"
  18. #include "FuzzerDefs.h"
  19. #include "FuzzerPlatform.h"
  20. /* This code is public-domain - it is based on libcrypt
  21. * placed in the public domain by Wei Dai and other contributors.
  22. */
  23. #include <iomanip>
  24. #include <sstream>
  25. #include <stdint.h>
  26. #include <string.h>
  27. namespace { // Added for LibFuzzer
  28. #ifdef __BIG_ENDIAN__
  29. # define SHA_BIG_ENDIAN
  30. // Windows is always little endian and MSVC doesn't have <endian.h>
  31. #elif defined __LITTLE_ENDIAN__ || LIBFUZZER_WINDOWS
  32. /* override */
  33. #elif defined __BYTE_ORDER
  34. # if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  35. # define SHA_BIG_ENDIAN
  36. # endif
  37. #else // ! defined __LITTLE_ENDIAN__
  38. # include <endian.h> // machine/endian.h
  39. # if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  40. # define SHA_BIG_ENDIAN
  41. # endif
  42. #endif
  43. /* header */
  44. #define HASH_LENGTH 20
  45. #define BLOCK_LENGTH 64
  46. typedef struct sha1nfo {
  47. uint32_t buffer[BLOCK_LENGTH/4];
  48. uint32_t state[HASH_LENGTH/4];
  49. uint32_t byteCount;
  50. uint8_t bufferOffset;
  51. uint8_t keyBuffer[BLOCK_LENGTH];
  52. uint8_t innerHash[HASH_LENGTH];
  53. } sha1nfo;
  54. /* public API - prototypes - TODO: doxygen*/
  55. /**
  56. */
  57. void sha1_init(sha1nfo *s);
  58. /**
  59. */
  60. void sha1_writebyte(sha1nfo *s, uint8_t data);
  61. /**
  62. */
  63. void sha1_write(sha1nfo *s, const char *data, size_t len);
  64. /**
  65. */
  66. uint8_t* sha1_result(sha1nfo *s);
  67. /* code */
  68. #define SHA1_K0 0x5a827999
  69. #define SHA1_K20 0x6ed9eba1
  70. #define SHA1_K40 0x8f1bbcdc
  71. #define SHA1_K60 0xca62c1d6
  72. void sha1_init(sha1nfo *s) {
  73. s->state[0] = 0x67452301;
  74. s->state[1] = 0xefcdab89;
  75. s->state[2] = 0x98badcfe;
  76. s->state[3] = 0x10325476;
  77. s->state[4] = 0xc3d2e1f0;
  78. s->byteCount = 0;
  79. s->bufferOffset = 0;
  80. }
  81. uint32_t sha1_rol32(uint32_t number, uint8_t bits) {
  82. return ((number << bits) | (number >> (32-bits)));
  83. }
  84. void sha1_hashBlock(sha1nfo *s) {
  85. uint8_t i;
  86. uint32_t a,b,c,d,e,t;
  87. a=s->state[0];
  88. b=s->state[1];
  89. c=s->state[2];
  90. d=s->state[3];
  91. e=s->state[4];
  92. for (i=0; i<80; i++) {
  93. if (i>=16) {
  94. t = s->buffer[(i+13)&15] ^ s->buffer[(i+8)&15] ^ s->buffer[(i+2)&15] ^ s->buffer[i&15];
  95. s->buffer[i&15] = sha1_rol32(t,1);
  96. }
  97. if (i<20) {
  98. t = (d ^ (b & (c ^ d))) + SHA1_K0;
  99. } else if (i<40) {
  100. t = (b ^ c ^ d) + SHA1_K20;
  101. } else if (i<60) {
  102. t = ((b & c) | (d & (b | c))) + SHA1_K40;
  103. } else {
  104. t = (b ^ c ^ d) + SHA1_K60;
  105. }
  106. t+=sha1_rol32(a,5) + e + s->buffer[i&15];
  107. e=d;
  108. d=c;
  109. c=sha1_rol32(b,30);
  110. b=a;
  111. a=t;
  112. }
  113. s->state[0] += a;
  114. s->state[1] += b;
  115. s->state[2] += c;
  116. s->state[3] += d;
  117. s->state[4] += e;
  118. }
  119. // Adds the least significant byte of |data|.
  120. void sha1_addUncounted(sha1nfo *s, uint32_t data) {
  121. uint8_t *const b = (uint8_t *)s->buffer;
  122. #ifdef SHA_BIG_ENDIAN
  123. b[s->bufferOffset] = static_cast<uint8_t>(data);
  124. #else
  125. b[s->bufferOffset ^ 3] = static_cast<uint8_t>(data);
  126. #endif
  127. s->bufferOffset++;
  128. if (s->bufferOffset == BLOCK_LENGTH) {
  129. sha1_hashBlock(s);
  130. s->bufferOffset = 0;
  131. }
  132. }
  133. void sha1_writebyte(sha1nfo *s, uint8_t data) {
  134. ++s->byteCount;
  135. sha1_addUncounted(s, data);
  136. }
  137. void sha1_write(sha1nfo *s, const char *data, size_t len) {
  138. for (;len--;) sha1_writebyte(s, (uint8_t) *data++);
  139. }
  140. void sha1_pad(sha1nfo *s) {
  141. // Implement SHA-1 padding (fips180-2 §5.1.1)
  142. // Pad with 0x80 followed by 0x00 until the end of the block
  143. sha1_addUncounted(s, 0x80);
  144. while (s->bufferOffset != 56) sha1_addUncounted(s, 0x00);
  145. // Append length in the last 8 bytes
  146. sha1_addUncounted(s, 0); // We're only using 32 bit lengths
  147. sha1_addUncounted(s, 0); // But SHA-1 supports 64 bit lengths
  148. sha1_addUncounted(s, 0); // So zero pad the top bits
  149. sha1_addUncounted(s, s->byteCount >> 29); // Shifting to multiply by 8
  150. sha1_addUncounted(s, s->byteCount >> 21); // as SHA-1 supports bitstreams as well as
  151. sha1_addUncounted(s, s->byteCount >> 13); // byte.
  152. sha1_addUncounted(s, s->byteCount >> 5);
  153. sha1_addUncounted(s, s->byteCount << 3);
  154. }
  155. uint8_t* sha1_result(sha1nfo *s) {
  156. // Pad to complete the last block
  157. sha1_pad(s);
  158. #ifndef SHA_BIG_ENDIAN
  159. // Swap byte order back
  160. int i;
  161. for (i=0; i<5; i++) {
  162. s->state[i]=
  163. (((s->state[i])<<24)& 0xff000000)
  164. | (((s->state[i])<<8) & 0x00ff0000)
  165. | (((s->state[i])>>8) & 0x0000ff00)
  166. | (((s->state[i])>>24)& 0x000000ff);
  167. }
  168. #endif
  169. // Return pointer to hash (20 characters)
  170. return (uint8_t*) s->state;
  171. }
  172. } // namespace; Added for LibFuzzer
  173. namespace fuzzer {
  174. // The rest is added for LibFuzzer
  175. void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out) {
  176. sha1nfo s;
  177. sha1_init(&s);
  178. sha1_write(&s, (const char*)Data, Len);
  179. memcpy(Out, sha1_result(&s), HASH_LENGTH);
  180. }
  181. std::string Sha1ToString(const uint8_t Sha1[kSHA1NumBytes]) {
  182. std::stringstream SS;
  183. for (int i = 0; i < kSHA1NumBytes; i++)
  184. SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Sha1[i];
  185. return SS.str();
  186. }
  187. std::string Hash(const Unit &U) {
  188. uint8_t Hash[kSHA1NumBytes];
  189. ComputeSHA1(U.data(), U.size(), Hash);
  190. return Sha1ToString(Hash);
  191. }
  192. }