Botan  1.11.30
Public Types | 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 Types

typedef SCAN_Name Spec
 

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
 
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 CTR_BEmake (const Spec &spec)
 
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.

Member Typedef Documentation

Definition at line 22 of file stream_cipher.h.

Constructor & Destructor Documentation

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

Definition at line 22 of file ctr.cpp.

Referenced by make().

22  :
23  m_cipher(ciph),
24  m_counter(m_cipher->parallel_bytes()),
25  m_pad(m_counter.size()),
26  m_ctr_size(m_cipher->block_size()),
27  m_pad_pos(0)
28  {
29  }
Botan::CTR_BE::CTR_BE ( BlockCipher cipher,
size_t  ctr_size 
)

Definition at line 31 of file ctr.cpp.

31  :
32  m_cipher(cipher),
33  m_counter(m_cipher->parallel_bytes()),
34  m_pad(m_counter.size()),
35  m_ctr_size(ctr_size),
36  m_pad_pos(0)
37  {
38  //BOTAN_CHECK_ARG(m_ctr_size > 0 && m_ctr_size <= cipher->block_size(), "Invalid CTR size");
39  if(m_ctr_size == 0 || m_ctr_size > m_cipher->block_size())
40  throw Invalid_Argument("Invalid CTR-BE counter size");
41  }
void cipher(const byte in[], byte out[], size_t length) override
Definition: ctr.cpp:64

Member Function Documentation

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

References Botan::xor_buf().

65  {
66  while(length >= m_pad.size() - m_pad_pos)
67  {
68  xor_buf(out, in, &m_pad[m_pad_pos], m_pad.size() - m_pad_pos);
69  length -= (m_pad.size() - m_pad_pos);
70  in += (m_pad.size() - m_pad_pos);
71  out += (m_pad.size() - m_pad_pos);
72  increment_counter();
73  }
74  xor_buf(out, in, &m_pad[m_pad_pos], length);
75  m_pad_pos += length;
76  }
void xor_buf(T out[], const T in[], size_t length)
Definition: mem_ops.h:90
void Botan::StreamCipher::cipher1 ( byte  buf[],
size_t  len 
)
inlineinherited

Encrypt or decrypt a message

Parameters
bufthe plaintext / ciphertext
lenthe length of buf in bytes

Definition at line 50 of file stream_cipher.h.

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

51  { cipher(buf, buf, len); }
virtual void cipher(const byte in[], byte out[], size_t len)=0
void Botan::CTR_BE::clear ( )
overridevirtual

Implements Botan::SymmetricAlgorithm.

Definition at line 43 of file ctr.cpp.

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

44  {
45  m_cipher->clear();
46  zeroise(m_pad);
47  zeroise(m_counter);
48  m_pad_pos = 0;
49  }
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:186
CTR_BE* Botan::CTR_BE::clone ( ) const
inlineoverridevirtual

Get 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:22
std::unique_ptr< StreamCipher > Botan::StreamCipher::create ( const std::string &  algo_spec,
const std::string &  provider = "" 
)
staticinherited

Create an instance based on a name Will return a null pointer if the algo/provider combination cannot be found. If provider is empty then best available is chosen.

Definition at line 33 of file stream_cipher.cpp.

Referenced by Botan::get_cipher_mode(), Botan::get_stream_cipher(), Botan::Lion::make(), and Botan::make_stream_cipher().

35  {
36  return std::unique_ptr<StreamCipher>(make_a<StreamCipher>(Botan::StreamCipher::Spec(algo_spec), provider));
37  }
template<typename Alloc >
void Botan::StreamCipher::decrypt ( std::vector< byte, Alloc > &  inout)
inlineinherited

Definition at line 62 of file stream_cipher.h.

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

Definition at line 54 of file stream_cipher.h.

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

Definition at line 58 of file stream_cipher.h.

59  { cipher(inout.data(), inout.data(), inout.size()); }
virtual void cipher(const byte in[], byte out[], size_t len)=0
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  }
CTR_BE * Botan::CTR_BE::make ( const Spec spec)
static

Definition at line 12 of file ctr.cpp.

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

Referenced by Botan::StreamCipher::set_iv().

13  {
14  if(spec.algo_name() == "CTR-BE" && spec.arg_count() == 1)
15  {
16  if(auto c = BlockCipher::create(spec.arg(0)))
17  return new CTR_BE(c.release());
18  }
19  return nullptr;
20  }
static std::unique_ptr< BlockCipher > create(const std::string &algo_spec, const std::string &provider="")
CTR_BE(BlockCipher *cipher)
Definition: ctr.cpp:22
size_t Botan::SymmetricAlgorithm::maximum_keylength ( ) const
inlineinherited
Returns
minimum allowed key length

Definition at line 36 of file sym_algo.h.

37  {
38  return key_spec().maximum_keylength();
39  }
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
maxmium allowed key length

Definition at line 44 of file sym_algo.h.

45  {
46  return key_spec().minimum_keylength();
47  }
size_t minimum_keylength() const
Definition: key_spec.h:61
virtual Key_Length_Specification key_spec() const =0
std::string Botan::CTR_BE::name ( ) const
overridevirtual

Implements Botan::SymmetricAlgorithm.

Definition at line 59 of file ctr.cpp.

Referenced by set_iv().

60  {
61  return ("CTR-BE(" + m_cipher->name() + ")");
62  }
std::vector< std::string > Botan::StreamCipher::providers ( const std::string &  algo_spec)
staticinherited

Returns the list of available providers for this algorithm, empty if not available

Definition at line 39 of file stream_cipher.cpp.

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

40  {
41  return providers_of<StreamCipher>(StreamCipher::Spec(algo_spec));
42  }
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 128 of file ctr.cpp.

129  {
130  throw Not_Implemented("CTR_BE::seek");
131  }
void Botan::CTR_BE::set_iv ( const byte  [],
size_t  iv_len 
)
overridevirtual

Resync the cipher using the IV

Parameters
ivthe initialization vector
iv_lenthe length of the IV in bytes

Reimplemented from Botan::StreamCipher.

Definition at line 78 of file ctr.cpp.

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

Referenced by clear().

79  {
80  if(!valid_iv_length(iv_len))
81  throw Invalid_IV_Length(name(), iv_len);
82 
83  const size_t bs = m_cipher->block_size();
84 
85  zeroise(m_counter);
86 
87  const size_t n_wide = m_counter.size() / m_cipher->block_size();
88  buffer_insert(m_counter, 0, iv, iv_len);
89 
90  // Set m_counter blocks to IV, IV + 1, ... IV + n
91  for(size_t i = 1; i != n_wide; ++i)
92  {
93  buffer_insert(m_counter, i*bs, &m_counter[(i-1)*bs], bs);
94 
95  for(size_t j = 0; j != m_ctr_size; ++j)
96  if(++m_counter[i*bs + (bs - 1 - j)])
97  break;
98  }
99 
100  m_cipher->encrypt_n(m_counter.data(), m_pad.data(), n_wide);
101  m_pad_pos = 0;
102  }
std::string name() const override
Definition: ctr.cpp:59
size_t buffer_insert(std::vector< T, Alloc > &buf, size_t buf_offset, const T input[], size_t input_length)
Definition: secmem.h:108
bool valid_iv_length(size_t iv_len) const override
Definition: ctr.h:26
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:186
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 63 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::pbkdf2(), and Botan::TLS_PRF::TLS_PRF().

64  {
65  set_key(key.begin(), key.length());
66  }
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:63
template<typename Alloc >
void Botan::SymmetricAlgorithm::set_key ( const std::vector< byte, Alloc > &  key)
inlineinherited

Definition at line 69 of file sym_algo.h.

70  {
71  set_key(key.data(), key.size());
72  }
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:63
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 79 of file sym_algo.h.

80  {
81  if(!valid_keylength(length))
82  throw Invalid_Key_Length(name(), length);
83  key_schedule(key, length);
84  }
bool valid_keylength(size_t length) const
Definition: sym_algo.h:54
virtual std::string name() const =0
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()); }
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 54 of file sym_algo.h.

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

55  {
56  return key_spec().valid_keylength(length);
57  }
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: