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

randombit / botan / 24648292556

19 Apr 2026 10:53PM UTC coverage: 89.474% (+0.03%) from 89.442%
24648292556

push

github

web-flow
Merge pull request #5536 from randombit/jack/x509-misc

Various PKIX optimizations and bug fixes

106453 of 118977 relevant lines covered (89.47%)

11452293.24 hits per line

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

96.72
/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/assert.h>
12
#include <botan/ec_group.h>
13
#include <botan/hash.h>
14
#include <botan/mem_ops.h>
15
#include <botan/pem.h>
16
#include <botan/internal/ffi_ec.h>
17
#include <botan/internal/ffi_mp.h>
18
#include <botan/internal/ffi_pkey.h>
19
#include <botan/internal/ffi_rng.h>
20
#include <botan/internal/ffi_util.h>
21

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

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

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

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

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

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

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

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

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

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

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

66
#if defined(BOTAN_HAS_ED448)
67
   #include <botan/ed448.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
template <class ECPublicKey_t>
143
int pubkey_load_ec_sec1(std::unique_ptr<ECPublicKey_t>& key,
14✔
144
                        std::span<const uint8_t> sec1,
145
                        std::string_view curve_name) {
146
   if(!Botan::EC_Group::supports_named_group(curve_name)) {
14✔
147
      return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
148
   }
149

150
   const auto group = Botan::EC_Group::from_name(curve_name);
14✔
151

152
   if(auto pt = Botan::EC_AffinePoint::deserialize(group, sec1)) {
14✔
153
      key.reset(new ECPublicKey_t(group, pt.value()));
14✔
154
      return BOTAN_FFI_SUCCESS;
14✔
155
   } else {
156
      return BOTAN_FFI_ERROR_BAD_PARAMETER;
157
   }
158
}
14✔
159

160
#endif
161

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

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

181
Botan::BigInt privkey_get_field(const Botan::Private_Key& key, std::string_view field) {
35✔
182
#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
183
   // Not currently handled by get_int_field
184
   if(const Botan::EC_PublicKey* ecc = dynamic_cast<const Botan::EC_PublicKey*>(&key)) {
35✔
185
      if(field == "public_x") {
16✔
186
         return Botan::BigInt::from_bytes(ecc->_public_ec_point().x_bytes());
4✔
187
      } else if(field == "public_y") {
14✔
188
         return Botan::BigInt::from_bytes(ecc->_public_ec_point().y_bytes());
8✔
189
      }
190
   }
191
#endif
192

193
   try {
29✔
194
      return key.get_int_field(field);
29✔
195
   } catch(Botan::Unknown_PK_Field_Name&) {
4✔
196
      throw Botan_FFI::FFI_Error("Unknown key field", BOTAN_FFI_ERROR_BAD_PARAMETER);
4✔
197
   }
4✔
198
}
199

200
}  // namespace
201

202
extern "C" {
203

204
using namespace Botan_FFI;
205

206
int botan_pubkey_get_field(botan_mp_t output, botan_pubkey_t key, const char* field_name_cstr) {
40✔
207
   if(field_name_cstr == nullptr) {
40✔
208
      return BOTAN_FFI_ERROR_NULL_POINTER;
209
   }
210

211
   const std::string field_name(field_name_cstr);
40✔
212

213
   return BOTAN_FFI_VISIT(key, [=](const auto& k) { safe_get(output) = pubkey_get_field(k, field_name); });
158✔
214
}
40✔
215

216
int botan_privkey_get_field(botan_mp_t output, botan_privkey_t key, const char* field_name_cstr) {
35✔
217
   if(field_name_cstr == nullptr) {
35✔
218
      return BOTAN_FFI_ERROR_NULL_POINTER;
219
   }
220

221
   const std::string field_name(field_name_cstr);
35✔
222

223
   return BOTAN_FFI_VISIT(key, [=](const auto& k) { safe_get(output) = privkey_get_field(k, field_name); });
136✔
224
}
35✔
225

226
/* RSA specific operations */
227

228
int botan_privkey_create_rsa(botan_privkey_t* key_obj, botan_rng_t rng_obj, size_t n_bits) {
2✔
229
   if(n_bits < 1024 || n_bits > 16 * 1024) {
2✔
230
      return BOTAN_FFI_ERROR_BAD_PARAMETER;
231
   }
232

233
   const std::string n_str = std::to_string(n_bits);
2✔
234

235
   return botan_privkey_create(key_obj, "RSA", n_str.c_str(), rng_obj);
2✔
236
}
2✔
237

238
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✔
239
#if defined(BOTAN_HAS_RSA)
240
   if(key == nullptr) {
2✔
241
      return BOTAN_FFI_ERROR_NULL_POINTER;
242
   }
243
   *key = nullptr;
2✔
244

245
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
246
      auto rsa = std::make_unique<Botan::RSA_PrivateKey>(safe_get(rsa_p), safe_get(rsa_q), safe_get(rsa_e));
2✔
247
      return ffi_new_object(key, std::move(rsa));
2✔
248
   });
3✔
249
#else
250
   BOTAN_UNUSED(key, rsa_p, rsa_q, rsa_e);
251
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
252
#endif
253
}
254

255
int botan_privkey_load_rsa_pkcs1(botan_privkey_t* key, const uint8_t bits[], size_t len) {
1✔
256
#if defined(BOTAN_HAS_RSA)
257
   if(Botan::any_null_pointers(key, bits)) {
1✔
258
      return BOTAN_FFI_ERROR_NULL_POINTER;
259
   }
260
   *key = nullptr;
1✔
261

262
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
263
      const Botan::AlgorithmIdentifier alg_id("RSA", Botan::AlgorithmIdentifier::USE_NULL_PARAM);
1✔
264
      auto rsa = std::make_unique<Botan::RSA_PrivateKey>(alg_id, std::span{bits, len});
1✔
265
      return ffi_new_object(key, std::move(rsa));
2✔
266
   });
2✔
267
#else
268
   BOTAN_UNUSED(key, bits, len);
269
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
270
#endif
271
}
272

273
int botan_pubkey_load_rsa(botan_pubkey_t* key, botan_mp_t n, botan_mp_t e) {
4✔
274
#if defined(BOTAN_HAS_RSA)
275
   if(key == nullptr) {
4✔
276
      return BOTAN_FFI_ERROR_NULL_POINTER;
277
   }
278
   *key = nullptr;
4✔
279
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
280
      auto rsa = std::make_unique<Botan::RSA_PublicKey>(safe_get(n), safe_get(e));
4✔
281
      return ffi_new_object(key, std::move(rsa));
3✔
282
   });
7✔
283
#else
284
   BOTAN_UNUSED(key, n, e);
285
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
286
#endif
287
}
288

289
int botan_pubkey_load_rsa_pkcs1(botan_pubkey_t* key, const uint8_t bits[], size_t len) {
×
290
#if defined(BOTAN_HAS_RSA)
291
   if(Botan::any_null_pointers(key, bits)) {
×
292
      return BOTAN_FFI_ERROR_NULL_POINTER;
293
   }
294
   *key = nullptr;
×
295

296
   return ffi_guard_thunk(__func__, [=]() -> int {
×
297
      const Botan::AlgorithmIdentifier alg_id("RSA", Botan::AlgorithmIdentifier::USE_NULL_PARAM);
×
298
      auto rsa = std::make_unique<Botan::RSA_PublicKey>(alg_id, std::span{bits, len});
×
299
      return ffi_new_object(key, std::move(rsa));
×
300
   });
×
301
#else
302
   BOTAN_UNUSED(key, bits, len);
303
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
304
#endif
305
}
306

307
int botan_privkey_rsa_get_p(botan_mp_t p, botan_privkey_t key) {
1✔
308
   return botan_privkey_get_field(p, key, "p");
1✔
309
}
310

311
int botan_privkey_rsa_get_q(botan_mp_t q, botan_privkey_t key) {
1✔
312
   return botan_privkey_get_field(q, key, "q");
1✔
313
}
314

315
int botan_privkey_rsa_get_n(botan_mp_t n, botan_privkey_t key) {
1✔
316
   return botan_privkey_get_field(n, key, "n");
1✔
317
}
318

319
int botan_privkey_rsa_get_e(botan_mp_t e, botan_privkey_t key) {
1✔
320
   return botan_privkey_get_field(e, key, "e");
1✔
321
}
322

323
int botan_privkey_rsa_get_d(botan_mp_t d, botan_privkey_t key) {
1✔
324
   return botan_privkey_get_field(d, key, "d");
1✔
325
}
326

327
int botan_pubkey_rsa_get_e(botan_mp_t e, botan_pubkey_t key) {
1✔
328
   return botan_pubkey_get_field(e, key, "e");
1✔
329
}
330

331
int botan_pubkey_rsa_get_n(botan_mp_t n, botan_pubkey_t key) {
1✔
332
   return botan_pubkey_get_field(n, key, "n");
1✔
333
}
334

335
int botan_privkey_rsa_get_privkey(botan_privkey_t rsa_key, uint8_t out[], size_t* out_len, uint32_t flags) {
4✔
336
#if defined(BOTAN_HAS_RSA)
337
   return BOTAN_FFI_VISIT(rsa_key, [=](const auto& k) -> int {
14✔
338
      if(const Botan::RSA_PrivateKey* rsa = dynamic_cast<const Botan::RSA_PrivateKey*>(&k)) {
339
         if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
340
            return write_vec_output(out, out_len, rsa->private_key_bits());
341
         } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
342
            // TODO define new generic functions for this
343
            return write_str_output(reinterpret_cast<char*>(out),
344
                                    out_len,
345
                                    Botan::PEM_Code::encode(rsa->private_key_bits(), "RSA PRIVATE KEY"));
346
         } else {
347
            return BOTAN_FFI_ERROR_BAD_FLAG;
348
         }
349
      } else {
350
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
351
      }
352
   });
353
#else
354
   BOTAN_UNUSED(rsa_key, out, out_len, flags);
355
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
356
#endif
357
}
358

359
/* DSA specific operations */
360
int botan_privkey_create_dsa(botan_privkey_t* key, botan_rng_t rng_obj, size_t pbits, size_t qbits) {
1✔
361
#if defined(BOTAN_HAS_DSA)
362

363
   if(Botan::any_null_pointers(rng_obj, key)) {
1✔
364
      return BOTAN_FFI_ERROR_NULL_POINTER;
365
   }
366

367
   if((pbits % 64 != 0) || (qbits % 8 != 0) || (pbits < 1024) || (pbits > 3072) || (qbits < 160) || (qbits > 256)) {
1✔
368
      return BOTAN_FFI_ERROR_BAD_PARAMETER;
369
   }
370

371
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
372
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
1✔
373
      const Botan::DL_Group group(rng, Botan::DL_Group::Prime_Subgroup, pbits, qbits);
1✔
374
      auto dsa = std::make_unique<Botan::DSA_PrivateKey>(rng, group);
1✔
375
      return ffi_new_object(key, std::move(dsa));
1✔
376
   });
3✔
377
#else
378
   BOTAN_UNUSED(key, rng_obj, pbits, qbits);
379
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
380
#endif
381
}
382

383
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✔
384
#if defined(BOTAN_HAS_DSA)
385
   if(key == nullptr) {
2✔
386
      return BOTAN_FFI_ERROR_NULL_POINTER;
387
   }
388
   *key = nullptr;
2✔
389

390
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
391
      const Botan::DL_Group group(safe_get(p), safe_get(q), safe_get(g));
2✔
392
      auto dsa = std::make_unique<Botan::DSA_PrivateKey>(group, safe_get(x));
2✔
393
      return ffi_new_object(key, std::move(dsa));
2✔
394
   });
6✔
395
#else
396
   BOTAN_UNUSED(key, p, q, g, x);
397
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
398
#endif
399
}
400

401
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✔
402
#if defined(BOTAN_HAS_DSA)
403
   if(key == nullptr) {
2✔
404
      return BOTAN_FFI_ERROR_NULL_POINTER;
405
   }
406
   *key = nullptr;
2✔
407

408
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
409
      const Botan::DL_Group group(safe_get(p), safe_get(q), safe_get(g));
2✔
410
      auto dsa = std::make_unique<Botan::DSA_PublicKey>(group, safe_get(y));
2✔
411
      return ffi_new_object(key, std::move(dsa));
2✔
412
   });
6✔
413
#else
414
   BOTAN_UNUSED(key, p, q, g, y);
415
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
416
#endif
417
}
418

419
int botan_privkey_dsa_get_x(botan_mp_t x, botan_privkey_t key) {
2✔
420
   return botan_privkey_get_field(x, key, "x");
2✔
421
}
422

423
int botan_pubkey_dsa_get_p(botan_mp_t p, botan_pubkey_t key) {
2✔
424
   return botan_pubkey_get_field(p, key, "p");
2✔
425
}
426

427
int botan_pubkey_dsa_get_q(botan_mp_t q, botan_pubkey_t key) {
2✔
428
   return botan_pubkey_get_field(q, key, "q");
2✔
429
}
430

431
int botan_pubkey_dsa_get_g(botan_mp_t g, botan_pubkey_t key) {
2✔
432
   return botan_pubkey_get_field(g, key, "g");
2✔
433
}
434

435
int botan_pubkey_dsa_get_y(botan_mp_t y, botan_pubkey_t key) {
2✔
436
   return botan_pubkey_get_field(y, key, "y");
2✔
437
}
438

439
int botan_privkey_create_ecdsa(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
1✔
440
   return botan_privkey_create(key_obj, "ECDSA", param_str, rng_obj);
1✔
441
}
442

443
/* ECDSA specific operations */
444

445
int botan_pubkey_ecc_key_used_explicit_encoding(botan_pubkey_t key) {
9✔
446
#if defined(BOTAN_HAS_ECC_KEY)
447
   return ffi_guard_thunk(__func__, [=]() -> int {
9✔
448
      const Botan::Public_Key& pub_key = safe_get(key);
9✔
449
      const Botan::EC_PublicKey* ec_key = dynamic_cast<const Botan::EC_PublicKey*>(&pub_key);
9✔
450

451
      if(ec_key == nullptr) {
9✔
452
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
453
      }
454

455
      return ec_key->domain().used_explicit_encoding() ? 1 : 0;
8✔
456
   });
9✔
457
#else
458
   BOTAN_UNUSED(key);
459
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
460
#endif
461
}
462

463
// NOLINTBEGIN(misc-misplaced-const)
464

465
int botan_pubkey_load_ecdsa(botan_pubkey_t* key,
2✔
466
                            const botan_mp_t public_x,
467
                            const botan_mp_t public_y,
468
                            const char* curve_name) {
469
#if defined(BOTAN_HAS_ECDSA)
470
   if(Botan::any_null_pointers(key, curve_name)) {
2✔
471
      return BOTAN_FFI_ERROR_NULL_POINTER;
472
   }
473
   *key = nullptr;
2✔
474

475
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
476
      std::unique_ptr<Botan::ECDSA_PublicKey> p_key;
2✔
477

478
      const int rc = pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name);
2✔
479
      if(rc == BOTAN_FFI_SUCCESS) {
2✔
480
         ffi_new_object(key, std::move(p_key));
2✔
481
      }
482

483
      return rc;
2✔
484
   });
4✔
485
#else
486
   BOTAN_UNUSED(key, public_x, public_y, curve_name);
487
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
488
#endif
489
}
490

491
int botan_pubkey_load_ecdsa_sec1(botan_pubkey_t* key, const uint8_t sec1[], size_t sec1_len, const char* curve_name) {
3✔
492
#if defined(BOTAN_HAS_ECDSA)
493
   if(Botan::any_null_pointers(key, sec1, curve_name)) {
3✔
494
      return BOTAN_FFI_ERROR_NULL_POINTER;
495
   }
496
   *key = nullptr;
3✔
497

498
   return ffi_guard_thunk(__func__, [=]() -> int {
3✔
499
      std::unique_ptr<Botan::ECDSA_PublicKey> p_key;
3✔
500

501
      const int rc = pubkey_load_ec_sec1(p_key, {sec1, sec1_len}, curve_name);
3✔
502
      if(rc == BOTAN_FFI_SUCCESS) {
3✔
503
         ffi_new_object(key, std::move(p_key));
3✔
504
      }
505

506
      return rc;
3✔
507
   });
6✔
508
#else
509
   BOTAN_UNUSED(key, sec1, sec1_len, curve_name);
510
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
511
#endif
512
}
513

514
int botan_privkey_load_ecdsa(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
2✔
515
#if defined(BOTAN_HAS_ECDSA)
516
   if(Botan::any_null_pointers(key, curve_name)) {
2✔
517
      return BOTAN_FFI_ERROR_NULL_POINTER;
518
   }
519
   *key = nullptr;
2✔
520

521
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
522
      std::unique_ptr<Botan::ECDSA_PrivateKey> p_key;
2✔
523
      const int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
2✔
524
      if(rc == BOTAN_FFI_SUCCESS) {
2✔
525
         ffi_new_object(key, std::move(p_key));
4✔
526
      }
527
      return rc;
2✔
528
   });
4✔
529
#else
530
   BOTAN_UNUSED(key, scalar, curve_name);
531
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
532
#endif
533
}
534

535
/* ElGamal specific operations */
536
int botan_privkey_create_elgamal(botan_privkey_t* key, botan_rng_t rng_obj, size_t pbits, size_t qbits) {
1✔
537
#if defined(BOTAN_HAS_ELGAMAL)
538
   if(Botan::any_null_pointers(key, rng_obj)) {
1✔
539
      return BOTAN_FFI_ERROR_NULL_POINTER;
540
   }
541
   *key = nullptr;
1✔
542

543
   if(pbits < 1024 || qbits < 160) {
1✔
544
      return BOTAN_FFI_ERROR_BAD_PARAMETER;
545
   }
546

547
   const Botan::DL_Group::PrimeType prime_type =
1✔
548
      ((pbits - 1) == qbits) ? Botan::DL_Group::Strong : Botan::DL_Group::Prime_Subgroup;
1✔
549

550
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
551
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
1✔
552
      const Botan::DL_Group group(rng, prime_type, pbits, qbits);
1✔
553
      auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(rng, group);
1✔
554
      return ffi_new_object(key, std::move(elg));
1✔
555
   });
3✔
556
#else
557
   BOTAN_UNUSED(key, rng_obj, pbits, qbits);
558
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
559
#endif
560
}
561

562
int botan_pubkey_load_elgamal(botan_pubkey_t* key, botan_mp_t p, botan_mp_t g, botan_mp_t y) {
2✔
563
#if defined(BOTAN_HAS_ELGAMAL)
564
   if(key == nullptr) {
2✔
565
      return BOTAN_FFI_ERROR_NULL_POINTER;
566
   }
567
   *key = nullptr;
2✔
568
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
569
      const Botan::DL_Group group(safe_get(p), safe_get(g));
2✔
570
      auto elg = std::make_unique<Botan::ElGamal_PublicKey>(group, safe_get(y));
2✔
571
      return ffi_new_object(key, std::move(elg));
2✔
572
   });
6✔
573
#else
574
   BOTAN_UNUSED(key, p, g, y);
575
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
576
#endif
577
}
578

579
int botan_privkey_load_elgamal(botan_privkey_t* key, botan_mp_t p, botan_mp_t g, botan_mp_t x) {
2✔
580
#if defined(BOTAN_HAS_ELGAMAL)
581
   if(key == nullptr) {
2✔
582
      return BOTAN_FFI_ERROR_NULL_POINTER;
583
   }
584
   *key = nullptr;
2✔
585
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
586
      const Botan::DL_Group group(safe_get(p), safe_get(g));
2✔
587
      auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(group, safe_get(x));
2✔
588
      return ffi_new_object(key, std::move(elg));
2✔
589
   });
6✔
590
#else
591
   BOTAN_UNUSED(key, p, g, x);
592
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
593
#endif
594
}
595

596
/* Diffie Hellman specific operations */
597

598
int botan_privkey_create_dh(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
2✔
599
   return botan_privkey_create(key_obj, "DH", param_str, rng_obj);
2✔
600
}
601

602
int botan_privkey_load_dh(botan_privkey_t* key, botan_mp_t p, botan_mp_t g, botan_mp_t x) {
1✔
603
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
604
   if(key == nullptr) {
1✔
605
      return BOTAN_FFI_ERROR_NULL_POINTER;
606
   }
607
   *key = nullptr;
1✔
608
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
609
      const Botan::DL_Group group(safe_get(p), safe_get(g));
1✔
610
      auto dh = std::make_unique<Botan::DH_PrivateKey>(group, safe_get(x));
1✔
611
      return ffi_new_object(key, std::move(dh));
1✔
612
   });
3✔
613
#else
614
   BOTAN_UNUSED(key, p, g, x);
615
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
616
#endif
617
}
618

619
int botan_pubkey_load_dh(botan_pubkey_t* key, botan_mp_t p, botan_mp_t g, botan_mp_t y) {
1✔
620
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
621
   if(key == nullptr) {
1✔
622
      return BOTAN_FFI_ERROR_NULL_POINTER;
623
   }
624
   *key = nullptr;
1✔
625
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
626
      const Botan::DL_Group group(safe_get(p), safe_get(g));
1✔
627
      auto dh = std::make_unique<Botan::DH_PublicKey>(group, safe_get(y));
1✔
628
      return ffi_new_object(key, std::move(dh));
1✔
629
   });
3✔
630
#else
631
   BOTAN_UNUSED(key, p, g, y);
632
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
633
#endif
634
}
635

636
/* ECDH + x25519/x448 specific operations */
637

638
int botan_privkey_create_ecdh(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
2✔
639
   if(Botan::any_null_pointers(key_obj, param_str)) {
2✔
640
      return BOTAN_FFI_ERROR_NULL_POINTER;
641
   }
642
   *key_obj = nullptr;
2✔
643

644
   const std::string params(param_str);
2✔
645

646
   if(params == "X25519" || params == "x25519" || params == "curve25519") {
2✔
647
      return botan_privkey_create(key_obj, "X25519", "", rng_obj);
×
648
   }
649

650
   if(params == "X448" || params == "x448") {
2✔
651
      return botan_privkey_create(key_obj, "X448", "", rng_obj);
×
652
   }
653

654
   return botan_privkey_create(key_obj, "ECDH", param_str, rng_obj);
2✔
655
}
2✔
656

657
int botan_pubkey_load_ecdh(botan_pubkey_t* key,
1✔
658
                           const botan_mp_t public_x,
659
                           const botan_mp_t public_y,
660
                           const char* curve_name) {
661
#if defined(BOTAN_HAS_ECDH)
662
   if(Botan::any_null_pointers(key, curve_name)) {
1✔
663
      return BOTAN_FFI_ERROR_NULL_POINTER;
664
   }
665
   *key = nullptr;
1✔
666
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
667
      std::unique_ptr<Botan::ECDH_PublicKey> p_key;
1✔
668
      const int rc = pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name);
1✔
669

670
      if(rc == BOTAN_FFI_SUCCESS) {
1✔
671
         ffi_new_object(key, std::move(p_key));
1✔
672
      }
673
      return rc;
1✔
674
   });
2✔
675
#else
676
   BOTAN_UNUSED(key, public_x, public_y, curve_name);
677
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
678
#endif
679
}
680

681
int botan_pubkey_load_ecdh_sec1(botan_pubkey_t* key, const uint8_t sec1[], size_t sec1_len, const char* curve_name) {
7✔
682
#if defined(BOTAN_HAS_ECDH)
683
   if(Botan::any_null_pointers(key, sec1, curve_name)) {
7✔
684
      return BOTAN_FFI_ERROR_NULL_POINTER;
685
   }
686
   *key = nullptr;
7✔
687

688
   return ffi_guard_thunk(__func__, [=]() -> int {
7✔
689
      std::unique_ptr<Botan::ECDH_PublicKey> p_key;
7✔
690

691
      const int rc = pubkey_load_ec_sec1(p_key, {sec1, sec1_len}, curve_name);
7✔
692
      if(rc == BOTAN_FFI_SUCCESS) {
7✔
693
         ffi_new_object(key, std::move(p_key));
7✔
694
      }
695

696
      return rc;
7✔
697
   });
14✔
698
#else
699
   BOTAN_UNUSED(key, sec1, sec1_len, curve_name);
700
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
701
#endif
702
}
703

704
int botan_privkey_load_ecdh(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
4✔
705
#if defined(BOTAN_HAS_ECDH)
706
   if(Botan::any_null_pointers(key, curve_name)) {
4✔
707
      return BOTAN_FFI_ERROR_NULL_POINTER;
708
   }
709
   *key = nullptr;
4✔
710
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
711
      std::unique_ptr<Botan::ECDH_PrivateKey> p_key;
4✔
712
      const int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
4✔
713
      if(rc == BOTAN_FFI_SUCCESS) {
4✔
714
         ffi_new_object(key, std::move(p_key));
8✔
715
      }
716
      return rc;
4✔
717
   });
8✔
718
#else
719
   BOTAN_UNUSED(key, scalar, curve_name);
720
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
721
#endif
722
}
723

724
/* SM2 specific operations */
725

726
int botan_pubkey_sm2_compute_za(
2✔
727
   uint8_t out[], size_t* out_len, const char* ident, const char* hash_algo, const botan_pubkey_t key) {
728
   if(Botan::any_null_pointers(out, out_len, ident, hash_algo, key)) {
2✔
729
      return BOTAN_FFI_ERROR_NULL_POINTER;
730
   }
731

732
#if defined(BOTAN_HAS_SM2)
733
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
734
      const Botan::Public_Key& pub_key = safe_get(key);
2✔
735
      const Botan::EC_PublicKey* ec_key = dynamic_cast<const Botan::EC_PublicKey*>(&pub_key);
2✔
736

737
      if(ec_key == nullptr) {
2✔
738
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
739
      }
740

741
      if(ec_key->algo_name() != "SM2") {
2✔
742
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
743
      }
744

745
      const std::string ident_str(ident);
2✔
746
      std::unique_ptr<Botan::HashFunction> hash = Botan::HashFunction::create_or_throw(hash_algo);
2✔
747

748
      const auto& pt = ec_key->_public_ec_point();
2✔
749

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

752
      return write_vec_output(out, out_len, za);
2✔
753
   });
6✔
754
#else
755
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
756
#endif
757
}
758

759
int botan_pubkey_load_sm2(botan_pubkey_t* key,
3✔
760
                          const botan_mp_t public_x,
761
                          const botan_mp_t public_y,
762
                          const char* curve_name) {
763
#if defined(BOTAN_HAS_SM2)
764
   if(Botan::any_null_pointers(key, curve_name)) {
3✔
765
      return BOTAN_FFI_ERROR_NULL_POINTER;
766
   }
767
   *key = nullptr;
3✔
768

769
   return ffi_guard_thunk(__func__, [=]() -> int {
3✔
770
      std::unique_ptr<Botan::SM2_PublicKey> p_key;
3✔
771
      if(pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name) == 0) {
3✔
772
         return ffi_new_object(key, std::move(p_key));
3✔
773
      } else {
774
         return BOTAN_FFI_ERROR_UNKNOWN_ERROR;
775
      }
776
   });
6✔
777
#else
778
   BOTAN_UNUSED(key, public_x, public_y, curve_name);
779
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
780
#endif
781
}
782

783
int botan_pubkey_load_sm2_sec1(botan_pubkey_t* key, const uint8_t sec1[], size_t sec1_len, const char* curve_name) {
4✔
784
#if defined(BOTAN_HAS_SM2)
785
   if(Botan::any_null_pointers(key, sec1, curve_name)) {
4✔
786
      return BOTAN_FFI_ERROR_NULL_POINTER;
787
   }
788
   *key = nullptr;
4✔
789

790
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
791
      std::unique_ptr<Botan::SM2_PublicKey> p_key;
4✔
792

793
      const int rc = pubkey_load_ec_sec1(p_key, {sec1, sec1_len}, curve_name);
4✔
794
      if(rc == BOTAN_FFI_SUCCESS) {
4✔
795
         ffi_new_object(key, std::move(p_key));
4✔
796
      }
797

798
      return rc;
4✔
799
   });
8✔
800
#else
801
   BOTAN_UNUSED(key, sec1, sec1_len, curve_name);
802
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
803
#endif
804
}
805

806
int botan_privkey_load_sm2(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
3✔
807
#if defined(BOTAN_HAS_SM2)
808
   if(Botan::any_null_pointers(key, curve_name)) {
3✔
809
      return BOTAN_FFI_ERROR_NULL_POINTER;
810
   }
811
   *key = nullptr;
3✔
812

813
   return ffi_guard_thunk(__func__, [=]() -> int {
3✔
814
      std::unique_ptr<Botan::SM2_PrivateKey> p_key;
3✔
815
      const int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
3✔
816

817
      if(rc == BOTAN_FFI_SUCCESS) {
3✔
818
         ffi_new_object(key, std::move(p_key));
6✔
819
      }
820
      return rc;
3✔
821
   });
6✔
822
#else
823
   BOTAN_UNUSED(key, scalar, curve_name);
824
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
825
#endif
826
}
827

828
int botan_pubkey_load_sm2_enc(botan_pubkey_t* key,
1✔
829
                              const botan_mp_t public_x,
830
                              const botan_mp_t public_y,
831
                              const char* curve_name) {
832
   return botan_pubkey_load_sm2(key, public_x, public_y, curve_name);
1✔
833
}
834

835
int botan_privkey_load_sm2_enc(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
1✔
836
   return botan_privkey_load_sm2(key, scalar, curve_name);
1✔
837
}
838

839
/* EC key specific operations */
840

841
int botan_ec_privkey_get_private_key(botan_privkey_t key, botan_ec_scalar_t* value) {
2✔
842
   if(Botan::any_null_pointers(value)) {
2✔
843
      return BOTAN_FFI_ERROR_NULL_POINTER;
844
   }
845
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
846
      const Botan::EC_PrivateKey* ec_key = dynamic_cast<const Botan::EC_PrivateKey*>(&safe_get(key));
4✔
847
      if(ec_key == nullptr) {
2✔
848
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
849
      }
850
      return ffi_new_object(value, std::make_unique<Botan::EC_Scalar>(ec_key->_private_key()));
2✔
851
   });
2✔
852
}
853

854
int botan_ec_privkey_get_group(botan_privkey_t key, botan_ec_group_t* ec_group) {
2✔
855
   if(Botan::any_null_pointers(ec_group)) {
2✔
856
      return BOTAN_FFI_ERROR_NULL_POINTER;
857
   }
858

859
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
860
      const Botan::EC_PrivateKey* ec_key = dynamic_cast<const Botan::EC_PrivateKey*>(&safe_get(key));
4✔
861
      if(ec_key == nullptr) {
2✔
862
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
863
      }
864
      return ffi_new_object(ec_group, std::make_unique<Botan::EC_Group>(ec_key->domain()));
2✔
865
   });
2✔
866
}
867

868
int botan_ec_pubkey_get_group(botan_pubkey_t key, botan_ec_group_t* ec_group) {
1✔
869
   if(Botan::any_null_pointers(ec_group)) {
1✔
870
      return BOTAN_FFI_ERROR_NULL_POINTER;
871
   }
872
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
873
      const Botan::EC_PublicKey* ec_key = dynamic_cast<const Botan::EC_PublicKey*>(&safe_get(key));
2✔
874
      if(ec_key == nullptr) {
1✔
875
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
876
      }
877
      return ffi_new_object(ec_group, std::make_unique<Botan::EC_Group>(ec_key->domain()));
1✔
878
   });
1✔
879
}
880

881
/* Ed25519 specific operations */
882

883
int botan_privkey_load_ed25519(botan_privkey_t* key, const uint8_t privkey[32]) {
1✔
884
#if defined(BOTAN_HAS_ED25519)
885
   if(key == nullptr) {
1✔
886
      return BOTAN_FFI_ERROR_NULL_POINTER;
887
   }
888
   *key = nullptr;
1✔
889
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
890
      auto ed25519 =
1✔
891
         std::make_unique<Botan::Ed25519_PrivateKey>(Botan::Ed25519_PrivateKey::from_seed(std::span{privkey, 32}));
2✔
892
      return ffi_new_object(key, std::move(ed25519));
2✔
893
   });
2✔
894
#else
895
   BOTAN_UNUSED(key, privkey);
896
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
897
#endif
898
}
899

900
int botan_pubkey_load_ed25519(botan_pubkey_t* key, const uint8_t pubkey[32]) {
1✔
901
#if defined(BOTAN_HAS_ED25519)
902
   if(key == nullptr) {
1✔
903
      return BOTAN_FFI_ERROR_NULL_POINTER;
904
   }
905
   *key = nullptr;
1✔
906
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
907
      const std::vector<uint8_t> pubkey_vec(pubkey, pubkey + 32);
1✔
908
      auto ed25519 = std::make_unique<Botan::Ed25519_PublicKey>(pubkey_vec);
1✔
909
      return ffi_new_object(key, std::move(ed25519));
1✔
910
   });
3✔
911
#else
912
   BOTAN_UNUSED(key, pubkey);
913
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
914
#endif
915
}
916

917
int botan_privkey_ed25519_get_privkey(botan_privkey_t key, uint8_t output[64]) {
1✔
918
   if(output == nullptr) {
1✔
919
      return BOTAN_FFI_ERROR_NULL_POINTER;
920
   }
921
#if defined(BOTAN_HAS_ED25519)
922
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
4✔
923
      if(auto ed = dynamic_cast<const Botan::Ed25519_PrivateKey*>(&k)) {
924
         const auto ed_key = ed->raw_private_key_bits();
925
         if(ed_key.size() != 64) {
926
            return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE;
927
         }
928
         Botan::copy_mem(output, ed_key.data(), ed_key.size());
929
         return BOTAN_FFI_SUCCESS;
930
      } else {
931
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
932
      }
933
   });
934
#else
935
   BOTAN_UNUSED(key, output);
936
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
937
#endif
938
}
939

940
int botan_pubkey_ed25519_get_pubkey(botan_pubkey_t key, uint8_t output[32]) {
1✔
941
   if(output == nullptr) {
1✔
942
      return BOTAN_FFI_ERROR_NULL_POINTER;
943
   }
944
#if defined(BOTAN_HAS_ED25519)
945
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
2✔
946
      if(auto ed = dynamic_cast<const Botan::Ed25519_PublicKey*>(&k)) {
947
         const std::vector<uint8_t>& ed_key = ed->get_public_key();
948
         if(ed_key.size() != 32) {
949
            return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE;
950
         }
951
         Botan::copy_mem(output, ed_key.data(), ed_key.size());
952
         return BOTAN_FFI_SUCCESS;
953
      } else {
954
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
955
      }
956
   });
957
#else
958
   BOTAN_UNUSED(key, output);
959
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
960
#endif
961
}
962

963
/* Ed448 specific operations */
964

965
int botan_privkey_load_ed448(botan_privkey_t* key, const uint8_t privkey[57]) {
1✔
966
#if defined(BOTAN_HAS_ED448)
967
   if(key == nullptr) {
1✔
968
      return BOTAN_FFI_ERROR_NULL_POINTER;
969
   }
970
   *key = nullptr;
1✔
971
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
972
      auto ed448 = std::make_unique<Botan::Ed448_PrivateKey>(std::span(privkey, 57));
1✔
973
      return ffi_new_object(key, std::move(ed448));
2✔
974
   });
2✔
975
#else
976
   BOTAN_UNUSED(key, privkey);
977
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
978
#endif
979
}
980

981
int botan_pubkey_load_ed448(botan_pubkey_t* key, const uint8_t pubkey[57]) {
1✔
982
#if defined(BOTAN_HAS_ED448)
983
   if(key == nullptr) {
1✔
984
      return BOTAN_FFI_ERROR_NULL_POINTER;
985
   }
986
   *key = nullptr;
1✔
987
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
988
      auto ed448 = std::make_unique<Botan::Ed448_PublicKey>(std::span(pubkey, 57));
1✔
989
      return ffi_new_object(key, std::move(ed448));
1✔
990
   });
2✔
991
#else
992
   BOTAN_UNUSED(key, pubkey);
993
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
994
#endif
995
}
996

997
int botan_privkey_ed448_get_privkey(botan_privkey_t key, uint8_t output[57]) {
1✔
998
   if(output == nullptr) {
1✔
999
      return BOTAN_FFI_ERROR_NULL_POINTER;
1000
   }
1001
#if defined(BOTAN_HAS_ED448)
1002
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
4✔
1003
      if(auto ed = dynamic_cast<const Botan::Ed448_PrivateKey*>(&k)) {
1004
         const auto ed_key = ed->raw_private_key_bits();
1005
         Botan::copy_mem(std::span(output, 57), ed_key);
1006
         return BOTAN_FFI_SUCCESS;
1007
      } else {
1008
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1009
      }
1010
   });
1011
#else
1012
   BOTAN_UNUSED(key, output);
1013
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1014
#endif
1015
}
1016

1017
int botan_pubkey_ed448_get_pubkey(botan_pubkey_t key, uint8_t output[57]) {
1✔
1018
   if(output == nullptr) {
1✔
1019
      return BOTAN_FFI_ERROR_NULL_POINTER;
1020
   }
1021
#if defined(BOTAN_HAS_ED448)
1022
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
3✔
1023
      if(auto ed = dynamic_cast<const Botan::Ed448_PublicKey*>(&k)) {
1024
         const auto ed_key = ed->public_key_bits();
1025
         Botan::copy_mem(std::span(output, 57), ed_key);
1026
         return BOTAN_FFI_SUCCESS;
1027
      } else {
1028
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1029
      }
1030
   });
1031
#else
1032
   BOTAN_UNUSED(key, output);
1033
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1034
#endif
1035
}
1036

1037
/* X25519 specific operations */
1038

1039
int botan_privkey_load_x25519(botan_privkey_t* key, const uint8_t privkey[32]) {
1✔
1040
#if defined(BOTAN_HAS_X25519)
1041
   if(key == nullptr) {
1✔
1042
      return BOTAN_FFI_ERROR_NULL_POINTER;
1043
   }
1044
   *key = nullptr;
1✔
1045
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
1046
      auto x25519 = std::make_unique<Botan::X25519_PrivateKey>(std::span{privkey, 32});
1✔
1047
      return ffi_new_object(key, std::move(x25519));
2✔
1048
   });
2✔
1049
#else
1050
   BOTAN_UNUSED(key, privkey);
1051
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1052
#endif
1053
}
1054

1055
int botan_pubkey_load_x25519(botan_pubkey_t* key, const uint8_t pubkey[32]) {
1✔
1056
#if defined(BOTAN_HAS_X25519)
1057
   if(key == nullptr) {
1✔
1058
      return BOTAN_FFI_ERROR_NULL_POINTER;
1059
   }
1060
   *key = nullptr;
1✔
1061
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
1062
      auto x25519 = std::make_unique<Botan::X25519_PublicKey>(std::span{pubkey, 32});
1✔
1063
      return ffi_new_object(key, std::move(x25519));
1✔
1064
   });
2✔
1065
#else
1066
   BOTAN_UNUSED(key, pubkey);
1067
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1068
#endif
1069
}
1070

1071
int botan_privkey_x25519_get_privkey(botan_privkey_t key, uint8_t output[32]) {
1✔
1072
   if(output == nullptr) {
1✔
1073
      return BOTAN_FFI_ERROR_NULL_POINTER;
1074
   }
1075
#if defined(BOTAN_HAS_X25519)
1076
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
4✔
1077
      if(auto x25519 = dynamic_cast<const Botan::X25519_PrivateKey*>(&k)) {
1078
         const auto x25519_key = x25519->raw_private_key_bits();
1079
         if(x25519_key.size() != 32) {
1080
            return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE;
1081
         }
1082
         Botan::copy_mem(output, x25519_key.data(), x25519_key.size());
1083
         return BOTAN_FFI_SUCCESS;
1084
      } else {
1085
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1086
      }
1087
   });
1088
#else
1089
   BOTAN_UNUSED(key, output);
1090
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1091
#endif
1092
}
1093

1094
int botan_pubkey_x25519_get_pubkey(botan_pubkey_t key, uint8_t output[32]) {
2✔
1095
   if(output == nullptr) {
2✔
1096
      return BOTAN_FFI_ERROR_NULL_POINTER;
1097
   }
1098
#if defined(BOTAN_HAS_X25519)
1099
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
6✔
1100
      if(auto x25519 = dynamic_cast<const Botan::X25519_PublicKey*>(&k)) {
1101
         Botan::copy_mem(std::span{output, 32}, x25519->raw_public_key_bits());
1102
         return BOTAN_FFI_SUCCESS;
1103
      } else {
1104
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1105
      }
1106
   });
1107
#else
1108
   BOTAN_UNUSED(key, output);
1109
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1110
#endif
1111
}
1112

1113
/* X448 specific operations */
1114

1115
int botan_privkey_load_x448(botan_privkey_t* key, const uint8_t privkey[56]) {
1✔
1116
#if defined(BOTAN_HAS_X448)
1117
   if(key == nullptr) {
1✔
1118
      return BOTAN_FFI_ERROR_NULL_POINTER;
1119
   }
1120
   *key = nullptr;
1✔
1121
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
1122
      auto x448 = std::make_unique<Botan::X448_PrivateKey>(std::span{privkey, 56});
1✔
1123
      return ffi_new_object(key, std::move(x448));
2✔
1124
   });
2✔
1125
#else
1126
   BOTAN_UNUSED(key, privkey);
1127
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1128
#endif
1129
}
1130

1131
int botan_pubkey_load_x448(botan_pubkey_t* key, const uint8_t pubkey[56]) {
1✔
1132
#if defined(BOTAN_HAS_X448)
1133
   if(key == nullptr) {
1✔
1134
      return BOTAN_FFI_ERROR_NULL_POINTER;
1135
   }
1136
   *key = nullptr;
1✔
1137
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
1138
      auto x448 = std::make_unique<Botan::X448_PublicKey>(std::span{pubkey, 56});
1✔
1139
      return ffi_new_object(key, std::move(x448));
1✔
1140
   });
2✔
1141
#else
1142
   BOTAN_UNUSED(key, pubkey);
1143
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1144
#endif
1145
}
1146

1147
int botan_privkey_x448_get_privkey(botan_privkey_t key, uint8_t output[56]) {
1✔
1148
   if(output == nullptr) {
1✔
1149
      return BOTAN_FFI_ERROR_NULL_POINTER;
1150
   }
1151
#if defined(BOTAN_HAS_X448)
1152
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
4✔
1153
      if(auto x448 = dynamic_cast<const Botan::X448_PrivateKey*>(&k)) {
1154
         const auto x448_key = x448->raw_private_key_bits();
1155
         Botan::copy_mem(std::span{output, 56}, x448_key);
1156
         return BOTAN_FFI_SUCCESS;
1157
      } else {
1158
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1159
      }
1160
   });
1161
#else
1162
   BOTAN_UNUSED(key, output);
1163
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1164
#endif
1165
}
1166

1167
int botan_pubkey_x448_get_pubkey(botan_pubkey_t key, uint8_t output[56]) {
2✔
1168
   if(output == nullptr) {
2✔
1169
      return BOTAN_FFI_ERROR_NULL_POINTER;
1170
   }
1171
#if defined(BOTAN_HAS_X448)
1172
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
6✔
1173
      if(auto x448 = dynamic_cast<const Botan::X448_PublicKey*>(&k)) {
1174
         Botan::copy_mem(std::span{output, 56}, x448->raw_public_key_bits());
1175
         return BOTAN_FFI_SUCCESS;
1176
      } else {
1177
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1178
      }
1179
   });
1180
#else
1181
   BOTAN_UNUSED(key, output);
1182
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1183
#endif
1184
}
1185

1186
/*
1187
* Algorithm specific key operations: Kyber
1188
*/
1189

1190
int botan_privkey_load_kyber(botan_privkey_t* key, const uint8_t privkey[], size_t key_len) {
4✔
1191
#if defined(BOTAN_HAS_KYBER)
1192
   if(Botan::any_null_pointers(key, privkey)) {
4✔
1193
      return BOTAN_FFI_ERROR_NULL_POINTER;
1194
   }
1195
   *key = nullptr;
4✔
1196

1197
   const auto mode = [](size_t len) -> std::optional<Botan::KyberMode> {
12✔
1198
      if(len == 1632) {
4✔
1199
         return Botan::KyberMode::Kyber512_R3;
1✔
1200
      } else if(len == 2400) {
3✔
1201
         return Botan::KyberMode::Kyber768_R3;
2✔
1202
      } else if(len == 3168) {
1✔
1203
         return Botan::KyberMode::Kyber1024_R3;
1✔
1204
      } else {
1205
         return {};
×
1206
      }
1207
   }(key_len);
4✔
1208

1209
   if(mode.has_value()) {
4✔
1210
      return ffi_guard_thunk(__func__, [=]() -> int {
4✔
1211
         auto kyber = std::make_unique<Botan::Kyber_PrivateKey>(std::span{privkey, key_len}, *mode);
4✔
1212
         return ffi_new_object(key, std::move(kyber));
8✔
1213
      });
4✔
1214
   } else {
1215
      return BOTAN_FFI_ERROR_BAD_PARAMETER;
1216
   }
1217
#else
1218
   BOTAN_UNUSED(key, key_len, privkey);
1219
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1220
#endif
1221
}
1222

1223
int botan_pubkey_load_kyber(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len) {
4✔
1224
#if defined(BOTAN_HAS_KYBER)
1225
   if(Botan::any_null_pointers(key, pubkey)) {
4✔
1226
      return BOTAN_FFI_ERROR_NULL_POINTER;
1227
   }
1228
   *key = nullptr;
4✔
1229

1230
   const auto mode = [](size_t len) -> std::optional<Botan::KyberMode> {
12✔
1231
      if(len == 800) {
4✔
1232
         return Botan::KyberMode::Kyber512_R3;
1✔
1233
      } else if(len == 1184) {
3✔
1234
         return Botan::KyberMode::Kyber768_R3;
2✔
1235
      } else if(len == 1568) {
1✔
1236
         return Botan::KyberMode::Kyber1024_R3;
1✔
1237
      } else {
1238
         return {};
×
1239
      }
1240
   }(key_len);
4✔
1241

1242
   if(mode.has_value()) {
4✔
1243
      return ffi_guard_thunk(__func__, [=]() -> int {
4✔
1244
         auto kyber = std::make_unique<Botan::Kyber_PublicKey>(std::span{pubkey, key_len}, *mode);
4✔
1245
         return ffi_new_object(key, std::move(kyber));
4✔
1246
      });
4✔
1247
   } else {
1248
      return BOTAN_FFI_ERROR_BAD_PARAMETER;
1249
   }
1250
#else
1251
   BOTAN_UNUSED(key, pubkey, key_len);
1252
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1253
#endif
1254
}
1255

1256
int botan_privkey_view_kyber_raw_key(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) {
4✔
1257
#if defined(BOTAN_HAS_KYBER)
1258
   return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
16✔
1259
      if(auto kyber = dynamic_cast<const Botan::Kyber_PrivateKey*>(&k)) {
1260
         return invoke_view_callback(view, ctx, kyber->raw_private_key_bits());
1261
      } else {
1262
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1263
      }
1264
   });
1265
#else
1266
   BOTAN_UNUSED(key, ctx, view);
1267
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1268
#endif
1269
}
1270

1271
int botan_pubkey_view_kyber_raw_key(botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) {
8✔
1272
#if defined(BOTAN_HAS_KYBER)
1273
   return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
32✔
1274
      if(auto kyber = dynamic_cast<const Botan::Kyber_PublicKey*>(&k)) {
1275
         return invoke_view_callback(view, ctx, kyber->public_key_bits());
1276
      } else {
1277
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1278
      }
1279
   });
1280
#else
1281
   BOTAN_UNUSED(key, ctx, view);
1282
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1283
#endif
1284
}
1285

1286
/*
1287
* Algorithm specific key operations: ML-KEM
1288
*/
1289

1290
int botan_privkey_load_ml_kem(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* mlkem_mode) {
4✔
1291
#if defined(BOTAN_HAS_ML_KEM)
1292
   if(Botan::any_null_pointers(key, privkey, mlkem_mode)) {
4✔
1293
      return BOTAN_FFI_ERROR_NULL_POINTER;
1294
   }
1295

1296
   *key = nullptr;
4✔
1297

1298
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
1299
      auto mode = Botan::ML_KEM_Mode(mlkem_mode);
4✔
1300
      if(!mode.is_ml_kem()) {
4✔
1301
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1302
      }
1303

1304
      auto mlkem_key = std::make_unique<Botan::ML_KEM_PrivateKey>(std::span{privkey, key_len}, mode);
4✔
1305
      return ffi_new_object(key, std::move(mlkem_key));
4✔
1306
   });
8✔
1307
#else
1308
   BOTAN_UNUSED(key, key_len, privkey, mlkem_mode);
1309
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1310
#endif
1311
}
1312

1313
int botan_pubkey_load_ml_kem(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* mlkem_mode) {
4✔
1314
#if defined(BOTAN_HAS_ML_KEM)
1315
   if(Botan::any_null_pointers(key, pubkey, mlkem_mode)) {
4✔
1316
      return BOTAN_FFI_ERROR_NULL_POINTER;
1317
   }
1318

1319
   *key = nullptr;
4✔
1320

1321
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
1322
      auto mode = Botan::ML_KEM_Mode(mlkem_mode);
4✔
1323
      if(!mode.is_ml_kem()) {
4✔
1324
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1325
      }
1326

1327
      auto mlkem_key = std::make_unique<Botan::ML_KEM_PublicKey>(std::span{pubkey, key_len}, mode.mode());
4✔
1328
      return ffi_new_object(key, std::move(mlkem_key));
4✔
1329
   });
8✔
1330
#else
1331
   BOTAN_UNUSED(key, key_len, pubkey, mlkem_mode);
1332
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1333
#endif
1334
}
1335

1336
/*
1337
* Algorithm specific key operations: ML-DSA
1338
*/
1339

1340
int botan_privkey_load_ml_dsa(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* mldsa_mode) {
4✔
1341
#if defined(BOTAN_HAS_ML_DSA)
1342
   if(Botan::any_null_pointers(key, privkey, mldsa_mode)) {
4✔
1343
      return BOTAN_FFI_ERROR_NULL_POINTER;
1344
   }
1345

1346
   *key = nullptr;
4✔
1347

1348
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
1349
      auto mode = Botan::ML_DSA_Mode(mldsa_mode);
4✔
1350
      if(!mode.is_ml_dsa()) {
4✔
1351
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1352
      }
1353

1354
      auto mldsa_key = std::make_unique<Botan::ML_DSA_PrivateKey>(std::span{privkey, key_len}, mode);
4✔
1355
      return ffi_new_object(key, std::move(mldsa_key));
4✔
1356
   });
8✔
1357
#else
1358
   BOTAN_UNUSED(key, key_len, privkey, mldsa_mode);
1359
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1360
#endif
1361
}
1362

1363
int botan_pubkey_load_ml_dsa(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* mldsa_mode) {
4✔
1364
#if defined(BOTAN_HAS_ML_DSA)
1365
   if(Botan::any_null_pointers(key, pubkey, mldsa_mode)) {
4✔
1366
      return BOTAN_FFI_ERROR_NULL_POINTER;
1367
   }
1368

1369
   *key = nullptr;
4✔
1370

1371
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
1372
      auto mode = Botan::ML_DSA_Mode(mldsa_mode);
4✔
1373
      if(!mode.is_ml_dsa()) {
4✔
1374
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1375
      }
1376

1377
      auto mldsa_key = std::make_unique<Botan::ML_DSA_PublicKey>(std::span{pubkey, key_len}, mode);
4✔
1378
      return ffi_new_object(key, std::move(mldsa_key));
4✔
1379
   });
8✔
1380
#else
1381
   BOTAN_UNUSED(key, key_len, pubkey, mldsa_mode);
1382
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1383
#endif
1384
}
1385

1386
/*
1387
* Algorithm specific key operations: SLH-DSA
1388
*/
1389

1390
int botan_privkey_load_slh_dsa(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* slhdsa_mode) {
13✔
1391
#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
1392
   if(Botan::any_null_pointers(key, privkey, slhdsa_mode)) {
13✔
1393
      return BOTAN_FFI_ERROR_NULL_POINTER;
1394
   }
1395

1396
   *key = nullptr;
13✔
1397

1398
   return ffi_guard_thunk(__func__, [=]() -> int {
13✔
1399
      auto mode = Botan::SLH_DSA_Parameters::create(slhdsa_mode);
13✔
1400
      if(!mode.is_slh_dsa()) {
13✔
1401
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1402
      }
1403

1404
      auto slhdsa_key = std::make_unique<Botan::SLH_DSA_PrivateKey>(std::span{privkey, key_len}, mode);
13✔
1405
      return ffi_new_object(key, std::move(slhdsa_key));
13✔
1406
   });
26✔
1407
#else
1408
   BOTAN_UNUSED(key, key_len, privkey, slhdsa_mode);
1409
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1410
#endif
1411
}
1412

1413
int botan_pubkey_load_slh_dsa(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* slhdsa_mode) {
13✔
1414
#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
1415
   if(Botan::any_null_pointers(key, pubkey, slhdsa_mode)) {
13✔
1416
      return BOTAN_FFI_ERROR_NULL_POINTER;
1417
   }
1418

1419
   *key = nullptr;
13✔
1420

1421
   return ffi_guard_thunk(__func__, [=]() -> int {
13✔
1422
      auto mode = Botan::SLH_DSA_Parameters::create(slhdsa_mode);
13✔
1423
      if(!mode.is_slh_dsa()) {
13✔
1424
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1425
      }
1426

1427
      auto mldsa_key = std::make_unique<Botan::SLH_DSA_PublicKey>(std::span{pubkey, key_len}, mode);
13✔
1428
      return ffi_new_object(key, std::move(mldsa_key));
13✔
1429
   });
26✔
1430
#else
1431
   BOTAN_UNUSED(key, key_len, pubkey, slhdsa_mode);
1432
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1433
#endif
1434
}
1435

1436
/*
1437
* Algorithm specific key operations: FrodoKEM
1438
*/
1439

1440
int botan_privkey_load_frodokem(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* frodo_mode) {
13✔
1441
#if defined(BOTAN_HAS_FRODOKEM)
1442
   if(Botan::any_null_pointers(key, privkey, frodo_mode)) {
13✔
1443
      return BOTAN_FFI_ERROR_NULL_POINTER;
1444
   }
1445

1446
   *key = nullptr;
13✔
1447

1448
   return ffi_guard_thunk(__func__, [=]() -> int {
13✔
1449
      const auto mode = Botan::FrodoKEMMode(frodo_mode);
13✔
1450
      auto frodo_key = std::make_unique<Botan::FrodoKEM_PrivateKey>(std::span{privkey, key_len}, mode);
13✔
1451
      return ffi_new_object(key, std::move(frodo_key));
26✔
1452
   });
26✔
1453
#else
1454
   BOTAN_UNUSED(key, privkey, key_len, frodo_mode);
1455
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1456
#endif
1457
}
1458

1459
int botan_pubkey_load_frodokem(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* frodo_mode) {
13✔
1460
#if defined(BOTAN_HAS_FRODOKEM)
1461
   if(Botan::any_null_pointers(key, pubkey, frodo_mode)) {
13✔
1462
      return BOTAN_FFI_ERROR_NULL_POINTER;
1463
   }
1464

1465
   *key = nullptr;
13✔
1466

1467
   return ffi_guard_thunk(__func__, [=]() -> int {
13✔
1468
      const auto mode = Botan::FrodoKEMMode(frodo_mode);
13✔
1469
      auto frodo_key = std::make_unique<Botan::FrodoKEM_PublicKey>(std::span{pubkey, key_len}, mode);
13✔
1470
      return ffi_new_object(key, std::move(frodo_key));
13✔
1471
   });
26✔
1472
#else
1473
   BOTAN_UNUSED(key, pubkey, key_len, frodo_mode);
1474
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1475
#endif
1476
}
1477

1478
/*
1479
* Algorithm specific key operations : Classic McEliece
1480
*/
1481

1482
int botan_privkey_load_classic_mceliece(botan_privkey_t* key,
17✔
1483
                                        const uint8_t privkey[],
1484
                                        size_t key_len,
1485
                                        const char* cmce_mode) {
1486
#if defined(BOTAN_HAS_CLASSICMCELIECE)
1487
   if(Botan::any_null_pointers(key, privkey, cmce_mode)) {
17✔
1488
      return BOTAN_FFI_ERROR_NULL_POINTER;
1489
   }
1490

1491
   *key = nullptr;
17✔
1492

1493
   return ffi_guard_thunk(__func__, [=]() -> int {
17✔
1494
      const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode);
17✔
1495
      auto cmce_key = std::make_unique<Botan::Classic_McEliece_PrivateKey>(std::span{privkey, key_len}, mode);
17✔
1496
      return ffi_new_object(key, std::move(cmce_key));
34✔
1497
   });
34✔
1498
#else
1499
   BOTAN_UNUSED(key, privkey, key_len, cmce_mode);
1500
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1501
#endif
1502
}
1503

1504
int botan_pubkey_load_classic_mceliece(botan_pubkey_t* key,
17✔
1505
                                       const uint8_t pubkey[],
1506
                                       size_t key_len,
1507
                                       const char* cmce_mode) {
1508
#if defined(BOTAN_HAS_CLASSICMCELIECE)
1509
   if(Botan::any_null_pointers(key, pubkey, cmce_mode)) {
17✔
1510
      return BOTAN_FFI_ERROR_NULL_POINTER;
1511
   }
1512

1513
   *key = nullptr;
17✔
1514

1515
   return ffi_guard_thunk(__func__, [=]() -> int {
17✔
1516
      const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode);
17✔
1517
      auto cmce_key = std::make_unique<Botan::Classic_McEliece_PublicKey>(std::span{pubkey, key_len}, mode);
17✔
1518
      return ffi_new_object(key, std::move(cmce_key));
17✔
1519
   });
34✔
1520
#else
1521
   BOTAN_UNUSED(key, pubkey, key_len, cmce_mode);
1522
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1523
#endif
1524
}
1525

1526
int botan_pubkey_view_ec_public_point(const botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) {
7✔
1527
#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
1528
   return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
28✔
1529
      if(auto ecc = dynamic_cast<const Botan::EC_PublicKey*>(&k)) {
1530
         auto pt = ecc->_public_ec_point().serialize_uncompressed();
1531
         return invoke_view_callback(view, ctx, pt);
1532
      } else {
1533
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1534
      }
1535
   });
1536
#else
1537
   BOTAN_UNUSED(key, view, ctx);
1538
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1539
#endif
1540
}
1541

1542
// NOLINTEND(misc-misplaced-const)
1543

1544
int botan_privkey_create_mceliece(botan_privkey_t* key_obj, botan_rng_t rng_obj, size_t n, size_t t) {
1✔
1545
   const std::string mce_params = std::to_string(n) + "," + std::to_string(t);
3✔
1546
   return botan_privkey_create(key_obj, "McEliece", mce_params.c_str(), rng_obj);
1✔
1547
}
1✔
1548

1549
int botan_mceies_decrypt(botan_privkey_t mce_key_obj,
×
1550
                         const char* aead,
1551
                         const uint8_t ct[],
1552
                         size_t ct_len,
1553
                         const uint8_t ad[],
1554
                         size_t ad_len,
1555
                         uint8_t out[],
1556
                         size_t* out_len) {
1557
   BOTAN_UNUSED(mce_key_obj, aead, ct, ct_len, ad, ad_len, out, out_len);
×
1558
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
×
1559
}
1560

1561
int botan_mceies_encrypt(botan_pubkey_t mce_key_obj,
×
1562
                         botan_rng_t rng_obj,
1563
                         const char* aead,
1564
                         const uint8_t pt[],
1565
                         size_t pt_len,
1566
                         const uint8_t ad[],
1567
                         size_t ad_len,
1568
                         uint8_t out[],
1569
                         size_t* out_len) {
1570
   BOTAN_UNUSED(mce_key_obj, rng_obj, aead, pt, pt_len, ad, ad_len, out, out_len);
×
1571
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
×
1572
}
1573
}
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