| 1 | //====- SHA256.cpp - SHA256 implementation ---*- C++ -* ======// |
| 2 | // |
| 3 | // 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /* |
| 9 | * The SHA-256 Secure Hash Standard was published by NIST in 2002. |
| 10 | * |
| 11 | * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf |
| 12 | * |
| 13 | * The implementation is based on nacl's sha256 implementation [0] and LLVM's |
| 14 | * pre-exsiting SHA1 code [1]. |
| 15 | * |
| 16 | * [0] https://hyperelliptic.org/nacl/nacl-20110221.tar.bz2 (public domain |
| 17 | * code) |
| 18 | * [1] llvm/lib/Support/SHA1.{h,cpp} |
| 19 | */ |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #ifndef LLVM_SUPPORT_SHA256_H |
| 23 | #define LLVM_SUPPORT_SHA256_H |
| 24 | |
| 25 | #include "llvm/Support/Compiler.h" |
| 26 | #include <array> |
| 27 | #include <cstdint> |
| 28 | |
| 29 | namespace llvm { |
| 30 | |
| 31 | template <typename T> class ArrayRef; |
| 32 | class StringRef; |
| 33 | |
| 34 | class SHA256 { |
| 35 | public: |
| 36 | explicit SHA256() { init(); } |
| 37 | |
| 38 | /// Reinitialize the internal state |
| 39 | LLVM_ABI void init(); |
| 40 | |
| 41 | /// Digest more data. |
| 42 | LLVM_ABI void update(ArrayRef<uint8_t> Data); |
| 43 | |
| 44 | /// Digest more data. |
| 45 | LLVM_ABI void update(StringRef Str); |
| 46 | |
| 47 | /// Return the current raw 256-bits SHA256 for the digested |
| 48 | /// data since the last call to init(). This call will add data to the |
| 49 | /// internal state and as such is not suited for getting an intermediate |
| 50 | /// result (see result()). |
| 51 | LLVM_ABI std::array<uint8_t, 32> final(); |
| 52 | |
| 53 | /// Return the current raw 256-bits SHA256 for the digested |
| 54 | /// data since the last call to init(). This is suitable for getting the |
| 55 | /// SHA256 at any time without invalidating the internal state so that more |
| 56 | /// calls can be made into update. |
| 57 | LLVM_ABI std::array<uint8_t, 32> result(); |
| 58 | |
| 59 | /// Returns a raw 256-bit SHA256 hash for the given data. |
| 60 | LLVM_ABI static std::array<uint8_t, 32> hash(ArrayRef<uint8_t> Data); |
| 61 | |
| 62 | private: |
| 63 | /// Define some constants. |
| 64 | /// "static constexpr" would be cleaner but MSVC does not support it yet. |
| 65 | enum { BLOCK_LENGTH = 64 }; |
| 66 | enum { HASH_LENGTH = 32 }; |
| 67 | |
| 68 | // Internal State |
| 69 | struct { |
| 70 | union { |
| 71 | uint8_t C[BLOCK_LENGTH]; |
| 72 | uint32_t L[BLOCK_LENGTH / 4]; |
| 73 | } Buffer; |
| 74 | uint32_t State[HASH_LENGTH / 4]; |
| 75 | uint32_t ByteCount; |
| 76 | uint8_t BufferOffset; |
| 77 | } InternalState; |
| 78 | |
| 79 | // Helper |
| 80 | void writebyte(uint8_t data); |
| 81 | void hashBlock(); |
| 82 | void addUncounted(uint8_t data); |
| 83 | void pad(); |
| 84 | |
| 85 | void final(std::array<uint32_t, HASH_LENGTH / 4> &HashResult); |
| 86 | }; |
| 87 | |
| 88 | } // namespace llvm |
| 89 | |
| 90 | #endif // LLVM_SUPPORT_SHA256_H |
| 91 | |