Botan 3.13.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 /// Reset the per-message state (nonce/ghash/text-len/buffer) but
49 /// preserve any associated data set via set_associated_data
50 void reset_state();
51
52 std::string name() const override { return "GHASH"; }
53
54 std::string provider() const;
55
56 private:
57 void ghash_update(std::span<uint8_t, GCM_BS> x, std::span<const uint8_t> input);
58 void ghash_zeropad(std::span<uint8_t, GCM_BS> x);
59 void ghash_final_block(std::span<uint8_t, GCM_BS> x, uint64_t ad_len, uint64_t pt_len);
60
61#if defined(BOTAN_HAS_GHASH_CLMUL_CPU)
62 static void ghash_precompute_cpu(const uint8_t H[16], secure_vector<uint64_t>& H_pow);
63
64 static void ghash_multiply_cpu(uint8_t x[16],
66 const uint8_t input[],
67 size_t blocks);
68#endif
69
70#if defined(BOTAN_HAS_GHASH_AVX512_CLMUL)
71 static void ghash_precompute_avx512_clmul(const uint8_t H[16], uint64_t H_pow[16 * 2]);
72
73 static void ghash_multiply_avx512_clmul(uint8_t x[16],
74 const uint64_t H_pow[16 * 2],
75 const uint8_t input[],
76 size_t blocks);
77#endif
78
79#if defined(BOTAN_HAS_GHASH_CLMUL_VPERM)
80 static void ghash_multiply_vperm(uint8_t x[16], const uint64_t HM[256], const uint8_t input[], size_t blocks);
81#endif
82
83 void key_schedule(std::span<const uint8_t> key) override;
84
85 void ghash_multiply(std::span<uint8_t, GCM_BS> x, std::span<const uint8_t> input, size_t blocks);
86
87 static void ghash_precompute_base(std::span<const uint8_t, GCM_BS> key, secure_vector<uint64_t>& HM);
88
89 static void ghash_multiply_base(std::span<uint8_t, GCM_BS> x,
91 std::span<const uint8_t> input,
92 size_t blocks);
93
94 /// Polyval reuses the GHASH tables and kernels for its non-CLMUL fallback
95 friend class Polyval;
96
97 private:
99
100 /// cache of hash state after consuming the AD, reused for multiple messages
101 std::array<uint8_t, GCM_BS> m_H_ad{};
102 /// hash state used for update() or update_associated_data()
103 std::array<uint8_t, GCM_BS> m_ghash{};
106
107 std::optional<std::array<uint8_t, GCM_BS>> m_nonce;
108 uint64_t m_ad_len = 0;
109 uint64_t m_text_len = 0;
110};
111
112} // namespace Botan
113
114#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:196
std::string provider() const
Definition ghash.cpp:22
std::string name() const override
Definition ghash.h:52
void final(std::span< uint8_t > out)
Definition ghash.cpp:215
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:230
void reset_associated_data()
Reset the AAD state without resetting the key (used in GMAC::final_result).
Definition ghash.cpp:188
void reset_state()
Definition ghash.cpp:247
void clear() override
Definition ghash.cpp:239
void update(std::span< const uint8_t > in)
Definition ghash.cpp:202
void start(std::span< const uint8_t > nonce)
Definition ghash.cpp:169
Key_Length_Specification key_spec() const override
Definition ghash.h:42
friend class Polyval
Polyval reuses the GHASH tables and kernels for its non-CLMUL fallback.
Definition ghash.h:95
bool has_keying_material() const override
Definition ghash.cpp:107
void set_associated_data(std::span< const uint8_t > ad)
Monolithic setting of associated data usid in the GCM use-case.
Definition ghash.cpp:178
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128