diff options
author | Jack Lloyd <[email protected]> | 2018-08-14 21:42:42 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-08-14 21:52:24 -0400 |
commit | 759d55886dcc1f5351c22e568c87b3a30644e055 (patch) | |
tree | 64fd9b69344152fa618983045bce7dd1b519c80b /src/lib/ffi/ffi_mp.cpp | |
parent | b100a4f1de538e120413d72f09fd2ab9e43d81b5 (diff) |
Cleanup of BigInt encoding/decoding functions
Instigated by finding a bug where BigInt::encode with decimal output
would often have a leading '0' char. Which is papered over in the IO
operator, but was exposed by botan_mp_to_str which called BigInt::encode
directly.
Split BigInt::encode/decode into two versions, one taking the Base
argument and the other using the (previously default) binary base.
With a view of eventually deprecating the versions taking a base.
Add BigInt::to_dec_string() and BigInt::to_hex_string()
Diffstat (limited to 'src/lib/ffi/ffi_mp.cpp')
-rw-r--r-- | src/lib/ffi/ffi_mp.cpp | 19 |
1 files changed, 7 insertions, 12 deletions
diff --git a/src/lib/ffi/ffi_mp.cpp b/src/lib/ffi/ffi_mp.cpp index 17d5d19c7..c7854ce9a 100644 --- a/src/lib/ffi/ffi_mp.cpp +++ b/src/lib/ffi/ffi_mp.cpp @@ -67,7 +67,7 @@ int botan_mp_set_from_radix_str(botan_mp_t mp, const char* str, size_t radix) const uint8_t* bytes = Botan::cast_char_ptr_to_uint8(str); const size_t len = strlen(str); - bn = Botan::BigInt::decode(bytes, len, base); + bn = Botan::BigInt(bytes, len, base); }); } @@ -99,26 +99,21 @@ int botan_mp_from_bin(botan_mp_t mp, const uint8_t bin[], size_t bin_len) int botan_mp_to_hex(const botan_mp_t mp, char* out) { return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { - std::vector<uint8_t> hex = Botan::BigInt::encode(bn, Botan::BigInt::Hexadecimal); - std::memcpy(out, hex.data(), hex.size()); - out[hex.size()] = 0; // null terminate + const std::string hex = bn.to_hex_string(); + std::strcpy(out, hex.c_str()); }); } int botan_mp_to_str(const botan_mp_t mp, uint8_t digit_base, char* out, size_t* out_len) { return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { - Botan::BigInt::Base base; + if(digit_base == 0 || digit_base == 10) - base = Botan::BigInt::Decimal; + return write_str_output(out, out_len, bn.to_dec_string()); else if(digit_base == 16) - base = Botan::BigInt::Hexadecimal; + return write_str_output(out, out_len, bn.to_hex_string()); else - throw FFI_Error("botan_mp_to_str invalid digit base", BOTAN_FFI_ERROR_BAD_PARAMETER); - - std::vector<uint8_t> hex = Botan::BigInt::encode(bn, base); - hex.push_back(0); // null terminator - return write_str_output(out, out_len, hex); + return BOTAN_FFI_ERROR_BAD_PARAMETER; }); } |