/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/concepts.h> |
12 | | #include <botan/secmem.h> |
13 | | #include <span> |
14 | | #include <string_view> |
15 | | |
16 | | namespace Botan { |
17 | | |
18 | | /** |
19 | | * This class represents any kind of computation which uses an internal |
20 | | * state, such as hash functions or MACs |
21 | | */ |
22 | | class BOTAN_PUBLIC_API(2, 0) Buffered_Computation { |
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 | 3 | 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 contiguous data range |
39 | | */ |
40 | 7 | void update(std::span<const uint8_t> in) { add_data(in.data(), in.size()); } |
41 | | |
42 | | void update_be(uint16_t val); |
43 | | void update_be(uint32_t val); |
44 | | void update_be(uint64_t val); |
45 | | |
46 | | void update_le(uint16_t val); |
47 | | void update_le(uint32_t val); |
48 | | void update_le(uint64_t val); |
49 | | |
50 | | /** |
51 | | * Add new input to process. |
52 | | * @param str the input to process as a std::string_view. Will be interpreted |
53 | | * as a byte array based on the strings encoding. |
54 | | */ |
55 | 0 | void update(std::string_view str) { add_data(cast_char_ptr_to_uint8(str.data()), str.size()); } |
56 | | |
57 | | /** |
58 | | * Process a single byte. |
59 | | * @param in the byte to process |
60 | | */ |
61 | 0 | void update(uint8_t in) { add_data(&in, 1); } |
62 | | |
63 | | /** |
64 | | * Complete the computation and retrieve the |
65 | | * final result. |
66 | | * @param out The byte array to be filled with the result. |
67 | | * Must be of length output_length() |
68 | | */ |
69 | 4 | void final(uint8_t out[]) { final_result(out); } |
70 | | |
71 | | /** |
72 | | * Complete the computation and retrieve the |
73 | | * final result as a container of your choice. |
74 | | * @return a contiguous container holding the result |
75 | | */ |
76 | | template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>> |
77 | 2 | T final() { |
78 | 2 | T output(output_length()); |
79 | 2 | final_result(output.data()); |
80 | 2 | return output; |
81 | 2 | } Unexecuted instantiation: std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > Botan::Buffered_Computation::final<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >() std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > Botan::Buffered_Computation::final<std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > >() Line | Count | Source | 77 | 2 | T final() { | 78 | 2 | T output(output_length()); | 79 | 2 | final_result(output.data()); | 80 | 2 | return output; | 81 | 2 | } |
|
82 | | |
83 | 0 | std::vector<uint8_t> final_stdvec() { return final<std::vector<uint8_t>>(); } |
84 | | |
85 | 0 | void final(std::span<uint8_t> out) { |
86 | 0 | BOTAN_ASSERT_NOMSG(out.size() >= output_length()); |
87 | 0 | final_result(out.data()); |
88 | 0 | } |
89 | | |
90 | | template <concepts::resizable_byte_buffer T> |
91 | | void final(T& out) { |
92 | | out.resize(output_length()); |
93 | | final_result(out.data()); |
94 | | } |
95 | | |
96 | | /** |
97 | | * Update and finalize computation. Does the same as calling update() |
98 | | * and final() consecutively. |
99 | | * @param in the input to process as a byte array |
100 | | * @param length the length of the byte array |
101 | | * @result the result of the call to final() |
102 | | */ |
103 | | template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>> |
104 | | T process(const uint8_t in[], size_t length) { |
105 | | update(in, length); |
106 | | return final<T>(); |
107 | | } |
108 | | |
109 | | /** |
110 | | * Update and finalize computation. Does the same as calling update() |
111 | | * and final() consecutively. |
112 | | * @param in the input to process as a string |
113 | | * @result the result of the call to final() |
114 | | */ |
115 | | template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>> |
116 | | T process(std::string_view in) { |
117 | | update(in); |
118 | | return final<T>(); |
119 | | } |
120 | | |
121 | | /** |
122 | | * Update and finalize computation. Does the same as calling update() |
123 | | * and final() consecutively. |
124 | | * @param in the input to process as a contiguous container |
125 | | * @result the result of the call to final() |
126 | | */ |
127 | | template <concepts::resizable_byte_buffer T = secure_vector<uint8_t>> |
128 | | T process(std::span<const uint8_t> in) { |
129 | | update(in); |
130 | | return final<T>(); |
131 | | } |
132 | | |
133 | 2 | virtual ~Buffered_Computation() = default; |
134 | | |
135 | | private: |
136 | | /** |
137 | | * Add more data to the computation |
138 | | * @param input is an input buffer |
139 | | * @param length is the length of input in bytes |
140 | | */ |
141 | | virtual void add_data(const uint8_t input[], size_t length) = 0; |
142 | | |
143 | | /** |
144 | | * Write the final output to out |
145 | | * @param out is an output buffer of output_length() |
146 | | */ |
147 | | virtual void final_result(uint8_t out[]) = 0; |
148 | | }; |
149 | | |
150 | | } // namespace Botan |
151 | | |
152 | | #endif |