Botan 3.11.0
Crypto and TLS for C&
ghash.h
Go to the documentation of this file.
1/*
2* (C) 2013 Jack Lloyd
3* (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_GCM_GHASH_H_
9#define BOTAN_GCM_GHASH_H_
10
11#include <botan/sym_algo.h>
12#include <botan/internal/alignment_buffer.h>
13
14namespace Botan {
15
16/**
17* GCM's GHASH
18*/
20 private:
21 static constexpr size_t GCM_BS = 16;
22
23 public:
24 /// Hashing of non-default length nonce values for both GCM and GMAC use-cases
25 void nonce_hash(std::span<uint8_t, GCM_BS> y0, std::span<const uint8_t> nonce);
26
27 void start(std::span<const uint8_t> nonce);
28
29 void update(std::span<const uint8_t> in);
30
31 /// Monolithic setting of associated data usid in the GCM use-case
32 void set_associated_data(std::span<const uint8_t> ad);
33
34 /// Incremental update of associated data used in the GMAC use-case
35 void update_associated_data(std::span<const uint8_t> ad);
36
37 /// Reset the AAD state without resetting the key (used in GMAC::final_result)
39
40 void final(std::span<uint8_t> out);
41
43
44 bool has_keying_material() const override;
45
46 void clear() override;
47
48 void reset_state();
49
50 std::string name() const override { return "GHASH"; }
51
52 std::string provider() const;
53
54 private:
55 void ghash_update(std::span<uint8_t, GCM_BS> x, std::span<const uint8_t> input);
56 void ghash_zeropad(std::span<uint8_t, GCM_BS> x);
57 void ghash_final_block(std::span<uint8_t, GCM_BS> x, uint64_t ad_len, uint64_t pt_len);
58
59#if defined(BOTAN_HAS_GHASH_CLMUL_CPU)
60 static void ghash_precompute_cpu(const uint8_t H[16], secure_vector<uint64_t>& H_pow);
61
62 static void ghash_multiply_cpu(uint8_t x[16],
64 const uint8_t input[],
65 size_t blocks);
66#endif
67
68#if defined(BOTAN_HAS_GHASH_AVX512_CLMUL)
69 static void ghash_precompute_avx512_clmul(const uint8_t H[16], uint64_t H_pow[16 * 2]);
70
71 static void ghash_multiply_avx512_clmul(uint8_t x[16],
72 const uint64_t H_pow[16 * 2],
73 const uint8_t input[],
74 size_t blocks);
75#endif
76
77#if defined(BOTAN_HAS_GHASH_CLMUL_VPERM)
78 static void ghash_multiply_vperm(uint8_t x[16], const uint64_t HM[256], const uint8_t input[], size_t blocks);
79#endif
80
81 void key_schedule(std::span<const uint8_t> key) override;
82
83 void ghash_multiply(std::span<uint8_t, GCM_BS> x, std::span<const uint8_t> input, size_t blocks);
84
85 private:
87
88 /// cache of hash state after consuming the AD, reused for multiple messages
89 std::array<uint8_t, GCM_BS> m_H_ad{};
90 /// hash state used for update() or update_associated_data()
91 std::array<uint8_t, GCM_BS> m_ghash{};
94
95 std::optional<std::array<uint8_t, GCM_BS>> m_nonce;
96 size_t m_ad_len = 0;
97 size_t m_text_len = 0;
98};
99
100} // namespace Botan
101
102#endif
Alignment buffer helper.
void update_associated_data(std::span< const uint8_t > ad)
Incremental update of associated data used in the GMAC use-case.
Definition ghash.cpp:181
std::string provider() const
Definition ghash.cpp:21
std::string name() const override
Definition ghash.h:50
void final(std::span< uint8_t > out)
Definition ghash.cpp:194
void nonce_hash(std::span< uint8_t, GCM_BS > y0, std::span< const uint8_t > nonce)
Hashing of non-default length nonce values for both GCM and GMAC use-cases.
Definition ghash.cpp:209
void reset_associated_data()
Reset the AAD state without resetting the key (used in GMAC::final_result).
Definition ghash.cpp:173
void reset_state()
Definition ghash.cpp:224
void clear() override
Definition ghash.cpp:218
void update(std::span< const uint8_t > in)
Definition ghash.cpp:187
void start(std::span< const uint8_t > nonce)
Definition ghash.cpp:156
Key_Length_Specification key_spec() const override
Definition ghash.h:42
bool has_keying_material() const override
Definition ghash.cpp:99
void set_associated_data(std::span< const uint8_t > ad)
Monolithic setting of associated data usid in the GCM use-case.
Definition ghash.cpp:163
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68