Botan 2.19.5 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#include <botan/internal/codec_base.h>
11#include <botan/internal/rounding.h>
12#include <botan/internal/ct_utils.h>
13
14 namespace Botan {
15
16 namespace {
17
18 class Base32 final
19 {
20 public:
21 static inline std::string name() noexcept
22 {
23 return "base32";
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[8])
69 {
70 out_ptr[0] = (decode_buf[0] << 3) | (decode_buf[1] >> 2);
71 out_ptr[1] = (decode_buf[1] << 6) | (decode_buf[2] << 1) | (decode_buf[3] >> 4);
72 out_ptr[2] = (decode_buf[3] << 4) | (decode_buf[4] >> 1);
73 out_ptr[3] = (decode_buf[4] << 7) | (decode_buf[5] << 2) | (decode_buf[6] >> 3);
74 out_ptr[4] = (decode_buf[6] << 5) | decode_buf[7];
75 }
76
77 static inline size_t bytes_to_remove(size_t final_truncate)
78 {
79 return final_truncate ? (final_truncate / 2) + 1 : 0;
80 }
81
82 private:
83 static const size_t m_encoding_bits = 5;
84 static const size_t m_remaining_bits_before_padding = 6;
85
86 static const size_t m_encoding_bytes_in = 5;
87 static const size_t m_encoding_bytes_out = 8;
88 };
89
90 namespace {
91
92 char lookup_base32_char(uint8_t x)
93 {
94 BOTAN_DEBUG_ASSERT(x < 32);
95
96 const auto in_AZ = CT::Mask<uint8_t>::is_lt(x, 26);
97
98 const char c_AZ = 'A' + x;
99 const char c_27 = '2' + (x - 26);
100
101 return in_AZ.select(c_AZ, c_27);
102 }
103
104}
105
106//static
107 void Base32::encode(char out[8], const uint8_t in[5]) noexcept
108 {
109 const uint8_t b0 = (in[0] & 0xF8) >> 3;
110 const uint8_t b1 = ((in[0] & 0x07) << 2) | (in[1] >> 6);
111 const uint8_t b2 = ((in[1] & 0x3E) >> 1);
112 const uint8_t b3 = ((in[1] & 0x01) << 4) | (in[2] >> 4);
113 const uint8_t b4 = ((in[2] & 0x0F) << 1) | (in[3] >> 7);
114 const uint8_t b5 = ((in[3] & 0x7C) >> 2);
115 const uint8_t b6 = ((in[3] & 0x03) << 3) | (in[4] >> 5);
116 const uint8_t b7 = in[4] & 0x1F;
117
118 out[0] = lookup_base32_char(b0);
119 out[1] = lookup_base32_char(b1);
120 out[2] = lookup_base32_char(b2);
121 out[3] = lookup_base32_char(b3);
122 out[4] = lookup_base32_char(b4);
123 out[5] = lookup_base32_char(b5);
124 out[6] = lookup_base32_char(b6);
125 out[7] = lookup_base32_char(b7);
126 }
127
128//static
129uint8_t Base32::lookup_binary_value(char input) noexcept
130 {
131 const uint8_t c = static_cast<uint8_t>(input);
132
133 const auto is_alpha_upper = CT::Mask<uint8_t>::is_within_range(c, uint8_t('A'), uint8_t('Z'));
134 const auto is_decimal = CT::Mask<uint8_t>::is_within_range(c, uint8_t('2'), uint8_t('7'));
135
136 const auto is_equal = CT::Mask<uint8_t>::is_equal(c, uint8_t('='));
137 const auto is_whitespace = CT::Mask<uint8_t>::is_any_of(c, {
138 uint8_t(' '), uint8_t('\t'), uint8_t('\n'), uint8_t('\r')
139 });
140
141 const uint8_t c_upper = c - uint8_t('A');
142 const uint8_t c_decim = c - uint8_t('2') + 26;
143
144 uint8_t ret = 0xFF; // default value
145
146 ret = is_alpha_upper.select(c_upper, ret);
147 ret = is_decimal.select(c_decim, ret);
148 ret = is_equal.select(0x81, ret);
149 ret = is_whitespace.select(0x80, ret);
150
151 return ret;
152 }
153
154//static
155 bool Base32::check_bad_char(uint8_t bin, char input, bool ignore_ws)
156 {
157 if(bin <= 0x1F)
158 {
159 return true;
160 }
161 else if(!(bin == 0x81 || (bin == 0x80 && ignore_ws)))
162 {
163 std::string bad_char(1, input);
164 if(bad_char == "\t")
165 { bad_char = "\\t"; }
166 else if(bad_char == "\n")
167 { bad_char = "\\n"; }
168 else if(bad_char == "\r")
169 { bad_char = "\\r"; }
170
171 throw Invalid_Argument(
172 std::string("base32_decode: invalid base32 character '") +
173 bad_char + "'");
174 }
175 return false;
176 }
177
178}
179
180 size_t base32_encode(char out[],
181 const uint8_t in[],
182 size_t input_length,
183 size_t& input_consumed,
184 bool final_inputs)
185 {
186 return base_encode(Base32(), out, in, input_length, input_consumed, final_inputs);
187 }
188
189std::string base32_encode(const uint8_t input[],
190 size_t input_length)
191 {
192 return base_encode_to_string(Base32(), input, input_length);
193 }
194
195 size_t base32_decode(uint8_t out[],
196 const char in[],
197 size_t input_length,
198 size_t& input_consumed,
199 bool final_inputs,
200 bool ignore_ws)
201 {
202 return base_decode(Base32(), out, in, input_length, input_consumed, final_inputs, ignore_ws);
203 }
204
205 size_t base32_decode(uint8_t output[],
206 const char input[],
207 size_t input_length,
208 bool ignore_ws)
209 {
210 return base_decode_full(Base32(), output, input, input_length, ignore_ws);
211 }
212
213 size_t base32_decode(uint8_t output[],
214 const std::string& input,
215 bool ignore_ws)
216 {
217 return base32_decode(output, input.data(), input.length(), ignore_ws);
218 }
219
220 secure_vector<uint8_t> base32_decode(const char input[],
221 size_t input_length,
222 bool ignore_ws)
223 {
224 return base_decode_to_vec<secure_vector<uint8_t>>(Base32(), input, input_length, ignore_ws);
225 }
226
227 secure_vector<uint8_t> base32_decode(const std::string& input,
228 bool ignore_ws)
229 {
230 return base32_decode(input.data(), input.size(), ignore_ws);
231 }
232
233}
BOTAN_DEBUG_ASSERT
#define BOTAN_DEBUG_ASSERT(expr)
Definition: assert.h:123
Botan::CT::Mask::is_equal
static Mask< T > is_equal(T x, T y)
Definition: ct_utils.h:149
Botan::CT::Mask::is_any_of
static Mask< T > is_any_of(T v, std::initializer_list< T > accepted)
Definition: ct_utils.h:196
Botan::CT::Mask::is_lt
static Mask< T > is_lt(T x, T y)
Definition: ct_utils.h:157
Botan::CT::Mask::is_within_range
static Mask< T > is_within_range(T v, T l, T u)
Definition: ct_utils.h:186
name
std::string name
Definition: commoncrypto_hash.cpp:24
final
int(* final)(unsigned char *, CTX *)
Definition: commoncrypto_hash.cpp:29
Botan::PEM_Code::encode
std::string encode(const uint8_t der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:43
Botan::PEM_Code::decode
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
Definition: pem.cpp:68
Botan
Definition: alg_id.cpp:13
Botan::base_encode
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
Botan::base32_decode
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:195
Botan::base_decode
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
Botan::base_encode_to_string
std::string base_encode_to_string(Base &&base, const uint8_t input[], size_t input_length)
Definition: codec_base.h:86
Botan::base32_encode
size_t base32_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition: base32.cpp:180
Botan::secure_vector
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
Botan::round_up
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:21
Botan::base_decode_full
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