Botan  2.11.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 */
65 class BOTAN_PUBLIC_API(2,0) GCM_Encryption final : public GCM_Mode
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 */
88 class BOTAN_PUBLIC_API(2,0) GCM_Decryption final : public GCM_Mode
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
size_t * tag_size
Definition: ffi.h:450
std::string size_t len
Definition: pk_keys.h:305
bool BigInt BigInt size_t size_t const std::vector< uint8_t > size_t offset
Definition: numthry.h:271
size_t const uint8_t size_t input_length
Definition: base32.h:30
const uint8_t size_t nonce_len
Definition: ffi.h:504
const uint8_t * buf
Definition: ffi.h:371
size_t * output_length
Definition: ffi.h:280
int(* final)(unsigned char *, CTX *)
char * name
Definition: ffi.h:330
class BOTAN_PUBLIC_API(2, 11) Argon2 final class BOTAN_PUBLIC_API(2, 11) Argon2_Family final void size_t const char size_t const uint8_t size_t const uint8_t size_t const uint8_t size_t ad_len
Definition: argon2.h:87
std::string m_cipher_name
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:55
class BOTAN_PUBLIC_API(2, 11) Argon2 final class BOTAN_PUBLIC_API(2, 11) Argon2_Family final void size_t const char size_t const uint8_t size_t const uint8_t size_t const uint8_t ad[]
Definition: argon2.h:87
void BlockCipher * cipher
Definition: package.h:29
class BOTAN_PUBLIC_API(2, 11) Argon2 final class BOTAN_PUBLIC_API(2, 11) Argon2_Family final void size_t const char size_t const uint8_t size_t const uint8_t key[]
Definition: argon2.h:87
Definition: alg_id.cpp:13
class BOTAN_PUBLIC_API(2, 0) AlgorithmIdentifier final bool BOTAN_PUBLIC_API(2, 0) operator
Name Constraints.
Definition: asn1_obj.h:66
const uint8_t * nonce
Definition: ffi.h:504