FuzzerSHA1.cpp 5.0 KB

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