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

randombit / botan / 21753596263

06 Feb 2026 02:13PM UTC coverage: 90.063% (-0.01%) from 90.073%
21753596263

Pull #5289

github

web-flow
Merge 587099284 into 8ea0ca252
Pull Request #5289: Further misc header reductions, forward declarations, etc

102237 of 113517 relevant lines covered (90.06%)

11402137.11 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/hex.h>
11
   #include <botan/sodium.h>
12
   #include <cstring>
13
#endif
14

15
namespace Botan_Tests {
16

17
#if defined(BOTAN_HAS_SODIUM_API)
18

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

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

47
         return results;
1✔
48
      }
×
49

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

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

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

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

62
         return result;
1✔
63
      }
×
64

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

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

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

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

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

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

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

100
         return result;
1✔
101
      }
1✔
102

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

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

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

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

113
         return result;
1✔
114
      }
1✔
115

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

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

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

127
         return result;
1✔
128
      }
1✔
129

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

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

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

138
         return result;
1✔
139
      }
1✔
140

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

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

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

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

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

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

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

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

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

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

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

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

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

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

189
         return result;
1✔
190
      }
1✔
191

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

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

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

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

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

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

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

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

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

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

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

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

279
         return result;
1✔
280
      }
1✔
281

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

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

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

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

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

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

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

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

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

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

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

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

369
         return result;
1✔
370
      }
1✔
371

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

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

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

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

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

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

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

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

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

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

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

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

459
         return result;
1✔
460
      }
1✔
461

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

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

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

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

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

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

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

488
         return result;
1✔
489
      }
1✔
490

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

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

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

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

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

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

512
         return result;
1✔
513
      }
1✔
514

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

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

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

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

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

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

536
         return result;
1✔
537
      }
1✔
538

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

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

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

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

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

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

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

562
         return result;
1✔
563
      }
1✔
564

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

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

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

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

576
         return result;
1✔
577
      }
1✔
578

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

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

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

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

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

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

600
         return result;
1✔
601
      }
1✔
602

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

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

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

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

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

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

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

627
         return result;
1✔
628
      }
1✔
629

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

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

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

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

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

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

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

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

669
         return result;
1✔
670
      }
1✔
671

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

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

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

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

690
         return result;
1✔
691
      }
1✔
692

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

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

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

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

711
         return result;
1✔
712
      }
1✔
713

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

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

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

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

732
         return result;
1✔
733
      }
1✔
734

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

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

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

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

753
         return result;
1✔
754
      }
1✔
755

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

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

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

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

774
         return result;
1✔
775
      }
1✔
776
};
777

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

780
#endif
781

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