Botan 3.3.0
Crypto and TLS for C&
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::SHAKE_Cipher Botan::StreamCipher Botan::SymmetricAlgorithm

Public Member Functions

size_t buffer_size () const final
 
void cipher (const uint8_t in[], uint8_t out[], size_t len)
 
void cipher (std::span< const uint8_t > in, std::span< uint8_t > out)
 
void cipher1 (std::span< uint8_t > buf)
 
void cipher1 (uint8_t buf[], size_t len)
 
void clear () final
 
StreamCipherclone () const
 
void decrypt (std::span< uint8_t > inout)
 
virtual size_t default_iv_length () const
 
void encipher (std::span< uint8_t > inout)
 
void encrypt (std::span< uint8_t > inout)
 
bool has_keying_material () const final
 
Key_Length_Specification key_spec () const final
 
template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
T keystream_bytes (size_t bytes)
 
size_t maximum_keylength () const
 
size_t minimum_keylength () const
 
std::string name () const override
 
std::unique_ptr< StreamCiphernew_object () const override
 
virtual std::string provider () const
 
void seek (uint64_t offset) final
 
void set_iv (const uint8_t iv[], size_t iv_len)
 
void set_iv (std::span< const uint8_t > iv)
 
void set_key (const SymmetricKey &key)
 
void set_key (const uint8_t key[], size_t length)
 
void set_key (std::span< const uint8_t > key)
 
 SHAKE_128_Cipher ()
 
virtual bool valid_iv_length (size_t iv_len) const
 
bool valid_keylength (size_t length) const
 
void write_keystream (std::span< uint8_t > out)
 
void write_keystream (uint8_t out[], size_t len)
 

Static Public Member Functions

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

Protected Member Functions

void assert_key_material_set () const
 
void assert_key_material_set (bool predicate) const
 

Detailed Description

Definition at line 60 of file shake_cipher.h.

Constructor & Destructor Documentation

◆ SHAKE_128_Cipher()

Botan::SHAKE_128_Cipher::SHAKE_128_Cipher ( )

Definition at line 96 of file shake_cipher.cpp.

96: SHAKE_Cipher(256) {}
SHAKE_Cipher(size_t keccak_capacity)

Member Function Documentation

◆ assert_key_material_set() [1/2]

void Botan::SymmetricAlgorithm::assert_key_material_set ( ) const
inlineprotectedinherited

Definition at line 139 of file sym_algo.h.

virtual bool has_keying_material() const =0
void assert_key_material_set() const
Definition sym_algo.h:139

References Botan::SymmetricAlgorithm::assert_key_material_set().

Referenced by Botan::SymmetricAlgorithm::assert_key_material_set(), Botan::Salsa20::cipher_bytes(), Botan::AES_128::decrypt_n(), Botan::AES_192::decrypt_n(), Botan::AES_256::decrypt_n(), Botan::ARIA_128::decrypt_n(), Botan::ARIA_192::decrypt_n(), Botan::ARIA_256::decrypt_n(), Botan::Blowfish::decrypt_n(), Botan::Camellia_128::decrypt_n(), Botan::Camellia_192::decrypt_n(), Botan::Camellia_256::decrypt_n(), Botan::CAST_128::decrypt_n(), Botan::DES::decrypt_n(), Botan::TripleDES::decrypt_n(), Botan::GOST_28147_89::decrypt_n(), Botan::IDEA::decrypt_n(), Botan::Kuznyechik::decrypt_n(), Botan::Lion::decrypt_n(), Botan::Noekeon::decrypt_n(), Botan::SEED::decrypt_n(), Botan::Serpent::decrypt_n(), Botan::SHACAL2::decrypt_n(), Botan::SM4::decrypt_n(), Botan::Threefish_512::decrypt_n(), Botan::Twofish::decrypt_n(), Botan::AES_128::encrypt_n(), Botan::AES_192::encrypt_n(), Botan::AES_256::encrypt_n(), Botan::ARIA_128::encrypt_n(), Botan::ARIA_192::encrypt_n(), Botan::ARIA_256::encrypt_n(), Botan::Blowfish::encrypt_n(), Botan::Camellia_128::encrypt_n(), Botan::Camellia_192::encrypt_n(), Botan::Camellia_256::encrypt_n(), Botan::CAST_128::encrypt_n(), Botan::DES::encrypt_n(), Botan::TripleDES::encrypt_n(), Botan::GOST_28147_89::encrypt_n(), Botan::IDEA::encrypt_n(), Botan::Kuznyechik::encrypt_n(), Botan::Lion::encrypt_n(), Botan::Noekeon::encrypt_n(), Botan::SEED::encrypt_n(), Botan::Serpent::encrypt_n(), Botan::SHACAL2::encrypt_n(), Botan::SM4::encrypt_n(), Botan::Threefish_512::encrypt_n(), Botan::Twofish::encrypt_n(), Botan::GHASH::final(), Botan::GHASH::ghash_update(), Botan::ChaCha::seek(), Botan::CTR_BE::seek(), Botan::Salsa20::seek(), Botan::OCB_Mode::set_associated_data_n(), Botan::Salsa20::set_iv_bytes(), Botan::GHASH::update(), and Botan::GHASH::update_associated_data().

◆ assert_key_material_set() [2/2]

void Botan::SymmetricAlgorithm::assert_key_material_set ( bool predicate) const
inlineprotectedinherited

Definition at line 141 of file sym_algo.h.

141 {
142 if(!predicate) {
143 throw_key_not_set_error();
144 }
145 }

◆ buffer_size()

size_t Botan::SHAKE_Cipher::buffer_size ( ) const
inlinefinalvirtualinherited

Return the optimium buffer size to use with this cipher

Most stream ciphers internally produce blocks of bytes. This function returns that block size. Aligning buffer sizes to a multiple of this size may improve performance by reducing internal buffering overhead.

Note the return value of this function may change for any particular algorithm due to changes in the implementation from release to release, or changes in the runtime environment (such as CPUID indicating availability of an optimized implementation). It is not intrinsic to the algorithm; it is just a suggestion for gaining best performance.

Implements Botan::StreamCipher.

Definition at line 36 of file shake_cipher.h.

36{ return m_keccak.byte_rate(); }
size_t byte_rate() const
Definition keccak_perm.h:55

References Botan::Keccak_Permutation::byte_rate().

◆ cipher() [1/2]

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

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

Definition at line 56 of file stream_cipher.h.

56{ cipher_bytes(in, out, len); }
virtual void cipher_bytes(const uint8_t in[], uint8_t out[], size_t len)=0

Referenced by Botan::StreamCipher::create(), Botan::Sodium::crypto_stream_salsa20_xor_ic(), and Botan::Sodium::crypto_stream_xsalsa20_xor_ic().

◆ cipher() [2/2]

void Botan::StreamCipher::cipher ( std::span< const uint8_t > in,
std::span< uint8_t > out )
inlineinherited

Encrypt or decrypt a message

Parameters
inthe plaintext
outthe byte array to hold the output, i.e. the ciphertext with at least the same size as in

Definition at line 64 of file stream_cipher.h.

64 {
65 BOTAN_ARG_CHECK(in.size() <= out.size(),
66 "Output buffer of stream cipher must be at least as long as input buffer");
67 cipher_bytes(in.data(), out.data(), in.size());
68 }
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29

References BOTAN_ARG_CHECK.

◆ cipher1() [1/2]

void Botan::StreamCipher::cipher1 ( std::span< uint8_t > buf)
inlineinherited

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

Parameters
bufthe plaintext / ciphertext

Definition at line 114 of file stream_cipher.h.

114{ cipher(buf, buf); }
void cipher(const uint8_t in[], uint8_t out[], size_t len)

◆ cipher1() [2/2]

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

107{ cipher(buf, buf, len); }

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

◆ clear()

void Botan::SHAKE_Cipher::clear ( )
finalvirtualinherited

Reset the internal state. This includes not just the key, but any partial message that may have been in process.

Implements Botan::SymmetricAlgorithm.

Definition at line 36 of file shake_cipher.cpp.

36 {
37 m_keccak.clear();
38 m_has_keying_material = false;
39 zeroise(m_keystream_buffer);
40 m_bytes_generated = 0;
41}
void zeroise(std::vector< T, Alloc > &vec)
Definition secmem.h:108

References Botan::Keccak_Permutation::clear(), and Botan::zeroise().

◆ clone()

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

Definition at line 182 of file stream_cipher.h.

182{ return this->new_object().release(); }
virtual std::unique_ptr< StreamCipher > new_object() const =0

◆ create()

std::unique_ptr< StreamCipher > Botan::StreamCipher::create ( std::string_view algo_spec,
std::string_view 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 40 of file stream_cipher.cpp.

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

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

◆ create_or_throw()

std::unique_ptr< StreamCipher > Botan::StreamCipher::create_or_throw ( std::string_view algo_spec,
std::string_view 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 122 of file stream_cipher.cpp.

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

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

Referenced by Botan::ChaCha_RNG::ChaCha_RNG(), Botan::ChaCha_RNG::ChaCha_RNG(), Botan::ChaCha_RNG::ChaCha_RNG(), Botan::ChaCha_RNG::ChaCha_RNG(), Botan::ChaCha_RNG::ChaCha_RNG(), Botan::Sodium::crypto_secretbox_detached(), Botan::Sodium::crypto_secretbox_open_detached(), Botan::Sodium::crypto_secretbox_xsalsa20poly1305(), Botan::Sodium::crypto_secretbox_xsalsa20poly1305_open(), Botan::Sodium::crypto_stream_chacha20(), Botan::Sodium::crypto_stream_chacha20_ietf(), Botan::Sodium::crypto_stream_chacha20_ietf_xor_ic(), Botan::Sodium::crypto_stream_chacha20_xor_ic(), Botan::Sodium::crypto_stream_xchacha20(), and Botan::Sodium::crypto_stream_xchacha20_xor_ic().

◆ decrypt()

void Botan::StreamCipher::decrypt ( std::span< uint8_t > inout)
inlineinherited

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

Parameters
inoutthe plaintext / ciphertext

Definition at line 135 of file stream_cipher.h.

135{ cipher(inout.data(), inout.data(), inout.size()); }

◆ default_iv_length()

size_t Botan::StreamCipher::default_iv_length ( ) const
virtualinherited

Return the default (preferred) nonce length If this function returns 0, then this cipher does not support nonces

Default implementation returns 0

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

Definition at line 133 of file stream_cipher.cpp.

133 {
134 return 0;
135}

◆ encipher()

void Botan::StreamCipher::encipher ( std::span< uint8_t > inout)
inlineinherited

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

Parameters
inoutthe plaintext / ciphertext

Definition at line 121 of file stream_cipher.h.

121{ cipher(inout.data(), inout.data(), inout.size()); }

◆ encrypt()

void Botan::StreamCipher::encrypt ( std::span< uint8_t > inout)
inlineinherited

Encrypt a message The message is encrypted in place.

Parameters
inoutthe plaintext / ciphertext

Definition at line 128 of file stream_cipher.h.

128{ cipher(inout.data(), inout.data(), inout.size()); }

◆ has_keying_material()

bool Botan::SHAKE_Cipher::has_keying_material ( ) const
inlinefinalvirtualinherited
Returns
true if a key has been set on this object

Implements Botan::SymmetricAlgorithm.

Definition at line 34 of file shake_cipher.h.

34{ return m_has_keying_material; }

◆ key_spec()

Key_Length_Specification Botan::SHAKE_Cipher::key_spec ( ) const
finalvirtualinherited
Returns
object describing limits on key size

Implements Botan::SymmetricAlgorithm.

Definition at line 92 of file shake_cipher.cpp.

92 {
93 return Key_Length_Specification(1, 160);
94}

◆ keystream_bytes()

template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
T Botan::StreamCipher::keystream_bytes ( size_t bytes)
inlineinherited

Get bytes from the keystream

Parameters
bytesThe number of bytes to be produced

Definition at line 95 of file stream_cipher.h.

95 {
96 T out(bytes);
97 write_keystream(out);
98 return out;
99 }
void write_keystream(uint8_t out[], size_t len)
FE_25519 T
Definition ge.cpp:34

References T.

◆ maximum_keylength()

size_t Botan::SymmetricAlgorithm::maximum_keylength ( ) const
inlineinherited
Returns
maximum allowed key length

Definition at line 95 of file sym_algo.h.

95{ return key_spec().maximum_keylength(); }
size_t maximum_keylength() const
Definition sym_algo.h:54
virtual Key_Length_Specification key_spec() const =0

◆ minimum_keylength()

size_t Botan::SymmetricAlgorithm::minimum_keylength ( ) const
inlineinherited
Returns
minimum allowed key length

Definition at line 100 of file sym_algo.h.

100{ return key_spec().minimum_keylength(); }
size_t minimum_keylength() const
Definition sym_algo.h:49

◆ name()

std::string Botan::SHAKE_128_Cipher::name ( ) const
inlineoverridevirtual
Returns
the algorithm name

Implements Botan::SymmetricAlgorithm.

Definition at line 64 of file shake_cipher.h.

64{ return "SHAKE-128"; }

◆ new_object()

std::unique_ptr< StreamCipher > Botan::SHAKE_128_Cipher::new_object ( ) const
inlineoverridevirtual
Returns
new object representing the same algorithm as *this

Implements Botan::StreamCipher.

Definition at line 66 of file shake_cipher.h.

66{ return std::make_unique<SHAKE_128_Cipher>(); }

◆ provider()

virtual std::string Botan::StreamCipher::provider ( ) const
inlinevirtualinherited
Returns
provider information about this implementation. Default is "base", might also return "sse2", "avx2" or some other arbitrary string.

Reimplemented in Botan::ChaCha.

Definition at line 199 of file stream_cipher.h.

199{ return "base"; }

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

◆ providers()

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

Definition at line 129 of file stream_cipher.cpp.

129 {
130 return probe_providers_of<StreamCipher>(algo_spec);
131}

◆ seek()

void Botan::SHAKE_Cipher::seek ( uint64_t offset)
finalvirtualinherited

Seeking is not supported, this function will throw

Implements Botan::StreamCipher.

Definition at line 32 of file shake_cipher.cpp.

32 {
33 throw Not_Implemented("SHAKE_Cipher::seek");
34}

◆ set_iv() [1/2]

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

Resync the cipher using the IV

Parameters
ivthe initialization vector
iv_lenthe length of the IV in bytes

Definition at line 157 of file stream_cipher.h.

157{ set_iv_bytes(iv, iv_len); }
virtual void set_iv_bytes(const uint8_t iv[], size_t iv_len)=0

Referenced by Botan::Sodium::crypto_stream_salsa20(), Botan::Sodium::crypto_stream_salsa20_xor_ic(), Botan::Sodium::crypto_stream_xsalsa20(), Botan::Sodium::crypto_stream_xsalsa20_xor_ic(), Botan::Sodium::randombytes_buf_deterministic(), and Botan::SIV_Mode::set_ctr_iv().

◆ set_iv() [2/2]

void Botan::StreamCipher::set_iv ( std::span< const uint8_t > iv)
inlineinherited

Resync the cipher using the IV

Parameters
ivthe initialization vector

Definition at line 163 of file stream_cipher.h.

163{ set_iv_bytes(iv.data(), iv.size()); }

◆ set_key() [1/3]

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

◆ set_key() [2/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 126 of file sym_algo.h.

126{ set_key(std::span{key, length}); }

References Botan::SymmetricAlgorithm::set_key().

Referenced by Botan::SymmetricAlgorithm::set_key().

◆ set_key() [3/3]

void Botan::SymmetricAlgorithm::set_key ( std::span< const uint8_t > key)
inherited

Set the symmetric key of this object.

Parameters
keythe contiguous byte range to be set.

Definition at line 17 of file sym_algo.cpp.

17 {
18 if(!valid_keylength(key.size())) {
19 throw Invalid_Key_Length(name(), key.size());
20 }
21 key_schedule(key);
22}
bool valid_keylength(size_t length) const
Definition sym_algo.h:107
virtual std::string name() const =0

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

◆ valid_iv_length()

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

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

Definition at line 177 of file stream_cipher.h.

177{ 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 107 of file sym_algo.h.

107{ return key_spec().valid_keylength(length); }
bool valid_keylength(size_t length) const
Definition sym_algo.h:42

Referenced by Botan::SymmetricAlgorithm::set_key().

◆ write_keystream() [1/2]

void Botan::StreamCipher::write_keystream ( std::span< uint8_t > out)
inlineinherited

Fill a given buffer with keystream bytes

The contents of out are ignored/overwritten

Parameters
outthe byte array to hold the keystream

Definition at line 87 of file stream_cipher.h.

87{ generate_keystream(out.data(), out.size()); }
virtual void generate_keystream(uint8_t out[], size_t len)

◆ write_keystream() [2/2]

void Botan::StreamCipher::write_keystream ( uint8_t out[],
size_t len )
inlineinherited

Write keystream bytes to a buffer

The contents of out are ignored/overwritten

Parameters
outthe byte array to hold the keystream
lenthe length of out in bytes

Definition at line 78 of file stream_cipher.h.

78{ generate_keystream(out, len); }

Referenced by Botan::Sodium::crypto_stream_salsa20(), Botan::Sodium::crypto_stream_xsalsa20(), and Botan::Sodium::randombytes_buf_deterministic().


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