/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 | | |
10 | | #include <botan/exceptn.h> |
11 | | #include <botan/internal/charset.h> |
12 | | #include <botan/internal/codec_base.h> |
13 | | #include <botan/internal/ct_utils.h> |
14 | | #include <botan/internal/fmt.h> |
15 | | #include <botan/internal/rounding.h> |
16 | | |
17 | | namespace Botan { |
18 | | |
19 | | namespace { |
20 | | |
21 | | class Base64 final { |
22 | | public: |
23 | 0 | static inline std::string name() noexcept { return "base64"; } |
24 | | |
25 | 0 | static inline size_t encoding_bytes_in() noexcept { return m_encoding_bytes_in; } |
26 | | |
27 | 0 | static inline size_t encoding_bytes_out() noexcept { return m_encoding_bytes_out; } |
28 | | |
29 | 0 | static inline size_t decoding_bytes_in() noexcept { return m_encoding_bytes_out; } |
30 | | |
31 | 0 | static inline size_t decoding_bytes_out() noexcept { return m_encoding_bytes_in; } |
32 | | |
33 | 0 | static inline size_t bits_consumed() noexcept { return m_encoding_bits; } |
34 | | |
35 | 0 | static inline size_t remaining_bits_before_padding() noexcept { return m_remaining_bits_before_padding; } |
36 | | |
37 | 0 | static inline size_t encode_max_output(size_t input_length) { |
38 | 0 | return (round_up(input_length, m_encoding_bytes_in) / m_encoding_bytes_in) * m_encoding_bytes_out; |
39 | 0 | } |
40 | | |
41 | 0 | static inline size_t decode_max_output(size_t input_length) { |
42 | 0 | return (round_up(input_length, m_encoding_bytes_out) * m_encoding_bytes_in) / m_encoding_bytes_out; |
43 | 0 | } |
44 | | |
45 | | static void encode(char out[8], const uint8_t in[5]) noexcept; |
46 | | |
47 | | static uint8_t lookup_binary_value(char input) noexcept; |
48 | | |
49 | | static bool check_bad_char(uint8_t bin, char input, bool ignore_ws); |
50 | | |
51 | 0 | static void decode(uint8_t* out_ptr, const uint8_t decode_buf[4]) { |
52 | 0 | out_ptr[0] = (decode_buf[0] << 2) | (decode_buf[1] >> 4); |
53 | 0 | out_ptr[1] = (decode_buf[1] << 4) | (decode_buf[2] >> 2); |
54 | 0 | out_ptr[2] = (decode_buf[2] << 6) | decode_buf[3]; |
55 | 0 | } |
56 | | |
57 | 0 | static inline size_t bytes_to_remove(size_t final_truncate) { return final_truncate; } |
58 | | |
59 | | private: |
60 | | static const size_t m_encoding_bits = 6; |
61 | | static const size_t m_remaining_bits_before_padding = 8; |
62 | | |
63 | | static const size_t m_encoding_bytes_in = 3; |
64 | | static const size_t m_encoding_bytes_out = 4; |
65 | | }; |
66 | | |
67 | 0 | char lookup_base64_char(uint8_t x) { |
68 | 0 | BOTAN_DEBUG_ASSERT(x < 64); |
69 | |
|
70 | 0 | const auto in_AZ = CT::Mask<uint8_t>::is_lt(x, 26); |
71 | 0 | const auto in_09 = CT::Mask<uint8_t>::is_within_range(x, 52, 61); |
72 | 0 | const auto eq_plus = CT::Mask<uint8_t>::is_equal(x, 62); |
73 | 0 | const auto eq_slash = CT::Mask<uint8_t>::is_equal(x, 63); |
74 | |
|
75 | 0 | const char c_AZ = 'A' + x; |
76 | 0 | const char c_az = 'a' + (x - 26); |
77 | 0 | const char c_09 = '0' + (x - 2 * 26); |
78 | 0 | const char c_plus = '+'; |
79 | 0 | const char c_slash = '/'; |
80 | |
|
81 | 0 | char ret = c_az; |
82 | 0 | ret = in_AZ.select(c_AZ, ret); |
83 | 0 | ret = in_09.select(c_09, ret); |
84 | 0 | ret = eq_plus.select(c_plus, ret); |
85 | 0 | ret = eq_slash.select(c_slash, ret); |
86 | |
|
87 | 0 | return ret; |
88 | 0 | } |
89 | | |
90 | | //static |
91 | 0 | void Base64::encode(char out[8], const uint8_t in[5]) noexcept { |
92 | 0 | const uint8_t b0 = (in[0] & 0xFC) >> 2; |
93 | 0 | const uint8_t b1 = ((in[0] & 0x03) << 4) | (in[1] >> 4); |
94 | 0 | const uint8_t b2 = ((in[1] & 0x0F) << 2) | (in[2] >> 6); |
95 | 0 | const uint8_t b3 = in[2] & 0x3F; |
96 | 0 | out[0] = lookup_base64_char(b0); |
97 | 0 | out[1] = lookup_base64_char(b1); |
98 | 0 | out[2] = lookup_base64_char(b2); |
99 | 0 | out[3] = lookup_base64_char(b3); |
100 | 0 | } |
101 | | |
102 | | //static |
103 | 0 | uint8_t Base64::lookup_binary_value(char input) noexcept { |
104 | 0 | const uint8_t c = static_cast<uint8_t>(input); |
105 | |
|
106 | 0 | const auto is_alpha_upper = CT::Mask<uint8_t>::is_within_range(c, uint8_t('A'), uint8_t('Z')); |
107 | 0 | const auto is_alpha_lower = CT::Mask<uint8_t>::is_within_range(c, uint8_t('a'), uint8_t('z')); |
108 | 0 | const auto is_decimal = CT::Mask<uint8_t>::is_within_range(c, uint8_t('0'), uint8_t('9')); |
109 | |
|
110 | 0 | const auto is_plus = CT::Mask<uint8_t>::is_equal(c, uint8_t('+')); |
111 | 0 | const auto is_slash = CT::Mask<uint8_t>::is_equal(c, uint8_t('/')); |
112 | 0 | const auto is_equal = CT::Mask<uint8_t>::is_equal(c, uint8_t('=')); |
113 | |
|
114 | 0 | const auto is_whitespace = |
115 | 0 | CT::Mask<uint8_t>::is_any_of(c, {uint8_t(' '), uint8_t('\t'), uint8_t('\n'), uint8_t('\r')}); |
116 | |
|
117 | 0 | const uint8_t c_upper = c - uint8_t('A'); |
118 | 0 | const uint8_t c_lower = c - uint8_t('a') + 26; |
119 | 0 | const uint8_t c_decim = c - uint8_t('0') + 2 * 26; |
120 | |
|
121 | 0 | uint8_t ret = 0xFF; // default value |
122 | |
|
123 | 0 | ret = is_alpha_upper.select(c_upper, ret); |
124 | 0 | ret = is_alpha_lower.select(c_lower, ret); |
125 | 0 | ret = is_decimal.select(c_decim, ret); |
126 | 0 | ret = is_plus.select(62, ret); |
127 | 0 | ret = is_slash.select(63, ret); |
128 | 0 | ret = is_equal.select(0x81, ret); |
129 | 0 | ret = is_whitespace.select(0x80, ret); |
130 | |
|
131 | 0 | return ret; |
132 | 0 | } |
133 | | |
134 | | //static |
135 | 0 | bool Base64::check_bad_char(uint8_t bin, char input, bool ignore_ws) { |
136 | 0 | if(bin <= 0x3F) { |
137 | 0 | return true; |
138 | 0 | } else if(!(bin == 0x81 || (bin == 0x80 && ignore_ws))) { |
139 | 0 | throw Invalid_Argument(fmt("base64_decode: invalid character '{}'", format_char_for_display(input))); |
140 | 0 | } |
141 | 0 | return false; |
142 | 0 | } |
143 | | |
144 | | } // namespace |
145 | | |
146 | 0 | size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t& input_consumed, bool final_inputs) { |
147 | 0 | return base_encode(Base64(), out, in, input_length, input_consumed, final_inputs); |
148 | 0 | } |
149 | | |
150 | 0 | std::string base64_encode(const uint8_t input[], size_t input_length) { |
151 | 0 | return base_encode_to_string(Base64(), input, input_length); |
152 | 0 | } |
153 | | |
154 | | size_t base64_decode( |
155 | 0 | uint8_t out[], const char in[], size_t input_length, size_t& input_consumed, bool final_inputs, bool ignore_ws) { |
156 | 0 | return base_decode(Base64(), out, in, input_length, input_consumed, final_inputs, ignore_ws); |
157 | 0 | } |
158 | | |
159 | 0 | size_t base64_decode(uint8_t output[], const char input[], size_t input_length, bool ignore_ws) { |
160 | 0 | return base_decode_full(Base64(), output, input, input_length, ignore_ws); |
161 | 0 | } |
162 | | |
163 | 0 | size_t base64_decode(uint8_t output[], std::string_view input, bool ignore_ws) { |
164 | 0 | return base64_decode(output, input.data(), input.length(), ignore_ws); |
165 | 0 | } |
166 | | |
167 | 0 | size_t base64_decode(std::span<uint8_t> output, std::string_view input, bool ignore_ws) { |
168 | 0 | if(output.size() < base64_decode_max_output(input.size())) { |
169 | 0 | throw Invalid_Argument("base64_decode: output buffer is too short"); |
170 | 0 | } |
171 | 0 | return base64_decode(output.data(), input.data(), input.length(), ignore_ws); |
172 | 0 | } |
173 | | |
174 | 0 | secure_vector<uint8_t> base64_decode(const char input[], size_t input_length, bool ignore_ws) { |
175 | 0 | return base_decode_to_vec<secure_vector<uint8_t>>(Base64(), input, input_length, ignore_ws); |
176 | 0 | } |
177 | | |
178 | 0 | secure_vector<uint8_t> base64_decode(std::string_view input, bool ignore_ws) { |
179 | 0 | return base64_decode(input.data(), input.size(), ignore_ws); |
180 | 0 | } |
181 | | |
182 | 0 | size_t base64_encode_max_output(size_t input_length) { return Base64::encode_max_output(input_length); } |
183 | | |
184 | 0 | size_t base64_decode_max_output(size_t input_length) { return Base64::decode_max_output(input_length); } |
185 | | |
186 | | } // namespace Botan |