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

randombit / botan / 22632786303

03 Mar 2026 04:33PM UTC coverage: 90.222% (-0.1%) from 90.317%
22632786303

Pull #5404

github

web-flow
Merge 29e610b86 into a78272b61
Pull Request #5404: Add EC scalars and points to FFI

103594 of 114821 relevant lines covered (90.22%)

11540883.54 hits per line

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

92.78
/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));
4✔
248
   });
4✔
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(key == nullptr || bits == nullptr) {
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(key == nullptr || bits == nullptr) {
×
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((rng_obj == nullptr) || (key == nullptr)) {
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(key == nullptr || curve_name == nullptr) {
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(key == nullptr || sec1 == nullptr || curve_name == nullptr) {
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(key == nullptr || curve_name == nullptr) {
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(key == nullptr || rng_obj == nullptr) {
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(key_obj == nullptr || param_str == nullptr) {
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(key == nullptr || curve_name == nullptr) {
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(key == nullptr || sec1 == nullptr || curve_name == nullptr) {
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(key == nullptr || curve_name == nullptr) {
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(out == nullptr || out_len == nullptr || ident == nullptr || hash_algo == nullptr || key == nullptr) {
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(key == nullptr || curve_name == nullptr) {
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(key == nullptr || sec1 == nullptr || curve_name == nullptr) {
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(key == nullptr || curve_name == nullptr) {
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) {
×
842
   if(Botan::any_null_pointers(value)) {
×
843
      return BOTAN_FFI_ERROR_NULL_POINTER;
844
   }
845
   return ffi_guard_thunk(__func__, [=]() -> int {
×
846
      const Botan::EC_PrivateKey* ec_key = dynamic_cast<const Botan::EC_PrivateKey*>(&safe_get(key));
×
847
      if(ec_key == nullptr) {
×
848
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
849
      }
850
      return ffi_new_object(value, std::make_unique<Botan::EC_Scalar>(ec_key->_private_key()));
×
851
   });
×
852
}
853

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

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

868
int botan_ec_pubkey_get_group(botan_pubkey_t key, botan_ec_group_t* ec_group) {
×
869
   if(Botan::any_null_pointers(ec_group)) {
×
870
      return BOTAN_FFI_ERROR_NULL_POINTER;
871
   }
872
   return ffi_guard_thunk(__func__, [=]() -> int {
×
873
      const Botan::EC_PublicKey* ec_key = dynamic_cast<const Botan::EC_PublicKey*>(&safe_get(key));
×
874
      if(ec_key == nullptr) {
×
875
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
876
      }
877
      return ffi_new_object(ec_group, std::make_unique<Botan::EC_Group>(ec_key->domain()));
×
878
   });
×
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 defined(BOTAN_HAS_ED25519)
919
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
4✔
920
      if(auto ed = dynamic_cast<const Botan::Ed25519_PrivateKey*>(&k)) {
921
         const auto ed_key = ed->raw_private_key_bits();
922
         if(ed_key.size() != 64) {
923
            return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE;
924
         }
925
         Botan::copy_mem(output, ed_key.data(), ed_key.size());
926
         return BOTAN_FFI_SUCCESS;
927
      } else {
928
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
929
      }
930
   });
931
#else
932
   BOTAN_UNUSED(key, output);
933
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
934
#endif
935
}
936

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

957
/* Ed448 specific operations */
958

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

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

991
int botan_privkey_ed448_get_privkey(botan_privkey_t key, uint8_t output[57]) {
1✔
992
#if defined(BOTAN_HAS_ED448)
993
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
4✔
994
      if(auto ed = dynamic_cast<const Botan::Ed448_PrivateKey*>(&k)) {
995
         const auto ed_key = ed->raw_private_key_bits();
996
         Botan::copy_mem(std::span(output, 57), ed_key);
997
         return BOTAN_FFI_SUCCESS;
998
      } else {
999
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1000
      }
1001
   });
1002
#else
1003
   BOTAN_UNUSED(key, output);
1004
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1005
#endif
1006
}
1007

1008
int botan_pubkey_ed448_get_pubkey(botan_pubkey_t key, uint8_t output[57]) {
1✔
1009
#if defined(BOTAN_HAS_ED448)
1010
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
3✔
1011
      if(auto ed = dynamic_cast<const Botan::Ed448_PublicKey*>(&k)) {
1012
         const auto ed_key = ed->public_key_bits();
1013
         Botan::copy_mem(std::span(output, 57), ed_key);
1014
         return BOTAN_FFI_SUCCESS;
1015
      } else {
1016
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1017
      }
1018
   });
1019
#else
1020
   BOTAN_UNUSED(key, output);
1021
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1022
#endif
1023
}
1024

1025
/* X25519 specific operations */
1026

1027
int botan_privkey_load_x25519(botan_privkey_t* key, const uint8_t privkey[32]) {
1✔
1028
#if defined(BOTAN_HAS_X25519)
1029
   if(key == nullptr) {
1✔
1030
      return BOTAN_FFI_ERROR_NULL_POINTER;
1031
   }
1032
   *key = nullptr;
1✔
1033
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
1034
      auto x25519 = std::make_unique<Botan::X25519_PrivateKey>(std::span{privkey, 32});
1✔
1035
      return ffi_new_object(key, std::move(x25519));
2✔
1036
   });
2✔
1037
#else
1038
   BOTAN_UNUSED(key, privkey);
1039
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1040
#endif
1041
}
1042

1043
int botan_pubkey_load_x25519(botan_pubkey_t* key, const uint8_t pubkey[32]) {
1✔
1044
#if defined(BOTAN_HAS_X25519)
1045
   if(key == nullptr) {
1✔
1046
      return BOTAN_FFI_ERROR_NULL_POINTER;
1047
   }
1048
   *key = nullptr;
1✔
1049
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
1050
      auto x25519 = std::make_unique<Botan::X25519_PublicKey>(std::span{pubkey, 32});
1✔
1051
      return ffi_new_object(key, std::move(x25519));
1✔
1052
   });
2✔
1053
#else
1054
   BOTAN_UNUSED(key, pubkey);
1055
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1056
#endif
1057
}
1058

1059
int botan_privkey_x25519_get_privkey(botan_privkey_t key, uint8_t output[32]) {
1✔
1060
#if defined(BOTAN_HAS_X25519)
1061
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
4✔
1062
      if(auto x25519 = dynamic_cast<const Botan::X25519_PrivateKey*>(&k)) {
1063
         const auto x25519_key = x25519->raw_private_key_bits();
1064
         if(x25519_key.size() != 32) {
1065
            return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE;
1066
         }
1067
         Botan::copy_mem(output, x25519_key.data(), x25519_key.size());
1068
         return BOTAN_FFI_SUCCESS;
1069
      } else {
1070
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1071
      }
1072
   });
1073
#else
1074
   BOTAN_UNUSED(key, output);
1075
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1076
#endif
1077
}
1078

1079
int botan_pubkey_x25519_get_pubkey(botan_pubkey_t key, uint8_t output[32]) {
2✔
1080
#if defined(BOTAN_HAS_X25519)
1081
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
6✔
1082
      if(auto x25519 = dynamic_cast<const Botan::X25519_PublicKey*>(&k)) {
1083
         Botan::copy_mem(std::span{output, 32}, x25519->raw_public_key_bits());
1084
         return BOTAN_FFI_SUCCESS;
1085
      } else {
1086
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1087
      }
1088
   });
1089
#else
1090
   BOTAN_UNUSED(key, output);
1091
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1092
#endif
1093
}
1094

1095
/* X448 specific operations */
1096

1097
int botan_privkey_load_x448(botan_privkey_t* key, const uint8_t privkey[56]) {
1✔
1098
#if defined(BOTAN_HAS_X448)
1099
   if(key == nullptr) {
1✔
1100
      return BOTAN_FFI_ERROR_NULL_POINTER;
1101
   }
1102
   *key = nullptr;
1✔
1103
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
1104
      auto x448 = std::make_unique<Botan::X448_PrivateKey>(std::span{privkey, 56});
1✔
1105
      return ffi_new_object(key, std::move(x448));
2✔
1106
   });
2✔
1107
#else
1108
   BOTAN_UNUSED(key, privkey);
1109
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1110
#endif
1111
}
1112

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

1129
int botan_privkey_x448_get_privkey(botan_privkey_t key, uint8_t output[56]) {
1✔
1130
#if defined(BOTAN_HAS_X448)
1131
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
4✔
1132
      if(auto x448 = dynamic_cast<const Botan::X448_PrivateKey*>(&k)) {
1133
         const auto x448_key = x448->raw_private_key_bits();
1134
         Botan::copy_mem(std::span{output, 56}, x448_key);
1135
         return BOTAN_FFI_SUCCESS;
1136
      } else {
1137
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1138
      }
1139
   });
1140
#else
1141
   BOTAN_UNUSED(key, output);
1142
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1143
#endif
1144
}
1145

1146
int botan_pubkey_x448_get_pubkey(botan_pubkey_t key, uint8_t output[56]) {
2✔
1147
#if defined(BOTAN_HAS_X448)
1148
   return BOTAN_FFI_VISIT(key, [=](const auto& k) {
6✔
1149
      if(auto x448 = dynamic_cast<const Botan::X448_PublicKey*>(&k)) {
1150
         Botan::copy_mem(std::span{output, 56}, x448->raw_public_key_bits());
1151
         return BOTAN_FFI_SUCCESS;
1152
      } else {
1153
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1154
      }
1155
   });
1156
#else
1157
   BOTAN_UNUSED(key, output);
1158
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1159
#endif
1160
}
1161

1162
/*
1163
* Algorithm specific key operations: Kyber
1164
*/
1165

1166
int botan_privkey_load_kyber(botan_privkey_t* key, const uint8_t privkey[], size_t key_len) {
4✔
1167
#if defined(BOTAN_HAS_KYBER)
1168
   if(key == nullptr) {
4✔
1169
      return BOTAN_FFI_ERROR_NULL_POINTER;
1170
   }
1171
   *key = nullptr;
4✔
1172

1173
   const auto mode = [](size_t len) -> std::optional<Botan::KyberMode> {
12✔
1174
      if(len == 1632) {
4✔
1175
         return Botan::KyberMode::Kyber512_R3;
1✔
1176
      } else if(len == 2400) {
3✔
1177
         return Botan::KyberMode::Kyber768_R3;
2✔
1178
      } else if(len == 3168) {
1✔
1179
         return Botan::KyberMode::Kyber1024_R3;
1✔
1180
      } else {
1181
         return {};
×
1182
      }
1183
   }(key_len);
4✔
1184

1185
   if(mode.has_value()) {
4✔
1186
      return ffi_guard_thunk(__func__, [=]() -> int {
4✔
1187
         auto kyber = std::make_unique<Botan::Kyber_PrivateKey>(std::span{privkey, key_len}, *mode);
4✔
1188
         return ffi_new_object(key, std::move(kyber));
8✔
1189
      });
4✔
1190
   } else {
1191
      return BOTAN_FFI_ERROR_BAD_PARAMETER;
1192
   }
1193
#else
1194
   BOTAN_UNUSED(key, key_len, privkey);
1195
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1196
#endif
1197
}
1198

1199
int botan_pubkey_load_kyber(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len) {
4✔
1200
#if defined(BOTAN_HAS_KYBER)
1201
   if(key == nullptr) {
4✔
1202
      return BOTAN_FFI_ERROR_NULL_POINTER;
1203
   }
1204
   *key = nullptr;
4✔
1205

1206
   const auto mode = [](size_t len) -> std::optional<Botan::KyberMode> {
12✔
1207
      if(len == 800) {
4✔
1208
         return Botan::KyberMode::Kyber512_R3;
1✔
1209
      } else if(len == 1184) {
3✔
1210
         return Botan::KyberMode::Kyber768_R3;
2✔
1211
      } else if(len == 1568) {
1✔
1212
         return Botan::KyberMode::Kyber1024_R3;
1✔
1213
      } else {
1214
         return {};
×
1215
      }
1216
   }(key_len);
4✔
1217

1218
   if(mode.has_value()) {
4✔
1219
      auto kyber = std::make_unique<Botan::Kyber_PublicKey>(std::span{pubkey, key_len}, *mode);
4✔
1220
      return ffi_new_object(key, std::move(kyber));
4✔
1221
   } else {
4✔
1222
      return BOTAN_FFI_ERROR_BAD_PARAMETER;
1223
   }
1224
#else
1225
   BOTAN_UNUSED(key, pubkey, key_len);
1226
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1227
#endif
1228
}
1229

1230
int botan_privkey_view_kyber_raw_key(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) {
4✔
1231
#if defined(BOTAN_HAS_KYBER)
1232
   return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
16✔
1233
      if(auto kyber = dynamic_cast<const Botan::Kyber_PrivateKey*>(&k)) {
1234
         return invoke_view_callback(view, ctx, kyber->raw_private_key_bits());
1235
      } else {
1236
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1237
      }
1238
   });
1239
#else
1240
   BOTAN_UNUSED(key, ctx, view);
1241
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1242
#endif
1243
}
1244

1245
int botan_pubkey_view_kyber_raw_key(botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) {
8✔
1246
#if defined(BOTAN_HAS_KYBER)
1247
   return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
32✔
1248
      if(auto kyber = dynamic_cast<const Botan::Kyber_PublicKey*>(&k)) {
1249
         return invoke_view_callback(view, ctx, kyber->public_key_bits());
1250
      } else {
1251
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1252
      }
1253
   });
1254
#else
1255
   BOTAN_UNUSED(key, ctx, view);
1256
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1257
#endif
1258
}
1259

1260
/*
1261
* Algorithm specific key operations: ML-KEM
1262
*/
1263

1264
int botan_privkey_load_ml_kem(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* mlkem_mode) {
4✔
1265
#if defined(BOTAN_HAS_ML_KEM)
1266
   if(key == nullptr || privkey == nullptr || mlkem_mode == nullptr) {
4✔
1267
      return BOTAN_FFI_ERROR_NULL_POINTER;
1268
   }
1269

1270
   *key = nullptr;
4✔
1271

1272
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
1273
      auto mode = Botan::ML_KEM_Mode(mlkem_mode);
4✔
1274
      if(!mode.is_ml_kem()) {
4✔
1275
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1276
      }
1277

1278
      auto mlkem_key = std::make_unique<Botan::ML_KEM_PrivateKey>(std::span{privkey, key_len}, mode);
4✔
1279
      return ffi_new_object(key, std::move(mlkem_key));
4✔
1280
   });
8✔
1281
#else
1282
   BOTAN_UNUSED(key, key_len, privkey, mlkem_mode);
1283
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1284
#endif
1285
}
1286

1287
int botan_pubkey_load_ml_kem(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* mlkem_mode) {
4✔
1288
#if defined(BOTAN_HAS_ML_KEM)
1289
   if(key == nullptr || pubkey == nullptr || mlkem_mode == nullptr) {
4✔
1290
      return BOTAN_FFI_ERROR_NULL_POINTER;
1291
   }
1292

1293
   *key = nullptr;
4✔
1294

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

1301
      auto mlkem_key = std::make_unique<Botan::ML_KEM_PublicKey>(std::span{pubkey, key_len}, mode.mode());
4✔
1302
      return ffi_new_object(key, std::move(mlkem_key));
4✔
1303
   });
8✔
1304
#else
1305
   BOTAN_UNUSED(key, key_len, pubkey, mlkem_mode);
1306
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1307
#endif
1308
}
1309

1310
/*
1311
* Algorithm specific key operations: ML-DSA
1312
*/
1313

1314
int botan_privkey_load_ml_dsa(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* mldsa_mode) {
4✔
1315
#if defined(BOTAN_HAS_ML_DSA)
1316
   if(key == nullptr || privkey == nullptr || mldsa_mode == nullptr) {
4✔
1317
      return BOTAN_FFI_ERROR_NULL_POINTER;
1318
   }
1319

1320
   *key = nullptr;
4✔
1321

1322
   return ffi_guard_thunk(__func__, [=]() -> int {
4✔
1323
      auto mode = Botan::ML_DSA_Mode(mldsa_mode);
4✔
1324
      if(!mode.is_ml_dsa()) {
4✔
1325
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1326
      }
1327

1328
      auto mldsa_key = std::make_unique<Botan::ML_DSA_PrivateKey>(std::span{privkey, key_len}, mode);
4✔
1329
      return ffi_new_object(key, std::move(mldsa_key));
4✔
1330
   });
8✔
1331
#else
1332
   BOTAN_UNUSED(key, key_len, privkey, mldsa_mode);
1333
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1334
#endif
1335
}
1336

1337
int botan_pubkey_load_ml_dsa(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* mldsa_mode) {
4✔
1338
#if defined(BOTAN_HAS_ML_DSA)
1339
   if(key == nullptr || pubkey == nullptr || mldsa_mode == nullptr) {
4✔
1340
      return BOTAN_FFI_ERROR_NULL_POINTER;
1341
   }
1342

1343
   *key = nullptr;
4✔
1344

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

1351
      auto mldsa_key = std::make_unique<Botan::ML_DSA_PublicKey>(std::span{pubkey, key_len}, mode);
4✔
1352
      return ffi_new_object(key, std::move(mldsa_key));
4✔
1353
   });
8✔
1354
#else
1355
   BOTAN_UNUSED(key, key_len, pubkey, mldsa_mode);
1356
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1357
#endif
1358
}
1359

1360
/*
1361
* Algorithm specific key operations: SLH-DSA
1362
*/
1363

1364
int botan_privkey_load_slh_dsa(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* slhdsa_mode) {
13✔
1365
#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
1366
   if(key == nullptr || privkey == nullptr || slhdsa_mode == nullptr) {
13✔
1367
      return BOTAN_FFI_ERROR_NULL_POINTER;
1368
   }
1369

1370
   *key = nullptr;
13✔
1371

1372
   return ffi_guard_thunk(__func__, [=]() -> int {
13✔
1373
      auto mode = Botan::SLH_DSA_Parameters::create(slhdsa_mode);
13✔
1374
      if(!mode.is_slh_dsa()) {
13✔
1375
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1376
      }
1377

1378
      auto slhdsa_key = std::make_unique<Botan::SLH_DSA_PrivateKey>(std::span{privkey, key_len}, mode);
13✔
1379
      return ffi_new_object(key, std::move(slhdsa_key));
13✔
1380
   });
26✔
1381
#else
1382
   BOTAN_UNUSED(key, key_len, privkey, slhdsa_mode);
1383
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1384
#endif
1385
}
1386

1387
int botan_pubkey_load_slh_dsa(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* slhdsa_mode) {
13✔
1388
#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
1389
   if(key == nullptr || pubkey == nullptr || slhdsa_mode == nullptr) {
13✔
1390
      return BOTAN_FFI_ERROR_NULL_POINTER;
1391
   }
1392

1393
   *key = nullptr;
13✔
1394

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

1401
      auto mldsa_key = std::make_unique<Botan::SLH_DSA_PublicKey>(std::span{pubkey, key_len}, mode);
13✔
1402
      return ffi_new_object(key, std::move(mldsa_key));
13✔
1403
   });
26✔
1404
#else
1405
   BOTAN_UNUSED(key, key_len, pubkey, slhdsa_mode);
1406
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1407
#endif
1408
}
1409

1410
/*
1411
* Algorithm specific key operations: FrodoKEM
1412
*/
1413

1414
int botan_privkey_load_frodokem(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* frodo_mode) {
13✔
1415
#if defined(BOTAN_HAS_FRODOKEM)
1416
   if(key == nullptr || privkey == nullptr || frodo_mode == nullptr) {
13✔
1417
      return BOTAN_FFI_ERROR_NULL_POINTER;
1418
   }
1419

1420
   *key = nullptr;
13✔
1421

1422
   return ffi_guard_thunk(__func__, [=]() -> int {
13✔
1423
      const auto mode = Botan::FrodoKEMMode(frodo_mode);
13✔
1424
      auto frodo_key = std::make_unique<Botan::FrodoKEM_PrivateKey>(std::span{privkey, key_len}, mode);
13✔
1425
      return ffi_new_object(key, std::move(frodo_key));
26✔
1426
   });
26✔
1427
#else
1428
   BOTAN_UNUSED(key, privkey, key_len, frodo_mode);
1429
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1430
#endif
1431
}
1432

1433
int botan_pubkey_load_frodokem(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* frodo_mode) {
13✔
1434
#if defined(BOTAN_HAS_FRODOKEM)
1435
   if(key == nullptr || pubkey == nullptr || frodo_mode == nullptr) {
13✔
1436
      return BOTAN_FFI_ERROR_NULL_POINTER;
1437
   }
1438

1439
   *key = nullptr;
13✔
1440

1441
   return ffi_guard_thunk(__func__, [=]() -> int {
13✔
1442
      const auto mode = Botan::FrodoKEMMode(frodo_mode);
13✔
1443
      auto frodo_key = std::make_unique<Botan::FrodoKEM_PublicKey>(std::span{pubkey, key_len}, mode);
13✔
1444
      return ffi_new_object(key, std::move(frodo_key));
13✔
1445
   });
26✔
1446
#else
1447
   BOTAN_UNUSED(key, pubkey, key_len, frodo_mode);
1448
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1449
#endif
1450
}
1451

1452
/*
1453
* Algorithm specific key operations : Classic McEliece
1454
*/
1455

1456
int botan_privkey_load_classic_mceliece(botan_privkey_t* key,
17✔
1457
                                        const uint8_t privkey[],
1458
                                        size_t key_len,
1459
                                        const char* cmce_mode) {
1460
#if defined(BOTAN_HAS_CLASSICMCELIECE)
1461
   if(key == nullptr || privkey == nullptr || cmce_mode == nullptr) {
17✔
1462
      return BOTAN_FFI_ERROR_NULL_POINTER;
1463
   }
1464

1465
   *key = nullptr;
17✔
1466

1467
   return ffi_guard_thunk(__func__, [=]() -> int {
17✔
1468
      const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode);
17✔
1469
      auto cmce_key = std::make_unique<Botan::Classic_McEliece_PrivateKey>(std::span{privkey, key_len}, mode);
17✔
1470
      return ffi_new_object(key, std::move(cmce_key));
34✔
1471
   });
34✔
1472
#else
1473
   BOTAN_UNUSED(key, privkey, key_len, cmce_mode);
1474
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1475
#endif
1476
}
1477

1478
int botan_pubkey_load_classic_mceliece(botan_pubkey_t* key,
17✔
1479
                                       const uint8_t pubkey[],
1480
                                       size_t key_len,
1481
                                       const char* cmce_mode) {
1482
#if defined(BOTAN_HAS_CLASSICMCELIECE)
1483
   if(key == nullptr || pubkey == nullptr || cmce_mode == nullptr) {
17✔
1484
      return BOTAN_FFI_ERROR_NULL_POINTER;
1485
   }
1486

1487
   *key = nullptr;
17✔
1488

1489
   return ffi_guard_thunk(__func__, [=]() -> int {
17✔
1490
      const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode);
17✔
1491
      auto cmce_key = std::make_unique<Botan::Classic_McEliece_PublicKey>(std::span{pubkey, key_len}, mode);
17✔
1492
      return ffi_new_object(key, std::move(cmce_key));
17✔
1493
   });
34✔
1494
#else
1495
   BOTAN_UNUSED(key, pubkey, key_len, cmce_mode);
1496
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1497
#endif
1498
}
1499

1500
int botan_pubkey_view_ec_public_point(const botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) {
6✔
1501
#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
1502
   return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
24✔
1503
      if(auto ecc = dynamic_cast<const Botan::EC_PublicKey*>(&k)) {
1504
         auto pt = ecc->_public_ec_point().serialize_uncompressed();
1505
         return invoke_view_callback(view, ctx, pt);
1506
      } else {
1507
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
1508
      }
1509
   });
1510
#else
1511
   BOTAN_UNUSED(key, view, ctx);
1512
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
1513
#endif
1514
}
1515

1516
// NOLINTEND(misc-misplaced-const)
1517

1518
int botan_privkey_create_mceliece(botan_privkey_t* key_obj, botan_rng_t rng_obj, size_t n, size_t t) {
1✔
1519
   const std::string mce_params = std::to_string(n) + "," + std::to_string(t);
3✔
1520
   return botan_privkey_create(key_obj, "McEliece", mce_params.c_str(), rng_obj);
1✔
1521
}
1✔
1522

1523
int botan_mceies_decrypt(botan_privkey_t mce_key_obj,
×
1524
                         const char* aead,
1525
                         const uint8_t ct[],
1526
                         size_t ct_len,
1527
                         const uint8_t ad[],
1528
                         size_t ad_len,
1529
                         uint8_t out[],
1530
                         size_t* out_len) {
1531
   BOTAN_UNUSED(mce_key_obj, aead, ct, ct_len, ad, ad_len, out, out_len);
×
1532
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
×
1533
}
1534

1535
int botan_mceies_encrypt(botan_pubkey_t mce_key_obj,
×
1536
                         botan_rng_t rng_obj,
1537
                         const char* aead,
1538
                         const uint8_t pt[],
1539
                         size_t pt_len,
1540
                         const uint8_t ad[],
1541
                         size_t ad_len,
1542
                         uint8_t out[],
1543
                         size_t* out_len) {
1544
   BOTAN_UNUSED(mce_key_obj, rng_obj, aead, pt, pt_len, ad, ad_len, out, out_len);
×
1545
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
×
1546
}
1547
}
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