Botan 3.13.0
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/assert.h>
15#include <botan/block_cipher.h>
16#include <botan/sym_algo.h>
17
18namespace Botan {
19
20class StreamCipher;
21class GHASH;
22
23/**
24* GCM Mode
25*/
26class GCM_Mode : public AEAD_Mode /* NOLINT(*-special-member-functions) */ {
27 public:
28 void set_associated_data_n(size_t idx, std::span<const uint8_t> ad) final;
29
30 std::string name() const final;
31
32 size_t update_granularity() const final;
33
34 size_t ideal_granularity() const final;
35
37
38 bool valid_nonce_length(size_t len) const final;
39
40 size_t tag_size() const final { return m_tag_size; }
41
42 void clear() final;
43
44 void reset() final;
45
46 std::string provider() const final;
47
48 bool has_keying_material() const final;
49
50 ~GCM_Mode() override;
51
52 protected:
53 GCM_Mode(std::unique_ptr<BlockCipher> cipher, size_t tag_size);
54
55 static const size_t GCM_BS = 16;
56
57 const size_t m_tag_size; // NOLINT(*non-private-member-variable*)
58 const std::string m_cipher_name; // NOLINT(*non-private-member-variable*)
59
60 std::unique_ptr<StreamCipher> m_ctr; // NOLINT(*non-private-member-variable*)
61 std::unique_ptr<GHASH> m_ghash; // NOLINT(*non-private-member-variable*)
62 bool m_in_msg = false; // NOLINT(*non-private-member-variable*)
63
64 private:
65 void start_msg(const uint8_t nonce[], size_t nonce_len) override;
66
67 void key_schedule(std::span<const uint8_t> key) override;
68};
69
70/**
71* GCM Encryption
72*/
73class GCM_Encryption final : public GCM_Mode {
74 public:
75 /**
76 * @param cipher the 128 bit block cipher to use
77 * @param tag_size is how big the auth tag will be
78 */
79 explicit GCM_Encryption(std::unique_ptr<BlockCipher> cipher, size_t tag_size = 16) :
80 GCM_Mode(std::move(cipher), tag_size) {}
81
82 size_t output_length(size_t input_length) const override;
83
84 size_t minimum_final_size() const override { return 0; }
85
86 private:
87 size_t process_msg(uint8_t buf[], size_t size) override;
88 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
89};
90
91/**
92* GCM Decryption
93*/
94class GCM_Decryption final : public GCM_Mode {
95 public:
96 /**
97 * @param cipher the 128 bit block cipher to use
98 * @param tag_size is how big the auth tag will be
99 */
100 explicit GCM_Decryption(std::unique_ptr<BlockCipher> cipher, size_t tag_size = 16) :
101 GCM_Mode(std::move(cipher), tag_size) {}
102
103 size_t output_length(size_t input_length) const override;
104
105 size_t minimum_final_size() const override { return tag_size(); }
106
107 private:
108 size_t process_msg(uint8_t buf[], size_t size) override;
109 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
110};
111
112} // namespace Botan
113
114#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:105
GCM_Decryption(std::unique_ptr< BlockCipher > cipher, size_t tag_size=16)
Definition gcm.h:100
GCM_Encryption(std::unique_ptr< BlockCipher > cipher, size_t tag_size=16)
Definition gcm.h:79
size_t minimum_final_size() const override
Definition gcm.h:84
bool m_in_msg
Definition gcm.h:62
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:55
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:61
size_t tag_size() const final
Definition gcm.h:40
std::string name() const final
Definition gcm.cpp:55
const size_t m_tag_size
Definition gcm.h:57
GCM_Mode(std::unique_ptr< BlockCipher > cipher, size_t tag_size)
Definition gcm.cpp:26
const std::string m_cipher_name
Definition gcm.h:58
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:60
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