Botan 3.5.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/mem_ops.h>
13#include <botan/secmem.h>
14#include <span>
15#include <string_view>
16
17namespace Botan {
18
19/**
20* This class represents any kind of computation which uses an internal
21* state, such as hash functions or MACs
22*/
24 public:
25 /**
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 void update_be(uint16_t val);
44 void update_be(uint32_t val);
45 void update_be(uint64_t val);
46
47 void update_le(uint16_t val);
48 void update_le(uint32_t val);
49 void update_le(uint64_t val);
50
51 /**
52 * Add new input to process.
53 * @param str the input to process as a std::string_view. Will be interpreted
54 * as a byte array based on the strings encoding.
55 */
56 void update(std::string_view str) { add_data({cast_char_ptr_to_uint8(str.data()), str.size()}); }
57
58 /**
59 * Process a single byte.
60 * @param in the byte to process
61 */
62 void update(uint8_t in) { add_data({&in, 1}); }
63
64 /**
65 * Complete the computation and retrieve the
66 * final result.
67 * @param out The byte array to be filled with the result.
68 * Must be of length output_length()
69 */
70 void final(uint8_t out[]) { final_result({out, output_length()}); }
71
72 /**
73 * Complete the computation and retrieve the
74 * final result as a container of your choice.
75 * @return a contiguous container holding the result
76 */
77 template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
78 T final() {
79 T output(output_length());
80 final_result(output);
81 return output;
82 }
83
84 std::vector<uint8_t> final_stdvec() { return final<std::vector<uint8_t>>(); }
85
86 void final(std::span<uint8_t> out) {
87 BOTAN_ARG_CHECK(out.size() >= output_length(), "provided output buffer has insufficient capacity");
88 final_result(out);
89 }
90
91 template <concepts::resizable_byte_buffer T>
92 void final(T& out) {
93 out.resize(output_length());
94 final_result(out);
95 }
96
97 /**
98 * Update and finalize computation. Does the same as calling update()
99 * and final() consecutively.
100 * @param in the input to process as a byte array
101 * @param length the length of the byte array
102 * @result the result of the call to final()
103 */
104 template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
105 T process(const uint8_t in[], size_t length) {
106 update(in, length);
107 return final<T>();
108 }
109
110 /**
111 * Update and finalize computation. Does the same as calling update()
112 * and final() consecutively.
113 * @param in the input to process as a string
114 * @result the result of the call to final()
115 */
116 template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
117 T process(std::string_view in) {
118 update(in);
119 return final<T>();
120 }
121
122 /**
123 * Update and finalize computation. Does the same as calling update()
124 * and final() consecutively.
125 * @param in the input to process as a contiguous container
126 * @result the result of the call to final()
127 */
128 template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
129 T process(std::span<const uint8_t> in) {
130 update(in);
131 return final<T>();
132 }
133
134 virtual ~Buffered_Computation() = default;
135
136 private:
137 /**
138 * Add more data to the computation
139 * @param input is an input buffer
140 */
141 virtual void add_data(std::span<const uint8_t> input) = 0;
142
143 /**
144 * Write the final output to out
145 * @param out is an output buffer of output_length()
146 */
147 virtual void final_result(std::span<uint8_t> out) = 0;
148};
149
150} // namespace Botan
151
152#endif
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
T process(std::string_view in)
Definition buf_comp.h:117
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:129
void update(std::span< const uint8_t > in)
Definition buf_comp.h:41
void update(std::string_view str)
Definition buf_comp.h:56
void update(uint8_t in)
Definition buf_comp.h:62
virtual size_t output_length() const =0
std::vector< uint8_t > final_stdvec()
Definition buf_comp.h:84
virtual ~Buffered_Computation()=default
T process(const uint8_t in[], size_t length)
Definition buf_comp.h:105
int(* update)(CTX *, const void *, CC_LONG len)
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
FE_25519 T
Definition ge.cpp:34
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition mem_ops.h:273