Botan 3.5.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#include <botan/block_cipher.h>
14#include <botan/sym_algo.h>
15
16namespace Botan {
17
18class StreamCipher;
19class GHASH;
20
21/**
22* GCM Mode
23*/
24class GCM_Mode : public AEAD_Mode {
25 public:
26 void set_associated_data_n(size_t idx, std::span<const uint8_t> ad) override final;
27
28 std::string name() const override final;
29
30 size_t update_granularity() const override final;
31
32 size_t ideal_granularity() const override final;
33
34 Key_Length_Specification key_spec() const override final;
35
36 bool valid_nonce_length(size_t len) const override final;
37
38 size_t tag_size() const override final { return m_tag_size; }
39
40 void clear() override final;
41
42 void reset() override final;
43
44 std::string provider() const override final;
45
46 bool has_keying_material() const override final;
47
49
50 protected:
51 GCM_Mode(std::unique_ptr<BlockCipher> cipher, size_t tag_size);
52
53 static const size_t GCM_BS = 16;
54
55 const size_t m_tag_size;
56 const std::string m_cipher_name;
57
58 std::unique_ptr<StreamCipher> m_ctr;
59 std::unique_ptr<GHASH> m_ghash;
60
61 private:
62 void start_msg(const uint8_t nonce[], size_t nonce_len) override;
63
64 void key_schedule(std::span<const uint8_t> key) override;
65
66 secure_vector<uint8_t> m_y0;
67};
68
69/**
70* GCM Encryption
71*/
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 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 { return input_length + tag_size(); }
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*/
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 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 BOTAN_ARG_CHECK(input_length >= tag_size(), "Sufficient input");
104 return input_length - tag_size();
105 }
106
107 size_t minimum_final_size() const override { return tag_size(); }
108
109 private:
110 size_t process_msg(uint8_t buf[], size_t size) override;
111 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
112};
113
114} // namespace Botan
115
116#endif
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
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:107
size_t output_length(size_t input_length) const override
Definition gcm.h:102
GCM_Decryption(std::unique_ptr< BlockCipher > cipher, size_t tag_size=16)
Definition gcm.h:99
size_t output_length(size_t input_length) const override
Definition gcm.h:81
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
static const size_t GCM_BS
Definition gcm.h:53
std::unique_ptr< GHASH > m_ghash
Definition gcm.h:59
size_t tag_size() const override final
Definition gcm.h:38
std::string provider() const override final
Definition gcm.cpp:56
const size_t m_tag_size
Definition gcm.h:55
size_t update_granularity() const override final
Definition gcm.cpp:60
Key_Length_Specification key_spec() const override final
Definition gcm.cpp:73
const std::string m_cipher_name
Definition gcm.h:56
bool has_keying_material() const override final
Definition gcm.cpp:77
void clear() override final
Definition gcm.cpp:42
void reset() override final
Definition gcm.cpp:48
size_t ideal_granularity() const override final
Definition gcm.cpp:64
std::unique_ptr< StreamCipher > m_ctr
Definition gcm.h:58
void set_associated_data_n(size_t idx, std::span< const uint8_t > ad) override final
Definition gcm.cpp:92
std::string name() const override final
Definition gcm.cpp:52
bool valid_nonce_length(size_t len) const override final
Definition gcm.cpp:68
int(* final)(unsigned char *, CTX *)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61