diff options
author | Jack Lloyd <[email protected]> | 2017-08-15 16:10:28 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2017-08-15 16:10:28 -0400 |
commit | c917ab6ebdd2f4661eb7045887e3945078f62082 (patch) | |
tree | a5c2e1880aa6fbf9715514655941e468de1b1c77 /src/lib/ffi | |
parent | 3739081a4de5cc70695dd210c38cd64611c53665 (diff) |
Add botan_hex_decode, botan_base64_encode, botan_base64_decode FFI funcs
Diffstat (limited to 'src/lib/ffi')
-rw-r--r-- | src/lib/ffi/ffi.cpp | 32 | ||||
-rw-r--r-- | src/lib/ffi/ffi.h | 23 |
2 files changed, 52 insertions, 3 deletions
diff --git a/src/lib/ffi/ffi.cpp b/src/lib/ffi/ffi.cpp index 28eac401a..1cd8e4b90 100644 --- a/src/lib/ffi/ffi.cpp +++ b/src/lib/ffi/ffi.cpp @@ -9,6 +9,7 @@ #include <botan/version.h> #include <botan/mem_ops.h> #include <botan/hex.h> +#include <botan/base64.h> #include <cstdio> namespace Botan_FFI { @@ -74,5 +75,36 @@ int botan_hex_encode(const uint8_t* in, size_t len, char* out, uint32_t flags) }); } +int botan_hex_decode(const char* hex_str, size_t in_len, uint8_t* out, size_t* out_len) + { + return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() { + const std::vector<uint8_t> bin = Botan::hex_decode(hex_str, in_len); + return Botan_FFI::write_vec_output(out, out_len, bin); + }); + } + +int botan_base64_encode(const uint8_t* in, size_t len, char* out, size_t* out_len) + { + return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() { + const std::string base64 = Botan::base64_encode(in, len); + return Botan_FFI::write_str_output(out, out_len, base64); + }); + } + +int botan_base64_decode(const char* base64_str, size_t in_len, + uint8_t* out, size_t* out_len) + { + return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() { + if(*out_len < Botan::base64_decode_max_output(in_len)) + { + *out_len = Botan::base64_decode_max_output(in_len); + return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE; + } + + *out_len = Botan::base64_decode(out, std::string(base64_str, in_len)); + return BOTAN_FFI_SUCCESS; + }); + } + } diff --git a/src/lib/ffi/ffi.h b/src/lib/ffi/ffi.h index 37f38ae1b..6ee286ad3 100644 --- a/src/lib/ffi/ffi.h +++ b/src/lib/ffi/ffi.h @@ -173,9 +173,26 @@ BOTAN_DLL int botan_scrub_mem(uint8_t* mem, size_t bytes); */ BOTAN_DLL int botan_hex_encode(const uint8_t* x, size_t len, char* out, uint32_t flags); -// TODO: botan_hex_decode -// TODO: botan_base64_encode -// TODO: botan_base64_decode +/** +* Perform hex decoding +* @param hex_str a string of hex chars (whitespace is ignored) +* @param in_len the length of hex_str +* @param out the output buffer should be at least strlen(hex_str)/2 bytes +* @param out_len the size of out +*/ +BOTAN_DLL int botan_hex_decode(const char* hex_str, size_t in_len, uint8_t* out, size_t* out_len); + +/** +* Perform base64 encoding +*/ +BOTAN_DLL int botan_base64_encode(const uint8_t* x, size_t len, char* out, size_t* out_len); + + +/** +* Perform base64 decoding +*/ +BOTAN_DLL int botan_base64_decode(const char* base64_str, size_t in_len, + uint8_t* out, size_t* out_len); /** * RNG type |