Botan 3.9.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
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 secure_vector<uint8_t> m_y0;
69};
70
71/**
72* GCM Encryption
73*/
74class GCM_Encryption final : public GCM_Mode {
75 public:
76 /**
77 * @param cipher the 128 bit block cipher to use
78 * @param tag_size is how big the auth tag will be
79 */
80 explicit GCM_Encryption(std::unique_ptr<BlockCipher> cipher, size_t tag_size = 16) :
81 GCM_Mode(std::move(cipher), tag_size) {}
82
83 size_t output_length(size_t input_length) const override { return input_length + tag_size(); }
84
85 size_t minimum_final_size() const override { return 0; }
86
87 private:
88 size_t process_msg(uint8_t buf[], size_t size) override;
89 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
90};
91
92/**
93* GCM Decryption
94*/
95class GCM_Decryption final : public GCM_Mode {
96 public:
97 /**
98 * @param cipher the 128 bit block cipher to use
99 * @param tag_size is how big the auth tag will be
100 */
101 explicit GCM_Decryption(std::unique_ptr<BlockCipher> cipher, size_t tag_size = 16) :
102 GCM_Mode(std::move(cipher), tag_size) {}
103
104 size_t output_length(size_t input_length) const override {
105 BOTAN_ARG_CHECK(input_length >= tag_size(), "Sufficient input");
106 return input_length - tag_size();
107 }
108
109 size_t minimum_final_size() const override { return tag_size(); }
110
111 private:
112 size_t process_msg(uint8_t buf[], size_t size) override;
113 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
114};
115
116} // namespace Botan
117
118#endif
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
virtual void finish_msg(secure_vector< uint8_t > &final_block, size_t offset=0)=0
virtual size_t process_msg(uint8_t msg[], size_t msg_len)=0
size_t minimum_final_size() const override
Definition gcm.h:109
size_t output_length(size_t input_length) const override
Definition gcm.h:104
GCM_Decryption(std::unique_ptr< BlockCipher > cipher, size_t tag_size=16)
Definition gcm.h:101
size_t output_length(size_t input_length) const override
Definition gcm.h:83
GCM_Encryption(std::unique_ptr< BlockCipher > cipher, size_t tag_size=16)
Definition gcm.h:80
size_t minimum_final_size() const override
Definition gcm.h:85
std::string provider() const final
Definition gcm.cpp:56
Key_Length_Specification key_spec() const final
Definition gcm.cpp:73
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:92
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:52
const size_t m_tag_size
Definition gcm.h:57
GCM_Mode(std::unique_ptr< BlockCipher > cipher, size_t tag_size)
Definition gcm.cpp:24
const std::string m_cipher_name
Definition gcm.h:58
size_t ideal_granularity() const final
Definition gcm.cpp:64
bool valid_nonce_length(size_t len) const final
Definition gcm.cpp:68
bool has_keying_material() const final
Definition gcm.cpp:77
size_t update_granularity() const final
Definition gcm.cpp:60
std::unique_ptr< StreamCipher > m_ctr
Definition gcm.h:60
void clear() final
Definition gcm.cpp:42
void reset() final
Definition gcm.cpp:48
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:69