Botan 3.11.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
23 m_cipher->clear();
24 m_ghash->clear();
25 zeroise(m_H);
26 m_initialized = false;
27}
28
29GMAC::~GMAC() = default;
30
32 return m_cipher->key_spec();
33}
34
35std::string GMAC::name() const {
36 return fmt("GMAC({})", m_cipher->name());
37}
38
39std::string GMAC::provider() const {
40 return m_ghash->provider();
41}
42
43size_t GMAC::output_length() const {
44 return GCM_BS;
45}
46
47void GMAC::add_data(std::span<const uint8_t> input) {
48 m_ghash->update_associated_data(input);
49}
50
52 return m_cipher->has_keying_material();
53}
54
55void GMAC::key_schedule(std::span<const uint8_t> key) {
56 clear();
57 m_cipher->set_key(key);
58
59 m_cipher->encrypt(m_H);
60 m_ghash->set_key(m_H);
61}
62
63void GMAC::start_msg(std::span<const uint8_t> nonce) {
64 std::array<uint8_t, GCM_BS> y0 = {0};
65
66 if(nonce.size() == 12) {
67 copy_mem(y0.data(), nonce.data(), nonce.size());
68 y0[GCM_BS - 1] = 1;
69 } else {
70 m_ghash->nonce_hash(y0, nonce);
71 }
72
73 m_cipher->encrypt(y0.data());
74 m_ghash->start(y0);
75 m_initialized = true;
76}
77
78void GMAC::final_result(std::span<uint8_t> mac) {
79 // This ensures the GMAC computation has been initialized with a fresh
80 // nonce. The aim of this check is to prevent developers from re-using
81 // nonces (and potential nonce-reuse attacks).
82 if(!m_initialized) {
83 throw Invalid_State("GMAC was not used with a fresh nonce");
84 }
85
86 m_ghash->final(mac.first(output_length()));
87 m_ghash->reset_associated_data();
88}
89
90std::unique_ptr<MessageAuthenticationCode> GMAC::new_object() const {
91 return std::make_unique<GMAC>(m_cipher->new_object());
92}
93} // namespace Botan
std::string name() const override
Definition gmac.cpp:35
GMAC(std::unique_ptr< BlockCipher > cipher)
Definition gmac.cpp:19
~GMAC() override
std::string provider() const override
Definition gmac.cpp:39
std::unique_ptr< MessageAuthenticationCode > new_object() const override
Definition gmac.cpp:90
Key_Length_Specification key_spec() const override
Definition gmac.cpp:31
bool has_keying_material() const override
Definition gmac.cpp:51
void clear() override
Definition gmac.cpp:22
size_t output_length() const override
Definition gmac.cpp:43
void zeroise(std::vector< T, Alloc > &vec)
Definition secmem.h:124
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53