Botan  2.8.0
Crypto and TLS for C++11
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/sym_algo.h>
14 
15 namespace Botan {
16 
17 class BlockCipher;
18 class StreamCipher;
19 class GHASH;
20 
21 /**
22 * GCM Mode
23 */
24 class BOTAN_PUBLIC_API(2,0) GCM_Mode : public AEAD_Mode
25  {
26  public:
27  void set_associated_data(const uint8_t ad[], size_t ad_len) override;
28 
29  std::string name() const override;
30 
31  size_t update_granularity() const override;
32 
33  Key_Length_Specification key_spec() const override;
34 
35  bool valid_nonce_length(size_t len) const override;
36 
37  size_t tag_size() const override { return m_tag_size; }
38 
39  void clear() override;
40 
41  void reset() override;
42 
43  std::string provider() const override;
44  protected:
45  GCM_Mode(BlockCipher* cipher, size_t tag_size);
46 
47  ~GCM_Mode();
48 
49  static const size_t GCM_BS = 16;
50 
51  const size_t m_tag_size;
52  const std::string m_cipher_name;
53 
54  std::unique_ptr<StreamCipher> m_ctr;
55  std::unique_ptr<GHASH> m_ghash;
56  private:
57  void start_msg(const uint8_t nonce[], size_t nonce_len) override;
58 
59  void key_schedule(const uint8_t key[], size_t length) override;
60  };
61 
62 /**
63 * GCM Encryption
64 */
66  {
67  public:
68  /**
69  * @param cipher the 128 bit block cipher to use
70  * @param tag_size is how big the auth tag will be
71  */
72  GCM_Encryption(BlockCipher* cipher, size_t tag_size = 16) :
73  GCM_Mode(cipher, tag_size) {}
74 
75  size_t output_length(size_t input_length) const override
76  { return input_length + tag_size(); }
77 
78  size_t minimum_final_size() const override { return 0; }
79 
80  size_t process(uint8_t buf[], size_t size) override;
81 
82  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
83  };
84 
85 /**
86 * GCM Decryption
87 */
89  {
90  public:
91  /**
92  * @param cipher the 128 bit block cipher to use
93  * @param tag_size is how big the auth tag will be
94  */
95  GCM_Decryption(BlockCipher* cipher, size_t tag_size = 16) :
96  GCM_Mode(cipher, tag_size) {}
97 
98  size_t output_length(size_t input_length) const override
99  {
100  BOTAN_ASSERT(input_length >= tag_size(), "Sufficient input");
101  return input_length - tag_size();
102  }
103 
104  size_t minimum_final_size() const override { return tag_size(); }
105 
106  size_t process(uint8_t buf[], size_t size) override;
107 
108  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
109  };
110 
111 }
112 
113 #endif
std::unique_ptr< StreamCipher > m_ctr
Definition: gcm.h:54
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
GCM_Decryption(BlockCipher *cipher, size_t tag_size=16)
Definition: gcm.h:95
const size_t m_tag_size
Definition: gcm.h:51
size_t minimum_final_size() const override
Definition: gcm.h:78
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:55
std::string name
size_t output_length(size_t input_length) const override
Definition: gcm.h:75
GCM_Encryption(BlockCipher *cipher, size_t tag_size=16)
Definition: gcm.h:72
const std::string m_cipher_name
Definition: gcm.h:52
Definition: alg_id.cpp:13
size_t tag_size() const override
Definition: gcm.h:37
std::unique_ptr< GHASH > m_ghash
Definition: gcm.h:55
size_t minimum_final_size() const override
Definition: gcm.h:104
size_t output_length(size_t input_length) const override
Definition: gcm.h:98
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88