Botan 3.6.1
Crypto and TLS for C&
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#include <span>
14
15namespace Botan {
16
17/**
18* Interface for AEAD (Authenticated Encryption with Associated Data)
19* modes. These modes provide both encryption and message
20* authentication, and can authenticate additional per-message data
21* which is not included in the ciphertext (for instance a sequence
22* number).
23*/
25 public:
26 /**
27 * Create an AEAD mode
28 * @param algo the algorithm to create
29 * @param direction specify if this should be an encryption or decryption AEAD
30 * @param provider optional specification for provider to use
31 * @return an AEAD mode or a null pointer if not available
32 */
33 static std::unique_ptr<AEAD_Mode> create(std::string_view algo,
34 Cipher_Dir direction,
35 std::string_view provider = "");
36
37 /**
38 * Create an AEAD mode, or throw
39 * @param algo the algorithm to create
40 * @param direction specify if this should be an encryption or decryption AEAD
41 * @param provider optional specification for provider to use
42 * @return an AEAD mode, or throw an exception
43 */
44 static std::unique_ptr<AEAD_Mode> create_or_throw(std::string_view algo,
45 Cipher_Dir direction,
46 std::string_view provider = "");
47
48 /**
49 * Set associated data that is not included in the ciphertext but that
50 * should be authenticated. Must be called after set_key() and before
51 * start().
52 *
53 * Unless reset by another call, the associated data is kept between
54 * messages. Thus, if the AD does not change, calling once (after
55 * set_key()) is the optimum.
56 *
57 * @param ad the associated data
58 */
59 void set_associated_data(std::span<const uint8_t> ad) { set_associated_data_n(0, ad); }
60
61 void set_associated_data(const uint8_t ad[], size_t ad_len) { set_associated_data(std::span(ad, ad_len)); }
62
63 /**
64 * Set associated data that is not included in the ciphertext but
65 * that should be authenticated. Must be called after set_key() and
66 * before start().
67 *
68 * Unless reset by another call, the associated data is kept
69 * between messages. Thus, if the AD does not change, calling
70 * once (after set_key()) is the optimum.
71 *
72 * Some AEADs (namely SIV) support multiple AD inputs. For
73 * all other modes only nominal AD input 0 is supported; all
74 * other values of idx will cause an exception.
75 *
76 * Derived AEADs must implement this. For AEADs where
77 * `maximum_associated_data_inputs()` returns 1 (the default), the
78 * @p idx must simply be ignored.
79 *
80 * @param idx which associated data to set
81 * @param ad the associated data
82 */
83 virtual void set_associated_data_n(size_t idx, std::span<const uint8_t> ad) = 0;
84
85 /**
86 * Returns the maximum supported number of associated data inputs which
87 * can be provided to set_associated_data_n
88 *
89 * If returns 0, then no associated data is supported.
90 */
91 virtual size_t maximum_associated_data_inputs() const { return 1; }
92
93 /**
94 * Most AEADs require the key to be set prior to setting the AD
95 * A few allow the AD to be set even before the cipher is keyed.
96 * Such ciphers would return false from this function.
97 */
98 virtual bool associated_data_requires_key() const { return true; }
99
100 /**
101 * Set associated data that is not included in the ciphertext but
102 * that should be authenticated. Must be called after set_key() and
103 * before start().
104 *
105 * See @ref set_associated_data().
106 *
107 * @param ad the associated data
108 */
109 template <typename Alloc>
110 BOTAN_DEPRECATED("Simply use set_associated_data")
111 void set_associated_data_vec(const std::vector<uint8_t, Alloc>& ad) {
112 set_associated_data(ad);
113 }
114
115 /**
116 * Set associated data that is not included in the ciphertext but
117 * that should be authenticated. Must be called after set_key() and
118 * before start().
119 *
120 * See @ref set_associated_data().
121 *
122 * @param ad the associated data
123 */
124 BOTAN_DEPRECATED("Use set_associated_data") void set_ad(std::span<const uint8_t> ad) { set_associated_data(ad); }
125
126 /**
127 * @return default AEAD nonce size (a commonly supported value among AEAD
128 * modes, and large enough that random collisions are unlikely)
129 */
130 size_t default_nonce_length() const override { return 12; }
131
132 ~AEAD_Mode() override = default;
133};
134
135/**
136* Get an AEAD mode by name (eg "AES-128/GCM" or "Serpent/EAX")
137* @param name AEAD name
138* @param direction Cipher_Dir::Encryption or Cipher_Dir::Decryption
139*/
140BOTAN_DEPRECATED("Use AEAD_Mode::create") inline AEAD_Mode* get_aead(std::string_view name, Cipher_Dir direction) {
141 return AEAD_Mode::create(name, direction, "").release();
142}
143
144} // namespace Botan
145
146#endif
void set_associated_data(std::span< const uint8_t > ad)
Definition aead.h:59
virtual size_t maximum_associated_data_inputs() const
Definition aead.h:91
size_t default_nonce_length() const override
Definition aead.h:130
virtual void set_associated_data_n(size_t idx, std::span< const uint8_t > ad)=0
static std::unique_ptr< AEAD_Mode > create(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
Definition aead.cpp:53
void set_associated_data(const uint8_t ad[], size_t ad_len)
Definition aead.h:61
~AEAD_Mode() override=default
virtual bool associated_data_requires_key() const
Definition aead.h:98
std::string name
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
#define BOTAN_DEPRECATED(msg)
Definition compiler.h:125
AEAD_Mode * get_aead(std::string_view name, Cipher_Dir direction)
Definition aead.h:140