Botan  2.4.0
Crypto and TLS for C++11
aead.h
Go to the documentation of this file.
1 /*
2 * Interface for AEAD modes
3 * (C) 2013 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_AEAD_MODE_H_
9 #define BOTAN_AEAD_MODE_H_
10 
11 #include <botan/cipher_mode.h>
12 
13 namespace Botan {
14 
15 /**
16 * Interface for AEAD (Authenticated Encryption with Associated Data)
17 * modes. These modes provide both encryption and message
18 * authentication, and can authenticate additional per-message data
19 * which is not included in the ciphertext (for instance a sequence
20 * number).
21 */
23  {
24  public:
25  bool authenticated() const override { return true; }
26 
27  /**
28  * Set associated data that is not included in the ciphertext but
29  * that should be authenticated. Must be called after set_key and
30  * before start.
31  *
32  * Unless reset by another call, the associated data is kept
33  * between messages. Thus, if the AD does not change, calling
34  * once (after set_key) is the optimum.
35  *
36  * @param ad the associated data
37  * @param ad_len length of add in bytes
38  */
39  virtual void set_associated_data(const uint8_t ad[], size_t ad_len) = 0;
40 
41  /**
42  * Set associated data that is not included in the ciphertext but
43  * that should be authenticated. Must be called after set_key and
44  * before start.
45  *
46  * See @ref set_associated_data().
47  *
48  * @param ad the associated data
49  */
50  template<typename Alloc>
51  void set_associated_data_vec(const std::vector<uint8_t, Alloc>& ad)
52  {
53  set_associated_data(ad.data(), ad.size());
54  }
55 
56  /**
57  * Set associated data that is not included in the ciphertext but
58  * that should be authenticated. Must be called after set_key and
59  * before start.
60  *
61  * See @ref set_associated_data().
62  *
63  * @param ad the associated data
64  */
65  template<typename Alloc>
66  void set_ad(const std::vector<uint8_t, Alloc>& ad)
67  {
68  set_associated_data(ad.data(), ad.size());
69  }
70 
71  /**
72  * @return default AEAD nonce size (a commonly supported value among AEAD
73  * modes, and large enough that random collisions are unlikely)
74  */
75  size_t default_nonce_length() const override { return 12; }
76 
77  virtual ~AEAD_Mode() = default;
78  };
79 
80 /**
81 * Get an AEAD mode by name (eg "AES-128/GCM" or "Serpent/EAX")
82 * @param name AEAD name
83 * @param direction ENCRYPTION or DECRYPTION
84 */
85 BOTAN_PUBLIC_API(2,0) AEAD_Mode* get_aead(const std::string& name, Cipher_Dir direction);
86 
87 }
88 
89 #endif
size_t default_nonce_length() const override
Definition: aead.h:75
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
void set_ad(const std::vector< uint8_t, Alloc > &ad)
Definition: aead.h:66
bool authenticated() const override
Definition: aead.h:25
void set_associated_data_vec(const std::vector< uint8_t, Alloc > &ad)
Definition: aead.h:51
Definition: alg_id.cpp:13
AEAD_Mode * get_aead(const std::string &algo, Cipher_Dir dir)
Definition: aead.cpp:42