blob: 99c075f75257940fda03842285e3511d92c8b869 [file] [log] [blame]
george.karpenkov29efa6d2017-08-21 23:25:50 +00001//===- FuzzerSHA1.h - Private copy of the SHA1 implementation ---*- C++ -* ===//
2//
chandlerc40284492019-01-19 08:50:56 +00003// 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
george.karpenkov29efa6d2017-08-21 23:25:50 +00006//
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
18#include "FuzzerSHA1.h"
19#include "FuzzerDefs.h"
20
21/* This code is public-domain - it is based on libcrypt
22 * placed in the public domain by Wei Dai and other contributors.
23 */
24
25#include <iomanip>
26#include <sstream>
27#include <stdint.h>
28#include <string.h>
29
30namespace { // Added for LibFuzzer
31
32#ifdef __BIG_ENDIAN__
33# define SHA_BIG_ENDIAN
34#elif defined __LITTLE_ENDIAN__
35/* override */
36#elif defined __BYTE_ORDER
37# if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
38# define SHA_BIG_ENDIAN
39# endif
40#else // ! defined __LITTLE_ENDIAN__
41# include <endian.h> // machine/endian.h
42# if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
43# define SHA_BIG_ENDIAN
44# endif
45#endif
46
47
48/* header */
49
50#define HASH_LENGTH 20
51#define BLOCK_LENGTH 64
52
53typedef struct sha1nfo {
54 uint32_t buffer[BLOCK_LENGTH/4];
55 uint32_t state[HASH_LENGTH/4];
56 uint32_t byteCount;
57 uint8_t bufferOffset;
58 uint8_t keyBuffer[BLOCK_LENGTH];
59 uint8_t innerHash[HASH_LENGTH];
60} sha1nfo;
61
62/* public API - prototypes - TODO: doxygen*/
63
64/**
65 */
66void sha1_init(sha1nfo *s);
67/**
68 */
69void sha1_writebyte(sha1nfo *s, uint8_t data);
70/**
71 */
72void sha1_write(sha1nfo *s, const char *data, size_t len);
73/**
74 */
75uint8_t* sha1_result(sha1nfo *s);
76
77
78/* code */
79#define SHA1_K0 0x5a827999
80#define SHA1_K20 0x6ed9eba1
81#define SHA1_K40 0x8f1bbcdc
82#define SHA1_K60 0xca62c1d6
83
84void sha1_init(sha1nfo *s) {
85 s->state[0] = 0x67452301;
86 s->state[1] = 0xefcdab89;
87 s->state[2] = 0x98badcfe;
88 s->state[3] = 0x10325476;
89 s->state[4] = 0xc3d2e1f0;
90 s->byteCount = 0;
91 s->bufferOffset = 0;
92}
93
94uint32_t sha1_rol32(uint32_t number, uint8_t bits) {
95 return ((number << bits) | (number >> (32-bits)));
96}
97
98void sha1_hashBlock(sha1nfo *s) {
99 uint8_t i;
100 uint32_t a,b,c,d,e,t;
101
102 a=s->state[0];
103 b=s->state[1];
104 c=s->state[2];
105 d=s->state[3];
106 e=s->state[4];
107 for (i=0; i<80; i++) {
108 if (i>=16) {
109 t = s->buffer[(i+13)&15] ^ s->buffer[(i+8)&15] ^ s->buffer[(i+2)&15] ^ s->buffer[i&15];
110 s->buffer[i&15] = sha1_rol32(t,1);
111 }
112 if (i<20) {
113 t = (d ^ (b & (c ^ d))) + SHA1_K0;
114 } else if (i<40) {
115 t = (b ^ c ^ d) + SHA1_K20;
116 } else if (i<60) {
117 t = ((b & c) | (d & (b | c))) + SHA1_K40;
118 } else {
119 t = (b ^ c ^ d) + SHA1_K60;
120 }
121 t+=sha1_rol32(a,5) + e + s->buffer[i&15];
122 e=d;
123 d=c;
124 c=sha1_rol32(b,30);
125 b=a;
126 a=t;
127 }
128 s->state[0] += a;
129 s->state[1] += b;
130 s->state[2] += c;
131 s->state[3] += d;
132 s->state[4] += e;
133}
134
135void sha1_addUncounted(sha1nfo *s, uint8_t data) {
136 uint8_t * const b = (uint8_t*) s->buffer;
137#ifdef SHA_BIG_ENDIAN
138 b[s->bufferOffset] = data;
139#else
140 b[s->bufferOffset ^ 3] = data;
141#endif
142 s->bufferOffset++;
143 if (s->bufferOffset == BLOCK_LENGTH) {
144 sha1_hashBlock(s);
145 s->bufferOffset = 0;
146 }
147}
148
149void sha1_writebyte(sha1nfo *s, uint8_t data) {
150 ++s->byteCount;
151 sha1_addUncounted(s, data);
152}
153
154void sha1_write(sha1nfo *s, const char *data, size_t len) {
155 for (;len--;) sha1_writebyte(s, (uint8_t) *data++);
156}
157
158void sha1_pad(sha1nfo *s) {
159 // Implement SHA-1 padding (fips180-2 ยง5.1.1)
160
161 // Pad with 0x80 followed by 0x00 until the end of the block
162 sha1_addUncounted(s, 0x80);
163 while (s->bufferOffset != 56) sha1_addUncounted(s, 0x00);
164
165 // Append length in the last 8 bytes
166 sha1_addUncounted(s, 0); // We're only using 32 bit lengths
167 sha1_addUncounted(s, 0); // But SHA-1 supports 64 bit lengths
168 sha1_addUncounted(s, 0); // So zero pad the top bits
169 sha1_addUncounted(s, s->byteCount >> 29); // Shifting to multiply by 8
170 sha1_addUncounted(s, s->byteCount >> 21); // as SHA-1 supports bitstreams as well as
171 sha1_addUncounted(s, s->byteCount >> 13); // byte.
172 sha1_addUncounted(s, s->byteCount >> 5);
173 sha1_addUncounted(s, s->byteCount << 3);
174}
175
176uint8_t* sha1_result(sha1nfo *s) {
177 // Pad to complete the last block
178 sha1_pad(s);
179
180#ifndef SHA_BIG_ENDIAN
181 // Swap byte order back
182 int i;
183 for (i=0; i<5; i++) {
184 s->state[i]=
185 (((s->state[i])<<24)& 0xff000000)
186 | (((s->state[i])<<8) & 0x00ff0000)
187 | (((s->state[i])>>8) & 0x0000ff00)
188 | (((s->state[i])>>24)& 0x000000ff);
189 }
190#endif
191
192 // Return pointer to hash (20 characters)
193 return (uint8_t*) s->state;
194}
195
196} // namespace; Added for LibFuzzer
197
198namespace fuzzer {
199
200// The rest is added for LibFuzzer
201void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out) {
202 sha1nfo s;
203 sha1_init(&s);
204 sha1_write(&s, (const char*)Data, Len);
205 memcpy(Out, sha1_result(&s), HASH_LENGTH);
206}
207
208std::string Sha1ToString(const uint8_t Sha1[kSHA1NumBytes]) {
209 std::stringstream SS;
210 for (int i = 0; i < kSHA1NumBytes; i++)
211 SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Sha1[i];
212 return SS.str();
213}
214
215std::string Hash(const Unit &U) {
216 uint8_t Hash[kSHA1NumBytes];
217 ComputeSHA1(U.data(), U.size(), Hash);
218 return Sha1ToString(Hash);
219}
220
221}