Botan 3.14.1
Crypto and TLS for C&
gcm.h
Go to the documentation of this file.
1/*
2* GCM Mode
3* (C) 2013 Jack Lloyd
4* (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#ifndef BOTAN_AEAD_GCM_H_
10#define BOTAN_AEAD_GCM_H_
11
12#include <botan/aead.h>
13
14#include <botan/block_cipher.h>
15#include <botan/sym_algo.h>
16
17namespace Botan {
18
19class StreamCipher;
20class GHASH;
21
22/**
23* GCM Mode
24*/
25class GCM_Mode : public AEAD_Mode /* NOLINT(*-special-member-functions) */ {
26 public:
27 void set_associated_data_n(size_t idx, std::span<const uint8_t> ad) final;
28
29 std::string name() const final;
30
31 size_t update_granularity() const final;
32
33 size_t ideal_granularity() const final;
34
36
37 bool valid_nonce_length(size_t len) const final;
38
39 size_t tag_size() const final { return m_tag_size; }
40
41 void clear() final;
42
43 void reset() final;
44
45 std::string provider() const final;
46
47 bool has_keying_material() const final;
48
49 ~GCM_Mode() override;
50
51 protected:
52 GCM_Mode(std::unique_ptr<BlockCipher> cipher, size_t tag_size);
53
54 static const size_t GCM_BS = 16;
55
56 const size_t m_tag_size; // NOLINT(*non-private-member-variable*)
57 const std::string m_cipher_name; // NOLINT(*non-private-member-variable*)
58
59 std::unique_ptr<StreamCipher> m_ctr; // NOLINT(*non-private-member-variable*)
60 std::unique_ptr<GHASH> m_ghash; // NOLINT(*non-private-member-variable*)
61 bool m_in_msg = false; // NOLINT(*non-private-member-variable*)
62
63 private:
64 void start_msg(const uint8_t nonce[], size_t nonce_len) override;
65
66 void key_schedule(std::span<const uint8_t> key) override;
67};
68
69/**
70* GCM Encryption
71*/
72class GCM_Encryption final : public GCM_Mode {
73 public:
74 /**
75 * @param cipher the 128 bit block cipher to use
76 * @param tag_size is how big the auth tag will be
77 */
78 explicit GCM_Encryption(std::unique_ptr<BlockCipher> cipher, size_t tag_size = 16) :
79 GCM_Mode(std::move(cipher), tag_size) {}
80
81 size_t output_length(size_t input_length) const override;
82
83 size_t minimum_final_size() const override { return 0; }
84
85 private:
86 size_t process_msg(uint8_t buf[], size_t size) override;
87 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
88};
89
90/**
91* GCM Decryption
92*/
93class GCM_Decryption final : public GCM_Mode {
94 public:
95 /**
96 * @param cipher the 128 bit block cipher to use
97 * @param tag_size is how big the auth tag will be
98 */
99 explicit GCM_Decryption(std::unique_ptr<BlockCipher> cipher, size_t tag_size = 16) :
100 GCM_Mode(std::move(cipher), tag_size) {}
101
102 size_t output_length(size_t input_length) const override;
103
104 size_t minimum_final_size() const override { return tag_size(); }
105
106 private:
107 size_t process_msg(uint8_t buf[], size_t size) override;
108 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
109};
110
111} // namespace Botan
112
113#endif
virtual void finish_msg(secure_vector< uint8_t > &final_block, size_t offset=0)=0
virtual size_t output_length(size_t input_length) const =0
virtual size_t process_msg(uint8_t msg[], size_t msg_len)=0
size_t minimum_final_size() const override
Definition gcm.h:104
GCM_Decryption(std::unique_ptr< BlockCipher > cipher, size_t tag_size=16)
Definition gcm.h:99
GCM_Encryption(std::unique_ptr< BlockCipher > cipher, size_t tag_size=16)
Definition gcm.h:78
size_t minimum_final_size() const override
Definition gcm.h:83
bool m_in_msg
Definition gcm.h:61
std::string provider() const final
Definition gcm.cpp:59
Key_Length_Specification key_spec() const final
Definition gcm.cpp:76
static const size_t GCM_BS
Definition gcm.h:54
void set_associated_data_n(size_t idx, std::span< const uint8_t > ad) final
Definition gcm.cpp:96
std::unique_ptr< GHASH > m_ghash
Definition gcm.h:60
size_t tag_size() const final
Definition gcm.h:39
std::string name() const final
Definition gcm.cpp:55
const size_t m_tag_size
Definition gcm.h:56
GCM_Mode(std::unique_ptr< BlockCipher > cipher, size_t tag_size)
Definition gcm.cpp:26
const std::string m_cipher_name
Definition gcm.h:57
size_t ideal_granularity() const final
Definition gcm.cpp:67
bool valid_nonce_length(size_t len) const final
Definition gcm.cpp:71
bool has_keying_material() const final
Definition gcm.cpp:80
size_t update_granularity() const final
Definition gcm.cpp:63
std::unique_ptr< StreamCipher > m_ctr
Definition gcm.h:59
void clear() final
Definition gcm.cpp:44
void reset() final
Definition gcm.cpp:50
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128