Botan 3.5.0
Crypto and TLS for C&
base32.cpp
Go to the documentation of this file.
1/*
2* Base32 Encoding and Decoding
3* (C) 2018 Erwan Chaussy
4* (C) 2018,2020 Jack Lloyd
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/base32.h>
10
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 Base32 final {
22 public:
23 static std::string name() noexcept { return "base32"; }
24
25 static size_t encoding_bytes_in() noexcept { return m_encoding_bytes_in; }
26
27 static size_t encoding_bytes_out() noexcept { return m_encoding_bytes_out; }
28
29 static size_t decoding_bytes_in() noexcept { return m_encoding_bytes_out; }
30
31 static size_t decoding_bytes_out() noexcept { return m_encoding_bytes_in; }
32
33 static size_t bits_consumed() noexcept { return m_encoding_bits; }
34
35 static size_t remaining_bits_before_padding() noexcept { return m_remaining_bits_before_padding; }
36
37 static 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 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[8]) {
52 out_ptr[0] = (decode_buf[0] << 3) | (decode_buf[1] >> 2);
53 out_ptr[1] = (decode_buf[1] << 6) | (decode_buf[2] << 1) | (decode_buf[3] >> 4);
54 out_ptr[2] = (decode_buf[3] << 4) | (decode_buf[4] >> 1);
55 out_ptr[3] = (decode_buf[4] << 7) | (decode_buf[5] << 2) | (decode_buf[6] >> 3);
56 out_ptr[4] = (decode_buf[6] << 5) | decode_buf[7];
57 }
58
59 static size_t bytes_to_remove(size_t final_truncate) { return final_truncate ? (final_truncate / 2) + 1 : 0; }
60
61 private:
62 static const size_t m_encoding_bits = 5;
63 static const size_t m_remaining_bits_before_padding = 6;
64
65 static const size_t m_encoding_bytes_in = 5;
66 static const size_t m_encoding_bytes_out = 8;
67};
68
69namespace {
70
71char lookup_base32_char(uint8_t x) {
72 BOTAN_DEBUG_ASSERT(x < 32);
73
74 const auto in_AZ = CT::Mask<uint8_t>::is_lt(x, 26);
75
76 const char c_AZ = 'A' + x;
77 const char c_27 = '2' + (x - 26);
78
79 return in_AZ.select(c_AZ, c_27);
80}
81
82} // namespace
83
84//static
85void Base32::encode(char out[8], const uint8_t in[5]) noexcept {
86 const uint8_t b0 = (in[0] & 0xF8) >> 3;
87 const uint8_t b1 = ((in[0] & 0x07) << 2) | (in[1] >> 6);
88 const uint8_t b2 = ((in[1] & 0x3E) >> 1);
89 const uint8_t b3 = ((in[1] & 0x01) << 4) | (in[2] >> 4);
90 const uint8_t b4 = ((in[2] & 0x0F) << 1) | (in[3] >> 7);
91 const uint8_t b5 = ((in[3] & 0x7C) >> 2);
92 const uint8_t b6 = ((in[3] & 0x03) << 3) | (in[4] >> 5);
93 const uint8_t b7 = in[4] & 0x1F;
94
95 out[0] = lookup_base32_char(b0);
96 out[1] = lookup_base32_char(b1);
97 out[2] = lookup_base32_char(b2);
98 out[3] = lookup_base32_char(b3);
99 out[4] = lookup_base32_char(b4);
100 out[5] = lookup_base32_char(b5);
101 out[6] = lookup_base32_char(b6);
102 out[7] = lookup_base32_char(b7);
103}
104
105//static
106uint8_t Base32::lookup_binary_value(char input) noexcept {
107 const uint8_t c = static_cast<uint8_t>(input);
108
109 const auto is_alpha_upper = CT::Mask<uint8_t>::is_within_range(c, uint8_t('A'), uint8_t('Z'));
110 const auto is_decimal = CT::Mask<uint8_t>::is_within_range(c, uint8_t('2'), uint8_t('7'));
111
112 const auto is_equal = CT::Mask<uint8_t>::is_equal(c, uint8_t('='));
113 const auto is_whitespace =
114 CT::Mask<uint8_t>::is_any_of(c, {uint8_t(' '), uint8_t('\t'), uint8_t('\n'), uint8_t('\r')});
115
116 const uint8_t c_upper = c - uint8_t('A');
117 const uint8_t c_decim = c - uint8_t('2') + 26;
118
119 uint8_t ret = 0xFF; // default value
120
121 ret = is_alpha_upper.select(c_upper, ret);
122 ret = is_decimal.select(c_decim, ret);
123 ret = is_equal.select(0x81, ret);
124 ret = is_whitespace.select(0x80, ret);
125
126 return ret;
127}
128
129//static
130bool Base32::check_bad_char(uint8_t bin, char input, bool ignore_ws) {
131 if(bin <= 0x1F) {
132 return true;
133 } else if(!(bin == 0x81 || (bin == 0x80 && ignore_ws))) {
134 throw Invalid_Argument(fmt("base32_decode: invalid character '{}'", format_char_for_display(input)));
135 }
136 return false;
137}
138
139} // namespace
140
141size_t base32_encode(char out[], const uint8_t in[], size_t input_length, size_t& input_consumed, bool final_inputs) {
142 return base_encode(Base32(), out, in, input_length, input_consumed, final_inputs);
143}
144
145std::string base32_encode(const uint8_t input[], size_t input_length) {
146 return base_encode_to_string(Base32(), input, input_length);
147}
148
150 uint8_t out[], const char in[], size_t input_length, size_t& input_consumed, bool final_inputs, bool ignore_ws) {
151 return base_decode(Base32(), out, in, input_length, input_consumed, final_inputs, ignore_ws);
152}
153
154size_t base32_decode(uint8_t output[], const char input[], size_t input_length, bool ignore_ws) {
155 return base_decode_full(Base32(), output, input, input_length, ignore_ws);
156}
157
158size_t base32_decode(uint8_t output[], std::string_view input, bool ignore_ws) {
159 return base32_decode(output, input.data(), input.length(), ignore_ws);
160}
161
162secure_vector<uint8_t> base32_decode(const char input[], size_t input_length, bool ignore_ws) {
163 return base_decode_to_vec<secure_vector<uint8_t>>(Base32(), input, input_length, ignore_ws);
164}
165
166secure_vector<uint8_t> base32_decode(std::string_view input, bool ignore_ws) {
167 return base32_decode(input.data(), input.size(), ignore_ws);
168}
169
170} // namespace Botan
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:98
static constexpr Mask< T > is_within_range(T v, T l, T u)
Definition ct_utils.h:278
static constexpr Mask< T > is_equal(T x, T y)
Definition ct_utils.h:250
static constexpr Mask< T > is_lt(T x, T y)
Definition ct_utils.h:258
static constexpr Mask< T > is_any_of(T v, std::initializer_list< T > accepted)
Definition ct_utils.h:287
std::string name
int(* final)(unsigned char *, CTX *)
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:486
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
Vector base_decode_to_vec(Base &&base, const char input[], size_t input_length, bool ignore_ws)
Definition codec_base.h:183
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 base32_decode(uint8_t out[], const char in[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws)
Definition base32.cpp:149
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 base32_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition base32.cpp:141
size_t round_up(size_t n, size_t align_to)
Definition rounding.h:25
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