Botan 3.13.0
Crypto and TLS for C&
stream_cipher.h
Go to the documentation of this file.
1/*
2* Stream Cipher
3* (C) 1999-2007 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_STREAM_CIPHER_H_
9#define BOTAN_STREAM_CIPHER_H_
10
11#include <botan/concepts.h>
12#include <botan/secmem.h>
13#include <botan/sym_algo.h>
14#include <memory>
15#include <optional>
16#include <string>
17#include <string_view>
18#include <vector>
19
20namespace Botan {
21
22/**
23* Base class for all stream ciphers
24*/
26 public:
27 /**
28 * Create an instance based on a name
29 * If provider is empty then best available is chosen.
30 * @param algo_spec algorithm name
31 * @param provider provider implementation to use
32 * @return a null pointer if the algo/provider combination cannot be found
33 */
34 static std::unique_ptr<StreamCipher> create(std::string_view algo_spec, std::string_view provider = "");
35
36 /**
37 * Create an instance based on a name
38 * If provider is empty then best available is chosen.
39 * @param algo_spec algorithm name
40 * @param provider provider implementation to use
41 * Throws a Lookup_Error if the algo/provider combination cannot be found
42 */
43 static std::unique_ptr<StreamCipher> create_or_throw(std::string_view algo_spec, std::string_view provider = "");
44
45 /**
46 * List the providers available for a given stream cipher
47 * @return list of available providers for this algorithm, empty if not available
48 */
49 static std::vector<std::string> providers(std::string_view algo_spec);
50
51 /**
52 * Encrypt or decrypt a message
53 *
54 * Processes all bytes plain/ciphertext from @p in and writes the result to
55 * @p out.
56 *
57 * @param in the plaintext
58 * @param out the byte array to hold the output, i.e. the ciphertext
59 * @param len the length of both in and out in bytes
60 */
61 void cipher(const uint8_t in[], uint8_t out[], size_t len) { cipher_bytes(in, out, len); }
62
63 /**
64 * Encrypt or decrypt a message
65 * @param in the plaintext
66 * @param out the byte array to hold the output, i.e. the ciphertext
67 * with at least the same size as @p in
68 */
69 void cipher(std::span<const uint8_t> in, std::span<uint8_t> out);
70
71 /**
72 * Write keystream bytes to a buffer
73 *
74 * The contents of @p out are ignored/overwritten
75 *
76 * @param out the byte array to hold the keystream
77 * @param len the length of out in bytes
78 */
79 void write_keystream(uint8_t out[], size_t len) { generate_keystream(out, len); }
80
81 /**
82 * Fill a given buffer with keystream bytes
83 *
84 * The contents of @p out are ignored/overwritten
85 *
86 * @param out the byte array to hold the keystream
87 */
88 void write_keystream(std::span<uint8_t> out) { generate_keystream(out.data(), out.size()); }
89
90 /**
91 * Get @p bytes from the keystream
92 *
93 * The bytes are written into a continuous byte buffer of your choosing.
94 *
95 * @param bytes The number of bytes to be produced
96 */
97 template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
98 T keystream_bytes(size_t bytes) {
99 T out(bytes);
100 write_keystream(out);
101 return out;
102 }
103
104 /**
105 * Encrypt or decrypt a message
106 * The message is encrypted/decrypted in place.
107 * @param buf the plaintext / ciphertext
108 * @param len the length of buf in bytes
109 */
110 void cipher1(uint8_t buf[], size_t len) { cipher(buf, buf, len); }
111
112 /**
113 * Encrypt or decrypt a message
114 * The message is encrypted/decrypted in place.
115 * @param buf the plaintext / ciphertext
116 */
117 void cipher1(std::span<uint8_t> buf) { cipher(buf, buf); }
118
119 /**
120 * Encrypt a message
121 * The message is encrypted/decrypted in place.
122 * @param inout the plaintext / ciphertext
123 */
124 void encipher(std::span<uint8_t> inout) { cipher(inout.data(), inout.data(), inout.size()); }
125
126 /**
127 * Encrypt a message
128 * The message is encrypted in place.
129 * @param inout the plaintext / ciphertext
130 */
131 void encrypt(std::span<uint8_t> inout) { cipher(inout.data(), inout.data(), inout.size()); }
132
133 /**
134 * Decrypt a message in place
135 * The message is decrypted in place.
136 * @param inout the plaintext / ciphertext
137 */
138 void decrypt(std::span<uint8_t> inout) { cipher(inout.data(), inout.data(), inout.size()); }
139
140 /**
141 * Return the optimium buffer size to use with this cipher
142 *
143 * Most stream ciphers internally produce blocks of bytes. This function
144 * returns that block size. Aligning buffer sizes to a multiple of this
145 * size may improve performance by reducing internal buffering overhead.
146 *
147 * Note the return value of this function may change for any particular
148 * algorithm due to changes in the implementation from release to release,
149 * or changes in the runtime environment (such as CPUID indicating
150 * availability of an optimized implementation). It is not intrinsic to
151 * the algorithm; it is just a suggestion for gaining best performance.
152 */
153 virtual size_t buffer_size() const = 0;
154
155 /**
156 * Resync the cipher using the IV
157 *
158 * Load @p IV into the stream cipher state. This should happen after the
159 * key is set (set_key()) and before any operation (encrypt(), decrypt() or
160 * seek()) is called.
161 *
162 * If the cipher does not support IVs, then a call with an empty IV will be
163 * accepted and any other length will cause an Invalid_IV_Length exception.
164 *
165 * @param iv the initialization vector
166 * @param iv_len the length of the IV in bytes
167 */
168 void set_iv(const uint8_t iv[], size_t iv_len) { set_iv_bytes(iv, iv_len); }
169
170 /**
171 * Resync the cipher using the IV
172 * @param iv the initialization vector
173 * @throws Invalid_IV_Length if an incompatible IV was passed.
174 */
175 void set_iv(std::span<const uint8_t> iv) { set_iv_bytes(iv.data(), iv.size()); }
176
177 /**
178 * Return the default (preferred) nonce length
179 *
180 * If this function returns zero, then this cipher does not support nonces;
181 * in this case any call to set_iv with a (non-empty) value will fail.
182 *
183 * Default implementation returns 0
184 */
185 virtual size_t default_iv_length() const;
186
187 /**
188 * Test if a nonce length is valid for this cipher
189 * @param iv_len the length of the IV in bytes
190 * @return if the length is valid for this algorithm
191 */
192 virtual bool valid_iv_length(size_t iv_len) const { return (iv_len == 0); }
193
194 /**
195 * Create a new uninitialized object of the same type
196 * @return a new object representing the same algorithm as *this
197 */
198 StreamCipher* clone() const { return this->new_object().release(); }
199
200 /**
201 * Create a new uninitialized object of the same type
202 * @return new object representing the same algorithm as *this
203 */
204 virtual std::unique_ptr<StreamCipher> new_object() const = 0;
205
206 /**
207 * Set the offset and the state used later to generate the keystream
208 *
209 * Sets the state of the stream cipher and keystream according to the
210 * passed @p offset, exactly as if @p offset bytes had first been
211 * encrypted. The key and (if required) the IV have to be set before this
212 * can be called.
213 *
214 * @note Not all ciphers support seeking; such objects will throw
215 * Not_Implemented in this case. Use supports_seek() to query
216 * in advance.
217 *
218 * @param offset the offset where we begin to generate the keystream
219 */
220 virtual void seek(uint64_t offset) = 0;
221
222 /**
223 * Test whether this cipher supports seeking within the keystream
224 * @return true if this cipher implements seek(); false if seek() will
225 * throw Not_Implemented for any offset.
226 */
227 virtual bool supports_seek() const = 0;
228
229 /**
230 * Many stream ciphers are internally based on encrypting a counter of some
231 * kind. If the counter wraps around, keystream bytes would be repeated.
232 *
233 * This function returns the number of keystream bytes that can still be
234 * produced under the current key/nonce settings, if that limit fits in a
235 * uint64_t. If there is no specific limit (eg due to being based on
236 * permutations rather than a counter), or if the limit is at least 2**64
237 * bytes (where consuming the entire keystream is not practically
238 * possible), then this function returns nullopt.
239 *
240 * Note this returns nullopt if no key is set (there are no keystream bytes
241 * at all available, in that state) or potentially if the nonce is not set
242 * (as in some cases, such as ChaCha, the available counter bytes vary
243 * depending on the size of the nonce used).
244 */
245 virtual std::optional<uint64_t> remaining_keystream_bytes() const = 0;
246
247 /**
248 * Return the name of the provider implementing this object
249 * @return provider information about this implementation. Default is "base",
250 * might also return "sse2", "avx2" or some other arbitrary string.
251 */
252 virtual std::string provider() const { return "base"; }
253
254 protected:
255 /**
256 * Encrypt or decrypt a message
257 */
258 virtual void cipher_bytes(const uint8_t in[], uint8_t out[], size_t len) = 0;
259
260 /**
261 * Write keystream bytes to a buffer
262 */
263 virtual void generate_keystream(uint8_t out[], size_t len);
264
265 /**
266 * Resync the cipher using the IV
267 */
268 virtual void set_iv_bytes(const uint8_t iv[], size_t iv_len) = 0;
269};
270
271} // namespace Botan
272
273#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
virtual size_t buffer_size() const =0
void cipher1(uint8_t buf[], size_t len)
T keystream_bytes(size_t bytes)
void set_iv(const uint8_t iv[], size_t iv_len)
static std::unique_ptr< StreamCipher > create_or_throw(std::string_view algo_spec, std::string_view provider="")
virtual void set_iv_bytes(const uint8_t iv[], size_t iv_len)=0
static std::unique_ptr< StreamCipher > create(std::string_view algo_spec, std::string_view provider="")
void decrypt(std::span< uint8_t > inout)
void encipher(std::span< uint8_t > inout)
virtual bool supports_seek() const =0
virtual std::optional< uint64_t > remaining_keystream_bytes() const =0
StreamCipher * clone() const
void set_iv(std::span< const uint8_t > iv)
void write_keystream(std::span< uint8_t > out)
virtual std::unique_ptr< StreamCipher > new_object() const =0
void write_keystream(uint8_t out[], size_t len)
void encrypt(std::span< uint8_t > inout)
virtual void seek(uint64_t offset)=0
virtual void generate_keystream(uint8_t out[], size_t len)
void cipher1(std::span< uint8_t > buf)
virtual bool valid_iv_length(size_t iv_len) const
virtual void cipher_bytes(const uint8_t in[], uint8_t out[], size_t len)=0
static std::vector< std::string > providers(std::string_view algo_spec)
void cipher(const uint8_t in[], uint8_t out[], size_t len)
virtual std::string provider() const