10#include <botan/internal/ghash.h>
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>
17#if defined(BOTAN_HAS_CPUID)
18 #include <botan/internal/cpuid.h>
24#if defined(BOTAN_HAS_GHASH_CLMUL_CPU)
30#if defined(BOTAN_HAS_GHASH_CLMUL_VPERM)
39void GHASH::ghash_multiply(std::span<uint8_t, GCM_BS> x, std::span<const uint8_t> input,
size_t blocks) {
42#if defined(BOTAN_HAS_GHASH_CLMUL_CPU)
45 return ghash_multiply_cpu(x.data(), m_H_pow.data(), input.data(), blocks);
49#if defined(BOTAN_HAS_GHASH_CLMUL_VPERM)
51 return ghash_multiply_vperm(x.data(), m_HM.data(), input.data(), blocks);
59 BufferSlicer in(input);
60 for(
size_t b = 0; b != blocks; ++b) {
65 std::array<uint64_t, 2> Z{};
67 for(
size_t i = 0; i != 64; ++i) {
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]);
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]);
89 return !m_HM.empty() || !m_H_pow.empty();
92void GHASH::key_schedule(std::span<const uint8_t> key) {
100#if defined(BOTAN_HAS_GHASH_CLMUL_CPU)
103 if(m_H_pow.size() != 8) {
106 ghash_precompute_cpu(key.data(), m_H_pow.data());
112 const uint64_t R = 0xE100000000000000;
114 if(m_HM.size() != 256) {
119 for(
size_t i = 0; i != 2; ++i) {
120 for(
size_t j = 0; j != 64; ++j) {
125 m_HM[4 * j + 2 * i] = H[0];
126 m_HM[4 * j + 2 * i + 1] = H[1];
130 H[1] = (H[1] >> 1) | (H[0] << 63);
131 H[0] = (H[0] >> 1) ^
carry;
138 auto& n = m_nonce.emplace();
148 ghash_update(m_H_ad, input);
149 ghash_zeropad(m_H_ad);
150 m_ad_len = input.size();
155 ghash_update(m_ghash, ad);
156 m_ad_len += ad.size();
162 ghash_update(m_ghash, input);
163 m_text_len += input.size();
167 BOTAN_ARG_CHECK(!mac.empty() && mac.size() <= GCM_BS,
"GHASH output length");
171 ghash_zeropad(m_ghash);
172 ghash_final_block(m_ghash, m_ad_len, m_text_len);
174 xor_buf(mac, std::span{m_ghash}.first(mac.size()), std::span{*m_nonce}.first(mac.size()));
185 ghash_update(y0, nonce);
187 ghash_final_block(y0, 0, nonce.size());
204 m_text_len = m_ad_len = 0;
207void GHASH::ghash_update(std::span<uint8_t, GCM_BS> x, std::span<const uint8_t> input) {
211 ghash_multiply(x, one_block.value(), 1);
216 if(full_blocks > 0) {
217 ghash_multiply(x, aligned_data, full_blocks);
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);
231void GHASH::ghash_final_block(std::span<uint8_t, GCM_BS> x, uint64_t ad_len, uint64_t text_len) {
233 const auto final_block =
store_be(8 * ad_len, 8 * text_len);
234 ghash_multiply(x, final_block, 1);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ARG_CHECK(expr, msg)
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)
bool in_alignment() const
static bool has(CPUID::Feature feat)
static constexpr Mask< T > expand(T v)
static constexpr Mask< T > expand_top_bit(T v)
void update_associated_data(std::span< const uint8_t > ad)
Incremental update of associated data used in the GMAC use-case.
std::string provider() const
void final(std::span< uint8_t > out)
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.
void update(std::span< const uint8_t > in)
void start(std::span< const uint8_t > nonce)
bool has_keying_material() const override
void set_associated_data(std::span< const uint8_t > ad)
Monolithic setting of associated data usid in the GCM use-case.
void assert_key_material_set() const
constexpr auto scoped_poison(const Ts &... xs)
void zap(std::vector< T, Alloc > &vec)
void secure_scrub_memory(void *ptr, size_t n)
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)
constexpr void copy_mem(T *out, const T *in, size_t n)
constexpr auto store_be(ParamTs &&... params)
constexpr auto load_be(ParamTs &&... params)