9#include <botan/base32.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>
23 static std::string
name() noexcept {
return "base32"; }
25 static size_t encoding_bytes_in() noexcept {
return m_encoding_bytes_in; }
27 static size_t encoding_bytes_out() noexcept {
return m_encoding_bytes_out; }
29 static size_t decoding_bytes_in() noexcept {
return m_encoding_bytes_out; }
31 static size_t decoding_bytes_out() noexcept {
return m_encoding_bytes_in; }
33 static size_t bits_consumed() noexcept {
return m_encoding_bits; }
35 static size_t remaining_bits_before_padding() noexcept {
return m_remaining_bits_before_padding; }
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;
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;
45 static void encode(
char out[8],
const uint8_t in[5])
noexcept;
47 static uint8_t lookup_binary_value(
char input)
noexcept;
49 static bool check_bad_char(uint8_t bin,
char input,
bool ignore_ws);
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];
59 static size_t bytes_to_remove(
size_t final_truncate) {
return final_truncate ? (final_truncate / 2) + 1 : 0; }
62 static const size_t m_encoding_bits = 5;
63 static const size_t m_remaining_bits_before_padding = 6;
65 static const size_t m_encoding_bytes_in = 5;
66 static const size_t m_encoding_bytes_out = 8;
71char lookup_base32_char(uint8_t x) {
76 const char c_AZ =
'A' + x;
77 const char c_27 =
'2' + (x - 26);
79 return in_AZ.select(c_AZ, c_27);
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;
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);
106uint8_t Base32::lookup_binary_value(
char input)
noexcept {
107 const uint8_t c =
static_cast<uint8_t
>(input);
113 const auto is_whitespace =
116 const uint8_t c_upper = c - uint8_t(
'A');
117 const uint8_t c_decim = c - uint8_t(
'2') + 26;
121 ret = is_alpha_upper.select(c_upper, ret);
122 ret = is_decimal.select(c_decim, ret);
124 ret = is_whitespace.select(0x80, ret);
130bool Base32::check_bad_char(uint8_t bin,
char input,
bool ignore_ws) {
133 }
else if(!(bin == 0x81 || (bin == 0x80 && ignore_ws))) {
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);
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);
154size_t base32_decode(uint8_t output[],
const char input[],
size_t input_length,
bool ignore_ws) {
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);
#define BOTAN_DEBUG_ASSERT(expr)
static constexpr Mask< T > is_within_range(T v, T l, T u)
static constexpr Mask< T > is_equal(T x, T y)
static constexpr Mask< T > is_lt(T x, T y)
static constexpr Mask< T > is_any_of(T v, std::initializer_list< T > accepted)
int(* final)(unsigned char *, CTX *)
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
std::string format_char_for_display(char c)
std::string fmt(std::string_view format, const T &... args)
Vector base_decode_to_vec(Base &&base, const char input[], size_t input_length, bool ignore_ws)
size_t base_encode(Base &&base, char output[], const uint8_t input[], size_t input_length, size_t &input_consumed, bool final_inputs)
size_t base32_decode(uint8_t out[], const char in[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws)
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)
std::string base_encode_to_string(Base &&base, const uint8_t input[], size_t input_length)
std::vector< T, secure_allocator< T > > secure_vector
size_t base32_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
size_t round_up(size_t n, size_t align_to)
size_t base_decode_full(Base &&base, uint8_t output[], const char input[], size_t input_length, bool ignore_ws)