/src/serenity/Userland/Libraries/LibCrypto/Hash/SHA1.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/CPUFeatures.h> |
10 | | #include <LibCrypto/Hash/HashFunction.h> |
11 | | |
12 | | #ifndef KERNEL |
13 | | # include <AK/ByteString.h> |
14 | | #endif |
15 | | |
16 | | namespace Crypto::Hash { |
17 | | |
18 | | namespace SHA1Constants { |
19 | | |
20 | | constexpr static u32 InitializationHashes[5] { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 }; |
21 | | |
22 | | constexpr static u32 RoundConstants[4] { |
23 | | 0X5a827999, |
24 | | 0X6ed9eba1, |
25 | | 0X8f1bbcdc, |
26 | | 0Xca62c1d6, |
27 | | }; |
28 | | |
29 | | } |
30 | | |
31 | | class SHA1 final : public HashFunction<512, 160> { |
32 | | public: |
33 | | using HashFunction::update; |
34 | | |
35 | | SHA1() |
36 | 84 | { |
37 | 84 | reset(); |
38 | 84 | } |
39 | | |
40 | | virtual void update(u8 const*, size_t) override; |
41 | | |
42 | | virtual DigestType digest() override; |
43 | | virtual DigestType peek() override; |
44 | | |
45 | | static DigestType hash(u8 const* data, size_t length) |
46 | 84 | { |
47 | 84 | SHA1 sha; |
48 | 84 | sha.update(data, length); |
49 | 84 | return sha.digest(); |
50 | 84 | } |
51 | | |
52 | 0 | static DigestType hash(ByteBuffer const& buffer) { return hash(buffer.data(), buffer.size()); } |
53 | 0 | static DigestType hash(StringView buffer) { return hash((u8 const*)buffer.characters_without_null_termination(), buffer.length()); } |
54 | | |
55 | | #ifndef KERNEL |
56 | | virtual ByteString class_name() const override |
57 | 0 | { |
58 | 0 | return "SHA1"; |
59 | 0 | } |
60 | | #endif |
61 | | |
62 | | virtual void reset() override |
63 | 168 | { |
64 | 168 | m_data_length = 0; |
65 | 168 | m_bit_length = 0; |
66 | 1.00k | for (auto i = 0; i < 5; ++i) |
67 | 840 | m_state[i] = SHA1Constants::InitializationHashes[i]; |
68 | 168 | } |
69 | | |
70 | | private: |
71 | | template<CPUFeatures> |
72 | | void transform_impl(); |
73 | | |
74 | | static void (SHA1::*const transform_dispatched)(); |
75 | 99.3k | void transform() { return (this->*transform_dispatched)(); } |
76 | | |
77 | | u8 m_data_buffer[BlockSize] {}; |
78 | | size_t m_data_length { 0 }; |
79 | | |
80 | | u64 m_bit_length { 0 }; |
81 | | u32 m_state[5]; |
82 | | |
83 | | constexpr static auto FinalBlockDataSize = BlockSize - 8; |
84 | | constexpr static auto Rounds = 80; |
85 | | }; |
86 | | |
87 | | } |