Botan  2.1.0
Crypto and TLS for C++11
Public Member Functions | Static Public Member Functions | List of all members
Botan::StreamCipher Class Referenceabstract

#include <stream_cipher.h>

Inheritance diagram for Botan::StreamCipher:
Botan::SymmetricAlgorithm Botan::ChaCha Botan::CTR_BE Botan::OFB Botan::RC4 Botan::Salsa20 Botan::SHAKE_128_Cipher

Public Member Functions

virtual void cipher (const uint8_t in[], uint8_t out[], size_t len)=0
 
void cipher1 (uint8_t buf[], size_t len)
 
virtual void clear ()=0
 
virtual StreamCipherclone () const =0
 
template<typename Alloc >
void decrypt (std::vector< uint8_t, Alloc > &inout)
 
template<typename Alloc >
void encipher (std::vector< uint8_t, Alloc > &inout)
 
template<typename Alloc >
void encrypt (std::vector< uint8_t, Alloc > &inout)
 
virtual Key_Length_Specification key_spec () const =0
 
size_t maximum_keylength () const
 
size_t minimum_keylength () const
 
virtual std::string name () const =0
 
virtual std::string provider () const
 
virtual void seek (uint64_t offset)=0
 
virtual void set_iv (const uint8_t iv[], size_t iv_len)=0
 
void set_key (const SymmetricKey &key)
 
template<typename Alloc >
void set_key (const std::vector< uint8_t, Alloc > &key)
 
void set_key (const uint8_t key[], size_t length)
 
virtual bool valid_iv_length (size_t iv_len) const
 
bool valid_keylength (size_t length) const
 
virtual ~StreamCipher ()=default
 

Static Public Member Functions

static std::unique_ptr< StreamCiphercreate (const std::string &algo_spec, const std::string &provider="")
 
static std::unique_ptr< StreamCiphercreate_or_throw (const std::string &algo_spec, const std::string &provider="")
 
static std::vector< std::string > providers (const std::string &algo_spec)
 

Detailed Description

Base class for all stream ciphers

Definition at line 19 of file stream_cipher.h.

Constructor & Destructor Documentation

virtual Botan::StreamCipher::~StreamCipher ( )
virtualdefault

Member Function Documentation

virtual void Botan::StreamCipher::cipher ( const uint8_t  in[],
uint8_t  out[],
size_t  len 
)
pure virtual

Encrypt or decrypt a message

Parameters
inthe plaintext
outthe byte array to hold the output, i.e. the ciphertext
lenthe length of both in and out in bytes

Implemented in Botan::ChaCha, Botan::SHAKE_128_Cipher, Botan::CTR_BE, Botan::OFB, Botan::RC4, and Botan::Salsa20.

Referenced by Botan::SIV_Decryption::finish().

void Botan::StreamCipher::cipher1 ( uint8_t  buf[],
size_t  len 
)
inline

Encrypt or decrypt a message The message is encrypted/decrypted in place.

Parameters
bufthe plaintext / ciphertext
lenthe length of buf in bytes

Definition at line 65 of file stream_cipher.h.

Referenced by Botan::SIV_Encryption::finish().

66  { cipher(buf, buf, len); }
virtual void cipher(const uint8_t in[], uint8_t out[], size_t len)=0
virtual void Botan::SymmetricAlgorithm::clear ( )
pure virtualinherited
virtual StreamCipher* Botan::StreamCipher::clone ( ) const
pure virtual
Returns
a new object representing the same algorithm as *this

Implemented in Botan::SHAKE_128_Cipher, Botan::CTR_BE, Botan::OFB, Botan::Salsa20, Botan::RC4, and Botan::ChaCha.

std::unique_ptr< StreamCipher > Botan::StreamCipher::create ( const std::string &  algo_spec,
const std::string &  provider = "" 
)
static

Create an instance based on a name If provider is empty then best available is chosen.

Parameters
algo_specalgorithm name
providerprovider implementation to use
Returns
a null pointer if the algo/provider combination cannot be found

Definition at line 41 of file stream_cipher.cpp.

References Botan::SCAN_Name::algo_name(), Botan::SCAN_Name::arg(), Botan::SCAN_Name::arg_as_integer(), Botan::SCAN_Name::arg_count(), BOTAN_UNUSED, and Botan::BlockCipher::create().

Referenced by Botan::BlockCipher::create(), create_or_throw(), Botan::get_cipher_mode(), and Botan::get_stream_cipher().

43  {
44  const SCAN_Name req(algo_spec);
45 
46 #if defined(BOTAN_HAS_CTR_BE)
47  if(req.algo_name() == "CTR-BE" && req.arg_count() == 1)
48  {
49  if(provider.empty() || provider == "base")
50  {
51  if(auto c = BlockCipher::create(req.arg(0)))
52  return std::unique_ptr<StreamCipher>(new CTR_BE(c.release()));
53  }
54  }
55 #endif
56 
57 #if defined(BOTAN_HAS_CHACHA)
58  if(req.algo_name() == "ChaCha")
59  {
60  if(provider.empty() || provider == "base")
61  return std::unique_ptr<StreamCipher>(new ChaCha(req.arg_as_integer(0, 20)));
62  }
63 #endif
64 
65 #if defined(BOTAN_HAS_SALSA20)
66  if(req.algo_name() == "Salsa20")
67  {
68  if(provider.empty() || provider == "base")
69  return std::unique_ptr<StreamCipher>(new Salsa20);
70  }
71 #endif
72 
73 #if defined(BOTAN_HAS_SHAKE_CIPHER)
74  if(req.algo_name() == "SHAKE-128")
75  {
76  if(provider.empty() || provider == "base")
77  return std::unique_ptr<StreamCipher>(new SHAKE_128_Cipher);
78  }
79 #endif
80 
81 #if defined(BOTAN_HAS_OFB)
82  if(req.algo_name() == "OFB" && req.arg_count() == 1)
83  {
84  if(provider.empty() || provider == "base")
85  {
86  if(auto c = BlockCipher::create(req.arg(0)))
87  return std::unique_ptr<StreamCipher>(new OFB(c.release()));
88  }
89  }
90 #endif
91 
92 #if defined(BOTAN_HAS_RC4)
93 
94  if(req.algo_name() == "RC4" ||
95  req.algo_name() == "ARC4" ||
96  req.algo_name() == "MARK-4")
97  {
98  const size_t skip = (req.algo_name() == "MARK-4") ? 256 : req.arg_as_integer(0, 0);
99 
100 #if defined(BOTAN_HAS_OPENSSL)
101  if(provider.empty() || provider == "openssl")
102  {
103  return std::unique_ptr<StreamCipher>(make_openssl_rc4(skip));
104  }
105 #endif
106 
107  if(provider.empty() || provider == "base")
108  {
109  return std::unique_ptr<StreamCipher>(new RC4(skip));
110  }
111  }
112 
113 #endif
114 
115  BOTAN_UNUSED(req);
117 
118  return nullptr;
119  }
virtual std::string provider() const
#define BOTAN_UNUSED(v)
Definition: assert.h:92
static std::unique_ptr< BlockCipher > create(const std::string &algo_spec, const std::string &provider="")
std::unique_ptr< StreamCipher > Botan::StreamCipher::create_or_throw ( const std::string &  algo_spec,
const std::string &  provider = "" 
)
static

Create an instance based on a name If provider is empty then best available is chosen.

Parameters
algo_specalgorithm name
providerprovider implementation to use Throws a Lookup_Error if the algo/provider combination cannot be found

Definition at line 123 of file stream_cipher.cpp.

References create().

Referenced by Botan::make_stream_cipher().

125  {
126  if(auto sc = StreamCipher::create(algo, provider))
127  {
128  return sc;
129  }
130  throw Lookup_Error("Stream cipher", algo, provider);
131  }
virtual std::string provider() const
static std::unique_ptr< StreamCipher > create(const std::string &algo_spec, const std::string &provider="")
template<typename Alloc >
void Botan::StreamCipher::decrypt ( std::vector< uint8_t, Alloc > &  inout)
inline

Decrypt a message in place The message is decrypted in place.

Parameters
inoutthe plaintext / ciphertext

Definition at line 92 of file stream_cipher.h.

93  { cipher(inout.data(), inout.data(), inout.size()); }
virtual void cipher(const uint8_t in[], uint8_t out[], size_t len)=0
template<typename Alloc >
void Botan::StreamCipher::encipher ( std::vector< uint8_t, Alloc > &  inout)
inline

Encrypt a message The message is encrypted/decrypted in place.

Parameters
inoutthe plaintext / ciphertext

Definition at line 74 of file stream_cipher.h.

75  { cipher(inout.data(), inout.data(), inout.size()); }
virtual void cipher(const uint8_t in[], uint8_t out[], size_t len)=0
template<typename Alloc >
void Botan::StreamCipher::encrypt ( std::vector< uint8_t, Alloc > &  inout)
inline

Encrypt a message The message is encrypted in place.

Parameters
inoutthe plaintext / ciphertext

Definition at line 83 of file stream_cipher.h.

84  { cipher(inout.data(), inout.data(), inout.size()); }
virtual void cipher(const uint8_t in[], uint8_t out[], size_t len)=0
virtual Key_Length_Specification Botan::SymmetricAlgorithm::key_spec ( ) const
pure virtualinherited
size_t Botan::SymmetricAlgorithm::maximum_keylength ( ) const
inlineinherited
Returns
minimum allowed key length

Definition at line 39 of file sym_algo.h.

40  {
41  return key_spec().maximum_keylength();
42  }
size_t maximum_keylength() const
Definition: key_spec.h:69
virtual Key_Length_Specification key_spec() const =0
size_t Botan::SymmetricAlgorithm::minimum_keylength ( ) const
inlineinherited
Returns
maximum allowed key length

Definition at line 47 of file sym_algo.h.

48  {
49  return key_spec().minimum_keylength();
50  }
size_t minimum_keylength() const
Definition: key_spec.h:61
virtual Key_Length_Specification key_spec() const =0
virtual std::string Botan::SymmetricAlgorithm::name ( ) const
pure virtualinherited
virtual std::string Botan::StreamCipher::provider ( ) const
inlinevirtual
Returns
provider information about this implementation. Default is "base", might also return "sse2", "avx2", "openssl", or some other arbitrary string.

Reimplemented in Botan::ChaCha.

Definition at line 123 of file stream_cipher.h.

123 { return "base"; }
std::vector< std::string > Botan::StreamCipher::providers ( const std::string &  algo_spec)
static
Returns
list of available providers for this algorithm, empty if not available

Definition at line 133 of file stream_cipher.cpp.

Referenced by Botan::get_stream_cipher_providers().

134  {
135  return probe_providers_of<StreamCipher>(algo_spec, {"base", "openssl"});
136  }
virtual void Botan::StreamCipher::seek ( uint64_t  offset)
pure virtual

Set the offset and the state used later to generate the keystream

Parameters
offsetthe offset where we begin to generate the keystream

Implemented in Botan::ChaCha, Botan::CTR_BE, Botan::OFB, Botan::RC4, Botan::Salsa20, and Botan::SHAKE_128_Cipher.

virtual void Botan::StreamCipher::set_iv ( const uint8_t  iv[],
size_t  iv_len 
)
pure virtual

Resync the cipher using the IV

Parameters
ivthe initialization vector
iv_lenthe length of the IV in bytes

Implemented in Botan::SHAKE_128_Cipher, Botan::ChaCha, Botan::CTR_BE, Botan::OFB, Botan::RC4, and Botan::Salsa20.

Referenced by Botan::SIV_Mode::set_ctr_iv().

void Botan::SymmetricAlgorithm::set_key ( const SymmetricKey key)
inlineinherited

Set the symmetric key of this object.

Parameters
keythe SymmetricKey to be set.

Definition at line 66 of file sym_algo.h.

References Botan::OctetString::begin(), and Botan::OctetString::length().

Referenced by Botan::aont_package(), Botan::aont_unpackage(), botan_block_cipher_set_key(), botan_mac_set_key(), and Botan::pbkdf2().

67  {
68  set_key(key.begin(), key.length());
69  }
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:66
template<typename Alloc >
void Botan::SymmetricAlgorithm::set_key ( const std::vector< uint8_t, Alloc > &  key)
inlineinherited

Definition at line 72 of file sym_algo.h.

73  {
74  set_key(key.data(), key.size());
75  }
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:66
void Botan::SymmetricAlgorithm::set_key ( const uint8_t  key[],
size_t  length 
)
inlineinherited

Set the symmetric key of this object.

Parameters
keythe to be set as a byte array.
lengthin bytes of key param

Definition at line 82 of file sym_algo.h.

83  {
84  if(!valid_keylength(length))
85  throw Invalid_Key_Length(name(), length);
86  key_schedule(key, length);
87  }
bool valid_keylength(size_t length) const
Definition: sym_algo.h:57
virtual std::string name() const =0
virtual bool Botan::StreamCipher::valid_iv_length ( size_t  iv_len) const
inlinevirtual
Parameters
iv_lenthe length of the IV in bytes
Returns
if the length is valid for this algorithm

Reimplemented in Botan::ChaCha, Botan::SHAKE_128_Cipher, Botan::CTR_BE, Botan::OFB, and Botan::Salsa20.

Definition at line 106 of file stream_cipher.h.

106 { return (iv_len == 0); }
bool Botan::SymmetricAlgorithm::valid_keylength ( size_t  length) const
inlineinherited

Check whether a given key length is valid for this algorithm.

Parameters
lengththe key length to be checked.
Returns
true if the key length is valid.

Definition at line 57 of file sym_algo.h.

Referenced by Botan::aont_package(), and Botan::aont_unpackage().

58  {
59  return key_spec().valid_keylength(length);
60  }
bool valid_keylength(size_t length) const
Definition: key_spec.h:51
virtual Key_Length_Specification key_spec() const =0

The documentation for this class was generated from the following files: