Botan 3.13.0
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 /**
62 * Set associated data that is not included in the ciphertext but
63 * that should be authenticated. Must be called after set_key() and
64 * before start().
65 *
66 * @param ad the associated data
67 * @param ad_len length of ad in bytes
68 */
69 void set_associated_data(const uint8_t ad[], size_t ad_len) { set_associated_data(std::span(ad, ad_len)); }
70
71 /**
72 * Set associated data that is not included in the ciphertext but
73 * that should be authenticated. Must be called after set_key() and
74 * before start().
75 *
76 * Unless reset by another call, the associated data is kept
77 * between messages. Thus, if the AD does not change, calling
78 * once (after set_key()) is the optimum.
79 *
80 * Some AEADs (namely SIV) support multiple AD inputs. For
81 * all other modes only nominal AD input 0 is supported; all
82 * other values of idx will cause an exception.
83 *
84 * Derived AEADs must implement this. For AEADs where
85 * `maximum_associated_data_inputs()` returns 1 (the default), the
86 * @p idx must simply be ignored.
87 *
88 * @param idx which associated data to set
89 * @param ad the associated data
90 */
91 virtual void set_associated_data_n(size_t idx, std::span<const uint8_t> ad) = 0;
92
93 /**
94 * Returns the maximum supported number of associated data inputs which
95 * can be provided to set_associated_data_n
96 *
97 * If returns 0, then no associated data is supported.
98 */
99 virtual size_t maximum_associated_data_inputs() const { return 1; }
100
101 /**
102 * Most AEADs require the key to be set prior to setting the AD
103 * A few allow the AD to be set even before the cipher is keyed.
104 * Such ciphers would return false from this function.
105 */
106 virtual bool associated_data_requires_key() const { return true; }
107
108 /**
109 * Set associated data that is not included in the ciphertext but
110 * that should be authenticated. Must be called after set_key() and
111 * before start().
112 *
113 * See @ref set_associated_data().
114 *
115 * @param ad the associated data
116 */
117 template <typename Alloc>
118 BOTAN_DEPRECATED("Simply use set_associated_data")
119 void set_associated_data_vec(const std::vector<uint8_t, Alloc>& ad) {
121 }
122
123 /**
124 * Set associated data that is not included in the ciphertext but
125 * that should be authenticated. Must be called after set_key() and
126 * before start().
127 *
128 * See @ref set_associated_data().
129 *
130 * @param ad the associated data
131 */
132 BOTAN_DEPRECATED("Use set_associated_data") void set_ad(std::span<const uint8_t> ad) { set_associated_data(ad); }
133
134 /**
135 * Return the default nonce length for this mode
136 * @return default AEAD nonce size (a commonly supported value among AEAD
137 * modes, and large enough that random collisions are unlikely)
138 */
139 size_t default_nonce_length() const override { return 12; }
140};
141
142/**
143* Get an AEAD mode by name (eg "AES-128/GCM" or "Serpent/EAX")
144* @param name AEAD name
145* @param direction Cipher_Dir::Encryption or Cipher_Dir::Decryption
146*/
147BOTAN_DEPRECATED("Use AEAD_Mode::create") inline AEAD_Mode* get_aead(std::string_view name, Cipher_Dir direction) {
148 return AEAD_Mode::create(name, direction, "").release();
149}
150
151} // namespace Botan
152
153#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_DEPRECATED(msg)
Definition api.h:73
void set_associated_data(std::span< const uint8_t > ad)
Definition aead.h:59
static std::unique_ptr< AEAD_Mode > create_or_throw(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
Definition aead.cpp:53
virtual size_t maximum_associated_data_inputs() const
Definition aead.h:99
void set_associated_data_vec(const std::vector< uint8_t, Alloc > &ad)
Definition aead.h:119
size_t default_nonce_length() const override
Definition aead.h:139
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:63
void set_associated_data(const uint8_t ad[], size_t ad_len)
Definition aead.h:69
virtual bool associated_data_requires_key() const
Definition aead.h:106
void set_ad(std::span< const uint8_t > ad)
Definition aead.h:132
virtual std::string provider() const
AEAD_Mode * get_aead(std::string_view name, Cipher_Dir direction)
Definition aead.h:147