Botan 3.14.1
Crypto and TLS for C&
base64.cpp
Go to the documentation of this file.
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/bit_ops.h>
12#include <botan/internal/charset.h>
13#include <botan/internal/codec_base.h>
14#include <botan/internal/fmt.h>
15#include <botan/internal/int_utils.h>
16#include <botan/internal/loadstor.h>
17
18#if defined(BOTAN_HAS_BASE64_AVX512)
19 #include <botan/internal/base64_avx512.h>
20 #include <botan/internal/cpuid.h>
21 #include <botan/internal/mem_utils.h>
22#endif
23
24namespace Botan {
25
26namespace {
27
28class Base64 final {
29 public:
30 static std::string name() { return "base64"; }
31
32 static constexpr size_t encoding_bytes_in() noexcept { return m_encoding_bytes_in; }
33
34 static constexpr size_t encoding_bytes_out() noexcept { return m_encoding_bytes_out; }
35
36 static constexpr size_t decoding_bytes_in() noexcept { return m_encoding_bytes_out; }
37
38 static constexpr size_t decoding_bytes_out() noexcept { return m_encoding_bytes_in; }
39
40 static constexpr size_t bits_consumed() noexcept { return m_encoding_bits; }
41
42 static constexpr size_t remaining_bits_before_padding() noexcept { return m_remaining_bits_before_padding; }
43
44 static constexpr size_t encode_max_output(size_t input_length) {
45 const size_t encoding_blocks = ceil_division(input_length, m_encoding_bytes_in);
46 return mul_or_throw(encoding_blocks, m_encoding_bytes_out, "Input too large to base64 encode");
47 }
48
49 static constexpr size_t decode_max_output(size_t input_length) {
50 // Cannot overflow since each block shrinks when decoded
51 return ceil_division(input_length, m_encoding_bytes_out) * m_encoding_bytes_in;
52 }
53
54 static void encode(char out[4], const uint8_t in[3]) noexcept;
55
56 static uint8_t lookup_binary_value(char input) noexcept;
57
58 static bool check_bad_char(uint8_t bin, char input, bool ignore_ws);
59
60 static void decode(uint8_t* out_ptr, const uint8_t decode_buf[4]) {
61 out_ptr[0] = (decode_buf[0] << 2) | (decode_buf[1] >> 4);
62 out_ptr[1] = (decode_buf[1] << 4) | (decode_buf[2] >> 2);
63 out_ptr[2] = (decode_buf[2] << 6) | decode_buf[3];
64 }
65
66 static size_t bytes_to_remove(size_t final_truncate) { return final_truncate; }
67
68 private:
69 static constexpr size_t m_encoding_bits = 6;
70 static constexpr size_t m_remaining_bits_before_padding = 8;
71
72 static constexpr size_t m_encoding_bytes_in = 3;
73 static constexpr size_t m_encoding_bytes_out = 4;
74};
75
76uint32_t lookup_base64_chars(uint32_t x32) {
77 /*
78 * The basic insight of this approach is that our goal is computing
79 * f(x) = y where x is in [0,63) and y is the correct base64 encoding.
80 *
81 * Instead of doing this directly, we compute
82 * offset(x) such that f(x) = x + offset(x)
83 *
84 * This is described in
85 * http://0x80.pl/notesen/2016-01-12-sse-base64-encoding.html#improved-version
86 *
87 * Here we do a SWAR (simd within a register) implementation of Wojciech's lookup_version2_swar
88 */
89
90 uint32_t r = x32 + 0x41414141;
91
92 r += (~swar_lt<uint32_t>(x32, 0x1A1A1A1A)) & 0x06060606;
93 r -= (~swar_lt<uint32_t>(x32, 0x34343434)) & 0x4B4B4B4B;
94 r -= (~swar_lt<uint32_t>(x32, 0x3E3E3E3E)) & 0x0F0F0F0F;
95 r += (~swar_lt<uint32_t>(x32, 0x3F3F3F3F)) & 0x03030303;
96
97 return r;
98}
99
100//static
101void Base64::encode(char out[4], const uint8_t in[3]) noexcept {
102 const uint32_t b0 = (in[0] & 0xFC) >> 2;
103 const uint32_t b1 = ((in[0] & 0x03) << 4) | (in[1] >> 4);
104 const uint32_t b2 = ((in[1] & 0x0F) << 2) | (in[2] >> 6);
105 const uint32_t b3 = in[2] & 0x3F;
106
107 const uint32_t z = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
108
109 const uint32_t b64 = lookup_base64_chars(z);
110
111 out[0] = static_cast<char>(get_byte<0>(b64));
112 out[1] = static_cast<char>(get_byte<1>(b64));
113 out[2] = static_cast<char>(get_byte<2>(b64));
114 out[3] = static_cast<char>(get_byte<3>(b64));
115}
116
117//static
118uint8_t Base64::lookup_binary_value(char input) noexcept {
119 const auto has_zero_byte = [](uint64_t v) { return ((v - 0x0101010101010101) & ~(v) & 0x8080808080808080); };
120
121 // Assumes each byte is either 0x00 or 0x80
122 const auto index_of_first_set_byte = [](uint64_t v) {
123 return ((((v - 1) & 0x0101010101010101) * 0x0101010101010101) >> 56) - 1;
124 };
125
126 constexpr uint64_t lo = 0x0101010101010101;
127
128 const uint8_t x = static_cast<uint8_t>(input);
129
130 const uint64_t x8 = x * lo;
131
132 // Defines the valid ASCII ranges of base64, except the special chars (below)
133 constexpr uint64_t val_l = make_uint64(0, 0, 0, 0, 0, 'A', 'a', '0');
134 constexpr uint64_t val_u = make_uint64(0, 0, 0, 0, 0, 26, 26, 10);
135
136 // If x is in one of the ranges return a mask. Otherwise we xor in at the
137 // high word which will be our invalid marker
138 const auto v_mask = swar_in_range<uint64_t>(x8, val_l, val_u) ^ 0x80000000;
139
140 // This is the offset added to x to get the value
141 const uint64_t val_v = 0xbfb904 ^ (0xFF000000 - (x << 24));
142
143 const uint8_t z = x + static_cast<uint8_t>(val_v >> (8 * index_of_first_set_byte(v_mask)));
144
145 // Valid base64 special characters, and some whitespace chars
146 constexpr uint64_t specials_i = make_uint64(0, '+', '/', '=', ' ', '\n', '\t', '\r');
147
148 const uint64_t specials_v = 0x3e3f8180808080 ^ (static_cast<uint64_t>(z) << 56);
149
150 const uint64_t smask = has_zero_byte(x8 ^ specials_i) ^ 0x8000000000000000;
151
152 return static_cast<uint8_t>(specials_v >> (8 * index_of_first_set_byte(smask)));
153}
154
155//static
156bool Base64::check_bad_char(uint8_t bin, char input, bool ignore_ws) {
157 if(bin <= 0x3F) {
158 return true;
159 } else if(!(bin == 0x81 || (bin == 0x80 && ignore_ws))) {
160 throw Invalid_Argument(fmt("base64_decode: invalid character '{}'", format_char_for_display(input)));
161 }
162 return false;
163}
164
165} // namespace
166
167size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t& input_consumed, bool final_inputs) {
168 size_t consumed = 0;
169 size_t produced = 0;
170
171#if defined(BOTAN_HAS_BASE64_AVX512)
172 if(input_length >= 48 && CPUID::has(CPUID::Feature::AVX512)) {
173 consumed = base64_encode_avx512(out, in, input_length);
174 produced = consumed / 3 * 4;
175 }
176#endif
177
178 size_t remainder_consumed = 0;
179 produced +=
180 base_encode(Base64(), out + produced, in + consumed, input_length - consumed, remainder_consumed, final_inputs);
181
182 input_consumed = consumed + remainder_consumed;
183 return produced;
184}
185
186std::string base64_encode(const uint8_t input[], size_t input_length) {
187 const size_t output_length = Base64::encode_max_output(input_length);
188 std::string output(output_length, 0);
189
190 size_t consumed = 0;
191 size_t produced = 0;
192
193 if(output_length > 0) {
194 produced = base64_encode(&output.front(), input, input_length, consumed, true);
195 }
196
197 BOTAN_ASSERT_EQUAL(consumed, input_length, "Consumed the entire input");
198 BOTAN_ASSERT_EQUAL(produced, output.size(), "Produced expected size");
199
200 return output;
201}
202
204 uint8_t out[], const char in[], size_t input_length, size_t& input_consumed, bool final_inputs, bool ignore_ws) {
205 return base_decode(Base64(), out, in, input_length, input_consumed, final_inputs, ignore_ws);
206}
207
208size_t base64_decode(uint8_t output[], const char input[], size_t input_length, bool ignore_ws) {
209#if defined(BOTAN_HAS_BASE64_AVX512)
210 if(input_length >= 128 && CPUID::has(CPUID::Feature::AVX512, CPUID::Feature::POPCNT)) {
211 // Decoding directly stops at the first block containing whitespace,
212 // padding, or an invalid character
213 const uint8_t* in8 = as_span_of_bytes(input, input_length).data();
214 const size_t bulk = base64_decode_avx512(output, in8, input_length);
215 size_t written = bulk / 4 * 3;
216
217 const uint8_t* rest = in8 + bulk;
218 size_t rest_len = input_length - bulk;
219
220 // If the stop may have been due to whitespace, compact the remainder
221 // and continue decoding
222 secure_vector<uint8_t> compacted;
223 if(ignore_ws && rest_len > 128) {
224 compacted.resize(rest_len);
225 const size_t clean_len = base64_strip_ws_avx512(compacted.data(), rest, rest_len);
226 rest = compacted.data();
227 rest_len = clean_len;
228
229 const size_t bulk2 = base64_decode_avx512(output + written, rest, rest_len);
230 written += bulk2 / 4 * 3;
231 rest += bulk2;
232 rest_len -= bulk2;
233 }
234
235 // The scalar decoder handles the final blocks, and with them all of
236 // the padding logic and error reporting
237 const size_t tail_written =
238 base_decode_full(Base64(), output + written, cast_uint8_ptr_to_char(rest), rest_len, ignore_ws);
239
240 return written + tail_written;
241 }
242#endif
243
244 return base_decode_full(Base64(), output, input, input_length, ignore_ws);
245}
246
247size_t base64_decode(uint8_t output[], std::string_view input, bool ignore_ws) {
248 return base64_decode(output, input.data(), input.length(), ignore_ws);
249}
250
251size_t base64_decode(std::span<uint8_t> output, std::string_view input, bool ignore_ws) {
252 if(output.size() < base64_decode_max_output(input.size())) {
253 throw Invalid_Argument("base64_decode: output buffer is too short");
254 }
255 return base64_decode(output.data(), input.data(), input.length(), ignore_ws);
256}
257
258secure_vector<uint8_t> base64_decode(const char input[], size_t input_length, bool ignore_ws) {
259 secure_vector<uint8_t> bin(Base64::decode_max_output(input_length));
260 const size_t written = base64_decode(bin.data(), input, input_length, ignore_ws);
261 bin.resize(written);
262 return bin;
263}
264
265secure_vector<uint8_t> base64_decode(std::string_view input, bool ignore_ws) {
266 return base64_decode(input.data(), input.size(), ignore_ws);
267}
268
269size_t base64_encode_max_output(size_t input_length) {
270 return Base64::encode_max_output(input_length);
271}
272
273size_t base64_decode_max_output(size_t input_length) {
274 return Base64::decode_max_output(input_length);
275}
276
277} // namespace Botan
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition assert.h:95
static bool has(CPUID::Feature feat)
Definition cpuid.h:93
const char * name
Definition ffi_tls.h:59
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
Definition pem.cpp:41
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
Definition pem.cpp:64
constexpr uint8_t get_byte(T input)
Definition loadstor.h:79
std::string format_char_for_display(char c)
Definition charset.cpp:243
constexpr T mul_or_throw(T a, T b, std::string_view msg)
Definition int_utils.h:81
constexpr T swar_lt(T a, T b)
Definition int_utils.h:121
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
Definition mem_utils.h:59
constexpr auto out_ptr(T &outptr) noexcept
Definition stl_util.h:164
constexpr uint64_t make_uint64(uint8_t i0, uint8_t i1, uint8_t i2, uint8_t i3, uint8_t i4, uint8_t i5, uint8_t i6, uint8_t i7)
Definition loadstor.h:121
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
BOTAN_FORCE_INLINE constexpr T ceil_division(T a, T b)
Definition bit_ops.h:167
constexpr T swar_in_range(T v, T lower, T upper)
Definition int_utils.h:144
constexpr size_t index_of_first_set_byte(T v)
Definition int_utils.h:160
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition base64.cpp:167
size_t base_encode(const Base &base, char output[], const uint8_t input[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition codec_base.h:35
size_t base64_decode(uint8_t out[], const char in[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws)
Definition base64.cpp:203
BOTAN_FN_ISA_AVX512 size_t base64_encode_avx512(char out[], const uint8_t in[], size_t length)
size_t base_decode(const Base &base, uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws=true)
Definition codec_base.h:118
size_t base64_encode_max_output(size_t input_length)
Definition base64.cpp:269
size_t base64_decode_max_output(size_t input_length)
Definition base64.cpp:273
BOTAN_FN_ISA_AVX512_POPCNT size_t base64_strip_ws_avx512(uint8_t out[], const uint8_t in[], size_t length)
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition mem_ops.h:323
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
BOTAN_FN_ISA_AVX512_POPCNT size_t base64_decode_avx512(uint8_t out[], const uint8_t in[], size_t length)
size_t base_decode_full(const Base &base, uint8_t output[], const char input[], size_t input_length, bool ignore_ws)
Definition codec_base.h:204