Botan 3.14.1
Crypto and TLS for C&
ffi_spake2p.cpp
Go to the documentation of this file.
1/*
2* (C) 2026 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/ffi.h>
8
9#include <botan/assert.h>
10#include <botan/internal/ffi_rng.h>
11#include <botan/internal/ffi_util.h>
12
13#if defined(BOTAN_HAS_PAKE_SPAKE2PLUS)
14 #include <botan/spake2p.h>
15 #include <botan/internal/ffi_ec.h>
16#endif
17
18#if defined(BOTAN_HAS_PAKE_SPAKE2PLUS)
19
20namespace {
21
22Botan::SPAKE2p::SystemParameters spake2p_params_from_name(const char* ciphersuite) {
24
25 const std::string_view name(ciphersuite);
26
27 if(name == "P256-SHA256") {
28 return SystemParameters::rfc9383_p256_sha256();
29 } else if(name == "P256-SHA512") {
30 return SystemParameters::rfc9383_p256_sha512();
31 } else if(name == "P384-SHA256") {
32 return SystemParameters::rfc9383_p384_sha256();
33 } else if(name == "P384-SHA512") {
34 return SystemParameters::rfc9383_p384_sha512();
35 } else if(name == "P521-SHA512") {
36 return SystemParameters::rfc9383_p521_sha512();
37 } else {
38 throw Botan::Invalid_Argument("Unknown SPAKE2+ ciphersuite");
39 }
40}
41
42std::span<const uint8_t> spake2p_opt_span(const uint8_t* ptr, size_t len) {
43 if(ptr == nullptr && len > 0) {
44 throw Botan_FFI::FFI_Error("Null pointer with non-zero length", BOTAN_FFI_ERROR_NULL_POINTER);
45 }
46 return {ptr, len};
47}
48
49} // namespace
50
51#endif
52
53extern "C" {
54
55using namespace Botan_FFI;
56
57#if defined(BOTAN_HAS_PAKE_SPAKE2PLUS)
58BOTAN_FFI_DECLARE_UNIQUE_STRUCT(botan_spake2p_params_struct, Botan::SPAKE2p::SystemParameters, 0x2E1B4A96);
59BOTAN_FFI_DECLARE_UNIQUE_STRUCT(botan_spake2p_prover_struct, Botan::SPAKE2p::ProverContext, 0x9F337C29);
60BOTAN_FFI_DECLARE_UNIQUE_STRUCT(botan_spake2p_verifier_struct, Botan::SPAKE2p::VerifierContext, 0xD70A9E13);
61#else
62BOTAN_FFI_DECLARE_DUMMY_STRUCT(botan_spake2p_params_struct, 0x2E1B4A96);
63BOTAN_FFI_DECLARE_DUMMY_STRUCT(botan_spake2p_prover_struct, 0x9F337C29);
64BOTAN_FFI_DECLARE_DUMMY_STRUCT(botan_spake2p_verifier_struct, 0xD70A9E13);
65#endif
66
67int botan_spake2p_params_init(botan_spake2p_params_t* params, const char* ciphersuite) {
68 if(any_null_pointers(params, ciphersuite)) {
70 }
71
72#if defined(BOTAN_HAS_PAKE_SPAKE2PLUS)
73 return ffi_guard_thunk(__func__, [=]() -> int {
74 *params = nullptr;
75 auto p = std::make_unique<Botan::SPAKE2p::SystemParameters>(spake2p_params_from_name(ciphersuite));
76 return ffi_new_object(params, std::move(p));
77 });
78#else
80#endif
81}
82
84 botan_spake2p_params_t* params, botan_ec_group_t group, const uint8_t seed[], size_t seed_len, const char* hash_fn) {
85 if(any_null_pointers(params, group, hash_fn)) {
87 }
88
89#if defined(BOTAN_HAS_PAKE_SPAKE2PLUS)
90 return ffi_guard_thunk(__func__, [=]() -> int {
91 auto p = std::make_unique<Botan::SPAKE2p::SystemParameters>(
92 Botan::SPAKE2p::SystemParameters::custom(safe_get(group), spake2p_opt_span(seed, seed_len), hash_fn));
93 return ffi_new_object(params, std::move(p));
94 });
95#else
96 BOTAN_UNUSED(params, group, seed, seed_len, hash_fn);
98#endif
99}
100
104
106#if defined(BOTAN_HAS_PAKE_SPAKE2PLUS)
107 return BOTAN_FFI_VISIT(params, [=](const auto& p) -> int {
108 if(share_size == nullptr) {
110 }
111 *share_size = p.share_size();
112 return BOTAN_FFI_SUCCESS;
113 });
114#else
115 BOTAN_UNUSED(params, share_size);
117#endif
118}
119
121 if(any_null_pointers(params, confirmation_size)) {
123 }
124
125#if defined(BOTAN_HAS_PAKE_SPAKE2PLUS)
126 return BOTAN_FFI_VISIT(params, [=](const auto& p) -> int {
127 *confirmation_size = p.confirmation_size();
128 return BOTAN_FFI_SUCCESS;
129 });
130#else
131 BOTAN_UNUSED(params, confirmation_size);
133#endif
134}
135
137 const char* password,
138 const uint8_t prover_id[],
139 size_t prover_id_len,
140 const uint8_t verifier_id[],
141 size_t verifier_id_len,
142 const uint8_t salt[],
143 size_t salt_len,
144 botan_view_ctx ctx,
145 botan_view_bin_fn view) {
146 if(any_null_pointers(params, password)) {
148 }
149
150#if defined(BOTAN_HAS_PAKE_SPAKE2PLUS)
151 return ffi_guard_thunk(__func__, [=]() -> int {
153 password,
154 spake2p_opt_span(prover_id, prover_id_len),
155 spake2p_opt_span(verifier_id, verifier_id_len),
156 spake2p_opt_span(salt, salt_len));
157 return invoke_view_callback(view, ctx, sec.serialize());
158 });
159#else
160 BOTAN_UNUSED(params, password, prover_id, prover_id_len, verifier_id, verifier_id_len);
161 BOTAN_UNUSED(salt, salt_len, ctx, view);
163#endif
164}
165
167 botan_rng_t rng,
168 const uint8_t secret[],
169 size_t secret_len,
170 botan_view_ctx ctx,
171 botan_view_bin_fn view) {
172#if defined(BOTAN_HAS_PAKE_SPAKE2PLUS)
173 if(any_null_pointers(params, rng, secret)) {
175 }
176
177 return ffi_guard_thunk(__func__, [=]() -> int {
178 const auto sec = Botan::SPAKE2p::ProverSecret::deserialize(safe_get(params), {secret, secret_len});
179 return invoke_view_callback(view, ctx, sec.registration_record(safe_get(rng)).serialize());
180 });
181#else
182 BOTAN_UNUSED(params, rng, secret, secret_len, ctx, view);
184#endif
185}
186
189 const uint8_t secret[],
190 size_t secret_len,
191 const uint8_t prover_id[],
192 size_t prover_id_len,
193 const uint8_t verifier_id[],
194 size_t verifier_id_len,
195 const uint8_t context[],
196 size_t context_len) {
197#if defined(BOTAN_HAS_PAKE_SPAKE2PLUS)
198 if(any_null_pointers(prover, params, secret)) {
200 }
201
202 return ffi_guard_thunk(__func__, [=]() -> int {
203 *prover = nullptr;
204 const auto& p = safe_get(params);
205 const auto sec = Botan::SPAKE2p::ProverSecret::deserialize(p, {secret, secret_len});
206 auto ctx = std::make_unique<Botan::SPAKE2p::ProverContext>(p,
207 sec,
208 spake2p_opt_span(prover_id, prover_id_len),
209 spake2p_opt_span(verifier_id, verifier_id_len),
210 spake2p_opt_span(context, context_len));
211 return ffi_new_object(prover, std::move(ctx));
212 });
213#else
214 BOTAN_UNUSED(prover, params, secret, secret_len, prover_id, prover_id_len);
215 BOTAN_UNUSED(verifier_id, verifier_id_len, context, context_len);
217#endif
218}
219
223
225 botan_rng_t rng,
226 botan_view_ctx ctx,
227 botan_view_bin_fn view) {
228#if defined(BOTAN_HAS_PAKE_SPAKE2PLUS)
229 return BOTAN_FFI_VISIT(
230 prover, [=](auto& p) -> int { return invoke_view_callback(view, ctx, p.generate_message(safe_get(rng))); });
231#else
232 BOTAN_UNUSED(prover, rng, ctx, view);
234#endif
235}
236
238 botan_rng_t rng,
239 const uint8_t peer_message[],
240 size_t peer_message_len,
241 botan_view_ctx ctx,
242 botan_view_bin_fn view) {
243 if(any_null_pointers(peer_message)) {
245 }
246#if defined(BOTAN_HAS_PAKE_SPAKE2PLUS)
247 return BOTAN_FFI_VISIT(prover, [=](auto& p) -> int {
248 return invoke_view_callback(view, ctx, p.process_message({peer_message, peer_message_len}, safe_get(rng)));
249 });
250#else
251 BOTAN_UNUSED(prover, rng, peer_message, peer_message_len, ctx, view);
253#endif
254}
255
257#if defined(BOTAN_HAS_PAKE_SPAKE2PLUS)
258 return BOTAN_FFI_VISIT(prover, [=](auto& p) -> int { return invoke_view_callback(view, ctx, p.shared_secret()); });
259#else
260 BOTAN_UNUSED(prover, ctx, view);
262#endif
263}
264
267 const uint8_t record[],
268 size_t record_len,
269 const uint8_t prover_id[],
270 size_t prover_id_len,
271 const uint8_t verifier_id[],
272 size_t verifier_id_len,
273 const uint8_t context[],
274 size_t context_len) {
275 if(any_null_pointers(verifier, record)) {
277 }
278
279#if defined(BOTAN_HAS_PAKE_SPAKE2PLUS)
280 return ffi_guard_thunk(__func__, [=]() -> int {
281 *verifier = nullptr;
282 const auto& p = safe_get(params);
283 const auto rec = Botan::SPAKE2p::RegistrationRecord::deserialize(p, {record, record_len});
284 auto ctx = std::make_unique<Botan::SPAKE2p::VerifierContext>(p,
285 rec,
286 spake2p_opt_span(prover_id, prover_id_len),
287 spake2p_opt_span(verifier_id, verifier_id_len),
288 spake2p_opt_span(context, context_len));
289 return ffi_new_object(verifier, std::move(ctx));
290 });
291#else
292 BOTAN_UNUSED(verifier, params, record, record_len, prover_id, prover_id_len);
293 BOTAN_UNUSED(verifier_id, verifier_id_len, context, context_len);
295#endif
296}
297
301
303 botan_rng_t rng,
304 const uint8_t peer_message[],
305 size_t peer_message_len,
306 botan_view_ctx ctx,
307 botan_view_bin_fn view) {
308 if(any_null_pointers(peer_message)) {
310 }
311
312#if defined(BOTAN_HAS_PAKE_SPAKE2PLUS)
313 return BOTAN_FFI_VISIT(verifier, [=](auto& v) -> int {
314 return invoke_view_callback(view, ctx, v.process_message({peer_message, peer_message_len}, safe_get(rng)));
315 });
316#else
317 BOTAN_UNUSED(verifier, rng, peer_message, peer_message_len, ctx, view);
319#endif
320}
321
323 const uint8_t confirmation[],
324 size_t confirmation_len) {
325 if(confirmation == nullptr) {
327 }
328
329#if defined(BOTAN_HAS_PAKE_SPAKE2PLUS)
330 return BOTAN_FFI_VISIT(verifier, [=](auto& v) -> int {
331 v.verify_confirmation({confirmation, confirmation_len});
332 return BOTAN_FFI_SUCCESS;
333 });
334#else
335 BOTAN_UNUSED(verifier, confirmation, confirmation_len);
337#endif
338}
339
341#if defined(BOTAN_HAS_PAKE_SPAKE2PLUS)
342 return BOTAN_FFI_VISIT(verifier, [](auto& v) -> int {
343 v.skip_confirmation();
344 return BOTAN_FFI_SUCCESS;
345 });
346#else
347 BOTAN_UNUSED(verifier);
349#endif
350}
351
353 botan_view_ctx ctx,
354 botan_view_bin_fn view) {
355#if defined(BOTAN_HAS_PAKE_SPAKE2PLUS)
356 return BOTAN_FFI_VISIT(verifier, [=](auto& v) -> int { return invoke_view_callback(view, ctx, v.shared_secret()); });
357#else
358 BOTAN_UNUSED(verifier, ctx, view);
360#endif
361}
362}
#define BOTAN_UNUSED
Definition assert.h:151
static ProverSecret from_password(const SystemParameters &params, std::string_view password, std::span< const uint8_t > prover_id, std::span< const uint8_t > verifier_id, std::span< const uint8_t > salt)
Definition spake2p.cpp:288
static ProverSecret deserialize(const SystemParameters &params, std::span< const uint8_t > secret)
Definition spake2p.cpp:301
static RegistrationRecord deserialize(const SystemParameters &params, std::span< const uint8_t > record)
Definition spake2p.cpp:262
static SystemParameters custom(const EC_Group &group, std::span< const uint8_t > seed, std::string_view hash_fn)
Definition spake2p.cpp:222
struct botan_spake2p_prover_struct * botan_spake2p_prover_t
Definition ffi.h:4956
struct botan_ec_group_struct * botan_ec_group_t
Definition ffi.h:1541
int(* botan_view_bin_fn)(botan_view_ctx view_ctx, const uint8_t *data, size_t len)
Definition ffi.h:161
void * botan_view_ctx
Definition ffi.h:152
struct botan_rng_struct * botan_rng_t
Definition ffi.h:343
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:131
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114
struct botan_spake2p_verifier_struct * botan_spake2p_verifier_t
Definition ffi.h:4961
struct botan_spake2p_params_struct * botan_spake2p_params_t
Definition ffi.h:4841
int botan_spake2p_params_init(botan_spake2p_params_t *params, const char *ciphersuite)
int botan_spake2p_prover_init(botan_spake2p_prover_t *prover, botan_spake2p_params_t params, const uint8_t secret[], size_t secret_len, const uint8_t prover_id[], size_t prover_id_len, const uint8_t verifier_id[], size_t verifier_id_len, const uint8_t context[], size_t context_len)
int botan_spake2p_derive_secret(botan_spake2p_params_t params, const char *password, const uint8_t prover_id[], size_t prover_id_len, const uint8_t verifier_id[], size_t verifier_id_len, const uint8_t salt[], size_t salt_len, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_spake2p_verifier_verify_confirmation(botan_spake2p_verifier_t verifier, const uint8_t confirmation[], size_t confirmation_len)
int botan_spake2p_verifier_process_message(botan_spake2p_verifier_t verifier, botan_rng_t rng, const uint8_t peer_message[], size_t peer_message_len, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_spake2p_params_share_size(botan_spake2p_params_t params, size_t *share_size)
int botan_spake2p_prover_shared_secret(botan_spake2p_prover_t prover, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_spake2p_params_confirmation_size(botan_spake2p_params_t params, size_t *confirmation_size)
int botan_spake2p_verifier_destroy(botan_spake2p_verifier_t verifier)
int botan_spake2p_verifier_skip_confirmation(botan_spake2p_verifier_t verifier)
int botan_spake2p_prover_process_message(botan_spake2p_prover_t prover, botan_rng_t rng, const uint8_t peer_message[], size_t peer_message_len, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_spake2p_params_init_custom(botan_spake2p_params_t *params, botan_ec_group_t group, const uint8_t seed[], size_t seed_len, const char *hash_fn)
int botan_spake2p_verifier_init(botan_spake2p_verifier_t *verifier, botan_spake2p_params_t params, const uint8_t record[], size_t record_len, const uint8_t prover_id[], size_t prover_id_len, const uint8_t verifier_id[], size_t verifier_id_len, const uint8_t context[], size_t context_len)
int botan_spake2p_prover_destroy(botan_spake2p_prover_t prover)
int botan_spake2p_verifier_shared_secret(botan_spake2p_verifier_t verifier, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_spake2p_params_destroy(botan_spake2p_params_t params)
int botan_spake2p_prover_generate_message(botan_spake2p_prover_t prover, botan_rng_t rng, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_spake2p_registration_record(botan_spake2p_params_t params, botan_rng_t rng, const uint8_t secret[], size_t secret_len, botan_view_ctx ctx, botan_view_bin_fn view)
const char * name
Definition ffi_tls.h:59
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:327
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:357
#define BOTAN_FFI_DECLARE_DUMMY_STRUCT(NAME, MAGIC)
Definition ffi_util.h:174
#define BOTAN_FFI_DECLARE_UNIQUE_STRUCT(NAME, TYPE, MAGIC)
Definition ffi_util.h:164
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
Definition ffi_util.h:359
auto safe_get(T *p) -> typename T::element_type &
Definition ffi_util.h:191
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:347
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:229
bool any_null_pointers(Ptrs... ptr)
Definition mem_utils.h:54