Coverage Report

Created: 2026-12-01 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Botan-3.13.0/build/include/public/botan/xof.h
Line
Count
Source
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
20
namespace 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
 */
29
class BOTAN_PUBLIC_API(3, 2) XOF /* NOLINT(*special-member-functions) */ {
30
   public:
31
189k
      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
4.10k
      void clear() {
67
4.10k
         m_xof_started = false;
68
4.10k
         reset();
69
4.10k
      }
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
189k
      virtual bool valid_salt_length(size_t salt_len) const {
94
         // Salts are not supported by default
95
189k
         return salt_len == 0;
96
189k
      }
97
98
      /**
99
       * Return the key lengths supported by this XOF
100
       * @returns an object describing limits on the key size
101
       */
102
189k
      virtual Key_Length_Specification key_spec() const {
103
         // Keys are not supported by default
104
189k
         return Key_Length_Specification(0);
105
189k
      }
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
383k
      void update(std::span<const uint8_t> input) {
148
383k
         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
189k
            start();
152
189k
         }
153
383k
         add_data(input);
154
383k
      }
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
11.4k
      T output(size_t bytes) {
162
11.4k
         T out(bytes);
163
11.4k
         generate_bytes(out);
164
11.4k
         return out;
165
11.4k
      }
Unexecuted instantiation: _ZN5Botan3XOF6outputITkNS_8concepts21resizable_byte_bufferENSt3__16vectorIhNS3_9allocatorIhEEEEEET_m
_ZN5Botan3XOF6outputITkNS_8concepts21resizable_byte_bufferENS_6StrongINSt3__16vectorIhNS4_9allocatorIhEEEENS_25DilithiumHashedPublicKey_EJEEEEET_m
Line
Count
Source
161
4.10k
      T output(size_t bytes) {
162
4.10k
         T out(bytes);
163
4.10k
         generate_bytes(out);
164
4.10k
         return out;
165
4.10k
      }
_ZN5Botan3XOF6outputITkNS_8concepts21resizable_byte_bufferENS_6StrongINSt3__16vectorIhNS4_9allocatorIhEEEENS_31DilithiumMessageRepresentative_EJEEEEET_m
Line
Count
Source
161
4.10k
      T output(size_t bytes) {
162
4.10k
         T out(bytes);
163
4.10k
         generate_bytes(out);
164
4.10k
         return out;
165
4.10k
      }
_ZN5Botan3XOF6outputITkNS_8concepts21resizable_byte_bufferENS_6StrongINSt3__16vectorIhNS4_9allocatorIhEEEENS_24DilithiumCommitmentHash_EJEEEEET_m
Line
Count
Source
161
3.29k
      T output(size_t bytes) {
162
3.29k
         T out(bytes);
163
3.29k
         generate_bytes(out);
164
3.29k
         return out;
165
3.29k
      }
Unexecuted instantiation: _ZN5Botan3XOF6outputITkNS_8concepts21resizable_byte_bufferENS_6StrongINSt3__16vectorIhNS4_9allocatorIhEEEENS_20DilithiumPublicSeed_EJEEEEET_m
Unexecuted instantiation: _ZN5Botan3XOF6outputITkNS_8concepts21resizable_byte_bufferENS_6StrongINSt3__16vectorIhNS_16secure_allocatorIhEEEENS_22DilithiumSeedRhoPrime_EJEEEEET_m
Unexecuted instantiation: _ZN5Botan3XOF6outputITkNS_8concepts21resizable_byte_bufferENS_6StrongINSt3__16vectorIhNS_16secure_allocatorIhEEEENS_15DilithiumSeedK_EJEEEEET_m
Unexecuted instantiation: _ZN5Botan3XOF6outputITkNS_8concepts21resizable_byte_bufferENS_6StrongINSt3__16vectorIhNS_16secure_allocatorIhEEEENS_24KyberSamplingRandomness_EJEEEEET_m
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
44.5M
      std::array<uint8_t, count> output() {
173
44.5M
         std::array<uint8_t, count> out;  // NOLINT(*-member-init)
174
44.5M
         generate_bytes(out);
175
44.5M
         return out;
176
44.5M
      }
std::__1::array<unsigned char, 8ul> Botan::XOF::output<8ul>()
Line
Count
Source
172
3.29k
      std::array<uint8_t, count> output() {
173
3.29k
         std::array<uint8_t, count> out;  // NOLINT(*-member-init)
174
3.29k
         generate_bytes(out);
175
3.29k
         return out;
176
3.29k
      }
std::__1::array<unsigned char, 1ul> Botan::XOF::output<1ul>()
Line
Count
Source
172
207k
      std::array<uint8_t, count> output() {
173
207k
         std::array<uint8_t, count> out;  // NOLINT(*-member-init)
174
207k
         generate_bytes(out);
175
207k
         return out;
176
207k
      }
std::__1::array<unsigned char, 3ul> Botan::XOF::output<3ul>()
Line
Count
Source
172
44.3M
      std::array<uint8_t, count> output() {
173
44.3M
         std::array<uint8_t, count> out;  // NOLINT(*-member-init)
174
44.3M
         generate_bytes(out);
175
44.3M
         return out;
176
44.3M
      }
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
0
      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
1.80k
      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
       */
197
0
      uint8_t output_next_byte() {
198
0
         uint8_t out = 0;
199
0
         generate_bytes({&out, 1});
200
0
         return out;
201
0
      }
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