Botan 3.7.1
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(secure_vector<uint8_t>& 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 void final(std::span<uint8_t> out);
38
40
41 bool has_keying_material() const override;
42
43 void clear() override;
44
45 void reset();
46
47 std::string name() const override { return "GHASH"; }
48
49 std::string provider() const;
50
51 private:
52 void ghash_update(std::span<uint8_t, GCM_BS> x, std::span<const uint8_t> input);
53 void ghash_zeropad(std::span<uint8_t, GCM_BS> x);
54 void ghash_final_block(std::span<uint8_t, GCM_BS> x, uint64_t ad_len, uint64_t pt_len);
55
56#if defined(BOTAN_HAS_GHASH_CLMUL_CPU)
57 static void ghash_precompute_cpu(const uint8_t H[16], uint64_t H_pow[4 * 2]);
58
59 static void ghash_multiply_cpu(uint8_t x[16], const uint64_t H_pow[4 * 2], const uint8_t input[], size_t blocks);
60#endif
61
62#if defined(BOTAN_HAS_GHASH_CLMUL_VPERM)
63 static void ghash_multiply_vperm(uint8_t x[16], const uint64_t HM[256], const uint8_t input[], size_t blocks);
64#endif
65
66 void key_schedule(std::span<const uint8_t> key) override;
67
68 void ghash_multiply(std::span<uint8_t, GCM_BS> x, std::span<const uint8_t> input, size_t blocks);
69
70 private:
72
73 std::array<uint8_t, GCM_BS> m_H_ad; /// cache of hash state after consuming the AD, reused for multiple messages
74 std::array<uint8_t, GCM_BS> m_ghash; /// hash state used for update() or update_associated_data()
77
78 std::optional<std::array<uint8_t, GCM_BS>> m_nonce;
79 size_t m_ad_len = 0;
80 size_t m_text_len = 0;
81};
82
83} // namespace Botan
84
85#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:143
std::string provider() const
Definition ghash.cpp:20
void nonce_hash(secure_vector< uint8_t > &y0, std::span< const uint8_t > nonce)
Hashing of non-default length nonce values for both GCM and GMAC use-cases.
Definition ghash.cpp:171
std::string name() const override
Definition ghash.h:47
void clear() override
Definition ghash.cpp:182
void reset()
Definition ghash.cpp:187
void start(std::span< const uint8_t > nonce)
Definition ghash.cpp:126
Key_Length_Specification key_spec() const override
Definition ghash.h:39
bool has_keying_material() const override
Definition ghash.cpp:85
void set_associated_data(std::span< const uint8_t > ad)
Monolithic setting of associated data usid in the GCM use-case.
Definition ghash.cpp:133
int(* update)(CTX *, const void *, CC_LONG len)
int(* final)(unsigned char *, CTX *)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61