• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 13600848748

28 Feb 2025 10:18PM UTC coverage: 91.686% (-0.004%) from 91.69%
13600848748

push

github

web-flow
Merge pull request #4726 from randombit/jack/ffi-misc

Various FFI cleanups and documentation fixes

95826 of 104515 relevant lines covered (91.69%)

11371281.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.02
/src/lib/ffi/ffi_pkey_algs.cpp
1
/*
2
* (C) 2015,2017 Jack Lloyd
3
* (C) 2017 Ribose Inc
4
* (C) 2018 René Korthaus, Rohde & Schwarz Cybersecurity
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#include <botan/ffi.h>
10

11
#include <botan/hash.h>
12
#include <botan/pem.h>
13
#include <botan/internal/ffi_mp.h>
14
#include <botan/internal/ffi_pkey.h>
15
#include <botan/internal/ffi_rng.h>
16
#include <botan/internal/ffi_util.h>
17

18
#if defined(BOTAN_HAS_DL_GROUP)
19
   #include <botan/dl_group.h>
20
#endif
21

22
#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
23
   #include <botan/ecc_key.h>
24
#endif
25

26
#if defined(BOTAN_HAS_RSA)
27
   #include <botan/rsa.h>
28
#endif
29

30
#if defined(BOTAN_HAS_ELGAMAL)
31
   #include <botan/elgamal.h>
32
#endif
33

34
#if defined(BOTAN_HAS_DSA)
35
   #include <botan/dsa.h>
36
#endif
37

38
#if defined(BOTAN_HAS_ECDSA)
39
   #include <botan/ecdsa.h>
40
#endif
41

42
#if defined(BOTAN_HAS_SM2)
43
   #include <botan/sm2.h>
44
#endif
45

46
#if defined(BOTAN_HAS_ECDH)
47
   #include <botan/ecdh.h>
48
#endif
49

50
#if defined(BOTAN_HAS_X25519)
51
   #include <botan/x25519.h>
52
#endif
53

54
#if defined(BOTAN_HAS_X448)
55
   #include <botan/x448.h>
56
#endif
57

58
#if defined(BOTAN_HAS_ED25519)
59
   #include <botan/ed25519.h>
60
#endif
61

62
#if defined(BOTAN_HAS_ED448)
63
   #include <botan/ed448.h>
64
#endif
65

66
#if defined(BOTAN_HAS_MCELIECE)
67
   #include <botan/mceliece.h>
68
#endif
69

70
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
71
   #include <botan/dh.h>
72
#endif
73

74
#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
75
   #include <botan/kyber.h>
76
#endif
77

78
#if defined(BOTAN_HAS_ML_KEM)
79
   #include <botan/ml_kem.h>
80
#endif
81

82
#if defined(BOTAN_HAS_FRODOKEM)
83
   #include <botan/frodokem.h>
84
#endif
85

86
#if defined(BOTAN_HAS_ML_DSA)
87
   #include <botan/ml_dsa.h>
88
#endif
89

90
#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
91
   #include <botan/slh_dsa.h>
92
#endif
93

94
#if defined(BOTAN_HAS_CLASSICMCELIECE)
95
   #include <botan/cmce.h>
96
#endif
97

98
namespace {
99

100
#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
101

102
// These are always called within an existing try/catch block
103

104
template <class ECPrivateKey_t>
105
int privkey_load_ec(std::unique_ptr<ECPrivateKey_t>& key, const Botan::BigInt& scalar, const char* curve_name) {
9✔
106
   if(curve_name == nullptr) {
9✔
107
      return BOTAN_FFI_ERROR_NULL_POINTER;
108
   }
109
   if(!Botan::EC_Group::supports_named_group(curve_name)) {
9✔
110
      return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
111
   }
112

113
   Botan::Null_RNG null_rng;
9✔
114
   const auto grp = Botan::EC_Group::from_name(curve_name);
9✔
115
   key.reset(new ECPrivateKey_t(null_rng, grp, scalar));
9✔
116
   return BOTAN_FFI_SUCCESS;
117
}
9✔
118

119
template <class ECPublicKey_t>
120
int pubkey_load_ec(std::unique_ptr<ECPublicKey_t>& key,
6✔
121
                   const Botan::BigInt& public_x,
122
                   const Botan::BigInt& public_y,
123
                   const char* curve_name) {
124
   if(curve_name == nullptr) {
6✔
125
      return BOTAN_FFI_ERROR_NULL_POINTER;
126
   }
127

128
   if(!Botan::EC_Group::supports_named_group(curve_name)) {
6✔
129
      return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
130
   }
131

132
   const auto group = Botan::EC_Group::from_name(curve_name);
6✔
133

134
   if(auto pt = Botan::EC_AffinePoint::from_bigint_xy(group, public_x, public_y)) {
6✔
135
      key.reset(new ECPublicKey_t(group, pt.value()));
6✔
136
      return BOTAN_FFI_SUCCESS;
6✔
137
   } else {
138
      return BOTAN_FFI_ERROR_BAD_PARAMETER;
139
   }
140
}
6✔
141

142
#endif
143

144
Botan::BigInt pubkey_get_field(const Botan::Public_Key& key, std::string_view field) {
40✔
145
#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
146
   // Not currently handled by get_int_field
147
   if(const Botan::EC_PublicKey* ecc = dynamic_cast<const Botan::EC_PublicKey*>(&key)) {
40✔
148
      if(field == "public_x") {
15✔
149
         return Botan::BigInt::from_bytes(ecc->_public_ec_point().x_bytes());
16✔
150
      } else if(field == "public_y") {
7✔
151
         return Botan::BigInt::from_bytes(ecc->_public_ec_point().y_bytes());
12✔
152
      }
153
   }
154
#endif
155

156
   try {
26✔
157
      return key.get_int_field(field);
26✔
158
   } catch(Botan::Unknown_PK_Field_Name&) {
2✔
159
      throw Botan_FFI::FFI_Error("Unknown key field", BOTAN_FFI_ERROR_BAD_PARAMETER);
2✔
160
   }
2✔
161
}
162

163
Botan::BigInt privkey_get_field(const Botan::Private_Key& key, std::string_view field) {
35✔
164
#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
165
   // Not currently handled by get_int_field
166
   if(const Botan::EC_PublicKey* ecc = dynamic_cast<const Botan::EC_PublicKey*>(&key)) {
35✔
167
      if(field == "public_x") {
16✔
168
         return Botan::BigInt::from_bytes(ecc->_public_ec_point().x_bytes());
4✔
169
      } else if(field == "public_y") {
14✔
170
         return Botan::BigInt::from_bytes(ecc->_public_ec_point().y_bytes());
8✔
171
      }
172
   }
173
#endif
174

175
   try {
29✔
176
      return key.get_int_field(field);
29✔
177
   } catch(Botan::Unknown_PK_Field_Name&) {
4✔
178
      throw Botan_FFI::FFI_Error("Unknown key field", BOTAN_FFI_ERROR_BAD_PARAMETER);
4✔
179
   }
4✔
180
}
181

182
}  // namespace
183

184
extern "C" {
185

186
using namespace Botan_FFI;
187

188
int botan_pubkey_get_field(botan_mp_t output, botan_pubkey_t key, const char* field_name_cstr) {
40✔
189
   if(field_name_cstr == nullptr) {
40✔
190
      return BOTAN_FFI_ERROR_NULL_POINTER;
191
   }
192

193
   const std::string field_name(field_name_cstr);
40✔
194

195
   return BOTAN_FFI_VISIT(key, [=](const auto& k) { safe_get(output) = pubkey_get_field(k, field_name); });
158✔
196
}
40✔
197

198
int botan_privkey_get_field(botan_mp_t output, botan_privkey_t key, const char* field_name_cstr) {
35✔
199
   if(field_name_cstr == nullptr) {
35✔
200
      return BOTAN_FFI_ERROR_NULL_POINTER;
201
   }
202

203
   const std::string field_name(field_name_cstr);
35✔
204

205
   return BOTAN_FFI_VISIT(key, [=](const auto& k) { safe_get(output) = privkey_get_field(k, field_name); });
136✔
206
}
35✔
207

208
/* RSA specific operations */
209

210
int botan_privkey_create_rsa(botan_privkey_t* key_obj, botan_rng_t rng_obj, size_t n_bits) {
1✔
211
   if(n_bits < 1024 || n_bits > 16 * 1024) {
1✔
212
      return BOTAN_FFI_ERROR_BAD_PARAMETER;
213
   }
214

215
   std::string n_str = std::to_string(n_bits);
1✔
216

217
   return botan_privkey_create(key_obj, "RSA", n_str.c_str(), rng_obj);
1✔
218
}
1✔
219

220
int botan_privkey_load_rsa(botan_privkey_t* key, botan_mp_t rsa_p, botan_mp_t rsa_q, botan_mp_t rsa_e) {
2✔
221
#if defined(BOTAN_HAS_RSA)
222
   if(key == nullptr) {
2✔
223
      return BOTAN_FFI_ERROR_NULL_POINTER;
224
   }
225
   *key = nullptr;
2✔
226

227
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
228
      auto rsa = std::make_unique<Botan::RSA_PrivateKey>(safe_get(rsa_p), safe_get(rsa_q), safe_get(rsa_e));
2✔
229
      *key = new botan_privkey_struct(std::move(rsa));
2✔
230
      return BOTAN_FFI_SUCCESS;
2✔
231
   });
2✔
232
#else
233
   BOTAN_UNUSED(key, rsa_p, rsa_q, rsa_e);
234
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
235
#endif
236
}
237

238
int botan_privkey_load_rsa_pkcs1(botan_privkey_t* key, const uint8_t bits[], size_t len) {
1✔
239
#if defined(BOTAN_HAS_RSA)
240
   if(key == nullptr || bits == nullptr) {
1✔
241
      return BOTAN_FFI_ERROR_NULL_POINTER;
242
   }
243
   *key = nullptr;
1✔
244

245
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
246
      Botan::AlgorithmIdentifier alg_id("RSA", Botan::AlgorithmIdentifier::USE_NULL_PARAM);
1✔
247
      auto rsa = std::make_unique<Botan::RSA_PrivateKey>(alg_id, std::span{bits, len});
1✔
248
      *key = new botan_privkey_struct(std::move(rsa));
1✔
249
      return BOTAN_FFI_SUCCESS;
1✔
250
   });
1✔
251
#else
252
   BOTAN_UNUSED(key, bits, len);
253
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
254
#endif
255
}
256

257
int botan_pubkey_load_rsa(botan_pubkey_t* key, botan_mp_t n, botan_mp_t e) {
4✔
258
#if defined(BOTAN_HAS_RSA)
259
   if(key == nullptr) {
4✔
260
      return BOTAN_FFI_ERROR_NULL_POINTER;
261
   }
262
   *key = nullptr;
4✔
263
   return ffi_guard_thunk(__func__, [=]() -> int {
8✔
264
      auto rsa = std::make_unique<Botan::RSA_PublicKey>(safe_get(n), safe_get(e));
4✔
265
      *key = new botan_pubkey_struct(std::move(rsa));
6✔
266
      return BOTAN_FFI_SUCCESS;
3✔
267
   });
3✔
268
#else
269
   BOTAN_UNUSED(key, n, e);
270
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
271
#endif
272
}
273

274
int botan_privkey_rsa_get_p(botan_mp_t p, botan_privkey_t key) {
1✔
275
   return botan_privkey_get_field(p, key, "p");
1✔
276
}
277

278
int botan_privkey_rsa_get_q(botan_mp_t q, botan_privkey_t key) {
1✔
279
   return botan_privkey_get_field(q, key, "q");
1✔
280
}
281

282
int botan_privkey_rsa_get_n(botan_mp_t n, botan_privkey_t key) {
1✔
283
   return botan_privkey_get_field(n, key, "n");
1✔
284
}
285

286
int botan_privkey_rsa_get_e(botan_mp_t e, botan_privkey_t key) {
1✔
287
   return botan_privkey_get_field(e, key, "e");
1✔
288
}
289

290
int botan_privkey_rsa_get_d(botan_mp_t d, botan_privkey_t key) {
1✔
291
   return botan_privkey_get_field(d, key, "d");
1✔
292
}
293

294
int botan_pubkey_rsa_get_e(botan_mp_t e, botan_pubkey_t key) {
1✔
295
   return botan_pubkey_get_field(e, key, "e");
1✔
296
}
297

298
int botan_pubkey_rsa_get_n(botan_mp_t n, botan_pubkey_t key) {
1✔
299
   return botan_pubkey_get_field(n, key, "n");
1✔
300
}
301

302
int botan_privkey_rsa_get_privkey(botan_privkey_t rsa_key, uint8_t out[], size_t* out_len, uint32_t flags) {
4✔
303
#if defined(BOTAN_HAS_RSA)
304
   return BOTAN_FFI_VISIT(rsa_key, [=](const auto& k) -> int {
14✔
305
      if(const Botan::RSA_PrivateKey* rsa = dynamic_cast<const Botan::RSA_PrivateKey*>(&k)) {
306
         if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
307
            return write_vec_output(out, out_len, rsa->private_key_bits());
308
         } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
309
            return write_str_output(out, out_len, Botan::PEM_Code::encode(rsa->private_key_bits(), "RSA PRIVATE KEY"));
310
         } else {
311
            return BOTAN_FFI_ERROR_BAD_FLAG;
312
         }
313
      } else {
314
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
315
      }
316
   });
317
#else
318
   BOTAN_UNUSED(rsa_key, out, out_len, flags);
319
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
320
#endif
321
}
322

323
/* DSA specific operations */
324
int botan_privkey_create_dsa(botan_privkey_t* key, botan_rng_t rng_obj, size_t pbits, size_t qbits) {
1✔
325
#if defined(BOTAN_HAS_DSA)
326

327
   if((rng_obj == nullptr) || (key == nullptr)) {
1✔
328
      return BOTAN_FFI_ERROR_NULL_POINTER;
329
   }
330

331
   if((pbits % 64) || (qbits % 8) || (pbits < 1024) || (pbits > 3072) || (qbits < 160) || (qbits > 256)) {
1✔
332
      return BOTAN_FFI_ERROR_BAD_PARAMETER;
333
   }
334

335
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
336
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
1✔
337
      Botan::DL_Group group(rng, Botan::DL_Group::Prime_Subgroup, pbits, qbits);
1✔
338
      auto dsa = std::make_unique<Botan::DSA_PrivateKey>(rng, group);
1✔
339
      *key = new botan_privkey_struct(std::move(dsa));
2✔
340
      return BOTAN_FFI_SUCCESS;
2✔
341
   });
2✔
342
#else
343
   BOTAN_UNUSED(key, rng_obj, pbits, qbits);
344
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
345
#endif
346
}
347

348
int botan_privkey_load_dsa(botan_privkey_t* key, botan_mp_t p, botan_mp_t q, botan_mp_t g, botan_mp_t x) {
2✔
349
#if defined(BOTAN_HAS_DSA)
350
   if(key == nullptr) {
2✔
351
      return BOTAN_FFI_ERROR_NULL_POINTER;
352
   }
353
   *key = nullptr;
2✔
354

355
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
356
      Botan::DL_Group group(safe_get(p), safe_get(q), safe_get(g));
2✔
357
      auto dsa = std::make_unique<Botan::DSA_PrivateKey>(group, safe_get(x));
2✔
358
      *key = new botan_privkey_struct(std::move(dsa));
4✔
359
      return BOTAN_FFI_SUCCESS;
4✔
360
   });
4✔
361
#else
362
   BOTAN_UNUSED(key, p, q, g, x);
363
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
364
#endif
365
}
366

367
int botan_pubkey_load_dsa(botan_pubkey_t* key, botan_mp_t p, botan_mp_t q, botan_mp_t g, botan_mp_t y) {
2✔
368
#if defined(BOTAN_HAS_DSA)
369
   if(key == nullptr) {
2✔
370
      return BOTAN_FFI_ERROR_NULL_POINTER;
371
   }
372
   *key = nullptr;
2✔
373

374
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
375
      Botan::DL_Group group(safe_get(p), safe_get(q), safe_get(g));
2✔
376
      auto dsa = std::make_unique<Botan::DSA_PublicKey>(group, safe_get(y));
2✔
377
      *key = new botan_pubkey_struct(std::move(dsa));
4✔
378
      return BOTAN_FFI_SUCCESS;
2✔
379
   });
4✔
380
#else
381
   BOTAN_UNUSED(key, p, q, g, y);
382
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
383
#endif
384
}
385

386
int botan_privkey_dsa_get_x(botan_mp_t x, botan_privkey_t key) {
2✔
387
   return botan_privkey_get_field(x, key, "x");
2✔
388
}
389

390
int botan_pubkey_dsa_get_p(botan_mp_t p, botan_pubkey_t key) {
2✔
391
   return botan_pubkey_get_field(p, key, "p");
2✔
392
}
393

394
int botan_pubkey_dsa_get_q(botan_mp_t q, botan_pubkey_t key) {
2✔
395
   return botan_pubkey_get_field(q, key, "q");
2✔
396
}
397

398
int botan_pubkey_dsa_get_g(botan_mp_t g, botan_pubkey_t key) {
2✔
399
   return botan_pubkey_get_field(g, key, "g");
2✔
400
}
401

402
int botan_pubkey_dsa_get_y(botan_mp_t y, botan_pubkey_t key) {
2✔
403
   return botan_pubkey_get_field(y, key, "y");
2✔
404
}
405

406
int botan_privkey_create_ecdsa(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
1✔
407
   return botan_privkey_create(key_obj, "ECDSA", param_str, rng_obj);
1✔
408
}
409

410
/* ECDSA specific operations */
411

412
int botan_pubkey_ecc_key_used_explicit_encoding(botan_pubkey_t key) {
1✔
413
#if defined(BOTAN_HAS_ECC_KEY)
414
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
415
      const Botan::Public_Key& pub_key = safe_get(key);
1✔
416
      const Botan::EC_PublicKey* ec_key = dynamic_cast<const Botan::EC_PublicKey*>(&pub_key);
1✔
417

418
      if(ec_key == nullptr) {
1✔
419
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
420
      }
421

422
      return ec_key->domain().used_explicit_encoding() ? 1 : 0;
1✔
423
   });
1✔
424
#else
425
   BOTAN_UNUSED(key);
426
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
427
#endif
428
}
429

430
int botan_pubkey_load_ecdsa(botan_pubkey_t* key,
2✔
431
                            const botan_mp_t public_x,
432
                            const botan_mp_t public_y,
433
                            const char* curve_name) {
434
#if defined(BOTAN_HAS_ECDSA)
435
   if(key == nullptr || curve_name == nullptr) {
2✔
436
      return BOTAN_FFI_ERROR_NULL_POINTER;
437
   }
438
   *key = nullptr;
2✔
439

440
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
441
      std::unique_ptr<Botan::ECDSA_PublicKey> p_key;
2✔
442

443
      int rc = pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name);
2✔
444
      if(rc == BOTAN_FFI_SUCCESS) {
2✔
445
         *key = new botan_pubkey_struct(std::move(p_key));
4✔
446
      }
447

448
      return rc;
2✔
449
   });
2✔
450
#else
451
   BOTAN_UNUSED(key, public_x, public_y, curve_name);
452
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
453
#endif
454
}
455

456
int botan_privkey_load_ecdsa(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
2✔
457
#if defined(BOTAN_HAS_ECDSA)
458
   if(key == nullptr || curve_name == nullptr) {
2✔
459
      return BOTAN_FFI_ERROR_NULL_POINTER;
460
   }
461
   *key = nullptr;
2✔
462

463
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
464
      std::unique_ptr<Botan::ECDSA_PrivateKey> p_key;
2✔
465
      int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
2✔
466
      if(rc == BOTAN_FFI_SUCCESS) {
2✔
467
         *key = new botan_privkey_struct(std::move(p_key));
4✔
468
      }
469
      return rc;
2✔
470
   });
2✔
471
#else
472
   BOTAN_UNUSED(key, scalar, curve_name);
473
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
474
#endif
475
}
476

477
/* ElGamal specific operations */
478
int botan_privkey_create_elgamal(botan_privkey_t* key, botan_rng_t rng_obj, size_t pbits, size_t qbits) {
1✔
479
#if defined(BOTAN_HAS_ELGAMAL)
480
   if(key == nullptr || rng_obj == nullptr) {
1✔
481
      return BOTAN_FFI_ERROR_NULL_POINTER;
482
   }
483
   *key = nullptr;
1✔
484

485
   if(pbits < 1024 || qbits < 160) {
1✔
486
      return BOTAN_FFI_ERROR_BAD_PARAMETER;
487
   }
488

489
   Botan::DL_Group::PrimeType prime_type =
1✔
490
      ((pbits - 1) == qbits) ? Botan::DL_Group::Strong : Botan::DL_Group::Prime_Subgroup;
1✔
491

492
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
493
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
1✔
494
      Botan::DL_Group group(rng, prime_type, pbits, qbits);
1✔
495
      auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(rng, group);
1✔
496
      *key = new botan_privkey_struct(std::move(elg));
2✔
497
      return BOTAN_FFI_SUCCESS;
2✔
498
   });
2✔
499
#else
500
   BOTAN_UNUSED(key, rng_obj, pbits, qbits);
501
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
502
#endif
503
}
504

505
int botan_pubkey_load_elgamal(botan_pubkey_t* key, botan_mp_t p, botan_mp_t g, botan_mp_t y) {
2✔
506
#if defined(BOTAN_HAS_ELGAMAL)
507
   if(key == nullptr) {
2✔
508
      return BOTAN_FFI_ERROR_NULL_POINTER;
509
   }
510
   *key = nullptr;
2✔
511
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
512
      Botan::DL_Group group(safe_get(p), safe_get(g));
2✔
513
      auto elg = std::make_unique<Botan::ElGamal_PublicKey>(group, safe_get(y));
2✔
514
      *key = new botan_pubkey_struct(std::move(elg));
4✔
515
      return BOTAN_FFI_SUCCESS;
2✔
516
   });
4✔
517
#else
518
   BOTAN_UNUSED(key, p, g, y);
519
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
520
#endif
521
}
522

523
int botan_privkey_load_elgamal(botan_privkey_t* key, botan_mp_t p, botan_mp_t g, botan_mp_t x) {
2✔
524
#if defined(BOTAN_HAS_ELGAMAL)
525
   if(key == nullptr) {
2✔
526
      return BOTAN_FFI_ERROR_NULL_POINTER;
527
   }
528
   *key = nullptr;
2✔
529
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
530
      Botan::DL_Group group(safe_get(p), safe_get(g));
2✔
531
      auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(group, safe_get(x));
2✔
532
      *key = new botan_privkey_struct(std::move(elg));
4✔
533
      return BOTAN_FFI_SUCCESS;
4✔
534
   });
4✔
535
#else
536
   BOTAN_UNUSED(key, p, g, x);
537
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
538
#endif
539
}
540

541
/* Diffie Hellman specific operations */
542

543
int botan_privkey_create_dh(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
2✔
544
   return botan_privkey_create(key_obj, "DH", param_str, rng_obj);
2✔
545
}
546

547
int botan_privkey_load_dh(botan_privkey_t* key, botan_mp_t p, botan_mp_t g, botan_mp_t x) {
1✔
548
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
549
   if(key == nullptr) {
1✔
550
      return BOTAN_FFI_ERROR_NULL_POINTER;
551
   }
552
   *key = nullptr;
1✔
553
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
554
      Botan::DL_Group group(safe_get(p), safe_get(g));
1✔
555
      auto dh = std::make_unique<Botan::DH_PrivateKey>(group, safe_get(x));
1✔
556
      *key = new botan_privkey_struct(std::move(dh));
2✔
557
      return BOTAN_FFI_SUCCESS;
2✔
558
   });
2✔
559
#else
560
   BOTAN_UNUSED(key, p, g, x);
561
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
562
#endif
563
}
564

565
int botan_pubkey_load_dh(botan_pubkey_t* key, botan_mp_t p, botan_mp_t g, botan_mp_t y) {
1✔
566
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
567
   if(key == nullptr) {
1✔
568
      return BOTAN_FFI_ERROR_NULL_POINTER;
569
   }
570
   *key = nullptr;
1✔
571
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
572
      Botan::DL_Group group(safe_get(p), safe_get(g));
1✔
573
      auto dh = std::make_unique<Botan::DH_PublicKey>(group, safe_get(y));
1✔
574
      *key = new botan_pubkey_struct(std::move(dh));
2✔
575
      return BOTAN_FFI_SUCCESS;
1✔
576
   });
2✔
577
#else
578
   BOTAN_UNUSED(key, p, g, y);
579
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
580
#endif
581
}
582

583
/* ECDH + x25519/x448 specific operations */
584

585
int botan_privkey_create_ecdh(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
2✔
586
   if(key_obj == nullptr || param_str == nullptr) {
2✔
587
      return BOTAN_FFI_ERROR_NULL_POINTER;
588
   }
589
   *key_obj = nullptr;
2✔
590

591
   const std::string params(param_str);
2✔
592

593
   if(params == "X25519" || params == "x25519" || params == "curve25519") {
2✔
594
      return botan_privkey_create(key_obj, "X25519", "", rng_obj);
×
595
   }
596

597
   if(params == "X448" || params == "x448") {
2✔
598
      return botan_privkey_create(key_obj, "X448", "", rng_obj);
×
599
   }
600

601
   return botan_privkey_create(key_obj, "ECDH", param_str, rng_obj);
2✔
602
}
2✔
603

604
int botan_pubkey_load_ecdh(botan_pubkey_t* key,
1✔
605
                           const botan_mp_t public_x,
606
                           const botan_mp_t public_y,
607
                           const char* curve_name) {
608
#if defined(BOTAN_HAS_ECDH)
609
   if(key == nullptr || curve_name == nullptr) {
1✔
610
      return BOTAN_FFI_ERROR_NULL_POINTER;
611
   }
612
   *key = nullptr;
1✔
613
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
614
      std::unique_ptr<Botan::ECDH_PublicKey> p_key;
1✔
615
      int rc = pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name);
1✔
616

617
      if(rc == BOTAN_FFI_SUCCESS) {
1✔
618
         *key = new botan_pubkey_struct(std::move(p_key));
2✔
619
      }
620
      return rc;
1✔
621
   });
1✔
622
#else
623
   BOTAN_UNUSED(key, public_x, public_y, curve_name);
624
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
625
#endif
626
}
627

628
int botan_privkey_load_ecdh(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
4✔
629
#if defined(BOTAN_HAS_ECDH)
630
   if(key == nullptr || curve_name == nullptr) {
4✔
631
      return BOTAN_FFI_ERROR_NULL_POINTER;
632
   }
633
   *key = nullptr;
4✔
634
   return ffi_guard_thunk(__func__, [=]() -> int {
8✔
635
      std::unique_ptr<Botan::ECDH_PrivateKey> p_key;
4✔
636
      int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
4✔
637
      if(rc == BOTAN_FFI_SUCCESS) {
4✔
638
         *key = new botan_privkey_struct(std::move(p_key));
8✔
639
      }
640
      return rc;
4✔
641
   });
4✔
642
#else
643
   BOTAN_UNUSED(key, scalar, curve_name);
644
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
645
#endif
646
}
647

648
/* SM2 specific operations */
649

650
int botan_pubkey_sm2_compute_za(
2✔
651
   uint8_t out[], size_t* out_len, const char* ident, const char* hash_algo, const botan_pubkey_t key) {
652
   if(out == nullptr || out_len == nullptr || ident == nullptr || hash_algo == nullptr || key == nullptr) {
2✔
653
      return BOTAN_FFI_ERROR_NULL_POINTER;
654
   }
655

656
#if defined(BOTAN_HAS_SM2)
657
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
658
      const Botan::Public_Key& pub_key = safe_get(key);
2✔
659
      const Botan::EC_PublicKey* ec_key = dynamic_cast<const Botan::EC_PublicKey*>(&pub_key);
2✔
660

661
      if(ec_key == nullptr) {
2✔
662
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
663
      }
664

665
      if(ec_key->algo_name() != "SM2") {
2✔
666
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
667
      }
668

669
      const std::string ident_str(ident);
2✔
670
      std::unique_ptr<Botan::HashFunction> hash = Botan::HashFunction::create_or_throw(hash_algo);
2✔
671

672
      const auto& pt = ec_key->_public_ec_point();
2✔
673

674
      const auto za = Botan::sm2_compute_za(*hash, ident_str, ec_key->domain(), pt);
2✔
675

676
      return write_vec_output(out, out_len, za);
2✔
677
   });
4✔
678
#else
679
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
680
#endif
681
}
682

683
int botan_pubkey_load_sm2(botan_pubkey_t* key,
3✔
684
                          const botan_mp_t public_x,
685
                          const botan_mp_t public_y,
686
                          const char* curve_name) {
687
#if defined(BOTAN_HAS_SM2)
688
   if(key == nullptr || curve_name == nullptr) {
3✔
689
      return BOTAN_FFI_ERROR_NULL_POINTER;
690
   }
691
   *key = nullptr;
3✔
692

693
   return ffi_guard_thunk(__func__, [=]() -> int {
6✔
694
      std::unique_ptr<Botan::SM2_PublicKey> p_key;
3✔
695
      if(!pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name)) {
3✔
696
         *key = new botan_pubkey_struct(std::move(p_key));
6✔
697
         return BOTAN_FFI_SUCCESS;
3✔
698
      }
699
      return BOTAN_FFI_ERROR_UNKNOWN_ERROR;
700
   });
3✔
701
#else
702
   BOTAN_UNUSED(key, public_x, public_y, curve_name);
703
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
704
#endif
705
}
706

707
int botan_privkey_load_sm2(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
3✔
708
#if defined(BOTAN_HAS_SM2)
709
   if(key == nullptr || curve_name == nullptr) {
3✔
710
      return BOTAN_FFI_ERROR_NULL_POINTER;
711
   }
712
   *key = nullptr;
3✔
713

714
   return ffi_guard_thunk(__func__, [=]() -> int {
6✔
715
      std::unique_ptr<Botan::SM2_PrivateKey> p_key;
3✔
716
      int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
3✔
717

718
      if(rc == BOTAN_FFI_SUCCESS) {
3✔
719
         *key = new botan_privkey_struct(std::move(p_key));
6✔
720
      }
721
      return rc;
3✔
722
   });
3✔
723
#else
724
   BOTAN_UNUSED(key, scalar, curve_name);
725
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
726
#endif
727
}
728

729
int botan_pubkey_load_sm2_enc(botan_pubkey_t* key,
1✔
730
                              const botan_mp_t public_x,
731
                              const botan_mp_t public_y,
732
                              const char* curve_name) {
733
   return botan_pubkey_load_sm2(key, public_x, public_y, curve_name);
1✔
734
}
735

736
int botan_privkey_load_sm2_enc(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
1✔
737
   return botan_privkey_load_sm2(key, scalar, curve_name);
1✔
738
}
739

740
/* Ed25519 specific operations */
741

742
int botan_privkey_load_ed25519(botan_privkey_t* key, const uint8_t privkey[32]) {
1✔
743
#if defined(BOTAN_HAS_ED25519)
744
   if(key == nullptr) {
1✔
745
      return BOTAN_FFI_ERROR_NULL_POINTER;
746
   }
747
   *key = nullptr;
1✔
748
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
749
      auto ed25519 =
1✔
750
         std::make_unique<Botan::Ed25519_PrivateKey>(Botan::Ed25519_PrivateKey::from_seed(std::span{privkey, 32}));
2✔
751
      *key = new botan_privkey_struct(std::move(ed25519));
1✔
752
      return BOTAN_FFI_SUCCESS;
1✔
753
   });
1✔
754
#else
755
   BOTAN_UNUSED(key, privkey);
756
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
757
#endif
758
}
759

760
int botan_pubkey_load_ed25519(botan_pubkey_t* key, const uint8_t pubkey[32]) {
1✔
761
#if defined(BOTAN_HAS_ED25519)
762
   if(key == nullptr) {
1✔
763
      return BOTAN_FFI_ERROR_NULL_POINTER;
764
   }
765
   *key = nullptr;
1✔
766
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
767
      const std::vector<uint8_t> pubkey_vec(pubkey, pubkey + 32);
1✔
768
      auto ed25519 = std::make_unique<Botan::Ed25519_PublicKey>(pubkey_vec);
1✔
769
      *key = new botan_pubkey_struct(std::move(ed25519));
2✔
770
      return BOTAN_FFI_SUCCESS;
1✔
771
   });
2✔
772
#else
773
   BOTAN_UNUSED(key, pubkey);
774
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
775
#endif
776
}
777

778
int botan_privkey_ed25519_get_privkey(botan_privkey_t key, uint8_t output[64]) {
1✔
779
#if defined(BOTAN_HAS_ED25519)
780
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
4✔
781
      if(auto ed = dynamic_cast<const Botan::Ed25519_PrivateKey*>(&k)) {
782
         const auto ed_key = ed->raw_private_key_bits();
783
         if(ed_key.size() != 64) {
784
            return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE;
785
         }
786
         Botan::copy_mem(output, ed_key.data(), ed_key.size());
787
         return BOTAN_FFI_SUCCESS;
788
      } else {
789
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
790
      }
791
   });
792
#else
793
   BOTAN_UNUSED(key, output);
794
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
795
#endif
796
}
797

798
int botan_pubkey_ed25519_get_pubkey(botan_pubkey_t key, uint8_t output[32]) {
1✔
799
#if defined(BOTAN_HAS_ED25519)
800
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
2✔
801
      if(auto ed = dynamic_cast<const Botan::Ed25519_PublicKey*>(&k)) {
802
         const std::vector<uint8_t>& ed_key = ed->get_public_key();
803
         if(ed_key.size() != 32) {
804
            return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE;
805
         }
806
         Botan::copy_mem(output, ed_key.data(), ed_key.size());
807
         return BOTAN_FFI_SUCCESS;
808
      } else {
809
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
810
      }
811
   });
812
#else
813
   BOTAN_UNUSED(key, output);
814
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
815
#endif
816
}
817

818
/* Ed448 specific operations */
819

820
int botan_privkey_load_ed448(botan_privkey_t* key, const uint8_t privkey[57]) {
1✔
821
#if defined(BOTAN_HAS_ED448)
822
   if(key == nullptr) {
1✔
823
      return BOTAN_FFI_ERROR_NULL_POINTER;
824
   }
825
   *key = nullptr;
1✔
826
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
827
      auto ed448 = std::make_unique<Botan::Ed448_PrivateKey>(std::span(privkey, 57));
1✔
828
      *key = new botan_privkey_struct(std::move(ed448));
2✔
829
      return BOTAN_FFI_SUCCESS;
1✔
830
   });
1✔
831
#else
832
   BOTAN_UNUSED(key, privkey);
833
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
834
#endif
835
}
836

837
int botan_pubkey_load_ed448(botan_pubkey_t* key, const uint8_t pubkey[57]) {
1✔
838
#if defined(BOTAN_HAS_ED448)
839
   if(key == nullptr) {
1✔
840
      return BOTAN_FFI_ERROR_NULL_POINTER;
841
   }
842
   *key = nullptr;
1✔
843
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
844
      auto ed448 = std::make_unique<Botan::Ed448_PublicKey>(std::span(pubkey, 57));
1✔
845
      *key = new botan_pubkey_struct(std::move(ed448));
2✔
846
      return BOTAN_FFI_SUCCESS;
1✔
847
   });
1✔
848
#else
849
   BOTAN_UNUSED(key, pubkey);
850
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
851
#endif
852
}
853

854
int botan_privkey_ed448_get_privkey(botan_privkey_t key, uint8_t output[57]) {
1✔
855
#if defined(BOTAN_HAS_ED448)
856
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
4✔
857
      if(auto ed = dynamic_cast<const Botan::Ed448_PrivateKey*>(&k)) {
858
         const auto ed_key = ed->raw_private_key_bits();
859
         Botan::copy_mem(std::span(output, 57), ed_key);
860
         return BOTAN_FFI_SUCCESS;
861
      } else {
862
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
863
      }
864
   });
865
#else
866
   BOTAN_UNUSED(key, output);
867
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
868
#endif
869
}
870

871
int botan_pubkey_ed448_get_pubkey(botan_pubkey_t key, uint8_t output[57]) {
1✔
872
#if defined(BOTAN_HAS_ED448)
873
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
3✔
874
      if(auto ed = dynamic_cast<const Botan::Ed448_PublicKey*>(&k)) {
875
         const auto ed_key = ed->public_key_bits();
876
         Botan::copy_mem(std::span(output, 57), ed_key);
877
         return BOTAN_FFI_SUCCESS;
878
      } else {
879
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
880
      }
881
   });
882
#else
883
   BOTAN_UNUSED(key, output);
884
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
885
#endif
886
}
887

888
/* X25519 specific operations */
889

890
int botan_privkey_load_x25519(botan_privkey_t* key, const uint8_t privkey[32]) {
1✔
891
#if defined(BOTAN_HAS_X25519)
892
   if(key == nullptr) {
1✔
893
      return BOTAN_FFI_ERROR_NULL_POINTER;
894
   }
895
   *key = nullptr;
1✔
896
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
897
      auto x25519 = std::make_unique<Botan::X25519_PrivateKey>(std::span{privkey, 32});
1✔
898
      *key = new botan_privkey_struct(std::move(x25519));
2✔
899
      return BOTAN_FFI_SUCCESS;
1✔
900
   });
1✔
901
#else
902
   BOTAN_UNUSED(key, privkey);
903
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
904
#endif
905
}
906

907
int botan_pubkey_load_x25519(botan_pubkey_t* key, const uint8_t pubkey[32]) {
1✔
908
#if defined(BOTAN_HAS_X25519)
909
   if(key == nullptr) {
1✔
910
      return BOTAN_FFI_ERROR_NULL_POINTER;
911
   }
912
   *key = nullptr;
1✔
913
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
914
      auto x25519 = std::make_unique<Botan::X25519_PublicKey>(std::span{pubkey, 32});
1✔
915
      *key = new botan_pubkey_struct(std::move(x25519));
2✔
916
      return BOTAN_FFI_SUCCESS;
1✔
917
   });
1✔
918
#else
919
   BOTAN_UNUSED(key, pubkey);
920
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
921
#endif
922
}
923

924
int botan_privkey_x25519_get_privkey(botan_privkey_t key, uint8_t output[32]) {
1✔
925
#if defined(BOTAN_HAS_X25519)
926
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
4✔
927
      if(auto x25519 = dynamic_cast<const Botan::X25519_PrivateKey*>(&k)) {
928
         const auto x25519_key = x25519->raw_private_key_bits();
929
         if(x25519_key.size() != 32) {
930
            return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE;
931
         }
932
         Botan::copy_mem(output, x25519_key.data(), x25519_key.size());
933
         return BOTAN_FFI_SUCCESS;
934
      } else {
935
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
936
      }
937
   });
938
#else
939
   BOTAN_UNUSED(key, output);
940
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
941
#endif
942
}
943

944
int botan_pubkey_x25519_get_pubkey(botan_pubkey_t key, uint8_t output[32]) {
2✔
945
#if defined(BOTAN_HAS_X25519)
946
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
6✔
947
      if(auto x25519 = dynamic_cast<const Botan::X25519_PublicKey*>(&k)) {
948
         Botan::copy_mem(std::span{output, 32}, x25519->raw_public_key_bits());
949
         return BOTAN_FFI_SUCCESS;
950
      } else {
951
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
952
      }
953
   });
954
#else
955
   BOTAN_UNUSED(key, output);
956
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
957
#endif
958
}
959

960
/* X448 specific operations */
961

962
int botan_privkey_load_x448(botan_privkey_t* key, const uint8_t privkey[56]) {
1✔
963
#if defined(BOTAN_HAS_X448)
964
   if(key == nullptr) {
1✔
965
      return BOTAN_FFI_ERROR_NULL_POINTER;
966
   }
967
   *key = nullptr;
1✔
968
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
969
      auto x448 = std::make_unique<Botan::X448_PrivateKey>(std::span{privkey, 56});
1✔
970
      *key = new botan_privkey_struct(std::move(x448));
2✔
971
      return BOTAN_FFI_SUCCESS;
1✔
972
   });
1✔
973
#else
974
   BOTAN_UNUSED(key, privkey);
975
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
976
#endif
977
}
978

979
int botan_pubkey_load_x448(botan_pubkey_t* key, const uint8_t pubkey[56]) {
1✔
980
#if defined(BOTAN_HAS_X448)
981
   if(key == nullptr) {
1✔
982
      return BOTAN_FFI_ERROR_NULL_POINTER;
983
   }
984
   *key = nullptr;
1✔
985
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
986
      auto x448 = std::make_unique<Botan::X448_PublicKey>(std::span{pubkey, 56});
1✔
987
      *key = new botan_pubkey_struct(std::move(x448));
2✔
988
      return BOTAN_FFI_SUCCESS;
1✔
989
   });
1✔
990
#else
991
   BOTAN_UNUSED(key, pubkey);
992
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
993
#endif
994
}
995

996
int botan_privkey_x448_get_privkey(botan_privkey_t key, uint8_t output[56]) {
1✔
997
#if defined(BOTAN_HAS_X448)
998
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
4✔
999
      if(auto x448 = dynamic_cast<const Botan::X448_PrivateKey*>(&k)) {
1000
         const auto x448_key = x448->raw_private_key_bits();
1001
         Botan::copy_mem(std::span{output, 56}, x448_key);
1002
         return BOTAN_FFI_SUCCESS;
1003
      } else {
1004
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1005
      }
1006
   });
1007
#else
1008
   BOTAN_UNUSED(key, output);
1009
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1010
#endif
1011
}
1012

1013
int botan_pubkey_x448_get_pubkey(botan_pubkey_t key, uint8_t output[56]) {
2✔
1014
#if defined(BOTAN_HAS_X448)
1015
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
6✔
1016
      if(auto x448 = dynamic_cast<const Botan::X448_PublicKey*>(&k)) {
1017
         Botan::copy_mem(std::span{output, 56}, x448->raw_public_key_bits());
1018
         return BOTAN_FFI_SUCCESS;
1019
      } else {
1020
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1021
      }
1022
   });
1023
#else
1024
   BOTAN_UNUSED(key, output);
1025
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1026
#endif
1027
}
1028

1029
/*
1030
* Algorithm specific key operations: Kyber
1031
*/
1032

1033
int botan_privkey_load_kyber(botan_privkey_t* key, const uint8_t privkey[], size_t key_len) {
4✔
1034
#if defined(BOTAN_HAS_KYBER)
1035
   if(key == nullptr) {
4✔
1036
      return BOTAN_FFI_ERROR_NULL_POINTER;
1037
   }
1038
   *key = nullptr;
4✔
1039

1040
   const auto mode = [](size_t len) -> std::optional<Botan::KyberMode> {
12✔
1041
      if(len == 1632) {
4✔
1042
         return Botan::KyberMode::Kyber512_R3;
1✔
1043
      } else if(len == 2400) {
3✔
1044
         return Botan::KyberMode::Kyber768_R3;
2✔
1045
      } else if(len == 3168) {
1✔
1046
         return Botan::KyberMode::Kyber1024_R3;
1✔
1047
      } else {
1048
         return {};
×
1049
      }
1050
   }(key_len);
4✔
1051

1052
   if(mode.has_value()) {
4✔
1053
      return ffi_guard_thunk(__func__, [=]() -> int {
8✔
1054
         auto kyber = std::make_unique<Botan::Kyber_PrivateKey>(std::span{privkey, key_len}, *mode);
4✔
1055
         *key = new botan_privkey_struct(std::move(kyber));
4✔
1056
         return BOTAN_FFI_SUCCESS;
4✔
1057
      });
4✔
1058
   } else {
1059
      return BOTAN_FFI_ERROR_BAD_PARAMETER;
1060
   }
1061
#else
1062
   BOTAN_UNUSED(key, key_len, privkey);
1063
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1064
#endif
1065
}
1066

1067
int botan_pubkey_load_kyber(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len) {
4✔
1068
#if defined(BOTAN_HAS_KYBER)
1069
   if(key == nullptr) {
4✔
1070
      return BOTAN_FFI_ERROR_NULL_POINTER;
1071
   }
1072
   *key = nullptr;
4✔
1073

1074
   const auto mode = [](size_t len) -> std::optional<Botan::KyberMode> {
12✔
1075
      if(len == 800) {
4✔
1076
         return Botan::KyberMode::Kyber512_R3;
1✔
1077
      } else if(len == 1184) {
3✔
1078
         return Botan::KyberMode::Kyber768_R3;
2✔
1079
      } else if(len == 1568) {
1✔
1080
         return Botan::KyberMode::Kyber1024_R3;
1✔
1081
      } else {
1082
         return {};
×
1083
      }
1084
   }(key_len);
4✔
1085

1086
   if(mode.has_value()) {
4✔
1087
      auto kyber = std::make_unique<Botan::Kyber_PublicKey>(std::span{pubkey, key_len}, *mode);
4✔
1088
      *key = new botan_pubkey_struct(std::move(kyber));
8✔
1089
      return BOTAN_FFI_SUCCESS;
4✔
1090
   } else {
4✔
1091
      return BOTAN_FFI_ERROR_BAD_PARAMETER;
1092
   }
1093
#else
1094
   BOTAN_UNUSED(key, pubkey, key_len);
1095
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1096
#endif
1097
}
1098

1099
int botan_privkey_view_kyber_raw_key(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) {
4✔
1100
#if defined(BOTAN_HAS_KYBER)
1101
   return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
16✔
1102
      if(auto kyber = dynamic_cast<const Botan::Kyber_PrivateKey*>(&k)) {
1103
         return invoke_view_callback(view, ctx, kyber->raw_private_key_bits());
1104
      } else {
1105
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1106
      }
1107
   });
1108
#else
1109
   BOTAN_UNUSED(key, ctx, view);
1110
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1111
#endif
1112
}
1113

1114
int botan_pubkey_view_kyber_raw_key(botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) {
8✔
1115
#if defined(BOTAN_HAS_KYBER)
1116
   return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
32✔
1117
      if(auto kyber = dynamic_cast<const Botan::Kyber_PublicKey*>(&k)) {
1118
         return invoke_view_callback(view, ctx, kyber->public_key_bits());
1119
      } else {
1120
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1121
      }
1122
   });
1123
#else
1124
   BOTAN_UNUSED(key, ctx, view);
1125
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1126
#endif
1127
}
1128

1129
/*
1130
* Algorithm specific key operations: ML-KEM
1131
*/
1132

1133
int botan_privkey_load_ml_kem(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* mlkem_mode) {
4✔
1134
#if defined(BOTAN_HAS_ML_KEM)
1135
   if(key == nullptr || privkey == nullptr || mlkem_mode == nullptr) {
4✔
1136
      return BOTAN_FFI_ERROR_NULL_POINTER;
1137
   }
1138

1139
   *key = nullptr;
4✔
1140

1141
   return ffi_guard_thunk(__func__, [=]() -> int {
8✔
1142
      auto mode = Botan::ML_KEM_Mode(mlkem_mode);
4✔
1143
      if(!mode.is_ml_kem()) {
4✔
1144
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1145
      }
1146

1147
      auto mlkem_key = std::make_unique<Botan::ML_KEM_PrivateKey>(std::span{privkey, key_len}, mode);
4✔
1148
      *key = new botan_privkey_struct(std::move(mlkem_key));
4✔
1149
      return BOTAN_FFI_SUCCESS;
4✔
1150
   });
4✔
1151
#else
1152
   BOTAN_UNUSED(key, key_len, privkey, mlkem_mode);
1153
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1154
#endif
1155
}
1156

1157
int botan_pubkey_load_ml_kem(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* mlkem_mode) {
4✔
1158
#if defined(BOTAN_HAS_ML_KEM)
1159
   if(key == nullptr || pubkey == nullptr || mlkem_mode == nullptr) {
4✔
1160
      return BOTAN_FFI_ERROR_NULL_POINTER;
1161
   }
1162

1163
   *key = nullptr;
4✔
1164

1165
   return ffi_guard_thunk(__func__, [=]() -> int {
8✔
1166
      auto mode = Botan::ML_KEM_Mode(mlkem_mode);
4✔
1167
      if(!mode.is_ml_kem()) {
4✔
1168
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1169
      }
1170

1171
      auto mlkem_key = std::make_unique<Botan::ML_KEM_PublicKey>(std::span{pubkey, key_len}, mode.mode());
4✔
1172
      *key = new botan_pubkey_struct(std::move(mlkem_key));
8✔
1173
      return BOTAN_FFI_SUCCESS;
4✔
1174
   });
4✔
1175
#else
1176
   BOTAN_UNUSED(key, key_len, pubkey, mlkem_mode);
1177
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1178
#endif
1179
}
1180

1181
/*
1182
* Algorithm specific key operations: ML-DSA
1183
*/
1184

1185
int botan_privkey_load_ml_dsa(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* mldsa_mode) {
4✔
1186
#if defined(BOTAN_HAS_ML_DSA)
1187
   if(key == nullptr || privkey == nullptr || mldsa_mode == nullptr) {
4✔
1188
      return BOTAN_FFI_ERROR_NULL_POINTER;
1189
   }
1190

1191
   *key = nullptr;
4✔
1192

1193
   return ffi_guard_thunk(__func__, [=]() -> int {
8✔
1194
      auto mode = Botan::ML_DSA_Mode(mldsa_mode);
4✔
1195
      if(!mode.is_ml_dsa()) {
4✔
1196
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1197
      }
1198

1199
      auto mldsa_key = std::make_unique<Botan::ML_DSA_PrivateKey>(std::span{privkey, key_len}, mode);
4✔
1200
      *key = new botan_privkey_struct(std::move(mldsa_key));
4✔
1201
      return BOTAN_FFI_SUCCESS;
4✔
1202
   });
4✔
1203
#else
1204
   BOTAN_UNUSED(key, key_len, privkey, mldsa_mode);
1205
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1206
#endif
1207
}
1208

1209
int botan_pubkey_load_ml_dsa(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* mldsa_mode) {
4✔
1210
#if defined(BOTAN_HAS_ML_DSA)
1211
   if(key == nullptr || pubkey == nullptr || mldsa_mode == nullptr) {
4✔
1212
      return BOTAN_FFI_ERROR_NULL_POINTER;
1213
   }
1214

1215
   *key = nullptr;
4✔
1216

1217
   return ffi_guard_thunk(__func__, [=]() -> int {
8✔
1218
      auto mode = Botan::ML_DSA_Mode(mldsa_mode);
4✔
1219
      if(!mode.is_ml_dsa()) {
4✔
1220
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1221
      }
1222

1223
      auto mldsa_key = std::make_unique<Botan::ML_DSA_PublicKey>(std::span{pubkey, key_len}, mode);
4✔
1224
      *key = new botan_pubkey_struct(std::move(mldsa_key));
8✔
1225
      return BOTAN_FFI_SUCCESS;
4✔
1226
   });
4✔
1227
#else
1228
   BOTAN_UNUSED(key, key_len, pubkey, mldsa_mode);
1229
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1230
#endif
1231
}
1232

1233
/*
1234
* Algorithm specific key operations: SLH-DSA
1235
*/
1236

1237
int botan_privkey_load_slh_dsa(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* slhdsa_mode) {
13✔
1238
#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
1239
   if(key == nullptr || privkey == nullptr || slhdsa_mode == nullptr) {
13✔
1240
      return BOTAN_FFI_ERROR_NULL_POINTER;
1241
   }
1242

1243
   *key = nullptr;
13✔
1244

1245
   return ffi_guard_thunk(__func__, [=]() -> int {
26✔
1246
      auto mode = Botan::SLH_DSA_Parameters::create(slhdsa_mode);
13✔
1247
      if(!mode.is_slh_dsa()) {
13✔
1248
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1249
      }
1250

1251
      auto slhdsa_key = std::make_unique<Botan::SLH_DSA_PrivateKey>(std::span{privkey, key_len}, mode);
13✔
1252
      *key = new botan_privkey_struct(std::move(slhdsa_key));
13✔
1253
      return BOTAN_FFI_SUCCESS;
13✔
1254
   });
13✔
1255
#else
1256
   BOTAN_UNUSED(key, key_len, privkey, slhdsa_mode);
1257
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1258
#endif
1259
}
1260

1261
int botan_pubkey_load_slh_dsa(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* slhdsa_mode) {
13✔
1262
#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
1263
   if(key == nullptr || pubkey == nullptr || slhdsa_mode == nullptr) {
13✔
1264
      return BOTAN_FFI_ERROR_NULL_POINTER;
1265
   }
1266

1267
   *key = nullptr;
13✔
1268

1269
   return ffi_guard_thunk(__func__, [=]() -> int {
26✔
1270
      auto mode = Botan::SLH_DSA_Parameters::create(slhdsa_mode);
13✔
1271
      if(!mode.is_slh_dsa()) {
13✔
1272
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1273
      }
1274

1275
      auto mldsa_key = std::make_unique<Botan::SLH_DSA_PublicKey>(std::span{pubkey, key_len}, mode);
13✔
1276
      *key = new botan_pubkey_struct(std::move(mldsa_key));
26✔
1277
      return BOTAN_FFI_SUCCESS;
13✔
1278
   });
13✔
1279
#else
1280
   BOTAN_UNUSED(key, key_len, pubkey, slhdsa_mode);
1281
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1282
#endif
1283
}
1284

1285
/*
1286
* Algorithm specific key operations: FrodoKEM
1287
*/
1288

1289
int botan_privkey_load_frodokem(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* frodo_mode) {
13✔
1290
#if defined(BOTAN_HAS_FRODOKEM)
1291
   if(key == nullptr || privkey == nullptr || frodo_mode == nullptr) {
13✔
1292
      return BOTAN_FFI_ERROR_NULL_POINTER;
1293
   }
1294

1295
   *key = nullptr;
13✔
1296

1297
   return ffi_guard_thunk(__func__, [=]() -> int {
26✔
1298
      const auto mode = Botan::FrodoKEMMode(frodo_mode);
13✔
1299
      auto frodo_key = std::make_unique<Botan::FrodoKEM_PrivateKey>(std::span{privkey, key_len}, mode);
13✔
1300
      *key = new botan_privkey_struct(std::move(frodo_key));
13✔
1301
      return BOTAN_FFI_SUCCESS;
13✔
1302
   });
13✔
1303
#else
1304
   BOTAN_UNUSED(key, privkey, key_len, frodo_mode);
1305
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1306
#endif
1307
}
1308

1309
int botan_pubkey_load_frodokem(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* frodo_mode) {
13✔
1310
#if defined(BOTAN_HAS_FRODOKEM)
1311
   if(key == nullptr || pubkey == nullptr || frodo_mode == nullptr) {
13✔
1312
      return BOTAN_FFI_ERROR_NULL_POINTER;
1313
   }
1314

1315
   *key = nullptr;
13✔
1316

1317
   return ffi_guard_thunk(__func__, [=]() -> int {
26✔
1318
      const auto mode = Botan::FrodoKEMMode(frodo_mode);
13✔
1319
      auto frodo_key = std::make_unique<Botan::FrodoKEM_PublicKey>(std::span{pubkey, key_len}, mode);
13✔
1320
      *key = new botan_pubkey_struct(std::move(frodo_key));
26✔
1321
      return BOTAN_FFI_SUCCESS;
13✔
1322
   });
13✔
1323
#else
1324
   BOTAN_UNUSED(key, pubkey, key_len, frodo_mode);
1325
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1326
#endif
1327
}
1328

1329
/*
1330
* Algorithm specific key operations : Classic McEliece
1331
*/
1332

1333
int botan_privkey_load_classic_mceliece(botan_privkey_t* key,
17✔
1334
                                        const uint8_t privkey[],
1335
                                        size_t key_len,
1336
                                        const char* cmce_mode) {
1337
#if defined(BOTAN_HAS_CLASSICMCELIECE)
1338
   if(key == nullptr || privkey == nullptr || cmce_mode == nullptr) {
17✔
1339
      return BOTAN_FFI_ERROR_NULL_POINTER;
1340
   }
1341

1342
   *key = nullptr;
17✔
1343

1344
   return ffi_guard_thunk(__func__, [=]() -> int {
34✔
1345
      const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode);
17✔
1346
      auto cmce_key = std::make_unique<Botan::Classic_McEliece_PrivateKey>(std::span{privkey, key_len}, mode);
17✔
1347
      *key = new botan_privkey_struct(std::move(cmce_key));
17✔
1348
      return BOTAN_FFI_SUCCESS;
17✔
1349
   });
17✔
1350
#else
1351
   BOTAN_UNUSED(key, privkey, key_len, cmce_mode);
1352
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1353
#endif
1354
}
1355

1356
int botan_pubkey_load_classic_mceliece(botan_pubkey_t* key,
17✔
1357
                                       const uint8_t pubkey[],
1358
                                       size_t key_len,
1359
                                       const char* cmce_mode) {
1360
#if defined(BOTAN_HAS_CLASSICMCELIECE)
1361
   if(key == nullptr || pubkey == nullptr || cmce_mode == nullptr) {
17✔
1362
      return BOTAN_FFI_ERROR_NULL_POINTER;
1363
   }
1364

1365
   *key = nullptr;
17✔
1366

1367
   return ffi_guard_thunk(__func__, [=]() -> int {
34✔
1368
      const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode);
17✔
1369
      auto cmce_key = std::make_unique<Botan::Classic_McEliece_PublicKey>(std::span{pubkey, key_len}, mode);
17✔
1370
      *key = new botan_pubkey_struct(std::move(cmce_key));
34✔
1371
      return BOTAN_FFI_SUCCESS;
17✔
1372
   });
17✔
1373
#else
1374
   BOTAN_UNUSED(key, pubkey, key_len, cmce_mode);
1375
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1376
#endif
1377
}
1378

1379
int botan_pubkey_view_ec_public_point(const botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) {
6✔
1380
#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
1381
   return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
24✔
1382
      if(auto ecc = dynamic_cast<const Botan::EC_PublicKey*>(&k)) {
1383
         auto pt = ecc->_public_ec_point().serialize_uncompressed();
1384
         return invoke_view_callback(view, ctx, pt);
1385
      } else {
1386
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1387
      }
1388
   });
1389
#else
1390
   BOTAN_UNUSED(key, view, ctx);
1391
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1392
#endif
1393
}
1394

1395
int botan_privkey_create_mceliece(botan_privkey_t* key_obj, botan_rng_t rng_obj, size_t n, size_t t) {
1✔
1396
   const std::string mce_params = std::to_string(n) + "," + std::to_string(t);
3✔
1397
   return botan_privkey_create(key_obj, "McEliece", mce_params.c_str(), rng_obj);
1✔
1398
}
1✔
1399

1400
int botan_mceies_decrypt(botan_privkey_t mce_key_obj,
×
1401
                         const char* aead,
1402
                         const uint8_t ct[],
1403
                         size_t ct_len,
1404
                         const uint8_t ad[],
1405
                         size_t ad_len,
1406
                         uint8_t out[],
1407
                         size_t* out_len) {
1408
   BOTAN_UNUSED(mce_key_obj, aead, ct, ct_len, ad, ad_len, out, out_len);
×
1409
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
×
1410
}
1411

1412
int botan_mceies_encrypt(botan_pubkey_t mce_key_obj,
×
1413
                         botan_rng_t rng_obj,
1414
                         const char* aead,
1415
                         const uint8_t pt[],
1416
                         size_t pt_len,
1417
                         const uint8_t ad[],
1418
                         size_t ad_len,
1419
                         uint8_t out[],
1420
                         size_t* out_len) {
1421
   BOTAN_UNUSED(mce_key_obj, rng_obj, aead, pt, pt_len, ad, ad_len, out, out_len);
×
1422
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
×
1423
}
1424
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc