Botan  2.11.0
Crypto and TLS for C++11
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 */
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  */
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  uint8_t inb[sizeof(T)];
61  store_be(in, inb);
62  add_data(inb, sizeof(inb));
63  }
64 
65  /**
66  * Add an integer in little-endian order
67  * @param in the value
68  */
69  template<typename T> void update_le(const T in)
70  {
71  uint8_t inb[sizeof(T)];
72  store_le(in, inb);
73  add_data(inb, sizeof(inb));
74  }
75 
76  /**
77  * Add new input to process.
78  * @param str the input to process as a std::string. Will be interpreted
79  * as a byte array based on the strings encoding.
80  */
81  void update(const std::string& str)
82  {
83  add_data(cast_char_ptr_to_uint8(str.data()), str.size());
84  }
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
94  * final result.
95  * @param out The byte array to be filled with the result.
96  * Must be of length output_length()
97  */
98  void final(uint8_t out[]) { final_result(out); }
99 
100  /**
101  * Complete the computation and retrieve the
102  * final result.
103  * @return secure_vector holding the result
104  */
106  {
107  secure_vector<uint8_t> output(output_length());
108  final_result(output.data());
109  return output;
110  }
111 
112  std::vector<uint8_t> final_stdvec()
113  {
114  std::vector<uint8_t> output(output_length());
115  final_result(output.data());
116  return output;
117  }
118 
119  template<typename Alloc>
120  void final(std::vector<uint8_t, Alloc>& out)
121  {
122  out.resize(output_length());
123  final_result(out.data());
124  }
125 
126  /**
127  * Update and finalize computation. Does the same as calling update()
128  * and final() consecutively.
129  * @param in the input to process as a byte array
130  * @param length the length of the byte array
131  * @result the result of the call to final()
132  */
133  secure_vector<uint8_t> process(const uint8_t in[], size_t length)
134  {
135  add_data(in, length);
136  return final();
137  }
138 
139  /**
140  * Update and finalize computation. Does the same as calling update()
141  * and final() consecutively.
142  * @param in the input to process
143  * @result the result of the call to final()
144  */
146  {
147  add_data(in.data(), in.size());
148  return final();
149  }
150 
151  /**
152  * Update and finalize computation. Does the same as calling update()
153  * and final() consecutively.
154  * @param in the input to process
155  * @result the result of the call to final()
156  */
157  secure_vector<uint8_t> process(const std::vector<uint8_t>& in)
158  {
159  add_data(in.data(), in.size());
160  return final();
161  }
162 
163  /**
164  * Update and finalize computation. Does the same as calling update()
165  * and final() consecutively.
166  * @param in the input to process as a string
167  * @result the result of the call to final()
168  */
169  secure_vector<uint8_t> process(const std::string& in)
170  {
171  update(in);
172  return final();
173  }
174 
175  virtual ~Buffered_Computation() = default;
176  private:
177  /**
178  * Add more data to the computation
179  * @param input is an input buffer
180  * @param length is the length of input in bytes
181  */
182  virtual void add_data(const uint8_t input[], size_t length) = 0;
183 
184  /**
185  * Write the final output to out
186  * @param out is an output buffer of output_length()
187  */
188  virtual void final_result(uint8_t out[]) = 0;
189  };
190 
191 }
192 
193 #endif
void update_le(const T in)
Definition: buf_comp.h:69
void update(const std::vector< uint8_t > &in)
Definition: buf_comp.h:49
secure_vector< uint8_t > process(const std::string &in)
Definition: buf_comp.h:169
void store_be(uint16_t in, uint8_t out[2])
Definition: loadstor.h:436
secure_vector< uint8_t > process(const std::vector< uint8_t > &in)
Definition: buf_comp.h:157
void update(const std::string &str)
Definition: buf_comp.h:81
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:159
secure_vector< uint8_t > process(const secure_vector< uint8_t > &in)
Definition: buf_comp.h:145
secure_vector< uint8_t > process(const uint8_t in[], size_t length)
Definition: buf_comp.h:133
Definition: alg_id.cpp:13
std::vector< uint8_t > final_stdvec()
Definition: buf_comp.h:112
void update(const secure_vector< uint8_t > &in)
Definition: buf_comp.h:40
void update(uint8_t in)
Definition: buf_comp.h:90
int(* update)(CTX *, const void *, CC_LONG len)
void update(const uint8_t in[], size_t length)
Definition: buf_comp.h:34
fe T
Definition: ge.cpp:37
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
void update_be(const T in)
Definition: buf_comp.h:58
void store_le(uint16_t in, uint8_t out[2])
Definition: loadstor.h:452