Botan 3.6.1
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/sym_algo.h>
13#include <memory>
14#include <string>
15#include <string_view>
16#include <vector>
17
18namespace Botan {
19
20/**
21* Base class for all stream ciphers
22*/
24 public:
25 ~StreamCipher() override = default;
26
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 * @return list of available providers for this algorithm, empty if not available
47 */
48 static std::vector<std::string> providers(std::string_view algo_spec);
49
50 /**
51 * Encrypt or decrypt a message
52 *
53 * Processes all bytes plain/ciphertext from @p in and writes the result to
54 * @p out.
55 *
56 * @param in the plaintext
57 * @param out the byte array to hold the output, i.e. the ciphertext
58 * @param len the length of both in and out in bytes
59 */
60 void cipher(const uint8_t in[], uint8_t out[], size_t len) { cipher_bytes(in, out, len); }
61
62 /**
63 * Encrypt or decrypt a message
64 * @param in the plaintext
65 * @param out the byte array to hold the output, i.e. the ciphertext
66 * with at least the same size as @p in
67 */
68 void cipher(std::span<const uint8_t> in, std::span<uint8_t> out) {
69 BOTAN_ARG_CHECK(in.size() <= out.size(),
70 "Output buffer of stream cipher must be at least as long as input buffer");
71 cipher_bytes(in.data(), out.data(), in.size());
72 }
73
74 /**
75 * Write keystream bytes to a buffer
76 *
77 * The contents of @p out are ignored/overwritten
78 *
79 * @param out the byte array to hold the keystream
80 * @param len the length of out in bytes
81 */
82 void write_keystream(uint8_t out[], size_t len) { generate_keystream(out, len); }
83
84 /**
85 * Fill a given buffer with keystream bytes
86 *
87 * The contents of @p out are ignored/overwritten
88 *
89 * @param out the byte array to hold the keystream
90 */
91 void write_keystream(std::span<uint8_t> out) { generate_keystream(out.data(), out.size()); }
92
93 /**
94 * Get @p bytes from the keystream
95 *
96 * The bytes are written into a continous byte buffer of your choosing.
97 *
98 * @param bytes The number of bytes to be produced
99 */
100 template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
101 T keystream_bytes(size_t bytes) {
102 T out(bytes);
103 write_keystream(out);
104 return out;
105 }
106
107 /**
108 * Encrypt or decrypt a message
109 * The message is encrypted/decrypted in place.
110 * @param buf the plaintext / ciphertext
111 * @param len the length of buf in bytes
112 */
113 void cipher1(uint8_t buf[], size_t len) { cipher(buf, buf, len); }
114
115 /**
116 * Encrypt or decrypt a message
117 * The message is encrypted/decrypted in place.
118 * @param buf the plaintext / ciphertext
119 */
120 void cipher1(std::span<uint8_t> buf) { cipher(buf, buf); }
121
122 /**
123 * Encrypt a message
124 * The message is encrypted/decrypted in place.
125 * @param inout the plaintext / ciphertext
126 */
127 void encipher(std::span<uint8_t> inout) { cipher(inout.data(), inout.data(), inout.size()); }
128
129 /**
130 * Encrypt a message
131 * The message is encrypted in place.
132 * @param inout the plaintext / ciphertext
133 */
134 void encrypt(std::span<uint8_t> inout) { cipher(inout.data(), inout.data(), inout.size()); }
135
136 /**
137 * Decrypt a message in place
138 * The message is decrypted in place.
139 * @param inout the plaintext / ciphertext
140 */
141 void decrypt(std::span<uint8_t> inout) { cipher(inout.data(), inout.data(), inout.size()); }
142
143 /**
144 * Return the optimium buffer size to use with this cipher
145 *
146 * Most stream ciphers internally produce blocks of bytes. This function
147 * returns that block size. Aligning buffer sizes to a multiple of this
148 * size may improve performance by reducing internal buffering overhead.
149 *
150 * Note the return value of this function may change for any particular
151 * algorithm due to changes in the implementation from release to release,
152 * or changes in the runtime environment (such as CPUID indicating
153 * availability of an optimized implementation). It is not intrinsic to
154 * the algorithm; it is just a suggestion for gaining best performance.
155 */
156 virtual size_t buffer_size() const = 0;
157
158 /**
159 * Resync the cipher using the IV
160 *
161 * Load @p IV into the stream cipher state. This should happen after the
162 * key is set (set_key()) and before any operation (encrypt(), decrypt() or
163 * seek()) is called.
164 *
165 * If the cipher does not support IVs, then a call with an empty IV will be
166 * accepted and any other length will cause an Invalid_IV_Length exception.
167 *
168 * @param iv the initialization vector
169 * @param iv_len the length of the IV in bytes
170 */
171 void set_iv(const uint8_t iv[], size_t iv_len) { set_iv_bytes(iv, iv_len); }
172
173 /**
174 * Resync the cipher using the IV
175 * @param iv the initialization vector
176 * @throws Invalid_IV_Length if an incompatible IV was passed.
177 */
178 void set_iv(std::span<const uint8_t> iv) { set_iv_bytes(iv.data(), iv.size()); }
179
180 /**
181 * Return the default (preferred) nonce length
182 *
183 * If this function returns zero, then this cipher does not support nonces;
184 * in this case any call to set_iv with a (non-empty) value will fail.
185 *
186 * Default implementation returns 0
187 */
188 virtual size_t default_iv_length() const;
189
190 /**
191 * @param iv_len the length of the IV in bytes
192 * @return if the length is valid for this algorithm
193 */
194 virtual bool valid_iv_length(size_t iv_len) const { return (iv_len == 0); }
195
196 /**
197 * @return a new object representing the same algorithm as *this
198 */
199 StreamCipher* clone() const { return this->new_object().release(); }
200
201 /**
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.
216 *
217 * @param offset the offset where we begin to generate the keystream
218 */
219 virtual void seek(uint64_t offset) = 0;
220
221 /**
222 * @return provider information about this implementation. Default is "base",
223 * might also return "sse2", "avx2" or some other arbitrary string.
224 */
225 virtual std::string provider() const { return "base"; }
226
227 protected:
228 /**
229 * Encrypt or decrypt a message
230 */
231 virtual void cipher_bytes(const uint8_t in[], uint8_t out[], size_t len) = 0;
232
233 /**
234 * Write keystream bytes to a buffer
235 */
236 virtual void generate_keystream(uint8_t out[], size_t len);
237
238 /**
239 * Resync the cipher using the IV
240 */
241 virtual void set_iv_bytes(const uint8_t iv[], size_t iv_len) = 0;
242};
243
244} // namespace Botan
245
246#endif
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
~StreamCipher() override=default
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)
virtual void set_iv_bytes(const uint8_t iv[], size_t iv_len)=0
void decrypt(std::span< uint8_t > inout)
void encipher(std::span< uint8_t > inout)
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
void cipher(std::span< const uint8_t > in, std::span< uint8_t > out)
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
void cipher(const uint8_t in[], uint8_t out[], size_t len)
virtual std::string provider() const
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
FE_25519 T
Definition ge.cpp:34