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