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