//===- FuzzerSHA1.h - Private copy of the SHA1 implementation ---*- C++ -* ===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // This code is taken from public domain // (http://oauth.googlecode.com/svn/code/c/liboauth/src/sha1.c) // and modified by adding anonymous namespace, adding an interface // function fuzzer::ComputeSHA1() and removing unnecessary code. // // lib/Fuzzer can not use SHA1 implementation from openssl because // openssl may not be available and because we may be fuzzing openssl itself. // For the same reason we do not want to depend on SHA1 from LLVM tree. //===----------------------------------------------------------------------===// #include "FuzzerSHA1.h" #include "FuzzerDefs.h" #include "FuzzerPlatform.h" /* This code is public-domain - it is based on libcrypt * placed in the public domain by Wei Dai and other contributors. */ #include <iomanip> #include <sstream> #include <stdint.h> #include <string.h> namespace { // Added for LibFuzzer #ifdef __BIG_ENDIAN__ #define SHA_BIG_ENDIAN // Windows is always little endian and MSVC doesn't have <endian.h> #elif defined __LITTLE_ENDIAN__ || LIBFUZZER_WINDOWS /* override */ #elif defined __BYTE_ORDER # if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ #define SHA_BIG_ENDIAN # endif #else // ! defined __LITTLE_ENDIAN__ # include <endian.h> // machine/endian.h # if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ #define SHA_BIG_ENDIAN # endif #endif /* header */ #define HASH_LENGTH … #define BLOCK_LENGTH … sha1nfo; /* public API - prototypes - TODO: doxygen*/ /** */ void sha1_init(sha1nfo *s); /** */ void sha1_writebyte(sha1nfo *s, uint8_t data); /** */ void sha1_write(sha1nfo *s, const char *data, size_t len); /** */ uint8_t* sha1_result(sha1nfo *s); /* code */ #define SHA1_K0 … #define SHA1_K20 … #define SHA1_K40 … #define SHA1_K60 … void sha1_init(sha1nfo *s) { … } uint32_t sha1_rol32(uint32_t number, uint8_t bits) { … } void sha1_hashBlock(sha1nfo *s) { … } // Adds the least significant byte of |data|. void sha1_addUncounted(sha1nfo *s, uint32_t data) { … } void sha1_writebyte(sha1nfo *s, uint8_t data) { … } void sha1_write(sha1nfo *s, const char *data, size_t len) { … } void sha1_pad(sha1nfo *s) { … } uint8_t* sha1_result(sha1nfo *s) { … } } // namespace; Added for LibFuzzer namespace fuzzer { // The rest is added for LibFuzzer void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out) { … } std::string Sha1ToString(const uint8_t Sha1[kSHA1NumBytes]) { … } std::string Hash(const Unit &U) { … } }