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

randombit / botan / 15276869812

27 May 2025 01:39PM UTC coverage: 90.953% (-0.02%) from 90.977%
15276869812

Pull #4877

github

web-flow
Merge 28ebe8b3d into fa28fc6f4
Pull Request #4877: Add FFI for X.509 creation

98141 of 107903 relevant lines covered (90.95%)

12735553.39 hits per line

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

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

10
#define BOTAN_NO_DEPRECATED_WARNINGS
11

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

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

26
#if defined(BOTAN_HAS_TPM2)
27
   #include <tss2/tss2_esys.h>
28
   #include <tss2/tss2_tctildr.h>
29
#endif
30

31
namespace Botan_Tests {
32

33
namespace {
34

35
#if defined(BOTAN_HAS_FFI)
36

37
   // NOLINTNEXTLINE(*-macro-usage)
38
   #define _TEST_FFI_STR_HELPER(x) #x
39
   // NOLINTNEXTLINE(*-macro-usage)
40
   #define _TEST_FFI_STR(x) _TEST_FFI_STR_HELPER(x)
41
   // NOLINTNEXTLINE(*-macro-usage)
42
   #define _TEST_FFI_SOURCE_LOCATION(func, file, line) (func " invoked at " file ":" _TEST_FFI_STR(line))
43

44
   // NOLINTNEXTLINE(*-macro-usage)
45
   #define TEST_FFI_OK(func, args) result.test_rc_ok(_TEST_FFI_SOURCE_LOCATION(#func, __FILE__, __LINE__), func args)
46
   // NOLINTNEXTLINE(*-macro-usage)
47
   #define TEST_FFI_INIT(func, args) \
48
      result.test_rc_init(_TEST_FFI_SOURCE_LOCATION(#func, __FILE__, __LINE__), func args)
49
   // NOLINTNEXTLINE(*-macro-usage)
50
   #define TEST_FFI_FAIL(msg, func, args) \
51
      result.test_rc_fail(_TEST_FFI_SOURCE_LOCATION(#func, __FILE__, __LINE__), msg, func args)
52
   // NOLINTNEXTLINE(*-macro-usage)
53
   #define TEST_FFI_RC(rc, func, args) \
54
      result.test_rc(_TEST_FFI_SOURCE_LOCATION(#func, __FILE__, __LINE__), rc, func args)
55

56
   // NOLINTNEXTLINE(*-macro-usage)
57
   #define REQUIRE_FFI_OK(func, args)                           \
58
      if(!TEST_FFI_OK(func, args)) {                            \
59
         result.test_note("Exiting test early due to failure"); \
60
         return;                                                \
61
      }
62

63
class FFI_Test : public Test {
52✔
64
   public:
65
      std::vector<Test::Result> run() override {
52✔
66
         Test::Result result(this->name());
52✔
67

68
         botan_rng_t rng;
52✔
69
         if(botan_rng_init(&rng, "system") != 0) {
52✔
70
            result.test_failure("Failed to init RNG");
×
71
            return {result};
×
72
         }
73

74
         result.start_timer();
52✔
75
         ffi_test(result, rng);
52✔
76
         result.end_timer();
52✔
77

78
         botan_rng_destroy(rng);
52✔
79

80
         return {result};
104✔
81
      }
104✔
82

83
   private:
84
      virtual std::string name() const = 0;
85
      virtual void ffi_test(Test::Result& result, botan_rng_t rng) = 0;
86
};
87

88
void ffi_test_pubkey_export(Test::Result& result, botan_pubkey_t pub, botan_privkey_t priv, botan_rng_t rng) {
11✔
89
   // export public key
90
   size_t pubkey_len = 0;
11✔
91
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
11✔
92
               botan_pubkey_export,
93
               (pub, nullptr, &pubkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
94

95
   std::vector<uint8_t> pubkey(pubkey_len);
11✔
96
   TEST_FFI_OK(botan_pubkey_export, (pub, pubkey.data(), &pubkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
11✔
97

98
   pubkey_len = 0;
11✔
99
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
11✔
100
               botan_pubkey_export,
101
               (pub, nullptr, &pubkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
102

103
   pubkey.resize(pubkey_len);
11✔
104
   TEST_FFI_OK(botan_pubkey_export, (pub, pubkey.data(), &pubkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
11✔
105

106
   // reimport exported public key
107
   botan_pubkey_t pub_copy;
11✔
108
   TEST_FFI_OK(botan_pubkey_load, (&pub_copy, pubkey.data(), pubkey_len));
11✔
109
   TEST_FFI_OK(botan_pubkey_check_key, (pub_copy, rng, 0));
11✔
110
   TEST_FFI_OK(botan_pubkey_destroy, (pub_copy));
11✔
111

112
   // export private key
113
   std::vector<uint8_t> privkey;
11✔
114
   size_t privkey_len = 0;
11✔
115

116
   // call with nullptr to query the length
117
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
11✔
118
               botan_privkey_export,
119
               (priv, nullptr, &privkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
120

121
   privkey.resize(privkey_len);
11✔
122
   privkey_len = privkey.size();  // set buffer size
11✔
123

124
   TEST_FFI_OK(botan_privkey_export, (priv, privkey.data(), &privkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
11✔
125

126
   privkey.resize(privkey_len);
11✔
127

128
   result.test_gte("Reasonable size", privkey.size(), 32);
11✔
129

130
   // reimport exported private key
131
   botan_privkey_t copy;
11✔
132
   TEST_FFI_OK(botan_privkey_load, (&copy, rng, privkey.data(), privkey.size(), nullptr));
11✔
133
   botan_privkey_destroy(copy);
11✔
134

135
   // Now again for PEM
136
   privkey_len = 0;
11✔
137

138
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
11✔
139
               botan_privkey_export,
140
               (priv, nullptr, &privkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
141

142
   privkey.resize(privkey_len);
11✔
143
   TEST_FFI_OK(botan_privkey_export, (priv, privkey.data(), &privkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
11✔
144

145
   TEST_FFI_OK(botan_privkey_load, (&copy, rng, privkey.data(), privkey.size(), nullptr));
11✔
146
   botan_privkey_destroy(copy);
11✔
147

148
   #if defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_PKCS5_PBES2)
149
   const size_t pbkdf_iter = 1000;
11✔
150

151
   // export private key encrypted
152
   privkey_len = 0;
11✔
153
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
11✔
154
               botan_privkey_export_encrypted_pbkdf_iter,
155
               (priv, nullptr, &privkey_len, rng, "password", pbkdf_iter, "", "", BOTAN_PRIVKEY_EXPORT_FLAG_DER));
156

157
   privkey.resize(privkey_len);
11✔
158
   privkey_len = privkey.size();
11✔
159

160
   TEST_FFI_OK(
11✔
161
      botan_privkey_export_encrypted_pbkdf_iter,
162
      (priv, privkey.data(), &privkey_len, rng, "password", pbkdf_iter, "", "", BOTAN_PRIVKEY_EXPORT_FLAG_DER));
163

164
   // reimport encrypted private key
165
   botan_privkey_load(&copy, rng, privkey.data(), privkey.size(), "password");
11✔
166
   botan_privkey_destroy(copy);
11✔
167

168
   privkey_len = 0;
11✔
169
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
11✔
170
               botan_privkey_export_encrypted_pbkdf_iter,
171
               (priv, nullptr, &privkey_len, rng, "password", pbkdf_iter, "", "", BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
172

173
   privkey.resize(privkey_len);
11✔
174
   TEST_FFI_OK(
11✔
175
      botan_privkey_export_encrypted_pbkdf_iter,
176
      (priv, privkey.data(), &privkey_len, rng, "password", pbkdf_iter, "", "", BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
177

178
   privkey.resize(privkey_len * 2);
11✔
179
   privkey_len = privkey.size();
11✔
180
   const uint32_t pbkdf_msec = 100;
11✔
181
   size_t pbkdf_iters_out = 0;
11✔
182

183
      #if defined(BOTAN_HAS_SCRYPT)
184
   const std::string pbe_hash = "Scrypt";
11✔
185
      #else
186
   const std::string pbe_hash = "SHA-512";
187
      #endif
188

189
      #if defined(BOTAN_HAS_AEAD_GCM)
190
   const std::string pbe_cipher = "AES-256/GCM";
11✔
191
      #else
192
   const std::string pbe_cipher = "AES-256/CBC";
193
      #endif
194

195
   TEST_FFI_OK(botan_privkey_export_encrypted_pbkdf_msec,
11✔
196
               (priv,
197
                privkey.data(),
198
                &privkey_len,
199
                rng,
200
                "password",
201
                pbkdf_msec,
202
                &pbkdf_iters_out,
203
                pbe_cipher.c_str(),
204
                pbe_hash.c_str(),
205
                0));
206

207
   if(pbe_hash == "Scrypt") {
11✔
208
      result.test_eq("Scrypt iters set to zero in this API", pbkdf_iters_out, 0);
22✔
209
   } else {
210
      // PBKDF2 currently always rounds to multiple of 2000
211
      result.test_eq("Expected PBKDF2 iters", pbkdf_iters_out % 2000, 0);
×
212
   }
213

214
   privkey.resize(privkey_len);
11✔
215

216
   TEST_FFI_OK(botan_privkey_load, (&copy, rng, privkey.data(), privkey.size(), "password"));
11✔
217
   botan_privkey_destroy(copy);
11✔
218
   #endif
219

220
   // calculate fingerprint
221
   size_t strength = 0;
11✔
222
   TEST_FFI_OK(botan_pubkey_estimated_strength, (pub, &strength));
11✔
223
   result.test_gte("estimated strength", strength, 1);
11✔
224

225
   size_t fingerprint_len = 0;
11✔
226
   TEST_FFI_RC(
11✔
227
      BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_pubkey_fingerprint, (pub, "SHA-256", nullptr, &fingerprint_len));
228

229
   std::vector<uint8_t> fingerprint(fingerprint_len);
11✔
230
   TEST_FFI_OK(botan_pubkey_fingerprint, (pub, "SHA-256", fingerprint.data(), &fingerprint_len));
22✔
231
}
33✔
232

233
class FFI_Utils_Test final : public FFI_Test {
×
234
   public:
235
      std::string name() const override { return "FFI Utils"; }
1✔
236

237
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
238
         result.test_is_eq("FFI API version macro", uint32_t(BOTAN_FFI_API_VERSION), uint32_t(BOTAN_HAS_FFI));
1✔
239
         result.test_is_eq("FFI API version function", botan_ffi_api_version(), uint32_t(BOTAN_HAS_FFI));
1✔
240
         result.test_is_eq("Major version", botan_version_major(), Botan::version_major());
1✔
241
         result.test_is_eq("Minor version", botan_version_minor(), Botan::version_minor());
1✔
242
         result.test_is_eq("Patch version", botan_version_patch(), Botan::version_patch());
1✔
243
         result.test_is_eq("Botan version", botan_version_string(), Botan::version_cstr());
1✔
244
         result.test_is_eq("Botan version datestamp", botan_version_datestamp(), Botan::version_datestamp());
1✔
245
         result.test_is_eq("FFI supports its own version", botan_ffi_supports_api(botan_ffi_api_version()), 0);
1✔
246

247
         result.test_is_eq("FFI compile time time var matches botan_ffi_api_version",
1✔
248
                           botan_ffi_api_version(),
1✔
249
                           uint32_t(BOTAN_FFI_API_VERSION));
1✔
250

251
         result.test_is_eq("FFI supports 2.0 version", botan_ffi_supports_api(20150515), 0);
1✔
252
         result.test_is_eq("FFI supports 2.1 version", botan_ffi_supports_api(20170327), 0);
1✔
253
         result.test_is_eq("FFI supports 2.3 version", botan_ffi_supports_api(20170815), 0);
1✔
254
         result.test_is_eq("FFI supports 2.8 version", botan_ffi_supports_api(20180713), 0);
1✔
255

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

258
         const std::vector<uint8_t> mem1 = {0xFF, 0xAA, 0xFF};
1✔
259
         const std::vector<uint8_t> mem2 = {0xFF, 0xA9, 0xFF};
1✔
260

261
         TEST_FFI_RC(0, botan_constant_time_compare, (mem1.data(), mem1.data(), mem1.size()));
1✔
262
         TEST_FFI_RC(-1, botan_constant_time_compare, (mem1.data(), mem2.data(), mem1.size()));
1✔
263

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

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

270
         std::string outstr;
1✔
271
         std::vector<uint8_t> outbuf;
1✔
272

273
         outstr.resize(2 * bin.size());
1✔
274
         TEST_FFI_OK(botan_hex_encode, (bin.data(), bin.size(), &outstr[0], 0));
1✔
275
         result.test_eq("uppercase hex", outstr, "AADE01");
2✔
276

277
         TEST_FFI_OK(botan_hex_encode, (bin.data(), bin.size(), &outstr[0], BOTAN_FFI_HEX_LOWER_CASE));
1✔
278
         result.test_eq("lowercase hex", outstr, "aade01");
2✔
279
      }
4✔
280
};
281

282
class FFI_RNG_Test final : public FFI_Test {
×
283
   public:
284
      std::string name() const override { return "FFI RNG"; }
1✔
285

286
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
287
         // RNG test and initialization
288
         botan_rng_t rng;
1✔
289
         botan_rng_t system_rng;
1✔
290
         botan_rng_t hwrng_rng = nullptr;
1✔
291
         botan_rng_t null_rng;
1✔
292
         botan_rng_t custom_rng;
1✔
293
         botan_rng_t tpm2_rng = nullptr;
1✔
294

295
         botan_tpm2_ctx_t tpm2_ctx = nullptr;
1✔
296
         botan_tpm2_session_t tpm2_session = nullptr;
1✔
297

298
         TEST_FFI_FAIL("invalid rng type", botan_rng_init, (&rng, "invalid_type"));
2✔
299

300
         REQUIRE_FFI_OK(botan_rng_init, (&system_rng, "system"));
1✔
301
         REQUIRE_FFI_OK(botan_rng_init, (&null_rng, "null"));
1✔
302

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

306
         std::vector<uint8_t> outbuf(512);
1✔
307

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

311
         if(rc != 0) {
1✔
312
            REQUIRE_FFI_OK(botan_rng_init, (&rng, "user"));
×
313
            REQUIRE_FFI_OK(botan_rng_destroy, (rng));
×
314
         }
315

316
         if(rc == 0) {
1✔
317
            TEST_FFI_OK(botan_rng_get, (rng, outbuf.data(), outbuf.size()));
1✔
318
            TEST_FFI_OK(botan_rng_reseed, (rng, 256));
1✔
319

320
            TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE, botan_rng_reseed_from_rng, (rng, null_rng, 256));
1✔
321
            if(hwrng_rng) {
1✔
322
               TEST_FFI_OK(botan_rng_reseed_from_rng, (rng, hwrng_rng, 256));
2✔
323
            }
324
            TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE, botan_rng_get, (null_rng, outbuf.data(), outbuf.size()));
1✔
325

326
            TEST_FFI_OK(botan_rng_destroy, (rng));
2✔
327
         }
328

329
         if(TEST_FFI_OK(botan_rng_init, (&rng, "user"))) {
1✔
330
            TEST_FFI_OK(botan_rng_get, (rng, outbuf.data(), outbuf.size()));
1✔
331
            TEST_FFI_OK(botan_rng_reseed, (rng, 256));
1✔
332

333
            TEST_FFI_OK(botan_rng_reseed_from_rng, (rng, system_rng, 256));
1✔
334

335
            uint8_t not_really_entropy[32] = {0};
1✔
336
            TEST_FFI_OK(botan_rng_add_entropy, (rng, not_really_entropy, 32));
2✔
337
         }
338

339
         uint8_t system_rng_buf[4096];
1✔
340
         TEST_FFI_OK(botan_system_rng_get, (system_rng_buf, sizeof(system_rng_buf)));
1✔
341

342
         size_t cb_counter = 0;
1✔
343

344
         auto custom_get_cb = +[](void* context, uint8_t* out, size_t out_len) -> int {
1✔
345
            for(size_t i = 0; i != out_len; ++i) {
346
               out[i] = 0x12;
347
            }
348
            (*(static_cast<size_t*>(context)))++;
349
            return 0;
350
         };
351

352
         auto custom_add_entropy_cb = +[](void* context, const uint8_t input[], size_t length) -> int {
1✔
353
            BOTAN_UNUSED(input, length);
354
            (*(static_cast<size_t*>(context)))++;
355
            return 0;
356
         };
357

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

360
         if(TEST_FFI_OK(
1✔
361
               botan_rng_init_custom,
362
               (&custom_rng, "custom rng", &cb_counter, custom_get_cb, custom_add_entropy_cb, custom_destroy_cb))) {
363
            Botan::clear_mem(outbuf.data(), outbuf.size());
1✔
364
            TEST_FFI_OK(botan_rng_get, (custom_rng, outbuf.data(), outbuf.size()));
1✔
365
            result.test_eq("custom_get_cb called", cb_counter, 1);
1✔
366
            std::vector<uint8_t> pattern(outbuf.size(), 0x12);
1✔
367
            result.test_eq("custom_get_cb returned bytes", pattern, outbuf);
2✔
368

369
            TEST_FFI_OK(botan_rng_reseed, (custom_rng, 256));
1✔
370
            result.test_eq("custom_add_entropy_cb called", cb_counter, 2);
1✔
371

372
            TEST_FFI_OK(botan_rng_reseed_from_rng, (custom_rng, system_rng, 256));
1✔
373
            result.test_eq("custom_add_entropy_cb called", cb_counter, 3);
1✔
374

375
            uint8_t not_really_entropy[32] = {0};
1✔
376
            TEST_FFI_OK(botan_rng_add_entropy, (custom_rng, not_really_entropy, 32));
1✔
377
            result.test_eq("custom_add_entropy_cb called", cb_counter, 4);
1✔
378

379
            TEST_FFI_OK(botan_rng_destroy, (custom_rng));
1✔
380
            result.test_eq("custom_destroy_cb called", cb_counter, 5);
2✔
381
         }
1✔
382

383
   #ifdef BOTAN_HAS_JITTER_RNG
384
         botan_rng_t jitter_rng;
1✔
385
         if(TEST_FFI_OK(botan_rng_init, (&jitter_rng, "jitter"))) {
1✔
386
            std::vector<uint8_t> buf(256);
1✔
387
            TEST_FFI_OK(botan_rng_get, (jitter_rng, outbuf.data(), buf.size()));
1✔
388
            TEST_FFI_OK(botan_rng_destroy, (jitter_rng));
2✔
389
         }
1✔
390
   #endif
391

392
         const auto tcti_name = Test::options().tpm2_tcti_name().value_or("");
1✔
393
         const auto tcti_conf = Test::options().tpm2_tcti_conf().value_or("");
1✔
394
         if(tcti_name.empty() || tcti_name == "disabled") {
1✔
395
            result.test_note("TPM2 tests are disabled.");
×
396
         } else {
397
            auto tpm2_test_rng = [&](botan_tpm2_ctx_t tpm2_context) {
3✔
398
               // Create and use an RNG without a TPM2 session
399
               // (communication between application and TPM won't be encrypted)
400
               if(TEST_FFI_INIT(botan_tpm2_rng_init, (&tpm2_rng, tpm2_context, nullptr, nullptr, nullptr))) {
2✔
401
                  Botan::clear_mem(outbuf.data(), outbuf.size());
2✔
402

403
                  TEST_FFI_OK(botan_rng_get, (tpm2_rng, outbuf.data(), outbuf.size()));
2✔
404
                  TEST_FFI_OK(botan_rng_reseed, (tpm2_rng, 256));
2✔
405

406
                  TEST_FFI_OK(botan_rng_reseed_from_rng, (tpm2_rng, system_rng, 256));
2✔
407

408
                  uint8_t not_really_entropy[32] = {0};
2✔
409
                  TEST_FFI_OK(botan_rng_add_entropy, (tpm2_rng, not_really_entropy, 32));
2✔
410
                  TEST_FFI_OK(botan_rng_destroy, (tpm2_rng));
4✔
411
               }
412

413
               // Create an anonymous TPM2 session
414
               if(TEST_FFI_INIT(botan_tpm2_unauthenticated_session_init, (&tpm2_session, tpm2_context))) {
2✔
415
                  // Create and use an RNG with an anonymous TPM2 session
416
                  // (communication between application and TPM will be encrypted)
417
                  if(TEST_FFI_INIT(botan_tpm2_rng_init, (&tpm2_rng, tpm2_context, tpm2_session, nullptr, nullptr))) {
2✔
418
                     Botan::clear_mem(outbuf.data(), outbuf.size());
2✔
419

420
                     TEST_FFI_OK(botan_rng_get, (tpm2_rng, outbuf.data(), outbuf.size()));
2✔
421
                     TEST_FFI_OK(botan_rng_reseed, (tpm2_rng, 256));
2✔
422

423
                     TEST_FFI_OK(botan_rng_reseed_from_rng, (tpm2_rng, system_rng, 256));
2✔
424

425
                     uint8_t not_really_entropy[32] = {0};
2✔
426
                     TEST_FFI_OK(botan_rng_add_entropy, (tpm2_rng, not_really_entropy, 32));
2✔
427
                     TEST_FFI_OK(botan_rng_destroy, (tpm2_rng));
4✔
428
                  }
429

430
                  TEST_FFI_OK(botan_tpm2_session_destroy, (tpm2_session));
4✔
431
               }
432
            };
2✔
433

434
            if(TEST_FFI_INIT(botan_tpm2_ctx_init_ex, (&tpm2_ctx, tcti_name.c_str(), tcti_conf.c_str()))) {
1✔
435
               if(botan_tpm2_supports_crypto_backend() == 1) {
1✔
436
                  TEST_FFI_OK(botan_tpm2_ctx_enable_crypto_backend, (tpm2_ctx, system_rng));
1✔
437
                  result.test_note("TPM2 crypto backend enabled");
2✔
438
               } else {
439
                  result.test_note("TPM2 crypto backend not supported");
×
440
               }
441

442
               tpm2_test_rng(tpm2_ctx);
1✔
443
               TEST_FFI_OK(botan_tpm2_ctx_destroy, (tpm2_ctx));
2✔
444
            }
445

446
   #if defined(BOTAN_HAS_TPM2)
447
            TSS2_TCTI_CONTEXT* tcti_ctx;
1✔
448
            ESYS_CONTEXT* esys_ctx;
1✔
449

450
            if(TEST_FFI_INIT(Tss2_TctiLdr_Initialize_Ex, (tcti_name.c_str(), tcti_conf.c_str(), &tcti_ctx))) {
1✔
451
               if(TEST_FFI_INIT(Esys_Initialize, (&esys_ctx, tcti_ctx, nullptr /* ABI version */))) {
1✔
452
                  botan_tpm2_crypto_backend_state_t cbs = nullptr;
1✔
453

454
                  // enable the botan-based TSS2 crypto backend on a bare ESYS_CONTEXT
455
                  if(botan_tpm2_supports_crypto_backend() == 1) {
1✔
456
                     TEST_FFI_OK(botan_tpm2_enable_crypto_backend, (&cbs, esys_ctx, system_rng));
1✔
457
                     result.test_note("TPM2 crypto backend enabled");
2✔
458
                  } else {
459
                     result.test_note("TPM2 crypto backend not supported");
×
460
                  }
461

462
                  // initialize the Botan TPM2 FFI wrapper from the bare ESYS_CONTEXT
463
                  if(TEST_FFI_INIT(botan_tpm2_ctx_from_esys, (&tpm2_ctx, esys_ctx))) {
1✔
464
                     tpm2_test_rng(tpm2_ctx);
1✔
465
                     TEST_FFI_OK(botan_tpm2_ctx_destroy, (tpm2_ctx));
2✔
466
                  }
467

468
                  if(cbs != nullptr) {
1✔
469
                     TEST_FFI_OK(botan_tpm2_crypto_backend_state_destroy, (cbs));
2✔
470
                  }
471

472
                  Esys_Finalize(&esys_ctx);
1✔
473
               }
474
               Tss2_TctiLdr_Finalize(&tcti_ctx);
1✔
475
            }
476
   #endif
477
         }
478

479
         TEST_FFI_OK(botan_rng_destroy, (rng));
1✔
480
         TEST_FFI_OK(botan_rng_destroy, (null_rng));
1✔
481
         TEST_FFI_OK(botan_rng_destroy, (system_rng));
1✔
482
         TEST_FFI_OK(botan_rng_destroy, (hwrng_rng));
2✔
483
      }
2✔
484
};
485

486
class FFI_RSA_Cert_Test final : public FFI_Test {
×
487
   public:
488
      std::string name() const override { return "FFI RSA cert"; }
1✔
489

490
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
491
         botan_x509_cert_t cert;
1✔
492
         if(TEST_FFI_INIT(botan_x509_cert_load_file, (&cert, Test::data_file("x509/ocsp/randombit.pem").c_str()))) {
1✔
493
            TEST_FFI_RC(0, botan_x509_cert_hostname_match, (cert, "randombit.net"));
1✔
494
            TEST_FFI_RC(0, botan_x509_cert_hostname_match, (cert, "www.randombit.net"));
1✔
495
            TEST_FFI_RC(-1, botan_x509_cert_hostname_match, (cert, "*.randombit.net"));
1✔
496
            TEST_FFI_RC(-1, botan_x509_cert_hostname_match, (cert, "flub.randombit.net"));
1✔
497
            TEST_FFI_RC(-1, botan_x509_cert_hostname_match, (cert, "randombit.net.com"));
1✔
498

499
            botan_x509_cert_t copy;
1✔
500
            TEST_FFI_OK(botan_x509_cert_dup, (&copy, cert));
1✔
501
            TEST_FFI_RC(0, botan_x509_cert_hostname_match, (copy, "randombit.net"));
1✔
502

503
            TEST_FFI_OK(botan_x509_cert_destroy, (copy));
1✔
504
            TEST_FFI_OK(botan_x509_cert_destroy, (cert));
2✔
505
         }
506
      }
1✔
507
};
508

509
class FFI_ZFEC_Test final : public FFI_Test {
×
510
   public:
511
      std::string name() const override { return "FFI ZFEC"; }
1✔
512

513
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
514
         /* exercise a simple success case
515
          */
516

517
         // Select some arbitrary, valid encoding parameters.  There is
518
         // nothing special about them but some relationships between these
519
         // values and other inputs must hold.
520
         const size_t K = 3;
1✔
521
         const size_t N = 11;
1✔
522

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

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

533
         // K of the blocks are required so the total information represented
534
         // can be this multiple.  totalSize must be a multiple of K and it
535
         // always will be using this construction.
536
         const size_t totalSize = blockSize * K;
1✔
537

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

542
         // Allocate memory for the encoding and decoding output parameters.
543
         std::vector<uint8_t> encoded_buf(N * blockSize);
1✔
544
         std::vector<uint8_t> decoded_buf(K * blockSize);
1✔
545

546
         std::vector<uint8_t*> encoded(N);
1✔
547
         for(size_t i = 0; i < N; ++i) {
12✔
548
            encoded[i] = &encoded_buf[i * blockSize];
11✔
549
         }
550
         std::vector<uint8_t*> decoded(K);
1✔
551
         for(size_t i = 0; i < K; ++i) {
4✔
552
            decoded[i] = &decoded_buf[i * blockSize];
3✔
553
         }
554

555
         // First encode the complete input string into N blocks where K are
556
         // required for reconstruction.  The N encoded blocks will end up in
557
         // `encoded`.
558
         if(!TEST_FFI_INIT(botan_zfec_encode, (K, N, input, totalSize, encoded.data()))) {
1✔
559
            return;
560
         }
561

562
         // Any K blocks can be decoded to reproduce the original input (split
563
         // across an array of K strings of blockSize bytes each).  This loop
564
         // only exercises decoding with consecutive blocks because it's
565
         // harder to pick non-consecutive blocks out for a test.
566
         for(size_t offset = 0; offset < N - K; ++offset) {
9✔
567
            result.test_note("About to decode with offset " + std::to_string(offset));
24✔
568
            // Pass in the K shares starting from `offset` (and their indexes)
569
            // so that we can try decoding a certain group of blocks here.  Any
570
            // K shares *should* work.
571
            REQUIRE_FFI_OK(botan_zfec_decode,
8✔
572
                           (K, N, indexes.data() + offset, encoded.data() + offset, blockSize, decoded.data()));
573

574
            // Check that the original input bytes have been written to the
575
            // output parameter.
576
            for(size_t k = 0, pos = 0; k < K; ++k, pos += blockSize) {
32✔
577
               TEST_FFI_RC(0, botan_constant_time_compare, (input + pos, decoded[k], blockSize));
48✔
578
            }
579
         }
580

581
         /* Exercise a couple basic failure cases, such as you encounter if
582
          * the caller supplies invalid parameters.  We don't try to
583
          * exhaustively prove invalid parameters are handled through this
584
          * interface since the implementation only passes them through to
585
          * ZFEC::{encode,decode} where the real checking is.  We just want to
586
          * see that errors can propagate.
587
          */
588
         TEST_FFI_FAIL("encode with out-of-bounds encoding parameters should have failed",
2✔
589
                       botan_zfec_encode,
590
                       (0, 0, nullptr, 0, nullptr));
591
         TEST_FFI_FAIL("decode with out-of-bounds encoding parameters should have failed",
2✔
592
                       botan_zfec_decode,
593
                       (0, 0, nullptr, nullptr, 0, nullptr));
594
      }
3✔
595
};
596

597
class FFI_CRL_Test final : public FFI_Test {
×
598
   public:
599
      std::string name() const override { return "FFI CRL"; }
1✔
600

601
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
602
         const char* crl_string =
1✔
603
            "-----BEGIN X509 CRL-----\n"
604
            "MIICoTCCAQkCAQEwDQYJKoZIhvcNAQELBQAwgZQxLTArBgNVBAMTJFVzYWJsZSBj\n"
605
            "ZXJ0IHZhbGlkYXRpb246IFRlbXBvcmFyeSBDQTE5MDcGA1UECxMwQ2VudHJlIGZv\n"
606
            "ciBSZXNlYXJjaCBvbiBDcnlwdG9ncmFwaHkgYW5kIFNlY3VyaXR5MRswGQYDVQQK\n"
607
            "ExJNYXNhcnlrIFVuaXZlcnNpdHkxCzAJBgNVBAYTAkNaGA8yMDUwMDIyNTE1MjE0\n"
608
            "MloYDzIwNTAwMjI1MTUyNDQxWjAAoDowODAfBgNVHSMEGDAWgBRKzxAvI4+rVVo/\n"
609
            "JzLigRznREyB+TAVBgNVHRQEDgIMXcr16yNys/gjeuCFMA0GCSqGSIb3DQEBCwUA\n"
610
            "A4IBgQCfxv/5REM/KUnzeVycph3dJr1Yrtxhc6pZmQ9pMzSW/nawLN3rUHm5oG44\n"
611
            "ZuQgjvzE4PnbU0/DNRu/4w3H58kgrctJHHXbbvkU3lf2ZZLh2wBl+EUh92+/COow\n"
612
            "ZyGB+jqj/XwB99hYUhrY6NLEWRz08kpgG6dnNMEU0uFqdQKWk0CQPnmgPRgDb8BW\n"
613
            "IuMBcjY7aF9XoCZFOqPYdEvUKzAo4QGCf7uJ7fNGS3LqvjaLjAHJseSr5/yR7Q9r\n"
614
            "nEdI38yKPbRj0tNHe7j+BbYg31C+X+AZZKJtlTg8GxYR3qfQio1kDgpZ3rQLzHY3\n"
615
            "ea2MLX/Kdx9cPSwh4KwlcDxQmQKoELb4EnZW1CScSBHi9HQyCBNyCkgkOBMGcJqz\n"
616
            "Ihq1dGeSf8eca9+Avk5kAQ3yjXK1TI2CDEi0msrXLr9XbgowXiOLLzR+rYkhQz+V\n"
617
            "RnIoBwjnrGoJoz636KS170SZCB9ARNs17WE4IvbJdZrTXNOGaVZCQUUpiLRj4ZSO\n"
618
            "Na/nobI=\n"
619
            "-----END X509 CRL-----";
620

621
         botan_x509_crl_t bytecrl;
1✔
622
         if(!TEST_FFI_INIT(botan_x509_crl_load, (&bytecrl, reinterpret_cast<const uint8_t*>(crl_string), 966))) {
1✔
623
            return;
×
624
         }
625

626
         botan_x509_crl_t crl;
1✔
627
         REQUIRE_FFI_OK(botan_x509_crl_load_file, (&crl, Test::data_file("x509/nist/root.crl").c_str()));
1✔
628

629
         botan_x509_cert_t cert1;
1✔
630
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&cert1, Test::data_file("x509/nist/test01/end.crt").c_str()));
1✔
631
         TEST_FFI_RC(-1, botan_x509_is_revoked, (crl, cert1));
1✔
632
         TEST_FFI_OK(botan_x509_cert_destroy, (cert1));
1✔
633

634
         botan_x509_cert_t cert2;
1✔
635
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&cert2, Test::data_file("x509/nist/test20/int.crt").c_str()));
1✔
636
         TEST_FFI_RC(0, botan_x509_is_revoked, (crl, cert2));
1✔
637
         TEST_FFI_RC(-1, botan_x509_is_revoked, (bytecrl, cert2));
1✔
638
         TEST_FFI_OK(botan_x509_cert_destroy, (cert2));
1✔
639

640
         TEST_FFI_OK(botan_x509_crl_destroy, (crl));
1✔
641
         TEST_FFI_OK(botan_x509_crl_destroy, (bytecrl));
2✔
642
      }
643
};
644

645
class FFI_Cert_Validation_Test final : public FFI_Test {
×
646
   public:
647
      std::string name() const override { return "FFI Cert Validation"; }
1✔
648

649
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
650
         botan_x509_cert_t root;
1✔
651
         int rc;
1✔
652

653
         if(!TEST_FFI_INIT(botan_x509_cert_load_file, (&root, Test::data_file("x509/nist/root.crt").c_str()))) {
1✔
654
            return;
×
655
         }
656

657
         botan_x509_cert_t end2;
1✔
658
         botan_x509_cert_t sub2;
1✔
659
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&end2, Test::data_file("x509/nist/test02/end.crt").c_str()));
1✔
660
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&sub2, Test::data_file("x509/nist/test02/int.crt").c_str()));
1✔
661

662
         TEST_FFI_RC(1, botan_x509_cert_verify, (&rc, end2, &sub2, 1, &root, 1, nullptr, 0, nullptr, 0));
1✔
663
         result.confirm("Validation test02 failed", rc == 5002);
2✔
664
         result.test_eq("Validation test02 status string", botan_x509_cert_validation_status(rc), "Signature error");
1✔
665

666
         TEST_FFI_RC(1, botan_x509_cert_verify, (&rc, end2, nullptr, 0, &root, 1, nullptr, 0, nullptr, 0));
1✔
667
         result.confirm("Validation test02 failed (missing int)", rc == 3000);
2✔
668
         result.test_eq(
1✔
669
            "Validation test02 status string", botan_x509_cert_validation_status(rc), "Certificate issuer not found");
670

671
         botan_x509_cert_t end7;
1✔
672
         botan_x509_cert_t sub7;
1✔
673
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&end7, Test::data_file("x509/nist/test07/end.crt").c_str()));
1✔
674
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&sub7, Test::data_file("x509/nist/test07/int.crt").c_str()));
1✔
675

676
         botan_x509_cert_t subs[2] = {sub2, sub7};
1✔
677
         TEST_FFI_RC(1, botan_x509_cert_verify, (&rc, end7, subs, 2, &root, 1, nullptr, 0, nullptr, 0));
1✔
678
         result.confirm("Validation test07 failed with expected error", rc == 1001);
2✔
679
         result.test_eq("Validation test07 status string",
1✔
680
                        botan_x509_cert_validation_status(rc),
681
                        "Hash function used is considered too weak for security");
682

683
         TEST_FFI_RC(0, botan_x509_cert_verify, (&rc, end7, subs, 2, &root, 1, nullptr, 80, nullptr, 0));
1✔
684
         result.confirm("Validation test07 passed", rc == 0);
2✔
685
         result.test_eq("Validation test07 status string", botan_x509_cert_validation_status(rc), "Verified");
1✔
686

687
         TEST_FFI_RC(1,
1✔
688
                     botan_x509_cert_verify_with_crl,
689
                     (&rc, end7, subs, 2, nullptr, 0, nullptr, 0, "x509/farce", 0, nullptr, 0));
690
         result.confirm("Validation test07 failed with expected error", rc == 3000);
2✔
691
         result.test_eq(
1✔
692
            "Validation test07 status string", botan_x509_cert_validation_status(rc), "Certificate issuer not found");
693

694
         botan_x509_crl_t rootcrl;
1✔
695

696
         REQUIRE_FFI_OK(botan_x509_crl_load_file, (&rootcrl, Test::data_file("x509/nist/root.crl").c_str()));
1✔
697
         TEST_FFI_RC(
1✔
698
            0, botan_x509_cert_verify_with_crl, (&rc, end7, subs, 2, &root, 1, &rootcrl, 1, nullptr, 80, nullptr, 0));
699
         result.confirm("Validation test07 with CRL passed", rc == 0);
2✔
700
         result.test_eq("Validation test07 with CRL status string", botan_x509_cert_validation_status(rc), "Verified");
1✔
701

702
         botan_x509_cert_t end20;
1✔
703
         botan_x509_cert_t sub20;
1✔
704
         botan_x509_crl_t sub20crl;
1✔
705
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&end20, Test::data_file("x509/nist/test20/end.crt").c_str()));
1✔
706
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&sub20, Test::data_file("x509/nist/test20/int.crt").c_str()));
1✔
707
         REQUIRE_FFI_OK(botan_x509_crl_load_file, (&sub20crl, Test::data_file("x509/nist/test20/int.crl").c_str()));
1✔
708
         botan_x509_crl_t crls[2] = {sub20crl, rootcrl};
1✔
709
         TEST_FFI_RC(
1✔
710
            1, botan_x509_cert_verify_with_crl, (&rc, end20, &sub20, 1, &root, 1, crls, 2, nullptr, 80, nullptr, 0));
711
         result.confirm("Validation test20 failed with expected error", rc == 5000);
2✔
712
         result.test_eq(
1✔
713
            "Validation test20 status string", botan_x509_cert_validation_status(rc), "Certificate is revoked");
714

715
         TEST_FFI_OK(botan_x509_cert_destroy, (end2));
1✔
716
         TEST_FFI_OK(botan_x509_cert_destroy, (sub2));
1✔
717
         TEST_FFI_OK(botan_x509_cert_destroy, (end7));
1✔
718
         TEST_FFI_OK(botan_x509_cert_destroy, (sub7));
1✔
719
         TEST_FFI_OK(botan_x509_cert_destroy, (end20));
1✔
720
         TEST_FFI_OK(botan_x509_cert_destroy, (sub20));
1✔
721
         TEST_FFI_OK(botan_x509_crl_destroy, (sub20crl));
1✔
722
         TEST_FFI_OK(botan_x509_cert_destroy, (root));
1✔
723
         TEST_FFI_OK(botan_x509_crl_destroy, (rootcrl));
2✔
724
      }
725
};
726

727
class FFI_Cert_Creation_Test final : public FFI_Test {
×
728
   public:
729
      std::string name() const override { return "FFI Cert Creation"; }
1✔
730

731
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
732
         const std::string hash_fn{"SHA-256"};
1✔
733
         const std::string padding_method{"EMSA3(SHA-256)"};
1✔
734

735
         botan_privkey_t ca_key;
1✔
736
         botan_privkey_t cert_key;
1✔
737

738
         if(TEST_FFI_INIT(botan_privkey_create_rsa, (&ca_key, rng, 4096))) {
1✔
739
            TEST_FFI_OK(botan_privkey_create_rsa, (&cert_key, rng, 4096));
1✔
740

741
            uint64_t now =
1✔
742
               std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())
1✔
743
                  .count();
1✔
744

745
            botan_x509_time_t not_before;
1✔
746
            botan_x509_time_t not_after;
1✔
747

748
            if(TEST_FFI_INIT(botan_x509_create_time, (&not_before, now))) {
1✔
749
               TEST_FFI_OK(botan_x509_create_time, (&not_after, now + 60 * 60));
1✔
750

751
               botan_x509_cert_opts_t ca_opts;
1✔
752
               TEST_FFI_OK(botan_x509_create_cert_opts, (&ca_opts, "Test CA/US/Botan Project/Testing", nullptr));
1✔
753
               TEST_FFI_OK(botan_x509_cert_opts_ca_key, (ca_opts, 1));
1✔
754
               TEST_FFI_OK(botan_x509_cert_opts_uri, (ca_opts, "https://botan.randombit.net"));
1✔
755
               const char* dns_names[] = {"botan.randombit.net", "randombit.net"};
1✔
756
               TEST_FFI_OK(botan_x509_cert_opts_more_dns, (ca_opts, dns_names, 2));
1✔
757

758
               botan_x509_cert_t ca_cert;
1✔
759
               TEST_FFI_OK(botan_x509_create_self_signed_cert, (&ca_cert, ca_key, ca_opts, hash_fn.c_str(), "", rng));
1✔
760

761
               botan_x509_ca_t ca;
1✔
762
               TEST_FFI_OK(botan_x509_create_ca, (&ca, ca_cert, ca_key, hash_fn.c_str(), padding_method.c_str(), rng));
1✔
763

764
               botan_x509_cert_opts_t req_opts;
1✔
765
               TEST_FFI_OK(botan_x509_create_cert_opts, (&req_opts, "Test CA/US/Botan Project/Testing", nullptr));
1✔
766

767
               botan_x509_pkcs10_req_t req;
1✔
768
               TEST_FFI_OK(botan_x509_create_pkcs10_req, (&req, req_opts, cert_key, hash_fn.c_str(), rng));
1✔
769

770
               botan_x509_cert_t cert;
1✔
771
               TEST_FFI_OK(botan_x509_sign_req, (&cert, ca, req, rng, not_before, not_after));
1✔
772

773
               int rc;
1✔
774
               TEST_FFI_RC(0, botan_x509_cert_verify, (&rc, cert, nullptr, 0, &ca_cert, 1, nullptr, 0, nullptr, 0));
1✔
775

776
               TEST_FFI_OK(botan_x509_time_destroy, (not_before));
1✔
777
               TEST_FFI_OK(botan_x509_time_destroy, (not_after));
1✔
778
               TEST_FFI_OK(botan_x509_cert_opts_destroy, (ca_opts));
1✔
779
               TEST_FFI_OK(botan_x509_cert_opts_destroy, (req_opts));
1✔
780
               TEST_FFI_OK(botan_x509_ca_destroy, (ca));
1✔
781
               TEST_FFI_OK(botan_x509_pkcs10_req_destroy, (req));
1✔
782
               TEST_FFI_OK(botan_x509_cert_destroy, (ca_cert));
1✔
783
               TEST_FFI_OK(botan_x509_cert_destroy, (cert));
2✔
784
            }
785

786
            TEST_FFI_OK(botan_privkey_destroy, (ca_key));
1✔
787
            TEST_FFI_OK(botan_privkey_destroy, (cert_key));
2✔
788
         }
789
      }
1✔
790
};
791

792
class FFI_ECDSA_Certificate_Test final : public FFI_Test {
×
793
   public:
794
      std::string name() const override { return "FFI ECDSA cert"; }
1✔
795

796
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
797
         botan_x509_cert_t cert;
1✔
798
         if(TEST_FFI_INIT(botan_x509_cert_load_file, (&cert, Test::data_file("x509/ecc/isrg-root-x2.pem").c_str()))) {
1✔
799
            size_t date_len = 0;
1✔
800
            TEST_FFI_RC(
1✔
801
               BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_x509_cert_get_time_starts, (cert, nullptr, &date_len));
802

803
            date_len = 8;
1✔
804
            TEST_FFI_RC(
1✔
805
               BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_x509_cert_get_time_starts, (cert, nullptr, &date_len));
806

807
            std::string date(date_len - 1, '0');
1✔
808
            TEST_FFI_OK(botan_x509_cert_get_time_starts, (cert, &date[0], &date_len));
1✔
809
            result.test_eq("cert valid from", date, "200904000000Z");
2✔
810

811
            date_len = 0;
1✔
812
            TEST_FFI_RC(
1✔
813
               BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_x509_cert_get_time_expires, (cert, nullptr, &date_len));
814

815
            date.resize(date_len - 1);
1✔
816
            TEST_FFI_OK(botan_x509_cert_get_time_expires, (cert, &date[0], &date_len));
1✔
817
            result.test_eq("cert valid until", date, "400917160000Z");
2✔
818

819
            uint64_t not_before = 0;
1✔
820
            TEST_FFI_OK(botan_x509_cert_not_before, (cert, &not_before));
1✔
821
            result.confirm("cert not before", not_before == 1599177600);
2✔
822

823
            uint64_t not_after = 0;
1✔
824
            TEST_FFI_OK(botan_x509_cert_not_after, (cert, &not_after));
1✔
825
            result.confirm("cert not after", not_after == 2231510400);
2✔
826

827
            size_t serial_len = 0;
1✔
828
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
829
                        botan_x509_cert_get_serial_number,
830
                        (cert, nullptr, &serial_len));
831

832
            std::vector<uint8_t> serial(serial_len);
1✔
833
            TEST_FFI_OK(botan_x509_cert_get_serial_number, (cert, serial.data(), &serial_len));
1✔
834
            result.test_eq("cert serial length", serial.size(), 16);
1✔
835
            result.test_eq("cert serial", Botan::hex_encode(serial), "41D29DD172EAEEA780C12C6CE92F8752");
2✔
836

837
            size_t fingerprint_len = 0;
1✔
838
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
839
                        botan_x509_cert_get_fingerprint,
840
                        (cert, "SHA-256", nullptr, &fingerprint_len));
841

842
            std::vector<uint8_t> fingerprint(fingerprint_len);
1✔
843
            TEST_FFI_OK(botan_x509_cert_get_fingerprint, (cert, "SHA-256", fingerprint.data(), &fingerprint_len));
1✔
844
            result.test_eq(
1✔
845
               "cert fingerprint",
846
               reinterpret_cast<const char*>(fingerprint.data()),
1✔
847
               "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");
848

849
            size_t key_id_len = 0;
1✔
850
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
851
                        botan_x509_cert_get_authority_key_id,
852
                        (cert, nullptr, &key_id_len));
853

854
            result.test_eq("No AKID", key_id_len, 0);
1✔
855

856
            key_id_len = 0;
1✔
857
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
858
                        botan_x509_cert_get_subject_key_id,
859
                        (cert, nullptr, &key_id_len));
860

861
            std::vector<uint8_t> key_id(key_id_len);
1✔
862
            TEST_FFI_OK(botan_x509_cert_get_subject_key_id, (cert, key_id.data(), &key_id_len));
1✔
863
            result.test_eq("cert subject key id",
3✔
864
                           Botan::hex_encode(key_id.data(), key_id.size(), true),
2✔
865
                           "7C4296AEDE4B483BFA92F89E8CCF6D8BA9723795");
866

867
            size_t pubkey_len = 0;
1✔
868
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
869
                        botan_x509_cert_get_public_key_bits,
870
                        (cert, nullptr, &pubkey_len));
871

872
            std::vector<uint8_t> pubkey(pubkey_len);
1✔
873
            TEST_FFI_OK(botan_x509_cert_get_public_key_bits, (cert, pubkey.data(), &pubkey_len));
1✔
874

875
   #if defined(BOTAN_HAS_ECDSA)
876
            botan_pubkey_t pub;
1✔
877
            if(TEST_FFI_OK(botan_x509_cert_get_public_key, (cert, &pub))) {
1✔
878
               TEST_FFI_RC(0, botan_pubkey_ecc_key_used_explicit_encoding, (pub));
1✔
879
               TEST_FFI_OK(botan_pubkey_destroy, (pub));
2✔
880
            }
881
   #endif
882

883
            size_t dn_len = 0;
1✔
884
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
885
                        botan_x509_cert_get_issuer_dn,
886
                        (cert, "Name", 0, nullptr, &dn_len));
887

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

892
            dn_len = 0;
1✔
893
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
894
                        botan_x509_cert_get_subject_dn,
895
                        (cert, "Name", 0, nullptr, &dn_len));
896

897
            dn.resize(dn_len);
1✔
898
            TEST_FFI_OK(botan_x509_cert_get_subject_dn, (cert, "Name", 0, dn.data(), &dn_len));
1✔
899
            result.test_eq("subject dn", reinterpret_cast<const char*>(dn.data()), "ISRG Root X2");
1✔
900

901
            size_t printable_len = 0;
1✔
902
            TEST_FFI_RC(
1✔
903
               BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_x509_cert_to_string, (cert, nullptr, &printable_len));
904

905
            std::string printable(printable_len - 1, '0');
1✔
906
            TEST_FFI_OK(botan_x509_cert_to_string, (cert, &printable[0], &printable_len));
1✔
907

908
            TEST_FFI_RC(0, botan_x509_cert_allowed_usage, (cert, KEY_CERT_SIGN));
1✔
909
            TEST_FFI_RC(0, botan_x509_cert_allowed_usage, (cert, CRL_SIGN));
1✔
910
            TEST_FFI_RC(1, botan_x509_cert_allowed_usage, (cert, DIGITAL_SIGNATURE));
1✔
911

912
            TEST_FFI_OK(botan_x509_cert_destroy, (cert));
2✔
913
         }
6✔
914
      }
1✔
915
};
916

917
class FFI_PKCS_Hashid_Test final : public FFI_Test {
×
918
   public:
919
      std::string name() const override { return "FFI PKCS hash id"; }
1✔
920

921
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
922
         std::vector<uint8_t> hash_id(64);
1✔
923
         size_t hash_id_len = hash_id.size();
1✔
924

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

928
            hash_id.resize(hash_id_len);
1✔
929
            result.test_eq("Expected SHA_256 PKCS hash id", hash_id, "3031300D060960864801650304020105000420");
1✔
930

931
            hash_id_len = 3;  // too short
1✔
932
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
2✔
933
                        botan_pkcs_hash_id,
934
                        ("SHA-256", hash_id.data(), &hash_id_len));
935
         }
936
      }
1✔
937
};
938

939
class FFI_CBC_Cipher_Test final : public FFI_Test {
×
940
   public:
941
      std::string name() const override { return "FFI CBC cipher"; }
1✔
942

943
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
944
         botan_cipher_t cipher_encrypt, cipher_decrypt;
1✔
945

946
         if(TEST_FFI_INIT(botan_cipher_init, (&cipher_encrypt, "AES-128/CBC/PKCS7", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
1✔
947
            size_t min_keylen = 0;
1✔
948
            size_t max_keylen = 0;
1✔
949
            TEST_FFI_OK(botan_cipher_query_keylen, (cipher_encrypt, &min_keylen, &max_keylen));
1✔
950
            result.test_int_eq(min_keylen, 16, "Min key length");
1✔
951
            result.test_int_eq(max_keylen, 16, "Max key length");
1✔
952

953
            // from https://github.com/geertj/bluepass/blob/master/tests/vectors/aes-cbc-pkcs7.txt
954
            const std::vector<uint8_t> plaintext =
1✔
955
               Botan::hex_decode("0397f4f6820b1f9386f14403be5ac16e50213bd473b4874b9bcbf5f318ee686b1d");
1✔
956
            const std::vector<uint8_t> symkey = Botan::hex_decode("898be9cc5004ed0fa6e117c9a3099d31");
1✔
957
            const std::vector<uint8_t> nonce = Botan::hex_decode("9dea7621945988f96491083849b068df");
1✔
958
            const std::vector<uint8_t> exp_ciphertext = Botan::hex_decode(
1✔
959
               "e232cd6ef50047801ee681ec30f61d53cfd6b0bca02fd03c1b234baa10ea82ac9dab8b960926433a19ce6dea08677e34");
1✔
960

961
            size_t output_written = 0;
1✔
962
            size_t input_consumed = 0;
1✔
963

964
            // Test that after clear or final the object can be reused
965
            for(size_t r = 0; r != 2; ++r) {
3✔
966
               size_t ctext_len;
2✔
967
               TEST_FFI_OK(botan_cipher_output_length, (cipher_encrypt, plaintext.size(), &ctext_len));
2✔
968
               result.test_eq("Expected size of padded message", ctext_len, plaintext.size() + 15);
2✔
969
               std::vector<uint8_t> ciphertext(ctext_len);
2✔
970

971
               size_t update_granularity = 0;
2✔
972
               size_t ideal_granularity = 0;
2✔
973
               size_t taglen = 0;
2✔
974

975
               TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_encrypt, &update_granularity));
2✔
976
               TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_encrypt, &ideal_granularity));
2✔
977
               TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_encrypt, &taglen));
2✔
978

979
               result.test_eq(
2✔
980
                  "ideal granularity is a multiple of update granularity", ideal_granularity % update_granularity, 0);
981
               result.test_eq("not an AEAD, hence no tag", taglen, 0);
2✔
982

983
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2✔
984
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
985
               TEST_FFI_OK(botan_cipher_update,
2✔
986
                           (cipher_encrypt,
987
                            0,
988
                            ciphertext.data(),
989
                            ciphertext.size(),
990
                            &output_written,
991
                            plaintext.data(),
992
                            plaintext.size(),
993
                            &input_consumed));
994
               TEST_FFI_OK(botan_cipher_clear, (cipher_encrypt));
2✔
995

996
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2✔
997
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
998
               TEST_FFI_OK(botan_cipher_update,
2✔
999
                           (cipher_encrypt,
1000
                            BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1001
                            ciphertext.data(),
1002
                            ciphertext.size(),
1003
                            &output_written,
1004
                            plaintext.data(),
1005
                            plaintext.size(),
1006
                            &input_consumed));
1007

1008
               ciphertext.resize(output_written);
2✔
1009
               result.test_eq("AES/CBC ciphertext", ciphertext, exp_ciphertext);
4✔
1010

1011
               if(TEST_FFI_OK(botan_cipher_init, (&cipher_decrypt, "AES-128/CBC", BOTAN_CIPHER_INIT_FLAG_DECRYPT))) {
2✔
1012
                  size_t ptext_len;
2✔
1013
                  TEST_FFI_OK(botan_cipher_output_length, (cipher_decrypt, ciphertext.size(), &ptext_len));
2✔
1014
                  std::vector<uint8_t> decrypted(ptext_len);
2✔
1015

1016
                  TEST_FFI_RC(0, botan_cipher_is_authenticated, (cipher_encrypt));
2✔
1017

1018
                  TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_decrypt, &update_granularity));
2✔
1019
                  TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_decrypt, &ideal_granularity));
2✔
1020
                  TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_decrypt, &taglen));
2✔
1021

1022
                  result.test_eq("ideal granularity is a multiple of update granularity (decrypt)",
2✔
1023
                                 ideal_granularity % update_granularity,
1024
                                 0);
1025
                  result.test_eq("not an AEAD, hence no tag (decrypt)", taglen, 0);
2✔
1026

1027
                  TEST_FFI_OK(botan_cipher_set_key, (cipher_decrypt, symkey.data(), symkey.size()));
2✔
1028
                  TEST_FFI_OK(botan_cipher_start, (cipher_decrypt, nonce.data(), nonce.size()));
2✔
1029
                  TEST_FFI_OK(botan_cipher_update,
2✔
1030
                              (cipher_decrypt,
1031
                               BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1032
                               decrypted.data(),
1033
                               decrypted.size(),
1034
                               &output_written,
1035
                               ciphertext.data(),
1036
                               ciphertext.size(),
1037
                               &input_consumed));
1038

1039
                  decrypted.resize(output_written);
2✔
1040

1041
                  result.test_eq("AES/CBC plaintext", decrypted, plaintext);
4✔
1042

1043
                  TEST_FFI_OK(botan_cipher_destroy, (cipher_decrypt));
4✔
1044
               }
2✔
1045
            }
2✔
1046

1047
            TEST_FFI_OK(botan_cipher_destroy, (cipher_encrypt));
2✔
1048
         }
4✔
1049
      }
1✔
1050
};
1051

1052
class FFI_GCM_Test final : public FFI_Test {
×
1053
   public:
1054
      std::string name() const override { return "FFI GCM"; }
1✔
1055

1056
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1057
         botan_cipher_t cipher_encrypt, cipher_decrypt;
1✔
1058

1059
         if(TEST_FFI_INIT(botan_cipher_init, (&cipher_encrypt, "AES-128/GCM", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
1✔
1060
            char namebuf[18];
1✔
1061
            size_t name_len = 15;
1✔
1062
            TEST_FFI_FAIL("output buffer too short", botan_cipher_name, (cipher_encrypt, namebuf, &name_len));
2✔
1063
            result.test_eq("name len", name_len, 16);
1✔
1064

1065
            name_len = sizeof(namebuf);
1✔
1066
            if(TEST_FFI_OK(botan_cipher_name, (cipher_encrypt, namebuf, &name_len))) {
1✔
1067
               result.test_eq("name len", name_len, 16);
1✔
1068
               result.test_eq("name", std::string(namebuf), "AES-128/GCM(16)");
2✔
1069
            }
1070

1071
            size_t min_keylen = 0;
1✔
1072
            size_t max_keylen = 0;
1✔
1073
            size_t nonce_len = 0;
1✔
1074
            size_t tag_len = 0;
1✔
1075
            size_t update_granularity = 0;
1✔
1076
            size_t ideal_granularity = 0;
1✔
1077

1078
            TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_encrypt, &update_granularity));
1✔
1079
            TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_encrypt, &ideal_granularity));
1✔
1080

1081
            result.test_eq(
1✔
1082
               "ideal granularity is a multiple of update granularity", ideal_granularity % update_granularity, 0);
1083

1084
            TEST_FFI_OK(botan_cipher_query_keylen, (cipher_encrypt, &min_keylen, &max_keylen));
1✔
1085
            result.test_int_eq(min_keylen, 16, "Min key length");
1✔
1086
            result.test_int_eq(max_keylen, 16, "Max key length");
1✔
1087

1088
            TEST_FFI_OK(botan_cipher_get_default_nonce_length, (cipher_encrypt, &nonce_len));
1✔
1089
            result.test_int_eq(nonce_len, 12, "Expected default GCM nonce length");
1✔
1090

1091
            TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_encrypt, &tag_len));
1✔
1092
            result.test_int_eq(tag_len, 16, "Expected GCM tag length");
1✔
1093

1094
            TEST_FFI_RC(1, botan_cipher_is_authenticated, (cipher_encrypt));
1✔
1095

1096
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 12));
1✔
1097
            // GCM accepts any nonce size except zero
1098
            TEST_FFI_RC(0, botan_cipher_valid_nonce_length, (cipher_encrypt, 0));
1✔
1099
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 1));
1✔
1100
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 100009));
1✔
1101

1102
            // NIST test vector
1103
            const std::vector<uint8_t> plaintext = Botan::hex_decode(
1✔
1104
               "D9313225F88406E5A55909C5AFF5269A86A7A9531534F7DA2E4C303D8A318A721C3C0C95956809532FCF0E2449A6B525B16AEDF5AA0DE657BA637B39");
1✔
1105

1106
            const std::vector<uint8_t> symkey = Botan::hex_decode("FEFFE9928665731C6D6A8F9467308308");
1✔
1107
            const std::vector<uint8_t> nonce = Botan::hex_decode("CAFEBABEFACEDBADDECAF888");
1✔
1108
            const std::vector<uint8_t> exp_ciphertext = Botan::hex_decode(
1✔
1109
               "42831EC2217774244B7221B784D0D49CE3AA212F2C02A4E035C17E2329ACA12E21D514B25466931C7D8F6A5AAC84AA051BA30B396A0AAC973D58E0915BC94FBC3221A5DB94FAE95AE7121A47");
1✔
1110
            const std::vector<uint8_t> aad = Botan::hex_decode("FEEDFACEDEADBEEFFEEDFACEDEADBEEFABADDAD2");
1✔
1111

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

1114
            size_t output_written = 0;
1✔
1115
            size_t input_consumed = 0;
1✔
1116

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

1121
               // First use a nonce of the AAD, and ensure reset works
1122
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, aad.data(), aad.size()));
2✔
1123
               TEST_FFI_OK(botan_cipher_reset, (cipher_encrypt));
2✔
1124

1125
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
1126
               TEST_FFI_OK(botan_cipher_update,
2✔
1127
                           (cipher_encrypt,
1128
                            0,
1129
                            ciphertext.data(),
1130
                            ciphertext.size(),
1131
                            &output_written,
1132
                            plaintext.data(),
1133
                            plaintext.size(),
1134
                            &input_consumed));
1135
               TEST_FFI_OK(botan_cipher_clear, (cipher_encrypt));
2✔
1136

1137
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2✔
1138
               TEST_FFI_OK(botan_cipher_set_associated_data, (cipher_encrypt, aad.data(), aad.size()));
2✔
1139
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
1140
               TEST_FFI_OK(botan_cipher_update,
2✔
1141
                           (cipher_encrypt,
1142
                            BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1143
                            ciphertext.data(),
1144
                            ciphertext.size(),
1145
                            &output_written,
1146
                            plaintext.data(),
1147
                            plaintext.size(),
1148
                            &input_consumed));
1149

1150
               ciphertext.resize(output_written);
2✔
1151
               result.test_eq("AES/GCM ciphertext", ciphertext, exp_ciphertext);
4✔
1152

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

1156
                  TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_decrypt, &update_granularity));
2✔
1157
                  TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_decrypt, &ideal_granularity));
2✔
1158

1159
                  result.test_eq("ideal granularity is a multiple of update granularity (decrypt)",
2✔
1160
                                 ideal_granularity % update_granularity,
1161
                                 0);
1162

1163
                  TEST_FFI_OK(botan_cipher_set_key, (cipher_decrypt, symkey.data(), symkey.size()));
2✔
1164
                  TEST_FFI_OK(botan_cipher_set_associated_data, (cipher_decrypt, aad.data(), aad.size()));
2✔
1165
                  TEST_FFI_OK(botan_cipher_start, (cipher_decrypt, nonce.data(), nonce.size()));
2✔
1166
                  TEST_FFI_OK(botan_cipher_update,
2✔
1167
                              (cipher_decrypt,
1168
                               BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1169
                               decrypted.data(),
1170
                               decrypted.size(),
1171
                               &output_written,
1172
                               ciphertext.data(),
1173
                               ciphertext.size(),
1174
                               &input_consumed));
1175

1176
                  result.test_int_eq(input_consumed, ciphertext.size(), "All input consumed");
2✔
1177
                  result.test_int_eq(output_written, decrypted.size(), "Expected output size produced");
2✔
1178
                  result.test_eq("AES/GCM plaintext", decrypted, plaintext);
4✔
1179

1180
                  TEST_FFI_OK(botan_cipher_destroy, (cipher_decrypt));
4✔
1181
               }
2✔
1182
            }
1183

1184
            TEST_FFI_OK(botan_cipher_destroy, (cipher_encrypt));
2✔
1185
         }
6✔
1186
      }
1✔
1187
};
1188

1189
class FFI_ChaCha20Poly1305_Test final : public FFI_Test {
×
1190
   public:
1191
      std::string name() const override { return "FFI ChaCha20Poly1305"; }
1✔
1192

1193
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1194
         botan_cipher_t cipher_encrypt, cipher_decrypt;
1✔
1195

1196
         if(TEST_FFI_INIT(botan_cipher_init, (&cipher_encrypt, "ChaCha20Poly1305", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
1✔
1197
            std::array<char, 17> namebuf;
1✔
1198
            size_t name_len = 15;
1✔
1199
            TEST_FFI_FAIL("output buffer too short", botan_cipher_name, (cipher_encrypt, namebuf.data(), &name_len));
2✔
1200
            result.test_eq("name len", name_len, 17);
1✔
1201

1202
            name_len = namebuf.size();
1✔
1203
            if(TEST_FFI_OK(botan_cipher_name, (cipher_encrypt, namebuf.data(), &name_len))) {
1✔
1204
               result.test_eq("name len", name_len, 17);
1✔
1205
               result.test_eq("name", std::string(namebuf.data()), "ChaCha20Poly1305");
2✔
1206
            }
1207

1208
            size_t min_keylen = 0;
1✔
1209
            size_t max_keylen = 0;
1✔
1210
            size_t nonce_len = 0;
1✔
1211
            size_t tag_len = 0;
1✔
1212
            size_t update_granularity = 0;
1✔
1213
            size_t ideal_granularity = 0;
1✔
1214

1215
            TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_encrypt, &update_granularity));
1✔
1216
            TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_encrypt, &ideal_granularity));
1✔
1217

1218
            result.test_eq(
1✔
1219
               "ideal granularity is a multiple of update granularity", ideal_granularity % update_granularity, 0);
1220

1221
            TEST_FFI_OK(botan_cipher_query_keylen, (cipher_encrypt, &min_keylen, &max_keylen));
1✔
1222
            result.test_int_eq(min_keylen, 32, "Min key length");
1✔
1223
            result.test_int_eq(max_keylen, 32, "Max key length");
1✔
1224

1225
            TEST_FFI_OK(botan_cipher_get_default_nonce_length, (cipher_encrypt, &nonce_len));
1✔
1226
            result.test_int_eq(nonce_len, 12, "Expected default ChaCha20Poly1305 nonce length");
1✔
1227

1228
            TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_encrypt, &tag_len));
1✔
1229
            result.test_int_eq(tag_len, 16, "Expected Chacha20Poly1305 tag length");
1✔
1230

1231
            TEST_FFI_RC(1, botan_cipher_is_authenticated, (cipher_encrypt));
1✔
1232

1233
            // From RFC 7539
1234
            const std::vector<uint8_t> plaintext = Botan::hex_decode(
1✔
1235
               "4C616469657320616E642047656E746C656D656E206F662074686520636C617373206F66202739393A204966204920636F756C64206F6666657220796F75206F6E6C79206F6E652074697020666F7220746865206675747572652C2073756E73637265656E20776F756C642062652069742E");
1✔
1236
            const std::vector<uint8_t> symkey =
1✔
1237
               Botan::hex_decode("808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9F");
1✔
1238
            const std::vector<uint8_t> nonce = Botan::hex_decode("070000004041424344454647");
1✔
1239
            const std::vector<uint8_t> exp_ciphertext = Botan::hex_decode(
1✔
1240
               "D31A8D34648E60DB7B86AFBC53EF7EC2A4ADED51296E08FEA9E2B5A736EE62D63DBEA45E8CA9671282FAFB69DA92728B1A71DE0A9E060B2905D6A5B67ECD3B3692DDBD7F2D778B8C9803AEE328091B58FAB324E4FAD675945585808B4831D7BC3FF4DEF08E4B7A9DE576D26586CEC64B61161AE10B594F09E26A7E902ECBD0600691");
1✔
1241
            const std::vector<uint8_t> aad = Botan::hex_decode("50515253C0C1C2C3C4C5C6C7");
1✔
1242

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

1245
            size_t output_written = 0;
1✔
1246
            size_t input_consumed = 0;
1✔
1247

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

1252
               // First use a nonce of the AAD, and ensure reset works
1253
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, aad.data(), aad.size()));
2✔
1254
               TEST_FFI_OK(botan_cipher_reset, (cipher_encrypt));
2✔
1255

1256
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
1257
               TEST_FFI_OK(botan_cipher_update,
2✔
1258
                           (cipher_encrypt,
1259
                            0,
1260
                            ciphertext.data(),
1261
                            ciphertext.size(),
1262
                            &output_written,
1263
                            plaintext.data(),
1264
                            plaintext.size(),
1265
                            &input_consumed));
1266
               TEST_FFI_OK(botan_cipher_clear, (cipher_encrypt));
2✔
1267

1268
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2✔
1269
               TEST_FFI_OK(botan_cipher_set_associated_data, (cipher_encrypt, aad.data(), aad.size()));
2✔
1270
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
1271
               TEST_FFI_OK(botan_cipher_update,
2✔
1272
                           (cipher_encrypt,
1273
                            BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1274
                            ciphertext.data(),
1275
                            ciphertext.size(),
1276
                            &output_written,
1277
                            plaintext.data(),
1278
                            plaintext.size(),
1279
                            &input_consumed));
1280

1281
               ciphertext.resize(output_written);
2✔
1282
               result.test_eq("AES/GCM ciphertext", ciphertext, exp_ciphertext);
4✔
1283

1284
               if(TEST_FFI_OK(botan_cipher_init,
2✔
1285
                              (&cipher_decrypt, "ChaCha20Poly1305", BOTAN_CIPHER_INIT_FLAG_DECRYPT))) {
1286
                  std::vector<uint8_t> decrypted(plaintext.size());
2✔
1287

1288
                  TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_decrypt, &update_granularity));
2✔
1289
                  TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_decrypt, &ideal_granularity));
2✔
1290

1291
                  result.test_eq("ideal granularity is a multiple of update granularity (decrypt)",
2✔
1292
                                 ideal_granularity % update_granularity,
1293
                                 0);
1294

1295
                  TEST_FFI_OK(botan_cipher_set_key, (cipher_decrypt, symkey.data(), symkey.size()));
2✔
1296
                  TEST_FFI_OK(botan_cipher_set_associated_data, (cipher_decrypt, aad.data(), aad.size()));
2✔
1297
                  TEST_FFI_OK(botan_cipher_start, (cipher_decrypt, nonce.data(), nonce.size()));
2✔
1298
                  TEST_FFI_OK(botan_cipher_update,
2✔
1299
                              (cipher_decrypt,
1300
                               BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1301
                               decrypted.data(),
1302
                               decrypted.size(),
1303
                               &output_written,
1304
                               ciphertext.data(),
1305
                               ciphertext.size(),
1306
                               &input_consumed));
1307

1308
                  result.test_int_eq(input_consumed, ciphertext.size(), "All input consumed");
2✔
1309
                  result.test_int_eq(output_written, decrypted.size(), "Expected output size produced");
2✔
1310
                  result.test_eq("AES/GCM plaintext", decrypted, plaintext);
4✔
1311

1312
                  TEST_FFI_OK(botan_cipher_destroy, (cipher_decrypt));
4✔
1313
               }
2✔
1314
            }
1315

1316
            TEST_FFI_OK(botan_cipher_destroy, (cipher_encrypt));
2✔
1317
         }
6✔
1318
      }
1✔
1319
};
1320

1321
class FFI_EAX_Test final : public FFI_Test {
×
1322
   public:
1323
      std::string name() const override { return "FFI EAX"; }
1✔
1324

1325
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1326
         botan_cipher_t cipher_encrypt, cipher_decrypt;
1✔
1327

1328
         if(TEST_FFI_INIT(botan_cipher_init, (&cipher_encrypt, "AES-128/EAX", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
1✔
1329
            size_t min_keylen = 0;
1✔
1330
            size_t max_keylen = 0;
1✔
1331
            size_t mod_keylen = 0;
1✔
1332
            size_t nonce_len = 0;
1✔
1333
            size_t tag_len = 0;
1✔
1334
            size_t update_granularity = 0;
1✔
1335
            size_t ideal_granularity = 0;
1✔
1336

1337
            TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_encrypt, &update_granularity));
1✔
1338
            TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_encrypt, &ideal_granularity));
1✔
1339

1340
            result.test_eq(
1✔
1341
               "ideal granularity is a multiple of update granularity", ideal_granularity % update_granularity, 0);
1342

1343
            TEST_FFI_OK(botan_cipher_query_keylen, (cipher_encrypt, &min_keylen, &max_keylen));
1✔
1344
            result.test_int_eq(min_keylen, 16, "Min key length");
1✔
1345
            result.test_int_eq(max_keylen, 16, "Max key length");
1✔
1346

1347
            TEST_FFI_OK(botan_cipher_get_keyspec, (cipher_encrypt, &min_keylen, &max_keylen, &mod_keylen));
1✔
1348
            result.test_int_eq(min_keylen, 16, "Min key length");
1✔
1349
            result.test_int_eq(max_keylen, 16, "Max key length");
1✔
1350
            result.test_int_eq(mod_keylen, 1, "Mod key length");
1✔
1351

1352
            TEST_FFI_OK(botan_cipher_get_default_nonce_length, (cipher_encrypt, &nonce_len));
1✔
1353
            result.test_int_eq(nonce_len, 12, "Expected default EAX nonce length");
1✔
1354

1355
            TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_encrypt, &tag_len));
1✔
1356
            result.test_int_eq(tag_len, 16, "Expected EAX tag length");
1✔
1357

1358
            TEST_FFI_RC(1, botan_cipher_is_authenticated, (cipher_encrypt));
1✔
1359

1360
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 12));
1✔
1361
            // EAX accepts any nonce size...
1362
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 0));
1✔
1363

1364
            const std::vector<uint8_t> plaintext =
1✔
1365
               Botan::hex_decode("0000000000000000000000000000000011111111111111111111111111111111");
1✔
1366
            const std::vector<uint8_t> symkey = Botan::hex_decode("000102030405060708090a0b0c0d0e0f");
1✔
1367
            const std::vector<uint8_t> nonce = Botan::hex_decode("3c8cc2970a008f75cc5beae2847258c2");
1✔
1368
            const std::vector<uint8_t> exp_ciphertext = Botan::hex_decode(
1✔
1369
               "3c441f32ce07822364d7a2990e50bb13d7b02a26969e4a937e5e9073b0d9c968db90bdb3da3d00afd0fc6a83551da95e");
1✔
1370

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

1373
            size_t output_written = 0;
1✔
1374
            size_t input_consumed = 0;
1✔
1375

1376
            // Test that after clear or final the object can be reused
1377
            for(size_t r = 0; r != 2; ++r) {
3✔
1378
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2✔
1379
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
1380
               TEST_FFI_OK(botan_cipher_update,
2✔
1381
                           (cipher_encrypt,
1382
                            0,
1383
                            ciphertext.data(),
1384
                            ciphertext.size(),
1385
                            &output_written,
1386
                            plaintext.data(),
1387
                            plaintext.size(),
1388
                            &input_consumed));
1389
               TEST_FFI_OK(botan_cipher_clear, (cipher_encrypt));
2✔
1390

1391
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2✔
1392
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
1393
               TEST_FFI_OK(botan_cipher_update,
2✔
1394
                           (cipher_encrypt,
1395
                            BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1396
                            ciphertext.data(),
1397
                            ciphertext.size(),
1398
                            &output_written,
1399
                            plaintext.data(),
1400
                            plaintext.size(),
1401
                            &input_consumed));
1402

1403
               ciphertext.resize(output_written);
2✔
1404
               result.test_eq("AES/EAX ciphertext", ciphertext, exp_ciphertext);
4✔
1405

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

1409
                  TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_decrypt, &update_granularity));
2✔
1410
                  TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_decrypt, &ideal_granularity));
2✔
1411

1412
                  result.test_eq("ideal granularity is a multiple of update granularity (decrypt)",
2✔
1413
                                 ideal_granularity % update_granularity,
1414
                                 0);
1415

1416
                  TEST_FFI_OK(botan_cipher_set_key, (cipher_decrypt, symkey.data(), symkey.size()));
2✔
1417
                  TEST_FFI_OK(botan_cipher_start, (cipher_decrypt, nonce.data(), nonce.size()));
2✔
1418
                  TEST_FFI_OK(botan_cipher_update,
2✔
1419
                              (cipher_decrypt,
1420
                               BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1421
                               decrypted.data(),
1422
                               decrypted.size(),
1423
                               &output_written,
1424
                               ciphertext.data(),
1425
                               ciphertext.size(),
1426
                               &input_consumed));
1427

1428
                  result.test_int_eq(input_consumed, ciphertext.size(), "All input consumed");
2✔
1429
                  result.test_int_eq(output_written, decrypted.size(), "Expected output size produced");
2✔
1430
                  result.test_eq("AES/EAX plaintext", decrypted, plaintext);
4✔
1431

1432
                  TEST_FFI_OK(botan_cipher_destroy, (cipher_decrypt));
4✔
1433
               }
2✔
1434
            }
1435

1436
            TEST_FFI_OK(botan_cipher_destroy, (cipher_encrypt));
2✔
1437
         }
5✔
1438
      }
1✔
1439
};
1440

1441
class FFI_AEAD_Test final : public FFI_Test {
×
1442
   public:
1443
      std::string name() const override { return "FFI AEAD"; }
1✔
1444

1445
      void ffi_test(Test::Result& merged_result, botan_rng_t rng) override {
1✔
1446
         botan_cipher_t cipher_encrypt, cipher_decrypt;
1✔
1447

1448
         std::array<std::string, 5> aeads = {
1✔
1449
            "AES-128/GCM", "ChaCha20Poly1305", "AES-128/EAX", "AES-256/SIV", "AES-128/CCM"};
1✔
1450

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

1454
            if(!TEST_FFI_INIT(botan_cipher_init, (&cipher_encrypt, aead.c_str(), BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
5✔
1455
               continue;
×
1456
            }
1457

1458
            if(!botan_cipher_is_authenticated(cipher_encrypt)) {
5✔
1459
               result.test_failure("Cipher " + aead + " claims is not authenticated");
×
1460
               botan_cipher_destroy(cipher_encrypt);
×
1461
               continue;
×
1462
            }
1463

1464
            size_t min_keylen = 0;
5✔
1465
            size_t max_keylen = 0;
5✔
1466
            size_t update_granularity = 0;
5✔
1467
            size_t ideal_granularity = 0;
5✔
1468
            size_t noncelen = 0;
5✔
1469
            size_t taglen = 0;
5✔
1470
            constexpr size_t pt_multiplier = 5;
5✔
1471
            TEST_FFI_OK(botan_cipher_query_keylen, (cipher_encrypt, &min_keylen, &max_keylen));
5✔
1472
            TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_encrypt, &update_granularity));
5✔
1473
            TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_encrypt, &ideal_granularity));
5✔
1474
            TEST_FFI_OK(botan_cipher_get_default_nonce_length, (cipher_encrypt, &noncelen));
5✔
1475
            TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_encrypt, &taglen));
5✔
1476

1477
            result.test_eq(
5✔
1478
               "ideal granularity is a multiple of update granularity", ideal_granularity % update_granularity, 0);
1479

1480
            std::vector<uint8_t> key(max_keylen);
5✔
1481
            TEST_FFI_OK(botan_rng_get, (rng, key.data(), key.size()));
5✔
1482
            TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, key.data(), key.size()));
5✔
1483

1484
            std::vector<uint8_t> nonce(noncelen);
5✔
1485
            TEST_FFI_OK(botan_rng_get, (rng, nonce.data(), nonce.size()));
5✔
1486
            TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
5✔
1487

1488
            std::vector<uint8_t> plaintext(ideal_granularity * pt_multiplier);
5✔
1489
            std::vector<uint8_t> ciphertext(ideal_granularity * pt_multiplier + taglen);
5✔
1490
            TEST_FFI_OK(botan_rng_get, (rng, plaintext.data(), plaintext.size()));
5✔
1491

1492
            std::vector<uint8_t> dummy_buffer(1024);
5✔
1493
            TEST_FFI_OK(botan_rng_get, (rng, dummy_buffer.data(), dummy_buffer.size()));
5✔
1494
            std::vector<uint8_t> dummy_buffer_reference = dummy_buffer;
5✔
1495

1496
            const bool requires_entire_message = botan_cipher_requires_entire_message(cipher_encrypt);
5✔
1497
            result.test_eq(
5✔
1498
               "requires entire message", requires_entire_message, (aead == "AES-256/SIV" || aead == "AES-128/CCM"));
5✔
1499

1500
            std::span<const uint8_t> pt_slicer(plaintext);
5✔
1501
            std::span<uint8_t> ct_stuffer(ciphertext);
5✔
1502

1503
            // Process data that is explicitly a multiple of the ideal
1504
            // granularity and therefore should be aligned with the cipher's
1505
            // internal block size.
1506
            for(size_t i = 0; i < pt_multiplier; ++i) {
30✔
1507
               size_t output_written = 0;
25✔
1508
               size_t input_consumed = 0;
25✔
1509

1510
               auto pt_chunk = pt_slicer.first(ideal_granularity);
25✔
1511

1512
               // The existing implementation won't consume any bytes from the
1513
               // input if there is no space in the output buffer. Even when
1514
               // the cipher is a mode that won't produce any output until the
1515
               // entire message is processed. Hence, give it some dummy buffer.
1516
               BOTAN_ASSERT_NOMSG(dummy_buffer.size() > ideal_granularity);
25✔
1517
               auto ct_chunk = (requires_entire_message) ? std::span(dummy_buffer).first(ideal_granularity)
25✔
1518
                                                         : ct_stuffer.first(ideal_granularity);
1519

1520
               TEST_FFI_OK(botan_cipher_update,
25✔
1521
                           (cipher_encrypt,
1522
                            0 /* don't finalize */,
1523
                            ct_chunk.data(),
1524
                            ct_chunk.size(),
1525
                            &output_written,
1526
                            pt_chunk.data(),
1527
                            pt_chunk.size(),
1528
                            &input_consumed));
1529

1530
               result.test_gt("some input consumed", input_consumed, 0);
25✔
1531
               result.test_lte("at most, all input consumed", input_consumed, pt_chunk.size());
25✔
1532
               pt_slicer = pt_slicer.subspan(input_consumed);
25✔
1533

1534
               if(requires_entire_message) {
25✔
1535
                  result.test_eq("no output produced", output_written, 0);
20✔
1536
               } else {
1537
                  result.test_eq("all bytes produced", output_written, input_consumed);
15✔
1538
                  ct_stuffer = ct_stuffer.subspan(output_written);
15✔
1539
               }
1540
            }
1541

1542
            // Trying to pull a part of the authentication tag should fail,
1543
            // as we must consume the entire tag in a single invocation to
1544
            // botan_cipher_update().
1545
            size_t final_output_written = 42;
5✔
1546
            size_t final_input_consumed = 1337;
5✔
1547
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
5✔
1548
                        botan_cipher_update,
1549
                        (cipher_encrypt,
1550
                         BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1551
                         dummy_buffer.data(),
1552
                         3, /* not enough to hold any reasonable auth'n tag */
1553
                         &final_output_written,
1554
                         pt_slicer.data(),  // remaining bytes (typically 0)
1555
                         pt_slicer.size(),
1556
                         &final_input_consumed));
1557

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

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

1563
            auto final_ct_chunk = ct_stuffer.first(expected_final_size);
5✔
1564

1565
            TEST_FFI_OK(botan_cipher_update,
5✔
1566
                        (cipher_encrypt,
1567
                         BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1568
                         final_ct_chunk.data(),
1569
                         final_ct_chunk.size(),
1570
                         &final_output_written,
1571
                         nullptr,  // no more input
1572
                         0,
1573
                         &final_input_consumed));
1574

1575
            result.test_eq("no bytes consumed in final", final_input_consumed, 0);
5✔
1576
            result.test_eq("final bytes written", final_output_written, expected_final_size);
5✔
1577
            result.test_eq("dummy buffer unchanged", dummy_buffer, dummy_buffer_reference);
10✔
1578

1579
            TEST_FFI_OK(botan_cipher_destroy, (cipher_encrypt));
5✔
1580

1581
            // ----------------------------------------------------------------
1582

1583
            TEST_FFI_INIT(botan_cipher_init, (&cipher_decrypt, aead.c_str(), BOTAN_CIPHER_INIT_FLAG_DECRYPT));
5✔
1584

1585
            TEST_FFI_OK(botan_cipher_get_update_granularity, (cipher_decrypt, &update_granularity));
5✔
1586
            TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (cipher_decrypt, &ideal_granularity));
5✔
1587

1588
            result.test_eq("ideal granularity is a multiple of update granularity (decrypt)",
5✔
1589
                           ideal_granularity % update_granularity,
1590
                           0);
1591

1592
            TEST_FFI_OK(botan_cipher_set_key, (cipher_decrypt, key.data(), key.size()));
5✔
1593
            TEST_FFI_OK(botan_cipher_start, (cipher_decrypt, nonce.data(), nonce.size()));
5✔
1594

1595
            std::vector<uint8_t> decrypted(plaintext.size());
5✔
1596

1597
            std::span<const uint8_t> ct_slicer(ciphertext);
5✔
1598
            std::span<uint8_t> pt_stuffer(decrypted);
5✔
1599

1600
            // Process data that is explicitly a multiple of the ideal
1601
            // granularity and therefore should be aligned with the cipher's
1602
            // internal block size.
1603
            for(size_t i = 0; i < pt_multiplier; ++i) {
30✔
1604
               size_t output_written = 42;
25✔
1605
               size_t input_consumed = 1337;
25✔
1606

1607
               auto ct_chunk = ct_slicer.first(ideal_granularity);
25✔
1608

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

1616
               TEST_FFI_OK(botan_cipher_update,
25✔
1617
                           (cipher_decrypt,
1618
                            0 /* don't finalize */,
1619
                            pt_chunk.data(),
1620
                            pt_chunk.size(),
1621
                            &output_written,
1622
                            ct_chunk.data(),
1623
                            ct_chunk.size(),
1624
                            &input_consumed));
1625

1626
               result.test_gt("some input consumed", input_consumed, 0);
25✔
1627
               result.test_lte("at most, all input consumed", input_consumed, ct_chunk.size());
25✔
1628
               ct_slicer = ct_slicer.subspan(input_consumed);
25✔
1629

1630
               if(requires_entire_message) {
25✔
1631
                  result.test_eq("no output produced", output_written, 0);
20✔
1632
               } else {
1633
                  result.test_eq("all bytes produced", output_written, input_consumed);
15✔
1634
                  pt_stuffer = pt_stuffer.subspan(output_written);
15✔
1635
               }
1636
            }
1637

1638
            const size_t expected_final_size_dec = requires_entire_message ? plaintext.size() : pt_stuffer.size();
5✔
1639
            auto pt_chunk = pt_stuffer.first(expected_final_size_dec);
5✔
1640

1641
            size_t final_output_written_dec = 42;
5✔
1642
            size_t final_input_consumed_dec = 1337;
5✔
1643

1644
            TEST_FFI_OK(botan_cipher_update,
5✔
1645
                        (cipher_decrypt,
1646
                         BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1647
                         pt_chunk.data(),
1648
                         pt_chunk.size(),
1649
                         &final_output_written_dec,
1650
                         ct_slicer.data(),  // remaining bytes (typically 0)
1651
                         ct_slicer.size(),
1652
                         &final_input_consumed_dec));
1653

1654
            result.test_eq("remaining bytes consumed in final (decrypt)", final_input_consumed_dec, ct_slicer.size());
5✔
1655
            result.test_eq("bytes written in final (decrypt)", final_output_written_dec, expected_final_size_dec);
5✔
1656
            result.test_eq("dummy buffer unchanged", dummy_buffer, dummy_buffer_reference);
10✔
1657

1658
            result.test_eq("decrypted plaintext", decrypted, plaintext);
10✔
1659

1660
            TEST_FFI_OK(botan_cipher_destroy, (cipher_decrypt));
5✔
1661

1662
            merged_result.merge(result, true /* ignore names */);
5✔
1663
         }
35✔
1664
      }
1✔
1665
};
1666

1667
class FFI_StreamCipher_Test final : public FFI_Test {
×
1668
   public:
1669
      std::string name() const override { return "FFI stream ciphers"; }
1✔
1670

1671
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1672
         botan_cipher_t ctr;
1✔
1673

1674
         if(TEST_FFI_INIT(botan_cipher_init, (&ctr, "AES-128/CTR-BE", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
1✔
1675
            const std::vector<uint8_t> key = Botan::hex_decode("2B7E151628AED2A6ABF7158809CF4F3C");
1✔
1676
            const std::vector<uint8_t> nonce = Botan::hex_decode("F0F1F2F3F4F5F6F7F8F9FAFBFCFDFF");
1✔
1677
            const std::vector<uint8_t> pt = Botan::hex_decode(
1✔
1678
               "AE2D8A571E03AC9C9EB76FAC45AF8E5130C81C46A35CE411E5FBC1191A0A52EFF69F2445DF4F9B17AD2B417BE66C3710");
1✔
1679
            const std::vector<uint8_t> exp_ct = Botan::hex_decode(
1✔
1680
               "9806F66B7970FDFF8617187BB9FFFDFF5AE4DF3EDBD5D35E5B4F09020DB03EAB1E031DDA2FBE03D1792170A0F3009CEE");
1✔
1681

1682
            std::vector<uint8_t> ct(pt.size());
1✔
1683

1684
            size_t update_granularity = 0;
1✔
1685
            size_t ideal_granularity = 0;
1✔
1686

1687
            TEST_FFI_OK(botan_cipher_get_update_granularity, (ctr, &update_granularity));
1✔
1688
            TEST_FFI_OK(botan_cipher_get_ideal_update_granularity, (ctr, &ideal_granularity));
1✔
1689

1690
            result.test_eq(
1✔
1691
               "ideal granularity is a multiple of update granularity", ideal_granularity % update_granularity, 0);
1692

1693
            TEST_FFI_RC(0, botan_cipher_is_authenticated, (ctr));
1✔
1694

1695
            size_t input_consumed = 0;
1✔
1696
            size_t output_written = 0;
1✔
1697

1698
            TEST_FFI_OK(botan_cipher_set_key, (ctr, key.data(), key.size()));
1✔
1699
            TEST_FFI_OK(botan_cipher_start, (ctr, nonce.data(), nonce.size()));
1✔
1700

1701
            // Test partial updates...
1702
            TEST_FFI_OK(botan_cipher_update,
1✔
1703
                        (ctr, 0, ct.data(), ct.size(), &output_written, pt.data(), 5, &input_consumed));
1704

1705
            result.test_int_eq(output_written, 5, "Expected output written");
1✔
1706
            result.test_int_eq(input_consumed, 5, "Expected input consumed");
1✔
1707

1708
            TEST_FFI_OK(botan_cipher_update,
1✔
1709
                        (ctr, 0, &ct[5], ct.size() - 5, &output_written, &pt[5], pt.size() - 5, &input_consumed));
1710

1711
            result.test_int_eq(output_written, ct.size() - 5, "Expected output written");
1✔
1712
            result.test_int_eq(input_consumed, pt.size() - 5, "Expected input consumed");
1✔
1713
            result.test_eq("AES-128/CTR ciphertext", ct, exp_ct);
2✔
1714

1715
            TEST_FFI_OK(botan_cipher_destroy, (ctr));
2✔
1716
         }
5✔
1717
      }
1✔
1718
};
1719

1720
class FFI_HashFunction_Test final : public FFI_Test {
×
1721
   public:
1722
      std::string name() const override { return "FFI hash"; }
1✔
1723

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

1727
         botan_hash_t hash;
1✔
1728
         TEST_FFI_FAIL("invalid hash name", botan_hash_init, (&hash, "SHA-255", 0));
2✔
1729
         TEST_FFI_FAIL("invalid flags", botan_hash_init, (&hash, "SHA-256", 1));
2✔
1730

1731
         if(TEST_FFI_INIT(botan_hash_init, (&hash, "SHA-256", 0))) {
1✔
1732
            char namebuf[10];
1✔
1733
            size_t name_len = 7;
1✔
1734
            TEST_FFI_FAIL("output buffer too short", botan_hash_name, (hash, namebuf, &name_len));
2✔
1735
            result.test_eq("name len", name_len, 8);
1✔
1736

1737
            name_len = sizeof(namebuf);
1✔
1738
            if(TEST_FFI_OK(botan_hash_name, (hash, namebuf, &name_len))) {
1✔
1739
               result.test_eq("name len", name_len, 8);
1✔
1740
               result.test_eq("name", std::string(namebuf), "SHA-256");
2✔
1741
            }
1742

1743
            size_t block_size;
1✔
1744
            if(TEST_FFI_OK(botan_hash_block_size, (hash, &block_size))) {
1✔
1745
               result.test_eq("hash block size", block_size, 64);
2✔
1746
            }
1747

1748
            size_t output_len;
1✔
1749
            if(TEST_FFI_OK(botan_hash_output_length, (hash, &output_len))) {
1✔
1750
               result.test_eq("hash output length", output_len, 32);
1✔
1751

1752
               std::vector<uint8_t> outbuf(output_len);
1✔
1753

1754
               // Test that after clear or final the object can be reused
1755
               for(size_t r = 0; r != 2; ++r) {
3✔
1756
                  TEST_FFI_OK(botan_hash_update, (hash, reinterpret_cast<const uint8_t*>(input_str), 1));
2✔
1757
                  TEST_FFI_OK(botan_hash_clear, (hash));
2✔
1758

1759
                  TEST_FFI_OK(botan_hash_update,
2✔
1760
                              (hash, reinterpret_cast<const uint8_t*>(input_str), std::strlen(input_str)));
1761
                  TEST_FFI_OK(botan_hash_final, (hash, outbuf.data()));
2✔
1762

1763
                  result.test_eq(
4✔
1764
                     "SHA-256 output", outbuf, "B5D4045C3F466FA91FE2CC6ABE79232A1A57CDF104F7A26E716E0A1E2789DF78");
1765
               }
1766

1767
               // Test botan_hash_copy_state
1768
               const char* msg = "message digest";
1✔
1769
               const char* expected = "F7846F55CF23E14EEBEAB5B4E1550CAD5B509E3348FBC4EFA3A1413D393CB650";
1✔
1770
               TEST_FFI_OK(botan_hash_clear, (hash));
1✔
1771
               TEST_FFI_OK(botan_hash_update, (hash, reinterpret_cast<const uint8_t*>(&msg[0]), 1));
1✔
1772
               botan_hash_t fork;
1✔
1773
               if(TEST_FFI_OK(botan_hash_copy_state, (&fork, hash))) {
1✔
1774
                  TEST_FFI_OK(botan_hash_update,
1✔
1775
                              (fork, reinterpret_cast<const uint8_t*>(&msg[1]), std::strlen(msg) - 2));
1776

1777
                  TEST_FFI_OK(botan_hash_update,
1✔
1778
                              (hash, reinterpret_cast<const uint8_t*>(&msg[1]), std::strlen(msg) - 1));
1779
                  TEST_FFI_OK(botan_hash_final, (hash, outbuf.data()));
1✔
1780
                  result.test_eq("hashing split", outbuf, expected);
1✔
1781

1782
                  TEST_FFI_OK(botan_hash_update,
1✔
1783
                              (fork, reinterpret_cast<const uint8_t*>(&msg[std::strlen(msg) - 1]), 1));
1784
                  TEST_FFI_OK(botan_hash_final, (fork, outbuf.data()));
1✔
1785
                  result.test_eq("hashing split", outbuf, expected);
1✔
1786

1787
                  TEST_FFI_OK(botan_hash_destroy, (fork));
2✔
1788
               }
1789
            }
1✔
1790

1791
            TEST_FFI_OK(botan_hash_destroy, (hash));
2✔
1792
         }
1793
      }
1✔
1794
};
1795

1796
class FFI_MAC_Test final : public FFI_Test {
×
1797
   public:
1798
      std::string name() const override { return "FFI MAC"; }
1✔
1799

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

1803
         // MAC test
1804
         botan_mac_t mac;
1✔
1805
         TEST_FFI_FAIL("bad flag", botan_mac_init, (&mac, "HMAC(SHA-256)", 1));
2✔
1806
         TEST_FFI_FAIL("bad name", botan_mac_init, (&mac, "HMAC(SHA-259)", 0));
2✔
1807

1808
         if(TEST_FFI_INIT(botan_mac_init, (&mac, "HMAC(SHA-256)", 0))) {
1✔
1809
            char namebuf[16];
1✔
1810
            size_t name_len = 13;
1✔
1811
            TEST_FFI_FAIL("output buffer too short", botan_mac_name, (mac, namebuf, &name_len));
2✔
1812
            result.test_eq("name len", name_len, 14);
1✔
1813

1814
            name_len = sizeof(namebuf);
1✔
1815
            if(TEST_FFI_OK(botan_mac_name, (mac, namebuf, &name_len))) {
1✔
1816
               result.test_eq("name len", name_len, 14);
1✔
1817
               result.test_eq("name", std::string(namebuf), "HMAC(SHA-256)");
2✔
1818
            }
1819

1820
            size_t min_keylen = 0, max_keylen = 0, mod_keylen = 0;
1✔
1821
            TEST_FFI_RC(0, botan_mac_get_keyspec, (mac, nullptr, nullptr, nullptr));
1✔
1822
            TEST_FFI_RC(0, botan_mac_get_keyspec, (mac, &min_keylen, nullptr, nullptr));
1✔
1823
            TEST_FFI_RC(0, botan_mac_get_keyspec, (mac, nullptr, &max_keylen, nullptr));
1✔
1824
            TEST_FFI_RC(0, botan_mac_get_keyspec, (mac, nullptr, nullptr, &mod_keylen));
1✔
1825

1826
            result.test_eq("Expected min keylen", min_keylen, 0);
1✔
1827
            result.test_eq("Expected max keylen", max_keylen, 4096);
1✔
1828
            result.test_eq("Expected mod keylen", mod_keylen, 1);
1✔
1829

1830
            size_t output_len;
1✔
1831
            if(TEST_FFI_OK(botan_mac_output_length, (mac, &output_len))) {
1✔
1832
               result.test_eq("MAC output length", output_len, 32);
1✔
1833

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

1837
               // Test that after clear or final the object can be reused
1838
               for(size_t r = 0; r != 2; ++r) {
3✔
1839
                  TEST_FFI_OK(botan_mac_set_key, (mac, mac_key, sizeof(mac_key)));
2✔
1840
                  TEST_FFI_OK(botan_mac_update,
2✔
1841
                              (mac, reinterpret_cast<const uint8_t*>(input_str), std::strlen(input_str)));
1842
                  TEST_FFI_OK(botan_mac_clear, (mac));
2✔
1843

1844
                  TEST_FFI_OK(botan_mac_set_key, (mac, mac_key, sizeof(mac_key)));
2✔
1845
                  TEST_FFI_OK(botan_mac_update,
2✔
1846
                              (mac, reinterpret_cast<const uint8_t*>(input_str), std::strlen(input_str)));
1847
                  TEST_FFI_OK(botan_mac_final, (mac, outbuf.data()));
2✔
1848

1849
                  result.test_eq(
4✔
1850
                     "HMAC output", outbuf, "1A82EEA984BC4A7285617CC0D05F1FE1D6C96675924A81BC965EE8FF7B0697A7");
1851
               }
1852
            }
1✔
1853

1854
            TEST_FFI_OK(botan_mac_destroy, (mac));
2✔
1855
         }
1856
      }
1✔
1857
};
1858

1859
class FFI_Scrypt_Test final : public FFI_Test {
×
1860
   public:
1861
      std::string name() const override { return "FFI Scrypt"; }
1✔
1862

1863
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1864
         std::vector<uint8_t> output(24);
1✔
1865
         const uint8_t salt[8] = {0};
1✔
1866
         const char* pass = "password";
1✔
1867

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

1871
            size_t N, r, p;
1✔
1872
            TEST_FFI_OK(botan_pwdhash_timed,
1✔
1873
                        ("Scrypt", 50, &r, &p, &N, output.data(), output.size(), "bunny", 5, salt, sizeof(salt)));
1874

1875
            std::vector<uint8_t> cmp(output.size());
1✔
1876

1877
            TEST_FFI_OK(botan_pwdhash, ("Scrypt", N, r, p, cmp.data(), cmp.size(), "bunny", 5, salt, sizeof(salt)));
1✔
1878
            result.test_eq("recomputed scrypt", cmp, output);
2✔
1879
         }
1✔
1880
      }
1✔
1881
};
1882

1883
class FFI_KDF_Test final : public FFI_Test {
×
1884
   public:
1885
      std::string name() const override { return "FFI KDF"; }
1✔
1886

1887
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
1888
         std::vector<uint8_t> outbuf;
1✔
1889

1890
         const std::string passphrase = "ltexmfeyylmlbrsyikaw";
1✔
1891

1892
         const std::vector<uint8_t> pbkdf_salt = Botan::hex_decode("ED1F39A0A7F3889AAF7E60743B3BC1CC2C738E60");
1✔
1893
         const size_t pbkdf_out_len = 10;
1✔
1894
         const size_t pbkdf_iterations = 1000;
1✔
1895

1896
         outbuf.resize(pbkdf_out_len);
1✔
1897

1898
         if(TEST_FFI_INIT(botan_pbkdf,
1✔
1899
                          ("PBKDF2(SHA-1)",
1900
                           outbuf.data(),
1901
                           outbuf.size(),
1902
                           passphrase.c_str(),
1903
                           pbkdf_salt.data(),
1904
                           pbkdf_salt.size(),
1905
                           pbkdf_iterations))) {
1906
            result.test_eq("PBKDF output", outbuf, "027AFADD48F4BE8DCC4F");
1✔
1907

1908
            size_t iters_10ms, iters_100ms;
1✔
1909

1910
            TEST_FFI_OK(botan_pbkdf_timed,
1✔
1911
                        ("PBKDF2(SHA-1)",
1912
                         outbuf.data(),
1913
                         outbuf.size(),
1914
                         passphrase.c_str(),
1915
                         pbkdf_salt.data(),
1916
                         pbkdf_salt.size(),
1917
                         10,
1918
                         &iters_10ms));
1919
            TEST_FFI_OK(botan_pbkdf_timed,
1✔
1920
                        ("PBKDF2(SHA-1)",
1921
                         outbuf.data(),
1922
                         outbuf.size(),
1923
                         passphrase.c_str(),
1924
                         pbkdf_salt.data(),
1925
                         pbkdf_salt.size(),
1926
                         100,
1927
                         &iters_100ms));
1928

1929
            result.test_note("PBKDF timed 10 ms " + std::to_string(iters_10ms) + " iterations " + "100 ms " +
5✔
1930
                             std::to_string(iters_100ms) + " iterations");
4✔
1931
         }
1932

1933
         const std::vector<uint8_t> kdf_secret = Botan::hex_decode("92167440112E");
1✔
1934
         const std::vector<uint8_t> kdf_salt = Botan::hex_decode("45A9BEDED69163123D0348F5185F61ABFB1BF18D6AEA454F");
1✔
1935
         const size_t kdf_out_len = 18;
1✔
1936
         outbuf.resize(kdf_out_len);
1✔
1937

1938
         if(TEST_FFI_INIT(botan_kdf,
1✔
1939
                          ("KDF2(SHA-1)",
1940
                           outbuf.data(),
1941
                           outbuf.size(),
1942
                           kdf_secret.data(),
1943
                           kdf_secret.size(),
1944
                           kdf_salt.data(),
1945
                           kdf_salt.size(),
1946
                           nullptr,
1947
                           0))) {
1948
            result.test_eq("KDF output", outbuf, "3A5DC9AA1C872B4744515AC2702D6396FC2A");
2✔
1949
         }
1950

1951
         size_t out_len = 64;
1✔
1952
         std::string outstr;
1✔
1953
         outstr.resize(out_len);
1✔
1954

1955
         int rc =
1✔
1956
            botan_bcrypt_generate(reinterpret_cast<uint8_t*>(&outstr[0]), &out_len, passphrase.c_str(), rng, 4, 0);
1✔
1957

1958
         if(rc == 0) {
1✔
1959
            result.test_eq("bcrypt output size", out_len, 61);
1✔
1960

1961
            TEST_FFI_OK(botan_bcrypt_is_valid, (passphrase.c_str(), outstr.data()));
1✔
1962
            TEST_FFI_FAIL("bad password", botan_bcrypt_is_valid, ("nope", outstr.data()));
2✔
1963
         }
1964
      }
5✔
1965
};
1966

1967
class FFI_Blockcipher_Test final : public FFI_Test {
×
1968
   public:
1969
      std::string name() const override { return "FFI block ciphers"; }
1✔
1970

1971
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1972
         botan_block_cipher_t cipher;
1✔
1973

1974
         if(TEST_FFI_INIT(botan_block_cipher_init, (&cipher, "AES-128"))) {
1✔
1975
            char namebuf[10];
1✔
1976
            size_t name_len = 7;
1✔
1977
            TEST_FFI_FAIL("output buffer too short", botan_block_cipher_name, (cipher, namebuf, &name_len));
2✔
1978
            result.test_eq("name len", name_len, 8);
1✔
1979

1980
            name_len = sizeof(namebuf);
1✔
1981
            if(TEST_FFI_OK(botan_block_cipher_name, (cipher, namebuf, &name_len))) {
1✔
1982
               result.test_eq("name len", name_len, 8);
1✔
1983
               result.test_eq("name", std::string(namebuf), "AES-128");
2✔
1984
            }
1985

1986
            const std::vector<uint8_t> zero16(16, 0);
1✔
1987
            std::vector<uint8_t> block(16, 0);
1✔
1988

1989
            TEST_FFI_OK(botan_block_cipher_clear, (cipher));
1✔
1990

1991
            TEST_FFI_RC(
1✔
1992
               BOTAN_FFI_ERROR_KEY_NOT_SET, botan_block_cipher_encrypt_blocks, (cipher, block.data(), block.data(), 1));
1993
            TEST_FFI_RC(
1✔
1994
               BOTAN_FFI_ERROR_KEY_NOT_SET, botan_block_cipher_decrypt_blocks, (cipher, block.data(), block.data(), 1));
1995

1996
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_block_cipher_encrypt_blocks, (cipher, nullptr, nullptr, 0));
1✔
1997
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_block_cipher_decrypt_blocks, (cipher, nullptr, nullptr, 0));
1✔
1998

1999
            TEST_FFI_RC(16, botan_block_cipher_block_size, (cipher));
1✔
2000

2001
            size_t min_keylen = 0, max_keylen = 0, mod_keylen = 0;
1✔
2002
            TEST_FFI_RC(0, botan_block_cipher_get_keyspec, (cipher, nullptr, nullptr, nullptr));
1✔
2003
            TEST_FFI_RC(0, botan_block_cipher_get_keyspec, (cipher, &min_keylen, nullptr, nullptr));
1✔
2004
            TEST_FFI_RC(0, botan_block_cipher_get_keyspec, (cipher, nullptr, &max_keylen, nullptr));
1✔
2005
            TEST_FFI_RC(0, botan_block_cipher_get_keyspec, (cipher, nullptr, nullptr, &mod_keylen));
1✔
2006

2007
            result.test_eq("Expected min keylen", min_keylen, 16);
1✔
2008
            result.test_eq("Expected max keylen", max_keylen, 16);
1✔
2009
            result.test_eq("Expected mod keylen", mod_keylen, 1);
1✔
2010

2011
            TEST_FFI_OK(botan_block_cipher_set_key, (cipher, zero16.data(), zero16.size()));
1✔
2012

2013
            TEST_FFI_OK(botan_block_cipher_encrypt_blocks, (cipher, block.data(), block.data(), 1));
1✔
2014
            result.test_eq("AES-128 encryption works", block, "66E94BD4EF8A2C3B884CFA59CA342B2E");
1✔
2015

2016
            TEST_FFI_OK(botan_block_cipher_encrypt_blocks, (cipher, block.data(), block.data(), 1));
1✔
2017
            result.test_eq("AES-128 encryption works", block, "F795BD4A52E29ED713D313FA20E98DBC");
1✔
2018

2019
            TEST_FFI_OK(botan_block_cipher_decrypt_blocks, (cipher, block.data(), block.data(), 1));
1✔
2020
            result.test_eq("AES-128 decryption works", block, "66E94BD4EF8A2C3B884CFA59CA342B2E");
1✔
2021

2022
            TEST_FFI_OK(botan_block_cipher_decrypt_blocks, (cipher, block.data(), block.data(), 1));
1✔
2023
            result.test_eq("AES-128 decryption works", block, "00000000000000000000000000000000");
1✔
2024

2025
            TEST_FFI_OK(botan_block_cipher_clear, (cipher));
1✔
2026
            botan_block_cipher_destroy(cipher);
1✔
2027
         }
2✔
2028
      }
1✔
2029
};
2030

2031
class FFI_ErrorHandling_Test final : public FFI_Test {
×
2032
   public:
2033
      std::string name() const override { return "FFI error handling"; }
1✔
2034

2035
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
2036
         // delete of null is ok/ignored
2037
         TEST_FFI_RC(0, botan_hash_destroy, (nullptr));
1✔
2038

2039
   #if !defined(BOTAN_HAS_SANITIZER_UNDEFINED)
2040
         // Confirm that botan_x_destroy checks the argument type
2041
         botan_mp_t mp;
1✔
2042
         botan_mp_init(&mp);
1✔
2043
         TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT, botan_hash_destroy, (reinterpret_cast<botan_hash_t>(mp)));
1✔
2044
         TEST_FFI_RC(0, botan_mp_destroy, (mp));
1✔
2045
   #endif
2046

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

2052
            if(err) {
150✔
2053
               std::string s(err);
150✔
2054

2055
               if(s != "Unknown error") {
150✔
2056
                  result.confirm("No duplicate messages", !errors.contains(s));
42✔
2057
                  errors.insert(s);
21✔
2058
               }
2059
            }
150✔
2060
         }
2061
      }
1✔
2062
};
2063

2064
class FFI_Base64_Test final : public FFI_Test {
×
2065
   public:
2066
      std::string name() const override { return "FFI base64"; }
1✔
2067

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

2072
         size_t out_len = sizeof(out_buf);
1✔
2073
         TEST_FFI_OK(botan_base64_encode, (bin, sizeof(bin), out_buf, &out_len));
1✔
2074

2075
         result.test_eq("encoded string", out_buf, "FoofBunny900");
1✔
2076

2077
         out_len -= 1;
1✔
2078
         TEST_FFI_RC(
1✔
2079
            BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_base64_encode, (bin, sizeof(bin), out_buf, &out_len));
2080

2081
         const char* base64 = "U3VjaCBiYXNlNjQgd293IQ==";
1✔
2082
         uint8_t out_bin[1024] = {0};
1✔
2083

2084
         out_len = 3;
1✔
2085
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
2086
                     botan_base64_decode,
2087
                     (base64, strlen(base64), out_bin, &out_len));
2088

2089
         result.test_eq("output length", out_len, 18);
1✔
2090

2091
         out_len = sizeof(out_bin);
1✔
2092
         TEST_FFI_OK(botan_base64_decode, (base64, strlen(base64), out_bin, &out_len));
1✔
2093

2094
         result.test_eq(
3✔
2095
            "decoded string", std::string(reinterpret_cast<const char*>(out_bin), out_len), "Such base64 wow!");
2✔
2096
      }
1✔
2097
};
2098

2099
class FFI_Hex_Test final : public FFI_Test {
×
2100
   public:
2101
      std::string name() const override { return "FFI hex"; }
1✔
2102

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

2107
         TEST_FFI_OK(botan_hex_encode, (bin, sizeof(bin), hex_buf, 0));
1✔
2108

2109
         result.test_eq("encoded string", hex_buf, "DEADBEEF");
1✔
2110

2111
         const char* hex = "67657420796572206A756D626F20736872696D70";
1✔
2112
         uint8_t out_bin[1024] = {0};
1✔
2113
         size_t out_len = 5;
1✔
2114

2115
         TEST_FFI_RC(
1✔
2116
            BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_hex_decode, (hex, strlen(hex), out_bin, &out_len));
2117

2118
         out_len = sizeof(out_bin);
1✔
2119
         TEST_FFI_OK(botan_hex_decode, (hex, strlen(hex), out_bin, &out_len));
1✔
2120

2121
         result.test_eq(
3✔
2122
            "decoded string", std::string(reinterpret_cast<const char*>(out_bin), out_len), "get yer jumbo shrimp");
2✔
2123
      }
1✔
2124
};
2125

2126
class FFI_MP_Test final : public FFI_Test {
×
2127
   public:
2128
      std::string name() const override { return "FFI MP"; }
1✔
2129

2130
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
2131
         char str_buf[1024] = {0};
1✔
2132
         size_t str_len = 0;
1✔
2133

2134
         botan_mp_t x;
1✔
2135
         botan_mp_init(&x);
1✔
2136
         TEST_FFI_RC(0, botan_mp_is_odd, (x));
1✔
2137
         TEST_FFI_RC(1, botan_mp_is_even, (x));
1✔
2138
         TEST_FFI_RC(0, botan_mp_is_negative, (x));
1✔
2139
         TEST_FFI_RC(1, botan_mp_is_positive, (x));
1✔
2140
         TEST_FFI_RC(1, botan_mp_is_zero, (x));
1✔
2141
         botan_mp_destroy(x);
1✔
2142

2143
         botan_mp_init(&x);
1✔
2144
         size_t bn_bytes = 0;
1✔
2145
         TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes));
1✔
2146
         result.test_eq("Expected size for MP 0", bn_bytes, 0);
1✔
2147

2148
         botan_mp_set_from_int(x, 5);
1✔
2149
         TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes));
1✔
2150
         result.test_eq("Expected size for MP 5", bn_bytes, 1);
1✔
2151

2152
         botan_mp_add_u32(x, x, 75);
1✔
2153
         TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes));
1✔
2154
         result.test_eq("Expected size for MP 80", bn_bytes, 1);
1✔
2155

2156
         str_len = sizeof(str_buf);
1✔
2157
         TEST_FFI_OK(botan_mp_to_str, (x, 10, str_buf, &str_len));
1✔
2158
         result.test_eq("botan_mp_add", std::string(str_buf), "80");
2✔
2159

2160
         botan_mp_sub_u32(x, x, 80);
1✔
2161
         TEST_FFI_RC(1, botan_mp_is_zero, (x));
1✔
2162
         botan_mp_add_u32(x, x, 259);
1✔
2163
         TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes));
1✔
2164
         result.test_eq("Expected size for MP 259", bn_bytes, 2);
1✔
2165

2166
         str_len = sizeof(str_buf);
1✔
2167
         TEST_FFI_OK(botan_mp_to_str, (x, 10, str_buf, &str_len));
1✔
2168
         result.test_eq("botan_mp_add", std::string(str_buf), "259");
2✔
2169

2170
         TEST_FFI_RC(1, botan_mp_is_odd, (x));
1✔
2171
         TEST_FFI_RC(0, botan_mp_is_even, (x));
1✔
2172
         TEST_FFI_RC(0, botan_mp_is_negative, (x));
1✔
2173
         TEST_FFI_RC(1, botan_mp_is_positive, (x));
1✔
2174
         TEST_FFI_RC(0, botan_mp_is_zero, (x));
1✔
2175

2176
         {
1✔
2177
            botan_mp_t zero;
1✔
2178
            botan_mp_init(&zero);
1✔
2179
            int cmp;
1✔
2180
            TEST_FFI_OK(botan_mp_cmp, (&cmp, x, zero));
1✔
2181
            result.confirm("bigint_mp_cmp(+, 0)", cmp == 1);
2✔
2182

2183
            TEST_FFI_OK(botan_mp_cmp, (&cmp, zero, x));
1✔
2184
            result.confirm("bigint_mp_cmp(0, +)", cmp == -1);
2✔
2185

2186
            TEST_FFI_RC(0, botan_mp_is_negative, (x));
1✔
2187
            TEST_FFI_RC(1, botan_mp_is_positive, (x));
1✔
2188
            TEST_FFI_OK(botan_mp_flip_sign, (x));
1✔
2189
            TEST_FFI_RC(1, botan_mp_is_negative, (x));
1✔
2190
            TEST_FFI_RC(0, botan_mp_is_positive, (x));
1✔
2191

2192
            // test no negative zero
2193
            TEST_FFI_RC(0, botan_mp_is_negative, (zero));
1✔
2194
            TEST_FFI_RC(1, botan_mp_is_positive, (zero));
1✔
2195
            TEST_FFI_OK(botan_mp_flip_sign, (zero));
1✔
2196
            TEST_FFI_RC(0, botan_mp_is_negative, (zero));
1✔
2197
            TEST_FFI_RC(1, botan_mp_is_positive, (zero));
1✔
2198

2199
            TEST_FFI_OK(botan_mp_cmp, (&cmp, x, zero));
1✔
2200
            result.confirm("bigint_mp_cmp(-, 0)", cmp == -1);
2✔
2201

2202
            TEST_FFI_OK(botan_mp_cmp, (&cmp, zero, x));
1✔
2203
            result.confirm("bigint_mp_cmp(0, -)", cmp == 1);
2✔
2204

2205
            TEST_FFI_OK(botan_mp_cmp, (&cmp, zero, zero));
1✔
2206
            result.confirm("bigint_mp_cmp(0, 0)", cmp == 0);
2✔
2207

2208
            TEST_FFI_OK(botan_mp_cmp, (&cmp, x, x));
1✔
2209
            result.confirm("bigint_mp_cmp(x, x)", cmp == 0);
2✔
2210

2211
            TEST_FFI_OK(botan_mp_flip_sign, (x));
1✔
2212

2213
            botan_mp_destroy(zero);
1✔
2214
         }
2215

2216
         size_t x_bits = 0;
1✔
2217
         TEST_FFI_OK(botan_mp_num_bits, (x, &x_bits));
1✔
2218
         result.test_eq("botan_mp_num_bits", x_bits, 9);
1✔
2219

2220
         TEST_FFI_OK(botan_mp_to_hex, (x, str_buf));
1✔
2221
         result.test_eq("botan_mp_to_hex", std::string(str_buf), "0x0103");
2✔
2222

2223
         uint32_t x_32;
1✔
2224
         TEST_FFI_OK(botan_mp_to_uint32, (x, &x_32));
1✔
2225
         result.test_eq("botan_mp_to_uint32", size_t(x_32), size_t(0x103));
1✔
2226

2227
         TEST_FFI_RC(1, botan_mp_get_bit, (x, 1));
1✔
2228
         TEST_FFI_RC(0, botan_mp_get_bit, (x, 87));
1✔
2229
         TEST_FFI_OK(botan_mp_set_bit, (x, 87));
1✔
2230
         TEST_FFI_RC(1, botan_mp_get_bit, (x, 87));
1✔
2231
         TEST_FFI_OK(botan_mp_to_hex, (x, str_buf));
1✔
2232
         result.test_eq("botan_mp_set_bit", std::string(str_buf), "0x8000000000000000000103");
2✔
2233

2234
         TEST_FFI_OK(botan_mp_clear_bit, (x, 87));
1✔
2235
         TEST_FFI_OK(botan_mp_to_hex, (x, str_buf));
1✔
2236
         result.test_eq("botan_mp_set_bit", std::string(str_buf), "0x0103");
2✔
2237

2238
         botan_mp_t y;
1✔
2239
         TEST_FFI_OK(botan_mp_init, (&y));
1✔
2240
         TEST_FFI_OK(botan_mp_set_from_int, (y, 0x1234567));
1✔
2241

2242
         botan_mp_t r;
1✔
2243
         botan_mp_init(&r);
1✔
2244

2245
         TEST_FFI_OK(botan_mp_add, (r, x, y));
1✔
2246
         str_len = sizeof(str_buf);
1✔
2247
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2248
         result.test_eq("botan_mp_add", std::string(str_buf), "19089002");
2✔
2249

2250
         TEST_FFI_OK(botan_mp_mul, (r, x, y));
1✔
2251
         str_len = sizeof(str_buf);
1✔
2252
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2253
         result.test_eq("botan_mp_mul", std::string(str_buf), "4943984437");
2✔
2254
         TEST_FFI_RC(0, botan_mp_is_negative, (r));
1✔
2255

2256
         botan_mp_t q;
1✔
2257
         botan_mp_init(&q);
1✔
2258
         TEST_FFI_OK(botan_mp_div, (q, r, y, x));
1✔
2259

2260
         str_len = sizeof(str_buf);
1✔
2261
         TEST_FFI_OK(botan_mp_to_str, (q, 10, str_buf, &str_len));
1✔
2262
         result.test_eq("botan_mp_div_q", std::string(str_buf), "73701");
2✔
2263

2264
         str_len = sizeof(str_buf);
1✔
2265
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2266
         result.test_eq("botan_mp_div_r", std::string(str_buf), "184");
2✔
2267

2268
         TEST_FFI_OK(botan_mp_set_from_str, (y, "4943984437"));
1✔
2269
         TEST_FFI_OK(botan_mp_sub, (r, x, y));
1✔
2270
         str_len = sizeof(str_buf);
1✔
2271
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2272
         result.test_eq("botan_mp_sub", std::string(str_buf), "-4943984178");
2✔
2273
         TEST_FFI_RC(1, botan_mp_is_negative, (r));
1✔
2274

2275
         TEST_FFI_OK(botan_mp_lshift, (r, x, 39));
1✔
2276
         str_len = sizeof(str_buf);
1✔
2277
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2278
         result.test_eq("botan_mp_lshift", std::string(str_buf), "142386755796992");
2✔
2279

2280
         TEST_FFI_OK(botan_mp_rshift, (r, r, 3));
1✔
2281
         str_len = sizeof(str_buf);
1✔
2282
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2283
         result.test_eq("botan_mp_rshift", std::string(str_buf), "17798344474624");
2✔
2284

2285
         TEST_FFI_OK(botan_mp_gcd, (r, x, y));
1✔
2286
         str_len = sizeof(str_buf);
1✔
2287
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2288
         result.test_eq("botan_mp_gcd", std::string(str_buf), "259");
2✔
2289

2290
         botan_mp_t p;
1✔
2291
         botan_mp_init(&p);
1✔
2292
         const uint8_t M127[] = {
1✔
2293
            0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
2294
         TEST_FFI_OK(botan_mp_from_bin, (p, M127, sizeof(M127)));
1✔
2295
         TEST_FFI_RC(1, botan_mp_is_prime, (p, rng, 64));
1✔
2296

2297
         size_t p_bits = 0;
1✔
2298
         TEST_FFI_OK(botan_mp_num_bits, (p, &p_bits));
1✔
2299
         result.test_eq("botan_mp_num_bits", p_bits, 127);
1✔
2300

2301
         TEST_FFI_OK(botan_mp_mod_inverse, (r, x, p));
1✔
2302
         str_len = sizeof(str_buf);
1✔
2303
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2304
         result.test_eq("botan_mp_mod_inverse", std::string(str_buf), "40728777507911553541948312086427855425");
2✔
2305

2306
         TEST_FFI_OK(botan_mp_powmod, (r, x, r, p));
1✔
2307
         str_len = sizeof(str_buf);
1✔
2308
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2309
         result.test_eq("botan_mp_powmod", std::string(str_buf), "40550417419160441638948180641668117560");
2✔
2310

2311
         TEST_FFI_OK(botan_mp_num_bytes, (r, &bn_bytes));
1✔
2312
         result.test_eq("botan_mp_num_bytes", bn_bytes, 16);
1✔
2313

2314
         std::vector<uint8_t> bn_buf;
1✔
2315
         bn_buf.resize(bn_bytes);
1✔
2316
         botan_mp_to_bin(r, bn_buf.data());
1✔
2317
         result.test_eq("botan_mp_to_bin", bn_buf, "1E81B9EFE0BE1902F6D03F9F5E5FB438");
1✔
2318

2319
         TEST_FFI_OK(botan_mp_set_from_mp, (y, r));
1✔
2320
         TEST_FFI_OK(botan_mp_mod_mul, (r, x, y, p));
1✔
2321
         str_len = sizeof(str_buf);
1✔
2322
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2323
         result.test_eq("botan_mp_mod_mul", std::string(str_buf), "123945920473931248854653259523111998693");
2✔
2324

2325
         str_len = 0;
1✔
2326
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
2327

2328
         size_t x_bytes;
1✔
2329
         botan_mp_rand_bits(x, rng, 512);
1✔
2330
         TEST_FFI_OK(botan_mp_num_bytes, (x, &x_bytes));
1✔
2331
         result.test_lte("botan_mp_num_bytes", x_bytes, 512 / 8);
1✔
2332

2333
         TEST_FFI_OK(botan_mp_set_from_radix_str, (x, "909A", 16));
1✔
2334
         TEST_FFI_OK(botan_mp_to_uint32, (x, &x_32));
1✔
2335
         result.test_eq("botan_mp_set_from_radix_str(16)", x_32, static_cast<size_t>(0x909A));
1✔
2336

2337
         TEST_FFI_OK(botan_mp_set_from_radix_str, (x, "9098135", 10));
1✔
2338
         TEST_FFI_OK(botan_mp_to_uint32, (x, &x_32));
1✔
2339
         result.test_eq("botan_mp_set_from_radix_str(10)", x_32, static_cast<size_t>(9098135));
1✔
2340

2341
         botan_mp_destroy(p);
1✔
2342
         botan_mp_destroy(x);
1✔
2343
         botan_mp_destroy(y);
1✔
2344
         botan_mp_destroy(r);
1✔
2345
         botan_mp_destroy(q);
1✔
2346
      }
1✔
2347
};
2348

2349
class FFI_FPE_Test final : public FFI_Test {
×
2350
   public:
2351
      std::string name() const override { return "FFI FPE"; }
1✔
2352

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

2356
         botan_mp_t n;
1✔
2357
         botan_mp_init(&n);
1✔
2358
         botan_mp_set_from_str(n, "1000000000");
1✔
2359

2360
         botan_fpe_t fpe;
1✔
2361
         if(!TEST_FFI_INIT(botan_fpe_fe1_init, (&fpe, n, key, sizeof(key), 5, 0))) {
1✔
2362
            botan_mp_destroy(n);
×
2363
            return;
×
2364
         }
2365

2366
         botan_mp_t x;
1✔
2367
         botan_mp_init(&x);
1✔
2368
         botan_mp_set_from_str(x, "178051120");
1✔
2369

2370
         TEST_FFI_OK(botan_fpe_encrypt, (fpe, x, nullptr, 0));
1✔
2371

2372
         uint32_t xval = 0;
1✔
2373
         TEST_FFI_OK(botan_mp_to_uint32, (x, &xval));
1✔
2374
         result.test_eq("Expected FPE ciphertext", xval, size_t(605648666));
1✔
2375

2376
         TEST_FFI_OK(botan_fpe_encrypt, (fpe, x, nullptr, 0));
1✔
2377
         TEST_FFI_OK(botan_fpe_decrypt, (fpe, x, nullptr, 0));
1✔
2378
         TEST_FFI_OK(botan_fpe_decrypt, (fpe, x, nullptr, 0));
1✔
2379

2380
         TEST_FFI_OK(botan_mp_to_uint32, (x, &xval));
1✔
2381
         result.test_eq("FPE round trip", xval, size_t(178051120));
1✔
2382

2383
         TEST_FFI_OK(botan_fpe_destroy, (fpe));
1✔
2384
         TEST_FFI_OK(botan_mp_destroy, (x));
1✔
2385
         TEST_FFI_OK(botan_mp_destroy, (n));
2✔
2386
      }
2387
};
2388

2389
class FFI_TOTP_Test final : public FFI_Test {
×
2390
   public:
2391
      std::string name() const override { return "FFI TOTP"; }
1✔
2392

2393
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
2394
         const std::vector<uint8_t> key = Botan::hex_decode("3132333435363738393031323334353637383930");
1✔
2395
         const size_t digits = 8;
1✔
2396
         const size_t timestep = 30;
1✔
2397
         botan_totp_t totp;
1✔
2398

2399
         if(!TEST_FFI_INIT(botan_totp_init, (&totp, key.data(), key.size(), "SHA-1", digits, timestep))) {
1✔
2400
            return;
×
2401
         }
2402

2403
         uint32_t code;
1✔
2404
         TEST_FFI_OK(botan_totp_generate, (totp, &code, 59));
1✔
2405
         result.confirm("TOTP code", code == 94287082);
2✔
2406

2407
         TEST_FFI_OK(botan_totp_generate, (totp, &code, 1111111109));
1✔
2408
         result.confirm("TOTP code 2", code == 7081804);
2✔
2409

2410
         TEST_FFI_OK(botan_totp_check, (totp, 94287082, 59 + 60, 60));
1✔
2411
         TEST_FFI_RC(1, botan_totp_check, (totp, 94287082, 59 + 31, 1));
1✔
2412
         TEST_FFI_RC(1, botan_totp_check, (totp, 94287082, 59 + 61, 1));
1✔
2413

2414
         TEST_FFI_OK(botan_totp_destroy, (totp));
2✔
2415
      }
1✔
2416
};
2417

2418
class FFI_HOTP_Test final : public FFI_Test {
×
2419
   public:
2420
      std::string name() const override { return "FFI HOTP"; }
1✔
2421

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

2426
         botan_hotp_t hotp;
1✔
2427
         uint32_t hotp_val;
1✔
2428

2429
         if(!TEST_FFI_INIT(botan_hotp_init, (&hotp, key.data(), key.size(), "SHA-1", digits))) {
1✔
2430
            return;
×
2431
         }
2432

2433
         TEST_FFI_OK(botan_hotp_generate, (hotp, &hotp_val, 0));
1✔
2434
         result.confirm("Valid value for counter 0", hotp_val == 755224);
2✔
2435
         TEST_FFI_OK(botan_hotp_generate, (hotp, &hotp_val, 1));
1✔
2436
         result.confirm("Valid value for counter 0", hotp_val == 287082);
2✔
2437
         TEST_FFI_OK(botan_hotp_generate, (hotp, &hotp_val, 2));
1✔
2438
         result.confirm("Valid value for counter 0", hotp_val == 359152);
2✔
2439
         TEST_FFI_OK(botan_hotp_generate, (hotp, &hotp_val, 0));
1✔
2440
         result.confirm("Valid value for counter 0", hotp_val == 755224);
2✔
2441

2442
         uint64_t next_ctr = 0;
1✔
2443

2444
         TEST_FFI_OK(botan_hotp_check, (hotp, &next_ctr, 755224, 0, 0));
1✔
2445
         result.confirm("HOTP resync", next_ctr == 1);
2✔
2446
         TEST_FFI_OK(botan_hotp_check, (hotp, nullptr, 359152, 2, 0));
1✔
2447
         TEST_FFI_RC(1, botan_hotp_check, (hotp, nullptr, 359152, 1, 0));
1✔
2448
         TEST_FFI_OK(botan_hotp_check, (hotp, &next_ctr, 359152, 0, 2));
1✔
2449
         result.confirm("HOTP resync", next_ctr == 3);
2✔
2450

2451
         TEST_FFI_OK(botan_hotp_destroy, (hotp));
2✔
2452
      }
1✔
2453
};
2454

2455
class FFI_Keywrap_Test final : public FFI_Test {
×
2456
   public:
2457
      std::string name() const override { return "FFI Keywrap"; }
1✔
2458

2459
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
2460
         const uint8_t key[16] = {0};
1✔
2461
         const uint8_t kek[16] = {0xFF, 0};
1✔
2462

2463
         uint8_t wrapped[16 + 8] = {0};
1✔
2464
         size_t wrapped_keylen = sizeof(wrapped);
1✔
2465

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

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

2473
            result.test_eq(
1✔
2474
               nullptr, "Wrapped key", wrapped, wrapped_keylen, expected_wrapped_key, sizeof(expected_wrapped_key));
2475

2476
            uint8_t dec_key[16] = {0};
1✔
2477
            size_t dec_keylen = sizeof(dec_key);
1✔
2478
            TEST_FFI_OK(botan_key_unwrap3394, (wrapped, sizeof(wrapped), kek, sizeof(kek), dec_key, &dec_keylen));
1✔
2479

2480
            result.test_eq(nullptr, "Unwrapped key", dec_key, dec_keylen, key, sizeof(key));
2✔
2481
         }
2482
      }
1✔
2483
};
2484

2485
class FFI_XMSS_Test final : public FFI_Test {
×
2486
   public:
2487
      std::string name() const override { return "FFI XMSS"; }
1✔
2488

2489
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
2490
         botan_privkey_t priv;
1✔
2491
         if(TEST_FFI_INIT(botan_privkey_create, (&priv, "XMSS", "XMSS-SHA2_10_256", rng))) {
1✔
2492
            TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
1✔
2493

2494
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_privkey_stateful_operation, (priv, nullptr));
1✔
2495
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_privkey_remaining_operations, (priv, nullptr));
1✔
2496

2497
            int stateful;
1✔
2498
            TEST_FFI_OK(botan_privkey_stateful_operation, (priv, &stateful));
1✔
2499
            result.confirm("key is stateful", stateful, true);
2✔
2500

2501
            uint64_t remaining;
1✔
2502
            TEST_FFI_OK(botan_privkey_remaining_operations, (priv, &remaining));
1✔
2503
            result.confirm("key has remaining operations", remaining == 1024, true);
2✔
2504

2505
            TEST_FFI_OK(botan_privkey_destroy, (priv));
2✔
2506
         }
2507
      }
1✔
2508
};
2509

2510
class FFI_RSA_Test final : public FFI_Test {
×
2511
   public:
2512
      std::string name() const override { return "FFI RSA"; }
1✔
2513

2514
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
2515
         botan_privkey_t priv;
1✔
2516

2517
         if(TEST_FFI_INIT(botan_privkey_create_rsa, (&priv, rng, 1024))) {
1✔
2518
            TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
1✔
2519

2520
            int stateful;
1✔
2521
            TEST_FFI_OK(botan_privkey_stateful_operation, (priv, &stateful));
1✔
2522
            result.confirm("key is not stateful", stateful, false);
2✔
2523

2524
            uint64_t remaining;
1✔
2525
            TEST_FFI_FAIL("key is not stateful", botan_privkey_remaining_operations, (priv, &remaining));
2✔
2526

2527
            botan_pubkey_t pub;
1✔
2528
            TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
2529
            TEST_FFI_OK(botan_pubkey_check_key, (pub, rng, 0));
1✔
2530

2531
            ffi_test_pubkey_export(result, pub, priv, rng);
1✔
2532

2533
            botan_mp_t p, q, d, n, e;
1✔
2534
            botan_mp_init(&p);
1✔
2535
            botan_mp_init(&q);
1✔
2536
            botan_mp_init(&d);
1✔
2537
            botan_mp_init(&n);
1✔
2538
            botan_mp_init(&e);
1✔
2539

2540
            TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_privkey_get_field, (p, priv, "quux"));
1✔
2541
            TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_pubkey_get_field, (p, pub, "quux"));
1✔
2542

2543
            TEST_FFI_OK(botan_privkey_rsa_get_p, (p, priv));
1✔
2544
            TEST_FFI_OK(botan_privkey_rsa_get_q, (q, priv));
1✔
2545
            TEST_FFI_OK(botan_privkey_rsa_get_d, (d, priv));
1✔
2546
            TEST_FFI_OK(botan_privkey_rsa_get_e, (e, priv));
1✔
2547
            TEST_FFI_OK(botan_privkey_rsa_get_n, (n, priv));
1✔
2548

2549
            // Confirm same (e,n) values in public key
2550
            {
1✔
2551
               botan_mp_t pub_e, pub_n;
1✔
2552
               botan_mp_init(&pub_e);
1✔
2553
               botan_mp_init(&pub_n);
1✔
2554
               TEST_FFI_OK(botan_pubkey_rsa_get_e, (pub_e, pub));
1✔
2555
               TEST_FFI_OK(botan_pubkey_rsa_get_n, (pub_n, pub));
1✔
2556

2557
               TEST_FFI_RC(1, botan_mp_equal, (pub_e, e));
1✔
2558
               TEST_FFI_RC(1, botan_mp_equal, (pub_n, n));
1✔
2559
               botan_mp_destroy(pub_e);
1✔
2560
               botan_mp_destroy(pub_n);
1✔
2561
            }
2562

2563
            TEST_FFI_RC(1, botan_mp_is_prime, (p, rng, 64));
1✔
2564
            TEST_FFI_RC(1, botan_mp_is_prime, (q, rng, 64));
1✔
2565

2566
            // Test p != q
2567
            TEST_FFI_RC(0, botan_mp_equal, (p, q));
1✔
2568

2569
            // Test p * q == n
2570
            botan_mp_t x;
1✔
2571
            botan_mp_init(&x);
1✔
2572
            TEST_FFI_OK(botan_mp_mul, (x, p, q));
1✔
2573

2574
            TEST_FFI_RC(1, botan_mp_equal, (x, n));
1✔
2575
            botan_mp_destroy(x);
1✔
2576

2577
            botan_privkey_t loaded_privkey;
1✔
2578
            // First try loading a bogus key and verify check_key fails
2579
            TEST_FFI_OK(botan_privkey_load_rsa, (&loaded_privkey, n, d, q));
1✔
2580
            TEST_FFI_RC(-1, botan_privkey_check_key, (loaded_privkey, rng, 0));
1✔
2581
            botan_privkey_destroy(loaded_privkey);
1✔
2582

2583
            TEST_FFI_OK(botan_privkey_load_rsa, (&loaded_privkey, p, q, e));
1✔
2584
            TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
1✔
2585

2586
            botan_pubkey_t loaded_pubkey;
1✔
2587
            TEST_FFI_OK(botan_pubkey_load_rsa, (&loaded_pubkey, n, e));
1✔
2588
            TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey, rng, 0));
1✔
2589

2590
            botan_mp_destroy(p);
1✔
2591
            botan_mp_destroy(q);
1✔
2592
            botan_mp_destroy(d);
1✔
2593
            botan_mp_destroy(e);
1✔
2594
            botan_mp_destroy(n);
1✔
2595

2596
            size_t pkcs1_len = 0;
1✔
2597
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
2598
                        botan_privkey_rsa_get_privkey,
2599
                        (loaded_privkey, nullptr, &pkcs1_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
2600

2601
            std::vector<uint8_t> pkcs1(pkcs1_len);
1✔
2602
            TEST_FFI_OK(botan_privkey_rsa_get_privkey,
1✔
2603
                        (loaded_privkey, pkcs1.data(), &pkcs1_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
2604

2605
            botan_privkey_t privkey_from_pkcs1;
1✔
2606
            TEST_FFI_OK(botan_privkey_load_rsa_pkcs1, (&privkey_from_pkcs1, pkcs1.data(), pkcs1_len));
1✔
2607
            TEST_FFI_OK(botan_privkey_destroy, (privkey_from_pkcs1));
1✔
2608

2609
            pkcs1_len = 0;
1✔
2610
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
2611
                        botan_privkey_rsa_get_privkey,
2612
                        (loaded_privkey, nullptr, &pkcs1_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
2613
            pkcs1.resize(pkcs1_len);
1✔
2614
            TEST_FFI_OK(botan_privkey_rsa_get_privkey,
1✔
2615
                        (loaded_privkey, pkcs1.data(), &pkcs1_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
2616

2617
            char namebuf[32] = {0};
1✔
2618
            size_t name_len = sizeof(namebuf);
1✔
2619
            if(TEST_FFI_OK(botan_pubkey_algo_name, (loaded_pubkey, namebuf, &name_len))) {
1✔
2620
               result.test_eq("algo name", std::string(namebuf), "RSA");
2✔
2621
            }
2622

2623
            name_len = sizeof(namebuf);
1✔
2624
            if(TEST_FFI_OK(botan_privkey_algo_name, (loaded_privkey, namebuf, &name_len))) {
1✔
2625
               result.test_eq("algo name", std::string(namebuf), "RSA");
2✔
2626
            }
2627

2628
            botan_pk_op_encrypt_t encrypt;
1✔
2629
            if(TEST_FFI_INIT(botan_pk_op_encrypt_create, (&encrypt, loaded_pubkey, "OAEP(SHA-256)", 0))) {
1✔
2630
               std::vector<uint8_t> plaintext(32);
1✔
2631
               TEST_FFI_OK(botan_rng_get, (rng, plaintext.data(), plaintext.size()));
1✔
2632

2633
               size_t ctext_len;
1✔
2634
               TEST_FFI_OK(botan_pk_op_encrypt_output_length, (encrypt, plaintext.size(), &ctext_len));
1✔
2635
               std::vector<uint8_t> ciphertext(ctext_len);
1✔
2636

2637
               if(TEST_FFI_OK(botan_pk_op_encrypt,
1✔
2638
                              (encrypt, rng, ciphertext.data(), &ctext_len, plaintext.data(), plaintext.size()))) {
2639
                  ciphertext.resize(ctext_len);
1✔
2640

2641
                  botan_pk_op_decrypt_t decrypt;
1✔
2642
                  if(TEST_FFI_OK(botan_pk_op_decrypt_create, (&decrypt, priv, "OAEP(SHA-256)", 0))) {
1✔
2643
                     size_t decrypted_len;
1✔
2644
                     TEST_FFI_OK(botan_pk_op_decrypt_output_length, (decrypt, ciphertext.size(), &decrypted_len));
1✔
2645
                     std::vector<uint8_t> decrypted(decrypted_len);
1✔
2646
                     TEST_FFI_OK(botan_pk_op_decrypt,
1✔
2647
                                 (decrypt, decrypted.data(), &decrypted_len, ciphertext.data(), ciphertext.size()));
2648
                     decrypted.resize(decrypted_len);
1✔
2649

2650
                     result.test_eq("RSA plaintext", decrypted, plaintext);
2✔
2651
                  }
1✔
2652

2653
                  TEST_FFI_OK(botan_pk_op_decrypt_destroy, (decrypt));
2✔
2654
               }
2655

2656
               TEST_FFI_OK(botan_pk_op_encrypt_destroy, (encrypt));
2✔
2657
            }
2✔
2658

2659
            TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey));
1✔
2660
            TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
2661
            TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
1✔
2662
            TEST_FFI_OK(botan_privkey_destroy, (priv));
2✔
2663
         }
1✔
2664
      }
1✔
2665
};
2666

2667
class FFI_DSA_Test final : public FFI_Test {
×
2668
   public:
2669
      std::string name() const override { return "FFI DSA"; }
1✔
2670

2671
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
2672
         botan_privkey_t priv;
1✔
2673

2674
         if(TEST_FFI_INIT(botan_privkey_create, (&priv, "DSA", "dsa/jce/1024", rng))) {
1✔
2675
            do_dsa_test(priv, rng, result);
1✔
2676
         }
2677

2678
         if(TEST_FFI_INIT(botan_privkey_create_dsa, (&priv, rng, 1024, 160))) {
1✔
2679
            do_dsa_test(priv, rng, result);
1✔
2680
         }
2681
      }
1✔
2682

2683
   private:
2684
      static void do_dsa_test(botan_privkey_t priv, botan_rng_t rng, Test::Result& result) {
2✔
2685
         TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
2✔
2686

2687
         botan_pubkey_t pub;
2✔
2688
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
2✔
2689
         TEST_FFI_OK(botan_pubkey_check_key, (pub, rng, 0));
2✔
2690

2691
         ffi_test_pubkey_export(result, pub, priv, rng);
2✔
2692

2693
         botan_mp_t p, q, g, x, y;
2✔
2694
         botan_mp_init(&p);
2✔
2695
         botan_mp_init(&q);
2✔
2696
         botan_mp_init(&g);
2✔
2697
         botan_mp_init(&x);
2✔
2698
         botan_mp_init(&y);
2✔
2699

2700
         TEST_FFI_OK(botan_privkey_dsa_get_x, (x, priv));
2✔
2701
         TEST_FFI_OK(botan_pubkey_dsa_get_g, (g, pub));
2✔
2702
         TEST_FFI_OK(botan_pubkey_dsa_get_p, (p, pub));
2✔
2703
         TEST_FFI_OK(botan_pubkey_dsa_get_q, (q, pub));
2✔
2704
         TEST_FFI_OK(botan_pubkey_dsa_get_y, (y, pub));
2✔
2705

2706
         botan_mp_t cmp;
2✔
2707
         botan_mp_init(&cmp);
2✔
2708
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_privkey_get_field, (cmp, priv, "quux"));
2✔
2709

2710
         TEST_FFI_OK(botan_privkey_get_field, (cmp, priv, "x"));
2✔
2711
         TEST_FFI_RC(1, botan_mp_equal, (cmp, x));
2✔
2712

2713
         TEST_FFI_OK(botan_privkey_get_field, (cmp, priv, "y"));
2✔
2714
         TEST_FFI_RC(1, botan_mp_equal, (cmp, y));
2✔
2715

2716
         TEST_FFI_OK(botan_privkey_get_field, (cmp, priv, "p"));
2✔
2717
         TEST_FFI_RC(1, botan_mp_equal, (cmp, p));
2✔
2718
         botan_mp_destroy(cmp);
2✔
2719

2720
         botan_privkey_t loaded_privkey;
2✔
2721
         TEST_FFI_OK(botan_privkey_load_dsa, (&loaded_privkey, p, q, g, x));
2✔
2722
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
2✔
2723

2724
         botan_pubkey_t loaded_pubkey;
2✔
2725
         TEST_FFI_OK(botan_pubkey_load_dsa, (&loaded_pubkey, p, q, g, y));
2✔
2726
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey, rng, 0));
2✔
2727

2728
         botan_mp_destroy(p);
2✔
2729
         botan_mp_destroy(q);
2✔
2730
         botan_mp_destroy(g);
2✔
2731
         botan_mp_destroy(y);
2✔
2732
         botan_mp_destroy(x);
2✔
2733

2734
         botan_pk_op_sign_t signer;
2✔
2735

2736
         std::vector<uint8_t> message(6, 6);
2✔
2737
         std::vector<uint8_t> signature;
2✔
2738

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

2743
            size_t sig_len;
2✔
2744
            TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_len));
2✔
2745
            signature.resize(sig_len);
2✔
2746

2747
            size_t output_sig_len = sig_len;
2✔
2748
            TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &output_sig_len));
2✔
2749
            result.test_lte("Output length is upper bound", output_sig_len, sig_len);
2✔
2750
            signature.resize(output_sig_len);
2✔
2751

2752
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
4✔
2753
         }
2754

2755
         botan_pk_op_verify_t verifier = nullptr;
2✔
2756

2757
         if(!signature.empty() && TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, "SHA-256", 0))) {
4✔
2758
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2759
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2✔
2760

2761
            // TODO: randomize this
2762
            signature[0] ^= 1;
2✔
2763
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2764
            TEST_FFI_RC(
2✔
2765
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2766

2767
            message[0] ^= 1;
2✔
2768
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2769
            TEST_FFI_RC(
2✔
2770
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2771

2772
            signature[0] ^= 1;
2✔
2773
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2774
            TEST_FFI_RC(
2✔
2775
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2776

2777
            message[0] ^= 1;
2✔
2778
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2779
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2✔
2780

2781
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
4✔
2782
         }
2783

2784
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey));
2✔
2785
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
2✔
2786
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
2✔
2787
         TEST_FFI_OK(botan_privkey_destroy, (priv));
4✔
2788
      }
4✔
2789
};
2790

2791
class FFI_ECDSA_Test final : public FFI_Test {
×
2792
   public:
2793
      std::string name() const override { return "FFI ECDSA"; }
1✔
2794

2795
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
2796
         static const char* kCurve = "secp384r1";
1✔
2797
         botan_privkey_t priv;
1✔
2798
         botan_pubkey_t pub;
1✔
2799

2800
         if(!TEST_FFI_INIT(botan_privkey_create_ecdsa, (&priv, rng, kCurve))) {
1✔
2801
            return;
×
2802
         }
2803

2804
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
2805
         ffi_test_pubkey_export(result, pub, priv, rng);
1✔
2806

2807
         // Check key load functions
2808
         botan_mp_t private_scalar, public_x, public_y;
1✔
2809
         botan_mp_init(&private_scalar);
1✔
2810
         botan_mp_init(&public_x);
1✔
2811
         botan_mp_init(&public_y);
1✔
2812

2813
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_privkey_get_field, (private_scalar, priv, "quux"));
1✔
2814
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_pubkey_get_field, (private_scalar, pub, "quux"));
1✔
2815

2816
         TEST_FFI_OK(botan_privkey_get_field, (private_scalar, priv, "x"));
1✔
2817
         TEST_FFI_OK(botan_pubkey_get_field, (public_x, pub, "public_x"));
1✔
2818
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub, "public_y"));
1✔
2819

2820
         botan_privkey_t loaded_privkey;
1✔
2821
         botan_pubkey_t loaded_pubkey;
1✔
2822
         TEST_FFI_OK(botan_privkey_load_ecdsa, (&loaded_privkey, private_scalar, kCurve));
1✔
2823
         TEST_FFI_OK(botan_pubkey_load_ecdsa, (&loaded_pubkey, public_x, public_y, kCurve));
1✔
2824
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
1✔
2825
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey, rng, 0));
1✔
2826

2827
         char namebuf[32] = {0};
1✔
2828
         size_t name_len = sizeof(namebuf);
1✔
2829

2830
         TEST_FFI_OK(botan_pubkey_algo_name, (pub, &namebuf[0], &name_len));
1✔
2831
         result.test_eq(namebuf, namebuf, "ECDSA");
1✔
2832

2833
         std::vector<uint8_t> message(1280), signature;
1✔
2834
         TEST_FFI_OK(botan_rng_get, (rng, message.data(), message.size()));
1✔
2835

2836
         for(uint32_t flags = 0; flags <= 1; ++flags) {
3✔
2837
            botan_pk_op_sign_t signer;
2✔
2838
            if(TEST_FFI_INIT(botan_pk_op_sign_create, (&signer, loaded_privkey, "SHA-384", flags))) {
2✔
2839
               // TODO: break input into multiple calls to update
2840
               TEST_FFI_OK(botan_pk_op_sign_update, (signer, message.data(), message.size()));
2✔
2841

2842
               size_t sig_len;
2✔
2843
               TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_len));
2✔
2844

2845
               signature.resize(sig_len);
2✔
2846

2847
               size_t output_sig_len = signature.size();
2✔
2848
               TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &output_sig_len));
2✔
2849
               signature.resize(output_sig_len);
2✔
2850

2851
               TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
4✔
2852
            }
2853

2854
            botan_pk_op_verify_t verifier = nullptr;
2✔
2855

2856
            if(!signature.empty() && TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, "SHA-384", flags))) {
4✔
2857
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2858
               TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2✔
2859

2860
               // TODO: randomize this
2861
               signature[0] ^= 1;
2✔
2862
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2863
               TEST_FFI_RC(BOTAN_FFI_INVALID_VERIFIER,
2✔
2864
                           botan_pk_op_verify_finish,
2865
                           (verifier, signature.data(), signature.size()));
2866

2867
               message[0] ^= 1;
2✔
2868
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2869
               TEST_FFI_RC(BOTAN_FFI_INVALID_VERIFIER,
2✔
2870
                           botan_pk_op_verify_finish,
2871
                           (verifier, signature.data(), signature.size()));
2872

2873
               signature[0] ^= 1;
2✔
2874
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2875
               TEST_FFI_RC(BOTAN_FFI_INVALID_VERIFIER,
2✔
2876
                           botan_pk_op_verify_finish,
2877
                           (verifier, signature.data(), signature.size()));
2878

2879
               message[0] ^= 1;
2✔
2880
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2881
               TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2✔
2882

2883
               TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
4✔
2884
            }
2885
         }
2886

2887
         TEST_FFI_OK(botan_mp_destroy, (private_scalar));
1✔
2888
         TEST_FFI_OK(botan_mp_destroy, (public_x));
1✔
2889
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1✔
2890
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
2891
         TEST_FFI_OK(botan_privkey_destroy, (priv));
1✔
2892
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
1✔
2893
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey));
2✔
2894
      }
2✔
2895
};
2896

2897
class FFI_SM2_Sig_Test final : public FFI_Test {
×
2898
   public:
2899
      std::string name() const override { return "FFI SM2 Sig"; }
1✔
2900

2901
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
2902
         static const char* kCurve = "sm2p256v1";
1✔
2903
         const std::string sm2_ident = "SM2 Ident Field";
1✔
2904
         botan_privkey_t priv;
1✔
2905
         botan_pubkey_t pub;
1✔
2906
         botan_privkey_t loaded_privkey;
1✔
2907
         botan_pubkey_t loaded_pubkey;
1✔
2908

2909
         if(!TEST_FFI_INIT(botan_privkey_create, (&priv, "SM2_Sig", kCurve, rng))) {
1✔
2910
            return;
2911
         }
2912

2913
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
2914
         ffi_test_pubkey_export(result, pub, priv, rng);
1✔
2915

2916
         uint8_t za[32];
1✔
2917
         size_t sizeof_za = sizeof(za);
1✔
2918
         TEST_FFI_OK(botan_pubkey_sm2_compute_za, (za, &sizeof_za, "Ident", "SM3", pub));
1✔
2919

2920
         // Check key load functions
2921
         botan_mp_t private_scalar, public_x, public_y;
1✔
2922
         botan_mp_init(&private_scalar);
1✔
2923
         botan_mp_init(&public_x);
1✔
2924
         botan_mp_init(&public_y);
1✔
2925

2926
         TEST_FFI_OK(botan_privkey_get_field, (private_scalar, priv, "x"));
1✔
2927
         TEST_FFI_OK(botan_pubkey_get_field, (public_x, pub, "public_x"));
1✔
2928
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub, "public_y"));
1✔
2929
         REQUIRE_FFI_OK(botan_privkey_load_sm2, (&loaded_privkey, private_scalar, kCurve));
1✔
2930
         REQUIRE_FFI_OK(botan_pubkey_load_sm2, (&loaded_pubkey, public_x, public_y, kCurve));
1✔
2931
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
1✔
2932
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey, rng, 0));
1✔
2933

2934
         char namebuf[32] = {0};
1✔
2935
         size_t name_len = sizeof(namebuf);
1✔
2936

2937
         TEST_FFI_OK(botan_pubkey_algo_name, (pub, &namebuf[0], &name_len));
1✔
2938
         result.test_eq(namebuf, namebuf, "SM2");
1✔
2939

2940
         std::vector<uint8_t> message(1280), signature;
1✔
2941
         TEST_FFI_OK(botan_rng_get, (rng, message.data(), message.size()));
1✔
2942
         botan_pk_op_sign_t signer;
1✔
2943
         if(TEST_FFI_OK(botan_pk_op_sign_create, (&signer, loaded_privkey, sm2_ident.c_str(), 0))) {
1✔
2944
            // TODO: break input into multiple calls to update
2945
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message.data(), message.size()));
1✔
2946

2947
            size_t sig_len;
1✔
2948
            TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_len));
1✔
2949

2950
            signature.resize(sig_len);
1✔
2951

2952
            TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &sig_len));
1✔
2953
            signature.resize(sig_len);
1✔
2954

2955
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
2✔
2956
         }
2957

2958
         botan_pk_op_verify_t verifier = nullptr;
1✔
2959

2960
         if(!signature.empty() && TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, sm2_ident.c_str(), 0))) {
2✔
2961
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1✔
2962
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
1✔
2963

2964
            // TODO: randomize this
2965
            signature[0] ^= 1;
1✔
2966
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1✔
2967
            TEST_FFI_RC(
1✔
2968
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2969

2970
            message[0] ^= 1;
1✔
2971
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1✔
2972
            TEST_FFI_RC(
1✔
2973
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2974

2975
            signature[0] ^= 1;
1✔
2976
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1✔
2977
            TEST_FFI_RC(
1✔
2978
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2979

2980
            message[0] ^= 1;
1✔
2981
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1✔
2982
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
1✔
2983

2984
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
2✔
2985
         }
2986

2987
         TEST_FFI_OK(botan_mp_destroy, (private_scalar));
1✔
2988
         TEST_FFI_OK(botan_mp_destroy, (public_x));
1✔
2989
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1✔
2990
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
2991
         TEST_FFI_OK(botan_privkey_destroy, (priv));
1✔
2992
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
1✔
2993
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey));
2✔
2994
      }
2✔
2995
};
2996

2997
class FFI_SM2_Enc_Test final : public FFI_Test {
×
2998
   public:
2999
      std::string name() const override { return "FFI SM2 Enc"; }
1✔
3000

3001
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
3002
         static const char* kCurve = "sm2p256v1";
1✔
3003
         botan_privkey_t priv;
1✔
3004
         botan_pubkey_t pub;
1✔
3005
         botan_privkey_t loaded_privkey;
1✔
3006
         botan_pubkey_t loaded_pubkey;
1✔
3007

3008
         if(!TEST_FFI_INIT(botan_privkey_create, (&priv, "SM2_Enc", kCurve, rng))) {
1✔
3009
            return;
×
3010
         }
3011

3012
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
3013
         ffi_test_pubkey_export(result, pub, priv, rng);
1✔
3014

3015
         uint8_t za[32];
1✔
3016
         size_t sizeof_za = sizeof(za);
1✔
3017
         TEST_FFI_OK(botan_pubkey_sm2_compute_za, (za, &sizeof_za, "Ident", "SM3", pub));
1✔
3018

3019
         // Check key load functions
3020
         botan_mp_t private_scalar, public_x, public_y;
1✔
3021
         botan_mp_init(&private_scalar);
1✔
3022
         botan_mp_init(&public_x);
1✔
3023
         botan_mp_init(&public_y);
1✔
3024

3025
         TEST_FFI_OK(botan_privkey_get_field, (private_scalar, priv, "x"));
1✔
3026
         TEST_FFI_OK(botan_pubkey_get_field, (public_x, pub, "public_x"));
1✔
3027
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub, "public_y"));
1✔
3028
         REQUIRE_FFI_OK(botan_privkey_load_sm2_enc, (&loaded_privkey, private_scalar, kCurve));
1✔
3029
         REQUIRE_FFI_OK(botan_pubkey_load_sm2_enc, (&loaded_pubkey, public_x, public_y, kCurve));
1✔
3030
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
1✔
3031
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey, rng, 0));
1✔
3032

3033
         char namebuf[32] = {0};
1✔
3034
         size_t name_len = sizeof(namebuf);
1✔
3035

3036
         TEST_FFI_OK(botan_pubkey_algo_name, (pub, &namebuf[0], &name_len));
1✔
3037
         result.test_eq(namebuf, namebuf, "SM2");
1✔
3038

3039
         std::vector<uint8_t> message(32);
1✔
3040

3041
         std::vector<uint8_t> ciphertext;
1✔
3042
         TEST_FFI_OK(botan_rng_get, (rng, message.data(), message.size()));
1✔
3043

3044
         botan_pk_op_encrypt_t enc;
1✔
3045
         if(TEST_FFI_OK(botan_pk_op_encrypt_create, (&enc, loaded_pubkey, "", 0))) {
1✔
3046
            size_t ctext_len;
1✔
3047
            TEST_FFI_OK(botan_pk_op_encrypt_output_length, (enc, message.size(), &ctext_len));
1✔
3048

3049
            ciphertext.resize(ctext_len);
1✔
3050
            TEST_FFI_OK(botan_pk_op_encrypt, (enc, rng, ciphertext.data(), &ctext_len, message.data(), message.size()));
1✔
3051
            ciphertext.resize(ctext_len);
1✔
3052

3053
            botan_pk_op_decrypt_t dec;
1✔
3054
            TEST_FFI_OK(botan_pk_op_decrypt_create, (&dec, loaded_privkey, "", 0));
1✔
3055

3056
            std::vector<uint8_t> recovered(message.size());
1✔
3057
            size_t recovered_len = recovered.size();
1✔
3058

3059
            TEST_FFI_OK(botan_pk_op_decrypt,
1✔
3060
                        (dec, recovered.data(), &recovered_len, ciphertext.data(), ciphertext.size()));
3061

3062
            botan_pk_op_decrypt_destroy(dec);
1✔
3063
         }
1✔
3064
         botan_pk_op_encrypt_destroy(enc);
1✔
3065

3066
         TEST_FFI_OK(botan_mp_destroy, (private_scalar));
1✔
3067
         TEST_FFI_OK(botan_mp_destroy, (public_x));
1✔
3068
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1✔
3069
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
3070
         TEST_FFI_OK(botan_privkey_destroy, (priv));
1✔
3071
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
1✔
3072
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey));
2✔
3073
      }
2✔
3074
};
3075

3076
class FFI_ECDH_Test final : public FFI_Test {
×
3077
   public:
3078
      std::string name() const override { return "FFI ECDH"; }
1✔
3079

3080
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
3081
         botan_privkey_t priv1;
1✔
3082
         if(!TEST_FFI_INIT(botan_privkey_create_ecdh, (&priv1, rng, "secp256r1"))) {
1✔
3083
            return;
×
3084
         }
3085

3086
         botan_privkey_t priv2;
1✔
3087
         REQUIRE_FFI_OK(botan_privkey_create_ecdh, (&priv2, rng, "secp256r1"));
1✔
3088

3089
         botan_pubkey_t pub1;
1✔
3090
         REQUIRE_FFI_OK(botan_privkey_export_pubkey, (&pub1, priv1));
1✔
3091

3092
         botan_pubkey_t pub2;
1✔
3093
         REQUIRE_FFI_OK(botan_privkey_export_pubkey, (&pub2, priv2));
1✔
3094

3095
         /* Reload key-pair1 in order to test functions for key loading */
3096
         botan_mp_t private_scalar, public_x, public_y;
1✔
3097
         botan_mp_init(&private_scalar);
1✔
3098
         botan_mp_init(&public_x);
1✔
3099
         botan_mp_init(&public_y);
1✔
3100

3101
         TEST_FFI_OK(botan_privkey_get_field, (private_scalar, priv1, "x"));
1✔
3102
         TEST_FFI_OK(botan_pubkey_get_field, (public_x, pub1, "public_x"));
1✔
3103
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub1, "public_y"));
1✔
3104

3105
         botan_privkey_t loaded_privkey1;
1✔
3106
         botan_pubkey_t loaded_pubkey1;
1✔
3107
         REQUIRE_FFI_OK(botan_privkey_load_ecdh, (&loaded_privkey1, private_scalar, "secp256r1"));
1✔
3108
         REQUIRE_FFI_OK(botan_pubkey_load_ecdh, (&loaded_pubkey1, public_x, public_y, "secp256r1"));
1✔
3109
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey1, rng, 0));
1✔
3110
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey1, rng, 0));
1✔
3111

3112
         ffi_test_pubkey_export(result, loaded_pubkey1, priv1, rng);
1✔
3113
         ffi_test_pubkey_export(result, pub2, priv2, rng);
1✔
3114

3115
   #if defined(BOTAN_HAS_KDF2) && defined(BOTAN_HAS_SHA_256)
3116
         constexpr bool has_kdf2_sha256 = true;
1✔
3117
   #else
3118
         constexpr bool has_kdf2_sha256 = false;
3119
   #endif
3120

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

3124
         botan_pk_op_ka_t ka1;
1✔
3125
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka1, loaded_privkey1, kdf, 0));
1✔
3126
         botan_pk_op_ka_t ka2;
1✔
3127
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka2, priv2, kdf, 0));
1✔
3128

3129
         size_t pubkey1_len = 0;
1✔
3130
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
3131
                     botan_pk_op_key_agreement_export_public,
3132
                     (priv1, nullptr, &pubkey1_len));
3133
         std::vector<uint8_t> pubkey1(pubkey1_len);
1✔
3134
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_export_public, (priv1, pubkey1.data(), &pubkey1_len));
1✔
3135
         size_t pubkey2_len = 0;
1✔
3136
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
3137
                     botan_pk_op_key_agreement_export_public,
3138
                     (priv2, nullptr, &pubkey2_len));
3139
         std::vector<uint8_t> pubkey2(pubkey2_len);
1✔
3140
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_export_public, (priv2, pubkey2.data(), &pubkey2_len));
1✔
3141

3142
         std::vector<uint8_t> salt(salt_len);
1✔
3143
         TEST_FFI_OK(botan_rng_get, (rng, salt.data(), salt.size()));
1✔
3144

3145
         const size_t shared_key_len = 32;
1✔
3146

3147
         std::vector<uint8_t> key1(shared_key_len);
1✔
3148
         size_t key1_len = key1.size();
1✔
3149
         TEST_FFI_OK(botan_pk_op_key_agreement,
1✔
3150
                     (ka1, key1.data(), &key1_len, pubkey2.data(), pubkey2.size(), salt.data(), salt.size()));
3151

3152
         std::vector<uint8_t> key2(shared_key_len);
1✔
3153
         size_t key2_len = key2.size();
1✔
3154
         TEST_FFI_OK(botan_pk_op_key_agreement,
1✔
3155
                     (ka2, key2.data(), &key2_len, pubkey1.data(), pubkey1.size(), salt.data(), salt.size()));
3156

3157
         result.test_eq("shared ECDH key", key1, key2);
2✔
3158

3159
         TEST_FFI_OK(botan_mp_destroy, (private_scalar));
1✔
3160
         TEST_FFI_OK(botan_mp_destroy, (public_x));
1✔
3161
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1✔
3162
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka1));
1✔
3163
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka2));
1✔
3164
         TEST_FFI_OK(botan_privkey_destroy, (priv1));
1✔
3165
         TEST_FFI_OK(botan_privkey_destroy, (priv2));
1✔
3166
         TEST_FFI_OK(botan_pubkey_destroy, (pub1));
1✔
3167
         TEST_FFI_OK(botan_pubkey_destroy, (pub2));
1✔
3168
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey1));
1✔
3169
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey1));
2✔
3170
      }
5✔
3171
};
3172

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

3177
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
3178
         botan_privkey_t priv;
1✔
3179
         if(TEST_FFI_INIT(botan_privkey_create_mceliece, (&priv, rng, 2048, 50))) {
1✔
3180
            botan_pubkey_t pub;
1✔
3181
            TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
3182

3183
            ffi_test_pubkey_export(result, pub, priv, rng);
1✔
3184

3185
            char namebuf[32] = {0};
1✔
3186
            size_t name_len = sizeof(namebuf);
1✔
3187
            if(TEST_FFI_OK(botan_pubkey_algo_name, (pub, namebuf, &name_len))) {
1✔
3188
               result.test_eq("algo name", std::string(namebuf), "McEliece");
2✔
3189
            }
3190

3191
            // TODO test KEM
3192

3193
            TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
3194
            TEST_FFI_OK(botan_privkey_destroy, (priv));
2✔
3195
         }
3196
      }
1✔
3197
};
3198

3199
class FFI_Ed25519_Test final : public FFI_Test {
×
3200
   public:
3201
      std::string name() const override { return "FFI Ed25519"; }
1✔
3202

3203
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
3204
         botan_pubkey_t pub;
1✔
3205
         botan_privkey_t priv;
1✔
3206

3207
         // From draft-koch-eddsa-for-openpgp-04
3208
         const std::vector<uint8_t> seed =
1✔
3209
            Botan::hex_decode("1a8b1ff05ded48e18bf50166c664ab023ea70003d78d9e41f5758a91d850f8d2");
1✔
3210
         const std::vector<uint8_t> pubkey =
1✔
3211
            Botan::hex_decode("3f098994bdd916ed4053197934e4a87c80733a1280d62f8010992e43ee3b2406");
1✔
3212
         const std::vector<uint8_t> message = Botan::hex_decode("4f70656e504750040016080006050255f95f9504ff0000000c");
1✔
3213
         const std::vector<uint8_t> exp_sig = Botan::hex_decode(
1✔
3214
            "56f90cca98e2102637bd983fdb16c131dfd27ed82bf4dde5606e0d756aed3366"
3215
            "d09c4fa11527f038e0f57f2201d82f2ea2c9033265fa6ceb489e854bae61b404");
1✔
3216

3217
         if(!TEST_FFI_INIT(botan_privkey_load_ed25519, (&priv, seed.data()))) {
1✔
3218
            return;
×
3219
         }
3220

3221
         uint8_t retr_privkey[64];
1✔
3222
         TEST_FFI_OK(botan_privkey_ed25519_get_privkey, (priv, retr_privkey));
1✔
3223

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

3226
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
3227

3228
         uint8_t retr_pubkey[32];
1✔
3229
         TEST_FFI_OK(botan_pubkey_ed25519_get_pubkey, (pub, retr_pubkey));
1✔
3230
         result.test_eq(nullptr, "Public key matches", retr_pubkey, 32, pubkey.data(), pubkey.size());
1✔
3231

3232
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
3233
         TEST_FFI_OK(botan_pubkey_load_ed25519, (&pub, pubkey.data()));
1✔
3234

3235
         botan_pk_op_sign_t signer;
1✔
3236
         std::vector<uint8_t> signature;
1✔
3237

3238
         if(TEST_FFI_OK(botan_pk_op_sign_create, (&signer, priv, "SHA-256", 0))) {
1✔
3239
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message.data(), message.size()));
1✔
3240

3241
            size_t sig_len;
1✔
3242
            TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_len));
1✔
3243

3244
            signature.resize(sig_len);
1✔
3245

3246
            TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &sig_len));
1✔
3247
            signature.resize(sig_len);
1✔
3248

3249
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
2✔
3250
         }
3251

3252
         result.test_eq("Expected signature", signature, exp_sig);
2✔
3253

3254
         botan_pk_op_verify_t verifier;
1✔
3255

3256
         if(TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, "SHA-256", 0))) {
1✔
3257
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1✔
3258
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
1✔
3259

3260
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
2✔
3261
         }
3262

3263
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
3264
         TEST_FFI_OK(botan_privkey_destroy, (priv));
2✔
3265
      }
5✔
3266
};
3267

3268
class FFI_Ed448_Test final : public FFI_Test {
×
3269
   public:
3270
      std::string name() const override { return "FFI Ed448"; }
1✔
3271

3272
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
3273
         botan_pubkey_t pub;
1✔
3274
         botan_privkey_t priv;
1✔
3275

3276
         // RFC 8032: Testvector Ed448, 1 octet
3277
         const auto sk = Botan::hex_decode(
1✔
3278
            "c4eab05d357007c632f3dbb48489924d552b08fe0c353a0d4a1f00acda2c463afbea67c5e8d2877c5e3bc397a659949ef8021e954e0a12274e");
1✔
3279
         const auto pk_ref = Botan::hex_decode(
1✔
3280
            "43ba28f430cdff456ae531545f7ecd0ac834a55d9358c0372bfa0c6c6798c0866aea01eb00742802b8438ea4cb82169c235160627b4c3a9480");
1✔
3281
         const auto msg = Botan::hex_decode("03");
1✔
3282
         const auto sig_ref = Botan::hex_decode(
1✔
3283
            "26b8f91727bd62897af15e41eb43c377efb9c610d48f2335cb0bd0087810f4352541b143c4b981b7e18f62de8ccdf633fc1bf037ab7cd779805e0dbcc0aae1cbcee1afb2e027df36bc04dcecbf154336c19f0af7e0a6472905e799f1953d2a0ff3348ab21aa4adafd1d234441cf807c03a00");
1✔
3284

3285
         if(!TEST_FFI_INIT(botan_privkey_load_ed448, (&priv, sk.data()))) {
1✔
3286
            return;
×
3287
         }
3288

3289
         std::vector<uint8_t> retr_privkey(57);
1✔
3290
         TEST_FFI_OK(botan_privkey_ed448_get_privkey, (priv, retr_privkey.data()));
1✔
3291
         result.test_is_eq("Private key matches", retr_privkey, sk);
1✔
3292

3293
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
3294

3295
         std::vector<uint8_t> retr_pubkey(57);
1✔
3296
         TEST_FFI_OK(botan_pubkey_ed448_get_pubkey, (pub, retr_pubkey.data()));
1✔
3297
         result.test_is_eq("Public key matches", retr_pubkey, pk_ref);
1✔
3298

3299
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
3300
         TEST_FFI_OK(botan_pubkey_load_ed448, (&pub, pk_ref.data()));
1✔
3301

3302
         botan_pk_op_sign_t signer;
1✔
3303
         std::vector<uint8_t> signature;
1✔
3304

3305
         if(TEST_FFI_OK(botan_pk_op_sign_create, (&signer, priv, "Pure", 0))) {
1✔
3306
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, msg.data(), msg.size()));
1✔
3307

3308
            size_t sig_len;
1✔
3309
            TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_len));
1✔
3310

3311
            signature.resize(sig_len);
1✔
3312

3313
            TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &sig_len));
1✔
3314
            signature.resize(sig_len);
1✔
3315

3316
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
2✔
3317
         }
3318

3319
         result.test_eq("Expected signature", signature, sig_ref);
2✔
3320

3321
         botan_pk_op_verify_t verifier;
1✔
3322

3323
         if(TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, "Pure", 0))) {
1✔
3324
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, msg.data(), msg.size()));
1✔
3325
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
1✔
3326

3327
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
2✔
3328
         }
3329

3330
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
3331
         TEST_FFI_OK(botan_privkey_destroy, (priv));
2✔
3332
      }
7✔
3333
};
3334

3335
class FFI_X25519_Test final : public FFI_Test {
×
3336
   public:
3337
      std::string name() const override { return "FFI X25519"; }
1✔
3338

3339
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
3340
         // From RFC 8037
3341

3342
         const std::vector<uint8_t> a_pub_bits =
1✔
3343
            Botan::hex_decode("de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f");
1✔
3344
         const std::vector<uint8_t> b_priv_bits =
1✔
3345
            Botan::hex_decode("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a");
1✔
3346
         const std::vector<uint8_t> b_pub_bits =
1✔
3347
            Botan::hex_decode("8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a");
1✔
3348
         const std::vector<uint8_t> shared_secret_bits =
1✔
3349
            Botan::hex_decode("4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742");
1✔
3350

3351
         botan_privkey_t b_priv;
1✔
3352
         if(!TEST_FFI_INIT(botan_privkey_load_x25519, (&b_priv, b_priv_bits.data()))) {
1✔
3353
            return;
3354
         }
3355

3356
         std::vector<uint8_t> privkey_read(32);
1✔
3357
         TEST_FFI_OK(botan_privkey_x25519_get_privkey, (b_priv, privkey_read.data()));
1✔
3358
         result.test_eq("X25519 private key", privkey_read, b_priv_bits);
2✔
3359

3360
         std::vector<uint8_t> pubkey_read(32);
1✔
3361

3362
         botan_pubkey_t b_pub;
1✔
3363
         TEST_FFI_OK(botan_privkey_export_pubkey, (&b_pub, b_priv));
1✔
3364
         TEST_FFI_OK(botan_pubkey_x25519_get_pubkey, (b_pub, pubkey_read.data()));
1✔
3365
         result.test_eq("X25519 public key b", pubkey_read, b_pub_bits);
2✔
3366

3367
         botan_pubkey_t a_pub;
1✔
3368
         TEST_FFI_OK(botan_pubkey_load_x25519, (&a_pub, a_pub_bits.data()));
1✔
3369
         TEST_FFI_OK(botan_pubkey_x25519_get_pubkey, (a_pub, pubkey_read.data()));
1✔
3370
         result.test_eq("X25519 public key a", pubkey_read, a_pub_bits);
2✔
3371

3372
         botan_pk_op_ka_t ka;
1✔
3373
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka, b_priv, "Raw", 0));
1✔
3374

3375
         std::vector<uint8_t> shared_output(32);
1✔
3376
         size_t shared_len = shared_output.size();
1✔
3377
         TEST_FFI_OK(botan_pk_op_key_agreement,
1✔
3378
                     (ka, shared_output.data(), &shared_len, a_pub_bits.data(), a_pub_bits.size(), nullptr, 0));
3379

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

3382
         TEST_FFI_OK(botan_pubkey_destroy, (a_pub));
1✔
3383
         TEST_FFI_OK(botan_pubkey_destroy, (b_pub));
1✔
3384
         TEST_FFI_OK(botan_privkey_destroy, (b_priv));
1✔
3385
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka));
2✔
3386
      }
7✔
3387
};
3388

3389
class FFI_X448_Test final : public FFI_Test {
×
3390
   public:
3391
      std::string name() const override { return "FFI X448"; }
1✔
3392

3393
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
3394
         // From RFC 7748 Section 6.2
3395
         const auto a_pub_ref = Botan::hex_decode(
1✔
3396
            "9b08f7cc31b7e3e67d22d5aea121074a273bd2b83de09c63faa73d2c22c5d9bbc836647241d953d40c5b12da88120d53177f80e532c41fa0");
1✔
3397
         const auto b_priv_ref = Botan::hex_decode(
1✔
3398
            "1c306a7ac2a0e2e0990b294470cba339e6453772b075811d8fad0d1d6927c120bb5ee8972b0d3e21374c9c921b09d1b0366f10b65173992d");
1✔
3399
         const auto b_pub_ref = Botan::hex_decode(
1✔
3400
            "3eb7a829b0cd20f5bcfc0b599b6feccf6da4627107bdb0d4f345b43027d8b972fc3e34fb4232a13ca706dcb57aec3dae07bdc1c67bf33609");
1✔
3401
         const auto shared_secret_ref = Botan::hex_decode(
1✔
3402
            "07fff4181ac6cc95ec1c16a94a0f74d12da232ce40a77552281d282bb60c0b56fd2464c335543936521c24403085d59a449a5037514a879d");
1✔
3403

3404
         botan_privkey_t b_priv;
1✔
3405
         if(!TEST_FFI_INIT(botan_privkey_load_x448, (&b_priv, b_priv_ref.data()))) {
1✔
3406
            return;
3407
         }
3408

3409
         std::vector<uint8_t> privkey_read(56);
1✔
3410
         TEST_FFI_OK(botan_privkey_x448_get_privkey, (b_priv, privkey_read.data()));
1✔
3411
         result.test_eq("X448 private key", privkey_read, b_priv_ref);
2✔
3412

3413
         std::vector<uint8_t> pubkey_read(56);
1✔
3414

3415
         botan_pubkey_t b_pub;
1✔
3416
         TEST_FFI_OK(botan_privkey_export_pubkey, (&b_pub, b_priv));
1✔
3417
         TEST_FFI_OK(botan_pubkey_x448_get_pubkey, (b_pub, pubkey_read.data()));
1✔
3418
         result.test_eq("X448 public key b", pubkey_read, b_pub_ref);
2✔
3419

3420
         botan_pubkey_t a_pub;
1✔
3421
         TEST_FFI_OK(botan_pubkey_load_x448, (&a_pub, a_pub_ref.data()));
1✔
3422
         TEST_FFI_OK(botan_pubkey_x448_get_pubkey, (a_pub, pubkey_read.data()));
1✔
3423
         result.test_eq("X448 public key a", pubkey_read, a_pub_ref);
2✔
3424

3425
         botan_pk_op_ka_t ka;
1✔
3426
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka, b_priv, "Raw", 0));
1✔
3427

3428
         std::vector<uint8_t> shared_output(56);
1✔
3429
         size_t shared_len = shared_output.size();
1✔
3430
         TEST_FFI_OK(botan_pk_op_key_agreement,
1✔
3431
                     (ka, shared_output.data(), &shared_len, a_pub_ref.data(), a_pub_ref.size(), nullptr, 0));
3432

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

3435
         TEST_FFI_OK(botan_pubkey_destroy, (a_pub));
1✔
3436
         TEST_FFI_OK(botan_pubkey_destroy, (b_pub));
1✔
3437
         TEST_FFI_OK(botan_privkey_destroy, (b_priv));
1✔
3438
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka));
2✔
3439
      }
7✔
3440
};
3441

3442
/**
3443
 * Helper class for testing "view"-style API functions that take a callback
3444
 * that gets passed a variable-length buffer of bytes.
3445
 *
3446
 * Example:
3447
 *   botan_privkey_t priv;
3448
 *   ViewBytesSink sink;
3449
 *   botan_privkey_view_raw(priv, sink.delegate(), sink.callback());
3450
 *   std::cout << hex_encode(sink.get()) << std::endl;
3451
 */
3452
class ViewBytesSink final {
×
3453
   public:
3454
      void* delegate() { return this; }
3455

3456
      botan_view_bin_fn callback() { return &write_fn; }
200✔
3457

3458
      const std::vector<uint8_t>& get() { return m_buf; }
3459

3460
   private:
3461
      static int write_fn(void* ctx, const uint8_t buf[], size_t len) {
200✔
3462
         if(!ctx || !buf) {
200✔
3463
            return BOTAN_FFI_ERROR_NULL_POINTER;
3464
         }
3465

3466
         auto* sink = static_cast<ViewBytesSink*>(ctx);
200✔
3467
         sink->m_buf.assign(buf, buf + len);
200✔
3468

3469
         return 0;
200✔
3470
      }
3471

3472
   private:
3473
      std::vector<uint8_t> m_buf;
3474
};
3475

3476
/**
3477
 * See ViewBytesSink for how to use this. Works for `botan_view_str_fn` instead.
3478
*/
3479
class ViewStringSink final {
9✔
3480
   public:
3481
      void* delegate() { return this; }
3482

3483
      botan_view_str_fn callback() { return &write_fn; }
3✔
3484

3485
      std::string_view get() { return m_str; }
3✔
3486

3487
   private:
3488
      static int write_fn(void* ctx, const char* str, size_t len) {
3✔
3489
         if(!ctx || !str) {
3✔
3490
            return BOTAN_FFI_ERROR_NULL_POINTER;
3491
         }
3492

3493
         auto* sink = static_cast<ViewStringSink*>(ctx);
3✔
3494
         // discard the null terminator
3495
         sink->m_str = std::string(str, len - 1);
3✔
3496

3497
         return 0;
3✔
3498
      }
3499

3500
   private:
3501
      std::string m_str;
3502
};
3503

3504
/**
3505
 * Base class for roundtrip tests of FFI bindings for Key Encapsulation Mechanisms.
3506
 */
3507
class FFI_KEM_Roundtrip_Test : public FFI_Test {
3✔
3508
   protected:
3509
      using privkey_loader_fn_t = int (*)(botan_privkey_t*, const uint8_t[], size_t, const char*);
3510
      using pubkey_loader_fn_t = int (*)(botan_pubkey_t*, const uint8_t[], size_t, const char*);
3511

3512
   protected:
3513
      virtual const char* algo() const = 0;
3514
      virtual privkey_loader_fn_t private_key_load_function() const = 0;
3515
      virtual pubkey_loader_fn_t public_key_load_function() const = 0;
3516
      virtual std::vector<const char*> modes() const = 0;
3517

3518
   public:
3519
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
3✔
3520
         for(auto mode : modes()) {
34✔
3521
            // generate a key pair
3522
            botan_privkey_t priv;
31✔
3523
            botan_pubkey_t pub;
31✔
3524
            if(!TEST_FFI_INIT(botan_privkey_create, (&priv, algo(), mode, rng))) {
31✔
3525
               continue;
×
3526
            }
3527
            TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
31✔
3528

3529
            // raw-encode the key pair
3530
            ViewBytesSink priv_bytes;
31✔
3531
            ViewBytesSink pub_bytes;
31✔
3532
            TEST_FFI_OK(botan_privkey_view_raw, (priv, priv_bytes.delegate(), priv_bytes.callback()));
31✔
3533
            TEST_FFI_OK(botan_pubkey_view_raw, (pub, pub_bytes.delegate(), pub_bytes.callback()));
31✔
3534

3535
            // decode the key pair from raw encoding
3536
            botan_privkey_t priv_loaded;
31✔
3537
            botan_pubkey_t pub_loaded;
31✔
3538
            TEST_FFI_OK(private_key_load_function(),
31✔
3539
                        (&priv_loaded, priv_bytes.get().data(), priv_bytes.get().size(), mode));
3540
            TEST_FFI_OK(public_key_load_function(),
31✔
3541
                        (&pub_loaded, pub_bytes.get().data(), pub_bytes.get().size(), mode));
3542

3543
            // re-encode and compare to the first round
3544
            ViewBytesSink priv_bytes2;
31✔
3545
            ViewBytesSink pub_bytes2;
31✔
3546
            TEST_FFI_OK(botan_privkey_view_raw, (priv_loaded, priv_bytes2.delegate(), priv_bytes2.callback()));
31✔
3547
            TEST_FFI_OK(botan_pubkey_view_raw, (pub_loaded, pub_bytes2.delegate(), pub_bytes2.callback()));
31✔
3548
            result.test_eq("private key encoding", priv_bytes.get(), priv_bytes2.get());
62✔
3549
            result.test_eq("public key encoding", pub_bytes.get(), pub_bytes2.get());
62✔
3550

3551
            // KEM encryption (using the loaded public key)
3552
            botan_pk_op_kem_encrypt_t kem_enc;
31✔
3553
            TEST_FFI_OK(botan_pk_op_kem_encrypt_create, (&kem_enc, pub_loaded, "Raw"));
31✔
3554

3555
            // explicitly query output lengths
3556
            size_t shared_key_length = 0;
31✔
3557
            size_t ciphertext_length = 0;
31✔
3558
            TEST_FFI_OK(botan_pk_op_kem_encrypt_shared_key_length, (kem_enc, 0, &shared_key_length));
31✔
3559
            TEST_FFI_OK(botan_pk_op_kem_encrypt_encapsulated_key_length, (kem_enc, &ciphertext_length));
31✔
3560

3561
            // check that insufficient buffer space is handled correctly
3562
            size_t shared_key_length_out = 0;
31✔
3563
            size_t ciphertext_length_out = 0;
31✔
3564
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
31✔
3565
                        botan_pk_op_kem_encrypt_create_shared_key,
3566
                        (kem_enc,
3567
                         rng,
3568
                         nullptr /* no salt */,
3569
                         0,
3570
                         0 /* default key length */,
3571
                         nullptr,
3572
                         &shared_key_length_out,
3573
                         nullptr,
3574
                         &ciphertext_length_out));
3575

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

3580
            // allocate buffers (with additional space) and perform the actual encryption
3581
            shared_key_length_out = shared_key_length * 2;
31✔
3582
            ciphertext_length_out = ciphertext_length * 2;
31✔
3583
            Botan::secure_vector<uint8_t> shared_key(shared_key_length_out);
31✔
3584
            std::vector<uint8_t> ciphertext(ciphertext_length_out);
31✔
3585
            TEST_FFI_OK(botan_pk_op_kem_encrypt_create_shared_key,
31✔
3586
                        (kem_enc,
3587
                         rng,
3588
                         nullptr /* no salt */,
3589
                         0,
3590
                         0 /* default key length */,
3591
                         shared_key.data(),
3592
                         &shared_key_length_out,
3593
                         ciphertext.data(),
3594
                         &ciphertext_length_out));
3595
            result.test_eq("shared key length", shared_key_length, shared_key_length_out);
31✔
3596
            result.test_eq("ciphertext length", ciphertext_length, ciphertext_length_out);
31✔
3597
            shared_key.resize(shared_key_length_out);
31✔
3598
            ciphertext.resize(ciphertext_length_out);
31✔
3599
            TEST_FFI_OK(botan_pk_op_kem_encrypt_destroy, (kem_enc));
31✔
3600

3601
            // KEM decryption (using the generated private key)
3602
            botan_pk_op_kem_decrypt_t kem_dec;
31✔
3603
            TEST_FFI_OK(botan_pk_op_kem_decrypt_create, (&kem_dec, priv, "Raw"));
31✔
3604
            size_t shared_key_length2 = 0;
31✔
3605
            TEST_FFI_OK(botan_pk_op_kem_decrypt_shared_key_length, (kem_dec, shared_key_length, &shared_key_length2));
31✔
3606
            result.test_eq("shared key lengths are consistent", shared_key_length, shared_key_length2);
31✔
3607

3608
            // check that insufficient buffer space is handled correctly
3609
            shared_key_length_out = 0;
31✔
3610
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
31✔
3611
                        botan_pk_op_kem_decrypt_shared_key,
3612
                        (kem_dec,
3613
                         nullptr /* no salt */,
3614
                         0,
3615
                         ciphertext.data(),
3616
                         ciphertext.size(),
3617
                         0 /* default length */,
3618
                         nullptr,
3619
                         &shared_key_length_out));
3620
            result.test_eq("reported buffer length requirement", shared_key_length, shared_key_length_out);
31✔
3621

3622
            // allocate buffer (double the size) and perform the actual decryption
3623
            shared_key_length_out = shared_key_length * 2;
31✔
3624
            Botan::secure_vector<uint8_t> shared_key2(shared_key_length_out);
31✔
3625
            TEST_FFI_OK(botan_pk_op_kem_decrypt_shared_key,
31✔
3626
                        (kem_dec,
3627
                         nullptr /* no salt */,
3628
                         0,
3629
                         ciphertext.data(),
3630
                         ciphertext.size(),
3631
                         0 /* default length */,
3632
                         shared_key2.data(),
3633
                         &shared_key_length_out));
3634
            result.test_eq("shared key output length", shared_key_length, shared_key_length_out);
31✔
3635
            shared_key2.resize(shared_key_length_out);
31✔
3636
            TEST_FFI_OK(botan_pk_op_kem_decrypt_destroy, (kem_dec));
31✔
3637

3638
            // final check and clean up
3639
            result.test_eq("shared keys match", shared_key, shared_key2);
62✔
3640

3641
            TEST_FFI_OK(botan_pubkey_destroy, (pub));
31✔
3642
            TEST_FFI_OK(botan_pubkey_destroy, (pub_loaded));
31✔
3643
            TEST_FFI_OK(botan_privkey_destroy, (priv));
31✔
3644
            TEST_FFI_OK(botan_privkey_destroy, (priv_loaded));
62✔
3645
         }
217✔
3646
      }
3✔
3647
};
3648

3649
/**
3650
 * Base class for roundtrip tests of FFI bindings for Signature Mechanisms.
3651
 */
3652
class FFI_Signature_Roundtrip_Test : public FFI_Test {
2✔
3653
   protected:
3654
      using privkey_loader_fn_t = int (*)(botan_privkey_t*, const uint8_t[], size_t, const char*);
3655
      using pubkey_loader_fn_t = int (*)(botan_pubkey_t*, const uint8_t[], size_t, const char*);
3656

3657
   protected:
3658
      virtual const char* algo() const = 0;
3659
      virtual privkey_loader_fn_t private_key_load_function() const = 0;
3660
      virtual pubkey_loader_fn_t public_key_load_function() const = 0;
3661
      virtual std::vector<const char*> modes() const = 0;
3662
      virtual const char* hash_algo_or_padding() const = 0;
3663

3664
   public:
3665
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
2✔
3666
         const std::vector<uint8_t> message1 = {'H', 'e', 'l', 'l', 'o', ' '};
2✔
3667
         const std::vector<uint8_t> message2 = {'W', 'o', 'r', 'l', 'd', '!'};
2✔
3668

3669
         for(auto mode : modes()) {
17✔
3670
            // generate a key pair
3671
            botan_privkey_t priv;
15✔
3672
            botan_pubkey_t pub;
15✔
3673
            if(!TEST_FFI_INIT(botan_privkey_create, (&priv, algo(), mode, rng))) {
15✔
3674
               continue;
×
3675
            }
3676
            TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
15✔
3677

3678
            // raw-encode the key pair
3679
            ViewBytesSink priv_bytes;
15✔
3680
            ViewBytesSink pub_bytes;
15✔
3681
            TEST_FFI_OK(botan_privkey_view_raw, (priv, priv_bytes.delegate(), priv_bytes.callback()));
15✔
3682
            TEST_FFI_OK(botan_pubkey_view_raw, (pub, pub_bytes.delegate(), pub_bytes.callback()));
15✔
3683

3684
            // decode the key pair from raw encoding
3685
            botan_privkey_t priv_loaded;
15✔
3686
            botan_pubkey_t pub_loaded;
15✔
3687
            TEST_FFI_OK(private_key_load_function(),
15✔
3688
                        (&priv_loaded, priv_bytes.get().data(), priv_bytes.get().size(), mode));
3689
            TEST_FFI_OK(public_key_load_function(),
15✔
3690
                        (&pub_loaded, pub_bytes.get().data(), pub_bytes.get().size(), mode));
3691

3692
            // re-encode and compare to the first round
3693
            ViewBytesSink priv_bytes2;
15✔
3694
            ViewBytesSink pub_bytes2;
15✔
3695
            TEST_FFI_OK(botan_privkey_view_raw, (priv_loaded, priv_bytes2.delegate(), priv_bytes2.callback()));
15✔
3696
            TEST_FFI_OK(botan_pubkey_view_raw, (pub_loaded, pub_bytes2.delegate(), pub_bytes2.callback()));
15✔
3697
            result.test_eq("private key encoding", priv_bytes.get(), priv_bytes2.get());
30✔
3698
            result.test_eq("public key encoding", pub_bytes.get(), pub_bytes2.get());
30✔
3699

3700
            // Signature Creation (using the loaded private key)
3701
            botan_pk_op_sign_t signer;
15✔
3702
            TEST_FFI_OK(botan_pk_op_sign_create, (&signer, priv_loaded, hash_algo_or_padding(), 0));
15✔
3703

3704
            // explicitly query the signature output length
3705
            size_t sig_output_length = 0;
15✔
3706
            TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_output_length));
15✔
3707

3708
            // pass a message to the signer
3709
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message1.data(), message1.size()));
15✔
3710
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message2.data(), message2.size()));
15✔
3711

3712
            // check that insufficient buffer space is handled correctly
3713
            size_t sig_output_length_out = 0;
15✔
3714
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
15✔
3715
                        botan_pk_op_sign_finish,
3716
                        (signer, rng, nullptr, &sig_output_length_out));
3717
            result.test_eq("reported sig lengths are equal", sig_output_length, sig_output_length_out);
15✔
3718

3719
            // Recreate signer and try again
3720
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
15✔
3721
            TEST_FFI_OK(botan_pk_op_sign_create, (&signer, priv_loaded, hash_algo_or_padding(), 0));
15✔
3722
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message1.data(), message1.size()));
15✔
3723
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message2.data(), message2.size()));
15✔
3724

3725
            // allocate buffers (with additional space) and perform the actual signing
3726
            sig_output_length_out = sig_output_length * 2;
15✔
3727
            Botan::secure_vector<uint8_t> signature(sig_output_length_out);
15✔
3728
            TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &sig_output_length_out));
15✔
3729
            result.test_eq("signature length", sig_output_length, sig_output_length_out);
15✔
3730
            signature.resize(sig_output_length_out);
15✔
3731
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
15✔
3732

3733
            // Signature verification (using the generated public key)
3734
            botan_pk_op_verify_t verifier;
15✔
3735
            TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, hash_algo_or_padding(), 0));
15✔
3736
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message1.data(), message1.size()));
15✔
3737
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message2.data(), message2.size()));
15✔
3738

3739
            // Verify signature
3740
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
15✔
3741
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
15✔
3742

3743
            // Verify signature with wrong message (only first half)
3744
            TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, hash_algo_or_padding(), 0));
15✔
3745
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message1.data(), message1.size()));
15✔
3746
            TEST_FFI_RC(
15✔
3747
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
3748
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
15✔
3749

3750
            // Cleanup
3751
            TEST_FFI_OK(botan_pubkey_destroy, (pub));
15✔
3752
            TEST_FFI_OK(botan_pubkey_destroy, (pub_loaded));
15✔
3753
            TEST_FFI_OK(botan_privkey_destroy, (priv));
15✔
3754
            TEST_FFI_OK(botan_privkey_destroy, (priv_loaded));
30✔
3755
         }
77✔
3756
      }
4✔
3757
};
3758

3759
class FFI_Kyber512_Test final : public FFI_Test {
×
3760
   public:
3761
      std::string name() const override { return "FFI Kyber512"; }
1✔
3762

3763
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
3764
         const std::vector<uint8_t> a_pub_bits = Botan::hex_decode(
1✔
3765
            "5fc44b99d7584f38cd28360cc5625a905b96af12930ed5b5fe2a82fc5aa7dc4b829fe37635f13f5af2a6d3081dad878785698a0aa914374c4e43b89f094a7892aa149a38b49c06a068d829a8d249e753a375d097a0f162e6c3a4dfe8c79761410c605ed3899a3fc44378e14f28879e8f148077e6bc3bb2ae56178c491611bf6aaf5f9a9cb9b5659223007940bcd6f8a23280a56015330e8577259587b12606f4c937ea13606cb3bb046066ad294261e2b22022bcc74678a5520570d88e4ceb42692631e7e3711c4b2fd5347f0328598340cb3c65c8f55ac02716831094cb6eb90f175b173d9c650329aaf513633633bb2ce6858e7447abc41b6fb06da8782572c332b09660366926bf529ed8caaa6243ccdb152b36ba6e47c714145c86f5b3b61de84ef1470d03fa0135e35194fa1fb3bc860fa500d1299aee88ce56054376c1199c553dd90a8d6f9cc763c811d0c66da6f851abf1056635a34a68aa7815868f153a3a5c77fcc8b1eb1807fbf62a6fb43b355700e78230943a2ba1e11b181345b11b4d46266e7b359f074a500c8857d79ba60f64262d662ccd9c8489a4c19df67437db193f95b9765181d9152262b1166f97be53497f001cb1be79024d6a2289bcc704e1b1d821015366a3cc8a484e6bc2e1f1b889f19323e3101aa09ad9ea62ba4005039bbfb5998055f93fbf77b14433116d5958422654dada1127213f02b78717a5a0454271d5b0c02517a6c27a3c3610101d753c09a25571775477dc13b2e404db4965b9a9350330c73a8a3642d39af8a23839ab85c6355b12f279f849813c280d54c5913e99b6946a0aaf012c8cab025396b255f002d837c761d42a4aeb38c5f456aaf79e162700c6b4048eca6f9a7367f90238d67bcf8e6a0d8a553c071522f9d2394e28483d2048be2a8f9c8c8e39991a41273c7eacaefc6a308be870b45b41176412954a1a0fd83d362a5ab288663dec5456b6286d0b2cecb01922fb3d473802ea2b86639bce02450339261cffb114e1e725e90677826a1688f686b29a78779c9822315dafc55753e98c8ed3221f2b3220805c8a28983355207da36fb72f9bc85c0a13b9d041586fd583feb12afd5a402dd33b43543f5fa4eb436c8d");
1✔
3766
         const std::vector<uint8_t> b_priv_bits = Botan::hex_decode(
1✔
3767
            "cf8c33fabbc3e6685f60779a456412e9060a792a3f67d90389815773e06dd2071e3906a1159921485b6221b73dfc723da9d45bbc99523c55b203f81856ba8d38f731c6594a0a9796f485c7cad02ee3a80a737cada2e40b580a1060a1364d365169539ce4d800eab9723153c5b266853b3a33112ccb03491e77f57aeb74c7670426c5bb02615b1907b353beda51f38a788774b1c9eb3c49f89df59ba00e196f180a37fd9acd6691b493d78f95a49906b26fa46125663a37b0b2614197473ba012956bc9348a9afc8907527bb7c2dcbd384b85630b3687096ffd9a93d603121706c06c05baca286bebea0ecdf391ba489d7ff6a7a1c7c3dd0c521c02bb3fa7c3e5b98f19509486ab33f1597346b03c7197865792896c1a553e8379c6f51729e9907d09b4fbc5b4279298f60195998225d95c2aef335c9e6a26f4988e0f19c307fc00f8aa0b21090daab0c540363f121044729322bebb97427227e8b01705826879ab4d42e30bf191068b705527a3ae9a5b3e30371c50b5b75d189903e60175f904c1488be7e872d3dc3be1bb31921027cb075f6bb051c28980c65b9959e8acf2d38671d27285d03419512008335492476e0c23ac6263425f8a65fbc1a38110b38db641f3a9872fa312d26b9c81f11de1d9378b629e9bdc7ca31496ac9511bff42a7a96b28f740a84c2904fe4056ca1989b28439222cee31c554e982d318366037070c4bb778b261e3479c605317922947e4340560732ca8fda63e2a7ac8cc67015e97d308c361e634f8cdb1e836b572367334c949011a9c52f391194baa91f08a71e847d3be202554ab1c4f871c9b26db5f4992db5b3688b1ea877925d02895d470b9659c51213b206b82de489455c6502856b841d000aab3c065ff6720f9222914b6421224d716684a7762fc98ba7f4655f4cfa82fc52472b01b1e22255fc990b20240b802034c99777b71b5c0d8a4c733b69d5f76ddfdb1128da3e194c1169a924f5d868d1dbc4ddcb30197c25005391c1fcc098010af1e9ab72bc8c3bd1c58b15583035bc2dd0898e148fb20b8c502a31489b00dd645ab24709bff5c37d1a7fee5c56d7490ae77a61b1c5951e359d2a89a946f200b1f647c0283de5d403282c693dfccca1c1b54804ab86f36e1f16550a77b75328916c770c225b142dfb750659ba6d532003f8b6d48825b122682051acda7c20f09117b3e5c93e8c1bd446672512947d964f0484a05b85af82f4401d323484d23373c0c6c8990a3fba70f8a2c13e7350ce945c8f4882b1929f10d18bb45a883c116d1ce06493a50580069743d26062da9171006405d27e2f7220a897024dca507b737a6c32ba3d925e15c9789589507a768ca075beb2fb17a17144c52810566a972dba60ad806a2ff15957a3bbd85a8f1ef34445d18cc668ca7a8ac5fc7545934221a4c92c60024069cc34d592ac30c88c3376bc133622e2b192e9b4b60a9a84e5304873e62d6f98103cc8c43f6024fb7739418cc2602b21416040220cb3ec588679c033d61a3c697c403463c17c13a63d7304044a9f024b907b440b39b7b6423723dda943447a900af0170144755d9a968f634e26f892ee19cd00a97b48d5041b4473ec8741e077126f85bbb8334b8b2cbd63429afa543a540740c1e56893f6b2f2f7c42a12bcd48c647726cca06841212656ff9b3d3e799e92c48414d653a61ca8dc2c49b7342469a868b7938db8d7844e6311134b2c30538ffa927ee3961a44e0a66438c5643aa1d13658fe8a7c2a84702a8211422831994ca1e801131ab88a25162d3cd988ebe09e6cb01ca7324256691635c536f576990664841b7c89b0dc325472271fdc2b0824a9514b5eb46a743f9734ee20648a407f5d505cc9614748f344a16950b5483e45516236d43afd1494d564c23e20b64a124593604404c879a776a5bd9399629600e86aa1a641be7a13418cda318bbc3af0c2a66c999841251a1c2868b05028f6a56a72c33d8a653c77f69af4247dc6f80d329921ce3355894605c35372ddf0c84cacb42d6ccbdf39a55b672a891749622a011747a01625764c788413f2c0fd877275c88fd5730fa9e87a3c783702223a4443525cc5b5381c976dd9cb08ac47490125ca5c70baac01ff9143bc40ceeb2a16c1529d70c07074a0013749656ff4b16d890868b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb3338e5d6352d5a5006d7cd901489e7f851711c08e00cd4162ccfc2564d5893d52b2c7300e2d894b0eaa40a6ab254506d8c1176a33c4a1b2879604b1b80df48d31dd");
1✔
3768
         const std::vector<uint8_t> b_pub_bits = Botan::hex_decode(
1✔
3769
            "ee5c56d7490ae77a61b1c5951e359d2a89a946f200b1f647c0283de5d403282c693dfccca1c1b54804ab86f36e1f16550a77b75328916c770c225b142dfb750659ba6d532003f8b6d48825b122682051acda7c20f09117b3e5c93e8c1bd446672512947d964f0484a05b85af82f4401d323484d23373c0c6c8990a3fba70f8a2c13e7350ce945c8f4882b1929f10d18bb45a883c116d1ce06493a50580069743d26062da9171006405d27e2f7220a897024dca507b737a6c32ba3d925e15c9789589507a768ca075beb2fb17a17144c52810566a972dba60ad806a2ff15957a3bbd85a8f1ef34445d18cc668ca7a8ac5fc7545934221a4c92c60024069cc34d592ac30c88c3376bc133622e2b192e9b4b60a9a84e5304873e62d6f98103cc8c43f6024fb7739418cc2602b21416040220cb3ec588679c033d61a3c697c403463c17c13a63d7304044a9f024b907b440b39b7b6423723dda943447a900af0170144755d9a968f634e26f892ee19cd00a97b48d5041b4473ec8741e077126f85bbb8334b8b2cbd63429afa543a540740c1e56893f6b2f2f7c42a12bcd48c647726cca06841212656ff9b3d3e799e92c48414d653a61ca8dc2c49b7342469a868b7938db8d7844e6311134b2c30538ffa927ee3961a44e0a66438c5643aa1d13658fe8a7c2a84702a8211422831994ca1e801131ab88a25162d3cd988ebe09e6cb01ca7324256691635c536f576990664841b7c89b0dc325472271fdc2b0824a9514b5eb46a743f9734ee20648a407f5d505cc9614748f344a16950b5483e45516236d43afd1494d564c23e20b64a124593604404c879a776a5bd9399629600e86aa1a641be7a13418cda318bbc3af0c2a66c999841251a1c2868b05028f6a56a72c33d8a653c77f69af4247dc6f80d329921ce3355894605c35372ddf0c84cacb42d6ccbdf39a55b672a891749622a011747a01625764c788413f2c0fd877275c88fd5730fa9e87a3c783702223a4443525cc5b5381c976dd9cb08ac47490125ca5c70baac01ff9143bc40ceeb2a16c1529d70c07074a0013749656ff4b16d890868b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb333");
1✔
3770

3771
         botan_privkey_t b_priv;
1✔
3772
         if(!TEST_FFI_INIT(botan_privkey_load_kyber, (&b_priv, b_priv_bits.data(), 1632))) {
1✔
3773
            return;
×
3774
         }
3775

3776
         ViewBytesSink privkey_read;
1✔
3777
         ViewBytesSink privkey_read_raw;
1✔
3778
         TEST_FFI_OK(botan_privkey_view_kyber_raw_key, (b_priv, privkey_read.delegate(), privkey_read.callback()));
1✔
3779
         TEST_FFI_OK(botan_privkey_view_raw, (b_priv, privkey_read_raw.delegate(), privkey_read_raw.callback()));
1✔
3780
         result.test_eq("kyber512 private key", privkey_read.get(), b_priv_bits);
2✔
3781
         result.test_eq("kyber512 private key raw", privkey_read_raw.get(), b_priv_bits);
2✔
3782

3783
         ViewBytesSink pubkey_read;
1✔
3784
         ViewBytesSink pubkey_read_raw;
1✔
3785

3786
         botan_pubkey_t b_pub;
1✔
3787
         TEST_FFI_OK(botan_privkey_export_pubkey, (&b_pub, b_priv));
1✔
3788
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (b_pub, pubkey_read.delegate(), pubkey_read.callback()));
1✔
3789
         TEST_FFI_OK(botan_pubkey_view_raw, (b_pub, pubkey_read_raw.delegate(), pubkey_read_raw.callback()));
1✔
3790
         result.test_eq("kyber512 public key b", pubkey_read.get(), b_pub_bits);
2✔
3791
         result.test_eq("kyber512 raw public key b", pubkey_read_raw.get(), b_pub_bits);
2✔
3792

3793
         botan_pubkey_t a_pub;
1✔
3794
         TEST_FFI_OK(botan_pubkey_load_kyber, (&a_pub, a_pub_bits.data(), 800));
1✔
3795
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (a_pub, pubkey_read.delegate(), pubkey_read.callback()));
1✔
3796
         result.test_eq("kyber512 public key a", pubkey_read.get(), a_pub_bits);
2✔
3797

3798
         TEST_FFI_OK(botan_pubkey_destroy, (a_pub));
1✔
3799
         TEST_FFI_OK(botan_pubkey_destroy, (b_pub));
1✔
3800
         TEST_FFI_OK(botan_privkey_destroy, (b_priv));
2✔
3801
      }
7✔
3802
};
3803

3804
class FFI_Kyber768_Test final : public FFI_Test {
×
3805
   public:
3806
      std::string name() const override { return "FFI Kyber768"; }
1✔
3807

3808
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
3809
         const std::vector<uint8_t> a_pub_bits = Botan::hex_decode(
1✔
3810
            "38d4851e5c010da39a7470bc1c80916f78c7bd5891dcd3b1ea84b6f051b346b803c1b97c94604020b7279b27836c3049ea0b9a3758510b7559593237ade72587462206b709f365848c96559326a5fe6c6f4cf531fd1a18477267f66ba14af20163c41f1138a01995147f271ddfc2be5361b281e6029d210584b2859b7383667284f767bb32782baad10933da0138a3a0660a149531c03f9c8ffccb33e3c3a5a7984e21fab26aa72a8a6b942f265e52551a9c800e5a44805f0c0141a05554213387f105df56458496bd8f469051886da223cb9fe78e7b390bf94b0a937691af9550082b76d045cb4d29c23c67942608d078a1c80f24767a945d19f077d82c9b9b197073464abe69cf7c5626177308f384672d5263b0c4826db4470e1a70e4751e3918abe8fcbc3bc0531ae89e5512214b5cc94a16a014bcb3826c79fbf4add0825eeefbab88cb7cff37bb8d491f8de902578a1e961655565b7718782a23504fdc13c783f130e177925e305d1fbc63cc8c15c2c67f85500cca785de9f480490558ef71aaf0fb5b513914401269b309c4c59c64d2a757d8855f58465615925f1ea6812cb143fff383e1048e285118bf932944b86fbdf4b1b9e65685664a07775c46952aaada1168f54b47c7a231e7355c64637467b5a3c09cab67bb35f58640c2726283bb63530a15f66eca48a840c00ca8862e283c73bfbb413a2915b8d1159a043f12c59bfa828248249b76106faa61a127a0280c586350e7a42cb74ca49cabd606891ec7cb8e84affe4b2e14c71658332b755611bab7977fa76ce736b21ed34a17ac0ec3561ca9b282d4a2bc407697924b1cf918ba83d3a4fdc82564c95bd904bdeee91ed6ccb36baa88a05c80712901bf280aee6538ec2078c2a84ee5862fc137cd92e97968d69fc3453a1e1cb161c50c9f2473a0d09037b188a0fa01efc344c2ac8fe8592b0a58456662a95033659a158a2d90a6e50c253a87975785ce29c4570000a154d4b3b2c642205c8c7cf9ac6b1071fbb368ab950a744b88c95ba5243017831120a9048338d29847830d12a933a09abd21a46b828cb14e808cd35129c9dc6e5b931d4a126fefe07909618e2b4586e7b6b424963b7323ba505ba112bb9b834a7d1b78ad0df53d556a1c69369f09148b1dc9938df59223f087fd6833be5b2bc2651fe58911ac01467f9297dfdc22b41a0f1702718710b78cf35b1865813a896d45214d338155b6c043c532330c002d520739467a504a866637fb3451c849f8f83e6a94147f168da53acdf9d8affd968a84124a9abc09af960cd3b29f2344831bb41e67605eebf00df202857117399dd748b6514aed61bb2f6cb841d168d5f35e20054573a331cd4882a04b072c179158825bcf471266da0dcceab1a021c73254751d5a161c1a92062c220a217a69d9823314b4de996fe8d45f6db5af16c1561495a4c43090bc394c94e1b0ec738eb56267201c2ecd1c7b4993c0efc0284bdc9a091c294f95703a7178822c8a95b79b1e4591e0998d893875c1a879c08a073cc67df426bba792c18ae6c1feba879bec54812c2affa012973b700ad48e271078280864268600a7aa309eaa1098750a0f8a522eb929577b412f7855613688b72f9bc85c0a13b9d041586fd583feb12afd5a402dd33b43543f5fa4eb436c8d");
1✔
3811
         const std::vector<uint8_t> b_priv_bits = Botan::hex_decode(
1✔
3812
            "9bc4986eec853ac90896c7300f914216773baa687f57f81f94e6b5a26515c0a74c73b19dc6b9888755a1d1db6a1128b02c5144ed9ba75e874a68a14dee3a9bf770b767121fbbdb292e097444757fe2f324a875b0a07a4fbdcc8fd6137fce80c69f412be103b01f8b3bed392600f830d1b20d73ca0b386666d16c5d96db10e309041890bfd4ab7bdec27191979abe7637e76153918cc2af1545724abfd95ca496281a0cc6ca63a87819b75aa86452e5027d429cad64a9c029112a3a7b9fb993c6a6d0671f6c9e24986b83f67cf1236d94c051559616826a947eaa15074025d1ab810c80220e8a8c2c222d4e8594bf35811b444a57e9bf94fb2479d174d6b38c8c3b4730437c244b2513232ec798adec4a8e597bca83bca5948468f93c022ba65311a91e3d95c299c70c3f1a43cd0c83dd500bd2211b9b385b82f2c003b10d295765b3e367563a5f19033fc1231db9620f835c95f5a261190a5da1c24ed528316f0a1520d223e208a8c99b24d28598ade74fc88370e7f45102c5a6411891c661b356b9a32e1cc0fafaa085d7670e8bcb5e768eb255204f2445b5b73b04079881903a553c865744343a925c7899777b1c9dd5579a481512f8157061606a9a67c041d38bc179048be17dd9e19dc0a572bce595afa3b68ff21bf78a63a7560636b6bb01ec3362e2aaabc8965818b7f2417ca8f66a5a2a67f72a3931e125d638a872862a7b680a54aa1f25d90dbd567635ec6664919e29517325a5c5048cc8d1c31af5e4866e85025b9184a7b75ed7f2c9c8d88259fa2ec5b05ed3751584f88308c67ff1a72fa2453305160baf404db7d4be56f1547b20bb7dec23f02b10db584b28ca40d8b39c1c3ab9f3d7bbda0822604ca48f26694d69810aa888ae3c0983c5ba4cb74211f7a5361ccdee694f4202aebe13a75b1b2dda1b3232376be213582afc4fde3474766671fe865e2fd98384eb65b2f349f1e24269b91bd9d08c80849735a9951304afd130b5c2211314630aed4b6ac3b1252a0999ff5a3ec26a283342389d219fd243706128b389eb2334fb2a6184a4eab6735d7428df5633ce030b8f853ee47c4401fc5d06b43c9b66b7aeeb23b5f000a30a6f88f027ee3994fe8b63e51b83bc24bb733a3773a35cbe138f6d9c91a3a3898bca6776030d740ac355176547d624719656a9a44e91c63faf7699dc6c2c45575718d48828828b39043c2fda2af416837efc38d17c56d4b63c63a5ab43434647d029f7b236b288958f06910763610f8b2f027a8dcd780039ab34a6871427476ff6500240e83b87c95dcfa45ac5315ef34b343fb609eb296e915c849bb8c57f57c69b177eaa8456377403fe8c6627a3282d45308f675d67085a15f0b1b55aa2a8f21afd6c05c3c00e9eb8c32418cb41963ce427b43e7545c58325c7b9368db2333de424dbeb3430f007d18a68d73b7dc67960b28206a68a1be400a770b5cd9d45a72824ca00345ac56491c1414fe5287a2eb2ad61f3bdcd0c84c335b04a703425d79dbd02b0a0e90de5b331c3c29f6562969e04cdf7095b2a7646b3d006b0b83cb68580b5ccb71de1b4d9f131bac133d6088e10613a00599d81d4818403a4bea83905304cc45ca645a9b2c6484cf9490f1755c744d9988ed60475e6ae44355ed15c7b549366f29581ec2721fd6704e0ca3f878812805675141c0a15a7b7ac35a9e3f8b2a010bc184981c57852895b2695d56131e32326717f6b101df1bc82b3ba0222d52656d118538c4ca3416be1c76ba37a9901a36e4883be6c541f2bbb561818cd2f136b98f658250545c1fa5bcfdc04374016db1c5132447fd6d568866451c25412f72967de868eaeb9c546fb40ea88cd84a1a586ca51c74bc9c3e56e104323afb658d1ac003151bdc35879a4b6762648bff0caa682f1b3319805d2326d5a46af832aacefbaa1ea820568ea3925870e9b6577eb93898e1b0cfbd1c995cb4cd6cabb979813819749b40a9952f50e97c4365e777dcfe9084219294c205acb350e07db9c98f53444546460962e2bf5aa1cc12273d882fcc215a7397b3f9b307c56b9a0429b30f88453a2376669b28b4bc4b84b51714b6652b7a1a0b53e9ec61b55ca51cdb38243239ee5f18243e515f178768a888f475b3d9060136bb22ee355b3da02c16ad83bdbb4aaa13809cb4bcd5bb53710737d3883632f9254e3336af61621a376720572450e3937c0d930e349adbfa7642ff822b9135e18f943e0178617604d10e0c09ffb3e09783c09d12cbe93311757af9857b77d1488ed39321ca97c3745c5bc176ca81274c8321bcb2029938b32bbd01aec137032f9760849701649120050b50d8353c36b8bb7724a67e7660fc93324065d63912c35a86d8fe60683067bbd2685c552ad8c65c77c57c937676fd61595c453174bf996e9d3a9ccb837b25464115c1ba3343ac097b80735aed3225091167cd8f841ffe49c5e698ef542124253084623179394433a4b61547b9ee09c98c2736ea086bd69d1bc7ae68a6ae5ce682a215860006b4604dee45a3e212f97643acb77a79a880382e483537c5198d4483a176d25aac9c3670a3f30956f18ad441904776bcd48131c7d6465bce0c133010f3176c92ec962f5c6b84d4c3ae949619cf48172997ca3b1ccf5c8cc7a67923e1295801048a3d40ac4f2c6467c750fc71314a0c1fc22637dd52e7ea50907ea973d765a6a9bb2b11aa405b9187f72026696710e61af3e41c33da1a05eb65e6523704f078e74e32f10e00247967aee3a8c6546889ef67cd613ad7236583f2104122ac6c6a40a84dc96d81c569e76a952c0a25f396e48337a4fe029a4c91cc7406872706a55573b75f160a4facad7c85fab141c63454bf48990729096ce9965604c7cc1e60ae6868dcc41bc3df71c3e5593f0488b0c6a3063e817f9f4bacb17599c8666ff3591126b4891fb7f5d29660bab60cf5007043a4311d41ab3b29787184f3d3c9ab7cc247f635145b67e970505ba44ad0e06b11ec5cda4175295199d19d660204cbdc17947cc66442d3a2cd408a20fe98174f31ee4e5bb3fa8bcd102bedc26527e9bae836442978a6ccbe510f93ab77569ab1f09d0e6312dd0cc0bcaf095fa8a52a7212d14714a7bf852416c9b026301bd965c30a43d24d97298346a46b2c4bc814ba4059653358b03c9456c60bf0193932eaa2f24ea8e4b010a5a4425ce4540fbab90d8e55c97ac2687f15ff5299278824a08d4743e1a62e1c6619cd3278cd75a97a5b4e3a38668b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb3339ae9c9ce46d9da0e714c0bae8712a670d0e5dcfdd1dd0d045932c79c559b2ab3c7300e2d894b0eaa40a6ab254506d8c1176a33c4a1b2879604b1b80df48d31dd");
1✔
3813
         const std::vector<uint8_t> b_pub_bits = Botan::hex_decode(
1✔
3814
            "f9490f1755c744d9988ed60475e6ae44355ed15c7b549366f29581ec2721fd6704e0ca3f878812805675141c0a15a7b7ac35a9e3f8b2a010bc184981c57852895b2695d56131e32326717f6b101df1bc82b3ba0222d52656d118538c4ca3416be1c76ba37a9901a36e4883be6c541f2bbb561818cd2f136b98f658250545c1fa5bcfdc04374016db1c5132447fd6d568866451c25412f72967de868eaeb9c546fb40ea88cd84a1a586ca51c74bc9c3e56e104323afb658d1ac003151bdc35879a4b6762648bff0caa682f1b3319805d2326d5a46af832aacefbaa1ea820568ea3925870e9b6577eb93898e1b0cfbd1c995cb4cd6cabb979813819749b40a9952f50e97c4365e777dcfe9084219294c205acb350e07db9c98f53444546460962e2bf5aa1cc12273d882fcc215a7397b3f9b307c56b9a0429b30f88453a2376669b28b4bc4b84b51714b6652b7a1a0b53e9ec61b55ca51cdb38243239ee5f18243e515f178768a888f475b3d9060136bb22ee355b3da02c16ad83bdbb4aaa13809cb4bcd5bb53710737d3883632f9254e3336af61621a376720572450e3937c0d930e349adbfa7642ff822b9135e18f943e0178617604d10e0c09ffb3e09783c09d12cbe93311757af9857b77d1488ed39321ca97c3745c5bc176ca81274c8321bcb2029938b32bbd01aec137032f9760849701649120050b50d8353c36b8bb7724a67e7660fc93324065d63912c35a86d8fe60683067bbd2685c552ad8c65c77c57c937676fd61595c453174bf996e9d3a9ccb837b25464115c1ba3343ac097b80735aed3225091167cd8f841ffe49c5e698ef542124253084623179394433a4b61547b9ee09c98c2736ea086bd69d1bc7ae68a6ae5ce682a215860006b4604dee45a3e212f97643acb77a79a880382e483537c5198d4483a176d25aac9c3670a3f30956f18ad441904776bcd48131c7d6465bce0c133010f3176c92ec962f5c6b84d4c3ae949619cf48172997ca3b1ccf5c8cc7a67923e1295801048a3d40ac4f2c6467c750fc71314a0c1fc22637dd52e7ea50907ea973d765a6a9bb2b11aa405b9187f72026696710e61af3e41c33da1a05eb65e6523704f078e74e32f10e00247967aee3a8c6546889ef67cd613ad7236583f2104122ac6c6a40a84dc96d81c569e76a952c0a25f396e48337a4fe029a4c91cc7406872706a55573b75f160a4facad7c85fab141c63454bf48990729096ce9965604c7cc1e60ae6868dcc41bc3df71c3e5593f0488b0c6a3063e817f9f4bacb17599c8666ff3591126b4891fb7f5d29660bab60cf5007043a4311d41ab3b29787184f3d3c9ab7cc247f635145b67e970505ba44ad0e06b11ec5cda4175295199d19d660204cbdc17947cc66442d3a2cd408a20fe98174f31ee4e5bb3fa8bcd102bedc26527e9bae836442978a6ccbe510f93ab77569ab1f09d0e6312dd0cc0bcaf095fa8a52a7212d14714a7bf852416c9b026301bd965c30a43d24d97298346a46b2c4bc814ba4059653358b03c9456c60bf0193932eaa2f24ea8e4b010a5a4425ce4540fbab90d8e55c97ac2687f15ff5299278824a08d4743e1a62e1c6619cd3278cd75a97a5b4e3a38668b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb333");
1✔
3815

3816
         botan_privkey_t b_priv;
1✔
3817
         if(!TEST_FFI_INIT(botan_privkey_load_kyber, (&b_priv, b_priv_bits.data(), 2400))) {
1✔
3818
            return;
×
3819
         }
3820

3821
         ViewBytesSink privkey_read;
1✔
3822
         ViewBytesSink privkey_read_raw;
1✔
3823
         TEST_FFI_OK(botan_privkey_view_kyber_raw_key, (b_priv, privkey_read.delegate(), privkey_read.callback()));
1✔
3824
         TEST_FFI_OK(botan_privkey_view_raw, (b_priv, privkey_read_raw.delegate(), privkey_read_raw.callback()));
1✔
3825
         result.test_eq("kyber768 private key", privkey_read.get(), b_priv_bits);
2✔
3826
         result.test_eq("kyber768 private key raw", privkey_read_raw.get(), b_priv_bits);
2✔
3827

3828
         ViewBytesSink pubkey_read;
1✔
3829
         ViewBytesSink pubkey_read_raw;
1✔
3830

3831
         botan_pubkey_t b_pub;
1✔
3832
         TEST_FFI_OK(botan_privkey_export_pubkey, (&b_pub, b_priv));
1✔
3833
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (b_pub, pubkey_read.delegate(), pubkey_read.callback()));
1✔
3834
         TEST_FFI_OK(botan_pubkey_view_raw, (b_pub, pubkey_read_raw.delegate(), pubkey_read_raw.callback()));
1✔
3835
         result.test_eq("kyber768 public key b", pubkey_read.get(), b_pub_bits);
2✔
3836
         result.test_eq("kyber768 public key raw b", pubkey_read_raw.get(), b_pub_bits);
2✔
3837

3838
         botan_pubkey_t a_pub;
1✔
3839
         TEST_FFI_OK(botan_pubkey_load_kyber, (&a_pub, a_pub_bits.data(), 1184));
1✔
3840
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (a_pub, pubkey_read.delegate(), pubkey_read.callback()));
1✔
3841
         result.test_eq("kyber768 public key a", pubkey_read.get(), a_pub_bits);
2✔
3842

3843
         TEST_FFI_OK(botan_pubkey_destroy, (a_pub));
1✔
3844
         TEST_FFI_OK(botan_pubkey_destroy, (b_pub));
1✔
3845
         TEST_FFI_OK(botan_privkey_destroy, (b_priv));
2✔
3846
      }
7✔
3847
};
3848

3849
class FFI_Kyber1024_Test final : public FFI_Test {
×
3850
   public:
3851
      std::string name() const override { return "FFI Kyber1024"; }
1✔
3852

3853
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
3854
         const std::vector<uint8_t> a_pub_bits = Botan::hex_decode(
1✔
3855
            "9779a4d1fc45ec261f048b9c9daa38c9ec228b6505e8905226b38486802059c2c5c89601560634cb1337b1315365144842bc405a292e683cafa4514526945c4dfb68ee2acdb8b79532836696d53125a045bdbb8a3bcc8123083d1e682c5bd7820c76c448351151474f69d601d7708dbb2c979d77527494b68520a8ff66c34162ca2aec8072a2a51ff259389648e75b95c16abe14604edbfabaf400b76a7a0f07db61dce19102f43b2d1060747b02c4425485341fd5d9bffaa7016061374963b985209c6b9a7db3f94958311d027900a3d8c44f8435b093a236a0509f1928df7719ce8c4e90228f4db87cb9e882f2712180238845d31eb906c60eb63e0ff55e84b867a91b79ae74a03ac00473c8c1b3e6aedcc37f1e69b8e136019bdb01c374a122e164c32584b2fbcf5e013a45127c893326b6860378cb5525a48b522b30132b7688214b69808dd19aa4ff033e16252016fc9479222424393e51db7115e38114e6893cdcc8ae1117a4316548010ab4629fb672148d031a6c601a6a4a661d872d8ef693750a115958716ab9263eb0516357102358cde5256464805ca8f59661751ad6a475a7ec78cb7319c3bbc544e1bb1185aaaeb751ba0b3923246e33f40a4ff4780b745362a218d169474d7208a9f7577228308af20f8d2403a27477cf53cff133a247d5c2b298bf21bac6dc44649e63afcef54ad3a07a74e447b36bacca295e053368ef7c146e1c28edb31b3777c941a7c27dc559437b0ff83c2910f827ef244d7726af2b9708654bcfb139a26844c6f4b79cd9e4747065a77596b927f96cbdbc7267ac9e32ac396f6c4ce739aa0aa60fccac178edab1e04688fe71a74201a99bc64b55f8cb89e11545e5275af48626ec667520ba8a7c88c2307e793455ef8780fae16412ea59e92406f595902dc21c612b142801cf31abc0475c7bde772d51c55a2dfc651bc5ab4143022813cc4c8cc6f52c8d27e3cd7851849a8c4343f0c7ed6c6cb9f765c5047e55bc48aad5b932128287f70939db753a61c0d7db1830c256288c1c85cc9bbce58366dac528c893d61b69850bcb82758e836936e1f8163861586482cad35b4db1437fad6980b66280b1652d72f52407a8015f85c29e75b2158933a4f5887db320b583cdcbca274eac21f8702879b8bc4afb62ba056d146512921554c765464d4c96bd3c9a9720187c3339d0593bc4bba59155469ff6b8688a5fc5fa6b46d4070668bd168c5f796492760940999927628236b2412968438d2cbc2a978abc097320291b0ed7e4631ec9bbe28ac63ab6860e976ac1552afc43897d6937a092432dc481a914c3a1273c43009e8ec523cb93ced9898f90905340ba6e01bc572d03a893fa21c4342d88dc3e77b33b1fc063dcdb689d8c4ffc840ce879cfb3471385512cc7c7591383b1fac89832bb1cb3324e6d2868918844cb20ca4df812b42824192a43380430a37a7f3ac41ed064cfe055157b91b5c0108fb7a613a0112af4d64e48f8a345328a99b7bf0c91cb037215b0177ad4c263e6d78f5958c848158e4fd2117c248e0cab3cf98c1e27868838f8428ba0562df6b61e8736e2b8b266607918e0ce2ce3af67c81fa5c2a4d2bc8e871825b702b3beca397b33a518dac8b1393d494a90900c01b55925857cbf8027051d36a4f141a4d2dc440341305e033cb50a074be459c76a339a9a52c447360dea2a5699ccea683a42430aa6fc9545c75c0492641af7a4e9267bcc384e55714cf49741eba3b69d6417a555475d3c021112b8b3588c63747b5ce2ccfee91297fa419f9c4298978fff0870d8a855b48899ca9bb47d836d62d2038cc3816f3a698bb3bbff78c7a015b0ea1960634292e1d50b03e1043a98ca9b27063e668b05e2c17d692d382b181365a818518ec747720337ca1868596af42a90fab1870373d74b8f6d42ac86b18bca3ab04764680cf2060c529abe5b8ef4474dc8cc47a8033ebd884d3e0a0f1a94420d8a3c9162b7af87a2c8a394647434f4a3bc2b477813ac82cf387185b587f7f68938eccddd6934d143ba17bb4a712566d2a55aaddb3323713667401b4a20b86c23bf1076439cb6b8c115389dba4e6f0c915be7602b9703b535070e4a5c5649a5d7080515026706b2604575cbd0687f2729a92a997d21ea7200c41ed8315275f4c7b72f9bc85c0a13b9d041586fd583feb12afd5a402dd33b43543f5fa4eb436c8d");
1✔
3856
         const std::vector<uint8_t> b_priv_bits = Botan::hex_decode(
1✔
3857
            "9bc4986eec853ac90896c7300f914216773baa687f57f81f94e6b5a26515c0a74c73b19dc6b9888755a1d1db6a1128b02c5144ed9ba75e874a68a14dee3a9bf770b767121fbbdb292e097444757fe2f324a875b0a07a4fbdcc8fd6137fce80c69f412be103b01f8b3bed392600f830d1b20d73ca0b386666d16c5d96db10e309041890bfd4ab7bdec27191979abe7637e76153918cc2af1545724abfd95ca496281a0cc6ca63a87819b75aa86452e5027d429cad64a9c029112a3a7b9fb993c6a6d0671f6c9e24986b83f67cf1236d94c051559616826a947eaa15074025d1ab810c80220e8a8c2c222d4e8594bf35811b444a57e9bf94fb2479d174d6b38c8c3b4730437c244b2513232ec798adec4a8e597bca83bca5948468f93c022ba65311a91e3d95c299c70c3f1a43cd0c83dd500bd2211b9b385b82f2c003b10d295765b3e367563a5f19033fc1231db9620f835c95f5a261190a5da1c24ed528316f0a1520d223e208a8c99b24d28598ade74fc88370e7f45102c5a6411891c661b356b9a32e1cc0fafaa085d7670e8bcb5e768eb255204f2445b5b73b04079881903a553c865744343a925c7899777b1c9dd5579a481512f8157061606a9a67c041d38bc179048be17dd9e19dc0a572bce595afa3b68ff21bf78a63a7560636b6bb01ec3362e2aaabc8965818b7f2417ca8f66a5a2a67f72a3931e125d638a872862a7b680a54aa1f25d90dbd567635ec6664919e29517325a5c5048cc8d1c31af5e4866e85025b9184a7b75ed7f2c9c8d88259fa2ec5b05ed3751584f88308c67ff1a72fa2453305160baf404db7d4be56f1547b20bb7dec23f02b10db584b28ca40d8b39c1c3ab9f3d7bbda0822604ca48f26694d69810aa888ae3c0983c5ba4cb74211f7a5361ccdee694f4202aebe13a75b1b2dda1b3232376be213582afc4fde3474766671fe865e2fd98384eb65b2f349f1e24269b91bd9d08c80849735a9951304afd130b5c2211314630aed4b6ac3b1252a0999ff5a3ec26a283342389d219fd243706128b389eb2334fb2a6184a4eab6735d7428df5633ce030b8f853ee47c4401fc5d06b43c9b66b7aeeb23b5f000a30a6f88f027ee3994fe8b63e51b83bc24bb733a3773a35cbe138f6d9c91a3a3898bca6776030d740ac355176547d624719656a9a44e91c63faf7699dc6c2c45575718d48828828b39043c2fda2af416837efc38d17c56d4b63c63a5ab43434647d029f7b236b288958f06910763610f8b2f027a8dcd780039ab34a6871427476ff6500240e83b87c95dcfa45ac5315ef34b343fb609eb296e915c849bb8c57f57c69b177eaa8456377403fe8c6627a3282d45308f675d67085a15f0b1b55aa2a8f21afd6c05c3c00e9eb8c32418cb41963ce427b43e7545c58325c7b9368db2333de424dbeb3430f007d18a68d73b7dc67960b28206a68a1be400a770b5cd9d45a72824ca00345ac56491c1414fe5287a2eb2ad61f3bdcd0c84c335b04a703425d79dbd02b0a0e90de5b331c3c29f6562969e04cdf7095b2a7646b3d006b0b83cb68580b5ccb71de1b4d9f131bac133d6088e10613a00599d81d4818403a4bea83905304cc45ca645a9b2c6484ce4f7bfbe3597f0c2c50784065abc214b5518ed8c7d427205392987d807cdfa571d09932e0a1217e7c5703dfc9be7c4a0f2f33338f316b8c203c6b84285a0957c363d3722145b592f9d6277b1b7446aa2005663b9898901da3b8978eaa8aee87a209c72bfd6cd10a2bedac527f26c419b4bb9a4ca13dba14cff5671461c4cff238d213988120c3cae3b15b607269420c14a9b23a6bb887ad048e6688528137dd5c071172bcaddd5ba4fb9acfb1712ca04aa267b0686a237414b16c4ac03525a45513a393605a29f3b4c07dc5cd082c67b770e69723c6425a23f573216c4cabd6c9c39533f16643bc6151672c466dea63fb8e2180fbcb453c5670b01c0fc7043d198b6c320bf0dfc5016d00f46dc0ba75c7369fc13b65733301b3a92c0cf8dcc1f01e33291a9974d5b5debfa3be5531c69a066a6f4230ba778502654b7e76191d3ba68b1488c848d8ef11bb9ac5a5374af6ccacd02432b247a963685127728825a162c43c1cc79a7b60f571481a3a635a18f0050cbe6f4af1a5a2224044622bb3893bc837be699d5279dac78c9d49818ebc91d4ff6cb54b294e4c378d8eb584f561d21ca87b7c77b1c551f52112ba72c4b6514bb286856e89b48b0a62bad093894474b246a515f849056c9aeb572060ee11b93e236204a8ff1784ede0c3f4c0cd0cdb2a50bd4bcb1225c0dd2caa8a129fdcb01f0e1a75da465b6b8742e4946dbe3ab21ca6c4e592d398a5700375b7b770c17e117663b51313824854585ece4a1520c59a513555f548afdc6748eb9b770b7af8adc9b3c1861efc00ebde93f91fa1539c3411ec9b154c93d1f508b9714cdd0aaabcb436951aa54978b84faa8bd838c29a12657a6389fa658237326bf80e44cff183d9c9107d30c7b4df83908a8badbc1bb5328ae61e6451291710408196261bb913b285c168fe5c7456cd74970377330139beb4c8b4c00c5954254a748308bc30f3a1ccabed83628422e5515b294e80bfed024902a81518c0e6827caa13230016b72aec10d31aab9f9b072652486c3b779c845804d001b223b4065667a5e318b4e0b27c41a099168ce763ac245c1c26354cfc8214cacf01dc8c99c5fc855639cb7486a7a6b71766d62369ee6349f521591344fb8026099fc055c3a0ca2002271c9361f52acaf4675a97b3593f3447b284ad69a9711501411e14e48f080e2a0bceba07a02f79dc15c3c971c1e671a66bf7144395a94d08c46603c4369b25b38d924693492f6500745cb1d2a037e2cd814fac70d8254aea4526e061648d227350dec1ba6305299a45bde4029a9f63e4f62acb5246ba8b54208a37b98fa77e4b720f02b18969635cc16cb6e236ff78111b621cab0339c60b529a9207c94a74925aa3d177636a6d205d72b429f614d452358b98582e42c2f3a32c94202a8625a760483807bbac74c083a7a786ae7b46aff3810a5318e3f479e67e83b4c94587d345ffda17273a55e0432281c670e36b76539263636445e4d110fa1c92b2e65b1ee993b26c41c8c66b181206ff559728993503e7611fa0ca24156642d29a52cbb6aeff88fa1bb790b4344472503ae77967d11a904550a95cc47c3435f1e44b4b890cf5dc4cbd7486a6cab60d1a6622e40c67e854ed0e6a0c4c2aa92a29731e69bc6b3ca9e83893df4bdb37338055bc16bc7cd875b0691d0212109b77236b10b0c72b7a76e9ce346061028be3271c480435e2482d3d2733ed619c2d3698d29a3225a7731a62db4f214a0b11569e91895fbccb8a92d18f94d307a542ff96d2ce8c2bbaa8fab948c83365162268124cabe934b3a5e6530b7f74397a980c243bdce2989747565dd9ba65a7498aa7c3ee711c3be239e93cb45134c082a900cc951756afa42e74a65001822cab95f41798fdef89ea8a751740842e2f7a271e98dbd5158d7455fe9b9cdcd0a0f68c91e59b00979673a1deabf8d213971f994c0bb08899a91e0b896319b9fc69651e3453b14d1cc94462341548fc81c643ab0b10303c7be2c3458b226d0e45f58b21658a853b84615ac5234da9bc5699646fc53c0df907f9a3c598dab6c409b7884a7a84e1a51c2caa80779cd86b77314dabae85caa28972bb6503273a82eb96ab24af6bb0ad0c9ba07ce24216696e265206abc9e147ceef1ad2e343d17aabbc4803496366c7afb53f604c92323c70be5c1e3f601c215c888f3ab239c72aba0634a2901a71891f5d072d41b524e410c7f276ff1109878d7196b5615f1f50c13976257600a821909cb36cf8d2857a2f801cf258913f56a5153234640b9e61bbd4e7b69f83a1eb8ac6b92cb841e808b700aa784a733451048211797563cccc748c9fbb54b3b65445cba676149c7ae9a9a2ecab6599b8505bc561ad85d8c052ed9669e50e684270c86f1cb4fd6708a6746c643392c8a088477dc8bf58ba4a54a8b79e523fc19a28bd47d7e5a334d8296ec86c7f52b7e73b475a0716422953cdd8a8aed0b3a84dc8425145c240c55870240a23977215b364c5596286bc496728717c329dd4b5d0b310980bc0b3f591a2d5cb2c9eaccfa1c7d3a096fc11091a4007a0f23a59782699c3171da53bc7b914f26c95391d6445073a1bd7a44691bafab9c9aceccc7ec389255c3a0ff24c71b30b6bf80c010803383485a7b295991d759cefbae257bbdee1806818565ebd09bf814c98686bbf44a0b14d28735f79ca2261bf9a31b2ca090c7667168b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb33357e74a31d3cd71513fb880e1e177438f29009bab0a131fec4cc24752015efe71c7300e2d894b0eaa40a6ab254506d8c1176a33c4a1b2879604b1b80df48d31dd");
1✔
3858
         const std::vector<uint8_t> b_pub_bits = Botan::hex_decode(
1✔
3859
            "93bc837be699d5279dac78c9d49818ebc91d4ff6cb54b294e4c378d8eb584f561d21ca87b7c77b1c551f52112ba72c4b6514bb286856e89b48b0a62bad093894474b246a515f849056c9aeb572060ee11b93e236204a8ff1784ede0c3f4c0cd0cdb2a50bd4bcb1225c0dd2caa8a129fdcb01f0e1a75da465b6b8742e4946dbe3ab21ca6c4e592d398a5700375b7b770c17e117663b51313824854585ece4a1520c59a513555f548afdc6748eb9b770b7af8adc9b3c1861efc00ebde93f91fa1539c3411ec9b154c93d1f508b9714cdd0aaabcb436951aa54978b84faa8bd838c29a12657a6389fa658237326bf80e44cff183d9c9107d30c7b4df83908a8badbc1bb5328ae61e6451291710408196261bb913b285c168fe5c7456cd74970377330139beb4c8b4c00c5954254a748308bc30f3a1ccabed83628422e5515b294e80bfed024902a81518c0e6827caa13230016b72aec10d31aab9f9b072652486c3b779c845804d001b223b4065667a5e318b4e0b27c41a099168ce763ac245c1c26354cfc8214cacf01dc8c99c5fc855639cb7486a7a6b71766d62369ee6349f521591344fb8026099fc055c3a0ca2002271c9361f52acaf4675a97b3593f3447b284ad69a9711501411e14e48f080e2a0bceba07a02f79dc15c3c971c1e671a66bf7144395a94d08c46603c4369b25b38d924693492f6500745cb1d2a037e2cd814fac70d8254aea4526e061648d227350dec1ba6305299a45bde4029a9f63e4f62acb5246ba8b54208a37b98fa77e4b720f02b18969635cc16cb6e236ff78111b621cab0339c60b529a9207c94a74925aa3d177636a6d205d72b429f614d452358b98582e42c2f3a32c94202a8625a760483807bbac74c083a7a786ae7b46aff3810a5318e3f479e67e83b4c94587d345ffda17273a55e0432281c670e36b76539263636445e4d110fa1c92b2e65b1ee993b26c41c8c66b181206ff559728993503e7611fa0ca24156642d29a52cbb6aeff88fa1bb790b4344472503ae77967d11a904550a95cc47c3435f1e44b4b890cf5dc4cbd7486a6cab60d1a6622e40c67e854ed0e6a0c4c2aa92a29731e69bc6b3ca9e83893df4bdb37338055bc16bc7cd875b0691d0212109b77236b10b0c72b7a76e9ce346061028be3271c480435e2482d3d2733ed619c2d3698d29a3225a7731a62db4f214a0b11569e91895fbccb8a92d18f94d307a542ff96d2ce8c2bbaa8fab948c83365162268124cabe934b3a5e6530b7f74397a980c243bdce2989747565dd9ba65a7498aa7c3ee711c3be239e93cb45134c082a900cc951756afa42e74a65001822cab95f41798fdef89ea8a751740842e2f7a271e98dbd5158d7455fe9b9cdcd0a0f68c91e59b00979673a1deabf8d213971f994c0bb08899a91e0b896319b9fc69651e3453b14d1cc94462341548fc81c643ab0b10303c7be2c3458b226d0e45f58b21658a853b84615ac5234da9bc5699646fc53c0df907f9a3c598dab6c409b7884a7a84e1a51c2caa80779cd86b77314dabae85caa28972bb6503273a82eb96ab24af6bb0ad0c9ba07ce24216696e265206abc9e147ceef1ad2e343d17aabbc4803496366c7afb53f604c92323c70be5c1e3f601c215c888f3ab239c72aba0634a2901a71891f5d072d41b524e410c7f276ff1109878d7196b5615f1f50c13976257600a821909cb36cf8d2857a2f801cf258913f56a5153234640b9e61bbd4e7b69f83a1eb8ac6b92cb841e808b700aa784a733451048211797563cccc748c9fbb54b3b65445cba676149c7ae9a9a2ecab6599b8505bc561ad85d8c052ed9669e50e684270c86f1cb4fd6708a6746c643392c8a088477dc8bf58ba4a54a8b79e523fc19a28bd47d7e5a334d8296ec86c7f52b7e73b475a0716422953cdd8a8aed0b3a84dc8425145c240c55870240a23977215b364c5596286bc496728717c329dd4b5d0b310980bc0b3f591a2d5cb2c9eaccfa1c7d3a096fc11091a4007a0f23a59782699c3171da53bc7b914f26c95391d6445073a1bd7a44691bafab9c9aceccc7ec389255c3a0ff24c71b30b6bf80c010803383485a7b295991d759cefbae257bbdee1806818565ebd09bf814c98686bbf44a0b14d28735f79ca2261bf9a31b2ca090c7667168b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb333");
1✔
3860

3861
         botan_privkey_t b_priv;
1✔
3862
         if(!TEST_FFI_INIT(botan_privkey_load_kyber, (&b_priv, b_priv_bits.data(), 3168))) {
1✔
3863
            return;
×
3864
         }
3865

3866
         ViewBytesSink privkey_read;
1✔
3867
         ViewBytesSink privkey_read_raw;
1✔
3868
         TEST_FFI_OK(botan_privkey_view_kyber_raw_key, (b_priv, privkey_read.delegate(), privkey_read.callback()));
1✔
3869
         TEST_FFI_OK(botan_privkey_view_raw, (b_priv, privkey_read_raw.delegate(), privkey_read_raw.callback()));
1✔
3870
         result.test_eq("kyber1024 private key", privkey_read.get(), b_priv_bits);
2✔
3871
         result.test_eq("kyber1024 private key raw", privkey_read_raw.get(), b_priv_bits);
2✔
3872

3873
         ViewBytesSink pubkey_read;
1✔
3874
         ViewBytesSink pubkey_read_raw;
1✔
3875

3876
         botan_pubkey_t b_pub;
1✔
3877
         TEST_FFI_OK(botan_privkey_export_pubkey, (&b_pub, b_priv));
1✔
3878
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (b_pub, pubkey_read.delegate(), pubkey_read.callback()));
1✔
3879
         TEST_FFI_OK(botan_pubkey_view_raw, (b_pub, pubkey_read_raw.delegate(), pubkey_read_raw.callback()));
1✔
3880
         result.test_eq("kyber1024 public key b", pubkey_read.get(), b_pub_bits);
2✔
3881
         result.test_eq("kyber1024 public key raw b", pubkey_read_raw.get(), b_pub_bits);
2✔
3882

3883
         botan_pubkey_t a_pub;
1✔
3884
         TEST_FFI_OK(botan_pubkey_load_kyber, (&a_pub, a_pub_bits.data(), 1568));
1✔
3885
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (a_pub, pubkey_read.delegate(), pubkey_read.callback()));
1✔
3886
         result.test_eq("kyber1024 public key a", pubkey_read.get(), a_pub_bits);
2✔
3887

3888
         TEST_FFI_OK(botan_pubkey_destroy, (a_pub));
1✔
3889
         TEST_FFI_OK(botan_pubkey_destroy, (b_pub));
1✔
3890
         TEST_FFI_OK(botan_privkey_destroy, (b_priv));
2✔
3891
      }
7✔
3892
};
3893

3894
class FFI_ML_KEM_Test final : public FFI_KEM_Roundtrip_Test {
×
3895
   public:
3896
      std::string name() const override { return "FFI ML-KEM"; }
1✔
3897

3898
   private:
3899
      const char* algo() const override { return "ML-KEM"; }
3✔
3900

3901
      privkey_loader_fn_t private_key_load_function() const override { return botan_privkey_load_ml_kem; }
3✔
3902

3903
      pubkey_loader_fn_t public_key_load_function() const override { return botan_pubkey_load_ml_kem; }
3✔
3904

3905
      std::vector<const char*> modes() const override { return {"ML-KEM-512", "ML-KEM-768", "ML-KEM-1024"}; }
1✔
3906
};
3907

3908
class FFI_FrodoKEM_Test final : public FFI_KEM_Roundtrip_Test {
×
3909
   public:
3910
      std::string name() const override { return "FFI FrodoKEM"; }
1✔
3911

3912
   protected:
3913
      const char* algo() const override { return "FrodoKEM"; }
12✔
3914

3915
      privkey_loader_fn_t private_key_load_function() const override { return botan_privkey_load_frodokem; }
12✔
3916

3917
      pubkey_loader_fn_t public_key_load_function() const override { return botan_pubkey_load_frodokem; }
12✔
3918

3919
      std::vector<const char*> modes() const override {
1✔
3920
         return std::vector{
1✔
3921
            "FrodoKEM-640-SHAKE",
3922
            "FrodoKEM-976-SHAKE",
3923
            "FrodoKEM-1344-SHAKE",
3924
            "eFrodoKEM-640-SHAKE",
3925
            "eFrodoKEM-976-SHAKE",
3926
            "eFrodoKEM-1344-SHAKE",
3927
            "FrodoKEM-640-AES",
3928
            "FrodoKEM-976-AES",
3929
            "FrodoKEM-1344-AES",
3930
            "eFrodoKEM-640-AES",
3931
            "eFrodoKEM-976-AES",
3932
            "eFrodoKEM-1344-AES",
3933
         };
1✔
3934
      }
3935
};
3936

3937
class FFI_ML_DSA_Test final : public FFI_Signature_Roundtrip_Test {
×
3938
   public:
3939
      std::string name() const override { return "FFI ML-DSA"; }
1✔
3940

3941
   private:
3942
      const char* algo() const override { return "ML-DSA"; }
3✔
3943

3944
      privkey_loader_fn_t private_key_load_function() const override { return botan_privkey_load_ml_dsa; }
3✔
3945

3946
      pubkey_loader_fn_t public_key_load_function() const override { return botan_pubkey_load_ml_dsa; }
3✔
3947

3948
      std::vector<const char*> modes() const override {
1✔
3949
         return {
1✔
3950
            "ML-DSA-4x4",
3951
            "ML-DSA-6x5",
3952
            "ML-DSA-8x7",
3953
         };
1✔
3954
      }
3955

3956
      const char* hash_algo_or_padding() const override { return ""; }
12✔
3957
};
3958

3959
class FFI_SLH_DSA_Test final : public FFI_Signature_Roundtrip_Test {
×
3960
   public:
3961
      std::string name() const override { return "FFI SLH-DSA"; }
1✔
3962

3963
   private:
3964
      const char* algo() const override { return "SLH-DSA"; }
12✔
3965

3966
      privkey_loader_fn_t private_key_load_function() const override { return botan_privkey_load_slh_dsa; }
12✔
3967

3968
      pubkey_loader_fn_t public_key_load_function() const override { return botan_pubkey_load_slh_dsa; }
12✔
3969

3970
      std::vector<const char*> modes() const override {
1✔
3971
         auto modes = std::vector{
1✔
3972
            "SLH-DSA-SHA2-128f",
3973
            "SLH-DSA-SHAKE-128f",
3974
            "SLH-DSA-SHA2-192f",
3975
            "SLH-DSA-SHAKE-192f",
3976
            "SLH-DSA-SHA2-256f",
3977
            "SLH-DSA-SHAKE-256f",
3978
         };
1✔
3979

3980
         if(Test::run_long_tests()) {
1✔
3981
            modes = Botan::concat(modes,
3✔
3982
                                  std::vector{
1✔
3983
                                     "SLH-DSA-SHA2-128s",
3984
                                     "SLH-DSA-SHA2-192s",
3985
                                     "SLH-DSA-SHA2-256s",
3986
                                     "SLH-DSA-SHAKE-128s",
3987
                                     "SLH-DSA-SHAKE-192s",
3988
                                     "SLH-DSA-SHAKE-256s",
3989
                                  });
2✔
3990
         }
3991

3992
         return modes;
1✔
3993
      }
×
3994

3995
      const char* hash_algo_or_padding() const override { return ""; }
48✔
3996
};
3997

3998
class FFI_Classic_McEliece_Test final : public FFI_KEM_Roundtrip_Test {
×
3999
   public:
4000
      std::string name() const override { return "FFI Classic McEliece"; }
1✔
4001

4002
   protected:
4003
      const char* algo() const override { return "ClassicMcEliece"; }
16✔
4004

4005
      privkey_loader_fn_t private_key_load_function() const override { return botan_privkey_load_classic_mceliece; }
16✔
4006

4007
      pubkey_loader_fn_t public_key_load_function() const override { return botan_pubkey_load_classic_mceliece; }
16✔
4008

4009
      std::vector<const char*> modes() const override {
1✔
4010
         auto modes = std::vector{
1✔
4011
            "348864f",
4012
            "460896f",
4013
         };
1✔
4014
         if(Test::run_long_tests()) {
1✔
4015
            modes = Botan::concat(modes,
3✔
4016
                                  std::vector{
1✔
4017
                                     "348864",
4018
                                     "460896",
4019
                                     "6688128",
4020
                                     "6688128f",
4021
                                     "6688128pc",
4022
                                     "6688128pcf",
4023
                                     "6960119",
4024
                                     "6960119f",
4025
                                     "6960119pc",
4026
                                     "6960119pcf",
4027
                                     "8192128",
4028
                                     "8192128f",
4029
                                     "8192128pc",
4030
                                     "8192128pcf",
4031
                                  });
2✔
4032
         }
4033
         return modes;
1✔
4034
      }
×
4035
};
4036

4037
class FFI_ElGamal_Test final : public FFI_Test {
×
4038
   public:
4039
      std::string name() const override { return "FFI ElGamal"; }
1✔
4040

4041
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
4042
         botan_privkey_t priv;
1✔
4043

4044
         if(TEST_FFI_INIT(botan_privkey_create, (&priv, "ElGamal", "modp/ietf/1024", rng))) {
1✔
4045
            do_elgamal_test(priv, rng, result);
1✔
4046
         }
4047

4048
         if(TEST_FFI_INIT(botan_privkey_create_elgamal, (&priv, rng, 1024, 160))) {
1✔
4049
            do_elgamal_test(priv, rng, result);
1✔
4050
         }
4051
      }
1✔
4052

4053
   private:
4054
      static void do_elgamal_test(botan_privkey_t priv, botan_rng_t rng, Test::Result& result) {
2✔
4055
         TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
2✔
4056

4057
         botan_pubkey_t pub;
2✔
4058
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
2✔
4059
         TEST_FFI_OK(botan_pubkey_check_key, (pub, rng, 0));
2✔
4060

4061
         ffi_test_pubkey_export(result, pub, priv, rng);
2✔
4062
         botan_mp_t p, g, x, y;
2✔
4063
         botan_mp_init(&p);
2✔
4064
         botan_mp_init(&g);
2✔
4065
         botan_mp_init(&x);
2✔
4066
         botan_mp_init(&y);
2✔
4067

4068
         TEST_FFI_OK(botan_pubkey_get_field, (p, pub, "p"));
2✔
4069
         TEST_FFI_OK(botan_pubkey_get_field, (g, pub, "g"));
2✔
4070
         TEST_FFI_OK(botan_pubkey_get_field, (y, pub, "y"));
2✔
4071
         TEST_FFI_OK(botan_privkey_get_field, (x, priv, "x"));
2✔
4072

4073
         size_t p_len = 0;
2✔
4074
         TEST_FFI_OK(botan_mp_num_bytes, (p, &p_len));
2✔
4075

4076
         botan_privkey_t loaded_privkey;
2✔
4077
         TEST_FFI_OK(botan_privkey_load_elgamal, (&loaded_privkey, p, g, x));
2✔
4078

4079
         botan_pubkey_t loaded_pubkey;
2✔
4080
         TEST_FFI_OK(botan_pubkey_load_elgamal, (&loaded_pubkey, p, g, y));
2✔
4081

4082
         botan_mp_destroy(p);
2✔
4083
         botan_mp_destroy(g);
2✔
4084
         botan_mp_destroy(y);
2✔
4085
         botan_mp_destroy(x);
2✔
4086

4087
         std::vector<uint8_t> plaintext(16, 0xFF);
2✔
4088
         std::vector<uint8_t> ciphertext;
2✔
4089
         std::vector<uint8_t> decryption;
2✔
4090

4091
   #if defined(BOTAN_HAS_OAEP) && defined(BOTAN_HAS_SHA2_32)
4092
         const std::string padding = "OAEP(SHA-256)";
2✔
4093
   #else
4094
         const std::string padding = "Raw";
4095
   #endif
4096

4097
         // Test encryption
4098
         botan_pk_op_encrypt_t op_enc;
2✔
4099
         if(TEST_FFI_OK(botan_pk_op_encrypt_create, (&op_enc, loaded_pubkey, padding.c_str(), 0))) {
2✔
4100
            size_t ctext_len;
2✔
4101
            TEST_FFI_OK(botan_pk_op_encrypt_output_length, (op_enc, plaintext.size(), &ctext_len));
2✔
4102
            ciphertext.resize(ctext_len);
2✔
4103
            TEST_FFI_OK(botan_pk_op_encrypt,
2✔
4104
                        (op_enc, rng, ciphertext.data(), &ctext_len, plaintext.data(), plaintext.size()));
4105
            ciphertext.resize(ctext_len);
2✔
4106
            TEST_FFI_OK(botan_pk_op_encrypt_destroy, (op_enc));
4✔
4107
         }
4108

4109
         // Test decryption
4110
         botan_pk_op_decrypt_t op_dec;
2✔
4111
         if(TEST_FFI_OK(botan_pk_op_decrypt_create, (&op_dec, loaded_privkey, padding.c_str(), 0))) {
2✔
4112
            size_t ptext_len;
2✔
4113
            TEST_FFI_OK(botan_pk_op_decrypt_output_length, (op_dec, ciphertext.size(), &ptext_len));
2✔
4114
            decryption.resize(ptext_len);
2✔
4115
            TEST_FFI_OK(botan_pk_op_decrypt,
2✔
4116
                        (op_dec, decryption.data(), &ptext_len, ciphertext.data(), ciphertext.size()));
4117
            decryption.resize(ptext_len);
2✔
4118
            TEST_FFI_OK(botan_pk_op_decrypt_destroy, (op_dec));
4✔
4119
         }
4120

4121
         result.test_eq("decryption worked", decryption, plaintext);
4✔
4122

4123
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey));
2✔
4124
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
2✔
4125
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
2✔
4126
         TEST_FFI_OK(botan_privkey_destroy, (priv));
4✔
4127
      }
8✔
4128
};
4129

4130
class FFI_DH_Test final : public FFI_Test {
×
4131
   public:
4132
      std::string name() const override { return "FFI DH"; }
1✔
4133

4134
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
4135
         botan_privkey_t priv1;
1✔
4136
         if(!TEST_FFI_INIT(botan_privkey_create_dh, (&priv1, rng, "modp/ietf/2048"))) {
1✔
4137
            return;
×
4138
         }
4139

4140
         botan_privkey_t priv2;
1✔
4141
         REQUIRE_FFI_OK(botan_privkey_create_dh, (&priv2, rng, "modp/ietf/2048"));
1✔
4142

4143
         botan_pubkey_t pub1;
1✔
4144
         REQUIRE_FFI_OK(botan_privkey_export_pubkey, (&pub1, priv1));
1✔
4145

4146
         botan_pubkey_t pub2;
1✔
4147
         REQUIRE_FFI_OK(botan_privkey_export_pubkey, (&pub2, priv2));
1✔
4148

4149
         // Reload key-pair1 in order to test functions for key loading
4150
         botan_mp_t private_x, public_g, public_p, public_y;
1✔
4151

4152
         botan_mp_init(&private_x);
1✔
4153
         botan_mp_init(&public_g);
1✔
4154
         botan_mp_init(&public_p);
1✔
4155
         botan_mp_init(&public_y);
1✔
4156

4157
         TEST_FFI_OK(botan_privkey_get_field, (private_x, priv1, "x"));
1✔
4158
         TEST_FFI_OK(botan_pubkey_get_field, (public_g, pub1, "g"));
1✔
4159
         TEST_FFI_OK(botan_pubkey_get_field, (public_p, pub1, "p"));
1✔
4160
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub1, "y"));
1✔
4161

4162
         botan_privkey_t loaded_privkey1;
1✔
4163
         botan_pubkey_t loaded_pubkey1;
1✔
4164
         TEST_FFI_OK(botan_privkey_load_dh, (&loaded_privkey1, public_p, public_g, private_x));
1✔
4165
         TEST_FFI_OK(botan_pubkey_load_dh, (&loaded_pubkey1, public_p, public_g, public_y));
1✔
4166

4167
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey1, rng, 0));
1✔
4168
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey1, rng, 0));
1✔
4169

4170
         botan_mp_t loaded_public_g, loaded_public_p, loaded_public_y;
1✔
4171
         botan_mp_init(&loaded_public_g);
1✔
4172
         botan_mp_init(&loaded_public_p);
1✔
4173
         botan_mp_init(&loaded_public_y);
1✔
4174

4175
         TEST_FFI_OK(botan_pubkey_get_field, (loaded_public_g, loaded_pubkey1, "g"));
1✔
4176
         TEST_FFI_OK(botan_pubkey_get_field, (loaded_public_p, loaded_pubkey1, "p"));
1✔
4177
         TEST_FFI_OK(botan_pubkey_get_field, (loaded_public_y, loaded_pubkey1, "y"));
1✔
4178

4179
         int cmp;
1✔
4180

4181
         TEST_FFI_OK(botan_mp_cmp, (&cmp, loaded_public_g, public_g));
1✔
4182
         result.confirm("bigint_mp_cmp(g, g)", cmp == 0);
2✔
4183

4184
         TEST_FFI_OK(botan_mp_cmp, (&cmp, loaded_public_p, public_p));
1✔
4185
         result.confirm("bigint_mp_cmp(p, p)", cmp == 0);
2✔
4186

4187
         TEST_FFI_OK(botan_mp_cmp, (&cmp, loaded_public_y, public_y));
1✔
4188
         result.confirm("bigint_mp_cmp(y, y)", cmp == 0);
2✔
4189

4190
         botan_pk_op_ka_t ka1;
1✔
4191
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka1, loaded_privkey1, "Raw", 0));
1✔
4192
         botan_pk_op_ka_t ka2;
1✔
4193
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka2, priv2, "Raw", 0));
1✔
4194

4195
         size_t pubkey1_len = 0;
1✔
4196
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
4197
                     botan_pk_op_key_agreement_export_public,
4198
                     (priv1, nullptr, &pubkey1_len));
4199
         std::vector<uint8_t> pubkey1(pubkey1_len);
1✔
4200
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_export_public, (priv1, pubkey1.data(), &pubkey1_len));
1✔
4201
         size_t pubkey2_len = 0;
1✔
4202
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
4203
                     botan_pk_op_key_agreement_export_public,
4204
                     (priv2, nullptr, &pubkey2_len));
4205
         std::vector<uint8_t> pubkey2(pubkey2_len);
1✔
4206
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_export_public, (priv2, pubkey2.data(), &pubkey2_len));
1✔
4207

4208
         const size_t shared_key_len = 256;
1✔
4209

4210
         std::vector<uint8_t> key1(shared_key_len);
1✔
4211
         size_t key1_len = key1.size();
1✔
4212

4213
         TEST_FFI_OK(botan_pk_op_key_agreement,
1✔
4214
                     (ka1, key1.data(), &key1_len, pubkey2.data(), pubkey2.size(), nullptr, 0));
4215

4216
         std::vector<uint8_t> key2(shared_key_len);
1✔
4217
         size_t key2_len = key2.size();
1✔
4218

4219
         TEST_FFI_OK(botan_pk_op_key_agreement,
1✔
4220
                     (ka2, key2.data(), &key2_len, pubkey1.data(), pubkey1.size(), nullptr, 0));
4221

4222
         result.test_eq("shared DH key", key1, key2);
2✔
4223

4224
         TEST_FFI_OK(botan_mp_destroy, (private_x));
1✔
4225
         TEST_FFI_OK(botan_mp_destroy, (public_p));
1✔
4226
         TEST_FFI_OK(botan_mp_destroy, (public_g));
1✔
4227
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1✔
4228

4229
         TEST_FFI_OK(botan_mp_destroy, (loaded_public_p));
1✔
4230
         TEST_FFI_OK(botan_mp_destroy, (loaded_public_g));
1✔
4231
         TEST_FFI_OK(botan_mp_destroy, (loaded_public_y));
1✔
4232

4233
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka1));
1✔
4234
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka2));
1✔
4235
         TEST_FFI_OK(botan_privkey_destroy, (priv1));
1✔
4236
         TEST_FFI_OK(botan_privkey_destroy, (priv2));
1✔
4237
         TEST_FFI_OK(botan_pubkey_destroy, (pub1));
1✔
4238
         TEST_FFI_OK(botan_pubkey_destroy, (pub2));
1✔
4239
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey1));
1✔
4240
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey1));
2✔
4241
      }
4✔
4242
};
4243

4244
class FFI_OID_Test final : public FFI_Test {
×
4245
   public:
4246
      std::string name() const override { return "FFI OID"; }
1✔
4247

4248
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
4249
         botan_asn1_oid_t oid;
1✔
4250
         botan_asn1_oid_t new_oid;
1✔
4251
         botan_asn1_oid_t new_oid_from_string;
1✔
4252
         botan_asn1_oid_t oid_a;
1✔
4253
         botan_asn1_oid_t oid_b;
1✔
4254
         botan_asn1_oid_t oid_c;
1✔
4255

4256
         TEST_FFI_FAIL("empty oid", botan_oid_from_string, (&oid, ""));
2✔
4257
         TEST_FFI_OK(botan_oid_from_string, (&oid, "1.2.3.4.5"));
1✔
4258

4259
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_oid_from_string, (&new_oid, "a.a.a"));
1✔
4260
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_oid_from_string, (&new_oid, "0.40"));
1✔
4261
         TEST_FFI_RC(
1✔
4262
            BOTAN_FFI_ERROR_BAD_PARAMETER, botan_oid_from_string, (&new_oid, "random-name-that-definitely-has-no-oid"));
4263

4264
         TEST_FFI_OK(botan_oid_from_string, (&new_oid, "1.2.3.4.5.6.7.8"));
1✔
4265
         TEST_FFI_OK(botan_oid_register, (new_oid, "random-name-that-definitely-has-no-oid"));
1✔
4266

4267
         TEST_FFI_OK(botan_oid_from_string, (&new_oid_from_string, "random-name-that-definitely-has-no-oid"));
1✔
4268
         TEST_FFI_RC(1, botan_oid_equal, (new_oid, new_oid_from_string));
1✔
4269

4270
         TEST_FFI_OK(botan_oid_from_string, (&oid_a, "1.2.3.4.5.6"));
1✔
4271
         TEST_FFI_OK(botan_oid_from_string, (&oid_b, "1.2.3.4.5.6"));
1✔
4272
         TEST_FFI_OK(botan_oid_from_string, (&oid_c, "1.2.3.4.4"));
1✔
4273

4274
         TEST_FFI_RC(1, botan_oid_equal, (oid_a, oid_b));
1✔
4275
         TEST_FFI_RC(0, botan_oid_equal, (oid_a, oid_c));
1✔
4276

4277
         int res;
1✔
4278

4279
         TEST_FFI_OK(botan_oid_cmp, (&res, oid_a, oid_b));
1✔
4280
         result.confirm("oid_a and oid_b are equal", res == 0);
2✔
4281

4282
         TEST_FFI_OK(botan_oid_cmp, (&res, oid_a, oid_c));
1✔
4283
         result.confirm("oid_a is bigger", res == 1);
2✔
4284

4285
         TEST_FFI_OK(botan_oid_cmp, (&res, oid_c, oid_a));
1✔
4286
         result.confirm("oid_c is smaller", res == -1);
2✔
4287

4288
         TEST_FFI_OK(botan_oid_destroy, (oid));
1✔
4289
         TEST_FFI_OK(botan_oid_destroy, (new_oid));
1✔
4290
         TEST_FFI_OK(botan_oid_destroy, (new_oid_from_string));
1✔
4291
         TEST_FFI_OK(botan_oid_destroy, (oid_a));
1✔
4292
         TEST_FFI_OK(botan_oid_destroy, (oid_b));
1✔
4293
         TEST_FFI_OK(botan_oid_destroy, (oid_c));
1✔
4294

4295
         botan_privkey_t priv;
1✔
4296
         if(TEST_FFI_INIT(botan_privkey_create_rsa, (&priv, rng, 1024))) {
1✔
4297
            TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
1✔
4298

4299
            const std::string oid_rsa_expexted = "1.2.840.113549.1.1.1";
1✔
4300

4301
            botan_asn1_oid_t rsa_oid_priv;
1✔
4302
            botan_asn1_oid_t rsa_oid_pub;
1✔
4303
            botan_asn1_oid_t rsa_oid_expected;
1✔
4304
            botan_asn1_oid_t rsa_oid_from_name;
1✔
4305

4306
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_oid_from_string, (&rsa_oid_expected, nullptr));
1✔
4307
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_oid_from_string, (nullptr, "1.2.3.4.5"));
1✔
4308
            TEST_FFI_OK(botan_oid_from_string, (&rsa_oid_expected, oid_rsa_expexted.c_str()));
1✔
4309
            TEST_FFI_OK(botan_privkey_oid, (&rsa_oid_priv, priv));
1✔
4310

4311
            TEST_FFI_RC(1, botan_oid_equal, (rsa_oid_priv, rsa_oid_expected));
1✔
4312

4313
            botan_pubkey_t pub;
1✔
4314
            TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
4315

4316
            TEST_FFI_OK(botan_pubkey_oid, (&rsa_oid_pub, pub));
1✔
4317
            TEST_FFI_RC(1, botan_oid_equal, (rsa_oid_pub, rsa_oid_expected));
1✔
4318

4319
            ViewStringSink oid_string;
1✔
4320
            TEST_FFI_OK(botan_oid_view_string, (rsa_oid_expected, oid_string.delegate(), oid_string.callback()));
1✔
4321
            std::string oid_actual = {oid_string.get().begin(), oid_string.get().end()};
2✔
4322

4323
            result.test_eq("oid to string", oid_actual, oid_rsa_expexted);
1✔
4324

4325
            TEST_FFI_OK(botan_oid_from_string, (&rsa_oid_from_name, "RSA"));
1✔
4326
            TEST_FFI_RC(1, botan_oid_equal, (rsa_oid_expected, rsa_oid_from_name));
1✔
4327

4328
            ViewStringSink rsa_name;
1✔
4329
            TEST_FFI_OK(botan_oid_view_name, (rsa_oid_from_name, rsa_name.delegate(), rsa_name.callback()));
1✔
4330
            std::string rsa_name_string = {rsa_name.get().begin(), rsa_name.get().end()};
2✔
4331
            result.test_eq("oid to name", rsa_name_string, "RSA");
2✔
4332

4333
            TEST_FFI_OK(botan_oid_destroy, (rsa_oid_priv));
1✔
4334
            TEST_FFI_OK(botan_oid_destroy, (rsa_oid_pub));
1✔
4335
            TEST_FFI_OK(botan_oid_destroy, (rsa_oid_expected));
1✔
4336
            TEST_FFI_OK(botan_oid_destroy, (rsa_oid_from_name));
1✔
4337

4338
            TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
4339
            TEST_FFI_OK(botan_privkey_destroy, (priv));
2✔
4340
         }
1✔
4341
      }
1✔
4342
};
4343

4344
class FFI_EC_Group_Test final : public FFI_Test {
×
4345
   public:
4346
      std::string name() const override { return "FFI EC Group"; }
1✔
4347

4348
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
4349
         int appl_spec_groups;
1✔
4350
         int named_group;
1✔
4351
         TEST_FFI_OK(botan_ec_group_supports_application_specific_group, (&appl_spec_groups));
1✔
4352
         TEST_FFI_OK(botan_ec_group_supports_named_group, ("secp256r1", &named_group));
1✔
4353
         result.confirm("application specific groups support matches build",
2✔
4354
                        appl_spec_groups,
4355
                        Botan::EC_Group::supports_application_specific_group());
1✔
4356
         result.confirm(
2✔
4357
            "named group support matches build", named_group, Botan::EC_Group::supports_named_group("secp256r1"));
1✔
4358

4359
         if(named_group) {
1✔
4360
            botan_ec_group_t group_from_name;
1✔
4361
            botan_asn1_oid_t oid_from_name;
1✔
4362
            botan_mp_t p_from_name;
1✔
4363
            botan_mp_t a_from_name;
1✔
4364
            botan_mp_t b_from_name;
1✔
4365
            botan_mp_t g_x_from_name;
1✔
4366
            botan_mp_t g_y_from_name;
1✔
4367
            botan_mp_t order_from_name;
1✔
4368

4369
            TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_ec_group_from_name, (&group_from_name, ""));
1✔
4370

4371
            TEST_FFI_OK(botan_ec_group_from_name, (&group_from_name, "secp256r1"));
1✔
4372

4373
            get_group_parameters(group_from_name,
1✔
4374
                                 &oid_from_name,
4375
                                 &p_from_name,
4376
                                 &a_from_name,
4377
                                 &b_from_name,
4378
                                 &g_x_from_name,
4379
                                 &g_y_from_name,
4380
                                 &order_from_name,
4381
                                 result);
4382

4383
            botan_asn1_oid_t group_oid;
1✔
4384
            botan_ec_group_t group_from_oid;
1✔
4385
            botan_asn1_oid_t oid_from_oid;
1✔
4386
            botan_mp_t p_from_oid;
1✔
4387
            botan_mp_t a_from_oid;
1✔
4388
            botan_mp_t b_from_oid;
1✔
4389
            botan_mp_t g_x_from_oid;
1✔
4390
            botan_mp_t g_y_from_oid;
1✔
4391
            botan_mp_t order_from_oid;
1✔
4392

4393
            TEST_FFI_OK(botan_oid_from_string, (&group_oid, "1.2.840.10045.3.1.7"));
1✔
4394

4395
            TEST_FFI_OK(botan_ec_group_from_oid, (&group_from_oid, group_oid));
1✔
4396

4397
            get_group_parameters(group_from_oid,
1✔
4398
                                 &oid_from_oid,
4399
                                 &p_from_oid,
4400
                                 &a_from_oid,
4401
                                 &b_from_oid,
4402
                                 &g_x_from_oid,
4403
                                 &g_y_from_oid,
4404
                                 &order_from_oid,
4405
                                 result);
4406

4407
            TEST_FFI_RC(1, botan_oid_equal, (group_oid, oid_from_oid));
1✔
4408
            TEST_FFI_RC(1, botan_oid_equal, (oid_from_name, oid_from_oid));
1✔
4409

4410
            if(appl_spec_groups) {
1✔
4411
               botan_asn1_oid_t group_parameter_oid;
1✔
4412
               botan_mp_t p_parameter;
1✔
4413
               botan_mp_t a_parameter;
1✔
4414
               botan_mp_t b_parameter;
1✔
4415
               botan_mp_t g_x_parameter;
1✔
4416
               botan_mp_t g_y_parameter;
1✔
4417
               botan_mp_t order_parameter;
1✔
4418

4419
               botan_ec_group_t group_from_parameters;
1✔
4420
               botan_asn1_oid_t oid_from_parameters;
1✔
4421
               botan_mp_t p_from_parameters;
1✔
4422
               botan_mp_t a_from_parameters;
1✔
4423
               botan_mp_t b_from_parameters;
1✔
4424
               botan_mp_t g_x_from_parameters;
1✔
4425
               botan_mp_t g_y_from_parameters;
1✔
4426
               botan_mp_t order_from_parameters;
1✔
4427

4428
               TEST_FFI_OK(botan_oid_from_string, (&group_parameter_oid, "1.3.6.1.4.1.25258.100.0"));
1✔
4429
               botan_oid_register(group_parameter_oid, "secp256r1-but-manually-registered");
1✔
4430
               botan_mp_init(&p_parameter);
1✔
4431
               botan_mp_init(&a_parameter);
1✔
4432
               botan_mp_init(&b_parameter);
1✔
4433
               botan_mp_init(&g_x_parameter);
1✔
4434
               botan_mp_init(&g_y_parameter);
1✔
4435
               botan_mp_init(&order_parameter);
1✔
4436

4437
               botan_mp_set_from_str(p_parameter, "0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF");
1✔
4438
               botan_mp_set_from_str(a_parameter, "0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC");
1✔
4439
               botan_mp_set_from_str(b_parameter, "0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B");
1✔
4440
               botan_mp_set_from_str(g_x_parameter,
1✔
4441
                                     "0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296");
4442
               botan_mp_set_from_str(g_y_parameter,
1✔
4443
                                     "0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");
4444
               botan_mp_set_from_str(order_parameter,
1✔
4445
                                     "0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
4446

4447
               TEST_FFI_OK(botan_ec_group_from_params,
1✔
4448
                           (&group_from_parameters,
4449
                            group_parameter_oid,
4450
                            p_parameter,
4451
                            a_parameter,
4452
                            b_parameter,
4453
                            g_x_parameter,
4454
                            g_y_parameter,
4455
                            order_parameter));
4456

4457
               get_group_parameters(group_from_parameters,
1✔
4458
                                    &oid_from_parameters,
4459
                                    &p_from_parameters,
4460
                                    &a_from_parameters,
4461
                                    &b_from_parameters,
4462
                                    &g_x_from_parameters,
4463
                                    &g_y_from_parameters,
4464
                                    &order_from_parameters,
4465
                                    result);
4466

4467
               botan_ec_group_t group_from_registered_oid;
1✔
4468

4469
               TEST_FFI_OK(botan_ec_group_from_name, (&group_from_registered_oid, "secp256r1-but-manually-registered"));
1✔
4470

4471
               // we registered this group under a different oid
4472
               TEST_FFI_RC(0, botan_oid_equal, (oid_from_oid, oid_from_parameters));
1✔
4473

4474
               TEST_FFI_RC(1, botan_ec_group_equal, (group_from_name, group_from_parameters));
1✔
4475
               TEST_FFI_RC(1, botan_ec_group_equal, (group_from_parameters, group_from_registered_oid));
1✔
4476

4477
               std::vector<std::tuple<botan_mp_t, botan_mp_t>> parameters_inner = {
1✔
4478
                  {p_from_name, p_from_parameters},
4479
                  {a_from_name, a_from_parameters},
4480
                  {b_from_name, b_from_parameters},
4481
                  {g_x_from_name, g_x_from_parameters},
4482
                  {g_y_from_name, g_y_from_parameters},
4483
                  {order_from_name, order_from_parameters}};
1✔
4484

4485
               for(auto [x, y] : parameters_inner) {
7✔
4486
                  TEST_FFI_RC(1, botan_mp_equal, (x, y));
6✔
4487
                  botan_mp_destroy(y);
6✔
4488
               }
4489

4490
               botan_mp_destroy(p_parameter);
1✔
4491
               botan_mp_destroy(a_parameter);
1✔
4492
               botan_mp_destroy(b_parameter);
1✔
4493
               botan_mp_destroy(g_x_parameter);
1✔
4494
               botan_mp_destroy(g_y_parameter);
1✔
4495
               botan_mp_destroy(order_parameter);
1✔
4496

4497
               botan_oid_destroy(group_parameter_oid);
1✔
4498
               botan_oid_destroy(oid_from_parameters);
1✔
4499

4500
               TEST_FFI_OK(botan_ec_group_destroy, (group_from_parameters));
1✔
4501
               TEST_FFI_OK(botan_ec_group_destroy, (group_from_registered_oid));
2✔
4502
            }
1✔
4503

4504
            botan_oid_destroy(oid_from_name);
1✔
4505
            botan_oid_destroy(group_oid);
1✔
4506
            botan_oid_destroy(oid_from_oid);
1✔
4507

4508
            std::vector<std::tuple<botan_mp_t, botan_mp_t>> parameters = {{p_from_name, p_from_oid},
1✔
4509
                                                                          {a_from_name, a_from_oid},
4510
                                                                          {b_from_name, b_from_oid},
4511
                                                                          {g_x_from_name, g_x_from_oid},
4512
                                                                          {g_y_from_name, g_y_from_oid},
4513
                                                                          {order_from_name, order_from_oid}};
1✔
4514

4515
            for(auto [x, y] : parameters) {
7✔
4516
               TEST_FFI_RC(1, botan_mp_equal, (x, y));
6✔
4517
               botan_mp_destroy(x);
6✔
4518
               botan_mp_destroy(y);
6✔
4519
            }
4520

4521
            botan_ec_group_t secp384r1;
1✔
4522
            botan_ec_group_t secp384r1_with_seed;
1✔
4523

4524
            TEST_FFI_OK(botan_ec_group_from_name, (&secp384r1, "secp384r1"));
1✔
4525
            TEST_FFI_OK(botan_ec_group_from_pem,
2✔
4526
                        (&secp384r1_with_seed, Test::read_data_file("x509/ecc/secp384r1_seed.pem").c_str()));
4527

4528
            botan_mp_t p;
1✔
4529
            botan_mp_t p_with_seed;
1✔
4530
            TEST_FFI_OK(botan_ec_group_get_p, (&p, secp384r1));
1✔
4531
            TEST_FFI_OK(botan_ec_group_get_p, (&p_with_seed, secp384r1_with_seed));
1✔
4532
            TEST_FFI_RC(1, botan_mp_equal, (p, p_with_seed));
1✔
4533
            botan_mp_destroy(p);
1✔
4534
            botan_mp_destroy(p_with_seed);
1✔
4535

4536
            TEST_FFI_RC(0, botan_ec_group_equal, (group_from_name, secp384r1));
1✔
4537
            TEST_FFI_RC(1, botan_ec_group_equal, (group_from_name, group_from_oid));
1✔
4538

4539
            ViewBytesSink der_bytes;
1✔
4540
            TEST_FFI_OK(botan_ec_group_view_der, (group_from_name, der_bytes.delegate(), der_bytes.callback()));
1✔
4541
            botan_ec_group_t group_from_ber;
1✔
4542
            TEST_FFI_OK(
1✔
4543
               botan_ec_group_from_ber,
4544
               (&group_from_ber, reinterpret_cast<const uint8_t*>(der_bytes.get().data()), der_bytes.get().size()));
4545

4546
            ViewStringSink pem_string;
1✔
4547
            TEST_FFI_OK(botan_ec_group_view_pem, (group_from_name, pem_string.delegate(), pem_string.callback()));
1✔
4548
            std::string pem_actual = {pem_string.get().begin(), pem_string.get().end()};
2✔
4549

4550
            botan_ec_group_t group_from_pem;
1✔
4551
            TEST_FFI_OK(botan_ec_group_from_pem, (&group_from_pem, pem_actual.c_str()));
1✔
4552

4553
            TEST_FFI_RC(1, botan_ec_group_equal, (group_from_name, group_from_ber));
1✔
4554
            TEST_FFI_RC(1, botan_ec_group_equal, (group_from_name, group_from_pem));
1✔
4555

4556
            botan_privkey_t priv;
1✔
4557
            TEST_FFI_OK(botan_ec_privkey_create, (&priv, "ECDSA", secp384r1, rng));
1✔
4558
            char namebuf[32] = {0};
1✔
4559
            size_t name_len = sizeof(namebuf);
1✔
4560

4561
            TEST_FFI_OK(botan_privkey_algo_name, (priv, &namebuf[0], &name_len));
1✔
4562
            result.test_eq("Key name is expected value", namebuf, "ECDSA");
1✔
4563

4564
            botan_privkey_destroy(priv);
1✔
4565

4566
            TEST_FFI_OK(botan_ec_group_destroy, (group_from_name));
1✔
4567
            TEST_FFI_OK(botan_ec_group_destroy, (group_from_oid));
1✔
4568
            TEST_FFI_OK(botan_ec_group_destroy, (secp384r1));
1✔
4569
            TEST_FFI_OK(botan_ec_group_destroy, (secp384r1_with_seed));
1✔
4570
            TEST_FFI_OK(botan_ec_group_destroy, (group_from_ber));
1✔
4571
            TEST_FFI_OK(botan_ec_group_destroy, (group_from_pem));
2✔
4572
         }
3✔
4573
      }
1✔
4574

4575
   private:
4576
      static void get_group_parameters(botan_ec_group_t ec_group,
3✔
4577
                                       botan_asn1_oid_t* oid,
4578
                                       botan_mp_t* p,
4579
                                       botan_mp_t* a,
4580
                                       botan_mp_t* b,
4581
                                       botan_mp_t* g_x,
4582
                                       botan_mp_t* g_y,
4583
                                       botan_mp_t* order,
4584
                                       Test::Result& result) {
4585
         TEST_FFI_OK(botan_ec_group_get_curve_oid, (oid, ec_group));
3✔
4586
         TEST_FFI_OK(botan_ec_group_get_p, (p, ec_group));
3✔
4587
         TEST_FFI_OK(botan_ec_group_get_a, (a, ec_group));
3✔
4588
         TEST_FFI_OK(botan_ec_group_get_b, (b, ec_group));
3✔
4589
         TEST_FFI_OK(botan_ec_group_get_g_x, (g_x, ec_group));
3✔
4590
         TEST_FFI_OK(botan_ec_group_get_g_y, (g_y, ec_group));
3✔
4591
         TEST_FFI_OK(botan_ec_group_get_order, (order, ec_group));
3✔
4592
      }
3✔
4593
};
4594

4595
BOTAN_REGISTER_TEST("ffi", "ffi_utils", FFI_Utils_Test);
4596
BOTAN_REGISTER_TEST("ffi", "ffi_rng", FFI_RNG_Test);
4597
BOTAN_REGISTER_TEST("ffi", "ffi_rsa_cert", FFI_RSA_Cert_Test);
4598
BOTAN_REGISTER_TEST("ffi", "ffi_zfec", FFI_ZFEC_Test);
4599
BOTAN_REGISTER_TEST("ffi", "ffi_crl", FFI_CRL_Test);
4600
BOTAN_REGISTER_TEST("ffi", "ffi_cert_validation", FFI_Cert_Validation_Test);
4601
BOTAN_REGISTER_TEST("ffi", "ffi_cert_creation", FFI_Cert_Creation_Test);
4602
BOTAN_REGISTER_TEST("ffi", "ffi_ecdsa_certificate", FFI_ECDSA_Certificate_Test);
4603
BOTAN_REGISTER_TEST("ffi", "ffi_pkcs_hashid", FFI_PKCS_Hashid_Test);
4604
BOTAN_REGISTER_TEST("ffi", "ffi_cbc_cipher", FFI_CBC_Cipher_Test);
4605
BOTAN_REGISTER_TEST("ffi", "ffi_gcm", FFI_GCM_Test);
4606
BOTAN_REGISTER_TEST("ffi", "ffi_chacha", FFI_ChaCha20Poly1305_Test);
4607
BOTAN_REGISTER_TEST("ffi", "ffi_eax", FFI_EAX_Test);
4608
BOTAN_REGISTER_TEST("ffi", "ffi_aead", FFI_AEAD_Test);
4609
BOTAN_REGISTER_TEST("ffi", "ffi_streamcipher", FFI_StreamCipher_Test);
4610
BOTAN_REGISTER_TEST("ffi", "ffi_hashfunction", FFI_HashFunction_Test);
4611
BOTAN_REGISTER_TEST("ffi", "ffi_mac", FFI_MAC_Test);
4612
BOTAN_REGISTER_TEST("ffi", "ffi_scrypt", FFI_Scrypt_Test);
4613
BOTAN_REGISTER_TEST("ffi", "ffi_kdf", FFI_KDF_Test);
4614
BOTAN_REGISTER_TEST("ffi", "ffi_blockcipher", FFI_Blockcipher_Test);
4615
BOTAN_REGISTER_TEST("ffi", "ffi_errorhandling", FFI_ErrorHandling_Test);
4616
BOTAN_REGISTER_TEST("ffi", "ffi_base64", FFI_Base64_Test);
4617
BOTAN_REGISTER_TEST("ffi", "ffi_hex", FFI_Hex_Test);
4618
BOTAN_REGISTER_TEST("ffi", "ffi_mp", FFI_MP_Test);
4619
BOTAN_REGISTER_TEST("ffi", "ffi_fpe", FFI_FPE_Test);
4620
BOTAN_REGISTER_TEST("ffi", "ffi_totp", FFI_TOTP_Test);
4621
BOTAN_REGISTER_TEST("ffi", "ffi_hotp", FFI_HOTP_Test);
4622
BOTAN_REGISTER_TEST("ffi", "ffi_keywrap", FFI_Keywrap_Test);
4623
BOTAN_REGISTER_TEST("ffi", "ffi_xmss", FFI_XMSS_Test);
4624
BOTAN_REGISTER_TEST("ffi", "ffi_rsa", FFI_RSA_Test);
4625
BOTAN_REGISTER_TEST("ffi", "ffi_dsa", FFI_DSA_Test);
4626
BOTAN_REGISTER_TEST("ffi", "ffi_ecdsa", FFI_ECDSA_Test);
4627
BOTAN_REGISTER_TEST("ffi", "ffi_sm2_sig", FFI_SM2_Sig_Test);
4628
BOTAN_REGISTER_TEST("ffi", "ffi_sm2_enc", FFI_SM2_Enc_Test);
4629
BOTAN_REGISTER_TEST("ffi", "ffi_ecdh", FFI_ECDH_Test);
4630
BOTAN_REGISTER_TEST("ffi", "ffi_mceliece", FFI_McEliece_Test);
4631
BOTAN_REGISTER_TEST("ffi", "ffi_ed25519", FFI_Ed25519_Test);
4632
BOTAN_REGISTER_TEST("ffi", "ffi_ed448", FFI_Ed448_Test);
4633
BOTAN_REGISTER_TEST("ffi", "ffi_x25519", FFI_X25519_Test);
4634
BOTAN_REGISTER_TEST("ffi", "ffi_x448", FFI_X448_Test);
4635
BOTAN_REGISTER_TEST("ffi", "ffi_kyber512", FFI_Kyber512_Test);
4636
BOTAN_REGISTER_TEST("ffi", "ffi_kyber768", FFI_Kyber768_Test);
4637
BOTAN_REGISTER_TEST("ffi", "ffi_kyber1024", FFI_Kyber1024_Test);
4638
BOTAN_REGISTER_TEST("ffi", "ffi_ml_kem", FFI_ML_KEM_Test);
4639
BOTAN_REGISTER_TEST("ffi", "ffi_ml_dsa", FFI_ML_DSA_Test);
4640
BOTAN_REGISTER_TEST("ffi", "ffi_slh_dsa", FFI_SLH_DSA_Test);
4641
BOTAN_REGISTER_TEST("ffi", "ffi_frodokem", FFI_FrodoKEM_Test);
4642
BOTAN_REGISTER_TEST("ffi", "ffi_cmce", FFI_Classic_McEliece_Test);
4643
BOTAN_REGISTER_TEST("ffi", "ffi_elgamal", FFI_ElGamal_Test);
4644
BOTAN_REGISTER_TEST("ffi", "ffi_dh", FFI_DH_Test);
4645
BOTAN_REGISTER_TEST("ffi", "ffi_oid", FFI_OID_Test);
4646
BOTAN_REGISTER_TEST("ffi", "ffi_ec_group", FFI_EC_Group_Test);
4647

4648
#endif
4649

4650
}  // namespace
4651

4652
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc