Botan 3.13.0
Crypto and TLS for C&
gmac.cpp
Go to the documentation of this file.
1/*
2 * GMAC
3 * (C) 2016 Matthias Gierlings, René Korthaus
4 * (C) 2017 Jack Lloyd
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9#include <botan/internal/gmac.h>
10
11#include <botan/block_cipher.h>
12#include <botan/exceptn.h>
13#include <botan/mem_ops.h>
14#include <botan/internal/fmt.h>
15#include <botan/internal/ghash.h>
16
17namespace Botan {
18
19GMAC::GMAC(std::unique_ptr<BlockCipher> cipher) :
20 m_cipher(std::move(cipher)), m_ghash(std::make_unique<GHASH>()), m_H(GCM_BS), m_initialized(false) {
21 if(m_cipher->block_size() != GCM_BS) {
22 throw Invalid_Argument(fmt("Invalid block cipher {} for GMAC", m_cipher->name()));
23 }
24}
25
27 m_cipher->clear();
28 m_ghash->clear();
29 zeroise(m_H);
30 m_initialized = false;
31}
32
33GMAC::~GMAC() = default;
34
36 return m_cipher->key_spec();
37}
38
39std::string GMAC::name() const {
40 return fmt("GMAC({})", m_cipher->name());
41}
42
43std::string GMAC::provider() const {
44 return m_ghash->provider();
45}
46
47size_t GMAC::output_length() const {
48 return GCM_BS;
49}
50
51void GMAC::add_data(std::span<const uint8_t> input) {
52 if(!m_initialized) {
53 throw Invalid_State("GMAC was not used with a fresh nonce");
54 }
55 m_ghash->update_associated_data(input);
56}
57
59 return m_cipher->has_keying_material();
60}
61
62void GMAC::key_schedule(std::span<const uint8_t> key) {
63 clear();
64 m_cipher->set_key(key);
65
66 m_cipher->encrypt(m_H);
67 m_ghash->set_key(m_H);
68}
69
70void GMAC::start_msg(std::span<const uint8_t> nonce) {
71 if(nonce.empty()) {
72 throw Invalid_IV_Length(name(), nonce.size());
73 }
74
75 std::array<uint8_t, GCM_BS> y0 = {0};
76
77 // Clear any AD accumulated by a prior start() that was never finalized
78 m_ghash->reset_state();
79 m_ghash->reset_associated_data();
80
81 if(nonce.size() == 12) {
82 copy_mem(y0.data(), nonce.data(), nonce.size());
83 y0[GCM_BS - 1] = 1;
84 } else {
85 m_ghash->nonce_hash(y0, nonce);
86 }
87
88 m_cipher->encrypt(y0.data());
89 m_ghash->start(y0);
90 m_initialized = true;
91}
92
93void GMAC::final_result(std::span<uint8_t> mac) {
94 // This ensures the GMAC computation has been initialized with a fresh
95 // nonce. The aim of this check is to prevent developers from re-using
96 // nonces (and potential nonce-reuse attacks).
97 if(!m_initialized) {
98 throw Invalid_State("GMAC was not used with a fresh nonce");
99 }
100
101 m_ghash->final(mac.first(output_length()));
102 m_ghash->reset_associated_data();
103}
104
105std::unique_ptr<MessageAuthenticationCode> GMAC::new_object() const {
106 return std::make_unique<GMAC>(m_cipher->new_object());
107}
108} // namespace Botan
std::string name() const override
Definition gmac.cpp:39
GMAC(std::unique_ptr< BlockCipher > cipher)
Definition gmac.cpp:19
~GMAC() override
std::string provider() const override
Definition gmac.cpp:43
std::unique_ptr< MessageAuthenticationCode > new_object() const override
Definition gmac.cpp:105
Key_Length_Specification key_spec() const override
Definition gmac.cpp:35
bool has_keying_material() const override
Definition gmac.cpp:58
void clear() override
Definition gmac.cpp:26
size_t output_length() const override
Definition gmac.cpp:47
void zeroise(std::vector< T, Alloc > &vec)
Definition secmem.h:241
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144