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

randombit / botan / 16293079084

15 Jul 2025 12:20PM UTC coverage: 90.627% (+0.003%) from 90.624%
16293079084

push

github

web-flow
Merge pull request #4990 from randombit/jack/string-and-span

Improve string<->span conversions

99640 of 109945 relevant lines covered (90.63%)

12253617.72 hits per line

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

97.89
/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) {
2✔
211
   if(n_bits < 1024 || n_bits > 16 * 1024) {
2✔
212
      return BOTAN_FFI_ERROR_BAD_PARAMETER;
213
   }
214

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

217
   return botan_privkey_create(key_obj, "RSA", n_str.c_str(), rng_obj);
2✔
218
}
2✔
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 {
2✔
228
      auto rsa = std::make_unique<Botan::RSA_PrivateKey>(safe_get(rsa_p), safe_get(rsa_q), safe_get(rsa_e));
2✔
229
      return ffi_new_object(key, std::move(rsa));
4✔
230
   });
4✔
231
#else
232
   BOTAN_UNUSED(key, rsa_p, rsa_q, rsa_e);
233
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
234
#endif
235
}
236

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

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

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

271
int botan_privkey_rsa_get_p(botan_mp_t p, botan_privkey_t key) {
1✔
272
   return botan_privkey_get_field(p, key, "p");
1✔
273
}
274

275
int botan_privkey_rsa_get_q(botan_mp_t q, botan_privkey_t key) {
1✔
276
   return botan_privkey_get_field(q, key, "q");
1✔
277
}
278

279
int botan_privkey_rsa_get_n(botan_mp_t n, botan_privkey_t key) {
1✔
280
   return botan_privkey_get_field(n, key, "n");
1✔
281
}
282

283
int botan_privkey_rsa_get_e(botan_mp_t e, botan_privkey_t key) {
1✔
284
   return botan_privkey_get_field(e, key, "e");
1✔
285
}
286

287
int botan_privkey_rsa_get_d(botan_mp_t d, botan_privkey_t key) {
1✔
288
   return botan_privkey_get_field(d, key, "d");
1✔
289
}
290

291
int botan_pubkey_rsa_get_e(botan_mp_t e, botan_pubkey_t key) {
1✔
292
   return botan_pubkey_get_field(e, key, "e");
1✔
293
}
294

295
int botan_pubkey_rsa_get_n(botan_mp_t n, botan_pubkey_t key) {
1✔
296
   return botan_pubkey_get_field(n, key, "n");
1✔
297
}
298

299
int botan_privkey_rsa_get_privkey(botan_privkey_t rsa_key, uint8_t out[], size_t* out_len, uint32_t flags) {
4✔
300
#if defined(BOTAN_HAS_RSA)
301
   return BOTAN_FFI_VISIT(rsa_key, [=](const auto& k) -> int {
14✔
302
      if(const Botan::RSA_PrivateKey* rsa = dynamic_cast<const Botan::RSA_PrivateKey*>(&k)) {
303
         if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
304
            return write_vec_output(out, out_len, rsa->private_key_bits());
305
         } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
306
            // TODO define new generic functions for this
307
            return write_str_output(reinterpret_cast<char*>(out),
308
                                    out_len,
309
                                    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 != 0) || (qbits % 8 != 0) || (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 {
1✔
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
      return ffi_new_object(key, std::move(dsa));
1✔
340
   });
3✔
341
#else
342
   BOTAN_UNUSED(key, rng_obj, pbits, qbits);
343
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
344
#endif
345
}
346

347
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✔
348
#if defined(BOTAN_HAS_DSA)
349
   if(key == nullptr) {
2✔
350
      return BOTAN_FFI_ERROR_NULL_POINTER;
351
   }
352
   *key = nullptr;
2✔
353

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

365
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✔
366
#if defined(BOTAN_HAS_DSA)
367
   if(key == nullptr) {
2✔
368
      return BOTAN_FFI_ERROR_NULL_POINTER;
369
   }
370
   *key = nullptr;
2✔
371

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

383
int botan_privkey_dsa_get_x(botan_mp_t x, botan_privkey_t key) {
2✔
384
   return botan_privkey_get_field(x, key, "x");
2✔
385
}
386

387
int botan_pubkey_dsa_get_p(botan_mp_t p, botan_pubkey_t key) {
2✔
388
   return botan_pubkey_get_field(p, key, "p");
2✔
389
}
390

391
int botan_pubkey_dsa_get_q(botan_mp_t q, botan_pubkey_t key) {
2✔
392
   return botan_pubkey_get_field(q, key, "q");
2✔
393
}
394

395
int botan_pubkey_dsa_get_g(botan_mp_t g, botan_pubkey_t key) {
2✔
396
   return botan_pubkey_get_field(g, key, "g");
2✔
397
}
398

399
int botan_pubkey_dsa_get_y(botan_mp_t y, botan_pubkey_t key) {
2✔
400
   return botan_pubkey_get_field(y, key, "y");
2✔
401
}
402

403
int botan_privkey_create_ecdsa(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
1✔
404
   return botan_privkey_create(key_obj, "ECDSA", param_str, rng_obj);
1✔
405
}
406

407
/* ECDSA specific operations */
408

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

415
      if(ec_key == nullptr) {
1✔
416
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
417
      }
418

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

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

437
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
438
      std::unique_ptr<Botan::ECDSA_PublicKey> p_key;
2✔
439

440
      int rc = pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name);
2✔
441
      if(rc == BOTAN_FFI_SUCCESS) {
2✔
442
         ffi_new_object(key, std::move(p_key));
2✔
443
      }
444

445
      return rc;
2✔
446
   });
4✔
447
#else
448
   BOTAN_UNUSED(key, public_x, public_y, curve_name);
449
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
450
#endif
451
}
452

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

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

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

482
   if(pbits < 1024 || qbits < 160) {
1✔
483
      return BOTAN_FFI_ERROR_BAD_PARAMETER;
484
   }
485

486
   Botan::DL_Group::PrimeType prime_type =
1✔
487
      ((pbits - 1) == qbits) ? Botan::DL_Group::Strong : Botan::DL_Group::Prime_Subgroup;
1✔
488

489
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
490
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
1✔
491
      Botan::DL_Group group(rng, prime_type, pbits, qbits);
1✔
492
      auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(rng, group);
1✔
493
      return ffi_new_object(key, std::move(elg));
1✔
494
   });
3✔
495
#else
496
   BOTAN_UNUSED(key, rng_obj, pbits, qbits);
497
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
498
#endif
499
}
500

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

518
int botan_privkey_load_elgamal(botan_privkey_t* key, botan_mp_t p, botan_mp_t g, botan_mp_t x) {
2✔
519
#if defined(BOTAN_HAS_ELGAMAL)
520
   if(key == nullptr) {
2✔
521
      return BOTAN_FFI_ERROR_NULL_POINTER;
522
   }
523
   *key = nullptr;
2✔
524
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
525
      Botan::DL_Group group(safe_get(p), safe_get(g));
2✔
526
      auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(group, safe_get(x));
2✔
527
      return ffi_new_object(key, std::move(elg));
2✔
528
   });
6✔
529
#else
530
   BOTAN_UNUSED(key, p, g, x);
531
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
532
#endif
533
}
534

535
/* Diffie Hellman specific operations */
536

537
int botan_privkey_create_dh(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
2✔
538
   return botan_privkey_create(key_obj, "DH", param_str, rng_obj);
2✔
539
}
540

541
int botan_privkey_load_dh(botan_privkey_t* key, botan_mp_t p, botan_mp_t g, botan_mp_t x) {
1✔
542
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
543
   if(key == nullptr) {
1✔
544
      return BOTAN_FFI_ERROR_NULL_POINTER;
545
   }
546
   *key = nullptr;
1✔
547
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
548
      Botan::DL_Group group(safe_get(p), safe_get(g));
1✔
549
      auto dh = std::make_unique<Botan::DH_PrivateKey>(group, safe_get(x));
1✔
550
      return ffi_new_object(key, std::move(dh));
1✔
551
   });
3✔
552
#else
553
   BOTAN_UNUSED(key, p, g, x);
554
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
555
#endif
556
}
557

558
int botan_pubkey_load_dh(botan_pubkey_t* key, botan_mp_t p, botan_mp_t g, botan_mp_t y) {
1✔
559
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
560
   if(key == nullptr) {
1✔
561
      return BOTAN_FFI_ERROR_NULL_POINTER;
562
   }
563
   *key = nullptr;
1✔
564
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
565
      Botan::DL_Group group(safe_get(p), safe_get(g));
1✔
566
      auto dh = std::make_unique<Botan::DH_PublicKey>(group, safe_get(y));
1✔
567
      return ffi_new_object(key, std::move(dh));
1✔
568
   });
3✔
569
#else
570
   BOTAN_UNUSED(key, p, g, y);
571
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
572
#endif
573
}
574

575
/* ECDH + x25519/x448 specific operations */
576

577
int botan_privkey_create_ecdh(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
2✔
578
   if(key_obj == nullptr || param_str == nullptr) {
2✔
579
      return BOTAN_FFI_ERROR_NULL_POINTER;
580
   }
581
   *key_obj = nullptr;
2✔
582

583
   const std::string params(param_str);
2✔
584

585
   if(params == "X25519" || params == "x25519" || params == "curve25519") {
2✔
586
      return botan_privkey_create(key_obj, "X25519", "", rng_obj);
×
587
   }
588

589
   if(params == "X448" || params == "x448") {
2✔
590
      return botan_privkey_create(key_obj, "X448", "", rng_obj);
×
591
   }
592

593
   return botan_privkey_create(key_obj, "ECDH", param_str, rng_obj);
2✔
594
}
2✔
595

596
int botan_pubkey_load_ecdh(botan_pubkey_t* key,
1✔
597
                           const botan_mp_t public_x,
598
                           const botan_mp_t public_y,
599
                           const char* curve_name) {
600
#if defined(BOTAN_HAS_ECDH)
601
   if(key == nullptr || curve_name == nullptr) {
1✔
602
      return BOTAN_FFI_ERROR_NULL_POINTER;
603
   }
604
   *key = nullptr;
1✔
605
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
606
      std::unique_ptr<Botan::ECDH_PublicKey> p_key;
1✔
607
      int rc = pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name);
1✔
608

609
      if(rc == BOTAN_FFI_SUCCESS) {
1✔
610
         ffi_new_object(key, std::move(p_key));
1✔
611
      }
612
      return rc;
1✔
613
   });
2✔
614
#else
615
   BOTAN_UNUSED(key, public_x, public_y, curve_name);
616
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
617
#endif
618
}
619

620
int botan_privkey_load_ecdh(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
4✔
621
#if defined(BOTAN_HAS_ECDH)
622
   if(key == nullptr || curve_name == nullptr) {
4✔
623
      return BOTAN_FFI_ERROR_NULL_POINTER;
624
   }
625
   *key = nullptr;
4✔
626
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
627
      std::unique_ptr<Botan::ECDH_PrivateKey> p_key;
4✔
628
      int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
4✔
629
      if(rc == BOTAN_FFI_SUCCESS) {
4✔
630
         ffi_new_object(key, std::move(p_key));
8✔
631
      }
632
      return rc;
4✔
633
   });
8✔
634
#else
635
   BOTAN_UNUSED(key, scalar, curve_name);
636
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
637
#endif
638
}
639

640
/* SM2 specific operations */
641

642
int botan_pubkey_sm2_compute_za(
2✔
643
   uint8_t out[], size_t* out_len, const char* ident, const char* hash_algo, const botan_pubkey_t key) {
644
   if(out == nullptr || out_len == nullptr || ident == nullptr || hash_algo == nullptr || key == nullptr) {
2✔
645
      return BOTAN_FFI_ERROR_NULL_POINTER;
646
   }
647

648
#if defined(BOTAN_HAS_SM2)
649
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
650
      const Botan::Public_Key& pub_key = safe_get(key);
2✔
651
      const Botan::EC_PublicKey* ec_key = dynamic_cast<const Botan::EC_PublicKey*>(&pub_key);
2✔
652

653
      if(ec_key == nullptr) {
2✔
654
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
655
      }
656

657
      if(ec_key->algo_name() != "SM2") {
2✔
658
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
659
      }
660

661
      const std::string ident_str(ident);
2✔
662
      std::unique_ptr<Botan::HashFunction> hash = Botan::HashFunction::create_or_throw(hash_algo);
2✔
663

664
      const auto& pt = ec_key->_public_ec_point();
2✔
665

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

668
      return write_vec_output(out, out_len, za);
2✔
669
   });
6✔
670
#else
671
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
672
#endif
673
}
674

675
int botan_pubkey_load_sm2(botan_pubkey_t* key,
3✔
676
                          const botan_mp_t public_x,
677
                          const botan_mp_t public_y,
678
                          const char* curve_name) {
679
#if defined(BOTAN_HAS_SM2)
680
   if(key == nullptr || curve_name == nullptr) {
3✔
681
      return BOTAN_FFI_ERROR_NULL_POINTER;
682
   }
683
   *key = nullptr;
3✔
684

685
   return ffi_guard_thunk(__func__, [=]() -> int {
3✔
686
      std::unique_ptr<Botan::SM2_PublicKey> p_key;
3✔
687
      if(!pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name)) {
3✔
688
         return ffi_new_object(key, std::move(p_key));
3✔
689
      } else {
690
         return BOTAN_FFI_ERROR_UNKNOWN_ERROR;
691
      }
692
   });
6✔
693
#else
694
   BOTAN_UNUSED(key, public_x, public_y, curve_name);
695
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
696
#endif
697
}
698

699
int botan_privkey_load_sm2(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
3✔
700
#if defined(BOTAN_HAS_SM2)
701
   if(key == nullptr || curve_name == nullptr) {
3✔
702
      return BOTAN_FFI_ERROR_NULL_POINTER;
703
   }
704
   *key = nullptr;
3✔
705

706
   return ffi_guard_thunk(__func__, [=]() -> int {
3✔
707
      std::unique_ptr<Botan::SM2_PrivateKey> p_key;
3✔
708
      int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
3✔
709

710
      if(rc == BOTAN_FFI_SUCCESS) {
3✔
711
         ffi_new_object(key, std::move(p_key));
6✔
712
      }
713
      return rc;
3✔
714
   });
6✔
715
#else
716
   BOTAN_UNUSED(key, scalar, curve_name);
717
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
718
#endif
719
}
720

721
int botan_pubkey_load_sm2_enc(botan_pubkey_t* key,
1✔
722
                              const botan_mp_t public_x,
723
                              const botan_mp_t public_y,
724
                              const char* curve_name) {
725
   return botan_pubkey_load_sm2(key, public_x, public_y, curve_name);
1✔
726
}
727

728
int botan_privkey_load_sm2_enc(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
1✔
729
   return botan_privkey_load_sm2(key, scalar, curve_name);
1✔
730
}
731

732
/* Ed25519 specific operations */
733

734
int botan_privkey_load_ed25519(botan_privkey_t* key, const uint8_t privkey[32]) {
1✔
735
#if defined(BOTAN_HAS_ED25519)
736
   if(key == nullptr) {
1✔
737
      return BOTAN_FFI_ERROR_NULL_POINTER;
738
   }
739
   *key = nullptr;
1✔
740
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
741
      auto ed25519 =
1✔
742
         std::make_unique<Botan::Ed25519_PrivateKey>(Botan::Ed25519_PrivateKey::from_seed(std::span{privkey, 32}));
2✔
743
      return ffi_new_object(key, std::move(ed25519));
2✔
744
   });
2✔
745
#else
746
   BOTAN_UNUSED(key, privkey);
747
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
748
#endif
749
}
750

751
int botan_pubkey_load_ed25519(botan_pubkey_t* key, const uint8_t pubkey[32]) {
1✔
752
#if defined(BOTAN_HAS_ED25519)
753
   if(key == nullptr) {
1✔
754
      return BOTAN_FFI_ERROR_NULL_POINTER;
755
   }
756
   *key = nullptr;
1✔
757
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
758
      const std::vector<uint8_t> pubkey_vec(pubkey, pubkey + 32);
1✔
759
      auto ed25519 = std::make_unique<Botan::Ed25519_PublicKey>(pubkey_vec);
1✔
760
      return ffi_new_object(key, std::move(ed25519));
1✔
761
   });
3✔
762
#else
763
   BOTAN_UNUSED(key, pubkey);
764
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
765
#endif
766
}
767

768
int botan_privkey_ed25519_get_privkey(botan_privkey_t key, uint8_t output[64]) {
1✔
769
#if defined(BOTAN_HAS_ED25519)
770
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
4✔
771
      if(auto ed = dynamic_cast<const Botan::Ed25519_PrivateKey*>(&k)) {
772
         const auto ed_key = ed->raw_private_key_bits();
773
         if(ed_key.size() != 64) {
774
            return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE;
775
         }
776
         Botan::copy_mem(output, ed_key.data(), ed_key.size());
777
         return BOTAN_FFI_SUCCESS;
778
      } else {
779
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
780
      }
781
   });
782
#else
783
   BOTAN_UNUSED(key, output);
784
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
785
#endif
786
}
787

788
int botan_pubkey_ed25519_get_pubkey(botan_pubkey_t key, uint8_t output[32]) {
1✔
789
#if defined(BOTAN_HAS_ED25519)
790
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
2✔
791
      if(auto ed = dynamic_cast<const Botan::Ed25519_PublicKey*>(&k)) {
792
         const std::vector<uint8_t>& ed_key = ed->get_public_key();
793
         if(ed_key.size() != 32) {
794
            return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE;
795
         }
796
         Botan::copy_mem(output, ed_key.data(), ed_key.size());
797
         return BOTAN_FFI_SUCCESS;
798
      } else {
799
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
800
      }
801
   });
802
#else
803
   BOTAN_UNUSED(key, output);
804
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
805
#endif
806
}
807

808
/* Ed448 specific operations */
809

810
int botan_privkey_load_ed448(botan_privkey_t* key, const uint8_t privkey[57]) {
1✔
811
#if defined(BOTAN_HAS_ED448)
812
   if(key == nullptr) {
1✔
813
      return BOTAN_FFI_ERROR_NULL_POINTER;
814
   }
815
   *key = nullptr;
1✔
816
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
817
      auto ed448 = std::make_unique<Botan::Ed448_PrivateKey>(std::span(privkey, 57));
1✔
818
      return ffi_new_object(key, std::move(ed448));
2✔
819
   });
2✔
820
#else
821
   BOTAN_UNUSED(key, privkey);
822
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
823
#endif
824
}
825

826
int botan_pubkey_load_ed448(botan_pubkey_t* key, const uint8_t pubkey[57]) {
1✔
827
#if defined(BOTAN_HAS_ED448)
828
   if(key == nullptr) {
1✔
829
      return BOTAN_FFI_ERROR_NULL_POINTER;
830
   }
831
   *key = nullptr;
1✔
832
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
833
      auto ed448 = std::make_unique<Botan::Ed448_PublicKey>(std::span(pubkey, 57));
1✔
834
      return ffi_new_object(key, std::move(ed448));
1✔
835
   });
2✔
836
#else
837
   BOTAN_UNUSED(key, pubkey);
838
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
839
#endif
840
}
841

842
int botan_privkey_ed448_get_privkey(botan_privkey_t key, uint8_t output[57]) {
1✔
843
#if defined(BOTAN_HAS_ED448)
844
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
4✔
845
      if(auto ed = dynamic_cast<const Botan::Ed448_PrivateKey*>(&k)) {
846
         const auto ed_key = ed->raw_private_key_bits();
847
         Botan::copy_mem(std::span(output, 57), ed_key);
848
         return BOTAN_FFI_SUCCESS;
849
      } else {
850
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
851
      }
852
   });
853
#else
854
   BOTAN_UNUSED(key, output);
855
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
856
#endif
857
}
858

859
int botan_pubkey_ed448_get_pubkey(botan_pubkey_t key, uint8_t output[57]) {
1✔
860
#if defined(BOTAN_HAS_ED448)
861
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
3✔
862
      if(auto ed = dynamic_cast<const Botan::Ed448_PublicKey*>(&k)) {
863
         const auto ed_key = ed->public_key_bits();
864
         Botan::copy_mem(std::span(output, 57), ed_key);
865
         return BOTAN_FFI_SUCCESS;
866
      } else {
867
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
868
      }
869
   });
870
#else
871
   BOTAN_UNUSED(key, output);
872
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
873
#endif
874
}
875

876
/* X25519 specific operations */
877

878
int botan_privkey_load_x25519(botan_privkey_t* key, const uint8_t privkey[32]) {
1✔
879
#if defined(BOTAN_HAS_X25519)
880
   if(key == nullptr) {
1✔
881
      return BOTAN_FFI_ERROR_NULL_POINTER;
882
   }
883
   *key = nullptr;
1✔
884
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
885
      auto x25519 = std::make_unique<Botan::X25519_PrivateKey>(std::span{privkey, 32});
1✔
886
      return ffi_new_object(key, std::move(x25519));
2✔
887
   });
2✔
888
#else
889
   BOTAN_UNUSED(key, privkey);
890
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
891
#endif
892
}
893

894
int botan_pubkey_load_x25519(botan_pubkey_t* key, const uint8_t pubkey[32]) {
1✔
895
#if defined(BOTAN_HAS_X25519)
896
   if(key == nullptr) {
1✔
897
      return BOTAN_FFI_ERROR_NULL_POINTER;
898
   }
899
   *key = nullptr;
1✔
900
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
901
      auto x25519 = std::make_unique<Botan::X25519_PublicKey>(std::span{pubkey, 32});
1✔
902
      return ffi_new_object(key, std::move(x25519));
1✔
903
   });
2✔
904
#else
905
   BOTAN_UNUSED(key, pubkey);
906
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
907
#endif
908
}
909

910
int botan_privkey_x25519_get_privkey(botan_privkey_t key, uint8_t output[32]) {
1✔
911
#if defined(BOTAN_HAS_X25519)
912
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
4✔
913
      if(auto x25519 = dynamic_cast<const Botan::X25519_PrivateKey*>(&k)) {
914
         const auto x25519_key = x25519->raw_private_key_bits();
915
         if(x25519_key.size() != 32) {
916
            return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE;
917
         }
918
         Botan::copy_mem(output, x25519_key.data(), x25519_key.size());
919
         return BOTAN_FFI_SUCCESS;
920
      } else {
921
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
922
      }
923
   });
924
#else
925
   BOTAN_UNUSED(key, output);
926
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
927
#endif
928
}
929

930
int botan_pubkey_x25519_get_pubkey(botan_pubkey_t key, uint8_t output[32]) {
2✔
931
#if defined(BOTAN_HAS_X25519)
932
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
6✔
933
      if(auto x25519 = dynamic_cast<const Botan::X25519_PublicKey*>(&k)) {
934
         Botan::copy_mem(std::span{output, 32}, x25519->raw_public_key_bits());
935
         return BOTAN_FFI_SUCCESS;
936
      } else {
937
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
938
      }
939
   });
940
#else
941
   BOTAN_UNUSED(key, output);
942
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
943
#endif
944
}
945

946
/* X448 specific operations */
947

948
int botan_privkey_load_x448(botan_privkey_t* key, const uint8_t privkey[56]) {
1✔
949
#if defined(BOTAN_HAS_X448)
950
   if(key == nullptr) {
1✔
951
      return BOTAN_FFI_ERROR_NULL_POINTER;
952
   }
953
   *key = nullptr;
1✔
954
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
955
      auto x448 = std::make_unique<Botan::X448_PrivateKey>(std::span{privkey, 56});
1✔
956
      return ffi_new_object(key, std::move(x448));
2✔
957
   });
2✔
958
#else
959
   BOTAN_UNUSED(key, privkey);
960
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
961
#endif
962
}
963

964
int botan_pubkey_load_x448(botan_pubkey_t* key, const uint8_t pubkey[56]) {
1✔
965
#if defined(BOTAN_HAS_X448)
966
   if(key == nullptr) {
1✔
967
      return BOTAN_FFI_ERROR_NULL_POINTER;
968
   }
969
   *key = nullptr;
1✔
970
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
971
      auto x448 = std::make_unique<Botan::X448_PublicKey>(std::span{pubkey, 56});
1✔
972
      return ffi_new_object(key, std::move(x448));
1✔
973
   });
2✔
974
#else
975
   BOTAN_UNUSED(key, pubkey);
976
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
977
#endif
978
}
979

980
int botan_privkey_x448_get_privkey(botan_privkey_t key, uint8_t output[56]) {
1✔
981
#if defined(BOTAN_HAS_X448)
982
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
4✔
983
      if(auto x448 = dynamic_cast<const Botan::X448_PrivateKey*>(&k)) {
984
         const auto x448_key = x448->raw_private_key_bits();
985
         Botan::copy_mem(std::span{output, 56}, x448_key);
986
         return BOTAN_FFI_SUCCESS;
987
      } else {
988
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
989
      }
990
   });
991
#else
992
   BOTAN_UNUSED(key, output);
993
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
994
#endif
995
}
996

997
int botan_pubkey_x448_get_pubkey(botan_pubkey_t key, uint8_t output[56]) {
2✔
998
#if defined(BOTAN_HAS_X448)
999
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
6✔
1000
      if(auto x448 = dynamic_cast<const Botan::X448_PublicKey*>(&k)) {
1001
         Botan::copy_mem(std::span{output, 56}, x448->raw_public_key_bits());
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
/*
1014
* Algorithm specific key operations: Kyber
1015
*/
1016

1017
int botan_privkey_load_kyber(botan_privkey_t* key, const uint8_t privkey[], size_t key_len) {
4✔
1018
#if defined(BOTAN_HAS_KYBER)
1019
   if(key == nullptr) {
4✔
1020
      return BOTAN_FFI_ERROR_NULL_POINTER;
1021
   }
1022
   *key = nullptr;
4✔
1023

1024
   const auto mode = [](size_t len) -> std::optional<Botan::KyberMode> {
12✔
1025
      if(len == 1632) {
4✔
1026
         return Botan::KyberMode::Kyber512_R3;
1✔
1027
      } else if(len == 2400) {
3✔
1028
         return Botan::KyberMode::Kyber768_R3;
2✔
1029
      } else if(len == 3168) {
1✔
1030
         return Botan::KyberMode::Kyber1024_R3;
1✔
1031
      } else {
1032
         return {};
×
1033
      }
1034
   }(key_len);
4✔
1035

1036
   if(mode.has_value()) {
4✔
1037
      return ffi_guard_thunk(__func__, [=]() -> int {
4✔
1038
         auto kyber = std::make_unique<Botan::Kyber_PrivateKey>(std::span{privkey, key_len}, *mode);
4✔
1039
         return ffi_new_object(key, std::move(kyber));
8✔
1040
      });
4✔
1041
   } else {
1042
      return BOTAN_FFI_ERROR_BAD_PARAMETER;
1043
   }
1044
#else
1045
   BOTAN_UNUSED(key, key_len, privkey);
1046
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1047
#endif
1048
}
1049

1050
int botan_pubkey_load_kyber(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len) {
4✔
1051
#if defined(BOTAN_HAS_KYBER)
1052
   if(key == nullptr) {
4✔
1053
      return BOTAN_FFI_ERROR_NULL_POINTER;
1054
   }
1055
   *key = nullptr;
4✔
1056

1057
   const auto mode = [](size_t len) -> std::optional<Botan::KyberMode> {
12✔
1058
      if(len == 800) {
4✔
1059
         return Botan::KyberMode::Kyber512_R3;
1✔
1060
      } else if(len == 1184) {
3✔
1061
         return Botan::KyberMode::Kyber768_R3;
2✔
1062
      } else if(len == 1568) {
1✔
1063
         return Botan::KyberMode::Kyber1024_R3;
1✔
1064
      } else {
1065
         return {};
×
1066
      }
1067
   }(key_len);
4✔
1068

1069
   if(mode.has_value()) {
4✔
1070
      auto kyber = std::make_unique<Botan::Kyber_PublicKey>(std::span{pubkey, key_len}, *mode);
4✔
1071
      return ffi_new_object(key, std::move(kyber));
4✔
1072
   } else {
4✔
1073
      return BOTAN_FFI_ERROR_BAD_PARAMETER;
1074
   }
1075
#else
1076
   BOTAN_UNUSED(key, pubkey, key_len);
1077
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1078
#endif
1079
}
1080

1081
int botan_privkey_view_kyber_raw_key(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) {
4✔
1082
#if defined(BOTAN_HAS_KYBER)
1083
   return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
16✔
1084
      if(auto kyber = dynamic_cast<const Botan::Kyber_PrivateKey*>(&k)) {
1085
         return invoke_view_callback(view, ctx, kyber->raw_private_key_bits());
1086
      } else {
1087
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1088
      }
1089
   });
1090
#else
1091
   BOTAN_UNUSED(key, ctx, view);
1092
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1093
#endif
1094
}
1095

1096
int botan_pubkey_view_kyber_raw_key(botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) {
8✔
1097
#if defined(BOTAN_HAS_KYBER)
1098
   return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
32✔
1099
      if(auto kyber = dynamic_cast<const Botan::Kyber_PublicKey*>(&k)) {
1100
         return invoke_view_callback(view, ctx, kyber->public_key_bits());
1101
      } else {
1102
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1103
      }
1104
   });
1105
#else
1106
   BOTAN_UNUSED(key, ctx, view);
1107
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1108
#endif
1109
}
1110

1111
/*
1112
* Algorithm specific key operations: ML-KEM
1113
*/
1114

1115
int botan_privkey_load_ml_kem(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* mlkem_mode) {
4✔
1116
#if defined(BOTAN_HAS_ML_KEM)
1117
   if(key == nullptr || privkey == nullptr || mlkem_mode == nullptr) {
4✔
1118
      return BOTAN_FFI_ERROR_NULL_POINTER;
1119
   }
1120

1121
   *key = nullptr;
4✔
1122

1123
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
1124
      auto mode = Botan::ML_KEM_Mode(mlkem_mode);
4✔
1125
      if(!mode.is_ml_kem()) {
4✔
1126
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1127
      }
1128

1129
      auto mlkem_key = std::make_unique<Botan::ML_KEM_PrivateKey>(std::span{privkey, key_len}, mode);
4✔
1130
      return ffi_new_object(key, std::move(mlkem_key));
4✔
1131
   });
8✔
1132
#else
1133
   BOTAN_UNUSED(key, key_len, privkey, mlkem_mode);
1134
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1135
#endif
1136
}
1137

1138
int botan_pubkey_load_ml_kem(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* mlkem_mode) {
4✔
1139
#if defined(BOTAN_HAS_ML_KEM)
1140
   if(key == nullptr || pubkey == nullptr || mlkem_mode == nullptr) {
4✔
1141
      return BOTAN_FFI_ERROR_NULL_POINTER;
1142
   }
1143

1144
   *key = nullptr;
4✔
1145

1146
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
1147
      auto mode = Botan::ML_KEM_Mode(mlkem_mode);
4✔
1148
      if(!mode.is_ml_kem()) {
4✔
1149
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1150
      }
1151

1152
      auto mlkem_key = std::make_unique<Botan::ML_KEM_PublicKey>(std::span{pubkey, key_len}, mode.mode());
4✔
1153
      return ffi_new_object(key, std::move(mlkem_key));
4✔
1154
   });
8✔
1155
#else
1156
   BOTAN_UNUSED(key, key_len, pubkey, mlkem_mode);
1157
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1158
#endif
1159
}
1160

1161
/*
1162
* Algorithm specific key operations: ML-DSA
1163
*/
1164

1165
int botan_privkey_load_ml_dsa(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* mldsa_mode) {
4✔
1166
#if defined(BOTAN_HAS_ML_DSA)
1167
   if(key == nullptr || privkey == nullptr || mldsa_mode == nullptr) {
4✔
1168
      return BOTAN_FFI_ERROR_NULL_POINTER;
1169
   }
1170

1171
   *key = nullptr;
4✔
1172

1173
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
1174
      auto mode = Botan::ML_DSA_Mode(mldsa_mode);
4✔
1175
      if(!mode.is_ml_dsa()) {
4✔
1176
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1177
      }
1178

1179
      auto mldsa_key = std::make_unique<Botan::ML_DSA_PrivateKey>(std::span{privkey, key_len}, mode);
4✔
1180
      return ffi_new_object(key, std::move(mldsa_key));
4✔
1181
   });
8✔
1182
#else
1183
   BOTAN_UNUSED(key, key_len, privkey, mldsa_mode);
1184
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1185
#endif
1186
}
1187

1188
int botan_pubkey_load_ml_dsa(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* mldsa_mode) {
4✔
1189
#if defined(BOTAN_HAS_ML_DSA)
1190
   if(key == nullptr || pubkey == nullptr || mldsa_mode == nullptr) {
4✔
1191
      return BOTAN_FFI_ERROR_NULL_POINTER;
1192
   }
1193

1194
   *key = nullptr;
4✔
1195

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

1202
      auto mldsa_key = std::make_unique<Botan::ML_DSA_PublicKey>(std::span{pubkey, key_len}, mode);
4✔
1203
      return ffi_new_object(key, std::move(mldsa_key));
4✔
1204
   });
8✔
1205
#else
1206
   BOTAN_UNUSED(key, key_len, pubkey, mldsa_mode);
1207
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1208
#endif
1209
}
1210

1211
/*
1212
* Algorithm specific key operations: SLH-DSA
1213
*/
1214

1215
int botan_privkey_load_slh_dsa(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* slhdsa_mode) {
13✔
1216
#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
1217
   if(key == nullptr || privkey == nullptr || slhdsa_mode == nullptr) {
13✔
1218
      return BOTAN_FFI_ERROR_NULL_POINTER;
1219
   }
1220

1221
   *key = nullptr;
13✔
1222

1223
   return ffi_guard_thunk(__func__, [=]() -> int {
13✔
1224
      auto mode = Botan::SLH_DSA_Parameters::create(slhdsa_mode);
13✔
1225
      if(!mode.is_slh_dsa()) {
13✔
1226
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1227
      }
1228

1229
      auto slhdsa_key = std::make_unique<Botan::SLH_DSA_PrivateKey>(std::span{privkey, key_len}, mode);
13✔
1230
      return ffi_new_object(key, std::move(slhdsa_key));
13✔
1231
   });
26✔
1232
#else
1233
   BOTAN_UNUSED(key, key_len, privkey, slhdsa_mode);
1234
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1235
#endif
1236
}
1237

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

1244
   *key = nullptr;
13✔
1245

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

1252
      auto mldsa_key = std::make_unique<Botan::SLH_DSA_PublicKey>(std::span{pubkey, key_len}, mode);
13✔
1253
      return ffi_new_object(key, std::move(mldsa_key));
13✔
1254
   });
26✔
1255
#else
1256
   BOTAN_UNUSED(key, key_len, pubkey, slhdsa_mode);
1257
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1258
#endif
1259
}
1260

1261
/*
1262
* Algorithm specific key operations: FrodoKEM
1263
*/
1264

1265
int botan_privkey_load_frodokem(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* frodo_mode) {
13✔
1266
#if defined(BOTAN_HAS_FRODOKEM)
1267
   if(key == nullptr || privkey == nullptr || frodo_mode == nullptr) {
13✔
1268
      return BOTAN_FFI_ERROR_NULL_POINTER;
1269
   }
1270

1271
   *key = nullptr;
13✔
1272

1273
   return ffi_guard_thunk(__func__, [=]() -> int {
13✔
1274
      const auto mode = Botan::FrodoKEMMode(frodo_mode);
13✔
1275
      auto frodo_key = std::make_unique<Botan::FrodoKEM_PrivateKey>(std::span{privkey, key_len}, mode);
13✔
1276
      return ffi_new_object(key, std::move(frodo_key));
26✔
1277
   });
26✔
1278
#else
1279
   BOTAN_UNUSED(key, privkey, key_len, frodo_mode);
1280
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1281
#endif
1282
}
1283

1284
int botan_pubkey_load_frodokem(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* frodo_mode) {
13✔
1285
#if defined(BOTAN_HAS_FRODOKEM)
1286
   if(key == nullptr || pubkey == nullptr || frodo_mode == nullptr) {
13✔
1287
      return BOTAN_FFI_ERROR_NULL_POINTER;
1288
   }
1289

1290
   *key = nullptr;
13✔
1291

1292
   return ffi_guard_thunk(__func__, [=]() -> int {
13✔
1293
      const auto mode = Botan::FrodoKEMMode(frodo_mode);
13✔
1294
      auto frodo_key = std::make_unique<Botan::FrodoKEM_PublicKey>(std::span{pubkey, key_len}, mode);
13✔
1295
      return ffi_new_object(key, std::move(frodo_key));
13✔
1296
   });
26✔
1297
#else
1298
   BOTAN_UNUSED(key, pubkey, key_len, frodo_mode);
1299
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1300
#endif
1301
}
1302

1303
/*
1304
* Algorithm specific key operations : Classic McEliece
1305
*/
1306

1307
int botan_privkey_load_classic_mceliece(botan_privkey_t* key,
17✔
1308
                                        const uint8_t privkey[],
1309
                                        size_t key_len,
1310
                                        const char* cmce_mode) {
1311
#if defined(BOTAN_HAS_CLASSICMCELIECE)
1312
   if(key == nullptr || privkey == nullptr || cmce_mode == nullptr) {
17✔
1313
      return BOTAN_FFI_ERROR_NULL_POINTER;
1314
   }
1315

1316
   *key = nullptr;
17✔
1317

1318
   return ffi_guard_thunk(__func__, [=]() -> int {
17✔
1319
      const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode);
17✔
1320
      auto cmce_key = std::make_unique<Botan::Classic_McEliece_PrivateKey>(std::span{privkey, key_len}, mode);
17✔
1321
      return ffi_new_object(key, std::move(cmce_key));
34✔
1322
   });
34✔
1323
#else
1324
   BOTAN_UNUSED(key, privkey, key_len, cmce_mode);
1325
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1326
#endif
1327
}
1328

1329
int botan_pubkey_load_classic_mceliece(botan_pubkey_t* key,
17✔
1330
                                       const uint8_t pubkey[],
1331
                                       size_t key_len,
1332
                                       const char* cmce_mode) {
1333
#if defined(BOTAN_HAS_CLASSICMCELIECE)
1334
   if(key == nullptr || pubkey == nullptr || cmce_mode == nullptr) {
17✔
1335
      return BOTAN_FFI_ERROR_NULL_POINTER;
1336
   }
1337

1338
   *key = nullptr;
17✔
1339

1340
   return ffi_guard_thunk(__func__, [=]() -> int {
17✔
1341
      const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode);
17✔
1342
      auto cmce_key = std::make_unique<Botan::Classic_McEliece_PublicKey>(std::span{pubkey, key_len}, mode);
17✔
1343
      return ffi_new_object(key, std::move(cmce_key));
17✔
1344
   });
34✔
1345
#else
1346
   BOTAN_UNUSED(key, pubkey, key_len, cmce_mode);
1347
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1348
#endif
1349
}
1350

1351
int botan_pubkey_view_ec_public_point(const botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) {
6✔
1352
#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
1353
   return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
24✔
1354
      if(auto ecc = dynamic_cast<const Botan::EC_PublicKey*>(&k)) {
1355
         auto pt = ecc->_public_ec_point().serialize_uncompressed();
1356
         return invoke_view_callback(view, ctx, pt);
1357
      } else {
1358
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1359
      }
1360
   });
1361
#else
1362
   BOTAN_UNUSED(key, view, ctx);
1363
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1364
#endif
1365
}
1366

1367
int botan_privkey_create_mceliece(botan_privkey_t* key_obj, botan_rng_t rng_obj, size_t n, size_t t) {
1✔
1368
   const std::string mce_params = std::to_string(n) + "," + std::to_string(t);
3✔
1369
   return botan_privkey_create(key_obj, "McEliece", mce_params.c_str(), rng_obj);
1✔
1370
}
1✔
1371

1372
int botan_mceies_decrypt(botan_privkey_t mce_key_obj,
×
1373
                         const char* aead,
1374
                         const uint8_t ct[],
1375
                         size_t ct_len,
1376
                         const uint8_t ad[],
1377
                         size_t ad_len,
1378
                         uint8_t out[],
1379
                         size_t* out_len) {
1380
   BOTAN_UNUSED(mce_key_obj, aead, ct, ct_len, ad, ad_len, out, out_len);
×
1381
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
×
1382
}
1383

1384
int botan_mceies_encrypt(botan_pubkey_t mce_key_obj,
×
1385
                         botan_rng_t rng_obj,
1386
                         const char* aead,
1387
                         const uint8_t pt[],
1388
                         size_t pt_len,
1389
                         const uint8_t ad[],
1390
                         size_t ad_len,
1391
                         uint8_t out[],
1392
                         size_t* out_len) {
1393
   BOTAN_UNUSED(mce_key_obj, rng_obj, aead, pt, pt_len, ad, ad_len, out, out_len);
×
1394
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
×
1395
}
1396
}
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