diff options
author | Rostyslav Khudolii <[email protected]> | 2022-03-25 13:43:13 +0100 |
---|---|---|
committer | Rostyslav Khudolii <[email protected]> | 2022-03-25 13:46:08 +0100 |
commit | c6425e72f65ee80b67728f4baa1f2c3aec20675b (patch) | |
tree | 6bf6e2926268350d2594dad33308335539348ced /src | |
parent | 29bdb995706a603f2817e99c8907ecbf185c1810 (diff) |
Fix review comments
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/ffi/ffi_srp6.cpp | 247 | ||||
-rwxr-xr-x | src/python/botan2.py | 28 |
2 files changed, 153 insertions, 122 deletions
diff --git a/src/lib/ffi/ffi_srp6.cpp b/src/lib/ffi/ffi_srp6.cpp index 7ae4d2204..c2a91695a 100644 --- a/src/lib/ffi/ffi_srp6.cpp +++ b/src/lib/ffi/ffi_srp6.cpp @@ -4,122 +4,153 @@ * Botan is released under the Simplified BSD License (see license.txt) */ -#include "botan/bigint.h" -#include "botan/ffi.h" -#include "botan/internal/ffi_rng.h" -#include "botan/internal/ffi_util.h" -#include "botan/rng.h" -#include "botan/srp6.h" -#include "botan/symkey.h" +#include <botan/ffi.h> +#include <botan/bigint.h> +#include <botan/internal/ffi_rng.h> +#include <botan/internal/ffi_util.h> +#include <botan/rng.h> +#include <botan/srp6.h> +#include <botan/symkey.h> extern "C" { -using namespace Botan_FFI; + using namespace Botan_FFI; -BOTAN_FFI_DECLARE_STRUCT(botan_srp6_server_session_struct, - Botan::SRP6_Server_Session, 0x44F7425F); + BOTAN_FFI_DECLARE_STRUCT(botan_srp6_server_session_struct, + Botan::SRP6_Server_Session, 0x44F7425F); -int botan_srp6_server_session_init(botan_srp6_server_session_t *srp6) { -return ffi_guard_thunk(__func__, [=]() -> int { -*srp6 = new botan_srp6_server_session_struct( - std::make_unique<Botan::SRP6_Server_Session>()); -return BOTAN_FFI_SUCCESS; -}); -} + int botan_srp6_server_session_init(botan_srp6_server_session_t* srp6) + { + return ffi_guard_thunk(__func__, [=]() -> int + { + *srp6 = new botan_srp6_server_session_struct( + std::make_unique<Botan::SRP6_Server_Session>()); + return BOTAN_FFI_SUCCESS; + }); + } -int botan_srp6_server_session_destroy(botan_srp6_server_session_t srp6) { -return BOTAN_FFI_CHECKED_DELETE(srp6); -} + int botan_srp6_server_session_destroy(botan_srp6_server_session_t srp6) + { + return BOTAN_FFI_CHECKED_DELETE(srp6); + } -int botan_srp6_server_session_step1(botan_srp6_server_session_t srp6, - const uint8_t *verifier, - size_t verifier_len, const char *group_id, - const char *hash_id, botan_rng_t rng_obj, - uint8_t b_pub[], size_t *b_pub_len) { -return BOTAN_FFI_DO(Botan::SRP6_Server_Session, srp6, s, { - if (!verifier || !group_id || !hash_id || !rng_obj) { - return BOTAN_FFI_ERROR_NULL_POINTER; - } - try { - Botan::RandomNumberGenerator &rng = safe_get(rng_obj); - auto v_bn = Botan::BigInt::decode(verifier, verifier_len); - auto b_pub_bn = s.step1(v_bn, group_id, hash_id, rng); - return write_vec_output(b_pub, b_pub_len, - Botan::BigInt::encode(b_pub_bn)); - } catch (Botan::Decoding_Error &) { - return BOTAN_FFI_ERROR_BAD_PARAMETER; - } catch (Botan::Lookup_Error &) { - return BOTAN_FFI_ERROR_BAD_PARAMETER; - } - }); -} + int botan_srp6_server_session_step1(botan_srp6_server_session_t srp6, + const uint8_t* verifier, + size_t verifier_len, const char* group_id, + const char* hash_id, botan_rng_t rng_obj, + uint8_t b_pub[], size_t* b_pub_len) + { + return BOTAN_FFI_DO(Botan::SRP6_Server_Session, srp6, s, + { + if(!verifier || !group_id || !hash_id || !rng_obj) + { + return BOTAN_FFI_ERROR_NULL_POINTER; + } + try + { + Botan::RandomNumberGenerator& rng = safe_get(rng_obj); + auto v_bn = Botan::BigInt::decode(verifier, verifier_len); + auto b_pub_bn = s.step1(v_bn, group_id, hash_id, rng); + return write_vec_output(b_pub, b_pub_len, + Botan::BigInt::encode(b_pub_bn)); + } + catch(Botan::Decoding_Error&) + { + return BOTAN_FFI_ERROR_BAD_PARAMETER; + } + catch(Botan::Lookup_Error&) + { + return BOTAN_FFI_ERROR_BAD_PARAMETER; + } + }); + } -int botan_srp6_server_session_step2(botan_srp6_server_session_t srp6, - const uint8_t a[], size_t a_len, - uint8_t key[], size_t *key_len) { -return BOTAN_FFI_DO(Botan::SRP6_Server_Session, srp6, s, { - if (!a) { - return BOTAN_FFI_ERROR_NULL_POINTER; - } - try { - Botan::BigInt a_bn = Botan::BigInt::decode(a, a_len); - auto key_sk = s.step2(a_bn); - return write_vec_output(key, key_len, key_sk.bits_of()); - } catch (Botan::Decoding_Error &) { - return BOTAN_FFI_ERROR_BAD_PARAMETER; - } - }); -} + int botan_srp6_server_session_step2(botan_srp6_server_session_t srp6, + const uint8_t a[], size_t a_len, + uint8_t key[], size_t* key_len) + { + return BOTAN_FFI_DO(Botan::SRP6_Server_Session, srp6, s, + { + if(!a) + { + return BOTAN_FFI_ERROR_NULL_POINTER; + } + try + { + Botan::BigInt a_bn = Botan::BigInt::decode(a, a_len); + auto key_sk = s.step2(a_bn); + return write_vec_output(key, key_len, key_sk.bits_of()); + } + catch(Botan::Decoding_Error&) + { + return BOTAN_FFI_ERROR_BAD_PARAMETER; + } + }); + } -int botan_generate_srp6_verifier(const char *username, const char *password, - const uint8_t salt[], size_t salt_len, - const char *group_id, const char *hash_id, - uint8_t verifier[], size_t *verifier_len) { -return ffi_guard_thunk(__func__, [=]() -> int { -if (!username || !password || !salt || !group_id || !hash_id) { -return BOTAN_FFI_ERROR_NULL_POINTER; -} -try { -std::vector<uint8_t> salt_vec(salt, salt + salt_len); -auto verifier_bn = Botan::generate_srp6_verifier( - username, password, salt_vec, group_id, hash_id); -return write_vec_output(verifier, verifier_len, - Botan::BigInt::encode(verifier_bn)); -} catch (Botan::Lookup_Error &) { -return BOTAN_FFI_ERROR_BAD_PARAMETER; -} -}); -} + int botan_generate_srp6_verifier(const char* username, const char* password, + const uint8_t salt[], size_t salt_len, + const char* group_id, const char* hash_id, + uint8_t verifier[], size_t* verifier_len) + { + return ffi_guard_thunk(__func__, [=]() -> int + { + if(!username || !password || !salt || !group_id || !hash_id) + { + return BOTAN_FFI_ERROR_NULL_POINTER; + } + try + { + std::vector<uint8_t> salt_vec(salt, salt + salt_len); + auto verifier_bn = Botan::generate_srp6_verifier( + username, password, salt_vec, group_id, hash_id); + return write_vec_output(verifier, verifier_len, + Botan::BigInt::encode(verifier_bn)); + } + catch(Botan::Lookup_Error&) + { + return BOTAN_FFI_ERROR_BAD_PARAMETER; + } + }); + } -int botan_srp6_client_agree(const char *identity, const char *password, - const char *group_id, const char *hash_id, - const uint8_t salt[], size_t salt_len, - const uint8_t b[], size_t b_len, botan_rng_t rng_obj, - uint8_t A[], size_t *A_len, uint8_t K[], - size_t *K_len) { -return ffi_guard_thunk(__func__, [=]() -> int { -if (!identity || !password || !salt || !group_id || !hash_id || !b || !rng_obj) { -return BOTAN_FFI_ERROR_NULL_POINTER; -} -try { -std::vector<uint8_t> saltv(salt, salt + salt_len); -Botan::RandomNumberGenerator &rng = safe_get(rng_obj); -auto b_bn = Botan::BigInt::decode(b, b_len); -auto [A_bn, K_sk] = Botan::srp6_client_agree( - identity, password, group_id, hash_id, saltv, b_bn, rng); -auto ret_a = write_vec_output(A, A_len, Botan::BigInt::encode(A_bn)); -auto ret_k = write_vec_output(K, K_len, K_sk.bits_of()); -if (ret_a != BOTAN_FFI_SUCCESS) { -return ret_a; -} -if (ret_k != BOTAN_FFI_SUCCESS) { -return ret_k; -} -return BOTAN_FFI_SUCCESS; -} catch (Botan::Lookup_Error &) { -return BOTAN_FFI_ERROR_BAD_PARAMETER; -} -}); -} + int botan_srp6_client_agree(const char* identity, const char* password, + const char* group_id, const char* hash_id, + const uint8_t salt[], size_t salt_len, + const uint8_t b[], size_t b_len, botan_rng_t rng_obj, + uint8_t A[], size_t* A_len, uint8_t K[], + size_t* K_len) + { + return ffi_guard_thunk(__func__, [=]() -> int + { + if(!identity || !password || !salt || !group_id || !hash_id || !b || !rng_obj) + { + return BOTAN_FFI_ERROR_NULL_POINTER; + } + try + { + std::vector<uint8_t> saltv(salt, salt + salt_len); + Botan::RandomNumberGenerator& rng = safe_get(rng_obj); + auto b_bn = Botan::BigInt::decode(b, b_len); + auto [A_bn, K_sk] = Botan::srp6_client_agree( + identity, password, group_id, hash_id, saltv, b_bn, rng); + auto ret_a = write_vec_output(A, A_len, Botan::BigInt::encode(A_bn)); + auto ret_k = write_vec_output(K, K_len, K_sk.bits_of()); + if(ret_a != BOTAN_FFI_SUCCESS) + { + return ret_a; + } + if(ret_k != BOTAN_FFI_SUCCESS) + { + return ret_k; + } + return BOTAN_FFI_SUCCESS; + } + catch(Botan::Lookup_Error&) + { + return BOTAN_FFI_ERROR_BAD_PARAMETER; + } + }); + } -} + } diff --git a/src/python/botan2.py b/src/python/botan2.py index 656800824..6e5b73d9b 100755 --- a/src/python/botan2.py +++ b/src/python/botan2.py @@ -101,10 +101,10 @@ def _set_prototypes(dll): def ffi_api(fn, args, allowed_errors=None): if allowed_errors is None: allowed_errors = [-10] - fn.argtypes = args - fn.restype = c_int - fn.errcheck = _errcheck - fn.allowed_errors = allowed_errors + fn.argtypes = args + fn.restype = c_int + fn.errcheck = _errcheck + fn.allowed_errors = allowed_errors dll.botan_version_string.argtypes = [] dll.botan_version_string.restype = c_char_p @@ -839,10 +839,10 @@ class SymmetricCipher(object): elif self._is_cbc: # Hack: the largest block size currently supported extra_bytes = 64 - out = create_string_buffer(inp_sz.value + extra_bytes) - out_sz = c_size_t(len(out)) - out_written = c_size_t(0) - flags = c_uint32(1 if final else 0) + out = create_string_buffer(inp_sz.value + extra_bytes) + out_sz = c_size_t(len(out)) + out_written = c_size_t(0) + flags = c_uint32(1 if final else 0) _DLL.botan_cipher_update(self.__obj, flags, out, out_sz, byref(out_written), @@ -1439,7 +1439,7 @@ class X509Cert(object): # pylint: disable=invalid-name arr_intermediates = c_intermediates() for i, ca in enumerate(intermediates): arr_intermediates[i] = ca.handle_() - len_intermediates = c_size_t(len(intermediates)) + len_intermediates = c_size_t(len(intermediates)) else: arr_intermediates = c_void_p(0) len_intermediates = c_size_t(0) @@ -1449,7 +1449,7 @@ class X509Cert(object): # pylint: disable=invalid-name arr_trusted = c_trusted() for i, ca in enumerate(trusted): arr_trusted[i] = ca.handle_() - len_trusted = c_size_t(len(trusted)) + len_trusted = c_size_t(len(trusted)) else: arr_trusted = c_void_p(0) len_trusted = c_size_t(0) @@ -1459,7 +1459,7 @@ class X509Cert(object): # pylint: disable=invalid-name arr_crls = c_crls() for i, crl in enumerate(crls): arr_crls[i] = crl.handle_() - len_crls = c_size_t(len(crls)) + len_crls = c_size_t(len(crls)) else: arr_crls = c_void_p(0) len_crls = c_size_t(0) @@ -1772,14 +1772,14 @@ class TOTP(object): def generate(self, timestamp=None): if timestamp is None: timestamp = int(system_time()) - code = c_uint32(0) - _DLL.botan_totp_generate(self.__obj, byref(code), timestamp) + code = c_uint32(0) + _DLL.botan_totp_generate(self.__obj, byref(code), timestamp) return code.value def check(self, code, timestamp=None, acceptable_drift=0): if timestamp is None: timestamp = int(system_time()) - rc = _DLL.botan_totp_check(self.__obj, code, timestamp, acceptable_drift) + rc = _DLL.botan_totp_check(self.__obj, code, timestamp, acceptable_drift) if rc == 0: return True return False |