Botan  2.7.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  * Encrypt or decrypt a message
62  * The message is encrypted/decrypted in place.
63  * @param buf the plaintext / ciphertext
64  * @param len the length of buf in bytes
65  */
66  void cipher1(uint8_t buf[], size_t len)
67  { cipher(buf, buf, len); }
68 
69  /**
70  * Encrypt a message
71  * The message is encrypted/decrypted in place.
72  * @param inout the plaintext / ciphertext
73  */
74  template<typename Alloc>
75  void encipher(std::vector<uint8_t, Alloc>& inout)
76  { cipher(inout.data(), inout.data(), inout.size()); }
77 
78  /**
79  * Encrypt a message
80  * The message is encrypted in place.
81  * @param inout the plaintext / ciphertext
82  */
83  template<typename Alloc>
84  void encrypt(std::vector<uint8_t, Alloc>& inout)
85  { cipher(inout.data(), inout.data(), inout.size()); }
86 
87  /**
88  * Decrypt a message in place
89  * The message is decrypted in place.
90  * @param inout the plaintext / ciphertext
91  */
92  template<typename Alloc>
93  void decrypt(std::vector<uint8_t, Alloc>& inout)
94  { cipher(inout.data(), inout.data(), inout.size()); }
95 
96  /**
97  * Resync the cipher using the IV
98  * @param iv the initialization vector
99  * @param iv_len the length of the IV in bytes
100  */
101  virtual void set_iv(const uint8_t iv[], size_t iv_len) = 0;
102 
103  /**
104  * @param iv_len the length of the IV in bytes
105  * @return if the length is valid for this algorithm
106  */
107  virtual bool valid_iv_length(size_t iv_len) const { return (iv_len == 0); }
108 
109  /**
110  * @return a new object representing the same algorithm as *this
111  */
112  virtual StreamCipher* clone() const = 0;
113 
114  /**
115  * Set the offset and the state used later to generate the keystream
116  * @param offset the offset where we begin to generate the keystream
117  */
118  virtual void seek(uint64_t offset) = 0;
119 
120  /**
121  * @return provider information about this implementation. Default is "base",
122  * might also return "sse2", "avx2", "openssl", or some other arbitrary string.
123  */
124  virtual std::string provider() const { return "base"; }
125  };
126 
127 }
128 
129 #endif
void cipher1(uint8_t buf[], size_t len)
Definition: stream_cipher.h:66
#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:84
virtual std::string provider() const
Definition: alg_id.cpp:13
void decrypt(std::vector< uint8_t, Alloc > &inout)
Definition: stream_cipher.h:93
void encipher(std::vector< uint8_t, Alloc > &inout)
Definition: stream_cipher.h:75