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

randombit / botan / 5111374265

29 May 2023 11:19AM UTC coverage: 92.227% (+0.5%) from 91.723%
5111374265

push

github

randombit
Next release will be 3.1.0. Update release notes

75588 of 81959 relevant lines covered (92.23%)

11886470.91 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
#endif
12

13
namespace Botan_Tests {
14

15
#if defined(BOTAN_HAS_SODIUM_API)
16

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

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

45
         return results;
1✔
46
      }
×
47

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

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

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

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

60
         return result;
1✔
61
      }
×
62

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

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

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

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

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

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

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

98
         return result;
1✔
99
      }
2✔
100

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

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

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

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

111
         return result;
1✔
112
      }
1✔
113

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

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

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

125
         return result;
1✔
126
      }
1✔
127

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

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

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

136
         return result;
1✔
137
      }
1✔
138

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

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

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

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

154
         const std::vector<uint8_t> ptext(15);
1✔
155
         std::vector<uint8_t> ctext(ptext.size() + 16);
1✔
156
         const std::vector<uint8_t> nonce(Botan::Sodium::crypto_box_noncebytes());
2✔
157

158
         result.test_rc_ok("crypto_box_easy",
1✔
159
                           Botan::Sodium::crypto_box_easy(
160
                              ctext.data(), ptext.data(), ptext.size(), nonce.data(), pk2.data(), sk1.data()));
1✔
161

162
         result.test_eq("ctext1", ctext, "11D78D4C32C5674390C0425D8BBB5928AFE7F767E2A7E4427E1A1362F1FD92");
1✔
163

164
         result.test_rc_ok("crypto_box_easy",
1✔
165
                           Botan::Sodium::crypto_box_easy(
166
                              ctext.data(), ptext.data(), ptext.size(), nonce.data(), pk1.data(), sk2.data()));
1✔
167

168
         // same shared secret, same nonce, same data -> same ciphertext
169
         result.test_eq("ctext2", ctext, "11D78D4C32C5674390C0425D8BBB5928AFE7F767E2A7E4427E1A1362F1FD92");
1✔
170

171
         std::vector<uint8_t> recovered(15);
1✔
172

173
         result.test_rc_ok("crypto_box_open_easy",
2✔
174
                           Botan::Sodium::crypto_box_open_easy(
175
                              recovered.data(), ctext.data(), ctext.size(), nonce.data(), pk1.data(), sk2.data()));
1✔
176

177
         result.test_eq("recover1", recovered, ptext);
1✔
178

179
         result.test_rc_ok("crypto_box_open_easy",
2✔
180
                           Botan::Sodium::crypto_box_open_easy(
181
                              recovered.data(), ctext.data(), ctext.size(), nonce.data(), pk2.data(), sk1.data()));
1✔
182

183
         result.test_eq("recover1", recovered, ptext);
1✔
184

185
         return result;
1✔
186
      }
9✔
187

188
      static Test::Result aead_chacha20poly1305() {
1✔
189
         Test::Result result("crypto_aead_chacha20poly1305");
1✔
190

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

197
         result.test_eq("key len", Botan::Sodium::crypto_aead_chacha20poly1305_keybytes(), key.size());
1✔
198
         result.test_eq("nonce len", Botan::Sodium::crypto_aead_chacha20poly1305_npubbytes(), nonce.size());
1✔
199

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

214
         result.test_eq("maclen", size_t(maclen), 16);
1✔
215
         result.test_eq("mac", mac, "09998877ABA156DDC68F8344098F68B9");
1✔
216
         result.test_eq("ctext", ctext, "9F07E7BE5551387A98BA977C732D08");
1✔
217

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

230
         result.test_eq("plaintext", recovered, in);
1✔
231

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

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

258
         result.test_eq("ctext_len", size_t(ctext_len), ctext.size());
1✔
259
         result.test_eq("ctext", ctext, "9F07E7BE5551387A98BA977C732D0809998877ABA156DDC68F8344098F68B9");
1✔
260

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

273
         result.test_eq("recovered", recovered, in);
1✔
274

275
         return result;
1✔
276
      }
6✔
277

278
      static Test::Result aead_chacha20poly1305_ietf() {
1✔
279
         Test::Result result("crypto_aead_chacha20poly1305_ietf");
1✔
280

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

287
         result.test_eq("key len", Botan::Sodium::crypto_aead_chacha20poly1305_ietf_keybytes(), key.size());
1✔
288
         result.test_eq("nonce len", Botan::Sodium::crypto_aead_chacha20poly1305_ietf_npubbytes(), nonce.size());
1✔
289

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

304
         result.test_eq("maclen", size_t(maclen), 16);
1✔
305
         result.test_eq("mac", mac, "3679F1FB9843FD81E26D962888296954");
1✔
306
         result.test_eq("ctext", ctext, "9F07E7BE5551387A98BA977C732D08");
1✔
307

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

320
         result.test_eq("plaintext", recovered, in);
1✔
321

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

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

348
         result.test_eq("ctext_len", size_t(ctext_len), ctext.size());
1✔
349
         result.test_eq("ctext", ctext, "9F07E7BE5551387A98BA977C732D083679F1FB9843FD81E26D962888296954");
1✔
350

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

363
         result.test_eq("recovered", recovered, in);
1✔
364

365
         return result;
1✔
366
      }
6✔
367

368
      static Test::Result aead_xchacha20poly1305() {
1✔
369
         Test::Result result("crypto_aead_xchacha20poly1305");
1✔
370

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

377
         result.test_eq("key len", Botan::Sodium::crypto_aead_xchacha20poly1305_ietf_keybytes(), key.size());
1✔
378
         result.test_eq("nonce len", Botan::Sodium::crypto_aead_xchacha20poly1305_ietf_npubbytes(), nonce.size());
1✔
379

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

394
         result.test_eq("maclen", size_t(maclen), 16);
1✔
395
         result.test_eq("mac", mac, "b2f7033812ac9ebd3745e2c99c7bbfeb");
1✔
396
         result.test_eq("ctext", ctext, "789e9689e5208d7fd9e1f3c5b5341f");
1✔
397

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

410
         result.test_eq("plaintext", recovered, in);
1✔
411

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

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

438
         result.test_eq("ctext_len", size_t(ctext_len), ctext.size());
1✔
439
         result.test_eq("ctext", ctext, "789e9689e5208d7fd9e1f3c5b5341fb2f7033812ac9ebd3745e2c99c7bbfeb");
1✔
440

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

453
         result.test_eq("recovered", recovered, in);
1✔
454

455
         return result;
1✔
456
      }
6✔
457

458
      static Test::Result auth_hmacsha512() {
1✔
459
         Test::Result result("crypto_auth_hmacsha512");
1✔
460

461
         const std::vector<uint8_t> key =
1✔
462
            Botan::hex_decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
1✔
463
         const std::vector<uint8_t> in = Botan::hex_decode("616263");
1✔
464

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

467
         std::vector<uint8_t> mac(64);
1✔
468
         Botan::Sodium::crypto_auth_hmacsha512(mac.data(), in.data(), in.size(), key.data());
1✔
469

470
         result.test_eq(
1✔
471
            "expected mac",
472
            mac,
473
            "69D4A21E226BF0D348CB9A847C01CF24E93E8AC30D7C951704B936F82F795A624B470E23ABD33AC8700E797F0F2A499B932BAC7D283BBBB37D8FECF70D5E08A7");
474

475
         result.test_rc_ok("verify",
1✔
476
                           Botan::Sodium::crypto_auth_hmacsha512_verify(mac.data(), in.data(), in.size(), key.data()));
1✔
477

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

484
         return result;
1✔
485
      }
3✔
486

487
      static Test::Result auth_hmacsha512256() {
1✔
488
         Test::Result result("crypto_auth_hmacsha512256");
1✔
489

490
         const std::vector<uint8_t> key =
1✔
491
            Botan::hex_decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
1✔
492
         const std::vector<uint8_t> in = Botan::hex_decode("616263");
1✔
493

494
         std::vector<uint8_t> mac(32);
1✔
495
         Botan::Sodium::crypto_auth_hmacsha512256(mac.data(), in.data(), in.size(), key.data());
1✔
496

497
         result.test_eq("expected mac", mac, "69D4A21E226BF0D348CB9A847C01CF24E93E8AC30D7C951704B936F82F795A62");
1✔
498

499
         result.test_rc_ok(
1✔
500
            "verify", Botan::Sodium::crypto_auth_hmacsha512256_verify(mac.data(), in.data(), in.size(), key.data()));
1✔
501

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

508
         return result;
1✔
509
      }
3✔
510

511
      static Test::Result auth_hmacsha256() {
1✔
512
         Test::Result result("crypto_auth_hmacsha256");
1✔
513

514
         const std::vector<uint8_t> key =
1✔
515
            Botan::hex_decode("0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20");
1✔
516
         const std::vector<uint8_t> in = Botan::hex_decode("616263");
1✔
517

518
         std::vector<uint8_t> mac(32);
1✔
519
         Botan::Sodium::crypto_auth_hmacsha256(mac.data(), in.data(), in.size(), key.data());
1✔
520

521
         result.test_eq("expected mac", mac, "A21B1F5D4CF4F73A4DD939750F7A066A7F98CC131CB16A6692759021CFAB8181");
1✔
522

523
         result.test_rc_ok("verify",
1✔
524
                           Botan::Sodium::crypto_auth_hmacsha256_verify(mac.data(), in.data(), in.size(), key.data()));
1✔
525

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

532
         return result;
1✔
533
      }
3✔
534

535
      static Test::Result auth_poly1305() {
1✔
536
         Test::Result result("crypto_onetimeauth_poly1305");
1✔
537

538
         const std::vector<uint8_t> key(Botan::Sodium::crypto_onetimeauth_keybytes(), 0x42);
1✔
539
         const std::vector<uint8_t> in(15);
1✔
540

541
         std::vector<uint8_t> mac(16);
1✔
542

543
         result.test_rc_ok("poly1305",
1✔
544
                           Botan::Sodium::crypto_onetimeauth_poly1305(mac.data(), in.data(), in.size(), key.data()));
545

546
         result.test_eq("expected mac", mac, "12154512151545121515451215154584");
1✔
547

548
         result.test_rc_ok(
1✔
549
            "poly1305 verify",
550
            Botan::Sodium::crypto_onetimeauth_poly1305_verify(mac.data(), in.data(), in.size(), key.data()));
1✔
551

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

558
         return result;
1✔
559
      }
3✔
560

561
      static Test::Result shorthash_siphash24() {
1✔
562
         Test::Result result("crypto_shorthash_siphash24");
1✔
563

564
         const std::vector<uint8_t> key = Botan::hex_decode("000102030405060708090A0B0C0D0E0F");
1✔
565
         const std::vector<uint8_t> in = Botan::hex_decode("000102030405060708090A0B0C0D0E");
1✔
566

567
         std::vector<uint8_t> mac(8);
1✔
568
         Botan::Sodium::crypto_shorthash_siphash24(mac.data(), in.data(), in.size(), key.data());
1✔
569

570
         result.test_eq("expected mac", mac, "E545BE4961CA29A1");
1✔
571

572
         return result;
1✔
573
      }
3✔
574

575
      static Test::Result secretbox_xsalsa20poly1305() {
1✔
576
         Test::Result result("secretbox_xsalsa20poly1305");
1✔
577

578
         const std::vector<uint8_t> ptext(33);
1✔
579
         std::vector<uint8_t> ctext(33);
1✔
580
         const std::vector<uint8_t> nonce(Botan::Sodium::crypto_secretbox_xsalsa20poly1305_noncebytes());
1✔
581
         const std::vector<uint8_t> key(Botan::Sodium::crypto_secretbox_xsalsa20poly1305_keybytes());
1✔
582

583
         result.test_rc_ok("encrypt",
1✔
584
                           Botan::Sodium::crypto_secretbox_xsalsa20poly1305(
585
                              ctext.data(), ptext.data(), ptext.size(), nonce.data(), key.data()));
586

587
         result.test_eq("ctext", ctext, "0000000000000000000000000000000042E45EB764A1B706D4776A849BC2526BC6");
1✔
588

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

594
         result.test_eq("decrypted", recovered, ptext);
1✔
595

596
         return result;
1✔
597
      }
5✔
598

599
      static Test::Result secretbox_xsalsa20poly1305_detached() {
1✔
600
         Test::Result result("secretbox_xsalsa20poly1305");
1✔
601

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

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

612
         result.test_eq("ctext", ctext, "C63EBBFFFE85CE2CEBDEF7DC42F494576D05BDD7B929EBB045F2A793F740277D05");
1✔
613
         result.test_eq("mac", mac, "0D6681DCED740667C699F0AC71BFD1BD");
1✔
614

615
         std::vector<uint8_t> recovered(ctext.size());
1✔
616

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

621
         result.test_eq("recovered", recovered, ptext);
1✔
622

623
         return result;
1✔
624
      }
6✔
625

626
      static Test::Result sign_ed25519() {
1✔
627
         Test::Result result("crypto_sign_ed25519");
1✔
628

629
         const std::vector<uint8_t> seed(32);
1✔
630
         std::vector<uint8_t> pk(32), sk(64);
1✔
631

632
         result.test_rc_ok("seed_keypair",
1✔
633
                           Botan::Sodium::crypto_sign_ed25519_seed_keypair(pk.data(), sk.data(), seed.data()));
634

635
         result.test_eq("pk", pk, "3B6A27BCCEB6A42D62A3A8D02A6F0D73653215771DE243A63AC048A18B59DA29");
1✔
636
         result.test_eq(
1✔
637
            "sk",
638
            sk,
639
            "00000000000000000000000000000000000000000000000000000000000000003B6A27BCCEB6A42D62A3A8D02A6F0D73653215771DE243A63AC048A18B59DA29");
640

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

649
         result.test_eq(
1✔
650
            "sig",
651
            sig,
652
            "2A26779BA6CBB5E54292257F725AF112B273C38728329682D99ED81BA6D7670350AE4CC53C5456FA437128D19298A5D949AB46E3D41AB3DBCFB0B35C895E9304");
653

654
         result.test_rc_ok(
1✔
655
            "verify",
656
            Botan::Sodium::crypto_sign_ed25519_verify_detached(sig.data(), msg.data(), msg.size(), pk.data()));
1✔
657

658
         sig[0] ^= 1;
1✔
659
         result.test_rc_fail(
2✔
660
            "verify",
661
            "reject invalid",
662
            Botan::Sodium::crypto_sign_ed25519_verify_detached(sig.data(), msg.data(), msg.size(), pk.data()));
1✔
663

664
         return result;
1✔
665
      }
5✔
666

667
      static Test::Result stream_salsa20() {
1✔
668
         Test::Result result("crypto_stream_salsa20");
1✔
669

670
         const std::vector<uint8_t> key =
1✔
671
            Botan::hex_decode("0F62B5085BAE0154A7FA4DA0F34699EC3F92E5388BDE3184D72A7DD02376C91C");
1✔
672
         const std::vector<uint8_t> nonce = Botan::hex_decode("288FF65DC42B92F9");
1✔
673
         const std::vector<uint8_t> expected =
1✔
674
            Botan::hex_decode("5E5E71F90199340304ABB22A37B6625BF883FB89CE3B21F54A10B81066EF87DA");
1✔
675

676
         std::vector<uint8_t> output(32);
1✔
677
         Botan::Sodium::crypto_stream_salsa20(output.data(), output.size(), nonce.data(), key.data());
1✔
678
         result.test_eq("stream", output, expected);
1✔
679

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

685
         return result;
1✔
686
      }
5✔
687

688
      static Test::Result stream_xsalsa20() {
1✔
689
         Test::Result result("crypto_stream_xsalsa20");
1✔
690

691
         const std::vector<uint8_t> key =
1✔
692
            Botan::hex_decode("1B27556473E985D462CD51197A9A46C76009549EAC6474F206C4EE0844F68389");
1✔
693
         const std::vector<uint8_t> nonce = Botan::hex_decode("69696EE955B62B73CD62BDA875FC73D68219E0036B7A0B37");
1✔
694
         const std::vector<uint8_t> expected =
1✔
695
            Botan::hex_decode("EEA6A7251C1E72916D11C2CB214D3C252539121D8E234E652D651FA4C8CFF880");
1✔
696

697
         std::vector<uint8_t> output(32);
1✔
698
         Botan::Sodium::crypto_stream_xsalsa20(output.data(), output.size(), nonce.data(), key.data());
1✔
699
         result.test_eq("stream", output, expected);
1✔
700

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

706
         return result;
1✔
707
      }
5✔
708

709
      static Test::Result stream_chacha20() {
1✔
710
         Test::Result result("crypto_stream_chacha20");
1✔
711

712
         const std::vector<uint8_t> key =
1✔
713
            Botan::hex_decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
1✔
714
         const std::vector<uint8_t> nonce = Botan::hex_decode("0001020304050607");
1✔
715
         const std::vector<uint8_t> expected =
1✔
716
            Botan::hex_decode("F798A189F195E66982105FFB640BB7757F579DA31602FC93EC01AC56F85AC3C1");
1✔
717

718
         std::vector<uint8_t> output(32);
1✔
719
         Botan::Sodium::crypto_stream_chacha20(output.data(), output.size(), nonce.data(), key.data());
1✔
720
         result.test_eq("stream", output, expected);
1✔
721

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

727
         return result;
1✔
728
      }
5✔
729

730
      static Test::Result stream_chacha20_ietf() {
1✔
731
         Test::Result result("crypto_stream_chacha20");
1✔
732

733
         const std::vector<uint8_t> key =
1✔
734
            Botan::hex_decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
1✔
735
         const std::vector<uint8_t> nonce = Botan::hex_decode("000102030405060708090A0B0C");
1✔
736
         const std::vector<uint8_t> expected =
1✔
737
            Botan::hex_decode("103AF111C18B549D39248FB07D60C29A95D1DB88D892F7B4AF709A5FD47A9E4B");
1✔
738

739
         std::vector<uint8_t> output(32);
1✔
740
         Botan::Sodium::crypto_stream_chacha20_ietf(output.data(), output.size(), nonce.data(), key.data());
1✔
741
         result.test_eq("stream", output, expected);
1✔
742

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

748
         return result;
1✔
749
      }
5✔
750

751
      static Test::Result stream_xchacha20() {
1✔
752
         Test::Result result("crypto_stream_xchacha20");
1✔
753

754
         const std::vector<uint8_t> key =
1✔
755
            Botan::hex_decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
1✔
756
         const std::vector<uint8_t> nonce = Botan::hex_decode("000102030405060708090a0b0c0d0e0f1011121314151617");
1✔
757
         const std::vector<uint8_t> expected =
1✔
758
            Botan::hex_decode("e53a61cef151e81401067de33adfc02e90ab205361b49b539fda7f0e63b1bc7d");
1✔
759

760
         std::vector<uint8_t> output(32);
1✔
761
         Botan::Sodium::crypto_stream_xchacha20(output.data(), output.size(), nonce.data(), key.data());
1✔
762
         result.test_eq("stream", output, expected);
1✔
763

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

769
         return result;
1✔
770
      }
5✔
771
};
772

773
BOTAN_REGISTER_TEST("compat", "sodium", Sodium_API_Tests);
774

775
#endif
776

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