Botan 3.0.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 <string_view>
14#include <span>
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*/
23 {
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)
42 {
43 add_data(in.data(), in.size());
44 }
45
46 void update_be(uint16_t val);
47 void update_be(uint32_t val);
48 void update_be(uint64_t val);
49
50 void update_le(uint16_t val);
51 void update_le(uint32_t val);
52 void update_le(uint64_t val);
53
54 /**
55 * Add new input to process.
56 * @param str the input to process as a std::string_view. Will be interpreted
57 * as a byte array based on the strings encoding.
58 */
59 void update(std::string_view str)
60 {
61 add_data(cast_char_ptr_to_uint8(str.data()), str.size());
62 }
63
64 /**
65 * Process a single byte.
66 * @param in the byte to process
67 */
68 void update(uint8_t in) { add_data(&in, 1); }
69
70 /**
71 * Complete the computation and retrieve the
72 * final result.
73 * @param out The byte array to be filled with the result.
74 * Must be of length output_length()
75 */
76 void final(uint8_t out[]) { final_result(out); }
77
78 /**
79 * Complete the computation and retrieve the
80 * final result as a container of your choice.
81 * @return a contiguous container holding the result
82 */
83 template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
84 T final()
85 {
86 T output(output_length());
87 final_result(output.data());
88 return output;
89 }
90
91 std::vector<uint8_t> final_stdvec()
92 {
93 return final<std::vector<uint8_t>>();
94 }
95
96 void final(std::span<uint8_t> out)
97 {
98 BOTAN_ASSERT_NOMSG(out.size() >= output_length());
99 final_result(out.data());
100 }
101
102 template<concepts::resizable_byte_buffer T>
103 void final(T& out)
104 {
105 out.resize(output_length());
106 final_result(out.data());
107 }
108
109 /**
110 * Update and finalize computation. Does the same as calling update()
111 * and final() consecutively.
112 * @param in the input to process as a byte array
113 * @param length the length of the byte array
114 * @result the result of the call to final()
115 */
116 template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
117 T process(const uint8_t in[], size_t length)
118 {
119 update(in, length);
120 return final<T>();
121 }
122
123 /**
124 * Update and finalize computation. Does the same as calling update()
125 * and final() consecutively.
126 * @param in the input to process as a string
127 * @result the result of the call to final()
128 */
129 template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
130 T process(std::string_view in)
131 {
132 update(in);
133 return final<T>();
134 }
135
136 /**
137 * Update and finalize computation. Does the same as calling update()
138 * and final() consecutively.
139 * @param in the input to process as a contiguous container
140 * @result the result of the call to final()
141 */
142 template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
143 T process(std::span<const uint8_t> in)
144 {
145 update(in);
146 return final<T>();
147 }
148
149 virtual ~Buffered_Computation() = default;
150 private:
151 /**
152 * Add more data to the computation
153 * @param input is an input buffer
154 * @param length is the length of input in bytes
155 */
156 virtual void add_data(const uint8_t input[], size_t length) = 0;
157
158 /**
159 * Write the final output to out
160 * @param out is an output buffer of output_length()
161 */
162 virtual void final_result(uint8_t out[]) = 0;
163 };
164
165}
166
167#endif
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:67
T process(std::string_view in)
Definition: buf_comp.h:130
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:143
void update(std::span< const uint8_t > in)
Definition: buf_comp.h:41
void update(std::string_view str)
Definition: buf_comp.h:59
void update(uint8_t in)
Definition: buf_comp.h:68
virtual size_t output_length() const =0
std::vector< uint8_t > final_stdvec()
Definition: buf_comp.h:91
virtual ~Buffered_Computation()=default
T process(const uint8_t in[], size_t length)
Definition: buf_comp.h:117
int(* update)(CTX *, const void *, CC_LONG len)
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
FE_25519 T
Definition: ge.cpp:36
Definition: alg_id.cpp:12
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:183