Botan 3.13.0
Crypto and TLS for C&
xof.h
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#ifndef BOTAN_XOF_BASE_CLASS_H_
10#define BOTAN_XOF_BASE_CLASS_H_
11
12#include <botan/concepts.h>
13#include <botan/secmem.h>
14#include <botan/sym_algo.h>
15#include <memory>
16#include <string>
17#include <string_view>
18#include <vector>
19
20namespace Botan {
21
22/**
23 * This class represents an eXtendable Output Function (XOF) objects
24 *
25 * A XOF transforms an arbitrary length input message into an indefinite
26 * stream of output bits. Typically, it is illegal to call `update()` after
27 * the first call to `output()`.
28 */
29class BOTAN_PUBLIC_API(3, 2) XOF /* NOLINT(*special-member-functions) */ {
30 public:
31 virtual ~XOF() = default;
32
33 /**
34 * Create an instance based on a name, or return null if the
35 * algo/provider combination cannot be found. If provider is
36 * empty then best available is chosen.
37 */
38 static std::unique_ptr<XOF> create(std::string_view algo_spec, std::string_view provider = "");
39
40 /**
41 * Create an instance based on a name
42 * If provider is empty then best available is chosen.
43 * @param algo_spec algorithm name
44 * @param provider provider implementation to use
45 * Throws Lookup_Error if not found.
46 */
47 static std::unique_ptr<XOF> create_or_throw(std::string_view algo_spec, std::string_view provider = "");
48
49 /**
50 * List the providers available for a given XOF
51 * @return list of available providers for this algorithm, empty if not available
52 * @param algo_spec algorithm name
53 */
54 static std::vector<std::string> providers(std::string_view algo_spec);
55
56 /**
57 * Return the name of the provider implementing this object
58 * @return provider information about this implementation. Default is "base",
59 * might also return "sse2", "avx2", "openssl", or some other arbitrary string.
60 */
61 virtual std::string provider() const;
62
63 /**
64 * Reset the state.
65 */
66 void clear() {
67 m_xof_started = false;
68 reset();
69 }
70
71 /**
72 * Return the name of this XOF
73 * @return the hash function name
74 */
75 virtual std::string name() const = 0;
76
77 /**
78 * Some XOFs can be parameterized with a @p salt and/or @p key. If required,
79 * this must be called before calling XOF::update().
80 *
81 * @sa XOF::valid_salt_length()
82 * @sa XOF::key_spec()
83 *
84 * @param salt a salt value to parameterize the XOF
85 * @param key a key to parameterize the XOF
86 */
87 void start(std::span<const uint8_t> salt = {}, std::span<const uint8_t> key = {});
88
89 /**
90 * Test if a salt length is valid for this XOF
91 * @returns true if salt length is acceptable, false otherwise
92 */
93 virtual bool valid_salt_length(size_t salt_len) const {
94 // Salts are not supported by default
95 return salt_len == 0;
96 }
97
98 /**
99 * Return the key lengths supported by this XOF
100 * @returns an object describing limits on the key size
101 */
103 // Keys are not supported by default
104 return Key_Length_Specification(0);
105 }
106
107 /**
108 * Return the internal block size of this XOF
109 * @return the intrinsic processing block size of this XOF
110 */
111 virtual size_t block_size() const = 0;
112
113 /**
114 * Return a new XOF object with the same state as *this.
115 *
116 * If the XOF is not yet in the output phase, it efficiently allows
117 * using several messages with a common prefix.
118 * Otherwise, the copied state will produce the same output
119 * bit stream as the original object at the time of this invocation.
120 *
121 * This function should be called `clone` but for consistency with
122 * other classes it is called `copy_state`.
123 *
124 * @return new XOF object
125 */
126 virtual std::unique_ptr<XOF> copy_state() const = 0;
127
128 /**
129 * Create a new uninitialized object of the same type
130 * @return new object representing the same algorithm as *this
131 */
132 virtual std::unique_ptr<XOF> new_object() const = 0;
133
134 /**
135 * Typically, this is `true` for new objects and becomes `false`
136 * once `output()` was called for the first time.
137 *
138 * @returns true iff calling `update()` is legal in the current object state
139 */
140 virtual bool accepts_input() const = 0;
141
142 /**
143 * Add @p input data to the XOF's internal state
144 *
145 * @param input the data that shall be
146 */
147 void update(std::span<const uint8_t> input) {
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 }
155
156 /**
157 * Generate output bytes into a newly allocated container
158 * @return the next @p bytes output bytes as the specified container type @p T.
159 */
160 template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
161 T output(size_t bytes) {
162 T out(bytes);
163 generate_bytes(out);
164 return out;
165 }
166
167 /**
168 * Generate a fixed number of output bytes into a std::array
169 * @return the next @p count output bytes as a std::array<>.
170 */
171 template <size_t count>
172 std::array<uint8_t, count> output() {
173 std::array<uint8_t, count> out; // NOLINT(*-member-init)
174 generate_bytes(out);
175 return out;
176 }
177
178 /**
179 * Convenience overload to generate a std::vector<uint8_t>. Same as calling
180 * `XOF::output<std::vector<uint8_t>>()`.
181 *
182 * @return the next @p bytes output bytes as a byte vector.
183 */
184 std::vector<uint8_t> output_stdvec(size_t bytes) { return output<std::vector<uint8_t>>(bytes); }
185
186 /**
187 * Generate output bytes into a caller provided buffer
188 * Fill @p output with the next output bytes. The number of bytes
189 * depends on the size of @p output.
190 */
191 void output(std::span<uint8_t> output) { generate_bytes(output); }
192
193 /**
194 * Generate a single output byte
195 * @return the next single output byte
196 */
198 uint8_t out = 0;
199 generate_bytes({&out, 1});
200 return out;
201 }
202
203 private:
204 /**
205 * Take @p salt and/or @p key to pre-parameterize the XOF. This must be called
206 * before calling XOF::update().
207 *
208 * @param salt a salt value to parameterize the XOF
209 * @param key a key to parameterize the XOF
210 */
211 virtual void start_msg(std::span<const uint8_t> salt, std::span<const uint8_t> key);
212
213 /**
214 * Consume @p input data bytes into the XOF's internal state
215 *
216 * Typically, XOFs may consume an arbitrary length of input data but
217 * should refuse accepting more input once the first output bit was
218 * generated. Implementations should throw `Invalid_State` in this
219 * case.
220 *
221 * @param input the span to be consumed entirely into the internal state
222 * @throws Invalid_State if input is added after generating output
223 */
224 virtual void add_data(std::span<const uint8_t> input) = 0;
225
226 /**
227 * Fill the entire @p output span with the next bytes in their output
228 * stream.
229 *
230 * The first invocation to `generate_bytes()` should typically transition
231 * the XOF's state to "output mode" and prevent any further calls to
232 * `XOF::add_data()`.
233 *
234 * @param output the span to be filled entirely with output bytes
235 */
236 virtual void generate_bytes(std::span<uint8_t> output) = 0;
237
238 /**
239 * Clear the XOF's internal state and allow for new input.
240 */
241 virtual void reset() = 0;
242
243 private:
244 bool m_xof_started = false;
245};
246
247} // namespace Botan
248
249#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
void clear()
Definition xof.h:66
virtual std::unique_ptr< XOF > new_object() const =0
static std::unique_ptr< XOF > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition xof.cpp:54
T output(size_t bytes)
Definition xof.h:161
void output(std::span< uint8_t > output)
Definition xof.h:191
virtual std::string provider() const
Definition xof.cpp:66
std::array< uint8_t, count > output()
Definition xof.h:172
uint8_t output_next_byte()
Definition xof.h:197
virtual bool valid_salt_length(size_t salt_len) const
Definition xof.h:93
std::vector< uint8_t > output_stdvec(size_t bytes)
Definition xof.h:184
virtual Key_Length_Specification key_spec() const
Definition xof.h:102
static std::unique_ptr< XOF > create(std::string_view algo_spec, std::string_view provider="")
Definition xof.cpp:28
virtual ~XOF()=default
virtual size_t block_size() const =0
virtual std::string name() const =0
void start(std::span< const uint8_t > salt={}, std::span< const uint8_t > key={})
Definition xof.cpp:70
virtual std::unique_ptr< XOF > copy_state() const =0
void update(std::span< const uint8_t > input)
Definition xof.h:147
static std::vector< std::string > providers(std::string_view algo_spec)
Definition xof.cpp:62
virtual bool accepts_input() const =0