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