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