Botan  2.10.0
Crypto and TLS for C++11
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/sym_algo.h>
12 #include <string>
13 #include <memory>
14 #include <vector>
15 
16 namespace Botan {
17 
18 /**
19 * Base class for all stream ciphers
20 */
21 class BOTAN_PUBLIC_API(2,0) StreamCipher : public SymmetricAlgorithm
22  {
23  public:
24  virtual ~StreamCipher() = default;
25 
26  /**
27  * Create an instance based on a name
28  * If provider is empty then best available is chosen.
29  * @param algo_spec algorithm name
30  * @param provider provider implementation to use
31  * @return a null pointer if the algo/provider combination cannot be found
32  */
33  static std::unique_ptr<StreamCipher>
34  create(const std::string& algo_spec,
35  const std::string& provider = "");
36 
37  /**
38  * Create an instance based on a name
39  * If provider is empty then best available is chosen.
40  * @param algo_spec algorithm name
41  * @param provider provider implementation to use
42  * Throws a Lookup_Error if the algo/provider combination cannot be found
43  */
44  static std::unique_ptr<StreamCipher>
45  create_or_throw(const std::string& algo_spec,
46  const std::string& provider = "");
47 
48  /**
49  * @return list of available providers for this algorithm, empty if not available
50  */
51  static std::vector<std::string> providers(const std::string& algo_spec);
52 
53  /**
54  * Encrypt or decrypt a message
55  * @param in the plaintext
56  * @param out the byte array to hold the output, i.e. the ciphertext
57  * @param len the length of both in and out in bytes
58  */
59  virtual void cipher(const uint8_t in[], uint8_t out[], size_t len) = 0;
60 
61  /**
62  * Write keystream bytes to a buffer
63  * @param out the byte array to hold the keystream
64  * @param len the length of out in bytes
65  */
66  virtual void write_keystream(uint8_t out[], size_t len)
67  {
68  clear_mem(out, len);
69  cipher1(out, len);
70  }
71 
72  /**
73  * Encrypt or decrypt a message
74  * The message is encrypted/decrypted in place.
75  * @param buf the plaintext / ciphertext
76  * @param len the length of buf in bytes
77  */
78  void cipher1(uint8_t buf[], size_t len)
79  { cipher(buf, buf, len); }
80 
81  /**
82  * Encrypt a message
83  * The message is encrypted/decrypted in place.
84  * @param inout the plaintext / ciphertext
85  */
86  template<typename Alloc>
87  void encipher(std::vector<uint8_t, Alloc>& inout)
88  { cipher(inout.data(), inout.data(), inout.size()); }
89 
90  /**
91  * Encrypt a message
92  * The message is encrypted in place.
93  * @param inout the plaintext / ciphertext
94  */
95  template<typename Alloc>
96  void encrypt(std::vector<uint8_t, Alloc>& inout)
97  { cipher(inout.data(), inout.data(), inout.size()); }
98 
99  /**
100  * Decrypt a message in place
101  * The message is decrypted in place.
102  * @param inout the plaintext / ciphertext
103  */
104  template<typename Alloc>
105  void decrypt(std::vector<uint8_t, Alloc>& inout)
106  { cipher(inout.data(), inout.data(), inout.size()); }
107 
108  /**
109  * Resync the cipher using the IV
110  * @param iv the initialization vector
111  * @param iv_len the length of the IV in bytes
112  */
113  virtual void set_iv(const uint8_t iv[], size_t iv_len) = 0;
114 
115  /**
116  * Return the default (preferred) nonce length
117  * If this function returns 0, then this cipher does not support nonces
118  */
119  virtual size_t default_iv_length() const { return 0; }
120 
121  /**
122  * @param iv_len the length of the IV in bytes
123  * @return if the length is valid for this algorithm
124  */
125  virtual bool valid_iv_length(size_t iv_len) const { return (iv_len == 0); }
126 
127  /**
128  * @return a new object representing the same algorithm as *this
129  */
130  virtual StreamCipher* clone() const = 0;
131 
132  /**
133  * Set the offset and the state used later to generate the keystream
134  * @param offset the offset where we begin to generate the keystream
135  */
136  virtual void seek(uint64_t offset) = 0;
137 
138  /**
139  * @return provider information about this implementation. Default is "base",
140  * might also return "sse2", "avx2", "openssl", or some other arbitrary string.
141  */
142  virtual std::string provider() const { return "base"; }
143  };
144 
145 }
146 
147 #endif
std::string size_t len
Definition: pk_keys.h:305
bool BigInt BigInt size_t size_t const std::vector< uint8_t > size_t offset
Definition: numthry.h:271
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:111
const uint8_t * buf
Definition: ffi.h:368
void const uint8_t in[]
Definition: mgf1.h:26
std::string decrypt(const uint8_t input[], size_t input_len, const std::string &passphrase)
Definition: cryptobox.cpp:162
void BlockCipher * cipher
Definition: package.h:29
Definition: alg_id.cpp:13
uint8_t out[]
Definition: pbkdf2.h:19
class BOTAN_PUBLIC_API(2, 0) AlgorithmIdentifier final bool BOTAN_PUBLIC_API(2, 0) operator
Name Constraints.
Definition: asn1_obj.h:65
std::string encrypt(const uint8_t input[], size_t input_len, const std::string &passphrase, RandomNumberGenerator &rng)
Definition: cryptobox.cpp:43