Botan  2.18.0
Crypto and TLS for C++11
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 #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  {
23  return "base64";
24  }
25 
26  static inline size_t encoding_bytes_in() noexcept
27  {
28  return m_encoding_bytes_in;
29  }
30  static inline size_t encoding_bytes_out() noexcept
31  {
32  return m_encoding_bytes_out;
33  }
34 
35  static inline size_t decoding_bytes_in() noexcept
36  {
37  return m_encoding_bytes_out;
38  }
39  static inline size_t decoding_bytes_out() noexcept
40  {
41  return m_encoding_bytes_in;
42  }
43 
44  static inline size_t bits_consumed() noexcept
45  {
46  return m_encoding_bits;
47  }
48  static inline size_t remaining_bits_before_padding() noexcept
49  {
50  return m_remaining_bits_before_padding;
51  }
52 
53  static inline size_t encode_max_output(size_t input_length)
54  {
55  return (round_up(input_length, m_encoding_bytes_in) / m_encoding_bytes_in) * m_encoding_bytes_out;
56  }
57  static inline size_t decode_max_output(size_t input_length)
58  {
59  return (round_up(input_length, m_encoding_bytes_out) * m_encoding_bytes_in) / m_encoding_bytes_out;
60  }
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  {
70  out_ptr[0] = (decode_buf[0] << 2) | (decode_buf[1] >> 4);
71  out_ptr[1] = (decode_buf[1] << 4) | (decode_buf[2] >> 2);
72  out_ptr[2] = (decode_buf[2] << 6) | decode_buf[3];
73  }
74 
75  static inline size_t bytes_to_remove(size_t final_truncate)
76  {
77  return final_truncate;
78  }
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  {
90  BOTAN_DEBUG_ASSERT(x < 64);
91 
92  const auto in_az = CT::Mask<uint8_t>::is_within_range(x, 26, 51);
93  const auto in_09 = CT::Mask<uint8_t>::is_within_range(x, 52, 61);
94  const auto eq_plus = CT::Mask<uint8_t>::is_equal(x, 62);
95  const auto eq_slash = CT::Mask<uint8_t>::is_equal(x, 63);
96 
97  const char c_AZ = 'A' + x;
98  const char c_az = 'a' + (x - 26);
99  const char c_09 = '0' + (x - 2*26);
100  const char c_plus = '+';
101  const char c_slash = '/';
102 
103  char ret = c_AZ;
104  ret = in_az.select(c_az, ret);
105  ret = in_09.select(c_09, ret);
106  ret = eq_plus.select(c_plus, ret);
107  ret = eq_slash.select(c_slash, ret);
108 
109  return ret;
110  }
111 
112 //static
113 void Base64::encode(char out[8], const uint8_t in[5]) noexcept
114  {
115  const uint8_t b0 = (in[0] & 0xFC) >> 2;
116  const uint8_t b1 = ((in[0] & 0x03) << 4) | (in[1] >> 4);
117  const uint8_t b2 = ((in[1] & 0x0F) << 2) | (in[2] >> 6);
118  const uint8_t b3 = in[2] & 0x3F;
119  out[0] = lookup_base64_char(b0);
120  out[1] = lookup_base64_char(b1);
121  out[2] = lookup_base64_char(b2);
122  out[3] = lookup_base64_char(b3);
123  }
124 
125 //static
126 uint8_t Base64::lookup_binary_value(char input) noexcept
127  {
128  const uint8_t c = static_cast<uint8_t>(input);
129 
130  const auto is_alpha_upper = CT::Mask<uint8_t>::is_within_range(c, uint8_t('A'), uint8_t('Z'));
131  const auto is_alpha_lower = CT::Mask<uint8_t>::is_within_range(c, uint8_t('a'), uint8_t('z'));
132  const auto is_decimal = CT::Mask<uint8_t>::is_within_range(c, uint8_t('0'), uint8_t('9'));
133 
134  const auto is_plus = CT::Mask<uint8_t>::is_equal(c, uint8_t('+'));
135  const auto is_slash = CT::Mask<uint8_t>::is_equal(c, uint8_t('/'));
136  const auto is_equal = CT::Mask<uint8_t>::is_equal(c, uint8_t('='));
137 
138  const auto is_whitespace = CT::Mask<uint8_t>::is_any_of(c, {
139  uint8_t(' '), uint8_t('\t'), uint8_t('\n'), uint8_t('\r')
140  });
141 
142  const uint8_t c_upper = c - uint8_t('A');
143  const uint8_t c_lower = c - uint8_t('a') + 26;
144  const uint8_t c_decim = c - uint8_t('0') + 2*26;
145 
146  uint8_t ret = 0xFF; // default value
147 
148  ret = is_alpha_upper.select(c_upper, ret);
149  ret = is_alpha_lower.select(c_lower, ret);
150  ret = is_decimal.select(c_decim, ret);
151  ret = is_plus.select(62, ret);
152  ret = is_slash.select(63, ret);
153  ret = is_equal.select(0x81, ret);
154  ret = is_whitespace.select(0x80, ret);
155 
156  return ret;
157  }
158 
159 //static
160 bool Base64::check_bad_char(uint8_t bin, char input, bool ignore_ws)
161  {
162  if(bin <= 0x3F)
163  {
164  return true;
165  }
166  else if(!(bin == 0x81 || (bin == 0x80 && ignore_ws)))
167  {
168  std::string bad_char(1, input);
169  if(bad_char == "\t")
170  { bad_char = "\\t"; }
171  else if(bad_char == "\n")
172  { bad_char = "\\n"; }
173  else if(bad_char == "\r")
174  { bad_char = "\\r"; }
175 
176  throw Invalid_Argument(
177  std::string("base64_decode: invalid base64 character '") +
178  bad_char + "'");
179  }
180  return false;
181  }
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  {
191  return base_encode(Base64(), out, in, input_length, input_consumed, final_inputs);
192  }
193 
194 std::string base64_encode(const uint8_t input[],
195  size_t input_length)
196  {
197  return base_encode_to_string(Base64(), input, input_length);
198  }
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  {
207  return base_decode(Base64(), out, in, input_length, input_consumed, final_inputs, ignore_ws);
208  }
209 
210 size_t base64_decode(uint8_t output[],
211  const char input[],
212  size_t input_length,
213  bool ignore_ws)
214  {
215  return base_decode_full(Base64(), output, input, input_length, ignore_ws);
216  }
217 
218 size_t base64_decode(uint8_t output[],
219  const std::string& input,
220  bool ignore_ws)
221  {
222  return base64_decode(output, input.data(), input.length(), ignore_ws);
223  }
224 
226  size_t input_length,
227  bool ignore_ws)
228  {
229  return base_decode_to_vec<secure_vector<uint8_t>>(Base64(), input, input_length, ignore_ws);
230  }
231 
232 secure_vector<uint8_t> base64_decode(const std::string& input,
233  bool ignore_ws)
234  {
235  return base64_decode(input.data(), input.size(), ignore_ws);
236  }
237 
238 size_t base64_encode_max_output(size_t input_length)
239  {
240  return Base64::encode_max_output(input_length);
241  }
242 
243 size_t base64_decode_max_output(size_t input_length)
244  {
245  return Base64::decode_max_output(input_length);
246  }
247 
248 }
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:124
size_t base64_decode_max_output(size_t input_length)
Definition: base64.cpp:243
size_t base64_encode_max_output(size_t input_length)
Definition: base64.cpp:238
int(* final)(unsigned char *, CTX *)
static Mask< T > is_within_range(T v, T l, T u)
Definition: ct_utils.h:186
std::string base_encode_to_string(Base &&base, const uint8_t input[], size_t input_length)
Definition: codec_base.h:86
std::string name
std::string encode(const uint8_t der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:43
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition: base64.cpp:185
#define BOTAN_DEBUG_ASSERT(expr)
Definition: assert.h:123
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
Definition: pem.cpp:68
Definition: alg_id.cpp:13
static Mask< T > is_any_of(T v, std::initializer_list< T > accepted)
Definition: ct_utils.h:196
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:200
size_t base_decode_full(Base &&base, uint8_t output[], const char input[], size_t input_length, bool ignore_ws)
Definition: codec_base.h:189
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:21
static Mask< T > is_equal(T x, T y)
Definition: ct_utils.h:149
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