/src/botan/build/include/botan/buf_comp.h
Line | Count | Source (jump to first uncovered line) |
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/secmem.h> |
12 | | #include <string> |
13 | | |
14 | | namespace Botan { |
15 | | |
16 | | /** |
17 | | * This class represents any kind of computation which uses an internal |
18 | | * state, such as hash functions or MACs |
19 | | */ |
20 | | class BOTAN_PUBLIC_API(2,0) Buffered_Computation |
21 | | { |
22 | | public: |
23 | | /** |
24 | | * @return length of the output of this function in bytes |
25 | | */ |
26 | | virtual size_t output_length() const = 0; |
27 | | |
28 | | /** |
29 | | * Add new input to process. |
30 | | * @param in the input to process as a byte array |
31 | | * @param length of param in in bytes |
32 | | */ |
33 | 521k | void update(const uint8_t in[], size_t length) { add_data(in, length); } |
34 | | |
35 | | /** |
36 | | * Add new input to process. |
37 | | * @param in the input to process as a secure_vector |
38 | | */ |
39 | | void update(const secure_vector<uint8_t>& in) |
40 | 443k | { |
41 | 443k | add_data(in.data(), in.size()); |
42 | 443k | } |
43 | | |
44 | | /** |
45 | | * Add new input to process. |
46 | | * @param in the input to process as a std::vector |
47 | | */ |
48 | | void update(const std::vector<uint8_t>& in) |
49 | 106k | { |
50 | 106k | add_data(in.data(), in.size()); |
51 | 106k | } |
52 | | |
53 | | void update_be(uint16_t val); |
54 | | void update_be(uint32_t val); |
55 | | void update_be(uint64_t val); |
56 | | |
57 | | void update_le(uint16_t val); |
58 | | void update_le(uint32_t val); |
59 | | void update_le(uint64_t val); |
60 | | |
61 | | /** |
62 | | * Add new input to process. |
63 | | * @param str the input to process as a std::string. Will be interpreted |
64 | | * as a byte array based on the strings encoding. |
65 | | */ |
66 | | void update(const std::string& str) |
67 | 6.99k | { |
68 | 6.99k | add_data(cast_char_ptr_to_uint8(str.data()), str.size()); |
69 | 6.99k | } |
70 | | |
71 | | /** |
72 | | * Process a single byte. |
73 | | * @param in the byte to process |
74 | | */ |
75 | 88 | void update(uint8_t in) { add_data(&in, 1); } |
76 | | |
77 | | /** |
78 | | * Complete the computation and retrieve the |
79 | | * final result. |
80 | | * @param out The byte array to be filled with the result. |
81 | | * Must be of length output_length() |
82 | | */ |
83 | 344k | void final(uint8_t out[]) { final_result(out); } |
84 | | |
85 | | /** |
86 | | * Complete the computation and retrieve the |
87 | | * final result. |
88 | | * @return secure_vector holding the result |
89 | | */ |
90 | | secure_vector<uint8_t> final() |
91 | 113k | { |
92 | 113k | secure_vector<uint8_t> output(output_length()); |
93 | 113k | final_result(output.data()); |
94 | 113k | return output; |
95 | 113k | } |
96 | | |
97 | | std::vector<uint8_t> final_stdvec() |
98 | 34.3k | { |
99 | 34.3k | std::vector<uint8_t> output(output_length()); |
100 | 34.3k | final_result(output.data()); |
101 | 34.3k | return output; |
102 | 34.3k | } |
103 | | |
104 | | template<typename Alloc> |
105 | | void final(std::vector<uint8_t, Alloc>& out) |
106 | 134k | { |
107 | 134k | out.resize(output_length()); |
108 | 134k | final_result(out.data()); |
109 | 134k | } void Botan::Buffered_Computation::final<Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&) Line | Count | Source | 106 | 76.9k | { | 107 | 76.9k | out.resize(output_length()); | 108 | 76.9k | final_result(out.data()); | 109 | 76.9k | } |
void Botan::Buffered_Computation::final<std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&) Line | Count | Source | 106 | 57.3k | { | 107 | 57.3k | out.resize(output_length()); | 108 | 57.3k | final_result(out.data()); | 109 | 57.3k | } |
|
110 | | |
111 | | /** |
112 | | * Update and finalize computation. Does the same as calling update() |
113 | | * and final() consecutively. |
114 | | * @param in the input to process as a byte array |
115 | | * @param length the length of the byte array |
116 | | * @result the result of the call to final() |
117 | | */ |
118 | | secure_vector<uint8_t> process(const uint8_t in[], size_t length) |
119 | 22.8k | { |
120 | 22.8k | add_data(in, length); |
121 | 22.8k | return final(); |
122 | 22.8k | } |
123 | | |
124 | | /** |
125 | | * Update and finalize computation. Does the same as calling update() |
126 | | * and final() consecutively. |
127 | | * @param in the input to process |
128 | | * @result the result of the call to final() |
129 | | */ |
130 | | secure_vector<uint8_t> process(const secure_vector<uint8_t>& in) |
131 | 76.9k | { |
132 | 76.9k | add_data(in.data(), in.size()); |
133 | 76.9k | return final(); |
134 | 76.9k | } |
135 | | |
136 | | /** |
137 | | * Update and finalize computation. Does the same as calling update() |
138 | | * and final() consecutively. |
139 | | * @param in the input to process |
140 | | * @result the result of the call to final() |
141 | | */ |
142 | | secure_vector<uint8_t> process(const std::vector<uint8_t>& in) |
143 | 0 | { |
144 | 0 | add_data(in.data(), in.size()); |
145 | 0 | return final(); |
146 | 0 | } |
147 | | |
148 | | /** |
149 | | * Update and finalize computation. Does the same as calling update() |
150 | | * and final() consecutively. |
151 | | * @param in the input to process as a string |
152 | | * @result the result of the call to final() |
153 | | */ |
154 | | secure_vector<uint8_t> process(const std::string& in) |
155 | 0 | { |
156 | 0 | update(in); |
157 | 0 | return final(); |
158 | 0 | } |
159 | | |
160 | 169k | virtual ~Buffered_Computation() = default; |
161 | | private: |
162 | | /** |
163 | | * Add more data to the computation |
164 | | * @param input is an input buffer |
165 | | * @param length is the length of input in bytes |
166 | | */ |
167 | | virtual void add_data(const uint8_t input[], size_t length) = 0; |
168 | | |
169 | | /** |
170 | | * Write the final output to out |
171 | | * @param out is an output buffer of output_length() |
172 | | */ |
173 | | virtual void final_result(uint8_t out[]) = 0; |
174 | | }; |
175 | | |
176 | | } |
177 | | |
178 | | #endif |