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