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

randombit / botan / 6513138375

13 Oct 2023 09:12PM UTC coverage: 91.707% (-0.002%) from 91.709%
6513138375

push

github

web-flow
Merge pull request #3753 from randombit/jack/bufferstuffer-pk-pad

Use BufferStuffer in pk_pad

80034 of 87271 relevant lines covered (91.71%)

8536716.38 hits per line

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

96.78
/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/ffi.h>
17
   #include <botan/hex.h>
18
   #include <botan/internal/loadstor.h>
19
   #include <set>
20
#endif
21

22
namespace Botan_Tests {
23

24
namespace {
25

26
#if defined(BOTAN_HAS_FFI)
27

28
   // NOLINTNEXTLINE(*-macro-usage)
29
   #define TEST_FFI_OK(func, args) result.test_rc_ok(#func, func args)
30
   // NOLINTNEXTLINE(*-macro-usage)
31
   #define TEST_FFI_INIT(func, args) result.test_rc_init(#func, func args)
32
   // NOLINTNEXTLINE(*-macro-usage)
33
   #define TEST_FFI_FAIL(msg, func, args) result.test_rc_fail(#func, msg, func args)
34
   // NOLINTNEXTLINE(*-macro-usage)
35
   #define TEST_FFI_RC(rc, func, args) result.test_rc(#func, rc, func args)
36

37
   // NOLINTNEXTLINE(*-macro-usage)
38
   #define REQUIRE_FFI_OK(func, args)                           \
39
      if(!TEST_FFI_OK(func, args)) {                            \
40
         result.test_note("Exiting test early due to failure"); \
41
         return;                                                \
42
      }
43

44
class FFI_Test : public Test {
39✔
45
   public:
46
      std::vector<Test::Result> run() override {
39✔
47
         Test::Result result(this->name());
39✔
48

49
         botan_rng_t rng;
39✔
50
         if(botan_rng_init(&rng, "system") != 0) {
39✔
51
            result.test_failure("Failed to init RNG");
×
52
            return {result};
×
53
         }
54

55
         result.start_timer();
39✔
56
         ffi_test(result, rng);
39✔
57
         result.end_timer();
39✔
58

59
         botan_rng_destroy(rng);
39✔
60

61
         return {result};
78✔
62
      }
39✔
63

64
   private:
65
      virtual std::string name() const = 0;
66
      virtual void ffi_test(Test::Result& result, botan_rng_t rng) = 0;
67
};
68

69
void ffi_test_pubkey_export(Test::Result& result, botan_pubkey_t pub, botan_privkey_t priv, botan_rng_t rng) {
11✔
70
   // export public key
71
   size_t pubkey_len = 0;
11✔
72
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
11✔
73
               botan_pubkey_export,
74
               (pub, nullptr, &pubkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
75

76
   std::vector<uint8_t> pubkey(pubkey_len);
11✔
77
   TEST_FFI_OK(botan_pubkey_export, (pub, pubkey.data(), &pubkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
11✔
78

79
   pubkey_len = 0;
11✔
80
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
11✔
81
               botan_pubkey_export,
82
               (pub, nullptr, &pubkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
83

84
   pubkey.resize(pubkey_len);
11✔
85
   TEST_FFI_OK(botan_pubkey_export, (pub, pubkey.data(), &pubkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
11✔
86

87
   // reimport exported public key
88
   botan_pubkey_t pub_copy;
11✔
89
   TEST_FFI_OK(botan_pubkey_load, (&pub_copy, pubkey.data(), pubkey_len));
11✔
90
   TEST_FFI_OK(botan_pubkey_check_key, (pub_copy, rng, 0));
11✔
91
   TEST_FFI_OK(botan_pubkey_destroy, (pub_copy));
11✔
92

93
   // export private key
94
   std::vector<uint8_t> privkey;
11✔
95
   size_t privkey_len = 0;
11✔
96

97
   // call with nullptr to query the length
98
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
11✔
99
               botan_privkey_export,
100
               (priv, nullptr, &privkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
101

102
   privkey.resize(privkey_len);
11✔
103
   privkey_len = privkey.size();  // set buffer size
11✔
104

105
   TEST_FFI_OK(botan_privkey_export, (priv, privkey.data(), &privkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
11✔
106

107
   privkey.resize(privkey_len);
11✔
108

109
   result.test_gte("Reasonable size", privkey.size(), 32);
11✔
110

111
   // reimport exported private key
112
   botan_privkey_t copy;
11✔
113
   TEST_FFI_OK(botan_privkey_load, (&copy, rng, privkey.data(), privkey.size(), nullptr));
11✔
114
   botan_privkey_destroy(copy);
11✔
115

116
   // Now again for PEM
117
   privkey_len = 0;
11✔
118

119
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
11✔
120
               botan_privkey_export,
121
               (priv, nullptr, &privkey_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
122

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

126
   TEST_FFI_OK(botan_privkey_load, (&copy, rng, privkey.data(), privkey.size(), nullptr));
11✔
127
   botan_privkey_destroy(copy);
11✔
128

129
   #if defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_PKCS5_PBES2)
130
   const size_t pbkdf_iter = 1000;
11✔
131

132
   // export private key encrypted
133
   privkey_len = 0;
11✔
134
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
11✔
135
               botan_privkey_export_encrypted_pbkdf_iter,
136
               (priv, nullptr, &privkey_len, rng, "password", pbkdf_iter, "", "", BOTAN_PRIVKEY_EXPORT_FLAG_DER));
137

138
   privkey.resize(privkey_len);
11✔
139
   privkey_len = privkey.size();
11✔
140

141
   TEST_FFI_OK(
11✔
142
      botan_privkey_export_encrypted_pbkdf_iter,
143
      (priv, privkey.data(), &privkey_len, rng, "password", pbkdf_iter, "", "", BOTAN_PRIVKEY_EXPORT_FLAG_DER));
144

145
   // reimport encrypted private key
146
   botan_privkey_load(&copy, rng, privkey.data(), privkey.size(), "password");
11✔
147
   botan_privkey_destroy(copy);
11✔
148

149
   privkey_len = 0;
11✔
150
   TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
11✔
151
               botan_privkey_export_encrypted_pbkdf_iter,
152
               (priv, nullptr, &privkey_len, rng, "password", pbkdf_iter, "", "", BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
153

154
   privkey.resize(privkey_len);
11✔
155
   TEST_FFI_OK(
11✔
156
      botan_privkey_export_encrypted_pbkdf_iter,
157
      (priv, privkey.data(), &privkey_len, rng, "password", pbkdf_iter, "", "", BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
158

159
   privkey.resize(privkey_len * 2);
11✔
160
   privkey_len = privkey.size();
11✔
161
   const uint32_t pbkdf_msec = 100;
11✔
162
   size_t pbkdf_iters_out = 0;
11✔
163

164
      #if defined(BOTAN_HAS_SCRYPT)
165
   const std::string pbe_hash = "Scrypt";
11✔
166
      #else
167
   const std::string pbe_hash = "SHA-512";
168
      #endif
169

170
      #if defined(BOTAN_HAS_AEAD_GCM)
171
   const std::string pbe_cipher = "AES-256/GCM";
11✔
172
      #else
173
   const std::string pbe_cipher = "AES-256/CBC";
174
      #endif
175

176
   TEST_FFI_OK(botan_privkey_export_encrypted_pbkdf_msec,
11✔
177
               (priv,
178
                privkey.data(),
179
                &privkey_len,
180
                rng,
181
                "password",
182
                pbkdf_msec,
183
                &pbkdf_iters_out,
184
                pbe_cipher.c_str(),
185
                pbe_hash.c_str(),
186
                0));
187

188
   if(pbe_hash == "Scrypt") {
11✔
189
      result.test_eq("Scrypt iters set to zero in this API", pbkdf_iters_out, 0);
22✔
190
   } else {
191
      // PBKDF2 currently always rounds to multiple of 2000
192
      result.test_eq("Expected PBKDF2 iters", pbkdf_iters_out % 2000, 0);
×
193
   }
194

195
   privkey.resize(privkey_len);
11✔
196

197
   TEST_FFI_OK(botan_privkey_load, (&copy, rng, privkey.data(), privkey.size(), "password"));
11✔
198
   botan_privkey_destroy(copy);
11✔
199
   #endif
200

201
   // calculate fingerprint
202
   size_t strength = 0;
11✔
203
   TEST_FFI_OK(botan_pubkey_estimated_strength, (pub, &strength));
11✔
204
   result.test_gte("estimated strength", strength, 1);
11✔
205

206
   size_t fingerprint_len = 0;
11✔
207
   TEST_FFI_RC(
11✔
208
      BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_pubkey_fingerprint, (pub, "SHA-256", nullptr, &fingerprint_len));
209

210
   std::vector<uint8_t> fingerprint(fingerprint_len);
11✔
211
   TEST_FFI_OK(botan_pubkey_fingerprint, (pub, "SHA-256", fingerprint.data(), &fingerprint_len));
22✔
212
}
33✔
213

214
class FFI_Utils_Test final : public FFI_Test {
×
215
   public:
216
      std::string name() const override { return "FFI Utils"; }
1✔
217

218
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
219
         result.test_is_eq("FFI API version", botan_ffi_api_version(), uint32_t(BOTAN_HAS_FFI));
1✔
220
         result.test_is_eq("Major version", botan_version_major(), Botan::version_major());
1✔
221
         result.test_is_eq("Minor version", botan_version_minor(), Botan::version_minor());
1✔
222
         result.test_is_eq("Patch version", botan_version_patch(), Botan::version_patch());
1✔
223
         result.test_is_eq("Botan version", botan_version_string(), Botan::version_cstr());
1✔
224
         result.test_is_eq("Botan version datestamp", botan_version_datestamp(), Botan::version_datestamp());
1✔
225
         result.test_is_eq("FFI supports its own version", botan_ffi_supports_api(botan_ffi_api_version()), 0);
1✔
226

227
         result.test_is_eq("FFI supports 2.0 version", botan_ffi_supports_api(20150515), 0);
1✔
228
         result.test_is_eq("FFI supports 2.1 version", botan_ffi_supports_api(20170327), 0);
1✔
229
         result.test_is_eq("FFI supports 2.3 version", botan_ffi_supports_api(20170815), 0);
1✔
230
         result.test_is_eq("FFI supports 2.8 version", botan_ffi_supports_api(20180713), 0);
1✔
231

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

234
         const std::vector<uint8_t> mem1 = {0xFF, 0xAA, 0xFF};
1✔
235
         const std::vector<uint8_t> mem2 = {0xFF, 0xA9, 0xFF};
1✔
236

237
         TEST_FFI_RC(0, botan_constant_time_compare, (mem1.data(), mem1.data(), mem1.size()));
1✔
238
         TEST_FFI_RC(-1, botan_constant_time_compare, (mem1.data(), mem2.data(), mem1.size()));
1✔
239

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

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

246
         std::string outstr;
1✔
247
         std::vector<uint8_t> outbuf;
1✔
248

249
         outstr.resize(2 * bin.size());
1✔
250
         TEST_FFI_OK(botan_hex_encode, (bin.data(), bin.size(), &outstr[0], 0));
1✔
251
         result.test_eq("uppercase hex", outstr, "AADE01");
2✔
252

253
         TEST_FFI_OK(botan_hex_encode, (bin.data(), bin.size(), &outstr[0], BOTAN_FFI_HEX_LOWER_CASE));
1✔
254
         result.test_eq("lowercase hex", outstr, "aade01");
2✔
255
      }
5✔
256
};
257

258
class FFI_RNG_Test final : public FFI_Test {
×
259
   public:
260
      std::string name() const override { return "FFI RNG"; }
1✔
261

262
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
263
         // RNG test and initialization
264
         botan_rng_t rng;
1✔
265
         botan_rng_t system_rng;
1✔
266
         botan_rng_t hwrng_rng = nullptr;
1✔
267
         botan_rng_t null_rng;
1✔
268
         botan_rng_t custom_rng;
1✔
269

270
         TEST_FFI_FAIL("invalid rng type", botan_rng_init, (&rng, "invalid_type"));
2✔
271

272
         REQUIRE_FFI_OK(botan_rng_init, (&system_rng, "system"));
1✔
273
         REQUIRE_FFI_OK(botan_rng_init, (&null_rng, "null"));
1✔
274

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

278
         std::vector<uint8_t> outbuf(512);
1✔
279

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

283
         if(rc != 0) {
1✔
284
            REQUIRE_FFI_OK(botan_rng_init, (&rng, "user"));
×
285
            REQUIRE_FFI_OK(botan_rng_destroy, (rng));
×
286
         }
287

288
         if(rc == 0) {
1✔
289
            TEST_FFI_OK(botan_rng_get, (rng, outbuf.data(), outbuf.size()));
1✔
290
            TEST_FFI_OK(botan_rng_reseed, (rng, 256));
1✔
291

292
            TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE, botan_rng_reseed_from_rng, (rng, null_rng, 256));
1✔
293
            if(hwrng_rng) {
1✔
294
               TEST_FFI_OK(botan_rng_reseed_from_rng, (rng, hwrng_rng, 256));
2✔
295
            }
296
            TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE, botan_rng_get, (null_rng, outbuf.data(), outbuf.size()));
1✔
297

298
            TEST_FFI_OK(botan_rng_destroy, (rng));
2✔
299
         }
300

301
         if(TEST_FFI_OK(botan_rng_init, (&rng, "user"))) {
1✔
302
            TEST_FFI_OK(botan_rng_get, (rng, outbuf.data(), outbuf.size()));
1✔
303
            TEST_FFI_OK(botan_rng_reseed, (rng, 256));
1✔
304

305
            TEST_FFI_OK(botan_rng_reseed_from_rng, (rng, system_rng, 256));
1✔
306

307
            uint8_t not_really_entropy[32] = {0};
1✔
308
            TEST_FFI_OK(botan_rng_add_entropy, (rng, not_really_entropy, 32));
2✔
309
         }
310

311
         uint8_t system_rng_buf[4096];
1✔
312
         TEST_FFI_OK(botan_system_rng_get, (system_rng_buf, sizeof(system_rng_buf)));
1✔
313

314
         size_t cb_counter = 0;
1✔
315

316
         auto custom_get_cb = +[](void* context, uint8_t* out, size_t out_len) -> int {
1✔
317
            for(size_t i = 0; i != out_len; ++i) {
318
               out[i] = 0x12;
319
            }
320
            (*(static_cast<size_t*>(context)))++;
321
            return 0;
322
         };
323

324
         auto custom_add_entropy_cb = +[](void* context, const uint8_t input[], size_t length) -> int {
1✔
325
            BOTAN_UNUSED(input, length);
326
            (*(static_cast<size_t*>(context)))++;
327
            return 0;
328
         };
329

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

332
         if(TEST_FFI_OK(
2✔
333
               botan_rng_init_custom,
334
               (&custom_rng, "custom rng", &cb_counter, custom_get_cb, custom_add_entropy_cb, custom_destroy_cb))) {
335
            Botan::clear_mem(outbuf.data(), outbuf.size());
1✔
336
            TEST_FFI_OK(botan_rng_get, (custom_rng, outbuf.data(), outbuf.size()));
1✔
337
            result.test_eq("custom_get_cb called", cb_counter, 1);
1✔
338
            std::vector<uint8_t> pattern(outbuf.size(), 0x12);
1✔
339
            result.test_eq("custom_get_cb returned bytes", pattern, outbuf);
1✔
340

341
            TEST_FFI_OK(botan_rng_reseed, (custom_rng, 256));
1✔
342
            result.test_eq("custom_add_entropy_cb called", cb_counter, 2);
1✔
343

344
            TEST_FFI_OK(botan_rng_reseed_from_rng, (custom_rng, system_rng, 256));
1✔
345
            result.test_eq("custom_add_entropy_cb called", cb_counter, 3);
1✔
346

347
            uint8_t not_really_entropy[32] = {0};
1✔
348
            TEST_FFI_OK(botan_rng_add_entropy, (custom_rng, not_really_entropy, 32));
1✔
349
            result.test_eq("custom_add_entropy_cb called", cb_counter, 4);
1✔
350

351
            TEST_FFI_OK(botan_rng_destroy, (custom_rng));
1✔
352
            result.test_eq("custom_destroy_cb called", cb_counter, 5);
2✔
353
         }
1✔
354

355
         TEST_FFI_OK(botan_rng_destroy, (rng));
1✔
356
         TEST_FFI_OK(botan_rng_destroy, (null_rng));
1✔
357
         TEST_FFI_OK(botan_rng_destroy, (system_rng));
1✔
358
         TEST_FFI_OK(botan_rng_destroy, (hwrng_rng));
2✔
359
      }
1✔
360
};
361

362
class FFI_RSA_Cert_Test final : public FFI_Test {
×
363
   public:
364
      std::string name() const override { return "FFI RSA cert"; }
1✔
365

366
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
367
         botan_x509_cert_t cert;
1✔
368
         if(TEST_FFI_INIT(botan_x509_cert_load_file, (&cert, Test::data_file("x509/ocsp/randombit.pem").c_str()))) {
4✔
369
            TEST_FFI_RC(0, botan_x509_cert_hostname_match, (cert, "randombit.net"));
1✔
370
            TEST_FFI_RC(0, botan_x509_cert_hostname_match, (cert, "www.randombit.net"));
1✔
371
            TEST_FFI_RC(-1, botan_x509_cert_hostname_match, (cert, "*.randombit.net"));
1✔
372
            TEST_FFI_RC(-1, botan_x509_cert_hostname_match, (cert, "flub.randombit.net"));
1✔
373
            TEST_FFI_RC(-1, botan_x509_cert_hostname_match, (cert, "randombit.net.com"));
1✔
374

375
            botan_x509_cert_t copy;
1✔
376
            TEST_FFI_OK(botan_x509_cert_dup, (&copy, cert));
1✔
377
            TEST_FFI_RC(0, botan_x509_cert_hostname_match, (copy, "randombit.net"));
1✔
378

379
            TEST_FFI_OK(botan_x509_cert_destroy, (copy));
1✔
380
            TEST_FFI_OK(botan_x509_cert_destroy, (cert));
2✔
381
         }
382
      }
1✔
383
};
384

385
class FFI_ZFEC_Test final : public FFI_Test {
×
386
   public:
387
      std::string name() const override { return "FFI ZFEC"; }
1✔
388

389
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
390
         /* exercise a simple success case
391
          */
392

393
         // Select some arbitrary, valid encoding parameters.  There is
394
         // nothing special about them but some relationships between these
395
         // values and other inputs must hold.
396
         const size_t K = 3;
1✔
397
         const size_t N = 11;
1✔
398

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

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

409
         // K of the blocks are required so the total information represented
410
         // can be this multiple.  totalSize must be a multiple of K and it
411
         // always will be using this construction.
412
         const size_t totalSize = blockSize * K;
1✔
413

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

418
         // Allocate memory for the encoding and decoding output parameters.
419
         std::vector<uint8_t> encoded_buf(N * blockSize);
1✔
420
         std::vector<uint8_t> decoded_buf(K * blockSize);
1✔
421

422
         std::vector<uint8_t*> encoded(N);
1✔
423
         for(size_t i = 0; i < N; ++i) {
12✔
424
            encoded[i] = &encoded_buf[i * blockSize];
11✔
425
         }
426
         std::vector<uint8_t*> decoded(K);
1✔
427
         for(size_t i = 0; i < K; ++i) {
4✔
428
            decoded[i] = &decoded_buf[i * blockSize];
3✔
429
         }
430

431
         // First encode the complete input string into N blocks where K are
432
         // required for reconstruction.  The N encoded blocks will end up in
433
         // `encoded`.
434
         if(!TEST_FFI_INIT(botan_zfec_encode, (K, N, input, totalSize, encoded.data()))) {
2✔
435
            return;
436
         }
437

438
         // Any K blocks can be decoded to reproduce the original input (split
439
         // across an array of K strings of blockSize bytes each).  This loop
440
         // only exercises decoding with consecutive blocks because it's
441
         // harder to pick non-consecutive blocks out for a test.
442
         for(size_t offset = 0; offset < N - K; ++offset) {
9✔
443
            result.test_note("About to decode with offset " + std::to_string(offset));
16✔
444
            // Pass in the K shares starting from `offset` (and their indexes)
445
            // so that we can try decoding a certain group of blocks here.  Any
446
            // K shares *should* work.
447
            REQUIRE_FFI_OK(botan_zfec_decode,
16✔
448
                           (K, N, indexes.data() + offset, encoded.data() + offset, blockSize, decoded.data()));
449

450
            // Check that the original input bytes have been written to the
451
            // output parameter.
452
            for(size_t k = 0, pos = 0; k < K; ++k, pos += blockSize) {
32✔
453
               TEST_FFI_RC(0, botan_constant_time_compare, (input + pos, decoded[k], blockSize));
48✔
454
            }
455
         }
456

457
         /* Exercise a couple basic failure cases, such as you encounter if
458
          * the caller supplies invalid parameters.  We don't try to
459
          * exhaustively prove invalid parameters are handled through this
460
          * interface since the implementation only passes them through to
461
          * ZFEC::{encode,decode} where the real checking is.  We just want to
462
          * see that errors can propagate.
463
          */
464
         TEST_FFI_FAIL("encode with out-of-bounds encoding parameters should have failed",
2✔
465
                       botan_zfec_encode,
466
                       (0, 0, nullptr, 0, nullptr));
467
         TEST_FFI_FAIL("decode with out-of-bounds encoding parameters should have failed",
3✔
468
                       botan_zfec_decode,
469
                       (0, 0, nullptr, nullptr, 0, nullptr));
470
      }
5✔
471
};
472

473
class FFI_CRL_Test final : public FFI_Test {
×
474
   public:
475
      std::string name() const override { return "FFI CRL"; }
1✔
476

477
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
478
         const char* crl_string =
1✔
479
            "-----BEGIN X509 CRL-----\n"
480
            "MIICoTCCAQkCAQEwDQYJKoZIhvcNAQELBQAwgZQxLTArBgNVBAMTJFVzYWJsZSBj\n"
481
            "ZXJ0IHZhbGlkYXRpb246IFRlbXBvcmFyeSBDQTE5MDcGA1UECxMwQ2VudHJlIGZv\n"
482
            "ciBSZXNlYXJjaCBvbiBDcnlwdG9ncmFwaHkgYW5kIFNlY3VyaXR5MRswGQYDVQQK\n"
483
            "ExJNYXNhcnlrIFVuaXZlcnNpdHkxCzAJBgNVBAYTAkNaGA8yMDUwMDIyNTE1MjE0\n"
484
            "MloYDzIwNTAwMjI1MTUyNDQxWjAAoDowODAfBgNVHSMEGDAWgBRKzxAvI4+rVVo/\n"
485
            "JzLigRznREyB+TAVBgNVHRQEDgIMXcr16yNys/gjeuCFMA0GCSqGSIb3DQEBCwUA\n"
486
            "A4IBgQCfxv/5REM/KUnzeVycph3dJr1Yrtxhc6pZmQ9pMzSW/nawLN3rUHm5oG44\n"
487
            "ZuQgjvzE4PnbU0/DNRu/4w3H58kgrctJHHXbbvkU3lf2ZZLh2wBl+EUh92+/COow\n"
488
            "ZyGB+jqj/XwB99hYUhrY6NLEWRz08kpgG6dnNMEU0uFqdQKWk0CQPnmgPRgDb8BW\n"
489
            "IuMBcjY7aF9XoCZFOqPYdEvUKzAo4QGCf7uJ7fNGS3LqvjaLjAHJseSr5/yR7Q9r\n"
490
            "nEdI38yKPbRj0tNHe7j+BbYg31C+X+AZZKJtlTg8GxYR3qfQio1kDgpZ3rQLzHY3\n"
491
            "ea2MLX/Kdx9cPSwh4KwlcDxQmQKoELb4EnZW1CScSBHi9HQyCBNyCkgkOBMGcJqz\n"
492
            "Ihq1dGeSf8eca9+Avk5kAQ3yjXK1TI2CDEi0msrXLr9XbgowXiOLLzR+rYkhQz+V\n"
493
            "RnIoBwjnrGoJoz636KS170SZCB9ARNs17WE4IvbJdZrTXNOGaVZCQUUpiLRj4ZSO\n"
494
            "Na/nobI=\n"
495
            "-----END X509 CRL-----";
496

497
         botan_x509_crl_t bytecrl;
1✔
498
         if(!TEST_FFI_INIT(botan_x509_crl_load, (&bytecrl, reinterpret_cast<const uint8_t*>(crl_string), 966))) {
2✔
499
            return;
×
500
         }
501

502
         botan_x509_crl_t crl;
1✔
503
         REQUIRE_FFI_OK(botan_x509_crl_load_file, (&crl, Test::data_file("x509/nist/root.crl").c_str()));
4✔
504

505
         botan_x509_cert_t cert1;
1✔
506
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&cert1, Test::data_file("x509/nist/test01/end.crt").c_str()));
4✔
507
         TEST_FFI_RC(-1, botan_x509_is_revoked, (crl, cert1));
1✔
508
         TEST_FFI_OK(botan_x509_cert_destroy, (cert1));
1✔
509

510
         botan_x509_cert_t cert2;
1✔
511
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&cert2, Test::data_file("x509/nist/test20/int.crt").c_str()));
4✔
512
         TEST_FFI_RC(0, botan_x509_is_revoked, (crl, cert2));
1✔
513
         TEST_FFI_RC(-1, botan_x509_is_revoked, (bytecrl, cert2));
1✔
514
         TEST_FFI_OK(botan_x509_cert_destroy, (cert2));
1✔
515

516
         TEST_FFI_OK(botan_x509_crl_destroy, (crl));
1✔
517
         TEST_FFI_OK(botan_x509_crl_destroy, (bytecrl));
2✔
518
      }
519
};
520

521
class FFI_Cert_Validation_Test final : public FFI_Test {
×
522
   public:
523
      std::string name() const override { return "FFI Cert Validation"; }
1✔
524

525
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
526
         botan_x509_cert_t root;
1✔
527
         int rc;
1✔
528

529
         if(!TEST_FFI_INIT(botan_x509_cert_load_file, (&root, Test::data_file("x509/nist/root.crt").c_str()))) {
4✔
530
            return;
×
531
         }
532

533
         botan_x509_cert_t end2;
1✔
534
         botan_x509_cert_t sub2;
1✔
535
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&end2, Test::data_file("x509/nist/test02/end.crt").c_str()));
4✔
536
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&sub2, Test::data_file("x509/nist/test02/int.crt").c_str()));
4✔
537

538
         TEST_FFI_RC(1, botan_x509_cert_verify, (&rc, end2, &sub2, 1, &root, 1, nullptr, 0, nullptr, 0));
1✔
539
         result.confirm("Validation failed", rc == 5002);
2✔
540
         result.test_eq("Validation status string", botan_x509_cert_validation_status(rc), "Signature error");
1✔
541

542
         TEST_FFI_RC(1, botan_x509_cert_verify, (&rc, end2, nullptr, 0, &root, 1, nullptr, 0, nullptr, 0));
1✔
543
         result.confirm("Validation failed", rc == 3000);
2✔
544
         result.test_eq(
1✔
545
            "Validation status string", botan_x509_cert_validation_status(rc), "Certificate issuer not found");
546

547
         botan_x509_cert_t end7;
1✔
548
         botan_x509_cert_t sub7;
1✔
549
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&end7, Test::data_file("x509/nist/test07/end.crt").c_str()));
4✔
550
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&sub7, Test::data_file("x509/nist/test07/int.crt").c_str()));
4✔
551

552
         botan_x509_cert_t subs[2] = {sub2, sub7};
1✔
553
         TEST_FFI_RC(1, botan_x509_cert_verify, (&rc, end7, subs, 2, &root, 1, nullptr, 0, nullptr, 0));
1✔
554
         result.confirm("Validation failed", rc == 1001);
2✔
555
         result.test_eq("Validation status string",
1✔
556
                        botan_x509_cert_validation_status(rc),
557
                        "Hash function used is considered too weak for security");
558

559
         TEST_FFI_RC(0, botan_x509_cert_verify, (&rc, end7, subs, 2, &root, 1, nullptr, 80, nullptr, 0));
1✔
560
         result.confirm("Validation passed", rc == 0);
2✔
561
         result.test_eq("Validation status string", botan_x509_cert_validation_status(rc), "Verified");
1✔
562

563
         TEST_FFI_RC(1,
1✔
564
                     botan_x509_cert_verify_with_crl,
565
                     (&rc, end7, subs, 2, nullptr, 0, nullptr, 0, "x509/farce", 0, nullptr, 0));
566
         result.confirm("Validation failed", rc == 3000);
2✔
567
         result.test_eq(
1✔
568
            "Validation status string", botan_x509_cert_validation_status(rc), "Certificate issuer not found");
569

570
         botan_x509_crl_t rootcrl;
1✔
571

572
         REQUIRE_FFI_OK(botan_x509_crl_load_file, (&rootcrl, Test::data_file("x509/nist/root.crl").c_str()));
4✔
573
         TEST_FFI_RC(
1✔
574
            0, botan_x509_cert_verify_with_crl, (&rc, end7, subs, 2, &root, 1, &rootcrl, 1, nullptr, 80, nullptr, 0));
575
         result.confirm("Validation passed", rc == 0);
2✔
576
         result.test_eq("Validation status string", botan_x509_cert_validation_status(rc), "Verified");
1✔
577

578
         botan_x509_cert_t end20;
1✔
579
         botan_x509_cert_t sub20;
1✔
580
         botan_x509_crl_t sub20crl;
1✔
581
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&end20, Test::data_file("x509/nist/test20/end.crt").c_str()));
4✔
582
         REQUIRE_FFI_OK(botan_x509_cert_load_file, (&sub20, Test::data_file("x509/nist/test20/int.crt").c_str()));
4✔
583
         REQUIRE_FFI_OK(botan_x509_crl_load_file, (&sub20crl, Test::data_file("x509/nist/test20/int.crl").c_str()));
4✔
584
         botan_x509_crl_t crls[2] = {sub20crl, rootcrl};
1✔
585
         TEST_FFI_RC(
1✔
586
            1, botan_x509_cert_verify_with_crl, (&rc, end20, &sub20, 1, &root, 1, crls, 2, nullptr, 80, nullptr, 0));
587
         result.confirm("Validation failed", rc == 5000);
2✔
588
         result.test_eq("Validation status string", botan_x509_cert_validation_status(rc), "Certificate is revoked");
1✔
589

590
         TEST_FFI_OK(botan_x509_cert_destroy, (end2));
1✔
591
         TEST_FFI_OK(botan_x509_cert_destroy, (sub2));
1✔
592
         TEST_FFI_OK(botan_x509_cert_destroy, (end7));
1✔
593
         TEST_FFI_OK(botan_x509_cert_destroy, (sub7));
1✔
594
         TEST_FFI_OK(botan_x509_cert_destroy, (end20));
1✔
595
         TEST_FFI_OK(botan_x509_cert_destroy, (sub20));
1✔
596
         TEST_FFI_OK(botan_x509_crl_destroy, (sub20crl));
1✔
597
         TEST_FFI_OK(botan_x509_cert_destroy, (root));
1✔
598
         TEST_FFI_OK(botan_x509_crl_destroy, (rootcrl));
2✔
599
      }
600
};
601

602
class FFI_ECDSA_Certificate_Test final : public FFI_Test {
×
603
   public:
604
      std::string name() const override { return "FFI ECDSA cert"; }
1✔
605

606
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
607
         botan_x509_cert_t cert;
1✔
608
         if(TEST_FFI_INIT(botan_x509_cert_load_file,
4✔
609
                          (&cert, Test::data_file("x509/ecc/CSCA.CSCA.csca-germany.1.crt").c_str()))) {
610
            size_t date_len = 0;
1✔
611
            TEST_FFI_RC(
1✔
612
               BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_x509_cert_get_time_starts, (cert, nullptr, &date_len));
613

614
            date_len = 8;
1✔
615
            TEST_FFI_RC(
1✔
616
               BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_x509_cert_get_time_starts, (cert, nullptr, &date_len));
617

618
            std::string date(date_len - 1, '0');
1✔
619
            TEST_FFI_OK(botan_x509_cert_get_time_starts, (cert, &date[0], &date_len));
1✔
620
            result.test_eq("cert valid from", date, "070719152718Z");
2✔
621

622
            date_len = 0;
1✔
623
            TEST_FFI_RC(
1✔
624
               BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_x509_cert_get_time_expires, (cert, nullptr, &date_len));
625

626
            date.resize(date_len - 1);
1✔
627
            TEST_FFI_OK(botan_x509_cert_get_time_expires, (cert, &date[0], &date_len));
1✔
628
            result.test_eq("cert valid until", date, "280119151800Z");
2✔
629

630
            uint64_t not_before = 0;
1✔
631
            TEST_FFI_OK(botan_x509_cert_not_before, (cert, &not_before));
1✔
632
            result.confirm("cert not before", not_before == 1184858838);
2✔
633

634
            uint64_t not_after = 0;
1✔
635
            TEST_FFI_OK(botan_x509_cert_not_after, (cert, &not_after));
1✔
636
            result.confirm("cert not after", not_after == 1831907880);
2✔
637

638
            size_t serial_len = 0;
1✔
639
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
640
                        botan_x509_cert_get_serial_number,
641
                        (cert, nullptr, &serial_len));
642

643
            std::vector<uint8_t> serial(serial_len);
1✔
644
            TEST_FFI_OK(botan_x509_cert_get_serial_number, (cert, serial.data(), &serial_len));
1✔
645
            result.test_eq("cert serial length", serial.size(), 1);
1✔
646
            result.test_int_eq(serial[0], 1, "cert serial");
1✔
647

648
            size_t fingerprint_len = 0;
1✔
649
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
650
                        botan_x509_cert_get_fingerprint,
651
                        (cert, "SHA-256", nullptr, &fingerprint_len));
652

653
            std::vector<uint8_t> fingerprint(fingerprint_len);
1✔
654
            TEST_FFI_OK(botan_x509_cert_get_fingerprint, (cert, "SHA-256", fingerprint.data(), &fingerprint_len));
1✔
655
            result.test_eq(
1✔
656
               "cert fingerprint",
657
               reinterpret_cast<const char*>(fingerprint.data()),
1✔
658
               "3B:6C:99:1C:D6:5A:51:FC:EB:17:E3:AA:F6:3C:1A:DA:14:1F:82:41:30:6F:64:EE:FF:63:F3:1F:D6:07:14:9F");
659

660
            size_t key_id_len = 0;
1✔
661
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
662
                        botan_x509_cert_get_authority_key_id,
663
                        (cert, nullptr, &key_id_len));
664

665
            std::vector<uint8_t> key_id(key_id_len);
1✔
666
            TEST_FFI_OK(botan_x509_cert_get_authority_key_id, (cert, key_id.data(), &key_id_len));
1✔
667
            result.test_eq("cert authority key id",
3✔
668
                           Botan::hex_encode(key_id.data(), key_id.size(), true),
2✔
669
                           "0096452DE588F966C4CCDF161DD1F3F5341B71E7");
670

671
            key_id_len = 0;
1✔
672
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
673
                        botan_x509_cert_get_subject_key_id,
674
                        (cert, nullptr, &key_id_len));
675

676
            key_id.resize(key_id_len);
1✔
677
            TEST_FFI_OK(botan_x509_cert_get_subject_key_id, (cert, key_id.data(), &key_id_len));
1✔
678
            result.test_eq("cert subject key id",
3✔
679
                           Botan::hex_encode(key_id.data(), key_id.size(), true),
2✔
680
                           "0096452DE588F966C4CCDF161DD1F3F5341B71E7");
681

682
            size_t pubkey_len = 0;
1✔
683
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
684
                        botan_x509_cert_get_public_key_bits,
685
                        (cert, nullptr, &pubkey_len));
686

687
            std::vector<uint8_t> pubkey(pubkey_len);
1✔
688
            TEST_FFI_OK(botan_x509_cert_get_public_key_bits, (cert, pubkey.data(), &pubkey_len));
1✔
689

690
            botan_pubkey_t pub;
1✔
691
            if(TEST_FFI_OK(botan_x509_cert_get_public_key, (cert, &pub))) {
2✔
692
               TEST_FFI_RC(1, botan_pubkey_ecc_key_used_explicit_encoding, (pub));
1✔
693
               TEST_FFI_OK(botan_pubkey_destroy, (pub));
2✔
694
            }
695

696
            size_t dn_len = 0;
1✔
697
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
698
                        botan_x509_cert_get_issuer_dn,
699
                        (cert, "Name", 0, nullptr, &dn_len));
700

701
            std::vector<uint8_t> dn(dn_len);
1✔
702
            TEST_FFI_OK(botan_x509_cert_get_issuer_dn, (cert, "Name", 0, dn.data(), &dn_len));
1✔
703
            result.test_eq("issuer dn", reinterpret_cast<const char*>(dn.data()), "csca-germany");
1✔
704

705
            dn_len = 0;
1✔
706
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
707
                        botan_x509_cert_get_subject_dn,
708
                        (cert, "Name", 0, nullptr, &dn_len));
709

710
            dn.resize(dn_len);
1✔
711
            TEST_FFI_OK(botan_x509_cert_get_subject_dn, (cert, "Name", 0, dn.data(), &dn_len));
1✔
712
            result.test_eq("subject dn", reinterpret_cast<const char*>(dn.data()), "csca-germany");
1✔
713

714
            size_t printable_len = 0;
1✔
715
            TEST_FFI_RC(
1✔
716
               BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_x509_cert_to_string, (cert, nullptr, &printable_len));
717

718
            std::string printable(printable_len - 1, '0');
1✔
719
            TEST_FFI_OK(botan_x509_cert_to_string, (cert, &printable[0], &printable_len));
1✔
720

721
            TEST_FFI_RC(0, botan_x509_cert_allowed_usage, (cert, KEY_CERT_SIGN));
1✔
722
            TEST_FFI_RC(0, botan_x509_cert_allowed_usage, (cert, CRL_SIGN));
1✔
723
            TEST_FFI_RC(1, botan_x509_cert_allowed_usage, (cert, DIGITAL_SIGNATURE));
1✔
724

725
            TEST_FFI_OK(botan_x509_cert_destroy, (cert));
2✔
726
         }
6✔
727
      }
1✔
728
};
729

730
class FFI_PKCS_Hashid_Test final : public FFI_Test {
×
731
   public:
732
      std::string name() const override { return "FFI PKCS hash id"; }
1✔
733

734
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
735
         std::vector<uint8_t> hash_id(64);
1✔
736
         size_t hash_id_len = hash_id.size();
1✔
737

738
         if(TEST_FFI_INIT(botan_pkcs_hash_id, ("SHA-256", hash_id.data(), &hash_id_len))) {
2✔
739
            result.test_eq("Expected SHA-256 PKCS hash id len", hash_id_len, 19);
1✔
740

741
            hash_id.resize(hash_id_len);
1✔
742
            result.test_eq("Expected SHA_256 PKCS hash id", hash_id, "3031300D060960864801650304020105000420");
1✔
743

744
            hash_id_len = 3;  // too short
1✔
745
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
2✔
746
                        botan_pkcs_hash_id,
747
                        ("SHA-256", hash_id.data(), &hash_id_len));
748
         }
749
      }
1✔
750
};
751

752
class FFI_CBC_Cipher_Test final : public FFI_Test {
×
753
   public:
754
      std::string name() const override { return "FFI CBC cipher"; }
1✔
755

756
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
757
         botan_cipher_t cipher_encrypt, cipher_decrypt;
1✔
758

759
         if(TEST_FFI_INIT(botan_cipher_init, (&cipher_encrypt, "AES-128/CBC/PKCS7", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
2✔
760
            size_t min_keylen = 0;
1✔
761
            size_t max_keylen = 0;
1✔
762
            TEST_FFI_OK(botan_cipher_query_keylen, (cipher_encrypt, &min_keylen, &max_keylen));
1✔
763
            result.test_int_eq(min_keylen, 16, "Min key length");
1✔
764
            result.test_int_eq(max_keylen, 16, "Max key length");
1✔
765

766
            // from https://github.com/geertj/bluepass/blob/master/tests/vectors/aes-cbc-pkcs7.txt
767
            const std::vector<uint8_t> plaintext =
1✔
768
               Botan::hex_decode("0397f4f6820b1f9386f14403be5ac16e50213bd473b4874b9bcbf5f318ee686b1d");
1✔
769
            const std::vector<uint8_t> symkey = Botan::hex_decode("898be9cc5004ed0fa6e117c9a3099d31");
1✔
770
            const std::vector<uint8_t> nonce = Botan::hex_decode("9dea7621945988f96491083849b068df");
1✔
771
            const std::vector<uint8_t> exp_ciphertext = Botan::hex_decode(
1✔
772
               "e232cd6ef50047801ee681ec30f61d53cfd6b0bca02fd03c1b234baa10ea82ac9dab8b960926433a19ce6dea08677e34");
1✔
773

774
            size_t output_written = 0;
1✔
775
            size_t input_consumed = 0;
1✔
776

777
            // Test that after clear or final the object can be reused
778
            for(size_t r = 0; r != 2; ++r) {
3✔
779
               size_t ctext_len;
2✔
780
               TEST_FFI_OK(botan_cipher_output_length, (cipher_encrypt, plaintext.size(), &ctext_len));
2✔
781
               result.test_eq("Expected size of padded message", ctext_len, plaintext.size() + 15);
2✔
782
               std::vector<uint8_t> ciphertext(ctext_len);
2✔
783

784
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2✔
785
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
786
               TEST_FFI_OK(botan_cipher_update,
2✔
787
                           (cipher_encrypt,
788
                            0,
789
                            ciphertext.data(),
790
                            ciphertext.size(),
791
                            &output_written,
792
                            plaintext.data(),
793
                            plaintext.size(),
794
                            &input_consumed));
795
               TEST_FFI_OK(botan_cipher_clear, (cipher_encrypt));
2✔
796

797
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2✔
798
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
799
               TEST_FFI_OK(botan_cipher_update,
2✔
800
                           (cipher_encrypt,
801
                            BOTAN_CIPHER_UPDATE_FLAG_FINAL,
802
                            ciphertext.data(),
803
                            ciphertext.size(),
804
                            &output_written,
805
                            plaintext.data(),
806
                            plaintext.size(),
807
                            &input_consumed));
808

809
               ciphertext.resize(output_written);
2✔
810
               result.test_eq("AES/CBC ciphertext", ciphertext, exp_ciphertext);
2✔
811

812
               if(TEST_FFI_OK(botan_cipher_init, (&cipher_decrypt, "AES-128/CBC", BOTAN_CIPHER_INIT_FLAG_DECRYPT))) {
4✔
813
                  size_t ptext_len;
2✔
814
                  TEST_FFI_OK(botan_cipher_output_length, (cipher_decrypt, ciphertext.size(), &ptext_len));
2✔
815
                  std::vector<uint8_t> decrypted(ptext_len);
2✔
816

817
                  TEST_FFI_OK(botan_cipher_set_key, (cipher_decrypt, symkey.data(), symkey.size()));
2✔
818
                  TEST_FFI_OK(botan_cipher_start, (cipher_decrypt, nonce.data(), nonce.size()));
2✔
819
                  TEST_FFI_OK(botan_cipher_update,
2✔
820
                              (cipher_decrypt,
821
                               BOTAN_CIPHER_UPDATE_FLAG_FINAL,
822
                               decrypted.data(),
823
                               decrypted.size(),
824
                               &output_written,
825
                               ciphertext.data(),
826
                               ciphertext.size(),
827
                               &input_consumed));
828

829
                  decrypted.resize(output_written);
2✔
830

831
                  result.test_eq("AES/CBC plaintext", decrypted, plaintext);
2✔
832

833
                  TEST_FFI_OK(botan_cipher_destroy, (cipher_decrypt));
4✔
834
               }
2✔
835
            }
2✔
836

837
            TEST_FFI_OK(botan_cipher_destroy, (cipher_encrypt));
2✔
838
         }
4✔
839
      }
1✔
840
};
841

842
class FFI_GCM_Test final : public FFI_Test {
×
843
   public:
844
      std::string name() const override { return "FFI GCM"; }
1✔
845

846
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
847
         botan_cipher_t cipher_encrypt, cipher_decrypt;
1✔
848

849
         if(TEST_FFI_INIT(botan_cipher_init, (&cipher_encrypt, "AES-128/GCM", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
2✔
850
            char namebuf[18];
1✔
851
            size_t name_len = 15;
1✔
852
            TEST_FFI_FAIL("output buffer too short", botan_cipher_name, (cipher_encrypt, namebuf, &name_len));
2✔
853
            result.test_eq("name len", name_len, 16);
1✔
854

855
            name_len = sizeof(namebuf);
1✔
856
            if(TEST_FFI_OK(botan_cipher_name, (cipher_encrypt, namebuf, &name_len))) {
2✔
857
               result.test_eq("name len", name_len, 16);
1✔
858
               result.test_eq("name", std::string(namebuf), "AES-128/GCM(16)");
2✔
859
            }
860

861
            size_t min_keylen = 0;
1✔
862
            size_t max_keylen = 0;
1✔
863
            size_t nonce_len = 0;
1✔
864
            size_t tag_len = 0;
1✔
865

866
            TEST_FFI_OK(botan_cipher_query_keylen, (cipher_encrypt, &min_keylen, &max_keylen));
1✔
867
            result.test_int_eq(min_keylen, 16, "Min key length");
1✔
868
            result.test_int_eq(max_keylen, 16, "Max key length");
1✔
869

870
            TEST_FFI_OK(botan_cipher_get_default_nonce_length, (cipher_encrypt, &nonce_len));
1✔
871
            result.test_int_eq(nonce_len, 12, "Expected default GCM nonce length");
1✔
872

873
            TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_encrypt, &tag_len));
1✔
874
            result.test_int_eq(tag_len, 16, "Expected GCM tag length");
1✔
875

876
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 12));
1✔
877
            // GCM accepts any nonce size except zero
878
            TEST_FFI_RC(0, botan_cipher_valid_nonce_length, (cipher_encrypt, 0));
1✔
879
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 1));
1✔
880
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 100009));
1✔
881

882
            // NIST test vector
883
            const std::vector<uint8_t> plaintext = Botan::hex_decode(
1✔
884
               "D9313225F88406E5A55909C5AFF5269A86A7A9531534F7DA2E4C303D8A318A721C3C0C95956809532FCF0E2449A6B525B16AEDF5AA0DE657BA637B39");
1✔
885

886
            const std::vector<uint8_t> symkey = Botan::hex_decode("FEFFE9928665731C6D6A8F9467308308");
1✔
887
            const std::vector<uint8_t> nonce = Botan::hex_decode("CAFEBABEFACEDBADDECAF888");
1✔
888
            const std::vector<uint8_t> exp_ciphertext = Botan::hex_decode(
1✔
889
               "42831EC2217774244B7221B784D0D49CE3AA212F2C02A4E035C17E2329ACA12E21D514B25466931C7D8F6A5AAC84AA051BA30B396A0AAC973D58E0915BC94FBC3221A5DB94FAE95AE7121A47");
1✔
890
            const std::vector<uint8_t> aad = Botan::hex_decode("FEEDFACEDEADBEEFFEEDFACEDEADBEEFABADDAD2");
1✔
891

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

894
            size_t output_written = 0;
1✔
895
            size_t input_consumed = 0;
1✔
896

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

901
               // First use a nonce of the AAD, and ensure reset works
902
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, aad.data(), aad.size()));
2✔
903
               TEST_FFI_OK(botan_cipher_reset, (cipher_encrypt));
2✔
904

905
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
906
               TEST_FFI_OK(botan_cipher_update,
2✔
907
                           (cipher_encrypt,
908
                            0,
909
                            ciphertext.data(),
910
                            ciphertext.size(),
911
                            &output_written,
912
                            plaintext.data(),
913
                            plaintext.size(),
914
                            &input_consumed));
915
               TEST_FFI_OK(botan_cipher_clear, (cipher_encrypt));
2✔
916

917
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2✔
918
               TEST_FFI_OK(botan_cipher_set_associated_data, (cipher_encrypt, aad.data(), aad.size()));
2✔
919
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
920
               TEST_FFI_OK(botan_cipher_update,
2✔
921
                           (cipher_encrypt,
922
                            BOTAN_CIPHER_UPDATE_FLAG_FINAL,
923
                            ciphertext.data(),
924
                            ciphertext.size(),
925
                            &output_written,
926
                            plaintext.data(),
927
                            plaintext.size(),
928
                            &input_consumed));
929

930
               ciphertext.resize(output_written);
2✔
931
               result.test_eq("AES/GCM ciphertext", ciphertext, exp_ciphertext);
2✔
932

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

936
                  TEST_FFI_OK(botan_cipher_set_key, (cipher_decrypt, symkey.data(), symkey.size()));
2✔
937
                  TEST_FFI_OK(botan_cipher_set_associated_data, (cipher_decrypt, aad.data(), aad.size()));
2✔
938
                  TEST_FFI_OK(botan_cipher_start, (cipher_decrypt, nonce.data(), nonce.size()));
2✔
939
                  TEST_FFI_OK(botan_cipher_update,
2✔
940
                              (cipher_decrypt,
941
                               BOTAN_CIPHER_UPDATE_FLAG_FINAL,
942
                               decrypted.data(),
943
                               decrypted.size(),
944
                               &output_written,
945
                               ciphertext.data(),
946
                               ciphertext.size(),
947
                               &input_consumed));
948

949
                  result.test_int_eq(input_consumed, ciphertext.size(), "All input consumed");
2✔
950
                  result.test_int_eq(output_written, decrypted.size(), "Expected output size produced");
2✔
951
                  result.test_eq("AES/GCM plaintext", decrypted, plaintext);
2✔
952

953
                  TEST_FFI_OK(botan_cipher_destroy, (cipher_decrypt));
4✔
954
               }
2✔
955
            }
956

957
            TEST_FFI_OK(botan_cipher_destroy, (cipher_encrypt));
2✔
958
         }
6✔
959
      }
1✔
960
};
961

962
class FFI_EAX_Test final : public FFI_Test {
×
963
   public:
964
      std::string name() const override { return "FFI EAX"; }
1✔
965

966
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
967
         botan_cipher_t cipher_encrypt, cipher_decrypt;
1✔
968

969
         if(TEST_FFI_INIT(botan_cipher_init, (&cipher_encrypt, "AES-128/EAX", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
2✔
970
            size_t min_keylen = 0;
1✔
971
            size_t max_keylen = 0;
1✔
972
            size_t mod_keylen = 0;
1✔
973
            size_t nonce_len = 0;
1✔
974
            size_t tag_len = 0;
1✔
975

976
            TEST_FFI_OK(botan_cipher_query_keylen, (cipher_encrypt, &min_keylen, &max_keylen));
1✔
977
            result.test_int_eq(min_keylen, 16, "Min key length");
1✔
978
            result.test_int_eq(max_keylen, 16, "Max key length");
1✔
979

980
            TEST_FFI_OK(botan_cipher_get_keyspec, (cipher_encrypt, &min_keylen, &max_keylen, &mod_keylen));
1✔
981
            result.test_int_eq(min_keylen, 16, "Min key length");
1✔
982
            result.test_int_eq(max_keylen, 16, "Max key length");
1✔
983
            result.test_int_eq(mod_keylen, 1, "Mod key length");
1✔
984

985
            TEST_FFI_OK(botan_cipher_get_default_nonce_length, (cipher_encrypt, &nonce_len));
1✔
986
            result.test_int_eq(nonce_len, 12, "Expected default EAX nonce length");
1✔
987

988
            TEST_FFI_OK(botan_cipher_get_tag_length, (cipher_encrypt, &tag_len));
1✔
989
            result.test_int_eq(tag_len, 16, "Expected EAX tag length");
1✔
990

991
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 12));
1✔
992
            // EAX accepts any nonce size...
993
            TEST_FFI_RC(1, botan_cipher_valid_nonce_length, (cipher_encrypt, 0));
1✔
994

995
            const std::vector<uint8_t> plaintext =
1✔
996
               Botan::hex_decode("0000000000000000000000000000000011111111111111111111111111111111");
1✔
997
            const std::vector<uint8_t> symkey = Botan::hex_decode("000102030405060708090a0b0c0d0e0f");
1✔
998
            const std::vector<uint8_t> nonce = Botan::hex_decode("3c8cc2970a008f75cc5beae2847258c2");
1✔
999
            const std::vector<uint8_t> exp_ciphertext = Botan::hex_decode(
1✔
1000
               "3c441f32ce07822364d7a2990e50bb13d7b02a26969e4a937e5e9073b0d9c968db90bdb3da3d00afd0fc6a83551da95e");
1✔
1001

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

1004
            size_t output_written = 0;
1✔
1005
            size_t input_consumed = 0;
1✔
1006

1007
            // Test that after clear or final the object can be reused
1008
            for(size_t r = 0; r != 2; ++r) {
3✔
1009
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2✔
1010
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
1011
               TEST_FFI_OK(botan_cipher_update,
2✔
1012
                           (cipher_encrypt,
1013
                            0,
1014
                            ciphertext.data(),
1015
                            ciphertext.size(),
1016
                            &output_written,
1017
                            plaintext.data(),
1018
                            plaintext.size(),
1019
                            &input_consumed));
1020
               TEST_FFI_OK(botan_cipher_clear, (cipher_encrypt));
2✔
1021

1022
               TEST_FFI_OK(botan_cipher_set_key, (cipher_encrypt, symkey.data(), symkey.size()));
2✔
1023
               TEST_FFI_OK(botan_cipher_start, (cipher_encrypt, nonce.data(), nonce.size()));
2✔
1024
               TEST_FFI_OK(botan_cipher_update,
2✔
1025
                           (cipher_encrypt,
1026
                            BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1027
                            ciphertext.data(),
1028
                            ciphertext.size(),
1029
                            &output_written,
1030
                            plaintext.data(),
1031
                            plaintext.size(),
1032
                            &input_consumed));
1033

1034
               ciphertext.resize(output_written);
2✔
1035
               result.test_eq("AES/EAX ciphertext", ciphertext, exp_ciphertext);
2✔
1036

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

1040
                  TEST_FFI_OK(botan_cipher_set_key, (cipher_decrypt, symkey.data(), symkey.size()));
2✔
1041
                  TEST_FFI_OK(botan_cipher_start, (cipher_decrypt, nonce.data(), nonce.size()));
2✔
1042
                  TEST_FFI_OK(botan_cipher_update,
2✔
1043
                              (cipher_decrypt,
1044
                               BOTAN_CIPHER_UPDATE_FLAG_FINAL,
1045
                               decrypted.data(),
1046
                               decrypted.size(),
1047
                               &output_written,
1048
                               ciphertext.data(),
1049
                               ciphertext.size(),
1050
                               &input_consumed));
1051

1052
                  result.test_int_eq(input_consumed, ciphertext.size(), "All input consumed");
2✔
1053
                  result.test_int_eq(output_written, decrypted.size(), "Expected output size produced");
2✔
1054
                  result.test_eq("AES/EAX plaintext", decrypted, plaintext);
2✔
1055

1056
                  TEST_FFI_OK(botan_cipher_destroy, (cipher_decrypt));
4✔
1057
               }
2✔
1058
            }
1059

1060
            TEST_FFI_OK(botan_cipher_destroy, (cipher_encrypt));
2✔
1061
         }
5✔
1062
      }
1✔
1063
};
1064

1065
class FFI_StreamCipher_Test final : public FFI_Test {
×
1066
   public:
1067
      std::string name() const override { return "FFI stream ciphers"; }
1✔
1068

1069
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1070
         botan_cipher_t ctr;
1✔
1071

1072
         if(TEST_FFI_INIT(botan_cipher_init, (&ctr, "AES-128/CTR-BE", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) {
2✔
1073
            const std::vector<uint8_t> key = Botan::hex_decode("2B7E151628AED2A6ABF7158809CF4F3C");
1✔
1074
            const std::vector<uint8_t> nonce = Botan::hex_decode("F0F1F2F3F4F5F6F7F8F9FAFBFCFDFF");
1✔
1075
            const std::vector<uint8_t> pt = Botan::hex_decode(
1✔
1076
               "AE2D8A571E03AC9C9EB76FAC45AF8E5130C81C46A35CE411E5FBC1191A0A52EFF69F2445DF4F9B17AD2B417BE66C3710");
1✔
1077
            const std::vector<uint8_t> exp_ct = Botan::hex_decode(
1✔
1078
               "9806F66B7970FDFF8617187BB9FFFDFF5AE4DF3EDBD5D35E5B4F09020DB03EAB1E031DDA2FBE03D1792170A0F3009CEE");
1✔
1079

1080
            std::vector<uint8_t> ct(pt.size());
1✔
1081

1082
            size_t input_consumed = 0;
1✔
1083
            size_t output_written = 0;
1✔
1084

1085
            TEST_FFI_OK(botan_cipher_set_key, (ctr, key.data(), key.size()));
1✔
1086
            TEST_FFI_OK(botan_cipher_start, (ctr, nonce.data(), nonce.size()));
1✔
1087

1088
            // Test partial updates...
1089
            TEST_FFI_OK(botan_cipher_update,
1✔
1090
                        (ctr, 0, ct.data(), ct.size(), &output_written, pt.data(), 5, &input_consumed));
1091

1092
            result.test_int_eq(output_written, 5, "Expected output written");
1✔
1093
            result.test_int_eq(input_consumed, 5, "Expected input consumed");
1✔
1094

1095
            TEST_FFI_OK(botan_cipher_update,
1✔
1096
                        (ctr, 0, &ct[5], ct.size() - 5, &output_written, &pt[5], pt.size() - 5, &input_consumed));
1097

1098
            result.test_int_eq(output_written, ct.size() - 5, "Expected output written");
1✔
1099
            result.test_int_eq(input_consumed, pt.size() - 5, "Expected input consumed");
1✔
1100
            result.test_eq("AES-128/CTR ciphertext", ct, exp_ct);
1✔
1101

1102
            TEST_FFI_OK(botan_cipher_destroy, (ctr));
2✔
1103
         }
5✔
1104
      }
1✔
1105
};
1106

1107
class FFI_HashFunction_Test final : public FFI_Test {
×
1108
   public:
1109
      std::string name() const override { return "FFI hash"; }
1✔
1110

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

1114
         botan_hash_t hash;
1✔
1115
         TEST_FFI_FAIL("invalid hash name", botan_hash_init, (&hash, "SHA-255", 0));
2✔
1116
         TEST_FFI_FAIL("invalid flags", botan_hash_init, (&hash, "SHA-256", 1));
2✔
1117

1118
         if(TEST_FFI_INIT(botan_hash_init, (&hash, "SHA-256", 0))) {
1✔
1119
            char namebuf[10];
1✔
1120
            size_t name_len = 7;
1✔
1121
            TEST_FFI_FAIL("output buffer too short", botan_hash_name, (hash, namebuf, &name_len));
2✔
1122
            result.test_eq("name len", name_len, 8);
1✔
1123

1124
            name_len = sizeof(namebuf);
1✔
1125
            if(TEST_FFI_OK(botan_hash_name, (hash, namebuf, &name_len))) {
1✔
1126
               result.test_eq("name len", name_len, 8);
1✔
1127
               result.test_eq("name", std::string(namebuf), "SHA-256");
2✔
1128
            }
1129

1130
            size_t block_size;
1✔
1131
            if(TEST_FFI_OK(botan_hash_block_size, (hash, &block_size))) {
2✔
1132
               result.test_eq("hash block size", block_size, 64);
2✔
1133
            }
1134

1135
            size_t output_len;
1✔
1136
            if(TEST_FFI_OK(botan_hash_output_length, (hash, &output_len))) {
2✔
1137
               result.test_eq("hash output length", output_len, 32);
1✔
1138

1139
               std::vector<uint8_t> outbuf(output_len);
1✔
1140

1141
               // Test that after clear or final the object can be reused
1142
               for(size_t r = 0; r != 2; ++r) {
3✔
1143
                  TEST_FFI_OK(botan_hash_update, (hash, reinterpret_cast<const uint8_t*>(input_str), 1));
2✔
1144
                  TEST_FFI_OK(botan_hash_clear, (hash));
2✔
1145

1146
                  TEST_FFI_OK(botan_hash_update,
2✔
1147
                              (hash, reinterpret_cast<const uint8_t*>(input_str), std::strlen(input_str)));
1148
                  TEST_FFI_OK(botan_hash_final, (hash, outbuf.data()));
2✔
1149

1150
                  result.test_eq(
4✔
1151
                     "SHA-256 output", outbuf, "B5D4045C3F466FA91FE2CC6ABE79232A1A57CDF104F7A26E716E0A1E2789DF78");
1152
               }
1153

1154
               // Test botan_hash_copy_state
1155
               const char* msg = "message digest";
1✔
1156
               const char* expected = "F7846F55CF23E14EEBEAB5B4E1550CAD5B509E3348FBC4EFA3A1413D393CB650";
1✔
1157
               TEST_FFI_OK(botan_hash_clear, (hash));
1✔
1158
               TEST_FFI_OK(botan_hash_update, (hash, reinterpret_cast<const uint8_t*>(&msg[0]), 1));
1✔
1159
               botan_hash_t fork;
1✔
1160
               if(TEST_FFI_OK(botan_hash_copy_state, (&fork, hash))) {
2✔
1161
                  TEST_FFI_OK(botan_hash_update,
1✔
1162
                              (fork, reinterpret_cast<const uint8_t*>(&msg[1]), std::strlen(msg) - 2));
1163

1164
                  TEST_FFI_OK(botan_hash_update,
1✔
1165
                              (hash, reinterpret_cast<const uint8_t*>(&msg[1]), std::strlen(msg) - 1));
1166
                  TEST_FFI_OK(botan_hash_final, (hash, outbuf.data()));
1✔
1167
                  result.test_eq("hashing split", outbuf, expected);
1✔
1168

1169
                  TEST_FFI_OK(botan_hash_update,
1✔
1170
                              (fork, reinterpret_cast<const uint8_t*>(&msg[std::strlen(msg) - 1]), 1));
1171
                  TEST_FFI_OK(botan_hash_final, (fork, outbuf.data()));
1✔
1172
                  result.test_eq("hashing split", outbuf, expected);
1✔
1173

1174
                  TEST_FFI_OK(botan_hash_destroy, (fork));
2✔
1175
               }
1176
            }
1✔
1177

1178
            TEST_FFI_OK(botan_hash_destroy, (hash));
2✔
1179
         }
1180
      }
1✔
1181
};
1182

1183
class FFI_MAC_Test final : public FFI_Test {
×
1184
   public:
1185
      std::string name() const override { return "FFI MAC"; }
1✔
1186

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

1190
         // MAC test
1191
         botan_mac_t mac;
1✔
1192
         TEST_FFI_FAIL("bad flag", botan_mac_init, (&mac, "HMAC(SHA-256)", 1));
2✔
1193
         TEST_FFI_FAIL("bad name", botan_mac_init, (&mac, "HMAC(SHA-259)", 0));
2✔
1194

1195
         if(TEST_FFI_INIT(botan_mac_init, (&mac, "HMAC(SHA-256)", 0))) {
1✔
1196
            char namebuf[16];
1✔
1197
            size_t name_len = 13;
1✔
1198
            TEST_FFI_FAIL("output buffer too short", botan_mac_name, (mac, namebuf, &name_len));
2✔
1199
            result.test_eq("name len", name_len, 14);
1✔
1200

1201
            name_len = sizeof(namebuf);
1✔
1202
            if(TEST_FFI_OK(botan_mac_name, (mac, namebuf, &name_len))) {
1✔
1203
               result.test_eq("name len", name_len, 14);
1✔
1204
               result.test_eq("name", std::string(namebuf), "HMAC(SHA-256)");
2✔
1205
            }
1206

1207
            size_t min_keylen = 0, max_keylen = 0, mod_keylen = 0;
1✔
1208
            TEST_FFI_RC(0, botan_mac_get_keyspec, (mac, nullptr, nullptr, nullptr));
1✔
1209
            TEST_FFI_RC(0, botan_mac_get_keyspec, (mac, &min_keylen, nullptr, nullptr));
1✔
1210
            TEST_FFI_RC(0, botan_mac_get_keyspec, (mac, nullptr, &max_keylen, nullptr));
1✔
1211
            TEST_FFI_RC(0, botan_mac_get_keyspec, (mac, nullptr, nullptr, &mod_keylen));
1✔
1212

1213
            result.test_eq("Expected min keylen", min_keylen, 0);
1✔
1214
            result.test_eq("Expected max keylen", max_keylen, 4096);
1✔
1215
            result.test_eq("Expected mod keylen", mod_keylen, 1);
1✔
1216

1217
            size_t output_len;
1✔
1218
            if(TEST_FFI_OK(botan_mac_output_length, (mac, &output_len))) {
2✔
1219
               result.test_eq("MAC output length", output_len, 32);
1✔
1220

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

1224
               // Test that after clear or final the object can be reused
1225
               for(size_t r = 0; r != 2; ++r) {
3✔
1226
                  TEST_FFI_OK(botan_mac_set_key, (mac, mac_key, sizeof(mac_key)));
2✔
1227
                  TEST_FFI_OK(botan_mac_update,
2✔
1228
                              (mac, reinterpret_cast<const uint8_t*>(input_str), std::strlen(input_str)));
1229
                  TEST_FFI_OK(botan_mac_clear, (mac));
2✔
1230

1231
                  TEST_FFI_OK(botan_mac_set_key, (mac, mac_key, sizeof(mac_key)));
2✔
1232
                  TEST_FFI_OK(botan_mac_update,
2✔
1233
                              (mac, reinterpret_cast<const uint8_t*>(input_str), std::strlen(input_str)));
1234
                  TEST_FFI_OK(botan_mac_final, (mac, outbuf.data()));
2✔
1235

1236
                  result.test_eq(
4✔
1237
                     "HMAC output", outbuf, "1A82EEA984BC4A7285617CC0D05F1FE1D6C96675924A81BC965EE8FF7B0697A7");
1238
               }
1239
            }
1✔
1240

1241
            TEST_FFI_OK(botan_mac_destroy, (mac));
2✔
1242
         }
1243
      }
1✔
1244
};
1245

1246
class FFI_Scrypt_Test final : public FFI_Test {
×
1247
   public:
1248
      std::string name() const override { return "FFI Scrypt"; }
1✔
1249

1250
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1251
         std::vector<uint8_t> output(24);
1✔
1252
         const uint8_t salt[8] = {0};
1✔
1253
         const char* pass = "password";
1✔
1254

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

1258
            size_t N, r, p;
1✔
1259
            TEST_FFI_OK(botan_pwdhash_timed,
1✔
1260
                        ("Scrypt", 50, &r, &p, &N, output.data(), output.size(), "bunny", 5, salt, sizeof(salt)));
1261

1262
            std::vector<uint8_t> cmp(output.size());
1✔
1263

1264
            TEST_FFI_OK(botan_pwdhash, ("Scrypt", N, r, p, cmp.data(), cmp.size(), "bunny", 5, salt, sizeof(salt)));
1✔
1265
            result.test_eq("recomputed scrypt", cmp, output);
2✔
1266
         }
1✔
1267
      }
1✔
1268
};
1269

1270
class FFI_KDF_Test final : public FFI_Test {
×
1271
   public:
1272
      std::string name() const override { return "FFI KDF"; }
1✔
1273

1274
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
1275
         std::vector<uint8_t> outbuf;
1✔
1276

1277
         const std::string passphrase = "ltexmfeyylmlbrsyikaw";
1✔
1278

1279
         const std::vector<uint8_t> pbkdf_salt = Botan::hex_decode("ED1F39A0A7F3889AAF7E60743B3BC1CC2C738E60");
1✔
1280
         const size_t pbkdf_out_len = 10;
1✔
1281
         const size_t pbkdf_iterations = 1000;
1✔
1282

1283
         outbuf.resize(pbkdf_out_len);
1✔
1284

1285
         if(TEST_FFI_INIT(botan_pbkdf,
1✔
1286
                          ("PBKDF2(SHA-1)",
1287
                           outbuf.data(),
1288
                           outbuf.size(),
1289
                           passphrase.c_str(),
1290
                           pbkdf_salt.data(),
1291
                           pbkdf_salt.size(),
1292
                           pbkdf_iterations))) {
1293
            result.test_eq("PBKDF output", outbuf, "027AFADD48F4BE8DCC4F");
1✔
1294

1295
            size_t iters_10ms, iters_100ms;
1✔
1296

1297
            TEST_FFI_OK(botan_pbkdf_timed,
1✔
1298
                        ("PBKDF2(SHA-1)",
1299
                         outbuf.data(),
1300
                         outbuf.size(),
1301
                         passphrase.c_str(),
1302
                         pbkdf_salt.data(),
1303
                         pbkdf_salt.size(),
1304
                         10,
1305
                         &iters_10ms));
1306
            TEST_FFI_OK(botan_pbkdf_timed,
1✔
1307
                        ("PBKDF2(SHA-1)",
1308
                         outbuf.data(),
1309
                         outbuf.size(),
1310
                         passphrase.c_str(),
1311
                         pbkdf_salt.data(),
1312
                         pbkdf_salt.size(),
1313
                         100,
1314
                         &iters_100ms));
1315

1316
            result.test_note("PBKDF timed 10 ms " + std::to_string(iters_10ms) + " iterations " + "100 ms " +
3✔
1317
                             std::to_string(iters_100ms) + " iterations");
4✔
1318
         }
1319

1320
         const std::vector<uint8_t> kdf_secret = Botan::hex_decode("92167440112E");
1✔
1321
         const std::vector<uint8_t> kdf_salt = Botan::hex_decode("45A9BEDED69163123D0348F5185F61ABFB1BF18D6AEA454F");
1✔
1322
         const size_t kdf_out_len = 18;
1✔
1323
         outbuf.resize(kdf_out_len);
1✔
1324

1325
         if(TEST_FFI_INIT(botan_kdf,
1✔
1326
                          ("KDF2(SHA-1)",
1327
                           outbuf.data(),
1328
                           outbuf.size(),
1329
                           kdf_secret.data(),
1330
                           kdf_secret.size(),
1331
                           kdf_salt.data(),
1332
                           kdf_salt.size(),
1333
                           nullptr,
1334
                           0))) {
1335
            result.test_eq("KDF output", outbuf, "3A5DC9AA1C872B4744515AC2702D6396FC2A");
2✔
1336
         }
1337

1338
         size_t out_len = 64;
1✔
1339
         std::string outstr;
1✔
1340
         outstr.resize(out_len);
1✔
1341

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

1345
         if(rc == 0) {
1✔
1346
            result.test_eq("bcrypt output size", out_len, 61);
1✔
1347

1348
            TEST_FFI_OK(botan_bcrypt_is_valid, (passphrase.c_str(), outstr.data()));
1✔
1349
            TEST_FFI_FAIL("bad password", botan_bcrypt_is_valid, ("nope", outstr.data()));
2✔
1350
         }
1351
      }
6✔
1352
};
1353

1354
class FFI_Blockcipher_Test final : public FFI_Test {
×
1355
   public:
1356
      std::string name() const override { return "FFI block ciphers"; }
1✔
1357

1358
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1359
         botan_block_cipher_t cipher;
1✔
1360

1361
         if(TEST_FFI_INIT(botan_block_cipher_init, (&cipher, "AES-128"))) {
2✔
1362
            char namebuf[10];
1✔
1363
            size_t name_len = 7;
1✔
1364
            TEST_FFI_FAIL("output buffer too short", botan_block_cipher_name, (cipher, namebuf, &name_len));
2✔
1365
            result.test_eq("name len", name_len, 8);
1✔
1366

1367
            name_len = sizeof(namebuf);
1✔
1368
            if(TEST_FFI_OK(botan_block_cipher_name, (cipher, namebuf, &name_len))) {
2✔
1369
               result.test_eq("name len", name_len, 8);
1✔
1370
               result.test_eq("name", std::string(namebuf), "AES-128");
2✔
1371
            }
1372

1373
            const std::vector<uint8_t> zero16(16, 0);
1✔
1374
            std::vector<uint8_t> block(16, 0);
1✔
1375

1376
            TEST_FFI_OK(botan_block_cipher_clear, (cipher));
1✔
1377

1378
            TEST_FFI_RC(
1✔
1379
               BOTAN_FFI_ERROR_KEY_NOT_SET, botan_block_cipher_encrypt_blocks, (cipher, block.data(), block.data(), 1));
1380
            TEST_FFI_RC(
1✔
1381
               BOTAN_FFI_ERROR_KEY_NOT_SET, botan_block_cipher_decrypt_blocks, (cipher, block.data(), block.data(), 1));
1382

1383
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_block_cipher_encrypt_blocks, (cipher, nullptr, nullptr, 0));
1✔
1384
            TEST_FFI_RC(BOTAN_FFI_ERROR_NULL_POINTER, botan_block_cipher_decrypt_blocks, (cipher, nullptr, nullptr, 0));
1✔
1385

1386
            TEST_FFI_RC(16, botan_block_cipher_block_size, (cipher));
1✔
1387

1388
            size_t min_keylen = 0, max_keylen = 0, mod_keylen = 0;
1✔
1389
            TEST_FFI_RC(0, botan_block_cipher_get_keyspec, (cipher, nullptr, nullptr, nullptr));
1✔
1390
            TEST_FFI_RC(0, botan_block_cipher_get_keyspec, (cipher, &min_keylen, nullptr, nullptr));
1✔
1391
            TEST_FFI_RC(0, botan_block_cipher_get_keyspec, (cipher, nullptr, &max_keylen, nullptr));
1✔
1392
            TEST_FFI_RC(0, botan_block_cipher_get_keyspec, (cipher, nullptr, nullptr, &mod_keylen));
1✔
1393

1394
            result.test_eq("Expected min keylen", min_keylen, 16);
1✔
1395
            result.test_eq("Expected max keylen", max_keylen, 16);
1✔
1396
            result.test_eq("Expected mod keylen", mod_keylen, 1);
1✔
1397

1398
            TEST_FFI_OK(botan_block_cipher_set_key, (cipher, zero16.data(), zero16.size()));
1✔
1399

1400
            TEST_FFI_OK(botan_block_cipher_encrypt_blocks, (cipher, block.data(), block.data(), 1));
1✔
1401
            result.test_eq("AES-128 encryption works", block, "66E94BD4EF8A2C3B884CFA59CA342B2E");
1✔
1402

1403
            TEST_FFI_OK(botan_block_cipher_encrypt_blocks, (cipher, block.data(), block.data(), 1));
1✔
1404
            result.test_eq("AES-128 encryption works", block, "F795BD4A52E29ED713D313FA20E98DBC");
1✔
1405

1406
            TEST_FFI_OK(botan_block_cipher_decrypt_blocks, (cipher, block.data(), block.data(), 1));
1✔
1407
            result.test_eq("AES-128 decryption works", block, "66E94BD4EF8A2C3B884CFA59CA342B2E");
1✔
1408

1409
            TEST_FFI_OK(botan_block_cipher_decrypt_blocks, (cipher, block.data(), block.data(), 1));
1✔
1410
            result.test_eq("AES-128 decryption works", block, "00000000000000000000000000000000");
1✔
1411

1412
            TEST_FFI_OK(botan_block_cipher_clear, (cipher));
1✔
1413
            botan_block_cipher_destroy(cipher);
1✔
1414
         }
2✔
1415
      }
1✔
1416
};
1417

1418
class FFI_ErrorHandling_Test final : public FFI_Test {
×
1419
   public:
1420
      std::string name() const override { return "FFI error handling"; }
1✔
1421

1422
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1423
         // delete of null is ok/ignored
1424
         TEST_FFI_RC(0, botan_hash_destroy, (nullptr));
1✔
1425

1426
   #if !defined(BOTAN_HAS_SANITIZER_UNDEFINED)
1427
         // Confirm that botan_x_destroy checks the argument type
1428
         botan_mp_t mp;
1✔
1429
         botan_mp_init(&mp);
1✔
1430
         TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT, botan_hash_destroy, (reinterpret_cast<botan_hash_t>(mp)));
1✔
1431
         TEST_FFI_RC(0, botan_mp_destroy, (mp));
1✔
1432
   #endif
1433

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

1439
            if(err) {
150✔
1440
               std::string s(err);
150✔
1441

1442
               if(s != "Unknown error") {
150✔
1443
                  result.confirm("No duplicate messages", !errors.contains(s));
40✔
1444
                  errors.insert(s);
170✔
1445
               }
1446
            }
150✔
1447
         }
1448
      }
1✔
1449
};
1450

1451
class FFI_Base64_Test final : public FFI_Test {
×
1452
   public:
1453
      std::string name() const override { return "FFI base64"; }
1✔
1454

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

1459
         size_t out_len = sizeof(out_buf);
1✔
1460
         TEST_FFI_OK(botan_base64_encode, (bin, sizeof(bin), out_buf, &out_len));
1✔
1461

1462
         result.test_eq("encoded string", out_buf, "FoofBunny900");
1✔
1463

1464
         out_len -= 1;
1✔
1465
         TEST_FFI_RC(
1✔
1466
            BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_base64_encode, (bin, sizeof(bin), out_buf, &out_len));
1467

1468
         const char* base64 = "U3VjaCBiYXNlNjQgd293IQ==";
1✔
1469
         uint8_t out_bin[1024] = {0};
1✔
1470

1471
         out_len = 3;
1✔
1472
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
1473
                     botan_base64_decode,
1474
                     (base64, strlen(base64), out_bin, &out_len));
1475

1476
         result.test_eq("output length", out_len, 18);
1✔
1477

1478
         out_len = sizeof(out_bin);
1✔
1479
         TEST_FFI_OK(botan_base64_decode, (base64, strlen(base64), out_bin, &out_len));
1✔
1480

1481
         result.test_eq(
3✔
1482
            "decoded string", std::string(reinterpret_cast<const char*>(out_bin), out_len), "Such base64 wow!");
3✔
1483
      }
1✔
1484
};
1485

1486
class FFI_Hex_Test final : public FFI_Test {
×
1487
   public:
1488
      std::string name() const override { return "FFI hex"; }
1✔
1489

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

1494
         TEST_FFI_OK(botan_hex_encode, (bin, sizeof(bin), hex_buf, 0));
1✔
1495

1496
         result.test_eq("encoded string", hex_buf, "DEADBEEF");
1✔
1497

1498
         const char* hex = "67657420796572206A756D626F20736872696D70";
1✔
1499
         uint8_t out_bin[1024] = {0};
1✔
1500
         size_t out_len = 5;
1✔
1501

1502
         TEST_FFI_RC(
1✔
1503
            BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_hex_decode, (hex, strlen(hex), out_bin, &out_len));
1504

1505
         out_len = sizeof(out_bin);
1✔
1506
         TEST_FFI_OK(botan_hex_decode, (hex, strlen(hex), out_bin, &out_len));
1✔
1507

1508
         result.test_eq(
3✔
1509
            "decoded string", std::string(reinterpret_cast<const char*>(out_bin), out_len), "get yer jumbo shrimp");
3✔
1510
      }
1✔
1511
};
1512

1513
class FFI_MP_Test final : public FFI_Test {
×
1514
   public:
1515
      std::string name() const override { return "FFI MP"; }
1✔
1516

1517
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
1518
         char str_buf[1024] = {0};
1✔
1519
         size_t str_len = 0;
1✔
1520

1521
         botan_mp_t x;
1✔
1522
         botan_mp_init(&x);
1✔
1523
         TEST_FFI_RC(0, botan_mp_is_odd, (x));
1✔
1524
         TEST_FFI_RC(1, botan_mp_is_even, (x));
1✔
1525
         TEST_FFI_RC(0, botan_mp_is_negative, (x));
1✔
1526
         TEST_FFI_RC(1, botan_mp_is_positive, (x));
1✔
1527
         TEST_FFI_RC(1, botan_mp_is_zero, (x));
1✔
1528
         botan_mp_destroy(x);
1✔
1529

1530
         botan_mp_init(&x);
1✔
1531
         size_t bn_bytes = 0;
1✔
1532
         TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes));
1✔
1533
         result.test_eq("Expected size for MP 0", bn_bytes, 0);
1✔
1534

1535
         botan_mp_set_from_int(x, 5);
1✔
1536
         TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes));
1✔
1537
         result.test_eq("Expected size for MP 5", bn_bytes, 1);
1✔
1538

1539
         botan_mp_add_u32(x, x, 75);
1✔
1540
         TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes));
1✔
1541
         result.test_eq("Expected size for MP 80", bn_bytes, 1);
1✔
1542

1543
         str_len = sizeof(str_buf);
1✔
1544
         TEST_FFI_OK(botan_mp_to_str, (x, 10, str_buf, &str_len));
1✔
1545
         result.test_eq("botan_mp_add", std::string(str_buf), "80");
2✔
1546

1547
         botan_mp_sub_u32(x, x, 80);
1✔
1548
         TEST_FFI_RC(1, botan_mp_is_zero, (x));
1✔
1549
         botan_mp_add_u32(x, x, 259);
1✔
1550
         TEST_FFI_OK(botan_mp_num_bytes, (x, &bn_bytes));
1✔
1551
         result.test_eq("Expected size for MP 259", bn_bytes, 2);
1✔
1552

1553
         str_len = sizeof(str_buf);
1✔
1554
         TEST_FFI_OK(botan_mp_to_str, (x, 10, str_buf, &str_len));
1✔
1555
         result.test_eq("botan_mp_add", std::string(str_buf), "259");
2✔
1556

1557
         TEST_FFI_RC(1, botan_mp_is_odd, (x));
1✔
1558
         TEST_FFI_RC(0, botan_mp_is_even, (x));
1✔
1559
         TEST_FFI_RC(0, botan_mp_is_negative, (x));
1✔
1560
         TEST_FFI_RC(1, botan_mp_is_positive, (x));
1✔
1561
         TEST_FFI_RC(0, botan_mp_is_zero, (x));
1✔
1562

1563
         {
1✔
1564
            botan_mp_t zero;
1✔
1565
            botan_mp_init(&zero);
1✔
1566
            int cmp;
1✔
1567
            TEST_FFI_OK(botan_mp_cmp, (&cmp, x, zero));
1✔
1568
            result.confirm("bigint_mp_cmp(+, 0)", cmp == 1);
2✔
1569

1570
            TEST_FFI_OK(botan_mp_cmp, (&cmp, zero, x));
1✔
1571
            result.confirm("bigint_mp_cmp(0, +)", cmp == -1);
2✔
1572

1573
            TEST_FFI_RC(0, botan_mp_is_negative, (x));
1✔
1574
            TEST_FFI_RC(1, botan_mp_is_positive, (x));
1✔
1575
            TEST_FFI_OK(botan_mp_flip_sign, (x));
1✔
1576
            TEST_FFI_RC(1, botan_mp_is_negative, (x));
1✔
1577
            TEST_FFI_RC(0, botan_mp_is_positive, (x));
1✔
1578

1579
            // test no negative zero
1580
            TEST_FFI_RC(0, botan_mp_is_negative, (zero));
1✔
1581
            TEST_FFI_RC(1, botan_mp_is_positive, (zero));
1✔
1582
            TEST_FFI_OK(botan_mp_flip_sign, (zero));
1✔
1583
            TEST_FFI_RC(0, botan_mp_is_negative, (zero));
1✔
1584
            TEST_FFI_RC(1, botan_mp_is_positive, (zero));
1✔
1585

1586
            TEST_FFI_OK(botan_mp_cmp, (&cmp, x, zero));
1✔
1587
            result.confirm("bigint_mp_cmp(-, 0)", cmp == -1);
2✔
1588

1589
            TEST_FFI_OK(botan_mp_cmp, (&cmp, zero, x));
1✔
1590
            result.confirm("bigint_mp_cmp(0, -)", cmp == 1);
2✔
1591

1592
            TEST_FFI_OK(botan_mp_cmp, (&cmp, zero, zero));
1✔
1593
            result.confirm("bigint_mp_cmp(0, 0)", cmp == 0);
2✔
1594

1595
            TEST_FFI_OK(botan_mp_cmp, (&cmp, x, x));
1✔
1596
            result.confirm("bigint_mp_cmp(x, x)", cmp == 0);
2✔
1597

1598
            TEST_FFI_OK(botan_mp_flip_sign, (x));
1✔
1599

1600
            botan_mp_destroy(zero);
1✔
1601
         }
1602

1603
         size_t x_bits = 0;
1✔
1604
         TEST_FFI_OK(botan_mp_num_bits, (x, &x_bits));
1✔
1605
         result.test_eq("botan_mp_num_bits", x_bits, 9);
1✔
1606

1607
         TEST_FFI_OK(botan_mp_to_hex, (x, str_buf));
1✔
1608
         result.test_eq("botan_mp_to_hex", std::string(str_buf), "0x0103");
2✔
1609

1610
         uint32_t x_32;
1✔
1611
         TEST_FFI_OK(botan_mp_to_uint32, (x, &x_32));
1✔
1612
         result.test_eq("botan_mp_to_uint32", size_t(x_32), size_t(0x103));
1✔
1613

1614
         TEST_FFI_RC(1, botan_mp_get_bit, (x, 1));
1✔
1615
         TEST_FFI_RC(0, botan_mp_get_bit, (x, 87));
1✔
1616
         TEST_FFI_OK(botan_mp_set_bit, (x, 87));
1✔
1617
         TEST_FFI_RC(1, botan_mp_get_bit, (x, 87));
1✔
1618
         TEST_FFI_OK(botan_mp_to_hex, (x, str_buf));
1✔
1619
         result.test_eq("botan_mp_set_bit", std::string(str_buf), "0x8000000000000000000103");
3✔
1620

1621
         TEST_FFI_OK(botan_mp_clear_bit, (x, 87));
1✔
1622
         TEST_FFI_OK(botan_mp_to_hex, (x, str_buf));
1✔
1623
         result.test_eq("botan_mp_set_bit", std::string(str_buf), "0x0103");
2✔
1624

1625
         botan_mp_t y;
1✔
1626
         TEST_FFI_OK(botan_mp_init, (&y));
1✔
1627
         TEST_FFI_OK(botan_mp_set_from_int, (y, 0x1234567));
1✔
1628

1629
         botan_mp_t r;
1✔
1630
         botan_mp_init(&r);
1✔
1631

1632
         TEST_FFI_OK(botan_mp_add, (r, x, y));
1✔
1633
         str_len = sizeof(str_buf);
1✔
1634
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
1635
         result.test_eq("botan_mp_add", std::string(str_buf), "19089002");
2✔
1636

1637
         TEST_FFI_OK(botan_mp_mul, (r, x, y));
1✔
1638
         str_len = sizeof(str_buf);
1✔
1639
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
1640
         result.test_eq("botan_mp_mul", std::string(str_buf), "4943984437");
2✔
1641
         TEST_FFI_RC(0, botan_mp_is_negative, (r));
1✔
1642

1643
         botan_mp_t q;
1✔
1644
         botan_mp_init(&q);
1✔
1645
         TEST_FFI_OK(botan_mp_div, (q, r, y, x));
1✔
1646

1647
         str_len = sizeof(str_buf);
1✔
1648
         TEST_FFI_OK(botan_mp_to_str, (q, 10, str_buf, &str_len));
1✔
1649
         result.test_eq("botan_mp_div_q", std::string(str_buf), "73701");
2✔
1650

1651
         str_len = sizeof(str_buf);
1✔
1652
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
1653
         result.test_eq("botan_mp_div_r", std::string(str_buf), "184");
2✔
1654

1655
         TEST_FFI_OK(botan_mp_set_from_str, (y, "4943984437"));
1✔
1656
         TEST_FFI_OK(botan_mp_sub, (r, x, y));
1✔
1657
         str_len = sizeof(str_buf);
1✔
1658
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
1659
         result.test_eq("botan_mp_sub", std::string(str_buf), "-4943984178");
2✔
1660
         TEST_FFI_RC(1, botan_mp_is_negative, (r));
1✔
1661

1662
         TEST_FFI_OK(botan_mp_lshift, (r, x, 39));
1✔
1663
         str_len = sizeof(str_buf);
1✔
1664
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
1665
         result.test_eq("botan_mp_lshift", std::string(str_buf), "142386755796992");
2✔
1666

1667
         TEST_FFI_OK(botan_mp_rshift, (r, r, 3));
1✔
1668
         str_len = sizeof(str_buf);
1✔
1669
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
1670
         result.test_eq("botan_mp_rshift", std::string(str_buf), "17798344474624");
2✔
1671

1672
         TEST_FFI_OK(botan_mp_gcd, (r, x, y));
1✔
1673
         str_len = sizeof(str_buf);
1✔
1674
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
1675
         result.test_eq("botan_mp_gcd", std::string(str_buf), "259");
2✔
1676

1677
         botan_mp_t p;
1✔
1678
         botan_mp_init(&p);
1✔
1679
         const uint8_t M127[] = {
1✔
1680
            0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1681
         TEST_FFI_OK(botan_mp_from_bin, (p, M127, sizeof(M127)));
1✔
1682
         TEST_FFI_RC(1, botan_mp_is_prime, (p, rng, 64));
1✔
1683

1684
         size_t p_bits = 0;
1✔
1685
         TEST_FFI_OK(botan_mp_num_bits, (p, &p_bits));
1✔
1686
         result.test_eq("botan_mp_num_bits", p_bits, 127);
1✔
1687

1688
         TEST_FFI_OK(botan_mp_mod_inverse, (r, x, p));
1✔
1689
         str_len = sizeof(str_buf);
1✔
1690
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
1691
         result.test_eq("botan_mp_mod_inverse", std::string(str_buf), "40728777507911553541948312086427855425");
3✔
1692

1693
         TEST_FFI_OK(botan_mp_powmod, (r, x, r, p));
1✔
1694
         str_len = sizeof(str_buf);
1✔
1695
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
1696
         result.test_eq("botan_mp_powmod", std::string(str_buf), "40550417419160441638948180641668117560");
3✔
1697

1698
         TEST_FFI_OK(botan_mp_num_bytes, (r, &bn_bytes));
1✔
1699
         result.test_eq("botan_mp_num_bytes", bn_bytes, 16);
1✔
1700

1701
         std::vector<uint8_t> bn_buf;
1✔
1702
         bn_buf.resize(bn_bytes);
1✔
1703
         botan_mp_to_bin(r, bn_buf.data());
1✔
1704
         result.test_eq("botan_mp_to_bin", bn_buf, "1E81B9EFE0BE1902F6D03F9F5E5FB438");
1✔
1705

1706
         TEST_FFI_OK(botan_mp_set_from_mp, (y, r));
1✔
1707
         TEST_FFI_OK(botan_mp_mod_mul, (r, x, y, p));
1✔
1708
         str_len = sizeof(str_buf);
1✔
1709
         TEST_FFI_OK(botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
1710
         result.test_eq("botan_mp_mod_mul", std::string(str_buf), "123945920473931248854653259523111998693");
3✔
1711

1712
         str_len = 0;
1✔
1713
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, botan_mp_to_str, (r, 10, str_buf, &str_len));
1✔
1714

1715
         size_t x_bytes;
1✔
1716
         botan_mp_rand_bits(x, rng, 512);
1✔
1717
         TEST_FFI_OK(botan_mp_num_bytes, (x, &x_bytes));
1✔
1718
         result.test_lte("botan_mp_num_bytes", x_bytes, 512 / 8);
1✔
1719

1720
         TEST_FFI_OK(botan_mp_set_from_radix_str, (x, "909A", 16));
1✔
1721
         TEST_FFI_OK(botan_mp_to_uint32, (x, &x_32));
1✔
1722
         result.test_eq("botan_mp_set_from_radix_str(16)", x_32, static_cast<size_t>(0x909A));
1✔
1723

1724
         TEST_FFI_OK(botan_mp_set_from_radix_str, (x, "9098135", 10));
1✔
1725
         TEST_FFI_OK(botan_mp_to_uint32, (x, &x_32));
1✔
1726
         result.test_eq("botan_mp_set_from_radix_str(10)", x_32, static_cast<size_t>(9098135));
1✔
1727

1728
         botan_mp_destroy(p);
1✔
1729
         botan_mp_destroy(x);
1✔
1730
         botan_mp_destroy(y);
1✔
1731
         botan_mp_destroy(r);
1✔
1732
         botan_mp_destroy(q);
1✔
1733
      }
1✔
1734
};
1735

1736
class FFI_FPE_Test final : public FFI_Test {
×
1737
   public:
1738
      std::string name() const override { return "FFI FPE"; }
1✔
1739

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

1743
         botan_mp_t n;
1✔
1744
         botan_mp_init(&n);
1✔
1745
         botan_mp_set_from_str(n, "1000000000");
1✔
1746

1747
         botan_fpe_t fpe;
1✔
1748
         if(!TEST_FFI_INIT(botan_fpe_fe1_init, (&fpe, n, key, sizeof(key), 5, 0))) {
2✔
1749
            botan_mp_destroy(n);
×
1750
            return;
×
1751
         }
1752

1753
         botan_mp_t x;
1✔
1754
         botan_mp_init(&x);
1✔
1755
         botan_mp_set_from_str(x, "178051120");
1✔
1756

1757
         TEST_FFI_OK(botan_fpe_encrypt, (fpe, x, nullptr, 0));
1✔
1758

1759
         uint32_t xval = 0;
1✔
1760
         TEST_FFI_OK(botan_mp_to_uint32, (x, &xval));
1✔
1761
         result.test_eq("Expected FPE ciphertext", xval, size_t(605648666));
1✔
1762

1763
         TEST_FFI_OK(botan_fpe_encrypt, (fpe, x, nullptr, 0));
1✔
1764
         TEST_FFI_OK(botan_fpe_decrypt, (fpe, x, nullptr, 0));
1✔
1765
         TEST_FFI_OK(botan_fpe_decrypt, (fpe, x, nullptr, 0));
1✔
1766

1767
         TEST_FFI_OK(botan_mp_to_uint32, (x, &xval));
1✔
1768
         result.test_eq("FPE round trip", xval, size_t(178051120));
1✔
1769

1770
         TEST_FFI_OK(botan_fpe_destroy, (fpe));
1✔
1771
         TEST_FFI_OK(botan_mp_destroy, (x));
1✔
1772
         TEST_FFI_OK(botan_mp_destroy, (n));
2✔
1773
      }
1774
};
1775

1776
class FFI_TOTP_Test final : public FFI_Test {
×
1777
   public:
1778
      std::string name() const override { return "FFI TOTP"; }
1✔
1779

1780
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1781
         const std::vector<uint8_t> key = Botan::hex_decode("3132333435363738393031323334353637383930");
1✔
1782
         const size_t digits = 8;
1✔
1783
         const size_t timestep = 30;
1✔
1784
         botan_totp_t totp;
1✔
1785

1786
         if(!TEST_FFI_INIT(botan_totp_init, (&totp, key.data(), key.size(), "SHA-1", digits, timestep))) {
1✔
1787
            return;
×
1788
         }
1789

1790
         uint32_t code;
1✔
1791
         TEST_FFI_OK(botan_totp_generate, (totp, &code, 59));
1✔
1792
         result.confirm("TOTP code", code == 94287082);
2✔
1793

1794
         TEST_FFI_OK(botan_totp_generate, (totp, &code, 1111111109));
1✔
1795
         result.confirm("TOTP code 2", code == 7081804);
2✔
1796

1797
         TEST_FFI_OK(botan_totp_check, (totp, 94287082, 59 + 60, 60));
1✔
1798
         TEST_FFI_RC(1, botan_totp_check, (totp, 94287082, 59 + 31, 1));
1✔
1799
         TEST_FFI_RC(1, botan_totp_check, (totp, 94287082, 59 + 61, 1));
1✔
1800

1801
         TEST_FFI_OK(botan_totp_destroy, (totp));
2✔
1802
      }
1✔
1803
};
1804

1805
class FFI_HOTP_Test final : public FFI_Test {
×
1806
   public:
1807
      std::string name() const override { return "FFI HOTP"; }
1✔
1808

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

1813
         botan_hotp_t hotp;
1✔
1814
         uint32_t hotp_val;
1✔
1815

1816
         if(!TEST_FFI_INIT(botan_hotp_init, (&hotp, key.data(), key.size(), "SHA-1", digits))) {
1✔
1817
            return;
×
1818
         }
1819

1820
         TEST_FFI_OK(botan_hotp_generate, (hotp, &hotp_val, 0));
1✔
1821
         result.confirm("Valid value for counter 0", hotp_val == 755224);
2✔
1822
         TEST_FFI_OK(botan_hotp_generate, (hotp, &hotp_val, 1));
1✔
1823
         result.confirm("Valid value for counter 0", hotp_val == 287082);
2✔
1824
         TEST_FFI_OK(botan_hotp_generate, (hotp, &hotp_val, 2));
1✔
1825
         result.confirm("Valid value for counter 0", hotp_val == 359152);
2✔
1826
         TEST_FFI_OK(botan_hotp_generate, (hotp, &hotp_val, 0));
1✔
1827
         result.confirm("Valid value for counter 0", hotp_val == 755224);
2✔
1828

1829
         uint64_t next_ctr = 0;
1✔
1830

1831
         TEST_FFI_OK(botan_hotp_check, (hotp, &next_ctr, 755224, 0, 0));
1✔
1832
         result.confirm("HOTP resync", next_ctr == 1);
2✔
1833
         TEST_FFI_OK(botan_hotp_check, (hotp, nullptr, 359152, 2, 0));
1✔
1834
         TEST_FFI_RC(1, botan_hotp_check, (hotp, nullptr, 359152, 1, 0));
1✔
1835
         TEST_FFI_OK(botan_hotp_check, (hotp, &next_ctr, 359152, 0, 2));
1✔
1836
         result.confirm("HOTP resync", next_ctr == 3);
2✔
1837

1838
         TEST_FFI_OK(botan_hotp_destroy, (hotp));
2✔
1839
      }
1✔
1840
};
1841

1842
class FFI_Keywrap_Test final : public FFI_Test {
×
1843
   public:
1844
      std::string name() const override { return "FFI Keywrap"; }
1✔
1845

1846
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
1847
         const uint8_t key[16] = {0};
1✔
1848
         const uint8_t kek[16] = {0xFF, 0};
1✔
1849

1850
         uint8_t wrapped[16 + 8] = {0};
1✔
1851
         size_t wrapped_keylen = sizeof(wrapped);
1✔
1852

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

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

1860
            result.test_eq(
1✔
1861
               nullptr, "Wrapped key", wrapped, wrapped_keylen, expected_wrapped_key, sizeof(expected_wrapped_key));
1862

1863
            uint8_t dec_key[16] = {0};
1✔
1864
            size_t dec_keylen = sizeof(dec_key);
1✔
1865
            TEST_FFI_OK(botan_key_unwrap3394, (wrapped, sizeof(wrapped), kek, sizeof(kek), dec_key, &dec_keylen));
1✔
1866

1867
            result.test_eq(nullptr, "Unwrapped key", dec_key, dec_keylen, key, sizeof(key));
2✔
1868
         }
1869
      }
1✔
1870
};
1871

1872
class FFI_RSA_Test final : public FFI_Test {
×
1873
   public:
1874
      std::string name() const override { return "FFI RSA"; }
1✔
1875

1876
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
1877
         botan_privkey_t priv;
1✔
1878

1879
         if(TEST_FFI_INIT(botan_privkey_create_rsa, (&priv, rng, 1024))) {
2✔
1880
            TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
1✔
1881

1882
            botan_pubkey_t pub;
1✔
1883
            TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
1884
            TEST_FFI_OK(botan_pubkey_check_key, (pub, rng, 0));
1✔
1885

1886
            ffi_test_pubkey_export(result, pub, priv, rng);
1✔
1887

1888
            botan_mp_t p, q, d, n, e;
1✔
1889
            botan_mp_init(&p);
1✔
1890
            botan_mp_init(&q);
1✔
1891
            botan_mp_init(&d);
1✔
1892
            botan_mp_init(&n);
1✔
1893
            botan_mp_init(&e);
1✔
1894

1895
            TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_privkey_get_field, (p, priv, "quux"));
1✔
1896
            TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_pubkey_get_field, (p, pub, "quux"));
1✔
1897

1898
            TEST_FFI_OK(botan_privkey_rsa_get_p, (p, priv));
1✔
1899
            TEST_FFI_OK(botan_privkey_rsa_get_q, (q, priv));
1✔
1900
            TEST_FFI_OK(botan_privkey_rsa_get_d, (d, priv));
1✔
1901
            TEST_FFI_OK(botan_privkey_rsa_get_e, (e, priv));
1✔
1902
            TEST_FFI_OK(botan_privkey_rsa_get_n, (n, priv));
1✔
1903

1904
            // Confirm same (e,n) values in public key
1905
            {
1✔
1906
               botan_mp_t pub_e, pub_n;
1✔
1907
               botan_mp_init(&pub_e);
1✔
1908
               botan_mp_init(&pub_n);
1✔
1909
               TEST_FFI_OK(botan_pubkey_rsa_get_e, (pub_e, pub));
1✔
1910
               TEST_FFI_OK(botan_pubkey_rsa_get_n, (pub_n, pub));
1✔
1911

1912
               TEST_FFI_RC(1, botan_mp_equal, (pub_e, e));
1✔
1913
               TEST_FFI_RC(1, botan_mp_equal, (pub_n, n));
1✔
1914
               botan_mp_destroy(pub_e);
1✔
1915
               botan_mp_destroy(pub_n);
1✔
1916
            }
1917

1918
            TEST_FFI_RC(1, botan_mp_is_prime, (p, rng, 64));
1✔
1919
            TEST_FFI_RC(1, botan_mp_is_prime, (q, rng, 64));
1✔
1920

1921
            // Test p != q
1922
            TEST_FFI_RC(0, botan_mp_equal, (p, q));
1✔
1923

1924
            // Test p * q == n
1925
            botan_mp_t x;
1✔
1926
            botan_mp_init(&x);
1✔
1927
            TEST_FFI_OK(botan_mp_mul, (x, p, q));
1✔
1928

1929
            TEST_FFI_RC(1, botan_mp_equal, (x, n));
1✔
1930
            botan_mp_destroy(x);
1✔
1931

1932
            botan_privkey_t loaded_privkey;
1✔
1933
            // First try loading a bogus key and verify check_key fails
1934
            TEST_FFI_OK(botan_privkey_load_rsa, (&loaded_privkey, n, d, q));
1✔
1935
            TEST_FFI_RC(-1, botan_privkey_check_key, (loaded_privkey, rng, 0));
1✔
1936
            botan_privkey_destroy(loaded_privkey);
1✔
1937

1938
            TEST_FFI_OK(botan_privkey_load_rsa, (&loaded_privkey, p, q, e));
1✔
1939
            TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
1✔
1940

1941
            botan_pubkey_t loaded_pubkey;
1✔
1942
            TEST_FFI_OK(botan_pubkey_load_rsa, (&loaded_pubkey, n, e));
1✔
1943
            TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey, rng, 0));
1✔
1944

1945
            botan_mp_destroy(p);
1✔
1946
            botan_mp_destroy(q);
1✔
1947
            botan_mp_destroy(d);
1✔
1948
            botan_mp_destroy(e);
1✔
1949
            botan_mp_destroy(n);
1✔
1950

1951
            size_t pkcs1_len = 0;
1✔
1952
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
1953
                        botan_privkey_rsa_get_privkey,
1954
                        (loaded_privkey, nullptr, &pkcs1_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
1955

1956
            std::vector<uint8_t> pkcs1(pkcs1_len);
1✔
1957
            TEST_FFI_OK(botan_privkey_rsa_get_privkey,
1✔
1958
                        (loaded_privkey, pkcs1.data(), &pkcs1_len, BOTAN_PRIVKEY_EXPORT_FLAG_DER));
1959

1960
            botan_privkey_t privkey_from_pkcs1;
1✔
1961
            TEST_FFI_OK(botan_privkey_load_rsa_pkcs1, (&privkey_from_pkcs1, pkcs1.data(), pkcs1_len));
1✔
1962
            TEST_FFI_OK(botan_privkey_destroy, (privkey_from_pkcs1));
1✔
1963

1964
            pkcs1_len = 0;
1✔
1965
            TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
1966
                        botan_privkey_rsa_get_privkey,
1967
                        (loaded_privkey, nullptr, &pkcs1_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
1968
            pkcs1.resize(pkcs1_len);
1✔
1969
            TEST_FFI_OK(botan_privkey_rsa_get_privkey,
1✔
1970
                        (loaded_privkey, pkcs1.data(), &pkcs1_len, BOTAN_PRIVKEY_EXPORT_FLAG_PEM));
1971

1972
            char namebuf[32] = {0};
1✔
1973
            size_t name_len = sizeof(namebuf);
1✔
1974
            if(TEST_FFI_OK(botan_pubkey_algo_name, (loaded_pubkey, namebuf, &name_len))) {
2✔
1975
               result.test_eq("algo name", std::string(namebuf), "RSA");
2✔
1976
            }
1977

1978
            name_len = sizeof(namebuf);
1✔
1979
            if(TEST_FFI_OK(botan_privkey_algo_name, (loaded_privkey, namebuf, &name_len))) {
2✔
1980
               result.test_eq("algo name", std::string(namebuf), "RSA");
2✔
1981
            }
1982

1983
            botan_pk_op_encrypt_t encrypt;
1✔
1984

1985
            if(TEST_FFI_INIT(botan_pk_op_encrypt_create, (&encrypt, loaded_pubkey, "OAEP(SHA-256)", 0))) {
2✔
1986
               std::vector<uint8_t> plaintext(32);
1✔
1987
               TEST_FFI_OK(botan_rng_get, (rng, plaintext.data(), plaintext.size()));
1✔
1988

1989
               size_t ctext_len;
1✔
1990
               TEST_FFI_OK(botan_pk_op_encrypt_output_length, (encrypt, plaintext.size(), &ctext_len));
1✔
1991
               std::vector<uint8_t> ciphertext(ctext_len);
1✔
1992

1993
               if(TEST_FFI_OK(botan_pk_op_encrypt,
2✔
1994
                              (encrypt, rng, ciphertext.data(), &ctext_len, plaintext.data(), plaintext.size()))) {
1995
                  ciphertext.resize(ctext_len);
1✔
1996

1997
                  botan_pk_op_decrypt_t decrypt;
1✔
1998
                  if(TEST_FFI_OK(botan_pk_op_decrypt_create, (&decrypt, priv, "OAEP(SHA-256)", 0))) {
2✔
1999
                     size_t decrypted_len;
1✔
2000
                     TEST_FFI_OK(botan_pk_op_decrypt_output_length, (decrypt, ciphertext.size(), &decrypted_len));
1✔
2001
                     std::vector<uint8_t> decrypted(decrypted_len);
1✔
2002
                     TEST_FFI_OK(botan_pk_op_decrypt,
1✔
2003
                                 (decrypt, decrypted.data(), &decrypted_len, ciphertext.data(), ciphertext.size()));
2004
                     decrypted.resize(decrypted_len);
1✔
2005

2006
                     result.test_eq("RSA plaintext", decrypted, plaintext);
2✔
2007
                  }
1✔
2008

2009
                  TEST_FFI_OK(botan_pk_op_decrypt_destroy, (decrypt));
2✔
2010
               }
2011

2012
               TEST_FFI_OK(botan_pk_op_encrypt_destroy, (encrypt));
2✔
2013
            }
2✔
2014

2015
            TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey));
1✔
2016
            TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
2017
            TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
1✔
2018
            TEST_FFI_OK(botan_privkey_destroy, (priv));
2✔
2019
         }
1✔
2020
      }
1✔
2021
};
2022

2023
class FFI_DSA_Test final : public FFI_Test {
×
2024
   public:
2025
      std::string name() const override { return "FFI DSA"; }
1✔
2026

2027
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
2028
         botan_privkey_t priv;
1✔
2029

2030
         if(TEST_FFI_INIT(botan_privkey_create, (&priv, "DSA", "dsa/jce/1024", rng))) {
2✔
2031
            do_dsa_test(priv, rng, result);
1✔
2032
         }
2033

2034
         if(TEST_FFI_INIT(botan_privkey_create_dsa, (&priv, rng, 1024, 160))) {
2✔
2035
            do_dsa_test(priv, rng, result);
1✔
2036
         }
2037
      }
1✔
2038

2039
   private:
2040
      static void do_dsa_test(botan_privkey_t priv, botan_rng_t rng, Test::Result& result) {
2✔
2041
         TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
2✔
2042

2043
         botan_pubkey_t pub;
2✔
2044
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
2✔
2045
         TEST_FFI_OK(botan_pubkey_check_key, (pub, rng, 0));
2✔
2046

2047
         ffi_test_pubkey_export(result, pub, priv, rng);
2✔
2048

2049
         botan_mp_t p, q, g, x, y;
2✔
2050
         botan_mp_init(&p);
2✔
2051
         botan_mp_init(&q);
2✔
2052
         botan_mp_init(&g);
2✔
2053
         botan_mp_init(&x);
2✔
2054
         botan_mp_init(&y);
2✔
2055

2056
         TEST_FFI_OK(botan_privkey_dsa_get_x, (x, priv));
2✔
2057
         TEST_FFI_OK(botan_pubkey_dsa_get_g, (g, pub));
2✔
2058
         TEST_FFI_OK(botan_pubkey_dsa_get_p, (p, pub));
2✔
2059
         TEST_FFI_OK(botan_pubkey_dsa_get_q, (q, pub));
2✔
2060
         TEST_FFI_OK(botan_pubkey_dsa_get_y, (y, pub));
2✔
2061

2062
         botan_mp_t cmp;
2✔
2063
         botan_mp_init(&cmp);
2✔
2064
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_privkey_get_field, (cmp, priv, "quux"));
2✔
2065

2066
         TEST_FFI_OK(botan_privkey_get_field, (cmp, priv, "x"));
2✔
2067
         TEST_FFI_RC(1, botan_mp_equal, (cmp, x));
2✔
2068

2069
         TEST_FFI_OK(botan_privkey_get_field, (cmp, priv, "y"));
2✔
2070
         TEST_FFI_RC(1, botan_mp_equal, (cmp, y));
2✔
2071

2072
         TEST_FFI_OK(botan_privkey_get_field, (cmp, priv, "p"));
2✔
2073
         TEST_FFI_RC(1, botan_mp_equal, (cmp, p));
2✔
2074
         botan_mp_destroy(cmp);
2✔
2075

2076
         botan_privkey_t loaded_privkey;
2✔
2077
         TEST_FFI_OK(botan_privkey_load_dsa, (&loaded_privkey, p, q, g, x));
2✔
2078
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
2✔
2079

2080
         botan_pubkey_t loaded_pubkey;
2✔
2081
         TEST_FFI_OK(botan_pubkey_load_dsa, (&loaded_pubkey, p, q, g, y));
2✔
2082
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey, rng, 0));
2✔
2083

2084
         botan_mp_destroy(p);
2✔
2085
         botan_mp_destroy(q);
2✔
2086
         botan_mp_destroy(g);
2✔
2087
         botan_mp_destroy(y);
2✔
2088
         botan_mp_destroy(x);
2✔
2089

2090
         botan_pk_op_sign_t signer;
2✔
2091

2092
         std::vector<uint8_t> message(6, 6);
2✔
2093
         std::vector<uint8_t> signature;
2✔
2094

2095
         if(TEST_FFI_OK(botan_pk_op_sign_create, (&signer, loaded_privkey, "SHA-256", 0))) {
4✔
2096
            // TODO: break input into multiple calls to update
2097
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message.data(), message.size()));
2✔
2098

2099
            size_t sig_len;
2✔
2100
            TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_len));
2✔
2101
            signature.resize(sig_len);
2✔
2102

2103
            size_t output_sig_len = sig_len;
2✔
2104
            TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &output_sig_len));
2✔
2105
            result.test_lte("Output length is upper bound", output_sig_len, sig_len);
2✔
2106
            signature.resize(output_sig_len);
2✔
2107

2108
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
4✔
2109
         }
2110

2111
         botan_pk_op_verify_t verifier = nullptr;
2✔
2112

2113
         if(!signature.empty() && TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, "SHA-256", 0))) {
4✔
2114
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2115
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2✔
2116

2117
            // TODO: randomize this
2118
            signature[0] ^= 1;
2✔
2119
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2120
            TEST_FFI_RC(
2✔
2121
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2122

2123
            message[0] ^= 1;
2✔
2124
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2125
            TEST_FFI_RC(
2✔
2126
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2127

2128
            signature[0] ^= 1;
2✔
2129
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2130
            TEST_FFI_RC(
2✔
2131
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2132

2133
            message[0] ^= 1;
2✔
2134
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2135
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2✔
2136

2137
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
4✔
2138
         }
2139

2140
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey));
2✔
2141
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
2✔
2142
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
2✔
2143
         TEST_FFI_OK(botan_privkey_destroy, (priv));
4✔
2144
      }
4✔
2145
};
2146

2147
class FFI_ECDSA_Test final : public FFI_Test {
×
2148
   public:
2149
      std::string name() const override { return "FFI ECDSA"; }
1✔
2150

2151
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
2152
         static const char* kCurve = "secp384r1";
1✔
2153
         botan_privkey_t priv;
1✔
2154
         botan_pubkey_t pub;
1✔
2155

2156
         if(!TEST_FFI_INIT(botan_privkey_create_ecdsa, (&priv, rng, kCurve))) {
2✔
2157
            return;
×
2158
         }
2159

2160
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
2161
         ffi_test_pubkey_export(result, pub, priv, rng);
1✔
2162

2163
         // Check key load functions
2164
         botan_mp_t private_scalar, public_x, public_y;
1✔
2165
         botan_mp_init(&private_scalar);
1✔
2166
         botan_mp_init(&public_x);
1✔
2167
         botan_mp_init(&public_y);
1✔
2168

2169
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_privkey_get_field, (private_scalar, priv, "quux"));
1✔
2170
         TEST_FFI_RC(BOTAN_FFI_ERROR_BAD_PARAMETER, botan_pubkey_get_field, (private_scalar, pub, "quux"));
1✔
2171

2172
         TEST_FFI_OK(botan_privkey_get_field, (private_scalar, priv, "x"));
1✔
2173
         TEST_FFI_OK(botan_pubkey_get_field, (public_x, pub, "public_x"));
1✔
2174
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub, "public_y"));
1✔
2175

2176
         botan_privkey_t loaded_privkey;
1✔
2177
         botan_pubkey_t loaded_pubkey;
1✔
2178
         TEST_FFI_OK(botan_privkey_load_ecdsa, (&loaded_privkey, private_scalar, kCurve));
1✔
2179
         TEST_FFI_OK(botan_pubkey_load_ecdsa, (&loaded_pubkey, public_x, public_y, kCurve));
1✔
2180
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
1✔
2181
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey, rng, 0));
1✔
2182

2183
         char namebuf[32] = {0};
1✔
2184
         size_t name_len = sizeof(namebuf);
1✔
2185

2186
         TEST_FFI_OK(botan_pubkey_algo_name, (pub, &namebuf[0], &name_len));
1✔
2187
         result.test_eq(namebuf, namebuf, "ECDSA");
1✔
2188

2189
         std::vector<uint8_t> message(1280), signature;
1✔
2190
         TEST_FFI_OK(botan_rng_get, (rng, message.data(), message.size()));
1✔
2191

2192
         for(uint32_t flags = 0; flags <= 1; ++flags) {
3✔
2193
            botan_pk_op_sign_t signer;
2✔
2194
            if(TEST_FFI_INIT(botan_pk_op_sign_create, (&signer, loaded_privkey, "SHA-384", flags))) {
4✔
2195
               // TODO: break input into multiple calls to update
2196
               TEST_FFI_OK(botan_pk_op_sign_update, (signer, message.data(), message.size()));
2✔
2197

2198
               size_t sig_len;
2✔
2199
               TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_len));
2✔
2200

2201
               signature.resize(sig_len);
2✔
2202

2203
               size_t output_sig_len = signature.size();
2✔
2204
               TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &output_sig_len));
2✔
2205
               signature.resize(output_sig_len);
2✔
2206

2207
               TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
4✔
2208
            }
2209

2210
            botan_pk_op_verify_t verifier = nullptr;
2✔
2211

2212
            if(!signature.empty() && TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, "SHA-384", flags))) {
4✔
2213
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2214
               TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2✔
2215

2216
               // TODO: randomize this
2217
               signature[0] ^= 1;
2✔
2218
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2219
               TEST_FFI_RC(BOTAN_FFI_INVALID_VERIFIER,
2✔
2220
                           botan_pk_op_verify_finish,
2221
                           (verifier, signature.data(), signature.size()));
2222

2223
               message[0] ^= 1;
2✔
2224
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2225
               TEST_FFI_RC(BOTAN_FFI_INVALID_VERIFIER,
2✔
2226
                           botan_pk_op_verify_finish,
2227
                           (verifier, signature.data(), signature.size()));
2228

2229
               signature[0] ^= 1;
2✔
2230
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2231
               TEST_FFI_RC(BOTAN_FFI_INVALID_VERIFIER,
2✔
2232
                           botan_pk_op_verify_finish,
2233
                           (verifier, signature.data(), signature.size()));
2234

2235
               message[0] ^= 1;
2✔
2236
               TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
2✔
2237
               TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2✔
2238

2239
               TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
4✔
2240
            }
2241
         }
2242

2243
         TEST_FFI_OK(botan_mp_destroy, (private_scalar));
1✔
2244
         TEST_FFI_OK(botan_mp_destroy, (public_x));
1✔
2245
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1✔
2246
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
2247
         TEST_FFI_OK(botan_privkey_destroy, (priv));
1✔
2248
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
1✔
2249
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey));
2✔
2250
      }
2✔
2251
};
2252

2253
class FFI_SM2_Sig_Test final : public FFI_Test {
×
2254
   public:
2255
      std::string name() const override { return "FFI SM2 Sig"; }
1✔
2256

2257
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
2258
         static const char* kCurve = "sm2p256v1";
1✔
2259
         const std::string sm2_ident = "SM2 Ident Field";
1✔
2260
         botan_privkey_t priv;
1✔
2261
         botan_pubkey_t pub;
1✔
2262
         botan_privkey_t loaded_privkey;
1✔
2263
         botan_pubkey_t loaded_pubkey;
1✔
2264

2265
         if(!TEST_FFI_INIT(botan_privkey_create, (&priv, "SM2_Sig", kCurve, rng))) {
2✔
2266
            return;
2267
         }
2268

2269
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
2270
         ffi_test_pubkey_export(result, pub, priv, rng);
1✔
2271

2272
         uint8_t za[32];
1✔
2273
         size_t sizeof_za = sizeof(za);
1✔
2274
         TEST_FFI_OK(botan_pubkey_sm2_compute_za, (za, &sizeof_za, "Ident", "SM3", pub));
1✔
2275

2276
         // Check key load functions
2277
         botan_mp_t private_scalar, public_x, public_y;
1✔
2278
         botan_mp_init(&private_scalar);
1✔
2279
         botan_mp_init(&public_x);
1✔
2280
         botan_mp_init(&public_y);
1✔
2281

2282
         TEST_FFI_OK(botan_privkey_get_field, (private_scalar, priv, "x"));
1✔
2283
         TEST_FFI_OK(botan_pubkey_get_field, (public_x, pub, "public_x"));
1✔
2284
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub, "public_y"));
1✔
2285
         REQUIRE_FFI_OK(botan_privkey_load_sm2, (&loaded_privkey, private_scalar, kCurve));
2✔
2286
         REQUIRE_FFI_OK(botan_pubkey_load_sm2, (&loaded_pubkey, public_x, public_y, kCurve));
2✔
2287
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
1✔
2288
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey, rng, 0));
1✔
2289

2290
         char namebuf[32] = {0};
1✔
2291
         size_t name_len = sizeof(namebuf);
1✔
2292

2293
         TEST_FFI_OK(botan_pubkey_algo_name, (pub, &namebuf[0], &name_len));
1✔
2294
         result.test_eq(namebuf, namebuf, "SM2");
1✔
2295

2296
         std::vector<uint8_t> message(1280), signature;
1✔
2297
         TEST_FFI_OK(botan_rng_get, (rng, message.data(), message.size()));
1✔
2298
         botan_pk_op_sign_t signer;
1✔
2299
         if(TEST_FFI_OK(botan_pk_op_sign_create, (&signer, loaded_privkey, sm2_ident.c_str(), 0))) {
2✔
2300
            // TODO: break input into multiple calls to update
2301
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message.data(), message.size()));
1✔
2302

2303
            size_t sig_len;
1✔
2304
            TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_len));
1✔
2305

2306
            signature.resize(sig_len);
1✔
2307

2308
            TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &sig_len));
1✔
2309
            signature.resize(sig_len);
1✔
2310

2311
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
2✔
2312
         }
2313

2314
         botan_pk_op_verify_t verifier = nullptr;
1✔
2315

2316
         if(!signature.empty() && TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, sm2_ident.c_str(), 0))) {
2✔
2317
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1✔
2318
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
1✔
2319

2320
            // TODO: randomize this
2321
            signature[0] ^= 1;
1✔
2322
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1✔
2323
            TEST_FFI_RC(
1✔
2324
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2325

2326
            message[0] ^= 1;
1✔
2327
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1✔
2328
            TEST_FFI_RC(
1✔
2329
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2330

2331
            signature[0] ^= 1;
1✔
2332
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1✔
2333
            TEST_FFI_RC(
1✔
2334
               BOTAN_FFI_INVALID_VERIFIER, botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
2335

2336
            message[0] ^= 1;
1✔
2337
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1✔
2338
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
1✔
2339

2340
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
2✔
2341
         }
2342

2343
         TEST_FFI_OK(botan_mp_destroy, (private_scalar));
1✔
2344
         TEST_FFI_OK(botan_mp_destroy, (public_x));
1✔
2345
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1✔
2346
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
2347
         TEST_FFI_OK(botan_privkey_destroy, (priv));
1✔
2348
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
1✔
2349
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey));
2✔
2350
      }
2✔
2351
};
2352

2353
class FFI_SM2_Enc_Test final : public FFI_Test {
×
2354
   public:
2355
      std::string name() const override { return "FFI SM2 Enc"; }
1✔
2356

2357
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
2358
         static const char* kCurve = "sm2p256v1";
1✔
2359
         botan_privkey_t priv;
1✔
2360
         botan_pubkey_t pub;
1✔
2361
         botan_privkey_t loaded_privkey;
1✔
2362
         botan_pubkey_t loaded_pubkey;
1✔
2363

2364
         if(!TEST_FFI_INIT(botan_privkey_create, (&priv, "SM2_Enc", kCurve, rng))) {
2✔
2365
            return;
×
2366
         }
2367

2368
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
2369
         ffi_test_pubkey_export(result, pub, priv, rng);
1✔
2370

2371
         uint8_t za[32];
1✔
2372
         size_t sizeof_za = sizeof(za);
1✔
2373
         TEST_FFI_OK(botan_pubkey_sm2_compute_za, (za, &sizeof_za, "Ident", "SM3", pub));
1✔
2374

2375
         // Check key load functions
2376
         botan_mp_t private_scalar, public_x, public_y;
1✔
2377
         botan_mp_init(&private_scalar);
1✔
2378
         botan_mp_init(&public_x);
1✔
2379
         botan_mp_init(&public_y);
1✔
2380

2381
         TEST_FFI_OK(botan_privkey_get_field, (private_scalar, priv, "x"));
1✔
2382
         TEST_FFI_OK(botan_pubkey_get_field, (public_x, pub, "public_x"));
1✔
2383
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub, "public_y"));
1✔
2384
         REQUIRE_FFI_OK(botan_privkey_load_sm2_enc, (&loaded_privkey, private_scalar, kCurve));
2✔
2385
         REQUIRE_FFI_OK(botan_pubkey_load_sm2_enc, (&loaded_pubkey, public_x, public_y, kCurve));
2✔
2386
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0));
1✔
2387
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey, rng, 0));
1✔
2388

2389
         char namebuf[32] = {0};
1✔
2390
         size_t name_len = sizeof(namebuf);
1✔
2391

2392
         TEST_FFI_OK(botan_pubkey_algo_name, (pub, &namebuf[0], &name_len));
1✔
2393
         result.test_eq(namebuf, namebuf, "SM2");
1✔
2394

2395
         std::vector<uint8_t> message(32);
1✔
2396

2397
         std::vector<uint8_t> ciphertext;
1✔
2398
         TEST_FFI_OK(botan_rng_get, (rng, message.data(), message.size()));
1✔
2399

2400
         botan_pk_op_encrypt_t enc;
1✔
2401
         if(TEST_FFI_OK(botan_pk_op_encrypt_create, (&enc, loaded_pubkey, "", 0))) {
2✔
2402
            size_t ctext_len;
1✔
2403
            TEST_FFI_OK(botan_pk_op_encrypt_output_length, (enc, message.size(), &ctext_len));
1✔
2404

2405
            ciphertext.resize(ctext_len);
1✔
2406
            TEST_FFI_OK(botan_pk_op_encrypt, (enc, rng, ciphertext.data(), &ctext_len, message.data(), message.size()));
1✔
2407
            ciphertext.resize(ctext_len);
1✔
2408

2409
            botan_pk_op_decrypt_t dec;
1✔
2410
            TEST_FFI_OK(botan_pk_op_decrypt_create, (&dec, loaded_privkey, "", 0));
1✔
2411

2412
            std::vector<uint8_t> recovered(message.size());
1✔
2413
            size_t recovered_len = recovered.size();
1✔
2414

2415
            TEST_FFI_OK(botan_pk_op_decrypt,
1✔
2416
                        (dec, recovered.data(), &recovered_len, ciphertext.data(), ciphertext.size()));
2417

2418
            botan_pk_op_decrypt_destroy(dec);
1✔
2419
         }
1✔
2420
         botan_pk_op_encrypt_destroy(enc);
1✔
2421

2422
         TEST_FFI_OK(botan_mp_destroy, (private_scalar));
1✔
2423
         TEST_FFI_OK(botan_mp_destroy, (public_x));
1✔
2424
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1✔
2425
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
2426
         TEST_FFI_OK(botan_privkey_destroy, (priv));
1✔
2427
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
1✔
2428
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey));
2✔
2429
      }
2✔
2430
};
2431

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

2436
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
2437
         botan_privkey_t priv1;
1✔
2438
         if(!TEST_FFI_INIT(botan_privkey_create_ecdh, (&priv1, rng, "secp256r1"))) {
2✔
2439
            return;
×
2440
         }
2441

2442
         botan_privkey_t priv2;
1✔
2443
         REQUIRE_FFI_OK(botan_privkey_create_ecdh, (&priv2, rng, "secp256r1"));
2✔
2444

2445
         botan_pubkey_t pub1;
1✔
2446
         REQUIRE_FFI_OK(botan_privkey_export_pubkey, (&pub1, priv1));
2✔
2447

2448
         botan_pubkey_t pub2;
1✔
2449
         REQUIRE_FFI_OK(botan_privkey_export_pubkey, (&pub2, priv2));
2✔
2450

2451
         /* Reload key-pair1 in order to test functions for key loading */
2452
         botan_mp_t private_scalar, public_x, public_y;
1✔
2453
         botan_mp_init(&private_scalar);
1✔
2454
         botan_mp_init(&public_x);
1✔
2455
         botan_mp_init(&public_y);
1✔
2456

2457
         TEST_FFI_OK(botan_privkey_get_field, (private_scalar, priv1, "x"));
1✔
2458
         TEST_FFI_OK(botan_pubkey_get_field, (public_x, pub1, "public_x"));
1✔
2459
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub1, "public_y"));
1✔
2460

2461
         botan_privkey_t loaded_privkey1;
1✔
2462
         botan_pubkey_t loaded_pubkey1;
1✔
2463
         REQUIRE_FFI_OK(botan_privkey_load_ecdh, (&loaded_privkey1, private_scalar, "secp256r1"));
2✔
2464
         REQUIRE_FFI_OK(botan_pubkey_load_ecdh, (&loaded_pubkey1, public_x, public_y, "secp256r1"));
2✔
2465
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey1, rng, 0));
1✔
2466
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey1, rng, 0));
1✔
2467

2468
         ffi_test_pubkey_export(result, loaded_pubkey1, priv1, rng);
1✔
2469
         ffi_test_pubkey_export(result, pub2, priv2, rng);
1✔
2470

2471
         botan_pk_op_ka_t ka1;
1✔
2472
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka1, loaded_privkey1, "KDF2(SHA-256)", 0));
2✔
2473
         botan_pk_op_ka_t ka2;
1✔
2474
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka2, priv2, "KDF2(SHA-256)", 0));
2✔
2475

2476
         size_t pubkey1_len = 0;
1✔
2477
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
2478
                     botan_pk_op_key_agreement_export_public,
2479
                     (priv1, nullptr, &pubkey1_len));
2480
         std::vector<uint8_t> pubkey1(pubkey1_len);
1✔
2481
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_export_public, (priv1, pubkey1.data(), &pubkey1_len));
2✔
2482
         size_t pubkey2_len = 0;
1✔
2483
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
2484
                     botan_pk_op_key_agreement_export_public,
2485
                     (priv2, nullptr, &pubkey2_len));
2486
         std::vector<uint8_t> pubkey2(pubkey2_len);
1✔
2487
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_export_public, (priv2, pubkey2.data(), &pubkey2_len));
2✔
2488

2489
         std::vector<uint8_t> salt(32);
1✔
2490
         TEST_FFI_OK(botan_rng_get, (rng, salt.data(), salt.size()));
1✔
2491

2492
         const size_t shared_key_len = 64;
1✔
2493

2494
         std::vector<uint8_t> key1(shared_key_len);
1✔
2495
         size_t key1_len = key1.size();
1✔
2496
         TEST_FFI_OK(botan_pk_op_key_agreement,
1✔
2497
                     (ka1, key1.data(), &key1_len, pubkey2.data(), pubkey2.size(), salt.data(), salt.size()));
2498

2499
         std::vector<uint8_t> key2(shared_key_len);
1✔
2500
         size_t key2_len = key2.size();
1✔
2501
         TEST_FFI_OK(botan_pk_op_key_agreement,
1✔
2502
                     (ka2, key2.data(), &key2_len, pubkey1.data(), pubkey1.size(), salt.data(), salt.size()));
2503

2504
         result.test_eq("shared ECDH key", key1, key2);
1✔
2505

2506
         TEST_FFI_OK(botan_mp_destroy, (private_scalar));
1✔
2507
         TEST_FFI_OK(botan_mp_destroy, (public_x));
1✔
2508
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1✔
2509
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka1));
1✔
2510
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka2));
1✔
2511
         TEST_FFI_OK(botan_privkey_destroy, (priv1));
1✔
2512
         TEST_FFI_OK(botan_privkey_destroy, (priv2));
1✔
2513
         TEST_FFI_OK(botan_pubkey_destroy, (pub1));
1✔
2514
         TEST_FFI_OK(botan_pubkey_destroy, (pub2));
1✔
2515
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey1));
1✔
2516
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey1));
2✔
2517
      }
5✔
2518
};
2519

2520
class FFI_McEliece_Test final : public FFI_Test {
×
2521
   public:
2522
      std::string name() const override { return "FFI McEliece"; }
1✔
2523

2524
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
2525
         botan_privkey_t priv;
1✔
2526
         if(TEST_FFI_INIT(botan_privkey_create_mceliece, (&priv, rng, 2048, 50))) {
2✔
2527
            botan_pubkey_t pub;
1✔
2528
            TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
2529

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

2532
            char namebuf[32] = {0};
1✔
2533
            size_t name_len = sizeof(namebuf);
1✔
2534
            if(TEST_FFI_OK(botan_pubkey_algo_name, (pub, namebuf, &name_len))) {
2✔
2535
               result.test_eq("algo name", std::string(namebuf), "McEliece");
2✔
2536
            }
2537

2538
            // TODO test KEM
2539

2540
            TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
2541
            TEST_FFI_OK(botan_privkey_destroy, (priv));
2✔
2542
         }
2543
      }
1✔
2544
};
2545

2546
class FFI_Ed25519_Test final : public FFI_Test {
×
2547
   public:
2548
      std::string name() const override { return "FFI Ed25519"; }
1✔
2549

2550
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
2551
         botan_pubkey_t pub;
1✔
2552
         botan_privkey_t priv;
1✔
2553

2554
         // From draft-koch-eddsa-for-openpgp-04
2555
         const std::vector<uint8_t> seed =
1✔
2556
            Botan::hex_decode("1a8b1ff05ded48e18bf50166c664ab023ea70003d78d9e41f5758a91d850f8d2");
1✔
2557
         const std::vector<uint8_t> pubkey =
1✔
2558
            Botan::hex_decode("3f098994bdd916ed4053197934e4a87c80733a1280d62f8010992e43ee3b2406");
1✔
2559
         const std::vector<uint8_t> message = Botan::hex_decode("4f70656e504750040016080006050255f95f9504ff0000000c");
1✔
2560
         const std::vector<uint8_t> exp_sig = Botan::hex_decode(
1✔
2561
            "56f90cca98e2102637bd983fdb16c131dfd27ed82bf4dde5606e0d756aed3366"
2562
            "d09c4fa11527f038e0f57f2201d82f2ea2c9033265fa6ceb489e854bae61b404");
1✔
2563

2564
         if(!TEST_FFI_INIT(botan_privkey_load_ed25519, (&priv, seed.data()))) {
2✔
2565
            return;
×
2566
         }
2567

2568
         uint8_t retr_privkey[64];
1✔
2569
         TEST_FFI_OK(botan_privkey_ed25519_get_privkey, (priv, retr_privkey));
1✔
2570

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

2573
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
1✔
2574

2575
         uint8_t retr_pubkey[32];
1✔
2576
         TEST_FFI_OK(botan_pubkey_ed25519_get_pubkey, (pub, retr_pubkey));
1✔
2577
         result.test_eq(nullptr, "Public key matches", retr_pubkey, 32, pubkey.data(), pubkey.size());
1✔
2578

2579
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
2580
         TEST_FFI_OK(botan_pubkey_load_ed25519, (&pub, pubkey.data()));
1✔
2581

2582
         botan_pk_op_sign_t signer;
1✔
2583
         std::vector<uint8_t> signature;
1✔
2584

2585
         if(TEST_FFI_OK(botan_pk_op_sign_create, (&signer, priv, "SHA-256", 0))) {
2✔
2586
            TEST_FFI_OK(botan_pk_op_sign_update, (signer, message.data(), message.size()));
1✔
2587

2588
            size_t sig_len;
1✔
2589
            TEST_FFI_OK(botan_pk_op_sign_output_length, (signer, &sig_len));
1✔
2590

2591
            signature.resize(sig_len);
1✔
2592

2593
            TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &sig_len));
1✔
2594
            signature.resize(sig_len);
1✔
2595

2596
            TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
2✔
2597
         }
2598

2599
         result.test_eq("Expected signature", signature, exp_sig);
1✔
2600

2601
         botan_pk_op_verify_t verifier;
1✔
2602

2603
         if(TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, "SHA-256", 0))) {
2✔
2604
            TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
1✔
2605
            TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
1✔
2606

2607
            TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
2✔
2608
         }
2609

2610
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
1✔
2611
         TEST_FFI_OK(botan_privkey_destroy, (priv));
2✔
2612
      }
5✔
2613
};
2614

2615
class FFI_X25519_Test final : public FFI_Test {
×
2616
   public:
2617
      std::string name() const override { return "FFI X25519"; }
1✔
2618

2619
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
2620
         // From RFC 8037
2621

2622
         const std::vector<uint8_t> a_pub_bits =
1✔
2623
            Botan::hex_decode("de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f");
1✔
2624
         const std::vector<uint8_t> b_priv_bits =
1✔
2625
            Botan::hex_decode("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a");
1✔
2626
         const std::vector<uint8_t> b_pub_bits =
1✔
2627
            Botan::hex_decode("8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a");
1✔
2628
         const std::vector<uint8_t> shared_secret_bits =
1✔
2629
            Botan::hex_decode("4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742");
1✔
2630

2631
         botan_privkey_t b_priv;
1✔
2632
         if(!TEST_FFI_INIT(botan_privkey_load_x25519, (&b_priv, b_priv_bits.data()))) {
2✔
2633
            return;
2634
         }
2635

2636
         std::vector<uint8_t> privkey_read(32);
1✔
2637
         TEST_FFI_OK(botan_privkey_x25519_get_privkey, (b_priv, privkey_read.data()));
1✔
2638
         result.test_eq("X25519 private key", privkey_read, b_priv_bits);
1✔
2639

2640
         std::vector<uint8_t> pubkey_read(32);
1✔
2641

2642
         botan_pubkey_t b_pub;
1✔
2643
         TEST_FFI_OK(botan_privkey_export_pubkey, (&b_pub, b_priv));
1✔
2644
         TEST_FFI_OK(botan_pubkey_x25519_get_pubkey, (b_pub, pubkey_read.data()));
1✔
2645
         result.test_eq("X25519 public key b", pubkey_read, b_pub_bits);
1✔
2646

2647
         botan_pubkey_t a_pub;
1✔
2648
         TEST_FFI_OK(botan_pubkey_load_x25519, (&a_pub, a_pub_bits.data()));
1✔
2649
         TEST_FFI_OK(botan_pubkey_x25519_get_pubkey, (a_pub, pubkey_read.data()));
1✔
2650
         result.test_eq("X25519 public key a", pubkey_read, a_pub_bits);
1✔
2651

2652
         botan_pk_op_ka_t ka;
1✔
2653
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka, b_priv, "Raw", 0));
2✔
2654

2655
         std::vector<uint8_t> shared_output(32);
1✔
2656
         size_t shared_len = shared_output.size();
1✔
2657
         TEST_FFI_OK(botan_pk_op_key_agreement,
1✔
2658
                     (ka, shared_output.data(), &shared_len, a_pub_bits.data(), a_pub_bits.size(), nullptr, 0));
2659

2660
         result.test_eq("Shared secret matches expected", shared_secret_bits, shared_output);
1✔
2661

2662
         TEST_FFI_OK(botan_pubkey_destroy, (a_pub));
1✔
2663
         TEST_FFI_OK(botan_pubkey_destroy, (b_pub));
1✔
2664
         TEST_FFI_OK(botan_privkey_destroy, (b_priv));
1✔
2665
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka));
2✔
2666
      }
7✔
2667
};
2668

2669
int botan_ffi_view_u8_fn(void* ctx, const uint8_t buf[], size_t len) {
9✔
2670
   if(!ctx || !buf) {
9✔
2671
      return BOTAN_FFI_ERROR_NULL_POINTER;
2672
   }
2673

2674
   std::memcpy(ctx, buf, len);
9✔
2675

2676
   return 0;
9✔
2677
}
2678

2679
class FFI_Kyber512_Test final : public FFI_Test {
×
2680
   public:
2681
      std::string name() const override { return "FFI Kyber512"; }
1✔
2682

2683
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
2684
         const std::vector<uint8_t> a_pub_bits = Botan::hex_decode(
1✔
2685
            "5fc44b99d7584f38cd28360cc5625a905b96af12930ed5b5fe2a82fc5aa7dc4b829fe37635f13f5af2a6d3081dad878785698a0aa914374c4e43b89f094a7892aa149a38b49c06a068d829a8d249e753a375d097a0f162e6c3a4dfe8c79761410c605ed3899a3fc44378e14f28879e8f148077e6bc3bb2ae56178c491611bf6aaf5f9a9cb9b5659223007940bcd6f8a23280a56015330e8577259587b12606f4c937ea13606cb3bb046066ad294261e2b22022bcc74678a5520570d88e4ceb42692631e7e3711c4b2fd5347f0328598340cb3c65c8f55ac02716831094cb6eb90f175b173d9c650329aaf513633633bb2ce6858e7447abc41b6fb06da8782572c332b09660366926bf529ed8caaa6243ccdb152b36ba6e47c714145c86f5b3b61de84ef1470d03fa0135e35194fa1fb3bc860fa500d1299aee88ce56054376c1199c553dd90a8d6f9cc763c811d0c66da6f851abf1056635a34a68aa7815868f153a3a5c77fcc8b1eb1807fbf62a6fb43b355700e78230943a2ba1e11b181345b11b4d46266e7b359f074a500c8857d79ba60f64262d662ccd9c8489a4c19df67437db193f95b9765181d9152262b1166f97be53497f001cb1be79024d6a2289bcc704e1b1d821015366a3cc8a484e6bc2e1f1b889f19323e3101aa09ad9ea62ba4005039bbfb5998055f93fbf77b14433116d5958422654dada1127213f02b78717a5a0454271d5b0c02517a6c27a3c3610101d753c09a25571775477dc13b2e404db4965b9a9350330c73a8a3642d39af8a23839ab85c6355b12f279f849813c280d54c5913e99b6946a0aaf012c8cab025396b255f002d837c761d42a4aeb38c5f456aaf79e162700c6b4048eca6f9a7367f90238d67bcf8e6a0d8a553c071522f9d2394e28483d2048be2a8f9c8c8e39991a41273c7eacaefc6a308be870b45b41176412954a1a0fd83d362a5ab288663dec5456b6286d0b2cecb01922fb3d473802ea2b86639bce02450339261cffb114e1e725e90677826a1688f686b29a78779c9822315dafc55753e98c8ed3221f2b3220805c8a28983355207da36fb72f9bc85c0a13b9d041586fd583feb12afd5a402dd33b43543f5fa4eb436c8d");
1✔
2686
         const std::vector<uint8_t> b_priv_bits = Botan::hex_decode(
1✔
2687
            "cf8c33fabbc3e6685f60779a456412e9060a792a3f67d90389815773e06dd2071e3906a1159921485b6221b73dfc723da9d45bbc99523c55b203f81856ba8d38f731c6594a0a9796f485c7cad02ee3a80a737cada2e40b580a1060a1364d365169539ce4d800eab9723153c5b266853b3a33112ccb03491e77f57aeb74c7670426c5bb02615b1907b353beda51f38a788774b1c9eb3c49f89df59ba00e196f180a37fd9acd6691b493d78f95a49906b26fa46125663a37b0b2614197473ba012956bc9348a9afc8907527bb7c2dcbd384b85630b3687096ffd9a93d603121706c06c05baca286bebea0ecdf391ba489d7ff6a7a1c7c3dd0c521c02bb3fa7c3e5b98f19509486ab33f1597346b03c7197865792896c1a553e8379c6f51729e9907d09b4fbc5b4279298f60195998225d95c2aef335c9e6a26f4988e0f19c307fc00f8aa0b21090daab0c540363f121044729322bebb97427227e8b01705826879ab4d42e30bf191068b705527a3ae9a5b3e30371c50b5b75d189903e60175f904c1488be7e872d3dc3be1bb31921027cb075f6bb051c28980c65b9959e8acf2d38671d27285d03419512008335492476e0c23ac6263425f8a65fbc1a38110b38db641f3a9872fa312d26b9c81f11de1d9378b629e9bdc7ca31496ac9511bff42a7a96b28f740a84c2904fe4056ca1989b28439222cee31c554e982d318366037070c4bb778b261e3479c605317922947e4340560732ca8fda63e2a7ac8cc67015e97d308c361e634f8cdb1e836b572367334c949011a9c52f391194baa91f08a71e847d3be202554ab1c4f871c9b26db5f4992db5b3688b1ea877925d02895d470b9659c51213b206b82de489455c6502856b841d000aab3c065ff6720f9222914b6421224d716684a7762fc98ba7f4655f4cfa82fc52472b01b1e22255fc990b20240b802034c99777b71b5c0d8a4c733b69d5f76ddfdb1128da3e194c1169a924f5d868d1dbc4ddcb30197c25005391c1fcc098010af1e9ab72bc8c3bd1c58b15583035bc2dd0898e148fb20b8c502a31489b00dd645ab24709bff5c37d1a7fee5c56d7490ae77a61b1c5951e359d2a89a946f200b1f647c0283de5d403282c693dfccca1c1b54804ab86f36e1f16550a77b75328916c770c225b142dfb750659ba6d532003f8b6d48825b122682051acda7c20f09117b3e5c93e8c1bd446672512947d964f0484a05b85af82f4401d323484d23373c0c6c8990a3fba70f8a2c13e7350ce945c8f4882b1929f10d18bb45a883c116d1ce06493a50580069743d26062da9171006405d27e2f7220a897024dca507b737a6c32ba3d925e15c9789589507a768ca075beb2fb17a17144c52810566a972dba60ad806a2ff15957a3bbd85a8f1ef34445d18cc668ca7a8ac5fc7545934221a4c92c60024069cc34d592ac30c88c3376bc133622e2b192e9b4b60a9a84e5304873e62d6f98103cc8c43f6024fb7739418cc2602b21416040220cb3ec588679c033d61a3c697c403463c17c13a63d7304044a9f024b907b440b39b7b6423723dda943447a900af0170144755d9a968f634e26f892ee19cd00a97b48d5041b4473ec8741e077126f85bbb8334b8b2cbd63429afa543a540740c1e56893f6b2f2f7c42a12bcd48c647726cca06841212656ff9b3d3e799e92c48414d653a61ca8dc2c49b7342469a868b7938db8d7844e6311134b2c30538ffa927ee3961a44e0a66438c5643aa1d13658fe8a7c2a84702a8211422831994ca1e801131ab88a25162d3cd988ebe09e6cb01ca7324256691635c536f576990664841b7c89b0dc325472271fdc2b0824a9514b5eb46a743f9734ee20648a407f5d505cc9614748f344a16950b5483e45516236d43afd1494d564c23e20b64a124593604404c879a776a5bd9399629600e86aa1a641be7a13418cda318bbc3af0c2a66c999841251a1c2868b05028f6a56a72c33d8a653c77f69af4247dc6f80d329921ce3355894605c35372ddf0c84cacb42d6ccbdf39a55b672a891749622a011747a01625764c788413f2c0fd877275c88fd5730fa9e87a3c783702223a4443525cc5b5381c976dd9cb08ac47490125ca5c70baac01ff9143bc40ceeb2a16c1529d70c07074a0013749656ff4b16d890868b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb3338e5d6352d5a5006d7cd901489e7f851711c08e00cd4162ccfc2564d5893d52b2c7300e2d894b0eaa40a6ab254506d8c1176a33c4a1b2879604b1b80df48d31dd");
1✔
2688
         const std::vector<uint8_t> b_pub_bits = Botan::hex_decode(
1✔
2689
            "ee5c56d7490ae77a61b1c5951e359d2a89a946f200b1f647c0283de5d403282c693dfccca1c1b54804ab86f36e1f16550a77b75328916c770c225b142dfb750659ba6d532003f8b6d48825b122682051acda7c20f09117b3e5c93e8c1bd446672512947d964f0484a05b85af82f4401d323484d23373c0c6c8990a3fba70f8a2c13e7350ce945c8f4882b1929f10d18bb45a883c116d1ce06493a50580069743d26062da9171006405d27e2f7220a897024dca507b737a6c32ba3d925e15c9789589507a768ca075beb2fb17a17144c52810566a972dba60ad806a2ff15957a3bbd85a8f1ef34445d18cc668ca7a8ac5fc7545934221a4c92c60024069cc34d592ac30c88c3376bc133622e2b192e9b4b60a9a84e5304873e62d6f98103cc8c43f6024fb7739418cc2602b21416040220cb3ec588679c033d61a3c697c403463c17c13a63d7304044a9f024b907b440b39b7b6423723dda943447a900af0170144755d9a968f634e26f892ee19cd00a97b48d5041b4473ec8741e077126f85bbb8334b8b2cbd63429afa543a540740c1e56893f6b2f2f7c42a12bcd48c647726cca06841212656ff9b3d3e799e92c48414d653a61ca8dc2c49b7342469a868b7938db8d7844e6311134b2c30538ffa927ee3961a44e0a66438c5643aa1d13658fe8a7c2a84702a8211422831994ca1e801131ab88a25162d3cd988ebe09e6cb01ca7324256691635c536f576990664841b7c89b0dc325472271fdc2b0824a9514b5eb46a743f9734ee20648a407f5d505cc9614748f344a16950b5483e45516236d43afd1494d564c23e20b64a124593604404c879a776a5bd9399629600e86aa1a641be7a13418cda318bbc3af0c2a66c999841251a1c2868b05028f6a56a72c33d8a653c77f69af4247dc6f80d329921ce3355894605c35372ddf0c84cacb42d6ccbdf39a55b672a891749622a011747a01625764c788413f2c0fd877275c88fd5730fa9e87a3c783702223a4443525cc5b5381c976dd9cb08ac47490125ca5c70baac01ff9143bc40ceeb2a16c1529d70c07074a0013749656ff4b16d890868b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb333");
1✔
2690

2691
         botan_privkey_t b_priv;
1✔
2692
         if(!TEST_FFI_INIT(botan_privkey_load_kyber, (&b_priv, b_priv_bits.data(), 1632))) {
2✔
2693
            return;
×
2694
         }
2695

2696
         std::vector<uint8_t> privkey_read(1632);
1✔
2697
         TEST_FFI_OK(botan_privkey_view_kyber_raw_key, (b_priv, privkey_read.data(), botan_ffi_view_u8_fn));
1✔
2698
         result.test_eq("kyber512 private key", privkey_read, b_priv_bits);
1✔
2699

2700
         std::vector<uint8_t> pubkey_read(800);
1✔
2701

2702
         botan_pubkey_t b_pub;
1✔
2703
         TEST_FFI_OK(botan_privkey_export_pubkey, (&b_pub, b_priv));
1✔
2704
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (b_pub, pubkey_read.data(), botan_ffi_view_u8_fn));
1✔
2705
         result.test_eq("kyber512 public key b", pubkey_read, b_pub_bits);
1✔
2706

2707
         botan_pubkey_t a_pub;
1✔
2708
         TEST_FFI_OK(botan_pubkey_load_kyber, (&a_pub, a_pub_bits.data(), 800));
1✔
2709
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (a_pub, pubkey_read.data(), botan_ffi_view_u8_fn));
1✔
2710
         result.test_eq("kyber512 public key a", pubkey_read, a_pub_bits);
1✔
2711

2712
         TEST_FFI_OK(botan_pubkey_destroy, (a_pub));
1✔
2713
         TEST_FFI_OK(botan_pubkey_destroy, (b_pub));
1✔
2714
         TEST_FFI_OK(botan_privkey_destroy, (b_priv));
2✔
2715
      }
5✔
2716
};
2717

2718
class FFI_Kyber768_Test final : public FFI_Test {
×
2719
   public:
2720
      std::string name() const override { return "FFI Kyber768"; }
1✔
2721

2722
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
2723
         const std::vector<uint8_t> a_pub_bits = Botan::hex_decode(
1✔
2724
            "38d4851e5c010da39a7470bc1c80916f78c7bd5891dcd3b1ea84b6f051b346b803c1b97c94604020b7279b27836c3049ea0b9a3758510b7559593237ade72587462206b709f365848c96559326a5fe6c6f4cf531fd1a18477267f66ba14af20163c41f1138a01995147f271ddfc2be5361b281e6029d210584b2859b7383667284f767bb32782baad10933da0138a3a0660a149531c03f9c8ffccb33e3c3a5a7984e21fab26aa72a8a6b942f265e52551a9c800e5a44805f0c0141a05554213387f105df56458496bd8f469051886da223cb9fe78e7b390bf94b0a937691af9550082b76d045cb4d29c23c67942608d078a1c80f24767a945d19f077d82c9b9b197073464abe69cf7c5626177308f384672d5263b0c4826db4470e1a70e4751e3918abe8fcbc3bc0531ae89e5512214b5cc94a16a014bcb3826c79fbf4add0825eeefbab88cb7cff37bb8d491f8de902578a1e961655565b7718782a23504fdc13c783f130e177925e305d1fbc63cc8c15c2c67f85500cca785de9f480490558ef71aaf0fb5b513914401269b309c4c59c64d2a757d8855f58465615925f1ea6812cb143fff383e1048e285118bf932944b86fbdf4b1b9e65685664a07775c46952aaada1168f54b47c7a231e7355c64637467b5a3c09cab67bb35f58640c2726283bb63530a15f66eca48a840c00ca8862e283c73bfbb413a2915b8d1159a043f12c59bfa828248249b76106faa61a127a0280c586350e7a42cb74ca49cabd606891ec7cb8e84affe4b2e14c71658332b755611bab7977fa76ce736b21ed34a17ac0ec3561ca9b282d4a2bc407697924b1cf918ba83d3a4fdc82564c95bd904bdeee91ed6ccb36baa88a05c80712901bf280aee6538ec2078c2a84ee5862fc137cd92e97968d69fc3453a1e1cb161c50c9f2473a0d09037b188a0fa01efc344c2ac8fe8592b0a58456662a95033659a158a2d90a6e50c253a87975785ce29c4570000a154d4b3b2c642205c8c7cf9ac6b1071fbb368ab950a744b88c95ba5243017831120a9048338d29847830d12a933a09abd21a46b828cb14e808cd35129c9dc6e5b931d4a126fefe07909618e2b4586e7b6b424963b7323ba505ba112bb9b834a7d1b78ad0df53d556a1c69369f09148b1dc9938df59223f087fd6833be5b2bc2651fe58911ac01467f9297dfdc22b41a0f1702718710b78cf35b1865813a896d45214d338155b6c043c532330c002d520739467a504a866637fb3451c849f8f83e6a94147f168da53acdf9d8affd968a84124a9abc09af960cd3b29f2344831bb41e67605eebf00df202857117399dd748b6514aed61bb2f6cb841d168d5f35e20054573a331cd4882a04b072c179158825bcf471266da0dcceab1a021c73254751d5a161c1a92062c220a217a69d9823314b4de996fe8d45f6db5af16c1561495a4c43090bc394c94e1b0ec738eb56267201c2ecd1c7b4993c0efc0284bdc9a091c294f95703a7178822c8a95b79b1e4591e0998d893875c1a879c08a073cc67df426bba792c18ae6c1feba879bec54812c2affa012973b700ad48e271078280864268600a7aa309eaa1098750a0f8a522eb929577b412f7855613688b72f9bc85c0a13b9d041586fd583feb12afd5a402dd33b43543f5fa4eb436c8d");
1✔
2725
         const std::vector<uint8_t> b_priv_bits = Botan::hex_decode(
1✔
2726
            "9bc4986eec853ac90896c7300f914216773baa687f57f81f94e6b5a26515c0a74c73b19dc6b9888755a1d1db6a1128b02c5144ed9ba75e874a68a14dee3a9bf770b767121fbbdb292e097444757fe2f324a875b0a07a4fbdcc8fd6137fce80c69f412be103b01f8b3bed392600f830d1b20d73ca0b386666d16c5d96db10e309041890bfd4ab7bdec27191979abe7637e76153918cc2af1545724abfd95ca496281a0cc6ca63a87819b75aa86452e5027d429cad64a9c029112a3a7b9fb993c6a6d0671f6c9e24986b83f67cf1236d94c051559616826a947eaa15074025d1ab810c80220e8a8c2c222d4e8594bf35811b444a57e9bf94fb2479d174d6b38c8c3b4730437c244b2513232ec798adec4a8e597bca83bca5948468f93c022ba65311a91e3d95c299c70c3f1a43cd0c83dd500bd2211b9b385b82f2c003b10d295765b3e367563a5f19033fc1231db9620f835c95f5a261190a5da1c24ed528316f0a1520d223e208a8c99b24d28598ade74fc88370e7f45102c5a6411891c661b356b9a32e1cc0fafaa085d7670e8bcb5e768eb255204f2445b5b73b04079881903a553c865744343a925c7899777b1c9dd5579a481512f8157061606a9a67c041d38bc179048be17dd9e19dc0a572bce595afa3b68ff21bf78a63a7560636b6bb01ec3362e2aaabc8965818b7f2417ca8f66a5a2a67f72a3931e125d638a872862a7b680a54aa1f25d90dbd567635ec6664919e29517325a5c5048cc8d1c31af5e4866e85025b9184a7b75ed7f2c9c8d88259fa2ec5b05ed3751584f88308c67ff1a72fa2453305160baf404db7d4be56f1547b20bb7dec23f02b10db584b28ca40d8b39c1c3ab9f3d7bbda0822604ca48f26694d69810aa888ae3c0983c5ba4cb74211f7a5361ccdee694f4202aebe13a75b1b2dda1b3232376be213582afc4fde3474766671fe865e2fd98384eb65b2f349f1e24269b91bd9d08c80849735a9951304afd130b5c2211314630aed4b6ac3b1252a0999ff5a3ec26a283342389d219fd243706128b389eb2334fb2a6184a4eab6735d7428df5633ce030b8f853ee47c4401fc5d06b43c9b66b7aeeb23b5f000a30a6f88f027ee3994fe8b63e51b83bc24bb733a3773a35cbe138f6d9c91a3a3898bca6776030d740ac355176547d624719656a9a44e91c63faf7699dc6c2c45575718d48828828b39043c2fda2af416837efc38d17c56d4b63c63a5ab43434647d029f7b236b288958f06910763610f8b2f027a8dcd780039ab34a6871427476ff6500240e83b87c95dcfa45ac5315ef34b343fb609eb296e915c849bb8c57f57c69b177eaa8456377403fe8c6627a3282d45308f675d67085a15f0b1b55aa2a8f21afd6c05c3c00e9eb8c32418cb41963ce427b43e7545c58325c7b9368db2333de424dbeb3430f007d18a68d73b7dc67960b28206a68a1be400a770b5cd9d45a72824ca00345ac56491c1414fe5287a2eb2ad61f3bdcd0c84c335b04a703425d79dbd02b0a0e90de5b331c3c29f6562969e04cdf7095b2a7646b3d006b0b83cb68580b5ccb71de1b4d9f131bac133d6088e10613a00599d81d4818403a4bea83905304cc45ca645a9b2c6484cf9490f1755c744d9988ed60475e6ae44355ed15c7b549366f29581ec2721fd6704e0ca3f878812805675141c0a15a7b7ac35a9e3f8b2a010bc184981c57852895b2695d56131e32326717f6b101df1bc82b3ba0222d52656d118538c4ca3416be1c76ba37a9901a36e4883be6c541f2bbb561818cd2f136b98f658250545c1fa5bcfdc04374016db1c5132447fd6d568866451c25412f72967de868eaeb9c546fb40ea88cd84a1a586ca51c74bc9c3e56e104323afb658d1ac003151bdc35879a4b6762648bff0caa682f1b3319805d2326d5a46af832aacefbaa1ea820568ea3925870e9b6577eb93898e1b0cfbd1c995cb4cd6cabb979813819749b40a9952f50e97c4365e777dcfe9084219294c205acb350e07db9c98f53444546460962e2bf5aa1cc12273d882fcc215a7397b3f9b307c56b9a0429b30f88453a2376669b28b4bc4b84b51714b6652b7a1a0b53e9ec61b55ca51cdb38243239ee5f18243e515f178768a888f475b3d9060136bb22ee355b3da02c16ad83bdbb4aaa13809cb4bcd5bb53710737d3883632f9254e3336af61621a376720572450e3937c0d930e349adbfa7642ff822b9135e18f943e0178617604d10e0c09ffb3e09783c09d12cbe93311757af9857b77d1488ed39321ca97c3745c5bc176ca81274c8321bcb2029938b32bbd01aec137032f9760849701649120050b50d8353c36b8bb7724a67e7660fc93324065d63912c35a86d8fe60683067bbd2685c552ad8c65c77c57c937676fd61595c453174bf996e9d3a9ccb837b25464115c1ba3343ac097b80735aed3225091167cd8f841ffe49c5e698ef542124253084623179394433a4b61547b9ee09c98c2736ea086bd69d1bc7ae68a6ae5ce682a215860006b4604dee45a3e212f97643acb77a79a880382e483537c5198d4483a176d25aac9c3670a3f30956f18ad441904776bcd48131c7d6465bce0c133010f3176c92ec962f5c6b84d4c3ae949619cf48172997ca3b1ccf5c8cc7a67923e1295801048a3d40ac4f2c6467c750fc71314a0c1fc22637dd52e7ea50907ea973d765a6a9bb2b11aa405b9187f72026696710e61af3e41c33da1a05eb65e6523704f078e74e32f10e00247967aee3a8c6546889ef67cd613ad7236583f2104122ac6c6a40a84dc96d81c569e76a952c0a25f396e48337a4fe029a4c91cc7406872706a55573b75f160a4facad7c85fab141c63454bf48990729096ce9965604c7cc1e60ae6868dcc41bc3df71c3e5593f0488b0c6a3063e817f9f4bacb17599c8666ff3591126b4891fb7f5d29660bab60cf5007043a4311d41ab3b29787184f3d3c9ab7cc247f635145b67e970505ba44ad0e06b11ec5cda4175295199d19d660204cbdc17947cc66442d3a2cd408a20fe98174f31ee4e5bb3fa8bcd102bedc26527e9bae836442978a6ccbe510f93ab77569ab1f09d0e6312dd0cc0bcaf095fa8a52a7212d14714a7bf852416c9b026301bd965c30a43d24d97298346a46b2c4bc814ba4059653358b03c9456c60bf0193932eaa2f24ea8e4b010a5a4425ce4540fbab90d8e55c97ac2687f15ff5299278824a08d4743e1a62e1c6619cd3278cd75a97a5b4e3a38668b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb3339ae9c9ce46d9da0e714c0bae8712a670d0e5dcfdd1dd0d045932c79c559b2ab3c7300e2d894b0eaa40a6ab254506d8c1176a33c4a1b2879604b1b80df48d31dd");
1✔
2727
         const std::vector<uint8_t> b_pub_bits = Botan::hex_decode(
1✔
2728
            "f9490f1755c744d9988ed60475e6ae44355ed15c7b549366f29581ec2721fd6704e0ca3f878812805675141c0a15a7b7ac35a9e3f8b2a010bc184981c57852895b2695d56131e32326717f6b101df1bc82b3ba0222d52656d118538c4ca3416be1c76ba37a9901a36e4883be6c541f2bbb561818cd2f136b98f658250545c1fa5bcfdc04374016db1c5132447fd6d568866451c25412f72967de868eaeb9c546fb40ea88cd84a1a586ca51c74bc9c3e56e104323afb658d1ac003151bdc35879a4b6762648bff0caa682f1b3319805d2326d5a46af832aacefbaa1ea820568ea3925870e9b6577eb93898e1b0cfbd1c995cb4cd6cabb979813819749b40a9952f50e97c4365e777dcfe9084219294c205acb350e07db9c98f53444546460962e2bf5aa1cc12273d882fcc215a7397b3f9b307c56b9a0429b30f88453a2376669b28b4bc4b84b51714b6652b7a1a0b53e9ec61b55ca51cdb38243239ee5f18243e515f178768a888f475b3d9060136bb22ee355b3da02c16ad83bdbb4aaa13809cb4bcd5bb53710737d3883632f9254e3336af61621a376720572450e3937c0d930e349adbfa7642ff822b9135e18f943e0178617604d10e0c09ffb3e09783c09d12cbe93311757af9857b77d1488ed39321ca97c3745c5bc176ca81274c8321bcb2029938b32bbd01aec137032f9760849701649120050b50d8353c36b8bb7724a67e7660fc93324065d63912c35a86d8fe60683067bbd2685c552ad8c65c77c57c937676fd61595c453174bf996e9d3a9ccb837b25464115c1ba3343ac097b80735aed3225091167cd8f841ffe49c5e698ef542124253084623179394433a4b61547b9ee09c98c2736ea086bd69d1bc7ae68a6ae5ce682a215860006b4604dee45a3e212f97643acb77a79a880382e483537c5198d4483a176d25aac9c3670a3f30956f18ad441904776bcd48131c7d6465bce0c133010f3176c92ec962f5c6b84d4c3ae949619cf48172997ca3b1ccf5c8cc7a67923e1295801048a3d40ac4f2c6467c750fc71314a0c1fc22637dd52e7ea50907ea973d765a6a9bb2b11aa405b9187f72026696710e61af3e41c33da1a05eb65e6523704f078e74e32f10e00247967aee3a8c6546889ef67cd613ad7236583f2104122ac6c6a40a84dc96d81c569e76a952c0a25f396e48337a4fe029a4c91cc7406872706a55573b75f160a4facad7c85fab141c63454bf48990729096ce9965604c7cc1e60ae6868dcc41bc3df71c3e5593f0488b0c6a3063e817f9f4bacb17599c8666ff3591126b4891fb7f5d29660bab60cf5007043a4311d41ab3b29787184f3d3c9ab7cc247f635145b67e970505ba44ad0e06b11ec5cda4175295199d19d660204cbdc17947cc66442d3a2cd408a20fe98174f31ee4e5bb3fa8bcd102bedc26527e9bae836442978a6ccbe510f93ab77569ab1f09d0e6312dd0cc0bcaf095fa8a52a7212d14714a7bf852416c9b026301bd965c30a43d24d97298346a46b2c4bc814ba4059653358b03c9456c60bf0193932eaa2f24ea8e4b010a5a4425ce4540fbab90d8e55c97ac2687f15ff5299278824a08d4743e1a62e1c6619cd3278cd75a97a5b4e3a38668b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb333");
1✔
2729

2730
         botan_privkey_t b_priv;
1✔
2731
         if(!TEST_FFI_INIT(botan_privkey_load_kyber, (&b_priv, b_priv_bits.data(), 2400))) {
2✔
2732
            return;
×
2733
         }
2734

2735
         std::vector<uint8_t> privkey_read(2400);
1✔
2736
         TEST_FFI_OK(botan_privkey_view_kyber_raw_key, (b_priv, privkey_read.data(), botan_ffi_view_u8_fn));
1✔
2737
         result.test_eq("kyber768 private key", privkey_read, b_priv_bits);
1✔
2738

2739
         std::vector<uint8_t> pubkey_read(1184);
1✔
2740

2741
         botan_pubkey_t b_pub;
1✔
2742
         TEST_FFI_OK(botan_privkey_export_pubkey, (&b_pub, b_priv));
1✔
2743
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (b_pub, pubkey_read.data(), botan_ffi_view_u8_fn));
1✔
2744
         result.test_eq("kyber768 public key b", pubkey_read, b_pub_bits);
1✔
2745

2746
         botan_pubkey_t a_pub;
1✔
2747
         TEST_FFI_OK(botan_pubkey_load_kyber, (&a_pub, a_pub_bits.data(), 1184));
1✔
2748
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (a_pub, pubkey_read.data(), botan_ffi_view_u8_fn));
1✔
2749
         result.test_eq("kyber768 public key a", pubkey_read, a_pub_bits);
1✔
2750

2751
         TEST_FFI_OK(botan_pubkey_destroy, (a_pub));
1✔
2752
         TEST_FFI_OK(botan_pubkey_destroy, (b_pub));
1✔
2753
         TEST_FFI_OK(botan_privkey_destroy, (b_priv));
2✔
2754
      }
5✔
2755
};
2756

2757
class FFI_Kyber1024_Test final : public FFI_Test {
×
2758
   public:
2759
      std::string name() const override { return "FFI Kyber1024"; }
1✔
2760

2761
      void ffi_test(Test::Result& result, botan_rng_t /*unused*/) override {
1✔
2762
         const std::vector<uint8_t> a_pub_bits = Botan::hex_decode(
1✔
2763
            "9779a4d1fc45ec261f048b9c9daa38c9ec228b6505e8905226b38486802059c2c5c89601560634cb1337b1315365144842bc405a292e683cafa4514526945c4dfb68ee2acdb8b79532836696d53125a045bdbb8a3bcc8123083d1e682c5bd7820c76c448351151474f69d601d7708dbb2c979d77527494b68520a8ff66c34162ca2aec8072a2a51ff259389648e75b95c16abe14604edbfabaf400b76a7a0f07db61dce19102f43b2d1060747b02c4425485341fd5d9bffaa7016061374963b985209c6b9a7db3f94958311d027900a3d8c44f8435b093a236a0509f1928df7719ce8c4e90228f4db87cb9e882f2712180238845d31eb906c60eb63e0ff55e84b867a91b79ae74a03ac00473c8c1b3e6aedcc37f1e69b8e136019bdb01c374a122e164c32584b2fbcf5e013a45127c893326b6860378cb5525a48b522b30132b7688214b69808dd19aa4ff033e16252016fc9479222424393e51db7115e38114e6893cdcc8ae1117a4316548010ab4629fb672148d031a6c601a6a4a661d872d8ef693750a115958716ab9263eb0516357102358cde5256464805ca8f59661751ad6a475a7ec78cb7319c3bbc544e1bb1185aaaeb751ba0b3923246e33f40a4ff4780b745362a218d169474d7208a9f7577228308af20f8d2403a27477cf53cff133a247d5c2b298bf21bac6dc44649e63afcef54ad3a07a74e447b36bacca295e053368ef7c146e1c28edb31b3777c941a7c27dc559437b0ff83c2910f827ef244d7726af2b9708654bcfb139a26844c6f4b79cd9e4747065a77596b927f96cbdbc7267ac9e32ac396f6c4ce739aa0aa60fccac178edab1e04688fe71a74201a99bc64b55f8cb89e11545e5275af48626ec667520ba8a7c88c2307e793455ef8780fae16412ea59e92406f595902dc21c612b142801cf31abc0475c7bde772d51c55a2dfc651bc5ab4143022813cc4c8cc6f52c8d27e3cd7851849a8c4343f0c7ed6c6cb9f765c5047e55bc48aad5b932128287f70939db753a61c0d7db1830c256288c1c85cc9bbce58366dac528c893d61b69850bcb82758e836936e1f8163861586482cad35b4db1437fad6980b66280b1652d72f52407a8015f85c29e75b2158933a4f5887db320b583cdcbca274eac21f8702879b8bc4afb62ba056d146512921554c765464d4c96bd3c9a9720187c3339d0593bc4bba59155469ff6b8688a5fc5fa6b46d4070668bd168c5f796492760940999927628236b2412968438d2cbc2a978abc097320291b0ed7e4631ec9bbe28ac63ab6860e976ac1552afc43897d6937a092432dc481a914c3a1273c43009e8ec523cb93ced9898f90905340ba6e01bc572d03a893fa21c4342d88dc3e77b33b1fc063dcdb689d8c4ffc840ce879cfb3471385512cc7c7591383b1fac89832bb1cb3324e6d2868918844cb20ca4df812b42824192a43380430a37a7f3ac41ed064cfe055157b91b5c0108fb7a613a0112af4d64e48f8a345328a99b7bf0c91cb037215b0177ad4c263e6d78f5958c848158e4fd2117c248e0cab3cf98c1e27868838f8428ba0562df6b61e8736e2b8b266607918e0ce2ce3af67c81fa5c2a4d2bc8e871825b702b3beca397b33a518dac8b1393d494a90900c01b55925857cbf8027051d36a4f141a4d2dc440341305e033cb50a074be459c76a339a9a52c447360dea2a5699ccea683a42430aa6fc9545c75c0492641af7a4e9267bcc384e55714cf49741eba3b69d6417a555475d3c021112b8b3588c63747b5ce2ccfee91297fa419f9c4298978fff0870d8a855b48899ca9bb47d836d62d2038cc3816f3a698bb3bbff78c7a015b0ea1960634292e1d50b03e1043a98ca9b27063e668b05e2c17d692d382b181365a818518ec747720337ca1868596af42a90fab1870373d74b8f6d42ac86b18bca3ab04764680cf2060c529abe5b8ef4474dc8cc47a8033ebd884d3e0a0f1a94420d8a3c9162b7af87a2c8a394647434f4a3bc2b477813ac82cf387185b587f7f68938eccddd6934d143ba17bb4a712566d2a55aaddb3323713667401b4a20b86c23bf1076439cb6b8c115389dba4e6f0c915be7602b9703b535070e4a5c5649a5d7080515026706b2604575cbd0687f2729a92a997d21ea7200c41ed8315275f4c7b72f9bc85c0a13b9d041586fd583feb12afd5a402dd33b43543f5fa4eb436c8d");
1✔
2764
         const std::vector<uint8_t> b_priv_bits = Botan::hex_decode(
1✔
2765
            "9bc4986eec853ac90896c7300f914216773baa687f57f81f94e6b5a26515c0a74c73b19dc6b9888755a1d1db6a1128b02c5144ed9ba75e874a68a14dee3a9bf770b767121fbbdb292e097444757fe2f324a875b0a07a4fbdcc8fd6137fce80c69f412be103b01f8b3bed392600f830d1b20d73ca0b386666d16c5d96db10e309041890bfd4ab7bdec27191979abe7637e76153918cc2af1545724abfd95ca496281a0cc6ca63a87819b75aa86452e5027d429cad64a9c029112a3a7b9fb993c6a6d0671f6c9e24986b83f67cf1236d94c051559616826a947eaa15074025d1ab810c80220e8a8c2c222d4e8594bf35811b444a57e9bf94fb2479d174d6b38c8c3b4730437c244b2513232ec798adec4a8e597bca83bca5948468f93c022ba65311a91e3d95c299c70c3f1a43cd0c83dd500bd2211b9b385b82f2c003b10d295765b3e367563a5f19033fc1231db9620f835c95f5a261190a5da1c24ed528316f0a1520d223e208a8c99b24d28598ade74fc88370e7f45102c5a6411891c661b356b9a32e1cc0fafaa085d7670e8bcb5e768eb255204f2445b5b73b04079881903a553c865744343a925c7899777b1c9dd5579a481512f8157061606a9a67c041d38bc179048be17dd9e19dc0a572bce595afa3b68ff21bf78a63a7560636b6bb01ec3362e2aaabc8965818b7f2417ca8f66a5a2a67f72a3931e125d638a872862a7b680a54aa1f25d90dbd567635ec6664919e29517325a5c5048cc8d1c31af5e4866e85025b9184a7b75ed7f2c9c8d88259fa2ec5b05ed3751584f88308c67ff1a72fa2453305160baf404db7d4be56f1547b20bb7dec23f02b10db584b28ca40d8b39c1c3ab9f3d7bbda0822604ca48f26694d69810aa888ae3c0983c5ba4cb74211f7a5361ccdee694f4202aebe13a75b1b2dda1b3232376be213582afc4fde3474766671fe865e2fd98384eb65b2f349f1e24269b91bd9d08c80849735a9951304afd130b5c2211314630aed4b6ac3b1252a0999ff5a3ec26a283342389d219fd243706128b389eb2334fb2a6184a4eab6735d7428df5633ce030b8f853ee47c4401fc5d06b43c9b66b7aeeb23b5f000a30a6f88f027ee3994fe8b63e51b83bc24bb733a3773a35cbe138f6d9c91a3a3898bca6776030d740ac355176547d624719656a9a44e91c63faf7699dc6c2c45575718d48828828b39043c2fda2af416837efc38d17c56d4b63c63a5ab43434647d029f7b236b288958f06910763610f8b2f027a8dcd780039ab34a6871427476ff6500240e83b87c95dcfa45ac5315ef34b343fb609eb296e915c849bb8c57f57c69b177eaa8456377403fe8c6627a3282d45308f675d67085a15f0b1b55aa2a8f21afd6c05c3c00e9eb8c32418cb41963ce427b43e7545c58325c7b9368db2333de424dbeb3430f007d18a68d73b7dc67960b28206a68a1be400a770b5cd9d45a72824ca00345ac56491c1414fe5287a2eb2ad61f3bdcd0c84c335b04a703425d79dbd02b0a0e90de5b331c3c29f6562969e04cdf7095b2a7646b3d006b0b83cb68580b5ccb71de1b4d9f131bac133d6088e10613a00599d81d4818403a4bea83905304cc45ca645a9b2c6484ce4f7bfbe3597f0c2c50784065abc214b5518ed8c7d427205392987d807cdfa571d09932e0a1217e7c5703dfc9be7c4a0f2f33338f316b8c203c6b84285a0957c363d3722145b592f9d6277b1b7446aa2005663b9898901da3b8978eaa8aee87a209c72bfd6cd10a2bedac527f26c419b4bb9a4ca13dba14cff5671461c4cff238d213988120c3cae3b15b607269420c14a9b23a6bb887ad048e6688528137dd5c071172bcaddd5ba4fb9acfb1712ca04aa267b0686a237414b16c4ac03525a45513a393605a29f3b4c07dc5cd082c67b770e69723c6425a23f573216c4cabd6c9c39533f16643bc6151672c466dea63fb8e2180fbcb453c5670b01c0fc7043d198b6c320bf0dfc5016d00f46dc0ba75c7369fc13b65733301b3a92c0cf8dcc1f01e33291a9974d5b5debfa3be5531c69a066a6f4230ba778502654b7e76191d3ba68b1488c848d8ef11bb9ac5a5374af6ccacd02432b247a963685127728825a162c43c1cc79a7b60f571481a3a635a18f0050cbe6f4af1a5a2224044622bb3893bc837be699d5279dac78c9d49818ebc91d4ff6cb54b294e4c378d8eb584f561d21ca87b7c77b1c551f52112ba72c4b6514bb286856e89b48b0a62bad093894474b246a515f849056c9aeb572060ee11b93e236204a8ff1784ede0c3f4c0cd0cdb2a50bd4bcb1225c0dd2caa8a129fdcb01f0e1a75da465b6b8742e4946dbe3ab21ca6c4e592d398a5700375b7b770c17e117663b51313824854585ece4a1520c59a513555f548afdc6748eb9b770b7af8adc9b3c1861efc00ebde93f91fa1539c3411ec9b154c93d1f508b9714cdd0aaabcb436951aa54978b84faa8bd838c29a12657a6389fa658237326bf80e44cff183d9c9107d30c7b4df83908a8badbc1bb5328ae61e6451291710408196261bb913b285c168fe5c7456cd74970377330139beb4c8b4c00c5954254a748308bc30f3a1ccabed83628422e5515b294e80bfed024902a81518c0e6827caa13230016b72aec10d31aab9f9b072652486c3b779c845804d001b223b4065667a5e318b4e0b27c41a099168ce763ac245c1c26354cfc8214cacf01dc8c99c5fc855639cb7486a7a6b71766d62369ee6349f521591344fb8026099fc055c3a0ca2002271c9361f52acaf4675a97b3593f3447b284ad69a9711501411e14e48f080e2a0bceba07a02f79dc15c3c971c1e671a66bf7144395a94d08c46603c4369b25b38d924693492f6500745cb1d2a037e2cd814fac70d8254aea4526e061648d227350dec1ba6305299a45bde4029a9f63e4f62acb5246ba8b54208a37b98fa77e4b720f02b18969635cc16cb6e236ff78111b621cab0339c60b529a9207c94a74925aa3d177636a6d205d72b429f614d452358b98582e42c2f3a32c94202a8625a760483807bbac74c083a7a786ae7b46aff3810a5318e3f479e67e83b4c94587d345ffda17273a55e0432281c670e36b76539263636445e4d110fa1c92b2e65b1ee993b26c41c8c66b181206ff559728993503e7611fa0ca24156642d29a52cbb6aeff88fa1bb790b4344472503ae77967d11a904550a95cc47c3435f1e44b4b890cf5dc4cbd7486a6cab60d1a6622e40c67e854ed0e6a0c4c2aa92a29731e69bc6b3ca9e83893df4bdb37338055bc16bc7cd875b0691d0212109b77236b10b0c72b7a76e9ce346061028be3271c480435e2482d3d2733ed619c2d3698d29a3225a7731a62db4f214a0b11569e91895fbccb8a92d18f94d307a542ff96d2ce8c2bbaa8fab948c83365162268124cabe934b3a5e6530b7f74397a980c243bdce2989747565dd9ba65a7498aa7c3ee711c3be239e93cb45134c082a900cc951756afa42e74a65001822cab95f41798fdef89ea8a751740842e2f7a271e98dbd5158d7455fe9b9cdcd0a0f68c91e59b00979673a1deabf8d213971f994c0bb08899a91e0b896319b9fc69651e3453b14d1cc94462341548fc81c643ab0b10303c7be2c3458b226d0e45f58b21658a853b84615ac5234da9bc5699646fc53c0df907f9a3c598dab6c409b7884a7a84e1a51c2caa80779cd86b77314dabae85caa28972bb6503273a82eb96ab24af6bb0ad0c9ba07ce24216696e265206abc9e147ceef1ad2e343d17aabbc4803496366c7afb53f604c92323c70be5c1e3f601c215c888f3ab239c72aba0634a2901a71891f5d072d41b524e410c7f276ff1109878d7196b5615f1f50c13976257600a821909cb36cf8d2857a2f801cf258913f56a5153234640b9e61bbd4e7b69f83a1eb8ac6b92cb841e808b700aa784a733451048211797563cccc748c9fbb54b3b65445cba676149c7ae9a9a2ecab6599b8505bc561ad85d8c052ed9669e50e684270c86f1cb4fd6708a6746c643392c8a088477dc8bf58ba4a54a8b79e523fc19a28bd47d7e5a334d8296ec86c7f52b7e73b475a0716422953cdd8a8aed0b3a84dc8425145c240c55870240a23977215b364c5596286bc496728717c329dd4b5d0b310980bc0b3f591a2d5cb2c9eaccfa1c7d3a096fc11091a4007a0f23a59782699c3171da53bc7b914f26c95391d6445073a1bd7a44691bafab9c9aceccc7ec389255c3a0ff24c71b30b6bf80c010803383485a7b295991d759cefbae257bbdee1806818565ebd09bf814c98686bbf44a0b14d28735f79ca2261bf9a31b2ca090c7667168b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb33357e74a31d3cd71513fb880e1e177438f29009bab0a131fec4cc24752015efe71c7300e2d894b0eaa40a6ab254506d8c1176a33c4a1b2879604b1b80df48d31dd");
1✔
2766
         const std::vector<uint8_t> b_pub_bits = Botan::hex_decode(
1✔
2767
            "93bc837be699d5279dac78c9d49818ebc91d4ff6cb54b294e4c378d8eb584f561d21ca87b7c77b1c551f52112ba72c4b6514bb286856e89b48b0a62bad093894474b246a515f849056c9aeb572060ee11b93e236204a8ff1784ede0c3f4c0cd0cdb2a50bd4bcb1225c0dd2caa8a129fdcb01f0e1a75da465b6b8742e4946dbe3ab21ca6c4e592d398a5700375b7b770c17e117663b51313824854585ece4a1520c59a513555f548afdc6748eb9b770b7af8adc9b3c1861efc00ebde93f91fa1539c3411ec9b154c93d1f508b9714cdd0aaabcb436951aa54978b84faa8bd838c29a12657a6389fa658237326bf80e44cff183d9c9107d30c7b4df83908a8badbc1bb5328ae61e6451291710408196261bb913b285c168fe5c7456cd74970377330139beb4c8b4c00c5954254a748308bc30f3a1ccabed83628422e5515b294e80bfed024902a81518c0e6827caa13230016b72aec10d31aab9f9b072652486c3b779c845804d001b223b4065667a5e318b4e0b27c41a099168ce763ac245c1c26354cfc8214cacf01dc8c99c5fc855639cb7486a7a6b71766d62369ee6349f521591344fb8026099fc055c3a0ca2002271c9361f52acaf4675a97b3593f3447b284ad69a9711501411e14e48f080e2a0bceba07a02f79dc15c3c971c1e671a66bf7144395a94d08c46603c4369b25b38d924693492f6500745cb1d2a037e2cd814fac70d8254aea4526e061648d227350dec1ba6305299a45bde4029a9f63e4f62acb5246ba8b54208a37b98fa77e4b720f02b18969635cc16cb6e236ff78111b621cab0339c60b529a9207c94a74925aa3d177636a6d205d72b429f614d452358b98582e42c2f3a32c94202a8625a760483807bbac74c083a7a786ae7b46aff3810a5318e3f479e67e83b4c94587d345ffda17273a55e0432281c670e36b76539263636445e4d110fa1c92b2e65b1ee993b26c41c8c66b181206ff559728993503e7611fa0ca24156642d29a52cbb6aeff88fa1bb790b4344472503ae77967d11a904550a95cc47c3435f1e44b4b890cf5dc4cbd7486a6cab60d1a6622e40c67e854ed0e6a0c4c2aa92a29731e69bc6b3ca9e83893df4bdb37338055bc16bc7cd875b0691d0212109b77236b10b0c72b7a76e9ce346061028be3271c480435e2482d3d2733ed619c2d3698d29a3225a7731a62db4f214a0b11569e91895fbccb8a92d18f94d307a542ff96d2ce8c2bbaa8fab948c83365162268124cabe934b3a5e6530b7f74397a980c243bdce2989747565dd9ba65a7498aa7c3ee711c3be239e93cb45134c082a900cc951756afa42e74a65001822cab95f41798fdef89ea8a751740842e2f7a271e98dbd5158d7455fe9b9cdcd0a0f68c91e59b00979673a1deabf8d213971f994c0bb08899a91e0b896319b9fc69651e3453b14d1cc94462341548fc81c643ab0b10303c7be2c3458b226d0e45f58b21658a853b84615ac5234da9bc5699646fc53c0df907f9a3c598dab6c409b7884a7a84e1a51c2caa80779cd86b77314dabae85caa28972bb6503273a82eb96ab24af6bb0ad0c9ba07ce24216696e265206abc9e147ceef1ad2e343d17aabbc4803496366c7afb53f604c92323c70be5c1e3f601c215c888f3ab239c72aba0634a2901a71891f5d072d41b524e410c7f276ff1109878d7196b5615f1f50c13976257600a821909cb36cf8d2857a2f801cf258913f56a5153234640b9e61bbd4e7b69f83a1eb8ac6b92cb841e808b700aa784a733451048211797563cccc748c9fbb54b3b65445cba676149c7ae9a9a2ecab6599b8505bc561ad85d8c052ed9669e50e684270c86f1cb4fd6708a6746c643392c8a088477dc8bf58ba4a54a8b79e523fc19a28bd47d7e5a334d8296ec86c7f52b7e73b475a0716422953cdd8a8aed0b3a84dc8425145c240c55870240a23977215b364c5596286bc496728717c329dd4b5d0b310980bc0b3f591a2d5cb2c9eaccfa1c7d3a096fc11091a4007a0f23a59782699c3171da53bc7b914f26c95391d6445073a1bd7a44691bafab9c9aceccc7ec389255c3a0ff24c71b30b6bf80c010803383485a7b295991d759cefbae257bbdee1806818565ebd09bf814c98686bbf44a0b14d28735f79ca2261bf9a31b2ca090c7667168b26c99bf8461495793bbb1b12ca369c825cb31d68731326bf4764b416bb333");
1✔
2768

2769
         botan_privkey_t b_priv;
1✔
2770
         if(!TEST_FFI_INIT(botan_privkey_load_kyber, (&b_priv, b_priv_bits.data(), 3168))) {
2✔
2771
            return;
×
2772
         }
2773

2774
         std::vector<uint8_t> privkey_read(3168);
1✔
2775
         TEST_FFI_OK(botan_privkey_view_kyber_raw_key, (b_priv, privkey_read.data(), botan_ffi_view_u8_fn));
1✔
2776
         result.test_eq("kyber1024 private key", privkey_read, b_priv_bits);
1✔
2777

2778
         std::vector<uint8_t> pubkey_read(1568);
1✔
2779

2780
         botan_pubkey_t b_pub;
1✔
2781
         TEST_FFI_OK(botan_privkey_export_pubkey, (&b_pub, b_priv));
1✔
2782
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (b_pub, pubkey_read.data(), botan_ffi_view_u8_fn));
1✔
2783
         result.test_eq("kyber1024 public key b", pubkey_read, b_pub_bits);
1✔
2784

2785
         botan_pubkey_t a_pub;
1✔
2786
         TEST_FFI_OK(botan_pubkey_load_kyber, (&a_pub, a_pub_bits.data(), 1568));
1✔
2787
         TEST_FFI_OK(botan_pubkey_view_kyber_raw_key, (a_pub, pubkey_read.data(), botan_ffi_view_u8_fn));
1✔
2788
         result.test_eq("kyber1024 public key a", pubkey_read, a_pub_bits);
1✔
2789

2790
         TEST_FFI_OK(botan_pubkey_destroy, (a_pub));
1✔
2791
         TEST_FFI_OK(botan_pubkey_destroy, (b_pub));
1✔
2792
         TEST_FFI_OK(botan_privkey_destroy, (b_priv));
2✔
2793
      }
5✔
2794
};
2795

2796
class FFI_ElGamal_Test final : public FFI_Test {
×
2797
   public:
2798
      std::string name() const override { return "FFI ElGamal"; }
1✔
2799

2800
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
2801
         botan_privkey_t priv;
1✔
2802

2803
         if(TEST_FFI_INIT(botan_privkey_create, (&priv, "ElGamal", "modp/ietf/1024", rng))) {
2✔
2804
            do_elgamal_test(priv, rng, result);
1✔
2805
         }
2806

2807
         if(TEST_FFI_INIT(botan_privkey_create_elgamal, (&priv, rng, 1024, 160))) {
2✔
2808
            do_elgamal_test(priv, rng, result);
1✔
2809
         }
2810
      }
1✔
2811

2812
   private:
2813
      static void do_elgamal_test(botan_privkey_t priv, botan_rng_t rng, Test::Result& result) {
2✔
2814
         TEST_FFI_OK(botan_privkey_check_key, (priv, rng, 0));
2✔
2815

2816
         botan_pubkey_t pub;
2✔
2817
         TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
2✔
2818
         TEST_FFI_OK(botan_pubkey_check_key, (pub, rng, 0));
2✔
2819

2820
         ffi_test_pubkey_export(result, pub, priv, rng);
2✔
2821
         botan_mp_t p, g, x, y;
2✔
2822
         botan_mp_init(&p);
2✔
2823
         botan_mp_init(&g);
2✔
2824
         botan_mp_init(&x);
2✔
2825
         botan_mp_init(&y);
2✔
2826

2827
         TEST_FFI_OK(botan_pubkey_get_field, (p, pub, "p"));
2✔
2828
         TEST_FFI_OK(botan_pubkey_get_field, (g, pub, "g"));
2✔
2829
         TEST_FFI_OK(botan_pubkey_get_field, (y, pub, "y"));
2✔
2830
         TEST_FFI_OK(botan_privkey_get_field, (x, priv, "x"));
2✔
2831

2832
         size_t p_len = 0;
2✔
2833
         TEST_FFI_OK(botan_mp_num_bytes, (p, &p_len));
2✔
2834

2835
         botan_privkey_t loaded_privkey;
2✔
2836
         TEST_FFI_OK(botan_privkey_load_elgamal, (&loaded_privkey, p, g, x));
2✔
2837

2838
         botan_pubkey_t loaded_pubkey;
2✔
2839
         TEST_FFI_OK(botan_pubkey_load_elgamal, (&loaded_pubkey, p, g, y));
2✔
2840

2841
         botan_mp_destroy(p);
2✔
2842
         botan_mp_destroy(g);
2✔
2843
         botan_mp_destroy(y);
2✔
2844
         botan_mp_destroy(x);
2✔
2845

2846
         std::vector<uint8_t> plaintext(16, 0xFF);
2✔
2847
         std::vector<uint8_t> ciphertext;
2✔
2848
         std::vector<uint8_t> decryption;
2✔
2849

2850
         // Test encryption
2851
         botan_pk_op_encrypt_t op_enc;
2✔
2852
         if(TEST_FFI_OK(botan_pk_op_encrypt_create, (&op_enc, loaded_pubkey, "Raw", 0))) {
4✔
2853
            size_t ctext_len;
2✔
2854
            TEST_FFI_OK(botan_pk_op_encrypt_output_length, (op_enc, plaintext.size(), &ctext_len));
2✔
2855
            ciphertext.resize(ctext_len);
2✔
2856
            TEST_FFI_OK(botan_pk_op_encrypt,
2✔
2857
                        (op_enc, rng, ciphertext.data(), &ctext_len, plaintext.data(), plaintext.size()));
2858
            ciphertext.resize(ctext_len);
2✔
2859
            TEST_FFI_OK(botan_pk_op_encrypt_destroy, (op_enc));
4✔
2860
         }
2861

2862
         // Test decryption
2863
         botan_pk_op_decrypt_t op_dec;
2✔
2864
         if(TEST_FFI_OK(botan_pk_op_decrypt_create, (&op_dec, loaded_privkey, "Raw", 0))) {
4✔
2865
            size_t ptext_len;
2✔
2866
            TEST_FFI_OK(botan_pk_op_decrypt_output_length, (op_dec, ciphertext.size(), &ptext_len));
2✔
2867
            decryption.resize(ptext_len);
2✔
2868
            TEST_FFI_OK(botan_pk_op_decrypt,
2✔
2869
                        (op_dec, decryption.data(), &ptext_len, ciphertext.data(), ciphertext.size()));
2870
            decryption.resize(ptext_len);
2✔
2871
            TEST_FFI_OK(botan_pk_op_decrypt_destroy, (op_dec));
4✔
2872
         }
2873

2874
         result.test_eq("decryption worked", decryption, plaintext);
2✔
2875

2876
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey));
2✔
2877
         TEST_FFI_OK(botan_pubkey_destroy, (pub));
2✔
2878
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey));
2✔
2879
         TEST_FFI_OK(botan_privkey_destroy, (priv));
4✔
2880
      }
6✔
2881
};
2882

2883
class FFI_DH_Test final : public FFI_Test {
×
2884
   public:
2885
      std::string name() const override { return "FFI DH"; }
1✔
2886

2887
      void ffi_test(Test::Result& result, botan_rng_t rng) override {
1✔
2888
         botan_privkey_t priv1;
1✔
2889
         if(!TEST_FFI_INIT(botan_privkey_create_dh, (&priv1, rng, "modp/ietf/2048"))) {
2✔
2890
            return;
×
2891
         }
2892

2893
         botan_privkey_t priv2;
1✔
2894
         REQUIRE_FFI_OK(botan_privkey_create_dh, (&priv2, rng, "modp/ietf/2048"));
2✔
2895

2896
         botan_pubkey_t pub1;
1✔
2897
         REQUIRE_FFI_OK(botan_privkey_export_pubkey, (&pub1, priv1));
2✔
2898

2899
         botan_pubkey_t pub2;
1✔
2900
         REQUIRE_FFI_OK(botan_privkey_export_pubkey, (&pub2, priv2));
2✔
2901

2902
         // Reload key-pair1 in order to test functions for key loading
2903
         botan_mp_t private_x, public_g, public_p, public_y;
1✔
2904

2905
         botan_mp_init(&private_x);
1✔
2906
         botan_mp_init(&public_g);
1✔
2907
         botan_mp_init(&public_p);
1✔
2908
         botan_mp_init(&public_y);
1✔
2909

2910
         TEST_FFI_OK(botan_privkey_get_field, (private_x, priv1, "x"));
1✔
2911
         TEST_FFI_OK(botan_pubkey_get_field, (public_g, pub1, "g"));
1✔
2912
         TEST_FFI_OK(botan_pubkey_get_field, (public_p, pub1, "p"));
1✔
2913
         TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub1, "y"));
1✔
2914

2915
         botan_privkey_t loaded_privkey1;
1✔
2916
         botan_pubkey_t loaded_pubkey1;
1✔
2917
         TEST_FFI_OK(botan_privkey_load_dh, (&loaded_privkey1, public_p, public_g, private_x));
1✔
2918
         TEST_FFI_OK(botan_pubkey_load_dh, (&loaded_pubkey1, public_p, public_g, public_y));
1✔
2919

2920
         TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey1, rng, 0));
1✔
2921
         TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey1, rng, 0));
1✔
2922

2923
         botan_mp_t loaded_public_g, loaded_public_p, loaded_public_y;
1✔
2924
         botan_mp_init(&loaded_public_g);
1✔
2925
         botan_mp_init(&loaded_public_p);
1✔
2926
         botan_mp_init(&loaded_public_y);
1✔
2927

2928
         TEST_FFI_OK(botan_pubkey_get_field, (loaded_public_g, loaded_pubkey1, "g"));
1✔
2929
         TEST_FFI_OK(botan_pubkey_get_field, (loaded_public_p, loaded_pubkey1, "p"));
1✔
2930
         TEST_FFI_OK(botan_pubkey_get_field, (loaded_public_y, loaded_pubkey1, "y"));
1✔
2931

2932
         int cmp;
1✔
2933

2934
         TEST_FFI_OK(botan_mp_cmp, (&cmp, loaded_public_g, public_g));
1✔
2935
         result.confirm("bigint_mp_cmp(g, g)", cmp == 0);
2✔
2936

2937
         TEST_FFI_OK(botan_mp_cmp, (&cmp, loaded_public_p, public_p));
1✔
2938
         result.confirm("bigint_mp_cmp(p, p)", cmp == 0);
2✔
2939

2940
         TEST_FFI_OK(botan_mp_cmp, (&cmp, loaded_public_y, public_y));
1✔
2941
         result.confirm("bigint_mp_cmp(y, y)", cmp == 0);
2✔
2942

2943
         botan_pk_op_ka_t ka1;
1✔
2944
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka1, loaded_privkey1, "Raw", 0));
2✔
2945
         botan_pk_op_ka_t ka2;
1✔
2946
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_create, (&ka2, priv2, "Raw", 0));
2✔
2947

2948
         size_t pubkey1_len = 0;
1✔
2949
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
2950
                     botan_pk_op_key_agreement_export_public,
2951
                     (priv1, nullptr, &pubkey1_len));
2952
         std::vector<uint8_t> pubkey1(pubkey1_len);
1✔
2953
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_export_public, (priv1, pubkey1.data(), &pubkey1_len));
2✔
2954
         size_t pubkey2_len = 0;
1✔
2955
         TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
1✔
2956
                     botan_pk_op_key_agreement_export_public,
2957
                     (priv2, nullptr, &pubkey2_len));
2958
         std::vector<uint8_t> pubkey2(pubkey2_len);
1✔
2959
         REQUIRE_FFI_OK(botan_pk_op_key_agreement_export_public, (priv2, pubkey2.data(), &pubkey2_len));
2✔
2960

2961
         const size_t shared_key_len = 256;
1✔
2962

2963
         std::vector<uint8_t> key1(shared_key_len);
1✔
2964
         size_t key1_len = key1.size();
1✔
2965

2966
         TEST_FFI_OK(botan_pk_op_key_agreement,
1✔
2967
                     (ka1, key1.data(), &key1_len, pubkey2.data(), pubkey2.size(), nullptr, 0));
2968

2969
         std::vector<uint8_t> key2(shared_key_len);
1✔
2970
         size_t key2_len = key2.size();
1✔
2971

2972
         TEST_FFI_OK(botan_pk_op_key_agreement,
1✔
2973
                     (ka2, key2.data(), &key2_len, pubkey1.data(), pubkey1.size(), nullptr, 0));
2974

2975
         result.test_eq("shared DH key", key1, key2);
1✔
2976

2977
         TEST_FFI_OK(botan_mp_destroy, (private_x));
1✔
2978
         TEST_FFI_OK(botan_mp_destroy, (public_p));
1✔
2979
         TEST_FFI_OK(botan_mp_destroy, (public_g));
1✔
2980
         TEST_FFI_OK(botan_mp_destroy, (public_y));
1✔
2981

2982
         TEST_FFI_OK(botan_mp_destroy, (loaded_public_p));
1✔
2983
         TEST_FFI_OK(botan_mp_destroy, (loaded_public_g));
1✔
2984
         TEST_FFI_OK(botan_mp_destroy, (loaded_public_y));
1✔
2985

2986
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka1));
1✔
2987
         TEST_FFI_OK(botan_pk_op_key_agreement_destroy, (ka2));
1✔
2988
         TEST_FFI_OK(botan_privkey_destroy, (priv1));
1✔
2989
         TEST_FFI_OK(botan_privkey_destroy, (priv2));
1✔
2990
         TEST_FFI_OK(botan_pubkey_destroy, (pub1));
1✔
2991
         TEST_FFI_OK(botan_pubkey_destroy, (pub2));
1✔
2992
         TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey1));
1✔
2993
         TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey1));
2✔
2994
      }
4✔
2995
};
2996

2997
BOTAN_REGISTER_TEST("ffi", "ffi_utils", FFI_Utils_Test);
2998
BOTAN_REGISTER_TEST("ffi", "ffi_rng", FFI_RNG_Test);
2999
BOTAN_REGISTER_TEST("ffi", "ffi_rsa_cert", FFI_RSA_Cert_Test);
3000
BOTAN_REGISTER_TEST("ffi", "ffi_zfec", FFI_ZFEC_Test);
3001
BOTAN_REGISTER_TEST("ffi", "ffi_crl", FFI_CRL_Test);
3002
BOTAN_REGISTER_TEST("ffi", "ffi_cert_validation", FFI_Cert_Validation_Test);
3003
BOTAN_REGISTER_TEST("ffi", "ffi_ecdsa_certificate", FFI_ECDSA_Certificate_Test);
3004
BOTAN_REGISTER_TEST("ffi", "ffi_pkcs_hashid", FFI_PKCS_Hashid_Test);
3005
BOTAN_REGISTER_TEST("ffi", "ffi_cbc_cipher", FFI_CBC_Cipher_Test);
3006
BOTAN_REGISTER_TEST("ffi", "ffi_gcm", FFI_GCM_Test);
3007
BOTAN_REGISTER_TEST("ffi", "ffi_eax", FFI_EAX_Test);
3008
BOTAN_REGISTER_TEST("ffi", "ffi_streamcipher", FFI_StreamCipher_Test);
3009
BOTAN_REGISTER_TEST("ffi", "ffi_hashfunction", FFI_HashFunction_Test);
3010
BOTAN_REGISTER_TEST("ffi", "ffi_mac", FFI_MAC_Test);
3011
BOTAN_REGISTER_TEST("ffi", "ffi_scrypt", FFI_Scrypt_Test);
3012
BOTAN_REGISTER_TEST("ffi", "ffi_kdf", FFI_KDF_Test);
3013
BOTAN_REGISTER_TEST("ffi", "ffi_blockcipher", FFI_Blockcipher_Test);
3014
BOTAN_REGISTER_TEST("ffi", "ffi_errorhandling", FFI_ErrorHandling_Test);
3015
BOTAN_REGISTER_TEST("ffi", "ffi_base64", FFI_Base64_Test);
3016
BOTAN_REGISTER_TEST("ffi", "ffi_hex", FFI_Hex_Test);
3017
BOTAN_REGISTER_TEST("ffi", "ffi_mp", FFI_MP_Test);
3018
BOTAN_REGISTER_TEST("ffi", "ffi_fpe", FFI_FPE_Test);
3019
BOTAN_REGISTER_TEST("ffi", "ffi_totp", FFI_TOTP_Test);
3020
BOTAN_REGISTER_TEST("ffi", "ffi_hotp", FFI_HOTP_Test);
3021
BOTAN_REGISTER_TEST("ffi", "ffi_keywrap", FFI_Keywrap_Test);
3022
BOTAN_REGISTER_TEST("ffi", "ffi_rsa", FFI_RSA_Test);
3023
BOTAN_REGISTER_TEST("ffi", "ffi_dsa", FFI_DSA_Test);
3024
BOTAN_REGISTER_TEST("ffi", "ffi_ecdsa", FFI_ECDSA_Test);
3025
BOTAN_REGISTER_TEST("ffi", "ffi_sm2_sig", FFI_SM2_Sig_Test);
3026
BOTAN_REGISTER_TEST("ffi", "ffi_sm2_enc", FFI_SM2_Enc_Test);
3027
BOTAN_REGISTER_TEST("ffi", "ffi_ecdh", FFI_ECDH_Test);
3028
BOTAN_REGISTER_TEST("ffi", "ffi_mceliece", FFI_McEliece_Test);
3029
BOTAN_REGISTER_TEST("ffi", "ffi_ed25519", FFI_Ed25519_Test);
3030
BOTAN_REGISTER_TEST("ffi", "ffi_x25519", FFI_X25519_Test);
3031
BOTAN_REGISTER_TEST("ffi", "ffi_kyber512", FFI_Kyber512_Test);
3032
BOTAN_REGISTER_TEST("ffi", "ffi_kyber768", FFI_Kyber768_Test);
3033
BOTAN_REGISTER_TEST("ffi", "ffi_kyber1024", FFI_Kyber1024_Test);
3034
BOTAN_REGISTER_TEST("ffi", "ffi_elgamal", FFI_ElGamal_Test);
3035
BOTAN_REGISTER_TEST("ffi", "ffi_dh", FFI_DH_Test);
3036

3037
#endif
3038

3039
}  // namespace
3040

3041
}  // 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

© 2025 Coveralls, Inc