Botan 3.0.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#include <botan/internal/ffi_rng.h>
10#include <botan/internal/ffi_util.h>
11
12#if defined(BOTAN_HAS_SRP6)
13 #include <botan/bigint.h>
14 #include <botan/dl_group.h>
15 #include <botan/rng.h>
16 #include <botan/srp6.h>
17 #include <botan/symkey.h>
18#endif
19
20extern "C" {
21
22using namespace Botan_FFI;
23
24#if defined(BOTAN_HAS_SRP6)
25BOTAN_FFI_DECLARE_STRUCT(botan_srp6_server_session_struct,
26 Botan::SRP6_Server_Session, 0x44F7425F);
27#else
28BOTAN_FFI_DECLARE_DUMMY_STRUCT(botan_srp6_server_session_struct, 0x44F7425F);
29#endif
30
32 {
33#if defined(BOTAN_HAS_SRP6)
34 return ffi_guard_thunk(__func__, [=]() -> int
35 {
36 *srp6 = new botan_srp6_server_session_struct(
37 std::make_unique<Botan::SRP6_Server_Session>());
38 return BOTAN_FFI_SUCCESS;
39 });
40#else
41 BOTAN_UNUSED(srp6);
43#endif
44 }
45
47 {
48 return BOTAN_FFI_CHECKED_DELETE(srp6);
49 }
50
51int botan_srp6_group_size(const char* group_id, size_t* group_p_bytes)
52 {
53#if defined(BOTAN_HAS_SRP6)
54 if(group_id == nullptr || group_p_bytes == nullptr)
56
57 return ffi_guard_thunk(__func__, [=]() -> int
58 {
59 Botan::DL_Group group(group_id);
60 *group_p_bytes = group.p_bytes();
61 return BOTAN_FFI_SUCCESS;
62 });
63#else
64 BOTAN_UNUSED(group_id, group_p_bytes);
66#endif
67 }
68
70 const uint8_t* verifier,
71 size_t verifier_len, const char* group_id,
72 const char* hash_id, botan_rng_t rng_obj,
73 uint8_t b_pub[], size_t* b_pub_len)
74 {
75#if defined(BOTAN_HAS_SRP6)
76 return BOTAN_FFI_VISIT(srp6, [=](auto& s) -> int
77 {
78 if(!verifier || !group_id || !hash_id || !rng_obj)
79 {
81 }
82 try
83 {
85 auto v_bn = Botan::BigInt::decode(verifier, verifier_len);
86 auto b_pub_bn = s.step1(v_bn, group_id, hash_id, rng);
87 return write_vec_output(b_pub, b_pub_len,
88 Botan::BigInt::encode(b_pub_bn));
89 }
91 {
93 }
95 {
97 }
98 });
99#else
100 BOTAN_UNUSED(srp6, verifier, verifier_len, group_id, hash_id, rng_obj, b_pub, b_pub_len);
102#endif
103 }
104
106 const uint8_t a[], size_t a_len,
107 uint8_t key[], size_t* key_len)
108 {
109#if defined(BOTAN_HAS_SRP6)
110 return BOTAN_FFI_VISIT(srp6, [=](auto& s) -> int
111 {
112 if(!a)
113 {
115 }
116 try
117 {
118 Botan::BigInt a_bn = Botan::BigInt::decode(a, a_len);
119 auto key_sk = s.step2(a_bn);
120 return write_vec_output(key, key_len, key_sk.bits_of());
121 }
123 {
125 }
126 });
127#else
128 BOTAN_UNUSED(srp6, a, a_len, key, key_len);
130#endif
131 }
132
133int botan_srp6_generate_verifier(const char* username, const char* password,
134 const uint8_t salt[], size_t salt_len,
135 const char* group_id, const char* hash_id,
136 uint8_t verifier[], size_t* verifier_len)
137 {
138#if defined(BOTAN_HAS_SRP6)
139 return ffi_guard_thunk(__func__, [=]() -> int
140 {
141 if(!username || !password || !salt || !group_id || !hash_id)
142 {
144 }
145 try
146 {
147 std::vector<uint8_t> salt_vec(salt, salt + salt_len);
148 auto verifier_bn = Botan::srp6_generate_verifier(
149 username, password, salt_vec, group_id, hash_id);
150 return write_vec_output(verifier, verifier_len,
151 Botan::BigInt::encode(verifier_bn));
152 }
153 catch(Botan::Lookup_Error&)
154 {
156 }
157 });
158#else
159 BOTAN_UNUSED(username, password, group_id, hash_id);
160 BOTAN_UNUSED(salt, salt_len, verifier, verifier_len);
162#endif
163 }
164
165int botan_srp6_client_agree(const char* identity, const char* password,
166 const char* group_id, const char* hash_id,
167 const uint8_t salt[], size_t salt_len,
168 const uint8_t b[], size_t b_len, botan_rng_t rng_obj,
169 uint8_t A[], size_t* A_len, uint8_t K[],
170 size_t* K_len)
171 {
172#if defined(BOTAN_HAS_SRP6)
173 return ffi_guard_thunk(__func__, [=]() -> int
174 {
175 if(!identity || !password || !salt || !group_id || !hash_id || !b || !rng_obj)
176 {
178 }
179 try
180 {
181 std::vector<uint8_t> saltv(salt, salt + salt_len);
183 auto b_bn = Botan::BigInt::decode(b, b_len);
184 auto [A_bn, K_sk] = Botan::srp6_client_agree(
185 identity, password, group_id, hash_id, saltv, b_bn, rng);
186 auto ret_a = write_vec_output(A, A_len, Botan::BigInt::encode(A_bn));
187 auto ret_k = write_vec_output(K, K_len, K_sk.bits_of());
188 if(ret_a != BOTAN_FFI_SUCCESS)
189 {
190 return ret_a;
191 }
192 if(ret_k != BOTAN_FFI_SUCCESS)
193 {
194 return ret_k;
195 }
196 return BOTAN_FFI_SUCCESS;
197 }
198 catch(Botan::Lookup_Error&)
199 {
201 }
202 });
203#else
204 BOTAN_UNUSED(identity, password, group_id, hash_id, rng_obj);
205 BOTAN_UNUSED(salt, salt_len, b, b_len, A, A_len, K, K_len);
207#endif
208 }
209
210}
#define BOTAN_UNUSED(...)
Definition: assert.h:141
static BigInt decode(const uint8_t buf[], size_t length)
Definition: bigint.h:805
static std::vector< uint8_t > encode(const BigInt &n)
Definition: bigint.h:780
size_t p_bytes() const
Definition: dl_group.cpp:516
struct botan_srp6_server_session_struct * botan_srp6_server_session_t
Definition: ffi.h:2043
struct botan_rng_struct * botan_rng_t
Definition: ffi.h:229
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:91
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:85
@ BOTAN_FFI_SUCCESS
Definition: ffi.h:70
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition: ffi.h:86
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:105
int botan_srp6_server_session_init(botan_srp6_server_session_t *srp6)
Definition: ffi_srp6.cpp:31
int botan_srp6_server_session_destroy(botan_srp6_server_session_t srp6)
Definition: ffi_srp6.cpp:46
int botan_srp6_group_size(const char *group_id, size_t *group_p_bytes)
Definition: ffi_srp6.cpp:51
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:69
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:133
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:165
#define BOTAN_FFI_VISIT(obj, lambda)
Definition: ffi_util.h:126
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:145
#define BOTAN_FFI_DECLARE_DUMMY_STRUCT(NAME, MAGIC)
Definition: ffi_util.h:60
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
Definition: ffi_util.h:55
T & safe_get(botan_struct< T, M > *p)
Definition: ffi_util.h:69
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition: ffi.cpp:120
int write_vec_output(uint8_t out[], size_t *out_len, const std::vector< uint8_t, Alloc > &buf)
Definition: ffi_util.h:214
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:74
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:141