Botan 3.12.0
Crypto and TLS for C&
ffi_srp6.cpp
Go to the documentation of this file.
1/*
2* (C) 2022 Rostyslav Khudolii
3* 2023 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/ffi.h>
9
10#include <botan/assert.h>
11#include <botan/internal/ffi_rng.h>
12#include <botan/internal/ffi_util.h>
13
14#if defined(BOTAN_HAS_SRP6)
15 #include <botan/bigint.h>
16 #include <botan/dl_group.h>
17 #include <botan/rng.h>
18 #include <botan/srp6.h>
19 #include <botan/symkey.h>
20#endif
21
22extern "C" {
23
24using namespace Botan_FFI;
25
26#if defined(BOTAN_HAS_SRP6)
27BOTAN_FFI_DECLARE_STRUCT(botan_srp6_server_session_struct, Botan::SRP6_Server_Session, 0x44F7425F);
28#else
29BOTAN_FFI_DECLARE_DUMMY_STRUCT(botan_srp6_server_session_struct, 0x44F7425F);
30#endif
31
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}
44
48
49int botan_srp6_group_size(const char* group_id, size_t* group_p_bytes) {
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}
65
67 const uint8_t* verifier,
68 size_t verifier_len,
69 const char* group_id,
70 const char* hash_id,
71 botan_rng_t rng_obj,
72 uint8_t b_pub[],
73 size_t* b_pub_len) {
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}
101
103 botan_srp6_server_session_t srp6, const uint8_t a[], size_t a_len, uint8_t key[], size_t* key_len) {
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}
122
123int botan_srp6_generate_verifier(const char* username,
124 const char* password,
125 const uint8_t salt[],
126 size_t salt_len,
127 const char* group_id,
128 const char* hash_id,
129 uint8_t verifier[],
130 size_t* verifier_len) {
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}
152
153int botan_srp6_client_agree(const char* identity,
154 const char* password,
155 const char* group_id,
156 const char* hash_id,
157 const uint8_t salt[],
158 size_t salt_len,
159 const uint8_t b[],
160 size_t b_len,
161 botan_rng_t rng_obj,
162 uint8_t A[],
163 size_t* A_len,
164 uint8_t K[],
165 size_t* K_len) {
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}
197}
#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
struct botan_srp6_server_session_struct * botan_srp6_server_session_t
Definition ffi.h:3029
struct botan_rng_struct * botan_rng_t
Definition ffi.h:291
@ 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
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)
Definition ffi_srp6.cpp:102
int botan_srp6_server_session_init(botan_srp6_server_session_t *srp6)
Definition ffi_srp6.cpp:32
int botan_srp6_server_session_destroy(botan_srp6_server_session_t srp6)
Definition ffi_srp6.cpp:45
int botan_srp6_group_size(const char *group_id, size_t *group_p_bytes)
Definition ffi_srp6.cpp:49
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 ffi_srp6.cpp:66
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)
Definition ffi_srp6.cpp:123
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)
Definition ffi_srp6.cpp:153
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:158
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:188
#define BOTAN_FFI_DECLARE_DUMMY_STRUCT(NAME, MAGIC)
Definition ffi_util.h:66
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
Definition ffi_util.h:61
int check_and_prepare_output_space(T out[], size_t *out_len, size_t required_len)
Definition ffi_util.h:230
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:79
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:178
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
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