Botan 3.12.0
Crypto and TLS for C&
ffi_srp6.cpp File Reference
#include <botan/ffi.h>
#include <botan/assert.h>
#include <botan/internal/ffi_rng.h>
#include <botan/internal/ffi_util.h>

Go to the source code of this file.

Functions

 BOTAN_FFI_DECLARE_DUMMY_STRUCT (botan_srp6_server_session_struct, 0x44F7425F)
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)
int botan_srp6_generate_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)
int botan_srp6_group_size (const char *group_id, size_t *group_p_bytes)
int botan_srp6_server_session_destroy (botan_srp6_server_session_t srp6)
int botan_srp6_server_session_init (botan_srp6_server_session_t *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)
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)

Function Documentation

◆ BOTAN_FFI_DECLARE_DUMMY_STRUCT()

BOTAN_FFI_DECLARE_DUMMY_STRUCT ( botan_srp6_server_session_struct ,
0x44F7425F  )

◆ botan_srp6_client_agree()

int botan_srp6_client_agree ( const char * username,
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 )

SRP6a Client side

Parameters
usernamethe username we are attempting login for
passwordthe password we are attempting to use
group_idspecifies the shared SRP group
Hash Function Identificationspecifies a secure hash function
saltis the salt value sent by the server
salt_lenthe length of salt
Bis the server's public value
B_lenis the server's public value length
rng_objis a random number generator object
Aout buffer to store the SRP-6 A value
A_lenSRP-6 A verifier value length
Kout buffer to store the symmetric value
K_lensymmetric key length
Returns
0 on success, negative on failure

Definition at line 153 of file ffi_srp6.cpp.

165 {
166#if defined(BOTAN_HAS_SRP6)
167 return ffi_guard_thunk(__func__, [=]() -> int {
168 if(any_null_pointers(identity, password, salt, group_id, hash_id, b, rng_obj)) {
170 }
171 try {
172 const std::vector<uint8_t> saltv(salt, salt + salt_len);
174 auto b_bn = Botan::BigInt::from_bytes({b, b_len});
175 const auto group = Botan::DL_Group::from_name(group_id);
176 const size_t a_bits = group.exponent_bits();
177 auto [A_bn, K_sk] = Botan::srp6_client_agree(identity, password, group, hash_id, saltv, b_bn, a_bits, rng);
178 auto ret_a = write_vec_output(A, A_len, A_bn.serialize(group.p_bytes()));
179 auto ret_k = write_vec_output(K, K_len, K_sk.bits_of());
180 if(ret_a != BOTAN_FFI_SUCCESS) {
181 return ret_a;
182 }
183 if(ret_k != BOTAN_FFI_SUCCESS) {
184 return ret_k;
185 }
186 return BOTAN_FFI_SUCCESS;
187 } catch(Botan::Lookup_Error&) {
189 }
190 });
191#else
192 BOTAN_UNUSED(identity, password, group_id, hash_id, rng_obj);
193 BOTAN_UNUSED(salt, salt_len, b, b_len, A, A_len, K, K_len);
195#endif
196}
#define BOTAN_UNUSED
Definition assert.h:144
static BigInt from_bytes(std::span< const uint8_t > bytes)
Definition bigint.cpp:83
static DL_Group from_name(std::string_view name)
Definition dl_group.cpp:262
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:140
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:133
@ BOTAN_FFI_SUCCESS
Definition ffi.h:116
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:134
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:79
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:95
int write_vec_output(uint8_t out[], size_t *out_len, std::span< const uint8_t > buf)
Definition ffi_util.h:264
bool any_null_pointers(Ptrs... ptr)
Definition mem_utils.h:54
std::pair< BigInt, SymmetricKey > srp6_client_agree(std::string_view identifier, std::string_view password, std::string_view group_id, std::string_view hash_id, const std::vector< uint8_t > &salt, const BigInt &B, RandomNumberGenerator &rng)
Definition srp6.cpp:66

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan::BigInt::from_bytes(), Botan::DL_Group::from_name(), Botan_FFI::safe_get(), Botan::srp6_client_agree(), and Botan_FFI::write_vec_output().

Referenced by botan_srp6_server_session_step1().

◆ botan_srp6_generate_verifier()

int botan_srp6_generate_verifier ( const char * identifier,
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 )

Generate a new SRP-6 verifier

Parameters
identifiera username or other client identifier
passwordthe secret used to authenticate user
salta randomly chosen value, at least 128 bits long
salt_lenthe length of salt
group_idspecifies the shared SRP group
Hash Function Identificationspecifies a secure hash function
verifierout buffer to store the SRP-6 verifier value
verifier_lenSRP-6 verifier value length
Returns
0 on success, negative on failure

Definition at line 123 of file ffi_srp6.cpp.

130 {
131#if defined(BOTAN_HAS_SRP6)
132 return ffi_guard_thunk(__func__, [=]() -> int {
133 if(any_null_pointers(username, password, salt, group_id, hash_id)) {
135 }
136 try {
137 const std::vector<uint8_t> salt_vec(salt, salt + salt_len);
138 const auto group = Botan::DL_Group::from_name(group_id);
139 const size_t p_bytes = group.p_bytes();
140 auto verifier_bn = Botan::srp6_generate_verifier(username, password, salt_vec, group, hash_id);
141 return write_vec_output(verifier, verifier_len, verifier_bn.serialize(p_bytes));
142 } catch(Botan::Lookup_Error&) {
144 }
145 });
146#else
147 BOTAN_UNUSED(username, password, group_id, hash_id);
148 BOTAN_UNUSED(salt, salt_len, verifier, verifier_len);
150#endif
151}
BigInt srp6_generate_verifier(std::string_view identifier, std::string_view password, const std::vector< uint8_t > &salt, std::string_view group_id, std::string_view hash_id)
Definition srp6.cpp:130

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan::DL_Group::from_name(), Botan::srp6_generate_verifier(), and Botan_FFI::write_vec_output().

Referenced by botan_srp6_server_session_step1().

◆ botan_srp6_group_size()

int botan_srp6_group_size ( const char * group_id,
size_t * group_p_bytes )

Return the size, in bytes, of the prime associated with group_id

Definition at line 49 of file ffi_srp6.cpp.

49 {
50#if defined(BOTAN_HAS_SRP6)
51 if(any_null_pointers(group_id, group_p_bytes)) {
53 }
54
55 return ffi_guard_thunk(__func__, [=]() -> int {
56 const auto group = Botan::DL_Group::from_name(group_id);
57 *group_p_bytes = group.p_bytes();
58 return BOTAN_FFI_SUCCESS;
59 });
60#else
61 BOTAN_UNUSED(group_id, group_p_bytes);
63#endif
64}

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan::DL_Group::from_name().

Referenced by botan_srp6_server_session_step1().

◆ botan_srp6_server_session_destroy()

int botan_srp6_server_session_destroy ( botan_srp6_server_session_t srp6)

Frees all resources of the SRP-6 server session object

Parameters
SRP-6aSRP-6 server session object
Returns
0 if success, error if invalid object handle

Definition at line 45 of file ffi_srp6.cpp.

45 {
46 return BOTAN_FFI_CHECKED_DELETE(srp6);
47}
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:188

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_srp6_server_session_init()

int botan_srp6_server_session_init ( botan_srp6_server_session_t * srp6)

Initialize an SRP-6 server session object

Parameters
SRP-6aSRP-6 server session object

Definition at line 32 of file ffi_srp6.cpp.

32 {
33#if defined(BOTAN_HAS_SRP6)
34 if(srp6 == nullptr) {
36 }
37 return ffi_guard_thunk(
38 __func__, [=]() -> int { return ffi_new_object(srp6, std::make_unique<Botan::SRP6_Server_Session>()); });
39#else
40 BOTAN_UNUSED(srp6);
42#endif
43}
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:178

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_srp6_server_session_step1()

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 )

Definition at line 66 of file ffi_srp6.cpp.

73 {
74#if defined(BOTAN_HAS_SRP6)
75 return BOTAN_FFI_VISIT(srp6, [=](auto& s) -> int {
76 if(any_null_pointers(verifier, group_id, hash_id, rng_obj)) {
78 }
79 try {
80 const auto group = Botan::DL_Group::from_name(group_id);
81 const auto rc = check_and_prepare_output_space(b_pub, b_pub_len, group.p_bytes());
82 if(rc != BOTAN_FFI_SUCCESS) {
83 return rc;
84 }
85
87 auto v_bn = Botan::BigInt::from_bytes(std::span{verifier, verifier_len});
88 auto b_pub_bn = s.step1(v_bn, group, hash_id, group.exponent_bits(), rng);
89 return write_vec_output(b_pub, b_pub_len, b_pub_bn.serialize(group.p_bytes()));
90 } catch(Botan::Decoding_Error&) {
92 } catch(Botan::Lookup_Error&) {
94 }
95 });
96#else
97 BOTAN_UNUSED(srp6, verifier, verifier_len, group_id, hash_id, rng_obj, b_pub, b_pub_len);
99#endif
100}
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:158
int check_and_prepare_output_space(T out[], size_t *out_len, size_t required_len)
Definition ffi_util.h:230

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_FFI_VISIT, BOTAN_UNUSED, Botan_FFI::check_and_prepare_output_space(), Botan::BigInt::from_bytes(), Botan::DL_Group::from_name(), Botan_FFI::safe_get(), and Botan_FFI::write_vec_output().

◆ botan_srp6_server_session_step2()

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 )

SRP-6 Server side step 2

Parameters
SRP-6aSRP-6 server session object
Athe client's value
A_lenthe client's value length
keyout buffer to store the symmetric key value
key_lensymmetric key length
Returns
0 on success, negative on failure

Definition at line 102 of file ffi_srp6.cpp.

103 {
104#if defined(BOTAN_HAS_SRP6)
105 return BOTAN_FFI_VISIT(srp6, [=](auto& s) -> int {
106 if(!a) {
108 }
109 try {
110 const Botan::BigInt a_bn = Botan::BigInt::from_bytes({a, a_len});
111 auto key_sk = s.step2(a_bn);
112 return write_vec_output(key, key_len, key_sk.bits_of());
113 } catch(Botan::Decoding_Error&) {
115 }
116 });
117#else
118 BOTAN_UNUSED(srp6, a, a_len, key, key_len);
120#endif
121}

References BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_VISIT, BOTAN_UNUSED, Botan::BigInt::from_bytes(), and Botan_FFI::write_vec_output().

Referenced by botan_srp6_server_session_step1().