Botan  1.10.9
buf_comp.h
Go to the documentation of this file.
1 /*
2 * Buffered Computation
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #ifndef BOTAN_BUFFERED_COMPUTATION_H__
9 #define BOTAN_BUFFERED_COMPUTATION_H__
10 
11 #include <botan/secmem.h>
12 #include <botan/get_byte.h>
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_DLL 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  void update(const byte 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 MemoryRegion
38  */
39  void update(const MemoryRegion<byte>& in)
40  {
41  add_data(&in[0], in.size());
42  }
43 
44  /**
45  * Add an integer in big-endian order
46  * @param in the value
47  */
48  template<typename T> void update_be(const T in)
49  {
50  for(size_t i = 0; i != sizeof(T); ++i)
51  {
52  byte b = get_byte(i, in);
53  add_data(&b, 1);
54  }
55  }
56 
57  /**
58  * Add new input to process.
59  * @param str the input to process as a std::string. Will be interpreted
60  * as a byte array based on
61  * the strings encoding.
62  */
63  void update(const std::string& str)
64  {
65  add_data(reinterpret_cast<const byte*>(str.data()), str.size());
66  }
67 
68  /**
69  * Process a single byte.
70  * @param in the byte to process
71  */
72  void update(byte in) { add_data(&in, 1); }
73 
74  /**
75  * Complete the computation and retrieve the
76  * final result.
77  * @param out The byte array to be filled with the result.
78  * Must be of length output_length()
79  */
80  void final(byte out[]) { final_result(out); }
81 
82  /**
83  * Complete the computation and retrieve the
84  * final result.
85  * @return SecureVector holding the result
86  */
88  {
89  SecureVector<byte> output(output_length());
90  final_result(&output[0]);
91  return output;
92  }
93 
94  /**
95  * Update and finalize computation. Does the same as calling update()
96  * and final() consecutively.
97  * @param in the input to process as a byte array
98  * @param length the length of the byte array
99  * @result the result of the call to final()
100  */
101  SecureVector<byte> process(const byte in[], size_t length)
102  {
103  add_data(in, length);
104  return final();
105  }
106 
107  /**
108  * Update and finalize computation. Does the same as calling update()
109  * and final() consecutively.
110  * @param in the input to process
111  * @result the result of the call to final()
112  */
114  {
115  add_data(&in[0], in.size());
116  return final();
117  }
118 
119  /**
120  * Update and finalize computation. Does the same as calling update()
121  * and final() consecutively.
122  * @param in the input to process as a string
123  * @result the result of the call to final()
124  */
125  SecureVector<byte> process(const std::string& in)
126  {
127  update(in);
128  return final();
129  }
130 
132  private:
133  /**
134  * Add more data to the computation
135  * @param input is an input buffer
136  * @param length is the length of input in bytes
137  */
138  virtual void add_data(const byte input[], size_t length) = 0;
139 
140  /**
141  * Write the final output to out
142  * @param out is an output buffer of output_length()
143  */
144  virtual void final_result(byte out[]) = 0;
145  };
146 
147 }
148 
149 #endif
void update(const std::string &str)
Definition: buf_comp.h:63
void update(const MemoryRegion< byte > &in)
Definition: buf_comp.h:39
byte get_byte(size_t byte_num, T input)
Definition: get_byte.h:21
unsigned char byte
Definition: types.h:22
SecureVector< byte > process(const std::string &in)
Definition: buf_comp.h:125
void update(const byte in[], size_t length)
Definition: buf_comp.h:33
SecureVector< byte > process(const byte in[], size_t length)
Definition: buf_comp.h:101
size_t size() const
Definition: secmem.h:29
virtual ~Buffered_Computation()
Definition: buf_comp.h:131
SecureVector< byte > process(const MemoryRegion< byte > &in)
Definition: buf_comp.h:113
void update_be(const T in)
Definition: buf_comp.h:48
void update(byte in)
Definition: buf_comp.h:72