Botan 3.8.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#include <botan/internal/stl_util.h>
16
17#if defined(BOTAN_HAS_CPUID)
18 #include <botan/internal/cpuid.h>
19#endif
20
21namespace Botan {
22
23std::string GHASH::provider() const {
24#if defined(BOTAN_HAS_GHASH_CLMUL_CPU)
26 return "clmul";
27 }
28#endif
29
30#if defined(BOTAN_HAS_GHASH_CLMUL_VPERM)
32 return "vperm";
33 }
34#endif
35
36 return "base";
37}
38
39void GHASH::ghash_multiply(std::span<uint8_t, GCM_BS> x, std::span<const uint8_t> input, size_t blocks) {
40 BOTAN_ASSERT_NOMSG(input.size() % GCM_BS == 0);
41
42#if defined(BOTAN_HAS_GHASH_CLMUL_CPU)
44 BOTAN_ASSERT_NOMSG(!m_H_pow.empty());
45 return ghash_multiply_cpu(x.data(), m_H_pow.data(), input.data(), blocks);
46 }
47#endif
48
49#if defined(BOTAN_HAS_GHASH_CLMUL_VPERM)
51 return ghash_multiply_vperm(x.data(), m_HM.data(), input.data(), blocks);
52 }
53#endif
54
55 auto scope = CT::scoped_poison(x);
56
58
59 BufferSlicer in(input);
60 for(size_t b = 0; b != blocks; ++b) {
61 const auto I = load_be<std::array<uint64_t, 2>>(in.take<GCM_BS>());
62 X[0] ^= I[0];
63 X[1] ^= I[1];
64
65 std::array<uint64_t, 2> Z{};
66
67 for(size_t i = 0; i != 64; ++i) {
68 const auto X0MASK = CT::Mask<uint64_t>::expand_top_bit(X[0]);
69 const auto X1MASK = CT::Mask<uint64_t>::expand_top_bit(X[1]);
70
71 X[0] <<= 1;
72 X[1] <<= 1;
73
74 Z[0] = X0MASK.select(Z[0] ^ m_HM[4 * i], Z[0]);
75 Z[1] = X0MASK.select(Z[1] ^ m_HM[4 * i + 1], Z[1]);
76
77 Z[0] = X1MASK.select(Z[0] ^ m_HM[4 * i + 2], Z[0]);
78 Z[1] = X1MASK.select(Z[1] ^ m_HM[4 * i + 3], Z[1]);
79 }
80
81 X[0] = Z[0];
82 X[1] = Z[1];
83 }
84
85 store_be(x, X);
86}
87
89 return !m_HM.empty() || !m_H_pow.empty();
90}
91
92void GHASH::key_schedule(std::span<const uint8_t> key) {
93 m_H_ad = {0};
94 m_ad_len = 0;
95 m_text_len = 0;
96
97 BOTAN_ASSERT_NOMSG(key.size() == GCM_BS);
98 auto H = load_be<std::array<uint64_t, 2>>(key.first<GCM_BS>());
99
100#if defined(BOTAN_HAS_GHASH_CLMUL_CPU)
102 zap(m_HM);
103 if(m_H_pow.size() != 8) {
104 m_H_pow.resize(8);
105 }
106 ghash_precompute_cpu(key.data(), m_H_pow.data());
107 // m_HM left empty
108 return;
109 }
110#endif
111
112 const uint64_t R = 0xE100000000000000;
113
114 if(m_HM.size() != 256) {
115 m_HM.resize(256);
116 }
117
118 // precompute the multiples of H
119 for(size_t i = 0; i != 2; ++i) {
120 for(size_t j = 0; j != 64; ++j) {
121 /*
122 we interleave H^1, H^65, H^2, H^66, H3, H67, H4, H68
123 to make indexing nicer in the multiplication code
124 */
125 m_HM[4 * j + 2 * i] = H[0];
126 m_HM[4 * j + 2 * i + 1] = H[1];
127
128 // GCM's bit ops are reversed so we carry out of the bottom
129 const uint64_t carry = CT::Mask<uint64_t>::expand(H[1] & 1).if_set_return(R);
130 H[1] = (H[1] >> 1) | (H[0] << 63);
131 H[0] = (H[0] >> 1) ^ carry;
132 }
133 }
134}
135
136void GHASH::start(std::span<const uint8_t> nonce) {
137 BOTAN_ARG_CHECK(nonce.size() == 16, "GHASH requires a 128-bit nonce");
138 auto& n = m_nonce.emplace();
139 copy_mem(n, nonce);
140 copy_mem(m_ghash, m_H_ad);
141}
142
143void GHASH::set_associated_data(std::span<const uint8_t> input) {
144 BOTAN_STATE_CHECK(!m_nonce);
145
147 m_H_ad = {0};
148 ghash_update(m_H_ad, input);
149 ghash_zeropad(m_H_ad);
150 m_ad_len = input.size();
151}
152
153void GHASH::update_associated_data(std::span<const uint8_t> ad) {
155 ghash_update(m_ghash, ad);
156 m_ad_len += ad.size();
157}
158
159void GHASH::update(std::span<const uint8_t> input) {
161 BOTAN_STATE_CHECK(m_nonce);
162 ghash_update(m_ghash, input);
163 m_text_len += input.size();
164}
165
166void GHASH::final(std::span<uint8_t> mac) {
167 BOTAN_ARG_CHECK(!mac.empty() && mac.size() <= GCM_BS, "GHASH output length");
168 BOTAN_STATE_CHECK(m_nonce);
170
171 ghash_zeropad(m_ghash);
172 ghash_final_block(m_ghash, m_ad_len, m_text_len);
173
174 xor_buf(mac, std::span{m_ghash}.first(mac.size()), std::span{*m_nonce}.first(mac.size()));
175
176 secure_scrub_memory(m_ghash);
177 m_text_len = 0;
178 m_nonce.reset();
179}
180
181void GHASH::nonce_hash(std::span<uint8_t, GCM_BS> y0, std::span<const uint8_t> nonce) {
183 BOTAN_STATE_CHECK(!m_nonce);
184
185 ghash_update(y0, nonce);
186 ghash_zeropad(y0);
187 ghash_final_block(y0, 0, nonce.size());
188}
189
191 zap(m_HM);
192 zap(m_H_pow);
193 reset();
194}
195
197 m_H_ad = {0};
198 secure_scrub_memory(m_ghash);
199 if(m_nonce) {
200 secure_scrub_memory(m_nonce.value());
201 m_nonce.reset();
202 }
203 m_buffer.clear();
204 m_text_len = m_ad_len = 0;
205}
206
207void GHASH::ghash_update(std::span<uint8_t, GCM_BS> x, std::span<const uint8_t> input) {
208 BufferSlicer in(input);
209 while(!in.empty()) {
210 if(const auto one_block = m_buffer.handle_unaligned_data(in)) {
211 ghash_multiply(x, one_block.value(), 1);
212 }
213
214 if(m_buffer.in_alignment()) {
215 const auto [aligned_data, full_blocks] = m_buffer.aligned_data_to_process(in);
216 if(full_blocks > 0) {
217 ghash_multiply(x, aligned_data, full_blocks);
218 }
219 }
220 }
221 BOTAN_ASSERT_NOMSG(in.empty());
222}
223
224void GHASH::ghash_zeropad(std::span<uint8_t, GCM_BS> x) {
225 if(!m_buffer.in_alignment()) {
226 m_buffer.fill_up_with_zeros();
227 ghash_multiply(x, m_buffer.consume(), 1);
228 }
229}
230
231void GHASH::ghash_final_block(std::span<uint8_t, GCM_BS> x, uint64_t ad_len, uint64_t text_len) {
232 BOTAN_STATE_CHECK(m_buffer.in_alignment());
233 const auto final_block = store_be(8 * ad_len, 8 * text_len);
234 ghash_multiply(x, final_block, 1);
235}
236
237} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:61
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:43
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:31
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 bool has(CPUID::Feature feat)
Definition cpuid.h:94
static constexpr Mask< T > expand(T v)
Definition ct_utils.h:409
static constexpr Mask< T > expand_top_bit(T v)
Definition ct_utils.h:427
void update_associated_data(std::span< const uint8_t > ad)
Incremental update of associated data used in the GMAC use-case.
Definition ghash.cpp:153
std::string provider() const
Definition ghash.cpp:23
void final(std::span< uint8_t > out)
Definition ghash.cpp:166
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:181
void clear() override
Definition ghash.cpp:190
void update(std::span< const uint8_t > in)
Definition ghash.cpp:159
void reset()
Definition ghash.cpp:196
void start(std::span< const uint8_t > nonce)
Definition ghash.cpp:136
bool has_keying_material() const override
Definition ghash.cpp:88
void set_associated_data(std::span< const uint8_t > ad)
Monolithic setting of associated data usid in the GCM use-case.
Definition ghash.cpp:143
void assert_key_material_set() const
Definition sym_algo.h:141
constexpr auto scoped_poison(const Ts &... xs)
Definition ct_utils.h:217
void zap(std::vector< T, Alloc > &vec)
Definition secmem.h:124
void secure_scrub_memory(void *ptr, size_t n)
Definition mem_utils.cpp:24
void carry(int64_t &h0, int64_t &h1)
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
Definition mem_ops.h:344
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:149
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504