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

randombit / botan / 20579846577

29 Dec 2025 06:24PM UTC coverage: 90.415% (+0.2%) from 90.243%
20579846577

push

github

web-flow
Merge pull request #5167 from randombit/jack/src-size-reductions

Changes to reduce unnecessary inclusions

101523 of 112285 relevant lines covered (90.42%)

12817276.56 hits per line

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

99.53
/src/tests/test_sodium.cpp
1
/*
2
* (C) 2019 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "tests.h"
8

9
#if defined(BOTAN_HAS_SODIUM_API)
10
   #include <botan/sodium.h>
11
   #include <cstring>
12
#endif
13

14
namespace Botan_Tests {
15

16
#if defined(BOTAN_HAS_SODIUM_API)
17

18
class Sodium_API_Tests : public Test {
1✔
19
   public:
20
      std::vector<Test::Result> run() override {
1✔
21
         std::vector<Test::Result> results;
1✔
22

23
         results.push_back(aead_chacha20poly1305());
2✔
24
         results.push_back(aead_chacha20poly1305_ietf());
2✔
25
         results.push_back(aead_xchacha20poly1305());
2✔
26
         results.push_back(auth_hmacsha256());
2✔
27
         results.push_back(auth_hmacsha512());
2✔
28
         results.push_back(auth_hmacsha512256());
2✔
29
         results.push_back(auth_poly1305());
2✔
30
         results.push_back(box_curve25519xsalsa20poly1305());
2✔
31
         results.push_back(hash_sha256());
2✔
32
         results.push_back(hash_sha512());
2✔
33
         results.push_back(randombytes_buf_deterministic());
2✔
34
         results.push_back(secretbox_xsalsa20poly1305());
2✔
35
         results.push_back(secretbox_xsalsa20poly1305_detached());
2✔
36
         results.push_back(shorthash_siphash24());
2✔
37
         results.push_back(stream_chacha20());
2✔
38
         results.push_back(stream_chacha20_ietf());
2✔
39
         results.push_back(stream_salsa20());
2✔
40
         results.push_back(stream_xchacha20());
2✔
41
         results.push_back(stream_xsalsa20());
2✔
42
         results.push_back(sign_ed25519());
2✔
43
         results.push_back(sodium_malloc());
2✔
44
         results.push_back(sodium_utils());
2✔
45

46
         return results;
1✔
47
      }
×
48

49
   private:
50
      static Test::Result sodium_malloc() {
1✔
51
         Test::Result result("sodium_malloc");
1✔
52

53
         void* p = Botan::Sodium::sodium_malloc(50);
1✔
54
         std::memset(p, 0xFF, 50);
1✔
55

56
         Botan::Sodium::sodium_free(p);
1✔
57
         Botan::Sodium::sodium_free(nullptr);
1✔
58

59
         result.test_success("Didn't crash");
1✔
60

61
         return result;
1✔
62
      }
×
63

64
      static Test::Result sodium_utils() {
1✔
65
         Test::Result result("sodium math utils");
1✔
66

67
         result.confirm("sodium_is_zero", Botan::Sodium::sodium_is_zero(nullptr, 0) == 1);
2✔
68

69
         std::vector<uint8_t> a(5);
1✔
70
         result.confirm("sodium_is_zero", Botan::Sodium::sodium_is_zero(a.data(), a.size()) == 1);
2✔
71
         Botan::Sodium::sodium_increment(a.data(), a.size());
1✔
72
         result.test_eq("sodium_increment", a, "0100000000");
1✔
73
         result.confirm("sodium_is_zero", Botan::Sodium::sodium_is_zero(a.data(), a.size()) == 0);
2✔
74

75
         std::memset(a.data(), 0xFF, a.size());
1✔
76
         Botan::Sodium::sodium_increment(a.data(), a.size());
1✔
77
         result.test_eq("sodium_increment", a, "0000000000");
1✔
78
         Botan::Sodium::sodium_increment(a.data(), a.size());
1✔
79
         result.test_eq("sodium_increment", a, "0100000000");
1✔
80

81
         result.confirm("sodium_compare", Botan::Sodium::sodium_compare(a.data(), a.data(), a.size()) == 0);
2✔
82
         result.confirm("sodium_memcmp", Botan::Sodium::sodium_memcmp(a.data(), a.data(), a.size()) == 0);
2✔
83

84
         std::vector<uint8_t> b(5, 0x10);
1✔
85
         result.confirm("sodium_compare a<b", Botan::Sodium::sodium_compare(a.data(), b.data(), a.size()) == -1);
2✔
86
         result.confirm("sodium_compare b<a", Botan::Sodium::sodium_compare(b.data(), a.data(), a.size()) == 1);
2✔
87
         result.confirm("sodium_memcmp a<b", Botan::Sodium::sodium_memcmp(a.data(), b.data(), a.size()) == -1);
2✔
88
         result.confirm("sodium_memcmp b<a", Botan::Sodium::sodium_memcmp(b.data(), a.data(), a.size()) == -1);
2✔
89

90
         Botan::Sodium::sodium_add(a.data(), b.data(), a.size());
1✔
91
         result.test_eq("sodium_add", a, "1110101010");
1✔
92
         Botan::Sodium::sodium_add(b.data(), a.data(), a.size());
1✔
93
         result.test_eq("sodium_add", b, "2120202020");
1✔
94
         Botan::Sodium::sodium_add(a.data(), b.data(), a.size());
1✔
95
         result.test_eq("sodium_add", a, "3230303030");
1✔
96
         Botan::Sodium::sodium_add(b.data(), a.data(), a.size());
1✔
97
         result.test_eq("sodium_add", b, "5350505050");
1✔
98

99
         return result;
1✔
100
      }
2✔
101

102
      static Test::Result randombytes_buf_deterministic() {
1✔
103
         Test::Result result("randombytes_buf_deterministic");
1✔
104

105
         const uint8_t seed[32] = {1, 0};
1✔
106
         std::vector<uint8_t> output(18);
1✔
107

108
         Botan::Sodium::randombytes_buf_deterministic(output.data(), output.size(), seed);
1✔
109

110
         result.test_eq("output", output, "04069B5F37E82F91DC37FD5EB99F1A4124B1");
1✔
111

112
         return result;
1✔
113
      }
1✔
114

115
      static Test::Result hash_sha512() {
1✔
116
         Test::Result result("crypto_hash_sha512");
1✔
117

118
         std::vector<uint8_t> output(64);
1✔
119
         Botan::Sodium::crypto_hash_sha512(output.data(), nullptr, 0);
1✔
120

121
         result.test_eq(
1✔
122
            "expected output",
123
            output,
124
            "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
125

126
         return result;
1✔
127
      }
1✔
128

129
      static Test::Result hash_sha256() {
1✔
130
         Test::Result result("crypto_hash_sha256");
1✔
131

132
         std::vector<uint8_t> output(32);
1✔
133
         Botan::Sodium::crypto_hash_sha256(output.data(), nullptr, 0);
1✔
134

135
         result.test_eq("expected output", output, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
1✔
136

137
         return result;
1✔
138
      }
1✔
139

140
      static Test::Result box_curve25519xsalsa20poly1305() {
1✔
141
         Test::Result result("crypto_box_curve25519xsalsa20poly1305");
1✔
142

143
         const std::vector<uint8_t> seed(32);
1✔
144

145
         std::vector<uint8_t> pk1(32);
1✔
146
         std::vector<uint8_t> sk1(32);
1✔
147
         result.test_rc_ok("seed_keypair", Botan::Sodium::crypto_box_seed_keypair(pk1.data(), sk1.data(), seed.data()));
1✔
148
         result.test_eq("pk1", pk1, "5BF55C73B82EBE22BE80F3430667AF570FAE2556A6415E6B30D4065300AA947D");
1✔
149
         result.test_eq("sk1", sk1, "5046ADC1DBA838867B2BBBFDD0C3423E58B57970B5267A90F57960924A87F196");
1✔
150

151
         std::vector<uint8_t> pk2(32);
1✔
152
         std::vector<uint8_t> sk2(32);
1✔
153
         result.test_rc_ok("seed_keypair", Botan::Sodium::crypto_box_seed_keypair(pk2.data(), sk2.data(), sk1.data()));
1✔
154
         result.test_eq("pk2", pk2, "E0CFC9C6B2FE5BF85F48671691225C03D763F2305206FE3D3B0ED7B76153684A");
1✔
155
         result.test_eq("sk2", sk2, "58E2E4C71F138FBC97F9341735B4581746761F9A104540007FE12CFC4D9FDA15");
1✔
156

157
         const std::vector<uint8_t> ptext(15);
1✔
158
         std::vector<uint8_t> ctext(ptext.size() + 16);
1✔
159
         const std::vector<uint8_t> nonce(Botan::Sodium::crypto_box_noncebytes());
1✔
160

161
         result.test_rc_ok("crypto_box_easy",
1✔
162
                           Botan::Sodium::crypto_box_easy(
163
                              ctext.data(), ptext.data(), ptext.size(), nonce.data(), pk2.data(), sk1.data()));
1✔
164

165
         result.test_eq("ctext1", ctext, "11D78D4C32C5674390C0425D8BBB5928AFE7F767E2A7E4427E1A1362F1FD92");
1✔
166

167
         result.test_rc_ok("crypto_box_easy",
1✔
168
                           Botan::Sodium::crypto_box_easy(
169
                              ctext.data(), ptext.data(), ptext.size(), nonce.data(), pk1.data(), sk2.data()));
1✔
170

171
         // same shared secret, same nonce, same data -> same ciphertext
172
         result.test_eq("ctext2", ctext, "11D78D4C32C5674390C0425D8BBB5928AFE7F767E2A7E4427E1A1362F1FD92");
1✔
173

174
         std::vector<uint8_t> recovered(15);
1✔
175

176
         result.test_rc_ok("crypto_box_open_easy",
1✔
177
                           Botan::Sodium::crypto_box_open_easy(
178
                              recovered.data(), ctext.data(), ctext.size(), nonce.data(), pk1.data(), sk2.data()));
1✔
179

180
         result.test_eq("recover1", recovered, ptext);
2✔
181

182
         result.test_rc_ok("crypto_box_open_easy",
1✔
183
                           Botan::Sodium::crypto_box_open_easy(
184
                              recovered.data(), ctext.data(), ctext.size(), nonce.data(), pk2.data(), sk1.data()));
1✔
185

186
         result.test_eq("recover1", recovered, ptext);
2✔
187

188
         return result;
1✔
189
      }
9✔
190

191
      static Test::Result aead_chacha20poly1305() {
1✔
192
         Test::Result result("crypto_aead_chacha20poly1305");
1✔
193

194
         const std::vector<uint8_t> key =
1✔
195
            Botan::hex_decode("0000000000000000000000000000000000000000000000000000000000000000");
1✔
196
         const std::vector<uint8_t> ad;
1✔
197
         const std::vector<uint8_t> nonce = Botan::hex_decode("0000000000000000");
1✔
198
         const std::vector<uint8_t> in = Botan::hex_decode("000000000000000000000000000000");
1✔
199

200
         result.test_eq("key len", Botan::Sodium::crypto_aead_chacha20poly1305_keybytes(), key.size());
1✔
201
         result.test_eq("nonce len", Botan::Sodium::crypto_aead_chacha20poly1305_npubbytes(), nonce.size());
1✔
202

203
         std::vector<uint8_t> ctext(in.size());
1✔
204
         std::vector<uint8_t> mac(16);
1✔
205
         unsigned long long maclen = 0;
1✔
206
         Botan::Sodium::crypto_aead_chacha20poly1305_encrypt_detached(ctext.data(),
1✔
207
                                                                      mac.data(),
208
                                                                      &maclen,
209
                                                                      in.data(),
210
                                                                      in.size(),
211
                                                                      ad.data(),
212
                                                                      ad.size(),
213
                                                                      nullptr,
214
                                                                      nonce.data(),
215
                                                                      key.data());
216

217
         result.test_eq("maclen", size_t(maclen), 16);
1✔
218
         result.test_eq("mac", mac, "09998877ABA156DDC68F8344098F68B9");
1✔
219
         result.test_eq("ctext", ctext, "9F07E7BE5551387A98BA977C732D08");
1✔
220

221
         std::vector<uint8_t> recovered(ctext.size());
1✔
222
         result.test_rc_ok("decrypt",
1✔
223
                           Botan::Sodium::crypto_aead_chacha20poly1305_decrypt_detached(recovered.data(),
224
                                                                                        nullptr,
225
                                                                                        ctext.data(),
1✔
226
                                                                                        ctext.size(),
227
                                                                                        mac.data(),
1✔
228
                                                                                        ad.data(),
229
                                                                                        ad.size(),
230
                                                                                        nonce.data(),
231
                                                                                        key.data()));
232

233
         result.test_eq("plaintext", recovered, in);
2✔
234

235
         mac[0] ^= 1;
1✔
236
         result.test_rc_fail("decrypt",
2✔
237
                             "invalid ciphertext",
238
                             Botan::Sodium::crypto_aead_chacha20poly1305_decrypt_detached(recovered.data(),
239
                                                                                          nullptr,
240
                                                                                          ctext.data(),
1✔
241
                                                                                          ctext.size(),
242
                                                                                          mac.data(),
1✔
243
                                                                                          ad.data(),
244
                                                                                          ad.size(),
245
                                                                                          nonce.data(),
246
                                                                                          key.data()));
247

248
         ctext.resize(in.size() + mac.size());
1✔
249
         unsigned long long ctext_len = 0;
1✔
250
         result.test_rc_ok("encrypt",
1✔
251
                           Botan::Sodium::crypto_aead_chacha20poly1305_encrypt(ctext.data(),
252
                                                                               &ctext_len,
253
                                                                               in.data(),
254
                                                                               in.size(),
255
                                                                               ad.data(),
256
                                                                               ad.size(),
257
                                                                               nullptr,
258
                                                                               nonce.data(),
259
                                                                               key.data()));
260

261
         result.test_eq("ctext_len", size_t(ctext_len), ctext.size());
1✔
262
         result.test_eq("ctext", ctext, "9F07E7BE5551387A98BA977C732D0809998877ABA156DDC68F8344098F68B9");
1✔
263

264
         unsigned long long recovered_len = 0;
1✔
265
         result.test_rc_ok("decrypt",
1✔
266
                           Botan::Sodium::crypto_aead_chacha20poly1305_decrypt(recovered.data(),
267
                                                                               &recovered_len,
268
                                                                               nullptr,
269
                                                                               ctext.data(),
1✔
270
                                                                               ctext.size(),
271
                                                                               ad.data(),
272
                                                                               ad.size(),
273
                                                                               nonce.data(),
274
                                                                               key.data()));
275

276
         result.test_eq("recovered", recovered, in);
2✔
277

278
         return result;
1✔
279
      }
5✔
280

281
      static Test::Result aead_chacha20poly1305_ietf() {
1✔
282
         Test::Result result("crypto_aead_chacha20poly1305_ietf");
1✔
283

284
         const std::vector<uint8_t> key =
1✔
285
            Botan::hex_decode("0000000000000000000000000000000000000000000000000000000000000000");
1✔
286
         const std::vector<uint8_t> ad;
1✔
287
         const std::vector<uint8_t> nonce = Botan::hex_decode("000000000000000000000000");
1✔
288
         const std::vector<uint8_t> in = Botan::hex_decode("000000000000000000000000000000");
1✔
289

290
         result.test_eq("key len", Botan::Sodium::crypto_aead_chacha20poly1305_ietf_keybytes(), key.size());
1✔
291
         result.test_eq("nonce len", Botan::Sodium::crypto_aead_chacha20poly1305_ietf_npubbytes(), nonce.size());
1✔
292

293
         std::vector<uint8_t> ctext(in.size());
1✔
294
         std::vector<uint8_t> mac(16);
1✔
295
         unsigned long long maclen = 0;
1✔
296
         Botan::Sodium::crypto_aead_chacha20poly1305_ietf_encrypt_detached(ctext.data(),
1✔
297
                                                                           mac.data(),
298
                                                                           &maclen,
299
                                                                           in.data(),
300
                                                                           in.size(),
301
                                                                           ad.data(),
302
                                                                           ad.size(),
303
                                                                           nullptr,
304
                                                                           nonce.data(),
305
                                                                           key.data());
306

307
         result.test_eq("maclen", size_t(maclen), 16);
1✔
308
         result.test_eq("mac", mac, "3679F1FB9843FD81E26D962888296954");
1✔
309
         result.test_eq("ctext", ctext, "9F07E7BE5551387A98BA977C732D08");
1✔
310

311
         std::vector<uint8_t> recovered(ctext.size());
1✔
312
         result.test_rc_ok("decrypt",
1✔
313
                           Botan::Sodium::crypto_aead_chacha20poly1305_ietf_decrypt_detached(recovered.data(),
314
                                                                                             nullptr,
315
                                                                                             ctext.data(),
1✔
316
                                                                                             ctext.size(),
317
                                                                                             mac.data(),
1✔
318
                                                                                             ad.data(),
319
                                                                                             ad.size(),
320
                                                                                             nonce.data(),
321
                                                                                             key.data()));
322

323
         result.test_eq("plaintext", recovered, in);
2✔
324

325
         mac[0] ^= 1;
1✔
326
         result.test_rc_fail("decrypt",
2✔
327
                             "invalid ciphertext",
328
                             Botan::Sodium::crypto_aead_chacha20poly1305_ietf_decrypt_detached(recovered.data(),
329
                                                                                               nullptr,
330
                                                                                               ctext.data(),
1✔
331
                                                                                               ctext.size(),
332
                                                                                               mac.data(),
1✔
333
                                                                                               ad.data(),
334
                                                                                               ad.size(),
335
                                                                                               nonce.data(),
336
                                                                                               key.data()));
337

338
         ctext.resize(in.size() + mac.size());
1✔
339
         unsigned long long ctext_len = 0;
1✔
340
         result.test_rc_ok("encrypt",
1✔
341
                           Botan::Sodium::crypto_aead_chacha20poly1305_ietf_encrypt(ctext.data(),
342
                                                                                    &ctext_len,
343
                                                                                    in.data(),
344
                                                                                    in.size(),
345
                                                                                    ad.data(),
346
                                                                                    ad.size(),
347
                                                                                    nullptr,
348
                                                                                    nonce.data(),
349
                                                                                    key.data()));
350

351
         result.test_eq("ctext_len", size_t(ctext_len), ctext.size());
1✔
352
         result.test_eq("ctext", ctext, "9F07E7BE5551387A98BA977C732D083679F1FB9843FD81E26D962888296954");
1✔
353

354
         unsigned long long recovered_len = 0;
1✔
355
         result.test_rc_ok("decrypt",
1✔
356
                           Botan::Sodium::crypto_aead_chacha20poly1305_ietf_decrypt(recovered.data(),
357
                                                                                    &recovered_len,
358
                                                                                    nullptr,
359
                                                                                    ctext.data(),
1✔
360
                                                                                    ctext.size(),
361
                                                                                    ad.data(),
362
                                                                                    ad.size(),
363
                                                                                    nonce.data(),
364
                                                                                    key.data()));
365

366
         result.test_eq("recovered", recovered, in);
2✔
367

368
         return result;
1✔
369
      }
5✔
370

371
      static Test::Result aead_xchacha20poly1305() {
1✔
372
         Test::Result result("crypto_aead_xchacha20poly1305");
1✔
373

374
         const std::vector<uint8_t> key =
1✔
375
            Botan::hex_decode("0000000000000000000000000000000000000000000000000000000000000000");
1✔
376
         const std::vector<uint8_t> ad;
1✔
377
         const std::vector<uint8_t> nonce = Botan::hex_decode("000000000000000000000000000000000000000000000000");
1✔
378
         const std::vector<uint8_t> in = Botan::hex_decode("000000000000000000000000000000");
1✔
379

380
         result.test_eq("key len", Botan::Sodium::crypto_aead_xchacha20poly1305_ietf_keybytes(), key.size());
1✔
381
         result.test_eq("nonce len", Botan::Sodium::crypto_aead_xchacha20poly1305_ietf_npubbytes(), nonce.size());
1✔
382

383
         std::vector<uint8_t> ctext(in.size());
1✔
384
         std::vector<uint8_t> mac(16);
1✔
385
         unsigned long long maclen = 0;
1✔
386
         Botan::Sodium::crypto_aead_xchacha20poly1305_ietf_encrypt_detached(ctext.data(),
1✔
387
                                                                            mac.data(),
388
                                                                            &maclen,
389
                                                                            in.data(),
390
                                                                            in.size(),
391
                                                                            ad.data(),
392
                                                                            ad.size(),
393
                                                                            nullptr,
394
                                                                            nonce.data(),
395
                                                                            key.data());
396

397
         result.test_eq("maclen", size_t(maclen), 16);
1✔
398
         result.test_eq("mac", mac, "b2f7033812ac9ebd3745e2c99c7bbfeb");
1✔
399
         result.test_eq("ctext", ctext, "789e9689e5208d7fd9e1f3c5b5341f");
1✔
400

401
         std::vector<uint8_t> recovered(ctext.size());
1✔
402
         result.test_rc_ok("decrypt",
1✔
403
                           Botan::Sodium::crypto_aead_xchacha20poly1305_ietf_decrypt_detached(recovered.data(),
404
                                                                                              nullptr,
405
                                                                                              ctext.data(),
1✔
406
                                                                                              ctext.size(),
407
                                                                                              mac.data(),
1✔
408
                                                                                              ad.data(),
409
                                                                                              ad.size(),
410
                                                                                              nonce.data(),
411
                                                                                              key.data()));
412

413
         result.test_eq("plaintext", recovered, in);
2✔
414

415
         mac[0] ^= 1;
1✔
416
         result.test_rc_fail("decrypt",
2✔
417
                             "invalid ciphertext",
418
                             Botan::Sodium::crypto_aead_xchacha20poly1305_ietf_decrypt_detached(recovered.data(),
419
                                                                                                nullptr,
420
                                                                                                ctext.data(),
1✔
421
                                                                                                ctext.size(),
422
                                                                                                mac.data(),
1✔
423
                                                                                                ad.data(),
424
                                                                                                ad.size(),
425
                                                                                                nonce.data(),
426
                                                                                                key.data()));
427

428
         ctext.resize(in.size() + mac.size());
1✔
429
         unsigned long long ctext_len = 0;
1✔
430
         result.test_rc_ok("encrypt",
1✔
431
                           Botan::Sodium::crypto_aead_xchacha20poly1305_ietf_encrypt(ctext.data(),
432
                                                                                     &ctext_len,
433
                                                                                     in.data(),
434
                                                                                     in.size(),
435
                                                                                     ad.data(),
436
                                                                                     ad.size(),
437
                                                                                     nullptr,
438
                                                                                     nonce.data(),
439
                                                                                     key.data()));
440

441
         result.test_eq("ctext_len", size_t(ctext_len), ctext.size());
1✔
442
         result.test_eq("ctext", ctext, "789e9689e5208d7fd9e1f3c5b5341fb2f7033812ac9ebd3745e2c99c7bbfeb");
1✔
443

444
         unsigned long long recovered_len = 0;
1✔
445
         result.test_rc_ok("decrypt",
1✔
446
                           Botan::Sodium::crypto_aead_xchacha20poly1305_ietf_decrypt(recovered.data(),
447
                                                                                     &recovered_len,
448
                                                                                     nullptr,
449
                                                                                     ctext.data(),
1✔
450
                                                                                     ctext.size(),
451
                                                                                     ad.data(),
452
                                                                                     ad.size(),
453
                                                                                     nonce.data(),
454
                                                                                     key.data()));
455

456
         result.test_eq("recovered", recovered, in);
2✔
457

458
         return result;
1✔
459
      }
5✔
460

461
      static Test::Result auth_hmacsha512() {
1✔
462
         Test::Result result("crypto_auth_hmacsha512");
1✔
463

464
         const std::vector<uint8_t> key =
1✔
465
            Botan::hex_decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
1✔
466
         const std::vector<uint8_t> in = Botan::hex_decode("616263");
1✔
467

468
         result.test_eq("key_size", key.size(), Botan::Sodium::crypto_auth_hmacsha512_keybytes());
1✔
469

470
         std::vector<uint8_t> mac(64);
1✔
471
         Botan::Sodium::crypto_auth_hmacsha512(mac.data(), in.data(), in.size(), key.data());
1✔
472

473
         result.test_eq(
1✔
474
            "expected mac",
475
            mac,
476
            "69D4A21E226BF0D348CB9A847C01CF24E93E8AC30D7C951704B936F82F795A624B470E23ABD33AC8700E797F0F2A499B932BAC7D283BBBB37D8FECF70D5E08A7");
477

478
         result.test_rc_ok("verify",
1✔
479
                           Botan::Sodium::crypto_auth_hmacsha512_verify(mac.data(), in.data(), in.size(), key.data()));
1✔
480

481
         mac[0] ^= 1;
1✔
482
         result.test_rc_fail(
2✔
483
            "verify",
484
            "invalid mac",
485
            Botan::Sodium::crypto_auth_hmacsha512_verify(mac.data(), in.data(), in.size(), key.data()));
1✔
486

487
         return result;
1✔
488
      }
3✔
489

490
      static Test::Result auth_hmacsha512256() {
1✔
491
         Test::Result result("crypto_auth_hmacsha512256");
1✔
492

493
         const std::vector<uint8_t> key =
1✔
494
            Botan::hex_decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
1✔
495
         const std::vector<uint8_t> in = Botan::hex_decode("616263");
1✔
496

497
         std::vector<uint8_t> mac(32);
1✔
498
         Botan::Sodium::crypto_auth_hmacsha512256(mac.data(), in.data(), in.size(), key.data());
1✔
499

500
         result.test_eq("expected mac", mac, "69D4A21E226BF0D348CB9A847C01CF24E93E8AC30D7C951704B936F82F795A62");
1✔
501

502
         result.test_rc_ok(
1✔
503
            "verify", Botan::Sodium::crypto_auth_hmacsha512256_verify(mac.data(), in.data(), in.size(), key.data()));
1✔
504

505
         mac[0] ^= 1;
1✔
506
         result.test_rc_fail(
2✔
507
            "verify",
508
            "invalid mac",
509
            Botan::Sodium::crypto_auth_hmacsha512256_verify(mac.data(), in.data(), in.size(), key.data()));
1✔
510

511
         return result;
1✔
512
      }
3✔
513

514
      static Test::Result auth_hmacsha256() {
1✔
515
         Test::Result result("crypto_auth_hmacsha256");
1✔
516

517
         const std::vector<uint8_t> key =
1✔
518
            Botan::hex_decode("0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20");
1✔
519
         const std::vector<uint8_t> in = Botan::hex_decode("616263");
1✔
520

521
         std::vector<uint8_t> mac(32);
1✔
522
         Botan::Sodium::crypto_auth_hmacsha256(mac.data(), in.data(), in.size(), key.data());
1✔
523

524
         result.test_eq("expected mac", mac, "A21B1F5D4CF4F73A4DD939750F7A066A7F98CC131CB16A6692759021CFAB8181");
1✔
525

526
         result.test_rc_ok("verify",
1✔
527
                           Botan::Sodium::crypto_auth_hmacsha256_verify(mac.data(), in.data(), in.size(), key.data()));
1✔
528

529
         mac[0] ^= 1;
1✔
530
         result.test_rc_fail(
2✔
531
            "verify",
532
            "invalid mac",
533
            Botan::Sodium::crypto_auth_hmacsha256_verify(mac.data(), in.data(), in.size(), key.data()));
1✔
534

535
         return result;
1✔
536
      }
3✔
537

538
      static Test::Result auth_poly1305() {
1✔
539
         Test::Result result("crypto_onetimeauth_poly1305");
1✔
540

541
         const std::vector<uint8_t> key(Botan::Sodium::crypto_onetimeauth_keybytes(), 0x42);
1✔
542
         const std::vector<uint8_t> in(15);
1✔
543

544
         std::vector<uint8_t> mac(16);
1✔
545

546
         result.test_rc_ok("poly1305",
1✔
547
                           Botan::Sodium::crypto_onetimeauth_poly1305(mac.data(), in.data(), in.size(), key.data()));
548

549
         result.test_eq("expected mac", mac, "12154512151545121515451215154584");
1✔
550

551
         result.test_rc_ok(
1✔
552
            "poly1305 verify",
553
            Botan::Sodium::crypto_onetimeauth_poly1305_verify(mac.data(), in.data(), in.size(), key.data()));
1✔
554

555
         mac[0] ^= 1;
1✔
556
         result.test_rc_fail(
2✔
557
            "poly1305 verify",
558
            "invalid mac",
559
            Botan::Sodium::crypto_onetimeauth_poly1305_verify(mac.data(), in.data(), in.size(), key.data()));
1✔
560

561
         return result;
1✔
562
      }
3✔
563

564
      static Test::Result shorthash_siphash24() {
1✔
565
         Test::Result result("crypto_shorthash_siphash24");
1✔
566

567
         const std::vector<uint8_t> key = Botan::hex_decode("000102030405060708090A0B0C0D0E0F");
1✔
568
         const std::vector<uint8_t> in = Botan::hex_decode("000102030405060708090A0B0C0D0E");
1✔
569

570
         std::vector<uint8_t> mac(8);
1✔
571
         Botan::Sodium::crypto_shorthash_siphash24(mac.data(), in.data(), in.size(), key.data());
1✔
572

573
         result.test_eq("expected mac", mac, "E545BE4961CA29A1");
1✔
574

575
         return result;
1✔
576
      }
3✔
577

578
      static Test::Result secretbox_xsalsa20poly1305() {
1✔
579
         Test::Result result("secretbox_xsalsa20poly1305");
1✔
580

581
         const std::vector<uint8_t> ptext(33);
1✔
582
         std::vector<uint8_t> ctext(33);
1✔
583
         const std::vector<uint8_t> nonce(Botan::Sodium::crypto_secretbox_xsalsa20poly1305_noncebytes());
1✔
584
         const std::vector<uint8_t> key(Botan::Sodium::crypto_secretbox_xsalsa20poly1305_keybytes());
1✔
585

586
         result.test_rc_ok("encrypt",
1✔
587
                           Botan::Sodium::crypto_secretbox_xsalsa20poly1305(
588
                              ctext.data(), ptext.data(), ptext.size(), nonce.data(), key.data()));
589

590
         result.test_eq("ctext", ctext, "0000000000000000000000000000000042E45EB764A1B706D4776A849BC2526BC6");
1✔
591

592
         std::vector<uint8_t> recovered(33);
1✔
593
         result.test_rc_ok("decrypt",
1✔
594
                           Botan::Sodium::crypto_secretbox_xsalsa20poly1305_open(
595
                              recovered.data(), ctext.data(), ctext.size(), nonce.data(), key.data()));
1✔
596

597
         result.test_eq("decrypted", recovered, ptext);
2✔
598

599
         return result;
1✔
600
      }
5✔
601

602
      static Test::Result secretbox_xsalsa20poly1305_detached() {
1✔
603
         Test::Result result("secretbox_xsalsa20poly1305");
1✔
604

605
         const std::vector<uint8_t> ptext(33);
1✔
606
         const std::vector<uint8_t> nonce(Botan::Sodium::crypto_secretbox_xsalsa20poly1305_noncebytes());
1✔
607
         const std::vector<uint8_t> key(Botan::Sodium::crypto_secretbox_xsalsa20poly1305_keybytes());
1✔
608
         std::vector<uint8_t> ctext(33);
1✔
609
         std::vector<uint8_t> mac(16);
1✔
610

611
         result.test_rc_ok("encrypt detached",
1✔
612
                           Botan::Sodium::crypto_secretbox_detached(
613
                              ctext.data(), mac.data(), ptext.data(), ptext.size(), nonce.data(), key.data()));
614

615
         result.test_eq("ctext", ctext, "C63EBBFFFE85CE2CEBDEF7DC42F494576D05BDD7B929EBB045F2A793F740277D05");
1✔
616
         result.test_eq("mac", mac, "0D6681DCED740667C699F0AC71BFD1BD");
1✔
617

618
         std::vector<uint8_t> recovered(ctext.size());
1✔
619

620
         result.test_rc_ok("open detached",
1✔
621
                           Botan::Sodium::crypto_secretbox_open_detached(
622
                              recovered.data(), ctext.data(), mac.data(), ctext.size(), nonce.data(), key.data()));
1✔
623

624
         result.test_eq("recovered", recovered, ptext);
2✔
625

626
         return result;
1✔
627
      }
6✔
628

629
      static Test::Result sign_ed25519() {
1✔
630
         Test::Result result("crypto_sign_ed25519");
1✔
631

632
         const std::vector<uint8_t> seed(32);
1✔
633
         std::vector<uint8_t> pk(32);
1✔
634
         std::vector<uint8_t> sk(64);
1✔
635

636
         result.test_rc_ok("seed_keypair",
1✔
637
                           Botan::Sodium::crypto_sign_ed25519_seed_keypair(pk.data(), sk.data(), seed.data()));
638

639
         result.test_eq("pk", pk, "3B6A27BCCEB6A42D62A3A8D02A6F0D73653215771DE243A63AC048A18B59DA29");
1✔
640
         result.test_eq(
1✔
641
            "sk",
642
            sk,
643
            "00000000000000000000000000000000000000000000000000000000000000003B6A27BCCEB6A42D62A3A8D02A6F0D73653215771DE243A63AC048A18B59DA29");
644

645
         const std::vector<uint8_t> msg = {1, 2, 3};
1✔
646
         std::vector<uint8_t> sig(64);
1✔
647
         unsigned long long sig_len = 0;
1✔
648
         result.test_rc_ok(
1✔
649
            "sign_detached",
650
            Botan::Sodium::crypto_sign_ed25519_detached(sig.data(), &sig_len, msg.data(), msg.size(), sk.data()));
1✔
651
         result.confirm("sig len", sig_len == 64);
2✔
652

653
         result.test_eq(
1✔
654
            "sig",
655
            sig,
656
            "2A26779BA6CBB5E54292257F725AF112B273C38728329682D99ED81BA6D7670350AE4CC53C5456FA437128D19298A5D949AB46E3D41AB3DBCFB0B35C895E9304");
657

658
         result.test_rc_ok(
1✔
659
            "verify",
660
            Botan::Sodium::crypto_sign_ed25519_verify_detached(sig.data(), msg.data(), msg.size(), pk.data()));
1✔
661

662
         sig[0] ^= 1;
1✔
663
         result.test_rc_fail(
2✔
664
            "verify",
665
            "reject invalid",
666
            Botan::Sodium::crypto_sign_ed25519_verify_detached(sig.data(), msg.data(), msg.size(), pk.data()));
1✔
667

668
         return result;
1✔
669
      }
5✔
670

671
      static Test::Result stream_salsa20() {
1✔
672
         Test::Result result("crypto_stream_salsa20");
1✔
673

674
         const std::vector<uint8_t> key =
1✔
675
            Botan::hex_decode("0F62B5085BAE0154A7FA4DA0F34699EC3F92E5388BDE3184D72A7DD02376C91C");
1✔
676
         const std::vector<uint8_t> nonce = Botan::hex_decode("288FF65DC42B92F9");
1✔
677
         const std::vector<uint8_t> expected =
1✔
678
            Botan::hex_decode("5E5E71F90199340304ABB22A37B6625BF883FB89CE3B21F54A10B81066EF87DA");
1✔
679

680
         std::vector<uint8_t> output(32);
1✔
681
         Botan::Sodium::crypto_stream_salsa20(output.data(), output.size(), nonce.data(), key.data());
1✔
682
         result.test_eq("stream", output, expected);
2✔
683

684
         std::vector<uint8_t> xor_output(32);
1✔
685
         Botan::Sodium::crypto_stream_salsa20_xor(
1✔
686
            xor_output.data(), output.data(), output.size(), nonce.data(), key.data());
1✔
687
         result.test_eq("stream", xor_output, std::vector<uint8_t>(32));  // all zeros
2✔
688

689
         return result;
1✔
690
      }
5✔
691

692
      static Test::Result stream_xsalsa20() {
1✔
693
         Test::Result result("crypto_stream_xsalsa20");
1✔
694

695
         const std::vector<uint8_t> key =
1✔
696
            Botan::hex_decode("1B27556473E985D462CD51197A9A46C76009549EAC6474F206C4EE0844F68389");
1✔
697
         const std::vector<uint8_t> nonce = Botan::hex_decode("69696EE955B62B73CD62BDA875FC73D68219E0036B7A0B37");
1✔
698
         const std::vector<uint8_t> expected =
1✔
699
            Botan::hex_decode("EEA6A7251C1E72916D11C2CB214D3C252539121D8E234E652D651FA4C8CFF880");
1✔
700

701
         std::vector<uint8_t> output(32);
1✔
702
         Botan::Sodium::crypto_stream_xsalsa20(output.data(), output.size(), nonce.data(), key.data());
1✔
703
         result.test_eq("stream", output, expected);
2✔
704

705
         std::vector<uint8_t> xor_output(32);
1✔
706
         Botan::Sodium::crypto_stream_xsalsa20_xor(
1✔
707
            xor_output.data(), output.data(), output.size(), nonce.data(), key.data());
1✔
708
         result.test_eq("stream", xor_output, std::vector<uint8_t>(32));  // all zeros
2✔
709

710
         return result;
1✔
711
      }
5✔
712

713
      static Test::Result stream_chacha20() {
1✔
714
         Test::Result result("crypto_stream_chacha20");
1✔
715

716
         const std::vector<uint8_t> key =
1✔
717
            Botan::hex_decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
1✔
718
         const std::vector<uint8_t> nonce = Botan::hex_decode("0001020304050607");
1✔
719
         const std::vector<uint8_t> expected =
1✔
720
            Botan::hex_decode("F798A189F195E66982105FFB640BB7757F579DA31602FC93EC01AC56F85AC3C1");
1✔
721

722
         std::vector<uint8_t> output(32);
1✔
723
         Botan::Sodium::crypto_stream_chacha20(output.data(), output.size(), nonce.data(), key.data());
1✔
724
         result.test_eq("stream", output, expected);
2✔
725

726
         std::vector<uint8_t> xor_output(32);
1✔
727
         Botan::Sodium::crypto_stream_chacha20_xor(
1✔
728
            xor_output.data(), output.data(), output.size(), nonce.data(), key.data());
1✔
729
         result.test_eq("stream", xor_output, std::vector<uint8_t>(32));  // all zeros
2✔
730

731
         return result;
1✔
732
      }
5✔
733

734
      static Test::Result stream_chacha20_ietf() {
1✔
735
         Test::Result result("crypto_stream_chacha20");
1✔
736

737
         const std::vector<uint8_t> key =
1✔
738
            Botan::hex_decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
1✔
739
         const std::vector<uint8_t> nonce = Botan::hex_decode("000102030405060708090A0B0C");
1✔
740
         const std::vector<uint8_t> expected =
1✔
741
            Botan::hex_decode("103AF111C18B549D39248FB07D60C29A95D1DB88D892F7B4AF709A5FD47A9E4B");
1✔
742

743
         std::vector<uint8_t> output(32);
1✔
744
         Botan::Sodium::crypto_stream_chacha20_ietf(output.data(), output.size(), nonce.data(), key.data());
1✔
745
         result.test_eq("stream", output, expected);
2✔
746

747
         std::vector<uint8_t> xor_output(32);
1✔
748
         Botan::Sodium::crypto_stream_chacha20_ietf_xor(
1✔
749
            xor_output.data(), output.data(), output.size(), nonce.data(), key.data());
1✔
750
         result.test_eq("stream", xor_output, std::vector<uint8_t>(32));  // all zeros
2✔
751

752
         return result;
1✔
753
      }
5✔
754

755
      static Test::Result stream_xchacha20() {
1✔
756
         Test::Result result("crypto_stream_xchacha20");
1✔
757

758
         const std::vector<uint8_t> key =
1✔
759
            Botan::hex_decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
1✔
760
         const std::vector<uint8_t> nonce = Botan::hex_decode("000102030405060708090a0b0c0d0e0f1011121314151617");
1✔
761
         const std::vector<uint8_t> expected =
1✔
762
            Botan::hex_decode("e53a61cef151e81401067de33adfc02e90ab205361b49b539fda7f0e63b1bc7d");
1✔
763

764
         std::vector<uint8_t> output(32);
1✔
765
         Botan::Sodium::crypto_stream_xchacha20(output.data(), output.size(), nonce.data(), key.data());
1✔
766
         result.test_eq("stream", output, expected);
2✔
767

768
         std::vector<uint8_t> xor_output(32);
1✔
769
         Botan::Sodium::crypto_stream_xchacha20_xor(
1✔
770
            xor_output.data(), output.data(), output.size(), nonce.data(), key.data());
1✔
771
         result.test_eq("stream", xor_output, std::vector<uint8_t>(32));  // all zeros
2✔
772

773
         return result;
1✔
774
      }
5✔
775
};
776

777
BOTAN_REGISTER_TEST("compat", "sodium", Sodium_API_Tests);
778

779
#endif
780

781
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc