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

#include <shake_cipher.h>

Inheritance diagram for Botan::SHAKE_128_Cipher:
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
 
StreamCipherclone () const override
 
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)
 
 SHAKE_128_Cipher ()
 
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

SHAKE-128 XOF presented as a stream cipher

Definition at line 19 of file shake_cipher.h.

Constructor & Destructor Documentation

◆ SHAKE_128_Cipher()

Botan::SHAKE_128_Cipher::SHAKE_128_Cipher ( )

Definition at line 14 of file shake_cipher.cpp.

14  :
15  m_buf_pos(0)
16  {}

Member Function Documentation

◆ cipher()

void Botan::SHAKE_128_Cipher::cipher ( const uint8_t  in[],
uint8_t  out[],
size_t  length 
)
overridevirtual

Produce more XOF output

Implements Botan::StreamCipher.

Definition at line 18 of file shake_cipher.cpp.

References Botan::copy_out_le(), Botan::SHA_3::permute(), Botan::SymmetricAlgorithm::verify_key_set(), and Botan::xor_buf().

19  {
20  verify_key_set(m_state.empty() == false);
21 
22  while(length >= m_buffer.size() - m_buf_pos)
23  {
24  xor_buf(out, in, &m_buffer[m_buf_pos], m_buffer.size() - m_buf_pos);
25  length -= (m_buffer.size() - m_buf_pos);
26  in += (m_buffer.size() - m_buf_pos);
27  out += (m_buffer.size() - m_buf_pos);
28 
29  SHA_3::permute(m_state.data());
30  copy_out_le(m_buffer.data(), m_buffer.size(), m_state.data());
31 
32  m_buf_pos = 0;
33  }
34  xor_buf(out, in, &m_buffer[m_buf_pos], length);
35  m_buf_pos += length;
36  }
void verify_key_set(bool cond) const
Definition: sym_algo.h:95
void copy_out_le(uint8_t out[], size_t out_bytes, const T in[])
Definition: loadstor.h:675
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: mem_ops.h:174
static void permute(uint64_t A[25])
Definition: sha3.cpp:14

◆ 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::SHAKE_128_Cipher::clear ( )
overridevirtual

Reset the state.

Implements Botan::SymmetricAlgorithm.

Definition at line 56 of file shake_cipher.cpp.

References Botan::zap().

57  {
58  zap(m_state);
59  zap(m_buffer);
60  m_buf_pos = 0;
61  }
void zap(std::vector< T, Alloc > &vec)
Definition: secmem.h:193

◆ clone()

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

Implements Botan::StreamCipher.

Definition at line 52 of file shake_cipher.h.

52 { return new SHAKE_128_Cipher; }

◆ 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(), 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().

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

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

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

135  {
136  if(auto sc = StreamCipher::create(algo, provider))
137  {
138  return sc;
139  }
140  throw Lookup_Error("Stream cipher", algo, provider);
141  }
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::SHAKE_128_Cipher::key_spec ( ) const
inlineoverridevirtual

In principle SHAKE can accept arbitrary length inputs, but this does not seem required for a stream cipher.

Implements Botan::SymmetricAlgorithm.

Definition at line 45 of file shake_cipher.h.

46  {
47  return Key_Length_Specification(16, 160, 8);
48  }

◆ 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::SHAKE_128_Cipher::name ( ) const
inlineoverridevirtual
Returns
the algorithm name

Implements Botan::SymmetricAlgorithm.

Definition at line 51 of file shake_cipher.h.

Referenced by set_iv().

51 { return "SHAKE-128"; }

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

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

◆ seek()

void Botan::SHAKE_128_Cipher::seek ( uint64_t  offset)
overridevirtual

Seeking is not supported, this function will throw

Implements Botan::StreamCipher.

Definition at line 73 of file shake_cipher.cpp.

74  {
75  throw Not_Implemented("SHAKE_128_Cipher::seek");
76  }

◆ set_iv()

void Botan::SHAKE_128_Cipher::set_iv ( const uint8_t  iv[],
size_t  iv_len 
)
overridevirtual

IV not supported, this function will throw unless iv_len == 0

Implements Botan::StreamCipher.

Definition at line 63 of file shake_cipher.cpp.

References name().

64  {
65  /*
66  * This could be supported in some way (say, by treating iv as
67  * a prefix or suffix of the key).
68  */
69  if(length != 0)
70  throw Invalid_IV_Length(name(), length);
71  }
std::string name() const override
Definition: shake_cipher.h:51

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

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< 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

◆ set_key() [3/3]

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

◆ valid_iv_length()

bool Botan::SHAKE_128_Cipher::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 39 of file shake_cipher.h.

39 { return (iv_len == 0); }

◆ 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

◆ verify_key_set()

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

Definition at line 95 of file sym_algo.h.

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

96  {
97  if(cond == false)
98  throw Key_Not_Set(name());
99  }
virtual std::string name() const =0

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