/* * Stream Cipher * (C) 1999-2007 Jack Lloyd * * Botan is released under the Simplified BSD License (see license.txt) */ #ifndef BOTAN_STREAM_CIPHER_H_ #define BOTAN_STREAM_CIPHER_H_ #include #include #include #include namespace Botan { /** * Base class for all stream ciphers */ class BOTAN_PUBLIC_API(2,0) StreamCipher : public SymmetricAlgorithm { public: virtual ~StreamCipher() = default; /** * Create an instance based on a name * If provider is empty then best available is chosen. * @param algo_spec algorithm name * @param provider provider implementation to use * @return a null pointer if the algo/provider combination cannot be found */ static std::unique_ptr create(const std::string& algo_spec, const std::string& provider = ""); /** * Create an instance based on a name * If provider is empty then best available is chosen. * @param algo_spec algorithm name * @param provider provider implementation to use * Throws a Lookup_Error if the algo/provider combination cannot be found */ static std::unique_ptr create_or_throw(const std::string& algo_spec, const std::string& provider = ""); /** * @return list of available providers for this algorithm, empty if not available */ static std::vector providers(const std::string& algo_spec); /** * Encrypt or decrypt a message * @param in the plaintext * @param out the byte array to hold the output, i.e. the ciphertext * @param len the length of both in and out in bytes */ virtual void cipher(const uint8_t in[], uint8_t out[], size_t len) = 0; /** * Write keystream bytes to a buffer * @param out the byte array to hold the keystream * @param len the length of out in bytes */ virtual void write_keystream(uint8_t out[], size_t len) { clear_mem(out, len); cipher1(out, len); } /** * Encrypt or decrypt a message * The message is encrypted/decrypted in place. * @param buf the plaintext / ciphertext * @param len the length of buf in bytes */ void cipher1(uint8_t buf[], size_t len) { cipher(buf, buf, len); } /** * Encrypt a message * The message is encrypted/decrypted in place. * @param inout the plaintext / ciphertext */ template void encipher(std::vector& inout) { cipher(inout.data(), inout.data(), inout.size()); } /** * Encrypt a message * The message is encrypted in place. * @param inout the plaintext / ciphertext */ template void encrypt(std::vector& inout) { cipher(inout.data(), inout.data(), inout.size()); } /** * Decrypt a message in place * The message is decrypted in place. * @param inout the plaintext / ciphertext */ template void decrypt(std::vector& inout) { cipher(inout.data(), inout.data(), inout.size()); } /** * Resync the cipher using the IV * @param iv the initialization vector * @param iv_len the length of the IV in bytes */ virtual void set_iv(const uint8_t iv[], size_t iv_len) = 0; /** * Return the default (preferred) nonce length * If this function returns 0, then this cipher does not support nonces */ virtual size_t default_iv_length() const { return 0; } /** * @param iv_len the length of the IV in bytes * @return if the length is valid for this algorithm */ virtual bool valid_iv_length(size_t iv_len) const { return (iv_len == 0); } /** * @return a new object representing the same algorithm as *this */ virtual StreamCipher* clone() const = 0; /** * Set the offset and the state used later to generate the keystream * @param offset the offset where we begin to generate the keystream */ virtual void seek(uint64_t offset) = 0; /** * @return provider information about this implementation. Default is "base", * might also return "sse2", "avx2", "openssl", or some other arbitrary string. */ virtual std::string provider() const { return "base"; } }; } #endif