/src/botan/src/lib/codec/base64/base64.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Base64 Encoding and Decoding |
3 | | * (C) 2010,2015,2020 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #include <botan/base64.h> |
9 | | #include <botan/internal/codec_base.h> |
10 | | #include <botan/exceptn.h> |
11 | | #include <botan/internal/rounding.h> |
12 | | #include <botan/internal/ct_utils.h> |
13 | | |
14 | | namespace Botan { |
15 | | |
16 | | namespace { |
17 | | |
18 | | class Base64 final |
19 | | { |
20 | | public: |
21 | | static inline std::string name() noexcept |
22 | 100 | { |
23 | 100 | return "base64"; |
24 | 100 | } |
25 | | |
26 | | static inline size_t encoding_bytes_in() noexcept |
27 | 0 | { |
28 | 0 | return m_encoding_bytes_in; |
29 | 0 | } |
30 | | static inline size_t encoding_bytes_out() noexcept |
31 | 0 | { |
32 | 0 | return m_encoding_bytes_out; |
33 | 0 | } |
34 | | |
35 | | static inline size_t decoding_bytes_in() noexcept |
36 | 6.11k | { |
37 | 6.11k | return m_encoding_bytes_out; |
38 | 6.11k | } |
39 | | static inline size_t decoding_bytes_out() noexcept |
40 | 6.11k | { |
41 | 6.11k | return m_encoding_bytes_in; |
42 | 6.11k | } |
43 | | |
44 | | static inline size_t bits_consumed() noexcept |
45 | 0 | { |
46 | 0 | return m_encoding_bits; |
47 | 0 | } |
48 | | static inline size_t remaining_bits_before_padding() noexcept |
49 | 0 | { |
50 | 0 | return m_remaining_bits_before_padding; |
51 | 0 | } |
52 | | |
53 | | static inline size_t encode_max_output(size_t input_length) |
54 | 0 | { |
55 | 0 | return (round_up(input_length, m_encoding_bytes_in) / m_encoding_bytes_in) * m_encoding_bytes_out; |
56 | 0 | } |
57 | | static inline size_t decode_max_output(size_t input_length) |
58 | 12.2k | { |
59 | 12.2k | return (round_up(input_length, m_encoding_bytes_out) * m_encoding_bytes_in) / m_encoding_bytes_out; |
60 | 12.2k | } |
61 | | |
62 | | static void encode(char out[8], const uint8_t in[5]) noexcept; |
63 | | |
64 | | static uint8_t lookup_binary_value(char input) noexcept; |
65 | | |
66 | | static bool check_bad_char(uint8_t bin, char input, bool ignore_ws); |
67 | | |
68 | | static void decode(uint8_t* out_ptr, const uint8_t decode_buf[4]) |
69 | 1.62M | { |
70 | 1.62M | out_ptr[0] = (decode_buf[0] << 2) | (decode_buf[1] >> 4); |
71 | 1.62M | out_ptr[1] = (decode_buf[1] << 4) | (decode_buf[2] >> 2); |
72 | 1.62M | out_ptr[2] = (decode_buf[2] << 6) | decode_buf[3]; |
73 | 1.62M | } |
74 | | |
75 | | static inline size_t bytes_to_remove(size_t final_truncate) |
76 | 6.00k | { |
77 | 6.00k | return final_truncate; |
78 | 6.00k | } |
79 | | |
80 | | private: |
81 | | static const size_t m_encoding_bits = 6; |
82 | | static const size_t m_remaining_bits_before_padding = 8; |
83 | | |
84 | | static const size_t m_encoding_bytes_in = 3; |
85 | | static const size_t m_encoding_bytes_out = 4; |
86 | | }; |
87 | | |
88 | | char lookup_base64_char(uint8_t x) |
89 | 0 | { |
90 | 0 | BOTAN_DEBUG_ASSERT(x < 64); |
91 | |
|
92 | 0 | const auto in_az = CT::Mask<uint8_t>::is_within_range(x, 26, 51); |
93 | 0 | const auto in_09 = CT::Mask<uint8_t>::is_within_range(x, 52, 61); |
94 | 0 | const auto eq_plus = CT::Mask<uint8_t>::is_equal(x, 62); |
95 | 0 | const auto eq_slash = CT::Mask<uint8_t>::is_equal(x, 63); |
96 | |
|
97 | 0 | const char c_AZ = 'A' + x; |
98 | 0 | const char c_az = 'a' + (x - 26); |
99 | 0 | const char c_09 = '0' + (x - 2*26); |
100 | 0 | const char c_plus = '+'; |
101 | 0 | const char c_slash = '/'; |
102 | |
|
103 | 0 | char ret = c_AZ; |
104 | 0 | ret = in_az.select(c_az, ret); |
105 | 0 | ret = in_09.select(c_09, ret); |
106 | 0 | ret = eq_plus.select(c_plus, ret); |
107 | 0 | ret = eq_slash.select(c_slash, ret); |
108 | |
|
109 | 0 | return ret; |
110 | 0 | } |
111 | | |
112 | | //static |
113 | | void Base64::encode(char out[8], const uint8_t in[5]) noexcept |
114 | 0 | { |
115 | 0 | const uint8_t b0 = (in[0] & 0xFC) >> 2; |
116 | 0 | const uint8_t b1 = ((in[0] & 0x03) << 4) | (in[1] >> 4); |
117 | 0 | const uint8_t b2 = ((in[1] & 0x0F) << 2) | (in[2] >> 6); |
118 | 0 | const uint8_t b3 = in[2] & 0x3F; |
119 | 0 | out[0] = lookup_base64_char(b0); |
120 | 0 | out[1] = lookup_base64_char(b1); |
121 | 0 | out[2] = lookup_base64_char(b2); |
122 | 0 | out[3] = lookup_base64_char(b3); |
123 | 0 | } |
124 | | |
125 | | //static |
126 | | uint8_t Base64::lookup_binary_value(char input) noexcept |
127 | 6.64M | { |
128 | 6.64M | const uint8_t c = static_cast<uint8_t>(input); |
129 | | |
130 | 6.64M | const auto is_alpha_upper = CT::Mask<uint8_t>::is_within_range(c, uint8_t('A'), uint8_t('Z')); |
131 | 6.64M | const auto is_alpha_lower = CT::Mask<uint8_t>::is_within_range(c, uint8_t('a'), uint8_t('z')); |
132 | 6.64M | const auto is_decimal = CT::Mask<uint8_t>::is_within_range(c, uint8_t('0'), uint8_t('9')); |
133 | | |
134 | 6.64M | const auto is_plus = CT::Mask<uint8_t>::is_equal(c, uint8_t('+')); |
135 | 6.64M | const auto is_slash = CT::Mask<uint8_t>::is_equal(c, uint8_t('/')); |
136 | 6.64M | const auto is_equal = CT::Mask<uint8_t>::is_equal(c, uint8_t('=')); |
137 | | |
138 | 6.64M | const auto is_whitespace = CT::Mask<uint8_t>::is_any_of(c, { |
139 | 6.64M | uint8_t(' '), uint8_t('\t'), uint8_t('\n'), uint8_t('\r') |
140 | 6.64M | }); |
141 | | |
142 | 6.64M | const uint8_t c_upper = c - uint8_t('A'); |
143 | 6.64M | const uint8_t c_lower = c - uint8_t('a') + 26; |
144 | 6.64M | const uint8_t c_decim = c - uint8_t('0') + 2*26; |
145 | | |
146 | 6.64M | uint8_t ret = 0xFF; // default value |
147 | | |
148 | 6.64M | ret = is_alpha_upper.select(c_upper, ret); |
149 | 6.64M | ret = is_alpha_lower.select(c_lower, ret); |
150 | 6.64M | ret = is_decimal.select(c_decim, ret); |
151 | 6.64M | ret = is_plus.select(62, ret); |
152 | 6.64M | ret = is_slash.select(63, ret); |
153 | 6.64M | ret = is_equal.select(0x81, ret); |
154 | 6.64M | ret = is_whitespace.select(0x80, ret); |
155 | | |
156 | 6.64M | return ret; |
157 | 6.64M | } |
158 | | |
159 | | //static |
160 | | bool Base64::check_bad_char(uint8_t bin, char input, bool ignore_ws) |
161 | 6.63M | { |
162 | 6.63M | if(bin <= 0x3F) |
163 | 6.50M | { |
164 | 6.50M | return true; |
165 | 6.50M | } |
166 | 130k | else if(!(bin == 0x81 || (bin == 0x80 && ignore_ws))) |
167 | 105 | { |
168 | 105 | std::string bad_char(1, input); |
169 | 105 | if(bad_char == "\t") |
170 | 0 | { bad_char = "\\t"; } |
171 | 105 | else if(bad_char == "\n") |
172 | 0 | { bad_char = "\\n"; } |
173 | 105 | else if(bad_char == "\r") |
174 | 0 | { bad_char = "\\r"; } |
175 | | |
176 | 105 | throw Invalid_Argument( |
177 | 105 | std::string("base64_decode: invalid base64 character '") + |
178 | 105 | bad_char + "'"); |
179 | 105 | } |
180 | 130k | return false; |
181 | 130k | } |
182 | | |
183 | | } |
184 | | |
185 | | size_t base64_encode(char out[], |
186 | | const uint8_t in[], |
187 | | size_t input_length, |
188 | | size_t& input_consumed, |
189 | | bool final_inputs) |
190 | 0 | { |
191 | 0 | return base_encode(Base64(), out, in, input_length, input_consumed, final_inputs); |
192 | 0 | } |
193 | | |
194 | | std::string base64_encode(const uint8_t input[], |
195 | | size_t input_length) |
196 | 0 | { |
197 | 0 | return base_encode_to_string(Base64(), input, input_length); |
198 | 0 | } |
199 | | |
200 | | size_t base64_decode(uint8_t out[], |
201 | | const char in[], |
202 | | size_t input_length, |
203 | | size_t& input_consumed, |
204 | | bool final_inputs, |
205 | | bool ignore_ws) |
206 | 0 | { |
207 | 0 | return base_decode(Base64(), out, in, input_length, input_consumed, final_inputs, ignore_ws); |
208 | 0 | } |
209 | | |
210 | | size_t base64_decode(uint8_t output[], |
211 | | const char input[], |
212 | | size_t input_length, |
213 | | bool ignore_ws) |
214 | 0 | { |
215 | 0 | return base_decode_full(Base64(), output, input, input_length, ignore_ws); |
216 | 0 | } |
217 | | |
218 | | size_t base64_decode(uint8_t output[], |
219 | | const std::string& input, |
220 | | bool ignore_ws) |
221 | 0 | { |
222 | 0 | return base64_decode(output, input.data(), input.length(), ignore_ws); |
223 | 0 | } |
224 | | |
225 | | secure_vector<uint8_t> base64_decode(const char input[], |
226 | | size_t input_length, |
227 | | bool ignore_ws) |
228 | 6.11k | { |
229 | 6.11k | return base_decode_to_vec<secure_vector<uint8_t>>(Base64(), input, input_length, ignore_ws); |
230 | 6.11k | } |
231 | | |
232 | | secure_vector<uint8_t> base64_decode(const std::string& input, |
233 | | bool ignore_ws) |
234 | 0 | { |
235 | 0 | return base64_decode(input.data(), input.size(), ignore_ws); |
236 | 0 | } |
237 | | |
238 | | size_t base64_encode_max_output(size_t input_length) |
239 | 0 | { |
240 | 0 | return Base64::encode_max_output(input_length); |
241 | 0 | } |
242 | | |
243 | | size_t base64_decode_max_output(size_t input_length) |
244 | 0 | { |
245 | 0 | return Base64::decode_max_output(input_length); |
246 | 0 | } |
247 | | |
248 | | } |