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