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

randombit / botan / 43671805193

14 Nov 2026 08:17PM UTC coverage: 92.476% (+2.5%) from 90.002%
43671805193

push

github

web-flow
Merge pull request #6128 from randombit/jack/prefer-montgomery

Prefer Montgomery over Barrett multiplication in most cases

145074 of 156877 relevant lines covered (92.48%)

8685307.64 hits per line

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

98.43
/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
#include "tests.h"
11
#include <botan/version.h>
12

13
#if defined(BOTAN_HAS_FFI)
14
   #include <botan/ber_dec.h>
15
   #include <botan/ec_group.h>
16
   #include <botan/ffi.h>
17
   #include <botan/hex.h>
18
   #include <botan/mem_ops.h>
19
   #include <botan/internal/calendar.h>
20
   #include <botan/internal/concat_util.h>
21
   #include <botan/internal/ffi_rng.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_X509)
30
   #include <botan/pkix_enums.h>
31
   #include <botan/pkix_types.h>
32
#endif
33

34
#if defined(BOTAN_HAS_TPM2)
35
   #include <tss2/tss2_esys.h>
36
   #include <tss2/tss2_tctildr.h>
37
#endif
38

39
namespace Botan_Tests {
40

41
namespace {
42

43
#if defined(BOTAN_HAS_FFI)
44

45
// NOLINTBEGIN(*-macro-usage)
46

47
   #define _TEST_FFI_STR_HELPER(x) #x
48

49
   #define _TEST_FFI_STR(x) _TEST_FFI_STR_HELPER(x)
50

51
   #define _TEST_FFI_SOURCE_LOCATION(func, file, line) (func " invoked at " file ":" _TEST_FFI_STR(line))
52

53
   #define TEST_FFI_OK(func, args) result.test_rc_ok(_TEST_FFI_SOURCE_LOCATION(#func, __FILE__, __LINE__), func args)
54

55
   #define TEST_FFI_INIT(func, args) \
56
      result.test_rc_init(_TEST_FFI_SOURCE_LOCATION(#func, __FILE__, __LINE__), func args)
57

58
   #define TEST_FFI_FAIL(msg, func, args) \
59
      result.test_rc_fail(_TEST_FFI_SOURCE_LOCATION(#func, __FILE__, __LINE__), msg, func args)
60

61
   #define TEST_FFI_RC(rc, func, args) \
62
      result.test_rc(_TEST_FFI_SOURCE_LOCATION(#func, __FILE__, __LINE__), func args, rc)
63

64
   #define REQUIRE_FFI_OK(func, args)                           \
65
      if(!TEST_FFI_OK(func, args)) {                            \
66
         result.test_note("Exiting test early due to failure"); \
67
         return;                                                \
68
      }
69

70
// NOLINTEND(*-macro-usage)
71

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

86
      botan_view_bin_fn callback() { return &write_fn; }
286 ✔
87

88
      std::span<const uint8_t> get() const { return m_buf; }
17 ✔
89

90
      const uint8_t* data() const { return m_buf.data(); }
19 ✔
91

92
      size_t size() const { return m_buf.size(); }
8 ✔
93

94
   private:
95
      static int write_fn(void* ctx, const uint8_t buf[], size_t len) {
273 ✔
96
         if(ctx == nullptr || buf == nullptr) {
273 ✔
97
            return BOTAN_FFI_ERROR_NULL_POINTER;
98
         }
99

100
         auto* sink = static_cast<ViewBytesSink*>(ctx);
273 ✔
101
         sink->m_buf.assign(buf, buf + len);
273 ✔
102

103
         return BOTAN_FFI_SUCCESS;
273 ✔
104
      }
105

106
   private:
107
      std::vector<uint8_t> m_buf;
108
};
109

110
/**
111
 * See ViewBytesSink for how to use this. Works for `botan_view_str_fn` instead.
112
*/
113
class ViewStringSink final {
28 ✔
114
   public:
115
      void* delegate() { return this; }
116

117
      botan_view_str_fn callback() { return &write_fn; }
76 ✔
118

119
      const std::string& get() { return m_str; }
63 ✔
120

121
   private:
122
      static int write_fn(void* ctx, const char* str, size_t len) {
74 ✔
123
         if(ctx == nullptr || str == nullptr) {
74 ✔
124
            return BOTAN_FFI_ERROR_NULL_POINTER;
125
         }
126

127
         auto* sink = static_cast<ViewStringSink*>(ctx);
74 ✔
128
         // discard the null terminator
129
         sink->m_str = std::string(str, len - 1);
74 ✔
130

131
         return BOTAN_FFI_SUCCESS;
74 ✔
132
      }
133

134
   private:
135
      std::string m_str;
136
};
137

138
// NOLINTBEGIN(*-init-variables)
139

140
class FFI_Test : public Test {
60 ✔
141
   public:
142
      std::vector<Test::Result> run() override {
60 ✔
143
         Test::Result result(this->name());
60 ✔
144

145
         if(!skip_this_test()) {
60 ✔
146
            botan_rng_t rng;
60 ✔
147
            if(botan_rng_init(&rng, "system") != 0) {
60 ✔
148
               result.test_failure("Failed to init RNG");
×
149
               return {result};
×
150
            }
151

152
            result.start_timer();
60 ✔
153
            ffi_test(result, rng);
60 ✔
154
            result.end_timer();
60 ✔
155

156
            botan_rng_destroy(rng);
60 ✔
157
         } else {
158
            result.test_note("FFI test asked to be skipped");
×
159
         }
160

161
         return {result};
120 ✔
162
      }
120 ✔
163

164
   private:
165
      virtual std::string name() const = 0;
166
      virtual void ffi_test(Test::Result& result, botan_rng_t rng) = 0;
167

168
      virtual bool skip_this_test() const { return false; }
57 ✔
169
};
170

171
void ffi_test_pubkey_export(Test::Result& result, botan_pubkey_t pub, botan_privkey_t priv, botan_rng_t rng) {
12 ✔
172
   // export public key
173
   size_t pubkey_len = 0;
12 ✔
174
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
12 ✔
175
               botan_pubkey_export,
176
               (pub, nullptr, &pubkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
177

178
   std::vector<uint8_t> pubkey(pubkey_len);
12 ✔
179
   TEST_FFI_OK(botan_pubkey_export, (pub, pubkey.data(), &pubkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
12 ✔
180

181
   pubkey_len = 0;
12 ✔
182
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
12 ✔
183
               botan_pubkey_export,
184
               (pub, nullptr, &pubkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
185

186
   pubkey.resize(pubkey_len);
12 ✔
187
   TEST_FFI_OK(botan_pubkey_export, (pub, pubkey.data(), &pubkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
12 ✔
188

189
   // reimport exported public key
190
   botan_pubkey_t pub_copy;
12 ✔
191
   TEST_FFI_OK(botan_pubkey_load, (&pub_copy, pubkey.data(), pubkey_len));
12 ✔
192
   TEST_FFI_OK(botan_pubkey_check_key, (pub_copy, rng, 0));
12 ✔
193
   TEST_FFI_OK(botan_pubkey_destroy, (pub_copy));
12 ✔
194

195
   // export private key
196
   std::vector<uint8_t> privkey;
12 ✔
197
   size_t privkey_len = 0;
12 ✔
198

199
   // call with nullptr to query the length
200
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
12 ✔
201
               botan_privkey_export,
202
               (priv, nullptr, &privkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
203

204
   privkey.resize(privkey_len);
12 ✔
205
   privkey_len = privkey.size();  // set buffer size
12 ✔
206

207
   TEST_FFI_OK(botan_privkey_export, (priv, privkey.data(), &privkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
12 ✔
208

209
   privkey.resize(privkey_len);
12 ✔
210

211
   result.test_sz_gte("Reasonable size", privkey.size(), 32);
12 ✔
212

213
   // reimport exported private key
214
   botan_privkey_t copy;
12 ✔
215
   TEST_FFI_OK(botan_privkey_load, (&copy, rng, privkey.data(), privkey.size(), nullptr));
12 ✔
216
   botan_privkey_destroy(copy);
12 ✔
217

218
   // Now again for PEM
219
   privkey_len = 0;
12 ✔
220

221
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
12 ✔
222
               botan_privkey_export,
223
               (priv, nullptr, &privkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
224

225
   privkey.resize(privkey_len);
12 ✔
226
   TEST_FFI_OK(botan_privkey_export, (priv, privkey.data(), &privkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
12 ✔
227

228
   TEST_FFI_OK(botan_privkey_load, (&copy, rng, privkey.data(), privkey.size(), nullptr));
12 ✔
229
   botan_privkey_destroy(copy);
12 ✔
230

231
   #if defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_PKCS5_PBES2)
232
   const size_t pbkdf_iter = 1000;
12 ✔
233

234
   // export private key encrypted
235
   privkey_len = 0;
12 ✔
236
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
12 ✔
237
               botan_privkey_export_encrypted_pbkdf_iter,
238
               (priv, nullptr, &privkey_len, rng, "password", pbkdf_iter, "", "", BOTAN_PRIVKEY_EXPORT_FLAG_DER));
239

240
   privkey.resize(privkey_len);
12 ✔
241
   privkey_len = privkey.size();
12 ✔
242

243
   TEST_FFI_OK(
12 ✔
244
      botan_privkey_export_encrypted_pbkdf_iter,
245
      (priv, privkey.data(), &privkey_len, rng, "password", pbkdf_iter, "", "", BOTAN_PRIVKEY_EXPORT_FLAG_DER));
246

247
   // reimport encrypted private key
248
   botan_privkey_load(&copy, rng, privkey.data(), privkey.size(), "password");
12 ✔
249
   botan_privkey_destroy(copy);
12 ✔
250

251
   privkey_len = 0;
12 ✔
252
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
12 ✔
253
               botan_privkey_export_encrypted_pbkdf_iter,
254
               (priv, nullptr, &privkey_len, rng, "password", pbkdf_iter, "", "", BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
255

256
   privkey.resize(privkey_len);
12 ✔
257
   TEST_FFI_OK(
12 ✔
258
      botan_privkey_export_encrypted_pbkdf_iter,
259
      (priv, privkey.data(), &privkey_len, rng, "password", pbkdf_iter, "", "", BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
260

261
   privkey.resize(privkey_len * 2);
12 ✔
262
   privkey_len = privkey.size();
12 ✔
263
   const uint32_t pbkdf_msec = 100;
12 ✔
264
   size_t pbkdf_iters_out = 0;
12 ✔
265

266
      #if defined(BOTAN_HAS_SCRYPT)
267
   const std::string pbe_hash = "Scrypt";
12 ✔
268
      #else
269
   const std::string pbe_hash = "SHA-512";
270
      #endif
271

272
      #if defined(BOTAN_HAS_AEAD_GCM)
273
   const std::string pbe_cipher = "AES-256/GCM";
12 ✔
274
      #else
275
   const std::string pbe_cipher = "AES-256/CBC";
276
      #endif
277

278
   TEST_FFI_OK(botan_privkey_export_encrypted_pbkdf_msec,
12 ✔
279
               (priv,
280
                privkey.data(),
281
                &privkey_len,
282
                rng,
283
                "password",
284
                pbkdf_msec,
285
                &pbkdf_iters_out,
286
                pbe_cipher.c_str(),
287
                pbe_hash.c_str(),
288
                0));
289

290
   if(pbe_hash == "Scrypt") {
12 ✔
291
      result.test_sz_eq("Scrypt iters set to zero in this API", pbkdf_iters_out, 0);
12 ✔
292
   } else {
293
      // PBKDF2 currently always rounds to multiple of 2000
294
      result.test_sz_eq("Expected PBKDF2 iters", pbkdf_iters_out % 2000, 0);
×
295
   }
296

297
   privkey.resize(privkey_len);
12 ✔
298

299
   TEST_FFI_OK(botan_privkey_load, (&copy, rng, privkey.data(), privkey.size(), "password"));
12 ✔
300
   botan_privkey_destroy(copy);
12 ✔
301
   #endif
302

303
   // calculate fingerprint
304
   size_t strength = 0;
12 ✔
305
   TEST_FFI_OK(botan_pubkey_estimated_strength, (pub, &strength));
12 ✔
306
   result.test_sz_gte("estimated strength", strength, 1);
12 ✔
307

308
   size_t fingerprint_len = 0;
12 ✔
309
   TEST_FFI_RC(
12 ✔
310
      BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_pubkey_fingerprint, (pub, "SHA-256", nullptr, &fingerprint_len));
311

312
   std::vector<uint8_t> fingerprint(fingerprint_len);
12 ✔
313
   TEST_FFI_OK(botan_pubkey_fingerprint, (pub, "SHA-256", fingerprint.data(), &fingerprint_len));
12 ✔
314
}
12 ✔
315

316
class FFI_Utils_Test final : public FFI_Test {
1 ✔
317
   public:
318
      std::string name() const override { return "FFI Utils"; }
1 ✔
319

320
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
321
         result.test_u32_eq("FFI API version macro", uint32_t(BOTAN_FFI_API_VERSION), uint32_t(BOTAN_HAS_FFI));
1 ✔
322
         result.test_u32_eq("FFI API version function", botan_ffi_api_version(), uint32_t(BOTAN_HAS_FFI));
1 ✔
323
         result.test_u32_eq("Major version", botan_version_major(), Botan::version_major());
1 ✔
324
         result.test_u32_eq("Minor version", botan_version_minor(), Botan::version_minor());
1 ✔
325
         result.test_u32_eq("Patch version", botan_version_patch(), Botan::version_patch());
1 ✔
326
         result.test_str_eq("Botan version", botan_version_string(), Botan::version_cstr());
1 ✔
327
         result.test_u32_eq("Botan version datestamp", botan_version_datestamp(), Botan::version_datestamp());
1 ✔
328
         result.test_rc_ok("FFI supports its own version", botan_ffi_supports_api(botan_ffi_api_version()));
1 ✔
329

330
         result.test_u32_eq("FFI compile time time var matches botan_ffi_api_version",
1 ✔
331
                            botan_ffi_api_version(),
332
                            uint32_t(BOTAN_FFI_API_VERSION));
333

334
         result.test_rc_ok("FFI supports 2.0 version", botan_ffi_supports_api(20150515));
1 ✔
335
         result.test_rc_ok("FFI supports 2.1 version", botan_ffi_supports_api(20170327));
1 ✔
336
         result.test_rc_ok("FFI supports 2.3 version", botan_ffi_supports_api(20170815));
1 ✔
337
         result.test_rc_ok("FFI supports 2.8 version", botan_ffi_supports_api(20180713));
1 ✔
338

339
         result.test_rc("FFI doesn't support bogus version", botan_ffi_supports_api(20160229), -1);
1 ✔
340

341
         const std::vector<uint8_t> mem1 = {0xFF, 0xAA, 0xFF};
1 ✔
342
         const std::vector<uint8_t> mem2 = {0xFF, 0xA9, 0xFF};
1 ✔
343

344
         TEST_FFI_RC(0, botan_constant_time_compare, (mem1.data(), mem1.data(), mem1.size()));
1 ✔
345
         TEST_FFI_RC(-1, botan_constant_time_compare, (mem1.data(), mem2.data(), mem1.size()));
1 ✔
346

347
         std::vector<uint8_t> to_zero = {0xFF, 0xA0};
1 ✔
348
         TEST_FFI_OK(botan_scrub_mem, (to_zero.data(), to_zero.size()));
1 ✔
349
         result.test_is_true("scrub_memory zeros", to_zero[0] == 0 && to_zero[1] == 0);
1 ✔
350

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

353
         std::string outstr;
1 ✔
354
         outstr.resize(2 * bin.size());
1 ✔
355
         TEST_FFI_OK(botan_hex_encode, (bin.data(), bin.size(), outstr.data(), 0));
1 ✔
356
         result.test_str_eq("uppercase hex", outstr, "AADE01");
1 ✔
357

358
         TEST_FFI_OK(botan_hex_encode, (bin.data(), bin.size(), outstr.data(), BOTAN_FFI_HEX_LOWER_CASE));
1 ✔
359
         result.test_str_eq("lowercase hex", outstr, "aade01");
1 ✔
360

361
         // botan_is_shared() and botan_use_count() with a handle type that may
362
         // share internal ownership of the underlying object (botan_rng_t)
363
         botan_rng_t sharable_rng = nullptr;
1 ✔
364
         REQUIRE_FFI_OK(botan_rng_init, (&sharable_rng, nullptr));
1 ✔
365

366
         result.test_u32_eq("RNG handle can be shared internally", uint32_t(botan_is_shared(sharable_rng)), 1);
1 ✔
367

368
         size_t use_count = 0;
1 ✔
369
         TEST_FFI_OK(botan_use_count, (sharable_rng, &use_count));
1 ✔
370
         result.test_sz_eq("RNG handle use count is 1", use_count, 1);
1 ✔
371

372
         {
1 ✔
373
            // simulate another handle retaining internal ownership of the RNG
374
            auto internal_reference = sharable_rng->unsafe_get_sp();
1 ✔
375
            TEST_FFI_OK(botan_use_count, (sharable_rng, &use_count));
1 ✔
376
            result.test_sz_eq("RNG handle use count is 1", use_count, 2);
1 ✔
377
         }
×
378

379
         TEST_FFI_OK(botan_use_count, (sharable_rng, &use_count));
1 ✔
380
         result.test_sz_eq("RNG handle use count after internal consumer is gone", use_count, 1);
1 ✔
381

382
         TEST_FFI_OK(botan_rng_destroy, (sharable_rng));
1 ✔
383

384
         // ... and with a handle type that owns the underlying object uniquely
385
         botan_mp_t unique_mp = nullptr;
1 ✔
386
         REQUIRE_FFI_OK(botan_mp_init, (&unique_mp));
1 ✔
387

388
         result.test_u32_eq("MP handle cannot be shared internally", uint32_t(botan_is_shared(unique_mp)), 0);
1 ✔
389

390
         use_count = 0;
1 ✔
391
         TEST_FFI_OK(botan_use_count, (unique_mp, &use_count));
1 ✔
392
         result.test_sz_eq("MP handle use count is always 1", use_count, 1);
1 ✔
393

394
         // Error handling for invalid handles
395
         TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_is_shared, (nullptr));
1 ✔
396

397
         use_count = 42;
1 ✔
398
         TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_use_count, (unique_mp, nullptr));
1 ✔
399
         TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_use_count, (nullptr, &use_count));
1 ✔
400
         result.test_sz_eq("use_count out-param zeroed on error", use_count, 0);
1 ✔
401

402
         TEST_FFI_OK(botan_mp_destroy, (unique_mp));
1 ✔
403

404
         // A live handle with invalidated magic must be rejected.
405
         struct Invalid_FFI_Handle final : Botan_FFI::botan_ffi_obj_base {
1 ✔
406
               Invalid_FFI_Handle() : botan_ffi_obj_base(0xDEADBEEF) { invalidate_magic(); }
1 ✔
407

408
               bool is_shared() const override { return false; }
×
409

410
               size_t use_count() const override { return 1; }
×
411
         } invalid_handle;
1 ✔
412

413
         auto* invalid_base = static_cast<Botan_FFI::botan_ffi_obj_base*>(&invalid_handle);
1 ✔
414
         use_count = 1337;
1 ✔
415
         TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT, botan_is_shared, (invalid_base));
1 ✔
416
         TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT, botan_use_count, (invalid_base, &use_count));
1 ✔
417
         result.test_sz_eq("use_count out-param zeroed on error", use_count, 0);
1 ✔
418
      }
1 ✔
419
};
420

421
class FFI_RNG_Test final : public FFI_Test {
1 ✔
422
   public:
423
      std::string name() const override { return "FFI RNG"; }
1 ✔
424

425
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
426
         // RNG test and initialization
427
         botan_rng_t rng;
1 ✔
428
         botan_rng_t system_rng;
1 ✔
429
         botan_rng_t hwrng_rng = nullptr;
1 ✔
430
         botan_rng_t null_rng;
1 ✔
431
         botan_rng_t custom_rng;
1 ✔
432
         botan_rng_t tpm2_rng = nullptr;
1 ✔
433

434
         botan_tpm2_ctx_t tpm2_ctx = nullptr;
1 ✔
435
         botan_tpm2_session_t tpm2_session = nullptr;
1 ✔
436

437
         TEST_FFI_FAIL("invalid rng type", botan_rng_init, (&rng, "invalid_type"));
1 ✔
438

439
         REQUIRE_FFI_OK(botan_rng_init, (&system_rng, "system"));
1 ✔
440
         REQUIRE_FFI_OK(botan_rng_init, (&null_rng, "null"));
1 ✔
441

442
         int rc = botan_rng_init(&hwrng_rng, "hwrng");
1 ✔
443
         result.test_is_true("Either success or not implemented", rc == 0 || rc == BOTAN_FFI_ERROR_NOT_IMPLEMENTED);
1 ✔
444

445
         std::vector<uint8_t> outbuf(512);
1 ✔
446

447
         rc = botan_rng_init(&rng, "user-threadsafe");
1 ✔
448
         result.test_is_true("Either success or not implemented", rc == 0 || rc == BOTAN_FFI_ERROR_NOT_IMPLEMENTED);
1 ✔
449

450
         if(rc != 0) {
1 ✔
451
            REQUIRE_FFI_OK(botan_rng_init, (&rng, "user"));
×
452
            REQUIRE_FFI_OK(botan_rng_destroy, (rng));
×
453
         }
454

455
         if(rc == 0) {
1 ✔
456
            TEST_FFI_OK(botan_rng_get, (rng, outbuf.data(), outbuf.size()));
1 ✔
457
            TEST_FFI_OK(botan_rng_reseed, (rng, 256));
1 ✔
458

459
            TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE, botan_rng_reseed_from_rng, (rng, null_rng, 256));
1 ✔
460
            if(hwrng_rng != nullptr) {
1 ✔
461
               TEST_FFI_OK(botan_rng_reseed_from_rng, (rng, hwrng_rng, 256));
1 ✔
462
            }
463
            TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE, botan_rng_get, (null_rng, outbuf.data(), outbuf.size()));
1 ✔
464

465
            TEST_FFI_OK(botan_rng_destroy, (rng));
1 ✔
466
         }
467

468
         if(TEST_FFI_OK(botan_rng_init, (&rng, "user"))) {
1 ✔
469
            TEST_FFI_OK(botan_rng_get, (rng, outbuf.data(), outbuf.size()));
1 ✔
470
            TEST_FFI_OK(botan_rng_reseed, (rng, 256));
1 ✔
471

472
            TEST_FFI_OK(botan_rng_reseed_from_rng, (rng, system_rng, 256));
1 ✔
473

474
            uint8_t not_really_entropy[32] = {0};
1 ✔
475
            TEST_FFI_OK(botan_rng_add_entropy, (rng, not_really_entropy, 32));
1 ✔
476
         }
477

478
         uint8_t system_rng_buf[4096];
1 ✔
479
         TEST_FFI_OK(botan_system_rng_get, (system_rng_buf, sizeof(system_rng_buf)));
1 ✔
480

481
         size_t cb_counter = 0;
1 ✔
482

483
         auto custom_get_cb = +[](void* context, uint8_t* out, size_t out_len) -> int {
1 ✔
484
            for(size_t i = 0; i != out_len; ++i) {
485
               out[i] = 0x12;
486
            }
487
            (*(static_cast<size_t*>(context)))++;
488
            return 0;
489
         };
490

491
         auto custom_add_entropy_cb = +[](void* context, const uint8_t input[], size_t length) -> int {
1 ✔
492
            BOTAN_UNUSED(input, length);
493
            (*(static_cast<size_t*>(context)))++;
494
            return 0;
495
         };
496

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

499
         if(TEST_FFI_OK(
1 ✔
500
               botan_rng_init_custom,
501
               (&custom_rng, "custom rng", &cb_counter, custom_get_cb, custom_add_entropy_cb, custom_destroy_cb))) {
502
            Botan::clear_mem(outbuf.data(), outbuf.size());
1 ✔
503
            TEST_FFI_OK(botan_rng_get, (custom_rng, outbuf.data(), outbuf.size()));
1 ✔
504
            result.test_sz_eq("custom_get_cb called", cb_counter, 1);
1 ✔
505
            std::vector<uint8_t> pattern(outbuf.size(), 0x12);
1 ✔
506
            result.test_bin_eq("custom_get_cb returned bytes", pattern, outbuf);
1 ✔
507

508
            TEST_FFI_OK(botan_rng_reseed, (custom_rng, 256));
1 ✔
509
            result.test_sz_eq("custom_add_entropy_cb called", cb_counter, 2);
1 ✔
510

511
            TEST_FFI_OK(botan_rng_reseed_from_rng, (custom_rng, system_rng, 256));
1 ✔
512
            result.test_sz_eq("custom_add_entropy_cb called", cb_counter, 3);
1 ✔
513

514
            uint8_t not_really_entropy[32] = {0};
1 ✔
515
            TEST_FFI_OK(botan_rng_add_entropy, (custom_rng, not_really_entropy, 32));
1 ✔
516
            result.test_sz_eq("custom_add_entropy_cb called", cb_counter, 4);
1 ✔
517

518
            TEST_FFI_OK(botan_rng_destroy, (custom_rng));
1 ✔
519
            result.test_sz_eq("custom_destroy_cb called", cb_counter, 5);
1 ✔
520
         }
1 ✔
521

522
   #if defined(BOTAN_HAS_HMAC_DRBG) && defined(BOTAN_HAS_SHA2_32)
523
         botan_rng_t drbg = nullptr;
1 ✔
524
         std::vector<uint8_t> drbg_seed(32, 0xAB);
1 ✔
525
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER,
1 ✔
526
                     botan_rng_init_drbg,
527
                     (&drbg, "HMAC_DRBG(SHA-256)", drbg_seed.data(), drbg_seed.size() - 1));
528
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_rng_init_drbg, (&drbg, "HMAC_DRBG(SHA-256)", nullptr, 0));
1 ✔
529
         if(TEST_FFI_OK(botan_rng_init_drbg, (&drbg, "HMAC_DRBG(SHA-256)", drbg_seed.data(), drbg_seed.size()))) {
1 ✔
530
            TEST_FFI_OK(botan_rng_get, (drbg, outbuf.data(), outbuf.size()));
1 ✔
531
            TEST_FFI_OK(botan_rng_destroy, (drbg));
1 ✔
532
         }
533
   #endif
534

535
   #ifdef BOTAN_HAS_JITTER_RNG
536
         botan_rng_t jitter_rng;
1 ✔
537
         if(TEST_FFI_OK(botan_rng_init, (&jitter_rng, "jitter"))) {
1 ✔
538
            const std::vector<uint8_t> buf(256);
1 ✔
539
            TEST_FFI_OK(botan_rng_get, (jitter_rng, outbuf.data(), buf.size()));
1 ✔
540
            TEST_FFI_OK(botan_rng_destroy, (jitter_rng));
1 ✔
541
         }
1 ✔
542
   #endif
543

544
         const auto tcti_name = Test::options().tpm2_tcti_name().value_or("");
1 ✔
545
         const auto tcti_conf = Test::options().tpm2_tcti_conf().value_or("");
1 ✔
546
         if(tcti_name.empty() || tcti_name == "disabled") {
1 ✔
547
            result.test_note("TPM2 tests are disabled.");
×
548
         } else {
549
            const auto tpm2_test_rng = [&](botan_tpm2_ctx_t tpm2_context) {
4 ✔
550
               // Create and use an RNG without a TPM2 session
551
               // (communication between application and TPM won't be encrypted)
552
               if(TEST_FFI_INIT(botan_tpm2_rng_init, (&tpm2_rng, tpm2_context, nullptr, nullptr, nullptr))) {
3 ✔
553
                  Botan::clear_mem(outbuf.data(), outbuf.size());
3 ✔
554

555
                  TEST_FFI_OK(botan_rng_get, (tpm2_rng, outbuf.data(), outbuf.size()));
3 ✔
556
                  TEST_FFI_OK(botan_rng_reseed, (tpm2_rng, 256));
3 ✔
557

558
                  TEST_FFI_OK(botan_rng_reseed_from_rng, (tpm2_rng, system_rng, 256));
3 ✔
559

560
                  uint8_t not_really_entropy[32] = {0};
3 ✔
561
                  TEST_FFI_OK(botan_rng_add_entropy, (tpm2_rng, not_really_entropy, 32));
3 ✔
562
                  TEST_FFI_OK(botan_rng_destroy, (tpm2_rng));
3 ✔
563
               }
564

565
               // Create an anonymous TPM2 session
566
               if(TEST_FFI_INIT(botan_tpm2_unauthenticated_session_init, (&tpm2_session, tpm2_context))) {
3 ✔
567
                  // Create and use an RNG with an anonymous TPM2 session
568
                  // (communication between application and TPM will be encrypted)
569
                  if(TEST_FFI_INIT(botan_tpm2_rng_init, (&tpm2_rng, tpm2_context, tpm2_session, nullptr, nullptr))) {
3 ✔
570
                     Botan::clear_mem(outbuf.data(), outbuf.size());
3 ✔
571

572
                     TEST_FFI_OK(botan_rng_get, (tpm2_rng, outbuf.data(), outbuf.size()));
3 ✔
573
                     TEST_FFI_OK(botan_rng_reseed, (tpm2_rng, 256));
3 ✔
574

575
                     TEST_FFI_OK(botan_rng_reseed_from_rng, (tpm2_rng, system_rng, 256));
3 ✔
576

577
                     uint8_t not_really_entropy[32] = {0};
3 ✔
578
                     TEST_FFI_OK(botan_rng_add_entropy, (tpm2_rng, not_really_entropy, 32));
3 ✔
579
                     TEST_FFI_OK(botan_rng_destroy, (tpm2_rng));
3 ✔
580
                  }
581

582
                  TEST_FFI_OK(botan_tpm2_session_destroy, (tpm2_session));
3 ✔
583
               }
584
            };
4 ✔
585

586
            if(TEST_FFI_INIT(botan_tpm2_ctx_init_ex, (&tpm2_ctx, tcti_name.c_str(), tcti_conf.c_str()))) {
1 ✔
587
               if(botan_tpm2_supports_crypto_backend() == 1) {
1 ✔
588
                  TEST_FFI_OK(botan_tpm2_ctx_enable_crypto_backend, (tpm2_ctx, system_rng));
1 ✔
589
                  result.test_note("TPM2 crypto backend enabled");
1 ✔
590
               } else {
591
                  result.test_note("TPM2 crypto backend not supported");
×
592
               }
593

594
               tpm2_test_rng(tpm2_ctx);
1 ✔
595
               TEST_FFI_OK(botan_tpm2_ctx_destroy, (tpm2_ctx));
1 ✔
596
            }
597

598
            const std::string tcti_nameconf = tcti_conf.empty() ? tcti_name : tcti_name + ":" + tcti_conf;
2 ✔
599
            if(TEST_FFI_INIT(botan_tpm2_ctx_init, (&tpm2_ctx, tcti_nameconf.c_str()))) {
1 ✔
600
               tpm2_test_rng(tpm2_ctx);
1 ✔
601
               TEST_FFI_OK(botan_tpm2_ctx_destroy, (tpm2_ctx));
1 ✔
602
            }
603

604
   #if defined(BOTAN_HAS_TPM2)
605
            TSS2_TCTI_CONTEXT* tcti_ctx;
1 ✔
606
            ESYS_CONTEXT* esys_ctx;
1 ✔
607

608
            if(TEST_FFI_INIT(Tss2_TctiLdr_Initialize_Ex, (tcti_name.c_str(), tcti_conf.c_str(), &tcti_ctx))) {
1 ✔
609
               if(TEST_FFI_INIT(Esys_Initialize, (&esys_ctx, tcti_ctx, nullptr /* ABI version */))) {
1 ✔
610
                  botan_tpm2_crypto_backend_state_t cbs = nullptr;
1 ✔
611

612
                  // enable the botan-based TSS2 crypto backend on a bare ESYS_CONTEXT
613
                  if(botan_tpm2_supports_crypto_backend() == 1) {
1 ✔
614
                     TEST_FFI_OK(botan_tpm2_enable_crypto_backend, (&cbs, esys_ctx, system_rng));
1 ✔
615
                     result.test_note("TPM2 crypto backend enabled");
1 ✔
616
                  } else {
617
                     result.test_note("TPM2 crypto backend not supported");
×
618
                  }
619

620
                  // initialize the Botan TPM2 FFI wrapper from the bare ESYS_CONTEXT
621
                  if(TEST_FFI_INIT(botan_tpm2_ctx_from_esys, (&tpm2_ctx, esys_ctx))) {
1 ✔
622
                     tpm2_test_rng(tpm2_ctx);
1 ✔
623
                     TEST_FFI_OK(botan_tpm2_ctx_destroy, (tpm2_ctx));
1 ✔
624
                  }
625

626
                  if(cbs != nullptr) {
1 ✔
627
                     TEST_FFI_OK(botan_tpm2_crypto_backend_state_destroy, (cbs));
1 ✔
628
                  }
629

630
                  Esys_Finalize(&esys_ctx);
1 ✔
631
               }
632
               Tss2_TctiLdr_Finalize(&tcti_ctx);
1 ✔
633
            }
634
   #endif
635
         }
1 ✔
636

637
         TEST_FFI_OK(botan_rng_destroy, (rng));
1 ✔
638
         TEST_FFI_OK(botan_rng_destroy, (null_rng));
1 ✔
639
         TEST_FFI_OK(botan_rng_destroy, (system_rng));
1 ✔
640
         TEST_FFI_OK(botan_rng_destroy, (hwrng_rng));
1 ✔
641
      }
1 ✔
642
};
643

644
class FFI_RSA_Cert_Test final : public FFI_Test {
1 ✔
645
   public:
646
      std::string name() const override { return "FFI RSA cert"; }
1 ✔
647

648
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
649
         botan_x509_cert_t cert;
1 ✔
650
         if(TEST_FFI_INIT(botan_x509_cert_load_file, (&cert, Test::data_file("x509/ocsp/randombit.pem").c_str()))) {
1 ✔
651
            TEST_FFI_RC(0, botan_x509_cert_hostname_match, (cert, "randombit.net"));
1 ✔
652
            TEST_FFI_RC(0, botan_x509_cert_hostname_match, (cert, "www.randombit.net"));
1 ✔
653
            TEST_FFI_RC(-1, botan_x509_cert_hostname_match, (cert, "*.randombit.net"));
1 ✔
654
            TEST_FFI_RC(-1, botan_x509_cert_hostname_match, (cert, "flub.randombit.net"));
1 ✔
655
            TEST_FFI_RC(-1, botan_x509_cert_hostname_match, (cert, "randombit.net.com"));
1 ✔
656

657
            botan_x509_cert_t copy;
1 ✔
658
            TEST_FFI_OK(botan_x509_cert_dup, (&copy, cert));
1 ✔
659
            TEST_FFI_RC(0, botan_x509_cert_hostname_match, (copy, "randombit.net"));
1 ✔
660

661
            TEST_FFI_OK(botan_x509_cert_destroy, (copy));
1 ✔
662
            TEST_FFI_OK(botan_x509_cert_destroy, (cert));
1 ✔
663
         }
664
      }
1 ✔
665
};
666

667
class FFI_ZFEC_Test final : public FFI_Test {
1 ✔
668
   public:
669
      std::string name() const override { return "FFI ZFEC"; }
1 ✔
670

671
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
672
         /* exercise a simple success case
673
          */
674

675
         // Select some arbitrary, valid encoding parameters.  There is
676
         // nothing special about them but some relationships between these
677
         // values and other inputs must hold.
678
         const size_t K = 3;
1 ✔
679
         const size_t N = 11;
1 ✔
680

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

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

691
         // K of the blocks are required so the total information represented
692
         // can be this multiple.  totalSize must be a multiple of K and it
693
         // always will be using this construction.
694
         const size_t totalSize = blockSize * K;
1 ✔
695

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

700
         // Allocate memory for the encoding and decoding output parameters.
701
         std::vector<uint8_t> encoded_buf(N * blockSize);
1 ✔
702
         std::vector<uint8_t> decoded_buf(K * blockSize);
1 ✔
703

704
         std::vector<uint8_t*> encoded(N);
1 ✔
705
         for(size_t i = 0; i < N; ++i) {
12 ✔
706
            encoded[i] = &encoded_buf[i * blockSize];
11 ✔
707
         }
708
         std::vector<uint8_t*> decoded(K);
1 ✔
709
         for(size_t i = 0; i < K; ++i) {
4 ✔
710
            decoded[i] = &decoded_buf[i * blockSize];
3 ✔
711
         }
712

713
         // First encode the complete input string into N blocks where K are
714
         // required for reconstruction.  The N encoded blocks will end up in
715
         // `encoded`.
716
         if(!TEST_FFI_INIT(botan_zfec_encode, (K, N, input, totalSize, encoded.data()))) {
1 ✔
717
            return;
718
         }
719

720
         // Any K blocks can be decoded to reproduce the original input (split
721
         // across an array of K strings of blockSize bytes each).  This loop
722
         // only exercises decoding with consecutive blocks because it's
723
         // harder to pick non-consecutive blocks out for a test.
724
         for(size_t offset = 0; offset < N - K; ++offset) {
9 ✔
725
            result.test_note("About to decode with offset " + std::to_string(offset));
24 ✔
726
            // Pass in the K shares starting from `offset` (and their indexes)
727
            // so that we can try decoding a certain group of blocks here.  Any
728
            // K shares *should* work.
729
            REQUIRE_FFI_OK(botan_zfec_decode,
8 ✔
730
                           (K, N, indexes.data() + offset, encoded.data() + offset, blockSize, decoded.data()));
731

732
            // Check that the original input bytes have been written to the
733
            // output parameter.
734
            for(size_t k = 0, pos = 0; k < K; ++k, pos += blockSize) {
32 ✔
735
               TEST_FFI_RC(0, botan_constant_time_compare, (input + pos, decoded[k], blockSize));
24 ✔
736
            }
737
         }
738

739
         /* Exercise a couple basic failure cases, such as you encounter if
740
          * the caller supplies invalid parameters.  We don't try to
741
          * exhaustively prove invalid parameters are handled through this
742
          * interface since the implementation only passes them through to
743
          * ZFEC::{encode,decode} where the real checking is.  We just want to
744
          * see that errors can propagate.
745
          */
746
         TEST_FFI_FAIL("encode with out-of-bounds encoding parameters should have failed",
1 ✔
747
                       botan_zfec_encode,
748
                       (0, 0, nullptr, 0, nullptr));
749
         TEST_FFI_FAIL("decode with out-of-bounds encoding parameters should have failed",
1 ✔
750
                       botan_zfec_decode,
751
                       (0, 0, nullptr, nullptr, 0, nullptr));
752
      }
1 ✔
753
};
754

755
class FFI_CRL_Test final : public FFI_Test {
1 ✔
756
   public:
757
      std::string name() const override { return "FFI CRL"; }
1 ✔
758

759
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
760
         const char* crl_string =
1 ✔
761
            "-----BEGIN X509 CRL-----\n"
762
            "MIICoTCCAQkCAQEwDQYJKoZIhvcNAQELBQAwgZQxLTArBgNVBAMTJFVzYWJsZSBj\n"
763
            "ZXJ0IHZhbGlkYXRpb246IFRlbXBvcmFyeSBDQTE5MDcGA1UECxMwQ2VudHJlIGZv\n"
764
            "ciBSZXNlYXJjaCBvbiBDcnlwdG9ncmFwaHkgYW5kIFNlY3VyaXR5MRswGQYDVQQK\n"
765
            "ExJNYXNhcnlrIFVuaXZlcnNpdHkxCzAJBgNVBAYTAkNaGA8yMDUwMDIyNTE1MjE0\n"
766
            "MloYDzIwNTAwMjI1MTUyNDQxWjAAoDowODAfBgNVHSMEGDAWgBRKzxAvI4+rVVo/\n"
767
            "JzLigRznREyB+TAVBgNVHRQEDgIMXcr16yNys/gjeuCFMA0GCSqGSIb3DQEBCwUA\n"
768
            "A4IBgQCfxv/5REM/KUnzeVycph3dJr1Yrtxhc6pZmQ9pMzSW/nawLN3rUHm5oG44\n"
769
            "ZuQgjvzE4PnbU0/DNRu/4w3H58kgrctJHHXbbvkU3lf2ZZLh2wBl+EUh92+/COow\n"
770
            "ZyGB+jqj/XwB99hYUhrY6NLEWRz08kpgG6dnNMEU0uFqdQKWk0CQPnmgPRgDb8BW\n"
771
            "IuMBcjY7aF9XoCZFOqPYdEvUKzAo4QGCf7uJ7fNGS3LqvjaLjAHJseSr5/yR7Q9r\n"
772
            "nEdI38yKPbRj0tNHe7j+BbYg31C+X+AZZKJtlTg8GxYR3qfQio1kDgpZ3rQLzHY3\n"
773
            "ea2MLX/Kdx9cPSwh4KwlcDxQmQKoELb4EnZW1CScSBHi9HQyCBNyCkgkOBMGcJqz\n"
774
            "Ihq1dGeSf8eca9+Avk5kAQ3yjXK1TI2CDEi0msrXLr9XbgowXiOLLzR+rYkhQz+V\n"
775
            "RnIoBwjnrGoJoz636KS170SZCB9ARNs17WE4IvbJdZrTXNOGaVZCQUUpiLRj4ZSO\n"
776
            "Na/nobI=\n"
777
            "-----END X509 CRL-----";
778

779
         botan_x509_crl_t bytecrl;
1 ✔
780
         if(!TEST_FFI_INIT(botan_x509_crl_load, (&bytecrl, reinterpret_cast<const uint8_t*>(crl_string), 966))) {
1 ✔
781
            return;
×
782
         }
783

784
         botan_x509_crl_t crl_without_next_update;
1 ✔
785
         if(!TEST_FFI_INIT(botan_x509_crl_load_file,
1 ✔
786
                           (&crl_without_next_update,
787
                            Test::data_file("x509/misc/crl_without_nextupdate/valid_forever.crl").c_str()))) {
788
            return;
789
         }
790

791
         uint64_t this_update;
1 ✔
792
         uint64_t next_update;
1 ✔
793
         TEST_FFI_RC(BOTAN_FFI_ERROR_NO_VALUE, botan_x509_crl_next_update, (crl_without_next_update, &next_update));
1 ✔
794
         TEST_FFI_RC(BOTAN_FFI_ERROR_NO_VALUE, botan_x509_crl_next_update, (crl_without_next_update, nullptr));
1 ✔
795
         TEST_FFI_OK(botan_x509_crl_this_update, (bytecrl, &this_update));
1 ✔
796
         TEST_FFI_OK(botan_x509_crl_next_update, (bytecrl, &next_update));
1 ✔
797
         result.test_u64_eq(
1 ✔
798
            "this update", this_update, Botan::calendar_point(2050, 2, 25, 15, 21, 42).seconds_since_epoch());
1 ✔
799
         result.test_u64_eq(
1 ✔
800
            "next update", next_update, Botan::calendar_point(2050, 2, 25, 15, 24, 41).seconds_since_epoch());
1 ✔
801

802
         TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_x509_crl_this_update, (bytecrl, nullptr));
1 ✔
803
         TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_x509_crl_next_update, (bytecrl, nullptr));
1 ✔
804

805
         ViewBytesSink akid;
1 ✔
806
         TEST_FFI_OK(botan_x509_crl_view_binary_values,
1 ✔
807
                     (bytecrl, BOTAN_X509_AUTHORITY_KEY_IDENTIFIER, 0, akid.delegate(), akid.callback()));
808
         result.test_bin_eq("authority key ID", akid.get(), "4ACF102F238FAB555A3F2732E2811CE7444C81F9");
1 ✔
809

810
         TEST_FFI_RC(BOTAN_FFI_ERROR_NO_VALUE,
1 ✔
811
                     botan_x509_crl_view_binary_values,
812
                     (bytecrl, BOTAN_X509_SUBJECT_KEY_IDENTIFIER, 0, akid.delegate(), akid.callback()));
813
         size_t akid_count;
1 ✔
814
         TEST_FFI_OK(botan_x509_crl_view_binary_values_count,
1 ✔
815
                     (bytecrl, BOTAN_X509_SUBJECT_KEY_IDENTIFIER, &akid_count));
816
         result.test_sz_eq("no subject key ID entries", akid_count, 0);
1 ✔
817

818
         botan_x509_crl_t crl;
1 ✔
819
         REQUIRE_FFI_OK(botan_x509_crl_load_file, (&crl, Test::data_file("x509/nist/root.crl").c_str()));
1 ✔
820

821
         botan_x509_cert_t cert1;
1 ✔
822
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&cert1, Test::data_file("x509/nist/test01/end.crt").c_str()));
1 ✔
823
         TEST_FFI_RC(-1, botan_x509_is_revoked, (crl, cert1));
1 ✔
824
         TEST_FFI_OK(botan_x509_cert_destroy, (cert1));
1 ✔
825

826
         botan_x509_cert_t cert2;
1 ✔
827
         std::vector<uint8_t> cert2_serial;
1 ✔
828
         botan_mp_t cert2_serial_bn;
1 ✔
829
         size_t cert2_serial_len = 0;
1 ✔
830
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&cert2, Test::data_file("x509/nist/test20/int.crt").c_str()));
1 ✔
831
         TEST_FFI_RC(0, botan_x509_is_revoked, (crl, cert2));
1 ✔
832
         TEST_FFI_RC(-1, botan_x509_is_revoked, (bytecrl, cert2));
1 ✔
833
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1 ✔
834
                     botan_x509_cert_get_serial_number,
835
                     (cert2, nullptr, &cert2_serial_len));
836
         cert2_serial.resize(cert2_serial_len);
1 ✔
837
         TEST_FFI_OK(botan_x509_cert_get_serial_number, (cert2, cert2_serial.data(), &cert2_serial_len));
1 ✔
838
         TEST_FFI_OK(botan_x509_cert_serial_number, (cert2, &cert2_serial_bn));
1 ✔
839
         TEST_FFI_OK(botan_x509_cert_destroy, (cert2));
1 ✔
840

841
         size_t entries;
1 ✔
842
         TEST_FFI_OK(botan_x509_crl_entries_count, (crl, &entries));
1 ✔
843
         result.test_sz_eq("one revoked cert", entries, 1);
1 ✔
844
         TEST_FFI_OK(botan_x509_crl_entries_count, (bytecrl, &entries));
1 ✔
845
         result.test_sz_eq("no revoked cert", entries, 0);
1 ✔
846

847
         ViewBytesSink serial;
1 ✔
848
         TEST_FFI_OK(botan_mp_view_bin, (cert2_serial_bn, serial.delegate(), serial.callback()));
1 ✔
849
         result.test_bin_eq("serial == serial_bn", serial.get(), cert2_serial);
1 ✔
850
         TEST_FFI_OK(botan_mp_destroy, (cert2_serial_bn));
1 ✔
851

852
         botan_x509_crl_entry_t entry;
1 ✔
853
         TEST_FFI_OK(botan_x509_crl_entries, (crl, 0, &entry));
1 ✔
854

855
         uint64_t ts;
1 ✔
856
         int reason;
1 ✔
857
         botan_mp_t entry_serial_bn;
1 ✔
858

859
         TEST_FFI_OK(botan_x509_crl_entry_view_serial_number, (entry, serial.delegate(), serial.callback()));
1 ✔
860
         TEST_FFI_OK(botan_x509_crl_entry_serial_number, (entry, &entry_serial_bn));
1 ✔
861
         TEST_FFI_OK(botan_x509_crl_entry_revocation_date, (entry, &ts));
1 ✔
862
         TEST_FFI_OK(botan_x509_crl_entry_reason, (entry, &reason));
1 ✔
863
         TEST_FFI_OK(botan_x509_crl_entry_destroy, (entry));
1 ✔
864

865
   #if defined(BOTAN_HAS_X509)
866
         result.test_u8_eq(
1 ✔
867
            "Reason", static_cast<uint8_t>(reason), Botan::to_underlying(Botan::CRL_Code::KeyCompromise));
868
   #endif
869
         result.test_u64_eq("Revocation time", ts, Botan::calendar_point(1999, 1, 1, 12, 0, 0).seconds_since_epoch());
1 ✔
870
         result.test_bin_eq("Revoked cert serial", serial.get(), cert2_serial);
1 ✔
871

872
         TEST_FFI_OK(botan_mp_view_bin, (entry_serial_bn, serial.delegate(), serial.callback()));
1 ✔
873
         result.test_bin_eq("Revoked cert serial_bn", serial.get(), cert2_serial);
1 ✔
874
         TEST_FFI_OK(botan_mp_destroy, (entry_serial_bn));
1 ✔
875

876
         TEST_FFI_RC(BOTAN_FFI_ERROR_OUT_OF_RANGE, botan_x509_crl_entries, (crl, 1, &entry));
1 ✔
877
         TEST_FFI_RC(BOTAN_FFI_ERROR_OUT_OF_RANGE, botan_x509_crl_entries, (bytecrl, 0, &entry));
1 ✔
878

879
         ViewStringSink crl_pem;
1 ✔
880
         TEST_FFI_OK(botan_x509_crl_view_string_values,
1 ✔
881
                     (bytecrl, BOTAN_X509_PEM_ENCODING, 0, crl_pem.delegate(), crl_pem.callback()));
882
         size_t pem_count;
1 ✔
883
         TEST_FFI_OK(botan_x509_crl_view_string_values_count, (bytecrl, BOTAN_X509_PEM_ENCODING, &pem_count));
1 ✔
884
         result.test_sz_eq("one PEM encoding", pem_count, 1);
1 ✔
885

886
         const auto remove_newlines = [](std::string_view str) {
3 ✔
887
            auto out = std::string(str);
2 ✔
888
            std::erase(out, '\n');
2 ✔
889
            std::erase(out, '\r');
2 ✔
890
            return out;
2 ✔
891
         };
892

893
         result.test_str_eq("CRL PEM", remove_newlines(crl_pem.get()), remove_newlines(crl_string));
1 ✔
894

895
         size_t rdn_count;
1 ✔
896
         TEST_FFI_OK(botan_x509_crl_view_issuer_dn_count, (bytecrl, "Name", &rdn_count));
1 ✔
897
         result.test_sz_eq("issuer DN 'name' count", rdn_count, 1);
1 ✔
898
         TEST_FFI_OK(botan_x509_crl_view_issuer_dn_count, (bytecrl, "State", &rdn_count));
1 ✔
899
         result.test_sz_eq("issuer DN 'state' count", rdn_count, 0);
1 ✔
900

901
         ViewStringSink dn;
1 ✔
902
         TEST_FFI_RC(BOTAN_FFI_ERROR_OUT_OF_RANGE,
1 ✔
903
                     botan_x509_crl_view_issuer_dn,
904
                     (bytecrl, "Name", 1, dn.delegate(), dn.callback()));
905

906
         TEST_FFI_OK(botan_x509_crl_view_issuer_dn, (bytecrl, "Name", 0, dn.delegate(), dn.callback()));
1 ✔
907
         result.test_str_eq("issuer dn", dn.get(), "Usable cert validation: Temporary CA");
1 ✔
908

909
         TEST_FFI_OK(botan_x509_crl_destroy, (crl));
1 ✔
910
         TEST_FFI_OK(botan_x509_crl_destroy, (bytecrl));
1 ✔
911
         TEST_FFI_OK(botan_x509_crl_destroy, (crl_without_next_update));
1 ✔
912

913
         const uint64_t now =
1 ✔
914
            std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())
1 ✔
915
               .count();
1 ✔
916

917
         const char* priv_string =
1 ✔
918
            "-----BEGIN PRIVATE KEY-----\n"
919
            "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgoVEKnWZw2Bfrf3MM\n"
920
            "WLrfvRcAqq/sOf58jny37NLGQHShRANCAARageRLkKQEh1M86zvqeeesx2u9duLP\n"
921
            "iWtHjIcunpiq6+IiB8IVu7Ncu6uPKoFS/mWzTvjgdNusmgNle9p3OAbE\n"
922
            "-----END PRIVATE KEY-----";
923

924
         botan_privkey_t ca_key;
1 ✔
925
         botan_x509_cert_t ca_cert;
1 ✔
926
         botan_x509_cert_t sub1_cert;
1 ✔
927
         botan_x509_cert_t sub2_cert;
1 ✔
928

929
         REQUIRE_FFI_OK(botan_privkey_load,
1 ✔
930
                        (&ca_key, nullptr, reinterpret_cast<const uint8_t*>(priv_string), 240, nullptr));
1 ✔
931
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&ca_cert, Test::data_file("x509/crl/ca.crt").c_str()));
1 ✔
932
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&sub1_cert, Test::data_file("x509/crl/sub1.crt").c_str()));
1 ✔
933
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&sub2_cert, Test::data_file("x509/crl/sub2.crt").c_str()));
1 ✔
934

935
         botan_pubkey_t ca_pubkey;
1 ✔
936
         REQUIRE_FFI_OK(botan_privkey_export_pubkey, (&ca_pubkey, ca_key));
1 ✔
937

938
         botan_x509_crl_t empty_crl;
1 ✔
939
         TEST_FFI_OK(botan_x509_crl_create, (&empty_crl, rng, ca_cert, ca_key, now, 86400, nullptr, nullptr));
1 ✔
940

941
         int rc;
1 ✔
942
         // both validate, because the crl is empty
943
         TEST_FFI_RC(0,
1 ✔
944
                     botan_x509_cert_verify_with_crl,
945
                     (&rc, sub1_cert, nullptr, 0, &ca_cert, 1, &empty_crl, 1, nullptr, 0, nullptr, 0));
946
         TEST_FFI_RC(0,
1 ✔
947
                     botan_x509_cert_verify_with_crl,
948
                     (&rc, sub2_cert, nullptr, 0, &ca_cert, 1, &empty_crl, 1, nullptr, 0, nullptr, 0));
949

950
         botan_x509_crl_entry_t crl_entry;
1 ✔
951
         TEST_FFI_RC(BOTAN_FFI_ERROR_OUT_OF_RANGE, botan_x509_crl_entries, (empty_crl, 0, &crl_entry));
1 ✔
952
         TEST_FFI_OK(botan_x509_crl_entry_create, (&crl_entry, sub2_cert, BOTAN_CRL_ENTRY_KEY_COMPROMISE));
1 ✔
953

954
         botan_x509_crl_t new_crl;
1 ✔
955
         const botan_x509_crl_entry_t crl_entries[1] = {crl_entry};
1 ✔
956

957
         TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER,
1 ✔
958
                     botan_x509_crl_update,
959
                     (&new_crl, empty_crl, rng, ca_cert, ca_key, now, 86400, nullptr, 1, nullptr, nullptr));
960
         TEST_FFI_OK(botan_x509_crl_update,
1 ✔
961
                     (&new_crl, empty_crl, rng, ca_cert, ca_key, now, 86400, crl_entries, 1, nullptr, nullptr));
962
         // sub 1 still validates
963
         TEST_FFI_RC(0,
1 ✔
964
                     botan_x509_cert_verify_with_crl,
965
                     (&rc, sub1_cert, nullptr, 0, &ca_cert, 1, &new_crl, 1, nullptr, 0, nullptr, 0));
966
         // but sub 2 is revoked
967
         TEST_FFI_RC(1,
1 ✔
968
                     botan_x509_cert_verify_with_crl,
969
                     (&rc, sub2_cert, nullptr, 0, &ca_cert, 1, &new_crl, 1, nullptr, 0, nullptr, 0));
970

971
         botan_x509_crl_entry_t crl_entry_2;
1 ✔
972
         TEST_FFI_RC(BOTAN_FFI_ERROR_OUT_OF_RANGE, botan_x509_crl_entries, (new_crl, 1, &crl_entry_2));
1 ✔
973
         TEST_FFI_OK(botan_x509_crl_entries, (new_crl, 0, &crl_entry_2));
1 ✔
974

975
         botan_mp_t serial_from_str;
1 ✔
976
         TEST_FFI_OK(botan_mp_init, (&serial_from_str));
1 ✔
977
         TEST_FFI_OK(botan_mp_set_from_str, (serial_from_str, "270431672985589325219914342203841486494"));
1 ✔
978

979
         uint64_t expire_time;
1 ✔
980
         TEST_FFI_OK(botan_x509_crl_entry_revocation_date, (crl_entry_2, &expire_time));
1 ✔
981
         TEST_FFI_OK(botan_x509_crl_entry_reason, (crl_entry_2, &reason));
1 ✔
982

983
         botan_mp_t serial_from_crl;
1 ✔
984
         TEST_FFI_OK(botan_x509_crl_entry_serial_number, (crl_entry_2, &serial_from_crl));
1 ✔
985
         TEST_FFI_RC(1, botan_mp_equal, (serial_from_str, serial_from_crl));
1 ✔
986
         result.test_is_true("expire time is correct", now - 20 <= expire_time && expire_time <= now + 20);
1 ✔
987
         result.test_is_true("reason is correct", reason == BOTAN_CRL_ENTRY_KEY_COMPROMISE);
1 ✔
988

989
         TEST_FFI_RC(1, botan_x509_crl_verify_signature, (new_crl, ca_pubkey));
1 ✔
990

991
         botan_x509_crl_t even_newer_crl;
1 ✔
992
         TEST_FFI_OK(botan_x509_crl_update,
1 ✔
993
                     (&even_newer_crl, new_crl, rng, ca_cert, ca_key, now, 456, nullptr, 0, nullptr, nullptr));
994

995
         TEST_FFI_OK(botan_x509_crl_next_update, (even_newer_crl, &expire_time));
1 ✔
996
         result.test_is_true("expire time is correct", expire_time == now + 456);
1 ✔
997

998
         TEST_FFI_OK(botan_x509_crl_entry_destroy, (crl_entry));
1 ✔
999
         TEST_FFI_OK(botan_x509_crl_entry_destroy, (crl_entry_2));
1 ✔
1000
         TEST_FFI_OK(botan_mp_destroy, (serial_from_str));
1 ✔
1001
         TEST_FFI_OK(botan_mp_destroy, (serial_from_crl));
1 ✔
1002
         TEST_FFI_OK(botan_x509_crl_destroy, (empty_crl));
1 ✔
1003
         TEST_FFI_OK(botan_x509_crl_destroy, (new_crl));
1 ✔
1004
         TEST_FFI_OK(botan_x509_crl_destroy, (even_newer_crl));
1 ✔
1005
         TEST_FFI_OK(botan_x509_cert_destroy, (ca_cert));
1 ✔
1006
         TEST_FFI_OK(botan_x509_cert_destroy, (sub1_cert));
1 ✔
1007
         TEST_FFI_OK(botan_x509_cert_destroy, (sub2_cert));
1 ✔
1008
         TEST_FFI_OK(botan_pubkey_destroy, (ca_pubkey));
1 ✔
1009
         TEST_FFI_OK(botan_privkey_destroy, (ca_key));
1 ✔
1010
      }
1 ✔
1011
};
1012

1013
class FFI_Cert_Validation_Test final : public FFI_Test {
1 ✔
1014
   public:
1015
      std::string name() const override { return "FFI Cert Validation"; }
1 ✔
1016

1017
      bool skip_this_test() const override {
1 ✔
1018
   #if !defined(BOTAN_HAS_PKCSV15_SIGNATURE_PADDING)
1019
         return true;
1020
   #else
1021
         return false;
1 ✔
1022
   #endif
1023
      }
1024

1025
      void verify_bare_pkcs1_rsa_signature(Test::Result& result, botan_x509_cert_t ee, botan_x509_cert_t ca) {
1 ✔
1026
         ViewBytesSink tbs_data;
1 ✔
1027
         ViewBytesSink sig_scheme;
1 ✔
1028
         ViewBytesSink signature;
1 ✔
1029
         ViewBytesSink public_key;
1 ✔
1030

1031
         TEST_FFI_OK(botan_x509_cert_view_binary_values,
1 ✔
1032
                     (ee, BOTAN_X509_TBS_DATA_BITS, 0, tbs_data.delegate(), tbs_data.callback()));
1033
         TEST_FFI_OK(botan_x509_cert_view_binary_values,
1 ✔
1034
                     (ee, BOTAN_X509_SIGNATURE_SCHEME_BITS, 0, sig_scheme.delegate(), sig_scheme.callback()));
1035
         TEST_FFI_OK(botan_x509_cert_view_binary_values,
1 ✔
1036
                     (ee, BOTAN_X509_SIGNATURE_BITS, 0, signature.delegate(), signature.callback()));
1037
         TEST_FFI_OK(botan_x509_cert_view_binary_values,
1 ✔
1038
                     (ca, BOTAN_X509_PUBLIC_KEY_PKCS8_BITS, 0, public_key.delegate(), public_key.callback()));
1039

1040
         // These values exist exactly once in a certificate
1041
         TEST_FFI_RC(BOTAN_FFI_ERROR_OUT_OF_RANGE,
1 ✔
1042
                     botan_x509_cert_view_binary_values,
1043
                     (ee, BOTAN_X509_TBS_DATA_BITS, 1, tbs_data.delegate(), tbs_data.callback()));
1044
         TEST_FFI_RC(BOTAN_FFI_ERROR_OUT_OF_RANGE,
1 ✔
1045
                     botan_x509_cert_view_binary_values,
1046
                     (ee, BOTAN_X509_SIGNATURE_SCHEME_BITS, 1, sig_scheme.delegate(), sig_scheme.callback()));
1047
         TEST_FFI_RC(BOTAN_FFI_ERROR_OUT_OF_RANGE,
1 ✔
1048
                     botan_x509_cert_view_binary_values,
1049
                     (ee, BOTAN_X509_SIGNATURE_BITS, 1, signature.delegate(), signature.callback()));
1050
         TEST_FFI_RC(BOTAN_FFI_ERROR_OUT_OF_RANGE,
1 ✔
1051
                     botan_x509_cert_view_binary_values,
1052
                     (ca, BOTAN_X509_PUBLIC_KEY_PKCS8_BITS, 1, public_key.delegate(), public_key.callback()));
1053

1054
         size_t count;
1 ✔
1055
         TEST_FFI_OK(botan_x509_cert_view_binary_values_count, (ee, BOTAN_X509_TBS_DATA_BITS, &count));
1 ✔
1056
         result.test_sz_eq("TBS data count", count, 1);
1 ✔
1057
         TEST_FFI_OK(botan_x509_cert_view_binary_values_count, (ee, BOTAN_X509_SIGNATURE_SCHEME_BITS, &count));
1 ✔
1058
         result.test_sz_eq("Signature scheme count", count, 1);
1 ✔
1059
         TEST_FFI_OK(botan_x509_cert_view_binary_values_count, (ee, BOTAN_X509_SIGNATURE_BITS, &count));
1 ✔
1060
         result.test_sz_eq("Signature count", count, 1);
1 ✔
1061
         TEST_FFI_OK(botan_x509_cert_view_binary_values_count, (ca, BOTAN_X509_PUBLIC_KEY_PKCS8_BITS, &count));
1 ✔
1062
         result.test_sz_eq("Public key count", count, 1);
1 ✔
1063

1064
         // At the moment there's no way to directly instantiate a signature
1065
         // verifier object with an encoded signature algorithm scheme. Hence,
1066
         // we just check that the hard-coded expectation is fulfilled.
1067
         //
1068
         // TODO: improve this if we ever have a pk_op_verify_t constructor that
1069
         //       takes an encoded AlgorithmIdentifier.
1070
         const auto expected_sig_scheme =
1 ✔
1071
            Botan::AlgorithmIdentifier("RSA/PKCS1v15(SHA-1)", Botan::AlgorithmIdentifier::USE_NULL_PARAM).BER_encode();
1 ✔
1072
         result.test_bin_eq("AlgorithmIdentifier", sig_scheme.get(), expected_sig_scheme);
1 ✔
1073

1074
         botan_pubkey_t pubkey;
1 ✔
1075
         TEST_FFI_INIT(botan_pubkey_load, (&pubkey, public_key.data(), public_key.size()));
1 ✔
1076

1077
         botan_pk_op_verify_t verifier;
1 ✔
1078
         TEST_FFI_INIT(botan_pk_op_verify_create, (&verifier, pubkey, "PKCS1v15(SHA-1)", 0));
1 ✔
1079
         TEST_FFI_OK(botan_pk_op_verify_update, (verifier, tbs_data.data(), tbs_data.size()));
1 ✔
1080
         TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
1 ✔
1081

1082
         TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
1 ✔
1083
         TEST_FFI_OK(botan_pubkey_destroy, (pubkey));
1 ✔
1084
      }
1 ✔
1085

1086
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
1087
         botan_x509_cert_t root;
1 ✔
1088
         int rc;
1 ✔
1089

1090
         if(!TEST_FFI_INIT(botan_x509_cert_load_file, (&root, Test::data_file("x509/nist/root.crt").c_str()))) {
1 ✔
1091
            return;
×
1092
         }
1093
         TEST_FFI_RC(1, botan_x509_cert_is_ca, (root));
1 ✔
1094

1095
         botan_x509_cert_t end2;
1 ✔
1096
         botan_x509_cert_t sub2;
1 ✔
1097
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&end2, Test::data_file("x509/nist/test02/end.crt").c_str()));
1 ✔
1098
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&sub2, Test::data_file("x509/nist/test02/int.crt").c_str()));
1 ✔
1099
         TEST_FFI_RC(0, botan_x509_cert_is_ca, (end2));
1 ✔
1100

1101
         size_t path_limit;
1 ✔
1102
         TEST_FFI_RC(BOTAN_FFI_ERROR_NO_VALUE, botan_x509_cert_get_path_length_constraint, (root, &path_limit));
1 ✔
1103

1104
         botan_x509_cert_t root_with_pathlen;
1 ✔
1105
         REQUIRE_FFI_OK(botan_x509_cert_load_file,
1 ✔
1106
                        (&root_with_pathlen, Test::data_file("x509/extended/02/root.crt").c_str()));
1 ✔
1107
         TEST_FFI_OK(botan_x509_cert_get_path_length_constraint, (root_with_pathlen, &path_limit));
1 ✔
1108
         result.test_sz_eq("Path length constraint", path_limit, 1);
1 ✔
1109

1110
         TEST_FFI_RC(1, botan_x509_cert_verify, (&rc, end2, &sub2, 1, &root, 1, nullptr, 0, nullptr, 0));
1 ✔
1111
         result.test_is_true("Validation test02 failed", rc == 5002);
1 ✔
1112
         result.test_str_eq(
1 ✔
1113
            "Validation test02 status string", botan_x509_cert_validation_status(rc), "Signature error");
1114

1115
         TEST_FFI_RC(1, botan_x509_cert_verify, (&rc, end2, nullptr, 0, &root, 1, nullptr, 0, nullptr, 0));
1 ✔
1116
         result.test_is_true("Validation test02 failed (missing int)", rc == 3000);
1 ✔
1117
         result.test_str_eq(
1 ✔
1118
            "Validation test02 status string", botan_x509_cert_validation_status(rc), "Certificate issuer not found");
1119

1120
         botan_x509_cert_t end7;
1 ✔
1121
         botan_x509_cert_t sub7;
1 ✔
1122
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&end7, Test::data_file("x509/nist/test07/end.crt").c_str()));
1 ✔
1123
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&sub7, Test::data_file("x509/nist/test07/int.crt").c_str()));
1 ✔
1124

1125
         const botan_x509_cert_t subs[2] = {sub2, sub7};
1 ✔
1126
         TEST_FFI_RC(1, botan_x509_cert_verify, (&rc, end7, subs, 2, &root, 1, nullptr, 0, nullptr, 0));
1 ✔
1127
         result.test_is_true("Validation test07 failed with expected error", rc == 1001);
1 ✔
1128
         result.test_str_eq("Validation test07 status string",
1 ✔
1129
                            botan_x509_cert_validation_status(rc),
1130
                            "Hash function used is considered too weak for security");
1131

1132
         TEST_FFI_RC(0, botan_x509_cert_verify, (&rc, end7, subs, 2, &root, 1, nullptr, 80, nullptr, 0));
1 ✔
1133
         result.test_is_true("Validation test07 passed", rc == 0);
1 ✔
1134
         result.test_str_eq("Validation test07 status string", botan_x509_cert_validation_status(rc), "Verified");
1 ✔
1135

1136
         verify_bare_pkcs1_rsa_signature(result, end7, sub7);
1 ✔
1137

1138
         TEST_FFI_RC(1,
1 ✔
1139
                     botan_x509_cert_verify_with_crl,
1140
                     (&rc, end7, subs, 2, nullptr, 0, nullptr, 0, "x509/farce", 0, nullptr, 0));
1141
         result.test_is_true("Validation test07 failed with expected error", rc == 3000);
1 ✔
1142
         result.test_str_eq(
1 ✔
1143
            "Validation test07 status string", botan_x509_cert_validation_status(rc), "Certificate issuer not found");
1144

1145
         botan_x509_crl_t rootcrl;
1 ✔
1146

1147
         REQUIRE_FFI_OK(botan_x509_crl_load_file, (&rootcrl, Test::data_file("x509/nist/root.crl").c_str()));
1 ✔
1148
         TEST_FFI_RC(
1 ✔
1149
            0, botan_x509_cert_verify_with_crl, (&rc, end7, subs, 2, &root, 1, &rootcrl, 1, nullptr, 80, nullptr, 0));
1150
         result.test_is_true("Validation test07 with CRL passed", rc == 0);
1 ✔
1151
         result.test_str_eq(
1 ✔
1152
            "Validation test07 with CRL status string", botan_x509_cert_validation_status(rc), "Verified");
1153

1154
         botan_x509_cert_t end20;
1 ✔
1155
         botan_x509_cert_t sub20;
1 ✔
1156
         botan_x509_crl_t sub20crl;
1 ✔
1157
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&end20, Test::data_file("x509/nist/test20/end.crt").c_str()));
1 ✔
1158
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&sub20, Test::data_file("x509/nist/test20/int.crt").c_str()));
1 ✔
1159
         REQUIRE_FFI_OK(botan_x509_crl_load_file, (&sub20crl, Test::data_file("x509/nist/test20/int.crl").c_str()));
1 ✔
1160
         const botan_x509_crl_t crls[2] = {sub20crl, rootcrl};
1 ✔
1161
         TEST_FFI_RC(
1 ✔
1162
            1, botan_x509_cert_verify_with_crl, (&rc, end20, &sub20, 1, &root, 1, crls, 2, nullptr, 80, nullptr, 0));
1163
         result.test_is_true("Validation test20 failed with expected error", rc == 5000);
1 ✔
1164
         result.test_str_eq(
1 ✔
1165
            "Validation test20 status string", botan_x509_cert_validation_status(rc), "Certificate is revoked");
1166

1167
         TEST_FFI_OK(botan_x509_cert_destroy, (root_with_pathlen));
1 ✔
1168
         TEST_FFI_OK(botan_x509_cert_destroy, (end2));
1 ✔
1169
         TEST_FFI_OK(botan_x509_cert_destroy, (sub2));
1 ✔
1170
         TEST_FFI_OK(botan_x509_cert_destroy, (end7));
1 ✔
1171
         TEST_FFI_OK(botan_x509_cert_destroy, (sub7));
1 ✔
1172
         TEST_FFI_OK(botan_x509_cert_destroy, (end20));
1 ✔
1173
         TEST_FFI_OK(botan_x509_cert_destroy, (sub20));
1 ✔
1174
         TEST_FFI_OK(botan_x509_crl_destroy, (sub20crl));
1 ✔
1175
         TEST_FFI_OK(botan_x509_cert_destroy, (root));
1 ✔
1176
         TEST_FFI_OK(botan_x509_crl_destroy, (rootcrl));
1 ✔
1177
      }
1178
};
1179

1180
class FFI_ECDSA_Certificate_Test final : public FFI_Test {
1 ✔
1181
   public:
1182
      std::string name() const override { return "FFI ECDSA cert"; }
1 ✔
1183

1184
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
1185
         botan_x509_cert_t cert;
1 ✔
1186
         if(TEST_FFI_INIT(botan_x509_cert_load_file, (&cert, Test::data_file("x509/ecc/isrg-root-x2.pem").c_str()))) {
1 ✔
1187
            size_t date_len = 0;
1 ✔
1188
            TEST_FFI_RC(
1 ✔
1189
               BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_x509_cert_get_time_starts, (cert, nullptr, &date_len));
1190

1191
            date_len = 8;
1 ✔
1192
            TEST_FFI_RC(
1 ✔
1193
               BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_x509_cert_get_time_starts, (cert, nullptr, &date_len));
1194

1195
            std::string date(date_len - 1, '0');
1 ✔
1196
            TEST_FFI_OK(botan_x509_cert_get_time_starts, (cert, date.data(), &date_len));
1 ✔
1197
            result.test_str_eq("cert valid from", date, "200904000000Z");
1 ✔
1198

1199
            date_len = 0;
1 ✔
1200
            TEST_FFI_RC(
1 ✔
1201
               BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_x509_cert_get_time_expires, (cert, nullptr, &date_len));
1202

1203
            date.resize(date_len - 1);
1 ✔
1204
            TEST_FFI_OK(botan_x509_cert_get_time_expires, (cert, date.data(), &date_len));
1 ✔
1205
            result.test_str_eq("cert valid until", date, "400917160000Z");
1 ✔
1206

1207
            uint64_t not_before = 0;
1 ✔
1208
            TEST_FFI_OK(botan_x509_cert_not_before, (cert, &not_before));
1 ✔
1209
            result.test_is_true("cert not before", not_before == 1599177600);
1 ✔
1210

1211
            uint64_t not_after = 0;
1 ✔
1212
            TEST_FFI_OK(botan_x509_cert_not_after, (cert, &not_after));
1 ✔
1213
            result.test_is_true("cert not after", not_after == 2231510400);
1 ✔
1214

1215
            size_t serial_len = 0;
1 ✔
1216
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1 ✔
1217
                        botan_x509_cert_get_serial_number,
1218
                        (cert, nullptr, &serial_len));
1219

1220
            std::vector<uint8_t> serial(serial_len);
1 ✔
1221
            TEST_FFI_OK(botan_x509_cert_get_serial_number, (cert, serial.data(), &serial_len));
1 ✔
1222
            result.test_sz_eq("cert serial length", serial.size(), 16);
1 ✔
1223
            result.test_bin_eq("cert serial", serial, "41D29DD172EAEEA780C12C6CE92F8752");
1 ✔
1224

1225
            ViewBytesSink serial_sink;
1 ✔
1226
            TEST_FFI_OK(botan_x509_cert_view_binary_values,
1 ✔
1227
                        (cert, BOTAN_X509_SERIAL_NUMBER, 0, serial_sink.delegate(), serial_sink.callback()));
1228
            result.test_bin_eq("cert serial (2)", serial_sink.get(), "41D29DD172EAEEA780C12C6CE92F8752");
1 ✔
1229

1230
            size_t fingerprint_len = 0;
1 ✔
1231
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1 ✔
1232
                        botan_x509_cert_get_fingerprint,
1233
                        (cert, "SHA-256", nullptr, &fingerprint_len));
1234

1235
            std::vector<uint8_t> fingerprint(fingerprint_len);
1 ✔
1236
            TEST_FFI_OK(botan_x509_cert_get_fingerprint, (cert, "SHA-256", fingerprint.data(), &fingerprint_len));
1 ✔
1237
            result.test_str_eq(
1 ✔
1238
               "cert fingerprint",
1239
               reinterpret_cast<const char*>(fingerprint.data()),
1 ✔
1240
               "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");
1241

1242
            size_t key_id_len = 0;
1 ✔
1243
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1 ✔
1244
                        botan_x509_cert_get_authority_key_id,
1245
                        (cert, nullptr, &key_id_len));
1246

1247
            result.test_sz_eq("No AKID", key_id_len, 0);
1 ✔
1248

1249
            // "No AKID" is explicitly communicated with an error code
1250
            ViewBytesSink key_id_sink;
1 ✔
1251
            TEST_FFI_RC(BOTAN_FFI_ERROR_NO_VALUE,
1 ✔
1252
                        botan_x509_cert_view_binary_values,
1253
                        (cert, BOTAN_X509_AUTHORITY_KEY_IDENTIFIER, 0, key_id_sink.delegate(), key_id_sink.callback()));
1254

1255
            key_id_len = 0;
1 ✔
1256
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1 ✔
1257
                        botan_x509_cert_get_subject_key_id,
1258
                        (cert, nullptr, &key_id_len));
1259

1260
            std::vector<uint8_t> key_id(key_id_len);
1 ✔
1261
            TEST_FFI_OK(botan_x509_cert_get_subject_key_id, (cert, key_id.data(), &key_id_len));
1 ✔
1262
            result.test_str_eq("cert subject key id",
1 ✔
1263
                               Botan::hex_encode(key_id.data(), key_id.size(), true),
1 ✔
1264
                               "7C4296AEDE4B483BFA92F89E8CCF6D8BA9723795");
1265

1266
            TEST_FFI_OK(botan_x509_cert_view_binary_values,
1 ✔
1267
                        (cert, BOTAN_X509_SUBJECT_KEY_IDENTIFIER, 0, key_id_sink.delegate(), key_id_sink.callback()));
1268
            result.test_bin_eq("cert subject key id", key_id_sink.get(), "7C4296AEDE4B483BFA92F89E8CCF6D8BA9723795");
1 ✔
1269

1270
            size_t pubkey_len = 0;
1 ✔
1271
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1 ✔
1272
                        botan_x509_cert_get_public_key_bits,
1273
                        (cert, nullptr, &pubkey_len));
1274

1275
            std::vector<uint8_t> pubkey(pubkey_len);
1 ✔
1276
            TEST_FFI_OK(botan_x509_cert_get_public_key_bits, (cert, pubkey.data(), &pubkey_len));
1 ✔
1277

1278
   #if defined(BOTAN_HAS_ECDSA)
1279
            botan_pubkey_t pub;
1 ✔
1280
            if(TEST_FFI_OK(botan_x509_cert_get_public_key, (cert, &pub))) {
1 ✔
1281
               TEST_FFI_RC(0, botan_pubkey_ecc_key_used_explicit_encoding, (pub));
1 ✔
1282
               TEST_FFI_OK(botan_pubkey_destroy, (pub));
1 ✔
1283
            }
1284
   #endif
1285

1286
            size_t rdn_count;
1 ✔
1287
            TEST_FFI_OK(botan_x509_cert_get_issuer_dn_count, (cert, "Name", &rdn_count));
1 ✔
1288
            result.test_sz_eq("issuer DN 'name' count", rdn_count, 1);
1 ✔
1289
            TEST_FFI_OK(botan_x509_cert_get_issuer_dn_count, (cert, "Organizational Unit", &rdn_count));
1 ✔
1290
            result.test_sz_eq("issuer DN 'organizational unit' count", rdn_count, 0);
1 ✔
1291

1292
            size_t dn_len = 0;
1 ✔
1293
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1 ✔
1294
                        botan_x509_cert_get_issuer_dn,
1295
                        (cert, "Name", 0, nullptr, &dn_len));
1296

1297
            std::vector<uint8_t> dn(dn_len);
1 ✔
1298
            TEST_FFI_OK(botan_x509_cert_get_issuer_dn, (cert, "Name", 0, dn.data(), &dn_len));
1 ✔
1299
            result.test_str_eq("issuer dn", reinterpret_cast<const char*>(dn.data()), "ISRG Root X2");
1 ✔
1300

1301
            TEST_FFI_OK(botan_x509_cert_get_subject_dn_count, (cert, "Name", &rdn_count));
1 ✔
1302
            result.test_sz_eq("subject DN 'name' count", rdn_count, 1);
1 ✔
1303
            TEST_FFI_OK(botan_x509_cert_get_subject_dn_count, (cert, "Organizational Unit", &rdn_count));
1 ✔
1304
            result.test_sz_eq("subject DN 'organizational unit' count", rdn_count, 0);
1 ✔
1305

1306
            dn_len = 0;
1 ✔
1307
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1 ✔
1308
                        botan_x509_cert_get_subject_dn,
1309
                        (cert, "Name", 0, nullptr, &dn_len));
1310

1311
            dn.resize(dn_len);
1 ✔
1312
            TEST_FFI_OK(botan_x509_cert_get_subject_dn, (cert, "Name", 0, dn.data(), &dn_len));
1 ✔
1313
            result.test_str_eq("subject dn", reinterpret_cast<const char*>(dn.data()), "ISRG Root X2");
1 ✔
1314

1315
            size_t printable_len = 0;
1 ✔
1316
            TEST_FFI_RC(
1 ✔
1317
               BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_x509_cert_to_string, (cert, nullptr, &printable_len));
1318

1319
            std::string printable(printable_len - 1, '0');
1 ✔
1320
            TEST_FFI_OK(botan_x509_cert_to_string, (cert, printable.data(), &printable_len));
1 ✔
1321

1322
            size_t count;
1 ✔
1323
            TEST_FFI_OK(botan_x509_cert_view_string_values_count, (cert, BOTAN_X509_PEM_ENCODING, &count));
1 ✔
1324
            result.test_sz_eq("one PEM encoding", count, 1);
1 ✔
1325
            TEST_FFI_OK(botan_x509_cert_view_binary_values_count, (cert, BOTAN_X509_DER_ENCODING, &count));
1 ✔
1326
            result.test_sz_eq("one DER encoding", count, 1);
1 ✔
1327

1328
            ViewBytesSink der;
1 ✔
1329
            ViewStringSink pem;
1 ✔
1330
            TEST_FFI_OK(botan_x509_cert_view_binary_values,
1 ✔
1331
                        (cert, BOTAN_X509_DER_ENCODING, 0, der.delegate(), der.callback()));
1332
            result.test_is_true("DER encoding produced something", !der.get().empty());
1 ✔
1333
            TEST_FFI_OK(botan_x509_cert_view_string_values,
1 ✔
1334
                        (cert, BOTAN_X509_PEM_ENCODING, 0, pem.delegate(), pem.callback()));
1335
            result.test_is_true("PEM encoding produced something", !pem.get().empty());
1 ✔
1336

1337
            TEST_FFI_RC(0, botan_x509_cert_allowed_usage, (cert, KEY_CERT_SIGN));
1 ✔
1338
            TEST_FFI_RC(0, botan_x509_cert_allowed_usage, (cert, CRL_SIGN));
1 ✔
1339
            TEST_FFI_RC(1, botan_x509_cert_allowed_usage, (cert, DIGITAL_SIGNATURE));
1 ✔
1340

1341
            TEST_FFI_OK(botan_x509_cert_destroy, (cert));
1 ✔
1342
         }
1 ✔
1343
      }
1 ✔
1344
};
1345

1346
class FFI_Cert_ExtKeyUsages_Test final : public FFI_Test {
1 ✔
1347
   public:
1348
      std::string name() const override { return "FFI X509 Extended Key Usage"; }
1 ✔
1349

1350
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
1351
         botan_x509_cert_t cert_with_eku;
1 ✔
1352
         if(!TEST_FFI_INIT(botan_x509_cert_load_file,
1 ✔
1353
                           (&cert_with_eku, Test::data_file("x509/pss_certs/03/end.crt").c_str()))) {
1354
            return;
×
1355
         }
1356

1357
         // Prepare some OID objects for OID-based EKU queries
1358
         botan_asn1_oid_t oid_srv_auth1;
1 ✔
1359
         botan_asn1_oid_t oid_srv_auth2;
1 ✔
1360
         botan_asn1_oid_t oid_ocsp_signing;
1 ✔
1361
         TEST_FFI_OK(botan_oid_from_string, (&oid_srv_auth1, "1.3.6.1.5.5.7.3.1"));
1 ✔
1362
         TEST_FFI_OK(botan_oid_from_string, (&oid_srv_auth2, "PKIX.ServerAuth"));
1 ✔
1363
         TEST_FFI_OK(botan_oid_from_string, (&oid_ocsp_signing, "PKIX.OCSPSigning"));
1 ✔
1364

1365
         // Make sure the OID object is checked for nullptr
1366
         TEST_FFI_RC(
1 ✔
1367
            BOTAN_FFI_ERROR_NULL_POINTER, botan_x509_cert_allowed_extended_usage_oid, (cert_with_eku, nullptr));
1368

1369
         // Should have serverAuth (TLS Web Server Authentication)
1370
         TEST_FFI_RC(1, botan_x509_cert_allowed_extended_usage_str, (cert_with_eku, "1.3.6.1.5.5.7.3.1"));
1 ✔
1371
         TEST_FFI_RC(1, botan_x509_cert_allowed_extended_usage_str, (cert_with_eku, "PKIX.ServerAuth"));
1 ✔
1372
         TEST_FFI_RC(1, botan_x509_cert_allowed_extended_usage_oid, (cert_with_eku, oid_srv_auth1));
1 ✔
1373
         TEST_FFI_RC(1, botan_x509_cert_allowed_extended_usage_oid, (cert_with_eku, oid_srv_auth2));
1 ✔
1374

1375
         // Should have clientAuth (TLS Web Client Authentication)
1376
         TEST_FFI_RC(1, botan_x509_cert_allowed_extended_usage_str, (cert_with_eku, "1.3.6.1.5.5.7.3.2"));
1 ✔
1377
         TEST_FFI_RC(1, botan_x509_cert_allowed_extended_usage_str, (cert_with_eku, "PKIX.ClientAuth"));
1 ✔
1378

1379
         // Should NOT have OCSPSigning
1380
         TEST_FFI_RC(0, botan_x509_cert_allowed_extended_usage_str, (cert_with_eku, "1.3.6.1.5.5.7.3.9"));
1 ✔
1381
         TEST_FFI_RC(0, botan_x509_cert_allowed_extended_usage_str, (cert_with_eku, "PKIX.OCSPSigning"));
1 ✔
1382

1383
         // Should NOT have codeSigning
1384
         TEST_FFI_RC(0, botan_x509_cert_allowed_extended_usage_str, (cert_with_eku, "1.3.6.1.5.5.7.3.3"));
1 ✔
1385
         TEST_FFI_RC(0, botan_x509_cert_allowed_extended_usage_str, (cert_with_eku, "PKIX.CodeSigning"));
1 ✔
1386
         TEST_FFI_RC(0, botan_x509_cert_allowed_extended_usage_oid, (cert_with_eku, oid_ocsp_signing));
1 ✔
1387

1388
         TEST_FFI_OK(botan_x509_cert_destroy, (cert_with_eku));
1 ✔
1389

1390
         botan_x509_cert_t cert_without_eku;
1 ✔
1391
         if(!TEST_FFI_INIT(botan_x509_cert_load_file,
1 ✔
1392
                           (&cert_without_eku, Test::data_file("x509/nist/root.crt").c_str()))) {
1393
            return;
1394
         }
1395

1396
         // Should return zero for any EKU query (no EKU extension present)
1397
         TEST_FFI_RC(0, botan_x509_cert_allowed_extended_usage_str, (cert_without_eku, "1.3.6.1.5.5.7.3.1"));
1 ✔
1398
         TEST_FFI_RC(0, botan_x509_cert_allowed_extended_usage_str, (cert_without_eku, "1.3.6.1.5.5.7.3.2"));
1 ✔
1399
         TEST_FFI_RC(0, botan_x509_cert_allowed_extended_usage_str, (cert_without_eku, "PKIX.OCSPSigning"));
1 ✔
1400
         TEST_FFI_RC(0, botan_x509_cert_allowed_extended_usage_str, (cert_without_eku, "PKIX.CodeSigning"));
1 ✔
1401

1402
         TEST_FFI_OK(botan_oid_destroy, (oid_srv_auth1));
1 ✔
1403
         TEST_FFI_OK(botan_oid_destroy, (oid_srv_auth2));
1 ✔
1404
         TEST_FFI_OK(botan_oid_destroy, (oid_ocsp_signing));
1 ✔
1405
         TEST_FFI_OK(botan_x509_cert_destroy, (cert_without_eku));
1 ✔
1406
      }
1407
};
1408

1409
   #if defined(BOTAN_HAS_X509)
1410

1411
auto read_distinguished_name(std::span<const uint8_t> bytes) {
32 ✔
1412
   auto dec = Botan::BER_Decoder(bytes, Botan::BER_Decoder::Limits::DER());
32 ✔
1413
   Botan::X509_DN dn;
32 ✔
1414
   dn.decode_from(dec);
32 ✔
1415
   return dn;
32 ✔
1416
}
32 ✔
1417

1418
class FFI_Cert_AlternativeNames_Test final : public FFI_Test {
1 ✔
1419
   private:
1420
      template <std::invocable<botan_x509_cert_t, size_t, botan_x509_general_name_t*> EnumeratorT,
1421
                std::invocable<botan_x509_cert_t, size_t*> CountFnT,
1422
                std::invocable<botan_x509_general_name_t> VisitorT>
1423
      static void visit_general_names(Test::Result& result,
12 ✔
1424
                                      botan_x509_cert_t cert,
1425
                                      EnumeratorT enumerator_fn,
1426
                                      CountFnT count_fn,
1427
                                      VisitorT visitor_fn) {
1428
         int rc = BOTAN_FFI_SUCCESS;
12 ✔
1429
         for(size_t i = 0; rc == BOTAN_FFI_SUCCESS; ++i) {
144 ✔
1430
            botan_x509_general_name_t gn;
1431
            rc = enumerator_fn(cert, i, &gn);
132 ✔
1432
            if(rc == BOTAN_FFI_SUCCESS) {
132 ✔
1433
               visitor_fn(gn);
120 ✔
1434
               TEST_FFI_OK(botan_x509_general_name_destroy, (gn));
120 ✔
1435
            } else if(rc == BOTAN_FFI_ERROR_OUT_OF_RANGE) {
12 ✔
1436
               // Now check we are at the expected index
1437
               size_t count;
1438
               TEST_FFI_OK(count_fn, (cert, &count));
12 ✔
1439
               result.test_sz_eq("enumerator reached end at expected index", i, count);
12 ✔
1440
            } else {
1441
               result.test_note(
×
1442
                  Botan::fmt("enumerator produced unexpected return code: {}", botan_error_description(rc)));
×
1443
            }
1444
         }
1445
      }
12 ✔
1446

1447
      template <typename EnumeratorT, typename CountFnT>
1448
      static auto read_string_alternative_names(Test::Result& result,
8 ✔
1449
                                                botan_x509_cert_t cert,
1450
                                                EnumeratorT enumerator_fn,
1451
                                                CountFnT count_fn,
1452
                                                botan_x509_general_name_types type) {
1453
         std::vector<std::string> out;
8 ✔
1454

1455
         visit_general_names(result, cert, enumerator_fn, count_fn, [&](botan_x509_general_name_t gn) {
88 ✔
1456
            unsigned int gn_type;
1457
            TEST_FFI_OK(botan_x509_general_name_get_type, (gn, &gn_type));
80 ✔
1458
            if(static_cast<botan_x509_general_name_types>(gn_type) == type) {
80 ✔
1459
               ViewStringSink str;
14 ✔
1460
               TEST_FFI_OK(botan_x509_general_name_view_string_value, (gn, str.delegate(), str.callback()));
14 ✔
1461
               out.push_back(str.get());
14 ✔
1462
            }
14 ✔
1463
         });
1464

1465
         return out;
8 ✔
1466
      }
×
1467

1468
      template <typename EnumeratorT, typename CountFnT>
1469
      static auto read_binary_alternative_names(Test::Result& result,
4 ✔
1470
                                                botan_x509_cert_t cert,
1471
                                                EnumeratorT enumerator_fn,
1472
                                                CountFnT count_fn,
1473
                                                botan_x509_general_name_types type) {
1474
         std::vector<std::vector<uint8_t>> out;
4 ✔
1475

1476
         visit_general_names(result, cert, enumerator_fn, count_fn, [&](botan_x509_general_name_t gn) {
44 ✔
1477
            unsigned int gn_type;
1478
            TEST_FFI_OK(botan_x509_general_name_get_type, (gn, &gn_type));
40 ✔
1479
            if(static_cast<botan_x509_general_name_types>(gn_type) == type) {
40 ✔
1480
               ViewBytesSink data;
8 ✔
1481
               TEST_FFI_OK(botan_x509_general_name_view_binary_value, (gn, data.delegate(), data.callback()));
8 ✔
1482
               out.emplace_back(data.get().begin(), data.get().end());
8 ✔
1483
            }
8 ✔
1484
         });
1485

1486
         return out;
4 ✔
1487
      }
×
1488

1489
      static auto read_common_names(std::span<const std::vector<uint8_t>> bytes) {
2 ✔
1490
         std::vector<std::string> result;
2 ✔
1491
         for(const auto& dn_bytes : bytes) {
8 ✔
1492
            const auto dn = read_distinguished_name(dn_bytes);
6 ✔
1493
            result.push_back(dn.get_first_attribute("X520.CommonName"));
12 ✔
1494
         }
6 ✔
1495
         return result;
2 ✔
1496
      }
×
1497

1498
   public:
1499
      std::string name() const override { return "FFI X509 Alternative Names"; }
1 ✔
1500

1501
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
1502
         botan_x509_cert_t cert_none;
1 ✔
1503
         if(!TEST_FFI_INIT(botan_x509_cert_load_file,
1 ✔
1504
                           (&cert_none, Test::data_file("x509/misc/no_alternative_names.pem").c_str()))) {
1505
            return;
×
1506
         }
1507

1508
         botan_x509_general_name_t nil = nullptr;
1 ✔
1509
         TEST_FFI_RC(BOTAN_FFI_ERROR_NO_VALUE, botan_x509_cert_subject_alternative_names, (cert_none, 0, &nil));
1 ✔
1510
         TEST_FFI_RC(BOTAN_FFI_ERROR_NO_VALUE, botan_x509_cert_issuer_alternative_names, (cert_none, 0, &nil));
1 ✔
1511
         result.test_is_true("no general name created", nil == nullptr);
1 ✔
1512

1513
         botan_x509_cert_t cert;
1 ✔
1514
         if(!TEST_FFI_INIT(botan_x509_cert_load_file,
1 ✔
1515
                           (&cert, Test::data_file("x509/misc/multiple_alternative_names.pem").c_str()))) {
1516
            return;
1517
         }
1518

1519
         const auto get_san = botan_x509_cert_subject_alternative_names;
1 ✔
1520
         const auto count_san = botan_x509_cert_subject_alternative_names_count;
1 ✔
1521

1522
         const auto san_email =
1 ✔
1523
            read_string_alternative_names(result, cert, get_san, count_san, BOTAN_X509_EMAIL_ADDRESS);
1 ✔
1524
         result.test_sz_eq("expected number of emails in SAN", san_email.size(), 2);
1 ✔
1525
         result.test_is_true("testing@x509-labs.com", Botan::value_exists(san_email, "testing@x509-labs.com"));
2 ✔
1526
         result.test_is_true("info@x509-labs.com", Botan::value_exists(san_email, "info@x509-labs.com"));
2 ✔
1527

1528
         const auto san_dns = read_string_alternative_names(result, cert, get_san, count_san, BOTAN_X509_DNS_NAME);
1 ✔
1529
         result.test_sz_eq("expected number of hostnames in SAN", san_dns.size(), 3);
1 ✔
1530
         result.test_is_true("test.x509-labs.com", Botan::value_exists(san_dns, "test.x509-labs.com"));
2 ✔
1531
         result.test_is_true("versuch.x509-labs.com", Botan::value_exists(san_dns, "versuch.x509-labs.com"));
2 ✔
1532
         result.test_is_true("trail.x509-labs.com", Botan::value_exists(san_dns, "trail.x509-labs.com"));
2 ✔
1533

1534
         const auto san_uri = read_string_alternative_names(result, cert, get_san, count_san, BOTAN_X509_URI);
1 ✔
1535
         result.test_sz_eq("expected number of URIs in SAN", san_uri.size(), 2);
1 ✔
1536
         result.test_is_true("https://x509-labs.com", Botan::value_exists(san_uri, "https://x509-labs.com"));
2 ✔
1537
         result.test_is_true("http://x509-labs.com", Botan::value_exists(san_uri, "http://x509-labs.com"));
2 ✔
1538

1539
         const auto san_ip4 = read_string_alternative_names(result, cert, get_san, count_san, BOTAN_X509_IP_ADDRESS);
1 ✔
1540
         result.test_sz_eq("expected number of IPv4 addresses", san_ip4.size(), 1);
1 ✔
1541
         result.test_is_true("127.0.0.1", Botan::value_exists(san_ip4, "127.0.0.1"));
2 ✔
1542
         const auto san_ip4_bin =
1 ✔
1543
            read_binary_alternative_names(result, cert, get_san, count_san, BOTAN_X509_IP_ADDRESS);
1 ✔
1544
         result.test_sz_eq("expected number of IPv4 addresses (bin)", san_ip4_bin.size(), 1);
1 ✔
1545
         result.test_bin_eq("127.0.0.1 (bin)", san_ip4_bin.front(), Botan::store_be(uint32_t(0x7F000001)));
1 ✔
1546

1547
         const auto san_dn_bytes =
1 ✔
1548
            read_binary_alternative_names(result, cert, get_san, count_san, BOTAN_X509_DIRECTORY_NAME);
1 ✔
1549
         result.test_sz_eq("expected number of DNs in SAN", san_dn_bytes.size(), 3);
1 ✔
1550
         const auto san_dn_cns = read_common_names(san_dn_bytes);
1 ✔
1551
         result.test_is_true("First Name", Botan::value_exists(san_dn_cns, "First Name"));
2 ✔
1552
         result.test_is_true("Middle Name", Botan::value_exists(san_dn_cns, "Middle Name"));
2 ✔
1553
         result.test_is_true("Last Name", Botan::value_exists(san_dn_cns, "Last Name"));
2 ✔
1554

1555
         auto get_ian = botan_x509_cert_issuer_alternative_names;
1 ✔
1556
         auto count_ian = botan_x509_cert_issuer_alternative_names_count;
1 ✔
1557

1558
         const auto ian_email =
1 ✔
1559
            read_string_alternative_names(result, cert, get_ian, count_ian, BOTAN_X509_EMAIL_ADDRESS);
1 ✔
1560
         result.test_sz_eq("expected number of emails in IAN", ian_email.size(), 0);
1 ✔
1561

1562
         const auto ian_dns = read_string_alternative_names(result, cert, get_ian, count_ian, BOTAN_X509_DNS_NAME);
1 ✔
1563
         result.test_sz_eq("expected number of hostnames in IAN", ian_dns.size(), 3);
1 ✔
1564
         result.test_is_true("test.x509-labs-ca.com", Botan::value_exists(ian_dns, "test.x509-labs-ca.com"));
2 ✔
1565
         result.test_is_true("versuch.x509-labs-ca.com", Botan::value_exists(ian_dns, "versuch.x509-labs-ca.com"));
2 ✔
1566
         result.test_is_true("trail.x509-labs-ca.com", Botan::value_exists(ian_dns, "trail.x509-labs-ca.com"));
2 ✔
1567

1568
         const auto ian_uri = read_string_alternative_names(result, cert, get_ian, count_ian, BOTAN_X509_URI);
1 ✔
1569
         result.test_sz_eq("expected number of URIs in IAN", ian_uri.size(), 2);
1 ✔
1570
         result.test_is_true("https://x509-labs-ca.com", Botan::value_exists(ian_uri, "https://x509-labs-ca.com"));
2 ✔
1571
         result.test_is_true("http://x509-labs-ca.com", Botan::value_exists(ian_uri, "http://x509-labs-ca.com"));
2 ✔
1572

1573
         const auto ian_ip4 = read_string_alternative_names(result, cert, get_ian, count_ian, BOTAN_X509_IP_ADDRESS);
1 ✔
1574
         result.test_sz_eq("expected number of IPv4 addresses", ian_ip4.size(), 1);
1 ✔
1575
         result.test_is_true("192.168.1.1", Botan::value_exists(ian_ip4, "192.168.1.1"));
2 ✔
1576
         const auto ian_ip4_bin =
1 ✔
1577
            read_binary_alternative_names(result, cert, get_ian, count_ian, BOTAN_X509_IP_ADDRESS);
1 ✔
1578
         result.test_sz_eq("expected number of IPv4 addresses (bin)", ian_ip4_bin.size(), 1);
1 ✔
1579
         result.test_bin_eq("192.168.1.1 (bin)", ian_ip4_bin.front(), Botan::store_be(uint32_t(0xC0A80101)));
1 ✔
1580

1581
         const auto ian_dn_bytes =
1 ✔
1582
            read_binary_alternative_names(result, cert, get_ian, count_ian, BOTAN_X509_DIRECTORY_NAME);
1 ✔
1583
         result.test_sz_eq("expected number of DNs in IAN", ian_dn_bytes.size(), 3);
1 ✔
1584
         const auto ian_dn_cns = read_common_names(ian_dn_bytes);
1 ✔
1585
         result.test_is_true("First CA", Botan::value_exists(ian_dn_cns, "First CA"));
2 ✔
1586
         result.test_is_true("Middle CA", Botan::value_exists(ian_dn_cns, "Middle CA"));
2 ✔
1587
         result.test_is_true("Last CA", Botan::value_exists(ian_dn_cns, "Last CA"));
2 ✔
1588

1589
         TEST_FFI_OK(botan_x509_cert_destroy, (cert));
1 ✔
1590
         TEST_FFI_OK(botan_x509_cert_destroy, (cert_none));
1 ✔
1591
      }
1 ✔
1592
};
1593

1594
class FFI_Cert_NameConstraints_Test final : public FFI_Test {
1 ✔
1595
   private:
1596
      static auto read_constraints(Test::Result& result, botan_x509_cert_t cert, bool permitted) {
2 ✔
1597
         std::vector<std::pair<botan_x509_general_name_types, std::string>> out;
2 ✔
1598

1599
         int rc = BOTAN_FFI_SUCCESS;
2 ✔
1600
         for(size_t i = 0; rc == BOTAN_FFI_SUCCESS; ++i) {
78 ✔
1601
            botan_x509_general_name_t constraint;
76 ✔
1602
            if(permitted) {
76 ✔
1603
               rc = botan_x509_cert_permitted_name_constraints(cert, i, &constraint);
73 ✔
1604
            } else {
1605
               rc = botan_x509_cert_excluded_name_constraints(cert, i, &constraint);
3 ✔
1606
            }
1607

1608
            if(rc == BOTAN_FFI_SUCCESS) {
76 ✔
1609
               ViewBytesSink bytes;
74 ✔
1610
               ViewStringSink string;
74 ✔
1611

1612
               unsigned int type;
74 ✔
1613
               const auto rc2 = botan_x509_general_name_get_type(constraint, &type);
74 ✔
1614
               if(rc2 == BOTAN_FFI_SUCCESS) {
74 ✔
1615
                  const auto gn_type = static_cast<botan_x509_general_name_types>(type);
74 ✔
1616
                  switch(gn_type) {
74 ✔
1617
                     case BOTAN_X509_EMAIL_ADDRESS:
49 ✔
1618
                     case BOTAN_X509_DNS_NAME:
49 ✔
1619
                     case BOTAN_X509_URI:
49 ✔
1620
                     case BOTAN_X509_IP_ADDRESS:
49 ✔
1621
                        TEST_FFI_OK(botan_x509_general_name_view_string_value,
49 ✔
1622
                                    (constraint, string.delegate(), string.callback()));
1623
                        out.emplace_back(gn_type, string.get());
49 ✔
1624
                        break;
1625
                     case BOTAN_X509_DIRECTORY_NAME:
25 ✔
1626
                        TEST_FFI_OK(botan_x509_general_name_view_binary_value,
25 ✔
1627
                                    (constraint, bytes.delegate(), bytes.callback()));
1628
                        out.emplace_back(gn_type, read_distinguished_name(bytes.get()).to_string());
50 ✔
1629
                        break;
25 ✔
1630
                     case BOTAN_X509_OTHER_NAME:
×
1631
                        out.emplace_back(gn_type, "<not supported>");
×
1632
                        break;
1633
                  }
1634
               } else {
1635
                  result.test_note(
×
1636
                     Botan::fmt("botan_x509_general_name_get_type returned {}", botan_error_description(rc2)));
×
1637
               }
1638

1639
               TEST_FFI_OK(botan_x509_general_name_destroy, (constraint));
74 ✔
1640
            } else if(rc == BOTAN_FFI_ERROR_OUT_OF_RANGE) {
76 ✔
1641
               // Now check that we are at the expected index
1642
               size_t count;
2 ✔
1643
               if(permitted) {
2 ✔
1644
                  TEST_FFI_OK(botan_x509_cert_permitted_name_constraints_count, (cert, &count));
1 ✔
1645
               } else {
1646
                  TEST_FFI_OK(botan_x509_cert_excluded_name_constraints_count, (cert, &count));
1 ✔
1647
               }
1648
               result.test_sz_eq("expected length of name constraint list", i, count);
2 ✔
1649
            } else {
1650
               result.test_failure(Botan::fmt("unexpected error code: {}", botan_error_description(rc)));
×
1651
            }
1652
         }
1653

1654
         return out;
2 ✔
1655
      }
×
1656

1657
   public:
1658
      std::string name() const override { return "FFI X509 Name Constraints"; }
1 ✔
1659

1660
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
1661
         botan_x509_cert_t cert;
1 ✔
1662
         if(!TEST_FFI_INIT(botan_x509_cert_load_file,
1 ✔
1663
                           (&cert, Test::data_file("x509/misc/name_constraint_ci/int.pem").c_str()))) {
1664
            return;
×
1665
         }
1666

1667
         const auto permitted = read_constraints(result, cert, true);
1 ✔
1668
         const auto excluded = read_constraints(result, cert, false);
1 ✔
1669

1670
         result.test_sz_eq("permissions", permitted.size(), 72);
1 ✔
1671
         result.test_sz_eq("exclusions", excluded.size(), 2);
1 ✔
1672

1673
         using V = decltype(permitted)::value_type;
1 ✔
1674
         result.test_is_true("email", Botan::value_exists(permitted, V{BOTAN_X509_EMAIL_ADDRESS, "pec.aruba.it"}));
1 ✔
1675
         result.test_is_true("DNS", Botan::value_exists(permitted, V{BOTAN_X509_DNS_NAME, "gov.it"}));
1 ✔
1676
         result.test_is_true(
1 ✔
1677
            "DN",
1678
            Botan::value_exists(
1 ✔
1679
               permitted,
1680
               V{BOTAN_X509_DIRECTORY_NAME, R"(C="IT",X520.State="Roma",X520.Locality="Roma",O="Sogei S.p.A.")"}));
1 ✔
1681
         result.test_is_true("IPv4", Botan::value_exists(excluded, V{BOTAN_X509_IP_ADDRESS, "0.0.0.0/0"}));
1 ✔
1682
         result.test_is_true("IPv6", Botan::value_exists(excluded, V{BOTAN_X509_IP_ADDRESS, "::/0"}));
1 ✔
1683

1684
         // below are more generic general_name_t tests
1685

1686
         botan_x509_general_name_t email;
1 ✔
1687
         botan_x509_general_name_t dns;
1 ✔
1688
         botan_x509_general_name_t dn;
1 ✔
1689
         botan_x509_general_name_t ip;
1 ✔
1690
         TEST_FFI_OK(botan_x509_cert_permitted_name_constraints, (cert, 0, &email));
1 ✔
1691
         TEST_FFI_OK(botan_x509_cert_permitted_name_constraints, (cert, 33, &dns));
1 ✔
1692
         TEST_FFI_OK(botan_x509_cert_permitted_name_constraints, (cert, 47, &dn));
1 ✔
1693
         TEST_FFI_OK(botan_x509_cert_excluded_name_constraints, (cert, 0, &ip));
1 ✔
1694

1695
         unsigned int type;
1 ✔
1696
         TEST_FFI_OK(botan_x509_general_name_get_type, (email, &type));
1 ✔
1697
         result.test_enum_eq("email", static_cast<botan_x509_general_name_types>(type), BOTAN_X509_EMAIL_ADDRESS);
1 ✔
1698
         TEST_FFI_OK(botan_x509_general_name_get_type, (dns, &type));
1 ✔
1699
         result.test_enum_eq("dns", static_cast<botan_x509_general_name_types>(type), BOTAN_X509_DNS_NAME);
1 ✔
1700
         TEST_FFI_OK(botan_x509_general_name_get_type, (dn, &type));
1 ✔
1701
         result.test_enum_eq("dn", static_cast<botan_x509_general_name_types>(type), BOTAN_X509_DIRECTORY_NAME);
1 ✔
1702
         TEST_FFI_OK(botan_x509_general_name_get_type, (ip, &type));
1 ✔
1703
         result.test_enum_eq("ip", static_cast<botan_x509_general_name_types>(type), BOTAN_X509_IP_ADDRESS);
1 ✔
1704

1705
         ViewBytesSink bin;
1 ✔
1706
         ViewStringSink str;
1 ✔
1707

1708
         TEST_FFI_OK(botan_x509_general_name_view_string_value, (email, str.delegate(), str.callback()));
1 ✔
1709
         result.test_str_eq("email as expected", str.get(), "agid.gov.it");
1 ✔
1710
         TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE,
1 ✔
1711
                     botan_x509_general_name_view_binary_value,
1712
                     (email, bin.delegate(), bin.callback()));
1713

1714
         TEST_FFI_OK(botan_x509_general_name_view_string_value, (dns, str.delegate(), str.callback()));
1 ✔
1715
         result.test_str_eq("dns as expected", str.get(), "agendadigitale.it");
1 ✔
1716
         TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE,
1 ✔
1717
                     botan_x509_general_name_view_binary_value,
1718
                     (dns, bin.delegate(), bin.callback()));
1719

1720
         TEST_FFI_OK(botan_x509_general_name_view_binary_value, (dn, bin.delegate(), bin.callback()));
1 ✔
1721
         const auto organization = read_distinguished_name(bin.get()).get_first_attribute("O");
1 ✔
1722
         result.test_str_eq("dn as expected", organization, "ACI Informatica S.p.A.");
1 ✔
1723
         TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE,
1 ✔
1724
                     botan_x509_general_name_view_string_value,
1725
                     (dn, str.delegate(), str.callback()));
1726

1727
         TEST_FFI_OK(botan_x509_general_name_view_binary_value, (ip, bin.delegate(), bin.callback()));
1 ✔
1728
         result.test_sz_eq("ip has correct length", bin.get().size(), 8);
1 ✔
1729
         TEST_FFI_OK(botan_x509_general_name_view_string_value, (ip, str.delegate(), str.callback()));
1 ✔
1730
         result.test_str_eq("ip has correct length", str.get(), "0.0.0.0/0");
1 ✔
1731

1732
         TEST_FFI_OK(botan_x509_general_name_destroy, (email));
1 ✔
1733
         TEST_FFI_OK(botan_x509_general_name_destroy, (dns));
1 ✔
1734
         TEST_FFI_OK(botan_x509_general_name_destroy, (dn));
1 ✔
1735
         TEST_FFI_OK(botan_x509_general_name_destroy, (ip));
1 ✔
1736

1737
         TEST_FFI_OK(botan_x509_cert_destroy, (cert));
1 ✔
1738
      }
1 ✔
1739
};
1740

1741
class FFI_Cert_AuthorityInformationAccess_Test final : public FFI_Test {
1 ✔
1742
   private:
1743
      static auto read_aia_string_list(Test::Result& result, botan_x509_cert_t cert, botan_x509_value_type value_type) {
3 ✔
1744
         std::vector<std::string> out;
3 ✔
1745

1746
         size_t count = 0;
3 ✔
1747
         TEST_FFI_OK(botan_x509_cert_view_string_values_count, (cert, value_type, &count));
3 ✔
1748
         for(size_t i = 0; i < count; ++i) {
8 ✔
1749
            TEST_FFI_OK(botan_x509_cert_view_string_values,
5 ✔
1750
                        (cert, value_type, i, &out, [](botan_view_ctx ctx, const char* str, size_t) -> int {
1751
                           static_cast<std::vector<std::string>*>(ctx)->emplace_back(str);
1752
                           return BOTAN_FFI_SUCCESS;
1753
                        }));
1754
         }
1755

1756
         return out;
3 ✔
1757
      }
×
1758

1759
   public:
1760
      std::string name() const override { return "FFI X509 Authority Information Access"; }
1 ✔
1761

1762
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
1763
         botan_x509_cert_t cert_with_aia;
1 ✔
1764
         botan_x509_cert_t cert_without_crl_dps;
1 ✔
1765
         if(!TEST_FFI_INIT(
1 ✔
1766
               botan_x509_cert_load_file,
1767
               (&cert_with_aia,
1768
                Test::data_file("x509/misc/contains_authority_info_access_with_two_ca_issuers.pem").c_str()))) {
1769
            return;
×
1770
         }
1771

1772
         if(!TEST_FFI_INIT(botan_x509_cert_load_file,
1 ✔
1773
                           (&cert_without_crl_dps, Test::data_file("x509/misc/no_alternative_names.pem").c_str()))) {
1774
            return;
1775
         }
1776

1777
         TEST_FFI_RC(
1 ✔
1778
            BOTAN_FFI_ERROR_OUT_OF_RANGE,
1779
            botan_x509_cert_view_string_values,
1780
            (cert_without_crl_dps, BOTAN_X509_CRL_DISTRIBUTION_URLS, 0, nullptr, [](auto, auto, auto) { return 0; }));
1781

1782
         const auto crl_dps = read_aia_string_list(result, cert_with_aia, BOTAN_X509_CRL_DISTRIBUTION_URLS);
1 ✔
1783
         result.test_sz_eq("has two CRL URI distribution points", crl_dps.size(), 2);
1 ✔
1784
         result.test_is_true("has expected CRL URI distribution point",
1 ✔
1785
                             Botan::value_exists(crl_dps, "http://crl.d-trust.net/crl/bdrive_test_ca_1-2_2017.crl"));
1 ✔
1786

1787
         const auto dummy_callback = [](auto, auto, auto) -> int { return BOTAN_FFI_SUCCESS; };
1 ✔
1788

1789
         TEST_FFI_RC(BOTAN_FFI_ERROR_OUT_OF_RANGE,
1 ✔
1790
                     botan_x509_cert_view_string_values,
1791
                     (cert_without_crl_dps, BOTAN_X509_OCSP_RESPONDER_URLS, 0, nullptr, dummy_callback));
1792
         TEST_FFI_RC(BOTAN_FFI_ERROR_OUT_OF_RANGE,
1 ✔
1793
                     botan_x509_cert_view_string_values,
1794
                     (cert_without_crl_dps, BOTAN_X509_CA_ISSUERS_URLS, 0, nullptr, dummy_callback));
1795

1796
         const auto ocsps = read_aia_string_list(result, cert_with_aia, BOTAN_X509_OCSP_RESPONDER_URLS);
1 ✔
1797
         result.test_is_true("OCSP responder found", Botan::value_exists(ocsps, "http://staging.ocsp.d-trust.net"));
2 ✔
1798

1799
         const auto cas = read_aia_string_list(result, cert_with_aia, BOTAN_X509_CA_ISSUERS_URLS);
1 ✔
1800
         result.test_is_true("CA issuer found",
1 ✔
1801
                             Botan::value_exists(cas, "http://www.d-trust.net/cgi-bin/Bdrive_Test_CA_1-2_2017.crt"));
1 ✔
1802

1803
         TEST_FFI_OK(botan_x509_cert_destroy, (cert_with_aia));
1 ✔
1804
         TEST_FFI_OK(botan_x509_cert_destroy, (cert_without_crl_dps));
1 ✔
1805
      }
1 ✔
1806
};
1807

1808
class FFI_Cert_ExtRFC3779_Test final : public FFI_Test {
1 ✔
1809
   public:
1810
      std::string name() const override { return "FFI RFC3779 certificate extensions"; }
1 ✔
1811

1812
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
1813
         botan_x509_cert_t ip_addr_blocks_cert;
1 ✔
1814
         if(!TEST_FFI_INIT(botan_x509_cert_load_file,
1 ✔
1815
                           (&ip_addr_blocks_cert, Test::data_file("x509/rfc3779/ip_safi.pem").c_str()))) {
1816
            return;
×
1817
         }
1818

1819
         size_t v4_count;
1 ✔
1820
         size_t v6_count;
1 ✔
1821
         TEST_FFI_OK(botan_x509_ext_ip_addr_blocks_get_counts, (ip_addr_blocks_cert, &v4_count, &v6_count));
1 ✔
1822
         result.test_sz_eq("V4 count is correct", v4_count, 3);
1 ✔
1823
         result.test_sz_eq("V6 count is correct", v6_count, 2);
1 ✔
1824

1825
         int has_safi;
1 ✔
1826
         uint8_t safi;
1 ✔
1827
         int present;
1 ✔
1828
         size_t count;
1 ✔
1829

1830
         TEST_FFI_RC(BOTAN_FFI_ERROR_OUT_OF_RANGE,
1 ✔
1831
                     botan_x509_ext_ip_addr_blocks_get_family,
1832
                     (ip_addr_blocks_cert, 0, v4_count + 1, &has_safi, &safi, &present, &count));
1833
         TEST_FFI_RC(BOTAN_FFI_ERROR_OUT_OF_RANGE,
1 ✔
1834
                     botan_x509_ext_ip_addr_blocks_get_family,
1835
                     (ip_addr_blocks_cert, 1, v6_count + 1, &has_safi, &safi, &present, &count));
1836

1837
         TEST_FFI_OK(botan_x509_ext_ip_addr_blocks_get_family,
1 ✔
1838
                     (ip_addr_blocks_cert, 0, 2, &has_safi, &safi, &present, &count));
1839

1840
         result.test_is_true("Family has a SAFI", has_safi == 1);
1 ✔
1841
         result.test_u8_eq("SAFI is correct", safi, 2);
1 ✔
1842
         result.test_is_true("Family is marked as inherit", present == 0);
1 ✔
1843

1844
         TEST_FFI_OK(botan_x509_ext_ip_addr_blocks_get_family,
1 ✔
1845
                     (ip_addr_blocks_cert, 0, 1, &has_safi, &safi, &present, &count));
1846

1847
         result.test_is_true("Family has a SAFI", has_safi == 1);
1 ✔
1848
         result.test_u8_eq("SAFI is correct", safi, 1);
1 ✔
1849
         result.test_is_true("Family is marked as inherit", present == 1);
1 ✔
1850
         result.test_sz_eq("Family has correct number of entries", count, 1);
1 ✔
1851

1852
         std::vector<uint8_t> min_addr(4);
1 ✔
1853
         std::vector<uint8_t> max_addr(4);
1 ✔
1854
         size_t out_len = 4;
1 ✔
1855

1856
         TEST_FFI_OK(botan_x509_ext_ip_addr_blocks_get_address,
1 ✔
1857
                     (ip_addr_blocks_cert, 0, 1, 0, min_addr.data(), max_addr.data(), &out_len));
1858

1859
         result.test_bin_eq("Min address is correct", min_addr, "C0A80000");
1 ✔
1860
         result.test_bin_eq("Max address is correct", max_addr, "C8000000");
1 ✔
1861

1862
         // only has 1 entry
1863
         TEST_FFI_RC(BOTAN_FFI_ERROR_OUT_OF_RANGE,
1 ✔
1864
                     botan_x509_ext_ip_addr_blocks_get_address,
1865
                     (ip_addr_blocks_cert, 0, 1, 1, min_addr.data(), max_addr.data(), &out_len));
1866

1867
         // only 5 families in total
1868
         TEST_FFI_RC(BOTAN_FFI_ERROR_OUT_OF_RANGE,
1 ✔
1869
                     botan_x509_ext_ip_addr_blocks_get_address,
1870
                     (ip_addr_blocks_cert, 0, 5, 0, min_addr.data(), max_addr.data(), &out_len));
1871

1872
         TEST_FFI_OK(botan_x509_cert_destroy, (ip_addr_blocks_cert));
1 ✔
1873

1874
         botan_x509_cert_t as_blocks_cert;
1 ✔
1875
         TEST_FFI_OK(botan_x509_cert_load_file,
1 ✔
1876
                     (&as_blocks_cert, Test::data_file("x509/rfc3779/as_asnum_only.pem").c_str()));
1877

1878
         // contains asnum 0-999, 5042 and no rdi
1879
         TEST_FFI_OK(botan_x509_ext_as_blocks_get_info, (as_blocks_cert, 1, &present, &count));
1 ✔
1880
         result.test_is_true("AS numbers are present", present == 1);
1 ✔
1881
         result.test_sz_eq("Correct number of AS ranges are present", count, 2);
1 ✔
1882

1883
         TEST_FFI_RC(
1 ✔
1884
            BOTAN_FFI_ERROR_NO_VALUE, botan_x509_ext_as_blocks_get_info, (as_blocks_cert, 0, &present, &count));
1885

1886
         uint32_t min_as;
1 ✔
1887
         uint32_t max_as;
1 ✔
1888

1889
         TEST_FFI_OK(botan_x509_ext_as_blocks_get_entry_at, (as_blocks_cert, 1, 0, &min_as, &max_as));
1 ✔
1890
         result.test_u32_eq("Min AS number is correct", min_as, 0);
1 ✔
1891
         result.test_u32_eq("Max AS number is correct", max_as, 999);
1 ✔
1892

1893
         TEST_FFI_OK(botan_x509_ext_as_blocks_get_entry_at, (as_blocks_cert, 1, 1, &min_as, &max_as));
1 ✔
1894
         result.test_u32_eq("Min AS number is correct", min_as, 5042);
1 ✔
1895
         result.test_u32_eq("Max AS number is correct", max_as, 5042);
1 ✔
1896

1897
         TEST_FFI_RC(BOTAN_FFI_ERROR_OUT_OF_RANGE,
1 ✔
1898
                     botan_x509_ext_as_blocks_get_entry_at,
1899
                     (as_blocks_cert, 1, 2, &min_as, &max_as));
1900

1901
         TEST_FFI_RC(
1 ✔
1902
            BOTAN_FFI_ERROR_NO_VALUE, botan_x509_ext_as_blocks_get_entry_at, (as_blocks_cert, 0, 0, &min_as, &max_as));
1903

1904
         TEST_FFI_OK(botan_x509_cert_destroy, (as_blocks_cert));
1 ✔
1905

1906
         botan_x509_cert_t no_ext_cert;
1 ✔
1907
         TEST_FFI_OK(botan_x509_cert_load_file, (&no_ext_cert, Test::data_file("x509/x509test/root.pem").c_str()));
1 ✔
1908
         TEST_FFI_RC(
1 ✔
1909
            BOTAN_FFI_ERROR_NO_VALUE, botan_x509_ext_ip_addr_blocks_get_counts, (no_ext_cert, &v4_count, &v6_count));
1910
         TEST_FFI_RC(BOTAN_FFI_ERROR_NO_VALUE,
1 ✔
1911
                     botan_x509_ext_ip_addr_blocks_get_family,
1912
                     (no_ext_cert, 0, 0, &has_safi, &safi, &present, &count));
1913
         TEST_FFI_RC(BOTAN_FFI_ERROR_NO_VALUE,
1 ✔
1914
                     botan_x509_ext_ip_addr_blocks_get_address,
1915
                     (no_ext_cert, 0, 0, 0, min_addr.data(), max_addr.data(), &out_len));
1916

1917
         TEST_FFI_RC(BOTAN_FFI_ERROR_NO_VALUE, botan_x509_ext_as_blocks_get_info, (no_ext_cert, 0, &present, &count));
1 ✔
1918
         TEST_FFI_RC(
1 ✔
1919
            BOTAN_FFI_ERROR_NO_VALUE, botan_x509_ext_as_blocks_get_entry_at, (no_ext_cert, 0, 0, &min_as, &max_as));
1920

1921
         TEST_FFI_OK(botan_x509_cert_destroy, (no_ext_cert));
1 ✔
1922
      }
1 ✔
1923
};
1924
   #endif
1925

1926
class FFI_PKCS_Hashid_Test final : public FFI_Test {
1 ✔
1927
   public:
1928
      std::string name() const override { return "FFI PKCS hash id"; }
1 ✔
1929

1930
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
1931
         std::vector<uint8_t> hash_id(64);
1 ✔
1932
         size_t hash_id_len = hash_id.size();
1 ✔
1933

1934
         if(TEST_FFI_INIT(botan_pkcs_hash_id, ("SHA-256", hash_id.data(), &hash_id_len))) {
1 ✔
1935
            result.test_sz_eq("Expected SHA-256 PKCS hash id len", hash_id_len, 19);
1 ✔
1936

1937
            hash_id.resize(hash_id_len);
1 ✔
1938
            result.test_bin_eq("Expected SHA_256 PKCS hash id", hash_id, "3031300D060960864801650304020105000420");
1 ✔
1939

1940
            hash_id_len = 3;  // too short
1 ✔
1941
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1 ✔
1942
                        botan_pkcs_hash_id,
1943
                        ("SHA-256", hash_id.data(), &hash_id_len));
1944
         }
1945
      }
1 ✔
1946
};
1947

1948
class FFI_CBC_Cipher_Test final : public FFI_Test {
1 ✔
1949
   public:
1950
      std::string name() const override { return "FFI CBC cipher"; }
1 ✔
1951

1952
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
1953
         botan_cipher_t cipher_encrypt;
1 ✔
1954
         botan_cipher_t cipher_decrypt;
1 ✔
1955

1956
         if(TEST_FFI_INIT(botan_cipher_init, (&cipher_encrypt, "AES-128/CBC/PKCS7", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
1 ✔
1957
            size_t min_keylen = 0;
1 ✔
1958
            size_t max_keylen = 0;
1 ✔
1959
            TEST_FFI_OK(botan_cipher_query_keylen, (cipher_encrypt, &min_keylen, &max_keylen));
1 ✔
1960
            result.test_sz_eq("Min key length", min_keylen, 16);
1 ✔
1961
            result.test_sz_eq("Max key length", max_keylen, 16);
1 ✔
1962

1963
            // from https://github.com/geertj/bluepass/blob/master/tests/vectors/aes-cbc-pkcs7.txt
1964
            const std::vector<uint8_t> plaintext =
1 ✔
1965
               Botan::hex_decode("0397f4f6820b1f9386f14403be5ac16e50213bd473b4874b9bcbf5f318ee686b1d");
1 ✔
1966
            const std::vector<uint8_t> symkey = Botan::hex_decode("898be9cc5004ed0fa6e117c9a3099d31");
1 ✔
1967
            const std::vector<uint8_t> nonce = Botan::hex_decode("9dea7621945988f96491083849b068df");
1 ✔
1968
            const std::vector<uint8_t> exp_ciphertext = Botan::hex_decode(
1 ✔
1969
               "e232cd6ef50047801ee681ec30f61d53cfd6b0bca02fd03c1b234baa10ea82ac9dab8b960926433a19ce6dea08677e34");
1 ✔
1970

1971
            size_t output_written = 0;
1 ✔
1972
            size_t input_consumed = 0;
1 ✔
1973

1974
            // Test that after clear or final the object can be reused
1975
            for(size_t r = 0; r != 2; ++r) {
3 ✔
1976
               size_t ctext_len;
2 ✔
1977
               TEST_FFI_OK(botan_cipher_output_length, (cipher_encrypt, plaintext.size(), &ctext_len));
2 ✔
1978
               result.test_sz_eq("Expected size of padded message", ctext_len, plaintext.size() + 15);
2 ✔
1979
               std::vector<uint8_t> ciphertext(ctext_len);
2 ✔
1980

1981
               size_t update_granularity = 0;
2 ✔
1982
               size_t ideal_granularity = 0;
2 ✔
1983
               size_t taglen = 0;
2 ✔
1984

1985
               TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_encrypt, &update_granularity));
2 ✔
1986
               TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_encrypt, &ideal_granularity));
2 ✔
1987
               TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_encrypt, &taglen));
2 ✔
1988

1989
               result.test_sz_eq(
2 ✔
1990
                  "ideal granularity is a multiple of update granularity", ideal_granularity % update_granularity, 0);
1991
               result.test_sz_eq("not an AEAD, hence no tag", taglen, 0);
2 ✔
1992

1993
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2 ✔
1994
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2 ✔
1995
               TEST_FFI_OK(botan_cipher_update,
2 ✔
1996
                           (cipher_encrypt,
1997
                            0,
1998
                            ciphertext.data(),
1999
                            ciphertext.size(),
2000
                            &output_written,
2001
                            plaintext.data(),
2002
                            plaintext.size(),
2003
                            &input_consumed));
2004
               TEST_FFI_OK(botan_cipher_clear, (cipher_encrypt));
2 ✔
2005

2006
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2 ✔
2007
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2 ✔
2008
               TEST_FFI_OK(botan_cipher_update,
2 ✔
2009
                           (cipher_encrypt,
2010
                            BOTAN_CIPHER_UPDATE_FLAG_FINAL,
2011
                            ciphertext.data(),
2012
                            ciphertext.size(),
2013
                            &output_written,
2014
                            plaintext.data(),
2015
                            plaintext.size(),
2016
                            &input_consumed));
2017

2018
               ciphertext.resize(output_written);
2 ✔
2019
               result.test_bin_eq("AES/CBC ciphertext", ciphertext, exp_ciphertext);
2 ✔
2020

2021
               if(TEST_FFI_OK(botan_cipher_init, (&cipher_decrypt, "AES-128/CBC", BOTAN_CIPHER_INIT_FLAG_DECRYPT))) {
2 ✔
2022
                  size_t ptext_len;
2 ✔
2023
                  TEST_FFI_OK(botan_cipher_output_length, (cipher_decrypt, ciphertext.size(), &ptext_len));
2 ✔
2024
                  std::vector<uint8_t> decrypted(ptext_len);
2 ✔
2025

2026
                  TEST_FFI_RC(0, botan_cipher_is_authenticated, (cipher_encrypt));
2 ✔
2027

2028
                  TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_decrypt, &update_granularity));
2 ✔
2029
                  TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_decrypt, &ideal_granularity));
2 ✔
2030
                  TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_decrypt, &taglen));
2 ✔
2031

2032
                  result.test_sz_eq("ideal granularity is a multiple of update granularity (decrypt)",
2 ✔
2033
                                    ideal_granularity % update_granularity,
2034
                                    0);
2035
                  result.test_sz_eq("not an AEAD, hence no tag (decrypt)", taglen, 0);
2 ✔
2036

2037
                  TEST_FFI_OK(botan_cipher_set_key, (cipher_decrypt, symkey.data(), symkey.size()));
2 ✔
2038
                  TEST_FFI_OK(botan_cipher_start, (cipher_decrypt, nonce.data(), nonce.size()));
2 ✔
2039
                  TEST_FFI_OK(botan_cipher_update,
2 ✔
2040
                              (cipher_decrypt,
2041
                               BOTAN_CIPHER_UPDATE_FLAG_FINAL,
2042
                               decrypted.data(),
2043
                               decrypted.size(),
2044
                               &output_written,
2045
                               ciphertext.data(),
2046
                               ciphertext.size(),
2047
                               &input_consumed));
2048

2049
                  decrypted.resize(output_written);
2 ✔
2050

2051
                  result.test_bin_eq("AES/CBC plaintext", decrypted, plaintext);
2 ✔
2052

2053
                  TEST_FFI_OK(botan_cipher_destroy, (cipher_decrypt));
2 ✔
2054
               }
2 ✔
2055
            }
2 ✔
2056

2057
            TEST_FFI_OK(botan_cipher_destroy, (cipher_encrypt));
1 ✔
2058
         }
1 ✔
2059
      }
1 ✔
2060
};
2061

2062
class FFI_GCM_Test final : public FFI_Test {
1 ✔
2063
   public:
2064
      std::string name() const override { return "FFI GCM"; }
1 ✔
2065

2066
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
2067
         botan_cipher_t cipher_encrypt;
1 ✔
2068
         botan_cipher_t cipher_decrypt;
1 ✔
2069

2070
         if(TEST_FFI_INIT(botan_cipher_init, (&cipher_encrypt, "AES-128/GCM", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
1 ✔
2071
            std::array<char, 18> namebuf{};
1 ✔
2072
            size_t name_len = 15;
1 ✔
2073
            TEST_FFI_FAIL("output buffer too short", botan_cipher_name, (cipher_encrypt, namebuf.data(), &name_len));
1 ✔
2074
            result.test_sz_eq("name len", name_len, 16);
1 ✔
2075

2076
            name_len = namebuf.size();
1 ✔
2077
            if(TEST_FFI_OK(botan_cipher_name, (cipher_encrypt, namebuf.data(), &name_len))) {
1 ✔
2078
               result.test_sz_eq("name len", name_len, 16);
1 ✔
2079
               result.test_str_eq("name", namebuf.data(), "AES-128/GCM(16)");
1 ✔
2080
            }
2081

2082
            size_t min_keylen = 0;
1 ✔
2083
            size_t max_keylen = 0;
1 ✔
2084
            size_t nonce_len = 0;
1 ✔
2085
            size_t tag_len = 0;
1 ✔
2086
            size_t update_granularity = 0;
1 ✔
2087
            size_t ideal_granularity = 0;
1 ✔
2088

2089
            TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_encrypt, &update_granularity));
1 ✔
2090
            TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_encrypt, &ideal_granularity));
1 ✔
2091

2092
            result.test_sz_eq(
1 ✔
2093
               "ideal granularity is a multiple of update granularity", ideal_granularity % update_granularity, 0);
2094

2095
            TEST_FFI_OK(botan_cipher_query_keylen, (cipher_encrypt, &min_keylen, &max_keylen));
1 ✔
2096
            result.test_sz_eq("Min key length", min_keylen, 16);
1 ✔
2097
            result.test_sz_eq("Max key length", max_keylen, 16);
1 ✔
2098

2099
            TEST_FFI_OK(botan_cipher_get_default_nonce_length, (cipher_encrypt, &nonce_len));
1 ✔
2100
            result.test_sz_eq("Expected default GCM nonce length", nonce_len, 12);
1 ✔
2101

2102
            TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_encrypt, &tag_len));
1 ✔
2103
            result.test_sz_eq("Expected GCM tag length", tag_len, 16);
1 ✔
2104

2105
            TEST_FFI_RC(1, botan_cipher_is_authenticated, (cipher_encrypt));
1 ✔
2106

2107
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 12));
1 ✔
2108
            // GCM accepts any nonce size except zero
2109
            TEST_FFI_RC(0, botan_cipher_valid_nonce_length, (cipher_encrypt, 0));
1 ✔
2110
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 1));
1 ✔
2111
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 100009));
1 ✔
2112

2113
            // NIST test vector
2114
            const std::vector<uint8_t> plaintext = Botan::hex_decode(
1 ✔
2115
               "D9313225F88406E5A55909C5AFF5269A86A7A9531534F7DA2E4C303D8A318A721C3C0C95956809532FCF0E2449A6B525B16AEDF5AA0DE657BA637B39");
1 ✔
2116

2117
            const std::vector<uint8_t> symkey = Botan::hex_decode("FEFFE9928665731C6D6A8F9467308308");
1 ✔
2118
            const std::vector<uint8_t> nonce = Botan::hex_decode("CAFEBABEFACEDBADDECAF888");
1 ✔
2119
            const std::vector<uint8_t> exp_ciphertext = Botan::hex_decode(
1 ✔
2120
               "42831EC2217774244B7221B784D0D49CE3AA212F2C02A4E035C17E2329ACA12E21D514B25466931C7D8F6A5AAC84AA051BA30B396A0AAC973D58E0915BC94FBC3221A5DB94FAE95AE7121A47");
1 ✔
2121
            const std::vector<uint8_t> aad = Botan::hex_decode("FEEDFACEDEADBEEFFEEDFACEDEADBEEFABADDAD2");
1 ✔
2122

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

2125
            size_t output_written = 0;
1 ✔
2126
            size_t input_consumed = 0;
1 ✔
2127

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

2132
               // First use a nonce of the AAD, and ensure reset works
2133
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, aad.data(), aad.size()));
2 ✔
2134
               TEST_FFI_OK(botan_cipher_reset, (cipher_encrypt));
2 ✔
2135

2136
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2 ✔
2137
               TEST_FFI_OK(botan_cipher_update,
2 ✔
2138
                           (cipher_encrypt,
2139
                            0,
2140
                            ciphertext.data(),
2141
                            ciphertext.size(),
2142
                            &output_written,
2143
                            plaintext.data(),
2144
                            plaintext.size(),
2145
                            &input_consumed));
2146
               TEST_FFI_OK(botan_cipher_clear, (cipher_encrypt));
2 ✔
2147

2148
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2 ✔
2149
               TEST_FFI_OK(botan_cipher_set_associated_data, (cipher_encrypt, aad.data(), aad.size()));
2 ✔
2150
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2 ✔
2151
               TEST_FFI_OK(botan_cipher_update,
2 ✔
2152
                           (cipher_encrypt,
2153
                            BOTAN_CIPHER_UPDATE_FLAG_FINAL,
2154
                            ciphertext.data(),
2155
                            ciphertext.size(),
2156
                            &output_written,
2157
                            plaintext.data(),
2158
                            plaintext.size(),
2159
                            &input_consumed));
2160

2161
               ciphertext.resize(output_written);
2 ✔
2162
               result.test_bin_eq("AES/GCM ciphertext", ciphertext, exp_ciphertext);
2 ✔
2163

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

2167
                  TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_decrypt, &update_granularity));
2 ✔
2168
                  TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_decrypt, &ideal_granularity));
2 ✔
2169

2170
                  result.test_sz_eq("ideal granularity is a multiple of update granularity (decrypt)",
2 ✔
2171
                                    ideal_granularity % update_granularity,
2172
                                    0);
2173

2174
                  TEST_FFI_OK(botan_cipher_set_key, (cipher_decrypt, symkey.data(), symkey.size()));
2 ✔
2175
                  TEST_FFI_OK(botan_cipher_set_associated_data, (cipher_decrypt, aad.data(), aad.size()));
2 ✔
2176
                  TEST_FFI_OK(botan_cipher_start, (cipher_decrypt, nonce.data(), nonce.size()));
2 ✔
2177
                  TEST_FFI_OK(botan_cipher_update,
2 ✔
2178
                              (cipher_decrypt,
2179
                               BOTAN_CIPHER_UPDATE_FLAG_FINAL,
2180
                               decrypted.data(),
2181
                               decrypted.size(),
2182
                               &output_written,
2183
                               ciphertext.data(),
2184
                               ciphertext.size(),
2185
                               &input_consumed));
2186

2187
                  result.test_sz_eq("All input consumed", input_consumed, ciphertext.size());
2 ✔
2188
                  result.test_sz_eq("Expected output size produced", output_written, decrypted.size());
2 ✔
2189
                  result.test_bin_eq("AES/GCM plaintext", decrypted, plaintext);
2 ✔
2190

2191
                  TEST_FFI_OK(botan_cipher_destroy, (cipher_decrypt));
2 ✔
2192
               }
2 ✔
2193
            }
2194

2195
            TEST_FFI_OK(botan_cipher_destroy, (cipher_encrypt));
1 ✔
2196
         }
1 ✔
2197
      }
1 ✔
2198
};
2199

2200
class FFI_ChaCha20Poly1305_Test final : public FFI_Test {
1 ✔
2201
   public:
2202
      std::string name() const override { return "FFI ChaCha20Poly1305"; }
1 ✔
2203

2204
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
2205
         botan_cipher_t cipher_encrypt;
1 ✔
2206
         botan_cipher_t cipher_decrypt;
1 ✔
2207

2208
         if(TEST_FFI_INIT(botan_cipher_init, (&cipher_encrypt, "ChaCha20Poly1305", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
1 ✔
2209
            std::array<char, 17> namebuf{};
1 ✔
2210
            size_t name_len = 15;
1 ✔
2211
            TEST_FFI_FAIL("output buffer too short", botan_cipher_name, (cipher_encrypt, namebuf.data(), &name_len));
1 ✔
2212
            result.test_sz_eq("name len", name_len, 17);
1 ✔
2213

2214
            name_len = namebuf.size();
1 ✔
2215
            if(TEST_FFI_OK(botan_cipher_name, (cipher_encrypt, namebuf.data(), &name_len))) {
1 ✔
2216
               result.test_sz_eq("name len", name_len, 17);
1 ✔
2217
               result.test_str_eq("name", std::string(namebuf.data()), "ChaCha20Poly1305");
1 ✔
2218
            }
2219

2220
            size_t min_keylen = 0;
1 ✔
2221
            size_t max_keylen = 0;
1 ✔
2222
            size_t nonce_len = 0;
1 ✔
2223
            size_t tag_len = 0;
1 ✔
2224
            size_t update_granularity = 0;
1 ✔
2225
            size_t ideal_granularity = 0;
1 ✔
2226

2227
            TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_encrypt, &update_granularity));
1 ✔
2228
            TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_encrypt, &ideal_granularity));
1 ✔
2229

2230
            result.test_sz_eq(
1 ✔
2231
               "ideal granularity is a multiple of update granularity", ideal_granularity % update_granularity, 0);
2232

2233
            TEST_FFI_OK(botan_cipher_query_keylen, (cipher_encrypt, &min_keylen, &max_keylen));
1 ✔
2234
            result.test_sz_eq("Min key length", min_keylen, 32);
1 ✔
2235
            result.test_sz_eq("Max key length", max_keylen, 32);
1 ✔
2236

2237
            TEST_FFI_OK(botan_cipher_get_default_nonce_length, (cipher_encrypt, &nonce_len));
1 ✔
2238
            result.test_sz_eq("Expected default ChaCha20Poly1305 nonce length", nonce_len, 12);
1 ✔
2239

2240
            TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_encrypt, &tag_len));
1 ✔
2241
            result.test_sz_eq("Expected Chacha20Poly1305 tag length", tag_len, 16);
1 ✔
2242

2243
            TEST_FFI_RC(1, botan_cipher_is_authenticated, (cipher_encrypt));
1 ✔
2244

2245
            // From RFC 7539
2246
            const std::vector<uint8_t> plaintext = Botan::hex_decode(
1 ✔
2247
               "4C616469657320616E642047656E746C656D656E206F662074686520636C617373206F66202739393A204966204920636F756C64206F6666657220796F75206F6E6C79206F6E652074697020666F7220746865206675747572652C2073756E73637265656E20776F756C642062652069742E");
1 ✔
2248
            const std::vector<uint8_t> symkey =
1 ✔
2249
               Botan::hex_decode("808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9F");
1 ✔
2250
            const std::vector<uint8_t> nonce = Botan::hex_decode("070000004041424344454647");
1 ✔
2251
            const std::vector<uint8_t> exp_ciphertext = Botan::hex_decode(
1 ✔
2252
               "D31A8D34648E60DB7B86AFBC53EF7EC2A4ADED51296E08FEA9E2B5A736EE62D63DBEA45E8CA9671282FAFB69DA92728B1A71DE0A9E060B2905D6A5B67ECD3B3692DDBD7F2D778B8C9803AEE328091B58FAB324E4FAD675945585808B4831D7BC3FF4DEF08E4B7A9DE576D26586CEC64B61161AE10B594F09E26A7E902ECBD0600691");
1 ✔
2253
            const std::vector<uint8_t> aad = Botan::hex_decode("50515253C0C1C2C3C4C5C6C7");
1 ✔
2254

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

2257
            size_t output_written = 0;
1 ✔
2258
            size_t input_consumed = 0;
1 ✔
2259

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

2264
               // First use a nonce of the AAD, and ensure reset works
2265
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, aad.data(), aad.size()));
2 ✔
2266
               TEST_FFI_OK(botan_cipher_reset, (cipher_encrypt));
2 ✔
2267

2268
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2 ✔
2269
               TEST_FFI_OK(botan_cipher_update,
2 ✔
2270
                           (cipher_encrypt,
2271
                            0,
2272
                            ciphertext.data(),
2273
                            ciphertext.size(),
2274
                            &output_written,
2275
                            plaintext.data(),
2276
                            plaintext.size(),
2277
                            &input_consumed));
2278
               TEST_FFI_OK(botan_cipher_clear, (cipher_encrypt));
2 ✔
2279

2280
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2 ✔
2281
               TEST_FFI_OK(botan_cipher_set_associated_data, (cipher_encrypt, aad.data(), aad.size()));
2 ✔
2282
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2 ✔
2283
               TEST_FFI_OK(botan_cipher_update,
2 ✔
2284
                           (cipher_encrypt,
2285
                            BOTAN_CIPHER_UPDATE_FLAG_FINAL,
2286
                            ciphertext.data(),
2287
                            ciphertext.size(),
2288
                            &output_written,
2289
                            plaintext.data(),
2290
                            plaintext.size(),
2291
                            &input_consumed));
2292

2293
               ciphertext.resize(output_written);
2 ✔
2294
               result.test_bin_eq("AES/GCM ciphertext", ciphertext, exp_ciphertext);
2 ✔
2295

2296
               if(TEST_FFI_OK(botan_cipher_init,
2 ✔
2297
                              (&cipher_decrypt, "ChaCha20Poly1305", BOTAN_CIPHER_INIT_FLAG_DECRYPT))) {
2298
                  std::vector<uint8_t> decrypted(plaintext.size());
2 ✔
2299

2300
                  TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_decrypt, &update_granularity));
2 ✔
2301
                  TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_decrypt, &ideal_granularity));
2 ✔
2302

2303
                  result.test_sz_eq("ideal granularity is a multiple of update granularity (decrypt)",
2 ✔
2304
                                    ideal_granularity % update_granularity,
2305
                                    0);
2306

2307
                  TEST_FFI_OK(botan_cipher_set_key, (cipher_decrypt, symkey.data(), symkey.size()));
2 ✔
2308
                  TEST_FFI_OK(botan_cipher_set_associated_data, (cipher_decrypt, aad.data(), aad.size()));
2 ✔
2309
                  TEST_FFI_OK(botan_cipher_start, (cipher_decrypt, nonce.data(), nonce.size()));
2 ✔
2310
                  TEST_FFI_OK(botan_cipher_update,
2 ✔
2311
                              (cipher_decrypt,
2312
                               BOTAN_CIPHER_UPDATE_FLAG_FINAL,
2313
                               decrypted.data(),
2314
                               decrypted.size(),
2315
                               &output_written,
2316
                               ciphertext.data(),
2317
                               ciphertext.size(),
2318
                               &input_consumed));
2319

2320
                  result.test_sz_eq("All input consumed", input_consumed, ciphertext.size());
2 ✔
2321
                  result.test_sz_eq("Expected output size produced", output_written, decrypted.size());
2 ✔
2322
                  result.test_bin_eq("AES/GCM plaintext", decrypted, plaintext);
2 ✔
2323

2324
                  TEST_FFI_OK(botan_cipher_destroy, (cipher_decrypt));
2 ✔
2325
               }
2 ✔
2326
            }
2327

2328
            TEST_FFI_OK(botan_cipher_destroy, (cipher_encrypt));
1 ✔
2329
         }
1 ✔
2330
      }
1 ✔
2331
};
2332

2333
class FFI_EAX_Test final : public FFI_Test {
1 ✔
2334
   public:
2335
      std::string name() const override { return "FFI EAX"; }
1 ✔
2336

2337
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
2338
         botan_cipher_t cipher_encrypt;
1 ✔
2339
         botan_cipher_t cipher_decrypt;
1 ✔
2340

2341
         if(TEST_FFI_INIT(botan_cipher_init, (&cipher_encrypt, "AES-128/EAX", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
1 ✔
2342
            size_t min_keylen = 0;
1 ✔
2343
            size_t max_keylen = 0;
1 ✔
2344
            size_t mod_keylen = 0;
1 ✔
2345
            size_t nonce_len = 0;
1 ✔
2346
            size_t tag_len = 0;
1 ✔
2347
            size_t update_granularity = 0;
1 ✔
2348
            size_t ideal_granularity = 0;
1 ✔
2349

2350
            TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_encrypt, &update_granularity));
1 ✔
2351
            TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_encrypt, &ideal_granularity));
1 ✔
2352

2353
            result.test_sz_eq(
1 ✔
2354
               "ideal granularity is a multiple of update granularity", ideal_granularity % update_granularity, 0);
2355

2356
            TEST_FFI_OK(botan_cipher_query_keylen, (cipher_encrypt, &min_keylen, &max_keylen));
1 ✔
2357
            result.test_sz_eq("Min key length", min_keylen, 16);
1 ✔
2358
            result.test_sz_eq("Max key length", max_keylen, 16);
1 ✔
2359

2360
            TEST_FFI_OK(botan_cipher_get_keyspec, (cipher_encrypt, &min_keylen, &max_keylen, &mod_keylen));
1 ✔
2361
            result.test_sz_eq("Min key length", min_keylen, 16);
1 ✔
2362
            result.test_sz_eq("Max key length", max_keylen, 16);
1 ✔
2363
            result.test_sz_eq("Mod key length", mod_keylen, 1);
1 ✔
2364

2365
            TEST_FFI_OK(botan_cipher_get_default_nonce_length, (cipher_encrypt, &nonce_len));
1 ✔
2366
            result.test_sz_eq("Expected default EAX nonce length", nonce_len, 12);
1 ✔
2367

2368
            TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_encrypt, &tag_len));
1 ✔
2369
            result.test_sz_eq("Expected EAX tag length", tag_len, 16);
1 ✔
2370

2371
            TEST_FFI_RC(1, botan_cipher_is_authenticated, (cipher_encrypt));
1 ✔
2372

2373
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 12));
1 ✔
2374
            // EAX accepts any nonce size...
2375
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 0));
1 ✔
2376

2377
            const std::vector<uint8_t> plaintext =
1 ✔
2378
               Botan::hex_decode("0000000000000000000000000000000011111111111111111111111111111111");
1 ✔
2379
            const std::vector<uint8_t> symkey = Botan::hex_decode("000102030405060708090a0b0c0d0e0f");
1 ✔
2380
            const std::vector<uint8_t> nonce = Botan::hex_decode("3c8cc2970a008f75cc5beae2847258c2");
1 ✔
2381
            const std::vector<uint8_t> exp_ciphertext = Botan::hex_decode(
1 ✔
2382
               "3c441f32ce07822364d7a2990e50bb13d7b02a26969e4a937e5e9073b0d9c968db90bdb3da3d00afd0fc6a83551da95e");
1 ✔
2383

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

2386
            size_t output_written = 0;
1 ✔
2387
            size_t input_consumed = 0;
1 ✔
2388

2389
            // Test that after clear or final the object can be reused
2390
            for(size_t r = 0; r != 2; ++r) {
3 ✔
2391
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2 ✔
2392
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2 ✔
2393
               TEST_FFI_OK(botan_cipher_update,
2 ✔
2394
                           (cipher_encrypt,
2395
                            0,
2396
                            ciphertext.data(),
2397
                            ciphertext.size(),
2398
                            &output_written,
2399
                            plaintext.data(),
2400
                            plaintext.size(),
2401
                            &input_consumed));
2402
               TEST_FFI_OK(botan_cipher_clear, (cipher_encrypt));
2 ✔
2403

2404
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2 ✔
2405
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2 ✔
2406
               TEST_FFI_OK(botan_cipher_update,
2 ✔
2407
                           (cipher_encrypt,
2408
                            BOTAN_CIPHER_UPDATE_FLAG_FINAL,
2409
                            ciphertext.data(),
2410
                            ciphertext.size(),
2411
                            &output_written,
2412
                            plaintext.data(),
2413
                            plaintext.size(),
2414
                            &input_consumed));
2415

2416
               ciphertext.resize(output_written);
2 ✔
2417
               result.test_bin_eq("AES/EAX ciphertext", ciphertext, exp_ciphertext);
2 ✔
2418

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

2422
                  TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_decrypt, &update_granularity));
2 ✔
2423
                  TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_decrypt, &ideal_granularity));
2 ✔
2424

2425
                  result.test_sz_eq("ideal granularity is a multiple of update granularity (decrypt)",
2 ✔
2426
                                    ideal_granularity % update_granularity,
2427
                                    0);
2428

2429
                  TEST_FFI_OK(botan_cipher_set_key, (cipher_decrypt, symkey.data(), symkey.size()));
2 ✔
2430
                  TEST_FFI_OK(botan_cipher_start, (cipher_decrypt, nonce.data(), nonce.size()));
2 ✔
2431
                  TEST_FFI_OK(botan_cipher_update,
2 ✔
2432
                              (cipher_decrypt,
2433
                               BOTAN_CIPHER_UPDATE_FLAG_FINAL,
2434
                               decrypted.data(),
2435
                               decrypted.size(),
2436
                               &output_written,
2437
                               ciphertext.data(),
2438
                               ciphertext.size(),
2439
                               &input_consumed));
2440

2441
                  result.test_sz_eq("All input consumed", input_consumed, ciphertext.size());
2 ✔
2442
                  result.test_sz_eq("Expected output size produced", output_written, decrypted.size());
2 ✔
2443
                  result.test_bin_eq("AES/EAX plaintext", decrypted, plaintext);
2 ✔
2444

2445
                  TEST_FFI_OK(botan_cipher_destroy, (cipher_decrypt));
2 ✔
2446
               }
2 ✔
2447
            }
2448

2449
            TEST_FFI_OK(botan_cipher_destroy, (cipher_encrypt));
1 ✔
2450
         }
1 ✔
2451
      }
1 ✔
2452
};
2453

2454
class FFI_AEAD_Test final : public FFI_Test {
1 ✔
2455
   public:
2456
      std::string name() const override { return "FFI AEAD"; }
1 ✔
2457

2458
      void ffi_test(Test::Result& merged_result, botan_rng_t rng) override {
1 ✔
2459
         botan_cipher_t cipher_encrypt;
1 ✔
2460
         botan_cipher_t cipher_decrypt;
1 ✔
2461

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

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

2468
            if(!TEST_FFI_INIT(botan_cipher_init, (&cipher_encrypt, aead.c_str(), BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
5 ✔
2469
               continue;
×
2470
            }
2471

2472
            if(botan_cipher_is_authenticated(cipher_encrypt) == 0) {
5 ✔
2473
               result.test_failure("Cipher " + aead + " claims is not authenticated");
×
2474
               botan_cipher_destroy(cipher_encrypt);
×
2475
               continue;
×
2476
            }
2477

2478
            size_t min_keylen = 0;
5 ✔
2479
            size_t max_keylen = 0;
5 ✔
2480
            size_t update_granularity = 0;
5 ✔
2481
            size_t ideal_granularity = 0;
5 ✔
2482
            size_t noncelen = 0;
5 ✔
2483
            size_t taglen = 0;
5 ✔
2484
            constexpr size_t pt_multiplier = 5;
5 ✔
2485
            TEST_FFI_OK(botan_cipher_query_keylen, (cipher_encrypt, &min_keylen, &max_keylen));
5 ✔
2486
            TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_encrypt, &update_granularity));
5 ✔
2487
            TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_encrypt, &ideal_granularity));
5 ✔
2488
            TEST_FFI_OK(botan_cipher_get_default_nonce_length, (cipher_encrypt, &noncelen));
5 ✔
2489
            TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_encrypt, &taglen));
5 ✔
2490

2491
            result.test_sz_eq(
5 ✔
2492
               "ideal granularity is a multiple of update granularity", ideal_granularity % update_granularity, 0);
2493

2494
            std::vector<uint8_t> key(max_keylen);
5 ✔
2495
            TEST_FFI_OK(botan_rng_get, (rng, key.data(), key.size()));
5 ✔
2496
            TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, key.data(), key.size()));
5 ✔
2497

2498
            std::vector<uint8_t> nonce(noncelen);
5 ✔
2499
            TEST_FFI_OK(botan_rng_get, (rng, nonce.data(), nonce.size()));
5 ✔
2500
            TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
5 ✔
2501

2502
            std::vector<uint8_t> plaintext(ideal_granularity * pt_multiplier);
5 ✔
2503
            std::vector<uint8_t> ciphertext(ideal_granularity * pt_multiplier + taglen);
5 ✔
2504
            TEST_FFI_OK(botan_rng_get, (rng, plaintext.data(), plaintext.size()));
5 ✔
2505

2506
            std::vector<uint8_t> dummy_buffer(1024);
5 ✔
2507
            TEST_FFI_OK(botan_rng_get, (rng, dummy_buffer.data(), dummy_buffer.size()));
5 ✔
2508
            std::vector<uint8_t> dummy_buffer_reference = dummy_buffer;
5 ✔
2509

2510
            const bool requires_entire_message = botan_cipher_requires_entire_message(cipher_encrypt) == 1;
5 ✔
2511
            result.test_bool_eq(
5 ✔
2512
               "requires entire message", requires_entire_message, (aead == "AES-256/SIV" || aead == "AES-128/CCM"));
5 ✔
2513

2514
            std::span<const uint8_t> pt_slicer(plaintext);
5 ✔
2515
            std::span<uint8_t> ct_stuffer(ciphertext);
5 ✔
2516

2517
            // Process data that is explicitly a multiple of the ideal
2518
            // granularity and therefore should be aligned with the cipher's
2519
            // internal block size.
2520
            for(size_t i = 0; i < pt_multiplier; ++i) {
30 ✔
2521
               size_t output_written = 0;
25 ✔
2522
               size_t input_consumed = 0;
25 ✔
2523

2524
               const auto pt_chunk = pt_slicer.first(ideal_granularity);
25 ✔
2525

2526
               // The existing implementation won't consume any bytes from the
2527
               // input if there is no space in the output buffer. Even when
2528
               // the cipher is a mode that won't produce any output until the
2529
               // entire message is processed. Hence, give it some dummy buffer.
2530
               BOTAN_ASSERT_NOMSG(dummy_buffer.size() > ideal_granularity);
25 ✔
2531
               const auto ct_chunk = (requires_entire_message) ? std::span(dummy_buffer).first(ideal_granularity)
25 ✔
2532
                                                               : ct_stuffer.first(ideal_granularity);
2533

2534
               TEST_FFI_OK(botan_cipher_update,
25 ✔
2535
                           (cipher_encrypt,
2536
                            0 /* don't finalize */,
2537
                            ct_chunk.data(),
2538
                            ct_chunk.size(),
2539
                            &output_written,
2540
                            pt_chunk.data(),
2541
                            pt_chunk.size(),
2542
                            &input_consumed));
2543

2544
               result.test_sz_gt("some input consumed", input_consumed, 0);
25 ✔
2545
               result.test_sz_lte("at most, all input consumed", input_consumed, pt_chunk.size());
25 ✔
2546
               pt_slicer = pt_slicer.subspan(input_consumed);
25 ✔
2547

2548
               if(requires_entire_message) {
25 ✔
2549
                  result.test_sz_eq("no output produced", output_written, 0);
10 ✔
2550
               } else {
2551
                  result.test_sz_eq("all bytes produced", output_written, input_consumed);
15 ✔
2552
                  ct_stuffer = ct_stuffer.subspan(output_written);
15 ✔
2553
               }
2554
            }
2555

2556
            // Trying to pull a part of the authentication tag should fail,
2557
            // as we must consume the entire tag in a single invocation to
2558
            // botan_cipher_update().
2559
            size_t final_output_written = 42;
5 ✔
2560
            size_t final_input_consumed = 1337;
5 ✔
2561
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
5 ✔
2562
                        botan_cipher_update,
2563
                        (cipher_encrypt,
2564
                         BOTAN_CIPHER_UPDATE_FLAG_FINAL,
2565
                         dummy_buffer.data(),
2566
                         3, /* not enough to hold any reasonable auth'n tag */
2567
                         &final_output_written,
2568
                         pt_slicer.data(),  // remaining bytes (typically 0)
2569
                         pt_slicer.size(),
2570
                         &final_input_consumed));
2571

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

2574
            result.test_sz_eq("remaining bytes consumed in bogus final", final_input_consumed, pt_slicer.size());
5 ✔
2575
            result.test_sz_eq(
5 ✔
2576
               "required buffer size is written in bogus final", final_output_written, expected_final_size);
2577

2578
            const auto final_ct_chunk = ct_stuffer.first(expected_final_size);
5 ✔
2579

2580
            TEST_FFI_OK(botan_cipher_update,
5 ✔
2581
                        (cipher_encrypt,
2582
                         BOTAN_CIPHER_UPDATE_FLAG_FINAL,
2583
                         final_ct_chunk.data(),
2584
                         final_ct_chunk.size(),
2585
                         &final_output_written,
2586
                         nullptr,  // no more input
2587
                         0,
2588
                         &final_input_consumed));
2589

2590
            result.test_sz_eq("no bytes consumed in final", final_input_consumed, 0);
5 ✔
2591
            result.test_sz_eq("final bytes written", final_output_written, expected_final_size);
5 ✔
2592
            result.test_bin_eq("dummy buffer unchanged", dummy_buffer, dummy_buffer_reference);
5 ✔
2593

2594
            TEST_FFI_OK(botan_cipher_destroy, (cipher_encrypt));
5 ✔
2595

2596
            // ----------------------------------------------------------------
2597

2598
            TEST_FFI_INIT(botan_cipher_init, (&cipher_decrypt, aead.c_str(), BOTAN_CIPHER_INIT_FLAG_DECRYPT));
5 ✔
2599

2600
            TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_decrypt, &update_granularity));
5 ✔
2601
            TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_decrypt, &ideal_granularity));
5 ✔
2602

2603
            result.test_sz_eq("ideal granularity is a multiple of update granularity (decrypt)",
5 ✔
2604
                              ideal_granularity % update_granularity,
2605
                              0);
2606

2607
            TEST_FFI_OK(botan_cipher_set_key, (cipher_decrypt, key.data(), key.size()));
5 ✔
2608
            TEST_FFI_OK(botan_cipher_start, (cipher_decrypt, nonce.data(), nonce.size()));
5 ✔
2609

2610
            std::vector<uint8_t> decrypted(plaintext.size());
5 ✔
2611

2612
            std::span<const uint8_t> ct_slicer(ciphertext);
5 ✔
2613
            std::span<uint8_t> pt_stuffer(decrypted);
5 ✔
2614

2615
            // Process data that is explicitly a multiple of the ideal
2616
            // granularity and therefore should be aligned with the cipher's
2617
            // internal block size.
2618
            for(size_t i = 0; i < pt_multiplier; ++i) {
30 ✔
2619
               size_t output_written = 42;
25 ✔
2620
               size_t input_consumed = 1337;
25 ✔
2621

2622
               const auto ct_chunk = ct_slicer.first(ideal_granularity);
25 ✔
2623

2624
               // The existing implementation won't consume any bytes from the
2625
               // input if there is no space in the output buffer. Even when
2626
               // the cipher is a mode that won't produce any output until the
2627
               // entire message is processed. Hence, give it some dummy buffer.
2628
               const auto pt_chunk = (requires_entire_message) ? std::span(dummy_buffer).first(ideal_granularity)
25 ✔
2629
                                                               : pt_stuffer.first(ideal_granularity);
2630

2631
               TEST_FFI_OK(botan_cipher_update,
25 ✔
2632
                           (cipher_decrypt,
2633
                            0 /* don't finalize */,
2634
                            pt_chunk.data(),
2635
                            pt_chunk.size(),
2636
                            &output_written,
2637
                            ct_chunk.data(),
2638
                            ct_chunk.size(),
2639
                            &input_consumed));
2640

2641
               result.test_sz_gt("some input consumed", input_consumed, 0);
25 ✔
2642
               result.test_sz_lte("at most, all input consumed", input_consumed, ct_chunk.size());
25 ✔
2643
               ct_slicer = ct_slicer.subspan(input_consumed);
25 ✔
2644

2645
               if(requires_entire_message) {
25 ✔
2646
                  result.test_sz_eq("no output produced", output_written, 0);
10 ✔
2647
               } else {
2648
                  result.test_sz_eq("all bytes produced", output_written, input_consumed);
15 ✔
2649
                  pt_stuffer = pt_stuffer.subspan(output_written);
15 ✔
2650
               }
2651
            }
2652

2653
            const size_t expected_final_size_dec = requires_entire_message ? plaintext.size() : pt_stuffer.size();
5 ✔
2654
            const auto pt_chunk = pt_stuffer.first(expected_final_size_dec);
5 ✔
2655

2656
            size_t final_output_written_dec = 42;
5 ✔
2657
            size_t final_input_consumed_dec = 1337;
5 ✔
2658

2659
            TEST_FFI_OK(botan_cipher_update,
5 ✔
2660
                        (cipher_decrypt,
2661
                         BOTAN_CIPHER_UPDATE_FLAG_FINAL,
2662
                         pt_chunk.data(),
2663
                         pt_chunk.size(),
2664
                         &final_output_written_dec,
2665
                         ct_slicer.data(),  // remaining bytes (typically 0)
2666
                         ct_slicer.size(),
2667
                         &final_input_consumed_dec));
2668

2669
            result.test_sz_eq(
5 ✔
2670
               "remaining bytes consumed in final (decrypt)", final_input_consumed_dec, ct_slicer.size());
2671
            result.test_sz_eq("bytes written in final (decrypt)", final_output_written_dec, expected_final_size_dec);
5 ✔
2672
            result.test_bin_eq("dummy buffer unchanged", dummy_buffer, dummy_buffer_reference);
5 ✔
2673

2674
            result.test_bin_eq("decrypted plaintext", decrypted, plaintext);
5 ✔
2675

2676
            TEST_FFI_OK(botan_cipher_destroy, (cipher_decrypt));
5 ✔
2677

2678
            merged_result.merge(result, true /* ignore names */);
5 ✔
2679
         }
5 ✔
2680
      }
1 ✔
2681
};
2682

2683
class FFI_StreamCipher_Test final : public FFI_Test {
1 ✔
2684
   public:
2685
      std::string name() const override { return "FFI stream ciphers"; }
1 ✔
2686

2687
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
2688
         botan_cipher_t ctr;
1 ✔
2689

2690
         if(TEST_FFI_INIT(botan_cipher_init, (&ctr, "AES-128/CTR-BE", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
1 ✔
2691
            const std::vector<uint8_t> key = Botan::hex_decode("2B7E151628AED2A6ABF7158809CF4F3C");
1 ✔
2692
            const std::vector<uint8_t> nonce = Botan::hex_decode("F0F1F2F3F4F5F6F7F8F9FAFBFCFDFF");
1 ✔
2693
            const std::vector<uint8_t> pt = Botan::hex_decode(
1 ✔
2694
               "AE2D8A571E03AC9C9EB76FAC45AF8E5130C81C46A35CE411E5FBC1191A0A52EFF69F2445DF4F9B17AD2B417BE66C3710");
1 ✔
2695
            const std::vector<uint8_t> exp_ct = Botan::hex_decode(
1 ✔
2696
               "9806F66B7970FDFF8617187BB9FFFDFF5AE4DF3EDBD5D35E5B4F09020DB03EAB1E031DDA2FBE03D1792170A0F3009CEE");
1 ✔
2697

2698
            std::vector<uint8_t> ct(pt.size());
1 ✔
2699

2700
            size_t update_granularity = 0;
1 ✔
2701
            size_t ideal_granularity = 0;
1 ✔
2702

2703
            TEST_FFI_OK(botan_cipher_get_update_granularity, (ctr, &update_granularity));
1 ✔
2704
            TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (ctr, &ideal_granularity));
1 ✔
2705

2706
            result.test_sz_eq(
1 ✔
2707
               "ideal granularity is a multiple of update granularity", ideal_granularity % update_granularity, 0);
2708

2709
            TEST_FFI_RC(0, botan_cipher_is_authenticated, (ctr));
1 ✔
2710

2711
            size_t input_consumed = 0;
1 ✔
2712
            size_t output_written = 0;
1 ✔
2713

2714
            TEST_FFI_OK(botan_cipher_set_key, (ctr, key.data(), key.size()));
1 ✔
2715
            TEST_FFI_OK(botan_cipher_start, (ctr, nonce.data(), nonce.size()));
1 ✔
2716

2717
            // Test partial updates...
2718
            TEST_FFI_OK(botan_cipher_update,
1 ✔
2719
                        (ctr, 0, ct.data(), ct.size(), &output_written, pt.data(), 5, &input_consumed));
2720

2721
            result.test_sz_eq("Expected output written", output_written, 5);
1 ✔
2722
            result.test_sz_eq("Expected input consumed", input_consumed, 5);
1 ✔
2723

2724
            TEST_FFI_OK(botan_cipher_update,
1 ✔
2725
                        (ctr, 0, &ct[5], ct.size() - 5, &output_written, &pt[5], pt.size() - 5, &input_consumed));
2726

2727
            result.test_sz_eq("Expected output written", output_written, ct.size() - 5);
1 ✔
2728
            result.test_sz_eq("Expected input consumed", input_consumed, pt.size() - 5);
1 ✔
2729
            result.test_bin_eq("AES-128/CTR ciphertext", ct, exp_ct);
1 ✔
2730

2731
            TEST_FFI_OK(botan_cipher_destroy, (ctr));
1 ✔
2732
         }
1 ✔
2733
      }
1 ✔
2734
};
2735

2736
class FFI_XOF_Test final : public FFI_Test {
1 ✔
2737
      std::string name() const override { return "FFI XOF"; }
1 ✔
2738

2739
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
2740
         const char* input1 = "XOF input";
1 ✔
2741
         const char* input2 = "more XOF input";
1 ✔
2742
         const char* input3 = "additional XOF input";
1 ✔
2743
         const char* xof_name = "SHAKE-128";
1 ✔
2744

2745
         botan_xof_t xof1;
1 ✔
2746
         TEST_FFI_FAIL("unknown XOF", botan_xof_init, (&xof1, "SCHUETTEL-128", 0));
1 ✔
2747
         TEST_FFI_FAIL("invalid flags", botan_xof_init, (&xof1, "SHAKE-128", 42));
1 ✔
2748

2749
         if(!TEST_FFI_INIT(botan_xof_init, (&xof1, xof_name, 0))) {
1 ✔
2750
            return;
×
2751
         }
2752

2753
         std::array<char, 10> out_name{};
1 ✔
2754
         size_t out_name_len = 5;
1 ✔
2755
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_xof_name, (xof1, nullptr, &out_name_len));
1 ✔
2756
         result.test_sz_eq("valid XOF name length", out_name_len, out_name.size());
1 ✔
2757
         TEST_FFI_OK(botan_xof_name, (xof1, out_name.data(), &out_name_len));
1 ✔
2758

2759
         size_t out_block_size;
1 ✔
2760
         TEST_FFI_OK(botan_xof_block_size, (xof1, &out_block_size));
1 ✔
2761
         result.test_sz_eq("valid XOF block size", out_block_size, 168);
1 ✔
2762

2763
         result.test_rc("ready for input", botan_xof_accepts_input(xof1), 1);
1 ✔
2764
         TEST_FFI_OK(botan_xof_update, (xof1, reinterpret_cast<const uint8_t*>(input1), strlen(input1)));
1 ✔
2765
         result.test_rc("still ready for input", botan_xof_accepts_input(xof1), 1);
1 ✔
2766
         TEST_FFI_OK(botan_xof_update, (xof1, reinterpret_cast<const uint8_t*>(input2), strlen(input2)));
1 ✔
2767

2768
         botan_xof_t xof2;
1 ✔
2769
         TEST_FFI_OK(botan_xof_copy_state, (&xof2, xof1));
1 ✔
2770
         result.test_rc("copy still ready for input", botan_xof_accepts_input(xof2), 1);
1 ✔
2771

2772
         std::array<uint8_t, 16> out_bytes{};
1 ✔
2773
         TEST_FFI_OK(botan_xof_output, (xof1, nullptr, 0));
1 ✔
2774
         TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_xof_output, (xof1, nullptr, 1));
1 ✔
2775
         TEST_FFI_OK(botan_xof_output, (xof1, out_bytes.data(), out_bytes.size()));
1 ✔
2776

2777
         result.test_bin_eq("expected first output", out_bytes, "2E870A5FE35999A7B15F9F0BB5AC1689");
1 ✔
2778
         result.test_sz_ne("no more input", botan_xof_accepts_input(xof1), 1);
1 ✔
2779
         TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE,
1 ✔
2780
                     botan_xof_update,
2781
                     (xof1, reinterpret_cast<const uint8_t*>(input1), strlen(input1)));
2782

2783
         TEST_FFI_OK(botan_xof_output, (xof1, out_bytes.data(), out_bytes.size()));
1 ✔
2784
         result.test_bin_eq("expected second output", out_bytes, "E266E213DA0F2763AE29601AB8F9DEDC");
1 ✔
2785

2786
         TEST_FFI_OK(botan_xof_update, (xof2, reinterpret_cast<const uint8_t*>(input3), strlen(input3)));
1 ✔
2787
         TEST_FFI_OK(botan_xof_output, (xof2, out_bytes.data(), out_bytes.size()));
1 ✔
2788
         result.test_bin_eq(
1 ✔
2789
            "expected first output after additional input", out_bytes, "D9D5416188659DDC5C26FCF52E49A157");
2790

2791
         TEST_FFI_OK(botan_xof_clear, (xof1));
1 ✔
2792
         result.test_rc("again ready for input", botan_xof_accepts_input(xof1), 1);
1 ✔
2793
         TEST_FFI_OK(botan_xof_update, (xof1, reinterpret_cast<const uint8_t*>(input1), strlen(input1)));
1 ✔
2794
         TEST_FFI_OK(botan_xof_update, (xof1, reinterpret_cast<const uint8_t*>(input2), strlen(input2)));
1 ✔
2795
         TEST_FFI_OK(botan_xof_update, (xof1, reinterpret_cast<const uint8_t*>(input3), strlen(input3)));
1 ✔
2796
         TEST_FFI_OK(botan_xof_output, (xof1, out_bytes.data(), out_bytes.size()));
1 ✔
2797
         result.test_bin_eq("expected first output with full input", out_bytes, "D9D5416188659DDC5C26FCF52E49A157");
1 ✔
2798

2799
         TEST_FFI_OK(botan_xof_destroy, (xof1));
1 ✔
2800
         TEST_FFI_OK(botan_xof_destroy, (xof2));
1 ✔
2801
      }
2802
};
2803

2804
class FFI_HashFunction_Test final : public FFI_Test {
1 ✔
2805
   public:
2806
      std::string name() const override { return "FFI hash"; }
1 ✔
2807

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

2811
         botan_hash_t hash;
1 ✔
2812
         TEST_FFI_FAIL("invalid hash name", botan_hash_init, (&hash, "SHA-255", 0));
1 ✔
2813
         TEST_FFI_FAIL("invalid flags", botan_hash_init, (&hash, "SHA-256", 1));
1 ✔
2814

2815
         if(TEST_FFI_INIT(botan_hash_init, (&hash, "SHA-256", 0))) {
1 ✔
2816
            std::array<char, 10> namebuf{};
1 ✔
2817
            size_t name_len = 7;
1 ✔
2818
            TEST_FFI_FAIL("output buffer too short", botan_hash_name, (hash, namebuf.data(), &name_len));
1 ✔
2819
            result.test_sz_eq("name len", name_len, 8);
1 ✔
2820

2821
            name_len = namebuf.size();
1 ✔
2822
            if(TEST_FFI_OK(botan_hash_name, (hash, namebuf.data(), &name_len))) {
1 ✔
2823
               result.test_sz_eq("name len", name_len, 8);
1 ✔
2824
               result.test_str_eq("name", namebuf.data(), "SHA-256");
1 ✔
2825
            }
2826

2827
            size_t block_size;
1 ✔
2828
            if(TEST_FFI_OK(botan_hash_block_size, (hash, &block_size))) {
1 ✔
2829
               result.test_sz_eq("hash block size", block_size, 64);
1 ✔
2830
            }
2831

2832
            size_t security_level;
1 ✔
2833
            if(TEST_FFI_OK(botan_hash_security_level, (hash, &security_level))) {
1 ✔
2834
               result.test_sz_eq("hash security level", security_level, 128);
1 ✔
2835
            }
2836

2837
            size_t output_len;
1 ✔
2838
            if(TEST_FFI_OK(botan_hash_output_length, (hash, &output_len))) {
1 ✔
2839
               result.test_sz_eq("hash output length", output_len, 32);
1 ✔
2840

2841
               std::vector<uint8_t> outbuf(output_len);
1 ✔
2842

2843
               // Test that after clear or final the object can be reused
2844
               for(size_t r = 0; r != 2; ++r) {
3 ✔
2845
                  TEST_FFI_OK(botan_hash_update, (hash, reinterpret_cast<const uint8_t*>(input_str), 1));
2 ✔
2846
                  TEST_FFI_OK(botan_hash_clear, (hash));
2 ✔
2847

2848
                  TEST_FFI_OK(botan_hash_update,
2 ✔
2849
                              (hash, reinterpret_cast<const uint8_t*>(input_str), std::strlen(input_str)));
2850
                  TEST_FFI_OK(botan_hash_final, (hash, outbuf.data()));
2 ✔
2851

2852
                  result.test_bin_eq(
2 ✔
2853
                     "SHA-256 output", outbuf, "B5D4045C3F466FA91FE2CC6ABE79232A1A57CDF104F7A26E716E0A1E2789DF78");
2854
               }
2855

2856
               // Test botan_hash_copy_state
2857
               const char* msg = "message digest";
1 ✔
2858
               const char* expected = "F7846F55CF23E14EEBEAB5B4E1550CAD5B509E3348FBC4EFA3A1413D393CB650";
1 ✔
2859
               TEST_FFI_OK(botan_hash_clear, (hash));
1 ✔
2860
               TEST_FFI_OK(botan_hash_update, (hash, reinterpret_cast<const uint8_t*>(&msg[0]), 1));
1 ✔
2861
               botan_hash_t fork;
1 ✔
2862
               if(TEST_FFI_OK(botan_hash_copy_state, (&fork, hash))) {
1 ✔
2863
                  TEST_FFI_OK(botan_hash_update,
1 ✔
2864
                              (fork, reinterpret_cast<const uint8_t*>(&msg[1]), std::strlen(msg) - 2));
2865

2866
                  TEST_FFI_OK(botan_hash_update,
1 ✔
2867
                              (hash, reinterpret_cast<const uint8_t*>(&msg[1]), std::strlen(msg) - 1));
2868
                  TEST_FFI_OK(botan_hash_final, (hash, outbuf.data()));
1 ✔
2869
                  result.test_bin_eq("hashing split", outbuf, expected);
1 ✔
2870

2871
                  TEST_FFI_OK(botan_hash_update,
1 ✔
2872
                              (fork, reinterpret_cast<const uint8_t*>(&msg[std::strlen(msg) - 1]), 1));
2873
                  TEST_FFI_OK(botan_hash_final, (fork, outbuf.data()));
1 ✔
2874
                  result.test_bin_eq("hashing split", outbuf, expected);
1 ✔
2875

2876
                  TEST_FFI_OK(botan_hash_destroy, (fork));
1 ✔
2877
               }
2878
            }
1 ✔
2879

2880
            TEST_FFI_OK(botan_hash_destroy, (hash));
1 ✔
2881
         }
2882
      }
1 ✔
2883
};
2884

2885
class FFI_MAC_Test final : public FFI_Test {
1 ✔
2886
   public:
2887
      std::string name() const override { return "FFI MAC"; }
1 ✔
2888

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

2892
         // MAC test
2893
         botan_mac_t mac;
1 ✔
2894
         TEST_FFI_FAIL("bad flag", botan_mac_init, (&mac, "HMAC(SHA-256)", 1));
1 ✔
2895
         TEST_FFI_FAIL("bad name", botan_mac_init, (&mac, "HMAC(SHA-259)", 0));
1 ✔
2896

2897
         if(TEST_FFI_INIT(botan_mac_init, (&mac, "HMAC(SHA-256)", 0))) {
1 ✔
2898
            std::array<char, 16> namebuf{};
1 ✔
2899
            size_t name_len = 13;
1 ✔
2900
            TEST_FFI_FAIL("output buffer too short", botan_mac_name, (mac, namebuf.data(), &name_len));
1 ✔
2901
            result.test_sz_eq("name len", name_len, 14);
1 ✔
2902

2903
            name_len = namebuf.size();
1 ✔
2904
            if(TEST_FFI_OK(botan_mac_name, (mac, namebuf.data(), &name_len))) {
1 ✔
2905
               result.test_sz_eq("name len", name_len, 14);
1 ✔
2906
               result.test_str_eq("name", namebuf.data(), "HMAC(SHA-256)");
1 ✔
2907
            }
2908

2909
            size_t min_keylen = 0;
1 ✔
2910
            size_t max_keylen = 0;
1 ✔
2911
            size_t mod_keylen = 0;
1 ✔
2912
            TEST_FFI_RC(0, botan_mac_get_keyspec, (mac, nullptr, nullptr, nullptr));
1 ✔
2913
            TEST_FFI_RC(0, botan_mac_get_keyspec, (mac, &min_keylen, nullptr, nullptr));
1 ✔
2914
            TEST_FFI_RC(0, botan_mac_get_keyspec, (mac, nullptr, &max_keylen, nullptr));
1 ✔
2915
            TEST_FFI_RC(0, botan_mac_get_keyspec, (mac, nullptr, nullptr, &mod_keylen));
1 ✔
2916

2917
            result.test_sz_eq("Expected min keylen", min_keylen, 0);
1 ✔
2918
            result.test_sz_eq("Expected max keylen", max_keylen, 8192);
1 ✔
2919
            result.test_sz_eq("Expected mod keylen", mod_keylen, 1);
1 ✔
2920

2921
            size_t output_len;
1 ✔
2922
            if(TEST_FFI_OK(botan_mac_output_length, (mac, &output_len))) {
1 ✔
2923
               result.test_sz_eq("MAC output length", output_len, 32);
1 ✔
2924

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

2928
               // Test that after clear or final the object can be reused
2929
               for(size_t r = 0; r != 2; ++r) {
3 ✔
2930
                  TEST_FFI_OK(botan_mac_set_key, (mac, mac_key, sizeof(mac_key)));
2 ✔
2931
                  TEST_FFI_OK(botan_mac_update,
2 ✔
2932
                              (mac, reinterpret_cast<const uint8_t*>(input_str), std::strlen(input_str)));
2933
                  TEST_FFI_OK(botan_mac_clear, (mac));
2 ✔
2934

2935
                  TEST_FFI_OK(botan_mac_set_key, (mac, mac_key, sizeof(mac_key)));
2 ✔
2936
                  TEST_FFI_OK(botan_mac_update,
2 ✔
2937
                              (mac, reinterpret_cast<const uint8_t*>(input_str), std::strlen(input_str)));
2938
                  TEST_FFI_OK(botan_mac_final, (mac, outbuf.data()));
2 ✔
2939

2940
                  result.test_bin_eq(
2 ✔
2941
                     "HMAC output", outbuf, "1A82EEA984BC4A7285617CC0D05F1FE1D6C96675924A81BC965EE8FF7B0697A7");
2942
               }
2943
            }
1 ✔
2944

2945
            TEST_FFI_OK(botan_mac_destroy, (mac));
1 ✔
2946
         }
2947
      }
1 ✔
2948
};
2949

2950
class FFI_Scrypt_Test final : public FFI_Test {
1 ✔
2951
   public:
2952
      std::string name() const override { return "FFI Scrypt"; }
1 ✔
2953

2954
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
2955
         std::vector<uint8_t> output(24);
1 ✔
2956
         const uint8_t salt[8] = {0};
1 ✔
2957
         const char* pass = "password";
1 ✔
2958

2959
         if(TEST_FFI_INIT(botan_scrypt, (output.data(), output.size(), pass, salt, sizeof(salt), 8, 1, 1))) {
1 ✔
2960
            result.test_bin_eq("scrypt output", output, "4B9B888D695288E002CC4F9D90808A4D296A45CE4471AFBB");
1 ✔
2961

2962
            size_t N;
1 ✔
2963
            size_t r;
1 ✔
2964
            size_t p;
1 ✔
2965
            TEST_FFI_OK(botan_pwdhash_timed,
1 ✔
2966
                        ("Scrypt", 50, &r, &p, &N, output.data(), output.size(), "bunny", 5, salt, sizeof(salt)));
2967

2968
            std::vector<uint8_t> cmp(output.size());
1 ✔
2969

2970
            TEST_FFI_OK(botan_pwdhash, ("Scrypt", N, r, p, cmp.data(), cmp.size(), "bunny", 5, salt, sizeof(salt)));
1 ✔
2971
            result.test_bin_eq("recomputed scrypt", cmp, output);
1 ✔
2972
         }
1 ✔
2973
      }
1 ✔
2974
};
2975

2976
class FFI_KDF_Test final : public FFI_Test {
1 ✔
2977
   public:
2978
      std::string name() const override { return "FFI KDF"; }
1 ✔
2979

2980
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
2981
         std::vector<uint8_t> outbuf;
1 ✔
2982

2983
         const std::string passphrase = "ltexmfeyylmlbrsyikaw";
1 ✔
2984

2985
         const std::vector<uint8_t> pbkdf_salt = Botan::hex_decode("ED1F39A0A7F3889AAF7E60743B3BC1CC2C738E60");
1 ✔
2986
         const size_t pbkdf_out_len = 10;
1 ✔
2987
         const size_t pbkdf_iterations = 1000;
1 ✔
2988

2989
         outbuf.resize(pbkdf_out_len);
1 ✔
2990

2991
         if(TEST_FFI_INIT(botan_pbkdf,
1 ✔
2992
                          ("PBKDF2(SHA-1)",
2993
                           outbuf.data(),
2994
                           outbuf.size(),
2995
                           passphrase.c_str(),
2996
                           pbkdf_salt.data(),
2997
                           pbkdf_salt.size(),
2998
                           pbkdf_iterations))) {
2999
            result.test_bin_eq("PBKDF output", outbuf, "027AFADD48F4BE8DCC4F");
1 ✔
3000

3001
            size_t iters_10ms;
1 ✔
3002
            size_t iters_100ms;
1 ✔
3003

3004
            TEST_FFI_OK(botan_pbkdf_timed,
1 ✔
3005
                        ("PBKDF2(SHA-1)",
3006
                         outbuf.data(),
3007
                         outbuf.size(),
3008
                         passphrase.c_str(),
3009
                         pbkdf_salt.data(),
3010
                         pbkdf_salt.size(),
3011
                         10,
3012
                         &iters_10ms));
3013
            TEST_FFI_OK(botan_pbkdf_timed,
1 ✔
3014
                        ("PBKDF2(SHA-1)",
3015
                         outbuf.data(),
3016
                         outbuf.size(),
3017
                         passphrase.c_str(),
3018
                         pbkdf_salt.data(),
3019
                         pbkdf_salt.size(),
3020
                         100,
3021
                         &iters_100ms));
3022

3023
            result.test_note("PBKDF timed 10 ms " + std::to_string(iters_10ms) + " iterations " + "100 ms " +
6 ✔
3024
                             std::to_string(iters_100ms) + " iterations");
4 ✔
3025
         }
3026

3027
         const std::vector<uint8_t> kdf_secret = Botan::hex_decode("92167440112E");
1 ✔
3028
         const std::vector<uint8_t> kdf_salt = Botan::hex_decode("45A9BEDED69163123D0348F5185F61ABFB1BF18D6AEA454F");
1 ✔
3029
         const size_t kdf_out_len = 18;
1 ✔
3030
         outbuf.resize(kdf_out_len);
1 ✔
3031

3032
         if(TEST_FFI_INIT(botan_kdf,
1 ✔
3033
                          ("KDF2(SHA-1)",
3034
                           outbuf.data(),
3035
                           outbuf.size(),
3036
                           kdf_secret.data(),
3037
                           kdf_secret.size(),
3038
                           kdf_salt.data(),
3039
                           kdf_salt.size(),
3040
                           nullptr,
3041
                           0))) {
3042
            result.test_bin_eq("KDF output", outbuf, "3A5DC9AA1C872B4744515AC2702D6396FC2A");
1 ✔
3043
         }
3044

3045
         size_t out_len = 64;
1 ✔
3046
         std::string outstr;
1 ✔
3047
         outstr.resize(out_len);
1 ✔
3048

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

3052
         if(rc == 0) {
1 ✔
3053
            result.test_sz_eq("bcrypt output size", out_len, 61);
1 ✔
3054

3055
            TEST_FFI_OK(botan_bcrypt_is_valid, (passphrase.c_str(), outstr.data()));
1 ✔
3056
            TEST_FFI_FAIL("bad password", botan_bcrypt_is_valid, ("nope", outstr.data()));
1 ✔
3057
         }
3058
      }
1 ✔
3059
};
3060

3061
class FFI_Blockcipher_Test final : public FFI_Test {
1 ✔
3062
   public:
3063
      std::string name() const override { return "FFI block ciphers"; }
1 ✔
3064

3065
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
3066
         botan_block_cipher_t cipher;
1 ✔
3067

3068
         if(TEST_FFI_INIT(botan_block_cipher_init, (&cipher, "AES-128"))) {
1 ✔
3069
            std::array<char, 10> namebuf{};
1 ✔
3070
            size_t name_len = 7;
1 ✔
3071
            TEST_FFI_FAIL("output buffer too short", botan_block_cipher_name, (cipher, namebuf.data(), &name_len));
1 ✔
3072
            result.test_sz_eq("name len", name_len, 8);
1 ✔
3073

3074
            name_len = namebuf.size();
1 ✔
3075
            if(TEST_FFI_OK(botan_block_cipher_name, (cipher, namebuf.data(), &name_len))) {
1 ✔
3076
               result.test_sz_eq("name len", name_len, 8);
1 ✔
3077
               result.test_str_eq("name", namebuf.data(), "AES-128");
1 ✔
3078
            }
3079

3080
            const std::vector<uint8_t> zero16(16, 0);
1 ✔
3081
            std::vector<uint8_t> block(16, 0);
1 ✔
3082

3083
            TEST_FFI_OK(botan_block_cipher_clear, (cipher));
1 ✔
3084

3085
            TEST_FFI_RC(
1 ✔
3086
               BOTAN_FFI_ERROR_KEY_NOT_SET, botan_block_cipher_encrypt_blocks, (cipher, block.data(), block.data(), 1));
3087
            TEST_FFI_RC(
1 ✔
3088
               BOTAN_FFI_ERROR_KEY_NOT_SET, botan_block_cipher_decrypt_blocks, (cipher, block.data(), block.data(), 1));
3089

3090
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_block_cipher_encrypt_blocks, (cipher, nullptr, nullptr, 0));
1 ✔
3091
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_block_cipher_decrypt_blocks, (cipher, nullptr, nullptr, 0));
1 ✔
3092

3093
            TEST_FFI_RC(16, botan_block_cipher_block_size, (cipher));
1 ✔
3094

3095
            size_t min_keylen = 0;
1 ✔
3096
            size_t max_keylen = 0;
1 ✔
3097
            size_t mod_keylen = 0;
1 ✔
3098
            TEST_FFI_RC(0, botan_block_cipher_get_keyspec, (cipher, nullptr, nullptr, nullptr));
1 ✔
3099
            TEST_FFI_RC(0, botan_block_cipher_get_keyspec, (cipher, &min_keylen, nullptr, nullptr));
1 ✔
3100
            TEST_FFI_RC(0, botan_block_cipher_get_keyspec, (cipher, nullptr, &max_keylen, nullptr));
1 ✔
3101
            TEST_FFI_RC(0, botan_block_cipher_get_keyspec, (cipher, nullptr, nullptr, &mod_keylen));
1 ✔
3102

3103
            result.test_sz_eq("Expected min keylen", min_keylen, 16);
1 ✔
3104
            result.test_sz_eq("Expected max keylen", max_keylen, 16);
1 ✔
3105
            result.test_sz_eq("Expected mod keylen", mod_keylen, 1);
1 ✔
3106

3107
            TEST_FFI_OK(botan_block_cipher_set_key, (cipher, zero16.data(), zero16.size()));
1 ✔
3108

3109
            TEST_FFI_OK(botan_block_cipher_encrypt_blocks, (cipher, block.data(), block.data(), 1));
1 ✔
3110
            result.test_bin_eq("AES-128 encryption works", block, "66E94BD4EF8A2C3B884CFA59CA342B2E");
1 ✔
3111

3112
            TEST_FFI_OK(botan_block_cipher_encrypt_blocks, (cipher, block.data(), block.data(), 1));
1 ✔
3113
            result.test_bin_eq("AES-128 encryption works", block, "F795BD4A52E29ED713D313FA20E98DBC");
1 ✔
3114

3115
            TEST_FFI_OK(botan_block_cipher_decrypt_blocks, (cipher, block.data(), block.data(), 1));
1 ✔
3116
            result.test_bin_eq("AES-128 decryption works", block, "66E94BD4EF8A2C3B884CFA59CA342B2E");
1 ✔
3117

3118
            TEST_FFI_OK(botan_block_cipher_decrypt_blocks, (cipher, block.data(), block.data(), 1));
1 ✔
3119
            result.test_bin_eq("AES-128 decryption works", block, "00000000000000000000000000000000");
1 ✔
3120

3121
            TEST_FFI_OK(botan_block_cipher_clear, (cipher));
1 ✔
3122
            botan_block_cipher_destroy(cipher);
1 ✔
3123
         }
1 ✔
3124

3125
         // The combined block size of this Cascade (the lcm of the two Lion
3126
         // block sizes) is too large to represent in an int, so asking for
3127
         // the block size must return an error rather than a truncated value.
3128
         // With a 32-bit size_t the Cascade itself refuses the construction.
3129
         if constexpr(sizeof(size_t) == 8) {
1 ✔
3130
            botan_block_cipher_t giant = nullptr;
1 ✔
3131
            if(TEST_FFI_INIT(botan_block_cipher_init,
1 ✔
3132
                             (&giant, "Cascade(Lion(SHA-256,ChaCha20,977022),Lion(SHA-256,ChaCha20,1006679))"))) {
3133
               TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE, botan_block_cipher_block_size, (giant));
1 ✔
3134
               botan_block_cipher_destroy(giant);
1 ✔
3135
            }
3136
         }
3137
      }
1 ✔
3138
};
3139

3140
class FFI_ErrorHandling_Test final : public FFI_Test {
1 ✔
3141
   public:
3142
      std::string name() const override { return "FFI error handling"; }
1 ✔
3143

3144
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
3145
         // delete of null is ok/ignored
3146
         TEST_FFI_RC(0, botan_hash_destroy, (nullptr));
1 ✔
3147

3148
   #if !defined(BOTAN_HAS_SANITIZER_UNDEFINED)
3149
         // Confirm that botan_x_destroy checks the argument type
3150
         botan_mp_t mp;
1 ✔
3151
         botan_mp_init(&mp);
1 ✔
3152
         TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT, botan_hash_destroy, (reinterpret_cast<botan_hash_t>(mp)));
1 ✔
3153
         TEST_FFI_RC(0, botan_mp_destroy, (mp));
1 ✔
3154
   #endif
3155

3156
         std::set<std::string> errors;
1 ✔
3157
         for(int i = -100; i != 50; ++i) {
151 ✔
3158
            const char* err = botan_error_description(i);
150 ✔
3159
            result.test_is_true("Never a null pointer", err != nullptr);
150 ✔
3160

3161
            if(err != nullptr) {
150 ✔
3162
               const std::string s(err);
150 ✔
3163

3164
               if(s != "Unknown error") {
150 ✔
3165
                  result.test_is_true("No duplicate messages", !errors.contains(s));
22 ✔
3166
                  errors.insert(s);
22 ✔
3167
               }
3168
            }
150 ✔
3169
         }
3170
      }
1 ✔
3171
};
3172

3173
class FFI_Base64_Test final : public FFI_Test {
1 ✔
3174
   public:
3175
      std::string name() const override { return "FFI base64"; }
1 ✔
3176

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

3181
         size_t out_len = sizeof(out_buf);
1 ✔
3182
         TEST_FFI_OK(botan_base64_encode, (bin, sizeof(bin), out_buf, &out_len));
1 ✔
3183

3184
         result.test_str_eq("encoded string", out_buf, "FoofBunny900");
1 ✔
3185

3186
         out_len -= 1;
1 ✔
3187
         TEST_FFI_RC(
1 ✔
3188
            BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_base64_encode, (bin, sizeof(bin), out_buf, &out_len));
3189

3190
         const char* base64 = "U3VjaCBiYXNlNjQgd293IQ==";
1 ✔
3191
         uint8_t out_bin[1024] = {0};
1 ✔
3192

3193
         out_len = 3;
1 ✔
3194
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1 ✔
3195
                     botan_base64_decode,
3196
                     (base64, strlen(base64), out_bin, &out_len));
3197

3198
         result.test_sz_eq("output length", out_len, 18);
1 ✔
3199

3200
         out_len = sizeof(out_bin);
1 ✔
3201
         TEST_FFI_OK(botan_base64_decode, (base64, strlen(base64), out_bin, &out_len));
1 ✔
3202

3203
         result.test_str_eq(
1 ✔
3204
            "decoded string", std::string(reinterpret_cast<const char*>(out_bin), out_len), "Such base64 wow!");
1 ✔
3205
      }
1 ✔
3206
};
3207

3208
class FFI_Hex_Test final : public FFI_Test {
1 ✔
3209
   public:
3210
      std::string name() const override { return "FFI hex"; }
1 ✔
3211

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

3216
         TEST_FFI_OK(botan_hex_encode, (bin, sizeof(bin), hex_buf, 0));
1 ✔
3217

3218
         result.test_str_eq("encoded string", hex_buf, "DEADBEEF");
1 ✔
3219

3220
         const char* hex = "67657420796572206A756D626F20736872696D70";
1 ✔
3221
         uint8_t out_bin[1024] = {0};
1 ✔
3222
         size_t out_len = 5;
1 ✔
3223

3224
         TEST_FFI_RC(
1 ✔
3225
            BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_hex_decode, (hex, strlen(hex), out_bin, &out_len));
3226

3227
         out_len = sizeof(out_bin);
1 ✔
3228
         TEST_FFI_OK(botan_hex_decode, (hex, strlen(hex), out_bin, &out_len));
1 ✔
3229

3230
         result.test_str_eq(
1 ✔
3231
            "decoded string", std::string(reinterpret_cast<const char*>(out_bin), out_len), "get yer jumbo shrimp");
1 ✔
3232
      }
1 ✔
3233
};
3234

3235
class FFI_MP_Test final : public FFI_Test {
1 ✔
3236
   public:
3237
      std::string name() const override { return "FFI MP"; }
1 ✔
3238

3239
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
3240
         char str_buf[1024] = {0};
1 ✔
3241
         size_t str_len = 0;
1 ✔
3242

3243
         botan_mp_t x;
1 ✔
3244
         botan_mp_init(&x);
1 ✔
3245
         TEST_FFI_RC(0, botan_mp_is_odd, (x));
1 ✔
3246
         TEST_FFI_RC(1, botan_mp_is_even, (x));
1 ✔
3247
         TEST_FFI_RC(0, botan_mp_is_negative, (x));
1 ✔
3248
         TEST_FFI_RC(1, botan_mp_is_positive, (x));
1 ✔
3249
         TEST_FFI_RC(1, botan_mp_is_zero, (x));
1 ✔
3250
         botan_mp_destroy(x);
1 ✔
3251

3252
         botan_mp_init(&x);
1 ✔
3253
         size_t bn_bytes = 0;
1 ✔
3254
         TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes));
1 ✔
3255
         result.test_sz_eq("Expected size for MP 0", bn_bytes, 0);
1 ✔
3256

3257
         botan_mp_set_from_int(x, 5);
1 ✔
3258
         TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes));
1 ✔
3259
         result.test_sz_eq("Expected size for MP 5", bn_bytes, 1);
1 ✔
3260

3261
         botan_mp_add_u32(x, x, 75);
1 ✔
3262
         TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes));
1 ✔
3263
         result.test_sz_eq("Expected size for MP 80", bn_bytes, 1);
1 ✔
3264

3265
         str_len = sizeof(str_buf);
1 ✔
3266
         TEST_FFI_OK(botan_mp_to_str, (x, 10, str_buf, &str_len));
1 ✔
3267
         result.test_str_eq("botan_mp_add", str_buf, "80");
1 ✔
3268

3269
         botan_mp_sub_u32(x, x, 80);
1 ✔
3270
         TEST_FFI_RC(1, botan_mp_is_zero, (x));
1 ✔
3271
         botan_mp_add_u32(x, x, 259);
1 ✔
3272
         TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes));
1 ✔
3273
         result.test_sz_eq("Expected size for MP 259", bn_bytes, 2);
1 ✔
3274

3275
         str_len = sizeof(str_buf);
1 ✔
3276
         TEST_FFI_OK(botan_mp_to_str, (x, 10, str_buf, &str_len));
1 ✔
3277
         result.test_str_eq("botan_mp_add", str_buf, "259");
1 ✔
3278

3279
         TEST_FFI_RC(1, botan_mp_is_odd, (x));
1 ✔
3280
         TEST_FFI_RC(0, botan_mp_is_even, (x));
1 ✔
3281
         TEST_FFI_RC(0, botan_mp_is_negative, (x));
1 ✔
3282
         TEST_FFI_RC(1, botan_mp_is_positive, (x));
1 ✔
3283
         TEST_FFI_RC(0, botan_mp_is_zero, (x));
1 ✔
3284

3285
         {
1 ✔
3286
            botan_mp_t zero;
1 ✔
3287
            botan_mp_init(&zero);
1 ✔
3288
            int cmp;
1 ✔
3289
            TEST_FFI_OK(botan_mp_cmp, (&cmp, x, zero));
1 ✔
3290
            result.test_is_true("bigint_mp_cmp(+, 0)", cmp == 1);
1 ✔
3291

3292
            TEST_FFI_OK(botan_mp_cmp, (&cmp, zero, x));
1 ✔
3293
            result.test_is_true("bigint_mp_cmp(0, +)", cmp == -1);
1 ✔
3294

3295
            TEST_FFI_RC(0, botan_mp_is_negative, (x));
1 ✔
3296
            TEST_FFI_RC(1, botan_mp_is_positive, (x));
1 ✔
3297
            TEST_FFI_OK(botan_mp_flip_sign, (x));
1 ✔
3298
            TEST_FFI_RC(1, botan_mp_is_negative, (x));
1 ✔
3299
            TEST_FFI_RC(0, botan_mp_is_positive, (x));
1 ✔
3300

3301
            // test no negative zero
3302
            TEST_FFI_RC(0, botan_mp_is_negative, (zero));
1 ✔
3303
            TEST_FFI_RC(1, botan_mp_is_positive, (zero));
1 ✔
3304
            TEST_FFI_OK(botan_mp_flip_sign, (zero));
1 ✔
3305
            TEST_FFI_RC(0, botan_mp_is_negative, (zero));
1 ✔
3306
            TEST_FFI_RC(1, botan_mp_is_positive, (zero));
1 ✔
3307

3308
            TEST_FFI_OK(botan_mp_cmp, (&cmp, x, zero));
1 ✔
3309
            result.test_is_true("bigint_mp_cmp(-, 0)", cmp == -1);
1 ✔
3310

3311
            TEST_FFI_OK(botan_mp_cmp, (&cmp, zero, x));
1 ✔
3312
            result.test_is_true("bigint_mp_cmp(0, -)", cmp == 1);
1 ✔
3313

3314
            TEST_FFI_OK(botan_mp_cmp, (&cmp, zero, zero));
1 ✔
3315
            result.test_is_true("bigint_mp_cmp(0, 0)", cmp == 0);
1 ✔
3316

3317
            TEST_FFI_OK(botan_mp_cmp, (&cmp, x, x));
1 ✔
3318
            result.test_is_true("bigint_mp_cmp(x, x)", cmp == 0);
1 ✔
3319

3320
            TEST_FFI_OK(botan_mp_flip_sign, (x));
1 ✔
3321

3322
            // Regression test for bug reported by @hgarrereyn
3323
            // See: GH #5128
3324
            botan_mp_t out_shift;
1 ✔
3325
            TEST_FFI_OK(botan_mp_init, (&out_shift));
1 ✔
3326
            TEST_FFI_OK(botan_mp_lshift, (out_shift, zero, 0));
1 ✔
3327

3328
            botan_mp_destroy(zero);
1 ✔
3329
            botan_mp_destroy(out_shift);
1 ✔
3330
         }
3331

3332
         size_t x_bits = 0;
1 ✔
3333
         TEST_FFI_OK(botan_mp_num_bits, (x, &x_bits));
1 ✔
3334
         result.test_sz_eq("botan_mp_num_bits", x_bits, 9);
1 ✔
3335

3336
         TEST_FFI_OK(botan_mp_to_hex, (x, str_buf));
1 ✔
3337
         result.test_str_eq("botan_mp_to_hex", str_buf, "0x0103");
1 ✔
3338

3339
         ViewStringSink hex_sink;
1 ✔
3340
         TEST_FFI_OK(botan_mp_view_hex, (x, hex_sink.delegate(), hex_sink.callback()));
1 ✔
3341
         result.test_str_eq("botan_mp_view_hex", hex_sink.get(), "0x0103");
1 ✔
3342

3343
         ViewStringSink str_sink;
1 ✔
3344
         TEST_FFI_OK(botan_mp_view_str, (x, 10, str_sink.delegate(), str_sink.callback()));
1 ✔
3345
         result.test_str_eq("botan_mp_view_str", str_sink.get(), "259");
1 ✔
3346

3347
         ViewBytesSink bin_sink;
1 ✔
3348
         TEST_FFI_OK(botan_mp_view_bin, (x, bin_sink.delegate(), bin_sink.callback()));
1 ✔
3349
         result.test_bin_eq("botan_mp_view_str", bin_sink.get(), "0103");
1 ✔
3350

3351
         uint32_t x_32;
1 ✔
3352
         TEST_FFI_OK(botan_mp_to_uint32, (x, &x_32));
1 ✔
3353
         result.test_sz_eq("botan_mp_to_uint32", size_t(x_32), size_t(0x103));
1 ✔
3354

3355
         TEST_FFI_RC(1, botan_mp_get_bit, (x, 1));
1 ✔
3356
         TEST_FFI_RC(0, botan_mp_get_bit, (x, 87));
1 ✔
3357
         TEST_FFI_OK(botan_mp_set_bit, (x, 87));
1 ✔
3358
         TEST_FFI_RC(1, botan_mp_get_bit, (x, 87));
1 ✔
3359
         TEST_FFI_OK(botan_mp_to_hex, (x, str_buf));
1 ✔
3360
         result.test_str_eq("botan_mp_set_bit", str_buf, "0x8000000000000000000103");
1 ✔
3361

3362
         TEST_FFI_OK(botan_mp_clear_bit, (x, 87));
1 ✔
3363
         TEST_FFI_OK(botan_mp_to_hex, (x, str_buf));
1 ✔
3364
         result.test_str_eq("botan_mp_set_bit", str_buf, "0x0103");
1 ✔
3365

3366
         botan_mp_t y;
1 ✔
3367
         TEST_FFI_OK(botan_mp_init, (&y));
1 ✔
3368
         TEST_FFI_OK(botan_mp_set_from_int, (y, 0x1234567));
1 ✔
3369

3370
         botan_mp_t r;
1 ✔
3371
         botan_mp_init(&r);
1 ✔
3372

3373
         TEST_FFI_OK(botan_mp_add, (r, x, y));
1 ✔
3374
         str_len = sizeof(str_buf);
1 ✔
3375
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1 ✔
3376
         result.test_str_eq("botan_mp_add", str_buf, "19089002");
1 ✔
3377

3378
         TEST_FFI_OK(botan_mp_mul, (r, x, y));
1 ✔
3379
         str_len = sizeof(str_buf);
1 ✔
3380
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1 ✔
3381
         result.test_str_eq("botan_mp_mul", str_buf, "4943984437");
1 ✔
3382
         TEST_FFI_RC(0, botan_mp_is_negative, (r));
1 ✔
3383

3384
         botan_mp_t q;
1 ✔
3385
         botan_mp_init(&q);
1 ✔
3386
         TEST_FFI_OK(botan_mp_div, (q, r, y, x));
1 ✔
3387

3388
         str_len = sizeof(str_buf);
1 ✔
3389
         TEST_FFI_OK(botan_mp_to_str, (q, 10, str_buf, &str_len));
1 ✔
3390
         result.test_str_eq("botan_mp_div_q", str_buf, "73701");
1 ✔
3391

3392
         str_len = sizeof(str_buf);
1 ✔
3393
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1 ✔
3394
         result.test_str_eq("botan_mp_div_r", str_buf, "184");
1 ✔
3395

3396
         TEST_FFI_OK(botan_mp_set_from_str, (y, "4943984437"));
1 ✔
3397
         TEST_FFI_OK(botan_mp_sub, (r, x, y));
1 ✔
3398
         str_len = sizeof(str_buf);
1 ✔
3399
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1 ✔
3400
         result.test_str_eq("botan_mp_sub", str_buf, "-4943984178");
1 ✔
3401
         TEST_FFI_RC(1, botan_mp_is_negative, (r));
1 ✔
3402

3403
         TEST_FFI_OK(botan_mp_lshift, (r, x, 39));
1 ✔
3404
         str_len = sizeof(str_buf);
1 ✔
3405
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1 ✔
3406
         result.test_str_eq("botan_mp_lshift", str_buf, "142386755796992");
1 ✔
3407

3408
         TEST_FFI_OK(botan_mp_rshift, (r, r, 3));
1 ✔
3409
         str_len = sizeof(str_buf);
1 ✔
3410
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1 ✔
3411
         result.test_str_eq("botan_mp_rshift", str_buf, "17798344474624");
1 ✔
3412

3413
         TEST_FFI_OK(botan_mp_gcd, (r, x, y));
1 ✔
3414
         str_len = sizeof(str_buf);
1 ✔
3415
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1 ✔
3416
         result.test_str_eq("botan_mp_gcd", str_buf, "259");
1 ✔
3417

3418
         botan_mp_t p;
1 ✔
3419
         botan_mp_init(&p);
1 ✔
3420
         const uint8_t M127[] = {
1 ✔
3421
            0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
3422
         TEST_FFI_OK(botan_mp_from_bin, (p, M127, sizeof(M127)));
1 ✔
3423
         TEST_FFI_RC(1, botan_mp_is_prime, (p, rng, 64));
1 ✔
3424

3425
         size_t p_bits = 0;
1 ✔
3426
         TEST_FFI_OK(botan_mp_num_bits, (p, &p_bits));
1 ✔
3427
         result.test_sz_eq("botan_mp_num_bits", p_bits, 127);
1 ✔
3428

3429
         TEST_FFI_OK(botan_mp_mod_inverse, (r, x, p));
1 ✔
3430
         str_len = sizeof(str_buf);
1 ✔
3431
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1 ✔
3432
         result.test_str_eq("botan_mp_mod_inverse", str_buf, "40728777507911553541948312086427855425");
1 ✔
3433

3434
         TEST_FFI_OK(botan_mp_powmod, (r, x, r, p));
1 ✔
3435
         str_len = sizeof(str_buf);
1 ✔
3436
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1 ✔
3437
         result.test_str_eq("botan_mp_powmod", str_buf, "40550417419160441638948180641668117560");
1 ✔
3438

3439
         TEST_FFI_OK(botan_mp_num_bytes, (r, &bn_bytes));
1 ✔
3440
         result.test_sz_eq("botan_mp_num_bytes", bn_bytes, 16);
1 ✔
3441

3442
         std::vector<uint8_t> bn_buf;
1 ✔
3443
         bn_buf.resize(bn_bytes);
1 ✔
3444
         botan_mp_to_bin(r, bn_buf.data());
1 ✔
3445
         result.test_bin_eq("botan_mp_to_bin", bn_buf, "1E81B9EFE0BE1902F6D03F9F5E5FB438");
1 ✔
3446

3447
         TEST_FFI_OK(botan_mp_set_from_mp, (y, r));
1 ✔
3448
         TEST_FFI_OK(botan_mp_mod_mul, (r, x, y, p));
1 ✔
3449
         str_len = sizeof(str_buf);
1 ✔
3450
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1 ✔
3451
         result.test_str_eq("botan_mp_mod_mul", str_buf, "123945920473931248854653259523111998693");
1 ✔
3452

3453
         // An even modulus is accepted, and the output may alias the modulus
3454
         botan_mp_t m;
1 ✔
3455
         botan_mp_init(&m);
1 ✔
3456
         TEST_FFI_OK(botan_mp_set_from_str, (m, "340282366920938463463374607431768211456"));
1 ✔
3457
         TEST_FFI_OK(botan_mp_mod_mul, (m, x, y, m));
1 ✔
3458
         str_len = sizeof(str_buf);
1 ✔
3459
         TEST_FFI_OK(botan_mp_to_str, (m, 10, str_buf, &str_len));
1 ✔
3460
         result.test_str_eq("botan_mp_mod_mul even modulus", str_buf, "294087103934400480586340563238996104360");
1 ✔
3461
         botan_mp_destroy(m);
1 ✔
3462

3463
         // The output may alias an input
3464
         TEST_FFI_OK(botan_mp_mod_mul, (y, x, y, p));
1 ✔
3465
         TEST_FFI_RC(1, botan_mp_equal, (y, r));
1 ✔
3466

3467
         // Inputs must be in [0, p)
3468
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_mp_mod_mul, (r, p, x, p));
1 ✔
3469
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_mp_mod_mul, (r, x, p, p));
1 ✔
3470
         TEST_FFI_OK(botan_mp_flip_sign, (y));
1 ✔
3471
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_mp_mod_mul, (r, x, y, p));
1 ✔
3472

3473
         str_len = 0;
1 ✔
3474
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_mp_to_str, (r, 10, str_buf, &str_len));
1 ✔
3475

3476
         size_t x_bytes;
1 ✔
3477
         botan_mp_rand_bits(x, rng, 512);
1 ✔
3478
         TEST_FFI_OK(botan_mp_num_bytes, (x, &x_bytes));
1 ✔
3479
         result.test_sz_lte("botan_mp_num_bytes", x_bytes, 512 / 8);
1 ✔
3480

3481
         TEST_FFI_OK(botan_mp_set_from_radix_str, (x, "909A", 16));
1 ✔
3482
         TEST_FFI_OK(botan_mp_to_uint32, (x, &x_32));
1 ✔
3483
         result.test_u32_eq("botan_mp_set_from_radix_str(16)", x_32, 0x909A);
1 ✔
3484

3485
         TEST_FFI_OK(botan_mp_set_from_radix_str, (x, "9098135", 10));
1 ✔
3486
         TEST_FFI_OK(botan_mp_to_uint32, (x, &x_32));
1 ✔
3487
         result.test_u32_eq("botan_mp_set_from_radix_str(10)", x_32, 9098135);
1 ✔
3488

3489
         botan_mp_destroy(p);
1 ✔
3490
         botan_mp_destroy(x);
1 ✔
3491
         botan_mp_destroy(y);
1 ✔
3492
         botan_mp_destroy(r);
1 ✔
3493
         botan_mp_destroy(q);
1 ✔
3494
      }
1 ✔
3495
};
3496

3497
class FFI_FPE_Test final : public FFI_Test {
1 ✔
3498
   public:
3499
      std::string name() const override { return "FFI FPE"; }
1 ✔
3500

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

3504
         botan_mp_t n;
1 ✔
3505
         botan_mp_init(&n);
1 ✔
3506
         botan_mp_set_from_str(n, "1000000000");
1 ✔
3507

3508
         botan_fpe_t fpe;
1 ✔
3509
         if(!TEST_FFI_INIT(botan_fpe_fe1_init, (&fpe, n, key, sizeof(key), 5, 0))) {
1 ✔
3510
            botan_mp_destroy(n);
×
3511
            return;
×
3512
         }
3513

3514
         botan_mp_t x;
1 ✔
3515
         botan_mp_init(&x);
1 ✔
3516
         botan_mp_set_from_str(x, "178051120");
1 ✔
3517

3518
         TEST_FFI_OK(botan_fpe_encrypt, (fpe, x, nullptr, 0));
1 ✔
3519

3520
         uint32_t xval = 0;
1 ✔
3521
         TEST_FFI_OK(botan_mp_to_uint32, (x, &xval));
1 ✔
3522
         result.test_sz_eq("Expected FPE ciphertext", xval, size_t(605648666));
1 ✔
3523

3524
         TEST_FFI_OK(botan_fpe_encrypt, (fpe, x, nullptr, 0));
1 ✔
3525
         TEST_FFI_OK(botan_fpe_decrypt, (fpe, x, nullptr, 0));
1 ✔
3526
         TEST_FFI_OK(botan_fpe_decrypt, (fpe, x, nullptr, 0));
1 ✔
3527

3528
         TEST_FFI_OK(botan_mp_to_uint32, (x, &xval));
1 ✔
3529
         result.test_sz_eq("FPE round trip", xval, size_t(178051120));
1 ✔
3530

3531
         TEST_FFI_OK(botan_fpe_destroy, (fpe));
1 ✔
3532
         TEST_FFI_OK(botan_mp_destroy, (x));
1 ✔
3533
         TEST_FFI_OK(botan_mp_destroy, (n));
1 ✔
3534
      }
3535
};
3536

3537
class FFI_TOTP_Test final : public FFI_Test {
1 ✔
3538
   public:
3539
      std::string name() const override { return "FFI TOTP"; }
1 ✔
3540

3541
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
3542
         const std::vector<uint8_t> key = Botan::hex_decode("3132333435363738393031323334353637383930");
1 ✔
3543
         const size_t digits = 8;
1 ✔
3544
         const size_t timestep = 30;
1 ✔
3545
         botan_totp_t totp;
1 ✔
3546

3547
         if(!TEST_FFI_INIT(botan_totp_init, (&totp, key.data(), key.size(), "SHA-1", digits, timestep))) {
1 ✔
3548
            return;
×
3549
         }
3550

3551
         uint32_t code;
1 ✔
3552

3553
         const uint64_t timestamp = 1000216740;
1 ✔
3554
         TEST_FFI_OK(botan_totp_generate, (totp, &code, timestamp));
1 ✔
3555
         result.test_u32_eq("TOTP code", code, 34097298);
1 ✔
3556

3557
         TEST_FFI_OK(botan_totp_generate, (totp, &code, 1111111109));
1 ✔
3558
         result.test_u32_eq("TOTP code 2", code, 7081804);
1 ✔
3559

3560
         TEST_FFI_OK(botan_totp_check, (totp, 34097298, timestamp + 60, 2));
1 ✔
3561
         TEST_FFI_RC(1, botan_totp_check, (totp, 34097298, timestamp + 61, 1));
1 ✔
3562

3563
         TEST_FFI_OK(botan_totp_destroy, (totp));
1 ✔
3564
      }
1 ✔
3565
};
3566

3567
class FFI_HOTP_Test final : public FFI_Test {
1 ✔
3568
   public:
3569
      std::string name() const override { return "FFI HOTP"; }
1 ✔
3570

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

3575
         botan_hotp_t hotp;
1 ✔
3576
         uint32_t hotp_val;
1 ✔
3577

3578
         if(!TEST_FFI_INIT(botan_hotp_init, (&hotp, key.data(), key.size(), "SHA-1", digits))) {
1 ✔
3579
            return;
×
3580
         }
3581

3582
         TEST_FFI_OK(botan_hotp_generate, (hotp, &hotp_val, 0));
1 ✔
3583
         result.test_u32_eq("Valid value for counter 0", hotp_val, 755224);
1 ✔
3584
         TEST_FFI_OK(botan_hotp_generate, (hotp, &hotp_val, 1));
1 ✔
3585
         result.test_u32_eq("Valid value for counter 0", hotp_val, 287082);
1 ✔
3586
         TEST_FFI_OK(botan_hotp_generate, (hotp, &hotp_val, 2));
1 ✔
3587
         result.test_u32_eq("Valid value for counter 0", hotp_val, 359152);
1 ✔
3588
         TEST_FFI_OK(botan_hotp_generate, (hotp, &hotp_val, 0));
1 ✔
3589
         result.test_u32_eq("Valid value for counter 0", hotp_val, 755224);
1 ✔
3590

3591
         uint64_t next_ctr = 0;
1 ✔
3592

3593
         TEST_FFI_OK(botan_hotp_check, (hotp, &next_ctr, 755224, 0, 0));
1 ✔
3594
         result.test_u64_eq("HOTP resync", next_ctr, 1);
1 ✔
3595
         TEST_FFI_OK(botan_hotp_check, (hotp, nullptr, 359152, 2, 0));
1 ✔
3596
         TEST_FFI_RC(1, botan_hotp_check, (hotp, nullptr, 359152, 1, 0));
1 ✔
3597
         TEST_FFI_OK(botan_hotp_check, (hotp, &next_ctr, 359152, 0, 2));
1 ✔
3598
         result.test_u64_eq("HOTP resync", next_ctr, 3);
1 ✔
3599

3600
         TEST_FFI_OK(botan_hotp_destroy, (hotp));
1 ✔
3601
      }
1 ✔
3602
};
3603

3604
class FFI_Keywrap_Test final : public FFI_Test {
1 ✔
3605
   public:
3606
      std::string name() const override { return "FFI Keywrap"; }
1 ✔
3607

3608
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
3609
         const uint8_t key[16] = {0};
1 ✔
3610
         const uint8_t kek[16] = {0xFF, 0};
1 ✔
3611

3612
         uint8_t wrapped[16 + 8] = {0};
1 ✔
3613
         size_t wrapped_keylen = sizeof(wrapped);
1 ✔
3614

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

3620
            result.test_sz_eq("Expected wrapped keylen size", wrapped_keylen, 16 + 8);
1 ✔
3621

3622
            result.test_bin_eq(
1 ✔
3623
               "Wrapped key", {wrapped, wrapped_keylen}, {expected_wrapped_key, sizeof(expected_wrapped_key)});
3624

3625
            uint8_t dec_key[16] = {0};
1 ✔
3626
            size_t dec_keylen = sizeof(dec_key);
1 ✔
3627
            TEST_FFI_OK(botan_key_unwrap3394, (wrapped, sizeof(wrapped), kek, sizeof(kek), dec_key, &dec_keylen));
1 ✔
3628

3629
            result.test_bin_eq("Unwrapped key", {dec_key, dec_keylen}, {key, sizeof(key)});
1 ✔
3630
         }
3631
      }
1 ✔
3632
};
3633

3634
class FFI_XMSS_Test final : public FFI_Test {
1 ✔
3635
   public:
3636
      std::string name() const override { return "FFI XMSS"; }
1 ✔
3637

3638
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
3639
         botan_privkey_t priv;
1 ✔
3640
         if(TEST_FFI_INIT(botan_privkey_create, (&priv, "XMSS", "XMSS-SHA2_10_256", rng))) {
1 ✔
3641
            TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
1 ✔
3642

3643
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_privkey_stateful_operation, (priv, nullptr));
1 ✔
3644
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_privkey_remaining_operations, (priv, nullptr));
1 ✔
3645

3646
            int stateful;
1 ✔
3647
            TEST_FFI_OK(botan_privkey_stateful_operation, (priv, &stateful));
1 ✔
3648
            result.test_is_true("key is stateful", stateful == 1);
1 ✔
3649

3650
            uint64_t remaining;
1 ✔
3651
            TEST_FFI_OK(botan_privkey_remaining_operations, (priv, &remaining));
1 ✔
3652
            result.test_u64_eq("key has remaining operations", remaining, 1024);
1 ✔
3653

3654
            TEST_FFI_OK(botan_privkey_destroy, (priv));
1 ✔
3655
         }
3656
      }
1 ✔
3657
};
3658

3659
class FFI_RSA_Test final : public FFI_Test {
1 ✔
3660
   public:
3661
      std::string name() const override { return "FFI RSA"; }
1 ✔
3662

3663
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
3664
         botan_privkey_t priv;
1 ✔
3665

3666
         if(TEST_FFI_INIT(botan_privkey_create_rsa, (&priv, rng, 1024))) {
1 ✔
3667
            TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
1 ✔
3668

3669
            int stateful;
1 ✔
3670
            TEST_FFI_OK(botan_privkey_stateful_operation, (priv, &stateful));
1 ✔
3671
            result.test_is_true("key is not stateful", stateful == 0);
1 ✔
3672

3673
            uint64_t remaining;
1 ✔
3674
            TEST_FFI_FAIL("key is not stateful", botan_privkey_remaining_operations, (priv, &remaining));
1 ✔
3675

3676
            botan_pubkey_t pub;
1 ✔
3677
            TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1 ✔
3678
            TEST_FFI_OK(botan_pubkey_check_key, (pub, rng, 0));
1 ✔
3679

3680
            ffi_test_pubkey_export(result, pub, priv, rng);
1 ✔
3681

3682
            botan_mp_t p;
1 ✔
3683
            botan_mp_t q;
1 ✔
3684
            botan_mp_t d;
1 ✔
3685
            botan_mp_t n;
1 ✔
3686
            botan_mp_t e;
1 ✔
3687
            botan_mp_init(&p);
1 ✔
3688
            botan_mp_init(&q);
1 ✔
3689
            botan_mp_init(&d);
1 ✔
3690
            botan_mp_init(&n);
1 ✔
3691
            botan_mp_init(&e);
1 ✔
3692

3693
            TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_privkey_get_field, (p, priv, "quux"));
1 ✔
3694
            TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_pubkey_get_field, (p, pub, "quux"));
1 ✔
3695

3696
            TEST_FFI_OK(botan_privkey_rsa_get_p, (p, priv));
1 ✔
3697
            TEST_FFI_OK(botan_privkey_rsa_get_q, (q, priv));
1 ✔
3698
            TEST_FFI_OK(botan_privkey_rsa_get_d, (d, priv));
1 ✔
3699
            TEST_FFI_OK(botan_privkey_rsa_get_e, (e, priv));
1 ✔
3700
            TEST_FFI_OK(botan_privkey_rsa_get_n, (n, priv));
1 ✔
3701

3702
            // Confirm same (e,n) values in public key
3703
            {
1 ✔
3704
               botan_mp_t pub_e;
1 ✔
3705
               botan_mp_t pub_n;
1 ✔
3706
               botan_mp_init(&pub_e);
1 ✔
3707
               botan_mp_init(&pub_n);
1 ✔
3708
               TEST_FFI_OK(botan_pubkey_rsa_get_e, (pub_e, pub));
1 ✔
3709
               TEST_FFI_OK(botan_pubkey_rsa_get_n, (pub_n, pub));
1 ✔
3710

3711
               TEST_FFI_RC(1, botan_mp_equal, (pub_e, e));
1 ✔
3712
               TEST_FFI_RC(1, botan_mp_equal, (pub_n, n));
1 ✔
3713
               botan_mp_destroy(pub_e);
1 ✔
3714
               botan_mp_destroy(pub_n);
1 ✔
3715
            }
3716

3717
            TEST_FFI_RC(1, botan_mp_is_prime, (p, rng, 64));
1 ✔
3718
            TEST_FFI_RC(1, botan_mp_is_prime, (q, rng, 64));
1 ✔
3719

3720
            // Test p != q
3721
            TEST_FFI_RC(0, botan_mp_equal, (p, q));
1 ✔
3722

3723
            // Test p * q == n
3724
            botan_mp_t x;
1 ✔
3725
            botan_mp_init(&x);
1 ✔
3726
            TEST_FFI_OK(botan_mp_mul, (x, p, q));
1 ✔
3727

3728
            TEST_FFI_RC(1, botan_mp_equal, (x, n));
1 ✔
3729
            botan_mp_destroy(x);
1 ✔
3730

3731
            botan_privkey_t loaded_privkey;
1 ✔
3732
            // First try loading a bogus key and verify it is rejected
3733
            TEST_FFI_RC(-1, botan_privkey_load_rsa, (&loaded_privkey, n, d, q));
1 ✔
3734

3735
            TEST_FFI_OK(botan_privkey_load_rsa, (&loaded_privkey, p, q, e));
1 ✔
3736
            TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
1 ✔
3737

3738
            botan_pubkey_t loaded_pubkey;
1 ✔
3739
            TEST_FFI_OK(botan_pubkey_load_rsa, (&loaded_pubkey, n, e));
1 ✔
3740
            TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey, rng, 0));
1 ✔
3741

3742
            botan_mp_destroy(p);
1 ✔
3743
            botan_mp_destroy(q);
1 ✔
3744
            botan_mp_destroy(d);
1 ✔
3745
            botan_mp_destroy(e);
1 ✔
3746
            botan_mp_destroy(n);
1 ✔
3747

3748
            size_t pkcs1_len = 0;
1 ✔
3749
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1 ✔
3750
                        botan_privkey_rsa_get_privkey,
3751
                        (loaded_privkey, nullptr, &pkcs1_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
3752

3753
            std::vector<uint8_t> pkcs1(pkcs1_len);
1 ✔
3754
            TEST_FFI_OK(botan_privkey_rsa_get_privkey,
1 ✔
3755
                        (loaded_privkey, pkcs1.data(), &pkcs1_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
3756

3757
            botan_privkey_t privkey_from_pkcs1;
1 ✔
3758
            TEST_FFI_OK(botan_privkey_load_rsa_pkcs1, (&privkey_from_pkcs1, pkcs1.data(), pkcs1_len));
1 ✔
3759
            TEST_FFI_OK(botan_privkey_destroy, (privkey_from_pkcs1));
1 ✔
3760

3761
            pkcs1_len = 0;
1 ✔
3762
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1 ✔
3763
                        botan_privkey_rsa_get_privkey,
3764
                        (loaded_privkey, nullptr, &pkcs1_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
3765
            pkcs1.resize(pkcs1_len);
1 ✔
3766
            TEST_FFI_OK(botan_privkey_rsa_get_privkey,
1 ✔
3767
                        (loaded_privkey, pkcs1.data(), &pkcs1_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
3768

3769
            std::array<char, 32> namebuf{};
1 ✔
3770
            size_t name_len = namebuf.size();
1 ✔
3771
            if(TEST_FFI_OK(botan_pubkey_algo_name, (loaded_pubkey, namebuf.data(), &name_len))) {
1 ✔
3772
               result.test_str_eq("algo name", namebuf.data(), "RSA");
1 ✔
3773
            }
3774

3775
            name_len = namebuf.size();
1 ✔
3776
            if(TEST_FFI_OK(botan_privkey_algo_name, (loaded_privkey, namebuf.data(), &name_len))) {
1 ✔
3777
               result.test_str_eq("algo name", namebuf.data(), "RSA");
1 ✔
3778
            }
3779

3780
            botan_pk_op_encrypt_t encrypt;
1 ✔
3781
            if(TEST_FFI_INIT(botan_pk_op_encrypt_create, (&encrypt, loaded_pubkey, "OAEP(SHA-256)", 0))) {
1 ✔
3782
               std::vector<uint8_t> plaintext(32);
1 ✔
3783
               TEST_FFI_OK(botan_rng_get, (rng, plaintext.data(), plaintext.size()));
1 ✔
3784

3785
               size_t ctext_len;
1 ✔
3786
               TEST_FFI_OK(botan_pk_op_encrypt_output_length, (encrypt, plaintext.size(), &ctext_len));
1 ✔
3787
               std::vector<uint8_t> ciphertext(ctext_len);
1 ✔
3788

3789
               if(TEST_FFI_OK(botan_pk_op_encrypt,
1 ✔
3790
                              (encrypt, rng, ciphertext.data(), &ctext_len, plaintext.data(), plaintext.size()))) {
3791
                  ciphertext.resize(ctext_len);
1 ✔
3792

3793
                  botan_pk_op_decrypt_t decrypt;
1 ✔
3794
                  if(TEST_FFI_OK(botan_pk_op_decrypt_create, (&decrypt, priv, "OAEP(SHA-256)", 0))) {
1 ✔
3795
                     size_t decrypted_len;
1 ✔
3796
                     TEST_FFI_OK(botan_pk_op_decrypt_output_length, (decrypt, ciphertext.size(), &decrypted_len));
1 ✔
3797
                     std::vector<uint8_t> decrypted(decrypted_len);
1 ✔
3798
                     TEST_FFI_OK(botan_pk_op_decrypt,
1 ✔
3799
                                 (decrypt, decrypted.data(), &decrypted_len, ciphertext.data(), ciphertext.size()));
3800
                     decrypted.resize(decrypted_len);
1 ✔
3801

3802
                     result.test_bin_eq("RSA plaintext", decrypted, plaintext);
1 ✔
3803
                  }
1 ✔
3804

3805
                  TEST_FFI_OK(botan_pk_op_decrypt_destroy, (decrypt));
1 ✔
3806
               }
3807

3808
               TEST_FFI_OK(botan_pk_op_encrypt_destroy, (encrypt));
1 ✔
3809
            }
1 ✔
3810

3811
            TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey));
1 ✔
3812
            TEST_FFI_OK(botan_pubkey_destroy, (pub));
1 ✔
3813
            TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
1 ✔
3814
            TEST_FFI_OK(botan_privkey_destroy, (priv));
1 ✔
3815
         }
1 ✔
3816
      }
1 ✔
3817
};
3818

3819
class FFI_DSA_Test final : public FFI_Test {
1 ✔
3820
   public:
3821
      std::string name() const override { return "FFI DSA"; }
1 ✔
3822

3823
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
3824
         botan_privkey_t priv;
1 ✔
3825

3826
         if(TEST_FFI_INIT(botan_privkey_create, (&priv, "DSA", "dsa/jce/1024", rng))) {
1 ✔
3827
            do_dsa_test(priv, rng, result);
1 ✔
3828
         }
3829

3830
         if(TEST_FFI_INIT(botan_privkey_create_dsa, (&priv, rng, 1024, 160))) {
1 ✔
3831
            do_dsa_test(priv, rng, result);
1 ✔
3832
         }
3833
      }
1 ✔
3834

3835
   private:
3836
      static void do_dsa_test(botan_privkey_t priv, botan_rng_t rng, Test::Result& result) {
2 ✔
3837
         TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
2 ✔
3838

3839
         botan_pubkey_t pub;
2 ✔
3840
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
2 ✔
3841
         TEST_FFI_OK(botan_pubkey_check_key, (pub, rng, 0));
2 ✔
3842

3843
         ffi_test_pubkey_export(result, pub, priv, rng);
2 ✔
3844

3845
         botan_mp_t p;
2 ✔
3846
         botan_mp_t q;
2 ✔
3847
         botan_mp_t g;
2 ✔
3848
         botan_mp_t x;
2 ✔
3849
         botan_mp_t y;
2 ✔
3850
         botan_mp_init(&p);
2 ✔
3851
         botan_mp_init(&q);
2 ✔
3852
         botan_mp_init(&g);
2 ✔
3853
         botan_mp_init(&x);
2 ✔
3854
         botan_mp_init(&y);
2 ✔
3855

3856
         TEST_FFI_OK(botan_privkey_dsa_get_x, (x, priv));
2 ✔
3857
         TEST_FFI_OK(botan_pubkey_dsa_get_g, (g, pub));
2 ✔
3858
         TEST_FFI_OK(botan_pubkey_dsa_get_p, (p, pub));
2 ✔
3859
         TEST_FFI_OK(botan_pubkey_dsa_get_q, (q, pub));
2 ✔
3860
         TEST_FFI_OK(botan_pubkey_dsa_get_y, (y, pub));
2 ✔
3861

3862
         botan_mp_t cmp;
2 ✔
3863
         botan_mp_init(&cmp);
2 ✔
3864
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_privkey_get_field, (cmp, priv, "quux"));
2 ✔
3865

3866
         TEST_FFI_OK(botan_privkey_get_field, (cmp, priv, "x"));
2 ✔
3867
         TEST_FFI_RC(1, botan_mp_equal, (cmp, x));
2 ✔
3868

3869
         TEST_FFI_OK(botan_privkey_get_field, (cmp, priv, "y"));
2 ✔
3870
         TEST_FFI_RC(1, botan_mp_equal, (cmp, y));
2 ✔
3871

3872
         TEST_FFI_OK(botan_privkey_get_field, (cmp, priv, "p"));
2 ✔
3873
         TEST_FFI_RC(1, botan_mp_equal, (cmp, p));
2 ✔
3874
         botan_mp_destroy(cmp);
2 ✔
3875

3876
         botan_privkey_t loaded_privkey;
2 ✔
3877
         TEST_FFI_OK(botan_privkey_load_dsa, (&loaded_privkey, p, q, g, x));
2 ✔
3878
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
2 ✔
3879

3880
         botan_pubkey_t loaded_pubkey;
2 ✔
3881
         TEST_FFI_OK(botan_pubkey_load_dsa, (&loaded_pubkey, p, q, g, y));
2 ✔
3882
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey, rng, 0));
2 ✔
3883

3884
         botan_mp_destroy(p);
2 ✔
3885
         botan_mp_destroy(q);
2 ✔
3886
         botan_mp_destroy(g);
2 ✔
3887
         botan_mp_destroy(y);
2 ✔
3888
         botan_mp_destroy(x);
2 ✔
3889

3890
         botan_pk_op_sign_t signer;
2 ✔
3891

3892
         std::vector<uint8_t> message(6, 6);
2 ✔
3893
         std::vector<uint8_t> signature;
2 ✔
3894

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

3899
            size_t sig_len;
2 ✔
3900
            TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_len));
2 ✔
3901
            signature.resize(sig_len);
2 ✔
3902

3903
            size_t output_sig_len = sig_len;
2 ✔
3904
            TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &output_sig_len));
2 ✔
3905
            result.test_sz_lte("Output length is upper bound", output_sig_len, sig_len);
2 ✔
3906
            signature.resize(output_sig_len);
2 ✔
3907

3908
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
2 ✔
3909
         }
3910

3911
         botan_pk_op_verify_t verifier = nullptr;
2 ✔
3912

3913
         if(!signature.empty() && TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, "SHA-256", 0))) {
2 ✔
3914
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2 ✔
3915
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2 ✔
3916

3917
            // TODO: randomize this
3918
            signature[0] ^= 1;
2 ✔
3919
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2 ✔
3920
            TEST_FFI_RC(
2 ✔
3921
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
3922

3923
            message[0] ^= 1;
2 ✔
3924
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2 ✔
3925
            TEST_FFI_RC(
2 ✔
3926
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
3927

3928
            signature[0] ^= 1;
2 ✔
3929
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2 ✔
3930
            TEST_FFI_RC(
2 ✔
3931
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
3932

3933
            message[0] ^= 1;
2 ✔
3934
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2 ✔
3935
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2 ✔
3936

3937
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
2 ✔
3938
         }
3939

3940
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey));
2 ✔
3941
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
2 ✔
3942
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
2 ✔
3943
         TEST_FFI_OK(botan_privkey_destroy, (priv));
2 ✔
3944
      }
2 ✔
3945
};
3946

3947
class FFI_ECDSA_Test final : public FFI_Test {
1 ✔
3948
   public:
3949
      std::string name() const override { return "FFI ECDSA"; }
1 ✔
3950

3951
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
3952
         static const char* kCurve = "secp384r1";
1 ✔
3953
         botan_privkey_t priv;
1 ✔
3954
         botan_pubkey_t pub;
1 ✔
3955

3956
         if(!TEST_FFI_INIT(botan_privkey_create_ecdsa, (&priv, rng, kCurve))) {
1 ✔
3957
            return;
×
3958
         }
3959

3960
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1 ✔
3961
         ffi_test_pubkey_export(result, pub, priv, rng);
1 ✔
3962

3963
         // Check key load functions
3964
         botan_mp_t private_scalar;
1 ✔
3965
         botan_mp_t public_x;
1 ✔
3966
         botan_mp_t public_y;
1 ✔
3967
         ViewBytesSink sec1;
1 ✔
3968
         botan_mp_init(&private_scalar);
1 ✔
3969
         botan_mp_init(&public_x);
1 ✔
3970
         botan_mp_init(&public_y);
1 ✔
3971

3972
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_privkey_get_field, (private_scalar, priv, "quux"));
1 ✔
3973
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_pubkey_get_field, (private_scalar, pub, "quux"));
1 ✔
3974

3975
         TEST_FFI_OK(botan_privkey_get_field, (private_scalar, priv, "x"));
1 ✔
3976
         TEST_FFI_OK(botan_pubkey_get_field, (public_x, pub, "public_x"));
1 ✔
3977
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub, "public_y"));
1 ✔
3978
         TEST_FFI_OK(botan_pubkey_view_raw, (pub, sec1.delegate(), sec1.callback()));
1 ✔
3979

3980
         botan_privkey_t loaded_privkey;
1 ✔
3981
         botan_pubkey_t loaded_pubkey1;
1 ✔
3982
         botan_pubkey_t loaded_pubkey2;
1 ✔
3983
         TEST_FFI_OK(botan_privkey_load_ecdsa, (&loaded_privkey, private_scalar, kCurve));
1 ✔
3984
         TEST_FFI_OK(botan_pubkey_load_ecdsa, (&loaded_pubkey1, public_x, public_y, kCurve));
1 ✔
3985
         TEST_FFI_OK(botan_pubkey_load_ecdsa_sec1, (&loaded_pubkey2, sec1.data(), sec1.size(), kCurve));
1 ✔
3986
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
1 ✔
3987
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey1, rng, 0));
1 ✔
3988
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey2, rng, 0));
1 ✔
3989

3990
         std::array<char, 32> namebuf{};
1 ✔
3991
         size_t name_len = namebuf.size();
1 ✔
3992

3993
         TEST_FFI_OK(botan_pubkey_algo_name, (pub, namebuf.data(), &name_len));
1 ✔
3994
         result.test_str_eq("Algo name is expected", namebuf.data(), "ECDSA");
1 ✔
3995

3996
         std::vector<uint8_t> message(1280);
1 ✔
3997
         std::vector<uint8_t> signature;
1 ✔
3998
         TEST_FFI_OK(botan_rng_get, (rng, message.data(), message.size()));
1 ✔
3999

4000
         for(uint32_t flags = 0; flags <= 1; ++flags) {
3 ✔
4001
            botan_pk_op_sign_t signer;
2 ✔
4002
            if(TEST_FFI_INIT(botan_pk_op_sign_create, (&signer, loaded_privkey, "SHA-384", flags))) {
2 ✔
4003
               // TODO: break input into multiple calls to update
4004
               TEST_FFI_OK(botan_pk_op_sign_update, (signer, message.data(), message.size()));
2 ✔
4005

4006
               size_t sig_len;
2 ✔
4007
               TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_len));
2 ✔
4008

4009
               signature.resize(sig_len);
2 ✔
4010

4011
               size_t output_sig_len = signature.size();
2 ✔
4012
               TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &output_sig_len));
2 ✔
4013
               signature.resize(output_sig_len);
2 ✔
4014

4015
               TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
2 ✔
4016
            }
4017

4018
            botan_pk_op_verify_t verifier = nullptr;
2 ✔
4019

4020
            if(!signature.empty() && TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, "SHA-384", flags))) {
2 ✔
4021
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2 ✔
4022
               TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2 ✔
4023

4024
               // TODO: randomize this
4025
               signature[0] ^= 1;
2 ✔
4026
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2 ✔
4027
               TEST_FFI_RC(BOTAN_FFI_INVALID_VERIFIER,
2 ✔
4028
                           botan_pk_op_verify_finish,
4029
                           (verifier, signature.data(), signature.size()));
4030

4031
               message[0] ^= 1;
2 ✔
4032
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2 ✔
4033
               TEST_FFI_RC(BOTAN_FFI_INVALID_VERIFIER,
2 ✔
4034
                           botan_pk_op_verify_finish,
4035
                           (verifier, signature.data(), signature.size()));
4036

4037
               signature[0] ^= 1;
2 ✔
4038
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2 ✔
4039
               TEST_FFI_RC(BOTAN_FFI_INVALID_VERIFIER,
2 ✔
4040
                           botan_pk_op_verify_finish,
4041
                           (verifier, signature.data(), signature.size()));
4042

4043
               message[0] ^= 1;
2 ✔
4044
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2 ✔
4045
               TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2 ✔
4046

4047
               TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
2 ✔
4048
            }
4049
         }
4050

4051
         TEST_FFI_OK(botan_mp_destroy, (private_scalar));
1 ✔
4052
         TEST_FFI_OK(botan_mp_destroy, (public_x));
1 ✔
4053
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1 ✔
4054
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1 ✔
4055
         TEST_FFI_OK(botan_privkey_destroy, (priv));
1 ✔
4056
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
1 ✔
4057
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey1));
1 ✔
4058
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey2));
1 ✔
4059
      }
1 ✔
4060
};
4061

4062
class FFI_SM2_Sig_Test final : public FFI_Test {
1 ✔
4063
   public:
4064
      std::string name() const override { return "FFI SM2 Sig"; }
1 ✔
4065

4066
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
4067
         static const char* kCurve = "sm2p256v1";
1 ✔
4068
         const std::string sm2_ident = "SM2 Ident Field";
1 ✔
4069
         botan_privkey_t priv;
1 ✔
4070
         botan_pubkey_t pub;
1 ✔
4071
         botan_privkey_t loaded_privkey;
1 ✔
4072
         botan_pubkey_t loaded_pubkey1;
1 ✔
4073
         botan_pubkey_t loaded_pubkey2;
1 ✔
4074

4075
         if(!TEST_FFI_INIT(botan_privkey_create, (&priv, "SM2_Sig", kCurve, rng))) {
1 ✔
4076
            return;
4077
         }
4078

4079
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1 ✔
4080
         ffi_test_pubkey_export(result, pub, priv, rng);
1 ✔
4081

4082
         uint8_t za[32];
1 ✔
4083
         size_t sizeof_za = sizeof(za);
1 ✔
4084
         TEST_FFI_OK(botan_pubkey_sm2_compute_za, (za, &sizeof_za, "Ident", "SM3", pub));
1 ✔
4085

4086
         // Check key load functions
4087
         botan_mp_t private_scalar;
1 ✔
4088
         botan_mp_t public_x;
1 ✔
4089
         botan_mp_t public_y;
1 ✔
4090
         ViewBytesSink sec1;
1 ✔
4091
         botan_mp_init(&private_scalar);
1 ✔
4092
         botan_mp_init(&public_x);
1 ✔
4093
         botan_mp_init(&public_y);
1 ✔
4094

4095
         TEST_FFI_OK(botan_privkey_get_field, (private_scalar, priv, "x"));
1 ✔
4096
         TEST_FFI_OK(botan_pubkey_get_field, (public_x, pub, "public_x"));
1 ✔
4097
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub, "public_y"));
1 ✔
4098
         TEST_FFI_OK(botan_pubkey_view_raw, (pub, sec1.delegate(), sec1.callback()));
1 ✔
4099
         REQUIRE_FFI_OK(botan_privkey_load_sm2, (&loaded_privkey, private_scalar, kCurve));
1 ✔
4100
         REQUIRE_FFI_OK(botan_pubkey_load_sm2, (&loaded_pubkey1, public_x, public_y, kCurve));
1 ✔
4101
         REQUIRE_FFI_OK(botan_pubkey_load_sm2_sec1, (&loaded_pubkey2, sec1.data(), sec1.size(), kCurve));
1 ✔
4102
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
1 ✔
4103
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey1, rng, 0));
1 ✔
4104
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey2, rng, 0));
1 ✔
4105

4106
         std::array<char, 32> namebuf{};
1 ✔
4107
         size_t name_len = namebuf.size();
1 ✔
4108

4109
         TEST_FFI_OK(botan_pubkey_algo_name, (pub, namebuf.data(), &name_len));
1 ✔
4110
         result.test_str_eq("Algo name is expected", namebuf.data(), "SM2");
1 ✔
4111

4112
         std::vector<uint8_t> message(1280);
1 ✔
4113
         std::vector<uint8_t> signature;
1 ✔
4114
         TEST_FFI_OK(botan_rng_get, (rng, message.data(), message.size()));
1 ✔
4115
         botan_pk_op_sign_t signer;
1 ✔
4116
         if(TEST_FFI_OK(botan_pk_op_sign_create, (&signer, loaded_privkey, sm2_ident.c_str(), 0))) {
1 ✔
4117
            // TODO: break input into multiple calls to update
4118
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message.data(), message.size()));
1 ✔
4119

4120
            size_t sig_len;
1 ✔
4121
            TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_len));
1 ✔
4122

4123
            signature.resize(sig_len);
1 ✔
4124

4125
            TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &sig_len));
1 ✔
4126
            signature.resize(sig_len);
1 ✔
4127

4128
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
1 ✔
4129
         }
4130

4131
         botan_pk_op_verify_t verifier = nullptr;
1 ✔
4132

4133
         if(!signature.empty() && TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, sm2_ident.c_str(), 0))) {
1 ✔
4134
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1 ✔
4135
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
1 ✔
4136

4137
            // TODO: randomize this
4138
            signature[0] ^= 1;
1 ✔
4139
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1 ✔
4140
            TEST_FFI_RC(
1 ✔
4141
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
4142

4143
            message[0] ^= 1;
1 ✔
4144
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1 ✔
4145
            TEST_FFI_RC(
1 ✔
4146
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
4147

4148
            signature[0] ^= 1;
1 ✔
4149
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1 ✔
4150
            TEST_FFI_RC(
1 ✔
4151
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
4152

4153
            message[0] ^= 1;
1 ✔
4154
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1 ✔
4155
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
1 ✔
4156

4157
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
1 ✔
4158
         }
4159

4160
         TEST_FFI_OK(botan_mp_destroy, (private_scalar));
1 ✔
4161
         TEST_FFI_OK(botan_mp_destroy, (public_x));
1 ✔
4162
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1 ✔
4163
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1 ✔
4164
         TEST_FFI_OK(botan_privkey_destroy, (priv));
1 ✔
4165
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
1 ✔
4166
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey1));
1 ✔
4167
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey2));
1 ✔
4168
      }
1 ✔
4169
};
4170

4171
class FFI_SM2_Enc_Test final : public FFI_Test {
1 ✔
4172
   public:
4173
      std::string name() const override { return "FFI SM2 Enc"; }
1 ✔
4174

4175
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
4176
         static const char* kCurve = "sm2p256v1";
1 ✔
4177
         botan_privkey_t priv;
1 ✔
4178
         botan_pubkey_t pub;
1 ✔
4179
         botan_privkey_t loaded_privkey;
1 ✔
4180
         botan_pubkey_t loaded_pubkey1;
1 ✔
4181
         botan_pubkey_t loaded_pubkey2;
1 ✔
4182

4183
         if(!TEST_FFI_INIT(botan_privkey_create, (&priv, "SM2_Enc", kCurve, rng))) {
1 ✔
4184
            return;
×
4185
         }
4186

4187
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1 ✔
4188
         ffi_test_pubkey_export(result, pub, priv, rng);
1 ✔
4189

4190
         uint8_t za[32];
1 ✔
4191
         size_t sizeof_za = sizeof(za);
1 ✔
4192
         TEST_FFI_OK(botan_pubkey_sm2_compute_za, (za, &sizeof_za, "Ident", "SM3", pub));
1 ✔
4193

4194
         // Check key load functions
4195
         botan_mp_t private_scalar;
1 ✔
4196
         botan_mp_t public_x;
1 ✔
4197
         botan_mp_t public_y;
1 ✔
4198
         ViewBytesSink sec1;
1 ✔
4199
         botan_mp_init(&private_scalar);
1 ✔
4200
         botan_mp_init(&public_x);
1 ✔
4201
         botan_mp_init(&public_y);
1 ✔
4202

4203
         TEST_FFI_OK(botan_privkey_get_field, (private_scalar, priv, "x"));
1 ✔
4204
         TEST_FFI_OK(botan_pubkey_get_field, (public_x, pub, "public_x"));
1 ✔
4205
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub, "public_y"));
1 ✔
4206
         TEST_FFI_OK(botan_pubkey_view_raw, (pub, sec1.delegate(), sec1.callback()));
1 ✔
4207
         REQUIRE_FFI_OK(botan_privkey_load_sm2_enc, (&loaded_privkey, private_scalar, kCurve));
1 ✔
4208
         REQUIRE_FFI_OK(botan_pubkey_load_sm2_enc, (&loaded_pubkey1, public_x, public_y, kCurve));
1 ✔
4209
         REQUIRE_FFI_OK(botan_pubkey_load_sm2_sec1, (&loaded_pubkey2, sec1.data(), sec1.size(), kCurve));
1 ✔
4210
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
1 ✔
4211
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey1, rng, 0));
1 ✔
4212
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey2, rng, 0));
1 ✔
4213

4214
         std::array<char, 32> namebuf{};
1 ✔
4215
         size_t name_len = namebuf.size();
1 ✔
4216

4217
         TEST_FFI_OK(botan_pubkey_algo_name, (pub, namebuf.data(), &name_len));
1 ✔
4218
         result.test_str_eq("Algo name is expected", namebuf.data(), "SM2");
1 ✔
4219

4220
         std::vector<uint8_t> message(32);
1 ✔
4221

4222
         std::vector<uint8_t> ciphertext;
1 ✔
4223
         TEST_FFI_OK(botan_rng_get, (rng, message.data(), message.size()));
1 ✔
4224

4225
         botan_pk_op_encrypt_t enc;
1 ✔
4226
         if(TEST_FFI_OK(botan_pk_op_encrypt_create, (&enc, loaded_pubkey1, "", 0))) {
1 ✔
4227
            size_t ctext_len;
1 ✔
4228
            TEST_FFI_OK(botan_pk_op_encrypt_output_length, (enc, message.size(), &ctext_len));
1 ✔
4229

4230
            ciphertext.resize(ctext_len);
1 ✔
4231
            TEST_FFI_OK(botan_pk_op_encrypt, (enc, rng, ciphertext.data(), &ctext_len, message.data(), message.size()));
1 ✔
4232
            ciphertext.resize(ctext_len);
1 ✔
4233

4234
            botan_pk_op_decrypt_t dec;
1 ✔
4235
            TEST_FFI_OK(botan_pk_op_decrypt_create, (&dec, loaded_privkey, "", 0));
1 ✔
4236

4237
            std::vector<uint8_t> recovered(message.size());
1 ✔
4238
            size_t recovered_len = recovered.size();
1 ✔
4239

4240
            TEST_FFI_OK(botan_pk_op_decrypt,
1 ✔
4241
                        (dec, recovered.data(), &recovered_len, ciphertext.data(), ciphertext.size()));
4242

4243
            botan_pk_op_decrypt_destroy(dec);
1 ✔
4244
         }
1 ✔
4245
         botan_pk_op_encrypt_destroy(enc);
1 ✔
4246

4247
         TEST_FFI_OK(botan_mp_destroy, (private_scalar));
1 ✔
4248
         TEST_FFI_OK(botan_mp_destroy, (public_x));
1 ✔
4249
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1 ✔
4250
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1 ✔
4251
         TEST_FFI_OK(botan_privkey_destroy, (priv));
1 ✔
4252
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
1 ✔
4253
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey1));
1 ✔
4254
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey2));
1 ✔
4255
      }
1 ✔
4256
};
4257

4258
class FFI_ECDH_Test final : public FFI_Test {
1 ✔
4259
   public:
4260
      std::string name() const override { return "FFI ECDH"; }
1 ✔
4261

4262
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
4263
         botan_privkey_t priv1;
1 ✔
4264
         if(!TEST_FFI_INIT(botan_privkey_create_ecdh, (&priv1, rng, "secp256r1"))) {
1 ✔
4265
            return;
×
4266
         }
4267

4268
         botan_privkey_t priv2;
1 ✔
4269
         REQUIRE_FFI_OK(botan_privkey_create_ecdh, (&priv2, rng, "secp256r1"));
1 ✔
4270

4271
         botan_pubkey_t pub1;
1 ✔
4272
         REQUIRE_FFI_OK(botan_privkey_export_pubkey, (&pub1, priv1));
1 ✔
4273

4274
         botan_pubkey_t pub2;
1 ✔
4275
         REQUIRE_FFI_OK(botan_privkey_export_pubkey, (&pub2, priv2));
1 ✔
4276

4277
         /* Reload key-pair1 in order to test functions for key loading */
4278
         botan_mp_t private_scalar;
1 ✔
4279
         botan_mp_t public_x;
1 ✔
4280
         botan_mp_t public_y;
1 ✔
4281
         ViewBytesSink sec1;
1 ✔
4282
         botan_mp_init(&private_scalar);
1 ✔
4283
         botan_mp_init(&public_x);
1 ✔
4284
         botan_mp_init(&public_y);
1 ✔
4285

4286
         TEST_FFI_OK(botan_privkey_get_field, (private_scalar, priv1, "x"));
1 ✔
4287
         TEST_FFI_OK(botan_pubkey_get_field, (public_x, pub1, "public_x"));
1 ✔
4288
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub1, "public_y"));
1 ✔
4289
         TEST_FFI_OK(botan_pubkey_view_raw, (pub1, sec1.delegate(), sec1.callback()));
1 ✔
4290

4291
         botan_privkey_t loaded_privkey1;
1 ✔
4292
         botan_pubkey_t loaded_pubkey1;
1 ✔
4293
         botan_pubkey_t loaded_pubkey2;
1 ✔
4294
         REQUIRE_FFI_OK(botan_privkey_load_ecdh, (&loaded_privkey1, private_scalar, "secp256r1"));
1 ✔
4295
         REQUIRE_FFI_OK(botan_pubkey_load_ecdh, (&loaded_pubkey1, public_x, public_y, "secp256r1"));
1 ✔
4296
         REQUIRE_FFI_OK(botan_pubkey_load_ecdh_sec1, (&loaded_pubkey2, sec1.data(), sec1.size(), "secp256r1"));
1 ✔
4297
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey1, rng, 0));
1 ✔
4298
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey1, rng, 0));
1 ✔
4299
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey2, rng, 0));
1 ✔
4300

4301
         ffi_test_pubkey_export(result, loaded_pubkey1, priv1, rng);
1 ✔
4302
         ffi_test_pubkey_export(result, loaded_pubkey2, priv1, rng);
1 ✔
4303
         ffi_test_pubkey_export(result, pub2, priv2, rng);
1 ✔
4304

4305
   #if defined(BOTAN_HAS_KDF2) && defined(BOTAN_HAS_SHA_256)
4306
         constexpr bool has_kdf2_sha256 = true;
1 ✔
4307
   #else
4308
         constexpr bool has_kdf2_sha256 = false;
4309
   #endif
4310

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

4314
         botan_pk_op_ka_t ka1;
1 ✔
4315
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka1, loaded_privkey1, kdf, 0));
1 ✔
4316
         botan_pk_op_ka_t ka2;
1 ✔
4317
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka2, priv2, kdf, 0));
1 ✔
4318

4319
         size_t pubkey1_len = 0;
1 ✔
4320
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1 ✔
4321
                     botan_pk_op_key_agreement_export_public,
4322
                     (priv1, nullptr, &pubkey1_len));
4323
         std::vector<uint8_t> pubkey1(pubkey1_len);
1 ✔
4324
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_export_public, (priv1, pubkey1.data(), &pubkey1_len));
1 ✔
4325
         size_t pubkey2_len = 0;
1 ✔
4326
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1 ✔
4327
                     botan_pk_op_key_agreement_export_public,
4328
                     (priv2, nullptr, &pubkey2_len));
4329
         std::vector<uint8_t> pubkey2(pubkey2_len);
1 ✔
4330
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_export_public, (priv2, pubkey2.data(), &pubkey2_len));
1 ✔
4331

4332
         std::vector<uint8_t> salt(salt_len);
1 ✔
4333
         TEST_FFI_OK(botan_rng_get, (rng, salt.data(), salt.size()));
1 ✔
4334

4335
         const size_t shared_key_len = 32;
1 ✔
4336

4337
         std::vector<uint8_t> key1(shared_key_len);
1 ✔
4338
         size_t key1_len = key1.size();
1 ✔
4339
         TEST_FFI_OK(botan_pk_op_key_agreement,
1 ✔
4340
                     (ka1, key1.data(), &key1_len, pubkey2.data(), pubkey2.size(), salt.data(), salt.size()));
4341

4342
         std::vector<uint8_t> key2(shared_key_len);
1 ✔
4343
         size_t key2_len = key2.size();
1 ✔
4344
         TEST_FFI_OK(botan_pk_op_key_agreement,
1 ✔
4345
                     (ka2, key2.data(), &key2_len, pubkey1.data(), pubkey1.size(), salt.data(), salt.size()));
4346

4347
         result.test_bin_eq("shared ECDH key", key1, key2);
1 ✔
4348

4349
         TEST_FFI_OK(botan_mp_destroy, (private_scalar));
1 ✔
4350
         TEST_FFI_OK(botan_mp_destroy, (public_x));
1 ✔
4351
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1 ✔
4352
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka1));
1 ✔
4353
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka2));
1 ✔
4354
         TEST_FFI_OK(botan_privkey_destroy, (priv1));
1 ✔
4355
         TEST_FFI_OK(botan_privkey_destroy, (priv2));
1 ✔
4356
         TEST_FFI_OK(botan_pubkey_destroy, (pub1));
1 ✔
4357
         TEST_FFI_OK(botan_pubkey_destroy, (pub2));
1 ✔
4358
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey1));
1 ✔
4359
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey1));
1 ✔
4360
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey2));
1 ✔
4361
      }
1 ✔
4362
};
4363

4364
class FFI_McEliece_Test final : public FFI_Test {
1 ✔
4365
   public:
4366
      std::string name() const override { return "FFI McEliece"; }
1 ✔
4367

4368
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
4369
         botan_privkey_t priv;
1 ✔
4370
         if(TEST_FFI_INIT(botan_privkey_create_mceliece, (&priv, rng, 1632, 33))) {
1 ✔
4371
            botan_pubkey_t pub;
1 ✔
4372
            TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1 ✔
4373

4374
            ffi_test_pubkey_export(result, pub, priv, rng);
1 ✔
4375

4376
            std::array<char, 32> namebuf{};
1 ✔
4377
            size_t name_len = namebuf.size();
1 ✔
4378
            if(TEST_FFI_OK(botan_pubkey_algo_name, (pub, namebuf.data(), &name_len))) {
1 ✔
4379
               result.test_str_eq("algo name", namebuf.data(), "McEliece");
1 ✔
4380
            }
4381

4382
            // TODO test KEM
4383

4384
            TEST_FFI_OK(botan_pubkey_destroy, (pub));
1 ✔
4385
            TEST_FFI_OK(botan_privkey_destroy, (priv));
1 ✔
4386
         }
4387
      }
1 ✔
4388
};
4389

4390
class FFI_Ed25519_Test final : public FFI_Test {
1 ✔
4391
   public:
4392
      std::string name() const override { return "FFI Ed25519"; }
1 ✔
4393

4394
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
4395
         botan_pubkey_t pub;
1 ✔
4396
         botan_privkey_t priv;
1 ✔
4397

4398
         // From draft-koch-eddsa-for-openpgp-04
4399
         const std::vector<uint8_t> seed =
1 ✔
4400
            Botan::hex_decode("1a8b1ff05ded48e18bf50166c664ab023ea70003d78d9e41f5758a91d850f8d2");
1 ✔
4401
         const std::vector<uint8_t> pubkey =
1 ✔
4402
            Botan::hex_decode("3f098994bdd916ed4053197934e4a87c80733a1280d62f8010992e43ee3b2406");
1 ✔
4403
         const std::vector<uint8_t> message = Botan::hex_decode("4f70656e504750040016080006050255f95f9504ff0000000c");
1 ✔
4404
         const std::vector<uint8_t> exp_sig = Botan::hex_decode(
1 ✔
4405
            "56f90cca98e2102637bd983fdb16c131dfd27ed82bf4dde5606e0d756aed3366"
4406
            "d09c4fa11527f038e0f57f2201d82f2ea2c9033265fa6ceb489e854bae61b404");
1 ✔
4407

4408
         if(!TEST_FFI_INIT(botan_privkey_load_ed25519, (&priv, seed.data()))) {
1 ✔
4409
            return;
×
4410
         }
4411

4412
         uint8_t retr_privkey[64];
1 ✔
4413
         TEST_FFI_OK(botan_privkey_ed25519_get_privkey, (priv, retr_privkey));
1 ✔
4414

4415
         result.test_bin_eq("Public key matches", {retr_privkey + 32, 32}, pubkey);
1 ✔
4416

4417
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1 ✔
4418

4419
         uint8_t retr_pubkey[32];
1 ✔
4420
         TEST_FFI_OK(botan_pubkey_ed25519_get_pubkey, (pub, retr_pubkey));
1 ✔
4421
         result.test_bin_eq("Public key matches", {retr_pubkey, 32}, pubkey);
1 ✔
4422

4423
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1 ✔
4424
         TEST_FFI_OK(botan_pubkey_load_ed25519, (&pub, pubkey.data()));
1 ✔
4425

4426
         botan_pk_op_sign_t signer;
1 ✔
4427
         std::vector<uint8_t> signature;
1 ✔
4428

4429
         if(TEST_FFI_OK(botan_pk_op_sign_create, (&signer, priv, "SHA-256", 0))) {
1 ✔
4430
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message.data(), message.size()));
1 ✔
4431

4432
            size_t sig_len;
1 ✔
4433
            TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_len));
1 ✔
4434

4435
            signature.resize(sig_len);
1 ✔
4436

4437
            TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &sig_len));
1 ✔
4438
            signature.resize(sig_len);
1 ✔
4439

4440
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
1 ✔
4441
         }
4442

4443
         result.test_bin_eq("Expected signature", signature, exp_sig);
1 ✔
4444

4445
         botan_pk_op_verify_t verifier;
1 ✔
4446

4447
         if(TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, "SHA-256", 0))) {
1 ✔
4448
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1 ✔
4449
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
1 ✔
4450

4451
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
1 ✔
4452
         }
4453

4454
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1 ✔
4455
         TEST_FFI_OK(botan_privkey_destroy, (priv));
1 ✔
4456
      }
1 ✔
4457
};
4458

4459
class FFI_Ed448_Test final : public FFI_Test {
1 ✔
4460
   public:
4461
      std::string name() const override { return "FFI Ed448"; }
1 ✔
4462

4463
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
4464
         botan_pubkey_t pub;
1 ✔
4465
         botan_privkey_t priv;
1 ✔
4466

4467
         // RFC 8032: Testvector Ed448, 1 octet
4468
         const auto sk = Botan::hex_decode(
1 ✔
4469
            "c4eab05d357007c632f3dbb48489924d552b08fe0c353a0d4a1f00acda2c463afbea67c5e8d2877c5e3bc397a659949ef8021e954e0a12274e");
1 ✔
4470
         const auto pk_ref = Botan::hex_decode(
1 ✔
4471
            "43ba28f430cdff456ae531545f7ecd0ac834a55d9358c0372bfa0c6c6798c0866aea01eb00742802b8438ea4cb82169c235160627b4c3a9480");
1 ✔
4472
         const auto msg = Botan::hex_decode("03");
1 ✔
4473
         const auto sig_ref = Botan::hex_decode(
1 ✔
4474
            "26b8f91727bd62897af15e41eb43c377efb9c610d48f2335cb0bd0087810f4352541b143c4b981b7e18f62de8ccdf633fc1bf037ab7cd779805e0dbcc0aae1cbcee1afb2e027df36bc04dcecbf154336c19f0af7e0a6472905e799f1953d2a0ff3348ab21aa4adafd1d234441cf807c03a00");
1 ✔
4475

4476
         if(!TEST_FFI_INIT(botan_privkey_load_ed448, (&priv, sk.data()))) {
1 ✔
4477
            return;
×
4478
         }
4479

4480
         std::vector<uint8_t> retr_privkey(57);
1 ✔
4481
         TEST_FFI_OK(botan_privkey_ed448_get_privkey, (priv, retr_privkey.data()));
1 ✔
4482
         result.test_bin_eq("Private key matches", retr_privkey, sk);
1 ✔
4483

4484
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1 ✔
4485

4486
         std::vector<uint8_t> retr_pubkey(57);
1 ✔
4487
         TEST_FFI_OK(botan_pubkey_ed448_get_pubkey, (pub, retr_pubkey.data()));
1 ✔
4488
         result.test_bin_eq("Public key matches", retr_pubkey, pk_ref);
1 ✔
4489

4490
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1 ✔
4491
         TEST_FFI_OK(botan_pubkey_load_ed448, (&pub, pk_ref.data()));
1 ✔
4492

4493
         botan_pk_op_sign_t signer;
1 ✔
4494
         std::vector<uint8_t> signature;
1 ✔
4495

4496
         if(TEST_FFI_OK(botan_pk_op_sign_create, (&signer, priv, "Pure", 0))) {
1 ✔
4497
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, msg.data(), msg.size()));
1 ✔
4498

4499
            size_t sig_len;
1 ✔
4500
            TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_len));
1 ✔
4501

4502
            signature.resize(sig_len);
1 ✔
4503

4504
            TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &sig_len));
1 ✔
4505
            signature.resize(sig_len);
1 ✔
4506

4507
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
1 ✔
4508
         }
4509

4510
         result.test_bin_eq("Expected signature", signature, sig_ref);
1 ✔
4511

4512
         botan_pk_op_verify_t verifier;
1 ✔
4513

4514
         if(TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, "Pure", 0))) {
1 ✔
4515
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, msg.data(), msg.size()));
1 ✔
4516
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
1 ✔
4517

4518
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
1 ✔
4519
         }
4520

4521
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1 ✔
4522
         TEST_FFI_OK(botan_privkey_destroy, (priv));
1 ✔
4523
      }
1 ✔
4524
};
4525

4526
class FFI_X25519_Test final : public FFI_Test {
1 ✔
4527
   public:
4528
      std::string name() const override { return "FFI X25519"; }
1 ✔
4529

4530
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
4531
         // From RFC 8037
4532

4533
         const std::vector<uint8_t> a_pub_bits =
1 ✔
4534
            Botan::hex_decode("de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f");
1 ✔
4535
         const std::vector<uint8_t> b_priv_bits =
1 ✔
4536
            Botan::hex_decode("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a");
1 ✔
4537
         const std::vector<uint8_t> b_pub_bits =
1 ✔
4538
            Botan::hex_decode("8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a");
1 ✔
4539
         const std::vector<uint8_t> shared_secret_bits =
1 ✔
4540
            Botan::hex_decode("4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742");
1 ✔
4541

4542
         botan_privkey_t b_priv;
1 ✔
4543
         if(!TEST_FFI_INIT(botan_privkey_load_x25519, (&b_priv, b_priv_bits.data()))) {
1 ✔
4544
            return;
4545
         }
4546

4547
         std::vector<uint8_t> privkey_read(32);
1 ✔
4548
         TEST_FFI_OK(botan_privkey_x25519_get_privkey, (b_priv, privkey_read.data()));
1 ✔
4549
         result.test_bin_eq("X25519 private key", privkey_read, b_priv_bits);
1 ✔
4550

4551
         std::vector<uint8_t> pubkey_read(32);
1 ✔
4552

4553
         botan_pubkey_t b_pub;
1 ✔
4554
         TEST_FFI_OK(botan_privkey_export_pubkey, (&b_pub, b_priv));
1 ✔
4555
         TEST_FFI_OK(botan_pubkey_x25519_get_pubkey, (b_pub, pubkey_read.data()));
1 ✔
4556
         result.test_bin_eq("X25519 public key b", pubkey_read, b_pub_bits);
1 ✔
4557

4558
         botan_pubkey_t a_pub;
1 ✔
4559
         TEST_FFI_OK(botan_pubkey_load_x25519, (&a_pub, a_pub_bits.data()));
1 ✔
4560
         TEST_FFI_OK(botan_pubkey_x25519_get_pubkey, (a_pub, pubkey_read.data()));
1 ✔
4561
         result.test_bin_eq("X25519 public key a", pubkey_read, a_pub_bits);
1 ✔
4562

4563
         botan_pk_op_ka_t ka;
1 ✔
4564
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka, b_priv, "Raw", 0));
1 ✔
4565

4566
         std::vector<uint8_t> shared_output(32);
1 ✔
4567
         size_t shared_len = shared_output.size();
1 ✔
4568
         TEST_FFI_OK(botan_pk_op_key_agreement,
1 ✔
4569
                     (ka, shared_output.data(), &shared_len, a_pub_bits.data(), a_pub_bits.size(), nullptr, 0));
4570

4571
         result.test_bin_eq("Shared secret matches expected", shared_secret_bits, shared_output);
1 ✔
4572

4573
         TEST_FFI_OK(botan_pubkey_destroy, (a_pub));
1 ✔
4574
         TEST_FFI_OK(botan_pubkey_destroy, (b_pub));
1 ✔
4575
         TEST_FFI_OK(botan_privkey_destroy, (b_priv));
1 ✔
4576
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka));
1 ✔
4577
      }
1 ✔
4578
};
4579

4580
class FFI_X448_Test final : public FFI_Test {
1 ✔
4581
   public:
4582
      std::string name() const override { return "FFI X448"; }
1 ✔
4583

4584
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
4585
         // From RFC 7748 Section 6.2
4586
         const auto a_pub_ref = Botan::hex_decode(
1 ✔
4587
            "9b08f7cc31b7e3e67d22d5aea121074a273bd2b83de09c63faa73d2c22c5d9bbc836647241d953d40c5b12da88120d53177f80e532c41fa0");
1 ✔
4588
         const auto b_priv_ref = Botan::hex_decode(
1 ✔
4589
            "1c306a7ac2a0e2e0990b294470cba339e6453772b075811d8fad0d1d6927c120bb5ee8972b0d3e21374c9c921b09d1b0366f10b65173992d");
1 ✔
4590
         const auto b_pub_ref = Botan::hex_decode(
1 ✔
4591
            "3eb7a829b0cd20f5bcfc0b599b6feccf6da4627107bdb0d4f345b43027d8b972fc3e34fb4232a13ca706dcb57aec3dae07bdc1c67bf33609");
1 ✔
4592
         const auto shared_secret_ref = Botan::hex_decode(
1 ✔
4593
            "07fff4181ac6cc95ec1c16a94a0f74d12da232ce40a77552281d282bb60c0b56fd2464c335543936521c24403085d59a449a5037514a879d");
1 ✔
4594

4595
         botan_privkey_t b_priv;
1 ✔
4596
         if(!TEST_FFI_INIT(botan_privkey_load_x448, (&b_priv, b_priv_ref.data()))) {
1 ✔
4597
            return;
4598
         }
4599

4600
         std::vector<uint8_t> privkey_read(56);
1 ✔
4601
         TEST_FFI_OK(botan_privkey_x448_get_privkey, (b_priv, privkey_read.data()));
1 ✔
4602
         result.test_bin_eq("X448 private key", privkey_read, b_priv_ref);
1 ✔
4603

4604
         std::vector<uint8_t> pubkey_read(56);
1 ✔
4605

4606
         botan_pubkey_t b_pub;
1 ✔
4607
         TEST_FFI_OK(botan_privkey_export_pubkey, (&b_pub, b_priv));
1 ✔
4608
         TEST_FFI_OK(botan_pubkey_x448_get_pubkey, (b_pub, pubkey_read.data()));
1 ✔
4609
         result.test_bin_eq("X448 public key b", pubkey_read, b_pub_ref);
1 ✔
4610

4611
         botan_pubkey_t a_pub;
1 ✔
4612
         TEST_FFI_OK(botan_pubkey_load_x448, (&a_pub, a_pub_ref.data()));
1 ✔
4613
         TEST_FFI_OK(botan_pubkey_x448_get_pubkey, (a_pub, pubkey_read.data()));
1 ✔
4614
         result.test_bin_eq("X448 public key a", pubkey_read, a_pub_ref);
1 ✔
4615

4616
         botan_pk_op_ka_t ka;
1 ✔
4617
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka, b_priv, "Raw", 0));
1 ✔
4618

4619
         std::vector<uint8_t> shared_output(56);
1 ✔
4620
         size_t shared_len = shared_output.size();
1 ✔
4621
         TEST_FFI_OK(botan_pk_op_key_agreement,
1 ✔
4622
                     (ka, shared_output.data(), &shared_len, a_pub_ref.data(), a_pub_ref.size(), nullptr, 0));
4623

4624
         result.test_bin_eq("Shared secret matches expected", shared_secret_ref, shared_output);
1 ✔
4625

4626
         TEST_FFI_OK(botan_pubkey_destroy, (a_pub));
1 ✔
4627
         TEST_FFI_OK(botan_pubkey_destroy, (b_pub));
1 ✔
4628
         TEST_FFI_OK(botan_privkey_destroy, (b_priv));
1 ✔
4629
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka));
1 ✔
4630
      }
1 ✔
4631
};
4632

4633
/**
4634
 * Base class for roundtrip tests of FFI bindings for Key Encapsulation Mechanisms.
4635
 */
4636
class FFI_KEM_Roundtrip_Test : public FFI_Test {
6 ✔
4637
   protected:
4638
      using privkey_loader_fn_t = int (*)(botan_privkey_t*, const uint8_t[], size_t, const char*);
4639
      using pubkey_loader_fn_t = int (*)(botan_pubkey_t*, const uint8_t[], size_t, const char*);
4640

4641
   protected:
4642
      virtual const char* algo() const = 0;
4643
      virtual privkey_loader_fn_t private_key_load_function() const = 0;
4644
      virtual pubkey_loader_fn_t public_key_load_function() const = 0;
4645
      virtual std::vector<const char*> modes() const = 0;
4646

4647
   public:
4648
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
3 ✔
4649
         for(const auto* mode : modes()) {
34 ✔
4650
            // generate a key pair
4651
            botan_privkey_t priv;
31 ✔
4652
            botan_pubkey_t pub;
31 ✔
4653
            if(!TEST_FFI_INIT(botan_privkey_create, (&priv, algo(), mode, rng))) {
31 ✔
4654
               continue;
×
4655
            }
4656
            TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
31 ✔
4657

4658
            // raw-encode the key pair
4659
            ViewBytesSink priv_bytes;
31 ✔
4660
            ViewBytesSink pub_bytes;
31 ✔
4661
            TEST_FFI_OK(botan_privkey_view_raw, (priv, priv_bytes.delegate(), priv_bytes.callback()));
31 ✔
4662
            TEST_FFI_OK(botan_pubkey_view_raw, (pub, pub_bytes.delegate(), pub_bytes.callback()));
31 ✔
4663

4664
            // decode the key pair from raw encoding
4665
            botan_privkey_t priv_loaded;
31 ✔
4666
            botan_pubkey_t pub_loaded;
31 ✔
4667
            TEST_FFI_OK(private_key_load_function(),
31 ✔
4668
                        (&priv_loaded, priv_bytes.get().data(), priv_bytes.get().size(), mode));
4669
            TEST_FFI_OK(public_key_load_function(),
31 ✔
4670
                        (&pub_loaded, pub_bytes.get().data(), pub_bytes.get().size(), mode));
4671

4672
            // re-encode and compare to the first round
4673
            ViewBytesSink priv_bytes2;
31 ✔
4674
            ViewBytesSink pub_bytes2;
31 ✔
4675
            TEST_FFI_OK(botan_privkey_view_raw, (priv_loaded, priv_bytes2.delegate(), priv_bytes2.callback()));
31 ✔
4676
            TEST_FFI_OK(botan_pubkey_view_raw, (pub_loaded, pub_bytes2.delegate(), pub_bytes2.callback()));
31 ✔
4677
            result.test_bin_eq("private key encoding", priv_bytes.get(), priv_bytes2.get());
31 ✔
4678
            result.test_bin_eq("public key encoding", pub_bytes.get(), pub_bytes2.get());
31 ✔
4679

4680
            // KEM encryption (using the loaded public key)
4681
            botan_pk_op_kem_encrypt_t kem_enc;
31 ✔
4682
            TEST_FFI_OK(botan_pk_op_kem_encrypt_create, (&kem_enc, pub_loaded, "Raw"));
31 ✔
4683

4684
            // explicitly query output lengths
4685
            size_t shared_key_length = 0;
31 ✔
4686
            size_t ciphertext_length = 0;
31 ✔
4687
            TEST_FFI_OK(botan_pk_op_kem_encrypt_shared_key_length, (kem_enc, 0, &shared_key_length));
31 ✔
4688
            TEST_FFI_OK(botan_pk_op_kem_encrypt_encapsulated_key_length, (kem_enc, &ciphertext_length));
31 ✔
4689

4690
            // check that insufficient buffer space is handled correctly
4691
            size_t shared_key_length_out = 0;
31 ✔
4692
            size_t ciphertext_length_out = 0;
31 ✔
4693
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
31 ✔
4694
                        botan_pk_op_kem_encrypt_create_shared_key,
4695
                        (kem_enc,
4696
                         rng,
4697
                         nullptr /* no salt */,
4698
                         0,
4699
                         0 /* default key length */,
4700
                         nullptr,
4701
                         &shared_key_length_out,
4702
                         nullptr,
4703
                         &ciphertext_length_out));
4704

4705
            // TODO: should this report both lengths for usage convenience?
4706
            result.test_is_true(
31 ✔
4707
               "at least one buffer length is reported",
4708
               shared_key_length_out == shared_key_length || ciphertext_length_out == ciphertext_length);
31 ✔
4709

4710
            // allocate buffers (with additional space) and perform the actual encryption
4711
            shared_key_length_out = shared_key_length * 2;
31 ✔
4712
            ciphertext_length_out = ciphertext_length * 2;
31 ✔
4713
            Botan::secure_vector<uint8_t> shared_key(shared_key_length_out);
31 ✔
4714
            std::vector<uint8_t> ciphertext(ciphertext_length_out);
31 ✔
4715
            TEST_FFI_OK(botan_pk_op_kem_encrypt_create_shared_key,
31 ✔
4716
                        (kem_enc,
4717
                         rng,
4718
                         nullptr /* no salt */,
4719
                         0,
4720
                         0 /* default key length */,
4721
                         shared_key.data(),
4722
                         &shared_key_length_out,
4723
                         ciphertext.data(),
4724
                         &ciphertext_length_out));
4725
            result.test_sz_eq("shared key length", shared_key_length, shared_key_length_out);
31 ✔
4726
            result.test_sz_eq("ciphertext length", ciphertext_length, ciphertext_length_out);
31 ✔
4727
            shared_key.resize(shared_key_length_out);
31 ✔
4728
            ciphertext.resize(ciphertext_length_out);
31 ✔
4729
            TEST_FFI_OK(botan_pk_op_kem_encrypt_destroy, (kem_enc));
31 ✔
4730

4731
            // KEM decryption (using the generated private key)
4732
            botan_pk_op_kem_decrypt_t kem_dec;
31 ✔
4733
            TEST_FFI_OK(botan_pk_op_kem_decrypt_create, (&kem_dec, priv, "Raw"));
31 ✔
4734
            size_t shared_key_length2 = 0;
31 ✔
4735
            TEST_FFI_OK(botan_pk_op_kem_decrypt_shared_key_length, (kem_dec, shared_key_length, &shared_key_length2));
31 ✔
4736
            result.test_sz_eq("shared key lengths are consistent", shared_key_length, shared_key_length2);
31 ✔
4737

4738
            // check that insufficient buffer space is handled correctly
4739
            shared_key_length_out = 0;
31 ✔
4740
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
31 ✔
4741
                        botan_pk_op_kem_decrypt_shared_key,
4742
                        (kem_dec,
4743
                         nullptr /* no salt */,
4744
                         0,
4745
                         ciphertext.data(),
4746
                         ciphertext.size(),
4747
                         0 /* default length */,
4748
                         nullptr,
4749
                         &shared_key_length_out));
4750
            result.test_sz_eq("reported buffer length requirement", shared_key_length, shared_key_length_out);
31 ✔
4751

4752
            // allocate buffer (double the size) and perform the actual decryption
4753
            shared_key_length_out = shared_key_length * 2;
31 ✔
4754
            Botan::secure_vector<uint8_t> shared_key2(shared_key_length_out);
31 ✔
4755
            TEST_FFI_OK(botan_pk_op_kem_decrypt_shared_key,
31 ✔
4756
                        (kem_dec,
4757
                         nullptr /* no salt */,
4758
                         0,
4759
                         ciphertext.data(),
4760
                         ciphertext.size(),
4761
                         0 /* default length */,
4762
                         shared_key2.data(),
4763
                         &shared_key_length_out));
4764
            result.test_sz_eq("shared key output length", shared_key_length, shared_key_length_out);
31 ✔
4765
            shared_key2.resize(shared_key_length_out);
31 ✔
4766
            TEST_FFI_OK(botan_pk_op_kem_decrypt_destroy, (kem_dec));
31 ✔
4767

4768
            // final check and clean up
4769
            result.test_bin_eq("shared keys match", shared_key, shared_key2);
31 ✔
4770

4771
            TEST_FFI_OK(botan_pubkey_destroy, (pub));
31 ✔
4772
            TEST_FFI_OK(botan_pubkey_destroy, (pub_loaded));
31 ✔
4773
            TEST_FFI_OK(botan_privkey_destroy, (priv));
31 ✔
4774
            TEST_FFI_OK(botan_privkey_destroy, (priv_loaded));
31 ✔
4775
         }
62 ✔
4776
      }
3 ✔
4777
};
4778

4779
/**
4780
 * Base class for roundtrip tests of FFI bindings for Signature Mechanisms.
4781
 */
4782
class FFI_Signature_Roundtrip_Test : public FFI_Test {
4 ✔
4783
   protected:
4784
      using privkey_loader_fn_t = int (*)(botan_privkey_t*, const uint8_t[], size_t, const char*);
4785
      using pubkey_loader_fn_t = int (*)(botan_pubkey_t*, const uint8_t[], size_t, const char*);
4786

4787
   protected:
4788
      virtual const char* algo() const = 0;
4789
      virtual privkey_loader_fn_t private_key_load_function() const = 0;
4790
      virtual pubkey_loader_fn_t public_key_load_function() const = 0;
4791
      virtual std::vector<const char*> modes() const = 0;
4792
      virtual const char* hash_algo_or_padding() const = 0;
4793

4794
   public:
4795
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
2 ✔
4796
         const std::vector<uint8_t> message1 = {'H', 'e', 'l', 'l', 'o', ' '};
2 ✔
4797
         const std::vector<uint8_t> message2 = {'W', 'o', 'r', 'l', 'd', '!'};
2 ✔
4798

4799
         for(const auto* mode : modes()) {
17 ✔
4800
            // generate a key pair
4801
            botan_privkey_t priv;
15 ✔
4802
            botan_pubkey_t pub;
15 ✔
4803
            if(!TEST_FFI_INIT(botan_privkey_create, (&priv, algo(), mode, rng))) {
15 ✔
4804
               continue;
×
4805
            }
4806
            TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
15 ✔
4807

4808
            // raw-encode the key pair
4809
            ViewBytesSink priv_bytes;
15 ✔
4810
            ViewBytesSink pub_bytes;
15 ✔
4811
            TEST_FFI_OK(botan_privkey_view_raw, (priv, priv_bytes.delegate(), priv_bytes.callback()));
15 ✔
4812
            TEST_FFI_OK(botan_pubkey_view_raw, (pub, pub_bytes.delegate(), pub_bytes.callback()));
15 ✔
4813

4814
            // decode the key pair from raw encoding
4815
            botan_privkey_t priv_loaded;
15 ✔
4816
            botan_pubkey_t pub_loaded;
15 ✔
4817
            TEST_FFI_OK(private_key_load_function(),
15 ✔
4818
                        (&priv_loaded, priv_bytes.get().data(), priv_bytes.get().size(), mode));
4819
            TEST_FFI_OK(public_key_load_function(),
15 ✔
4820
                        (&pub_loaded, pub_bytes.get().data(), pub_bytes.get().size(), mode));
4821

4822
            // re-encode and compare to the first round
4823
            ViewBytesSink priv_bytes2;
15 ✔
4824
            ViewBytesSink pub_bytes2;
15 ✔
4825
            TEST_FFI_OK(botan_privkey_view_raw, (priv_loaded, priv_bytes2.delegate(), priv_bytes2.callback()));
15 ✔
4826
            TEST_FFI_OK(botan_pubkey_view_raw, (pub_loaded, pub_bytes2.delegate(), pub_bytes2.callback()));
15 ✔
4827
            result.test_bin_eq("private key encoding", priv_bytes.get(), priv_bytes2.get());
15 ✔
4828
            result.test_bin_eq("public key encoding", pub_bytes.get(), pub_bytes2.get());
15 ✔
4829

4830
            // Signature Creation (using the loaded private key)
4831
            botan_pk_op_sign_t signer;
15 ✔
4832
            TEST_FFI_OK(botan_pk_op_sign_create, (&signer, priv_loaded, hash_algo_or_padding(), 0));
15 ✔
4833

4834
            // explicitly query the signature output length
4835
            size_t sig_output_length = 0;
15 ✔
4836
            TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_output_length));
15 ✔
4837

4838
            // pass a message to the signer
4839
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message1.data(), message1.size()));
15 ✔
4840
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message2.data(), message2.size()));
15 ✔
4841

4842
            // check that insufficient buffer space is handled correctly
4843
            size_t sig_output_length_out = 0;
15 ✔
4844
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
15 ✔
4845
                        botan_pk_op_sign_finish,
4846
                        (signer, rng, nullptr, &sig_output_length_out));
4847
            result.test_sz_eq("reported sig lengths are equal", sig_output_length, sig_output_length_out);
15 ✔
4848

4849
            // The failed call must leave the message in place, so retry using the same signer
4850
            // allocate buffers (with additional space) and perform the actual signing
4851
            sig_output_length_out = sig_output_length * 2;
15 ✔
4852
            Botan::secure_vector<uint8_t> signature(sig_output_length_out);
15 ✔
4853
            TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &sig_output_length_out));
15 ✔
4854
            result.test_sz_eq("signature length", sig_output_length, sig_output_length_out);
15 ✔
4855
            signature.resize(sig_output_length_out);
15 ✔
4856
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
15 ✔
4857

4858
            // Signature verification (using the generated public key)
4859
            botan_pk_op_verify_t verifier;
15 ✔
4860
            TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, hash_algo_or_padding(), 0));
15 ✔
4861
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message1.data(), message1.size()));
15 ✔
4862
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message2.data(), message2.size()));
15 ✔
4863

4864
            // Verify signature
4865
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
15 ✔
4866
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
15 ✔
4867

4868
            // Verify signature with wrong message (only first half)
4869
            TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, hash_algo_or_padding(), 0));
15 ✔
4870
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message1.data(), message1.size()));
15 ✔
4871
            TEST_FFI_RC(
15 ✔
4872
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
4873
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
15 ✔
4874

4875
            // Cleanup
4876
            TEST_FFI_OK(botan_pubkey_destroy, (pub));
15 ✔
4877
            TEST_FFI_OK(botan_pubkey_destroy, (pub_loaded));
15 ✔
4878
            TEST_FFI_OK(botan_privkey_destroy, (priv));
15 ✔
4879
            TEST_FFI_OK(botan_privkey_destroy, (priv_loaded));
15 ✔
4880
         }
15 ✔
4881
      }
2 ✔
4882
};
4883

4884
class FFI_Kyber512_Test final : public FFI_Test {
1 ✔
4885
   public:
4886
      std::string name() const override { return "FFI Kyber512"; }
1 ✔
4887

4888
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
4889
         const std::vector<uint8_t> a_pub_bits = Botan::hex_decode(
1 ✔
4890
            "5fc44b99d7584f38cd28360cc5625a905b96af12930ed5b5fe2a82fc5aa7dc4b829fe37635f13f5af2a6d3081dad878785698a0aa914374c4e43b89f094a7892aa149a38b49c06a068d829a8d249e753a375d097a0f162e6c3a4dfe8c79761410c605ed3899a3fc44378e14f28879e8f148077e6bc3bb2ae56178c491611bf6aaf5f9a9cb9b5659223007940bcd6f8a23280a56015330e8577259587b12606f4c937ea13606cb3bb046066ad294261e2b22022bcc74678a5520570d88e4ceb42692631e7e3711c4b2fd5347f0328598340cb3c65c8f55ac02716831094cb6eb90f175b173d9c650329aaf513633633bb2ce6858e7447abc41b6fb06da8782572c332b09660366926bf529ed8caaa6243ccdb152b36ba6e47c714145c86f5b3b61de84ef1470d03fa0135e35194fa1fb3bc860fa500d1299aee88ce56054376c1199c553dd90a8d6f9cc763c811d0c66da6f851abf1056635a34a68aa7815868f153a3a5c77fcc8b1eb1807fbf62a6fb43b355700e78230943a2ba1e11b181345b11b4d46266e7b359f074a500c8857d79ba60f64262d662ccd9c8489a4c19df67437db193f95b9765181d9152262b1166f97be53497f001cb1be79024d6a2289bcc704e1b1d821015366a3cc8a484e6bc2e1f1b889f19323e3101aa09ad9ea62ba4005039bbfb5998055f93fbf77b14433116d5958422654dada1127213f02b78717a5a0454271d5b0c02517a6c27a3c3610101d753c09a25571775477dc13b2e404db4965b9a9350330c73a8a3642d39af8a23839ab85c6355b12f279f849813c280d54c5913e99b6946a0aaf012c8cab025396b255f002d837c761d42a4aeb38c5f456aaf79e162700c6b4048eca6f9a7367f90238d67bcf8e6a0d8a553c071522f9d2394e28483d2048be2a8f9c8c8e39991a41273c7eacaefc6a308be870b45b41176412954a1a0fd83d362a5ab288663dec5456b6286d0b2cecb01922fb3d473802ea2b86639bce02450339261cffb114e1e725e90677826a1688f686b29a78779c9822315dafc55753e98c8ed3221f2b3220805c8a28983355207da36fb72f9bc85c0a13b9d041586fd583feb12afd5a402dd33b43543f5fa4eb436c8d");
1 ✔
4891
         const std::vector<uint8_t> b_priv_bits = Botan::hex_decode(
1 ✔
4892
            "cf8c33fabbc3e6685f60779a456412e9060a792a3f67d90389815773e06dd2071e3906a1159921485b6221b73dfc723da9d45bbc99523c55b203f81856ba8d38f731c6594a0a9796f485c7cad02ee3a80a737cada2e40b580a1060a1364d365169539ce4d800eab9723153c5b266853b3a33112ccb03491e77f57aeb74c7670426c5bb02615b1907b353beda51f38a788774b1c9eb3c49f89df59ba00e196f180a37fd9acd6691b493d78f95a49906b26fa46125663a37b0b2614197473ba012956bc9348a9afc8907527bb7c2dcbd384b85630b3687096ffd9a93d603121706c06c05baca286bebea0ecdf391ba489d7ff6a7a1c7c3dd0c521c02bb3fa7c3e5b98f19509486ab33f1597346b03c7197865792896c1a553e8379c6f51729e9907d09b4fbc5b4279298f60195998225d95c2aef335c9e6a26f4988e0f19c307fc00f8aa0b21090daab0c540363f121044729322bebb97427227e8b01705826879ab4d42e30bf191068b705527a3ae9a5b3e30371c50b5b75d189903e60175f904c1488be7e872d3dc3be1bb31921027cb075f6bb051c28980c65b9959e8acf2d38671d27285d03419512008335492476e0c23ac6263425f8a65fbc1a38110b38db641f3a9872fa312d26b9c81f11de1d9378b629e9bdc7ca31496ac9511bff42a7a96b28f740a84c2904fe4056ca1989b28439222cee31c554e982d318366037070c4bb778b261e3479c605317922947e4340560732ca8fda63e2a7ac8cc67015e97d308c361e634f8cdb1e836b572367334c949011a9c52f391194baa91f08a71e847d3be202554ab1c4f871c9b26db5f4992db5b3688b1ea877925d02895d470b9659c51213b206b82de489455c6502856b841d000aab3c065ff6720f9222914b6421224d716684a7762fc98ba7f4655f4cfa82fc52472b01b1e22255fc990b20240b802034c99777b71b5c0d8a4c733b69d5f76ddfdb1128da3e194c1169a924f5d868d1dbc4ddcb30197c25005391c1fcc098010af1e9ab72bc8c3bd1c58b15583035bc2dd0898e148fb20b8c502a31489b00dd645ab24709bff5c37d1a7fee5c56d7490ae77a61b1c5951e359d2a89a946f200b1f647c0283de5d403282c693dfccca1c1b54804ab86f36e1f16550a77b75328916c770c225b142dfb750659ba6d532003f8b6d48825b122682051acda7c20f09117b3e5c93e8c1bd446672512947d964f0484a05b85af82f4401d323484d23373c0c6c8990a3fba70f8a2c13e7350ce945c8f4882b1929f10d18bb45a883c116d1ce06493a50580069743d26062da9171006405d27e2f7220a897024dca507b737a6c32ba3d925e15c9789589507a768ca075beb2fb17a17144c52810566a972dba60ad806a2ff15957a3bbd85a8f1ef34445d18cc668ca7a8ac5fc7545934221a4c92c60024069cc34d592ac30c88c3376bc133622e2b192e9b4b60a9a84e5304873e62d6f98103cc8c43f6024fb7739418cc2602b21416040220cb3ec588679c033d61a3c697c403463c17c13a63d7304044a9f024b907b440b39b7b6423723dda943447a900af0170144755d9a968f634e26f892ee19cd00a97b48d5041b4473ec8741e077126f85bbb8334b8b2cbd63429afa543a540740c1e56893f6b2f2f7c42a12bcd48c647726cca06841212656ff9b3d3e799e92c48414d653a61ca8dc2c49b7342469a868b7938db8d7844e6311134b2c30538ffa927ee3961a44e0a66438c5643aa1d13658fe8a7c2a84702a8211422831994ca1e801131ab88a25162d3cd988ebe09e6cb01ca7324256691635c536f576990664841b7c89b0dc325472271fdc2b0824a9514b5eb46a743f9734ee20648a407f5d505cc9614748f344a16950b5483e45516236d43afd1494d564c23e20b64a124593604404c879a776a5bd9399629600e86aa1a641be7a13418cda318bbc3af0c2a66c999841251a1c2868b05028f6a56a72c33d8a653c77f69af4247dc6f80d329921ce3355894605c35372ddf0c84cacb42d6ccbdf39a55b672a891749622a011747a01625764c788413f2c0fd877275c88fd5730fa9e87a3c783702223a4443525cc5b5381c976dd9cb08ac47490125ca5c70baac01ff9143bc40ceeb2a16c1529d70c07074a0013749656ff4b16d890868b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb3338e5d6352d5a5006d7cd901489e7f851711c08e00cd4162ccfc2564d5893d52b2c7300e2d894b0eaa40a6ab254506d8c1176a33c4a1b2879604b1b80df48d31dd");
1 ✔
4893
         const std::vector<uint8_t> b_pub_bits = Botan::hex_decode(
1 ✔
4894
            "ee5c56d7490ae77a61b1c5951e359d2a89a946f200b1f647c0283de5d403282c693dfccca1c1b54804ab86f36e1f16550a77b75328916c770c225b142dfb750659ba6d532003f8b6d48825b122682051acda7c20f09117b3e5c93e8c1bd446672512947d964f0484a05b85af82f4401d323484d23373c0c6c8990a3fba70f8a2c13e7350ce945c8f4882b1929f10d18bb45a883c116d1ce06493a50580069743d26062da9171006405d27e2f7220a897024dca507b737a6c32ba3d925e15c9789589507a768ca075beb2fb17a17144c52810566a972dba60ad806a2ff15957a3bbd85a8f1ef34445d18cc668ca7a8ac5fc7545934221a4c92c60024069cc34d592ac30c88c3376bc133622e2b192e9b4b60a9a84e5304873e62d6f98103cc8c43f6024fb7739418cc2602b21416040220cb3ec588679c033d61a3c697c403463c17c13a63d7304044a9f024b907b440b39b7b6423723dda943447a900af0170144755d9a968f634e26f892ee19cd00a97b48d5041b4473ec8741e077126f85bbb8334b8b2cbd63429afa543a540740c1e56893f6b2f2f7c42a12bcd48c647726cca06841212656ff9b3d3e799e92c48414d653a61ca8dc2c49b7342469a868b7938db8d7844e6311134b2c30538ffa927ee3961a44e0a66438c5643aa1d13658fe8a7c2a84702a8211422831994ca1e801131ab88a25162d3cd988ebe09e6cb01ca7324256691635c536f576990664841b7c89b0dc325472271fdc2b0824a9514b5eb46a743f9734ee20648a407f5d505cc9614748f344a16950b5483e45516236d43afd1494d564c23e20b64a124593604404c879a776a5bd9399629600e86aa1a641be7a13418cda318bbc3af0c2a66c999841251a1c2868b05028f6a56a72c33d8a653c77f69af4247dc6f80d329921ce3355894605c35372ddf0c84cacb42d6ccbdf39a55b672a891749622a011747a01625764c788413f2c0fd877275c88fd5730fa9e87a3c783702223a4443525cc5b5381c976dd9cb08ac47490125ca5c70baac01ff9143bc40ceeb2a16c1529d70c07074a0013749656ff4b16d890868b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb333");
1 ✔
4895

4896
         botan_privkey_t b_priv;
1 ✔
4897
         if(!TEST_FFI_INIT(botan_privkey_load_kyber, (&b_priv, b_priv_bits.data(), 1632))) {
1 ✔
4898
            return;
×
4899
         }
4900

4901
         ViewBytesSink privkey_read;
1 ✔
4902
         ViewBytesSink privkey_read_raw;
1 ✔
4903
         TEST_FFI_OK(botan_privkey_view_kyber_raw_key, (b_priv, privkey_read.delegate(), privkey_read.callback()));
1 ✔
4904
         TEST_FFI_OK(botan_privkey_view_raw, (b_priv, privkey_read_raw.delegate(), privkey_read_raw.callback()));
1 ✔
4905
         result.test_bin_eq("kyber512 private key", privkey_read.get(), b_priv_bits);
1 ✔
4906
         result.test_bin_eq("kyber512 private key raw", privkey_read_raw.get(), b_priv_bits);
1 ✔
4907

4908
         ViewBytesSink pubkey_read;
1 ✔
4909
         ViewBytesSink pubkey_read_raw;
1 ✔
4910

4911
         botan_pubkey_t b_pub;
1 ✔
4912
         TEST_FFI_OK(botan_privkey_export_pubkey, (&b_pub, b_priv));
1 ✔
4913
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (b_pub, pubkey_read.delegate(), pubkey_read.callback()));
1 ✔
4914
         TEST_FFI_OK(botan_pubkey_view_raw, (b_pub, pubkey_read_raw.delegate(), pubkey_read_raw.callback()));
1 ✔
4915
         result.test_bin_eq("kyber512 public key b", pubkey_read.get(), b_pub_bits);
1 ✔
4916
         result.test_bin_eq("kyber512 raw public key b", pubkey_read_raw.get(), b_pub_bits);
1 ✔
4917

4918
         botan_pubkey_t a_pub;
1 ✔
4919
         TEST_FFI_OK(botan_pubkey_load_kyber, (&a_pub, a_pub_bits.data(), 800));
1 ✔
4920
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (a_pub, pubkey_read.delegate(), pubkey_read.callback()));
1 ✔
4921
         result.test_bin_eq("kyber512 public key a", pubkey_read.get(), a_pub_bits);
1 ✔
4922

4923
         TEST_FFI_OK(botan_pubkey_destroy, (a_pub));
1 ✔
4924
         TEST_FFI_OK(botan_pubkey_destroy, (b_pub));
1 ✔
4925
         TEST_FFI_OK(botan_privkey_destroy, (b_priv));
1 ✔
4926
      }
1 ✔
4927
};
4928

4929
class FFI_Kyber768_Test final : public FFI_Test {
1 ✔
4930
   public:
4931
      std::string name() const override { return "FFI Kyber768"; }
1 ✔
4932

4933
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
4934
         const std::vector<uint8_t> a_pub_bits = Botan::hex_decode(
1 ✔
4935
            "38d4851e5c010da39a7470bc1c80916f78c7bd5891dcd3b1ea84b6f051b346b803c1b97c94604020b7279b27836c3049ea0b9a3758510b7559593237ade72587462206b709f365848c96559326a5fe6c6f4cf531fd1a18477267f66ba14af20163c41f1138a01995147f271ddfc2be5361b281e6029d210584b2859b7383667284f767bb32782baad10933da0138a3a0660a149531c03f9c8ffccb33e3c3a5a7984e21fab26aa72a8a6b942f265e52551a9c800e5a44805f0c0141a05554213387f105df56458496bd8f469051886da223cb9fe78e7b390bf94b0a937691af9550082b76d045cb4d29c23c67942608d078a1c80f24767a945d19f077d82c9b9b197073464abe69cf7c5626177308f384672d5263b0c4826db4470e1a70e4751e3918abe8fcbc3bc0531ae89e5512214b5cc94a16a014bcb3826c79fbf4add0825eeefbab88cb7cff37bb8d491f8de902578a1e961655565b7718782a23504fdc13c783f130e177925e305d1fbc63cc8c15c2c67f85500cca785de9f480490558ef71aaf0fb5b513914401269b309c4c59c64d2a757d8855f58465615925f1ea6812cb143fff383e1048e285118bf932944b86fbdf4b1b9e65685664a07775c46952aaada1168f54b47c7a231e7355c64637467b5a3c09cab67bb35f58640c2726283bb63530a15f66eca48a840c00ca8862e283c73bfbb413a2915b8d1159a043f12c59bfa828248249b76106faa61a127a0280c586350e7a42cb74ca49cabd606891ec7cb8e84affe4b2e14c71658332b755611bab7977fa76ce736b21ed34a17ac0ec3561ca9b282d4a2bc407697924b1cf918ba83d3a4fdc82564c95bd904bdeee91ed6ccb36baa88a05c80712901bf280aee6538ec2078c2a84ee5862fc137cd92e97968d69fc3453a1e1cb161c50c9f2473a0d09037b188a0fa01efc344c2ac8fe8592b0a58456662a95033659a158a2d90a6e50c253a87975785ce29c4570000a154d4b3b2c642205c8c7cf9ac6b1071fbb368ab950a744b88c95ba5243017831120a9048338d29847830d12a933a09abd21a46b828cb14e808cd35129c9dc6e5b931d4a126fefe07909618e2b4586e7b6b424963b7323ba505ba112bb9b834a7d1b78ad0df53d556a1c69369f09148b1dc9938df59223f087fd6833be5b2bc2651fe58911ac01467f9297dfdc22b41a0f1702718710b78cf35b1865813a896d45214d338155b6c043c532330c002d520739467a504a866637fb3451c849f8f83e6a94147f168da53acdf9d8affd968a84124a9abc09af960cd3b29f2344831bb41e67605eebf00df202857117399dd748b6514aed61bb2f6cb841d168d5f35e20054573a331cd4882a04b072c179158825bcf471266da0dcceab1a021c73254751d5a161c1a92062c220a217a69d9823314b4de996fe8d45f6db5af16c1561495a4c43090bc394c94e1b0ec738eb56267201c2ecd1c7b4993c0efc0284bdc9a091c294f95703a7178822c8a95b79b1e4591e0998d893875c1a879c08a073cc67df426bba792c18ae6c1feba879bec54812c2affa012973b700ad48e271078280864268600a7aa309eaa1098750a0f8a522eb929577b412f7855613688b72f9bc85c0a13b9d041586fd583feb12afd5a402dd33b43543f5fa4eb436c8d");
1 ✔
4936
         const std::vector<uint8_t> b_priv_bits = Botan::hex_decode(
1 ✔
4937
            "9bc4986eec853ac90896c7300f914216773baa687f57f81f94e6b5a26515c0a74c73b19dc6b9888755a1d1db6a1128b02c5144ed9ba75e874a68a14dee3a9bf770b767121fbbdb292e097444757fe2f324a875b0a07a4fbdcc8fd6137fce80c69f412be103b01f8b3bed392600f830d1b20d73ca0b386666d16c5d96db10e309041890bfd4ab7bdec27191979abe7637e76153918cc2af1545724abfd95ca496281a0cc6ca63a87819b75aa86452e5027d429cad64a9c029112a3a7b9fb993c6a6d0671f6c9e24986b83f67cf1236d94c051559616826a947eaa15074025d1ab810c80220e8a8c2c222d4e8594bf35811b444a57e9bf94fb2479d174d6b38c8c3b4730437c244b2513232ec798adec4a8e597bca83bca5948468f93c022ba65311a91e3d95c299c70c3f1a43cd0c83dd500bd2211b9b385b82f2c003b10d295765b3e367563a5f19033fc1231db9620f835c95f5a261190a5da1c24ed528316f0a1520d223e208a8c99b24d28598ade74fc88370e7f45102c5a6411891c661b356b9a32e1cc0fafaa085d7670e8bcb5e768eb255204f2445b5b73b04079881903a553c865744343a925c7899777b1c9dd5579a481512f8157061606a9a67c041d38bc179048be17dd9e19dc0a572bce595afa3b68ff21bf78a63a7560636b6bb01ec3362e2aaabc8965818b7f2417ca8f66a5a2a67f72a3931e125d638a872862a7b680a54aa1f25d90dbd567635ec6664919e29517325a5c5048cc8d1c31af5e4866e85025b9184a7b75ed7f2c9c8d88259fa2ec5b05ed3751584f88308c67ff1a72fa2453305160baf404db7d4be56f1547b20bb7dec23f02b10db584b28ca40d8b39c1c3ab9f3d7bbda0822604ca48f26694d69810aa888ae3c0983c5ba4cb74211f7a5361ccdee694f4202aebe13a75b1b2dda1b3232376be213582afc4fde3474766671fe865e2fd98384eb65b2f349f1e24269b91bd9d08c80849735a9951304afd130b5c2211314630aed4b6ac3b1252a0999ff5a3ec26a283342389d219fd243706128b389eb2334fb2a6184a4eab6735d7428df5633ce030b8f853ee47c4401fc5d06b43c9b66b7aeeb23b5f000a30a6f88f027ee3994fe8b63e51b83bc24bb733a3773a35cbe138f6d9c91a3a3898bca6776030d740ac355176547d624719656a9a44e91c63faf7699dc6c2c45575718d48828828b39043c2fda2af416837efc38d17c56d4b63c63a5ab43434647d029f7b236b288958f06910763610f8b2f027a8dcd780039ab34a6871427476ff6500240e83b87c95dcfa45ac5315ef34b343fb609eb296e915c849bb8c57f57c69b177eaa8456377403fe8c6627a3282d45308f675d67085a15f0b1b55aa2a8f21afd6c05c3c00e9eb8c32418cb41963ce427b43e7545c58325c7b9368db2333de424dbeb3430f007d18a68d73b7dc67960b28206a68a1be400a770b5cd9d45a72824ca00345ac56491c1414fe5287a2eb2ad61f3bdcd0c84c335b04a703425d79dbd02b0a0e90de5b331c3c29f6562969e04cdf7095b2a7646b3d006b0b83cb68580b5ccb71de1b4d9f131bac133d6088e10613a00599d81d4818403a4bea83905304cc45ca645a9b2c6484cf9490f1755c744d9988ed60475e6ae44355ed15c7b549366f29581ec2721fd6704e0ca3f878812805675141c0a15a7b7ac35a9e3f8b2a010bc184981c57852895b2695d56131e32326717f6b101df1bc82b3ba0222d52656d118538c4ca3416be1c76ba37a9901a36e4883be6c541f2bbb561818cd2f136b98f658250545c1fa5bcfdc04374016db1c5132447fd6d568866451c25412f72967de868eaeb9c546fb40ea88cd84a1a586ca51c74bc9c3e56e104323afb658d1ac003151bdc35879a4b6762648bff0caa682f1b3319805d2326d5a46af832aacefbaa1ea820568ea3925870e9b6577eb93898e1b0cfbd1c995cb4cd6cabb979813819749b40a9952f50e97c4365e777dcfe9084219294c205acb350e07db9c98f53444546460962e2bf5aa1cc12273d882fcc215a7397b3f9b307c56b9a0429b30f88453a2376669b28b4bc4b84b51714b6652b7a1a0b53e9ec61b55ca51cdb38243239ee5f18243e515f178768a888f475b3d9060136bb22ee355b3da02c16ad83bdbb4aaa13809cb4bcd5bb53710737d3883632f9254e3336af61621a376720572450e3937c0d930e349adbfa7642ff822b9135e18f943e0178617604d10e0c09ffb3e09783c09d12cbe93311757af9857b77d1488ed39321ca97c3745c5bc176ca81274c8321bcb2029938b32bbd01aec137032f9760849701649120050b50d8353c36b8bb7724a67e7660fc93324065d63912c35a86d8fe60683067bbd2685c552ad8c65c77c57c937676fd61595c453174bf996e9d3a9ccb837b25464115c1ba3343ac097b80735aed3225091167cd8f841ffe49c5e698ef542124253084623179394433a4b61547b9ee09c98c2736ea086bd69d1bc7ae68a6ae5ce682a215860006b4604dee45a3e212f97643acb77a79a880382e483537c5198d4483a176d25aac9c3670a3f30956f18ad441904776bcd48131c7d6465bce0c133010f3176c92ec962f5c6b84d4c3ae949619cf48172997ca3b1ccf5c8cc7a67923e1295801048a3d40ac4f2c6467c750fc71314a0c1fc22637dd52e7ea50907ea973d765a6a9bb2b11aa405b9187f72026696710e61af3e41c33da1a05eb65e6523704f078e74e32f10e00247967aee3a8c6546889ef67cd613ad7236583f2104122ac6c6a40a84dc96d81c569e76a952c0a25f396e48337a4fe029a4c91cc7406872706a55573b75f160a4facad7c85fab141c63454bf48990729096ce9965604c7cc1e60ae6868dcc41bc3df71c3e5593f0488b0c6a3063e817f9f4bacb17599c8666ff3591126b4891fb7f5d29660bab60cf5007043a4311d41ab3b29787184f3d3c9ab7cc247f635145b67e970505ba44ad0e06b11ec5cda4175295199d19d660204cbdc17947cc66442d3a2cd408a20fe98174f31ee4e5bb3fa8bcd102bedc26527e9bae836442978a6ccbe510f93ab77569ab1f09d0e6312dd0cc0bcaf095fa8a52a7212d14714a7bf852416c9b026301bd965c30a43d24d97298346a46b2c4bc814ba4059653358b03c9456c60bf0193932eaa2f24ea8e4b010a5a4425ce4540fbab90d8e55c97ac2687f15ff5299278824a08d4743e1a62e1c6619cd3278cd75a97a5b4e3a38668b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb3339ae9c9ce46d9da0e714c0bae8712a670d0e5dcfdd1dd0d045932c79c559b2ab3c7300e2d894b0eaa40a6ab254506d8c1176a33c4a1b2879604b1b80df48d31dd");
1 ✔
4938
         const std::vector<uint8_t> b_pub_bits = Botan::hex_decode(
1 ✔
4939
            "f9490f1755c744d9988ed60475e6ae44355ed15c7b549366f29581ec2721fd6704e0ca3f878812805675141c0a15a7b7ac35a9e3f8b2a010bc184981c57852895b2695d56131e32326717f6b101df1bc82b3ba0222d52656d118538c4ca3416be1c76ba37a9901a36e4883be6c541f2bbb561818cd2f136b98f658250545c1fa5bcfdc04374016db1c5132447fd6d568866451c25412f72967de868eaeb9c546fb40ea88cd84a1a586ca51c74bc9c3e56e104323afb658d1ac003151bdc35879a4b6762648bff0caa682f1b3319805d2326d5a46af832aacefbaa1ea820568ea3925870e9b6577eb93898e1b0cfbd1c995cb4cd6cabb979813819749b40a9952f50e97c4365e777dcfe9084219294c205acb350e07db9c98f53444546460962e2bf5aa1cc12273d882fcc215a7397b3f9b307c56b9a0429b30f88453a2376669b28b4bc4b84b51714b6652b7a1a0b53e9ec61b55ca51cdb38243239ee5f18243e515f178768a888f475b3d9060136bb22ee355b3da02c16ad83bdbb4aaa13809cb4bcd5bb53710737d3883632f9254e3336af61621a376720572450e3937c0d930e349adbfa7642ff822b9135e18f943e0178617604d10e0c09ffb3e09783c09d12cbe93311757af9857b77d1488ed39321ca97c3745c5bc176ca81274c8321bcb2029938b32bbd01aec137032f9760849701649120050b50d8353c36b8bb7724a67e7660fc93324065d63912c35a86d8fe60683067bbd2685c552ad8c65c77c57c937676fd61595c453174bf996e9d3a9ccb837b25464115c1ba3343ac097b80735aed3225091167cd8f841ffe49c5e698ef542124253084623179394433a4b61547b9ee09c98c2736ea086bd69d1bc7ae68a6ae5ce682a215860006b4604dee45a3e212f97643acb77a79a880382e483537c5198d4483a176d25aac9c3670a3f30956f18ad441904776bcd48131c7d6465bce0c133010f3176c92ec962f5c6b84d4c3ae949619cf48172997ca3b1ccf5c8cc7a67923e1295801048a3d40ac4f2c6467c750fc71314a0c1fc22637dd52e7ea50907ea973d765a6a9bb2b11aa405b9187f72026696710e61af3e41c33da1a05eb65e6523704f078e74e32f10e00247967aee3a8c6546889ef67cd613ad7236583f2104122ac6c6a40a84dc96d81c569e76a952c0a25f396e48337a4fe029a4c91cc7406872706a55573b75f160a4facad7c85fab141c63454bf48990729096ce9965604c7cc1e60ae6868dcc41bc3df71c3e5593f0488b0c6a3063e817f9f4bacb17599c8666ff3591126b4891fb7f5d29660bab60cf5007043a4311d41ab3b29787184f3d3c9ab7cc247f635145b67e970505ba44ad0e06b11ec5cda4175295199d19d660204cbdc17947cc66442d3a2cd408a20fe98174f31ee4e5bb3fa8bcd102bedc26527e9bae836442978a6ccbe510f93ab77569ab1f09d0e6312dd0cc0bcaf095fa8a52a7212d14714a7bf852416c9b026301bd965c30a43d24d97298346a46b2c4bc814ba4059653358b03c9456c60bf0193932eaa2f24ea8e4b010a5a4425ce4540fbab90d8e55c97ac2687f15ff5299278824a08d4743e1a62e1c6619cd3278cd75a97a5b4e3a38668b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb333");
1 ✔
4940

4941
         botan_privkey_t b_priv;
1 ✔
4942
         if(!TEST_FFI_INIT(botan_privkey_load_kyber, (&b_priv, b_priv_bits.data(), 2400))) {
1 ✔
4943
            return;
×
4944
         }
4945

4946
         ViewBytesSink privkey_read;
1 ✔
4947
         ViewBytesSink privkey_read_raw;
1 ✔
4948
         TEST_FFI_OK(botan_privkey_view_kyber_raw_key, (b_priv, privkey_read.delegate(), privkey_read.callback()));
1 ✔
4949
         TEST_FFI_OK(botan_privkey_view_raw, (b_priv, privkey_read_raw.delegate(), privkey_read_raw.callback()));
1 ✔
4950
         result.test_bin_eq("kyber768 private key", privkey_read.get(), b_priv_bits);
1 ✔
4951
         result.test_bin_eq("kyber768 private key raw", privkey_read_raw.get(), b_priv_bits);
1 ✔
4952

4953
         ViewBytesSink pubkey_read;
1 ✔
4954
         ViewBytesSink pubkey_read_raw;
1 ✔
4955

4956
         botan_pubkey_t b_pub;
1 ✔
4957
         TEST_FFI_OK(botan_privkey_export_pubkey, (&b_pub, b_priv));
1 ✔
4958
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (b_pub, pubkey_read.delegate(), pubkey_read.callback()));
1 ✔
4959
         TEST_FFI_OK(botan_pubkey_view_raw, (b_pub, pubkey_read_raw.delegate(), pubkey_read_raw.callback()));
1 ✔
4960
         result.test_bin_eq("kyber768 public key b", pubkey_read.get(), b_pub_bits);
1 ✔
4961
         result.test_bin_eq("kyber768 public key raw b", pubkey_read_raw.get(), b_pub_bits);
1 ✔
4962

4963
         botan_pubkey_t a_pub;
1 ✔
4964
         TEST_FFI_OK(botan_pubkey_load_kyber, (&a_pub, a_pub_bits.data(), 1184));
1 ✔
4965
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (a_pub, pubkey_read.delegate(), pubkey_read.callback()));
1 ✔
4966
         result.test_bin_eq("kyber768 public key a", pubkey_read.get(), a_pub_bits);
1 ✔
4967

4968
         TEST_FFI_OK(botan_pubkey_destroy, (a_pub));
1 ✔
4969
         TEST_FFI_OK(botan_pubkey_destroy, (b_pub));
1 ✔
4970
         TEST_FFI_OK(botan_privkey_destroy, (b_priv));
1 ✔
4971
      }
1 ✔
4972
};
4973

4974
class FFI_Kyber1024_Test final : public FFI_Test {
1 ✔
4975
   public:
4976
      std::string name() const override { return "FFI Kyber1024"; }
1 ✔
4977

4978
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1 ✔
4979
         const std::vector<uint8_t> a_pub_bits = Botan::hex_decode(
1 ✔
4980
            "9779a4d1fc45ec261f048b9c9daa38c9ec228b6505e8905226b38486802059c2c5c89601560634cb1337b1315365144842bc405a292e683cafa4514526945c4dfb68ee2acdb8b79532836696d53125a045bdbb8a3bcc8123083d1e682c5bd7820c76c448351151474f69d601d7708dbb2c979d77527494b68520a8ff66c34162ca2aec8072a2a51ff259389648e75b95c16abe14604edbfabaf400b76a7a0f07db61dce19102f43b2d1060747b02c4425485341fd5d9bffaa7016061374963b985209c6b9a7db3f94958311d027900a3d8c44f8435b093a236a0509f1928df7719ce8c4e90228f4db87cb9e882f2712180238845d31eb906c60eb63e0ff55e84b867a91b79ae74a03ac00473c8c1b3e6aedcc37f1e69b8e136019bdb01c374a122e164c32584b2fbcf5e013a45127c893326b6860378cb5525a48b522b30132b7688214b69808dd19aa4ff033e16252016fc9479222424393e51db7115e38114e6893cdcc8ae1117a4316548010ab4629fb672148d031a6c601a6a4a661d872d8ef693750a115958716ab9263eb0516357102358cde5256464805ca8f59661751ad6a475a7ec78cb7319c3bbc544e1bb1185aaaeb751ba0b3923246e33f40a4ff4780b745362a218d169474d7208a9f7577228308af20f8d2403a27477cf53cff133a247d5c2b298bf21bac6dc44649e63afcef54ad3a07a74e447b36bacca295e053368ef7c146e1c28edb31b3777c941a7c27dc559437b0ff83c2910f827ef244d7726af2b9708654bcfb139a26844c6f4b79cd9e4747065a77596b927f96cbdbc7267ac9e32ac396f6c4ce739aa0aa60fccac178edab1e04688fe71a74201a99bc64b55f8cb89e11545e5275af48626ec667520ba8a7c88c2307e793455ef8780fae16412ea59e92406f595902dc21c612b142801cf31abc0475c7bde772d51c55a2dfc651bc5ab4143022813cc4c8cc6f52c8d27e3cd7851849a8c4343f0c7ed6c6cb9f765c5047e55bc48aad5b932128287f70939db753a61c0d7db1830c256288c1c85cc9bbce58366dac528c893d61b69850bcb82758e836936e1f8163861586482cad35b4db1437fad6980b66280b1652d72f52407a8015f85c29e75b2158933a4f5887db320b583cdcbca274eac21f8702879b8bc4afb62ba056d146512921554c765464d4c96bd3c9a9720187c3339d0593bc4bba59155469ff6b8688a5fc5fa6b46d4070668bd168c5f796492760940999927628236b2412968438d2cbc2a978abc097320291b0ed7e4631ec9bbe28ac63ab6860e976ac1552afc43897d6937a092432dc481a914c3a1273c43009e8ec523cb93ced9898f90905340ba6e01bc572d03a893fa21c4342d88dc3e77b33b1fc063dcdb689d8c4ffc840ce879cfb3471385512cc7c7591383b1fac89832bb1cb3324e6d2868918844cb20ca4df812b42824192a43380430a37a7f3ac41ed064cfe055157b91b5c0108fb7a613a0112af4d64e48f8a345328a99b7bf0c91cb037215b0177ad4c263e6d78f5958c848158e4fd2117c248e0cab3cf98c1e27868838f8428ba0562df6b61e8736e2b8b266607918e0ce2ce3af67c81fa5c2a4d2bc8e871825b702b3beca397b33a518dac8b1393d494a90900c01b55925857cbf8027051d36a4f141a4d2dc440341305e033cb50a074be459c76a339a9a52c447360dea2a5699ccea683a42430aa6fc9545c75c0492641af7a4e9267bcc384e55714cf49741eba3b69d6417a555475d3c021112b8b3588c63747b5ce2ccfee91297fa419f9c4298978fff0870d8a855b48899ca9bb47d836d62d2038cc3816f3a698bb3bbff78c7a015b0ea1960634292e1d50b03e1043a98ca9b27063e668b05e2c17d692d382b181365a818518ec747720337ca1868596af42a90fab1870373d74b8f6d42ac86b18bca3ab04764680cf2060c529abe5b8ef4474dc8cc47a8033ebd884d3e0a0f1a94420d8a3c9162b7af87a2c8a394647434f4a3bc2b477813ac82cf387185b587f7f68938eccddd6934d143ba17bb4a712566d2a55aaddb3323713667401b4a20b86c23bf1076439cb6b8c115389dba4e6f0c915be7602b9703b535070e4a5c5649a5d7080515026706b2604575cbd0687f2729a92a997d21ea7200c41ed8315275f4c7b72f9bc85c0a13b9d041586fd583feb12afd5a402dd33b43543f5fa4eb436c8d");
1 ✔
4981
         const std::vector<uint8_t> b_priv_bits = Botan::hex_decode(
1 ✔
4982
            "9bc4986eec853ac90896c7300f914216773baa687f57f81f94e6b5a26515c0a74c73b19dc6b9888755a1d1db6a1128b02c5144ed9ba75e874a68a14dee3a9bf770b767121fbbdb292e097444757fe2f324a875b0a07a4fbdcc8fd6137fce80c69f412be103b01f8b3bed392600f830d1b20d73ca0b386666d16c5d96db10e309041890bfd4ab7bdec27191979abe7637e76153918cc2af1545724abfd95ca496281a0cc6ca63a87819b75aa86452e5027d429cad64a9c029112a3a7b9fb993c6a6d0671f6c9e24986b83f67cf1236d94c051559616826a947eaa15074025d1ab810c80220e8a8c2c222d4e8594bf35811b444a57e9bf94fb2479d174d6b38c8c3b4730437c244b2513232ec798adec4a8e597bca83bca5948468f93c022ba65311a91e3d95c299c70c3f1a43cd0c83dd500bd2211b9b385b82f2c003b10d295765b3e367563a5f19033fc1231db9620f835c95f5a261190a5da1c24ed528316f0a1520d223e208a8c99b24d28598ade74fc88370e7f45102c5a6411891c661b356b9a32e1cc0fafaa085d7670e8bcb5e768eb255204f2445b5b73b04079881903a553c865744343a925c7899777b1c9dd5579a481512f8157061606a9a67c041d38bc179048be17dd9e19dc0a572bce595afa3b68ff21bf78a63a7560636b6bb01ec3362e2aaabc8965818b7f2417ca8f66a5a2a67f72a3931e125d638a872862a7b680a54aa1f25d90dbd567635ec6664919e29517325a5c5048cc8d1c31af5e4866e85025b9184a7b75ed7f2c9c8d88259fa2ec5b05ed3751584f88308c67ff1a72fa2453305160baf404db7d4be56f1547b20bb7dec23f02b10db584b28ca40d8b39c1c3ab9f3d7bbda0822604ca48f26694d69810aa888ae3c0983c5ba4cb74211f7a5361ccdee694f4202aebe13a75b1b2dda1b3232376be213582afc4fde3474766671fe865e2fd98384eb65b2f349f1e24269b91bd9d08c80849735a9951304afd130b5c2211314630aed4b6ac3b1252a0999ff5a3ec26a283342389d219fd243706128b389eb2334fb2a6184a4eab6735d7428df5633ce030b8f853ee47c4401fc5d06b43c9b66b7aeeb23b5f000a30a6f88f027ee3994fe8b63e51b83bc24bb733a3773a35cbe138f6d9c91a3a3898bca6776030d740ac355176547d624719656a9a44e91c63faf7699dc6c2c45575718d48828828b39043c2fda2af416837efc38d17c56d4b63c63a5ab43434647d029f7b236b288958f06910763610f8b2f027a8dcd780039ab34a6871427476ff6500240e83b87c95dcfa45ac5315ef34b343fb609eb296e915c849bb8c57f57c69b177eaa8456377403fe8c6627a3282d45308f675d67085a15f0b1b55aa2a8f21afd6c05c3c00e9eb8c32418cb41963ce427b43e7545c58325c7b9368db2333de424dbeb3430f007d18a68d73b7dc67960b28206a68a1be400a770b5cd9d45a72824ca00345ac56491c1414fe5287a2eb2ad61f3bdcd0c84c335b04a703425d79dbd02b0a0e90de5b331c3c29f6562969e04cdf7095b2a7646b3d006b0b83cb68580b5ccb71de1b4d9f131bac133d6088e10613a00599d81d4818403a4bea83905304cc45ca645a9b2c6484ce4f7bfbe3597f0c2c50784065abc214b5518ed8c7d427205392987d807cdfa571d09932e0a1217e7c5703dfc9be7c4a0f2f33338f316b8c203c6b84285a0957c363d3722145b592f9d6277b1b7446aa2005663b9898901da3b8978eaa8aee87a209c72bfd6cd10a2bedac527f26c419b4bb9a4ca13dba14cff5671461c4cff238d213988120c3cae3b15b607269420c14a9b23a6bb887ad048e6688528137dd5c071172bcaddd5ba4fb9acfb1712ca04aa267b0686a237414b16c4ac03525a45513a393605a29f3b4c07dc5cd082c67b770e69723c6425a23f573216c4cabd6c9c39533f16643bc6151672c466dea63fb8e2180fbcb453c5670b01c0fc7043d198b6c320bf0dfc5016d00f46dc0ba75c7369fc13b65733301b3a92c0cf8dcc1f01e33291a9974d5b5debfa3be5531c69a066a6f4230ba778502654b7e76191d3ba68b1488c848d8ef11bb9ac5a5374af6ccacd02432b247a963685127728825a162c43c1cc79a7b60f571481a3a635a18f0050cbe6f4af1a5a2224044622bb3893bc837be699d5279dac78c9d49818ebc91d4ff6cb54b294e4c378d8eb584f561d21ca87b7c77b1c551f52112ba72c4b6514bb286856e89b48b0a62bad093894474b246a515f849056c9aeb572060ee11b93e236204a8ff1784ede0c3f4c0cd0cdb2a50bd4bcb1225c0dd2caa8a129fdcb01f0e1a75da465b6b8742e4946dbe3ab21ca6c4e592d398a5700375b7b770c17e117663b51313824854585ece4a1520c59a513555f548afdc6748eb9b770b7af8adc9b3c1861efc00ebde93f91fa1539c3411ec9b154c93d1f508b9714cdd0aaabcb436951aa54978b84faa8bd838c29a12657a6389fa658237326bf80e44cff183d9c9107d30c7b4df83908a8badbc1bb5328ae61e6451291710408196261bb913b285c168fe5c7456cd74970377330139beb4c8b4c00c5954254a748308bc30f3a1ccabed83628422e5515b294e80bfed024902a81518c0e6827caa13230016b72aec10d31aab9f9b072652486c3b779c845804d001b223b4065667a5e318b4e0b27c41a099168ce763ac245c1c26354cfc8214cacf01dc8c99c5fc855639cb7486a7a6b71766d62369ee6349f521591344fb8026099fc055c3a0ca2002271c9361f52acaf4675a97b3593f3447b284ad69a9711501411e14e48f080e2a0bceba07a02f79dc15c3c971c1e671a66bf7144395a94d08c46603c4369b25b38d924693492f6500745cb1d2a037e2cd814fac70d8254aea4526e061648d227350dec1ba6305299a45bde4029a9f63e4f62acb5246ba8b54208a37b98fa77e4b720f02b18969635cc16cb6e236ff78111b621cab0339c60b529a9207c94a74925aa3d177636a6d205d72b429f614d452358b98582e42c2f3a32c94202a8625a760483807bbac74c083a7a786ae7b46aff3810a5318e3f479e67e83b4c94587d345ffda17273a55e0432281c670e36b76539263636445e4d110fa1c92b2e65b1ee993b26c41c8c66b181206ff559728993503e7611fa0ca24156642d29a52cbb6aeff88fa1bb790b4344472503ae77967d11a904550a95cc47c3435f1e44b4b890cf5dc4cbd7486a6cab60d1a6622e40c67e854ed0e6a0c4c2aa92a29731e69bc6b3ca9e83893df4bdb37338055bc16bc7cd875b0691d0212109b77236b10b0c72b7a76e9ce346061028be3271c480435e2482d3d2733ed619c2d3698d29a3225a7731a62db4f214a0b11569e91895fbccb8a92d18f94d307a542ff96d2ce8c2bbaa8fab948c83365162268124cabe934b3a5e6530b7f74397a980c243bdce2989747565dd9ba65a7498aa7c3ee711c3be239e93cb45134c082a900cc951756afa42e74a65001822cab95f41798fdef89ea8a751740842e2f7a271e98dbd5158d7455fe9b9cdcd0a0f68c91e59b00979673a1deabf8d213971f994c0bb08899a91e0b896319b9fc69651e3453b14d1cc94462341548fc81c643ab0b10303c7be2c3458b226d0e45f58b21658a853b84615ac5234da9bc5699646fc53c0df907f9a3c598dab6c409b7884a7a84e1a51c2caa80779cd86b77314dabae85caa28972bb6503273a82eb96ab24af6bb0ad0c9ba07ce24216696e265206abc9e147ceef1ad2e343d17aabbc4803496366c7afb53f604c92323c70be5c1e3f601c215c888f3ab239c72aba0634a2901a71891f5d072d41b524e410c7f276ff1109878d7196b5615f1f50c13976257600a821909cb36cf8d2857a2f801cf258913f56a5153234640b9e61bbd4e7b69f83a1eb8ac6b92cb841e808b700aa784a733451048211797563cccc748c9fbb54b3b65445cba676149c7ae9a9a2ecab6599b8505bc561ad85d8c052ed9669e50e684270c86f1cb4fd6708a6746c643392c8a088477dc8bf58ba4a54a8b79e523fc19a28bd47d7e5a334d8296ec86c7f52b7e73b475a0716422953cdd8a8aed0b3a84dc8425145c240c55870240a23977215b364c5596286bc496728717c329dd4b5d0b310980bc0b3f591a2d5cb2c9eaccfa1c7d3a096fc11091a4007a0f23a59782699c3171da53bc7b914f26c95391d6445073a1bd7a44691bafab9c9aceccc7ec389255c3a0ff24c71b30b6bf80c010803383485a7b295991d759cefbae257bbdee1806818565ebd09bf814c98686bbf44a0b14d28735f79ca2261bf9a31b2ca090c7667168b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb33357e74a31d3cd71513fb880e1e177438f29009bab0a131fec4cc24752015efe71c7300e2d894b0eaa40a6ab254506d8c1176a33c4a1b2879604b1b80df48d31dd");
1 ✔
4983
         const std::vector<uint8_t> b_pub_bits = Botan::hex_decode(
1 ✔
4984
            "93bc837be699d5279dac78c9d49818ebc91d4ff6cb54b294e4c378d8eb584f561d21ca87b7c77b1c551f52112ba72c4b6514bb286856e89b48b0a62bad093894474b246a515f849056c9aeb572060ee11b93e236204a8ff1784ede0c3f4c0cd0cdb2a50bd4bcb1225c0dd2caa8a129fdcb01f0e1a75da465b6b8742e4946dbe3ab21ca6c4e592d398a5700375b7b770c17e117663b51313824854585ece4a1520c59a513555f548afdc6748eb9b770b7af8adc9b3c1861efc00ebde93f91fa1539c3411ec9b154c93d1f508b9714cdd0aaabcb436951aa54978b84faa8bd838c29a12657a6389fa658237326bf80e44cff183d9c9107d30c7b4df83908a8badbc1bb5328ae61e6451291710408196261bb913b285c168fe5c7456cd74970377330139beb4c8b4c00c5954254a748308bc30f3a1ccabed83628422e5515b294e80bfed024902a81518c0e6827caa13230016b72aec10d31aab9f9b072652486c3b779c845804d001b223b4065667a5e318b4e0b27c41a099168ce763ac245c1c26354cfc8214cacf01dc8c99c5fc855639cb7486a7a6b71766d62369ee6349f521591344fb8026099fc055c3a0ca2002271c9361f52acaf4675a97b3593f3447b284ad69a9711501411e14e48f080e2a0bceba07a02f79dc15c3c971c1e671a66bf7144395a94d08c46603c4369b25b38d924693492f6500745cb1d2a037e2cd814fac70d8254aea4526e061648d227350dec1ba6305299a45bde4029a9f63e4f62acb5246ba8b54208a37b98fa77e4b720f02b18969635cc16cb6e236ff78111b621cab0339c60b529a9207c94a74925aa3d177636a6d205d72b429f614d452358b98582e42c2f3a32c94202a8625a760483807bbac74c083a7a786ae7b46aff3810a5318e3f479e67e83b4c94587d345ffda17273a55e0432281c670e36b76539263636445e4d110fa1c92b2e65b1ee993b26c41c8c66b181206ff559728993503e7611fa0ca24156642d29a52cbb6aeff88fa1bb790b4344472503ae77967d11a904550a95cc47c3435f1e44b4b890cf5dc4cbd7486a6cab60d1a6622e40c67e854ed0e6a0c4c2aa92a29731e69bc6b3ca9e83893df4bdb37338055bc16bc7cd875b0691d0212109b77236b10b0c72b7a76e9ce346061028be3271c480435e2482d3d2733ed619c2d3698d29a3225a7731a62db4f214a0b11569e91895fbccb8a92d18f94d307a542ff96d2ce8c2bbaa8fab948c83365162268124cabe934b3a5e6530b7f74397a980c243bdce2989747565dd9ba65a7498aa7c3ee711c3be239e93cb45134c082a900cc951756afa42e74a65001822cab95f41798fdef89ea8a751740842e2f7a271e98dbd5158d7455fe9b9cdcd0a0f68c91e59b00979673a1deabf8d213971f994c0bb08899a91e0b896319b9fc69651e3453b14d1cc94462341548fc81c643ab0b10303c7be2c3458b226d0e45f58b21658a853b84615ac5234da9bc5699646fc53c0df907f9a3c598dab6c409b7884a7a84e1a51c2caa80779cd86b77314dabae85caa28972bb6503273a82eb96ab24af6bb0ad0c9ba07ce24216696e265206abc9e147ceef1ad2e343d17aabbc4803496366c7afb53f604c92323c70be5c1e3f601c215c888f3ab239c72aba0634a2901a71891f5d072d41b524e410c7f276ff1109878d7196b5615f1f50c13976257600a821909cb36cf8d2857a2f801cf258913f56a5153234640b9e61bbd4e7b69f83a1eb8ac6b92cb841e808b700aa784a733451048211797563cccc748c9fbb54b3b65445cba676149c7ae9a9a2ecab6599b8505bc561ad85d8c052ed9669e50e684270c86f1cb4fd6708a6746c643392c8a088477dc8bf58ba4a54a8b79e523fc19a28bd47d7e5a334d8296ec86c7f52b7e73b475a0716422953cdd8a8aed0b3a84dc8425145c240c55870240a23977215b364c5596286bc496728717c329dd4b5d0b310980bc0b3f591a2d5cb2c9eaccfa1c7d3a096fc11091a4007a0f23a59782699c3171da53bc7b914f26c95391d6445073a1bd7a44691bafab9c9aceccc7ec389255c3a0ff24c71b30b6bf80c010803383485a7b295991d759cefbae257bbdee1806818565ebd09bf814c98686bbf44a0b14d28735f79ca2261bf9a31b2ca090c7667168b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb333");
1 ✔
4985

4986
         botan_privkey_t b_priv;
1 ✔
4987
         if(!TEST_FFI_INIT(botan_privkey_load_kyber, (&b_priv, b_priv_bits.data(), 3168))) {
1 ✔
4988
            return;
×
4989
         }
4990

4991
         ViewBytesSink privkey_read;
1 ✔
4992
         ViewBytesSink privkey_read_raw;
1 ✔
4993
         TEST_FFI_OK(botan_privkey_view_kyber_raw_key, (b_priv, privkey_read.delegate(), privkey_read.callback()));
1 ✔
4994
         TEST_FFI_OK(botan_privkey_view_raw, (b_priv, privkey_read_raw.delegate(), privkey_read_raw.callback()));
1 ✔
4995
         result.test_bin_eq("kyber1024 private key", privkey_read.get(), b_priv_bits);
1 ✔
4996
         result.test_bin_eq("kyber1024 private key raw", privkey_read_raw.get(), b_priv_bits);
1 ✔
4997

4998
         ViewBytesSink pubkey_read;
1 ✔
4999
         ViewBytesSink pubkey_read_raw;
1 ✔
5000

5001
         botan_pubkey_t b_pub;
1 ✔
5002
         TEST_FFI_OK(botan_privkey_export_pubkey, (&b_pub, b_priv));
1 ✔
5003
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (b_pub, pubkey_read.delegate(), pubkey_read.callback()));
1 ✔
5004
         TEST_FFI_OK(botan_pubkey_view_raw, (b_pub, pubkey_read_raw.delegate(), pubkey_read_raw.callback()));
1 ✔
5005
         result.test_bin_eq("kyber1024 public key b", pubkey_read.get(), b_pub_bits);
1 ✔
5006
         result.test_bin_eq("kyber1024 public key raw b", pubkey_read_raw.get(), b_pub_bits);
1 ✔
5007

5008
         botan_pubkey_t a_pub;
1 ✔
5009
         TEST_FFI_OK(botan_pubkey_load_kyber, (&a_pub, a_pub_bits.data(), 1568));
1 ✔
5010
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (a_pub, pubkey_read.delegate(), pubkey_read.callback()));
1 ✔
5011
         result.test_bin_eq("kyber1024 public key a", pubkey_read.get(), a_pub_bits);
1 ✔
5012

5013
         TEST_FFI_OK(botan_pubkey_destroy, (a_pub));
1 ✔
5014
         TEST_FFI_OK(botan_pubkey_destroy, (b_pub));
1 ✔
5015
         TEST_FFI_OK(botan_privkey_destroy, (b_priv));
1 ✔
5016
      }
1 ✔
5017
};
5018

5019
class FFI_ML_KEM_Test final : public FFI_KEM_Roundtrip_Test {
1 ✔
5020
   public:
5021
      std::string name() const override { return "FFI ML-KEM"; }
1 ✔
5022

5023
   private:
5024
      const char* algo() const override { return "ML-KEM"; }
3 ✔
5025

5026
      privkey_loader_fn_t private_key_load_function() const override { return botan_privkey_load_ml_kem; }
3 ✔
5027

5028
      pubkey_loader_fn_t public_key_load_function() const override { return botan_pubkey_load_ml_kem; }
3 ✔
5029

5030
      std::vector<const char*> modes() const override { return {"ML-KEM-512", "ML-KEM-768", "ML-KEM-1024"}; }
1 ✔
5031
};
5032

5033
class FFI_FrodoKEM_Test final : public FFI_KEM_Roundtrip_Test {
1 ✔
5034
   public:
5035
      std::string name() const override { return "FFI FrodoKEM"; }
1 ✔
5036

5037
   protected:
5038
      const char* algo() const override { return "FrodoKEM"; }
12 ✔
5039

5040
      privkey_loader_fn_t private_key_load_function() const override { return botan_privkey_load_frodokem; }
12 ✔
5041

5042
      pubkey_loader_fn_t public_key_load_function() const override { return botan_pubkey_load_frodokem; }
12 ✔
5043

5044
      std::vector<const char*> modes() const override {
1 ✔
5045
         return std::vector{
1 ✔
5046
            "FrodoKEM-640-SHAKE",
5047
            "FrodoKEM-976-SHAKE",
5048
            "FrodoKEM-1344-SHAKE",
5049
            "eFrodoKEM-640-SHAKE",
5050
            "eFrodoKEM-976-SHAKE",
5051
            "eFrodoKEM-1344-SHAKE",
5052
            "FrodoKEM-640-AES",
5053
            "FrodoKEM-976-AES",
5054
            "FrodoKEM-1344-AES",
5055
            "eFrodoKEM-640-AES",
5056
            "eFrodoKEM-976-AES",
5057
            "eFrodoKEM-1344-AES",
5058
         };
1 ✔
5059
      }
5060
};
5061

5062
class FFI_ML_DSA_Test final : public FFI_Signature_Roundtrip_Test {
1 ✔
5063
   public:
5064
      std::string name() const override { return "FFI ML-DSA"; }
1 ✔
5065

5066
   private:
5067
      const char* algo() const override { return "ML-DSA"; }
3 ✔
5068

5069
      privkey_loader_fn_t private_key_load_function() const override { return botan_privkey_load_ml_dsa; }
3 ✔
5070

5071
      pubkey_loader_fn_t public_key_load_function() const override { return botan_pubkey_load_ml_dsa; }
3 ✔
5072

5073
      std::vector<const char*> modes() const override {
1 ✔
5074
         return {
1 ✔
5075
            "ML-DSA-4x4",
5076
            "ML-DSA-6x5",
5077
            "ML-DSA-8x7",
5078
         };
1 ✔
5079
      }
5080

5081
      const char* hash_algo_or_padding() const override { return ""; }
9 ✔
5082
};
5083

5084
class FFI_SLH_DSA_Test final : public FFI_Signature_Roundtrip_Test {
1 ✔
5085
   public:
5086
      std::string name() const override { return "FFI SLH-DSA"; }
1 ✔
5087

5088
   private:
5089
      const char* algo() const override { return "SLH-DSA"; }
12 ✔
5090

5091
      privkey_loader_fn_t private_key_load_function() const override { return botan_privkey_load_slh_dsa; }
12 ✔
5092

5093
      pubkey_loader_fn_t public_key_load_function() const override { return botan_pubkey_load_slh_dsa; }
12 ✔
5094

5095
      std::vector<const char*> modes() const override {
1 ✔
5096
         auto modes = std::vector{
1 ✔
5097
            "SLH-DSA-SHA2-128f",
5098
            "SLH-DSA-SHAKE-128f",
5099
            "SLH-DSA-SHA2-192f",
5100
            "SLH-DSA-SHAKE-192f",
5101
            "SLH-DSA-SHA2-256f",
5102
            "SLH-DSA-SHAKE-256f",
5103
         };
1 ✔
5104

5105
         if(Test::run_long_tests()) {
1 ✔
5106
            modes = Botan::concat(modes,
3 ✔
5107
                                  std::vector{
1 ✔
5108
                                     "SLH-DSA-SHA2-128s",
5109
                                     "SLH-DSA-SHA2-192s",
5110
                                     "SLH-DSA-SHA2-256s",
5111
                                     "SLH-DSA-SHAKE-128s",
5112
                                     "SLH-DSA-SHAKE-192s",
5113
                                     "SLH-DSA-SHAKE-256s",
5114
                                  });
2 ✔
5115
         }
5116

5117
         return modes;
1 ✔
5118
      }
×
5119

5120
      const char* hash_algo_or_padding() const override { return ""; }
36 ✔
5121
};
5122

5123
class FFI_Classic_McEliece_Test final : public FFI_KEM_Roundtrip_Test {
1 ✔
5124
   public:
5125
      std::string name() const override { return "FFI Classic McEliece"; }
1 ✔
5126

5127
   protected:
5128
      const char* algo() const override { return "ClassicMcEliece"; }
16 ✔
5129

5130
      privkey_loader_fn_t private_key_load_function() const override { return botan_privkey_load_classic_mceliece; }
16 ✔
5131

5132
      pubkey_loader_fn_t public_key_load_function() const override { return botan_pubkey_load_classic_mceliece; }
16 ✔
5133

5134
      std::vector<const char*> modes() const override {
1 ✔
5135
         auto modes = std::vector{
1 ✔
5136
            "348864f",
5137
            "460896f",
5138
         };
1 ✔
5139
         if(Test::run_long_tests()) {
1 ✔
5140
            modes = Botan::concat(modes,
3 ✔
5141
                                  std::vector{
1 ✔
5142
                                     "348864",
5143
                                     "460896",
5144
                                     "6688128",
5145
                                     "6688128f",
5146
                                     "6688128pc",
5147
                                     "6688128pcf",
5148
                                     "6960119",
5149
                                     "6960119f",
5150
                                     "6960119pc",
5151
                                     "6960119pcf",
5152
                                     "8192128",
5153
                                     "8192128f",
5154
                                     "8192128pc",
5155
                                     "8192128pcf",
5156
                                  });
2 ✔
5157
         }
5158
         return modes;
1 ✔
5159
      }
×
5160
};
5161

5162
class FFI_ElGamal_Test final : public FFI_Test {
1 ✔
5163
   public:
5164
      std::string name() const override { return "FFI ElGamal"; }
1 ✔
5165

5166
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
5167
         botan_privkey_t priv;
1 ✔
5168

5169
         if(TEST_FFI_INIT(botan_privkey_create, (&priv, "ElGamal", "modp/ietf/1024", rng))) {
1 ✔
5170
            do_elgamal_test(priv, rng, result);
1 ✔
5171
         }
5172

5173
         if(TEST_FFI_INIT(botan_privkey_create_elgamal, (&priv, rng, 1024, 160))) {
1 ✔
5174
            do_elgamal_test(priv, rng, result);
1 ✔
5175
         }
5176
      }
1 ✔
5177

5178
   private:
5179
      static void do_elgamal_test(botan_privkey_t priv, botan_rng_t rng, Test::Result& result) {
2 ✔
5180
         TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
2 ✔
5181

5182
         botan_pubkey_t pub = nullptr;
2 ✔
5183
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
2 ✔
5184
         TEST_FFI_OK(botan_pubkey_check_key, (pub, rng, 0));
2 ✔
5185

5186
         ffi_test_pubkey_export(result, pub, priv, rng);
2 ✔
5187
         botan_mp_t p = nullptr;
2 ✔
5188
         botan_mp_t g = nullptr;
2 ✔
5189
         botan_mp_t x = nullptr;
2 ✔
5190
         botan_mp_t y = nullptr;
2 ✔
5191
         botan_mp_init(&p);
2 ✔
5192
         botan_mp_init(&g);
2 ✔
5193
         botan_mp_init(&x);
2 ✔
5194
         botan_mp_init(&y);
2 ✔
5195

5196
         TEST_FFI_OK(botan_pubkey_get_field, (p, pub, "p"));
2 ✔
5197
         TEST_FFI_OK(botan_pubkey_get_field, (g, pub, "g"));
2 ✔
5198
         TEST_FFI_OK(botan_pubkey_get_field, (y, pub, "y"));
2 ✔
5199
         TEST_FFI_OK(botan_privkey_get_field, (x, priv, "x"));
2 ✔
5200

5201
         size_t p_len = 0;
2 ✔
5202
         TEST_FFI_OK(botan_mp_num_bytes, (p, &p_len));
2 ✔
5203

5204
         botan_privkey_t loaded_privkey;
2 ✔
5205
         TEST_FFI_OK(botan_privkey_load_elgamal, (&loaded_privkey, p, g, x));
2 ✔
5206

5207
         botan_pubkey_t loaded_pubkey;
2 ✔
5208
         TEST_FFI_OK(botan_pubkey_load_elgamal, (&loaded_pubkey, p, g, y));
2 ✔
5209

5210
         botan_mp_destroy(p);
2 ✔
5211
         botan_mp_destroy(g);
2 ✔
5212
         botan_mp_destroy(y);
2 ✔
5213
         botan_mp_destroy(x);
2 ✔
5214

5215
         std::vector<uint8_t> plaintext(16, 0xFF);
2 ✔
5216
         std::vector<uint8_t> ciphertext;
2 ✔
5217
         std::vector<uint8_t> decryption;
2 ✔
5218

5219
   #if defined(BOTAN_HAS_OAEP) && defined(BOTAN_HAS_SHA2_32)
5220
         const std::string padding = "OAEP(SHA-256)";
2 ✔
5221
   #else
5222
         const std::string padding = "Raw";
5223
   #endif
5224

5225
         // Test encryption
5226
         botan_pk_op_encrypt_t op_enc;
2 ✔
5227
         if(TEST_FFI_OK(botan_pk_op_encrypt_create, (&op_enc, loaded_pubkey, padding.c_str(), 0))) {
2 ✔
5228
            size_t ctext_len;
2 ✔
5229
            TEST_FFI_OK(botan_pk_op_encrypt_output_length, (op_enc, plaintext.size(), &ctext_len));
2 ✔
5230
            ciphertext.resize(ctext_len);
2 ✔
5231
            TEST_FFI_OK(botan_pk_op_encrypt,
2 ✔
5232
                        (op_enc, rng, ciphertext.data(), &ctext_len, plaintext.data(), plaintext.size()));
5233
            ciphertext.resize(ctext_len);
2 ✔
5234
            TEST_FFI_OK(botan_pk_op_encrypt_destroy, (op_enc));
2 ✔
5235
         }
5236

5237
         // Test decryption
5238
         botan_pk_op_decrypt_t op_dec;
2 ✔
5239
         if(TEST_FFI_OK(botan_pk_op_decrypt_create, (&op_dec, loaded_privkey, padding.c_str(), 0))) {
2 ✔
5240
            size_t ptext_len;
2 ✔
5241
            TEST_FFI_OK(botan_pk_op_decrypt_output_length, (op_dec, ciphertext.size(), &ptext_len));
2 ✔
5242
            decryption.resize(ptext_len);
2 ✔
5243
            TEST_FFI_OK(botan_pk_op_decrypt,
2 ✔
5244
                        (op_dec, decryption.data(), &ptext_len, ciphertext.data(), ciphertext.size()));
5245
            decryption.resize(ptext_len);
2 ✔
5246
            TEST_FFI_OK(botan_pk_op_decrypt_destroy, (op_dec));
2 ✔
5247
         }
5248

5249
         result.test_bin_eq("decryption worked", decryption, plaintext);
2 ✔
5250

5251
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey));
2 ✔
5252
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
2 ✔
5253
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
2 ✔
5254
         TEST_FFI_OK(botan_privkey_destroy, (priv));
2 ✔
5255
      }
2 ✔
5256
};
5257

5258
class FFI_DH_Test final : public FFI_Test {
1 ✔
5259
   public:
5260
      std::string name() const override { return "FFI DH"; }
1 ✔
5261

5262
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
5263
         botan_privkey_t priv1;
1 ✔
5264
         if(!TEST_FFI_INIT(botan_privkey_create_dh, (&priv1, rng, "modp/ietf/2048"))) {
1 ✔
5265
            return;
×
5266
         }
5267

5268
         botan_privkey_t priv2;
1 ✔
5269
         REQUIRE_FFI_OK(botan_privkey_create_dh, (&priv2, rng, "modp/ietf/2048"));
1 ✔
5270

5271
         botan_pubkey_t pub1;
1 ✔
5272
         REQUIRE_FFI_OK(botan_privkey_export_pubkey, (&pub1, priv1));
1 ✔
5273

5274
         botan_pubkey_t pub2;
1 ✔
5275
         REQUIRE_FFI_OK(botan_privkey_export_pubkey, (&pub2, priv2));
1 ✔
5276

5277
         // Reload key-pair1 in order to test functions for key loading
5278
         botan_mp_t private_x;
1 ✔
5279
         botan_mp_t public_g;
1 ✔
5280
         botan_mp_t public_p;
1 ✔
5281
         botan_mp_t public_y;
1 ✔
5282

5283
         botan_mp_init(&private_x);
1 ✔
5284
         botan_mp_init(&public_g);
1 ✔
5285
         botan_mp_init(&public_p);
1 ✔
5286
         botan_mp_init(&public_y);
1 ✔
5287

5288
         TEST_FFI_OK(botan_privkey_get_field, (private_x, priv1, "x"));
1 ✔
5289
         TEST_FFI_OK(botan_pubkey_get_field, (public_g, pub1, "g"));
1 ✔
5290
         TEST_FFI_OK(botan_pubkey_get_field, (public_p, pub1, "p"));
1 ✔
5291
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub1, "y"));
1 ✔
5292

5293
         botan_privkey_t loaded_privkey1;
1 ✔
5294
         botan_pubkey_t loaded_pubkey1;
1 ✔
5295
         TEST_FFI_OK(botan_privkey_load_dh, (&loaded_privkey1, public_p, public_g, private_x));
1 ✔
5296
         TEST_FFI_OK(botan_pubkey_load_dh, (&loaded_pubkey1, public_p, public_g, public_y));
1 ✔
5297

5298
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey1, rng, 0));
1 ✔
5299
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey1, rng, 0));
1 ✔
5300

5301
         botan_mp_t loaded_public_g;
1 ✔
5302
         botan_mp_t loaded_public_p;
1 ✔
5303
         botan_mp_t loaded_public_y;
1 ✔
5304
         botan_mp_init(&loaded_public_g);
1 ✔
5305
         botan_mp_init(&loaded_public_p);
1 ✔
5306
         botan_mp_init(&loaded_public_y);
1 ✔
5307

5308
         TEST_FFI_OK(botan_pubkey_get_field, (loaded_public_g, loaded_pubkey1, "g"));
1 ✔
5309
         TEST_FFI_OK(botan_pubkey_get_field, (loaded_public_p, loaded_pubkey1, "p"));
1 ✔
5310
         TEST_FFI_OK(botan_pubkey_get_field, (loaded_public_y, loaded_pubkey1, "y"));
1 ✔
5311

5312
         int cmp;
1 ✔
5313

5314
         TEST_FFI_OK(botan_mp_cmp, (&cmp, loaded_public_g, public_g));
1 ✔
5315
         result.test_is_true("bigint_mp_cmp(g, g)", cmp == 0);
1 ✔
5316

5317
         TEST_FFI_OK(botan_mp_cmp, (&cmp, loaded_public_p, public_p));
1 ✔
5318
         result.test_is_true("bigint_mp_cmp(p, p)", cmp == 0);
1 ✔
5319

5320
         TEST_FFI_OK(botan_mp_cmp, (&cmp, loaded_public_y, public_y));
1 ✔
5321
         result.test_is_true("bigint_mp_cmp(y, y)", cmp == 0);
1 ✔
5322

5323
         botan_pk_op_ka_t ka1;
1 ✔
5324
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka1, loaded_privkey1, "Raw", 0));
1 ✔
5325
         botan_pk_op_ka_t ka2;
1 ✔
5326
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka2, priv2, "Raw", 0));
1 ✔
5327

5328
         size_t pubkey1_len = 0;
1 ✔
5329
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1 ✔
5330
                     botan_pk_op_key_agreement_export_public,
5331
                     (priv1, nullptr, &pubkey1_len));
5332
         std::vector<uint8_t> pubkey1(pubkey1_len);
1 ✔
5333
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_export_public, (priv1, pubkey1.data(), &pubkey1_len));
1 ✔
5334
         size_t pubkey2_len = 0;
1 ✔
5335
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1 ✔
5336
                     botan_pk_op_key_agreement_export_public,
5337
                     (priv2, nullptr, &pubkey2_len));
5338
         std::vector<uint8_t> pubkey2(pubkey2_len);
1 ✔
5339
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_export_public, (priv2, pubkey2.data(), &pubkey2_len));
1 ✔
5340

5341
         const size_t shared_key_len = 256;
1 ✔
5342

5343
         std::vector<uint8_t> key1(shared_key_len);
1 ✔
5344
         size_t key1_len = key1.size();
1 ✔
5345

5346
         TEST_FFI_OK(botan_pk_op_key_agreement,
1 ✔
5347
                     (ka1, key1.data(), &key1_len, pubkey2.data(), pubkey2.size(), nullptr, 0));
5348

5349
         std::vector<uint8_t> key2(shared_key_len);
1 ✔
5350
         size_t key2_len = key2.size();
1 ✔
5351

5352
         TEST_FFI_OK(botan_pk_op_key_agreement,
1 ✔
5353
                     (ka2, key2.data(), &key2_len, pubkey1.data(), pubkey1.size(), nullptr, 0));
5354

5355
         result.test_bin_eq("shared DH key", key1, key2);
1 ✔
5356

5357
         TEST_FFI_OK(botan_mp_destroy, (private_x));
1 ✔
5358
         TEST_FFI_OK(botan_mp_destroy, (public_p));
1 ✔
5359
         TEST_FFI_OK(botan_mp_destroy, (public_g));
1 ✔
5360
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1 ✔
5361

5362
         TEST_FFI_OK(botan_mp_destroy, (loaded_public_p));
1 ✔
5363
         TEST_FFI_OK(botan_mp_destroy, (loaded_public_g));
1 ✔
5364
         TEST_FFI_OK(botan_mp_destroy, (loaded_public_y));
1 ✔
5365

5366
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka1));
1 ✔
5367
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka2));
1 ✔
5368
         TEST_FFI_OK(botan_privkey_destroy, (priv1));
1 ✔
5369
         TEST_FFI_OK(botan_privkey_destroy, (priv2));
1 ✔
5370
         TEST_FFI_OK(botan_pubkey_destroy, (pub1));
1 ✔
5371
         TEST_FFI_OK(botan_pubkey_destroy, (pub2));
1 ✔
5372
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey1));
1 ✔
5373
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey1));
1 ✔
5374
      }
1 ✔
5375
};
5376

5377
class FFI_OID_Test final : public FFI_Test {
1 ✔
5378
   public:
5379
      std::string name() const override { return "FFI OID"; }
1 ✔
5380

5381
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
5382
         botan_asn1_oid_t oid;
1 ✔
5383
         botan_asn1_oid_t new_oid;
1 ✔
5384
         botan_asn1_oid_t new_oid_from_string;
1 ✔
5385
         botan_asn1_oid_t oid_a;
1 ✔
5386
         botan_asn1_oid_t oid_b;
1 ✔
5387
         botan_asn1_oid_t oid_c;
1 ✔
5388

5389
         TEST_FFI_FAIL("empty oid", botan_oid_from_string, (&oid, ""));
1 ✔
5390
         TEST_FFI_OK(botan_oid_from_string, (&oid, "1.2.3.4.5"));
1 ✔
5391

5392
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_oid_from_string, (&new_oid, "a.a.a"));
1 ✔
5393
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_oid_from_string, (&new_oid, "0.40"));
1 ✔
5394
         TEST_FFI_RC(
1 ✔
5395
            BOTAN_FFI_ERROR_BAD_PARAMETER, botan_oid_from_string, (&new_oid, "random-name-that-definitely-has-no-oid"));
5396

5397
         TEST_FFI_OK(botan_oid_from_string, (&new_oid, "1.2.3.4.5.6.7.8"));
1 ✔
5398
         TEST_FFI_OK(botan_oid_register, (new_oid, "random-name-that-definitely-has-no-oid"));
1 ✔
5399

5400
         TEST_FFI_OK(botan_oid_from_string, (&new_oid_from_string, "random-name-that-definitely-has-no-oid"));
1 ✔
5401
         TEST_FFI_RC(1, botan_oid_equal, (new_oid, new_oid_from_string));
1 ✔
5402

5403
         TEST_FFI_OK(botan_oid_from_string, (&oid_a, "1.2.3.4.5.6"));
1 ✔
5404
         TEST_FFI_OK(botan_oid_from_string, (&oid_b, "1.2.3.4.5.6"));
1 ✔
5405
         TEST_FFI_OK(botan_oid_from_string, (&oid_c, "1.2.3.4.4"));
1 ✔
5406

5407
         TEST_FFI_RC(1, botan_oid_equal, (oid_a, oid_b));
1 ✔
5408
         TEST_FFI_RC(0, botan_oid_equal, (oid_a, oid_c));
1 ✔
5409

5410
         int res;
1 ✔
5411

5412
         TEST_FFI_OK(botan_oid_cmp, (&res, oid_a, oid_b));
1 ✔
5413
         result.test_is_true("oid_a and oid_b are equal", res == 0);
1 ✔
5414

5415
         TEST_FFI_OK(botan_oid_cmp, (&res, oid_a, oid_c));
1 ✔
5416
         result.test_is_true("oid_a is bigger", res == 1);
1 ✔
5417

5418
         TEST_FFI_OK(botan_oid_cmp, (&res, oid_c, oid_a));
1 ✔
5419
         result.test_is_true("oid_c is smaller", res == -1);
1 ✔
5420

5421
         TEST_FFI_OK(botan_oid_destroy, (oid));
1 ✔
5422
         TEST_FFI_OK(botan_oid_destroy, (new_oid));
1 ✔
5423
         TEST_FFI_OK(botan_oid_destroy, (new_oid_from_string));
1 ✔
5424
         TEST_FFI_OK(botan_oid_destroy, (oid_a));
1 ✔
5425
         TEST_FFI_OK(botan_oid_destroy, (oid_b));
1 ✔
5426
         TEST_FFI_OK(botan_oid_destroy, (oid_c));
1 ✔
5427

5428
         botan_privkey_t priv;
1 ✔
5429
         if(TEST_FFI_INIT(botan_privkey_create_rsa, (&priv, rng, 1024))) {
1 ✔
5430
            TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
1 ✔
5431

5432
            const std::string oid_rsa_expected = "1.2.840.113549.1.1.1";
1 ✔
5433

5434
            botan_asn1_oid_t rsa_oid_priv;
1 ✔
5435
            botan_asn1_oid_t rsa_oid_pub;
1 ✔
5436
            botan_asn1_oid_t rsa_oid_expected;
1 ✔
5437
            botan_asn1_oid_t rsa_oid_from_name;
1 ✔
5438

5439
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_oid_from_string, (&rsa_oid_expected, nullptr));
1 ✔
5440
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_oid_from_string, (nullptr, "1.2.3.4.5"));
1 ✔
5441
            TEST_FFI_OK(botan_oid_from_string, (&rsa_oid_expected, oid_rsa_expected.c_str()));
1 ✔
5442
            TEST_FFI_OK(botan_privkey_oid, (&rsa_oid_priv, priv));
1 ✔
5443

5444
            TEST_FFI_RC(1, botan_oid_equal, (rsa_oid_priv, rsa_oid_expected));
1 ✔
5445

5446
            botan_pubkey_t pub;
1 ✔
5447
            TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1 ✔
5448

5449
            TEST_FFI_OK(botan_pubkey_oid, (&rsa_oid_pub, pub));
1 ✔
5450
            TEST_FFI_RC(1, botan_oid_equal, (rsa_oid_pub, rsa_oid_expected));
1 ✔
5451

5452
            ViewStringSink oid_string;
1 ✔
5453
            TEST_FFI_OK(botan_oid_view_string, (rsa_oid_expected, oid_string.delegate(), oid_string.callback()));
1 ✔
5454
            const std::string oid_actual = {oid_string.get().begin(), oid_string.get().end()};
2 ✔
5455

5456
            result.test_str_eq("oid to string", oid_actual, oid_rsa_expected);
1 ✔
5457

5458
            TEST_FFI_OK(botan_oid_from_string, (&rsa_oid_from_name, "RSA"));
1 ✔
5459
            TEST_FFI_RC(1, botan_oid_equal, (rsa_oid_expected, rsa_oid_from_name));
1 ✔
5460

5461
            ViewStringSink rsa_name;
1 ✔
5462
            TEST_FFI_OK(botan_oid_view_name, (rsa_oid_from_name, rsa_name.delegate(), rsa_name.callback()));
1 ✔
5463
            const std::string rsa_name_string = {rsa_name.get().begin(), rsa_name.get().end()};
2 ✔
5464
            result.test_str_eq("oid to name", rsa_name_string, "RSA");
1 ✔
5465

5466
            TEST_FFI_OK(botan_oid_destroy, (rsa_oid_priv));
1 ✔
5467
            TEST_FFI_OK(botan_oid_destroy, (rsa_oid_pub));
1 ✔
5468
            TEST_FFI_OK(botan_oid_destroy, (rsa_oid_expected));
1 ✔
5469
            TEST_FFI_OK(botan_oid_destroy, (rsa_oid_from_name));
1 ✔
5470

5471
            TEST_FFI_OK(botan_pubkey_destroy, (pub));
1 ✔
5472
            TEST_FFI_OK(botan_privkey_destroy, (priv));
1 ✔
5473
         }
1 ✔
5474
      }
1 ✔
5475
};
5476

5477
class FFI_EC_Group_Test final : public FFI_Test {
1 ✔
5478
   public:
5479
      std::string name() const override { return "FFI EC Group"; }
1 ✔
5480

5481
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
5482
         int appl_spec_groups;
1 ✔
5483
         int named_group;
1 ✔
5484
         TEST_FFI_OK(botan_ec_group_supports_application_specific_group, (&appl_spec_groups));
1 ✔
5485
         TEST_FFI_OK(botan_ec_group_supports_named_group, ("secp256r1", &named_group));
1 ✔
5486
         result.test_bool_eq("application specific groups support matches build",
1 ✔
5487
                             appl_spec_groups == 1,
5488
                             Botan::EC_Group::supports_application_specific_group());
1 ✔
5489
         result.test_bool_eq(
1 ✔
5490
            "named group support matches build", named_group == 1, Botan::EC_Group::supports_named_group("secp256r1"));
1 ✔
5491

5492
         if(named_group == 1) {
1 ✔
5493
            botan_ec_group_t group_from_name;
1 ✔
5494
            botan_asn1_oid_t oid_from_name;
1 ✔
5495
            botan_mp_t p_from_name;
1 ✔
5496
            botan_mp_t a_from_name;
1 ✔
5497
            botan_mp_t b_from_name;
1 ✔
5498
            botan_mp_t g_x_from_name;
1 ✔
5499
            botan_mp_t g_y_from_name;
1 ✔
5500
            botan_mp_t order_from_name;
1 ✔
5501

5502
            TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_ec_group_from_name, (&group_from_name, ""));
1 ✔
5503

5504
            TEST_FFI_OK(botan_ec_group_from_name, (&group_from_name, "secp256r1"));
1 ✔
5505

5506
            get_group_parameters(group_from_name,
1 ✔
5507
                                 &oid_from_name,
5508
                                 &p_from_name,
5509
                                 &a_from_name,
5510
                                 &b_from_name,
5511
                                 &g_x_from_name,
5512
                                 &g_y_from_name,
5513
                                 &order_from_name,
5514
                                 result);
5515

5516
            botan_asn1_oid_t group_oid;
1 ✔
5517
            botan_ec_group_t group_from_oid;
1 ✔
5518
            botan_asn1_oid_t oid_from_oid;
1 ✔
5519
            botan_mp_t p_from_oid;
1 ✔
5520
            botan_mp_t a_from_oid;
1 ✔
5521
            botan_mp_t b_from_oid;
1 ✔
5522
            botan_mp_t g_x_from_oid;
1 ✔
5523
            botan_mp_t g_y_from_oid;
1 ✔
5524
            botan_mp_t order_from_oid;
1 ✔
5525

5526
            TEST_FFI_OK(botan_oid_from_string, (&group_oid, "1.2.840.10045.3.1.7"));
1 ✔
5527

5528
            TEST_FFI_OK(botan_ec_group_from_oid, (&group_from_oid, group_oid));
1 ✔
5529

5530
            get_group_parameters(group_from_oid,
1 ✔
5531
                                 &oid_from_oid,
5532
                                 &p_from_oid,
5533
                                 &a_from_oid,
5534
                                 &b_from_oid,
5535
                                 &g_x_from_oid,
5536
                                 &g_y_from_oid,
5537
                                 &order_from_oid,
5538
                                 result);
5539

5540
            TEST_FFI_RC(1, botan_oid_equal, (group_oid, oid_from_oid));
1 ✔
5541
            TEST_FFI_RC(1, botan_oid_equal, (oid_from_name, oid_from_oid));
1 ✔
5542

5543
            if(appl_spec_groups == 1) {
1 ✔
5544
               botan_asn1_oid_t group_parameter_oid;
1 ✔
5545
               botan_mp_t p_parameter;
1 ✔
5546
               botan_mp_t a_parameter;
1 ✔
5547
               botan_mp_t b_parameter;
1 ✔
5548
               botan_mp_t g_x_parameter;
1 ✔
5549
               botan_mp_t g_y_parameter;
1 ✔
5550
               botan_mp_t order_parameter;
1 ✔
5551

5552
               botan_ec_group_t group_from_parameters;
1 ✔
5553
               botan_asn1_oid_t oid_from_parameters;
1 ✔
5554
               botan_mp_t p_from_parameters;
1 ✔
5555
               botan_mp_t a_from_parameters;
1 ✔
5556
               botan_mp_t b_from_parameters;
1 ✔
5557
               botan_mp_t g_x_from_parameters;
1 ✔
5558
               botan_mp_t g_y_from_parameters;
1 ✔
5559
               botan_mp_t order_from_parameters;
1 ✔
5560

5561
               TEST_FFI_OK(botan_oid_from_string, (&group_parameter_oid, "1.3.6.1.4.1.25258.100.0"));
1 ✔
5562
               botan_oid_register(group_parameter_oid, "secp256r1-but-manually-registered");
1 ✔
5563
               botan_mp_init(&p_parameter);
1 ✔
5564
               botan_mp_init(&a_parameter);
1 ✔
5565
               botan_mp_init(&b_parameter);
1 ✔
5566
               botan_mp_init(&g_x_parameter);
1 ✔
5567
               botan_mp_init(&g_y_parameter);
1 ✔
5568
               botan_mp_init(&order_parameter);
1 ✔
5569

5570
               botan_mp_set_from_str(p_parameter, "0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF");
1 ✔
5571
               botan_mp_set_from_str(a_parameter, "0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC");
1 ✔
5572
               botan_mp_set_from_str(b_parameter, "0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B");
1 ✔
5573
               botan_mp_set_from_str(g_x_parameter,
1 ✔
5574
                                     "0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296");
5575
               botan_mp_set_from_str(g_y_parameter,
1 ✔
5576
                                     "0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");
5577
               botan_mp_set_from_str(order_parameter,
1 ✔
5578
                                     "0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
5579

5580
               TEST_FFI_OK(botan_ec_group_from_params,
1 ✔
5581
                           (&group_from_parameters,
5582
                            group_parameter_oid,
5583
                            p_parameter,
5584
                            a_parameter,
5585
                            b_parameter,
5586
                            g_x_parameter,
5587
                            g_y_parameter,
5588
                            order_parameter));
5589

5590
               get_group_parameters(group_from_parameters,
1 ✔
5591
                                    &oid_from_parameters,
5592
                                    &p_from_parameters,
5593
                                    &a_from_parameters,
5594
                                    &b_from_parameters,
5595
                                    &g_x_from_parameters,
5596
                                    &g_y_from_parameters,
5597
                                    &order_from_parameters,
5598
                                    result);
5599

5600
               botan_ec_group_t group_from_registered_oid;
1 ✔
5601

5602
               TEST_FFI_OK(botan_ec_group_from_name, (&group_from_registered_oid, "secp256r1-but-manually-registered"));
1 ✔
5603

5604
               // we registered this group under a different oid
5605
               TEST_FFI_RC(0, botan_oid_equal, (oid_from_oid, oid_from_parameters));
1 ✔
5606

5607
               TEST_FFI_RC(1, botan_ec_group_equal, (group_from_name, group_from_parameters));
1 ✔
5608
               TEST_FFI_RC(1, botan_ec_group_equal, (group_from_parameters, group_from_registered_oid));
1 ✔
5609

5610
               const std::vector<std::tuple<botan_mp_t, botan_mp_t>> parameters_inner = {
1 ✔
5611
                  {p_from_name, p_from_parameters},
5612
                  {a_from_name, a_from_parameters},
5613
                  {b_from_name, b_from_parameters},
5614
                  {g_x_from_name, g_x_from_parameters},
5615
                  {g_y_from_name, g_y_from_parameters},
5616
                  {order_from_name, order_from_parameters}};
1 ✔
5617

5618
               for(auto [x, y] : parameters_inner) {
7 ✔
5619
                  TEST_FFI_RC(1, botan_mp_equal, (x, y));
6 ✔
5620
                  botan_mp_destroy(y);
6 ✔
5621
               }
5622

5623
               botan_mp_destroy(p_parameter);
1 ✔
5624
               botan_mp_destroy(a_parameter);
1 ✔
5625
               botan_mp_destroy(b_parameter);
1 ✔
5626
               botan_mp_destroy(g_x_parameter);
1 ✔
5627
               botan_mp_destroy(g_y_parameter);
1 ✔
5628
               botan_mp_destroy(order_parameter);
1 ✔
5629

5630
               TEST_FFI_RC(1, botan_ec_group_unregister, (group_parameter_oid));
1 ✔
5631
               TEST_FFI_RC(0, botan_ec_group_unregister, (group_parameter_oid));
1 ✔
5632
               TEST_FFI_RC(1, botan_ec_group_unregister, (oid_from_name));
1 ✔
5633
               TEST_FFI_RC(0, botan_ec_group_unregister, (oid_from_name));
1 ✔
5634

5635
               botan_ec_group_t unregistered_group;
1 ✔
5636
               TEST_FFI_RC(
1 ✔
5637
                  BOTAN_FFI_ERROR_BAD_PARAMETER, botan_ec_group_from_oid, (&unregistered_group, group_parameter_oid));
5638

5639
               botan_oid_destroy(group_parameter_oid);
1 ✔
5640
               botan_oid_destroy(oid_from_parameters);
1 ✔
5641

5642
               TEST_FFI_OK(botan_ec_group_destroy, (group_from_parameters));
1 ✔
5643
               TEST_FFI_OK(botan_ec_group_destroy, (group_from_registered_oid));
1 ✔
5644
            }
1 ✔
5645

5646
            botan_oid_destroy(oid_from_name);
1 ✔
5647
            botan_oid_destroy(group_oid);
1 ✔
5648
            botan_oid_destroy(oid_from_oid);
1 ✔
5649

5650
            const std::vector<std::tuple<botan_mp_t, botan_mp_t>> parameters = {{p_from_name, p_from_oid},
1 ✔
5651
                                                                                {a_from_name, a_from_oid},
5652
                                                                                {b_from_name, b_from_oid},
5653
                                                                                {g_x_from_name, g_x_from_oid},
5654
                                                                                {g_y_from_name, g_y_from_oid},
5655
                                                                                {order_from_name, order_from_oid}};
1 ✔
5656

5657
            for(auto [x, y] : parameters) {
7 ✔
5658
               TEST_FFI_RC(1, botan_mp_equal, (x, y));
6 ✔
5659
               botan_mp_destroy(x);
6 ✔
5660
               botan_mp_destroy(y);
6 ✔
5661
            }
5662

5663
            botan_ec_group_t secp384r1;
1 ✔
5664
            botan_ec_group_t secp384r1_with_seed;
1 ✔
5665

5666
            TEST_FFI_OK(botan_ec_group_from_name, (&secp384r1, "secp384r1"));
1 ✔
5667
            TEST_FFI_OK(botan_ec_group_from_pem,
1 ✔
5668
                        (&secp384r1_with_seed, Test::read_data_file("x509/ecc/secp384r1_seed.pem").c_str()));
5669

5670
            botan_mp_t p;
1 ✔
5671
            botan_mp_t p_with_seed;
1 ✔
5672
            TEST_FFI_OK(botan_ec_group_get_p, (&p, secp384r1));
1 ✔
5673
            TEST_FFI_OK(botan_ec_group_get_p, (&p_with_seed, secp384r1_with_seed));
1 ✔
5674
            TEST_FFI_RC(1, botan_mp_equal, (p, p_with_seed));
1 ✔
5675
            botan_mp_destroy(p);
1 ✔
5676
            botan_mp_destroy(p_with_seed);
1 ✔
5677

5678
            TEST_FFI_RC(0, botan_ec_group_equal, (group_from_name, secp384r1));
1 ✔
5679
            TEST_FFI_RC(1, botan_ec_group_equal, (group_from_name, group_from_oid));
1 ✔
5680

5681
            ViewBytesSink der_bytes;
1 ✔
5682
            TEST_FFI_OK(botan_ec_group_view_der, (group_from_name, der_bytes.delegate(), der_bytes.callback()));
1 ✔
5683
            botan_ec_group_t group_from_ber;
1 ✔
5684
            TEST_FFI_OK(
1 ✔
5685
               botan_ec_group_from_ber,
5686
               (&group_from_ber, reinterpret_cast<const uint8_t*>(der_bytes.get().data()), der_bytes.get().size()));
5687

5688
            ViewStringSink pem_string;
1 ✔
5689
            TEST_FFI_OK(botan_ec_group_view_pem, (group_from_name, pem_string.delegate(), pem_string.callback()));
1 ✔
5690
            const std::string pem_actual = {pem_string.get().begin(), pem_string.get().end()};
2 ✔
5691

5692
            botan_ec_group_t group_from_pem;
1 ✔
5693
            TEST_FFI_OK(botan_ec_group_from_pem, (&group_from_pem, pem_actual.c_str()));
1 ✔
5694

5695
            TEST_FFI_RC(1, botan_ec_group_equal, (group_from_name, group_from_ber));
1 ✔
5696
            TEST_FFI_RC(1, botan_ec_group_equal, (group_from_name, group_from_pem));
1 ✔
5697

5698
            botan_privkey_t priv;
1 ✔
5699
            TEST_FFI_OK(botan_ec_privkey_create, (&priv, "ECDSA", secp384r1, rng));
1 ✔
5700
            std::array<char, 32> namebuf{};
1 ✔
5701
            size_t name_len = namebuf.size();
1 ✔
5702

5703
            TEST_FFI_OK(botan_privkey_algo_name, (priv, namebuf.data(), &name_len));
1 ✔
5704
            result.test_str_eq("Key name is expected value", namebuf.data(), "ECDSA");
1 ✔
5705

5706
            botan_ec_group_t group_from_key;
1 ✔
5707
            TEST_FFI_OK(botan_ec_privkey_get_group, (priv, &group_from_key));
1 ✔
5708
            TEST_FFI_RC(1, botan_ec_group_equal, (group_from_key, secp384r1));
1 ✔
5709

5710
            botan_ec_scalar_t private_value;
1 ✔
5711
            TEST_FFI_OK(botan_ec_privkey_get_private_key, (priv, &private_value));
1 ✔
5712

5713
            botan_ec_scalar_destroy(private_value);
1 ✔
5714
            botan_privkey_destroy(priv);
1 ✔
5715

5716
            TEST_FFI_OK(botan_ec_group_destroy, (group_from_name));
1 ✔
5717
            TEST_FFI_OK(botan_ec_group_destroy, (group_from_oid));
1 ✔
5718
            TEST_FFI_OK(botan_ec_group_destroy, (secp384r1));
1 ✔
5719
            TEST_FFI_OK(botan_ec_group_destroy, (secp384r1_with_seed));
1 ✔
5720
            TEST_FFI_OK(botan_ec_group_destroy, (group_from_ber));
1 ✔
5721
            TEST_FFI_OK(botan_ec_group_destroy, (group_from_pem));
1 ✔
5722
            TEST_FFI_OK(botan_ec_group_destroy, (group_from_key));
1 ✔
5723
         }
2 ✔
5724
      }
1 ✔
5725

5726
   private:
5727
      static void get_group_parameters(botan_ec_group_t ec_group,
3 ✔
5728
                                       botan_asn1_oid_t* oid,
5729
                                       botan_mp_t* p,
5730
                                       botan_mp_t* a,
5731
                                       botan_mp_t* b,
5732
                                       botan_mp_t* g_x,
5733
                                       botan_mp_t* g_y,
5734
                                       botan_mp_t* order,
5735
                                       Test::Result& result) {
5736
         TEST_FFI_OK(botan_ec_group_get_curve_oid, (oid, ec_group));
3 ✔
5737
         TEST_FFI_OK(botan_ec_group_get_p, (p, ec_group));
3 ✔
5738
         TEST_FFI_OK(botan_ec_group_get_a, (a, ec_group));
3 ✔
5739
         TEST_FFI_OK(botan_ec_group_get_b, (b, ec_group));
3 ✔
5740
         TEST_FFI_OK(botan_ec_group_get_g_x, (g_x, ec_group));
3 ✔
5741
         TEST_FFI_OK(botan_ec_group_get_g_y, (g_y, ec_group));
3 ✔
5742
         TEST_FFI_OK(botan_ec_group_get_order, (order, ec_group));
3 ✔
5743
      }
3 ✔
5744
};
5745

5746
class FFI_EC_Point_Test final : public FFI_Test {
1 ✔
5747
   public:
5748
      std::string name() const override { return "FFI Points and Scalars"; }
1 ✔
5749

5750
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
5751
         botan_ec_group_t group;
1 ✔
5752

5753
         botan_ec_scalar_t random;
1 ✔
5754
         botan_mp_t to_scalar;
1 ✔
5755
         botan_ec_scalar_t from_mp;
1 ✔
5756
         botan_mp_t from_scalar;
1 ✔
5757

5758
         if(!Botan::EC_Group::supports_named_group("secp256r1")) {
1 ✔
5759
            result.test_note("Group needed for test not supported by this build configuration.");
×
5760
            return;
×
5761
         }
5762

5763
         TEST_FFI_OK(botan_mp_init, (&to_scalar));
1 ✔
5764
         TEST_FFI_OK(botan_mp_set_from_str, (to_scalar, "12345"));
1 ✔
5765

5766
         TEST_FFI_OK(botan_ec_group_from_name, (&group, "secp256r1"));
1 ✔
5767
         TEST_FFI_OK(botan_ec_scalar_random, (&random, group, rng));
1 ✔
5768
         TEST_FFI_OK(botan_ec_scalar_from_mp, (&from_mp, group, to_scalar));
1 ✔
5769
         TEST_FFI_OK(botan_ec_scalar_to_mp, (from_mp, &from_scalar));
1 ✔
5770
         TEST_FFI_RC(1, botan_mp_equal, (to_scalar, from_scalar));
1 ✔
5771

5772
         botan_ec_group_t group_from_scalar;
1 ✔
5773
         TEST_FFI_OK(botan_ec_scalar_get_group, (random, &group_from_scalar));
1 ✔
5774
         TEST_FFI_RC(1, botan_ec_group_equal, (group, group_from_scalar));
1 ✔
5775

5776
         TEST_FFI_OK(botan_ec_group_destroy, (group_from_scalar));
1 ✔
5777
         TEST_FFI_OK(botan_ec_scalar_destroy, (random));
1 ✔
5778
         TEST_FFI_OK(botan_ec_scalar_destroy, (from_mp));
1 ✔
5779
         TEST_FFI_OK(botan_mp_destroy, (to_scalar));
1 ✔
5780
         TEST_FFI_OK(botan_mp_destroy, (from_scalar));
1 ✔
5781

5782
         botan_ec_point_t identity;
1 ✔
5783
         botan_ec_point_t generator;
1 ✔
5784
         botan_ec_point_t generator_neg;
1 ✔
5785
         botan_ec_point_t out_add_ident;
1 ✔
5786
         botan_ec_point_t out_add_inverse;
1 ✔
5787

5788
         TEST_FFI_OK(botan_ec_point_identity, (&identity, group));
1 ✔
5789
         TEST_FFI_OK(botan_ec_point_generator, (&generator, group));
1 ✔
5790
         TEST_FFI_OK(botan_ec_point_negate, (&generator_neg, generator));
1 ✔
5791

5792
         TEST_FFI_OK(botan_ec_point_add, (&out_add_ident, generator, identity));
1 ✔
5793
         TEST_FFI_OK(botan_ec_point_add, (&out_add_inverse, generator, generator_neg));
1 ✔
5794

5795
         botan_ec_group_t group_from_point;
1 ✔
5796
         TEST_FFI_OK(botan_ec_point_get_group, (identity, &group_from_point));
1 ✔
5797
         TEST_FFI_RC(1, botan_ec_group_equal, (group, group_from_point));
1 ✔
5798

5799
         botan_mp_t generator_x;
1 ✔
5800
         botan_mp_t generator_y;
1 ✔
5801
         botan_ec_point_t point_from_xy;
1 ✔
5802
         TEST_FFI_OK(botan_ec_point_to_mp_xy, (generator, &generator_x, &generator_y));
1 ✔
5803
         TEST_FFI_OK(botan_ec_point_from_xy, (&point_from_xy, group, generator_x, generator_y));
1 ✔
5804
         TEST_FFI_RC(1, botan_ec_point_equal, (point_from_xy, generator));
1 ✔
5805

5806
         ViewBytesSink generator_bytes;
1 ✔
5807
         TEST_FFI_OK(botan_ec_point_view_xy_bytes, (generator, generator_bytes.delegate(), generator_bytes.callback()));
1 ✔
5808

5809
         ViewBytesSink gen_plus_ident_bytes;
1 ✔
5810
         TEST_FFI_OK(botan_ec_point_view_xy_bytes,
1 ✔
5811
                     (out_add_ident, gen_plus_ident_bytes.delegate(), gen_plus_ident_bytes.callback()));
5812

5813
         result.test_bin_eq("generator == out_add_ident", generator_bytes.get(), gen_plus_ident_bytes.get());
1 ✔
5814

5815
         TEST_FFI_RC(1, botan_ec_point_equal, (generator, out_add_ident));
1 ✔
5816
         TEST_FFI_RC(1, botan_ec_point_equal, (identity, out_add_inverse));
1 ✔
5817
         TEST_FFI_RC(1, botan_ec_point_is_identity, (out_add_inverse));
1 ✔
5818

5819
         ViewBytesSink identity_bytes;
1 ✔
5820
         TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE,
1 ✔
5821
                     botan_ec_point_view_xy_bytes,
5822
                     (identity, identity_bytes.delegate(), identity_bytes.callback()));
5823

5824
         botan_mp_t group_order;
1 ✔
5825
         TEST_FFI_OK(botan_ec_group_get_order, (&group_order, group));
1 ✔
5826
         botan_mp_t group_order_minus_1;
1 ✔
5827
         TEST_FFI_OK(botan_mp_init, (&group_order_minus_1));
1 ✔
5828

5829
         TEST_FFI_OK(botan_mp_sub_u32, (group_order_minus_1, group_order, 1));
1 ✔
5830
         botan_ec_scalar_t order;
1 ✔
5831
         TEST_FFI_OK(botan_ec_scalar_from_mp, (&order, group, group_order_minus_1));
1 ✔
5832

5833
         botan_ec_point_t out_mul_order_minus_one;
1 ✔
5834
         TEST_FFI_OK(botan_ec_point_mul, (&out_mul_order_minus_one, generator, order, rng));
1 ✔
5835

5836
         botan_ec_point_t out_add_gen_to_order;
1 ✔
5837
         TEST_FFI_OK(botan_ec_point_add, (&out_add_gen_to_order, out_mul_order_minus_one, generator));
1 ✔
5838

5839
         TEST_FFI_RC(1, botan_ec_point_is_identity, (out_add_gen_to_order));
1 ✔
5840

5841
         TEST_FFI_OK(botan_mp_destroy, (group_order));
1 ✔
5842
         TEST_FFI_OK(botan_mp_destroy, (group_order_minus_1));
1 ✔
5843
         TEST_FFI_OK(botan_mp_destroy, (generator_x));
1 ✔
5844
         TEST_FFI_OK(botan_mp_destroy, (generator_y));
1 ✔
5845
         TEST_FFI_OK(botan_ec_scalar_destroy, (order));
1 ✔
5846

5847
         TEST_FFI_OK(botan_ec_point_destroy, (identity));
1 ✔
5848
         TEST_FFI_OK(botan_ec_point_destroy, (generator));
1 ✔
5849
         TEST_FFI_OK(botan_ec_point_destroy, (generator_neg));
1 ✔
5850
         TEST_FFI_OK(botan_ec_point_destroy, (out_add_ident));
1 ✔
5851
         TEST_FFI_OK(botan_ec_point_destroy, (out_add_inverse));
1 ✔
5852
         TEST_FFI_OK(botan_ec_point_destroy, (out_mul_order_minus_one));
1 ✔
5853
         TEST_FFI_OK(botan_ec_point_destroy, (out_add_gen_to_order));
1 ✔
5854
         TEST_FFI_OK(botan_ec_point_destroy, (point_from_xy));
1 ✔
5855
         TEST_FFI_OK(botan_ec_group_destroy, (group_from_point));
1 ✔
5856
         TEST_FFI_OK(botan_ec_group_destroy, (group));
1 ✔
5857
      }
1 ✔
5858
};
5859

5860
class FFI_SRP6_Test final : public FFI_Test {
1 ✔
5861
   public:
5862
      std::string name() const override { return "FFI SRP6"; }
1 ✔
5863

5864
      bool skip_this_test() const override {
1 ✔
5865
   #if !defined(BOTAN_HAS_SRP6)
5866
         return true;
5867
   #else
5868
         return false;
1 ✔
5869
   #endif
5870
      }
5871

5872
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
5873
         constexpr size_t group_bytes = 128;
1 ✔
5874
         const char* username = "alice";
1 ✔
5875
         const char* password = "secret";
1 ✔
5876
         const char* srp_group = "modp/srp/1024";
1 ✔
5877
         const char* srp_hash = "SHA-256";
1 ✔
5878
         const auto salt = Botan::hex_decode("beb25379d1a8581eb5a727673a2441ee");
1 ✔
5879

5880
         std::array<uint8_t, group_bytes> output{};
1 ✔
5881

5882
         size_t group_size = 0;
1 ✔
5883
         TEST_FFI_OK(botan_srp6_group_size, (srp_group, &group_size));
1 ✔
5884
         result.test_sz_eq("reported group size", group_size, group_bytes);
1 ✔
5885

5886
         size_t short_output_len = output.size() / 2;
1 ✔
5887
         TEST_FFI_RC(
1 ✔
5888
            BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
5889
            botan_srp6_generate_verifier,
5890
            (username, password, salt.data(), salt.size(), srp_group, srp_hash, output.data(), &short_output_len));
5891
         result.test_sz_eq("requested verifier length", short_output_len, group_bytes);
1 ✔
5892

5893
         size_t verifier_output_len = short_output_len;
1 ✔
5894
         TEST_FFI_OK(
1 ✔
5895
            botan_srp6_generate_verifier,
5896
            (username, password, salt.data(), salt.size(), srp_group, srp_hash, output.data(), &verifier_output_len));
5897
         const auto verifier = std::vector(output.data(), output.data() + verifier_output_len);
1 ✔
5898

5899
         botan_srp6_server_session_t srp_server;
1 ✔
5900
         TEST_FFI_OK(botan_srp6_server_session_init, (&srp_server));
1 ✔
5901

5902
         short_output_len = output.size() / 2;
1 ✔
5903
         TEST_FFI_RC(
1 ✔
5904
            BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
5905
            botan_srp6_server_session_step1,
5906
            (srp_server, verifier.data(), verifier.size(), srp_group, srp_hash, rng, output.data(), &short_output_len));
5907
         result.test_sz_eq("requested B_pub length", short_output_len, group_bytes);
1 ✔
5908

5909
         size_t pub_B_output_len = short_output_len;
1 ✔
5910
         TEST_FFI_OK(
1 ✔
5911
            botan_srp6_server_session_step1,
5912
            (srp_server, verifier.data(), verifier.size(), srp_group, srp_hash, rng, output.data(), &pub_B_output_len));
5913
         const auto pub_B = std::vector(output.data(), output.data() + pub_B_output_len);
1 ✔
5914

5915
         std::array<uint8_t, group_bytes> output2{};
1 ✔
5916
         size_t short_output_len2 = output2.size() / 2;
1 ✔
5917
         short_output_len = output.size() / 2;
1 ✔
5918
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1 ✔
5919
                     botan_srp6_client_agree,
5920
                     (username,
5921
                      password,
5922
                      srp_group,
5923
                      srp_hash,
5924
                      salt.data(),
5925
                      salt.size(),
5926
                      pub_B.data(),
5927
                      pub_B.size(),
5928
                      rng,
5929
                      output.data(),
5930
                      &short_output_len,
5931
                      output2.data(),
5932
                      &short_output_len2));
5933
         result.test_sz_eq("requested pub_A length", short_output_len, group_bytes);
1 ✔
5934
         result.test_sz_eq("requested K1 length", short_output_len2, group_bytes);
1 ✔
5935

5936
         size_t pub_A_output_len = short_output_len;
1 ✔
5937
         size_t K1_output_len = short_output_len2;
1 ✔
5938
         TEST_FFI_OK(botan_srp6_client_agree,
1 ✔
5939
                     (username,
5940
                      password,
5941
                      srp_group,
5942
                      srp_hash,
5943
                      salt.data(),
5944
                      salt.size(),
5945
                      pub_B.data(),
5946
                      pub_B.size(),
5947
                      rng,
5948
                      output.data(),
5949
                      &pub_A_output_len,
5950
                      output2.data(),
5951
                      &K1_output_len));
5952
         const auto pub_A = std::vector(output.data(), output.data() + pub_A_output_len);
1 ✔
5953
         const auto K1 = std::vector(output2.data(), output2.data() + K1_output_len);
1 ✔
5954

5955
         short_output_len = output.size() / 2;
1 ✔
5956
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1 ✔
5957
                     botan_srp6_server_session_step2,
5958
                     (srp_server, pub_A.data(), pub_A.size(), output.data(), &short_output_len));
5959
         result.test_sz_eq("requested K2 length", short_output_len, group_bytes);
1 ✔
5960

5961
         size_t K2_output_len = short_output_len;
1 ✔
5962
         TEST_FFI_OK(botan_srp6_server_session_step2,
1 ✔
5963
                     (srp_server, pub_A.data(), pub_A.size(), output.data(), &K2_output_len));
5964
         const auto K2 = std::vector(output.data(), output.data() + K2_output_len);
1 ✔
5965

5966
         result.test_bin_eq("K1 == K2", K1, K2);
1 ✔
5967

5968
         TEST_FFI_OK(botan_srp6_server_session_destroy, (srp_server));
1 ✔
5969
      }
1 ✔
5970
};
5971

5972
class FFI_SPAKE2P_Test final : public FFI_Test {
1 ✔
5973
   public:
5974
      std::string name() const override { return "FFI SPAKE2+"; }
1 ✔
5975

5976
      bool skip_this_test() const override {
1 ✔
5977
   #if !defined(BOTAN_HAS_PAKE_SPAKE2PLUS)
5978
         return true;
5979
   #else
5980
         return false;
1 ✔
5981
   #endif
5982
      }
5983

5984
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1 ✔
5985
         const char* password = "hunter2";
1 ✔
5986
         const std::vector<uint8_t> prover_id = {'c', 'l', 'i', 'e', 'n', 't'};
1 ✔
5987
         const std::vector<uint8_t> verifier_id = {'s', 'e', 'r', 'v', 'e', 'r'};
1 ✔
5988
         const std::vector<uint8_t> context = {'f', 'f', 'i'};
1 ✔
5989
         const auto salt = Botan::hex_decode("adb63d2727f971e1b52b7ba1e42ab73c");
1 ✔
5990

5991
         botan_spake2p_params_t params;
1 ✔
5992
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_spake2p_params_init, (&params, "P37-MD5"));
1 ✔
5993
         TEST_FFI_OK(botan_spake2p_params_init, (&params, "P256-SHA256"));
1 ✔
5994

5995
         size_t share_size = 0;
1 ✔
5996
         TEST_FFI_OK(botan_spake2p_params_share_size, (params, &share_size));
1 ✔
5997
         result.test_sz_eq("share size", share_size, 65);
1 ✔
5998

5999
         size_t confirmation_size = 0;
1 ✔
6000
         TEST_FFI_OK(botan_spake2p_params_confirmation_size, (params, &confirmation_size));
1 ✔
6001
         result.test_sz_eq("confirmation size", confirmation_size, 32);
1 ✔
6002

6003
         ViewBytesSink secret;
1 ✔
6004
         TEST_FFI_OK(botan_spake2p_derive_secret,
1 ✔
6005
                     (params,
6006
                      password,
6007
                      prover_id.data(),
6008
                      prover_id.size(),
6009
                      verifier_id.data(),
6010
                      verifier_id.size(),
6011
                      salt.data(),
6012
                      salt.size(),
6013
                      secret.delegate(),
6014
                      secret.callback()));
6015
         result.test_sz_eq("secret length", secret.size(), 64);
1 ✔
6016

6017
         ViewBytesSink record;
1 ✔
6018
         TEST_FFI_OK(botan_spake2p_registration_record,
1 ✔
6019
                     (params, rng, secret.data(), secret.size(), record.delegate(), record.callback()));
6020
         result.test_sz_eq("record length", record.size(), 32 + 65);
1 ✔
6021

6022
         const auto init_prover = [&](botan_spake2p_prover_t* prover) {
4 ✔
6023
            TEST_FFI_OK(botan_spake2p_prover_init,
3 ✔
6024
                        (prover,
6025
                         params,
6026
                         secret.data(),
6027
                         secret.size(),
6028
                         prover_id.data(),
6029
                         prover_id.size(),
6030
                         verifier_id.data(),
6031
                         verifier_id.size(),
6032
                         context.data(),
6033
                         context.size()));
6034
         };
4 ✔
6035

6036
         const auto init_verifier = [&](botan_spake2p_verifier_t* verifier) {
4 ✔
6037
            TEST_FFI_OK(botan_spake2p_verifier_init,
3 ✔
6038
                        (verifier,
6039
                         params,
6040
                         record.data(),
6041
                         record.size(),
6042
                         prover_id.data(),
6043
                         prover_id.size(),
6044
                         verifier_id.data(),
6045
                         verifier_id.size(),
6046
                         context.data(),
6047
                         context.size()));
6048
         };
4 ✔
6049

6050
         // A successful exchange
6051
         {
1 ✔
6052
            botan_spake2p_prover_t prover;
1 ✔
6053
            init_prover(&prover);
1 ✔
6054
            botan_spake2p_verifier_t verifier;
1 ✔
6055
            init_verifier(&verifier);
1 ✔
6056

6057
            ViewBytesSink share_p;
1 ✔
6058
            TEST_FFI_OK(botan_spake2p_prover_generate_message, (prover, rng, share_p.delegate(), share_p.callback()));
1 ✔
6059
            result.test_sz_eq("share length", share_p.size(), share_size);
1 ✔
6060
            TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE,
1 ✔
6061
                        botan_spake2p_prover_generate_message,
6062
                        (prover, rng, share_p.delegate(), share_p.callback()));
6063

6064
            ViewBytesSink verifier_msg;
1 ✔
6065
            TEST_FFI_OK(
1 ✔
6066
               botan_spake2p_verifier_process_message,
6067
               (verifier, rng, share_p.data(), share_p.size(), verifier_msg.delegate(), verifier_msg.callback()));
6068
            result.test_sz_eq("verifier message length", verifier_msg.size(), share_size + confirmation_size);
1 ✔
6069

6070
            // The shared secret is not available before confirmation
6071
            ViewBytesSink key2;
1 ✔
6072
            TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE,
1 ✔
6073
                        botan_spake2p_verifier_shared_secret,
6074
                        (verifier, key2.delegate(), key2.callback()));
6075

6076
            ViewBytesSink confirmation;
1 ✔
6077
            TEST_FFI_OK(botan_spake2p_prover_process_message,
1 ✔
6078
                        (prover,
6079
                         rng,
6080
                         verifier_msg.data(),
6081
                         verifier_msg.size(),
6082
                         confirmation.delegate(),
6083
                         confirmation.callback()));
6084
            result.test_sz_eq("confirmation length", confirmation.size(), confirmation_size);
1 ✔
6085

6086
            TEST_FFI_OK(botan_spake2p_verifier_verify_confirmation,
1 ✔
6087
                        (verifier, confirmation.data(), confirmation.size()));
6088

6089
            ViewBytesSink key1;
1 ✔
6090
            TEST_FFI_OK(botan_spake2p_prover_shared_secret, (prover, key1.delegate(), key1.callback()));
1 ✔
6091
            result.test_sz_eq("shared secret length", key1.size(), 32);
1 ✔
6092

6093
            TEST_FFI_OK(botan_spake2p_verifier_shared_secret, (verifier, key2.delegate(), key2.callback()));
1 ✔
6094

6095
            result.test_bin_eq("Shared secrets match", key1.get(), key2.get());
1 ✔
6096

6097
            TEST_FFI_OK(botan_spake2p_prover_destroy, (prover));
1 ✔
6098
            TEST_FFI_OK(botan_spake2p_verifier_destroy, (verifier));
1 ✔
6099
         }
1 ✔
6100

6101
         // An exchange where the verifier explicitly skips the prover's confirmation
6102
         {
1 ✔
6103
            botan_spake2p_prover_t prover;
1 ✔
6104
            init_prover(&prover);
1 ✔
6105
            botan_spake2p_verifier_t verifier;
1 ✔
6106
            init_verifier(&verifier);
1 ✔
6107

6108
            TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE, botan_spake2p_verifier_skip_confirmation, (verifier));
1 ✔
6109

6110
            ViewBytesSink share_p;
1 ✔
6111
            TEST_FFI_OK(botan_spake2p_prover_generate_message, (prover, rng, share_p.delegate(), share_p.callback()));
1 ✔
6112

6113
            ViewBytesSink verifier_msg;
1 ✔
6114
            TEST_FFI_OK(
1 ✔
6115
               botan_spake2p_verifier_process_message,
6116
               (verifier, rng, share_p.data(), share_p.size(), verifier_msg.delegate(), verifier_msg.callback()));
6117

6118
            ViewBytesSink confirmation;
1 ✔
6119
            TEST_FFI_OK(botan_spake2p_prover_process_message,
1 ✔
6120
                        (prover,
6121
                         rng,
6122
                         verifier_msg.data(),
6123
                         verifier_msg.size(),
6124
                         confirmation.delegate(),
6125
                         confirmation.callback()));
6126

6127
            TEST_FFI_OK(botan_spake2p_verifier_skip_confirmation, (verifier));
1 ✔
6128

6129
            ViewBytesSink key1;
1 ✔
6130
            TEST_FFI_OK(botan_spake2p_prover_shared_secret, (prover, key1.delegate(), key1.callback()));
1 ✔
6131
            ViewBytesSink key2;
1 ✔
6132
            TEST_FFI_OK(botan_spake2p_verifier_shared_secret, (verifier, key2.delegate(), key2.callback()));
1 ✔
6133

6134
            result.test_bin_eq("Shared secrets match", key1.get(), key2.get());
1 ✔
6135

6136
            // Having skipped, the confirmation can no longer be checked
6137
            TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE,
1 ✔
6138
                        botan_spake2p_verifier_verify_confirmation,
6139
                        (verifier, confirmation.data(), confirmation.size()));
6140

6141
            TEST_FFI_OK(botan_spake2p_prover_destroy, (prover));
1 ✔
6142
            TEST_FFI_OK(botan_spake2p_verifier_destroy, (verifier));
1 ✔
6143
         }
1 ✔
6144

6145
         // A tampered confirmation is rejected and terminates the exchange
6146
         {
1 ✔
6147
            botan_spake2p_prover_t prover;
1 ✔
6148
            init_prover(&prover);
1 ✔
6149
            botan_spake2p_verifier_t verifier;
1 ✔
6150
            init_verifier(&verifier);
1 ✔
6151

6152
            ViewBytesSink share_p;
1 ✔
6153
            TEST_FFI_OK(botan_spake2p_prover_generate_message, (prover, rng, share_p.delegate(), share_p.callback()));
1 ✔
6154

6155
            ViewBytesSink verifier_msg;
1 ✔
6156
            TEST_FFI_OK(
1 ✔
6157
               botan_spake2p_verifier_process_message,
6158
               (verifier, rng, share_p.data(), share_p.size(), verifier_msg.delegate(), verifier_msg.callback()));
6159

6160
            // Tamper with the verifier's key confirmation
6161
            std::vector<uint8_t> tampered_msg(verifier_msg.get().begin(), verifier_msg.get().end());
1 ✔
6162
            tampered_msg[tampered_msg.size() - 1] ^= 0x01;
1 ✔
6163

6164
            ViewBytesSink confirmation;
1 ✔
6165
            TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_MAC,
1 ✔
6166
                        botan_spake2p_prover_process_message,
6167
                        (prover,
6168
                         rng,
6169
                         tampered_msg.data(),
6170
                         tampered_msg.size(),
6171
                         confirmation.delegate(),
6172
                         confirmation.callback()));
6173

6174
            ViewBytesSink key;
1 ✔
6175
            TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE,
1 ✔
6176
                        botan_spake2p_prover_shared_secret,
6177
                        (prover, key.delegate(), key.callback()));
6178

6179
            const std::vector<uint8_t> zeros(confirmation_size);
1 ✔
6180
            TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_MAC,
1 ✔
6181
                        botan_spake2p_verifier_verify_confirmation,
6182
                        (verifier, zeros.data(), zeros.size()));
6183

6184
            TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE, botan_spake2p_verifier_skip_confirmation, (verifier));
1 ✔
6185

6186
            TEST_FFI_OK(botan_spake2p_prover_destroy, (prover));
1 ✔
6187
            TEST_FFI_OK(botan_spake2p_verifier_destroy, (verifier));
1 ✔
6188
         }
1 ✔
6189

6190
         // Custom system parameters over the same group, so the secret can be reused
6191
         if(Botan::EC_Group::from_name("secp256r1").hash_to_curve_supported("SHA-256")) {
1 ✔
6192
            botan_ec_group_t group;
1 ✔
6193
            TEST_FFI_OK(botan_ec_group_from_name, (&group, "secp256r1"));
1 ✔
6194

6195
            const std::vector<uint8_t> seed = {'f', 'f', 'i', ' ', 's', 'e', 'e', 'd'};
1 ✔
6196
            botan_spake2p_params_t cparams;
1 ✔
6197
            const int rc = botan_spake2p_params_init_custom(&cparams, group, seed.data(), seed.size(), "SHA-256");
1 ✔
6198

6199
            if(rc == BOTAN_FFI_SUCCESS) {
1 ✔
6200
               ViewBytesSink crecord;
1 ✔
6201
               TEST_FFI_OK(botan_spake2p_registration_record,
1 ✔
6202
                           (cparams, rng, secret.data(), secret.size(), crecord.delegate(), crecord.callback()));
6203

6204
               botan_spake2p_prover_t prover;
1 ✔
6205
               TEST_FFI_OK(botan_spake2p_prover_init,
1 ✔
6206
                           (&prover,
6207
                            cparams,
6208
                            secret.data(),
6209
                            secret.size(),
6210
                            prover_id.data(),
6211
                            prover_id.size(),
6212
                            verifier_id.data(),
6213
                            verifier_id.size(),
6214
                            context.data(),
6215
                            context.size()));
6216

6217
               botan_spake2p_verifier_t verifier;
1 ✔
6218
               TEST_FFI_OK(botan_spake2p_verifier_init,
1 ✔
6219
                           (&verifier,
6220
                            cparams,
6221
                            crecord.data(),
6222
                            crecord.size(),
6223
                            prover_id.data(),
6224
                            prover_id.size(),
6225
                            verifier_id.data(),
6226
                            verifier_id.size(),
6227
                            context.data(),
6228
                            context.size()));
6229

6230
               ViewBytesSink share_p;
1 ✔
6231
               TEST_FFI_OK(botan_spake2p_prover_generate_message,
1 ✔
6232
                           (prover, rng, share_p.delegate(), share_p.callback()));
6233

6234
               ViewBytesSink verifier_msg;
1 ✔
6235
               TEST_FFI_OK(
1 ✔
6236
                  botan_spake2p_verifier_process_message,
6237
                  (verifier, rng, share_p.data(), share_p.size(), verifier_msg.delegate(), verifier_msg.callback()));
6238

6239
               ViewBytesSink confirmation;
1 ✔
6240
               TEST_FFI_OK(botan_spake2p_prover_process_message,
1 ✔
6241
                           (prover,
6242
                            rng,
6243
                            verifier_msg.data(),
6244
                            verifier_msg.size(),
6245
                            confirmation.delegate(),
6246
                            confirmation.callback()));
6247

6248
               TEST_FFI_OK(botan_spake2p_verifier_verify_confirmation,
1 ✔
6249
                           (verifier, confirmation.data(), confirmation.size()));
6250

6251
               ViewBytesSink key1;
1 ✔
6252
               TEST_FFI_OK(botan_spake2p_prover_shared_secret, (prover, key1.delegate(), key1.callback()));
1 ✔
6253
               ViewBytesSink key2;
1 ✔
6254
               TEST_FFI_OK(botan_spake2p_verifier_shared_secret, (verifier, key2.delegate(), key2.callback()));
1 ✔
6255

6256
               result.test_bin_eq("Shared secrets match", key1.get(), key2.get());
1 ✔
6257

6258
               TEST_FFI_OK(botan_spake2p_prover_destroy, (prover));
1 ✔
6259
               TEST_FFI_OK(botan_spake2p_verifier_destroy, (verifier));
1 ✔
6260
               TEST_FFI_OK(botan_spake2p_params_destroy, (cparams));
1 ✔
6261
            } else {
1 ✔
6262
               result.test_is_true("custom params requires hash to curve support",
×
6263
                                   rc == BOTAN_FFI_ERROR_NOT_IMPLEMENTED);
6264
            }
6265

6266
            TEST_FFI_OK(botan_ec_group_destroy, (group));
1 ✔
6267
         }
1 ✔
6268

6269
         TEST_FFI_OK(botan_spake2p_params_destroy, (params));
1 ✔
6270
      }
1 ✔
6271
};
6272

6273
// NOLINTEND(*-init-variables)
6274

6275
BOTAN_REGISTER_TEST("ffi", "ffi_utils", FFI_Utils_Test);
6276
BOTAN_REGISTER_TEST("ffi", "ffi_rng", FFI_RNG_Test);
6277
BOTAN_REGISTER_TEST("ffi", "ffi_rsa_cert", FFI_RSA_Cert_Test);
6278
BOTAN_REGISTER_TEST("ffi", "ffi_zfec", FFI_ZFEC_Test);
6279
BOTAN_REGISTER_TEST("ffi", "ffi_crl", FFI_CRL_Test);
6280
BOTAN_REGISTER_TEST("ffi", "ffi_cert_validation", FFI_Cert_Validation_Test);
6281
BOTAN_REGISTER_TEST("ffi", "ffi_ecdsa_certificate", FFI_ECDSA_Certificate_Test);
6282
BOTAN_REGISTER_TEST("ffi", "ffi_cert_ext_keyusage", FFI_Cert_ExtKeyUsages_Test);
6283
BOTAN_REGISTER_TEST("ffi", "ffi_pkcs_hashid", FFI_PKCS_Hashid_Test);
6284
BOTAN_REGISTER_TEST("ffi", "ffi_cbc_cipher", FFI_CBC_Cipher_Test);
6285
BOTAN_REGISTER_TEST("ffi", "ffi_gcm", FFI_GCM_Test);
6286
BOTAN_REGISTER_TEST("ffi", "ffi_chacha", FFI_ChaCha20Poly1305_Test);
6287
BOTAN_REGISTER_TEST("ffi", "ffi_eax", FFI_EAX_Test);
6288
BOTAN_REGISTER_TEST("ffi", "ffi_aead", FFI_AEAD_Test);
6289
BOTAN_REGISTER_TEST("ffi", "ffi_streamcipher", FFI_StreamCipher_Test);
6290
BOTAN_REGISTER_TEST("ffi", "ffi_xof", FFI_XOF_Test);
6291
BOTAN_REGISTER_TEST("ffi", "ffi_hashfunction", FFI_HashFunction_Test);
6292
BOTAN_REGISTER_TEST("ffi", "ffi_mac", FFI_MAC_Test);
6293
BOTAN_REGISTER_TEST("ffi", "ffi_scrypt", FFI_Scrypt_Test);
6294
BOTAN_REGISTER_TEST("ffi", "ffi_kdf", FFI_KDF_Test);
6295
BOTAN_REGISTER_TEST("ffi", "ffi_blockcipher", FFI_Blockcipher_Test);
6296
BOTAN_REGISTER_TEST("ffi", "ffi_errorhandling", FFI_ErrorHandling_Test);
6297
BOTAN_REGISTER_TEST("ffi", "ffi_base64", FFI_Base64_Test);
6298
BOTAN_REGISTER_TEST("ffi", "ffi_hex", FFI_Hex_Test);
6299
BOTAN_REGISTER_TEST("ffi", "ffi_mp", FFI_MP_Test);
6300
BOTAN_REGISTER_TEST("ffi", "ffi_fpe", FFI_FPE_Test);
6301
BOTAN_REGISTER_TEST("ffi", "ffi_totp", FFI_TOTP_Test);
6302
BOTAN_REGISTER_TEST("ffi", "ffi_hotp", FFI_HOTP_Test);
6303
BOTAN_REGISTER_TEST("ffi", "ffi_keywrap", FFI_Keywrap_Test);
6304
BOTAN_REGISTER_TEST("ffi", "ffi_xmss", FFI_XMSS_Test);
6305
BOTAN_REGISTER_TEST("ffi", "ffi_rsa", FFI_RSA_Test);
6306
BOTAN_REGISTER_TEST("ffi", "ffi_dsa", FFI_DSA_Test);
6307
BOTAN_REGISTER_TEST("ffi", "ffi_ecdsa", FFI_ECDSA_Test);
6308
BOTAN_REGISTER_TEST("ffi", "ffi_sm2_sig", FFI_SM2_Sig_Test);
6309
BOTAN_REGISTER_TEST("ffi", "ffi_sm2_enc", FFI_SM2_Enc_Test);
6310
BOTAN_REGISTER_TEST("ffi", "ffi_ecdh", FFI_ECDH_Test);
6311
BOTAN_REGISTER_TEST("ffi", "ffi_mceliece", FFI_McEliece_Test);
6312
BOTAN_REGISTER_TEST("ffi", "ffi_ed25519", FFI_Ed25519_Test);
6313
BOTAN_REGISTER_TEST("ffi", "ffi_ed448", FFI_Ed448_Test);
6314
BOTAN_REGISTER_TEST("ffi", "ffi_x25519", FFI_X25519_Test);
6315
BOTAN_REGISTER_TEST("ffi", "ffi_x448", FFI_X448_Test);
6316
BOTAN_REGISTER_TEST("ffi", "ffi_kyber512", FFI_Kyber512_Test);
6317
BOTAN_REGISTER_TEST("ffi", "ffi_kyber768", FFI_Kyber768_Test);
6318
BOTAN_REGISTER_TEST("ffi", "ffi_kyber1024", FFI_Kyber1024_Test);
6319
BOTAN_REGISTER_TEST("ffi", "ffi_ml_kem", FFI_ML_KEM_Test);
6320
BOTAN_REGISTER_TEST("ffi", "ffi_ml_dsa", FFI_ML_DSA_Test);
6321
BOTAN_REGISTER_TEST("ffi", "ffi_slh_dsa", FFI_SLH_DSA_Test);
6322
BOTAN_REGISTER_TEST("ffi", "ffi_frodokem", FFI_FrodoKEM_Test);
6323
BOTAN_REGISTER_TEST("ffi", "ffi_cmce", FFI_Classic_McEliece_Test);
6324
BOTAN_REGISTER_TEST("ffi", "ffi_elgamal", FFI_ElGamal_Test);
6325
BOTAN_REGISTER_TEST("ffi", "ffi_dh", FFI_DH_Test);
6326
BOTAN_REGISTER_TEST("ffi", "ffi_oid", FFI_OID_Test);
6327
BOTAN_REGISTER_SERIALIZED_TEST("ffi", "ffi_ec_group", FFI_EC_Group_Test);
6328
BOTAN_REGISTER_TEST("ffi", "ffi_ec_points", FFI_EC_Point_Test);
6329
BOTAN_REGISTER_TEST("ffi", "ffi_srp6", FFI_SRP6_Test);
6330
BOTAN_REGISTER_TEST("ffi", "ffi_spake2p", FFI_SPAKE2P_Test);
6331

6332
   #if defined(BOTAN_HAS_X509)
6333
BOTAN_REGISTER_TEST("ffi", "ffi_cert_alt_names", FFI_Cert_AlternativeNames_Test);
6334
BOTAN_REGISTER_TEST("ffi", "ffi_cert_name_constraints", FFI_Cert_NameConstraints_Test);
6335
BOTAN_REGISTER_TEST("ffi", "ffi_cert_aia", FFI_Cert_AuthorityInformationAccess_Test);
6336
BOTAN_REGISTER_TEST("ffi", "ffi_cert_ext_rfc3779", FFI_Cert_ExtRFC3779_Test);
6337
   #endif
6338

6339
#endif
6340

6341
}  // namespace
6342

6343
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc