Botan  1.11.34
Crypto and TLS for C++11
Public Member Functions | Static Public Member Functions | List of all members
Botan::CTR_BE Class Referencefinal

#include <ctr.h>

Inheritance diagram for Botan::CTR_BE:
Botan::StreamCipher Botan::SymmetricAlgorithm

Public Member Functions

void cipher (const byte in[], byte out[], size_t length) override
 
void cipher1 (byte buf[], size_t len)
 
void clear () override
 
CTR_BEclone () const override
 
 CTR_BE (BlockCipher *cipher)
 
 CTR_BE (BlockCipher *cipher, size_t ctr_size)
 
template<typename Alloc >
void decrypt (std::vector< byte, Alloc > &inout)
 
template<typename Alloc >
void encipher (std::vector< byte, Alloc > &inout)
 
template<typename Alloc >
void encrypt (std::vector< byte, Alloc > &inout)
 
Key_Length_Specification key_spec () const override
 
size_t maximum_keylength () const
 
size_t minimum_keylength () const
 
std::string name () const override
 
virtual std::string provider () const
 
void seek (u64bit offset) override
 
void set_iv (const byte iv[], size_t iv_len) override
 
void set_key (const SymmetricKey &key)
 
template<typename Alloc >
void set_key (const std::vector< byte, Alloc > &key)
 
void set_key (const byte key[], size_t length)
 
bool valid_iv_length (size_t iv_len) const override
 
bool valid_keylength (size_t length) const
 

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

CTR-BE (Counter mode, big-endian)

Definition at line 19 of file ctr.h.

Constructor & Destructor Documentation

§ CTR_BE() [1/2]

Botan::CTR_BE::CTR_BE ( BlockCipher cipher)
explicit
Parameters
cipherthe block cipher to use

Definition at line 12 of file ctr.cpp.

12  :
13  m_cipher(ciph),
14  m_counter(m_cipher->parallel_bytes()),
15  m_pad(m_counter.size()),
16  m_ctr_size(m_cipher->block_size()),
17  m_pad_pos(0)
18  {
19  }

§ CTR_BE() [2/2]

Botan::CTR_BE::CTR_BE ( BlockCipher cipher,
size_t  ctr_size 
)

Definition at line 21 of file ctr.cpp.

21  :
22  m_cipher(cipher),
23  m_counter(m_cipher->parallel_bytes()),
24  m_pad(m_counter.size()),
25  m_ctr_size(ctr_size),
26  m_pad_pos(0)
27  {
28  //BOTAN_CHECK_ARG(m_ctr_size > 0 && m_ctr_size <= cipher->block_size(), "Invalid CTR size");
29  if(m_ctr_size == 0 || m_ctr_size > m_cipher->block_size())
30  throw Invalid_Argument("Invalid CTR-BE counter size");
31  }
void cipher(const byte in[], byte out[], size_t length) override
Definition: ctr.cpp:54

Member Function Documentation

§ cipher()

void Botan::CTR_BE::cipher ( const byte  in[],
byte  out[],
size_t  len 
)
overridevirtual

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

Implements Botan::StreamCipher.

Definition at line 54 of file ctr.cpp.

References Botan::xor_buf().

55  {
56  while(length >= m_pad.size() - m_pad_pos)
57  {
58  xor_buf(out, in, &m_pad[m_pad_pos], m_pad.size() - m_pad_pos);
59  length -= (m_pad.size() - m_pad_pos);
60  in += (m_pad.size() - m_pad_pos);
61  out += (m_pad.size() - m_pad_pos);
62  increment_counter();
63  }
64  xor_buf(out, in, &m_pad[m_pad_pos], length);
65  m_pad_pos += length;
66  }
void xor_buf(T out[], const T in[], size_t length)
Definition: mem_ops.h:115

§ cipher1()

void Botan::StreamCipher::cipher1 ( byte  buf[],
size_t  len 
)
inlineinherited

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 byte in[], byte out[], size_t len)=0

§ clear()

void Botan::CTR_BE::clear ( )
overridevirtual

Reset the state.

Implements Botan::SymmetricAlgorithm.

Definition at line 33 of file ctr.cpp.

References set_iv(), and Botan::zeroise().

34  {
35  m_cipher->clear();
36  zeroise(m_pad);
37  zeroise(m_counter);
38  m_pad_pos = 0;
39  }
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:203

§ clone()

CTR_BE* Botan::CTR_BE::clone ( ) const
inlineoverridevirtual
Returns
a new object representing the same algorithm as *this

Implements Botan::StreamCipher.

Definition at line 36 of file ctr.h.

37  { return new CTR_BE(m_cipher->clone()); }
CTR_BE(BlockCipher *cipher)
Definition: ctr.cpp:12

§ create()

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

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(), and Botan::BlockCipher::create().

Referenced by Botan::BlockCipher::create(), Botan::StreamCipher::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  return nullptr;
116  }
virtual std::string provider() const
static std::unique_ptr< BlockCipher > create(const std::string &algo_spec, const std::string &provider="")

§ create_or_throw()

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

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 120 of file stream_cipher.cpp.

References Botan::StreamCipher::create().

Referenced by Botan::make_stream_cipher().

122  {
123  if(auto sc = StreamCipher::create(algo, provider))
124  {
125  return sc;
126  }
127  throw Lookup_Error("Stream cipher", algo, provider);
128  }
virtual std::string provider() const
static std::unique_ptr< StreamCipher > create(const std::string &algo_spec, const std::string &provider="")

§ decrypt()

template<typename Alloc >
void Botan::StreamCipher::decrypt ( std::vector< byte, Alloc > &  inout)
inlineinherited

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 byte in[], byte out[], size_t len)=0

§ encipher()

template<typename Alloc >
void Botan::StreamCipher::encipher ( std::vector< byte, Alloc > &  inout)
inlineinherited

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 byte in[], byte out[], size_t len)=0

§ encrypt()

template<typename Alloc >
void Botan::StreamCipher::encrypt ( std::vector< byte, Alloc > &  inout)
inlineinherited

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 byte in[], byte out[], size_t len)=0

§ key_spec()

Key_Length_Specification Botan::CTR_BE::key_spec ( ) const
inlineoverridevirtual
Returns
object describing limits on key size

Implements Botan::SymmetricAlgorithm.

Definition at line 29 of file ctr.h.

30  {
31  return m_cipher->key_spec();
32  }

§ maximum_keylength()

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

§ minimum_keylength()

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  }
virtual Key_Length_Specification key_spec() const =0
size_t minimum_keylength() const
Definition: key_spec.h:61

§ name()

std::string Botan::CTR_BE::name ( ) const
overridevirtual
Returns
the algorithm name

Implements Botan::SymmetricAlgorithm.

Definition at line 49 of file ctr.cpp.

Referenced by set_iv().

50  {
51  return ("CTR-BE(" + m_cipher->name() + ")");
52  }

§ provider()

virtual std::string Botan::StreamCipher::provider ( ) const
inlinevirtualinherited
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"; }

§ providers()

std::vector< std::string > Botan::StreamCipher::providers ( const std::string &  algo_spec)
staticinherited
Returns
list of available providers for this algorithm, empty if not available

Definition at line 130 of file stream_cipher.cpp.

Referenced by Botan::TLS::Ciphersuite::by_id(), and Botan::get_stream_cipher_providers().

131  {
132  return probe_providers_of<StreamCipher>(algo_spec, {"base", "openssl"});
133  }

§ seek()

void Botan::CTR_BE::seek ( u64bit  offset)
overridevirtual

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

Parameters
offsetthe offset where we begin to generate the keystream

Implements Botan::StreamCipher.

Definition at line 118 of file ctr.cpp.

119  {
120  throw Not_Implemented("CTR_BE::seek");
121  }

§ set_iv()

void Botan::CTR_BE::set_iv ( const byte  iv[],
size_t  iv_len 
)
overridevirtual

Resync the cipher using the IV

Parameters
ivthe initialization vector
iv_lenthe length of the IV in bytes

Implements Botan::StreamCipher.

Definition at line 68 of file ctr.cpp.

References Botan::buffer_insert(), name(), valid_iv_length(), and Botan::zeroise().

Referenced by clear().

69  {
70  if(!valid_iv_length(iv_len))
71  throw Invalid_IV_Length(name(), iv_len);
72 
73  const size_t bs = m_cipher->block_size();
74 
75  zeroise(m_counter);
76 
77  const size_t n_wide = m_counter.size() / m_cipher->block_size();
78  buffer_insert(m_counter, 0, iv, iv_len);
79 
80  // Set m_counter blocks to IV, IV + 1, ... IV + n
81  for(size_t i = 1; i != n_wide; ++i)
82  {
83  buffer_insert(m_counter, i*bs, &m_counter[(i-1)*bs], bs);
84 
85  for(size_t j = 0; j != m_ctr_size; ++j)
86  if(++m_counter[i*bs + (bs - 1 - j)])
87  break;
88  }
89 
90  m_cipher->encrypt_n(m_counter.data(), m_pad.data(), n_wide);
91  m_pad_pos = 0;
92  }
std::string name() const override
Definition: ctr.cpp:49
size_t buffer_insert(std::vector< T, Alloc > &buf, size_t buf_offset, const T input[], size_t input_length)
Definition: secmem.h:125
bool valid_iv_length(size_t iv_len) const override
Definition: ctr.h:26
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:203

§ set_key() [1/3]

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_mac_set_key(), Botan::GOST_34_11::clear(), Botan::DESX::decrypt_n(), Botan::TLS::TLS_CBC_HMAC_AEAD_Mode::key_spec(), Botan::pbkdf2(), and Botan::TLS_PRF::TLS_PRF().

67  {
68  set_key(key.begin(), key.length());
69  }
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:66

§ set_key() [2/3]

template<typename Alloc >
void Botan::SymmetricAlgorithm::set_key ( const std::vector< byte, 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

§ set_key() [3/3]

void Botan::SymmetricAlgorithm::set_key ( const byte  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

§ valid_iv_length()

bool Botan::CTR_BE::valid_iv_length ( size_t  iv_len) const
inlineoverridevirtual
Parameters
iv_lenthe length of the IV in bytes
Returns
if the length is valid for this algorithm

Reimplemented from Botan::StreamCipher.

Definition at line 26 of file ctr.h.

Referenced by set_iv().

27  { return (iv_len <= m_cipher->block_size()); }

§ valid_keylength()

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: