Coverage Report

Created: 2020-05-23 13:54

/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
433k
      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
369k
         {
41
369k
         add_data(in.data(), in.size());
42
369k
         }
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
92.8k
         {
50
92.8k
         add_data(in.data(), in.size());
51
92.8k
         }
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
7.00k
         {
68
7.00k
         add_data(cast_char_ptr_to_uint8(str.data()), str.size());
69
7.00k
         }
70
71
      /**
72
      * Process a single byte.
73
      * @param in the byte to process
74
      */
75
80
      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
285k
      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
93.0k
         {
92
93.0k
         secure_vector<uint8_t> output(output_length());
93
93.0k
         final_result(output.data());
94
93.0k
         return output;
95
93.0k
         }
96
97
      std::vector<uint8_t> final_stdvec()
98
30.7k
         {
99
30.7k
         std::vector<uint8_t> output(output_length());
100
30.7k
         final_result(output.data());
101
30.7k
         return output;
102
30.7k
         }
103
104
      template<typename Alloc>
105
         void final(std::vector<uint8_t, Alloc>& out)
106
113k
         {
107
113k
         out.resize(output_length());
108
113k
         final_result(out.data());
109
113k
         }
void Botan::Buffered_Computation::final<std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&)
Line
Count
Source
106
50.7k
         {
107
50.7k
         out.resize(output_length());
108
50.7k
         final_result(out.data());
109
50.7k
         }
void Botan::Buffered_Computation::final<Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&)
Line
Count
Source
106
63.0k
         {
107
63.0k
         out.resize(output_length());
108
63.0k
         final_result(out.data());
109
63.0k
         }
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
20.4k
         {
120
20.4k
         add_data(in, length);
121
20.4k
         return final();
122
20.4k
         }
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
63.0k
         {
132
63.0k
         add_data(in.data(), in.size());
133
63.0k
         return final();
134
63.0k
         }
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
149k
      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