Botan 3.13.0
Crypto and TLS for C&
buf_comp.h
Go to the documentation of this file.
1/*
2* Buffered Computation
3* (C) 1999-2007 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_BUFFERED_COMPUTATION_H_
9#define BOTAN_BUFFERED_COMPUTATION_H_
10
11#include <botan/concepts.h>
12#include <botan/secmem.h>
13#include <span>
14#include <string_view>
15
16namespace Botan {
17
18/**
19* This class represents any kind of computation which uses an internal
20* state, such as hash functions or MACs
21*/
22class BOTAN_PUBLIC_API(2, 0) Buffered_Computation /* NOLINT(*special-member-functions) */ {
23 public:
24 /**
25 * Return the output length of this function
26 * @return length of the output of this function in bytes
27 */
28 virtual size_t output_length() const = 0;
29
30 /**
31 * Add new input to process.
32 * @param in the input to process as a byte array
33 * @param length of param in in bytes
34 */
35 void update(const uint8_t in[], size_t length) { add_data({in, length}); }
36
37 /**
38 * Add new input to process.
39 * @param in the input to process as a contiguous data range
40 */
41 void update(std::span<const uint8_t> in) { add_data(in); }
42
43 /**
44 * Add new input to process, encoded as a big-endian integer
45 * @param val the value to process
46 */
47 void update_be(uint16_t val);
48
49 /**
50 * Add new input to process, encoded as a big-endian integer
51 * @param val the value to process
52 */
53 void update_be(uint32_t val);
54
55 /**
56 * Add new input to process, encoded as a big-endian integer
57 * @param val the value to process
58 */
59 void update_be(uint64_t val);
60
61 /**
62 * Add new input to process, encoded as a little-endian integer
63 * @param val the value to process
64 */
65 void update_le(uint16_t val);
66
67 /**
68 * Add new input to process, encoded as a little-endian integer
69 * @param val the value to process
70 */
71 void update_le(uint32_t val);
72
73 /**
74 * Add new input to process, encoded as a little-endian integer
75 * @param val the value to process
76 */
77 void update_le(uint64_t val);
78
79 /**
80 * Add new input to process.
81 * @param str the input to process as a std::string_view. Will be interpreted
82 * as a byte array based on the strings encoding.
83 */
84 void update(std::string_view str);
85
86 /**
87 * Process a single byte.
88 * @param in the byte to process
89 */
90 void update(uint8_t in) { add_data({&in, 1}); }
91
92 /**
93 * Complete the computation and retrieve the final result.
94 * @param out The byte array to be filled with the result, which
95 * must be of length output_length()
96 */
97 void final(uint8_t out[]) { final_result({out, output_length()}); }
98
99 /**
100 * Complete the computation and retrieve the final result as a container.
101 * @return a contiguous container holding the result
102 */
103 template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
104 T final() {
105 T output(output_length());
106 final_result(output);
107 return output;
108 }
109
110 /**
111 * Complete the computation and retrieve the final result
112 * @return a std::vector holding the result
113 */
114 std::vector<uint8_t> final_stdvec() { return final<std::vector<uint8_t>>(); }
115
116 /**
117 * Complete the computation and retrieve the final result
118 * @param out the buffer to write the result to, must be output_length() bytes
119 */
120 void final(std::span<uint8_t> out);
121
122 /**
123 * Complete the computation and retrieve the final result
124 * @param out a container which is resized to hold the result
125 */
126 template <concepts::resizable_byte_buffer T>
127 void final(T& out) {
128 out.resize(output_length());
129 final_result(out);
130 }
131
132 /**
133 * Update and finalize computation. Does the same as calling update()
134 * and final() consecutively.
135 * @param in the input to process as a byte array
136 * @param length the length of the byte array
137 * @result the result of the call to final()
138 */
139 template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
140 T process(const uint8_t in[], size_t length) {
141 update(in, length);
142 return final<T>();
143 }
144
145 /**
146 * Update and finalize computation. Does the same as calling update()
147 * and final() consecutively.
148 * @param in the input to process as a string
149 * @result the result of the call to final()
150 */
151 template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
152 T process(std::string_view in) {
153 update(in);
154 return final<T>();
155 }
156
157 /**
158 * Update and finalize computation. Does the same as calling update()
159 * and final() consecutively.
160 * @param in the input to process as a contiguous container
161 * @result the result of the call to final()
162 */
163 template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
164 T process(std::span<const uint8_t> in) {
165 update(in);
166 return final<T>();
167 }
168
169 virtual ~Buffered_Computation() = default;
170
171 private:
172 /**
173 * Add more data to the computation
174 * @param input is an input buffer
175 */
176 virtual void add_data(std::span<const uint8_t> input) = 0;
177
178 /**
179 * Write the final output to out
180 * @param out is an output buffer of output_length()
181 */
182 virtual void final_result(std::span<uint8_t> out) = 0;
183};
184
185} // namespace Botan
186
187#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
T process(std::string_view in)
Definition buf_comp.h:152
void update(const uint8_t in[], size_t length)
Definition buf_comp.h:35
T process(std::span< const uint8_t > in)
Definition buf_comp.h:164
void update(std::span< const uint8_t > in)
Definition buf_comp.h:41
void update(uint8_t in)
Definition buf_comp.h:90
virtual size_t output_length() const =0
std::vector< uint8_t > final_stdvec()
Definition buf_comp.h:114
void final(uint8_t out[])
Definition buf_comp.h:97
virtual ~Buffered_Computation()=default
T process(const uint8_t in[], size_t length)
Definition buf_comp.h:140