Botan 3.13.0
Crypto and TLS for C&
Botan::XOF Class Referenceabstract

#include <xof.h>

Inheritance diagram for Botan::XOF:
Botan::AES_256_CTR_XOF Botan::Ascon_XOF128 Botan::SHAKE_XOF Botan::cSHAKE_XOF Botan::SHAKE_128_XOF Botan::SHAKE_256_XOF Botan::cSHAKE_128_XOF Botan::cSHAKE_256_XOF

Public Member Functions

virtual bool accepts_input () const =0
virtual size_t block_size () const =0
void clear ()
virtual std::unique_ptr< XOFcopy_state () const =0
virtual Key_Length_Specification key_spec () const
virtual std::string name () const =0
virtual std::unique_ptr< XOFnew_object () const =0
template<size_t count>
std::array< uint8_t, count > output ()
template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
output (size_t bytes)
void output (std::span< uint8_t > output)
uint8_t output_next_byte ()
std::vector< uint8_t > output_stdvec (size_t bytes)
virtual std::string provider () const
void start (std::span< const uint8_t > salt={}, std::span< const uint8_t > key={})
void update (std::span< const uint8_t > input)
virtual bool valid_salt_length (size_t salt_len) const
virtual ~XOF ()=default

Static Public Member Functions

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

Detailed Description

This class represents an eXtendable Output Function (XOF) objects

A XOF transforms an arbitrary length input message into an indefinite stream of output bits. Typically, it is illegal to call update() after the first call to output().

Definition at line 29 of file xof.h.

Constructor & Destructor Documentation

◆ ~XOF()

virtual Botan::XOF::~XOF ( )
virtualdefault

Member Function Documentation

◆ accepts_input()

virtual bool Botan::XOF::accepts_input ( ) const
pure virtual

Typically, this is true for new objects and becomes false once output() was called for the first time.

Returns
true iff calling update() is legal in the current object state

Implemented in Botan::AES_256_CTR_XOF, Botan::Ascon_XOF128, Botan::cSHAKE_XOF, and Botan::SHAKE_XOF.

◆ block_size()

virtual size_t Botan::XOF::block_size ( ) const
pure virtual

Return the internal block size of this XOF

Returns
the intrinsic processing block size of this XOF

Implemented in Botan::AES_256_CTR_XOF, Botan::Ascon_XOF128, Botan::cSHAKE_XOF, and Botan::SHAKE_XOF.

◆ clear()

◆ copy_state()

virtual std::unique_ptr< XOF > Botan::XOF::copy_state ( ) const
pure virtual

Return a new XOF object with the same state as *this.

If the XOF is not yet in the output phase, it efficiently allows using several messages with a common prefix. Otherwise, the copied state will produce the same output bit stream as the original object at the time of this invocation.

This function should be called clone but for consistency with other classes it is called copy_state.

Returns
new XOF object

Implemented in Botan::AES_256_CTR_XOF, Botan::Ascon_XOF128, Botan::cSHAKE_128_XOF, Botan::cSHAKE_256_XOF, Botan::SHAKE_128_XOF, and Botan::SHAKE_256_XOF.

◆ create()

std::unique_ptr< XOF > Botan::XOF::create ( std::string_view algo_spec,
std::string_view provider = "" )
static

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

Definition at line 28 of file xof.cpp.

28 {
29 const SCAN_Name req(algo_spec);
30
31 if(!provider.empty() && provider != "base") {
32 return nullptr; // unknown provider
33 }
34
35#if defined(BOTAN_HAS_SHAKE_XOF)
36 if(req.algo_name() == "SHAKE-128" && req.arg_count() == 0) {
37 return std::make_unique<SHAKE_128_XOF>();
38 }
39 if(req.algo_name() == "SHAKE-256" && req.arg_count() == 0) {
40 return std::make_unique<SHAKE_256_XOF>();
41 }
42#endif
43
44#if defined(BOTAN_HAS_ASCON_XOF128)
45 if(req.algo_name() == "Ascon-XOF128" && req.arg_count() == 0) {
46 return std::make_unique<Ascon_XOF128>();
47 }
48#endif
49
50 return nullptr;
51}
virtual std::string provider() const
Definition xof.cpp:66

References Botan::SCAN_Name::algo_name(), Botan::SCAN_Name::arg_count(), and provider().

Referenced by botan_xof_init(), create_or_throw(), and ~XOF().

◆ create_or_throw()

std::unique_ptr< XOF > Botan::XOF::create_or_throw ( std::string_view algo_spec,
std::string_view provider = "" )
static

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 Lookup_Error if not found.

Definition at line 54 of file xof.cpp.

54 {
55 if(auto xof = XOF::create(algo_spec, provider)) {
56 return xof;
57 }
58 throw Lookup_Error("XOF", algo_spec, provider);
59}
static std::unique_ptr< XOF > create(std::string_view algo_spec, std::string_view provider="")
Definition xof.cpp:28

References create(), and provider().

Referenced by Botan::AES_256_CTR_XOF::AES_256_CTR_XOF(), Botan::create_pk_from_sk(), Botan::Kyber_Modern_Symmetric_Primitives::create_PRF(), Botan::ML_KEM_Symmetric_Primitives::create_PRF(), Botan::Kyber_Modern_Symmetric_Primitives::create_XOF(), Botan::ML_KEM_Symmetric_Primitives::create_XOF(), Botan::Dilithium_Symmetric_Primitives_Base::H(), Botan::Dilithium_Symmetric_Primitives_Base::H(), Botan::Dilithium_Symmetric_Primitives_Base::H_256(), Botan::Classic_McEliece_Parameters::prg(), Botan::sign_message(), and ~XOF().

◆ key_spec()

virtual Key_Length_Specification Botan::XOF::key_spec ( ) const
inlinevirtual

Return the key lengths supported by this XOF

Returns
an object describing limits on the key size

Reimplemented in Botan::AES_256_CTR_XOF.

Definition at line 102 of file xof.h.

102 {
103 // Keys are not supported by default
104 return Key_Length_Specification(0);
105 }

Referenced by start().

◆ name()

virtual std::string Botan::XOF::name ( ) const
pure virtual

Return the name of this XOF

Returns
the hash function name

Implemented in Botan::AES_256_CTR_XOF, Botan::Ascon_XOF128, Botan::cSHAKE_128_XOF, Botan::cSHAKE_256_XOF, Botan::SHAKE_128_XOF, and Botan::SHAKE_256_XOF.

References start().

Referenced by start().

◆ new_object()

virtual std::unique_ptr< XOF > Botan::XOF::new_object ( ) const
pure virtual

Create a new uninitialized object of the same type

Returns
new object representing the same algorithm as *this

Implemented in Botan::AES_256_CTR_XOF, Botan::Ascon_XOF128, Botan::cSHAKE_128_XOF, Botan::cSHAKE_256_XOF, Botan::SHAKE_128_XOF, and Botan::SHAKE_256_XOF.

◆ output() [1/3]

template<size_t count>
std::array< uint8_t, count > Botan::XOF::output ( )
inline

Generate a fixed number of output bytes into a std::array

Returns
the next count output bytes as a std::array<>.

Definition at line 172 of file xof.h.

172 {
173 std::array<uint8_t, count> out; // NOLINT(*-member-init)
174 generate_bytes(out);
175 return out;
176 }

◆ output() [2/3]

template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
T Botan::XOF::output ( size_t bytes)
inline

Generate output bytes into a newly allocated container

Returns
the next bytes output bytes as the specified container type T.

Definition at line 161 of file xof.h.

161 {
162 T out(bytes);
163 generate_bytes(out);
164 return out;
165 }

Referenced by Botan::FrodoMatrix::make_sample_generator(), output(), and output_stdvec().

◆ output() [3/3]

void Botan::XOF::output ( std::span< uint8_t > output)
inline

Generate output bytes into a caller provided buffer Fill output with the next output bytes. The number of bytes depends on the size of output.

Definition at line 191 of file xof.h.

191{ generate_bytes(output); }
T output(size_t bytes)
Definition xof.h:161

References output().

◆ output_next_byte()

uint8_t Botan::XOF::output_next_byte ( )
inline

Generate a single output byte

Returns
the next single output byte

Definition at line 197 of file xof.h.

197 {
198 uint8_t out = 0;
199 generate_bytes({&out, 1});
200 return out;
201 }

◆ output_stdvec()

std::vector< uint8_t > Botan::XOF::output_stdvec ( size_t bytes)
inline

Convenience overload to generate a std::vector<uint8_t>. Same as calling XOF::output<std::vector<uint8_t>>().

Returns
the next bytes output bytes as a byte vector.

Definition at line 184 of file xof.h.

184{ return output<std::vector<uint8_t>>(bytes); }

References output().

◆ provider()

std::string Botan::XOF::provider ( ) const
virtual

Return the name of the provider implementing this object

Returns
provider information about this implementation. Default is "base", might also return "sse2", "avx2", "openssl", or some other arbitrary string.

Reimplemented in Botan::Ascon_XOF128, Botan::cSHAKE_XOF, and Botan::SHAKE_XOF.

Definition at line 66 of file xof.cpp.

66 {
67 return "base";
68}

Referenced by create(), create_or_throw(), and ~XOF().

◆ providers()

std::vector< std::string > Botan::XOF::providers ( std::string_view algo_spec)
static

List the providers available for a given XOF

Returns
list of available providers for this algorithm, empty if not available
Parameters
algo_specalgorithm name

Definition at line 62 of file xof.cpp.

62 {
63 return probe_providers_of<XOF>(algo_spec, {"base"});
64}
std::vector< std::string > probe_providers_of(std::string_view algo_spec, const std::vector< std::string > &possible={"base"})
Definition scan_name.h:99

References Botan::probe_providers_of().

Referenced by ~XOF().

◆ start()

void Botan::XOF::start ( std::span< const uint8_t > salt = {},
std::span< const uint8_t > key = {} )

Some XOFs can be parameterized with a salt and/or key. If required, this must be called before calling XOF::update().

See also
XOF::valid_salt_length()
XOF::key_spec()
Parameters
salta salt value to parameterize the XOF
keya key to parameterize the XOF

Definition at line 70 of file xof.cpp.

70 {
71 if(!key_spec().valid_keylength(key.size())) {
72 throw Invalid_Key_Length(name(), key.size());
73 }
74
75 if(!valid_salt_length(salt.size())) {
76 throw Invalid_Argument(fmt("{} cannot accept a salt length of {}", name(), salt.size()));
77 }
78
79 m_xof_started = true;
80 start_msg(salt, key);
81}
virtual bool valid_salt_length(size_t salt_len) const
Definition xof.h:93
virtual Key_Length_Specification key_spec() const
Definition xof.h:102
virtual std::string name() const =0
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53

References Botan::fmt(), key_spec(), name(), and valid_salt_length().

Referenced by name(), and update().

◆ update()

void Botan::XOF::update ( std::span< const uint8_t > input)
inline

Add input data to the XOF's internal state

Parameters
inputthe data that shall be

Definition at line 147 of file xof.h.

147 {
148 if(!m_xof_started) {
149 // If the user didn't start() before the first input, we enforce
150 // it with a default value, here.
151 start();
152 }
153 add_data(input);
154 }
void start(std::span< const uint8_t > salt={}, std::span< const uint8_t > key={})
Definition xof.cpp:70

References start().

Referenced by Botan::Kyber_Modern_Symmetric_Primitives::init_PRF(), Botan::ML_KEM_Symmetric_Primitives::init_PRF(), Botan::Kyber_Modern_Symmetric_Primitives::init_XOF(), and Botan::ML_KEM_Symmetric_Primitives::init_XOF().

◆ valid_salt_length()

virtual bool Botan::XOF::valid_salt_length ( size_t salt_len) const
inlinevirtual

Test if a salt length is valid for this XOF

Returns
true if salt length is acceptable, false otherwise

Reimplemented in Botan::AES_256_CTR_XOF, and Botan::cSHAKE_XOF.

Definition at line 93 of file xof.h.

93 {
94 // Salts are not supported by default
95 return salt_len == 0;
96 }

Referenced by start().


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