Botan  2.7.0
Crypto and TLS for C++11
Public Member Functions | Static Public Member Functions | Protected 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 uint8_t in[], uint8_t out[], size_t length) override
 
void cipher1 (uint8_t 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< 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)
 
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 (uint64_t offset) override
 
void set_iv (const uint8_t iv[], size_t iv_len) override
 
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)
 
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)
 

Protected Member Functions

void verify_key_set (bool cond) const
 

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 14 of file ctr.cpp.

14  :
15  m_cipher(ciph),
16  m_block_size(m_cipher->block_size()),
17  m_ctr_size(m_block_size),
18  m_ctr_blocks(m_cipher->parallel_bytes() / m_block_size),
19  m_counter(m_cipher->parallel_bytes()),
20  m_pad(m_counter.size()),
21  m_pad_pos(0)
22  {
23  }

◆ CTR_BE() [2/2]

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

Definition at line 25 of file ctr.cpp.

25  :
26  m_cipher(cipher),
27  m_block_size(m_cipher->block_size()),
28  m_ctr_size(ctr_size),
29  m_ctr_blocks(m_cipher->parallel_bytes() / m_block_size),
30  m_counter(m_cipher->parallel_bytes()),
31  m_pad(m_counter.size()),
32  m_pad_pos(0)
33  {
34  BOTAN_ARG_CHECK(m_ctr_size >= 4 && m_ctr_size <= m_block_size,
35  "Invalid CTR-BE counter size");
36  }
#define BOTAN_ARG_CHECK(expr, msg)
Definition: assert.h:37
void cipher(const uint8_t in[], uint8_t out[], size_t length) override
Definition: ctr.cpp:64

Member Function Documentation

◆ cipher()

void Botan::CTR_BE::cipher ( const uint8_t  in[],
uint8_t  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 64 of file ctr.cpp.

References Botan::SymmetricAlgorithm::verify_key_set(), and Botan::xor_buf().

65  {
66  verify_key_set(m_iv.empty() == false);
67 
68  const uint8_t* pad_bits = &m_pad[0];
69  const size_t pad_size = m_pad.size();
70 
71  if(m_pad_pos > 0)
72  {
73  const size_t avail = pad_size - m_pad_pos;
74  const size_t take = std::min(length, avail);
75  xor_buf(out, in, pad_bits + m_pad_pos, take);
76  length -= take;
77  in += take;
78  out += take;
79  m_pad_pos += take;
80 
81  if(take == avail)
82  {
83  add_counter(m_ctr_blocks);
84  m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
85  m_pad_pos = 0;
86  }
87  }
88 
89  while(length >= pad_size)
90  {
91  xor_buf(out, in, pad_bits, pad_size);
92  length -= pad_size;
93  in += pad_size;
94  out += pad_size;
95 
96  add_counter(m_ctr_blocks);
97  m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
98  }
99 
100  xor_buf(out, in, pad_bits, length);
101  m_pad_pos += length;
102  }
void verify_key_set(bool cond) const
Definition: sym_algo.h:89
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: mem_ops.h:174

◆ cipher1()

void Botan::StreamCipher::cipher1 ( uint8_t  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 66 of file stream_cipher.h.

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

67  { cipher(buf, buf, len); }
virtual void cipher(const uint8_t in[], uint8_t out[], size_t len)=0

◆ clear()

void Botan::CTR_BE::clear ( )
overridevirtual

Reset the state.

Implements Botan::SymmetricAlgorithm.

Definition at line 38 of file ctr.cpp.

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

39  {
40  m_cipher->clear();
41  zeroise(m_pad);
42  zeroise(m_counter);
43  zap(m_iv);
44  m_pad_pos = 0;
45  }
void zap(std::vector< T, Alloc > &vec)
Definition: secmem.h:193
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:183

◆ 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(), m_ctr_size); }
CTR_BE(BlockCipher *cipher)
Definition: ctr.cpp:14

◆ 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 42 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::SCAN_Name::arg_count_between(), BOTAN_UNUSED, Botan::StreamCipher::cipher(), Botan::BlockCipher::create(), and Botan::StreamCipher::provider().

Referenced by Botan::BlockCipher::create(), Botan::Cipher_Mode::create(), and Botan::StreamCipher::create_or_throw().

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

References Botan::StreamCipher::create(), and Botan::StreamCipher::provider().

Referenced by Botan::ChaCha_RNG::ChaCha_RNG().

136  {
137  if(auto sc = StreamCipher::create(algo, provider))
138  {
139  return sc;
140  }
141  throw Lookup_Error("Stream cipher", algo, provider);
142  }
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< uint8_t, Alloc > &  inout)
inlineinherited

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

Parameters
inoutthe plaintext / ciphertext

Definition at line 93 of file stream_cipher.h.

94  { cipher(inout.data(), inout.data(), inout.size()); }
virtual void cipher(const uint8_t in[], uint8_t out[], size_t len)=0

◆ encipher()

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

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

Parameters
inoutthe plaintext / ciphertext

Definition at line 75 of file stream_cipher.h.

76  { cipher(inout.data(), inout.data(), inout.size()); }
virtual void cipher(const uint8_t in[], uint8_t out[], size_t len)=0

◆ encrypt()

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

Encrypt a message The message is encrypted in place.

Parameters
inoutthe plaintext / ciphertext

Definition at line 84 of file stream_cipher.h.

85  { cipher(inout.data(), inout.data(), inout.size()); }
virtual void cipher(const uint8_t in[], uint8_t 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 38 of file sym_algo.h.

39  {
40  return key_spec().maximum_keylength();
41  }
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 46 of file sym_algo.h.

47  {
48  return key_spec().minimum_keylength();
49  }
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 55 of file ctr.cpp.

References Botan::ASN1::to_string().

Referenced by set_iv().

56  {
57  if(m_ctr_size == m_block_size)
58  return ("CTR-BE(" + m_cipher->name() + ")");
59  else
60  return ("CTR-BE(" + m_cipher->name() + "," + std::to_string(m_ctr_size) + ")");
61 
62  }
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:210

◆ 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 124 of file stream_cipher.h.

Referenced by Botan::StreamCipher::create(), and Botan::StreamCipher::create_or_throw().

124 { 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 144 of file stream_cipher.cpp.

145  {
146  return probe_providers_of<StreamCipher>(algo_spec, {"base", "openssl"});
147  }

◆ seek()

void Botan::CTR_BE::seek ( uint64_t  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 176 of file ctr.cpp.

References Botan::buffer_insert(), Botan::SymmetricAlgorithm::verify_key_set(), and Botan::zeroise().

Referenced by set_iv().

177  {
178  verify_key_set(m_iv.empty() == false);
179 
180  const uint64_t base_counter = m_ctr_blocks * (offset / m_counter.size());
181 
182  zeroise(m_counter);
183  buffer_insert(m_counter, 0, m_iv);
184 
185  const size_t BS = m_block_size;
186 
187  // Set m_counter blocks to IV, IV + 1, ... IV + n
188  for(size_t i = 1; i != m_ctr_blocks; ++i)
189  {
190  buffer_insert(m_counter, i*BS, &m_counter[(i-1)*BS], BS);
191 
192  for(size_t j = 0; j != m_ctr_size; ++j)
193  if(++m_counter[i*BS + (BS - 1 - j)])
194  break;
195  }
196 
197  if(base_counter > 0)
198  add_counter(base_counter);
199 
200  m_cipher->encrypt_n(m_counter.data(), m_pad.data(), m_ctr_blocks);
201  m_pad_pos = offset % m_counter.size();
202  }
void verify_key_set(bool cond) const
Definition: sym_algo.h:89
size_t buffer_insert(std::vector< T, Alloc > &buf, size_t buf_offset, const T input[], size_t input_length)
Definition: secmem.h:103
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:183

◆ set_iv()

void Botan::CTR_BE::set_iv ( const uint8_t  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 104 of file ctr.cpp.

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

105  {
106  if(!valid_iv_length(iv_len))
107  throw Invalid_IV_Length(name(), iv_len);
108 
109  m_iv.resize(m_cipher->block_size());
110  zeroise(m_iv);
111  buffer_insert(m_iv, 0, iv, iv_len);
112 
113  seek(0);
114  }
std::string name() const override
Definition: ctr.cpp:55
void seek(uint64_t offset) override
Definition: ctr.cpp:176
size_t buffer_insert(std::vector< T, Alloc > &buf, size_t buf_offset, const T input[], size_t input_length)
Definition: secmem.h:103
bool valid_iv_length(size_t iv_len) const override
Definition: ctr.h:26
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:183

◆ 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 65 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(), Botan::FPE::fe1_decrypt(), Botan::FPE::fe1_encrypt(), and Botan::pbkdf2().

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

◆ set_key() [2/3]

template<typename Alloc >
void Botan::SymmetricAlgorithm::set_key ( const std::vector< uint8_t, Alloc > &  key)
inlineinherited

Definition at line 71 of file sym_algo.h.

72  {
73  set_key(key.data(), key.size());
74  }
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:65

◆ set_key() [3/3]

void Botan::SymmetricAlgorithm::set_key ( const uint8_t  key[],
size_t  length 
)
inherited

Set the symmetric key of this object.

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

Definition at line 17 of file sym_algo.cpp.

References Botan::SymmetricAlgorithm::name(), and Botan::SymmetricAlgorithm::valid_keylength().

18  {
19  if(!valid_keylength(length))
20  throw Invalid_Key_Length(name(), length);
21  key_schedule(key, length);
22  }
bool valid_keylength(size_t length) const
Definition: sym_algo.h:56
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 56 of file sym_algo.h.

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

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

◆ verify_key_set()

void Botan::SymmetricAlgorithm::verify_key_set ( bool  cond) const
inlineprotectedinherited

Definition at line 89 of file sym_algo.h.

Referenced by Botan::Salsa20::cipher(), cipher(), Botan::RC4::cipher(), Botan::SHAKE_128_Cipher::cipher(), Botan::ChaCha::cipher(), Botan::KASUMI::decrypt_n(), Botan::SM4::decrypt_n(), Botan::Threefish_512::decrypt_n(), Botan::CAST_128::decrypt_n(), Botan::Twofish::decrypt_n(), Botan::XTEA::decrypt_n(), Botan::MISTY1::decrypt_n(), Botan::DES::decrypt_n(), Botan::AES_128::decrypt_n(), Botan::CAST_256::decrypt_n(), Botan::Noekeon::decrypt_n(), Botan::Camellia_128::decrypt_n(), Botan::SEED::decrypt_n(), Botan::IDEA::decrypt_n(), Botan::Blowfish::decrypt_n(), Botan::SHACAL2::decrypt_n(), Botan::DESX::decrypt_n(), Botan::Serpent::decrypt_n(), Botan::Lion::decrypt_n(), Botan::ARIA_128::decrypt_n(), Botan::Camellia_192::decrypt_n(), Botan::TripleDES::decrypt_n(), Botan::ARIA_192::decrypt_n(), Botan::Camellia_256::decrypt_n(), Botan::GOST_28147_89::decrypt_n(), Botan::AES_192::decrypt_n(), Botan::ARIA_256::decrypt_n(), Botan::AES_256::decrypt_n(), Botan::IDEA::encrypt_n(), Botan::KASUMI::encrypt_n(), Botan::SEED::encrypt_n(), Botan::Camellia_128::encrypt_n(), Botan::AES_128::encrypt_n(), Botan::XTEA::encrypt_n(), Botan::CAST_256::encrypt_n(), Botan::Twofish::encrypt_n(), Botan::CAST_128::encrypt_n(), Botan::SM4::encrypt_n(), Botan::Threefish_512::encrypt_n(), Botan::SHACAL2::encrypt_n(), Botan::Noekeon::encrypt_n(), Botan::DESX::encrypt_n(), Botan::Blowfish::encrypt_n(), Botan::DES::encrypt_n(), Botan::MISTY1::encrypt_n(), Botan::Serpent::encrypt_n(), Botan::Lion::encrypt_n(), Botan::ARIA_128::encrypt_n(), Botan::Camellia_192::encrypt_n(), Botan::TripleDES::encrypt_n(), Botan::ARIA_192::encrypt_n(), Botan::Camellia_256::encrypt_n(), Botan::GOST_28147_89::encrypt_n(), Botan::AES_192::encrypt_n(), Botan::ARIA_256::encrypt_n(), Botan::AES_256::encrypt_n(), Botan::Salsa20::seek(), seek(), Botan::ChaCha::seek(), and Botan::GHASH::update_associated_data().

90  {
91  if(cond == false)
92  throw_key_not_set_error();
93  }

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