Botan 3.10.0
Crypto and TLS for C&
xof.cpp
Go to the documentation of this file.
1/*
2* Extendable Output Function Base Class
3* (C) 2023 Jack Lloyd
4* 2023 Fabian Albert, René Meusel - Rohde & Schwarz Cybersecurity
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/xof.h>
10
11#include <botan/assert.h>
12#include <botan/internal/scan_name.h>
13
14#if defined(BOTAN_HAS_SHAKE_XOF)
15 #include <botan/internal/shake_xof.h>
16#endif
17
18#if defined(BOTAN_HAS_ASCON_XOF128)
19 #include <botan/internal/ascon_xof128.h>
20#endif
21
22#include <botan/exceptn.h>
23#include <botan/internal/fmt.h>
24
25namespace Botan {
26
27//static
28std::unique_ptr<XOF> XOF::create(std::string_view algo_spec, std::string_view provider) {
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}
52
53//static
54std::unique_ptr<XOF> XOF::create_or_throw(std::string_view algo_spec, std::string_view provider) {
55 if(auto xof = XOF::create(algo_spec, provider)) {
56 return xof;
57 }
58 throw Lookup_Error("XOF", algo_spec, provider);
59}
60
61// static
62std::vector<std::string> XOF::providers(std::string_view algo_spec) {
63 return probe_providers_of<XOF>(algo_spec, {"base"});
64}
65
66std::string XOF::provider() const {
67 return "base";
68}
69
70void XOF::start(std::span<const uint8_t> salt, std::span<const uint8_t> key) {
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}
82
83void XOF::start_msg(std::span<const uint8_t> salt, std::span<const uint8_t> key) {
84 BOTAN_UNUSED(salt, key);
85}
86
87} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:144
size_t arg_count() const
Definition scan_name.h:49
const std::string & algo_name() const
Definition scan_name.h:44
static std::unique_ptr< XOF > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition xof.cpp:54
virtual std::string provider() const
Definition xof.cpp:66
virtual bool valid_salt_length(size_t salt_len) const
Definition xof.h:89
virtual Key_Length_Specification key_spec() const
Definition xof.h:97
static std::unique_ptr< XOF > create(std::string_view algo_spec, std::string_view provider="")
Definition xof.cpp:28
virtual std::string name() const =0
void start(std::span< const uint8_t > salt={}, std::span< const uint8_t > key={})
Definition xof.cpp:70
static std::vector< std::string > providers(std::string_view algo_spec)
Definition xof.cpp:62
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
std::vector< std::string > probe_providers_of(std::string_view algo_spec, const std::vector< std::string > &possible={"base"})
Definition scan_name.h:105