Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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