Path: blob/main/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerSHA1.cpp
35262 views
//===- FuzzerSHA1.h - Private copy of the SHA1 implementation ---*- C++ -* ===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7// This code is taken from public domain8// (http://oauth.googlecode.com/svn/code/c/liboauth/src/sha1.c)9// and modified by adding anonymous namespace, adding an interface10// function fuzzer::ComputeSHA1() and removing unnecessary code.11//12// lib/Fuzzer can not use SHA1 implementation from openssl because13// openssl may not be available and because we may be fuzzing openssl itself.14// For the same reason we do not want to depend on SHA1 from LLVM tree.15//===----------------------------------------------------------------------===//1617#include "FuzzerSHA1.h"18#include "FuzzerDefs.h"19#include "FuzzerPlatform.h"2021/* This code is public-domain - it is based on libcrypt22* placed in the public domain by Wei Dai and other contributors.23*/2425#include <iomanip>26#include <sstream>27#include <stdint.h>28#include <string.h>2930namespace { // Added for LibFuzzer3132#ifdef __BIG_ENDIAN__33# define SHA_BIG_ENDIAN34// Windows is always little endian and MSVC doesn't have <endian.h>35#elif defined __LITTLE_ENDIAN__ || LIBFUZZER_WINDOWS36/* override */37#elif defined __BYTE_ORDER38# if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__39# define SHA_BIG_ENDIAN40# endif41#else // ! defined __LITTLE_ENDIAN__42# include <endian.h> // machine/endian.h43# if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__44# define SHA_BIG_ENDIAN45# endif46#endif474849/* header */5051#define HASH_LENGTH 2052#define BLOCK_LENGTH 645354typedef struct sha1nfo {55uint32_t buffer[BLOCK_LENGTH/4];56uint32_t state[HASH_LENGTH/4];57uint32_t byteCount;58uint8_t bufferOffset;59uint8_t keyBuffer[BLOCK_LENGTH];60uint8_t innerHash[HASH_LENGTH];61} sha1nfo;6263/* public API - prototypes - TODO: doxygen*/6465/**66*/67void sha1_init(sha1nfo *s);68/**69*/70void sha1_writebyte(sha1nfo *s, uint8_t data);71/**72*/73void sha1_write(sha1nfo *s, const char *data, size_t len);74/**75*/76uint8_t* sha1_result(sha1nfo *s);777879/* code */80#define SHA1_K0 0x5a82799981#define SHA1_K20 0x6ed9eba182#define SHA1_K40 0x8f1bbcdc83#define SHA1_K60 0xca62c1d68485void sha1_init(sha1nfo *s) {86s->state[0] = 0x67452301;87s->state[1] = 0xefcdab89;88s->state[2] = 0x98badcfe;89s->state[3] = 0x10325476;90s->state[4] = 0xc3d2e1f0;91s->byteCount = 0;92s->bufferOffset = 0;93}9495uint32_t sha1_rol32(uint32_t number, uint8_t bits) {96return ((number << bits) | (number >> (32-bits)));97}9899void sha1_hashBlock(sha1nfo *s) {100uint8_t i;101uint32_t a,b,c,d,e,t;102103a=s->state[0];104b=s->state[1];105c=s->state[2];106d=s->state[3];107e=s->state[4];108for (i=0; i<80; i++) {109if (i>=16) {110t = s->buffer[(i+13)&15] ^ s->buffer[(i+8)&15] ^ s->buffer[(i+2)&15] ^ s->buffer[i&15];111s->buffer[i&15] = sha1_rol32(t,1);112}113if (i<20) {114t = (d ^ (b & (c ^ d))) + SHA1_K0;115} else if (i<40) {116t = (b ^ c ^ d) + SHA1_K20;117} else if (i<60) {118t = ((b & c) | (d & (b | c))) + SHA1_K40;119} else {120t = (b ^ c ^ d) + SHA1_K60;121}122t+=sha1_rol32(a,5) + e + s->buffer[i&15];123e=d;124d=c;125c=sha1_rol32(b,30);126b=a;127a=t;128}129s->state[0] += a;130s->state[1] += b;131s->state[2] += c;132s->state[3] += d;133s->state[4] += e;134}135136// Adds the least significant byte of |data|.137void sha1_addUncounted(sha1nfo *s, uint32_t data) {138uint8_t *const b = (uint8_t *)s->buffer;139#ifdef SHA_BIG_ENDIAN140b[s->bufferOffset] = static_cast<uint8_t>(data);141#else142b[s->bufferOffset ^ 3] = static_cast<uint8_t>(data);143#endif144s->bufferOffset++;145if (s->bufferOffset == BLOCK_LENGTH) {146sha1_hashBlock(s);147s->bufferOffset = 0;148}149}150151void sha1_writebyte(sha1nfo *s, uint8_t data) {152++s->byteCount;153sha1_addUncounted(s, data);154}155156void sha1_write(sha1nfo *s, const char *data, size_t len) {157for (;len--;) sha1_writebyte(s, (uint8_t) *data++);158}159160void sha1_pad(sha1nfo *s) {161// Implement SHA-1 padding (fips180-2 §5.1.1)162163// Pad with 0x80 followed by 0x00 until the end of the block164sha1_addUncounted(s, 0x80);165while (s->bufferOffset != 56) sha1_addUncounted(s, 0x00);166167// Append length in the last 8 bytes168sha1_addUncounted(s, 0); // We're only using 32 bit lengths169sha1_addUncounted(s, 0); // But SHA-1 supports 64 bit lengths170sha1_addUncounted(s, 0); // So zero pad the top bits171sha1_addUncounted(s, s->byteCount >> 29); // Shifting to multiply by 8172sha1_addUncounted(s, s->byteCount >> 21); // as SHA-1 supports bitstreams as well as173sha1_addUncounted(s, s->byteCount >> 13); // byte.174sha1_addUncounted(s, s->byteCount >> 5);175sha1_addUncounted(s, s->byteCount << 3);176}177178uint8_t* sha1_result(sha1nfo *s) {179// Pad to complete the last block180sha1_pad(s);181182#ifndef SHA_BIG_ENDIAN183// Swap byte order back184int i;185for (i=0; i<5; i++) {186s->state[i]=187(((s->state[i])<<24)& 0xff000000)188| (((s->state[i])<<8) & 0x00ff0000)189| (((s->state[i])>>8) & 0x0000ff00)190| (((s->state[i])>>24)& 0x000000ff);191}192#endif193194// Return pointer to hash (20 characters)195return (uint8_t*) s->state;196}197198} // namespace; Added for LibFuzzer199200namespace fuzzer {201202// The rest is added for LibFuzzer203void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out) {204sha1nfo s;205sha1_init(&s);206sha1_write(&s, (const char*)Data, Len);207memcpy(Out, sha1_result(&s), HASH_LENGTH);208}209210std::string Sha1ToString(const uint8_t Sha1[kSHA1NumBytes]) {211std::stringstream SS;212for (int i = 0; i < kSHA1NumBytes; i++)213SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Sha1[i];214return SS.str();215}216217std::string Hash(const Unit &U) {218uint8_t Hash[kSHA1NumBytes];219ComputeSHA1(U.data(), U.size(), Hash);220return Sha1ToString(Hash);221}222223}224225226