Botan 3.6.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/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/int_utils.h>
16#include <botan/internal/loadstor.h>
17#include <botan/internal/rounding.h>
18
19namespace Botan {
20
21namespace {
22
23class Base64 final {
24 public:
25 static std::string name() noexcept { return "base64"; }
26
27 static size_t encoding_bytes_in() noexcept { return m_encoding_bytes_in; }
28
29 static size_t encoding_bytes_out() noexcept { return m_encoding_bytes_out; }
30
31 static size_t decoding_bytes_in() noexcept { return m_encoding_bytes_out; }
32
33 static size_t decoding_bytes_out() noexcept { return m_encoding_bytes_in; }
34
35 static size_t bits_consumed() noexcept { return m_encoding_bits; }
36
37 static size_t remaining_bits_before_padding() noexcept { return m_remaining_bits_before_padding; }
38
39 static size_t encode_max_output(size_t input_length) {
40 return (round_up(input_length, m_encoding_bytes_in) / m_encoding_bytes_in) * m_encoding_bytes_out;
41 }
42
43 static size_t decode_max_output(size_t input_length) {
44 return (round_up(input_length, m_encoding_bytes_out) * m_encoding_bytes_in) / m_encoding_bytes_out;
45 }
46
47 static void encode(char out[4], const uint8_t in[3]) noexcept;
48
49 static uint8_t lookup_binary_value(char input) noexcept;
50
51 static bool check_bad_char(uint8_t bin, char input, bool ignore_ws);
52
53 static void decode(uint8_t* out_ptr, const uint8_t decode_buf[4]) {
54 out_ptr[0] = (decode_buf[0] << 2) | (decode_buf[1] >> 4);
55 out_ptr[1] = (decode_buf[1] << 4) | (decode_buf[2] >> 2);
56 out_ptr[2] = (decode_buf[2] << 6) | decode_buf[3];
57 }
58
59 static size_t bytes_to_remove(size_t final_truncate) { return final_truncate; }
60
61 private:
62 static const size_t m_encoding_bits = 6;
63 static const size_t m_remaining_bits_before_padding = 8;
64
65 static const size_t m_encoding_bytes_in = 3;
66 static const size_t m_encoding_bytes_out = 4;
67};
68
69uint32_t lookup_base64_chars(uint32_t x32) {
70 /*
71 * The basic insight of this approach is that our goal is computing
72 * f(x) = y where x is in [0,63) and y is the correct base64 encoding.
73 *
74 * Instead of doing this directly, we compute
75 * offset(x) such that f(x) = x + offset(x)
76 *
77 * This is described in
78 * http://0x80.pl/notesen/2016-01-12-sse-base64-encoding.html#improved-version
79 *
80 * Here we do a SWAR (simd within a register) implementation of Wojciech's lookup_version2_swar
81 */
82
83 uint32_t r = x32 + 0x41414141;
84
85 r += (~swar_lt<uint32_t>(x32, 0x1A1A1A1A)) & 0x06060606;
86 r -= (~swar_lt<uint32_t>(x32, 0x34343434)) & 0x4B4B4B4B;
87 r -= (~swar_lt<uint32_t>(x32, 0x3E3E3E3E)) & 0x0F0F0F0F;
88 r += (~swar_lt<uint32_t>(x32, 0x3F3F3F3F)) & 0x03030303;
89
90 return r;
91}
92
93//static
94void Base64::encode(char out[4], const uint8_t in[3]) noexcept {
95 const uint32_t b0 = (in[0] & 0xFC) >> 2;
96 const uint32_t b1 = ((in[0] & 0x03) << 4) | (in[1] >> 4);
97 const uint32_t b2 = ((in[1] & 0x0F) << 2) | (in[2] >> 6);
98 const uint32_t b3 = in[2] & 0x3F;
99
100 const uint32_t z = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
101
102 const uint32_t b64 = lookup_base64_chars(z);
103
104 out[0] = static_cast<char>(get_byte<0>(b64));
105 out[1] = static_cast<char>(get_byte<1>(b64));
106 out[2] = static_cast<char>(get_byte<2>(b64));
107 out[3] = static_cast<char>(get_byte<3>(b64));
108}
109
110//static
111uint8_t Base64::lookup_binary_value(char input) noexcept {
112 auto has_zero_byte = [](uint64_t v) { return ((v - 0x0101010101010101) & ~(v) & 0x8080808080808080); };
113
114 // Assumes each byte is either 0x00 or 0x80
115 auto index_of_first_set_byte = [](uint64_t v) {
116 return ((((v - 1) & 0x0101010101010101) * 0x0101010101010101) >> 56) - 1;
117 };
118
119 constexpr uint64_t lo = 0x0101010101010101;
120
121 const uint8_t x = static_cast<uint8_t>(input);
122
123 const uint64_t x8 = x * lo;
124
125 // Defines the valid ASCII ranges of base64, except the special chars (below)
126 constexpr uint64_t val_l = make_uint64(0, 0, 0, 0, 0, 'A', 'a', '0');
127 constexpr uint64_t val_u = make_uint64(0, 0, 0, 0, 0, 26, 26, 10);
128
129 // If x is in one of the ranges return a mask. Otherwise we xor in at the
130 // high word which will be our invalid marker
131 auto v_mask = swar_in_range<uint64_t>(x8, val_l, val_u) ^ 0x80000000;
132
133 // This is the offset added to x to get the value
134 const uint64_t val_v = 0xbfb904 ^ (0xFF000000 - (x << 24));
135
136 uint8_t z = x + static_cast<uint8_t>(val_v >> (8 * index_of_first_set_byte(v_mask)));
137
138 // Valid base64 special characters, and some whitespace chars
139 constexpr uint64_t specials_i = make_uint64(0, '+', '/', '=', ' ', '\n', '\t', '\r');
140
141 const uint64_t specials_v = 0x3e3f8180808080 ^ (static_cast<uint64_t>(z) << 56);
142
143 const uint64_t smask = has_zero_byte(x8 ^ specials_i) ^ 0x8000000000000000;
144
145 return static_cast<uint8_t>(specials_v >> (8 * index_of_first_set_byte(smask)));
146}
147
148//static
149bool Base64::check_bad_char(uint8_t bin, char input, bool ignore_ws) {
150 if(bin <= 0x3F) {
151 return true;
152 } else if(!(bin == 0x81 || (bin == 0x80 && ignore_ws))) {
153 throw Invalid_Argument(fmt("base64_decode: invalid character '{}'", format_char_for_display(input)));
154 }
155 return false;
156}
157
158} // namespace
159
160size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t& input_consumed, bool final_inputs) {
161 return base_encode(Base64(), out, in, input_length, input_consumed, final_inputs);
162}
163
164std::string base64_encode(const uint8_t input[], size_t input_length) {
165 return base_encode_to_string(Base64(), input, input_length);
166}
167
169 uint8_t out[], const char in[], size_t input_length, size_t& input_consumed, bool final_inputs, bool ignore_ws) {
170 return base_decode(Base64(), out, in, input_length, input_consumed, final_inputs, ignore_ws);
171}
172
173size_t base64_decode(uint8_t output[], const char input[], size_t input_length, bool ignore_ws) {
174 return base_decode_full(Base64(), output, input, input_length, ignore_ws);
175}
176
177size_t base64_decode(uint8_t output[], std::string_view input, bool ignore_ws) {
178 return base64_decode(output, input.data(), input.length(), ignore_ws);
179}
180
181size_t base64_decode(std::span<uint8_t> output, std::string_view input, bool ignore_ws) {
182 if(output.size() < base64_decode_max_output(input.size())) {
183 throw Invalid_Argument("base64_decode: output buffer is too short");
184 }
185 return base64_decode(output.data(), input.data(), input.length(), ignore_ws);
186}
187
188secure_vector<uint8_t> base64_decode(const char input[], size_t input_length, bool ignore_ws) {
189 return base_decode_to_vec<secure_vector<uint8_t>>(Base64(), input, input_length, ignore_ws);
190}
191
192secure_vector<uint8_t> base64_decode(std::string_view input, bool ignore_ws) {
193 return base64_decode(input.data(), input.size(), ignore_ws);
194}
195
196size_t base64_encode_max_output(size_t input_length) {
197 return Base64::encode_max_output(input_length);
198}
199
200size_t base64_decode_max_output(size_t input_length) {
201 return Base64::decode_max_output(input_length);
202}
203
204} // namespace Botan
std::string name
int(* final)(unsigned char *, CTX *)
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
Definition pem.cpp:39
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
Definition pem.cpp:62
constexpr uint8_t get_byte(T input)
Definition loadstor.h:75
std::string format_char_for_display(char c)
Definition charset.cpp:98
constexpr auto out_ptr(T &outptr) noexcept
Definition stl_util.h:420
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:117
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
constexpr T swar_in_range(T v, T lower, T upper)
Definition int_utils.h:114
constexpr size_t round_up(size_t n, size_t align_to)
Definition rounding.h:25
constexpr size_t index_of_first_set_byte(T v)
Definition int_utils.h:130
Vector base_decode_to_vec(Base &&base, const char input[], size_t input_length, bool ignore_ws)
Definition codec_base.h:183
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition base64.cpp:160
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:168
size_t base_encode(Base &&base, char output[], const uint8_t input[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition codec_base.h:34
size_t base64_encode_max_output(size_t input_length)
Definition base64.cpp:196
size_t base64_decode_max_output(size_t input_length)
Definition base64.cpp:200
size_t base_decode(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:112
std::string base_encode_to_string(Base &&base, const uint8_t input[], size_t input_length)
Definition codec_base.h:78
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
size_t base_decode_full(Base &&base, uint8_t output[], const char input[], size_t input_length, bool ignore_ws)
Definition codec_base.h:171