diff options
Diffstat (limited to 'comm/third_party/botan/src/lib/codec')
-rw-r--r-- | comm/third_party/botan/src/lib/codec/base32/base32.cpp | 233 | ||||
-rw-r--r-- | comm/third_party/botan/src/lib/codec/base32/base32.h | 127 | ||||
-rw-r--r-- | comm/third_party/botan/src/lib/codec/base32/info.txt | 3 | ||||
-rw-r--r-- | comm/third_party/botan/src/lib/codec/base58/base58.cpp | 189 | ||||
-rw-r--r-- | comm/third_party/botan/src/lib/codec/base58/base58.h | 76 | ||||
-rw-r--r-- | comm/third_party/botan/src/lib/codec/base58/info.txt | 8 | ||||
-rw-r--r-- | comm/third_party/botan/src/lib/codec/base64/base64.cpp | 248 | ||||
-rw-r--r-- | comm/third_party/botan/src/lib/codec/base64/base64.h | 141 | ||||
-rw-r--r-- | comm/third_party/botan/src/lib/codec/base64/info.txt | 3 | ||||
-rw-r--r-- | comm/third_party/botan/src/lib/codec/hex/hex.cpp | 210 | ||||
-rw-r--r-- | comm/third_party/botan/src/lib/codec/hex/hex.h | 148 | ||||
-rw-r--r-- | comm/third_party/botan/src/lib/codec/hex/info.txt | 3 |
12 files changed, 1389 insertions, 0 deletions
diff --git a/comm/third_party/botan/src/lib/codec/base32/base32.cpp b/comm/third_party/botan/src/lib/codec/base32/base32.cpp new file mode 100644 index 0000000000..224dae9916 --- /dev/null +++ b/comm/third_party/botan/src/lib/codec/base32/base32.cpp @@ -0,0 +1,233 @@ +/* +* Base32 Encoding and Decoding +* (C) 2018 Erwan Chaussy +* (C) 2018,2020 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include <botan/base32.h> +#include <botan/internal/codec_base.h> +#include <botan/internal/rounding.h> +#include <botan/internal/ct_utils.h> + +namespace Botan { + +namespace { + +class Base32 final + { + public: + static inline std::string name() noexcept + { + return "base32"; + } + + static inline size_t encoding_bytes_in() noexcept + { + return m_encoding_bytes_in; + } + static inline size_t encoding_bytes_out() noexcept + { + return m_encoding_bytes_out; + } + + static inline size_t decoding_bytes_in() noexcept + { + return m_encoding_bytes_out; + } + static inline size_t decoding_bytes_out() noexcept + { + return m_encoding_bytes_in; + } + + static inline size_t bits_consumed() noexcept + { + return m_encoding_bits; + } + static inline size_t remaining_bits_before_padding() noexcept + { + return m_remaining_bits_before_padding; + } + + static inline size_t encode_max_output(size_t input_length) + { + return (round_up(input_length, m_encoding_bytes_in) / m_encoding_bytes_in) * m_encoding_bytes_out; + } + static inline size_t decode_max_output(size_t input_length) + { + return (round_up(input_length, m_encoding_bytes_out) * m_encoding_bytes_in) / m_encoding_bytes_out; + } + + static void encode(char out[8], const uint8_t in[5]) noexcept; + + static uint8_t lookup_binary_value(char input) noexcept; + + static bool check_bad_char(uint8_t bin, char input, bool ignore_ws); + + static void decode(uint8_t* out_ptr, const uint8_t decode_buf[8]) + { + out_ptr[0] = (decode_buf[0] << 3) | (decode_buf[1] >> 2); + out_ptr[1] = (decode_buf[1] << 6) | (decode_buf[2] << 1) | (decode_buf[3] >> 4); + out_ptr[2] = (decode_buf[3] << 4) | (decode_buf[4] >> 1); + out_ptr[3] = (decode_buf[4] << 7) | (decode_buf[5] << 2) | (decode_buf[6] >> 3); + out_ptr[4] = (decode_buf[6] << 5) | decode_buf[7]; + } + + static inline size_t bytes_to_remove(size_t final_truncate) + { + return final_truncate ? (final_truncate / 2) + 1 : 0; + } + + private: + static const size_t m_encoding_bits = 5; + static const size_t m_remaining_bits_before_padding = 6; + + static const size_t m_encoding_bytes_in = 5; + static const size_t m_encoding_bytes_out = 8; + }; + +namespace { + +char lookup_base32_char(uint8_t x) + { + BOTAN_DEBUG_ASSERT(x < 32); + + const auto in_AZ = CT::Mask<uint8_t>::is_lt(x, 26); + + const char c_AZ = 'A' + x; + const char c_27 = '2' + (x - 26); + + return in_AZ.select(c_AZ, c_27); + } + +} + +//static +void Base32::encode(char out[8], const uint8_t in[5]) noexcept + { + const uint8_t b0 = (in[0] & 0xF8) >> 3; + const uint8_t b1 = ((in[0] & 0x07) << 2) | (in[1] >> 6); + const uint8_t b2 = ((in[1] & 0x3E) >> 1); + const uint8_t b3 = ((in[1] & 0x01) << 4) | (in[2] >> 4); + const uint8_t b4 = ((in[2] & 0x0F) << 1) | (in[3] >> 7); + const uint8_t b5 = ((in[3] & 0x7C) >> 2); + const uint8_t b6 = ((in[3] & 0x03) << 3) | (in[4] >> 5); + const uint8_t b7 = in[4] & 0x1F; + + out[0] = lookup_base32_char(b0); + out[1] = lookup_base32_char(b1); + out[2] = lookup_base32_char(b2); + out[3] = lookup_base32_char(b3); + out[4] = lookup_base32_char(b4); + out[5] = lookup_base32_char(b5); + out[6] = lookup_base32_char(b6); + out[7] = lookup_base32_char(b7); + } + +//static +uint8_t Base32::lookup_binary_value(char input) noexcept + { + const uint8_t c = static_cast<uint8_t>(input); + + const auto is_alpha_upper = CT::Mask<uint8_t>::is_within_range(c, uint8_t('A'), uint8_t('Z')); + const auto is_decimal = CT::Mask<uint8_t>::is_within_range(c, uint8_t('2'), uint8_t('7')); + + const auto is_equal = CT::Mask<uint8_t>::is_equal(c, uint8_t('=')); + const auto is_whitespace = CT::Mask<uint8_t>::is_any_of(c, { + uint8_t(' '), uint8_t('\t'), uint8_t('\n'), uint8_t('\r') + }); + + const uint8_t c_upper = c - uint8_t('A'); + const uint8_t c_decim = c - uint8_t('2') + 26; + + uint8_t ret = 0xFF; // default value + + ret = is_alpha_upper.select(c_upper, ret); + ret = is_decimal.select(c_decim, ret); + ret = is_equal.select(0x81, ret); + ret = is_whitespace.select(0x80, ret); + + return ret; + } + +//static +bool Base32::check_bad_char(uint8_t bin, char input, bool ignore_ws) + { + if(bin <= 0x1F) + { + return true; + } + else if(!(bin == 0x81 || (bin == 0x80 && ignore_ws))) + { + std::string bad_char(1, input); + if(bad_char == "\t") + { bad_char = "\\t"; } + else if(bad_char == "\n") + { bad_char = "\\n"; } + else if(bad_char == "\r") + { bad_char = "\\r"; } + + throw Invalid_Argument( + std::string("base32_decode: invalid base32 character '") + + bad_char + "'"); + } + return false; + } + +} + +size_t base32_encode(char out[], + const uint8_t in[], + size_t input_length, + size_t& input_consumed, + bool final_inputs) + { + return base_encode(Base32(), out, in, input_length, input_consumed, final_inputs); + } + +std::string base32_encode(const uint8_t input[], + size_t input_length) + { + return base_encode_to_string(Base32(), input, input_length); + } + +size_t base32_decode(uint8_t out[], + const char in[], + size_t input_length, + size_t& input_consumed, + bool final_inputs, + bool ignore_ws) + { + return base_decode(Base32(), out, in, input_length, input_consumed, final_inputs, ignore_ws); + } + +size_t base32_decode(uint8_t output[], + const char input[], + size_t input_length, + bool ignore_ws) + { + return base_decode_full(Base32(), output, input, input_length, ignore_ws); + } + +size_t base32_decode(uint8_t output[], + const std::string& input, + bool ignore_ws) + { + return base32_decode(output, input.data(), input.length(), ignore_ws); + } + +secure_vector<uint8_t> base32_decode(const char input[], + size_t input_length, + bool ignore_ws) + { + return base_decode_to_vec<secure_vector<uint8_t>>(Base32(), input, input_length, ignore_ws); + } + +secure_vector<uint8_t> base32_decode(const std::string& input, + bool ignore_ws) + { + return base32_decode(input.data(), input.size(), ignore_ws); + } + +} diff --git a/comm/third_party/botan/src/lib/codec/base32/base32.h b/comm/third_party/botan/src/lib/codec/base32/base32.h new file mode 100644 index 0000000000..d2bcb3e6ab --- /dev/null +++ b/comm/third_party/botan/src/lib/codec/base32/base32.h @@ -0,0 +1,127 @@ +/* +* Base32 Encoding and Decoding +* (C) 2018 Erwan Chaussy +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#ifndef BOTAN_BASE32_CODEC_H_ +#define BOTAN_BASE32_CODEC_H_ + +#include <botan/secmem.h> +#include <string> + +namespace Botan { + +/** +* Perform base32 encoding +* @param output an array of at least base32_encode_max_output bytes +* @param input is some binary data +* @param input_length length of input in bytes +* @param input_consumed is an output parameter which says how many +* bytes of input were actually consumed. If less than +* input_length, then the range input[consumed:length] +* should be passed in later along with more input. +* @param final_inputs true iff this is the last input, in which case + padding chars will be applied if needed +* @return number of bytes written to output +*/ +size_t BOTAN_PUBLIC_API(2, 7) base32_encode(char output[], + const uint8_t input[], + size_t input_length, + size_t& input_consumed, + bool final_inputs); + +/** +* Perform base32 encoding +* @param input some input +* @param input_length length of input in bytes +* @return base32 representation of input +*/ +std::string BOTAN_PUBLIC_API(2, 7) base32_encode(const uint8_t input[], + size_t input_length); + +/** +* Perform base32 encoding +* @param input some input +* @return base32 representation of input +*/ +template <typename Alloc> +std::string base32_encode(const std::vector<uint8_t, Alloc>& input) + { + return base32_encode(input.data(), input.size()); + } + +/** +* Perform base32 decoding +* @param output an array of at least base32_decode_max_output bytes +* @param input some base32 input +* @param input_length length of input in bytes +* @param input_consumed is an output parameter which says how many +* bytes of input were actually consumed. If less than +* input_length, then the range input[consumed:length] +* should be passed in later along with more input. +* @param final_inputs true iff this is the last input, in which case + padding is allowed +* @param ignore_ws ignore whitespace on input; if false, throw an + exception if whitespace is encountered +* @return number of bytes written to output +*/ +size_t BOTAN_PUBLIC_API(2, 7) base32_decode(uint8_t output[], + const char input[], + size_t input_length, + size_t& input_consumed, + bool final_inputs, + bool ignore_ws = true); + +/** +* Perform base32 decoding +* @param output an array of at least base32_decode_max_output bytes +* @param input some base32 input +* @param input_length length of input in bytes +* @param ignore_ws ignore whitespace on input; if false, throw an + exception if whitespace is encountered +* @return number of bytes written to output +*/ +size_t BOTAN_PUBLIC_API(2, 7) base32_decode(uint8_t output[], + const char input[], + size_t input_length, + bool ignore_ws = true); + +/** +* Perform base32 decoding +* @param output an array of at least base32_decode_max_output bytes +* @param input some base32 input +* @param ignore_ws ignore whitespace on input; if false, throw an + exception if whitespace is encountered +* @return number of bytes written to output +*/ +size_t BOTAN_PUBLIC_API(2, 7) base32_decode(uint8_t output[], + const std::string& input, + bool ignore_ws = true); + +/** +* Perform base32 decoding +* @param input some base32 input +* @param input_length the length of input in bytes +* @param ignore_ws ignore whitespace on input; if false, throw an + exception if whitespace is encountered +* @return decoded base32 output +*/ +secure_vector<uint8_t> BOTAN_PUBLIC_API(2, 7) base32_decode(const char input[], + size_t input_length, + bool ignore_ws = true); + +/** +* Perform base32 decoding +* @param input some base32 input +* @param ignore_ws ignore whitespace on input; if false, throw an + exception if whitespace is encountered +* @return decoded base32 output +*/ +secure_vector<uint8_t> BOTAN_PUBLIC_API(2, 7) base32_decode(const std::string& input, + bool ignore_ws = true); + +} // namespace Botan + +#endif diff --git a/comm/third_party/botan/src/lib/codec/base32/info.txt b/comm/third_party/botan/src/lib/codec/base32/info.txt new file mode 100644 index 0000000000..8e414c5a33 --- /dev/null +++ b/comm/third_party/botan/src/lib/codec/base32/info.txt @@ -0,0 +1,3 @@ +<defines> +BASE32_CODEC -> 20180418 +</defines> diff --git a/comm/third_party/botan/src/lib/codec/base58/base58.cpp b/comm/third_party/botan/src/lib/codec/base58/base58.cpp new file mode 100644 index 0000000000..a6d509012d --- /dev/null +++ b/comm/third_party/botan/src/lib/codec/base58/base58.cpp @@ -0,0 +1,189 @@ +/* +* (C) 2018,2020 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include <botan/base58.h> +#include <botan/exceptn.h> +#include <botan/bigint.h> +#include <botan/divide.h> +#include <botan/loadstor.h> +#include <botan/internal/ct_utils.h> +#include <botan/hash.h> + +namespace Botan { + +namespace { + +uint32_t sha256_d_checksum(const uint8_t input[], size_t input_length) + { + std::unique_ptr<HashFunction> sha256 = HashFunction::create_or_throw("SHA-256"); + + std::vector<uint8_t> checksum(32); + + sha256->update(input, input_length); + sha256->final(checksum); + + sha256->update(checksum); + sha256->final(checksum); + + return load_be<uint32_t>(checksum.data(), 0); + } + +char lookup_base58_char(uint8_t x) + { + // "123456789 ABCDEFGH JKLMN PQRSTUVWXYZ abcdefghijk mnopqrstuvwxyz" + BOTAN_DEBUG_ASSERT(x < 58); + + const auto is_dec_19 = CT::Mask<uint8_t>::is_lte(x, 8); + const auto is_alpha_AH = CT::Mask<uint8_t>::is_within_range(x, 9, 16); + const auto is_alpha_JN = CT::Mask<uint8_t>::is_within_range(x, 17, 21); + const auto is_alpha_PZ = CT::Mask<uint8_t>::is_within_range(x, 22, 32); + const auto is_alpha_ak = CT::Mask<uint8_t>::is_within_range(x, 33, 43); + // otherwise in 'm'-'z' + + const char c_19 = '1' + x; + const char c_AH = 'A' + (x - 9); + const char c_JN = 'J' + (x - 17); + const char c_PZ = 'P' + (x - 22); + const char c_ak = 'a' + (x - 33); + const char c_mz = 'm' + (x - 44); + + char ret = c_mz; + ret = is_dec_19.select(c_19, ret); + ret = is_alpha_AH.select(c_AH, ret); + ret = is_alpha_JN.select(c_JN, ret); + ret = is_alpha_PZ.select(c_PZ, ret); + ret = is_alpha_ak.select(c_ak, ret); + + return ret; + } + +std::string base58_encode(BigInt v, size_t leading_zeros) + { + const uint8_t radix = 58; + + std::string result; + BigInt q; + + while(v.is_nonzero()) + { + uint8_t r; + ct_divide_u8(v, radix, q, r); + result.push_back(lookup_base58_char(r)); + v.swap(q); + } + + for(size_t i = 0; i != leading_zeros; ++i) + result.push_back('1'); // 'zero' byte + + return std::string(result.rbegin(), result.rend()); + } + +template<typename T, typename Z> +size_t count_leading_zeros(const T input[], size_t input_length, Z zero) + { + size_t leading_zeros = 0; + + while(leading_zeros < input_length && input[leading_zeros] == zero) + leading_zeros += 1; + + return leading_zeros; + } + +uint8_t base58_value_of(char input) + { + // "123456789 ABCDEFGH JKLMN PQRSTUVWXYZ abcdefghijk mnopqrstuvwxyz" + + const uint8_t c = static_cast<uint8_t>(input); + + const auto is_dec_19 = CT::Mask<uint8_t>::is_within_range(c, uint8_t('1'), uint8_t('9')); + const auto is_alpha_AH = CT::Mask<uint8_t>::is_within_range(c, uint8_t('A'), uint8_t('H')); + const auto is_alpha_JN = CT::Mask<uint8_t>::is_within_range(c, uint8_t('J'), uint8_t('N')); + const auto is_alpha_PZ = CT::Mask<uint8_t>::is_within_range(c, uint8_t('P'), uint8_t('Z')); + + const auto is_alpha_ak = CT::Mask<uint8_t>::is_within_range(c, uint8_t('a'), uint8_t('k')); + const auto is_alpha_mz = CT::Mask<uint8_t>::is_within_range(c, uint8_t('m'), uint8_t('z')); + + const uint8_t c_dec_19 = c - uint8_t('1'); + const uint8_t c_AH = c - uint8_t('A') + 9; + const uint8_t c_JN = c - uint8_t('J') + 17; + const uint8_t c_PZ = c - uint8_t('P') + 22; + + const uint8_t c_ak = c - uint8_t('a') + 33; + const uint8_t c_mz = c - uint8_t('m') + 44; + + uint8_t ret = 0xFF; // default value + + ret = is_dec_19.select(c_dec_19, ret); + ret = is_alpha_AH.select(c_AH, ret); + ret = is_alpha_JN.select(c_JN, ret); + ret = is_alpha_PZ.select(c_PZ, ret); + ret = is_alpha_ak.select(c_ak, ret); + ret = is_alpha_mz.select(c_mz, ret); + return ret; + } + +} + +std::string base58_encode(const uint8_t input[], size_t input_length) + { + BigInt v(input, input_length); + return base58_encode(v, count_leading_zeros(input, input_length, 0)); + } + +std::string base58_check_encode(const uint8_t input[], size_t input_length) + { + BigInt v(input, input_length); + v <<= 32; + v += sha256_d_checksum(input, input_length); + return base58_encode(v, count_leading_zeros(input, input_length, 0)); + } + +std::vector<uint8_t> base58_decode(const char input[], size_t input_length) + { + const size_t leading_zeros = count_leading_zeros(input, input_length, '1'); + + BigInt v; + + for(size_t i = leading_zeros; i != input_length; ++i) + { + const char c = input[i]; + + if(c == ' ' || c == '\n') + continue; + + const uint8_t idx = base58_value_of(c); + + if(idx == 0xFF) + throw Decoding_Error("Invalid base58"); + + v *= 58; + v += idx; + } + + std::vector<uint8_t> output(v.bytes() + leading_zeros); + v.binary_encode(output.data() + leading_zeros); + return output; + } + +std::vector<uint8_t> base58_check_decode(const char input[], size_t input_length) + { + std::vector<uint8_t> dec = base58_decode(input, input_length); + + if(dec.size() < 4) + throw Decoding_Error("Invalid base58 too short for checksum"); + + const uint32_t computed_checksum = sha256_d_checksum(dec.data(), dec.size() - 4); + const uint32_t checksum = load_be<uint32_t>(&dec[dec.size()-4], 0); + + if(checksum != computed_checksum) + throw Decoding_Error("Invalid base58 checksum"); + + dec.resize(dec.size() - 4); + + return dec; + } + +} diff --git a/comm/third_party/botan/src/lib/codec/base58/base58.h b/comm/third_party/botan/src/lib/codec/base58/base58.h new file mode 100644 index 0000000000..4654a0557c --- /dev/null +++ b/comm/third_party/botan/src/lib/codec/base58/base58.h @@ -0,0 +1,76 @@ +/* +* (C) 2018 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#ifndef BOTAN_BASE58_CODEC_H_ +#define BOTAN_BASE58_CODEC_H_ + +#include <botan/secmem.h> +#include <vector> +#include <string> + +namespace Botan { + +/** +* Perform base58 encoding +* +* This is raw base58 encoding, without the checksum +*/ +std::string +BOTAN_PUBLIC_API(2,9) base58_encode(const uint8_t input[], + size_t input_length); + +/** +* Perform base58 encoding with checksum +*/ +std::string +BOTAN_PUBLIC_API(2,9) base58_check_encode(const uint8_t input[], + size_t input_length); + + +/** +* Perform base58 decoding +* +* This is raw base58 encoding, without the checksum +*/ +std::vector<uint8_t> +BOTAN_PUBLIC_API(2,9) base58_decode(const char input[], + size_t input_length); + +/** +* Perform base58 decoding with checksum +*/ +std::vector<uint8_t> +BOTAN_PUBLIC_API(2,9) base58_check_decode(const char input[], + size_t input_length); + + +// Some convenience wrappers: + +template<typename Alloc> +inline std::string base58_encode(const std::vector<uint8_t, Alloc>& vec) + { + return base58_encode(vec.data(), vec.size()); + } + +template<typename Alloc> +inline std::string base58_check_encode(const std::vector<uint8_t, Alloc>& vec) + { + return base58_check_encode(vec.data(), vec.size()); + } + +inline std::vector<uint8_t> base58_decode(const std::string& s) + { + return base58_decode(s.data(), s.size()); + } + +inline std::vector<uint8_t> base58_check_decode(const std::string& s) + { + return base58_check_decode(s.data(), s.size()); + } + +} + +#endif diff --git a/comm/third_party/botan/src/lib/codec/base58/info.txt b/comm/third_party/botan/src/lib/codec/base58/info.txt new file mode 100644 index 0000000000..0b09c016d0 --- /dev/null +++ b/comm/third_party/botan/src/lib/codec/base58/info.txt @@ -0,0 +1,8 @@ +<defines> +BASE58_CODEC -> 20181209 +</defines> + +<requires> +sha2_32 +bigint +</requires> diff --git a/comm/third_party/botan/src/lib/codec/base64/base64.cpp b/comm/third_party/botan/src/lib/codec/base64/base64.cpp new file mode 100644 index 0000000000..93675f0e34 --- /dev/null +++ b/comm/third_party/botan/src/lib/codec/base64/base64.cpp @@ -0,0 +1,248 @@ +/* +* Base64 Encoding and Decoding +* (C) 2010,2015,2020 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include <botan/base64.h> +#include <botan/internal/codec_base.h> +#include <botan/exceptn.h> +#include <botan/internal/rounding.h> +#include <botan/internal/ct_utils.h> + +namespace Botan { + +namespace { + +class Base64 final + { + public: + static inline std::string name() noexcept + { + return "base64"; + } + + static inline size_t encoding_bytes_in() noexcept + { + return m_encoding_bytes_in; + } + static inline size_t encoding_bytes_out() noexcept + { + return m_encoding_bytes_out; + } + + static inline size_t decoding_bytes_in() noexcept + { + return m_encoding_bytes_out; + } + static inline size_t decoding_bytes_out() noexcept + { + return m_encoding_bytes_in; + } + + static inline size_t bits_consumed() noexcept + { + return m_encoding_bits; + } + static inline size_t remaining_bits_before_padding() noexcept + { + return m_remaining_bits_before_padding; + } + + static inline size_t encode_max_output(size_t input_length) + { + return (round_up(input_length, m_encoding_bytes_in) / m_encoding_bytes_in) * m_encoding_bytes_out; + } + static inline size_t decode_max_output(size_t input_length) + { + return (round_up(input_length, m_encoding_bytes_out) * m_encoding_bytes_in) / m_encoding_bytes_out; + } + + static void encode(char out[8], const uint8_t in[5]) noexcept; + + static uint8_t lookup_binary_value(char input) noexcept; + + static bool check_bad_char(uint8_t bin, char input, bool ignore_ws); + + static void decode(uint8_t* out_ptr, const uint8_t decode_buf[4]) + { + out_ptr[0] = (decode_buf[0] << 2) | (decode_buf[1] >> 4); + out_ptr[1] = (decode_buf[1] << 4) | (decode_buf[2] >> 2); + out_ptr[2] = (decode_buf[2] << 6) | decode_buf[3]; + } + + static inline size_t bytes_to_remove(size_t final_truncate) + { + return final_truncate; + } + + private: + static const size_t m_encoding_bits = 6; + static const size_t m_remaining_bits_before_padding = 8; + + static const size_t m_encoding_bytes_in = 3; + static const size_t m_encoding_bytes_out = 4; + }; + +char lookup_base64_char(uint8_t x) + { + BOTAN_DEBUG_ASSERT(x < 64); + + const auto in_az = CT::Mask<uint8_t>::is_within_range(x, 26, 51); + const auto in_09 = CT::Mask<uint8_t>::is_within_range(x, 52, 61); + const auto eq_plus = CT::Mask<uint8_t>::is_equal(x, 62); + const auto eq_slash = CT::Mask<uint8_t>::is_equal(x, 63); + + const char c_AZ = 'A' + x; + const char c_az = 'a' + (x - 26); + const char c_09 = '0' + (x - 2*26); + const char c_plus = '+'; + const char c_slash = '/'; + + char ret = c_AZ; + ret = in_az.select(c_az, ret); + ret = in_09.select(c_09, ret); + ret = eq_plus.select(c_plus, ret); + ret = eq_slash.select(c_slash, ret); + + return ret; + } + +//static +void Base64::encode(char out[8], const uint8_t in[5]) noexcept + { + const uint8_t b0 = (in[0] & 0xFC) >> 2; + const uint8_t b1 = ((in[0] & 0x03) << 4) | (in[1] >> 4); + const uint8_t b2 = ((in[1] & 0x0F) << 2) | (in[2] >> 6); + const uint8_t b3 = in[2] & 0x3F; + out[0] = lookup_base64_char(b0); + out[1] = lookup_base64_char(b1); + out[2] = lookup_base64_char(b2); + out[3] = lookup_base64_char(b3); + } + +//static +uint8_t Base64::lookup_binary_value(char input) noexcept + { + const uint8_t c = static_cast<uint8_t>(input); + + const auto is_alpha_upper = CT::Mask<uint8_t>::is_within_range(c, uint8_t('A'), uint8_t('Z')); + const auto is_alpha_lower = CT::Mask<uint8_t>::is_within_range(c, uint8_t('a'), uint8_t('z')); + const auto is_decimal = CT::Mask<uint8_t>::is_within_range(c, uint8_t('0'), uint8_t('9')); + + const auto is_plus = CT::Mask<uint8_t>::is_equal(c, uint8_t('+')); + const auto is_slash = CT::Mask<uint8_t>::is_equal(c, uint8_t('/')); + const auto is_equal = CT::Mask<uint8_t>::is_equal(c, uint8_t('=')); + + const auto is_whitespace = CT::Mask<uint8_t>::is_any_of(c, { + uint8_t(' '), uint8_t('\t'), uint8_t('\n'), uint8_t('\r') + }); + + const uint8_t c_upper = c - uint8_t('A'); + const uint8_t c_lower = c - uint8_t('a') + 26; + const uint8_t c_decim = c - uint8_t('0') + 2*26; + + uint8_t ret = 0xFF; // default value + + ret = is_alpha_upper.select(c_upper, ret); + ret = is_alpha_lower.select(c_lower, ret); + ret = is_decimal.select(c_decim, ret); + ret = is_plus.select(62, ret); + ret = is_slash.select(63, ret); + ret = is_equal.select(0x81, ret); + ret = is_whitespace.select(0x80, ret); + + return ret; + } + +//static +bool Base64::check_bad_char(uint8_t bin, char input, bool ignore_ws) + { + if(bin <= 0x3F) + { + return true; + } + else if(!(bin == 0x81 || (bin == 0x80 && ignore_ws))) + { + std::string bad_char(1, input); + if(bad_char == "\t") + { bad_char = "\\t"; } + else if(bad_char == "\n") + { bad_char = "\\n"; } + else if(bad_char == "\r") + { bad_char = "\\r"; } + + throw Invalid_Argument( + std::string("base64_decode: invalid base64 character '") + + bad_char + "'"); + } + return false; + } + +} + +size_t base64_encode(char out[], + const uint8_t in[], + size_t input_length, + size_t& input_consumed, + bool final_inputs) + { + return base_encode(Base64(), out, in, input_length, input_consumed, final_inputs); + } + +std::string base64_encode(const uint8_t input[], + size_t input_length) + { + return base_encode_to_string(Base64(), input, input_length); + } + +size_t base64_decode(uint8_t out[], + const char in[], + size_t input_length, + size_t& input_consumed, + bool final_inputs, + bool ignore_ws) + { + return base_decode(Base64(), out, in, input_length, input_consumed, final_inputs, ignore_ws); + } + +size_t base64_decode(uint8_t output[], + const char input[], + size_t input_length, + bool ignore_ws) + { + return base_decode_full(Base64(), output, input, input_length, ignore_ws); + } + +size_t base64_decode(uint8_t output[], + const std::string& input, + bool ignore_ws) + { + return base64_decode(output, input.data(), input.length(), ignore_ws); + } + +secure_vector<uint8_t> base64_decode(const char input[], + size_t input_length, + bool ignore_ws) + { + return base_decode_to_vec<secure_vector<uint8_t>>(Base64(), input, input_length, ignore_ws); + } + +secure_vector<uint8_t> base64_decode(const std::string& input, + bool ignore_ws) + { + return base64_decode(input.data(), input.size(), ignore_ws); + } + +size_t base64_encode_max_output(size_t input_length) + { + return Base64::encode_max_output(input_length); + } + +size_t base64_decode_max_output(size_t input_length) + { + return Base64::decode_max_output(input_length); + } + +} diff --git a/comm/third_party/botan/src/lib/codec/base64/base64.h b/comm/third_party/botan/src/lib/codec/base64/base64.h new file mode 100644 index 0000000000..a20d03b0f7 --- /dev/null +++ b/comm/third_party/botan/src/lib/codec/base64/base64.h @@ -0,0 +1,141 @@ +/* +* Base64 Encoding and Decoding +* (C) 2010 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#ifndef BOTAN_BASE64_CODEC_H_ +#define BOTAN_BASE64_CODEC_H_ + +#include <botan/secmem.h> +#include <string> + +namespace Botan { + +/** +* Perform base64 encoding +* @param output an array of at least base64_encode_max_output bytes +* @param input is some binary data +* @param input_length length of input in bytes +* @param input_consumed is an output parameter which says how many +* bytes of input were actually consumed. If less than +* input_length, then the range input[consumed:length] +* should be passed in later along with more input. +* @param final_inputs true iff this is the last input, in which case + padding chars will be applied if needed +* @return number of bytes written to output +*/ +size_t BOTAN_PUBLIC_API(2,0) base64_encode(char output[], + const uint8_t input[], + size_t input_length, + size_t& input_consumed, + bool final_inputs); + +/** +* Perform base64 encoding +* @param input some input +* @param input_length length of input in bytes +* @return base64adecimal representation of input +*/ +std::string BOTAN_PUBLIC_API(2,0) base64_encode(const uint8_t input[], + size_t input_length); + +/** +* Perform base64 encoding +* @param input some input +* @return base64adecimal representation of input +*/ +template<typename Alloc> +std::string base64_encode(const std::vector<uint8_t, Alloc>& input) + { + return base64_encode(input.data(), input.size()); + } + +/** +* Perform base64 decoding +* @param output an array of at least base64_decode_max_output bytes +* @param input some base64 input +* @param input_length length of input in bytes +* @param input_consumed is an output parameter which says how many +* bytes of input were actually consumed. If less than +* input_length, then the range input[consumed:length] +* should be passed in later along with more input. +* @param final_inputs true iff this is the last input, in which case + padding is allowed +* @param ignore_ws ignore whitespace on input; if false, throw an + exception if whitespace is encountered +* @return number of bytes written to output +*/ +size_t BOTAN_PUBLIC_API(2,0) base64_decode(uint8_t output[], + const char input[], + size_t input_length, + size_t& input_consumed, + bool final_inputs, + bool ignore_ws = true); + +/** +* Perform base64 decoding +* @param output an array of at least base64_decode_max_output bytes +* @param input some base64 input +* @param input_length length of input in bytes +* @param ignore_ws ignore whitespace on input; if false, throw an + exception if whitespace is encountered +* @return number of bytes written to output +*/ +size_t BOTAN_PUBLIC_API(2,0) base64_decode(uint8_t output[], + const char input[], + size_t input_length, + bool ignore_ws = true); + +/** +* Perform base64 decoding +* @param output an array of at least base64_decode_max_output bytes +* @param input some base64 input +* @param ignore_ws ignore whitespace on input; if false, throw an + exception if whitespace is encountered +* @return number of bytes written to output +*/ +size_t BOTAN_PUBLIC_API(2,0) base64_decode(uint8_t output[], + const std::string& input, + bool ignore_ws = true); + +/** +* Perform base64 decoding +* @param input some base64 input +* @param input_length the length of input in bytes +* @param ignore_ws ignore whitespace on input; if false, throw an + exception if whitespace is encountered +* @return decoded base64 output +*/ +secure_vector<uint8_t> BOTAN_PUBLIC_API(2,0) base64_decode(const char input[], + size_t input_length, + bool ignore_ws = true); + +/** +* Perform base64 decoding +* @param input some base64 input +* @param ignore_ws ignore whitespace on input; if false, throw an + exception if whitespace is encountered +* @return decoded base64 output +*/ +secure_vector<uint8_t> BOTAN_PUBLIC_API(2,0) base64_decode(const std::string& input, + bool ignore_ws = true); + +/** +* Calculate the size of output buffer for base64_encode +* @param input_length the length of input in bytes +* @return the size of output buffer in bytes +*/ +size_t BOTAN_PUBLIC_API(2,1) base64_encode_max_output(size_t input_length); + +/** +* Calculate the size of output buffer for base64_decode +* @param input_length the length of input in bytes +* @return the size of output buffer in bytes +*/ +size_t BOTAN_PUBLIC_API(2,1) base64_decode_max_output(size_t input_length); + +} + +#endif diff --git a/comm/third_party/botan/src/lib/codec/base64/info.txt b/comm/third_party/botan/src/lib/codec/base64/info.txt new file mode 100644 index 0000000000..ceed636053 --- /dev/null +++ b/comm/third_party/botan/src/lib/codec/base64/info.txt @@ -0,0 +1,3 @@ +<defines> +BASE64_CODEC -> 20131128 +</defines> diff --git a/comm/third_party/botan/src/lib/codec/hex/hex.cpp b/comm/third_party/botan/src/lib/codec/hex/hex.cpp new file mode 100644 index 0000000000..1ae21f398b --- /dev/null +++ b/comm/third_party/botan/src/lib/codec/hex/hex.cpp @@ -0,0 +1,210 @@ +/* +* Hex Encoding and Decoding +* (C) 2010,2020 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include <botan/hex.h> +#include <botan/mem_ops.h> +#include <botan/exceptn.h> +#include <botan/internal/ct_utils.h> + +namespace Botan { + +namespace { + +char hex_encode_nibble(uint8_t n, bool uppercase) + { + BOTAN_DEBUG_ASSERT(n <= 15); + + const auto in_09 = CT::Mask<uint8_t>::is_lt(n, 10); + + const char c_09 = n + '0'; + const char c_af = n + (uppercase ? 'A' : 'a') - 10; + + return in_09.select(c_09, c_af); + } + +} + +void hex_encode(char output[], + const uint8_t input[], + size_t input_length, + bool uppercase) + { + for(size_t i = 0; i != input_length; ++i) + { + const uint8_t n0 = (input[i] >> 4) & 0xF; + const uint8_t n1 = (input[i] ) & 0xF; + + output[2*i ] = hex_encode_nibble(n0, uppercase); + output[2*i+1] = hex_encode_nibble(n1, uppercase); + } + } + +std::string hex_encode(const uint8_t input[], + size_t input_length, + bool uppercase) + { + std::string output(2 * input_length, 0); + + if(input_length) + hex_encode(&output.front(), input, input_length, uppercase); + + return output; + } + +namespace { + +uint8_t hex_char_to_bin(char input) + { + const uint8_t c = static_cast<uint8_t>(input); + + const auto is_alpha_upper = CT::Mask<uint8_t>::is_within_range(c, uint8_t('A'), uint8_t('F')); + const auto is_alpha_lower = CT::Mask<uint8_t>::is_within_range(c, uint8_t('a'), uint8_t('f')); + const auto is_decimal = CT::Mask<uint8_t>::is_within_range(c, uint8_t('0'), uint8_t('9')); + + const auto is_whitespace = CT::Mask<uint8_t>::is_any_of(c, { + uint8_t(' '), uint8_t('\t'), uint8_t('\n'), uint8_t('\r') + }); + + const uint8_t c_upper = c - uint8_t('A') + 10; + const uint8_t c_lower = c - uint8_t('a') + 10; + const uint8_t c_decim = c - uint8_t('0'); + + uint8_t ret = 0xFF; // default value + + ret = is_alpha_upper.select(c_upper, ret); + ret = is_alpha_lower.select(c_lower, ret); + ret = is_decimal.select(c_decim, ret); + ret = is_whitespace.select(0x80, ret); + + return ret; + } + +} + + +size_t hex_decode(uint8_t output[], + const char input[], + size_t input_length, + size_t& input_consumed, + bool ignore_ws) + { + uint8_t* out_ptr = output; + bool top_nibble = true; + + clear_mem(output, input_length / 2); + + for(size_t i = 0; i != input_length; ++i) + { + const uint8_t bin = hex_char_to_bin(input[i]); + + if(bin >= 0x10) + { + if(bin == 0x80 && ignore_ws) + continue; + + std::string bad_char(1, input[i]); + if(bad_char == "\t") + bad_char = "\\t"; + else if(bad_char == "\n") + bad_char = "\\n"; + + throw Invalid_Argument( + std::string("hex_decode: invalid hex character '") + + bad_char + "'"); + } + + if(top_nibble) + *out_ptr |= bin << 4; + else + *out_ptr |= bin; + + top_nibble = !top_nibble; + if(top_nibble) + ++out_ptr; + } + + input_consumed = input_length; + size_t written = (out_ptr - output); + + /* + * We only got half of a uint8_t at the end; zap the half-written + * output and mark it as unread + */ + if(!top_nibble) + { + *out_ptr = 0; + input_consumed -= 1; + } + + return written; + } + +size_t hex_decode(uint8_t output[], + const char input[], + size_t input_length, + bool ignore_ws) + { + size_t consumed = 0; + size_t written = hex_decode(output, input, input_length, + consumed, ignore_ws); + + if(consumed != input_length) + throw Invalid_Argument("hex_decode: input did not have full bytes"); + + return written; + } + +size_t hex_decode(uint8_t output[], + const std::string& input, + bool ignore_ws) + { + return hex_decode(output, input.data(), input.length(), ignore_ws); + } + +secure_vector<uint8_t> hex_decode_locked(const char input[], + size_t input_length, + bool ignore_ws) + { + secure_vector<uint8_t> bin(1 + input_length / 2); + + size_t written = hex_decode(bin.data(), + input, + input_length, + ignore_ws); + + bin.resize(written); + return bin; + } + +secure_vector<uint8_t> hex_decode_locked(const std::string& input, + bool ignore_ws) + { + return hex_decode_locked(input.data(), input.size(), ignore_ws); + } + +std::vector<uint8_t> hex_decode(const char input[], + size_t input_length, + bool ignore_ws) + { + std::vector<uint8_t> bin(1 + input_length / 2); + + size_t written = hex_decode(bin.data(), + input, + input_length, + ignore_ws); + + bin.resize(written); + return bin; + } + +std::vector<uint8_t> hex_decode(const std::string& input, + bool ignore_ws) + { + return hex_decode(input.data(), input.size(), ignore_ws); + } + +} diff --git a/comm/third_party/botan/src/lib/codec/hex/hex.h b/comm/third_party/botan/src/lib/codec/hex/hex.h new file mode 100644 index 0000000000..330d8a69a5 --- /dev/null +++ b/comm/third_party/botan/src/lib/codec/hex/hex.h @@ -0,0 +1,148 @@ +/* +* Hex Encoding and Decoding +* (C) 2010 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#ifndef BOTAN_HEX_CODEC_H_ +#define BOTAN_HEX_CODEC_H_ + +#include <botan/secmem.h> +#include <string> + +namespace Botan { + +/** +* Perform hex encoding +* @param output an array of at least input_length*2 bytes +* @param input is some binary data +* @param input_length length of input in bytes +* @param uppercase should output be upper or lower case? +*/ +void BOTAN_PUBLIC_API(2,0) hex_encode(char output[], + const uint8_t input[], + size_t input_length, + bool uppercase = true); + +/** +* Perform hex encoding +* @param input some input +* @param input_length length of input in bytes +* @param uppercase should output be upper or lower case? +* @return hexadecimal representation of input +*/ +std::string BOTAN_PUBLIC_API(2,0) hex_encode(const uint8_t input[], + size_t input_length, + bool uppercase = true); + +/** +* Perform hex encoding +* @param input some input +* @param uppercase should output be upper or lower case? +* @return hexadecimal representation of input +*/ +template<typename Alloc> +std::string hex_encode(const std::vector<uint8_t, Alloc>& input, + bool uppercase = true) + { + return hex_encode(input.data(), input.size(), uppercase); + } + +/** +* Perform hex decoding +* @param output an array of at least input_length/2 bytes +* @param input some hex input +* @param input_length length of input in bytes +* @param input_consumed is an output parameter which says how many +* bytes of input were actually consumed. If less than +* input_length, then the range input[consumed:length] +* should be passed in later along with more input. +* @param ignore_ws ignore whitespace on input; if false, throw an + exception if whitespace is encountered +* @return number of bytes written to output +*/ +size_t BOTAN_PUBLIC_API(2,0) hex_decode(uint8_t output[], + const char input[], + size_t input_length, + size_t& input_consumed, + bool ignore_ws = true); + +/** +* Perform hex decoding +* @param output an array of at least input_length/2 bytes +* @param input some hex input +* @param input_length length of input in bytes +* @param ignore_ws ignore whitespace on input; if false, throw an + exception if whitespace is encountered +* @return number of bytes written to output +*/ +size_t BOTAN_PUBLIC_API(2,0) hex_decode(uint8_t output[], + const char input[], + size_t input_length, + bool ignore_ws = true); + +/** +* Perform hex decoding +* @param output an array of at least input_length/2 bytes +* @param input some hex input +* @param ignore_ws ignore whitespace on input; if false, throw an + exception if whitespace is encountered +* @return number of bytes written to output +*/ +size_t BOTAN_PUBLIC_API(2,0) hex_decode(uint8_t output[], + const std::string& input, + bool ignore_ws = true); + +/** +* Perform hex decoding +* @param input some hex input +* @param input_length the length of input in bytes +* @param ignore_ws ignore whitespace on input; if false, throw an + exception if whitespace is encountered +* @return decoded hex output +*/ +std::vector<uint8_t> BOTAN_PUBLIC_API(2,0) +hex_decode(const char input[], + size_t input_length, + bool ignore_ws = true); + +/** +* Perform hex decoding +* @param input some hex input +* @param ignore_ws ignore whitespace on input; if false, throw an + exception if whitespace is encountered +* @return decoded hex output +*/ +std::vector<uint8_t> BOTAN_PUBLIC_API(2,0) +hex_decode(const std::string& input, + bool ignore_ws = true); + + +/** +* Perform hex decoding +* @param input some hex input +* @param input_length the length of input in bytes +* @param ignore_ws ignore whitespace on input; if false, throw an + exception if whitespace is encountered +* @return decoded hex output +*/ +secure_vector<uint8_t> BOTAN_PUBLIC_API(2,0) +hex_decode_locked(const char input[], + size_t input_length, + bool ignore_ws = true); + +/** +* Perform hex decoding +* @param input some hex input +* @param ignore_ws ignore whitespace on input; if false, throw an + exception if whitespace is encountered +* @return decoded hex output +*/ +secure_vector<uint8_t> BOTAN_PUBLIC_API(2,0) +hex_decode_locked(const std::string& input, + bool ignore_ws = true); + +} + +#endif diff --git a/comm/third_party/botan/src/lib/codec/hex/info.txt b/comm/third_party/botan/src/lib/codec/hex/info.txt new file mode 100644 index 0000000000..6ee27e57c2 --- /dev/null +++ b/comm/third_party/botan/src/lib/codec/hex/info.txt @@ -0,0 +1,3 @@ +<defines> +HEX_CODEC -> 20131128 +</defines> |