Botan 3.0.0-alpha0
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::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
 
template<typename Alloc >
void decrypt (std::vector< uint8_t, Alloc > &inout)
 
virtual size_t default_iv_length () const
 
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
 
std::unique_ptr< StreamCiphernew_object () 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
 
template<typename Alloc >
void set_key (const std::vector< uint8_t, Alloc > &key)
 
void set_key (const SymmetricKey &key)
 
void set_key (const uint8_t key[], size_t length)
 
 SHAKE_128_Cipher ()
 
virtual bool valid_iv_length (size_t iv_len) const
 
bool valid_keylength (size_t length) const
 
virtual void write_keystream (uint8_t out[], size_t len)
 

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 15 of file shake_cipher.cpp.

15 :
16 m_buf_pos(0)
17 {}

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 19 of file shake_cipher.cpp.

20 {
21 const size_t SHAKE_128_BYTERATE = (1600-256)/8;
22
23 verify_key_set(m_state.empty() == false);
24
25 while(length >= SHAKE_128_BYTERATE - m_buf_pos)
26 {
27 xor_buf(out, in, &m_buffer[m_buf_pos], SHAKE_128_BYTERATE - m_buf_pos);
28 length -= (SHAKE_128_BYTERATE - m_buf_pos);
29 in += (SHAKE_128_BYTERATE - m_buf_pos);
30 out += (SHAKE_128_BYTERATE - m_buf_pos);
31
32 SHA_3::permute(m_state.data());
33 copy_out_le(m_buffer.data(), SHAKE_128_BYTERATE, m_state.data());
34
35 m_buf_pos = 0;
36 }
37 xor_buf(out, in, &m_buffer[m_buf_pos], length);
38 m_buf_pos += length;
39 }
static void permute(uint64_t A[25])
Definition: sha3.cpp:17
void verify_key_set(bool cond) const
Definition: sym_algo.h:171
void copy_out_le(uint8_t out[], size_t out_bytes, const T in[])
Definition: loadstor.h:690
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: mem_ops.h:255

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

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

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

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

◆ clear()

void Botan::SHAKE_128_Cipher::clear ( )
overridevirtual

Reset the state.

Implements Botan::SymmetricAlgorithm.

Definition at line 53 of file shake_cipher.cpp.

54 {
55 zap(m_state);
56 zap(m_buffer);
57 m_buf_pos = 0;
58 }
void zap(std::vector< T, Alloc > &vec)
Definition: secmem.h:124

References Botan::zap().

◆ clone()

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

Definition at line 130 of file stream_cipher.h.

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

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

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

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

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

Referenced by 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(), Botan::Sodium::crypto_stream_xchacha20_xor_ic(), Botan::Kyber_90s_Symmetric_Primitives::PRF(), and Botan::Kyber_90s_Symmetric_Primitives::XOF().

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

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

◆ default_iv_length()

virtual size_t Botan::StreamCipher::default_iv_length ( ) const
inlinevirtualinherited

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

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

Definition at line 119 of file stream_cipher.h.

119{ return 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 87 of file stream_cipher.h.

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

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

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

◆ key_spec()

Key_Length_Specification Botan::SHAKE_128_Cipher::key_spec ( ) const
overridevirtual
Returns
object describing limits on key size

Implements Botan::SymmetricAlgorithm.

Definition at line 75 of file shake_cipher.cpp.

76 {
77 return Key_Length_Specification(1, 160);
78 }

◆ maximum_keylength()

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

Definition at line 120 of file sym_algo.h.

121 {
122 return key_spec().maximum_keylength();
123 }
size_t maximum_keylength() const
Definition: sym_algo.h:70
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 128 of file sym_algo.h.

129 {
130 return key_spec().minimum_keylength();
131 }
size_t minimum_keylength() const
Definition: sym_algo.h:62

Referenced by botan_block_cipher_get_keyspec(), and botan_mac_get_keyspec().

◆ name()

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

Implements Botan::SymmetricAlgorithm.

Definition at line 80 of file shake_cipher.cpp.

81 {
82 return "SHAKE-128";
83 }

Referenced by set_iv().

◆ new_object()

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

Implements Botan::StreamCipher.

Definition at line 85 of file shake_cipher.cpp.

86 {
87 return std::make_unique<SHAKE_128_Cipher>();
88 }

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

150{ return "base"; }

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

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

134 {
135 return probe_providers_of<StreamCipher>(algo_spec);
136 }

◆ 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 70 of file shake_cipher.cpp.

71 {
72 throw Not_Implemented("SHAKE_128_Cipher::seek");
73 }

◆ 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 60 of file shake_cipher.cpp.

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

References name().

◆ set_key() [1/3]

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

Definition at line 153 of file sym_algo.h.

154 {
155 set_key(key.data(), key.size());
156 }
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:147

◆ set_key() [2/3]

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

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

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:138
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 125 of file stream_cipher.h.

125{ 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 138 of file sym_algo.h.

139 {
140 return key_spec().valid_keylength(length);
141 }
bool valid_keylength(size_t length) const
Definition: sym_algo.h:52

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

◆ verify_key_set()

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

Definition at line 171 of file sym_algo.h.

172 {
173 if(cond == false)
174 throw_key_not_set_error();
175 }

Referenced by Botan::ChaCha::cipher(), Botan::CTR_BE::cipher(), Botan::RC4::cipher(), Botan::Salsa20::cipher(), cipher(), 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::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::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::OCB_Encryption::finish(), Botan::OCB_Decryption::finish(), Botan::GHASH::ghash_update(), Botan::CFB_Encryption::process(), Botan::CFB_Decryption::process(), Botan::ChaCha::seek(), Botan::CTR_BE::seek(), Botan::Salsa20::seek(), Botan::OCB_Mode::set_associated_data(), Botan::ChaCha::set_iv(), Botan::Salsa20::set_iv(), Botan::GHASH::update(), Botan::GHASH::update_associated_data(), and Botan::ChaCha::write_keystream().

◆ write_keystream()

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

Write keystream bytes to a buffer

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

Reimplemented in Botan::ChaCha.

Definition at line 66 of file stream_cipher.h.

67 {
68 clear_mem(out, len);
69 cipher1(out, len);
70 }
void cipher1(uint8_t buf[], size_t len)
Definition: stream_cipher.h:78
constexpr void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:115

References Botan::clear_mem().

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


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