Botan 3.14.1
Crypto and TLS for C&
ghash.cpp
Go to the documentation of this file.
1/*
2* GCM GHASH
3* (C) 2013,2015,2017 Jack Lloyd
4* (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity
5* (C) 2024 René Meusel, Rohde & Schwarz Cybersecurity
6*
7* Botan is released under the Simplified BSD License (see license.txt)
8*/
9
10#include <botan/internal/ghash.h>
11
12#include <botan/exceptn.h>
13#include <botan/internal/ct_utils.h>
14#include <botan/internal/loadstor.h>
15
16#if defined(BOTAN_HAS_CPUID)
17 #include <botan/internal/cpuid.h>
18#endif
19
20namespace Botan {
21
22std::string GHASH::provider() const {
23#if defined(BOTAN_HAS_GHASH_AVX512_CLMUL)
25 return *feat;
26 }
27#endif
28
29#if defined(BOTAN_HAS_GHASH_CLMUL_CPU)
30 if(auto feat = CPUID::check(CPUID::Feature::HW_CLMUL)) {
31 return *feat;
32 }
33#endif
34
35#if defined(BOTAN_HAS_GHASH_CLMUL_VPERM)
37 return *feat;
38 }
39#endif
40
41 return "base";
42}
43
44void GHASH::ghash_multiply(std::span<uint8_t, GCM_BS> x, std::span<const uint8_t> input, size_t blocks) {
45 BOTAN_ASSERT_NOMSG(input.size() % GCM_BS == 0);
46
47#if defined(BOTAN_HAS_GHASH_AVX512_CLMUL)
49 // The AVX-512 table layout differs from the one used by ghash_cpu
50 BOTAN_ASSERT_NOMSG(m_H_pow.size() == 32);
51 return ghash_multiply_avx512_clmul(x.data(), m_H_pow.data(), input.data(), blocks);
52 }
53#endif
54
55#if defined(BOTAN_HAS_GHASH_CLMUL_CPU)
57 BOTAN_ASSERT_NOMSG(!m_H_pow.empty());
58 return ghash_multiply_cpu(x.data(), m_H_pow, input.data(), blocks);
59 }
60#endif
61
62#if defined(BOTAN_HAS_GHASH_CLMUL_VPERM)
64 return ghash_multiply_vperm(x.data(), m_HM.data(), input.data(), blocks);
65 }
66#endif
67
68 ghash_multiply_base(x, m_HM, input, blocks);
69}
70
71void GHASH::ghash_multiply_base(std::span<uint8_t, GCM_BS> x,
73 std::span<const uint8_t> input,
74 size_t blocks) {
75 const auto scope = CT::scoped_poison(x);
76
78
79 BufferSlicer in(input);
80 for(size_t b = 0; b != blocks; ++b) {
81 const auto I = load_be<std::array<uint64_t, 2>>(in.take<GCM_BS>());
82 X[0] ^= I[0];
83 X[1] ^= I[1];
84
85 std::array<uint64_t, 2> Z{};
86
87 for(size_t i = 0; i != 64; ++i) {
88 const auto X0MASK = CT::Mask<uint64_t>::expand_top_bit(X[0]);
89 const auto X1MASK = CT::Mask<uint64_t>::expand_top_bit(X[1]);
90
91 X[0] <<= 1;
92 X[1] <<= 1;
93
94 Z[0] = X0MASK.select(Z[0] ^ HM[4 * i], Z[0]);
95 Z[1] = X0MASK.select(Z[1] ^ HM[4 * i + 1], Z[1]);
96
97 Z[0] = X1MASK.select(Z[0] ^ HM[4 * i + 2], Z[0]);
98 Z[1] = X1MASK.select(Z[1] ^ HM[4 * i + 3], Z[1]);
99 }
100
101 X[0] = Z[0];
102 X[1] = Z[1];
103 }
104
105 store_be(x, X);
106}
107
109 return !m_HM.empty() || !m_H_pow.empty();
110}
111
112void GHASH::key_schedule(std::span<const uint8_t> key) {
113 m_H_ad = {0};
114 m_ad_len = 0;
115 m_text_len = 0;
116
117 BOTAN_ASSERT_NOMSG(key.size() == GCM_BS);
118
119#if defined(BOTAN_HAS_GHASH_AVX512_CLMUL)
121 zap(m_HM);
122 if(m_H_pow.size() != 32) {
123 m_H_pow.resize(32);
124 }
125 ghash_precompute_avx512_clmul(key.data(), m_H_pow.data());
126 // m_HM left empty
127 return;
128 }
129#endif
130
131#if defined(BOTAN_HAS_GHASH_CLMUL_CPU)
133 zap(m_HM);
134 ghash_precompute_cpu(key.data(), m_H_pow);
135 // m_HM left empty
136 return;
137 }
138#endif
139
140 ghash_precompute_base(key.first<GCM_BS>(), m_HM);
141}
142
143void GHASH::ghash_precompute_base(std::span<const uint8_t, GCM_BS> key, secure_vector<uint64_t>& HM) {
144 auto H = load_be<std::array<uint64_t, 2>>(key);
145
146 const uint64_t R = 0xE100000000000000;
147
148 if(HM.size() != 256) {
149 HM.resize(256);
150 }
151
152 // precompute the multiples of H
153 for(size_t i = 0; i != 2; ++i) {
154 for(size_t j = 0; j != 64; ++j) {
155 /*
156 we interleave H^1, H^65, H^2, H^66, H3, H67, H4, H68
157 to make indexing nicer in the multiplication code
158 */
159 HM[4 * j + 2 * i] = H[0];
160 HM[4 * j + 2 * i + 1] = H[1];
161
162 // GCM's bit ops are reversed so we carry out of the bottom
163 const uint64_t carry = CT::Mask<uint64_t>::expand(H[1] & 1).if_set_return(R);
164 H[1] = (H[1] >> 1) | (H[0] << 63);
165 H[0] = (H[0] >> 1) ^ carry;
166 }
167 }
168}
169
170void GHASH::start(std::span<const uint8_t> nonce) {
171 BOTAN_ARG_CHECK(nonce.size() == 16, "GHASH requires a 128-bit nonce");
172 auto& n = m_nonce.emplace();
173 copy_mem(n, nonce);
174 copy_mem(m_ghash, m_H_ad);
175 m_buffer.clear();
176 m_text_len = 0;
177}
178
179void GHASH::set_associated_data(std::span<const uint8_t> input) {
180 BOTAN_STATE_CHECK(!m_nonce);
181
183 m_H_ad = {0};
184 ghash_update(m_H_ad, input);
185 ghash_zeropad(m_H_ad);
186 m_ad_len = input.size();
187}
188
190 // This should only be called in GMAC context
191 BOTAN_STATE_CHECK(m_text_len == 0);
193 m_H_ad = {0};
194 m_ad_len = 0;
195}
196
197void GHASH::update_associated_data(std::span<const uint8_t> ad) {
199 ghash_update(m_ghash, ad);
200 m_ad_len += ad.size();
201}
202
203void GHASH::update(std::span<const uint8_t> input) {
205 BOTAN_STATE_CHECK(m_nonce);
206 ghash_update(m_ghash, input);
207 m_text_len += input.size();
208
209 // NIST SP 800-38D limits plaintext/ciphertext to 2^39 - 256 bits
210 constexpr uint64_t GHASH_MAX_BYTES = (((static_cast<uint64_t>(1) << 39)) - 256) / 8;
211 if(m_text_len > GHASH_MAX_BYTES) {
212 throw Invalid_State("GCM message length limit exceeded");
213 }
214}
215
216void GHASH::final(std::span<uint8_t> mac) {
217 BOTAN_ARG_CHECK(!mac.empty() && mac.size() <= GCM_BS, "GHASH output length");
218 BOTAN_STATE_CHECK(m_nonce);
220
221 ghash_zeropad(m_ghash);
222 ghash_final_block(m_ghash, m_ad_len, m_text_len);
223
224 xor_buf(mac, std::span{m_ghash}.first(mac.size()), std::span{*m_nonce}.first(mac.size()));
225
226 secure_scrub_memory(m_ghash);
227 m_text_len = 0;
228 m_nonce.reset();
229}
230
231void GHASH::nonce_hash(std::span<uint8_t, GCM_BS> y0, std::span<const uint8_t> nonce) {
233 BOTAN_STATE_CHECK(!m_nonce);
234
235 ghash_update(y0, nonce);
236 ghash_zeropad(y0);
237 ghash_final_block(y0, 0, nonce.size());
238}
239
241 zap(m_HM);
242 zap(m_H_pow);
243 m_H_ad = {0};
244 m_ad_len = 0;
245 this->reset_state();
246}
247
249 secure_scrub_memory(m_ghash);
250 if(m_nonce) {
251 secure_scrub_memory(m_nonce.value());
252 m_nonce.reset();
253 }
254 m_buffer.clear();
255 m_text_len = 0;
256}
257
258void GHASH::ghash_update(std::span<uint8_t, GCM_BS> x, std::span<const uint8_t> input) {
259 BufferSlicer in(input);
260 while(!in.empty()) {
261 if(const auto one_block = m_buffer.handle_unaligned_data(in)) {
262 ghash_multiply(x, one_block.value(), 1);
263 }
264
265 if(m_buffer.in_alignment()) {
266 const auto [aligned_data, full_blocks] = m_buffer.aligned_data_to_process(in);
267 if(full_blocks > 0) {
268 ghash_multiply(x, aligned_data, full_blocks);
269 }
270 }
271 }
272 BOTAN_ASSERT_NOMSG(in.empty());
273}
274
275void GHASH::ghash_zeropad(std::span<uint8_t, GCM_BS> x) {
276 if(!m_buffer.in_alignment()) {
277 m_buffer.fill_up_with_zeros();
278 ghash_multiply(x, m_buffer.consume(), 1);
279 }
280}
281
282void GHASH::ghash_final_block(std::span<uint8_t, GCM_BS> x, uint64_t ad_len, uint64_t text_len) {
283 BOTAN_STATE_CHECK(m_buffer.in_alignment());
284 const auto final_block = store_be(8 * ad_len, 8 * text_len);
285 ghash_multiply(x, final_block, 1);
286}
287
288} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
std::tuple< std::span< const uint8_t >, size_t > aligned_data_to_process(BufferSlicer &slicer) const
std::optional< std::span< const T > > handle_unaligned_data(BufferSlicer &slicer)
static std::optional< std::string > check(CPUID::Feature feat)
Definition cpuid.h:66
static bool has(CPUID::Feature feat)
Definition cpuid.h:93
static constexpr Mask< T > expand(T v)
Definition ct_utils.h:397
static constexpr Mask< T > expand_top_bit(T v)
Definition ct_utils.h:420
void update_associated_data(std::span< const uint8_t > ad)
Incremental update of associated data used in the GMAC use-case.
Definition ghash.cpp:197
std::string provider() const
Definition ghash.cpp:22
void final(std::span< uint8_t > out)
Definition ghash.cpp:216
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:231
void reset_associated_data()
Reset the AAD state without resetting the key (used in GMAC::final_result).
Definition ghash.cpp:189
void reset_state()
Definition ghash.cpp:248
void clear() override
Definition ghash.cpp:240
void update(std::span< const uint8_t > in)
Definition ghash.cpp:203
void start(std::span< const uint8_t > nonce)
Definition ghash.cpp:170
bool has_keying_material() const override
Definition ghash.cpp:108
void set_associated_data(std::span< const uint8_t > ad)
Monolithic setting of associated data usid in the GCM use-case.
Definition ghash.cpp:179
void assert_key_material_set() const
Definition sym_algo.h:180
constexpr auto scoped_poison(const Ts &... xs)
Definition ct_utils.h:222
void zap(std::vector< T, Alloc > &vec)
Definition secmem.h:261
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
void secure_scrub_memory(void *ptr, size_t n)
Definition mem_utils.cpp:24
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
Definition mem_ops.h:403
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504