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