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

randombit / botan / 20824387176

08 Jan 2026 04:42PM UTC coverage: 90.443% (+0.02%) from 90.423%
20824387176

Pull #5217

github

web-flow
Merge 4510d2bda into b1ff92a4e
Pull Request #5217: FFI: Query Certificate Subject/Issuer Alternative Names and Name Constraints

101933 of 112704 relevant lines covered (90.44%)

12903206.61 hits per line

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

98.5
/src/tests/test_ffi.cpp
1
/*
2
* (C) 2015 Jack Lloyd
3
* (C) 2016 René Korthaus
4
* (C) 2018 Ribose Inc, Krzysztof Kwiatkowski
5
* (C) 2018 Ribose Inc
6
*
7
* Botan is released under the Simplified BSD License (see license.txt)
8
*/
9

10
#define BOTAN_NO_DEPRECATED_WARNINGS
11

12
#include "tests.h"
13
#include <botan/version.h>
14

15
#if defined(BOTAN_HAS_FFI)
16
   #include <botan/ber_dec.h>
17
   #include <botan/ec_group.h>
18
   #include <botan/ffi.h>
19
   #include <botan/hex.h>
20
   #include <botan/mem_ops.h>
21
   #include <botan/pkix_types.h>
22
   #include <botan/internal/fmt.h>
23
   #include <botan/internal/loadstor.h>
24
   #include <botan/internal/stl_util.h>
25
   #include <botan/internal/target_info.h>
26
   #include <set>
27
#endif
28

29
#if defined(BOTAN_HAS_TPM2)
30
   #include <tss2/tss2_esys.h>
31
   #include <tss2/tss2_tctildr.h>
32
#endif
33

34
namespace Botan_Tests {
35

36
namespace {
37

38
#if defined(BOTAN_HAS_FFI)
39

40
// NOLINTBEGIN(*-macro-usage)
41

42
   #define _TEST_FFI_STR_HELPER(x) #x
43

44
   #define _TEST_FFI_STR(x) _TEST_FFI_STR_HELPER(x)
45

46
   #define _TEST_FFI_SOURCE_LOCATION(func, file, line) (func " invoked at " file ":" _TEST_FFI_STR(line))
47

48
   #define TEST_FFI_OK(func, args) result.test_rc_ok(_TEST_FFI_SOURCE_LOCATION(#func, __FILE__, __LINE__), func args)
49

50
   #define TEST_FFI_INIT(func, args) \
51
      result.test_rc_init(_TEST_FFI_SOURCE_LOCATION(#func, __FILE__, __LINE__), func args)
52

53
   #define TEST_FFI_FAIL(msg, func, args) \
54
      result.test_rc_fail(_TEST_FFI_SOURCE_LOCATION(#func, __FILE__, __LINE__), msg, func args)
55

56
   #define TEST_FFI_RC(rc, func, args) \
57
      result.test_rc(_TEST_FFI_SOURCE_LOCATION(#func, __FILE__, __LINE__), rc, func args)
58

59
   #define REQUIRE_FFI_OK(func, args)                           \
60
      if(!TEST_FFI_OK(func, args)) {                            \
61
         result.test_note("Exiting test early due to failure"); \
62
         return;                                                \
63
      }
64

65
// NOLINTEND(*-macro-usage)
66

67
/**
68
 * Helper class for testing "view"-style API functions that take a callback
69
 * that gets passed a variable-length buffer of bytes.
70
 *
71
 * Example:
72
 *   botan_privkey_t priv;
73
 *   ViewBytesSink sink;
74
 *   botan_privkey_view_raw(priv, sink.delegate(), sink.callback());
75
 *   std::cout << hex_encode(sink.get()) << std::endl;
76
 */
77
class ViewBytesSink final {
×
78
   public:
79
      void* delegate() { return this; }
80

81
      botan_view_bin_fn callback() { return &write_fn; }
242✔
82

83
      std::span<const uint8_t> get() const { return m_buf; }
15✔
84

85
      const uint8_t* data() const { return m_buf.data(); }
4✔
86

87
      size_t size() const { return m_buf.size(); }
4✔
88

89
   private:
90
      static int write_fn(void* ctx, const uint8_t buf[], size_t len) {
240✔
91
         if(ctx == nullptr || buf == nullptr) {
240✔
92
            return BOTAN_FFI_ERROR_NULL_POINTER;
93
         }
94

95
         auto* sink = static_cast<ViewBytesSink*>(ctx);
240✔
96
         sink->m_buf.assign(buf, buf + len);
240✔
97

98
         return BOTAN_FFI_SUCCESS;
240✔
99
      }
100

101
   private:
102
      std::vector<uint8_t> m_buf;
103
};
104

105
/**
106
 * See ViewBytesSink for how to use this. Works for `botan_view_str_fn` instead.
107
*/
108
class ViewStringSink final {
28✔
109
   public:
110
      void* delegate() { return this; }
111

112
      botan_view_str_fn callback() { return &write_fn; }
71✔
113

114
      const std::string& get() { return m_str; }
67✔
115

116
   private:
117
      static int write_fn(void* ctx, const char* str, size_t len) {
70✔
118
         if(ctx == nullptr || str == nullptr) {
70✔
119
            return BOTAN_FFI_ERROR_NULL_POINTER;
120
         }
121

122
         auto* sink = static_cast<ViewStringSink*>(ctx);
70✔
123
         // discard the null terminator
124
         sink->m_str = std::string(str, len - 1);
70✔
125

126
         return BOTAN_FFI_SUCCESS;
70✔
127
      }
128

129
   private:
130
      std::string m_str;
131
};
132

133
// NOLINTBEGIN(*-init-variables)
134

135
class FFI_Test : public Test {
54✔
136
   public:
137
      std::vector<Test::Result> run() override {
54✔
138
         Test::Result result(this->name());
54✔
139

140
         if(!skip_this_test()) {
54✔
141
            botan_rng_t rng;
54✔
142
            if(botan_rng_init(&rng, "system") != 0) {
54✔
143
               result.test_failure("Failed to init RNG");
×
144
               return {result};
×
145
            }
146

147
            result.start_timer();
54✔
148
            ffi_test(result, rng);
54✔
149
            result.end_timer();
54✔
150

151
            botan_rng_destroy(rng);
54✔
152
         } else {
153
            result.test_note("FFI test asked to be skipped");
×
154
         }
155

156
         return {result};
108✔
157
      }
108✔
158

159
   private:
160
      virtual std::string name() const = 0;
161
      virtual void ffi_test(Test::Result& result, botan_rng_t rng) = 0;
162

163
      virtual bool skip_this_test() const { return false; }
52✔
164
};
165

166
void ffi_test_pubkey_export(Test::Result& result, botan_pubkey_t pub, botan_privkey_t priv, botan_rng_t rng) {
12✔
167
   // export public key
168
   size_t pubkey_len = 0;
12✔
169
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
12✔
170
               botan_pubkey_export,
171
               (pub, nullptr, &pubkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
172

173
   std::vector<uint8_t> pubkey(pubkey_len);
12✔
174
   TEST_FFI_OK(botan_pubkey_export, (pub, pubkey.data(), &pubkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
12✔
175

176
   pubkey_len = 0;
12✔
177
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
12✔
178
               botan_pubkey_export,
179
               (pub, nullptr, &pubkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
180

181
   pubkey.resize(pubkey_len);
12✔
182
   TEST_FFI_OK(botan_pubkey_export, (pub, pubkey.data(), &pubkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
12✔
183

184
   // reimport exported public key
185
   botan_pubkey_t pub_copy;
12✔
186
   TEST_FFI_OK(botan_pubkey_load, (&pub_copy, pubkey.data(), pubkey_len));
12✔
187
   TEST_FFI_OK(botan_pubkey_check_key, (pub_copy, rng, 0));
12✔
188
   TEST_FFI_OK(botan_pubkey_destroy, (pub_copy));
12✔
189

190
   // export private key
191
   std::vector<uint8_t> privkey;
12✔
192
   size_t privkey_len = 0;
12✔
193

194
   // call with nullptr to query the length
195
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
12✔
196
               botan_privkey_export,
197
               (priv, nullptr, &privkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
198

199
   privkey.resize(privkey_len);
12✔
200
   privkey_len = privkey.size();  // set buffer size
12✔
201

202
   TEST_FFI_OK(botan_privkey_export, (priv, privkey.data(), &privkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
12✔
203

204
   privkey.resize(privkey_len);
12✔
205

206
   result.test_gte("Reasonable size", privkey.size(), 32);
12✔
207

208
   // reimport exported private key
209
   botan_privkey_t copy;
12✔
210
   TEST_FFI_OK(botan_privkey_load, (&copy, rng, privkey.data(), privkey.size(), nullptr));
12✔
211
   botan_privkey_destroy(copy);
12✔
212

213
   // Now again for PEM
214
   privkey_len = 0;
12✔
215

216
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
12✔
217
               botan_privkey_export,
218
               (priv, nullptr, &privkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
219

220
   privkey.resize(privkey_len);
12✔
221
   TEST_FFI_OK(botan_privkey_export, (priv, privkey.data(), &privkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
12✔
222

223
   TEST_FFI_OK(botan_privkey_load, (&copy, rng, privkey.data(), privkey.size(), nullptr));
12✔
224
   botan_privkey_destroy(copy);
12✔
225

226
   #if defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_PKCS5_PBES2)
227
   const size_t pbkdf_iter = 1000;
12✔
228

229
   // export private key encrypted
230
   privkey_len = 0;
12✔
231
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
12✔
232
               botan_privkey_export_encrypted_pbkdf_iter,
233
               (priv, nullptr, &privkey_len, rng, "password", pbkdf_iter, "", "", BOTAN_PRIVKEY_EXPORT_FLAG_DER));
234

235
   privkey.resize(privkey_len);
12✔
236
   privkey_len = privkey.size();
12✔
237

238
   TEST_FFI_OK(
12✔
239
      botan_privkey_export_encrypted_pbkdf_iter,
240
      (priv, privkey.data(), &privkey_len, rng, "password", pbkdf_iter, "", "", BOTAN_PRIVKEY_EXPORT_FLAG_DER));
241

242
   // reimport encrypted private key
243
   botan_privkey_load(&copy, rng, privkey.data(), privkey.size(), "password");
12✔
244
   botan_privkey_destroy(copy);
12✔
245

246
   privkey_len = 0;
12✔
247
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
12✔
248
               botan_privkey_export_encrypted_pbkdf_iter,
249
               (priv, nullptr, &privkey_len, rng, "password", pbkdf_iter, "", "", BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
250

251
   privkey.resize(privkey_len);
12✔
252
   TEST_FFI_OK(
12✔
253
      botan_privkey_export_encrypted_pbkdf_iter,
254
      (priv, privkey.data(), &privkey_len, rng, "password", pbkdf_iter, "", "", BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
255

256
   privkey.resize(privkey_len * 2);
12✔
257
   privkey_len = privkey.size();
12✔
258
   const uint32_t pbkdf_msec = 100;
12✔
259
   size_t pbkdf_iters_out = 0;
12✔
260

261
      #if defined(BOTAN_HAS_SCRYPT)
262
   const std::string pbe_hash = "Scrypt";
12✔
263
      #else
264
   const std::string pbe_hash = "SHA-512";
265
      #endif
266

267
      #if defined(BOTAN_HAS_AEAD_GCM)
268
   const std::string pbe_cipher = "AES-256/GCM";
12✔
269
      #else
270
   const std::string pbe_cipher = "AES-256/CBC";
271
      #endif
272

273
   TEST_FFI_OK(botan_privkey_export_encrypted_pbkdf_msec,
12✔
274
               (priv,
275
                privkey.data(),
276
                &privkey_len,
277
                rng,
278
                "password",
279
                pbkdf_msec,
280
                &pbkdf_iters_out,
281
                pbe_cipher.c_str(),
282
                pbe_hash.c_str(),
283
                0));
284

285
   if(pbe_hash == "Scrypt") {
12✔
286
      result.test_eq("Scrypt iters set to zero in this API", pbkdf_iters_out, 0);
24✔
287
   } else {
288
      // PBKDF2 currently always rounds to multiple of 2000
289
      result.test_eq("Expected PBKDF2 iters", pbkdf_iters_out % 2000, 0);
×
290
   }
291

292
   privkey.resize(privkey_len);
12✔
293

294
   TEST_FFI_OK(botan_privkey_load, (&copy, rng, privkey.data(), privkey.size(), "password"));
12✔
295
   botan_privkey_destroy(copy);
12✔
296
   #endif
297

298
   // calculate fingerprint
299
   size_t strength = 0;
12✔
300
   TEST_FFI_OK(botan_pubkey_estimated_strength, (pub, &strength));
12✔
301
   result.test_gte("estimated strength", strength, 1);
12✔
302

303
   size_t fingerprint_len = 0;
12✔
304
   TEST_FFI_RC(
12✔
305
      BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_pubkey_fingerprint, (pub, "SHA-256", nullptr, &fingerprint_len));
306

307
   std::vector<uint8_t> fingerprint(fingerprint_len);
12✔
308
   TEST_FFI_OK(botan_pubkey_fingerprint, (pub, "SHA-256", fingerprint.data(), &fingerprint_len));
24✔
309
}
36✔
310

311
class FFI_Utils_Test final : public FFI_Test {
1✔
312
   public:
313
      std::string name() const override { return "FFI Utils"; }
1✔
314

315
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
316
         result.test_is_eq("FFI API version macro", uint32_t(BOTAN_FFI_API_VERSION), uint32_t(BOTAN_HAS_FFI));
1✔
317
         result.test_is_eq("FFI API version function", botan_ffi_api_version(), uint32_t(BOTAN_HAS_FFI));
1✔
318
         result.test_is_eq("Major version", botan_version_major(), Botan::version_major());
1✔
319
         result.test_is_eq("Minor version", botan_version_minor(), Botan::version_minor());
1✔
320
         result.test_is_eq("Patch version", botan_version_patch(), Botan::version_patch());
1✔
321
         result.test_is_eq("Botan version", botan_version_string(), Botan::version_cstr());
1✔
322
         result.test_is_eq("Botan version datestamp", botan_version_datestamp(), Botan::version_datestamp());
1✔
323
         result.test_is_eq("FFI supports its own version", botan_ffi_supports_api(botan_ffi_api_version()), 0);
1✔
324

325
         result.test_is_eq("FFI compile time time var matches botan_ffi_api_version",
1✔
326
                           botan_ffi_api_version(),
1✔
327
                           uint32_t(BOTAN_FFI_API_VERSION));
1✔
328

329
         result.test_is_eq("FFI supports 2.0 version", botan_ffi_supports_api(20150515), 0);
1✔
330
         result.test_is_eq("FFI supports 2.1 version", botan_ffi_supports_api(20170327), 0);
1✔
331
         result.test_is_eq("FFI supports 2.3 version", botan_ffi_supports_api(20170815), 0);
1✔
332
         result.test_is_eq("FFI supports 2.8 version", botan_ffi_supports_api(20180713), 0);
1✔
333

334
         result.test_is_eq("FFI doesn't support bogus version", botan_ffi_supports_api(20160229), -1);
1✔
335

336
         const std::vector<uint8_t> mem1 = {0xFF, 0xAA, 0xFF};
1✔
337
         const std::vector<uint8_t> mem2 = {0xFF, 0xA9, 0xFF};
1✔
338

339
         TEST_FFI_RC(0, botan_constant_time_compare, (mem1.data(), mem1.data(), mem1.size()));
1✔
340
         TEST_FFI_RC(-1, botan_constant_time_compare, (mem1.data(), mem2.data(), mem1.size()));
1✔
341

342
         std::vector<uint8_t> to_zero = {0xFF, 0xA0};
1✔
343
         TEST_FFI_OK(botan_scrub_mem, (to_zero.data(), to_zero.size()));
1✔
344
         result.confirm("scrub_memory zeros", to_zero[0] == 0 && to_zero[1] == 0);
2✔
345

346
         const std::vector<uint8_t> bin = {0xAA, 0xDE, 0x01};
1✔
347

348
         std::string outstr;
1✔
349
         outstr.resize(2 * bin.size());
1✔
350
         TEST_FFI_OK(botan_hex_encode, (bin.data(), bin.size(), outstr.data(), 0));
1✔
351
         result.test_eq("uppercase hex", outstr, "AADE01");
2✔
352

353
         TEST_FFI_OK(botan_hex_encode, (bin.data(), bin.size(), outstr.data(), BOTAN_FFI_HEX_LOWER_CASE));
1✔
354
         result.test_eq("lowercase hex", outstr, "aade01");
2✔
355
      }
4✔
356
};
357

358
class FFI_RNG_Test final : public FFI_Test {
1✔
359
   public:
360
      std::string name() const override { return "FFI RNG"; }
1✔
361

362
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
363
         // RNG test and initialization
364
         botan_rng_t rng;
1✔
365
         botan_rng_t system_rng;
1✔
366
         botan_rng_t hwrng_rng = nullptr;
1✔
367
         botan_rng_t null_rng;
1✔
368
         botan_rng_t custom_rng;
1✔
369
         botan_rng_t tpm2_rng = nullptr;
1✔
370

371
         botan_tpm2_ctx_t tpm2_ctx = nullptr;
1✔
372
         botan_tpm2_session_t tpm2_session = nullptr;
1✔
373

374
         TEST_FFI_FAIL("invalid rng type", botan_rng_init, (&rng, "invalid_type"));
2✔
375

376
         REQUIRE_FFI_OK(botan_rng_init, (&system_rng, "system"));
1✔
377
         REQUIRE_FFI_OK(botan_rng_init, (&null_rng, "null"));
1✔
378

379
         int rc = botan_rng_init(&hwrng_rng, "hwrng");
1✔
380
         result.confirm("Either success or not implemented", rc == 0 || rc == BOTAN_FFI_ERROR_NOT_IMPLEMENTED);
2✔
381

382
         std::vector<uint8_t> outbuf(512);
1✔
383

384
         rc = botan_rng_init(&rng, "user-threadsafe");
1✔
385
         result.confirm("Either success or not implemented", rc == 0 || rc == BOTAN_FFI_ERROR_NOT_IMPLEMENTED);
2✔
386

387
         if(rc != 0) {
1✔
388
            REQUIRE_FFI_OK(botan_rng_init, (&rng, "user"));
×
389
            REQUIRE_FFI_OK(botan_rng_destroy, (rng));
×
390
         }
391

392
         if(rc == 0) {
1✔
393
            TEST_FFI_OK(botan_rng_get, (rng, outbuf.data(), outbuf.size()));
1✔
394
            TEST_FFI_OK(botan_rng_reseed, (rng, 256));
1✔
395

396
            TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE, botan_rng_reseed_from_rng, (rng, null_rng, 256));
1✔
397
            if(hwrng_rng != nullptr) {
1✔
398
               TEST_FFI_OK(botan_rng_reseed_from_rng, (rng, hwrng_rng, 256));
2✔
399
            }
400
            TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE, botan_rng_get, (null_rng, outbuf.data(), outbuf.size()));
1✔
401

402
            TEST_FFI_OK(botan_rng_destroy, (rng));
2✔
403
         }
404

405
         if(TEST_FFI_OK(botan_rng_init, (&rng, "user"))) {
1✔
406
            TEST_FFI_OK(botan_rng_get, (rng, outbuf.data(), outbuf.size()));
1✔
407
            TEST_FFI_OK(botan_rng_reseed, (rng, 256));
1✔
408

409
            TEST_FFI_OK(botan_rng_reseed_from_rng, (rng, system_rng, 256));
1✔
410

411
            uint8_t not_really_entropy[32] = {0};
1✔
412
            TEST_FFI_OK(botan_rng_add_entropy, (rng, not_really_entropy, 32));
2✔
413
         }
414

415
         uint8_t system_rng_buf[4096];
1✔
416
         TEST_FFI_OK(botan_system_rng_get, (system_rng_buf, sizeof(system_rng_buf)));
1✔
417

418
         size_t cb_counter = 0;
1✔
419

420
         auto custom_get_cb = +[](void* context, uint8_t* out, size_t out_len) -> int {
1✔
421
            for(size_t i = 0; i != out_len; ++i) {
422
               out[i] = 0x12;
423
            }
424
            (*(static_cast<size_t*>(context)))++;
425
            return 0;
426
         };
427

428
         auto custom_add_entropy_cb = +[](void* context, const uint8_t input[], size_t length) -> int {
1✔
429
            BOTAN_UNUSED(input, length);
430
            (*(static_cast<size_t*>(context)))++;
431
            return 0;
432
         };
433

434
         auto custom_destroy_cb = +[](void* context) -> void { (*(static_cast<size_t*>(context)))++; };
1✔
435

436
         if(TEST_FFI_OK(
1✔
437
               botan_rng_init_custom,
438
               (&custom_rng, "custom rng", &cb_counter, custom_get_cb, custom_add_entropy_cb, custom_destroy_cb))) {
439
            Botan::clear_mem(outbuf.data(), outbuf.size());
1✔
440
            TEST_FFI_OK(botan_rng_get, (custom_rng, outbuf.data(), outbuf.size()));
1✔
441
            result.test_eq("custom_get_cb called", cb_counter, 1);
1✔
442
            std::vector<uint8_t> pattern(outbuf.size(), 0x12);
1✔
443
            result.test_eq("custom_get_cb returned bytes", pattern, outbuf);
2✔
444

445
            TEST_FFI_OK(botan_rng_reseed, (custom_rng, 256));
1✔
446
            result.test_eq("custom_add_entropy_cb called", cb_counter, 2);
1✔
447

448
            TEST_FFI_OK(botan_rng_reseed_from_rng, (custom_rng, system_rng, 256));
1✔
449
            result.test_eq("custom_add_entropy_cb called", cb_counter, 3);
1✔
450

451
            uint8_t not_really_entropy[32] = {0};
1✔
452
            TEST_FFI_OK(botan_rng_add_entropy, (custom_rng, not_really_entropy, 32));
1✔
453
            result.test_eq("custom_add_entropy_cb called", cb_counter, 4);
1✔
454

455
            TEST_FFI_OK(botan_rng_destroy, (custom_rng));
1✔
456
            result.test_eq("custom_destroy_cb called", cb_counter, 5);
2✔
457
         }
1✔
458

459
   #ifdef BOTAN_HAS_JITTER_RNG
460
         botan_rng_t jitter_rng;
1✔
461
         if(TEST_FFI_OK(botan_rng_init, (&jitter_rng, "jitter"))) {
1✔
462
            std::vector<uint8_t> buf(256);
1✔
463
            TEST_FFI_OK(botan_rng_get, (jitter_rng, outbuf.data(), buf.size()));
1✔
464
            TEST_FFI_OK(botan_rng_destroy, (jitter_rng));
2✔
465
         }
1✔
466
   #endif
467

468
         const auto tcti_name = Test::options().tpm2_tcti_name().value_or("");
1✔
469
         const auto tcti_conf = Test::options().tpm2_tcti_conf().value_or("");
1✔
470
         if(tcti_name.empty() || tcti_name == "disabled") {
1✔
471
            result.test_note("TPM2 tests are disabled.");
×
472
         } else {
473
            auto tpm2_test_rng = [&](botan_tpm2_ctx_t tpm2_context) {
3✔
474
               // Create and use an RNG without a TPM2 session
475
               // (communication between application and TPM won't be encrypted)
476
               if(TEST_FFI_INIT(botan_tpm2_rng_init, (&tpm2_rng, tpm2_context, nullptr, nullptr, nullptr))) {
2✔
477
                  Botan::clear_mem(outbuf.data(), outbuf.size());
2✔
478

479
                  TEST_FFI_OK(botan_rng_get, (tpm2_rng, outbuf.data(), outbuf.size()));
2✔
480
                  TEST_FFI_OK(botan_rng_reseed, (tpm2_rng, 256));
2✔
481

482
                  TEST_FFI_OK(botan_rng_reseed_from_rng, (tpm2_rng, system_rng, 256));
2✔
483

484
                  uint8_t not_really_entropy[32] = {0};
2✔
485
                  TEST_FFI_OK(botan_rng_add_entropy, (tpm2_rng, not_really_entropy, 32));
2✔
486
                  TEST_FFI_OK(botan_rng_destroy, (tpm2_rng));
4✔
487
               }
488

489
               // Create an anonymous TPM2 session
490
               if(TEST_FFI_INIT(botan_tpm2_unauthenticated_session_init, (&tpm2_session, tpm2_context))) {
2✔
491
                  // Create and use an RNG with an anonymous TPM2 session
492
                  // (communication between application and TPM will be encrypted)
493
                  if(TEST_FFI_INIT(botan_tpm2_rng_init, (&tpm2_rng, tpm2_context, tpm2_session, nullptr, nullptr))) {
2✔
494
                     Botan::clear_mem(outbuf.data(), outbuf.size());
2✔
495

496
                     TEST_FFI_OK(botan_rng_get, (tpm2_rng, outbuf.data(), outbuf.size()));
2✔
497
                     TEST_FFI_OK(botan_rng_reseed, (tpm2_rng, 256));
2✔
498

499
                     TEST_FFI_OK(botan_rng_reseed_from_rng, (tpm2_rng, system_rng, 256));
2✔
500

501
                     uint8_t not_really_entropy[32] = {0};
2✔
502
                     TEST_FFI_OK(botan_rng_add_entropy, (tpm2_rng, not_really_entropy, 32));
2✔
503
                     TEST_FFI_OK(botan_rng_destroy, (tpm2_rng));
4✔
504
                  }
505

506
                  TEST_FFI_OK(botan_tpm2_session_destroy, (tpm2_session));
4✔
507
               }
508
            };
2✔
509

510
            if(TEST_FFI_INIT(botan_tpm2_ctx_init_ex, (&tpm2_ctx, tcti_name.c_str(), tcti_conf.c_str()))) {
1✔
511
               if(botan_tpm2_supports_crypto_backend() == 1) {
1✔
512
                  TEST_FFI_OK(botan_tpm2_ctx_enable_crypto_backend, (tpm2_ctx, system_rng));
1✔
513
                  result.test_note("TPM2 crypto backend enabled");
2✔
514
               } else {
515
                  result.test_note("TPM2 crypto backend not supported");
×
516
               }
517

518
               tpm2_test_rng(tpm2_ctx);
1✔
519
               TEST_FFI_OK(botan_tpm2_ctx_destroy, (tpm2_ctx));
2✔
520
            }
521

522
   #if defined(BOTAN_HAS_TPM2)
523
            TSS2_TCTI_CONTEXT* tcti_ctx;
1✔
524
            ESYS_CONTEXT* esys_ctx;
1✔
525

526
            if(TEST_FFI_INIT(Tss2_TctiLdr_Initialize_Ex, (tcti_name.c_str(), tcti_conf.c_str(), &tcti_ctx))) {
1✔
527
               if(TEST_FFI_INIT(Esys_Initialize, (&esys_ctx, tcti_ctx, nullptr /* ABI version */))) {
1✔
528
                  botan_tpm2_crypto_backend_state_t cbs = nullptr;
1✔
529

530
                  // enable the botan-based TSS2 crypto backend on a bare ESYS_CONTEXT
531
                  if(botan_tpm2_supports_crypto_backend() == 1) {
1✔
532
                     TEST_FFI_OK(botan_tpm2_enable_crypto_backend, (&cbs, esys_ctx, system_rng));
1✔
533
                     result.test_note("TPM2 crypto backend enabled");
2✔
534
                  } else {
535
                     result.test_note("TPM2 crypto backend not supported");
×
536
                  }
537

538
                  // initialize the Botan TPM2 FFI wrapper from the bare ESYS_CONTEXT
539
                  if(TEST_FFI_INIT(botan_tpm2_ctx_from_esys, (&tpm2_ctx, esys_ctx))) {
1✔
540
                     tpm2_test_rng(tpm2_ctx);
1✔
541
                     TEST_FFI_OK(botan_tpm2_ctx_destroy, (tpm2_ctx));
2✔
542
                  }
543

544
                  if(cbs != nullptr) {
1✔
545
                     TEST_FFI_OK(botan_tpm2_crypto_backend_state_destroy, (cbs));
2✔
546
                  }
547

548
                  Esys_Finalize(&esys_ctx);
1✔
549
               }
550
               Tss2_TctiLdr_Finalize(&tcti_ctx);
1✔
551
            }
552
   #endif
553
         }
554

555
         TEST_FFI_OK(botan_rng_destroy, (rng));
1✔
556
         TEST_FFI_OK(botan_rng_destroy, (null_rng));
1✔
557
         TEST_FFI_OK(botan_rng_destroy, (system_rng));
1✔
558
         TEST_FFI_OK(botan_rng_destroy, (hwrng_rng));
2✔
559
      }
2✔
560
};
561

562
class FFI_RSA_Cert_Test final : public FFI_Test {
1✔
563
   public:
564
      std::string name() const override { return "FFI RSA cert"; }
1✔
565

566
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
567
         botan_x509_cert_t cert;
1✔
568
         if(TEST_FFI_INIT(botan_x509_cert_load_file, (&cert, Test::data_file("x509/ocsp/randombit.pem").c_str()))) {
1✔
569
            TEST_FFI_RC(0, botan_x509_cert_hostname_match, (cert, "randombit.net"));
1✔
570
            TEST_FFI_RC(0, botan_x509_cert_hostname_match, (cert, "www.randombit.net"));
1✔
571
            TEST_FFI_RC(-1, botan_x509_cert_hostname_match, (cert, "*.randombit.net"));
1✔
572
            TEST_FFI_RC(-1, botan_x509_cert_hostname_match, (cert, "flub.randombit.net"));
1✔
573
            TEST_FFI_RC(-1, botan_x509_cert_hostname_match, (cert, "randombit.net.com"));
1✔
574

575
            botan_x509_cert_t copy;
1✔
576
            TEST_FFI_OK(botan_x509_cert_dup, (&copy, cert));
1✔
577
            TEST_FFI_RC(0, botan_x509_cert_hostname_match, (copy, "randombit.net"));
1✔
578

579
            TEST_FFI_OK(botan_x509_cert_destroy, (copy));
1✔
580
            TEST_FFI_OK(botan_x509_cert_destroy, (cert));
2✔
581
         }
582
      }
1✔
583
};
584

585
class FFI_ZFEC_Test final : public FFI_Test {
1✔
586
   public:
587
      std::string name() const override { return "FFI ZFEC"; }
1✔
588

589
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
590
         /* exercise a simple success case
591
          */
592

593
         // Select some arbitrary, valid encoding parameters.  There is
594
         // nothing special about them but some relationships between these
595
         // values and other inputs must hold.
596
         const size_t K = 3;
1✔
597
         const size_t N = 11;
1✔
598

599
         // The decoder needs to know the indexes of the blocks being passed
600
         // in to it.  This array must equal [0..N) for the logic in the
601
         // decoding loop below to hold.
602
         const std::vector<size_t> indexes = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
1✔
603

604
         // This will be the size of each encoded (or decoded) block.  This is
605
         // an arbitrary value but it must match up with the length of the
606
         // test data given in `input`.
607
         const size_t blockSize = 15;
1✔
608

609
         // K of the blocks are required so the total information represented
610
         // can be this multiple.  totalSize must be a multiple of K and it
611
         // always will be using this construction.
612
         const size_t totalSize = blockSize * K;
1✔
613

614
         // Here's the complete original input (plus a trailing NUL that we
615
         // won't pass through ZFEC).  These are arbitrary bytes.
616
         const uint8_t input[totalSize + 1] = "Does this work?AAAAAAAAAAAAAAAzzzzzzzzzzzzzzz";
1✔
617

618
         // Allocate memory for the encoding and decoding output parameters.
619
         std::vector<uint8_t> encoded_buf(N * blockSize);
1✔
620
         std::vector<uint8_t> decoded_buf(K * blockSize);
1✔
621

622
         std::vector<uint8_t*> encoded(N);
1✔
623
         for(size_t i = 0; i < N; ++i) {
12✔
624
            encoded[i] = &encoded_buf[i * blockSize];
11✔
625
         }
626
         std::vector<uint8_t*> decoded(K);
1✔
627
         for(size_t i = 0; i < K; ++i) {
4✔
628
            decoded[i] = &decoded_buf[i * blockSize];
3✔
629
         }
630

631
         // First encode the complete input string into N blocks where K are
632
         // required for reconstruction.  The N encoded blocks will end up in
633
         // `encoded`.
634
         if(!TEST_FFI_INIT(botan_zfec_encode, (K, N, input, totalSize, encoded.data()))) {
1✔
635
            return;
636
         }
637

638
         // Any K blocks can be decoded to reproduce the original input (split
639
         // across an array of K strings of blockSize bytes each).  This loop
640
         // only exercises decoding with consecutive blocks because it's
641
         // harder to pick non-consecutive blocks out for a test.
642
         for(size_t offset = 0; offset < N - K; ++offset) {
9✔
643
            result.test_note("About to decode with offset " + std::to_string(offset));
24✔
644
            // Pass in the K shares starting from `offset` (and their indexes)
645
            // so that we can try decoding a certain group of blocks here.  Any
646
            // K shares *should* work.
647
            REQUIRE_FFI_OK(botan_zfec_decode,
8✔
648
                           (K, N, indexes.data() + offset, encoded.data() + offset, blockSize, decoded.data()));
649

650
            // Check that the original input bytes have been written to the
651
            // output parameter.
652
            for(size_t k = 0, pos = 0; k < K; ++k, pos += blockSize) {
32✔
653
               TEST_FFI_RC(0, botan_constant_time_compare, (input + pos, decoded[k], blockSize));
48✔
654
            }
655
         }
656

657
         /* Exercise a couple basic failure cases, such as you encounter if
658
          * the caller supplies invalid parameters.  We don't try to
659
          * exhaustively prove invalid parameters are handled through this
660
          * interface since the implementation only passes them through to
661
          * ZFEC::{encode,decode} where the real checking is.  We just want to
662
          * see that errors can propagate.
663
          */
664
         TEST_FFI_FAIL("encode with out-of-bounds encoding parameters should have failed",
2✔
665
                       botan_zfec_encode,
666
                       (0, 0, nullptr, 0, nullptr));
667
         TEST_FFI_FAIL("decode with out-of-bounds encoding parameters should have failed",
2✔
668
                       botan_zfec_decode,
669
                       (0, 0, nullptr, nullptr, 0, nullptr));
670
      }
3✔
671
};
672

673
class FFI_CRL_Test final : public FFI_Test {
1✔
674
   public:
675
      std::string name() const override { return "FFI CRL"; }
1✔
676

677
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
678
         const char* crl_string =
1✔
679
            "-----BEGIN X509 CRL-----\n"
680
            "MIICoTCCAQkCAQEwDQYJKoZIhvcNAQELBQAwgZQxLTArBgNVBAMTJFVzYWJsZSBj\n"
681
            "ZXJ0IHZhbGlkYXRpb246IFRlbXBvcmFyeSBDQTE5MDcGA1UECxMwQ2VudHJlIGZv\n"
682
            "ciBSZXNlYXJjaCBvbiBDcnlwdG9ncmFwaHkgYW5kIFNlY3VyaXR5MRswGQYDVQQK\n"
683
            "ExJNYXNhcnlrIFVuaXZlcnNpdHkxCzAJBgNVBAYTAkNaGA8yMDUwMDIyNTE1MjE0\n"
684
            "MloYDzIwNTAwMjI1MTUyNDQxWjAAoDowODAfBgNVHSMEGDAWgBRKzxAvI4+rVVo/\n"
685
            "JzLigRznREyB+TAVBgNVHRQEDgIMXcr16yNys/gjeuCFMA0GCSqGSIb3DQEBCwUA\n"
686
            "A4IBgQCfxv/5REM/KUnzeVycph3dJr1Yrtxhc6pZmQ9pMzSW/nawLN3rUHm5oG44\n"
687
            "ZuQgjvzE4PnbU0/DNRu/4w3H58kgrctJHHXbbvkU3lf2ZZLh2wBl+EUh92+/COow\n"
688
            "ZyGB+jqj/XwB99hYUhrY6NLEWRz08kpgG6dnNMEU0uFqdQKWk0CQPnmgPRgDb8BW\n"
689
            "IuMBcjY7aF9XoCZFOqPYdEvUKzAo4QGCf7uJ7fNGS3LqvjaLjAHJseSr5/yR7Q9r\n"
690
            "nEdI38yKPbRj0tNHe7j+BbYg31C+X+AZZKJtlTg8GxYR3qfQio1kDgpZ3rQLzHY3\n"
691
            "ea2MLX/Kdx9cPSwh4KwlcDxQmQKoELb4EnZW1CScSBHi9HQyCBNyCkgkOBMGcJqz\n"
692
            "Ihq1dGeSf8eca9+Avk5kAQ3yjXK1TI2CDEi0msrXLr9XbgowXiOLLzR+rYkhQz+V\n"
693
            "RnIoBwjnrGoJoz636KS170SZCB9ARNs17WE4IvbJdZrTXNOGaVZCQUUpiLRj4ZSO\n"
694
            "Na/nobI=\n"
695
            "-----END X509 CRL-----";
696

697
         botan_x509_crl_t bytecrl;
1✔
698
         if(!TEST_FFI_INIT(botan_x509_crl_load, (&bytecrl, reinterpret_cast<const uint8_t*>(crl_string), 966))) {
1✔
699
            return;
×
700
         }
701

702
         botan_x509_crl_t crl;
1✔
703
         REQUIRE_FFI_OK(botan_x509_crl_load_file, (&crl, Test::data_file("x509/nist/root.crl").c_str()));
1✔
704

705
         botan_x509_cert_t cert1;
1✔
706
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&cert1, Test::data_file("x509/nist/test01/end.crt").c_str()));
1✔
707
         TEST_FFI_RC(-1, botan_x509_is_revoked, (crl, cert1));
1✔
708
         TEST_FFI_OK(botan_x509_cert_destroy, (cert1));
1✔
709

710
         botan_x509_cert_t cert2;
1✔
711
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&cert2, Test::data_file("x509/nist/test20/int.crt").c_str()));
1✔
712
         TEST_FFI_RC(0, botan_x509_is_revoked, (crl, cert2));
1✔
713
         TEST_FFI_RC(-1, botan_x509_is_revoked, (bytecrl, cert2));
1✔
714
         TEST_FFI_OK(botan_x509_cert_destroy, (cert2));
1✔
715

716
         TEST_FFI_OK(botan_x509_crl_destroy, (crl));
1✔
717
         TEST_FFI_OK(botan_x509_crl_destroy, (bytecrl));
2✔
718
      }
719
};
720

721
class FFI_Cert_Validation_Test final : public FFI_Test {
1✔
722
   public:
723
      std::string name() const override { return "FFI Cert Validation"; }
1✔
724

725
      bool skip_this_test() const override {
1✔
726
   #if !defined(BOTAN_HAS_PKCSV15_SIGNATURE_PADDING)
727
         return true;
728
   #else
729
         return false;
1✔
730
   #endif
731
      }
732

733
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
734
         botan_x509_cert_t root;
1✔
735
         int rc;
1✔
736

737
         if(!TEST_FFI_INIT(botan_x509_cert_load_file, (&root, Test::data_file("x509/nist/root.crt").c_str()))) {
1✔
738
            return;
×
739
         }
740

741
         botan_x509_cert_t end2;
1✔
742
         botan_x509_cert_t sub2;
1✔
743
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&end2, Test::data_file("x509/nist/test02/end.crt").c_str()));
1✔
744
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&sub2, Test::data_file("x509/nist/test02/int.crt").c_str()));
1✔
745

746
         TEST_FFI_RC(1, botan_x509_cert_verify, (&rc, end2, &sub2, 1, &root, 1, nullptr, 0, nullptr, 0));
1✔
747
         result.confirm("Validation test02 failed", rc == 5002);
2✔
748
         result.test_eq("Validation test02 status string", botan_x509_cert_validation_status(rc), "Signature error");
1✔
749

750
         TEST_FFI_RC(1, botan_x509_cert_verify, (&rc, end2, nullptr, 0, &root, 1, nullptr, 0, nullptr, 0));
1✔
751
         result.confirm("Validation test02 failed (missing int)", rc == 3000);
2✔
752
         result.test_eq(
1✔
753
            "Validation test02 status string", botan_x509_cert_validation_status(rc), "Certificate issuer not found");
754

755
         botan_x509_cert_t end7;
1✔
756
         botan_x509_cert_t sub7;
1✔
757
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&end7, Test::data_file("x509/nist/test07/end.crt").c_str()));
1✔
758
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&sub7, Test::data_file("x509/nist/test07/int.crt").c_str()));
1✔
759

760
         const botan_x509_cert_t subs[2] = {sub2, sub7};
1✔
761
         TEST_FFI_RC(1, botan_x509_cert_verify, (&rc, end7, subs, 2, &root, 1, nullptr, 0, nullptr, 0));
1✔
762
         result.confirm("Validation test07 failed with expected error", rc == 1001);
2✔
763
         result.test_eq("Validation test07 status string",
1✔
764
                        botan_x509_cert_validation_status(rc),
765
                        "Hash function used is considered too weak for security");
766

767
         TEST_FFI_RC(0, botan_x509_cert_verify, (&rc, end7, subs, 2, &root, 1, nullptr, 80, nullptr, 0));
1✔
768
         result.confirm("Validation test07 passed", rc == 0);
2✔
769
         result.test_eq("Validation test07 status string", botan_x509_cert_validation_status(rc), "Verified");
1✔
770

771
         TEST_FFI_RC(1,
1✔
772
                     botan_x509_cert_verify_with_crl,
773
                     (&rc, end7, subs, 2, nullptr, 0, nullptr, 0, "x509/farce", 0, nullptr, 0));
774
         result.confirm("Validation test07 failed with expected error", rc == 3000);
2✔
775
         result.test_eq(
1✔
776
            "Validation test07 status string", botan_x509_cert_validation_status(rc), "Certificate issuer not found");
777

778
         botan_x509_crl_t rootcrl;
1✔
779

780
         REQUIRE_FFI_OK(botan_x509_crl_load_file, (&rootcrl, Test::data_file("x509/nist/root.crl").c_str()));
1✔
781
         TEST_FFI_RC(
1✔
782
            0, botan_x509_cert_verify_with_crl, (&rc, end7, subs, 2, &root, 1, &rootcrl, 1, nullptr, 80, nullptr, 0));
783
         result.confirm("Validation test07 with CRL passed", rc == 0);
2✔
784
         result.test_eq("Validation test07 with CRL status string", botan_x509_cert_validation_status(rc), "Verified");
1✔
785

786
         botan_x509_cert_t end20;
1✔
787
         botan_x509_cert_t sub20;
1✔
788
         botan_x509_crl_t sub20crl;
1✔
789
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&end20, Test::data_file("x509/nist/test20/end.crt").c_str()));
1✔
790
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&sub20, Test::data_file("x509/nist/test20/int.crt").c_str()));
1✔
791
         REQUIRE_FFI_OK(botan_x509_crl_load_file, (&sub20crl, Test::data_file("x509/nist/test20/int.crl").c_str()));
1✔
792
         const botan_x509_crl_t crls[2] = {sub20crl, rootcrl};
1✔
793
         TEST_FFI_RC(
1✔
794
            1, botan_x509_cert_verify_with_crl, (&rc, end20, &sub20, 1, &root, 1, crls, 2, nullptr, 80, nullptr, 0));
795
         result.confirm("Validation test20 failed with expected error", rc == 5000);
2✔
796
         result.test_eq(
1✔
797
            "Validation test20 status string", botan_x509_cert_validation_status(rc), "Certificate is revoked");
798

799
         TEST_FFI_OK(botan_x509_cert_destroy, (end2));
1✔
800
         TEST_FFI_OK(botan_x509_cert_destroy, (sub2));
1✔
801
         TEST_FFI_OK(botan_x509_cert_destroy, (end7));
1✔
802
         TEST_FFI_OK(botan_x509_cert_destroy, (sub7));
1✔
803
         TEST_FFI_OK(botan_x509_cert_destroy, (end20));
1✔
804
         TEST_FFI_OK(botan_x509_cert_destroy, (sub20));
1✔
805
         TEST_FFI_OK(botan_x509_crl_destroy, (sub20crl));
1✔
806
         TEST_FFI_OK(botan_x509_cert_destroy, (root));
1✔
807
         TEST_FFI_OK(botan_x509_crl_destroy, (rootcrl));
2✔
808
      }
809
};
810

811
class FFI_ECDSA_Certificate_Test final : public FFI_Test {
1✔
812
   public:
813
      std::string name() const override { return "FFI ECDSA cert"; }
1✔
814

815
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
816
         botan_x509_cert_t cert;
1✔
817
         if(TEST_FFI_INIT(botan_x509_cert_load_file, (&cert, Test::data_file("x509/ecc/isrg-root-x2.pem").c_str()))) {
1✔
818
            size_t date_len = 0;
1✔
819
            TEST_FFI_RC(
1✔
820
               BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_x509_cert_get_time_starts, (cert, nullptr, &date_len));
821

822
            date_len = 8;
1✔
823
            TEST_FFI_RC(
1✔
824
               BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_x509_cert_get_time_starts, (cert, nullptr, &date_len));
825

826
            std::string date(date_len - 1, '0');
1✔
827
            TEST_FFI_OK(botan_x509_cert_get_time_starts, (cert, date.data(), &date_len));
1✔
828
            result.test_eq("cert valid from", date, "200904000000Z");
2✔
829

830
            date_len = 0;
1✔
831
            TEST_FFI_RC(
1✔
832
               BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_x509_cert_get_time_expires, (cert, nullptr, &date_len));
833

834
            date.resize(date_len - 1);
1✔
835
            TEST_FFI_OK(botan_x509_cert_get_time_expires, (cert, date.data(), &date_len));
1✔
836
            result.test_eq("cert valid until", date, "400917160000Z");
2✔
837

838
            uint64_t not_before = 0;
1✔
839
            TEST_FFI_OK(botan_x509_cert_not_before, (cert, &not_before));
1✔
840
            result.confirm("cert not before", not_before == 1599177600);
2✔
841

842
            uint64_t not_after = 0;
1✔
843
            TEST_FFI_OK(botan_x509_cert_not_after, (cert, &not_after));
1✔
844
            result.confirm("cert not after", not_after == 2231510400);
2✔
845

846
            size_t serial_len = 0;
1✔
847
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
848
                        botan_x509_cert_get_serial_number,
849
                        (cert, nullptr, &serial_len));
850

851
            std::vector<uint8_t> serial(serial_len);
1✔
852
            TEST_FFI_OK(botan_x509_cert_get_serial_number, (cert, serial.data(), &serial_len));
1✔
853
            result.test_eq("cert serial length", serial.size(), 16);
1✔
854
            result.test_eq("cert serial", Botan::hex_encode(serial), "41D29DD172EAEEA780C12C6CE92F8752");
2✔
855

856
            size_t fingerprint_len = 0;
1✔
857
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
858
                        botan_x509_cert_get_fingerprint,
859
                        (cert, "SHA-256", nullptr, &fingerprint_len));
860

861
            std::vector<uint8_t> fingerprint(fingerprint_len);
1✔
862
            TEST_FFI_OK(botan_x509_cert_get_fingerprint, (cert, "SHA-256", fingerprint.data(), &fingerprint_len));
1✔
863
            result.test_eq(
1✔
864
               "cert fingerprint",
865
               reinterpret_cast<const char*>(fingerprint.data()),
1✔
866
               "69:72:9B:8E:15:A8:6E:FC:17:7A:57:AF:B7:17:1D:FC:64:AD:D2:8C:2F:CA:8C:F1:50:7E:34:45:3C:CB:14:70");
867

868
            size_t key_id_len = 0;
1✔
869
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
870
                        botan_x509_cert_get_authority_key_id,
871
                        (cert, nullptr, &key_id_len));
872

873
            result.test_eq("No AKID", key_id_len, 0);
1✔
874

875
            key_id_len = 0;
1✔
876
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
877
                        botan_x509_cert_get_subject_key_id,
878
                        (cert, nullptr, &key_id_len));
879

880
            std::vector<uint8_t> key_id(key_id_len);
1✔
881
            TEST_FFI_OK(botan_x509_cert_get_subject_key_id, (cert, key_id.data(), &key_id_len));
1✔
882
            result.test_eq("cert subject key id",
3✔
883
                           Botan::hex_encode(key_id.data(), key_id.size(), true),
2✔
884
                           "7C4296AEDE4B483BFA92F89E8CCF6D8BA9723795");
885

886
            size_t pubkey_len = 0;
1✔
887
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
888
                        botan_x509_cert_get_public_key_bits,
889
                        (cert, nullptr, &pubkey_len));
890

891
            std::vector<uint8_t> pubkey(pubkey_len);
1✔
892
            TEST_FFI_OK(botan_x509_cert_get_public_key_bits, (cert, pubkey.data(), &pubkey_len));
1✔
893

894
   #if defined(BOTAN_HAS_ECDSA)
895
            botan_pubkey_t pub;
1✔
896
            if(TEST_FFI_OK(botan_x509_cert_get_public_key, (cert, &pub))) {
1✔
897
               TEST_FFI_RC(0, botan_pubkey_ecc_key_used_explicit_encoding, (pub));
1✔
898
               TEST_FFI_OK(botan_pubkey_destroy, (pub));
2✔
899
            }
900
   #endif
901

902
            size_t dn_len = 0;
1✔
903
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
904
                        botan_x509_cert_get_issuer_dn,
905
                        (cert, "Name", 0, nullptr, &dn_len));
906

907
            std::vector<uint8_t> dn(dn_len);
1✔
908
            TEST_FFI_OK(botan_x509_cert_get_issuer_dn, (cert, "Name", 0, dn.data(), &dn_len));
1✔
909
            result.test_eq("issuer dn", reinterpret_cast<const char*>(dn.data()), "ISRG Root X2");
1✔
910

911
            dn_len = 0;
1✔
912
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
913
                        botan_x509_cert_get_subject_dn,
914
                        (cert, "Name", 0, nullptr, &dn_len));
915

916
            dn.resize(dn_len);
1✔
917
            TEST_FFI_OK(botan_x509_cert_get_subject_dn, (cert, "Name", 0, dn.data(), &dn_len));
1✔
918
            result.test_eq("subject dn", reinterpret_cast<const char*>(dn.data()), "ISRG Root X2");
1✔
919

920
            size_t printable_len = 0;
1✔
921
            TEST_FFI_RC(
1✔
922
               BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_x509_cert_to_string, (cert, nullptr, &printable_len));
923

924
            std::string printable(printable_len - 1, '0');
1✔
925
            TEST_FFI_OK(botan_x509_cert_to_string, (cert, printable.data(), &printable_len));
1✔
926

927
            TEST_FFI_RC(0, botan_x509_cert_allowed_usage, (cert, KEY_CERT_SIGN));
1✔
928
            TEST_FFI_RC(0, botan_x509_cert_allowed_usage, (cert, CRL_SIGN));
1✔
929
            TEST_FFI_RC(1, botan_x509_cert_allowed_usage, (cert, DIGITAL_SIGNATURE));
1✔
930

931
            TEST_FFI_OK(botan_x509_cert_destroy, (cert));
2✔
932
         }
6✔
933
      }
1✔
934
};
935

936
auto read_distinguished_name(std::span<const uint8_t> bytes) {
32✔
937
   auto dec = Botan::BER_Decoder(bytes);
32✔
938
   Botan::X509_DN dn;
32✔
939
   dn.decode_from(dec);
32✔
940
   return dn;
32✔
941
}
32✔
942

943
class FFI_Cert_AlternativeNames_Test final : public FFI_Test {
1✔
944
   private:
945
      template <std::invocable<botan_x509_cert_t, size_t, botan_x509_general_name_t*> EnumeratorT,
946
                std::invocable<botan_x509_general_name_t> VisitorT>
947
      static void visit_general_names(Test::Result& result,
12✔
948
                                      botan_x509_cert_t cert,
949
                                      EnumeratorT enumerator_fn,
950
                                      VisitorT visitor_fn) {
951
         int rc = BOTAN_FFI_SUCCESS;
12✔
952
         for(size_t i = 0; rc == BOTAN_FFI_SUCCESS; ++i) {
144✔
953
            botan_x509_general_name_t gn;
954
            rc = enumerator_fn(cert, i, &gn);
132✔
955
            if(rc == BOTAN_FFI_SUCCESS) {
132✔
956
               visitor_fn(gn);
120✔
957
               TEST_FFI_OK(botan_x509_general_name_destroy, (gn));
240✔
958
            } else if(rc != BOTAN_FFI_ERROR_OUT_OF_RANGE) {
12✔
959
               result.test_note(
×
960
                  Botan::fmt("enumerator produced unexpected return code: {}", botan_error_description(rc)));
×
961
            }
962
         }
963
      }
12✔
964

965
      template <typename EnumeratorT>
966
      static auto read_string_alternative_names(Test::Result& result,
8✔
967
                                                botan_x509_cert_t cert,
968
                                                EnumeratorT fn,
969
                                                botan_x509_general_name_types type) {
970
         std::vector<std::string> out;
8✔
971

972
         visit_general_names(result, cert, fn, [&](botan_x509_general_name_t gn) {
88✔
973
            unsigned int gn_type;
974
            TEST_FFI_OK(botan_x509_general_name_get_type, (gn, &gn_type));
80✔
975
            if(static_cast<botan_x509_general_name_types>(gn_type) == type) {
80✔
976
               ViewStringSink str;
14✔
977
               TEST_FFI_OK(botan_x509_general_name_view_string_value, (gn, str.delegate(), str.callback()));
14✔
978
               out.push_back(str.get());
14✔
979
            }
14✔
980
         });
981

982
         return out;
8✔
983
      }
×
984

985
      template <typename EnumeratorT>
986
      static auto read_binary_alternative_names(Test::Result& result,
4✔
987
                                                botan_x509_cert_t cert,
988
                                                EnumeratorT fn,
989
                                                botan_x509_general_name_types type) {
990
         std::vector<std::vector<uint8_t>> out;
4✔
991

992
         visit_general_names(result, cert, fn, [&](botan_x509_general_name_t gn) {
44✔
993
            unsigned int gn_type;
994
            TEST_FFI_OK(botan_x509_general_name_get_type, (gn, &gn_type));
40✔
995
            if(static_cast<botan_x509_general_name_types>(gn_type) == type) {
40✔
996
               ViewBytesSink data;
8✔
997
               TEST_FFI_OK(botan_x509_general_name_view_binary_value, (gn, data.delegate(), data.callback()));
8✔
998
               out.emplace_back(data.get().begin(), data.get().end());
8✔
999
            }
8✔
1000
         });
1001

1002
         return out;
4✔
1003
      }
×
1004

1005
      static auto read_common_names(std::span<const std::vector<uint8_t>> bytes) {
2✔
1006
         std::vector<std::string> result;
2✔
1007
         for(const auto& dn_bytes : bytes) {
8✔
1008
            const auto dn = read_distinguished_name(dn_bytes);
6✔
1009
            result.push_back(dn.get_first_attribute("X520.CommonName"));
12✔
1010
         }
6✔
1011
         return result;
2✔
1012
      }
×
1013

1014
   public:
1015
      std::string name() const override { return "FFI X509 Alternative Names"; }
1✔
1016

1017
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1018
         botan_x509_cert_t cert_none;
1✔
1019
         if(!TEST_FFI_INIT(botan_x509_cert_load_file,
1✔
1020
                           (&cert_none, Test::data_file("x509/misc/no_alternative_names.pem").c_str()))) {
1021
            return;
×
1022
         }
1023

1024
         botan_x509_general_name_t nil = nullptr;
1✔
1025
         TEST_FFI_RC(BOTAN_FFI_ERROR_NO_VALUE, botan_x509_cert_subject_alternative_names, (cert_none, 0, &nil));
1✔
1026
         TEST_FFI_RC(BOTAN_FFI_ERROR_NO_VALUE, botan_x509_cert_issuer_alternative_names, (cert_none, 0, &nil));
1✔
1027
         result.confirm("no general name created", nil == nullptr);
2✔
1028

1029
         botan_x509_cert_t cert;
1✔
1030
         if(!TEST_FFI_INIT(botan_x509_cert_load_file,
1✔
1031
                           (&cert, Test::data_file("x509/misc/multiple_alternative_names.pem").c_str()))) {
1032
            return;
1033
         }
1034

1035
         const auto get_san = botan_x509_cert_subject_alternative_names;
1✔
1036

1037
         const auto san_email = read_string_alternative_names(result, cert, get_san, BOTAN_X509_EMAIL_ADDRESS);
1✔
1038
         result.test_eq("expected number of emails in SAN", san_email.size(), 2);
1✔
1039
         result.confirm("testing@x509-labs.com", Botan::value_exists(san_email, "testing@x509-labs.com"));
2✔
1040
         result.confirm("info@x509-labs.com", Botan::value_exists(san_email, "info@x509-labs.com"));
2✔
1041

1042
         const auto san_dns = read_string_alternative_names(result, cert, get_san, BOTAN_X509_DNS_NAME);
1✔
1043
         result.test_eq("expected number of hostnames in SAN", san_dns.size(), 3);
1✔
1044
         result.confirm("test.x509-labs.com", Botan::value_exists(san_dns, "test.x509-labs.com"));
2✔
1045
         result.confirm("versuch.x509-labs.com", Botan::value_exists(san_dns, "versuch.x509-labs.com"));
2✔
1046
         result.confirm("trail.x509-labs.com", Botan::value_exists(san_dns, "trail.x509-labs.com"));
2✔
1047

1048
         const auto san_uri = read_string_alternative_names(result, cert, get_san, BOTAN_X509_URI);
1✔
1049
         result.test_eq("expected number of URIs in SAN", san_uri.size(), 2);
1✔
1050
         result.confirm("https://x509-labs.com", Botan::value_exists(san_uri, "https://x509-labs.com"));
2✔
1051
         result.confirm("http://x509-labs.com", Botan::value_exists(san_uri, "http://x509-labs.com"));
2✔
1052

1053
         const auto san_ip4 = read_string_alternative_names(result, cert, get_san, BOTAN_X509_IP_ADDRESS);
1✔
1054
         result.test_eq("expected number of IPv4 addresses", san_ip4.size(), 1);
1✔
1055
         result.confirm("127.0.0.1", Botan::value_exists(san_ip4, "127.0.0.1"));
2✔
1056
         const auto san_ip4_bin = read_binary_alternative_names(result, cert, get_san, BOTAN_X509_IP_ADDRESS);
1✔
1057
         result.test_eq("expected number of IPv4 addresses (bin)", san_ip4_bin.size(), 1);
1✔
1058
         result.test_eq("127.0.0.1 (bin)", san_ip4_bin.front(), Botan::store_be(uint32_t(0x7F000001)));
2✔
1059

1060
         const auto san_dn_bytes = read_binary_alternative_names(result, cert, get_san, BOTAN_X509_DIRECTORY_NAME);
1✔
1061
         result.test_eq("expected number of DNs in SAN", san_dn_bytes.size(), 3);
1✔
1062
         const auto san_dn_cns = read_common_names(san_dn_bytes);
1✔
1063
         result.confirm("First Name", Botan::value_exists(san_dn_cns, "First Name"));
2✔
1064
         result.confirm("Middle Name", Botan::value_exists(san_dn_cns, "Middle Name"));
2✔
1065
         result.confirm("Last Name", Botan::value_exists(san_dn_cns, "Last Name"));
2✔
1066

1067
         auto get_ian = botan_x509_cert_issuer_alternative_names;
1✔
1068

1069
         const auto ian_email = read_string_alternative_names(result, cert, get_ian, BOTAN_X509_EMAIL_ADDRESS);
1✔
1070
         result.test_eq("expected number of emails in IAN", ian_email.size(), 0);
1✔
1071

1072
         const auto ian_dns = read_string_alternative_names(result, cert, get_ian, BOTAN_X509_DNS_NAME);
1✔
1073
         result.test_eq("expected number of hostnames in IAN", ian_dns.size(), 3);
1✔
1074
         result.confirm("test.x509-labs-ca.com", Botan::value_exists(ian_dns, "test.x509-labs-ca.com"));
2✔
1075
         result.confirm("versuch.x509-labs-ca.com", Botan::value_exists(ian_dns, "versuch.x509-labs-ca.com"));
2✔
1076
         result.confirm("trail.x509-labs-ca.com", Botan::value_exists(ian_dns, "trail.x509-labs-ca.com"));
2✔
1077

1078
         const auto ian_uri = read_string_alternative_names(result, cert, get_ian, BOTAN_X509_URI);
1✔
1079
         result.test_eq("expected number of URIs in IAN", ian_uri.size(), 2);
1✔
1080
         result.confirm("https://x509-labs-ca.com", Botan::value_exists(ian_uri, "https://x509-labs-ca.com"));
2✔
1081
         result.confirm("http://x509-labs-ca.com", Botan::value_exists(ian_uri, "http://x509-labs-ca.com"));
2✔
1082

1083
         const auto ian_ip4 = read_string_alternative_names(result, cert, get_ian, BOTAN_X509_IP_ADDRESS);
1✔
1084
         result.test_eq("expected number of IPv4 addresses", ian_ip4.size(), 1);
1✔
1085
         result.confirm("192.168.1.1", Botan::value_exists(ian_ip4, "192.168.1.1"));
2✔
1086
         const auto ian_ip4_bin = read_binary_alternative_names(result, cert, get_ian, BOTAN_X509_IP_ADDRESS);
1✔
1087
         result.test_eq("expected number of IPv4 addresses (bin)", ian_ip4_bin.size(), 1);
1✔
1088
         result.test_eq("192.168.1.1 (bin)", ian_ip4_bin.front(), Botan::store_be(uint32_t(0xC0A80101)));
2✔
1089

1090
         const auto ian_dn_bytes = read_binary_alternative_names(result, cert, get_ian, BOTAN_X509_DIRECTORY_NAME);
1✔
1091
         result.test_eq("expected number of DNs in IAN", ian_dn_bytes.size(), 3);
1✔
1092
         const auto ian_dn_cns = read_common_names(ian_dn_bytes);
1✔
1093
         result.confirm("First CA", Botan::value_exists(ian_dn_cns, "First CA"));
2✔
1094
         result.confirm("Middle CA", Botan::value_exists(ian_dn_cns, "Middle CA"));
2✔
1095
         result.confirm("Last CA", Botan::value_exists(ian_dn_cns, "Last CA"));
2✔
1096

1097
         TEST_FFI_OK(botan_x509_cert_destroy, (cert));
1✔
1098
         TEST_FFI_OK(botan_x509_cert_destroy, (cert_none));
1✔
1099
      }
1✔
1100
};
1101

1102
class FFI_Cert_NameConstraints_Test final : public FFI_Test {
1✔
1103
   private:
1104
      static auto read_constraints(Test::Result& result, botan_x509_cert_t cert, bool permitted) {
2✔
1105
         std::vector<std::pair<botan_x509_general_name_types, std::string>> out;
2✔
1106

1107
         int rc = BOTAN_FFI_SUCCESS;
2✔
1108
         for(size_t i = 0; rc == BOTAN_FFI_SUCCESS; ++i) {
78✔
1109
            botan_x509_general_name_t constraint;
76✔
1110
            if(permitted) {
76✔
1111
               rc = botan_x509_cert_permitted_name_constraints(cert, i, &constraint);
73✔
1112
            } else {
1113
               rc = botan_x509_cert_excluded_name_constraints(cert, i, &constraint);
3✔
1114
            }
1115

1116
            if(rc == BOTAN_FFI_SUCCESS) {
76✔
1117
               ViewBytesSink bytes;
74✔
1118
               ViewStringSink string;
74✔
1119

1120
               unsigned int type;
74✔
1121
               const auto rc2 = botan_x509_general_name_get_type(constraint, &type);
74✔
1122
               if(rc2 == BOTAN_FFI_SUCCESS) {
74✔
1123
                  const auto gn_type = static_cast<botan_x509_general_name_types>(type);
73✔
1124
                  switch(gn_type) {
73✔
1125
                     case BOTAN_X509_EMAIL_ADDRESS:
48✔
1126
                     case BOTAN_X509_DNS_NAME:
48✔
1127
                     case BOTAN_X509_URI:
48✔
1128
                     case BOTAN_X509_IP_ADDRESS:
48✔
1129
                        TEST_FFI_OK(botan_x509_general_name_view_string_value,
48✔
1130
                                    (constraint, string.delegate(), string.callback()));
1131
                        out.emplace_back(gn_type, string.get());
48✔
1132
                        break;
1133
                     case BOTAN_X509_DIRECTORY_NAME:
25✔
1134
                        TEST_FFI_OK(botan_x509_general_name_view_binary_value,
25✔
1135
                                    (constraint, bytes.delegate(), bytes.callback()));
1136
                        out.emplace_back(gn_type, read_distinguished_name(bytes.get()).to_string());
50✔
1137
                        break;
25✔
1138
                     case BOTAN_X509_OTHER_NAME:
×
1139
                        out.emplace_back(gn_type, "<not supported>");
×
1140
                        break;
1141
                  }
1142
               } else {
1143
                  result.test_note(
1✔
1144
                     Botan::fmt("botan_x509_general_name_get_type returned {}", botan_error_description(rc2)));
2✔
1145
               }
1146

1147
               TEST_FFI_OK(botan_x509_general_name_destroy, (constraint));
148✔
1148
            } else if(rc != BOTAN_FFI_ERROR_OUT_OF_RANGE) {
101✔
1149
               result.test_failure(Botan::fmt("unexpected error code: {}", botan_error_description(rc)));
×
1150
            }
1151
         }
1152

1153
         return out;
2✔
1154
      }
×
1155

1156
   public:
1157
      std::string name() const override { return "FFI X509 Name Constraints"; }
1✔
1158

1159
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1160
         botan_x509_cert_t cert;
1✔
1161
         if(!TEST_FFI_INIT(botan_x509_cert_load_file,
1✔
1162
                           (&cert, Test::data_file("x509/misc/name_constraint_ci/int.pem").c_str()))) {
1163
            return;
×
1164
         }
1165

1166
         const auto permitted = read_constraints(result, cert, true);
1✔
1167
         const auto excluded = read_constraints(result, cert, false);
1✔
1168

1169
         result.test_eq_sz("permissions", permitted.size(), 72);
1✔
1170
         result.test_eq_sz("exclusions", excluded.size(), 1);
1✔
1171

1172
         using V = decltype(permitted)::value_type;
1✔
1173
         result.confirm("email", Botan::value_exists(permitted, V{BOTAN_X509_EMAIL_ADDRESS, "pec.aruba.it"}));
2✔
1174
         result.confirm("DNS", Botan::value_exists(permitted, V{BOTAN_X509_DNS_NAME, "gov.it"}));
2✔
1175
         result.confirm("DN",
1✔
1176
                        Botan::value_exists(permitted,
1✔
1177
                                            V{BOTAN_X509_DIRECTORY_NAME,
1✔
1178
                                              R"(C="IT",X520.State="Roma",X520.Locality="Roma",O="Sogei S.p.A.")"}));
1179
         result.confirm("IP", Botan::value_exists(excluded, V{BOTAN_X509_IP_ADDRESS, "0.0.0.0/0.0.0.0"}));
2✔
1180

1181
         // below are more generic general_name_t tests
1182

1183
         botan_x509_general_name_t email;
1✔
1184
         botan_x509_general_name_t dns;
1✔
1185
         botan_x509_general_name_t dn;
1✔
1186
         botan_x509_general_name_t ip;
1✔
1187
         TEST_FFI_OK(botan_x509_cert_permitted_name_constraints, (cert, 0, &email));
1✔
1188
         TEST_FFI_OK(botan_x509_cert_permitted_name_constraints, (cert, 33, &dns));
1✔
1189
         TEST_FFI_OK(botan_x509_cert_permitted_name_constraints, (cert, 47, &dn));
1✔
1190
         TEST_FFI_OK(botan_x509_cert_excluded_name_constraints, (cert, 0, &ip));
1✔
1191

1192
         unsigned int type;
1✔
1193
         TEST_FFI_OK(botan_x509_general_name_get_type, (email, &type));
1✔
1194
         result.test_is_eq("email", static_cast<botan_x509_general_name_types>(type), BOTAN_X509_EMAIL_ADDRESS);
1✔
1195
         TEST_FFI_OK(botan_x509_general_name_get_type, (dns, &type));
1✔
1196
         result.test_is_eq("dns", static_cast<botan_x509_general_name_types>(type), BOTAN_X509_DNS_NAME);
1✔
1197
         TEST_FFI_OK(botan_x509_general_name_get_type, (dn, &type));
1✔
1198
         result.test_is_eq("dn", static_cast<botan_x509_general_name_types>(type), BOTAN_X509_DIRECTORY_NAME);
1✔
1199
         TEST_FFI_OK(botan_x509_general_name_get_type, (ip, &type));
1✔
1200
         result.test_is_eq("ip", static_cast<botan_x509_general_name_types>(type), BOTAN_X509_IP_ADDRESS);
1✔
1201

1202
         ViewBytesSink bin;
1✔
1203
         ViewStringSink str;
1✔
1204

1205
         TEST_FFI_OK(botan_x509_general_name_view_string_value, (email, str.delegate(), str.callback()));
1✔
1206
         result.test_eq("email as expected", str.get(), "agid.gov.it");
2✔
1207
         TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE,
1✔
1208
                     botan_x509_general_name_view_binary_value,
1209
                     (email, bin.delegate(), bin.callback()));
1210

1211
         TEST_FFI_OK(botan_x509_general_name_view_string_value, (dns, str.delegate(), str.callback()));
1✔
1212
         result.test_eq("dns as expected", str.get(), "agendadigitale.it");
2✔
1213
         TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE,
1✔
1214
                     botan_x509_general_name_view_binary_value,
1215
                     (dns, bin.delegate(), bin.callback()));
1216

1217
         TEST_FFI_OK(botan_x509_general_name_view_binary_value, (dn, bin.delegate(), bin.callback()));
1✔
1218
         const auto organization = read_distinguished_name(bin.get()).get_first_attribute("O");
1✔
1219
         result.test_eq("dn as expected", organization, "ACI Informatica S.p.A.");
2✔
1220
         TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE,
1✔
1221
                     botan_x509_general_name_view_string_value,
1222
                     (dn, str.delegate(), str.callback()));
1223

1224
         TEST_FFI_OK(botan_x509_general_name_view_binary_value, (ip, bin.delegate(), bin.callback()));
1✔
1225
         result.test_eq_sz("ip has correct length", bin.get().size(), 8);
1✔
1226
         TEST_FFI_OK(botan_x509_general_name_view_string_value, (ip, str.delegate(), str.callback()));
1✔
1227
         result.test_eq("ip has correct length", str.get(), "0.0.0.0/0.0.0.0");
2✔
1228

1229
         TEST_FFI_OK(botan_x509_general_name_destroy, (email));
1✔
1230
         TEST_FFI_OK(botan_x509_general_name_destroy, (dns));
1✔
1231
         TEST_FFI_OK(botan_x509_general_name_destroy, (dn));
1✔
1232
         TEST_FFI_OK(botan_x509_general_name_destroy, (ip));
1✔
1233

1234
         TEST_FFI_OK(botan_x509_cert_destroy, (cert));
2✔
1235
      }
2✔
1236
};
1237

1238
class FFI_PKCS_Hashid_Test final : public FFI_Test {
1✔
1239
   public:
1240
      std::string name() const override { return "FFI PKCS hash id"; }
1✔
1241

1242
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1243
         std::vector<uint8_t> hash_id(64);
1✔
1244
         size_t hash_id_len = hash_id.size();
1✔
1245

1246
         if(TEST_FFI_INIT(botan_pkcs_hash_id, ("SHA-256", hash_id.data(), &hash_id_len))) {
1✔
1247
            result.test_eq("Expected SHA-256 PKCS hash id len", hash_id_len, 19);
1✔
1248

1249
            hash_id.resize(hash_id_len);
1✔
1250
            result.test_eq("Expected SHA_256 PKCS hash id", hash_id, "3031300D060960864801650304020105000420");
1✔
1251

1252
            hash_id_len = 3;  // too short
1✔
1253
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
2✔
1254
                        botan_pkcs_hash_id,
1255
                        ("SHA-256", hash_id.data(), &hash_id_len));
1256
         }
1257
      }
1✔
1258
};
1259

1260
class FFI_CBC_Cipher_Test final : public FFI_Test {
1✔
1261
   public:
1262
      std::string name() const override { return "FFI CBC cipher"; }
1✔
1263

1264
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1265
         botan_cipher_t cipher_encrypt;
1✔
1266
         botan_cipher_t cipher_decrypt;
1✔
1267

1268
         if(TEST_FFI_INIT(botan_cipher_init, (&cipher_encrypt, "AES-128/CBC/PKCS7", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
1✔
1269
            size_t min_keylen = 0;
1✔
1270
            size_t max_keylen = 0;
1✔
1271
            TEST_FFI_OK(botan_cipher_query_keylen, (cipher_encrypt, &min_keylen, &max_keylen));
1✔
1272
            result.test_int_eq(min_keylen, 16, "Min key length");
1✔
1273
            result.test_int_eq(max_keylen, 16, "Max key length");
1✔
1274

1275
            // from https://github.com/geertj/bluepass/blob/master/tests/vectors/aes-cbc-pkcs7.txt
1276
            const std::vector<uint8_t> plaintext =
1✔
1277
               Botan::hex_decode("0397f4f6820b1f9386f14403be5ac16e50213bd473b4874b9bcbf5f318ee686b1d");
1✔
1278
            const std::vector<uint8_t> symkey = Botan::hex_decode("898be9cc5004ed0fa6e117c9a3099d31");
1✔
1279
            const std::vector<uint8_t> nonce = Botan::hex_decode("9dea7621945988f96491083849b068df");
1✔
1280
            const std::vector<uint8_t> exp_ciphertext = Botan::hex_decode(
1✔
1281
               "e232cd6ef50047801ee681ec30f61d53cfd6b0bca02fd03c1b234baa10ea82ac9dab8b960926433a19ce6dea08677e34");
1✔
1282

1283
            size_t output_written = 0;
1✔
1284
            size_t input_consumed = 0;
1✔
1285

1286
            // Test that after clear or final the object can be reused
1287
            for(size_t r = 0; r != 2; ++r) {
3✔
1288
               size_t ctext_len;
2✔
1289
               TEST_FFI_OK(botan_cipher_output_length, (cipher_encrypt, plaintext.size(), &ctext_len));
2✔
1290
               result.test_eq("Expected size of padded message", ctext_len, plaintext.size() + 15);
2✔
1291
               std::vector<uint8_t> ciphertext(ctext_len);
2✔
1292

1293
               size_t update_granularity = 0;
2✔
1294
               size_t ideal_granularity = 0;
2✔
1295
               size_t taglen = 0;
2✔
1296

1297
               TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_encrypt, &update_granularity));
2✔
1298
               TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_encrypt, &ideal_granularity));
2✔
1299
               TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_encrypt, &taglen));
2✔
1300

1301
               result.test_eq(
2✔
1302
                  "ideal granularity is a multiple of update granularity", ideal_granularity % update_granularity, 0);
1303
               result.test_eq("not an AEAD, hence no tag", taglen, 0);
2✔
1304

1305
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2✔
1306
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
1307
               TEST_FFI_OK(botan_cipher_update,
2✔
1308
                           (cipher_encrypt,
1309
                            0,
1310
                            ciphertext.data(),
1311
                            ciphertext.size(),
1312
                            &output_written,
1313
                            plaintext.data(),
1314
                            plaintext.size(),
1315
                            &input_consumed));
1316
               TEST_FFI_OK(botan_cipher_clear, (cipher_encrypt));
2✔
1317

1318
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2✔
1319
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
1320
               TEST_FFI_OK(botan_cipher_update,
2✔
1321
                           (cipher_encrypt,
1322
                            BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1323
                            ciphertext.data(),
1324
                            ciphertext.size(),
1325
                            &output_written,
1326
                            plaintext.data(),
1327
                            plaintext.size(),
1328
                            &input_consumed));
1329

1330
               ciphertext.resize(output_written);
2✔
1331
               result.test_eq("AES/CBC ciphertext", ciphertext, exp_ciphertext);
4✔
1332

1333
               if(TEST_FFI_OK(botan_cipher_init, (&cipher_decrypt, "AES-128/CBC", BOTAN_CIPHER_INIT_FLAG_DECRYPT))) {
2✔
1334
                  size_t ptext_len;
2✔
1335
                  TEST_FFI_OK(botan_cipher_output_length, (cipher_decrypt, ciphertext.size(), &ptext_len));
2✔
1336
                  std::vector<uint8_t> decrypted(ptext_len);
2✔
1337

1338
                  TEST_FFI_RC(0, botan_cipher_is_authenticated, (cipher_encrypt));
2✔
1339

1340
                  TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_decrypt, &update_granularity));
2✔
1341
                  TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_decrypt, &ideal_granularity));
2✔
1342
                  TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_decrypt, &taglen));
2✔
1343

1344
                  result.test_eq("ideal granularity is a multiple of update granularity (decrypt)",
2✔
1345
                                 ideal_granularity % update_granularity,
1346
                                 0);
1347
                  result.test_eq("not an AEAD, hence no tag (decrypt)", taglen, 0);
2✔
1348

1349
                  TEST_FFI_OK(botan_cipher_set_key, (cipher_decrypt, symkey.data(), symkey.size()));
2✔
1350
                  TEST_FFI_OK(botan_cipher_start, (cipher_decrypt, nonce.data(), nonce.size()));
2✔
1351
                  TEST_FFI_OK(botan_cipher_update,
2✔
1352
                              (cipher_decrypt,
1353
                               BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1354
                               decrypted.data(),
1355
                               decrypted.size(),
1356
                               &output_written,
1357
                               ciphertext.data(),
1358
                               ciphertext.size(),
1359
                               &input_consumed));
1360

1361
                  decrypted.resize(output_written);
2✔
1362

1363
                  result.test_eq("AES/CBC plaintext", decrypted, plaintext);
4✔
1364

1365
                  TEST_FFI_OK(botan_cipher_destroy, (cipher_decrypt));
4✔
1366
               }
2✔
1367
            }
2✔
1368

1369
            TEST_FFI_OK(botan_cipher_destroy, (cipher_encrypt));
2✔
1370
         }
4✔
1371
      }
1✔
1372
};
1373

1374
class FFI_GCM_Test final : public FFI_Test {
1✔
1375
   public:
1376
      std::string name() const override { return "FFI GCM"; }
1✔
1377

1378
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1379
         botan_cipher_t cipher_encrypt;
1✔
1380
         botan_cipher_t cipher_decrypt;
1✔
1381

1382
         if(TEST_FFI_INIT(botan_cipher_init, (&cipher_encrypt, "AES-128/GCM", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
1✔
1383
            std::array<char, 18> namebuf{};
1✔
1384
            size_t name_len = 15;
1✔
1385
            TEST_FFI_FAIL("output buffer too short", botan_cipher_name, (cipher_encrypt, namebuf.data(), &name_len));
2✔
1386
            result.test_eq("name len", name_len, 16);
1✔
1387

1388
            name_len = namebuf.size();
1✔
1389
            if(TEST_FFI_OK(botan_cipher_name, (cipher_encrypt, namebuf.data(), &name_len))) {
1✔
1390
               result.test_eq("name len", name_len, 16);
1✔
1391
               result.test_eq("name", namebuf.data(), "AES-128/GCM(16)");
2✔
1392
            }
1393

1394
            size_t min_keylen = 0;
1✔
1395
            size_t max_keylen = 0;
1✔
1396
            size_t nonce_len = 0;
1✔
1397
            size_t tag_len = 0;
1✔
1398
            size_t update_granularity = 0;
1✔
1399
            size_t ideal_granularity = 0;
1✔
1400

1401
            TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_encrypt, &update_granularity));
1✔
1402
            TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_encrypt, &ideal_granularity));
1✔
1403

1404
            result.test_eq(
1✔
1405
               "ideal granularity is a multiple of update granularity", ideal_granularity % update_granularity, 0);
1406

1407
            TEST_FFI_OK(botan_cipher_query_keylen, (cipher_encrypt, &min_keylen, &max_keylen));
1✔
1408
            result.test_int_eq(min_keylen, 16, "Min key length");
1✔
1409
            result.test_int_eq(max_keylen, 16, "Max key length");
1✔
1410

1411
            TEST_FFI_OK(botan_cipher_get_default_nonce_length, (cipher_encrypt, &nonce_len));
1✔
1412
            result.test_int_eq(nonce_len, 12, "Expected default GCM nonce length");
1✔
1413

1414
            TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_encrypt, &tag_len));
1✔
1415
            result.test_int_eq(tag_len, 16, "Expected GCM tag length");
1✔
1416

1417
            TEST_FFI_RC(1, botan_cipher_is_authenticated, (cipher_encrypt));
1✔
1418

1419
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 12));
1✔
1420
            // GCM accepts any nonce size except zero
1421
            TEST_FFI_RC(0, botan_cipher_valid_nonce_length, (cipher_encrypt, 0));
1✔
1422
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 1));
1✔
1423
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 100009));
1✔
1424

1425
            // NIST test vector
1426
            const std::vector<uint8_t> plaintext = Botan::hex_decode(
1✔
1427
               "D9313225F88406E5A55909C5AFF5269A86A7A9531534F7DA2E4C303D8A318A721C3C0C95956809532FCF0E2449A6B525B16AEDF5AA0DE657BA637B39");
1✔
1428

1429
            const std::vector<uint8_t> symkey = Botan::hex_decode("FEFFE9928665731C6D6A8F9467308308");
1✔
1430
            const std::vector<uint8_t> nonce = Botan::hex_decode("CAFEBABEFACEDBADDECAF888");
1✔
1431
            const std::vector<uint8_t> exp_ciphertext = Botan::hex_decode(
1✔
1432
               "42831EC2217774244B7221B784D0D49CE3AA212F2C02A4E035C17E2329ACA12E21D514B25466931C7D8F6A5AAC84AA051BA30B396A0AAC973D58E0915BC94FBC3221A5DB94FAE95AE7121A47");
1✔
1433
            const std::vector<uint8_t> aad = Botan::hex_decode("FEEDFACEDEADBEEFFEEDFACEDEADBEEFABADDAD2");
1✔
1434

1435
            std::vector<uint8_t> ciphertext(tag_len + plaintext.size());
1✔
1436

1437
            size_t output_written = 0;
1✔
1438
            size_t input_consumed = 0;
1✔
1439

1440
            // Test that after clear or final the object can be reused
1441
            for(size_t r = 0; r != 2; ++r) {
3✔
1442
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2✔
1443

1444
               // First use a nonce of the AAD, and ensure reset works
1445
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, aad.data(), aad.size()));
2✔
1446
               TEST_FFI_OK(botan_cipher_reset, (cipher_encrypt));
2✔
1447

1448
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
1449
               TEST_FFI_OK(botan_cipher_update,
2✔
1450
                           (cipher_encrypt,
1451
                            0,
1452
                            ciphertext.data(),
1453
                            ciphertext.size(),
1454
                            &output_written,
1455
                            plaintext.data(),
1456
                            plaintext.size(),
1457
                            &input_consumed));
1458
               TEST_FFI_OK(botan_cipher_clear, (cipher_encrypt));
2✔
1459

1460
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2✔
1461
               TEST_FFI_OK(botan_cipher_set_associated_data, (cipher_encrypt, aad.data(), aad.size()));
2✔
1462
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
1463
               TEST_FFI_OK(botan_cipher_update,
2✔
1464
                           (cipher_encrypt,
1465
                            BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1466
                            ciphertext.data(),
1467
                            ciphertext.size(),
1468
                            &output_written,
1469
                            plaintext.data(),
1470
                            plaintext.size(),
1471
                            &input_consumed));
1472

1473
               ciphertext.resize(output_written);
2✔
1474
               result.test_eq("AES/GCM ciphertext", ciphertext, exp_ciphertext);
4✔
1475

1476
               if(TEST_FFI_OK(botan_cipher_init, (&cipher_decrypt, "AES-128/GCM", BOTAN_CIPHER_INIT_FLAG_DECRYPT))) {
2✔
1477
                  std::vector<uint8_t> decrypted(plaintext.size());
2✔
1478

1479
                  TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_decrypt, &update_granularity));
2✔
1480
                  TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_decrypt, &ideal_granularity));
2✔
1481

1482
                  result.test_eq("ideal granularity is a multiple of update granularity (decrypt)",
2✔
1483
                                 ideal_granularity % update_granularity,
1484
                                 0);
1485

1486
                  TEST_FFI_OK(botan_cipher_set_key, (cipher_decrypt, symkey.data(), symkey.size()));
2✔
1487
                  TEST_FFI_OK(botan_cipher_set_associated_data, (cipher_decrypt, aad.data(), aad.size()));
2✔
1488
                  TEST_FFI_OK(botan_cipher_start, (cipher_decrypt, nonce.data(), nonce.size()));
2✔
1489
                  TEST_FFI_OK(botan_cipher_update,
2✔
1490
                              (cipher_decrypt,
1491
                               BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1492
                               decrypted.data(),
1493
                               decrypted.size(),
1494
                               &output_written,
1495
                               ciphertext.data(),
1496
                               ciphertext.size(),
1497
                               &input_consumed));
1498

1499
                  result.test_int_eq(input_consumed, ciphertext.size(), "All input consumed");
2✔
1500
                  result.test_int_eq(output_written, decrypted.size(), "Expected output size produced");
2✔
1501
                  result.test_eq("AES/GCM plaintext", decrypted, plaintext);
4✔
1502

1503
                  TEST_FFI_OK(botan_cipher_destroy, (cipher_decrypt));
4✔
1504
               }
2✔
1505
            }
1506

1507
            TEST_FFI_OK(botan_cipher_destroy, (cipher_encrypt));
2✔
1508
         }
6✔
1509
      }
1✔
1510
};
1511

1512
class FFI_ChaCha20Poly1305_Test final : public FFI_Test {
1✔
1513
   public:
1514
      std::string name() const override { return "FFI ChaCha20Poly1305"; }
1✔
1515

1516
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1517
         botan_cipher_t cipher_encrypt;
1✔
1518
         botan_cipher_t cipher_decrypt;
1✔
1519

1520
         if(TEST_FFI_INIT(botan_cipher_init, (&cipher_encrypt, "ChaCha20Poly1305", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
1✔
1521
            std::array<char, 17> namebuf{};
1✔
1522
            size_t name_len = 15;
1✔
1523
            TEST_FFI_FAIL("output buffer too short", botan_cipher_name, (cipher_encrypt, namebuf.data(), &name_len));
2✔
1524
            result.test_eq("name len", name_len, 17);
1✔
1525

1526
            name_len = namebuf.size();
1✔
1527
            if(TEST_FFI_OK(botan_cipher_name, (cipher_encrypt, namebuf.data(), &name_len))) {
1✔
1528
               result.test_eq("name len", name_len, 17);
1✔
1529
               result.test_eq("name", std::string(namebuf.data()), "ChaCha20Poly1305");
2✔
1530
            }
1531

1532
            size_t min_keylen = 0;
1✔
1533
            size_t max_keylen = 0;
1✔
1534
            size_t nonce_len = 0;
1✔
1535
            size_t tag_len = 0;
1✔
1536
            size_t update_granularity = 0;
1✔
1537
            size_t ideal_granularity = 0;
1✔
1538

1539
            TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_encrypt, &update_granularity));
1✔
1540
            TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_encrypt, &ideal_granularity));
1✔
1541

1542
            result.test_eq(
1✔
1543
               "ideal granularity is a multiple of update granularity", ideal_granularity % update_granularity, 0);
1544

1545
            TEST_FFI_OK(botan_cipher_query_keylen, (cipher_encrypt, &min_keylen, &max_keylen));
1✔
1546
            result.test_int_eq(min_keylen, 32, "Min key length");
1✔
1547
            result.test_int_eq(max_keylen, 32, "Max key length");
1✔
1548

1549
            TEST_FFI_OK(botan_cipher_get_default_nonce_length, (cipher_encrypt, &nonce_len));
1✔
1550
            result.test_int_eq(nonce_len, 12, "Expected default ChaCha20Poly1305 nonce length");
1✔
1551

1552
            TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_encrypt, &tag_len));
1✔
1553
            result.test_int_eq(tag_len, 16, "Expected Chacha20Poly1305 tag length");
1✔
1554

1555
            TEST_FFI_RC(1, botan_cipher_is_authenticated, (cipher_encrypt));
1✔
1556

1557
            // From RFC 7539
1558
            const std::vector<uint8_t> plaintext = Botan::hex_decode(
1✔
1559
               "4C616469657320616E642047656E746C656D656E206F662074686520636C617373206F66202739393A204966204920636F756C64206F6666657220796F75206F6E6C79206F6E652074697020666F7220746865206675747572652C2073756E73637265656E20776F756C642062652069742E");
1✔
1560
            const std::vector<uint8_t> symkey =
1✔
1561
               Botan::hex_decode("808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9F");
1✔
1562
            const std::vector<uint8_t> nonce = Botan::hex_decode("070000004041424344454647");
1✔
1563
            const std::vector<uint8_t> exp_ciphertext = Botan::hex_decode(
1✔
1564
               "D31A8D34648E60DB7B86AFBC53EF7EC2A4ADED51296E08FEA9E2B5A736EE62D63DBEA45E8CA9671282FAFB69DA92728B1A71DE0A9E060B2905D6A5B67ECD3B3692DDBD7F2D778B8C9803AEE328091B58FAB324E4FAD675945585808B4831D7BC3FF4DEF08E4B7A9DE576D26586CEC64B61161AE10B594F09E26A7E902ECBD0600691");
1✔
1565
            const std::vector<uint8_t> aad = Botan::hex_decode("50515253C0C1C2C3C4C5C6C7");
1✔
1566

1567
            std::vector<uint8_t> ciphertext(tag_len + plaintext.size());
1✔
1568

1569
            size_t output_written = 0;
1✔
1570
            size_t input_consumed = 0;
1✔
1571

1572
            // Test that after clear or final the object can be reused
1573
            for(size_t r = 0; r != 2; ++r) {
3✔
1574
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2✔
1575

1576
               // First use a nonce of the AAD, and ensure reset works
1577
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, aad.data(), aad.size()));
2✔
1578
               TEST_FFI_OK(botan_cipher_reset, (cipher_encrypt));
2✔
1579

1580
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
1581
               TEST_FFI_OK(botan_cipher_update,
2✔
1582
                           (cipher_encrypt,
1583
                            0,
1584
                            ciphertext.data(),
1585
                            ciphertext.size(),
1586
                            &output_written,
1587
                            plaintext.data(),
1588
                            plaintext.size(),
1589
                            &input_consumed));
1590
               TEST_FFI_OK(botan_cipher_clear, (cipher_encrypt));
2✔
1591

1592
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2✔
1593
               TEST_FFI_OK(botan_cipher_set_associated_data, (cipher_encrypt, aad.data(), aad.size()));
2✔
1594
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
1595
               TEST_FFI_OK(botan_cipher_update,
2✔
1596
                           (cipher_encrypt,
1597
                            BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1598
                            ciphertext.data(),
1599
                            ciphertext.size(),
1600
                            &output_written,
1601
                            plaintext.data(),
1602
                            plaintext.size(),
1603
                            &input_consumed));
1604

1605
               ciphertext.resize(output_written);
2✔
1606
               result.test_eq("AES/GCM ciphertext", ciphertext, exp_ciphertext);
4✔
1607

1608
               if(TEST_FFI_OK(botan_cipher_init,
2✔
1609
                              (&cipher_decrypt, "ChaCha20Poly1305", BOTAN_CIPHER_INIT_FLAG_DECRYPT))) {
1610
                  std::vector<uint8_t> decrypted(plaintext.size());
2✔
1611

1612
                  TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_decrypt, &update_granularity));
2✔
1613
                  TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_decrypt, &ideal_granularity));
2✔
1614

1615
                  result.test_eq("ideal granularity is a multiple of update granularity (decrypt)",
2✔
1616
                                 ideal_granularity % update_granularity,
1617
                                 0);
1618

1619
                  TEST_FFI_OK(botan_cipher_set_key, (cipher_decrypt, symkey.data(), symkey.size()));
2✔
1620
                  TEST_FFI_OK(botan_cipher_set_associated_data, (cipher_decrypt, aad.data(), aad.size()));
2✔
1621
                  TEST_FFI_OK(botan_cipher_start, (cipher_decrypt, nonce.data(), nonce.size()));
2✔
1622
                  TEST_FFI_OK(botan_cipher_update,
2✔
1623
                              (cipher_decrypt,
1624
                               BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1625
                               decrypted.data(),
1626
                               decrypted.size(),
1627
                               &output_written,
1628
                               ciphertext.data(),
1629
                               ciphertext.size(),
1630
                               &input_consumed));
1631

1632
                  result.test_int_eq(input_consumed, ciphertext.size(), "All input consumed");
2✔
1633
                  result.test_int_eq(output_written, decrypted.size(), "Expected output size produced");
2✔
1634
                  result.test_eq("AES/GCM plaintext", decrypted, plaintext);
4✔
1635

1636
                  TEST_FFI_OK(botan_cipher_destroy, (cipher_decrypt));
4✔
1637
               }
2✔
1638
            }
1639

1640
            TEST_FFI_OK(botan_cipher_destroy, (cipher_encrypt));
2✔
1641
         }
6✔
1642
      }
1✔
1643
};
1644

1645
class FFI_EAX_Test final : public FFI_Test {
1✔
1646
   public:
1647
      std::string name() const override { return "FFI EAX"; }
1✔
1648

1649
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1650
         botan_cipher_t cipher_encrypt;
1✔
1651
         botan_cipher_t cipher_decrypt;
1✔
1652

1653
         if(TEST_FFI_INIT(botan_cipher_init, (&cipher_encrypt, "AES-128/EAX", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
1✔
1654
            size_t min_keylen = 0;
1✔
1655
            size_t max_keylen = 0;
1✔
1656
            size_t mod_keylen = 0;
1✔
1657
            size_t nonce_len = 0;
1✔
1658
            size_t tag_len = 0;
1✔
1659
            size_t update_granularity = 0;
1✔
1660
            size_t ideal_granularity = 0;
1✔
1661

1662
            TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_encrypt, &update_granularity));
1✔
1663
            TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_encrypt, &ideal_granularity));
1✔
1664

1665
            result.test_eq(
1✔
1666
               "ideal granularity is a multiple of update granularity", ideal_granularity % update_granularity, 0);
1667

1668
            TEST_FFI_OK(botan_cipher_query_keylen, (cipher_encrypt, &min_keylen, &max_keylen));
1✔
1669
            result.test_int_eq(min_keylen, 16, "Min key length");
1✔
1670
            result.test_int_eq(max_keylen, 16, "Max key length");
1✔
1671

1672
            TEST_FFI_OK(botan_cipher_get_keyspec, (cipher_encrypt, &min_keylen, &max_keylen, &mod_keylen));
1✔
1673
            result.test_int_eq(min_keylen, 16, "Min key length");
1✔
1674
            result.test_int_eq(max_keylen, 16, "Max key length");
1✔
1675
            result.test_int_eq(mod_keylen, 1, "Mod key length");
1✔
1676

1677
            TEST_FFI_OK(botan_cipher_get_default_nonce_length, (cipher_encrypt, &nonce_len));
1✔
1678
            result.test_int_eq(nonce_len, 12, "Expected default EAX nonce length");
1✔
1679

1680
            TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_encrypt, &tag_len));
1✔
1681
            result.test_int_eq(tag_len, 16, "Expected EAX tag length");
1✔
1682

1683
            TEST_FFI_RC(1, botan_cipher_is_authenticated, (cipher_encrypt));
1✔
1684

1685
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 12));
1✔
1686
            // EAX accepts any nonce size...
1687
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 0));
1✔
1688

1689
            const std::vector<uint8_t> plaintext =
1✔
1690
               Botan::hex_decode("0000000000000000000000000000000011111111111111111111111111111111");
1✔
1691
            const std::vector<uint8_t> symkey = Botan::hex_decode("000102030405060708090a0b0c0d0e0f");
1✔
1692
            const std::vector<uint8_t> nonce = Botan::hex_decode("3c8cc2970a008f75cc5beae2847258c2");
1✔
1693
            const std::vector<uint8_t> exp_ciphertext = Botan::hex_decode(
1✔
1694
               "3c441f32ce07822364d7a2990e50bb13d7b02a26969e4a937e5e9073b0d9c968db90bdb3da3d00afd0fc6a83551da95e");
1✔
1695

1696
            std::vector<uint8_t> ciphertext(tag_len + plaintext.size());
1✔
1697

1698
            size_t output_written = 0;
1✔
1699
            size_t input_consumed = 0;
1✔
1700

1701
            // Test that after clear or final the object can be reused
1702
            for(size_t r = 0; r != 2; ++r) {
3✔
1703
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2✔
1704
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
1705
               TEST_FFI_OK(botan_cipher_update,
2✔
1706
                           (cipher_encrypt,
1707
                            0,
1708
                            ciphertext.data(),
1709
                            ciphertext.size(),
1710
                            &output_written,
1711
                            plaintext.data(),
1712
                            plaintext.size(),
1713
                            &input_consumed));
1714
               TEST_FFI_OK(botan_cipher_clear, (cipher_encrypt));
2✔
1715

1716
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2✔
1717
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
1718
               TEST_FFI_OK(botan_cipher_update,
2✔
1719
                           (cipher_encrypt,
1720
                            BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1721
                            ciphertext.data(),
1722
                            ciphertext.size(),
1723
                            &output_written,
1724
                            plaintext.data(),
1725
                            plaintext.size(),
1726
                            &input_consumed));
1727

1728
               ciphertext.resize(output_written);
2✔
1729
               result.test_eq("AES/EAX ciphertext", ciphertext, exp_ciphertext);
4✔
1730

1731
               if(TEST_FFI_OK(botan_cipher_init, (&cipher_decrypt, "AES-128/EAX", BOTAN_CIPHER_INIT_FLAG_DECRYPT))) {
2✔
1732
                  std::vector<uint8_t> decrypted(plaintext.size());
2✔
1733

1734
                  TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_decrypt, &update_granularity));
2✔
1735
                  TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_decrypt, &ideal_granularity));
2✔
1736

1737
                  result.test_eq("ideal granularity is a multiple of update granularity (decrypt)",
2✔
1738
                                 ideal_granularity % update_granularity,
1739
                                 0);
1740

1741
                  TEST_FFI_OK(botan_cipher_set_key, (cipher_decrypt, symkey.data(), symkey.size()));
2✔
1742
                  TEST_FFI_OK(botan_cipher_start, (cipher_decrypt, nonce.data(), nonce.size()));
2✔
1743
                  TEST_FFI_OK(botan_cipher_update,
2✔
1744
                              (cipher_decrypt,
1745
                               BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1746
                               decrypted.data(),
1747
                               decrypted.size(),
1748
                               &output_written,
1749
                               ciphertext.data(),
1750
                               ciphertext.size(),
1751
                               &input_consumed));
1752

1753
                  result.test_int_eq(input_consumed, ciphertext.size(), "All input consumed");
2✔
1754
                  result.test_int_eq(output_written, decrypted.size(), "Expected output size produced");
2✔
1755
                  result.test_eq("AES/EAX plaintext", decrypted, plaintext);
4✔
1756

1757
                  TEST_FFI_OK(botan_cipher_destroy, (cipher_decrypt));
4✔
1758
               }
2✔
1759
            }
1760

1761
            TEST_FFI_OK(botan_cipher_destroy, (cipher_encrypt));
2✔
1762
         }
5✔
1763
      }
1✔
1764
};
1765

1766
class FFI_AEAD_Test final : public FFI_Test {
1✔
1767
   public:
1768
      std::string name() const override { return "FFI AEAD"; }
1✔
1769

1770
      void ffi_test(Test::Result& merged_result, botan_rng_t rng) override {
1✔
1771
         botan_cipher_t cipher_encrypt;
1✔
1772
         botan_cipher_t cipher_decrypt;
1✔
1773

1774
         const std::array<std::string, 5> aeads = {
1✔
1775
            "AES-128/GCM", "ChaCha20Poly1305", "AES-128/EAX", "AES-256/SIV", "AES-128/CCM"};
1✔
1776

1777
         for(const std::string& aead : aeads) {
6✔
1778
            Test::Result result(Botan::fmt("AEAD {}", aead));
5✔
1779

1780
            if(!TEST_FFI_INIT(botan_cipher_init, (&cipher_encrypt, aead.c_str(), BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
5✔
1781
               continue;
×
1782
            }
1783

1784
            if(botan_cipher_is_authenticated(cipher_encrypt) == 0) {
5✔
1785
               result.test_failure("Cipher " + aead + " claims is not authenticated");
×
1786
               botan_cipher_destroy(cipher_encrypt);
×
1787
               continue;
×
1788
            }
1789

1790
            size_t min_keylen = 0;
5✔
1791
            size_t max_keylen = 0;
5✔
1792
            size_t update_granularity = 0;
5✔
1793
            size_t ideal_granularity = 0;
5✔
1794
            size_t noncelen = 0;
5✔
1795
            size_t taglen = 0;
5✔
1796
            constexpr size_t pt_multiplier = 5;
5✔
1797
            TEST_FFI_OK(botan_cipher_query_keylen, (cipher_encrypt, &min_keylen, &max_keylen));
5✔
1798
            TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_encrypt, &update_granularity));
5✔
1799
            TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_encrypt, &ideal_granularity));
5✔
1800
            TEST_FFI_OK(botan_cipher_get_default_nonce_length, (cipher_encrypt, &noncelen));
5✔
1801
            TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_encrypt, &taglen));
5✔
1802

1803
            result.test_eq(
5✔
1804
               "ideal granularity is a multiple of update granularity", ideal_granularity % update_granularity, 0);
1805

1806
            std::vector<uint8_t> key(max_keylen);
5✔
1807
            TEST_FFI_OK(botan_rng_get, (rng, key.data(), key.size()));
5✔
1808
            TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, key.data(), key.size()));
5✔
1809

1810
            std::vector<uint8_t> nonce(noncelen);
5✔
1811
            TEST_FFI_OK(botan_rng_get, (rng, nonce.data(), nonce.size()));
5✔
1812
            TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
5✔
1813

1814
            std::vector<uint8_t> plaintext(ideal_granularity * pt_multiplier);
5✔
1815
            std::vector<uint8_t> ciphertext(ideal_granularity * pt_multiplier + taglen);
5✔
1816
            TEST_FFI_OK(botan_rng_get, (rng, plaintext.data(), plaintext.size()));
5✔
1817

1818
            std::vector<uint8_t> dummy_buffer(1024);
5✔
1819
            TEST_FFI_OK(botan_rng_get, (rng, dummy_buffer.data(), dummy_buffer.size()));
5✔
1820
            std::vector<uint8_t> dummy_buffer_reference = dummy_buffer;
5✔
1821

1822
            const bool requires_entire_message = botan_cipher_requires_entire_message(cipher_encrypt) == 1;
5✔
1823
            result.test_eq(
5✔
1824
               "requires entire message", requires_entire_message, (aead == "AES-256/SIV" || aead == "AES-128/CCM"));
5✔
1825

1826
            std::span<const uint8_t> pt_slicer(plaintext);
5✔
1827
            std::span<uint8_t> ct_stuffer(ciphertext);
5✔
1828

1829
            // Process data that is explicitly a multiple of the ideal
1830
            // granularity and therefore should be aligned with the cipher's
1831
            // internal block size.
1832
            for(size_t i = 0; i < pt_multiplier; ++i) {
30✔
1833
               size_t output_written = 0;
25✔
1834
               size_t input_consumed = 0;
25✔
1835

1836
               auto pt_chunk = pt_slicer.first(ideal_granularity);
25✔
1837

1838
               // The existing implementation won't consume any bytes from the
1839
               // input if there is no space in the output buffer. Even when
1840
               // the cipher is a mode that won't produce any output until the
1841
               // entire message is processed. Hence, give it some dummy buffer.
1842
               BOTAN_ASSERT_NOMSG(dummy_buffer.size() > ideal_granularity);
25✔
1843
               auto ct_chunk = (requires_entire_message) ? std::span(dummy_buffer).first(ideal_granularity)
25✔
1844
                                                         : ct_stuffer.first(ideal_granularity);
1845

1846
               TEST_FFI_OK(botan_cipher_update,
25✔
1847
                           (cipher_encrypt,
1848
                            0 /* don't finalize */,
1849
                            ct_chunk.data(),
1850
                            ct_chunk.size(),
1851
                            &output_written,
1852
                            pt_chunk.data(),
1853
                            pt_chunk.size(),
1854
                            &input_consumed));
1855

1856
               result.test_gt("some input consumed", input_consumed, 0);
25✔
1857
               result.test_lte("at most, all input consumed", input_consumed, pt_chunk.size());
25✔
1858
               pt_slicer = pt_slicer.subspan(input_consumed);
25✔
1859

1860
               if(requires_entire_message) {
25✔
1861
                  result.test_eq("no output produced", output_written, 0);
20✔
1862
               } else {
1863
                  result.test_eq("all bytes produced", output_written, input_consumed);
15✔
1864
                  ct_stuffer = ct_stuffer.subspan(output_written);
15✔
1865
               }
1866
            }
1867

1868
            // Trying to pull a part of the authentication tag should fail,
1869
            // as we must consume the entire tag in a single invocation to
1870
            // botan_cipher_update().
1871
            size_t final_output_written = 42;
5✔
1872
            size_t final_input_consumed = 1337;
5✔
1873
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
5✔
1874
                        botan_cipher_update,
1875
                        (cipher_encrypt,
1876
                         BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1877
                         dummy_buffer.data(),
1878
                         3, /* not enough to hold any reasonable auth'n tag */
1879
                         &final_output_written,
1880
                         pt_slicer.data(),  // remaining bytes (typically 0)
1881
                         pt_slicer.size(),
1882
                         &final_input_consumed));
1883

1884
            const size_t expected_final_size = requires_entire_message ? ciphertext.size() : taglen + pt_slicer.size();
5✔
1885

1886
            result.test_eq("remaining bytes consumed in bogus final", final_input_consumed, pt_slicer.size());
5✔
1887
            result.test_eq("required buffer size is written in bogus final", final_output_written, expected_final_size);
5✔
1888

1889
            auto final_ct_chunk = ct_stuffer.first(expected_final_size);
5✔
1890

1891
            TEST_FFI_OK(botan_cipher_update,
5✔
1892
                        (cipher_encrypt,
1893
                         BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1894
                         final_ct_chunk.data(),
1895
                         final_ct_chunk.size(),
1896
                         &final_output_written,
1897
                         nullptr,  // no more input
1898
                         0,
1899
                         &final_input_consumed));
1900

1901
            result.test_eq("no bytes consumed in final", final_input_consumed, 0);
5✔
1902
            result.test_eq("final bytes written", final_output_written, expected_final_size);
5✔
1903
            result.test_eq("dummy buffer unchanged", dummy_buffer, dummy_buffer_reference);
10✔
1904

1905
            TEST_FFI_OK(botan_cipher_destroy, (cipher_encrypt));
5✔
1906

1907
            // ----------------------------------------------------------------
1908

1909
            TEST_FFI_INIT(botan_cipher_init, (&cipher_decrypt, aead.c_str(), BOTAN_CIPHER_INIT_FLAG_DECRYPT));
5✔
1910

1911
            TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_decrypt, &update_granularity));
5✔
1912
            TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_decrypt, &ideal_granularity));
5✔
1913

1914
            result.test_eq("ideal granularity is a multiple of update granularity (decrypt)",
5✔
1915
                           ideal_granularity % update_granularity,
1916
                           0);
1917

1918
            TEST_FFI_OK(botan_cipher_set_key, (cipher_decrypt, key.data(), key.size()));
5✔
1919
            TEST_FFI_OK(botan_cipher_start, (cipher_decrypt, nonce.data(), nonce.size()));
5✔
1920

1921
            std::vector<uint8_t> decrypted(plaintext.size());
5✔
1922

1923
            std::span<const uint8_t> ct_slicer(ciphertext);
5✔
1924
            std::span<uint8_t> pt_stuffer(decrypted);
5✔
1925

1926
            // Process data that is explicitly a multiple of the ideal
1927
            // granularity and therefore should be aligned with the cipher's
1928
            // internal block size.
1929
            for(size_t i = 0; i < pt_multiplier; ++i) {
30✔
1930
               size_t output_written = 42;
25✔
1931
               size_t input_consumed = 1337;
25✔
1932

1933
               auto ct_chunk = ct_slicer.first(ideal_granularity);
25✔
1934

1935
               // The existing implementation won't consume any bytes from the
1936
               // input if there is no space in the output buffer. Even when
1937
               // the cipher is a mode that won't produce any output until the
1938
               // entire message is processed. Hence, give it some dummy buffer.
1939
               auto pt_chunk = (requires_entire_message) ? std::span(dummy_buffer).first(ideal_granularity)
25✔
1940
                                                         : pt_stuffer.first(ideal_granularity);
1941

1942
               TEST_FFI_OK(botan_cipher_update,
25✔
1943
                           (cipher_decrypt,
1944
                            0 /* don't finalize */,
1945
                            pt_chunk.data(),
1946
                            pt_chunk.size(),
1947
                            &output_written,
1948
                            ct_chunk.data(),
1949
                            ct_chunk.size(),
1950
                            &input_consumed));
1951

1952
               result.test_gt("some input consumed", input_consumed, 0);
25✔
1953
               result.test_lte("at most, all input consumed", input_consumed, ct_chunk.size());
25✔
1954
               ct_slicer = ct_slicer.subspan(input_consumed);
25✔
1955

1956
               if(requires_entire_message) {
25✔
1957
                  result.test_eq("no output produced", output_written, 0);
20✔
1958
               } else {
1959
                  result.test_eq("all bytes produced", output_written, input_consumed);
15✔
1960
                  pt_stuffer = pt_stuffer.subspan(output_written);
15✔
1961
               }
1962
            }
1963

1964
            const size_t expected_final_size_dec = requires_entire_message ? plaintext.size() : pt_stuffer.size();
5✔
1965
            auto pt_chunk = pt_stuffer.first(expected_final_size_dec);
5✔
1966

1967
            size_t final_output_written_dec = 42;
5✔
1968
            size_t final_input_consumed_dec = 1337;
5✔
1969

1970
            TEST_FFI_OK(botan_cipher_update,
5✔
1971
                        (cipher_decrypt,
1972
                         BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1973
                         pt_chunk.data(),
1974
                         pt_chunk.size(),
1975
                         &final_output_written_dec,
1976
                         ct_slicer.data(),  // remaining bytes (typically 0)
1977
                         ct_slicer.size(),
1978
                         &final_input_consumed_dec));
1979

1980
            result.test_eq("remaining bytes consumed in final (decrypt)", final_input_consumed_dec, ct_slicer.size());
5✔
1981
            result.test_eq("bytes written in final (decrypt)", final_output_written_dec, expected_final_size_dec);
5✔
1982
            result.test_eq("dummy buffer unchanged", dummy_buffer, dummy_buffer_reference);
10✔
1983

1984
            result.test_eq("decrypted plaintext", decrypted, plaintext);
10✔
1985

1986
            TEST_FFI_OK(botan_cipher_destroy, (cipher_decrypt));
5✔
1987

1988
            merged_result.merge(result, true /* ignore names */);
5✔
1989
         }
35✔
1990
      }
1✔
1991
};
1992

1993
class FFI_StreamCipher_Test final : public FFI_Test {
1✔
1994
   public:
1995
      std::string name() const override { return "FFI stream ciphers"; }
1✔
1996

1997
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1998
         botan_cipher_t ctr;
1✔
1999

2000
         if(TEST_FFI_INIT(botan_cipher_init, (&ctr, "AES-128/CTR-BE", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
1✔
2001
            const std::vector<uint8_t> key = Botan::hex_decode("2B7E151628AED2A6ABF7158809CF4F3C");
1✔
2002
            const std::vector<uint8_t> nonce = Botan::hex_decode("F0F1F2F3F4F5F6F7F8F9FAFBFCFDFF");
1✔
2003
            const std::vector<uint8_t> pt = Botan::hex_decode(
1✔
2004
               "AE2D8A571E03AC9C9EB76FAC45AF8E5130C81C46A35CE411E5FBC1191A0A52EFF69F2445DF4F9B17AD2B417BE66C3710");
1✔
2005
            const std::vector<uint8_t> exp_ct = Botan::hex_decode(
1✔
2006
               "9806F66B7970FDFF8617187BB9FFFDFF5AE4DF3EDBD5D35E5B4F09020DB03EAB1E031DDA2FBE03D1792170A0F3009CEE");
1✔
2007

2008
            std::vector<uint8_t> ct(pt.size());
1✔
2009

2010
            size_t update_granularity = 0;
1✔
2011
            size_t ideal_granularity = 0;
1✔
2012

2013
            TEST_FFI_OK(botan_cipher_get_update_granularity, (ctr, &update_granularity));
1✔
2014
            TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (ctr, &ideal_granularity));
1✔
2015

2016
            result.test_eq(
1✔
2017
               "ideal granularity is a multiple of update granularity", ideal_granularity % update_granularity, 0);
2018

2019
            TEST_FFI_RC(0, botan_cipher_is_authenticated, (ctr));
1✔
2020

2021
            size_t input_consumed = 0;
1✔
2022
            size_t output_written = 0;
1✔
2023

2024
            TEST_FFI_OK(botan_cipher_set_key, (ctr, key.data(), key.size()));
1✔
2025
            TEST_FFI_OK(botan_cipher_start, (ctr, nonce.data(), nonce.size()));
1✔
2026

2027
            // Test partial updates...
2028
            TEST_FFI_OK(botan_cipher_update,
1✔
2029
                        (ctr, 0, ct.data(), ct.size(), &output_written, pt.data(), 5, &input_consumed));
2030

2031
            result.test_int_eq(output_written, 5, "Expected output written");
1✔
2032
            result.test_int_eq(input_consumed, 5, "Expected input consumed");
1✔
2033

2034
            TEST_FFI_OK(botan_cipher_update,
1✔
2035
                        (ctr, 0, &ct[5], ct.size() - 5, &output_written, &pt[5], pt.size() - 5, &input_consumed));
2036

2037
            result.test_int_eq(output_written, ct.size() - 5, "Expected output written");
1✔
2038
            result.test_int_eq(input_consumed, pt.size() - 5, "Expected input consumed");
1✔
2039
            result.test_eq("AES-128/CTR ciphertext", ct, exp_ct);
2✔
2040

2041
            TEST_FFI_OK(botan_cipher_destroy, (ctr));
2✔
2042
         }
5✔
2043
      }
1✔
2044
};
2045

2046
class FFI_HashFunction_Test final : public FFI_Test {
1✔
2047
   public:
2048
      std::string name() const override { return "FFI hash"; }
1✔
2049

2050
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
2051
         const char* input_str = "ABC";
1✔
2052

2053
         botan_hash_t hash;
1✔
2054
         TEST_FFI_FAIL("invalid hash name", botan_hash_init, (&hash, "SHA-255", 0));
2✔
2055
         TEST_FFI_FAIL("invalid flags", botan_hash_init, (&hash, "SHA-256", 1));
2✔
2056

2057
         if(TEST_FFI_INIT(botan_hash_init, (&hash, "SHA-256", 0))) {
1✔
2058
            std::array<char, 10> namebuf{};
1✔
2059
            size_t name_len = 7;
1✔
2060
            TEST_FFI_FAIL("output buffer too short", botan_hash_name, (hash, namebuf.data(), &name_len));
2✔
2061
            result.test_eq("name len", name_len, 8);
1✔
2062

2063
            name_len = namebuf.size();
1✔
2064
            if(TEST_FFI_OK(botan_hash_name, (hash, namebuf.data(), &name_len))) {
1✔
2065
               result.test_eq("name len", name_len, 8);
1✔
2066
               result.test_eq("name", namebuf.data(), "SHA-256");
2✔
2067
            }
2068

2069
            size_t block_size;
1✔
2070
            if(TEST_FFI_OK(botan_hash_block_size, (hash, &block_size))) {
1✔
2071
               result.test_eq("hash block size", block_size, 64);
2✔
2072
            }
2073

2074
            size_t output_len;
1✔
2075
            if(TEST_FFI_OK(botan_hash_output_length, (hash, &output_len))) {
1✔
2076
               result.test_eq("hash output length", output_len, 32);
1✔
2077

2078
               std::vector<uint8_t> outbuf(output_len);
1✔
2079

2080
               // Test that after clear or final the object can be reused
2081
               for(size_t r = 0; r != 2; ++r) {
3✔
2082
                  TEST_FFI_OK(botan_hash_update, (hash, reinterpret_cast<const uint8_t*>(input_str), 1));
2✔
2083
                  TEST_FFI_OK(botan_hash_clear, (hash));
2✔
2084

2085
                  TEST_FFI_OK(botan_hash_update,
2✔
2086
                              (hash, reinterpret_cast<const uint8_t*>(input_str), std::strlen(input_str)));
2087
                  TEST_FFI_OK(botan_hash_final, (hash, outbuf.data()));
2✔
2088

2089
                  result.test_eq(
4✔
2090
                     "SHA-256 output", outbuf, "B5D4045C3F466FA91FE2CC6ABE79232A1A57CDF104F7A26E716E0A1E2789DF78");
2091
               }
2092

2093
               // Test botan_hash_copy_state
2094
               const char* msg = "message digest";
1✔
2095
               const char* expected = "F7846F55CF23E14EEBEAB5B4E1550CAD5B509E3348FBC4EFA3A1413D393CB650";
1✔
2096
               TEST_FFI_OK(botan_hash_clear, (hash));
1✔
2097
               TEST_FFI_OK(botan_hash_update, (hash, reinterpret_cast<const uint8_t*>(&msg[0]), 1));
1✔
2098
               botan_hash_t fork;
1✔
2099
               if(TEST_FFI_OK(botan_hash_copy_state, (&fork, hash))) {
1✔
2100
                  TEST_FFI_OK(botan_hash_update,
1✔
2101
                              (fork, reinterpret_cast<const uint8_t*>(&msg[1]), std::strlen(msg) - 2));
2102

2103
                  TEST_FFI_OK(botan_hash_update,
1✔
2104
                              (hash, reinterpret_cast<const uint8_t*>(&msg[1]), std::strlen(msg) - 1));
2105
                  TEST_FFI_OK(botan_hash_final, (hash, outbuf.data()));
1✔
2106
                  result.test_eq("hashing split", outbuf, expected);
1✔
2107

2108
                  TEST_FFI_OK(botan_hash_update,
1✔
2109
                              (fork, reinterpret_cast<const uint8_t*>(&msg[std::strlen(msg) - 1]), 1));
2110
                  TEST_FFI_OK(botan_hash_final, (fork, outbuf.data()));
1✔
2111
                  result.test_eq("hashing split", outbuf, expected);
1✔
2112

2113
                  TEST_FFI_OK(botan_hash_destroy, (fork));
2✔
2114
               }
2115
            }
1✔
2116

2117
            TEST_FFI_OK(botan_hash_destroy, (hash));
2✔
2118
         }
2119
      }
1✔
2120
};
2121

2122
class FFI_MAC_Test final : public FFI_Test {
1✔
2123
   public:
2124
      std::string name() const override { return "FFI MAC"; }
1✔
2125

2126
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
2127
         const char* input_str = "ABC";
1✔
2128

2129
         // MAC test
2130
         botan_mac_t mac;
1✔
2131
         TEST_FFI_FAIL("bad flag", botan_mac_init, (&mac, "HMAC(SHA-256)", 1));
2✔
2132
         TEST_FFI_FAIL("bad name", botan_mac_init, (&mac, "HMAC(SHA-259)", 0));
2✔
2133

2134
         if(TEST_FFI_INIT(botan_mac_init, (&mac, "HMAC(SHA-256)", 0))) {
1✔
2135
            std::array<char, 16> namebuf{};
1✔
2136
            size_t name_len = 13;
1✔
2137
            TEST_FFI_FAIL("output buffer too short", botan_mac_name, (mac, namebuf.data(), &name_len));
2✔
2138
            result.test_eq("name len", name_len, 14);
1✔
2139

2140
            name_len = namebuf.size();
1✔
2141
            if(TEST_FFI_OK(botan_mac_name, (mac, namebuf.data(), &name_len))) {
1✔
2142
               result.test_eq("name len", name_len, 14);
1✔
2143
               result.test_eq("name", namebuf.data(), "HMAC(SHA-256)");
2✔
2144
            }
2145

2146
            size_t min_keylen = 0;
1✔
2147
            size_t max_keylen = 0;
1✔
2148
            size_t mod_keylen = 0;
1✔
2149
            TEST_FFI_RC(0, botan_mac_get_keyspec, (mac, nullptr, nullptr, nullptr));
1✔
2150
            TEST_FFI_RC(0, botan_mac_get_keyspec, (mac, &min_keylen, nullptr, nullptr));
1✔
2151
            TEST_FFI_RC(0, botan_mac_get_keyspec, (mac, nullptr, &max_keylen, nullptr));
1✔
2152
            TEST_FFI_RC(0, botan_mac_get_keyspec, (mac, nullptr, nullptr, &mod_keylen));
1✔
2153

2154
            result.test_eq("Expected min keylen", min_keylen, 0);
1✔
2155
            result.test_eq("Expected max keylen", max_keylen, 8192);
1✔
2156
            result.test_eq("Expected mod keylen", mod_keylen, 1);
1✔
2157

2158
            size_t output_len;
1✔
2159
            if(TEST_FFI_OK(botan_mac_output_length, (mac, &output_len))) {
1✔
2160
               result.test_eq("MAC output length", output_len, 32);
1✔
2161

2162
               const uint8_t mac_key[] = {0xAA, 0xBB, 0xCC, 0xDD};
1✔
2163
               std::vector<uint8_t> outbuf(output_len);
1✔
2164

2165
               // Test that after clear or final the object can be reused
2166
               for(size_t r = 0; r != 2; ++r) {
3✔
2167
                  TEST_FFI_OK(botan_mac_set_key, (mac, mac_key, sizeof(mac_key)));
2✔
2168
                  TEST_FFI_OK(botan_mac_update,
2✔
2169
                              (mac, reinterpret_cast<const uint8_t*>(input_str), std::strlen(input_str)));
2170
                  TEST_FFI_OK(botan_mac_clear, (mac));
2✔
2171

2172
                  TEST_FFI_OK(botan_mac_set_key, (mac, mac_key, sizeof(mac_key)));
2✔
2173
                  TEST_FFI_OK(botan_mac_update,
2✔
2174
                              (mac, reinterpret_cast<const uint8_t*>(input_str), std::strlen(input_str)));
2175
                  TEST_FFI_OK(botan_mac_final, (mac, outbuf.data()));
2✔
2176

2177
                  result.test_eq(
4✔
2178
                     "HMAC output", outbuf, "1A82EEA984BC4A7285617CC0D05F1FE1D6C96675924A81BC965EE8FF7B0697A7");
2179
               }
2180
            }
1✔
2181

2182
            TEST_FFI_OK(botan_mac_destroy, (mac));
2✔
2183
         }
2184
      }
1✔
2185
};
2186

2187
class FFI_Scrypt_Test final : public FFI_Test {
1✔
2188
   public:
2189
      std::string name() const override { return "FFI Scrypt"; }
1✔
2190

2191
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
2192
         std::vector<uint8_t> output(24);
1✔
2193
         const uint8_t salt[8] = {0};
1✔
2194
         const char* pass = "password";
1✔
2195

2196
         if(TEST_FFI_INIT(botan_scrypt, (output.data(), output.size(), pass, salt, sizeof(salt), 8, 1, 1))) {
1✔
2197
            result.test_eq("scrypt output", output, "4B9B888D695288E002CC4F9D90808A4D296A45CE4471AFBB");
1✔
2198

2199
            size_t N;
1✔
2200
            size_t r;
1✔
2201
            size_t p;
1✔
2202
            TEST_FFI_OK(botan_pwdhash_timed,
1✔
2203
                        ("Scrypt", 50, &r, &p, &N, output.data(), output.size(), "bunny", 5, salt, sizeof(salt)));
2204

2205
            std::vector<uint8_t> cmp(output.size());
1✔
2206

2207
            TEST_FFI_OK(botan_pwdhash, ("Scrypt", N, r, p, cmp.data(), cmp.size(), "bunny", 5, salt, sizeof(salt)));
1✔
2208
            result.test_eq("recomputed scrypt", cmp, output);
2✔
2209
         }
1✔
2210
      }
1✔
2211
};
2212

2213
class FFI_KDF_Test final : public FFI_Test {
1✔
2214
   public:
2215
      std::string name() const override { return "FFI KDF"; }
1✔
2216

2217
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
2218
         std::vector<uint8_t> outbuf;
1✔
2219

2220
         const std::string passphrase = "ltexmfeyylmlbrsyikaw";
1✔
2221

2222
         const std::vector<uint8_t> pbkdf_salt = Botan::hex_decode("ED1F39A0A7F3889AAF7E60743B3BC1CC2C738E60");
1✔
2223
         const size_t pbkdf_out_len = 10;
1✔
2224
         const size_t pbkdf_iterations = 1000;
1✔
2225

2226
         outbuf.resize(pbkdf_out_len);
1✔
2227

2228
         if(TEST_FFI_INIT(botan_pbkdf,
1✔
2229
                          ("PBKDF2(SHA-1)",
2230
                           outbuf.data(),
2231
                           outbuf.size(),
2232
                           passphrase.c_str(),
2233
                           pbkdf_salt.data(),
2234
                           pbkdf_salt.size(),
2235
                           pbkdf_iterations))) {
2236
            result.test_eq("PBKDF output", outbuf, "027AFADD48F4BE8DCC4F");
1✔
2237

2238
            size_t iters_10ms;
1✔
2239
            size_t iters_100ms;
1✔
2240

2241
            TEST_FFI_OK(botan_pbkdf_timed,
1✔
2242
                        ("PBKDF2(SHA-1)",
2243
                         outbuf.data(),
2244
                         outbuf.size(),
2245
                         passphrase.c_str(),
2246
                         pbkdf_salt.data(),
2247
                         pbkdf_salt.size(),
2248
                         10,
2249
                         &iters_10ms));
2250
            TEST_FFI_OK(botan_pbkdf_timed,
1✔
2251
                        ("PBKDF2(SHA-1)",
2252
                         outbuf.data(),
2253
                         outbuf.size(),
2254
                         passphrase.c_str(),
2255
                         pbkdf_salt.data(),
2256
                         pbkdf_salt.size(),
2257
                         100,
2258
                         &iters_100ms));
2259

2260
            result.test_note("PBKDF timed 10 ms " + std::to_string(iters_10ms) + " iterations " + "100 ms " +
5✔
2261
                             std::to_string(iters_100ms) + " iterations");
4✔
2262
         }
2263

2264
         const std::vector<uint8_t> kdf_secret = Botan::hex_decode("92167440112E");
1✔
2265
         const std::vector<uint8_t> kdf_salt = Botan::hex_decode("45A9BEDED69163123D0348F5185F61ABFB1BF18D6AEA454F");
1✔
2266
         const size_t kdf_out_len = 18;
1✔
2267
         outbuf.resize(kdf_out_len);
1✔
2268

2269
         if(TEST_FFI_INIT(botan_kdf,
1✔
2270
                          ("KDF2(SHA-1)",
2271
                           outbuf.data(),
2272
                           outbuf.size(),
2273
                           kdf_secret.data(),
2274
                           kdf_secret.size(),
2275
                           kdf_salt.data(),
2276
                           kdf_salt.size(),
2277
                           nullptr,
2278
                           0))) {
2279
            result.test_eq("KDF output", outbuf, "3A5DC9AA1C872B4744515AC2702D6396FC2A");
2✔
2280
         }
2281

2282
         size_t out_len = 64;
1✔
2283
         std::string outstr;
1✔
2284
         outstr.resize(out_len);
1✔
2285

2286
         const int rc =
1✔
2287
            botan_bcrypt_generate(reinterpret_cast<uint8_t*>(outstr.data()), &out_len, passphrase.c_str(), rng, 4, 0);
1✔
2288

2289
         if(rc == 0) {
1✔
2290
            result.test_eq("bcrypt output size", out_len, 61);
1✔
2291

2292
            TEST_FFI_OK(botan_bcrypt_is_valid, (passphrase.c_str(), outstr.data()));
1✔
2293
            TEST_FFI_FAIL("bad password", botan_bcrypt_is_valid, ("nope", outstr.data()));
2✔
2294
         }
2295
      }
5✔
2296
};
2297

2298
class FFI_Blockcipher_Test final : public FFI_Test {
1✔
2299
   public:
2300
      std::string name() const override { return "FFI block ciphers"; }
1✔
2301

2302
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
2303
         botan_block_cipher_t cipher;
1✔
2304

2305
         if(TEST_FFI_INIT(botan_block_cipher_init, (&cipher, "AES-128"))) {
1✔
2306
            std::array<char, 10> namebuf{};
1✔
2307
            size_t name_len = 7;
1✔
2308
            TEST_FFI_FAIL("output buffer too short", botan_block_cipher_name, (cipher, namebuf.data(), &name_len));
2✔
2309
            result.test_eq("name len", name_len, 8);
1✔
2310

2311
            name_len = namebuf.size();
1✔
2312
            if(TEST_FFI_OK(botan_block_cipher_name, (cipher, namebuf.data(), &name_len))) {
1✔
2313
               result.test_eq("name len", name_len, 8);
1✔
2314
               result.test_eq("name", namebuf.data(), "AES-128");
2✔
2315
            }
2316

2317
            const std::vector<uint8_t> zero16(16, 0);
1✔
2318
            std::vector<uint8_t> block(16, 0);
1✔
2319

2320
            TEST_FFI_OK(botan_block_cipher_clear, (cipher));
1✔
2321

2322
            TEST_FFI_RC(
1✔
2323
               BOTAN_FFI_ERROR_KEY_NOT_SET, botan_block_cipher_encrypt_blocks, (cipher, block.data(), block.data(), 1));
2324
            TEST_FFI_RC(
1✔
2325
               BOTAN_FFI_ERROR_KEY_NOT_SET, botan_block_cipher_decrypt_blocks, (cipher, block.data(), block.data(), 1));
2326

2327
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_block_cipher_encrypt_blocks, (cipher, nullptr, nullptr, 0));
1✔
2328
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_block_cipher_decrypt_blocks, (cipher, nullptr, nullptr, 0));
1✔
2329

2330
            TEST_FFI_RC(16, botan_block_cipher_block_size, (cipher));
1✔
2331

2332
            size_t min_keylen = 0;
1✔
2333
            size_t max_keylen = 0;
1✔
2334
            size_t mod_keylen = 0;
1✔
2335
            TEST_FFI_RC(0, botan_block_cipher_get_keyspec, (cipher, nullptr, nullptr, nullptr));
1✔
2336
            TEST_FFI_RC(0, botan_block_cipher_get_keyspec, (cipher, &min_keylen, nullptr, nullptr));
1✔
2337
            TEST_FFI_RC(0, botan_block_cipher_get_keyspec, (cipher, nullptr, &max_keylen, nullptr));
1✔
2338
            TEST_FFI_RC(0, botan_block_cipher_get_keyspec, (cipher, nullptr, nullptr, &mod_keylen));
1✔
2339

2340
            result.test_eq("Expected min keylen", min_keylen, 16);
1✔
2341
            result.test_eq("Expected max keylen", max_keylen, 16);
1✔
2342
            result.test_eq("Expected mod keylen", mod_keylen, 1);
1✔
2343

2344
            TEST_FFI_OK(botan_block_cipher_set_key, (cipher, zero16.data(), zero16.size()));
1✔
2345

2346
            TEST_FFI_OK(botan_block_cipher_encrypt_blocks, (cipher, block.data(), block.data(), 1));
1✔
2347
            result.test_eq("AES-128 encryption works", block, "66E94BD4EF8A2C3B884CFA59CA342B2E");
1✔
2348

2349
            TEST_FFI_OK(botan_block_cipher_encrypt_blocks, (cipher, block.data(), block.data(), 1));
1✔
2350
            result.test_eq("AES-128 encryption works", block, "F795BD4A52E29ED713D313FA20E98DBC");
1✔
2351

2352
            TEST_FFI_OK(botan_block_cipher_decrypt_blocks, (cipher, block.data(), block.data(), 1));
1✔
2353
            result.test_eq("AES-128 decryption works", block, "66E94BD4EF8A2C3B884CFA59CA342B2E");
1✔
2354

2355
            TEST_FFI_OK(botan_block_cipher_decrypt_blocks, (cipher, block.data(), block.data(), 1));
1✔
2356
            result.test_eq("AES-128 decryption works", block, "00000000000000000000000000000000");
1✔
2357

2358
            TEST_FFI_OK(botan_block_cipher_clear, (cipher));
1✔
2359
            botan_block_cipher_destroy(cipher);
1✔
2360
         }
2✔
2361
      }
1✔
2362
};
2363

2364
class FFI_ErrorHandling_Test final : public FFI_Test {
1✔
2365
   public:
2366
      std::string name() const override { return "FFI error handling"; }
1✔
2367

2368
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
2369
         // delete of null is ok/ignored
2370
         TEST_FFI_RC(0, botan_hash_destroy, (nullptr));
1✔
2371

2372
   #if !defined(BOTAN_HAS_SANITIZER_UNDEFINED)
2373
         // Confirm that botan_x_destroy checks the argument type
2374
         botan_mp_t mp;
1✔
2375
         botan_mp_init(&mp);
1✔
2376
         TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT, botan_hash_destroy, (reinterpret_cast<botan_hash_t>(mp)));
1✔
2377
         TEST_FFI_RC(0, botan_mp_destroy, (mp));
1✔
2378
   #endif
2379

2380
         std::set<std::string> errors;
1✔
2381
         for(int i = -100; i != 50; ++i) {
151✔
2382
            const char* err = botan_error_description(i);
150✔
2383
            result.confirm("Never a null pointer", err != nullptr);
300✔
2384

2385
            if(err != nullptr) {
150✔
2386
               const std::string s(err);
150✔
2387

2388
               if(s != "Unknown error") {
150✔
2389
                  result.confirm("No duplicate messages", !errors.contains(s));
44✔
2390
                  errors.insert(s);
22✔
2391
               }
2392
            }
150✔
2393
         }
2394
      }
1✔
2395
};
2396

2397
class FFI_Base64_Test final : public FFI_Test {
1✔
2398
   public:
2399
      std::string name() const override { return "FFI base64"; }
1✔
2400

2401
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
2402
         const uint8_t bin[9] = {0x16, 0x8a, 0x1f, 0x06, 0xe9, 0xe7, 0xcb, 0xdd, 0x34};
1✔
2403
         char out_buf[1024] = {0};
1✔
2404

2405
         size_t out_len = sizeof(out_buf);
1✔
2406
         TEST_FFI_OK(botan_base64_encode, (bin, sizeof(bin), out_buf, &out_len));
1✔
2407

2408
         result.test_eq("encoded string", out_buf, "FoofBunny900");
1✔
2409

2410
         out_len -= 1;
1✔
2411
         TEST_FFI_RC(
1✔
2412
            BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_base64_encode, (bin, sizeof(bin), out_buf, &out_len));
2413

2414
         const char* base64 = "U3VjaCBiYXNlNjQgd293IQ==";
1✔
2415
         uint8_t out_bin[1024] = {0};
1✔
2416

2417
         out_len = 3;
1✔
2418
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
2419
                     botan_base64_decode,
2420
                     (base64, strlen(base64), out_bin, &out_len));
2421

2422
         result.test_eq("output length", out_len, 18);
1✔
2423

2424
         out_len = sizeof(out_bin);
1✔
2425
         TEST_FFI_OK(botan_base64_decode, (base64, strlen(base64), out_bin, &out_len));
1✔
2426

2427
         result.test_eq(
3✔
2428
            "decoded string", std::string(reinterpret_cast<const char*>(out_bin), out_len), "Such base64 wow!");
2✔
2429
      }
1✔
2430
};
2431

2432
class FFI_Hex_Test final : public FFI_Test {
1✔
2433
   public:
2434
      std::string name() const override { return "FFI hex"; }
1✔
2435

2436
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
2437
         const uint8_t bin[4] = {0xDE, 0xAD, 0xBE, 0xEF};
1✔
2438
         char hex_buf[16] = {0};
1✔
2439

2440
         TEST_FFI_OK(botan_hex_encode, (bin, sizeof(bin), hex_buf, 0));
1✔
2441

2442
         result.test_eq("encoded string", hex_buf, "DEADBEEF");
1✔
2443

2444
         const char* hex = "67657420796572206A756D626F20736872696D70";
1✔
2445
         uint8_t out_bin[1024] = {0};
1✔
2446
         size_t out_len = 5;
1✔
2447

2448
         TEST_FFI_RC(
1✔
2449
            BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_hex_decode, (hex, strlen(hex), out_bin, &out_len));
2450

2451
         out_len = sizeof(out_bin);
1✔
2452
         TEST_FFI_OK(botan_hex_decode, (hex, strlen(hex), out_bin, &out_len));
1✔
2453

2454
         result.test_eq(
3✔
2455
            "decoded string", std::string(reinterpret_cast<const char*>(out_bin), out_len), "get yer jumbo shrimp");
2✔
2456
      }
1✔
2457
};
2458

2459
class FFI_MP_Test final : public FFI_Test {
1✔
2460
   public:
2461
      std::string name() const override { return "FFI MP"; }
1✔
2462

2463
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
2464
         char str_buf[1024] = {0};
1✔
2465
         size_t str_len = 0;
1✔
2466

2467
         botan_mp_t x;
1✔
2468
         botan_mp_init(&x);
1✔
2469
         TEST_FFI_RC(0, botan_mp_is_odd, (x));
1✔
2470
         TEST_FFI_RC(1, botan_mp_is_even, (x));
1✔
2471
         TEST_FFI_RC(0, botan_mp_is_negative, (x));
1✔
2472
         TEST_FFI_RC(1, botan_mp_is_positive, (x));
1✔
2473
         TEST_FFI_RC(1, botan_mp_is_zero, (x));
1✔
2474
         botan_mp_destroy(x);
1✔
2475

2476
         botan_mp_init(&x);
1✔
2477
         size_t bn_bytes = 0;
1✔
2478
         TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes));
1✔
2479
         result.test_eq("Expected size for MP 0", bn_bytes, 0);
1✔
2480

2481
         botan_mp_set_from_int(x, 5);
1✔
2482
         TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes));
1✔
2483
         result.test_eq("Expected size for MP 5", bn_bytes, 1);
1✔
2484

2485
         botan_mp_add_u32(x, x, 75);
1✔
2486
         TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes));
1✔
2487
         result.test_eq("Expected size for MP 80", bn_bytes, 1);
1✔
2488

2489
         str_len = sizeof(str_buf);
1✔
2490
         TEST_FFI_OK(botan_mp_to_str, (x, 10, str_buf, &str_len));
1✔
2491
         result.test_eq("botan_mp_add", std::string(str_buf), "80");
2✔
2492

2493
         botan_mp_sub_u32(x, x, 80);
1✔
2494
         TEST_FFI_RC(1, botan_mp_is_zero, (x));
1✔
2495
         botan_mp_add_u32(x, x, 259);
1✔
2496
         TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes));
1✔
2497
         result.test_eq("Expected size for MP 259", bn_bytes, 2);
1✔
2498

2499
         str_len = sizeof(str_buf);
1✔
2500
         TEST_FFI_OK(botan_mp_to_str, (x, 10, str_buf, &str_len));
1✔
2501
         result.test_eq("botan_mp_add", std::string(str_buf), "259");
2✔
2502

2503
         TEST_FFI_RC(1, botan_mp_is_odd, (x));
1✔
2504
         TEST_FFI_RC(0, botan_mp_is_even, (x));
1✔
2505
         TEST_FFI_RC(0, botan_mp_is_negative, (x));
1✔
2506
         TEST_FFI_RC(1, botan_mp_is_positive, (x));
1✔
2507
         TEST_FFI_RC(0, botan_mp_is_zero, (x));
1✔
2508

2509
         {
1✔
2510
            botan_mp_t zero;
1✔
2511
            botan_mp_init(&zero);
1✔
2512
            int cmp;
1✔
2513
            TEST_FFI_OK(botan_mp_cmp, (&cmp, x, zero));
1✔
2514
            result.confirm("bigint_mp_cmp(+, 0)", cmp == 1);
2✔
2515

2516
            TEST_FFI_OK(botan_mp_cmp, (&cmp, zero, x));
1✔
2517
            result.confirm("bigint_mp_cmp(0, +)", cmp == -1);
2✔
2518

2519
            TEST_FFI_RC(0, botan_mp_is_negative, (x));
1✔
2520
            TEST_FFI_RC(1, botan_mp_is_positive, (x));
1✔
2521
            TEST_FFI_OK(botan_mp_flip_sign, (x));
1✔
2522
            TEST_FFI_RC(1, botan_mp_is_negative, (x));
1✔
2523
            TEST_FFI_RC(0, botan_mp_is_positive, (x));
1✔
2524

2525
            // test no negative zero
2526
            TEST_FFI_RC(0, botan_mp_is_negative, (zero));
1✔
2527
            TEST_FFI_RC(1, botan_mp_is_positive, (zero));
1✔
2528
            TEST_FFI_OK(botan_mp_flip_sign, (zero));
1✔
2529
            TEST_FFI_RC(0, botan_mp_is_negative, (zero));
1✔
2530
            TEST_FFI_RC(1, botan_mp_is_positive, (zero));
1✔
2531

2532
            TEST_FFI_OK(botan_mp_cmp, (&cmp, x, zero));
1✔
2533
            result.confirm("bigint_mp_cmp(-, 0)", cmp == -1);
2✔
2534

2535
            TEST_FFI_OK(botan_mp_cmp, (&cmp, zero, x));
1✔
2536
            result.confirm("bigint_mp_cmp(0, -)", cmp == 1);
2✔
2537

2538
            TEST_FFI_OK(botan_mp_cmp, (&cmp, zero, zero));
1✔
2539
            result.confirm("bigint_mp_cmp(0, 0)", cmp == 0);
2✔
2540

2541
            TEST_FFI_OK(botan_mp_cmp, (&cmp, x, x));
1✔
2542
            result.confirm("bigint_mp_cmp(x, x)", cmp == 0);
2✔
2543

2544
            TEST_FFI_OK(botan_mp_flip_sign, (x));
1✔
2545

2546
            // Regression test for bug reported by @hgarrereyn
2547
            // See: GH #5128
2548
            botan_mp_t out_shift;
1✔
2549
            TEST_FFI_OK(botan_mp_init, (&out_shift));
1✔
2550
            TEST_FFI_OK(botan_mp_lshift, (out_shift, zero, 0));
1✔
2551

2552
            botan_mp_destroy(zero);
1✔
2553
            botan_mp_destroy(out_shift);
1✔
2554
         }
2555

2556
         size_t x_bits = 0;
1✔
2557
         TEST_FFI_OK(botan_mp_num_bits, (x, &x_bits));
1✔
2558
         result.test_eq("botan_mp_num_bits", x_bits, 9);
1✔
2559

2560
         TEST_FFI_OK(botan_mp_to_hex, (x, str_buf));
1✔
2561
         result.test_eq("botan_mp_to_hex", std::string(str_buf), "0x0103");
2✔
2562

2563
         ViewStringSink hex_sink;
1✔
2564
         TEST_FFI_OK(botan_mp_view_hex, (x, hex_sink.delegate(), hex_sink.callback()));
1✔
2565
         result.test_eq("botan_mp_view_hex", hex_sink.get(), "0x0103");
2✔
2566

2567
         ViewStringSink str_sink;
1✔
2568
         TEST_FFI_OK(botan_mp_view_str, (x, 10, str_sink.delegate(), str_sink.callback()));
1✔
2569
         result.test_eq("botan_mp_view_str", str_sink.get(), "259");
2✔
2570

2571
         ViewBytesSink bin_sink;
1✔
2572
         TEST_FFI_OK(botan_mp_view_bin, (x, bin_sink.delegate(), bin_sink.callback()));
1✔
2573
         result.test_eq("botan_mp_view_str", bin_sink.get(), "0103");
1✔
2574

2575
         uint32_t x_32;
1✔
2576
         TEST_FFI_OK(botan_mp_to_uint32, (x, &x_32));
1✔
2577
         result.test_eq("botan_mp_to_uint32", size_t(x_32), size_t(0x103));
1✔
2578

2579
         TEST_FFI_RC(1, botan_mp_get_bit, (x, 1));
1✔
2580
         TEST_FFI_RC(0, botan_mp_get_bit, (x, 87));
1✔
2581
         TEST_FFI_OK(botan_mp_set_bit, (x, 87));
1✔
2582
         TEST_FFI_RC(1, botan_mp_get_bit, (x, 87));
1✔
2583
         TEST_FFI_OK(botan_mp_to_hex, (x, str_buf));
1✔
2584
         result.test_eq("botan_mp_set_bit", std::string(str_buf), "0x8000000000000000000103");
2✔
2585

2586
         TEST_FFI_OK(botan_mp_clear_bit, (x, 87));
1✔
2587
         TEST_FFI_OK(botan_mp_to_hex, (x, str_buf));
1✔
2588
         result.test_eq("botan_mp_set_bit", std::string(str_buf), "0x0103");
2✔
2589

2590
         botan_mp_t y;
1✔
2591
         TEST_FFI_OK(botan_mp_init, (&y));
1✔
2592
         TEST_FFI_OK(botan_mp_set_from_int, (y, 0x1234567));
1✔
2593

2594
         botan_mp_t r;
1✔
2595
         botan_mp_init(&r);
1✔
2596

2597
         TEST_FFI_OK(botan_mp_add, (r, x, y));
1✔
2598
         str_len = sizeof(str_buf);
1✔
2599
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2600
         result.test_eq("botan_mp_add", std::string(str_buf), "19089002");
2✔
2601

2602
         TEST_FFI_OK(botan_mp_mul, (r, x, y));
1✔
2603
         str_len = sizeof(str_buf);
1✔
2604
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2605
         result.test_eq("botan_mp_mul", std::string(str_buf), "4943984437");
2✔
2606
         TEST_FFI_RC(0, botan_mp_is_negative, (r));
1✔
2607

2608
         botan_mp_t q;
1✔
2609
         botan_mp_init(&q);
1✔
2610
         TEST_FFI_OK(botan_mp_div, (q, r, y, x));
1✔
2611

2612
         str_len = sizeof(str_buf);
1✔
2613
         TEST_FFI_OK(botan_mp_to_str, (q, 10, str_buf, &str_len));
1✔
2614
         result.test_eq("botan_mp_div_q", std::string(str_buf), "73701");
2✔
2615

2616
         str_len = sizeof(str_buf);
1✔
2617
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2618
         result.test_eq("botan_mp_div_r", std::string(str_buf), "184");
2✔
2619

2620
         TEST_FFI_OK(botan_mp_set_from_str, (y, "4943984437"));
1✔
2621
         TEST_FFI_OK(botan_mp_sub, (r, x, y));
1✔
2622
         str_len = sizeof(str_buf);
1✔
2623
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2624
         result.test_eq("botan_mp_sub", std::string(str_buf), "-4943984178");
2✔
2625
         TEST_FFI_RC(1, botan_mp_is_negative, (r));
1✔
2626

2627
         TEST_FFI_OK(botan_mp_lshift, (r, x, 39));
1✔
2628
         str_len = sizeof(str_buf);
1✔
2629
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2630
         result.test_eq("botan_mp_lshift", std::string(str_buf), "142386755796992");
2✔
2631

2632
         TEST_FFI_OK(botan_mp_rshift, (r, r, 3));
1✔
2633
         str_len = sizeof(str_buf);
1✔
2634
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2635
         result.test_eq("botan_mp_rshift", std::string(str_buf), "17798344474624");
2✔
2636

2637
         TEST_FFI_OK(botan_mp_gcd, (r, x, y));
1✔
2638
         str_len = sizeof(str_buf);
1✔
2639
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2640
         result.test_eq("botan_mp_gcd", std::string(str_buf), "259");
2✔
2641

2642
         botan_mp_t p;
1✔
2643
         botan_mp_init(&p);
1✔
2644
         const uint8_t M127[] = {
1✔
2645
            0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
2646
         TEST_FFI_OK(botan_mp_from_bin, (p, M127, sizeof(M127)));
1✔
2647
         TEST_FFI_RC(1, botan_mp_is_prime, (p, rng, 64));
1✔
2648

2649
         size_t p_bits = 0;
1✔
2650
         TEST_FFI_OK(botan_mp_num_bits, (p, &p_bits));
1✔
2651
         result.test_eq("botan_mp_num_bits", p_bits, 127);
1✔
2652

2653
         TEST_FFI_OK(botan_mp_mod_inverse, (r, x, p));
1✔
2654
         str_len = sizeof(str_buf);
1✔
2655
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2656
         result.test_eq("botan_mp_mod_inverse", std::string(str_buf), "40728777507911553541948312086427855425");
2✔
2657

2658
         TEST_FFI_OK(botan_mp_powmod, (r, x, r, p));
1✔
2659
         str_len = sizeof(str_buf);
1✔
2660
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2661
         result.test_eq("botan_mp_powmod", std::string(str_buf), "40550417419160441638948180641668117560");
2✔
2662

2663
         TEST_FFI_OK(botan_mp_num_bytes, (r, &bn_bytes));
1✔
2664
         result.test_eq("botan_mp_num_bytes", bn_bytes, 16);
1✔
2665

2666
         std::vector<uint8_t> bn_buf;
1✔
2667
         bn_buf.resize(bn_bytes);
1✔
2668
         botan_mp_to_bin(r, bn_buf.data());
1✔
2669
         result.test_eq("botan_mp_to_bin", bn_buf, "1E81B9EFE0BE1902F6D03F9F5E5FB438");
1✔
2670

2671
         TEST_FFI_OK(botan_mp_set_from_mp, (y, r));
1✔
2672
         TEST_FFI_OK(botan_mp_mod_mul, (r, x, y, p));
1✔
2673
         str_len = sizeof(str_buf);
1✔
2674
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2675
         result.test_eq("botan_mp_mod_mul", std::string(str_buf), "123945920473931248854653259523111998693");
2✔
2676

2677
         str_len = 0;
1✔
2678
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2679

2680
         size_t x_bytes;
1✔
2681
         botan_mp_rand_bits(x, rng, 512);
1✔
2682
         TEST_FFI_OK(botan_mp_num_bytes, (x, &x_bytes));
1✔
2683
         result.test_lte("botan_mp_num_bytes", x_bytes, 512 / 8);
1✔
2684

2685
         TEST_FFI_OK(botan_mp_set_from_radix_str, (x, "909A", 16));
1✔
2686
         TEST_FFI_OK(botan_mp_to_uint32, (x, &x_32));
1✔
2687
         result.test_eq("botan_mp_set_from_radix_str(16)", x_32, static_cast<size_t>(0x909A));
1✔
2688

2689
         TEST_FFI_OK(botan_mp_set_from_radix_str, (x, "9098135", 10));
1✔
2690
         TEST_FFI_OK(botan_mp_to_uint32, (x, &x_32));
1✔
2691
         result.test_eq("botan_mp_set_from_radix_str(10)", x_32, static_cast<size_t>(9098135));
1✔
2692

2693
         botan_mp_destroy(p);
1✔
2694
         botan_mp_destroy(x);
1✔
2695
         botan_mp_destroy(y);
1✔
2696
         botan_mp_destroy(r);
1✔
2697
         botan_mp_destroy(q);
1✔
2698
      }
2✔
2699
};
2700

2701
class FFI_FPE_Test final : public FFI_Test {
1✔
2702
   public:
2703
      std::string name() const override { return "FFI FPE"; }
1✔
2704

2705
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
2706
         const uint8_t key[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
1✔
2707

2708
         botan_mp_t n;
1✔
2709
         botan_mp_init(&n);
1✔
2710
         botan_mp_set_from_str(n, "1000000000");
1✔
2711

2712
         botan_fpe_t fpe;
1✔
2713
         if(!TEST_FFI_INIT(botan_fpe_fe1_init, (&fpe, n, key, sizeof(key), 5, 0))) {
1✔
2714
            botan_mp_destroy(n);
×
2715
            return;
×
2716
         }
2717

2718
         botan_mp_t x;
1✔
2719
         botan_mp_init(&x);
1✔
2720
         botan_mp_set_from_str(x, "178051120");
1✔
2721

2722
         TEST_FFI_OK(botan_fpe_encrypt, (fpe, x, nullptr, 0));
1✔
2723

2724
         uint32_t xval = 0;
1✔
2725
         TEST_FFI_OK(botan_mp_to_uint32, (x, &xval));
1✔
2726
         result.test_eq("Expected FPE ciphertext", xval, size_t(605648666));
1✔
2727

2728
         TEST_FFI_OK(botan_fpe_encrypt, (fpe, x, nullptr, 0));
1✔
2729
         TEST_FFI_OK(botan_fpe_decrypt, (fpe, x, nullptr, 0));
1✔
2730
         TEST_FFI_OK(botan_fpe_decrypt, (fpe, x, nullptr, 0));
1✔
2731

2732
         TEST_FFI_OK(botan_mp_to_uint32, (x, &xval));
1✔
2733
         result.test_eq("FPE round trip", xval, size_t(178051120));
1✔
2734

2735
         TEST_FFI_OK(botan_fpe_destroy, (fpe));
1✔
2736
         TEST_FFI_OK(botan_mp_destroy, (x));
1✔
2737
         TEST_FFI_OK(botan_mp_destroy, (n));
2✔
2738
      }
2739
};
2740

2741
class FFI_TOTP_Test final : public FFI_Test {
1✔
2742
   public:
2743
      std::string name() const override { return "FFI TOTP"; }
1✔
2744

2745
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
2746
         const std::vector<uint8_t> key = Botan::hex_decode("3132333435363738393031323334353637383930");
1✔
2747
         const size_t digits = 8;
1✔
2748
         const size_t timestep = 30;
1✔
2749
         botan_totp_t totp;
1✔
2750

2751
         if(!TEST_FFI_INIT(botan_totp_init, (&totp, key.data(), key.size(), "SHA-1", digits, timestep))) {
1✔
2752
            return;
×
2753
         }
2754

2755
         uint32_t code;
1✔
2756
         TEST_FFI_OK(botan_totp_generate, (totp, &code, 59));
1✔
2757
         result.confirm("TOTP code", code == 94287082);
2✔
2758

2759
         TEST_FFI_OK(botan_totp_generate, (totp, &code, 1111111109));
1✔
2760
         result.confirm("TOTP code 2", code == 7081804);
2✔
2761

2762
         TEST_FFI_OK(botan_totp_check, (totp, 94287082, 59 + 60, 60));
1✔
2763
         TEST_FFI_RC(1, botan_totp_check, (totp, 94287082, 59 + 31, 1));
1✔
2764
         TEST_FFI_RC(1, botan_totp_check, (totp, 94287082, 59 + 61, 1));
1✔
2765

2766
         TEST_FFI_OK(botan_totp_destroy, (totp));
2✔
2767
      }
1✔
2768
};
2769

2770
class FFI_HOTP_Test final : public FFI_Test {
1✔
2771
   public:
2772
      std::string name() const override { return "FFI HOTP"; }
1✔
2773

2774
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
2775
         const std::vector<uint8_t> key = Botan::hex_decode("3132333435363738393031323334353637383930");
1✔
2776
         const size_t digits = 6;
1✔
2777

2778
         botan_hotp_t hotp;
1✔
2779
         uint32_t hotp_val;
1✔
2780

2781
         if(!TEST_FFI_INIT(botan_hotp_init, (&hotp, key.data(), key.size(), "SHA-1", digits))) {
1✔
2782
            return;
×
2783
         }
2784

2785
         TEST_FFI_OK(botan_hotp_generate, (hotp, &hotp_val, 0));
1✔
2786
         result.confirm("Valid value for counter 0", hotp_val == 755224);
2✔
2787
         TEST_FFI_OK(botan_hotp_generate, (hotp, &hotp_val, 1));
1✔
2788
         result.confirm("Valid value for counter 0", hotp_val == 287082);
2✔
2789
         TEST_FFI_OK(botan_hotp_generate, (hotp, &hotp_val, 2));
1✔
2790
         result.confirm("Valid value for counter 0", hotp_val == 359152);
2✔
2791
         TEST_FFI_OK(botan_hotp_generate, (hotp, &hotp_val, 0));
1✔
2792
         result.confirm("Valid value for counter 0", hotp_val == 755224);
2✔
2793

2794
         uint64_t next_ctr = 0;
1✔
2795

2796
         TEST_FFI_OK(botan_hotp_check, (hotp, &next_ctr, 755224, 0, 0));
1✔
2797
         result.confirm("HOTP resync", next_ctr == 1);
2✔
2798
         TEST_FFI_OK(botan_hotp_check, (hotp, nullptr, 359152, 2, 0));
1✔
2799
         TEST_FFI_RC(1, botan_hotp_check, (hotp, nullptr, 359152, 1, 0));
1✔
2800
         TEST_FFI_OK(botan_hotp_check, (hotp, &next_ctr, 359152, 0, 2));
1✔
2801
         result.confirm("HOTP resync", next_ctr == 3);
2✔
2802

2803
         TEST_FFI_OK(botan_hotp_destroy, (hotp));
2✔
2804
      }
1✔
2805
};
2806

2807
class FFI_Keywrap_Test final : public FFI_Test {
1✔
2808
   public:
2809
      std::string name() const override { return "FFI Keywrap"; }
1✔
2810

2811
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
2812
         const uint8_t key[16] = {0};
1✔
2813
         const uint8_t kek[16] = {0xFF, 0};
1✔
2814

2815
         uint8_t wrapped[16 + 8] = {0};
1✔
2816
         size_t wrapped_keylen = sizeof(wrapped);
1✔
2817

2818
         if(TEST_FFI_INIT(botan_key_wrap3394, (key, sizeof(key), kek, sizeof(kek), wrapped, &wrapped_keylen))) {
1✔
2819
            const uint8_t expected_wrapped_key[16 + 8] = {0x04, 0x13, 0x37, 0x39, 0x82, 0xCF, 0xFA, 0x31,
1✔
2820
                                                          0x81, 0xCA, 0x4F, 0x59, 0x74, 0x4D, 0xED, 0x29,
2821
                                                          0x1F, 0x3F, 0xE5, 0x24, 0x00, 0x1B, 0x93, 0x20};
2822

2823
            result.test_eq("Expected wrapped keylen size", wrapped_keylen, 16 + 8);
1✔
2824

2825
            result.test_eq(
1✔
2826
               nullptr, "Wrapped key", wrapped, wrapped_keylen, expected_wrapped_key, sizeof(expected_wrapped_key));
2827

2828
            uint8_t dec_key[16] = {0};
1✔
2829
            size_t dec_keylen = sizeof(dec_key);
1✔
2830
            TEST_FFI_OK(botan_key_unwrap3394, (wrapped, sizeof(wrapped), kek, sizeof(kek), dec_key, &dec_keylen));
1✔
2831

2832
            result.test_eq(nullptr, "Unwrapped key", dec_key, dec_keylen, key, sizeof(key));
2✔
2833
         }
2834
      }
1✔
2835
};
2836

2837
class FFI_XMSS_Test final : public FFI_Test {
1✔
2838
   public:
2839
      std::string name() const override { return "FFI XMSS"; }
1✔
2840

2841
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
2842
         botan_privkey_t priv;
1✔
2843
         if(TEST_FFI_INIT(botan_privkey_create, (&priv, "XMSS", "XMSS-SHA2_10_256", rng))) {
1✔
2844
            TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
1✔
2845

2846
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_privkey_stateful_operation, (priv, nullptr));
1✔
2847
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_privkey_remaining_operations, (priv, nullptr));
1✔
2848

2849
            int stateful;
1✔
2850
            TEST_FFI_OK(botan_privkey_stateful_operation, (priv, &stateful));
1✔
2851
            result.confirm("key is stateful", stateful == 1, true);
2✔
2852

2853
            uint64_t remaining;
1✔
2854
            TEST_FFI_OK(botan_privkey_remaining_operations, (priv, &remaining));
1✔
2855
            result.confirm("key has remaining operations", remaining == 1024, true);
2✔
2856

2857
            TEST_FFI_OK(botan_privkey_destroy, (priv));
2✔
2858
         }
2859
      }
1✔
2860
};
2861

2862
class FFI_RSA_Test final : public FFI_Test {
1✔
2863
   public:
2864
      std::string name() const override { return "FFI RSA"; }
1✔
2865

2866
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
2867
         botan_privkey_t priv;
1✔
2868

2869
         if(TEST_FFI_INIT(botan_privkey_create_rsa, (&priv, rng, 1024))) {
1✔
2870
            TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
1✔
2871

2872
            int stateful;
1✔
2873
            TEST_FFI_OK(botan_privkey_stateful_operation, (priv, &stateful));
1✔
2874
            result.confirm("key is not stateful", stateful == 1, false);
2✔
2875

2876
            uint64_t remaining;
1✔
2877
            TEST_FFI_FAIL("key is not stateful", botan_privkey_remaining_operations, (priv, &remaining));
2✔
2878

2879
            botan_pubkey_t pub;
1✔
2880
            TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
2881
            TEST_FFI_OK(botan_pubkey_check_key, (pub, rng, 0));
1✔
2882

2883
            ffi_test_pubkey_export(result, pub, priv, rng);
1✔
2884

2885
            botan_mp_t p;
1✔
2886
            botan_mp_t q;
1✔
2887
            botan_mp_t d;
1✔
2888
            botan_mp_t n;
1✔
2889
            botan_mp_t e;
1✔
2890
            botan_mp_init(&p);
1✔
2891
            botan_mp_init(&q);
1✔
2892
            botan_mp_init(&d);
1✔
2893
            botan_mp_init(&n);
1✔
2894
            botan_mp_init(&e);
1✔
2895

2896
            TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_privkey_get_field, (p, priv, "quux"));
1✔
2897
            TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_pubkey_get_field, (p, pub, "quux"));
1✔
2898

2899
            TEST_FFI_OK(botan_privkey_rsa_get_p, (p, priv));
1✔
2900
            TEST_FFI_OK(botan_privkey_rsa_get_q, (q, priv));
1✔
2901
            TEST_FFI_OK(botan_privkey_rsa_get_d, (d, priv));
1✔
2902
            TEST_FFI_OK(botan_privkey_rsa_get_e, (e, priv));
1✔
2903
            TEST_FFI_OK(botan_privkey_rsa_get_n, (n, priv));
1✔
2904

2905
            // Confirm same (e,n) values in public key
2906
            {
1✔
2907
               botan_mp_t pub_e;
1✔
2908
               botan_mp_t pub_n;
1✔
2909
               botan_mp_init(&pub_e);
1✔
2910
               botan_mp_init(&pub_n);
1✔
2911
               TEST_FFI_OK(botan_pubkey_rsa_get_e, (pub_e, pub));
1✔
2912
               TEST_FFI_OK(botan_pubkey_rsa_get_n, (pub_n, pub));
1✔
2913

2914
               TEST_FFI_RC(1, botan_mp_equal, (pub_e, e));
1✔
2915
               TEST_FFI_RC(1, botan_mp_equal, (pub_n, n));
1✔
2916
               botan_mp_destroy(pub_e);
1✔
2917
               botan_mp_destroy(pub_n);
1✔
2918
            }
2919

2920
            TEST_FFI_RC(1, botan_mp_is_prime, (p, rng, 64));
1✔
2921
            TEST_FFI_RC(1, botan_mp_is_prime, (q, rng, 64));
1✔
2922

2923
            // Test p != q
2924
            TEST_FFI_RC(0, botan_mp_equal, (p, q));
1✔
2925

2926
            // Test p * q == n
2927
            botan_mp_t x;
1✔
2928
            botan_mp_init(&x);
1✔
2929
            TEST_FFI_OK(botan_mp_mul, (x, p, q));
1✔
2930

2931
            TEST_FFI_RC(1, botan_mp_equal, (x, n));
1✔
2932
            botan_mp_destroy(x);
1✔
2933

2934
            botan_privkey_t loaded_privkey;
1✔
2935
            // First try loading a bogus key and verify check_key fails
2936
            TEST_FFI_OK(botan_privkey_load_rsa, (&loaded_privkey, n, d, q));
1✔
2937
            TEST_FFI_RC(-1, botan_privkey_check_key, (loaded_privkey, rng, 0));
1✔
2938
            botan_privkey_destroy(loaded_privkey);
1✔
2939

2940
            TEST_FFI_OK(botan_privkey_load_rsa, (&loaded_privkey, p, q, e));
1✔
2941
            TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
1✔
2942

2943
            botan_pubkey_t loaded_pubkey;
1✔
2944
            TEST_FFI_OK(botan_pubkey_load_rsa, (&loaded_pubkey, n, e));
1✔
2945
            TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey, rng, 0));
1✔
2946

2947
            botan_mp_destroy(p);
1✔
2948
            botan_mp_destroy(q);
1✔
2949
            botan_mp_destroy(d);
1✔
2950
            botan_mp_destroy(e);
1✔
2951
            botan_mp_destroy(n);
1✔
2952

2953
            size_t pkcs1_len = 0;
1✔
2954
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
2955
                        botan_privkey_rsa_get_privkey,
2956
                        (loaded_privkey, nullptr, &pkcs1_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
2957

2958
            std::vector<uint8_t> pkcs1(pkcs1_len);
1✔
2959
            TEST_FFI_OK(botan_privkey_rsa_get_privkey,
1✔
2960
                        (loaded_privkey, pkcs1.data(), &pkcs1_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
2961

2962
            botan_privkey_t privkey_from_pkcs1;
1✔
2963
            TEST_FFI_OK(botan_privkey_load_rsa_pkcs1, (&privkey_from_pkcs1, pkcs1.data(), pkcs1_len));
1✔
2964
            TEST_FFI_OK(botan_privkey_destroy, (privkey_from_pkcs1));
1✔
2965

2966
            pkcs1_len = 0;
1✔
2967
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
2968
                        botan_privkey_rsa_get_privkey,
2969
                        (loaded_privkey, nullptr, &pkcs1_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
2970
            pkcs1.resize(pkcs1_len);
1✔
2971
            TEST_FFI_OK(botan_privkey_rsa_get_privkey,
1✔
2972
                        (loaded_privkey, pkcs1.data(), &pkcs1_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
2973

2974
            std::array<char, 32> namebuf{};
1✔
2975
            size_t name_len = namebuf.size();
1✔
2976
            if(TEST_FFI_OK(botan_pubkey_algo_name, (loaded_pubkey, namebuf.data(), &name_len))) {
1✔
2977
               result.test_eq("algo name", namebuf.data(), "RSA");
2✔
2978
            }
2979

2980
            name_len = namebuf.size();
1✔
2981
            if(TEST_FFI_OK(botan_privkey_algo_name, (loaded_privkey, namebuf.data(), &name_len))) {
1✔
2982
               result.test_eq("algo name", namebuf.data(), "RSA");
2✔
2983
            }
2984

2985
            botan_pk_op_encrypt_t encrypt;
1✔
2986
            if(TEST_FFI_INIT(botan_pk_op_encrypt_create, (&encrypt, loaded_pubkey, "OAEP(SHA-256)", 0))) {
1✔
2987
               std::vector<uint8_t> plaintext(32);
1✔
2988
               TEST_FFI_OK(botan_rng_get, (rng, plaintext.data(), plaintext.size()));
1✔
2989

2990
               size_t ctext_len;
1✔
2991
               TEST_FFI_OK(botan_pk_op_encrypt_output_length, (encrypt, plaintext.size(), &ctext_len));
1✔
2992
               std::vector<uint8_t> ciphertext(ctext_len);
1✔
2993

2994
               if(TEST_FFI_OK(botan_pk_op_encrypt,
1✔
2995
                              (encrypt, rng, ciphertext.data(), &ctext_len, plaintext.data(), plaintext.size()))) {
2996
                  ciphertext.resize(ctext_len);
1✔
2997

2998
                  botan_pk_op_decrypt_t decrypt;
1✔
2999
                  if(TEST_FFI_OK(botan_pk_op_decrypt_create, (&decrypt, priv, "OAEP(SHA-256)", 0))) {
1✔
3000
                     size_t decrypted_len;
1✔
3001
                     TEST_FFI_OK(botan_pk_op_decrypt_output_length, (decrypt, ciphertext.size(), &decrypted_len));
1✔
3002
                     std::vector<uint8_t> decrypted(decrypted_len);
1✔
3003
                     TEST_FFI_OK(botan_pk_op_decrypt,
1✔
3004
                                 (decrypt, decrypted.data(), &decrypted_len, ciphertext.data(), ciphertext.size()));
3005
                     decrypted.resize(decrypted_len);
1✔
3006

3007
                     result.test_eq("RSA plaintext", decrypted, plaintext);
2✔
3008
                  }
1✔
3009

3010
                  TEST_FFI_OK(botan_pk_op_decrypt_destroy, (decrypt));
2✔
3011
               }
3012

3013
               TEST_FFI_OK(botan_pk_op_encrypt_destroy, (encrypt));
2✔
3014
            }
2✔
3015

3016
            TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey));
1✔
3017
            TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
3018
            TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
1✔
3019
            TEST_FFI_OK(botan_privkey_destroy, (priv));
2✔
3020
         }
1✔
3021
      }
1✔
3022
};
3023

3024
class FFI_DSA_Test final : public FFI_Test {
1✔
3025
   public:
3026
      std::string name() const override { return "FFI DSA"; }
1✔
3027

3028
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
3029
         botan_privkey_t priv;
1✔
3030

3031
         if(TEST_FFI_INIT(botan_privkey_create, (&priv, "DSA", "dsa/jce/1024", rng))) {
1✔
3032
            do_dsa_test(priv, rng, result);
1✔
3033
         }
3034

3035
         if(TEST_FFI_INIT(botan_privkey_create_dsa, (&priv, rng, 1024, 160))) {
1✔
3036
            do_dsa_test(priv, rng, result);
1✔
3037
         }
3038
      }
1✔
3039

3040
   private:
3041
      static void do_dsa_test(botan_privkey_t priv, botan_rng_t rng, Test::Result& result) {
2✔
3042
         TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
2✔
3043

3044
         botan_pubkey_t pub;
2✔
3045
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
2✔
3046
         TEST_FFI_OK(botan_pubkey_check_key, (pub, rng, 0));
2✔
3047

3048
         ffi_test_pubkey_export(result, pub, priv, rng);
2✔
3049

3050
         botan_mp_t p;
2✔
3051
         botan_mp_t q;
2✔
3052
         botan_mp_t g;
2✔
3053
         botan_mp_t x;
2✔
3054
         botan_mp_t y;
2✔
3055
         botan_mp_init(&p);
2✔
3056
         botan_mp_init(&q);
2✔
3057
         botan_mp_init(&g);
2✔
3058
         botan_mp_init(&x);
2✔
3059
         botan_mp_init(&y);
2✔
3060

3061
         TEST_FFI_OK(botan_privkey_dsa_get_x, (x, priv));
2✔
3062
         TEST_FFI_OK(botan_pubkey_dsa_get_g, (g, pub));
2✔
3063
         TEST_FFI_OK(botan_pubkey_dsa_get_p, (p, pub));
2✔
3064
         TEST_FFI_OK(botan_pubkey_dsa_get_q, (q, pub));
2✔
3065
         TEST_FFI_OK(botan_pubkey_dsa_get_y, (y, pub));
2✔
3066

3067
         botan_mp_t cmp;
2✔
3068
         botan_mp_init(&cmp);
2✔
3069
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_privkey_get_field, (cmp, priv, "quux"));
2✔
3070

3071
         TEST_FFI_OK(botan_privkey_get_field, (cmp, priv, "x"));
2✔
3072
         TEST_FFI_RC(1, botan_mp_equal, (cmp, x));
2✔
3073

3074
         TEST_FFI_OK(botan_privkey_get_field, (cmp, priv, "y"));
2✔
3075
         TEST_FFI_RC(1, botan_mp_equal, (cmp, y));
2✔
3076

3077
         TEST_FFI_OK(botan_privkey_get_field, (cmp, priv, "p"));
2✔
3078
         TEST_FFI_RC(1, botan_mp_equal, (cmp, p));
2✔
3079
         botan_mp_destroy(cmp);
2✔
3080

3081
         botan_privkey_t loaded_privkey;
2✔
3082
         TEST_FFI_OK(botan_privkey_load_dsa, (&loaded_privkey, p, q, g, x));
2✔
3083
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
2✔
3084

3085
         botan_pubkey_t loaded_pubkey;
2✔
3086
         TEST_FFI_OK(botan_pubkey_load_dsa, (&loaded_pubkey, p, q, g, y));
2✔
3087
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey, rng, 0));
2✔
3088

3089
         botan_mp_destroy(p);
2✔
3090
         botan_mp_destroy(q);
2✔
3091
         botan_mp_destroy(g);
2✔
3092
         botan_mp_destroy(y);
2✔
3093
         botan_mp_destroy(x);
2✔
3094

3095
         botan_pk_op_sign_t signer;
2✔
3096

3097
         std::vector<uint8_t> message(6, 6);
2✔
3098
         std::vector<uint8_t> signature;
2✔
3099

3100
         if(TEST_FFI_OK(botan_pk_op_sign_create, (&signer, loaded_privkey, "SHA-256", 0))) {
2✔
3101
            // TODO: break input into multiple calls to update
3102
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message.data(), message.size()));
2✔
3103

3104
            size_t sig_len;
2✔
3105
            TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_len));
2✔
3106
            signature.resize(sig_len);
2✔
3107

3108
            size_t output_sig_len = sig_len;
2✔
3109
            TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &output_sig_len));
2✔
3110
            result.test_lte("Output length is upper bound", output_sig_len, sig_len);
2✔
3111
            signature.resize(output_sig_len);
2✔
3112

3113
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
4✔
3114
         }
3115

3116
         botan_pk_op_verify_t verifier = nullptr;
2✔
3117

3118
         if(!signature.empty() && TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, "SHA-256", 0))) {
4✔
3119
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
3120
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2✔
3121

3122
            // TODO: randomize this
3123
            signature[0] ^= 1;
2✔
3124
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
3125
            TEST_FFI_RC(
2✔
3126
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
3127

3128
            message[0] ^= 1;
2✔
3129
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
3130
            TEST_FFI_RC(
2✔
3131
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
3132

3133
            signature[0] ^= 1;
2✔
3134
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
3135
            TEST_FFI_RC(
2✔
3136
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
3137

3138
            message[0] ^= 1;
2✔
3139
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
3140
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2✔
3141

3142
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
4✔
3143
         }
3144

3145
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey));
2✔
3146
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
2✔
3147
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
2✔
3148
         TEST_FFI_OK(botan_privkey_destroy, (priv));
4✔
3149
      }
4✔
3150
};
3151

3152
class FFI_ECDSA_Test final : public FFI_Test {
1✔
3153
   public:
3154
      std::string name() const override { return "FFI ECDSA"; }
1✔
3155

3156
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
3157
         static const char* kCurve = "secp384r1";
1✔
3158
         botan_privkey_t priv;
1✔
3159
         botan_pubkey_t pub;
1✔
3160

3161
         if(!TEST_FFI_INIT(botan_privkey_create_ecdsa, (&priv, rng, kCurve))) {
1✔
3162
            return;
×
3163
         }
3164

3165
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
3166
         ffi_test_pubkey_export(result, pub, priv, rng);
1✔
3167

3168
         // Check key load functions
3169
         botan_mp_t private_scalar;
1✔
3170
         botan_mp_t public_x;
1✔
3171
         botan_mp_t public_y;
1✔
3172
         ViewBytesSink sec1;
1✔
3173
         botan_mp_init(&private_scalar);
1✔
3174
         botan_mp_init(&public_x);
1✔
3175
         botan_mp_init(&public_y);
1✔
3176

3177
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_privkey_get_field, (private_scalar, priv, "quux"));
1✔
3178
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_pubkey_get_field, (private_scalar, pub, "quux"));
1✔
3179

3180
         TEST_FFI_OK(botan_privkey_get_field, (private_scalar, priv, "x"));
1✔
3181
         TEST_FFI_OK(botan_pubkey_get_field, (public_x, pub, "public_x"));
1✔
3182
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub, "public_y"));
1✔
3183
         TEST_FFI_OK(botan_pubkey_view_raw, (pub, sec1.delegate(), sec1.callback()));
1✔
3184

3185
         botan_privkey_t loaded_privkey;
1✔
3186
         botan_pubkey_t loaded_pubkey1;
1✔
3187
         botan_pubkey_t loaded_pubkey2;
1✔
3188
         TEST_FFI_OK(botan_privkey_load_ecdsa, (&loaded_privkey, private_scalar, kCurve));
1✔
3189
         TEST_FFI_OK(botan_pubkey_load_ecdsa, (&loaded_pubkey1, public_x, public_y, kCurve));
1✔
3190
         TEST_FFI_OK(botan_pubkey_load_ecdsa_sec1, (&loaded_pubkey2, sec1.data(), sec1.size(), kCurve));
1✔
3191
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
1✔
3192
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey1, rng, 0));
1✔
3193
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey2, rng, 0));
1✔
3194

3195
         std::array<char, 32> namebuf{};
1✔
3196
         size_t name_len = namebuf.size();
1✔
3197

3198
         TEST_FFI_OK(botan_pubkey_algo_name, (pub, namebuf.data(), &name_len));
1✔
3199
         result.test_eq("Algo name is expected", namebuf.data(), "ECDSA");
1✔
3200

3201
         std::vector<uint8_t> message(1280);
1✔
3202
         std::vector<uint8_t> signature;
1✔
3203
         TEST_FFI_OK(botan_rng_get, (rng, message.data(), message.size()));
1✔
3204

3205
         for(uint32_t flags = 0; flags <= 1; ++flags) {
3✔
3206
            botan_pk_op_sign_t signer;
2✔
3207
            if(TEST_FFI_INIT(botan_pk_op_sign_create, (&signer, loaded_privkey, "SHA-384", flags))) {
2✔
3208
               // TODO: break input into multiple calls to update
3209
               TEST_FFI_OK(botan_pk_op_sign_update, (signer, message.data(), message.size()));
2✔
3210

3211
               size_t sig_len;
2✔
3212
               TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_len));
2✔
3213

3214
               signature.resize(sig_len);
2✔
3215

3216
               size_t output_sig_len = signature.size();
2✔
3217
               TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &output_sig_len));
2✔
3218
               signature.resize(output_sig_len);
2✔
3219

3220
               TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
4✔
3221
            }
3222

3223
            botan_pk_op_verify_t verifier = nullptr;
2✔
3224

3225
            if(!signature.empty() && TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, "SHA-384", flags))) {
4✔
3226
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
3227
               TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2✔
3228

3229
               // TODO: randomize this
3230
               signature[0] ^= 1;
2✔
3231
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
3232
               TEST_FFI_RC(BOTAN_FFI_INVALID_VERIFIER,
2✔
3233
                           botan_pk_op_verify_finish,
3234
                           (verifier, signature.data(), signature.size()));
3235

3236
               message[0] ^= 1;
2✔
3237
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
3238
               TEST_FFI_RC(BOTAN_FFI_INVALID_VERIFIER,
2✔
3239
                           botan_pk_op_verify_finish,
3240
                           (verifier, signature.data(), signature.size()));
3241

3242
               signature[0] ^= 1;
2✔
3243
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
3244
               TEST_FFI_RC(BOTAN_FFI_INVALID_VERIFIER,
2✔
3245
                           botan_pk_op_verify_finish,
3246
                           (verifier, signature.data(), signature.size()));
3247

3248
               message[0] ^= 1;
2✔
3249
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
3250
               TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2✔
3251

3252
               TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
4✔
3253
            }
3254
         }
3255

3256
         TEST_FFI_OK(botan_mp_destroy, (private_scalar));
1✔
3257
         TEST_FFI_OK(botan_mp_destroy, (public_x));
1✔
3258
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1✔
3259
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
3260
         TEST_FFI_OK(botan_privkey_destroy, (priv));
1✔
3261
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
1✔
3262
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey1));
1✔
3263
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey2));
2✔
3264
      }
3✔
3265
};
3266

3267
class FFI_SM2_Sig_Test final : public FFI_Test {
1✔
3268
   public:
3269
      std::string name() const override { return "FFI SM2 Sig"; }
1✔
3270

3271
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
3272
         static const char* kCurve = "sm2p256v1";
1✔
3273
         const std::string sm2_ident = "SM2 Ident Field";
1✔
3274
         botan_privkey_t priv;
1✔
3275
         botan_pubkey_t pub;
1✔
3276
         botan_privkey_t loaded_privkey;
1✔
3277
         botan_pubkey_t loaded_pubkey1;
1✔
3278
         botan_pubkey_t loaded_pubkey2;
1✔
3279

3280
         if(!TEST_FFI_INIT(botan_privkey_create, (&priv, "SM2_Sig", kCurve, rng))) {
1✔
3281
            return;
3282
         }
3283

3284
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
3285
         ffi_test_pubkey_export(result, pub, priv, rng);
1✔
3286

3287
         uint8_t za[32];
1✔
3288
         size_t sizeof_za = sizeof(za);
1✔
3289
         TEST_FFI_OK(botan_pubkey_sm2_compute_za, (za, &sizeof_za, "Ident", "SM3", pub));
1✔
3290

3291
         // Check key load functions
3292
         botan_mp_t private_scalar;
1✔
3293
         botan_mp_t public_x;
1✔
3294
         botan_mp_t public_y;
1✔
3295
         ViewBytesSink sec1;
1✔
3296
         botan_mp_init(&private_scalar);
1✔
3297
         botan_mp_init(&public_x);
1✔
3298
         botan_mp_init(&public_y);
1✔
3299

3300
         TEST_FFI_OK(botan_privkey_get_field, (private_scalar, priv, "x"));
1✔
3301
         TEST_FFI_OK(botan_pubkey_get_field, (public_x, pub, "public_x"));
1✔
3302
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub, "public_y"));
1✔
3303
         TEST_FFI_OK(botan_pubkey_view_raw, (pub, sec1.delegate(), sec1.callback()));
1✔
3304
         REQUIRE_FFI_OK(botan_privkey_load_sm2, (&loaded_privkey, private_scalar, kCurve));
1✔
3305
         REQUIRE_FFI_OK(botan_pubkey_load_sm2, (&loaded_pubkey1, public_x, public_y, kCurve));
1✔
3306
         REQUIRE_FFI_OK(botan_pubkey_load_sm2_sec1, (&loaded_pubkey2, sec1.data(), sec1.size(), kCurve));
1✔
3307
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
1✔
3308
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey1, rng, 0));
1✔
3309
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey2, rng, 0));
1✔
3310

3311
         std::array<char, 32> namebuf{};
1✔
3312
         size_t name_len = namebuf.size();
1✔
3313

3314
         TEST_FFI_OK(botan_pubkey_algo_name, (pub, namebuf.data(), &name_len));
1✔
3315
         result.test_eq("Algo name is expected", namebuf.data(), "SM2");
1✔
3316

3317
         std::vector<uint8_t> message(1280);
1✔
3318
         std::vector<uint8_t> signature;
1✔
3319
         TEST_FFI_OK(botan_rng_get, (rng, message.data(), message.size()));
1✔
3320
         botan_pk_op_sign_t signer;
1✔
3321
         if(TEST_FFI_OK(botan_pk_op_sign_create, (&signer, loaded_privkey, sm2_ident.c_str(), 0))) {
1✔
3322
            // TODO: break input into multiple calls to update
3323
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message.data(), message.size()));
1✔
3324

3325
            size_t sig_len;
1✔
3326
            TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_len));
1✔
3327

3328
            signature.resize(sig_len);
1✔
3329

3330
            TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &sig_len));
1✔
3331
            signature.resize(sig_len);
1✔
3332

3333
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
2✔
3334
         }
3335

3336
         botan_pk_op_verify_t verifier = nullptr;
1✔
3337

3338
         if(!signature.empty() && TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, sm2_ident.c_str(), 0))) {
2✔
3339
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1✔
3340
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
1✔
3341

3342
            // TODO: randomize this
3343
            signature[0] ^= 1;
1✔
3344
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1✔
3345
            TEST_FFI_RC(
1✔
3346
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
3347

3348
            message[0] ^= 1;
1✔
3349
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1✔
3350
            TEST_FFI_RC(
1✔
3351
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
3352

3353
            signature[0] ^= 1;
1✔
3354
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1✔
3355
            TEST_FFI_RC(
1✔
3356
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
3357

3358
            message[0] ^= 1;
1✔
3359
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1✔
3360
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
1✔
3361

3362
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
2✔
3363
         }
3364

3365
         TEST_FFI_OK(botan_mp_destroy, (private_scalar));
1✔
3366
         TEST_FFI_OK(botan_mp_destroy, (public_x));
1✔
3367
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1✔
3368
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
3369
         TEST_FFI_OK(botan_privkey_destroy, (priv));
1✔
3370
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
1✔
3371
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey1));
1✔
3372
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey2));
2✔
3373
      }
3✔
3374
};
3375

3376
class FFI_SM2_Enc_Test final : public FFI_Test {
1✔
3377
   public:
3378
      std::string name() const override { return "FFI SM2 Enc"; }
1✔
3379

3380
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
3381
         static const char* kCurve = "sm2p256v1";
1✔
3382
         botan_privkey_t priv;
1✔
3383
         botan_pubkey_t pub;
1✔
3384
         botan_privkey_t loaded_privkey;
1✔
3385
         botan_pubkey_t loaded_pubkey1;
1✔
3386
         botan_pubkey_t loaded_pubkey2;
1✔
3387

3388
         if(!TEST_FFI_INIT(botan_privkey_create, (&priv, "SM2_Enc", kCurve, rng))) {
1✔
3389
            return;
×
3390
         }
3391

3392
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
3393
         ffi_test_pubkey_export(result, pub, priv, rng);
1✔
3394

3395
         uint8_t za[32];
1✔
3396
         size_t sizeof_za = sizeof(za);
1✔
3397
         TEST_FFI_OK(botan_pubkey_sm2_compute_za, (za, &sizeof_za, "Ident", "SM3", pub));
1✔
3398

3399
         // Check key load functions
3400
         botan_mp_t private_scalar;
1✔
3401
         botan_mp_t public_x;
1✔
3402
         botan_mp_t public_y;
1✔
3403
         ViewBytesSink sec1;
1✔
3404
         botan_mp_init(&private_scalar);
1✔
3405
         botan_mp_init(&public_x);
1✔
3406
         botan_mp_init(&public_y);
1✔
3407

3408
         TEST_FFI_OK(botan_privkey_get_field, (private_scalar, priv, "x"));
1✔
3409
         TEST_FFI_OK(botan_pubkey_get_field, (public_x, pub, "public_x"));
1✔
3410
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub, "public_y"));
1✔
3411
         TEST_FFI_OK(botan_pubkey_view_raw, (pub, sec1.delegate(), sec1.callback()));
1✔
3412
         REQUIRE_FFI_OK(botan_privkey_load_sm2_enc, (&loaded_privkey, private_scalar, kCurve));
1✔
3413
         REQUIRE_FFI_OK(botan_pubkey_load_sm2_enc, (&loaded_pubkey1, public_x, public_y, kCurve));
1✔
3414
         REQUIRE_FFI_OK(botan_pubkey_load_sm2_sec1, (&loaded_pubkey2, sec1.data(), sec1.size(), kCurve));
1✔
3415
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
1✔
3416
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey1, rng, 0));
1✔
3417
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey2, rng, 0));
1✔
3418

3419
         std::array<char, 32> namebuf{};
1✔
3420
         size_t name_len = namebuf.size();
1✔
3421

3422
         TEST_FFI_OK(botan_pubkey_algo_name, (pub, namebuf.data(), &name_len));
1✔
3423
         result.test_eq("Algo name is expected", namebuf.data(), "SM2");
1✔
3424

3425
         std::vector<uint8_t> message(32);
1✔
3426

3427
         std::vector<uint8_t> ciphertext;
1✔
3428
         TEST_FFI_OK(botan_rng_get, (rng, message.data(), message.size()));
1✔
3429

3430
         botan_pk_op_encrypt_t enc;
1✔
3431
         if(TEST_FFI_OK(botan_pk_op_encrypt_create, (&enc, loaded_pubkey1, "", 0))) {
1✔
3432
            size_t ctext_len;
1✔
3433
            TEST_FFI_OK(botan_pk_op_encrypt_output_length, (enc, message.size(), &ctext_len));
1✔
3434

3435
            ciphertext.resize(ctext_len);
1✔
3436
            TEST_FFI_OK(botan_pk_op_encrypt, (enc, rng, ciphertext.data(), &ctext_len, message.data(), message.size()));
1✔
3437
            ciphertext.resize(ctext_len);
1✔
3438

3439
            botan_pk_op_decrypt_t dec;
1✔
3440
            TEST_FFI_OK(botan_pk_op_decrypt_create, (&dec, loaded_privkey, "", 0));
1✔
3441

3442
            std::vector<uint8_t> recovered(message.size());
1✔
3443
            size_t recovered_len = recovered.size();
1✔
3444

3445
            TEST_FFI_OK(botan_pk_op_decrypt,
1✔
3446
                        (dec, recovered.data(), &recovered_len, ciphertext.data(), ciphertext.size()));
3447

3448
            botan_pk_op_decrypt_destroy(dec);
1✔
3449
         }
1✔
3450
         botan_pk_op_encrypt_destroy(enc);
1✔
3451

3452
         TEST_FFI_OK(botan_mp_destroy, (private_scalar));
1✔
3453
         TEST_FFI_OK(botan_mp_destroy, (public_x));
1✔
3454
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1✔
3455
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
3456
         TEST_FFI_OK(botan_privkey_destroy, (priv));
1✔
3457
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
1✔
3458
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey1));
1✔
3459
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey2));
2✔
3460
      }
3✔
3461
};
3462

3463
class FFI_ECDH_Test final : public FFI_Test {
1✔
3464
   public:
3465
      std::string name() const override { return "FFI ECDH"; }
1✔
3466

3467
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
3468
         botan_privkey_t priv1;
1✔
3469
         if(!TEST_FFI_INIT(botan_privkey_create_ecdh, (&priv1, rng, "secp256r1"))) {
1✔
3470
            return;
×
3471
         }
3472

3473
         botan_privkey_t priv2;
1✔
3474
         REQUIRE_FFI_OK(botan_privkey_create_ecdh, (&priv2, rng, "secp256r1"));
1✔
3475

3476
         botan_pubkey_t pub1;
1✔
3477
         REQUIRE_FFI_OK(botan_privkey_export_pubkey, (&pub1, priv1));
1✔
3478

3479
         botan_pubkey_t pub2;
1✔
3480
         REQUIRE_FFI_OK(botan_privkey_export_pubkey, (&pub2, priv2));
1✔
3481

3482
         /* Reload key-pair1 in order to test functions for key loading */
3483
         botan_mp_t private_scalar;
1✔
3484
         botan_mp_t public_x;
1✔
3485
         botan_mp_t public_y;
1✔
3486
         ViewBytesSink sec1;
1✔
3487
         botan_mp_init(&private_scalar);
1✔
3488
         botan_mp_init(&public_x);
1✔
3489
         botan_mp_init(&public_y);
1✔
3490

3491
         TEST_FFI_OK(botan_privkey_get_field, (private_scalar, priv1, "x"));
1✔
3492
         TEST_FFI_OK(botan_pubkey_get_field, (public_x, pub1, "public_x"));
1✔
3493
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub1, "public_y"));
1✔
3494
         TEST_FFI_OK(botan_pubkey_view_raw, (pub1, sec1.delegate(), sec1.callback()));
1✔
3495

3496
         botan_privkey_t loaded_privkey1;
1✔
3497
         botan_pubkey_t loaded_pubkey1;
1✔
3498
         botan_pubkey_t loaded_pubkey2;
1✔
3499
         REQUIRE_FFI_OK(botan_privkey_load_ecdh, (&loaded_privkey1, private_scalar, "secp256r1"));
1✔
3500
         REQUIRE_FFI_OK(botan_pubkey_load_ecdh, (&loaded_pubkey1, public_x, public_y, "secp256r1"));
1✔
3501
         REQUIRE_FFI_OK(botan_pubkey_load_ecdh_sec1, (&loaded_pubkey2, sec1.data(), sec1.size(), "secp256r1"));
1✔
3502
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey1, rng, 0));
1✔
3503
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey1, rng, 0));
1✔
3504
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey2, rng, 0));
1✔
3505

3506
         ffi_test_pubkey_export(result, loaded_pubkey1, priv1, rng);
1✔
3507
         ffi_test_pubkey_export(result, loaded_pubkey2, priv1, rng);
1✔
3508
         ffi_test_pubkey_export(result, pub2, priv2, rng);
1✔
3509

3510
   #if defined(BOTAN_HAS_KDF2) && defined(BOTAN_HAS_SHA_256)
3511
         constexpr bool has_kdf2_sha256 = true;
1✔
3512
   #else
3513
         constexpr bool has_kdf2_sha256 = false;
3514
   #endif
3515

3516
         const char* kdf = has_kdf2_sha256 ? "KDF2(SHA-256)" : "Raw";
1✔
3517
         constexpr size_t salt_len = has_kdf2_sha256 ? 32 : 0;
1✔
3518

3519
         botan_pk_op_ka_t ka1;
1✔
3520
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka1, loaded_privkey1, kdf, 0));
1✔
3521
         botan_pk_op_ka_t ka2;
1✔
3522
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka2, priv2, kdf, 0));
1✔
3523

3524
         size_t pubkey1_len = 0;
1✔
3525
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
3526
                     botan_pk_op_key_agreement_export_public,
3527
                     (priv1, nullptr, &pubkey1_len));
3528
         std::vector<uint8_t> pubkey1(pubkey1_len);
1✔
3529
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_export_public, (priv1, pubkey1.data(), &pubkey1_len));
1✔
3530
         size_t pubkey2_len = 0;
1✔
3531
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
3532
                     botan_pk_op_key_agreement_export_public,
3533
                     (priv2, nullptr, &pubkey2_len));
3534
         std::vector<uint8_t> pubkey2(pubkey2_len);
1✔
3535
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_export_public, (priv2, pubkey2.data(), &pubkey2_len));
1✔
3536

3537
         std::vector<uint8_t> salt(salt_len);
1✔
3538
         TEST_FFI_OK(botan_rng_get, (rng, salt.data(), salt.size()));
1✔
3539

3540
         const size_t shared_key_len = 32;
1✔
3541

3542
         std::vector<uint8_t> key1(shared_key_len);
1✔
3543
         size_t key1_len = key1.size();
1✔
3544
         TEST_FFI_OK(botan_pk_op_key_agreement,
1✔
3545
                     (ka1, key1.data(), &key1_len, pubkey2.data(), pubkey2.size(), salt.data(), salt.size()));
3546

3547
         std::vector<uint8_t> key2(shared_key_len);
1✔
3548
         size_t key2_len = key2.size();
1✔
3549
         TEST_FFI_OK(botan_pk_op_key_agreement,
1✔
3550
                     (ka2, key2.data(), &key2_len, pubkey1.data(), pubkey1.size(), salt.data(), salt.size()));
3551

3552
         result.test_eq("shared ECDH key", key1, key2);
2✔
3553

3554
         TEST_FFI_OK(botan_mp_destroy, (private_scalar));
1✔
3555
         TEST_FFI_OK(botan_mp_destroy, (public_x));
1✔
3556
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1✔
3557
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka1));
1✔
3558
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka2));
1✔
3559
         TEST_FFI_OK(botan_privkey_destroy, (priv1));
1✔
3560
         TEST_FFI_OK(botan_privkey_destroy, (priv2));
1✔
3561
         TEST_FFI_OK(botan_pubkey_destroy, (pub1));
1✔
3562
         TEST_FFI_OK(botan_pubkey_destroy, (pub2));
1✔
3563
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey1));
1✔
3564
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey1));
1✔
3565
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey2));
2✔
3566
      }
6✔
3567
};
3568

3569
class FFI_McEliece_Test final : public FFI_Test {
1✔
3570
   public:
3571
      std::string name() const override { return "FFI McEliece"; }
1✔
3572

3573
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
3574
         botan_privkey_t priv;
1✔
3575
         if(TEST_FFI_INIT(botan_privkey_create_mceliece, (&priv, rng, 2048, 50))) {
1✔
3576
            botan_pubkey_t pub;
1✔
3577
            TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
3578

3579
            ffi_test_pubkey_export(result, pub, priv, rng);
1✔
3580

3581
            std::array<char, 32> namebuf{};
1✔
3582
            size_t name_len = namebuf.size();
1✔
3583
            if(TEST_FFI_OK(botan_pubkey_algo_name, (pub, namebuf.data(), &name_len))) {
1✔
3584
               result.test_eq("algo name", namebuf.data(), "McEliece");
2✔
3585
            }
3586

3587
            // TODO test KEM
3588

3589
            TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
3590
            TEST_FFI_OK(botan_privkey_destroy, (priv));
2✔
3591
         }
3592
      }
1✔
3593
};
3594

3595
class FFI_Ed25519_Test final : public FFI_Test {
1✔
3596
   public:
3597
      std::string name() const override { return "FFI Ed25519"; }
1✔
3598

3599
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
3600
         botan_pubkey_t pub;
1✔
3601
         botan_privkey_t priv;
1✔
3602

3603
         // From draft-koch-eddsa-for-openpgp-04
3604
         const std::vector<uint8_t> seed =
1✔
3605
            Botan::hex_decode("1a8b1ff05ded48e18bf50166c664ab023ea70003d78d9e41f5758a91d850f8d2");
1✔
3606
         const std::vector<uint8_t> pubkey =
1✔
3607
            Botan::hex_decode("3f098994bdd916ed4053197934e4a87c80733a1280d62f8010992e43ee3b2406");
1✔
3608
         const std::vector<uint8_t> message = Botan::hex_decode("4f70656e504750040016080006050255f95f9504ff0000000c");
1✔
3609
         const std::vector<uint8_t> exp_sig = Botan::hex_decode(
1✔
3610
            "56f90cca98e2102637bd983fdb16c131dfd27ed82bf4dde5606e0d756aed3366"
3611
            "d09c4fa11527f038e0f57f2201d82f2ea2c9033265fa6ceb489e854bae61b404");
1✔
3612

3613
         if(!TEST_FFI_INIT(botan_privkey_load_ed25519, (&priv, seed.data()))) {
1✔
3614
            return;
×
3615
         }
3616

3617
         uint8_t retr_privkey[64];
1✔
3618
         TEST_FFI_OK(botan_privkey_ed25519_get_privkey, (priv, retr_privkey));
1✔
3619

3620
         result.test_eq(nullptr, "Public key matches", retr_privkey + 32, 32, pubkey.data(), pubkey.size());
1✔
3621

3622
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
3623

3624
         uint8_t retr_pubkey[32];
1✔
3625
         TEST_FFI_OK(botan_pubkey_ed25519_get_pubkey, (pub, retr_pubkey));
1✔
3626
         result.test_eq(nullptr, "Public key matches", retr_pubkey, 32, pubkey.data(), pubkey.size());
1✔
3627

3628
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
3629
         TEST_FFI_OK(botan_pubkey_load_ed25519, (&pub, pubkey.data()));
1✔
3630

3631
         botan_pk_op_sign_t signer;
1✔
3632
         std::vector<uint8_t> signature;
1✔
3633

3634
         if(TEST_FFI_OK(botan_pk_op_sign_create, (&signer, priv, "SHA-256", 0))) {
1✔
3635
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message.data(), message.size()));
1✔
3636

3637
            size_t sig_len;
1✔
3638
            TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_len));
1✔
3639

3640
            signature.resize(sig_len);
1✔
3641

3642
            TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &sig_len));
1✔
3643
            signature.resize(sig_len);
1✔
3644

3645
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
2✔
3646
         }
3647

3648
         result.test_eq("Expected signature", signature, exp_sig);
2✔
3649

3650
         botan_pk_op_verify_t verifier;
1✔
3651

3652
         if(TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, "SHA-256", 0))) {
1✔
3653
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1✔
3654
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
1✔
3655

3656
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
2✔
3657
         }
3658

3659
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
3660
         TEST_FFI_OK(botan_privkey_destroy, (priv));
2✔
3661
      }
5✔
3662
};
3663

3664
class FFI_Ed448_Test final : public FFI_Test {
1✔
3665
   public:
3666
      std::string name() const override { return "FFI Ed448"; }
1✔
3667

3668
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
3669
         botan_pubkey_t pub;
1✔
3670
         botan_privkey_t priv;
1✔
3671

3672
         // RFC 8032: Testvector Ed448, 1 octet
3673
         const auto sk = Botan::hex_decode(
1✔
3674
            "c4eab05d357007c632f3dbb48489924d552b08fe0c353a0d4a1f00acda2c463afbea67c5e8d2877c5e3bc397a659949ef8021e954e0a12274e");
1✔
3675
         const auto pk_ref = Botan::hex_decode(
1✔
3676
            "43ba28f430cdff456ae531545f7ecd0ac834a55d9358c0372bfa0c6c6798c0866aea01eb00742802b8438ea4cb82169c235160627b4c3a9480");
1✔
3677
         const auto msg = Botan::hex_decode("03");
1✔
3678
         const auto sig_ref = Botan::hex_decode(
1✔
3679
            "26b8f91727bd62897af15e41eb43c377efb9c610d48f2335cb0bd0087810f4352541b143c4b981b7e18f62de8ccdf633fc1bf037ab7cd779805e0dbcc0aae1cbcee1afb2e027df36bc04dcecbf154336c19f0af7e0a6472905e799f1953d2a0ff3348ab21aa4adafd1d234441cf807c03a00");
1✔
3680

3681
         if(!TEST_FFI_INIT(botan_privkey_load_ed448, (&priv, sk.data()))) {
1✔
3682
            return;
×
3683
         }
3684

3685
         std::vector<uint8_t> retr_privkey(57);
1✔
3686
         TEST_FFI_OK(botan_privkey_ed448_get_privkey, (priv, retr_privkey.data()));
1✔
3687
         result.test_is_eq("Private key matches", retr_privkey, sk);
1✔
3688

3689
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
3690

3691
         std::vector<uint8_t> retr_pubkey(57);
1✔
3692
         TEST_FFI_OK(botan_pubkey_ed448_get_pubkey, (pub, retr_pubkey.data()));
1✔
3693
         result.test_is_eq("Public key matches", retr_pubkey, pk_ref);
1✔
3694

3695
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
3696
         TEST_FFI_OK(botan_pubkey_load_ed448, (&pub, pk_ref.data()));
1✔
3697

3698
         botan_pk_op_sign_t signer;
1✔
3699
         std::vector<uint8_t> signature;
1✔
3700

3701
         if(TEST_FFI_OK(botan_pk_op_sign_create, (&signer, priv, "Pure", 0))) {
1✔
3702
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, msg.data(), msg.size()));
1✔
3703

3704
            size_t sig_len;
1✔
3705
            TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_len));
1✔
3706

3707
            signature.resize(sig_len);
1✔
3708

3709
            TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &sig_len));
1✔
3710
            signature.resize(sig_len);
1✔
3711

3712
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
2✔
3713
         }
3714

3715
         result.test_eq("Expected signature", signature, sig_ref);
2✔
3716

3717
         botan_pk_op_verify_t verifier;
1✔
3718

3719
         if(TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, "Pure", 0))) {
1✔
3720
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, msg.data(), msg.size()));
1✔
3721
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
1✔
3722

3723
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
2✔
3724
         }
3725

3726
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
3727
         TEST_FFI_OK(botan_privkey_destroy, (priv));
2✔
3728
      }
7✔
3729
};
3730

3731
class FFI_X25519_Test final : public FFI_Test {
1✔
3732
   public:
3733
      std::string name() const override { return "FFI X25519"; }
1✔
3734

3735
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
3736
         // From RFC 8037
3737

3738
         const std::vector<uint8_t> a_pub_bits =
1✔
3739
            Botan::hex_decode("de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f");
1✔
3740
         const std::vector<uint8_t> b_priv_bits =
1✔
3741
            Botan::hex_decode("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a");
1✔
3742
         const std::vector<uint8_t> b_pub_bits =
1✔
3743
            Botan::hex_decode("8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a");
1✔
3744
         const std::vector<uint8_t> shared_secret_bits =
1✔
3745
            Botan::hex_decode("4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742");
1✔
3746

3747
         botan_privkey_t b_priv;
1✔
3748
         if(!TEST_FFI_INIT(botan_privkey_load_x25519, (&b_priv, b_priv_bits.data()))) {
1✔
3749
            return;
3750
         }
3751

3752
         std::vector<uint8_t> privkey_read(32);
1✔
3753
         TEST_FFI_OK(botan_privkey_x25519_get_privkey, (b_priv, privkey_read.data()));
1✔
3754
         result.test_eq("X25519 private key", privkey_read, b_priv_bits);
2✔
3755

3756
         std::vector<uint8_t> pubkey_read(32);
1✔
3757

3758
         botan_pubkey_t b_pub;
1✔
3759
         TEST_FFI_OK(botan_privkey_export_pubkey, (&b_pub, b_priv));
1✔
3760
         TEST_FFI_OK(botan_pubkey_x25519_get_pubkey, (b_pub, pubkey_read.data()));
1✔
3761
         result.test_eq("X25519 public key b", pubkey_read, b_pub_bits);
2✔
3762

3763
         botan_pubkey_t a_pub;
1✔
3764
         TEST_FFI_OK(botan_pubkey_load_x25519, (&a_pub, a_pub_bits.data()));
1✔
3765
         TEST_FFI_OK(botan_pubkey_x25519_get_pubkey, (a_pub, pubkey_read.data()));
1✔
3766
         result.test_eq("X25519 public key a", pubkey_read, a_pub_bits);
2✔
3767

3768
         botan_pk_op_ka_t ka;
1✔
3769
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka, b_priv, "Raw", 0));
1✔
3770

3771
         std::vector<uint8_t> shared_output(32);
1✔
3772
         size_t shared_len = shared_output.size();
1✔
3773
         TEST_FFI_OK(botan_pk_op_key_agreement,
1✔
3774
                     (ka, shared_output.data(), &shared_len, a_pub_bits.data(), a_pub_bits.size(), nullptr, 0));
3775

3776
         result.test_eq("Shared secret matches expected", shared_secret_bits, shared_output);
2✔
3777

3778
         TEST_FFI_OK(botan_pubkey_destroy, (a_pub));
1✔
3779
         TEST_FFI_OK(botan_pubkey_destroy, (b_pub));
1✔
3780
         TEST_FFI_OK(botan_privkey_destroy, (b_priv));
1✔
3781
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka));
2✔
3782
      }
7✔
3783
};
3784

3785
class FFI_X448_Test final : public FFI_Test {
1✔
3786
   public:
3787
      std::string name() const override { return "FFI X448"; }
1✔
3788

3789
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
3790
         // From RFC 7748 Section 6.2
3791
         const auto a_pub_ref = Botan::hex_decode(
1✔
3792
            "9b08f7cc31b7e3e67d22d5aea121074a273bd2b83de09c63faa73d2c22c5d9bbc836647241d953d40c5b12da88120d53177f80e532c41fa0");
1✔
3793
         const auto b_priv_ref = Botan::hex_decode(
1✔
3794
            "1c306a7ac2a0e2e0990b294470cba339e6453772b075811d8fad0d1d6927c120bb5ee8972b0d3e21374c9c921b09d1b0366f10b65173992d");
1✔
3795
         const auto b_pub_ref = Botan::hex_decode(
1✔
3796
            "3eb7a829b0cd20f5bcfc0b599b6feccf6da4627107bdb0d4f345b43027d8b972fc3e34fb4232a13ca706dcb57aec3dae07bdc1c67bf33609");
1✔
3797
         const auto shared_secret_ref = Botan::hex_decode(
1✔
3798
            "07fff4181ac6cc95ec1c16a94a0f74d12da232ce40a77552281d282bb60c0b56fd2464c335543936521c24403085d59a449a5037514a879d");
1✔
3799

3800
         botan_privkey_t b_priv;
1✔
3801
         if(!TEST_FFI_INIT(botan_privkey_load_x448, (&b_priv, b_priv_ref.data()))) {
1✔
3802
            return;
3803
         }
3804

3805
         std::vector<uint8_t> privkey_read(56);
1✔
3806
         TEST_FFI_OK(botan_privkey_x448_get_privkey, (b_priv, privkey_read.data()));
1✔
3807
         result.test_eq("X448 private key", privkey_read, b_priv_ref);
2✔
3808

3809
         std::vector<uint8_t> pubkey_read(56);
1✔
3810

3811
         botan_pubkey_t b_pub;
1✔
3812
         TEST_FFI_OK(botan_privkey_export_pubkey, (&b_pub, b_priv));
1✔
3813
         TEST_FFI_OK(botan_pubkey_x448_get_pubkey, (b_pub, pubkey_read.data()));
1✔
3814
         result.test_eq("X448 public key b", pubkey_read, b_pub_ref);
2✔
3815

3816
         botan_pubkey_t a_pub;
1✔
3817
         TEST_FFI_OK(botan_pubkey_load_x448, (&a_pub, a_pub_ref.data()));
1✔
3818
         TEST_FFI_OK(botan_pubkey_x448_get_pubkey, (a_pub, pubkey_read.data()));
1✔
3819
         result.test_eq("X448 public key a", pubkey_read, a_pub_ref);
2✔
3820

3821
         botan_pk_op_ka_t ka;
1✔
3822
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka, b_priv, "Raw", 0));
1✔
3823

3824
         std::vector<uint8_t> shared_output(56);
1✔
3825
         size_t shared_len = shared_output.size();
1✔
3826
         TEST_FFI_OK(botan_pk_op_key_agreement,
1✔
3827
                     (ka, shared_output.data(), &shared_len, a_pub_ref.data(), a_pub_ref.size(), nullptr, 0));
3828

3829
         result.test_eq("Shared secret matches expected", shared_secret_ref, shared_output);
2✔
3830

3831
         TEST_FFI_OK(botan_pubkey_destroy, (a_pub));
1✔
3832
         TEST_FFI_OK(botan_pubkey_destroy, (b_pub));
1✔
3833
         TEST_FFI_OK(botan_privkey_destroy, (b_priv));
1✔
3834
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka));
2✔
3835
      }
7✔
3836
};
3837

3838
/**
3839
 * Base class for roundtrip tests of FFI bindings for Key Encapsulation Mechanisms.
3840
 */
3841
class FFI_KEM_Roundtrip_Test : public FFI_Test {
6✔
3842
   protected:
3843
      using privkey_loader_fn_t = int (*)(botan_privkey_t*, const uint8_t[], size_t, const char*);
3844
      using pubkey_loader_fn_t = int (*)(botan_pubkey_t*, const uint8_t[], size_t, const char*);
3845

3846
   protected:
3847
      virtual const char* algo() const = 0;
3848
      virtual privkey_loader_fn_t private_key_load_function() const = 0;
3849
      virtual pubkey_loader_fn_t public_key_load_function() const = 0;
3850
      virtual std::vector<const char*> modes() const = 0;
3851

3852
   public:
3853
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
3✔
3854
         for(const auto* mode : modes()) {
34✔
3855
            // generate a key pair
3856
            botan_privkey_t priv;
31✔
3857
            botan_pubkey_t pub;
31✔
3858
            if(!TEST_FFI_INIT(botan_privkey_create, (&priv, algo(), mode, rng))) {
31✔
3859
               continue;
×
3860
            }
3861
            TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
31✔
3862

3863
            // raw-encode the key pair
3864
            ViewBytesSink priv_bytes;
31✔
3865
            ViewBytesSink pub_bytes;
31✔
3866
            TEST_FFI_OK(botan_privkey_view_raw, (priv, priv_bytes.delegate(), priv_bytes.callback()));
31✔
3867
            TEST_FFI_OK(botan_pubkey_view_raw, (pub, pub_bytes.delegate(), pub_bytes.callback()));
31✔
3868

3869
            // decode the key pair from raw encoding
3870
            botan_privkey_t priv_loaded;
31✔
3871
            botan_pubkey_t pub_loaded;
31✔
3872
            TEST_FFI_OK(private_key_load_function(),
31✔
3873
                        (&priv_loaded, priv_bytes.get().data(), priv_bytes.get().size(), mode));
3874
            TEST_FFI_OK(public_key_load_function(),
31✔
3875
                        (&pub_loaded, pub_bytes.get().data(), pub_bytes.get().size(), mode));
3876

3877
            // re-encode and compare to the first round
3878
            ViewBytesSink priv_bytes2;
31✔
3879
            ViewBytesSink pub_bytes2;
31✔
3880
            TEST_FFI_OK(botan_privkey_view_raw, (priv_loaded, priv_bytes2.delegate(), priv_bytes2.callback()));
31✔
3881
            TEST_FFI_OK(botan_pubkey_view_raw, (pub_loaded, pub_bytes2.delegate(), pub_bytes2.callback()));
31✔
3882
            result.test_eq("private key encoding", priv_bytes.get(), priv_bytes2.get());
62✔
3883
            result.test_eq("public key encoding", pub_bytes.get(), pub_bytes2.get());
62✔
3884

3885
            // KEM encryption (using the loaded public key)
3886
            botan_pk_op_kem_encrypt_t kem_enc;
31✔
3887
            TEST_FFI_OK(botan_pk_op_kem_encrypt_create, (&kem_enc, pub_loaded, "Raw"));
31✔
3888

3889
            // explicitly query output lengths
3890
            size_t shared_key_length = 0;
31✔
3891
            size_t ciphertext_length = 0;
31✔
3892
            TEST_FFI_OK(botan_pk_op_kem_encrypt_shared_key_length, (kem_enc, 0, &shared_key_length));
31✔
3893
            TEST_FFI_OK(botan_pk_op_kem_encrypt_encapsulated_key_length, (kem_enc, &ciphertext_length));
31✔
3894

3895
            // check that insufficient buffer space is handled correctly
3896
            size_t shared_key_length_out = 0;
31✔
3897
            size_t ciphertext_length_out = 0;
31✔
3898
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
31✔
3899
                        botan_pk_op_kem_encrypt_create_shared_key,
3900
                        (kem_enc,
3901
                         rng,
3902
                         nullptr /* no salt */,
3903
                         0,
3904
                         0 /* default key length */,
3905
                         nullptr,
3906
                         &shared_key_length_out,
3907
                         nullptr,
3908
                         &ciphertext_length_out));
3909

3910
            // TODO: should this report both lengths for usage convenience?
3911
            result.confirm("at least one buffer length is reported",
62✔
3912
                           shared_key_length_out == shared_key_length || ciphertext_length_out == ciphertext_length);
31✔
3913

3914
            // allocate buffers (with additional space) and perform the actual encryption
3915
            shared_key_length_out = shared_key_length * 2;
31✔
3916
            ciphertext_length_out = ciphertext_length * 2;
31✔
3917
            Botan::secure_vector<uint8_t> shared_key(shared_key_length_out);
31✔
3918
            std::vector<uint8_t> ciphertext(ciphertext_length_out);
31✔
3919
            TEST_FFI_OK(botan_pk_op_kem_encrypt_create_shared_key,
31✔
3920
                        (kem_enc,
3921
                         rng,
3922
                         nullptr /* no salt */,
3923
                         0,
3924
                         0 /* default key length */,
3925
                         shared_key.data(),
3926
                         &shared_key_length_out,
3927
                         ciphertext.data(),
3928
                         &ciphertext_length_out));
3929
            result.test_eq("shared key length", shared_key_length, shared_key_length_out);
31✔
3930
            result.test_eq("ciphertext length", ciphertext_length, ciphertext_length_out);
31✔
3931
            shared_key.resize(shared_key_length_out);
31✔
3932
            ciphertext.resize(ciphertext_length_out);
31✔
3933
            TEST_FFI_OK(botan_pk_op_kem_encrypt_destroy, (kem_enc));
31✔
3934

3935
            // KEM decryption (using the generated private key)
3936
            botan_pk_op_kem_decrypt_t kem_dec;
31✔
3937
            TEST_FFI_OK(botan_pk_op_kem_decrypt_create, (&kem_dec, priv, "Raw"));
31✔
3938
            size_t shared_key_length2 = 0;
31✔
3939
            TEST_FFI_OK(botan_pk_op_kem_decrypt_shared_key_length, (kem_dec, shared_key_length, &shared_key_length2));
31✔
3940
            result.test_eq("shared key lengths are consistent", shared_key_length, shared_key_length2);
31✔
3941

3942
            // check that insufficient buffer space is handled correctly
3943
            shared_key_length_out = 0;
31✔
3944
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
31✔
3945
                        botan_pk_op_kem_decrypt_shared_key,
3946
                        (kem_dec,
3947
                         nullptr /* no salt */,
3948
                         0,
3949
                         ciphertext.data(),
3950
                         ciphertext.size(),
3951
                         0 /* default length */,
3952
                         nullptr,
3953
                         &shared_key_length_out));
3954
            result.test_eq("reported buffer length requirement", shared_key_length, shared_key_length_out);
31✔
3955

3956
            // allocate buffer (double the size) and perform the actual decryption
3957
            shared_key_length_out = shared_key_length * 2;
31✔
3958
            Botan::secure_vector<uint8_t> shared_key2(shared_key_length_out);
31✔
3959
            TEST_FFI_OK(botan_pk_op_kem_decrypt_shared_key,
31✔
3960
                        (kem_dec,
3961
                         nullptr /* no salt */,
3962
                         0,
3963
                         ciphertext.data(),
3964
                         ciphertext.size(),
3965
                         0 /* default length */,
3966
                         shared_key2.data(),
3967
                         &shared_key_length_out));
3968
            result.test_eq("shared key output length", shared_key_length, shared_key_length_out);
31✔
3969
            shared_key2.resize(shared_key_length_out);
31✔
3970
            TEST_FFI_OK(botan_pk_op_kem_decrypt_destroy, (kem_dec));
31✔
3971

3972
            // final check and clean up
3973
            result.test_eq("shared keys match", shared_key, shared_key2);
62✔
3974

3975
            TEST_FFI_OK(botan_pubkey_destroy, (pub));
31✔
3976
            TEST_FFI_OK(botan_pubkey_destroy, (pub_loaded));
31✔
3977
            TEST_FFI_OK(botan_privkey_destroy, (priv));
31✔
3978
            TEST_FFI_OK(botan_privkey_destroy, (priv_loaded));
62✔
3979
         }
217✔
3980
      }
3✔
3981
};
3982

3983
/**
3984
 * Base class for roundtrip tests of FFI bindings for Signature Mechanisms.
3985
 */
3986
class FFI_Signature_Roundtrip_Test : public FFI_Test {
4✔
3987
   protected:
3988
      using privkey_loader_fn_t = int (*)(botan_privkey_t*, const uint8_t[], size_t, const char*);
3989
      using pubkey_loader_fn_t = int (*)(botan_pubkey_t*, const uint8_t[], size_t, const char*);
3990

3991
   protected:
3992
      virtual const char* algo() const = 0;
3993
      virtual privkey_loader_fn_t private_key_load_function() const = 0;
3994
      virtual pubkey_loader_fn_t public_key_load_function() const = 0;
3995
      virtual std::vector<const char*> modes() const = 0;
3996
      virtual const char* hash_algo_or_padding() const = 0;
3997

3998
   public:
3999
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
2✔
4000
         const std::vector<uint8_t> message1 = {'H', 'e', 'l', 'l', 'o', ' '};
2✔
4001
         const std::vector<uint8_t> message2 = {'W', 'o', 'r', 'l', 'd', '!'};
2✔
4002

4003
         for(const auto* mode : modes()) {
17✔
4004
            // generate a key pair
4005
            botan_privkey_t priv;
15✔
4006
            botan_pubkey_t pub;
15✔
4007
            if(!TEST_FFI_INIT(botan_privkey_create, (&priv, algo(), mode, rng))) {
15✔
4008
               continue;
×
4009
            }
4010
            TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
15✔
4011

4012
            // raw-encode the key pair
4013
            ViewBytesSink priv_bytes;
15✔
4014
            ViewBytesSink pub_bytes;
15✔
4015
            TEST_FFI_OK(botan_privkey_view_raw, (priv, priv_bytes.delegate(), priv_bytes.callback()));
15✔
4016
            TEST_FFI_OK(botan_pubkey_view_raw, (pub, pub_bytes.delegate(), pub_bytes.callback()));
15✔
4017

4018
            // decode the key pair from raw encoding
4019
            botan_privkey_t priv_loaded;
15✔
4020
            botan_pubkey_t pub_loaded;
15✔
4021
            TEST_FFI_OK(private_key_load_function(),
15✔
4022
                        (&priv_loaded, priv_bytes.get().data(), priv_bytes.get().size(), mode));
4023
            TEST_FFI_OK(public_key_load_function(),
15✔
4024
                        (&pub_loaded, pub_bytes.get().data(), pub_bytes.get().size(), mode));
4025

4026
            // re-encode and compare to the first round
4027
            ViewBytesSink priv_bytes2;
15✔
4028
            ViewBytesSink pub_bytes2;
15✔
4029
            TEST_FFI_OK(botan_privkey_view_raw, (priv_loaded, priv_bytes2.delegate(), priv_bytes2.callback()));
15✔
4030
            TEST_FFI_OK(botan_pubkey_view_raw, (pub_loaded, pub_bytes2.delegate(), pub_bytes2.callback()));
15✔
4031
            result.test_eq("private key encoding", priv_bytes.get(), priv_bytes2.get());
30✔
4032
            result.test_eq("public key encoding", pub_bytes.get(), pub_bytes2.get());
30✔
4033

4034
            // Signature Creation (using the loaded private key)
4035
            botan_pk_op_sign_t signer;
15✔
4036
            TEST_FFI_OK(botan_pk_op_sign_create, (&signer, priv_loaded, hash_algo_or_padding(), 0));
15✔
4037

4038
            // explicitly query the signature output length
4039
            size_t sig_output_length = 0;
15✔
4040
            TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_output_length));
15✔
4041

4042
            // pass a message to the signer
4043
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message1.data(), message1.size()));
15✔
4044
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message2.data(), message2.size()));
15✔
4045

4046
            // check that insufficient buffer space is handled correctly
4047
            size_t sig_output_length_out = 0;
15✔
4048
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
15✔
4049
                        botan_pk_op_sign_finish,
4050
                        (signer, rng, nullptr, &sig_output_length_out));
4051
            result.test_eq("reported sig lengths are equal", sig_output_length, sig_output_length_out);
15✔
4052

4053
            // Recreate signer and try again
4054
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
15✔
4055
            TEST_FFI_OK(botan_pk_op_sign_create, (&signer, priv_loaded, hash_algo_or_padding(), 0));
15✔
4056
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message1.data(), message1.size()));
15✔
4057
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message2.data(), message2.size()));
15✔
4058

4059
            // allocate buffers (with additional space) and perform the actual signing
4060
            sig_output_length_out = sig_output_length * 2;
15✔
4061
            Botan::secure_vector<uint8_t> signature(sig_output_length_out);
15✔
4062
            TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &sig_output_length_out));
15✔
4063
            result.test_eq("signature length", sig_output_length, sig_output_length_out);
15✔
4064
            signature.resize(sig_output_length_out);
15✔
4065
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
15✔
4066

4067
            // Signature verification (using the generated public key)
4068
            botan_pk_op_verify_t verifier;
15✔
4069
            TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, hash_algo_or_padding(), 0));
15✔
4070
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message1.data(), message1.size()));
15✔
4071
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message2.data(), message2.size()));
15✔
4072

4073
            // Verify signature
4074
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
15✔
4075
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
15✔
4076

4077
            // Verify signature with wrong message (only first half)
4078
            TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, hash_algo_or_padding(), 0));
15✔
4079
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message1.data(), message1.size()));
15✔
4080
            TEST_FFI_RC(
15✔
4081
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
4082
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
15✔
4083

4084
            // Cleanup
4085
            TEST_FFI_OK(botan_pubkey_destroy, (pub));
15✔
4086
            TEST_FFI_OK(botan_pubkey_destroy, (pub_loaded));
15✔
4087
            TEST_FFI_OK(botan_privkey_destroy, (priv));
15✔
4088
            TEST_FFI_OK(botan_privkey_destroy, (priv_loaded));
30✔
4089
         }
77✔
4090
      }
4✔
4091
};
4092

4093
class FFI_Kyber512_Test final : public FFI_Test {
1✔
4094
   public:
4095
      std::string name() const override { return "FFI Kyber512"; }
1✔
4096

4097
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
4098
         const std::vector<uint8_t> a_pub_bits = Botan::hex_decode(
1✔
4099
            "5fc44b99d7584f38cd28360cc5625a905b96af12930ed5b5fe2a82fc5aa7dc4b829fe37635f13f5af2a6d3081dad878785698a0aa914374c4e43b89f094a7892aa149a38b49c06a068d829a8d249e753a375d097a0f162e6c3a4dfe8c79761410c605ed3899a3fc44378e14f28879e8f148077e6bc3bb2ae56178c491611bf6aaf5f9a9cb9b5659223007940bcd6f8a23280a56015330e8577259587b12606f4c937ea13606cb3bb046066ad294261e2b22022bcc74678a5520570d88e4ceb42692631e7e3711c4b2fd5347f0328598340cb3c65c8f55ac02716831094cb6eb90f175b173d9c650329aaf513633633bb2ce6858e7447abc41b6fb06da8782572c332b09660366926bf529ed8caaa6243ccdb152b36ba6e47c714145c86f5b3b61de84ef1470d03fa0135e35194fa1fb3bc860fa500d1299aee88ce56054376c1199c553dd90a8d6f9cc763c811d0c66da6f851abf1056635a34a68aa7815868f153a3a5c77fcc8b1eb1807fbf62a6fb43b355700e78230943a2ba1e11b181345b11b4d46266e7b359f074a500c8857d79ba60f64262d662ccd9c8489a4c19df67437db193f95b9765181d9152262b1166f97be53497f001cb1be79024d6a2289bcc704e1b1d821015366a3cc8a484e6bc2e1f1b889f19323e3101aa09ad9ea62ba4005039bbfb5998055f93fbf77b14433116d5958422654dada1127213f02b78717a5a0454271d5b0c02517a6c27a3c3610101d753c09a25571775477dc13b2e404db4965b9a9350330c73a8a3642d39af8a23839ab85c6355b12f279f849813c280d54c5913e99b6946a0aaf012c8cab025396b255f002d837c761d42a4aeb38c5f456aaf79e162700c6b4048eca6f9a7367f90238d67bcf8e6a0d8a553c071522f9d2394e28483d2048be2a8f9c8c8e39991a41273c7eacaefc6a308be870b45b41176412954a1a0fd83d362a5ab288663dec5456b6286d0b2cecb01922fb3d473802ea2b86639bce02450339261cffb114e1e725e90677826a1688f686b29a78779c9822315dafc55753e98c8ed3221f2b3220805c8a28983355207da36fb72f9bc85c0a13b9d041586fd583feb12afd5a402dd33b43543f5fa4eb436c8d");
1✔
4100
         const std::vector<uint8_t> b_priv_bits = Botan::hex_decode(
1✔
4101
            "cf8c33fabbc3e6685f60779a456412e9060a792a3f67d90389815773e06dd2071e3906a1159921485b6221b73dfc723da9d45bbc99523c55b203f81856ba8d38f731c6594a0a9796f485c7cad02ee3a80a737cada2e40b580a1060a1364d365169539ce4d800eab9723153c5b266853b3a33112ccb03491e77f57aeb74c7670426c5bb02615b1907b353beda51f38a788774b1c9eb3c49f89df59ba00e196f180a37fd9acd6691b493d78f95a49906b26fa46125663a37b0b2614197473ba012956bc9348a9afc8907527bb7c2dcbd384b85630b3687096ffd9a93d603121706c06c05baca286bebea0ecdf391ba489d7ff6a7a1c7c3dd0c521c02bb3fa7c3e5b98f19509486ab33f1597346b03c7197865792896c1a553e8379c6f51729e9907d09b4fbc5b4279298f60195998225d95c2aef335c9e6a26f4988e0f19c307fc00f8aa0b21090daab0c540363f121044729322bebb97427227e8b01705826879ab4d42e30bf191068b705527a3ae9a5b3e30371c50b5b75d189903e60175f904c1488be7e872d3dc3be1bb31921027cb075f6bb051c28980c65b9959e8acf2d38671d27285d03419512008335492476e0c23ac6263425f8a65fbc1a38110b38db641f3a9872fa312d26b9c81f11de1d9378b629e9bdc7ca31496ac9511bff42a7a96b28f740a84c2904fe4056ca1989b28439222cee31c554e982d318366037070c4bb778b261e3479c605317922947e4340560732ca8fda63e2a7ac8cc67015e97d308c361e634f8cdb1e836b572367334c949011a9c52f391194baa91f08a71e847d3be202554ab1c4f871c9b26db5f4992db5b3688b1ea877925d02895d470b9659c51213b206b82de489455c6502856b841d000aab3c065ff6720f9222914b6421224d716684a7762fc98ba7f4655f4cfa82fc52472b01b1e22255fc990b20240b802034c99777b71b5c0d8a4c733b69d5f76ddfdb1128da3e194c1169a924f5d868d1dbc4ddcb30197c25005391c1fcc098010af1e9ab72bc8c3bd1c58b15583035bc2dd0898e148fb20b8c502a31489b00dd645ab24709bff5c37d1a7fee5c56d7490ae77a61b1c5951e359d2a89a946f200b1f647c0283de5d403282c693dfccca1c1b54804ab86f36e1f16550a77b75328916c770c225b142dfb750659ba6d532003f8b6d48825b122682051acda7c20f09117b3e5c93e8c1bd446672512947d964f0484a05b85af82f4401d323484d23373c0c6c8990a3fba70f8a2c13e7350ce945c8f4882b1929f10d18bb45a883c116d1ce06493a50580069743d26062da9171006405d27e2f7220a897024dca507b737a6c32ba3d925e15c9789589507a768ca075beb2fb17a17144c52810566a972dba60ad806a2ff15957a3bbd85a8f1ef34445d18cc668ca7a8ac5fc7545934221a4c92c60024069cc34d592ac30c88c3376bc133622e2b192e9b4b60a9a84e5304873e62d6f98103cc8c43f6024fb7739418cc2602b21416040220cb3ec588679c033d61a3c697c403463c17c13a63d7304044a9f024b907b440b39b7b6423723dda943447a900af0170144755d9a968f634e26f892ee19cd00a97b48d5041b4473ec8741e077126f85bbb8334b8b2cbd63429afa543a540740c1e56893f6b2f2f7c42a12bcd48c647726cca06841212656ff9b3d3e799e92c48414d653a61ca8dc2c49b7342469a868b7938db8d7844e6311134b2c30538ffa927ee3961a44e0a66438c5643aa1d13658fe8a7c2a84702a8211422831994ca1e801131ab88a25162d3cd988ebe09e6cb01ca7324256691635c536f576990664841b7c89b0dc325472271fdc2b0824a9514b5eb46a743f9734ee20648a407f5d505cc9614748f344a16950b5483e45516236d43afd1494d564c23e20b64a124593604404c879a776a5bd9399629600e86aa1a641be7a13418cda318bbc3af0c2a66c999841251a1c2868b05028f6a56a72c33d8a653c77f69af4247dc6f80d329921ce3355894605c35372ddf0c84cacb42d6ccbdf39a55b672a891749622a011747a01625764c788413f2c0fd877275c88fd5730fa9e87a3c783702223a4443525cc5b5381c976dd9cb08ac47490125ca5c70baac01ff9143bc40ceeb2a16c1529d70c07074a0013749656ff4b16d890868b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb3338e5d6352d5a5006d7cd901489e7f851711c08e00cd4162ccfc2564d5893d52b2c7300e2d894b0eaa40a6ab254506d8c1176a33c4a1b2879604b1b80df48d31dd");
1✔
4102
         const std::vector<uint8_t> b_pub_bits = Botan::hex_decode(
1✔
4103
            "ee5c56d7490ae77a61b1c5951e359d2a89a946f200b1f647c0283de5d403282c693dfccca1c1b54804ab86f36e1f16550a77b75328916c770c225b142dfb750659ba6d532003f8b6d48825b122682051acda7c20f09117b3e5c93e8c1bd446672512947d964f0484a05b85af82f4401d323484d23373c0c6c8990a3fba70f8a2c13e7350ce945c8f4882b1929f10d18bb45a883c116d1ce06493a50580069743d26062da9171006405d27e2f7220a897024dca507b737a6c32ba3d925e15c9789589507a768ca075beb2fb17a17144c52810566a972dba60ad806a2ff15957a3bbd85a8f1ef34445d18cc668ca7a8ac5fc7545934221a4c92c60024069cc34d592ac30c88c3376bc133622e2b192e9b4b60a9a84e5304873e62d6f98103cc8c43f6024fb7739418cc2602b21416040220cb3ec588679c033d61a3c697c403463c17c13a63d7304044a9f024b907b440b39b7b6423723dda943447a900af0170144755d9a968f634e26f892ee19cd00a97b48d5041b4473ec8741e077126f85bbb8334b8b2cbd63429afa543a540740c1e56893f6b2f2f7c42a12bcd48c647726cca06841212656ff9b3d3e799e92c48414d653a61ca8dc2c49b7342469a868b7938db8d7844e6311134b2c30538ffa927ee3961a44e0a66438c5643aa1d13658fe8a7c2a84702a8211422831994ca1e801131ab88a25162d3cd988ebe09e6cb01ca7324256691635c536f576990664841b7c89b0dc325472271fdc2b0824a9514b5eb46a743f9734ee20648a407f5d505cc9614748f344a16950b5483e45516236d43afd1494d564c23e20b64a124593604404c879a776a5bd9399629600e86aa1a641be7a13418cda318bbc3af0c2a66c999841251a1c2868b05028f6a56a72c33d8a653c77f69af4247dc6f80d329921ce3355894605c35372ddf0c84cacb42d6ccbdf39a55b672a891749622a011747a01625764c788413f2c0fd877275c88fd5730fa9e87a3c783702223a4443525cc5b5381c976dd9cb08ac47490125ca5c70baac01ff9143bc40ceeb2a16c1529d70c07074a0013749656ff4b16d890868b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb333");
1✔
4104

4105
         botan_privkey_t b_priv;
1✔
4106
         if(!TEST_FFI_INIT(botan_privkey_load_kyber, (&b_priv, b_priv_bits.data(), 1632))) {
1✔
4107
            return;
×
4108
         }
4109

4110
         ViewBytesSink privkey_read;
1✔
4111
         ViewBytesSink privkey_read_raw;
1✔
4112
         TEST_FFI_OK(botan_privkey_view_kyber_raw_key, (b_priv, privkey_read.delegate(), privkey_read.callback()));
1✔
4113
         TEST_FFI_OK(botan_privkey_view_raw, (b_priv, privkey_read_raw.delegate(), privkey_read_raw.callback()));
1✔
4114
         result.test_eq("kyber512 private key", privkey_read.get(), b_priv_bits);
2✔
4115
         result.test_eq("kyber512 private key raw", privkey_read_raw.get(), b_priv_bits);
2✔
4116

4117
         ViewBytesSink pubkey_read;
1✔
4118
         ViewBytesSink pubkey_read_raw;
1✔
4119

4120
         botan_pubkey_t b_pub;
1✔
4121
         TEST_FFI_OK(botan_privkey_export_pubkey, (&b_pub, b_priv));
1✔
4122
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (b_pub, pubkey_read.delegate(), pubkey_read.callback()));
1✔
4123
         TEST_FFI_OK(botan_pubkey_view_raw, (b_pub, pubkey_read_raw.delegate(), pubkey_read_raw.callback()));
1✔
4124
         result.test_eq("kyber512 public key b", pubkey_read.get(), b_pub_bits);
2✔
4125
         result.test_eq("kyber512 raw public key b", pubkey_read_raw.get(), b_pub_bits);
2✔
4126

4127
         botan_pubkey_t a_pub;
1✔
4128
         TEST_FFI_OK(botan_pubkey_load_kyber, (&a_pub, a_pub_bits.data(), 800));
1✔
4129
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (a_pub, pubkey_read.delegate(), pubkey_read.callback()));
1✔
4130
         result.test_eq("kyber512 public key a", pubkey_read.get(), a_pub_bits);
2✔
4131

4132
         TEST_FFI_OK(botan_pubkey_destroy, (a_pub));
1✔
4133
         TEST_FFI_OK(botan_pubkey_destroy, (b_pub));
1✔
4134
         TEST_FFI_OK(botan_privkey_destroy, (b_priv));
2✔
4135
      }
7✔
4136
};
4137

4138
class FFI_Kyber768_Test final : public FFI_Test {
1✔
4139
   public:
4140
      std::string name() const override { return "FFI Kyber768"; }
1✔
4141

4142
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
4143
         const std::vector<uint8_t> a_pub_bits = Botan::hex_decode(
1✔
4144
            "38d4851e5c010da39a7470bc1c80916f78c7bd5891dcd3b1ea84b6f051b346b803c1b97c94604020b7279b27836c3049ea0b9a3758510b7559593237ade72587462206b709f365848c96559326a5fe6c6f4cf531fd1a18477267f66ba14af20163c41f1138a01995147f271ddfc2be5361b281e6029d210584b2859b7383667284f767bb32782baad10933da0138a3a0660a149531c03f9c8ffccb33e3c3a5a7984e21fab26aa72a8a6b942f265e52551a9c800e5a44805f0c0141a05554213387f105df56458496bd8f469051886da223cb9fe78e7b390bf94b0a937691af9550082b76d045cb4d29c23c67942608d078a1c80f24767a945d19f077d82c9b9b197073464abe69cf7c5626177308f384672d5263b0c4826db4470e1a70e4751e3918abe8fcbc3bc0531ae89e5512214b5cc94a16a014bcb3826c79fbf4add0825eeefbab88cb7cff37bb8d491f8de902578a1e961655565b7718782a23504fdc13c783f130e177925e305d1fbc63cc8c15c2c67f85500cca785de9f480490558ef71aaf0fb5b513914401269b309c4c59c64d2a757d8855f58465615925f1ea6812cb143fff383e1048e285118bf932944b86fbdf4b1b9e65685664a07775c46952aaada1168f54b47c7a231e7355c64637467b5a3c09cab67bb35f58640c2726283bb63530a15f66eca48a840c00ca8862e283c73bfbb413a2915b8d1159a043f12c59bfa828248249b76106faa61a127a0280c586350e7a42cb74ca49cabd606891ec7cb8e84affe4b2e14c71658332b755611bab7977fa76ce736b21ed34a17ac0ec3561ca9b282d4a2bc407697924b1cf918ba83d3a4fdc82564c95bd904bdeee91ed6ccb36baa88a05c80712901bf280aee6538ec2078c2a84ee5862fc137cd92e97968d69fc3453a1e1cb161c50c9f2473a0d09037b188a0fa01efc344c2ac8fe8592b0a58456662a95033659a158a2d90a6e50c253a87975785ce29c4570000a154d4b3b2c642205c8c7cf9ac6b1071fbb368ab950a744b88c95ba5243017831120a9048338d29847830d12a933a09abd21a46b828cb14e808cd35129c9dc6e5b931d4a126fefe07909618e2b4586e7b6b424963b7323ba505ba112bb9b834a7d1b78ad0df53d556a1c69369f09148b1dc9938df59223f087fd6833be5b2bc2651fe58911ac01467f9297dfdc22b41a0f1702718710b78cf35b1865813a896d45214d338155b6c043c532330c002d520739467a504a866637fb3451c849f8f83e6a94147f168da53acdf9d8affd968a84124a9abc09af960cd3b29f2344831bb41e67605eebf00df202857117399dd748b6514aed61bb2f6cb841d168d5f35e20054573a331cd4882a04b072c179158825bcf471266da0dcceab1a021c73254751d5a161c1a92062c220a217a69d9823314b4de996fe8d45f6db5af16c1561495a4c43090bc394c94e1b0ec738eb56267201c2ecd1c7b4993c0efc0284bdc9a091c294f95703a7178822c8a95b79b1e4591e0998d893875c1a879c08a073cc67df426bba792c18ae6c1feba879bec54812c2affa012973b700ad48e271078280864268600a7aa309eaa1098750a0f8a522eb929577b412f7855613688b72f9bc85c0a13b9d041586fd583feb12afd5a402dd33b43543f5fa4eb436c8d");
1✔
4145
         const std::vector<uint8_t> b_priv_bits = Botan::hex_decode(
1✔
4146
            "9bc4986eec853ac90896c7300f914216773baa687f57f81f94e6b5a26515c0a74c73b19dc6b9888755a1d1db6a1128b02c5144ed9ba75e874a68a14dee3a9bf770b767121fbbdb292e097444757fe2f324a875b0a07a4fbdcc8fd6137fce80c69f412be103b01f8b3bed392600f830d1b20d73ca0b386666d16c5d96db10e309041890bfd4ab7bdec27191979abe7637e76153918cc2af1545724abfd95ca496281a0cc6ca63a87819b75aa86452e5027d429cad64a9c029112a3a7b9fb993c6a6d0671f6c9e24986b83f67cf1236d94c051559616826a947eaa15074025d1ab810c80220e8a8c2c222d4e8594bf35811b444a57e9bf94fb2479d174d6b38c8c3b4730437c244b2513232ec798adec4a8e597bca83bca5948468f93c022ba65311a91e3d95c299c70c3f1a43cd0c83dd500bd2211b9b385b82f2c003b10d295765b3e367563a5f19033fc1231db9620f835c95f5a261190a5da1c24ed528316f0a1520d223e208a8c99b24d28598ade74fc88370e7f45102c5a6411891c661b356b9a32e1cc0fafaa085d7670e8bcb5e768eb255204f2445b5b73b04079881903a553c865744343a925c7899777b1c9dd5579a481512f8157061606a9a67c041d38bc179048be17dd9e19dc0a572bce595afa3b68ff21bf78a63a7560636b6bb01ec3362e2aaabc8965818b7f2417ca8f66a5a2a67f72a3931e125d638a872862a7b680a54aa1f25d90dbd567635ec6664919e29517325a5c5048cc8d1c31af5e4866e85025b9184a7b75ed7f2c9c8d88259fa2ec5b05ed3751584f88308c67ff1a72fa2453305160baf404db7d4be56f1547b20bb7dec23f02b10db584b28ca40d8b39c1c3ab9f3d7bbda0822604ca48f26694d69810aa888ae3c0983c5ba4cb74211f7a5361ccdee694f4202aebe13a75b1b2dda1b3232376be213582afc4fde3474766671fe865e2fd98384eb65b2f349f1e24269b91bd9d08c80849735a9951304afd130b5c2211314630aed4b6ac3b1252a0999ff5a3ec26a283342389d219fd243706128b389eb2334fb2a6184a4eab6735d7428df5633ce030b8f853ee47c4401fc5d06b43c9b66b7aeeb23b5f000a30a6f88f027ee3994fe8b63e51b83bc24bb733a3773a35cbe138f6d9c91a3a3898bca6776030d740ac355176547d624719656a9a44e91c63faf7699dc6c2c45575718d48828828b39043c2fda2af416837efc38d17c56d4b63c63a5ab43434647d029f7b236b288958f06910763610f8b2f027a8dcd780039ab34a6871427476ff6500240e83b87c95dcfa45ac5315ef34b343fb609eb296e915c849bb8c57f57c69b177eaa8456377403fe8c6627a3282d45308f675d67085a15f0b1b55aa2a8f21afd6c05c3c00e9eb8c32418cb41963ce427b43e7545c58325c7b9368db2333de424dbeb3430f007d18a68d73b7dc67960b28206a68a1be400a770b5cd9d45a72824ca00345ac56491c1414fe5287a2eb2ad61f3bdcd0c84c335b04a703425d79dbd02b0a0e90de5b331c3c29f6562969e04cdf7095b2a7646b3d006b0b83cb68580b5ccb71de1b4d9f131bac133d6088e10613a00599d81d4818403a4bea83905304cc45ca645a9b2c6484cf9490f1755c744d9988ed60475e6ae44355ed15c7b549366f29581ec2721fd6704e0ca3f878812805675141c0a15a7b7ac35a9e3f8b2a010bc184981c57852895b2695d56131e32326717f6b101df1bc82b3ba0222d52656d118538c4ca3416be1c76ba37a9901a36e4883be6c541f2bbb561818cd2f136b98f658250545c1fa5bcfdc04374016db1c5132447fd6d568866451c25412f72967de868eaeb9c546fb40ea88cd84a1a586ca51c74bc9c3e56e104323afb658d1ac003151bdc35879a4b6762648bff0caa682f1b3319805d2326d5a46af832aacefbaa1ea820568ea3925870e9b6577eb93898e1b0cfbd1c995cb4cd6cabb979813819749b40a9952f50e97c4365e777dcfe9084219294c205acb350e07db9c98f53444546460962e2bf5aa1cc12273d882fcc215a7397b3f9b307c56b9a0429b30f88453a2376669b28b4bc4b84b51714b6652b7a1a0b53e9ec61b55ca51cdb38243239ee5f18243e515f178768a888f475b3d9060136bb22ee355b3da02c16ad83bdbb4aaa13809cb4bcd5bb53710737d3883632f9254e3336af61621a376720572450e3937c0d930e349adbfa7642ff822b9135e18f943e0178617604d10e0c09ffb3e09783c09d12cbe93311757af9857b77d1488ed39321ca97c3745c5bc176ca81274c8321bcb2029938b32bbd01aec137032f9760849701649120050b50d8353c36b8bb7724a67e7660fc93324065d63912c35a86d8fe60683067bbd2685c552ad8c65c77c57c937676fd61595c453174bf996e9d3a9ccb837b25464115c1ba3343ac097b80735aed3225091167cd8f841ffe49c5e698ef542124253084623179394433a4b61547b9ee09c98c2736ea086bd69d1bc7ae68a6ae5ce682a215860006b4604dee45a3e212f97643acb77a79a880382e483537c5198d4483a176d25aac9c3670a3f30956f18ad441904776bcd48131c7d6465bce0c133010f3176c92ec962f5c6b84d4c3ae949619cf48172997ca3b1ccf5c8cc7a67923e1295801048a3d40ac4f2c6467c750fc71314a0c1fc22637dd52e7ea50907ea973d765a6a9bb2b11aa405b9187f72026696710e61af3e41c33da1a05eb65e6523704f078e74e32f10e00247967aee3a8c6546889ef67cd613ad7236583f2104122ac6c6a40a84dc96d81c569e76a952c0a25f396e48337a4fe029a4c91cc7406872706a55573b75f160a4facad7c85fab141c63454bf48990729096ce9965604c7cc1e60ae6868dcc41bc3df71c3e5593f0488b0c6a3063e817f9f4bacb17599c8666ff3591126b4891fb7f5d29660bab60cf5007043a4311d41ab3b29787184f3d3c9ab7cc247f635145b67e970505ba44ad0e06b11ec5cda4175295199d19d660204cbdc17947cc66442d3a2cd408a20fe98174f31ee4e5bb3fa8bcd102bedc26527e9bae836442978a6ccbe510f93ab77569ab1f09d0e6312dd0cc0bcaf095fa8a52a7212d14714a7bf852416c9b026301bd965c30a43d24d97298346a46b2c4bc814ba4059653358b03c9456c60bf0193932eaa2f24ea8e4b010a5a4425ce4540fbab90d8e55c97ac2687f15ff5299278824a08d4743e1a62e1c6619cd3278cd75a97a5b4e3a38668b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb3339ae9c9ce46d9da0e714c0bae8712a670d0e5dcfdd1dd0d045932c79c559b2ab3c7300e2d894b0eaa40a6ab254506d8c1176a33c4a1b2879604b1b80df48d31dd");
1✔
4147
         const std::vector<uint8_t> b_pub_bits = Botan::hex_decode(
1✔
4148
            "f9490f1755c744d9988ed60475e6ae44355ed15c7b549366f29581ec2721fd6704e0ca3f878812805675141c0a15a7b7ac35a9e3f8b2a010bc184981c57852895b2695d56131e32326717f6b101df1bc82b3ba0222d52656d118538c4ca3416be1c76ba37a9901a36e4883be6c541f2bbb561818cd2f136b98f658250545c1fa5bcfdc04374016db1c5132447fd6d568866451c25412f72967de868eaeb9c546fb40ea88cd84a1a586ca51c74bc9c3e56e104323afb658d1ac003151bdc35879a4b6762648bff0caa682f1b3319805d2326d5a46af832aacefbaa1ea820568ea3925870e9b6577eb93898e1b0cfbd1c995cb4cd6cabb979813819749b40a9952f50e97c4365e777dcfe9084219294c205acb350e07db9c98f53444546460962e2bf5aa1cc12273d882fcc215a7397b3f9b307c56b9a0429b30f88453a2376669b28b4bc4b84b51714b6652b7a1a0b53e9ec61b55ca51cdb38243239ee5f18243e515f178768a888f475b3d9060136bb22ee355b3da02c16ad83bdbb4aaa13809cb4bcd5bb53710737d3883632f9254e3336af61621a376720572450e3937c0d930e349adbfa7642ff822b9135e18f943e0178617604d10e0c09ffb3e09783c09d12cbe93311757af9857b77d1488ed39321ca97c3745c5bc176ca81274c8321bcb2029938b32bbd01aec137032f9760849701649120050b50d8353c36b8bb7724a67e7660fc93324065d63912c35a86d8fe60683067bbd2685c552ad8c65c77c57c937676fd61595c453174bf996e9d3a9ccb837b25464115c1ba3343ac097b80735aed3225091167cd8f841ffe49c5e698ef542124253084623179394433a4b61547b9ee09c98c2736ea086bd69d1bc7ae68a6ae5ce682a215860006b4604dee45a3e212f97643acb77a79a880382e483537c5198d4483a176d25aac9c3670a3f30956f18ad441904776bcd48131c7d6465bce0c133010f3176c92ec962f5c6b84d4c3ae949619cf48172997ca3b1ccf5c8cc7a67923e1295801048a3d40ac4f2c6467c750fc71314a0c1fc22637dd52e7ea50907ea973d765a6a9bb2b11aa405b9187f72026696710e61af3e41c33da1a05eb65e6523704f078e74e32f10e00247967aee3a8c6546889ef67cd613ad7236583f2104122ac6c6a40a84dc96d81c569e76a952c0a25f396e48337a4fe029a4c91cc7406872706a55573b75f160a4facad7c85fab141c63454bf48990729096ce9965604c7cc1e60ae6868dcc41bc3df71c3e5593f0488b0c6a3063e817f9f4bacb17599c8666ff3591126b4891fb7f5d29660bab60cf5007043a4311d41ab3b29787184f3d3c9ab7cc247f635145b67e970505ba44ad0e06b11ec5cda4175295199d19d660204cbdc17947cc66442d3a2cd408a20fe98174f31ee4e5bb3fa8bcd102bedc26527e9bae836442978a6ccbe510f93ab77569ab1f09d0e6312dd0cc0bcaf095fa8a52a7212d14714a7bf852416c9b026301bd965c30a43d24d97298346a46b2c4bc814ba4059653358b03c9456c60bf0193932eaa2f24ea8e4b010a5a4425ce4540fbab90d8e55c97ac2687f15ff5299278824a08d4743e1a62e1c6619cd3278cd75a97a5b4e3a38668b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb333");
1✔
4149

4150
         botan_privkey_t b_priv;
1✔
4151
         if(!TEST_FFI_INIT(botan_privkey_load_kyber, (&b_priv, b_priv_bits.data(), 2400))) {
1✔
4152
            return;
×
4153
         }
4154

4155
         ViewBytesSink privkey_read;
1✔
4156
         ViewBytesSink privkey_read_raw;
1✔
4157
         TEST_FFI_OK(botan_privkey_view_kyber_raw_key, (b_priv, privkey_read.delegate(), privkey_read.callback()));
1✔
4158
         TEST_FFI_OK(botan_privkey_view_raw, (b_priv, privkey_read_raw.delegate(), privkey_read_raw.callback()));
1✔
4159
         result.test_eq("kyber768 private key", privkey_read.get(), b_priv_bits);
2✔
4160
         result.test_eq("kyber768 private key raw", privkey_read_raw.get(), b_priv_bits);
2✔
4161

4162
         ViewBytesSink pubkey_read;
1✔
4163
         ViewBytesSink pubkey_read_raw;
1✔
4164

4165
         botan_pubkey_t b_pub;
1✔
4166
         TEST_FFI_OK(botan_privkey_export_pubkey, (&b_pub, b_priv));
1✔
4167
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (b_pub, pubkey_read.delegate(), pubkey_read.callback()));
1✔
4168
         TEST_FFI_OK(botan_pubkey_view_raw, (b_pub, pubkey_read_raw.delegate(), pubkey_read_raw.callback()));
1✔
4169
         result.test_eq("kyber768 public key b", pubkey_read.get(), b_pub_bits);
2✔
4170
         result.test_eq("kyber768 public key raw b", pubkey_read_raw.get(), b_pub_bits);
2✔
4171

4172
         botan_pubkey_t a_pub;
1✔
4173
         TEST_FFI_OK(botan_pubkey_load_kyber, (&a_pub, a_pub_bits.data(), 1184));
1✔
4174
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (a_pub, pubkey_read.delegate(), pubkey_read.callback()));
1✔
4175
         result.test_eq("kyber768 public key a", pubkey_read.get(), a_pub_bits);
2✔
4176

4177
         TEST_FFI_OK(botan_pubkey_destroy, (a_pub));
1✔
4178
         TEST_FFI_OK(botan_pubkey_destroy, (b_pub));
1✔
4179
         TEST_FFI_OK(botan_privkey_destroy, (b_priv));
2✔
4180
      }
7✔
4181
};
4182

4183
class FFI_Kyber1024_Test final : public FFI_Test {
1✔
4184
   public:
4185
      std::string name() const override { return "FFI Kyber1024"; }
1✔
4186

4187
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
4188
         const std::vector<uint8_t> a_pub_bits = Botan::hex_decode(
1✔
4189
            "9779a4d1fc45ec261f048b9c9daa38c9ec228b6505e8905226b38486802059c2c5c89601560634cb1337b1315365144842bc405a292e683cafa4514526945c4dfb68ee2acdb8b79532836696d53125a045bdbb8a3bcc8123083d1e682c5bd7820c76c448351151474f69d601d7708dbb2c979d77527494b68520a8ff66c34162ca2aec8072a2a51ff259389648e75b95c16abe14604edbfabaf400b76a7a0f07db61dce19102f43b2d1060747b02c4425485341fd5d9bffaa7016061374963b985209c6b9a7db3f94958311d027900a3d8c44f8435b093a236a0509f1928df7719ce8c4e90228f4db87cb9e882f2712180238845d31eb906c60eb63e0ff55e84b867a91b79ae74a03ac00473c8c1b3e6aedcc37f1e69b8e136019bdb01c374a122e164c32584b2fbcf5e013a45127c893326b6860378cb5525a48b522b30132b7688214b69808dd19aa4ff033e16252016fc9479222424393e51db7115e38114e6893cdcc8ae1117a4316548010ab4629fb672148d031a6c601a6a4a661d872d8ef693750a115958716ab9263eb0516357102358cde5256464805ca8f59661751ad6a475a7ec78cb7319c3bbc544e1bb1185aaaeb751ba0b3923246e33f40a4ff4780b745362a218d169474d7208a9f7577228308af20f8d2403a27477cf53cff133a247d5c2b298bf21bac6dc44649e63afcef54ad3a07a74e447b36bacca295e053368ef7c146e1c28edb31b3777c941a7c27dc559437b0ff83c2910f827ef244d7726af2b9708654bcfb139a26844c6f4b79cd9e4747065a77596b927f96cbdbc7267ac9e32ac396f6c4ce739aa0aa60fccac178edab1e04688fe71a74201a99bc64b55f8cb89e11545e5275af48626ec667520ba8a7c88c2307e793455ef8780fae16412ea59e92406f595902dc21c612b142801cf31abc0475c7bde772d51c55a2dfc651bc5ab4143022813cc4c8cc6f52c8d27e3cd7851849a8c4343f0c7ed6c6cb9f765c5047e55bc48aad5b932128287f70939db753a61c0d7db1830c256288c1c85cc9bbce58366dac528c893d61b69850bcb82758e836936e1f8163861586482cad35b4db1437fad6980b66280b1652d72f52407a8015f85c29e75b2158933a4f5887db320b583cdcbca274eac21f8702879b8bc4afb62ba056d146512921554c765464d4c96bd3c9a9720187c3339d0593bc4bba59155469ff6b8688a5fc5fa6b46d4070668bd168c5f796492760940999927628236b2412968438d2cbc2a978abc097320291b0ed7e4631ec9bbe28ac63ab6860e976ac1552afc43897d6937a092432dc481a914c3a1273c43009e8ec523cb93ced9898f90905340ba6e01bc572d03a893fa21c4342d88dc3e77b33b1fc063dcdb689d8c4ffc840ce879cfb3471385512cc7c7591383b1fac89832bb1cb3324e6d2868918844cb20ca4df812b42824192a43380430a37a7f3ac41ed064cfe055157b91b5c0108fb7a613a0112af4d64e48f8a345328a99b7bf0c91cb037215b0177ad4c263e6d78f5958c848158e4fd2117c248e0cab3cf98c1e27868838f8428ba0562df6b61e8736e2b8b266607918e0ce2ce3af67c81fa5c2a4d2bc8e871825b702b3beca397b33a518dac8b1393d494a90900c01b55925857cbf8027051d36a4f141a4d2dc440341305e033cb50a074be459c76a339a9a52c447360dea2a5699ccea683a42430aa6fc9545c75c0492641af7a4e9267bcc384e55714cf49741eba3b69d6417a555475d3c021112b8b3588c63747b5ce2ccfee91297fa419f9c4298978fff0870d8a855b48899ca9bb47d836d62d2038cc3816f3a698bb3bbff78c7a015b0ea1960634292e1d50b03e1043a98ca9b27063e668b05e2c17d692d382b181365a818518ec747720337ca1868596af42a90fab1870373d74b8f6d42ac86b18bca3ab04764680cf2060c529abe5b8ef4474dc8cc47a8033ebd884d3e0a0f1a94420d8a3c9162b7af87a2c8a394647434f4a3bc2b477813ac82cf387185b587f7f68938eccddd6934d143ba17bb4a712566d2a55aaddb3323713667401b4a20b86c23bf1076439cb6b8c115389dba4e6f0c915be7602b9703b535070e4a5c5649a5d7080515026706b2604575cbd0687f2729a92a997d21ea7200c41ed8315275f4c7b72f9bc85c0a13b9d041586fd583feb12afd5a402dd33b43543f5fa4eb436c8d");
1✔
4190
         const std::vector<uint8_t> b_priv_bits = Botan::hex_decode(
1✔
4191
            "9bc4986eec853ac90896c7300f914216773baa687f57f81f94e6b5a26515c0a74c73b19dc6b9888755a1d1db6a1128b02c5144ed9ba75e874a68a14dee3a9bf770b767121fbbdb292e097444757fe2f324a875b0a07a4fbdcc8fd6137fce80c69f412be103b01f8b3bed392600f830d1b20d73ca0b386666d16c5d96db10e309041890bfd4ab7bdec27191979abe7637e76153918cc2af1545724abfd95ca496281a0cc6ca63a87819b75aa86452e5027d429cad64a9c029112a3a7b9fb993c6a6d0671f6c9e24986b83f67cf1236d94c051559616826a947eaa15074025d1ab810c80220e8a8c2c222d4e8594bf35811b444a57e9bf94fb2479d174d6b38c8c3b4730437c244b2513232ec798adec4a8e597bca83bca5948468f93c022ba65311a91e3d95c299c70c3f1a43cd0c83dd500bd2211b9b385b82f2c003b10d295765b3e367563a5f19033fc1231db9620f835c95f5a261190a5da1c24ed528316f0a1520d223e208a8c99b24d28598ade74fc88370e7f45102c5a6411891c661b356b9a32e1cc0fafaa085d7670e8bcb5e768eb255204f2445b5b73b04079881903a553c865744343a925c7899777b1c9dd5579a481512f8157061606a9a67c041d38bc179048be17dd9e19dc0a572bce595afa3b68ff21bf78a63a7560636b6bb01ec3362e2aaabc8965818b7f2417ca8f66a5a2a67f72a3931e125d638a872862a7b680a54aa1f25d90dbd567635ec6664919e29517325a5c5048cc8d1c31af5e4866e85025b9184a7b75ed7f2c9c8d88259fa2ec5b05ed3751584f88308c67ff1a72fa2453305160baf404db7d4be56f1547b20bb7dec23f02b10db584b28ca40d8b39c1c3ab9f3d7bbda0822604ca48f26694d69810aa888ae3c0983c5ba4cb74211f7a5361ccdee694f4202aebe13a75b1b2dda1b3232376be213582afc4fde3474766671fe865e2fd98384eb65b2f349f1e24269b91bd9d08c80849735a9951304afd130b5c2211314630aed4b6ac3b1252a0999ff5a3ec26a283342389d219fd243706128b389eb2334fb2a6184a4eab6735d7428df5633ce030b8f853ee47c4401fc5d06b43c9b66b7aeeb23b5f000a30a6f88f027ee3994fe8b63e51b83bc24bb733a3773a35cbe138f6d9c91a3a3898bca6776030d740ac355176547d624719656a9a44e91c63faf7699dc6c2c45575718d48828828b39043c2fda2af416837efc38d17c56d4b63c63a5ab43434647d029f7b236b288958f06910763610f8b2f027a8dcd780039ab34a6871427476ff6500240e83b87c95dcfa45ac5315ef34b343fb609eb296e915c849bb8c57f57c69b177eaa8456377403fe8c6627a3282d45308f675d67085a15f0b1b55aa2a8f21afd6c05c3c00e9eb8c32418cb41963ce427b43e7545c58325c7b9368db2333de424dbeb3430f007d18a68d73b7dc67960b28206a68a1be400a770b5cd9d45a72824ca00345ac56491c1414fe5287a2eb2ad61f3bdcd0c84c335b04a703425d79dbd02b0a0e90de5b331c3c29f6562969e04cdf7095b2a7646b3d006b0b83cb68580b5ccb71de1b4d9f131bac133d6088e10613a00599d81d4818403a4bea83905304cc45ca645a9b2c6484ce4f7bfbe3597f0c2c50784065abc214b5518ed8c7d427205392987d807cdfa571d09932e0a1217e7c5703dfc9be7c4a0f2f33338f316b8c203c6b84285a0957c363d3722145b592f9d6277b1b7446aa2005663b9898901da3b8978eaa8aee87a209c72bfd6cd10a2bedac527f26c419b4bb9a4ca13dba14cff5671461c4cff238d213988120c3cae3b15b607269420c14a9b23a6bb887ad048e6688528137dd5c071172bcaddd5ba4fb9acfb1712ca04aa267b0686a237414b16c4ac03525a45513a393605a29f3b4c07dc5cd082c67b770e69723c6425a23f573216c4cabd6c9c39533f16643bc6151672c466dea63fb8e2180fbcb453c5670b01c0fc7043d198b6c320bf0dfc5016d00f46dc0ba75c7369fc13b65733301b3a92c0cf8dcc1f01e33291a9974d5b5debfa3be5531c69a066a6f4230ba778502654b7e76191d3ba68b1488c848d8ef11bb9ac5a5374af6ccacd02432b247a963685127728825a162c43c1cc79a7b60f571481a3a635a18f0050cbe6f4af1a5a2224044622bb3893bc837be699d5279dac78c9d49818ebc91d4ff6cb54b294e4c378d8eb584f561d21ca87b7c77b1c551f52112ba72c4b6514bb286856e89b48b0a62bad093894474b246a515f849056c9aeb572060ee11b93e236204a8ff1784ede0c3f4c0cd0cdb2a50bd4bcb1225c0dd2caa8a129fdcb01f0e1a75da465b6b8742e4946dbe3ab21ca6c4e592d398a5700375b7b770c17e117663b51313824854585ece4a1520c59a513555f548afdc6748eb9b770b7af8adc9b3c1861efc00ebde93f91fa1539c3411ec9b154c93d1f508b9714cdd0aaabcb436951aa54978b84faa8bd838c29a12657a6389fa658237326bf80e44cff183d9c9107d30c7b4df83908a8badbc1bb5328ae61e6451291710408196261bb913b285c168fe5c7456cd74970377330139beb4c8b4c00c5954254a748308bc30f3a1ccabed83628422e5515b294e80bfed024902a81518c0e6827caa13230016b72aec10d31aab9f9b072652486c3b779c845804d001b223b4065667a5e318b4e0b27c41a099168ce763ac245c1c26354cfc8214cacf01dc8c99c5fc855639cb7486a7a6b71766d62369ee6349f521591344fb8026099fc055c3a0ca2002271c9361f52acaf4675a97b3593f3447b284ad69a9711501411e14e48f080e2a0bceba07a02f79dc15c3c971c1e671a66bf7144395a94d08c46603c4369b25b38d924693492f6500745cb1d2a037e2cd814fac70d8254aea4526e061648d227350dec1ba6305299a45bde4029a9f63e4f62acb5246ba8b54208a37b98fa77e4b720f02b18969635cc16cb6e236ff78111b621cab0339c60b529a9207c94a74925aa3d177636a6d205d72b429f614d452358b98582e42c2f3a32c94202a8625a760483807bbac74c083a7a786ae7b46aff3810a5318e3f479e67e83b4c94587d345ffda17273a55e0432281c670e36b76539263636445e4d110fa1c92b2e65b1ee993b26c41c8c66b181206ff559728993503e7611fa0ca24156642d29a52cbb6aeff88fa1bb790b4344472503ae77967d11a904550a95cc47c3435f1e44b4b890cf5dc4cbd7486a6cab60d1a6622e40c67e854ed0e6a0c4c2aa92a29731e69bc6b3ca9e83893df4bdb37338055bc16bc7cd875b0691d0212109b77236b10b0c72b7a76e9ce346061028be3271c480435e2482d3d2733ed619c2d3698d29a3225a7731a62db4f214a0b11569e91895fbccb8a92d18f94d307a542ff96d2ce8c2bbaa8fab948c83365162268124cabe934b3a5e6530b7f74397a980c243bdce2989747565dd9ba65a7498aa7c3ee711c3be239e93cb45134c082a900cc951756afa42e74a65001822cab95f41798fdef89ea8a751740842e2f7a271e98dbd5158d7455fe9b9cdcd0a0f68c91e59b00979673a1deabf8d213971f994c0bb08899a91e0b896319b9fc69651e3453b14d1cc94462341548fc81c643ab0b10303c7be2c3458b226d0e45f58b21658a853b84615ac5234da9bc5699646fc53c0df907f9a3c598dab6c409b7884a7a84e1a51c2caa80779cd86b77314dabae85caa28972bb6503273a82eb96ab24af6bb0ad0c9ba07ce24216696e265206abc9e147ceef1ad2e343d17aabbc4803496366c7afb53f604c92323c70be5c1e3f601c215c888f3ab239c72aba0634a2901a71891f5d072d41b524e410c7f276ff1109878d7196b5615f1f50c13976257600a821909cb36cf8d2857a2f801cf258913f56a5153234640b9e61bbd4e7b69f83a1eb8ac6b92cb841e808b700aa784a733451048211797563cccc748c9fbb54b3b65445cba676149c7ae9a9a2ecab6599b8505bc561ad85d8c052ed9669e50e684270c86f1cb4fd6708a6746c643392c8a088477dc8bf58ba4a54a8b79e523fc19a28bd47d7e5a334d8296ec86c7f52b7e73b475a0716422953cdd8a8aed0b3a84dc8425145c240c55870240a23977215b364c5596286bc496728717c329dd4b5d0b310980bc0b3f591a2d5cb2c9eaccfa1c7d3a096fc11091a4007a0f23a59782699c3171da53bc7b914f26c95391d6445073a1bd7a44691bafab9c9aceccc7ec389255c3a0ff24c71b30b6bf80c010803383485a7b295991d759cefbae257bbdee1806818565ebd09bf814c98686bbf44a0b14d28735f79ca2261bf9a31b2ca090c7667168b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb33357e74a31d3cd71513fb880e1e177438f29009bab0a131fec4cc24752015efe71c7300e2d894b0eaa40a6ab254506d8c1176a33c4a1b2879604b1b80df48d31dd");
1✔
4192
         const std::vector<uint8_t> b_pub_bits = Botan::hex_decode(
1✔
4193
            "93bc837be699d5279dac78c9d49818ebc91d4ff6cb54b294e4c378d8eb584f561d21ca87b7c77b1c551f52112ba72c4b6514bb286856e89b48b0a62bad093894474b246a515f849056c9aeb572060ee11b93e236204a8ff1784ede0c3f4c0cd0cdb2a50bd4bcb1225c0dd2caa8a129fdcb01f0e1a75da465b6b8742e4946dbe3ab21ca6c4e592d398a5700375b7b770c17e117663b51313824854585ece4a1520c59a513555f548afdc6748eb9b770b7af8adc9b3c1861efc00ebde93f91fa1539c3411ec9b154c93d1f508b9714cdd0aaabcb436951aa54978b84faa8bd838c29a12657a6389fa658237326bf80e44cff183d9c9107d30c7b4df83908a8badbc1bb5328ae61e6451291710408196261bb913b285c168fe5c7456cd74970377330139beb4c8b4c00c5954254a748308bc30f3a1ccabed83628422e5515b294e80bfed024902a81518c0e6827caa13230016b72aec10d31aab9f9b072652486c3b779c845804d001b223b4065667a5e318b4e0b27c41a099168ce763ac245c1c26354cfc8214cacf01dc8c99c5fc855639cb7486a7a6b71766d62369ee6349f521591344fb8026099fc055c3a0ca2002271c9361f52acaf4675a97b3593f3447b284ad69a9711501411e14e48f080e2a0bceba07a02f79dc15c3c971c1e671a66bf7144395a94d08c46603c4369b25b38d924693492f6500745cb1d2a037e2cd814fac70d8254aea4526e061648d227350dec1ba6305299a45bde4029a9f63e4f62acb5246ba8b54208a37b98fa77e4b720f02b18969635cc16cb6e236ff78111b621cab0339c60b529a9207c94a74925aa3d177636a6d205d72b429f614d452358b98582e42c2f3a32c94202a8625a760483807bbac74c083a7a786ae7b46aff3810a5318e3f479e67e83b4c94587d345ffda17273a55e0432281c670e36b76539263636445e4d110fa1c92b2e65b1ee993b26c41c8c66b181206ff559728993503e7611fa0ca24156642d29a52cbb6aeff88fa1bb790b4344472503ae77967d11a904550a95cc47c3435f1e44b4b890cf5dc4cbd7486a6cab60d1a6622e40c67e854ed0e6a0c4c2aa92a29731e69bc6b3ca9e83893df4bdb37338055bc16bc7cd875b0691d0212109b77236b10b0c72b7a76e9ce346061028be3271c480435e2482d3d2733ed619c2d3698d29a3225a7731a62db4f214a0b11569e91895fbccb8a92d18f94d307a542ff96d2ce8c2bbaa8fab948c83365162268124cabe934b3a5e6530b7f74397a980c243bdce2989747565dd9ba65a7498aa7c3ee711c3be239e93cb45134c082a900cc951756afa42e74a65001822cab95f41798fdef89ea8a751740842e2f7a271e98dbd5158d7455fe9b9cdcd0a0f68c91e59b00979673a1deabf8d213971f994c0bb08899a91e0b896319b9fc69651e3453b14d1cc94462341548fc81c643ab0b10303c7be2c3458b226d0e45f58b21658a853b84615ac5234da9bc5699646fc53c0df907f9a3c598dab6c409b7884a7a84e1a51c2caa80779cd86b77314dabae85caa28972bb6503273a82eb96ab24af6bb0ad0c9ba07ce24216696e265206abc9e147ceef1ad2e343d17aabbc4803496366c7afb53f604c92323c70be5c1e3f601c215c888f3ab239c72aba0634a2901a71891f5d072d41b524e410c7f276ff1109878d7196b5615f1f50c13976257600a821909cb36cf8d2857a2f801cf258913f56a5153234640b9e61bbd4e7b69f83a1eb8ac6b92cb841e808b700aa784a733451048211797563cccc748c9fbb54b3b65445cba676149c7ae9a9a2ecab6599b8505bc561ad85d8c052ed9669e50e684270c86f1cb4fd6708a6746c643392c8a088477dc8bf58ba4a54a8b79e523fc19a28bd47d7e5a334d8296ec86c7f52b7e73b475a0716422953cdd8a8aed0b3a84dc8425145c240c55870240a23977215b364c5596286bc496728717c329dd4b5d0b310980bc0b3f591a2d5cb2c9eaccfa1c7d3a096fc11091a4007a0f23a59782699c3171da53bc7b914f26c95391d6445073a1bd7a44691bafab9c9aceccc7ec389255c3a0ff24c71b30b6bf80c010803383485a7b295991d759cefbae257bbdee1806818565ebd09bf814c98686bbf44a0b14d28735f79ca2261bf9a31b2ca090c7667168b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb333");
1✔
4194

4195
         botan_privkey_t b_priv;
1✔
4196
         if(!TEST_FFI_INIT(botan_privkey_load_kyber, (&b_priv, b_priv_bits.data(), 3168))) {
1✔
4197
            return;
×
4198
         }
4199

4200
         ViewBytesSink privkey_read;
1✔
4201
         ViewBytesSink privkey_read_raw;
1✔
4202
         TEST_FFI_OK(botan_privkey_view_kyber_raw_key, (b_priv, privkey_read.delegate(), privkey_read.callback()));
1✔
4203
         TEST_FFI_OK(botan_privkey_view_raw, (b_priv, privkey_read_raw.delegate(), privkey_read_raw.callback()));
1✔
4204
         result.test_eq("kyber1024 private key", privkey_read.get(), b_priv_bits);
2✔
4205
         result.test_eq("kyber1024 private key raw", privkey_read_raw.get(), b_priv_bits);
2✔
4206

4207
         ViewBytesSink pubkey_read;
1✔
4208
         ViewBytesSink pubkey_read_raw;
1✔
4209

4210
         botan_pubkey_t b_pub;
1✔
4211
         TEST_FFI_OK(botan_privkey_export_pubkey, (&b_pub, b_priv));
1✔
4212
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (b_pub, pubkey_read.delegate(), pubkey_read.callback()));
1✔
4213
         TEST_FFI_OK(botan_pubkey_view_raw, (b_pub, pubkey_read_raw.delegate(), pubkey_read_raw.callback()));
1✔
4214
         result.test_eq("kyber1024 public key b", pubkey_read.get(), b_pub_bits);
2✔
4215
         result.test_eq("kyber1024 public key raw b", pubkey_read_raw.get(), b_pub_bits);
2✔
4216

4217
         botan_pubkey_t a_pub;
1✔
4218
         TEST_FFI_OK(botan_pubkey_load_kyber, (&a_pub, a_pub_bits.data(), 1568));
1✔
4219
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (a_pub, pubkey_read.delegate(), pubkey_read.callback()));
1✔
4220
         result.test_eq("kyber1024 public key a", pubkey_read.get(), a_pub_bits);
2✔
4221

4222
         TEST_FFI_OK(botan_pubkey_destroy, (a_pub));
1✔
4223
         TEST_FFI_OK(botan_pubkey_destroy, (b_pub));
1✔
4224
         TEST_FFI_OK(botan_privkey_destroy, (b_priv));
2✔
4225
      }
7✔
4226
};
4227

4228
class FFI_ML_KEM_Test final : public FFI_KEM_Roundtrip_Test {
1✔
4229
   public:
4230
      std::string name() const override { return "FFI ML-KEM"; }
1✔
4231

4232
   private:
4233
      const char* algo() const override { return "ML-KEM"; }
3✔
4234

4235
      privkey_loader_fn_t private_key_load_function() const override { return botan_privkey_load_ml_kem; }
3✔
4236

4237
      pubkey_loader_fn_t public_key_load_function() const override { return botan_pubkey_load_ml_kem; }
3✔
4238

4239
      std::vector<const char*> modes() const override { return {"ML-KEM-512", "ML-KEM-768", "ML-KEM-1024"}; }
1✔
4240
};
4241

4242
class FFI_FrodoKEM_Test final : public FFI_KEM_Roundtrip_Test {
1✔
4243
   public:
4244
      std::string name() const override { return "FFI FrodoKEM"; }
1✔
4245

4246
   protected:
4247
      const char* algo() const override { return "FrodoKEM"; }
12✔
4248

4249
      privkey_loader_fn_t private_key_load_function() const override { return botan_privkey_load_frodokem; }
12✔
4250

4251
      pubkey_loader_fn_t public_key_load_function() const override { return botan_pubkey_load_frodokem; }
12✔
4252

4253
      std::vector<const char*> modes() const override {
1✔
4254
         return std::vector{
1✔
4255
            "FrodoKEM-640-SHAKE",
4256
            "FrodoKEM-976-SHAKE",
4257
            "FrodoKEM-1344-SHAKE",
4258
            "eFrodoKEM-640-SHAKE",
4259
            "eFrodoKEM-976-SHAKE",
4260
            "eFrodoKEM-1344-SHAKE",
4261
            "FrodoKEM-640-AES",
4262
            "FrodoKEM-976-AES",
4263
            "FrodoKEM-1344-AES",
4264
            "eFrodoKEM-640-AES",
4265
            "eFrodoKEM-976-AES",
4266
            "eFrodoKEM-1344-AES",
4267
         };
1✔
4268
      }
4269
};
4270

4271
class FFI_ML_DSA_Test final : public FFI_Signature_Roundtrip_Test {
1✔
4272
   public:
4273
      std::string name() const override { return "FFI ML-DSA"; }
1✔
4274

4275
   private:
4276
      const char* algo() const override { return "ML-DSA"; }
3✔
4277

4278
      privkey_loader_fn_t private_key_load_function() const override { return botan_privkey_load_ml_dsa; }
3✔
4279

4280
      pubkey_loader_fn_t public_key_load_function() const override { return botan_pubkey_load_ml_dsa; }
3✔
4281

4282
      std::vector<const char*> modes() const override {
1✔
4283
         return {
1✔
4284
            "ML-DSA-4x4",
4285
            "ML-DSA-6x5",
4286
            "ML-DSA-8x7",
4287
         };
1✔
4288
      }
4289

4290
      const char* hash_algo_or_padding() const override { return ""; }
12✔
4291
};
4292

4293
class FFI_SLH_DSA_Test final : public FFI_Signature_Roundtrip_Test {
1✔
4294
   public:
4295
      std::string name() const override { return "FFI SLH-DSA"; }
1✔
4296

4297
   private:
4298
      const char* algo() const override { return "SLH-DSA"; }
12✔
4299

4300
      privkey_loader_fn_t private_key_load_function() const override { return botan_privkey_load_slh_dsa; }
12✔
4301

4302
      pubkey_loader_fn_t public_key_load_function() const override { return botan_pubkey_load_slh_dsa; }
12✔
4303

4304
      std::vector<const char*> modes() const override {
1✔
4305
         auto modes = std::vector{
1✔
4306
            "SLH-DSA-SHA2-128f",
4307
            "SLH-DSA-SHAKE-128f",
4308
            "SLH-DSA-SHA2-192f",
4309
            "SLH-DSA-SHAKE-192f",
4310
            "SLH-DSA-SHA2-256f",
4311
            "SLH-DSA-SHAKE-256f",
4312
         };
1✔
4313

4314
         if(Test::run_long_tests()) {
1✔
4315
            modes = Botan::concat(modes,
3✔
4316
                                  std::vector{
1✔
4317
                                     "SLH-DSA-SHA2-128s",
4318
                                     "SLH-DSA-SHA2-192s",
4319
                                     "SLH-DSA-SHA2-256s",
4320
                                     "SLH-DSA-SHAKE-128s",
4321
                                     "SLH-DSA-SHAKE-192s",
4322
                                     "SLH-DSA-SHAKE-256s",
4323
                                  });
2✔
4324
         }
4325

4326
         return modes;
1✔
4327
      }
×
4328

4329
      const char* hash_algo_or_padding() const override { return ""; }
48✔
4330
};
4331

4332
class FFI_Classic_McEliece_Test final : public FFI_KEM_Roundtrip_Test {
1✔
4333
   public:
4334
      std::string name() const override { return "FFI Classic McEliece"; }
1✔
4335

4336
   protected:
4337
      const char* algo() const override { return "ClassicMcEliece"; }
16✔
4338

4339
      privkey_loader_fn_t private_key_load_function() const override { return botan_privkey_load_classic_mceliece; }
16✔
4340

4341
      pubkey_loader_fn_t public_key_load_function() const override { return botan_pubkey_load_classic_mceliece; }
16✔
4342

4343
      std::vector<const char*> modes() const override {
1✔
4344
         auto modes = std::vector{
1✔
4345
            "348864f",
4346
            "460896f",
4347
         };
1✔
4348
         if(Test::run_long_tests()) {
1✔
4349
            modes = Botan::concat(modes,
3✔
4350
                                  std::vector{
1✔
4351
                                     "348864",
4352
                                     "460896",
4353
                                     "6688128",
4354
                                     "6688128f",
4355
                                     "6688128pc",
4356
                                     "6688128pcf",
4357
                                     "6960119",
4358
                                     "6960119f",
4359
                                     "6960119pc",
4360
                                     "6960119pcf",
4361
                                     "8192128",
4362
                                     "8192128f",
4363
                                     "8192128pc",
4364
                                     "8192128pcf",
4365
                                  });
2✔
4366
         }
4367
         return modes;
1✔
4368
      }
×
4369
};
4370

4371
class FFI_ElGamal_Test final : public FFI_Test {
1✔
4372
   public:
4373
      std::string name() const override { return "FFI ElGamal"; }
1✔
4374

4375
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
4376
         botan_privkey_t priv;
1✔
4377

4378
         if(TEST_FFI_INIT(botan_privkey_create, (&priv, "ElGamal", "modp/ietf/1024", rng))) {
1✔
4379
            do_elgamal_test(priv, rng, result);
1✔
4380
         }
4381

4382
         if(TEST_FFI_INIT(botan_privkey_create_elgamal, (&priv, rng, 1024, 160))) {
1✔
4383
            do_elgamal_test(priv, rng, result);
1✔
4384
         }
4385
      }
1✔
4386

4387
   private:
4388
      static void do_elgamal_test(botan_privkey_t priv, botan_rng_t rng, Test::Result& result) {
2✔
4389
         TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
2✔
4390

4391
         botan_pubkey_t pub = nullptr;
2✔
4392
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
2✔
4393
         TEST_FFI_OK(botan_pubkey_check_key, (pub, rng, 0));
2✔
4394

4395
         ffi_test_pubkey_export(result, pub, priv, rng);
2✔
4396
         botan_mp_t p = nullptr;
2✔
4397
         botan_mp_t g = nullptr;
2✔
4398
         botan_mp_t x = nullptr;
2✔
4399
         botan_mp_t y = nullptr;
2✔
4400
         botan_mp_init(&p);
2✔
4401
         botan_mp_init(&g);
2✔
4402
         botan_mp_init(&x);
2✔
4403
         botan_mp_init(&y);
2✔
4404

4405
         TEST_FFI_OK(botan_pubkey_get_field, (p, pub, "p"));
2✔
4406
         TEST_FFI_OK(botan_pubkey_get_field, (g, pub, "g"));
2✔
4407
         TEST_FFI_OK(botan_pubkey_get_field, (y, pub, "y"));
2✔
4408
         TEST_FFI_OK(botan_privkey_get_field, (x, priv, "x"));
2✔
4409

4410
         size_t p_len = 0;
2✔
4411
         TEST_FFI_OK(botan_mp_num_bytes, (p, &p_len));
2✔
4412

4413
         botan_privkey_t loaded_privkey;
2✔
4414
         TEST_FFI_OK(botan_privkey_load_elgamal, (&loaded_privkey, p, g, x));
2✔
4415

4416
         botan_pubkey_t loaded_pubkey;
2✔
4417
         TEST_FFI_OK(botan_pubkey_load_elgamal, (&loaded_pubkey, p, g, y));
2✔
4418

4419
         botan_mp_destroy(p);
2✔
4420
         botan_mp_destroy(g);
2✔
4421
         botan_mp_destroy(y);
2✔
4422
         botan_mp_destroy(x);
2✔
4423

4424
         std::vector<uint8_t> plaintext(16, 0xFF);
2✔
4425
         std::vector<uint8_t> ciphertext;
2✔
4426
         std::vector<uint8_t> decryption;
2✔
4427

4428
   #if defined(BOTAN_HAS_OAEP) && defined(BOTAN_HAS_SHA2_32)
4429
         const std::string padding = "OAEP(SHA-256)";
2✔
4430
   #else
4431
         const std::string padding = "Raw";
4432
   #endif
4433

4434
         // Test encryption
4435
         botan_pk_op_encrypt_t op_enc;
2✔
4436
         if(TEST_FFI_OK(botan_pk_op_encrypt_create, (&op_enc, loaded_pubkey, padding.c_str(), 0))) {
2✔
4437
            size_t ctext_len;
2✔
4438
            TEST_FFI_OK(botan_pk_op_encrypt_output_length, (op_enc, plaintext.size(), &ctext_len));
2✔
4439
            ciphertext.resize(ctext_len);
2✔
4440
            TEST_FFI_OK(botan_pk_op_encrypt,
2✔
4441
                        (op_enc, rng, ciphertext.data(), &ctext_len, plaintext.data(), plaintext.size()));
4442
            ciphertext.resize(ctext_len);
2✔
4443
            TEST_FFI_OK(botan_pk_op_encrypt_destroy, (op_enc));
4✔
4444
         }
4445

4446
         // Test decryption
4447
         botan_pk_op_decrypt_t op_dec;
2✔
4448
         if(TEST_FFI_OK(botan_pk_op_decrypt_create, (&op_dec, loaded_privkey, padding.c_str(), 0))) {
2✔
4449
            size_t ptext_len;
2✔
4450
            TEST_FFI_OK(botan_pk_op_decrypt_output_length, (op_dec, ciphertext.size(), &ptext_len));
2✔
4451
            decryption.resize(ptext_len);
2✔
4452
            TEST_FFI_OK(botan_pk_op_decrypt,
2✔
4453
                        (op_dec, decryption.data(), &ptext_len, ciphertext.data(), ciphertext.size()));
4454
            decryption.resize(ptext_len);
2✔
4455
            TEST_FFI_OK(botan_pk_op_decrypt_destroy, (op_dec));
4✔
4456
         }
4457

4458
         result.test_eq("decryption worked", decryption, plaintext);
4✔
4459

4460
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey));
2✔
4461
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
2✔
4462
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
2✔
4463
         TEST_FFI_OK(botan_privkey_destroy, (priv));
4✔
4464
      }
8✔
4465
};
4466

4467
class FFI_DH_Test final : public FFI_Test {
1✔
4468
   public:
4469
      std::string name() const override { return "FFI DH"; }
1✔
4470

4471
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
4472
         botan_privkey_t priv1;
1✔
4473
         if(!TEST_FFI_INIT(botan_privkey_create_dh, (&priv1, rng, "modp/ietf/2048"))) {
1✔
4474
            return;
×
4475
         }
4476

4477
         botan_privkey_t priv2;
1✔
4478
         REQUIRE_FFI_OK(botan_privkey_create_dh, (&priv2, rng, "modp/ietf/2048"));
1✔
4479

4480
         botan_pubkey_t pub1;
1✔
4481
         REQUIRE_FFI_OK(botan_privkey_export_pubkey, (&pub1, priv1));
1✔
4482

4483
         botan_pubkey_t pub2;
1✔
4484
         REQUIRE_FFI_OK(botan_privkey_export_pubkey, (&pub2, priv2));
1✔
4485

4486
         // Reload key-pair1 in order to test functions for key loading
4487
         botan_mp_t private_x;
1✔
4488
         botan_mp_t public_g;
1✔
4489
         botan_mp_t public_p;
1✔
4490
         botan_mp_t public_y;
1✔
4491

4492
         botan_mp_init(&private_x);
1✔
4493
         botan_mp_init(&public_g);
1✔
4494
         botan_mp_init(&public_p);
1✔
4495
         botan_mp_init(&public_y);
1✔
4496

4497
         TEST_FFI_OK(botan_privkey_get_field, (private_x, priv1, "x"));
1✔
4498
         TEST_FFI_OK(botan_pubkey_get_field, (public_g, pub1, "g"));
1✔
4499
         TEST_FFI_OK(botan_pubkey_get_field, (public_p, pub1, "p"));
1✔
4500
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub1, "y"));
1✔
4501

4502
         botan_privkey_t loaded_privkey1;
1✔
4503
         botan_pubkey_t loaded_pubkey1;
1✔
4504
         TEST_FFI_OK(botan_privkey_load_dh, (&loaded_privkey1, public_p, public_g, private_x));
1✔
4505
         TEST_FFI_OK(botan_pubkey_load_dh, (&loaded_pubkey1, public_p, public_g, public_y));
1✔
4506

4507
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey1, rng, 0));
1✔
4508
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey1, rng, 0));
1✔
4509

4510
         botan_mp_t loaded_public_g;
1✔
4511
         botan_mp_t loaded_public_p;
1✔
4512
         botan_mp_t loaded_public_y;
1✔
4513
         botan_mp_init(&loaded_public_g);
1✔
4514
         botan_mp_init(&loaded_public_p);
1✔
4515
         botan_mp_init(&loaded_public_y);
1✔
4516

4517
         TEST_FFI_OK(botan_pubkey_get_field, (loaded_public_g, loaded_pubkey1, "g"));
1✔
4518
         TEST_FFI_OK(botan_pubkey_get_field, (loaded_public_p, loaded_pubkey1, "p"));
1✔
4519
         TEST_FFI_OK(botan_pubkey_get_field, (loaded_public_y, loaded_pubkey1, "y"));
1✔
4520

4521
         int cmp;
1✔
4522

4523
         TEST_FFI_OK(botan_mp_cmp, (&cmp, loaded_public_g, public_g));
1✔
4524
         result.confirm("bigint_mp_cmp(g, g)", cmp == 0);
2✔
4525

4526
         TEST_FFI_OK(botan_mp_cmp, (&cmp, loaded_public_p, public_p));
1✔
4527
         result.confirm("bigint_mp_cmp(p, p)", cmp == 0);
2✔
4528

4529
         TEST_FFI_OK(botan_mp_cmp, (&cmp, loaded_public_y, public_y));
1✔
4530
         result.confirm("bigint_mp_cmp(y, y)", cmp == 0);
2✔
4531

4532
         botan_pk_op_ka_t ka1;
1✔
4533
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka1, loaded_privkey1, "Raw", 0));
1✔
4534
         botan_pk_op_ka_t ka2;
1✔
4535
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka2, priv2, "Raw", 0));
1✔
4536

4537
         size_t pubkey1_len = 0;
1✔
4538
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
4539
                     botan_pk_op_key_agreement_export_public,
4540
                     (priv1, nullptr, &pubkey1_len));
4541
         std::vector<uint8_t> pubkey1(pubkey1_len);
1✔
4542
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_export_public, (priv1, pubkey1.data(), &pubkey1_len));
1✔
4543
         size_t pubkey2_len = 0;
1✔
4544
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
4545
                     botan_pk_op_key_agreement_export_public,
4546
                     (priv2, nullptr, &pubkey2_len));
4547
         std::vector<uint8_t> pubkey2(pubkey2_len);
1✔
4548
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_export_public, (priv2, pubkey2.data(), &pubkey2_len));
1✔
4549

4550
         const size_t shared_key_len = 256;
1✔
4551

4552
         std::vector<uint8_t> key1(shared_key_len);
1✔
4553
         size_t key1_len = key1.size();
1✔
4554

4555
         TEST_FFI_OK(botan_pk_op_key_agreement,
1✔
4556
                     (ka1, key1.data(), &key1_len, pubkey2.data(), pubkey2.size(), nullptr, 0));
4557

4558
         std::vector<uint8_t> key2(shared_key_len);
1✔
4559
         size_t key2_len = key2.size();
1✔
4560

4561
         TEST_FFI_OK(botan_pk_op_key_agreement,
1✔
4562
                     (ka2, key2.data(), &key2_len, pubkey1.data(), pubkey1.size(), nullptr, 0));
4563

4564
         result.test_eq("shared DH key", key1, key2);
2✔
4565

4566
         TEST_FFI_OK(botan_mp_destroy, (private_x));
1✔
4567
         TEST_FFI_OK(botan_mp_destroy, (public_p));
1✔
4568
         TEST_FFI_OK(botan_mp_destroy, (public_g));
1✔
4569
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1✔
4570

4571
         TEST_FFI_OK(botan_mp_destroy, (loaded_public_p));
1✔
4572
         TEST_FFI_OK(botan_mp_destroy, (loaded_public_g));
1✔
4573
         TEST_FFI_OK(botan_mp_destroy, (loaded_public_y));
1✔
4574

4575
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka1));
1✔
4576
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka2));
1✔
4577
         TEST_FFI_OK(botan_privkey_destroy, (priv1));
1✔
4578
         TEST_FFI_OK(botan_privkey_destroy, (priv2));
1✔
4579
         TEST_FFI_OK(botan_pubkey_destroy, (pub1));
1✔
4580
         TEST_FFI_OK(botan_pubkey_destroy, (pub2));
1✔
4581
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey1));
1✔
4582
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey1));
2✔
4583
      }
4✔
4584
};
4585

4586
class FFI_OID_Test final : public FFI_Test {
1✔
4587
   public:
4588
      std::string name() const override { return "FFI OID"; }
1✔
4589

4590
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
4591
         botan_asn1_oid_t oid;
1✔
4592
         botan_asn1_oid_t new_oid;
1✔
4593
         botan_asn1_oid_t new_oid_from_string;
1✔
4594
         botan_asn1_oid_t oid_a;
1✔
4595
         botan_asn1_oid_t oid_b;
1✔
4596
         botan_asn1_oid_t oid_c;
1✔
4597

4598
         TEST_FFI_FAIL("empty oid", botan_oid_from_string, (&oid, ""));
2✔
4599
         TEST_FFI_OK(botan_oid_from_string, (&oid, "1.2.3.4.5"));
1✔
4600

4601
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_oid_from_string, (&new_oid, "a.a.a"));
1✔
4602
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_oid_from_string, (&new_oid, "0.40"));
1✔
4603
         TEST_FFI_RC(
1✔
4604
            BOTAN_FFI_ERROR_BAD_PARAMETER, botan_oid_from_string, (&new_oid, "random-name-that-definitely-has-no-oid"));
4605

4606
         TEST_FFI_OK(botan_oid_from_string, (&new_oid, "1.2.3.4.5.6.7.8"));
1✔
4607
         TEST_FFI_OK(botan_oid_register, (new_oid, "random-name-that-definitely-has-no-oid"));
1✔
4608

4609
         TEST_FFI_OK(botan_oid_from_string, (&new_oid_from_string, "random-name-that-definitely-has-no-oid"));
1✔
4610
         TEST_FFI_RC(1, botan_oid_equal, (new_oid, new_oid_from_string));
1✔
4611

4612
         TEST_FFI_OK(botan_oid_from_string, (&oid_a, "1.2.3.4.5.6"));
1✔
4613
         TEST_FFI_OK(botan_oid_from_string, (&oid_b, "1.2.3.4.5.6"));
1✔
4614
         TEST_FFI_OK(botan_oid_from_string, (&oid_c, "1.2.3.4.4"));
1✔
4615

4616
         TEST_FFI_RC(1, botan_oid_equal, (oid_a, oid_b));
1✔
4617
         TEST_FFI_RC(0, botan_oid_equal, (oid_a, oid_c));
1✔
4618

4619
         int res;
1✔
4620

4621
         TEST_FFI_OK(botan_oid_cmp, (&res, oid_a, oid_b));
1✔
4622
         result.confirm("oid_a and oid_b are equal", res == 0);
2✔
4623

4624
         TEST_FFI_OK(botan_oid_cmp, (&res, oid_a, oid_c));
1✔
4625
         result.confirm("oid_a is bigger", res == 1);
2✔
4626

4627
         TEST_FFI_OK(botan_oid_cmp, (&res, oid_c, oid_a));
1✔
4628
         result.confirm("oid_c is smaller", res == -1);
2✔
4629

4630
         TEST_FFI_OK(botan_oid_destroy, (oid));
1✔
4631
         TEST_FFI_OK(botan_oid_destroy, (new_oid));
1✔
4632
         TEST_FFI_OK(botan_oid_destroy, (new_oid_from_string));
1✔
4633
         TEST_FFI_OK(botan_oid_destroy, (oid_a));
1✔
4634
         TEST_FFI_OK(botan_oid_destroy, (oid_b));
1✔
4635
         TEST_FFI_OK(botan_oid_destroy, (oid_c));
1✔
4636

4637
         botan_privkey_t priv;
1✔
4638
         if(TEST_FFI_INIT(botan_privkey_create_rsa, (&priv, rng, 1024))) {
1✔
4639
            TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
1✔
4640

4641
            const std::string oid_rsa_expected = "1.2.840.113549.1.1.1";
1✔
4642

4643
            botan_asn1_oid_t rsa_oid_priv;
1✔
4644
            botan_asn1_oid_t rsa_oid_pub;
1✔
4645
            botan_asn1_oid_t rsa_oid_expected;
1✔
4646
            botan_asn1_oid_t rsa_oid_from_name;
1✔
4647

4648
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_oid_from_string, (&rsa_oid_expected, nullptr));
1✔
4649
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_oid_from_string, (nullptr, "1.2.3.4.5"));
1✔
4650
            TEST_FFI_OK(botan_oid_from_string, (&rsa_oid_expected, oid_rsa_expected.c_str()));
1✔
4651
            TEST_FFI_OK(botan_privkey_oid, (&rsa_oid_priv, priv));
1✔
4652

4653
            TEST_FFI_RC(1, botan_oid_equal, (rsa_oid_priv, rsa_oid_expected));
1✔
4654

4655
            botan_pubkey_t pub;
1✔
4656
            TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
4657

4658
            TEST_FFI_OK(botan_pubkey_oid, (&rsa_oid_pub, pub));
1✔
4659
            TEST_FFI_RC(1, botan_oid_equal, (rsa_oid_pub, rsa_oid_expected));
1✔
4660

4661
            ViewStringSink oid_string;
1✔
4662
            TEST_FFI_OK(botan_oid_view_string, (rsa_oid_expected, oid_string.delegate(), oid_string.callback()));
1✔
4663
            const std::string oid_actual = {oid_string.get().begin(), oid_string.get().end()};
2✔
4664

4665
            result.test_eq("oid to string", oid_actual, oid_rsa_expected);
1✔
4666

4667
            TEST_FFI_OK(botan_oid_from_string, (&rsa_oid_from_name, "RSA"));
1✔
4668
            TEST_FFI_RC(1, botan_oid_equal, (rsa_oid_expected, rsa_oid_from_name));
1✔
4669

4670
            ViewStringSink rsa_name;
1✔
4671
            TEST_FFI_OK(botan_oid_view_name, (rsa_oid_from_name, rsa_name.delegate(), rsa_name.callback()));
1✔
4672
            const std::string rsa_name_string = {rsa_name.get().begin(), rsa_name.get().end()};
2✔
4673
            result.test_eq("oid to name", rsa_name_string, "RSA");
2✔
4674

4675
            TEST_FFI_OK(botan_oid_destroy, (rsa_oid_priv));
1✔
4676
            TEST_FFI_OK(botan_oid_destroy, (rsa_oid_pub));
1✔
4677
            TEST_FFI_OK(botan_oid_destroy, (rsa_oid_expected));
1✔
4678
            TEST_FFI_OK(botan_oid_destroy, (rsa_oid_from_name));
1✔
4679

4680
            TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
4681
            TEST_FFI_OK(botan_privkey_destroy, (priv));
2✔
4682
         }
1✔
4683
      }
1✔
4684
};
4685

4686
class FFI_EC_Group_Test final : public FFI_Test {
1✔
4687
   public:
4688
      std::string name() const override { return "FFI EC Group"; }
1✔
4689

4690
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
4691
         int appl_spec_groups;
1✔
4692
         int named_group;
1✔
4693
         TEST_FFI_OK(botan_ec_group_supports_application_specific_group, (&appl_spec_groups));
1✔
4694
         TEST_FFI_OK(botan_ec_group_supports_named_group, ("secp256r1", &named_group));
1✔
4695
         result.confirm("application specific groups support matches build",
2✔
4696
                        appl_spec_groups == 1,
4697
                        Botan::EC_Group::supports_application_specific_group());
1✔
4698
         result.confirm(
2✔
4699
            "named group support matches build", named_group == 1, Botan::EC_Group::supports_named_group("secp256r1"));
1✔
4700

4701
         if(named_group == 1) {
1✔
4702
            botan_ec_group_t group_from_name;
1✔
4703
            botan_asn1_oid_t oid_from_name;
1✔
4704
            botan_mp_t p_from_name;
1✔
4705
            botan_mp_t a_from_name;
1✔
4706
            botan_mp_t b_from_name;
1✔
4707
            botan_mp_t g_x_from_name;
1✔
4708
            botan_mp_t g_y_from_name;
1✔
4709
            botan_mp_t order_from_name;
1✔
4710

4711
            TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_ec_group_from_name, (&group_from_name, ""));
1✔
4712

4713
            TEST_FFI_OK(botan_ec_group_from_name, (&group_from_name, "secp256r1"));
1✔
4714

4715
            get_group_parameters(group_from_name,
1✔
4716
                                 &oid_from_name,
4717
                                 &p_from_name,
4718
                                 &a_from_name,
4719
                                 &b_from_name,
4720
                                 &g_x_from_name,
4721
                                 &g_y_from_name,
4722
                                 &order_from_name,
4723
                                 result);
4724

4725
            botan_asn1_oid_t group_oid;
1✔
4726
            botan_ec_group_t group_from_oid;
1✔
4727
            botan_asn1_oid_t oid_from_oid;
1✔
4728
            botan_mp_t p_from_oid;
1✔
4729
            botan_mp_t a_from_oid;
1✔
4730
            botan_mp_t b_from_oid;
1✔
4731
            botan_mp_t g_x_from_oid;
1✔
4732
            botan_mp_t g_y_from_oid;
1✔
4733
            botan_mp_t order_from_oid;
1✔
4734

4735
            TEST_FFI_OK(botan_oid_from_string, (&group_oid, "1.2.840.10045.3.1.7"));
1✔
4736

4737
            TEST_FFI_OK(botan_ec_group_from_oid, (&group_from_oid, group_oid));
1✔
4738

4739
            get_group_parameters(group_from_oid,
1✔
4740
                                 &oid_from_oid,
4741
                                 &p_from_oid,
4742
                                 &a_from_oid,
4743
                                 &b_from_oid,
4744
                                 &g_x_from_oid,
4745
                                 &g_y_from_oid,
4746
                                 &order_from_oid,
4747
                                 result);
4748

4749
            TEST_FFI_RC(1, botan_oid_equal, (group_oid, oid_from_oid));
1✔
4750
            TEST_FFI_RC(1, botan_oid_equal, (oid_from_name, oid_from_oid));
1✔
4751

4752
            if(appl_spec_groups == 1) {
1✔
4753
               botan_asn1_oid_t group_parameter_oid;
1✔
4754
               botan_mp_t p_parameter;
1✔
4755
               botan_mp_t a_parameter;
1✔
4756
               botan_mp_t b_parameter;
1✔
4757
               botan_mp_t g_x_parameter;
1✔
4758
               botan_mp_t g_y_parameter;
1✔
4759
               botan_mp_t order_parameter;
1✔
4760

4761
               botan_ec_group_t group_from_parameters;
1✔
4762
               botan_asn1_oid_t oid_from_parameters;
1✔
4763
               botan_mp_t p_from_parameters;
1✔
4764
               botan_mp_t a_from_parameters;
1✔
4765
               botan_mp_t b_from_parameters;
1✔
4766
               botan_mp_t g_x_from_parameters;
1✔
4767
               botan_mp_t g_y_from_parameters;
1✔
4768
               botan_mp_t order_from_parameters;
1✔
4769

4770
               TEST_FFI_OK(botan_oid_from_string, (&group_parameter_oid, "1.3.6.1.4.1.25258.100.0"));
1✔
4771
               botan_oid_register(group_parameter_oid, "secp256r1-but-manually-registered");
1✔
4772
               botan_mp_init(&p_parameter);
1✔
4773
               botan_mp_init(&a_parameter);
1✔
4774
               botan_mp_init(&b_parameter);
1✔
4775
               botan_mp_init(&g_x_parameter);
1✔
4776
               botan_mp_init(&g_y_parameter);
1✔
4777
               botan_mp_init(&order_parameter);
1✔
4778

4779
               botan_mp_set_from_str(p_parameter, "0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF");
1✔
4780
               botan_mp_set_from_str(a_parameter, "0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC");
1✔
4781
               botan_mp_set_from_str(b_parameter, "0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B");
1✔
4782
               botan_mp_set_from_str(g_x_parameter,
1✔
4783
                                     "0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296");
4784
               botan_mp_set_from_str(g_y_parameter,
1✔
4785
                                     "0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");
4786
               botan_mp_set_from_str(order_parameter,
1✔
4787
                                     "0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
4788

4789
               TEST_FFI_OK(botan_ec_group_from_params,
1✔
4790
                           (&group_from_parameters,
4791
                            group_parameter_oid,
4792
                            p_parameter,
4793
                            a_parameter,
4794
                            b_parameter,
4795
                            g_x_parameter,
4796
                            g_y_parameter,
4797
                            order_parameter));
4798

4799
               get_group_parameters(group_from_parameters,
1✔
4800
                                    &oid_from_parameters,
4801
                                    &p_from_parameters,
4802
                                    &a_from_parameters,
4803
                                    &b_from_parameters,
4804
                                    &g_x_from_parameters,
4805
                                    &g_y_from_parameters,
4806
                                    &order_from_parameters,
4807
                                    result);
4808

4809
               botan_ec_group_t group_from_registered_oid;
1✔
4810

4811
               TEST_FFI_OK(botan_ec_group_from_name, (&group_from_registered_oid, "secp256r1-but-manually-registered"));
1✔
4812

4813
               // we registered this group under a different oid
4814
               TEST_FFI_RC(0, botan_oid_equal, (oid_from_oid, oid_from_parameters));
1✔
4815

4816
               TEST_FFI_RC(1, botan_ec_group_equal, (group_from_name, group_from_parameters));
1✔
4817
               TEST_FFI_RC(1, botan_ec_group_equal, (group_from_parameters, group_from_registered_oid));
1✔
4818

4819
               const std::vector<std::tuple<botan_mp_t, botan_mp_t>> parameters_inner = {
1✔
4820
                  {p_from_name, p_from_parameters},
4821
                  {a_from_name, a_from_parameters},
4822
                  {b_from_name, b_from_parameters},
4823
                  {g_x_from_name, g_x_from_parameters},
4824
                  {g_y_from_name, g_y_from_parameters},
4825
                  {order_from_name, order_from_parameters}};
1✔
4826

4827
               for(auto [x, y] : parameters_inner) {
7✔
4828
                  TEST_FFI_RC(1, botan_mp_equal, (x, y));
6✔
4829
                  botan_mp_destroy(y);
6✔
4830
               }
4831

4832
               botan_mp_destroy(p_parameter);
1✔
4833
               botan_mp_destroy(a_parameter);
1✔
4834
               botan_mp_destroy(b_parameter);
1✔
4835
               botan_mp_destroy(g_x_parameter);
1✔
4836
               botan_mp_destroy(g_y_parameter);
1✔
4837
               botan_mp_destroy(order_parameter);
1✔
4838

4839
               botan_oid_destroy(group_parameter_oid);
1✔
4840
               botan_oid_destroy(oid_from_parameters);
1✔
4841

4842
               TEST_FFI_OK(botan_ec_group_destroy, (group_from_parameters));
1✔
4843
               TEST_FFI_OK(botan_ec_group_destroy, (group_from_registered_oid));
2✔
4844
            }
1✔
4845

4846
            botan_oid_destroy(oid_from_name);
1✔
4847
            botan_oid_destroy(group_oid);
1✔
4848
            botan_oid_destroy(oid_from_oid);
1✔
4849

4850
            const std::vector<std::tuple<botan_mp_t, botan_mp_t>> parameters = {{p_from_name, p_from_oid},
1✔
4851
                                                                                {a_from_name, a_from_oid},
4852
                                                                                {b_from_name, b_from_oid},
4853
                                                                                {g_x_from_name, g_x_from_oid},
4854
                                                                                {g_y_from_name, g_y_from_oid},
4855
                                                                                {order_from_name, order_from_oid}};
1✔
4856

4857
            for(auto [x, y] : parameters) {
7✔
4858
               TEST_FFI_RC(1, botan_mp_equal, (x, y));
6✔
4859
               botan_mp_destroy(x);
6✔
4860
               botan_mp_destroy(y);
6✔
4861
            }
4862

4863
            botan_ec_group_t secp384r1;
1✔
4864
            botan_ec_group_t secp384r1_with_seed;
1✔
4865

4866
            TEST_FFI_OK(botan_ec_group_from_name, (&secp384r1, "secp384r1"));
1✔
4867
            TEST_FFI_OK(botan_ec_group_from_pem,
2✔
4868
                        (&secp384r1_with_seed, Test::read_data_file("x509/ecc/secp384r1_seed.pem").c_str()));
4869

4870
            botan_mp_t p;
1✔
4871
            botan_mp_t p_with_seed;
1✔
4872
            TEST_FFI_OK(botan_ec_group_get_p, (&p, secp384r1));
1✔
4873
            TEST_FFI_OK(botan_ec_group_get_p, (&p_with_seed, secp384r1_with_seed));
1✔
4874
            TEST_FFI_RC(1, botan_mp_equal, (p, p_with_seed));
1✔
4875
            botan_mp_destroy(p);
1✔
4876
            botan_mp_destroy(p_with_seed);
1✔
4877

4878
            TEST_FFI_RC(0, botan_ec_group_equal, (group_from_name, secp384r1));
1✔
4879
            TEST_FFI_RC(1, botan_ec_group_equal, (group_from_name, group_from_oid));
1✔
4880

4881
            ViewBytesSink der_bytes;
1✔
4882
            TEST_FFI_OK(botan_ec_group_view_der, (group_from_name, der_bytes.delegate(), der_bytes.callback()));
1✔
4883
            botan_ec_group_t group_from_ber;
1✔
4884
            TEST_FFI_OK(
1✔
4885
               botan_ec_group_from_ber,
4886
               (&group_from_ber, reinterpret_cast<const uint8_t*>(der_bytes.get().data()), der_bytes.get().size()));
4887

4888
            ViewStringSink pem_string;
1✔
4889
            TEST_FFI_OK(botan_ec_group_view_pem, (group_from_name, pem_string.delegate(), pem_string.callback()));
1✔
4890
            const std::string pem_actual = {pem_string.get().begin(), pem_string.get().end()};
2✔
4891

4892
            botan_ec_group_t group_from_pem;
1✔
4893
            TEST_FFI_OK(botan_ec_group_from_pem, (&group_from_pem, pem_actual.c_str()));
1✔
4894

4895
            TEST_FFI_RC(1, botan_ec_group_equal, (group_from_name, group_from_ber));
1✔
4896
            TEST_FFI_RC(1, botan_ec_group_equal, (group_from_name, group_from_pem));
1✔
4897

4898
            botan_privkey_t priv;
1✔
4899
            TEST_FFI_OK(botan_ec_privkey_create, (&priv, "ECDSA", secp384r1, rng));
1✔
4900
            std::array<char, 32> namebuf{};
1✔
4901
            size_t name_len = namebuf.size();
1✔
4902

4903
            TEST_FFI_OK(botan_privkey_algo_name, (priv, namebuf.data(), &name_len));
1✔
4904
            result.test_eq("Key name is expected value", namebuf.data(), "ECDSA");
1✔
4905

4906
            botan_privkey_destroy(priv);
1✔
4907

4908
            TEST_FFI_OK(botan_ec_group_destroy, (group_from_name));
1✔
4909
            TEST_FFI_OK(botan_ec_group_destroy, (group_from_oid));
1✔
4910
            TEST_FFI_OK(botan_ec_group_destroy, (secp384r1));
1✔
4911
            TEST_FFI_OK(botan_ec_group_destroy, (secp384r1_with_seed));
1✔
4912
            TEST_FFI_OK(botan_ec_group_destroy, (group_from_ber));
1✔
4913
            TEST_FFI_OK(botan_ec_group_destroy, (group_from_pem));
2✔
4914
         }
3✔
4915
      }
1✔
4916

4917
   private:
4918
      static void get_group_parameters(botan_ec_group_t ec_group,
3✔
4919
                                       botan_asn1_oid_t* oid,
4920
                                       botan_mp_t* p,
4921
                                       botan_mp_t* a,
4922
                                       botan_mp_t* b,
4923
                                       botan_mp_t* g_x,
4924
                                       botan_mp_t* g_y,
4925
                                       botan_mp_t* order,
4926
                                       Test::Result& result) {
4927
         TEST_FFI_OK(botan_ec_group_get_curve_oid, (oid, ec_group));
3✔
4928
         TEST_FFI_OK(botan_ec_group_get_p, (p, ec_group));
3✔
4929
         TEST_FFI_OK(botan_ec_group_get_a, (a, ec_group));
3✔
4930
         TEST_FFI_OK(botan_ec_group_get_b, (b, ec_group));
3✔
4931
         TEST_FFI_OK(botan_ec_group_get_g_x, (g_x, ec_group));
3✔
4932
         TEST_FFI_OK(botan_ec_group_get_g_y, (g_y, ec_group));
3✔
4933
         TEST_FFI_OK(botan_ec_group_get_order, (order, ec_group));
3✔
4934
      }
3✔
4935
};
4936

4937
class FFI_SRP6_Test final : public FFI_Test {
1✔
4938
   public:
4939
      std::string name() const override { return "FFI SRP6"; }
1✔
4940

4941
      bool skip_this_test() const override {
1✔
4942
   #if !defined(BOTAN_HAS_SRP6)
4943
         return true;
4944
   #else
4945
         return false;
1✔
4946
   #endif
4947
      }
4948

4949
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
4950
         constexpr size_t group_bytes = 128;
1✔
4951
         const char* username = "alice";
1✔
4952
         const char* password = "secret";
1✔
4953
         const char* srp_group = "modp/srp/1024";
1✔
4954
         const char* srp_hash = "SHA-256";
1✔
4955
         const auto salt = Botan::hex_decode("beb25379d1a8581eb5a727673a2441ee");
1✔
4956

4957
         std::array<uint8_t, group_bytes> output{};
1✔
4958

4959
         size_t group_size = 0;
1✔
4960
         TEST_FFI_OK(botan_srp6_group_size, (srp_group, &group_size));
1✔
4961
         result.test_eq("reported group size", group_size, group_bytes);
1✔
4962

4963
         size_t short_output_len = output.size() / 2;
1✔
4964
         TEST_FFI_RC(
1✔
4965
            BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
4966
            botan_srp6_generate_verifier,
4967
            (username, password, salt.data(), salt.size(), srp_group, srp_hash, output.data(), &short_output_len));
4968
         result.test_eq("requested verifier length", short_output_len, group_bytes);
1✔
4969

4970
         size_t verifier_output_len = short_output_len;
1✔
4971
         TEST_FFI_OK(
1✔
4972
            botan_srp6_generate_verifier,
4973
            (username, password, salt.data(), salt.size(), srp_group, srp_hash, output.data(), &verifier_output_len));
4974
         const auto verifier = std::vector(output.data(), output.data() + verifier_output_len);
1✔
4975

4976
         botan_srp6_server_session_t srp_server;
1✔
4977
         TEST_FFI_OK(botan_srp6_server_session_init, (&srp_server));
1✔
4978

4979
         short_output_len = output.size() / 2;
1✔
4980
         TEST_FFI_RC(
1✔
4981
            BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
4982
            botan_srp6_server_session_step1,
4983
            (srp_server, verifier.data(), verifier.size(), srp_group, srp_hash, rng, output.data(), &short_output_len));
4984
         result.test_eq("requested B_pub length", short_output_len, group_bytes);
1✔
4985

4986
         size_t pub_B_output_len = short_output_len;
1✔
4987
         TEST_FFI_OK(
1✔
4988
            botan_srp6_server_session_step1,
4989
            (srp_server, verifier.data(), verifier.size(), srp_group, srp_hash, rng, output.data(), &pub_B_output_len));
4990
         const auto pub_B = std::vector(output.data(), output.data() + pub_B_output_len);
1✔
4991

4992
         std::array<uint8_t, group_bytes> output2{};
1✔
4993
         size_t short_output_len2 = output2.size() / 2;
1✔
4994
         short_output_len = output.size() / 2;
1✔
4995
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
4996
                     botan_srp6_client_agree,
4997
                     (username,
4998
                      password,
4999
                      srp_group,
5000
                      srp_hash,
5001
                      salt.data(),
5002
                      salt.size(),
5003
                      pub_B.data(),
5004
                      pub_B.size(),
5005
                      rng,
5006
                      output.data(),
5007
                      &short_output_len,
5008
                      output2.data(),
5009
                      &short_output_len2));
5010
         result.test_eq("requested pub_A length", short_output_len, group_bytes);
1✔
5011
         result.test_eq("requested K1 length", short_output_len2, group_bytes);
1✔
5012

5013
         size_t pub_A_output_len = short_output_len;
1✔
5014
         size_t K1_output_len = short_output_len2;
1✔
5015
         TEST_FFI_OK(botan_srp6_client_agree,
1✔
5016
                     (username,
5017
                      password,
5018
                      srp_group,
5019
                      srp_hash,
5020
                      salt.data(),
5021
                      salt.size(),
5022
                      pub_B.data(),
5023
                      pub_B.size(),
5024
                      rng,
5025
                      output.data(),
5026
                      &pub_A_output_len,
5027
                      output2.data(),
5028
                      &K1_output_len));
5029
         const auto pub_A = std::vector(output.data(), output.data() + pub_A_output_len);
1✔
5030
         const auto K1 = std::vector(output2.data(), output2.data() + K1_output_len);
1✔
5031

5032
         short_output_len = output.size() / 2;
1✔
5033
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
5034
                     botan_srp6_server_session_step2,
5035
                     (srp_server, pub_A.data(), pub_A.size(), output.data(), &short_output_len));
5036
         result.test_eq("requested K2 length", short_output_len, group_bytes);
1✔
5037

5038
         size_t K2_output_len = short_output_len;
1✔
5039
         TEST_FFI_OK(botan_srp6_server_session_step2,
1✔
5040
                     (srp_server, pub_A.data(), pub_A.size(), output.data(), &K2_output_len));
5041
         const auto K2 = std::vector(output.data(), output.data() + K2_output_len);
1✔
5042

5043
         result.test_eq("K1 == K2", K1, K2);
2✔
5044

5045
         TEST_FFI_OK(botan_srp6_server_session_destroy, (srp_server));
2✔
5046
      }
6✔
5047
};
5048

5049
// NOLINTEND(*-init-variables)
5050

5051
BOTAN_REGISTER_TEST("ffi", "ffi_utils", FFI_Utils_Test);
5052
BOTAN_REGISTER_TEST("ffi", "ffi_rng", FFI_RNG_Test);
5053
BOTAN_REGISTER_TEST("ffi", "ffi_rsa_cert", FFI_RSA_Cert_Test);
5054
BOTAN_REGISTER_TEST("ffi", "ffi_zfec", FFI_ZFEC_Test);
5055
BOTAN_REGISTER_TEST("ffi", "ffi_crl", FFI_CRL_Test);
5056
BOTAN_REGISTER_TEST("ffi", "ffi_cert_validation", FFI_Cert_Validation_Test);
5057
BOTAN_REGISTER_TEST("ffi", "ffi_ecdsa_certificate", FFI_ECDSA_Certificate_Test);
5058
BOTAN_REGISTER_TEST("ffi", "ffi_cert_alt_names", FFI_Cert_AlternativeNames_Test);
5059
BOTAN_REGISTER_TEST("ffi", "ffi_cert_name_constraints", FFI_Cert_NameConstraints_Test);
5060
BOTAN_REGISTER_TEST("ffi", "ffi_pkcs_hashid", FFI_PKCS_Hashid_Test);
5061
BOTAN_REGISTER_TEST("ffi", "ffi_cbc_cipher", FFI_CBC_Cipher_Test);
5062
BOTAN_REGISTER_TEST("ffi", "ffi_gcm", FFI_GCM_Test);
5063
BOTAN_REGISTER_TEST("ffi", "ffi_chacha", FFI_ChaCha20Poly1305_Test);
5064
BOTAN_REGISTER_TEST("ffi", "ffi_eax", FFI_EAX_Test);
5065
BOTAN_REGISTER_TEST("ffi", "ffi_aead", FFI_AEAD_Test);
5066
BOTAN_REGISTER_TEST("ffi", "ffi_streamcipher", FFI_StreamCipher_Test);
5067
BOTAN_REGISTER_TEST("ffi", "ffi_hashfunction", FFI_HashFunction_Test);
5068
BOTAN_REGISTER_TEST("ffi", "ffi_mac", FFI_MAC_Test);
5069
BOTAN_REGISTER_TEST("ffi", "ffi_scrypt", FFI_Scrypt_Test);
5070
BOTAN_REGISTER_TEST("ffi", "ffi_kdf", FFI_KDF_Test);
5071
BOTAN_REGISTER_TEST("ffi", "ffi_blockcipher", FFI_Blockcipher_Test);
5072
BOTAN_REGISTER_TEST("ffi", "ffi_errorhandling", FFI_ErrorHandling_Test);
5073
BOTAN_REGISTER_TEST("ffi", "ffi_base64", FFI_Base64_Test);
5074
BOTAN_REGISTER_TEST("ffi", "ffi_hex", FFI_Hex_Test);
5075
BOTAN_REGISTER_TEST("ffi", "ffi_mp", FFI_MP_Test);
5076
BOTAN_REGISTER_TEST("ffi", "ffi_fpe", FFI_FPE_Test);
5077
BOTAN_REGISTER_TEST("ffi", "ffi_totp", FFI_TOTP_Test);
5078
BOTAN_REGISTER_TEST("ffi", "ffi_hotp", FFI_HOTP_Test);
5079
BOTAN_REGISTER_TEST("ffi", "ffi_keywrap", FFI_Keywrap_Test);
5080
BOTAN_REGISTER_TEST("ffi", "ffi_xmss", FFI_XMSS_Test);
5081
BOTAN_REGISTER_TEST("ffi", "ffi_rsa", FFI_RSA_Test);
5082
BOTAN_REGISTER_TEST("ffi", "ffi_dsa", FFI_DSA_Test);
5083
BOTAN_REGISTER_TEST("ffi", "ffi_ecdsa", FFI_ECDSA_Test);
5084
BOTAN_REGISTER_TEST("ffi", "ffi_sm2_sig", FFI_SM2_Sig_Test);
5085
BOTAN_REGISTER_TEST("ffi", "ffi_sm2_enc", FFI_SM2_Enc_Test);
5086
BOTAN_REGISTER_TEST("ffi", "ffi_ecdh", FFI_ECDH_Test);
5087
BOTAN_REGISTER_TEST("ffi", "ffi_mceliece", FFI_McEliece_Test);
5088
BOTAN_REGISTER_TEST("ffi", "ffi_ed25519", FFI_Ed25519_Test);
5089
BOTAN_REGISTER_TEST("ffi", "ffi_ed448", FFI_Ed448_Test);
5090
BOTAN_REGISTER_TEST("ffi", "ffi_x25519", FFI_X25519_Test);
5091
BOTAN_REGISTER_TEST("ffi", "ffi_x448", FFI_X448_Test);
5092
BOTAN_REGISTER_TEST("ffi", "ffi_kyber512", FFI_Kyber512_Test);
5093
BOTAN_REGISTER_TEST("ffi", "ffi_kyber768", FFI_Kyber768_Test);
5094
BOTAN_REGISTER_TEST("ffi", "ffi_kyber1024", FFI_Kyber1024_Test);
5095
BOTAN_REGISTER_TEST("ffi", "ffi_ml_kem", FFI_ML_KEM_Test);
5096
BOTAN_REGISTER_TEST("ffi", "ffi_ml_dsa", FFI_ML_DSA_Test);
5097
BOTAN_REGISTER_TEST("ffi", "ffi_slh_dsa", FFI_SLH_DSA_Test);
5098
BOTAN_REGISTER_TEST("ffi", "ffi_frodokem", FFI_FrodoKEM_Test);
5099
BOTAN_REGISTER_TEST("ffi", "ffi_cmce", FFI_Classic_McEliece_Test);
5100
BOTAN_REGISTER_TEST("ffi", "ffi_elgamal", FFI_ElGamal_Test);
5101
BOTAN_REGISTER_TEST("ffi", "ffi_dh", FFI_DH_Test);
5102
BOTAN_REGISTER_TEST("ffi", "ffi_oid", FFI_OID_Test);
5103
BOTAN_REGISTER_TEST("ffi", "ffi_ec_group", FFI_EC_Group_Test);
5104
BOTAN_REGISTER_TEST("ffi", "ffi_srp6", FFI_SRP6_Test);
5105

5106
#endif
5107

5108
}  // namespace
5109

5110
}  // namespace Botan_Tests
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