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

randombit / botan / 11844561993

14 Nov 2024 07:58PM UTC coverage: 91.178% (+0.1%) from 91.072%
11844561993

Pull #4435

github

web-flow
Merge 81dcb29da into e430f157a
Pull Request #4435: Test duration values ​​are now presented in seconds with six digits of precision. Tests without time measurements have been edited.

91856 of 100744 relevant lines covered (91.18%)

9311006.71 hits per line

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

99.57
/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
         result.start_timer();
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
         result.end_timer();
1✔
62
         return result;
1✔
63
      }
×
64

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

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

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

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

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

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

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

101
         result.end_timer();
1✔
102
         return result;
1✔
103
      }
2✔
104

105
      static Test::Result randombytes_buf_deterministic() {
1✔
106
         Test::Result result("randombytes_buf_deterministic");
1✔
107
         result.start_timer();
1✔
108

109
         const uint8_t seed[32] = {1, 0};
1✔
110
         std::vector<uint8_t> output(18);
1✔
111

112
         Botan::Sodium::randombytes_buf_deterministic(output.data(), output.size(), seed);
1✔
113

114
         result.test_eq("output", output, "04069B5F37E82F91DC37FD5EB99F1A4124B1");
1✔
115

116
         result.end_timer();
1✔
117
         return result;
1✔
118
      }
1✔
119

120
      static Test::Result hash_sha512() {
1✔
121
         Test::Result result("crypto_hash_sha512");
1✔
122
         result.start_timer();
1✔
123

124
         std::vector<uint8_t> output(64);
1✔
125
         Botan::Sodium::crypto_hash_sha512(output.data(), nullptr, 0);
1✔
126

127
         result.test_eq(
1✔
128
            "expected output",
129
            output,
130
            "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
131

132
         result.end_timer();
1✔
133
         return result;
1✔
134
      }
1✔
135

136
      static Test::Result hash_sha256() {
1✔
137
         Test::Result result("crypto_hash_sha256");
1✔
138
         result.start_timer();
1✔
139

140
         std::vector<uint8_t> output(32);
1✔
141
         Botan::Sodium::crypto_hash_sha256(output.data(), nullptr, 0);
1✔
142

143
         result.test_eq("expected output", output, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
1✔
144

145
         result.end_timer();
1✔
146
         return result;
1✔
147
      }
1✔
148

149
      static Test::Result box_curve25519xsalsa20poly1305() {
1✔
150
         Test::Result result("crypto_box_curve25519xsalsa20poly1305");
1✔
151
         result.start_timer();
1✔
152

153
         const std::vector<uint8_t> seed(32);
1✔
154

155
         std::vector<uint8_t> pk1(32), sk1(32);
1✔
156
         result.test_rc_ok("seed_keypair", Botan::Sodium::crypto_box_seed_keypair(pk1.data(), sk1.data(), seed.data()));
1✔
157
         result.test_eq("pk1", pk1, "5BF55C73B82EBE22BE80F3430667AF570FAE2556A6415E6B30D4065300AA947D");
1✔
158
         result.test_eq("sk1", sk1, "5046ADC1DBA838867B2BBBFDD0C3423E58B57970B5267A90F57960924A87F196");
1✔
159

160
         std::vector<uint8_t> pk2(32), sk2(32);
1✔
161
         result.test_rc_ok("seed_keypair", Botan::Sodium::crypto_box_seed_keypair(pk2.data(), sk2.data(), sk1.data()));
1✔
162
         result.test_eq("pk2", pk2, "E0CFC9C6B2FE5BF85F48671691225C03D763F2305206FE3D3B0ED7B76153684A");
1✔
163
         result.test_eq("sk2", sk2, "58E2E4C71F138FBC97F9341735B4581746761F9A104540007FE12CFC4D9FDA15");
2✔
164

165
         const std::vector<uint8_t> ptext(15);
1✔
166
         std::vector<uint8_t> ctext(ptext.size() + 16);
1✔
167
         const std::vector<uint8_t> nonce(Botan::Sodium::crypto_box_noncebytes());
1✔
168

169
         result.test_rc_ok("crypto_box_easy",
1✔
170
                           Botan::Sodium::crypto_box_easy(
171
                              ctext.data(), ptext.data(), ptext.size(), nonce.data(), pk2.data(), sk1.data()));
1✔
172

173
         result.test_eq("ctext1", ctext, "11D78D4C32C5674390C0425D8BBB5928AFE7F767E2A7E4427E1A1362F1FD92");
1✔
174

175
         result.test_rc_ok("crypto_box_easy",
1✔
176
                           Botan::Sodium::crypto_box_easy(
177
                              ctext.data(), ptext.data(), ptext.size(), nonce.data(), pk1.data(), sk2.data()));
1✔
178

179
         // same shared secret, same nonce, same data -> same ciphertext
180
         result.test_eq("ctext2", ctext, "11D78D4C32C5674390C0425D8BBB5928AFE7F767E2A7E4427E1A1362F1FD92");
1✔
181

182
         std::vector<uint8_t> recovered(15);
1✔
183

184
         result.test_rc_ok("crypto_box_open_easy",
1✔
185
                           Botan::Sodium::crypto_box_open_easy(
186
                              recovered.data(), ctext.data(), ctext.size(), nonce.data(), pk1.data(), sk2.data()));
1✔
187

188
         result.test_eq("recover1", recovered, ptext);
1✔
189

190
         result.test_rc_ok("crypto_box_open_easy",
1✔
191
                           Botan::Sodium::crypto_box_open_easy(
192
                              recovered.data(), ctext.data(), ctext.size(), nonce.data(), pk2.data(), sk1.data()));
1✔
193

194
         result.test_eq("recover1", recovered, ptext);
1✔
195

196
         result.end_timer();
1✔
197
         return result;
1✔
198
      }
9✔
199

200
      static Test::Result aead_chacha20poly1305() {
1✔
201
         Test::Result result("crypto_aead_chacha20poly1305");
1✔
202
         result.start_timer();
1✔
203

204
         const std::vector<uint8_t> key =
1✔
205
            Botan::hex_decode("0000000000000000000000000000000000000000000000000000000000000000");
1✔
206
         const std::vector<uint8_t> ad;
1✔
207
         const std::vector<uint8_t> nonce = Botan::hex_decode("0000000000000000");
1✔
208
         const std::vector<uint8_t> in = Botan::hex_decode("000000000000000000000000000000");
1✔
209

210
         result.test_eq("key len", Botan::Sodium::crypto_aead_chacha20poly1305_keybytes(), key.size());
1✔
211
         result.test_eq("nonce len", Botan::Sodium::crypto_aead_chacha20poly1305_npubbytes(), nonce.size());
1✔
212

213
         std::vector<uint8_t> ctext(in.size());
1✔
214
         std::vector<uint8_t> mac(16);
1✔
215
         unsigned long long maclen = 0;
1✔
216
         Botan::Sodium::crypto_aead_chacha20poly1305_encrypt_detached(ctext.data(),
1✔
217
                                                                      mac.data(),
218
                                                                      &maclen,
219
                                                                      in.data(),
220
                                                                      in.size(),
221
                                                                      ad.data(),
222
                                                                      ad.size(),
223
                                                                      nullptr,
224
                                                                      nonce.data(),
225
                                                                      key.data());
226

227
         result.test_eq("maclen", size_t(maclen), 16);
1✔
228
         result.test_eq("mac", mac, "09998877ABA156DDC68F8344098F68B9");
1✔
229
         result.test_eq("ctext", ctext, "9F07E7BE5551387A98BA977C732D08");
1✔
230

231
         std::vector<uint8_t> recovered(ctext.size());
1✔
232
         result.test_rc_ok("decrypt",
1✔
233
                           Botan::Sodium::crypto_aead_chacha20poly1305_decrypt_detached(recovered.data(),
234
                                                                                        nullptr,
235
                                                                                        ctext.data(),
1✔
236
                                                                                        ctext.size(),
237
                                                                                        mac.data(),
1✔
238
                                                                                        ad.data(),
239
                                                                                        ad.size(),
240
                                                                                        nonce.data(),
241
                                                                                        key.data()));
242

243
         result.test_eq("plaintext", recovered, in);
1✔
244

245
         mac[0] ^= 1;
1✔
246
         result.test_rc_fail("decrypt",
2✔
247
                             "invalid ciphertext",
248
                             Botan::Sodium::crypto_aead_chacha20poly1305_decrypt_detached(recovered.data(),
249
                                                                                          nullptr,
250
                                                                                          ctext.data(),
1✔
251
                                                                                          ctext.size(),
252
                                                                                          mac.data(),
1✔
253
                                                                                          ad.data(),
254
                                                                                          ad.size(),
255
                                                                                          nonce.data(),
256
                                                                                          key.data()));
257

258
         ctext.resize(in.size() + mac.size());
1✔
259
         unsigned long long ctext_len;
1✔
260
         result.test_rc_ok("encrypt",
1✔
261
                           Botan::Sodium::crypto_aead_chacha20poly1305_encrypt(ctext.data(),
262
                                                                               &ctext_len,
263
                                                                               in.data(),
264
                                                                               in.size(),
265
                                                                               ad.data(),
266
                                                                               ad.size(),
267
                                                                               nullptr,
268
                                                                               nonce.data(),
269
                                                                               key.data()));
270

271
         result.test_eq("ctext_len", size_t(ctext_len), ctext.size());
1✔
272
         result.test_eq("ctext", ctext, "9F07E7BE5551387A98BA977C732D0809998877ABA156DDC68F8344098F68B9");
1✔
273

274
         unsigned long long recovered_len = 0;
1✔
275
         result.test_rc_ok("decrypt",
1✔
276
                           Botan::Sodium::crypto_aead_chacha20poly1305_decrypt(recovered.data(),
277
                                                                               &recovered_len,
278
                                                                               nullptr,
279
                                                                               ctext.data(),
1✔
280
                                                                               ctext.size(),
281
                                                                               ad.data(),
282
                                                                               ad.size(),
283
                                                                               nonce.data(),
284
                                                                               key.data()));
285

286
         result.test_eq("recovered", recovered, in);
1✔
287

288
         result.end_timer();
1✔
289
         return result;
1✔
290
      }
5✔
291

292
      static Test::Result aead_chacha20poly1305_ietf() {
1✔
293
         Test::Result result("crypto_aead_chacha20poly1305_ietf");
1✔
294
         result.start_timer();
1✔
295

296
         const std::vector<uint8_t> key =
1✔
297
            Botan::hex_decode("0000000000000000000000000000000000000000000000000000000000000000");
1✔
298
         const std::vector<uint8_t> ad;
1✔
299
         const std::vector<uint8_t> nonce = Botan::hex_decode("000000000000000000000000");
1✔
300
         const std::vector<uint8_t> in = Botan::hex_decode("000000000000000000000000000000");
1✔
301

302
         result.test_eq("key len", Botan::Sodium::crypto_aead_chacha20poly1305_ietf_keybytes(), key.size());
1✔
303
         result.test_eq("nonce len", Botan::Sodium::crypto_aead_chacha20poly1305_ietf_npubbytes(), nonce.size());
1✔
304

305
         std::vector<uint8_t> ctext(in.size());
1✔
306
         std::vector<uint8_t> mac(16);
1✔
307
         unsigned long long maclen = 0;
1✔
308
         Botan::Sodium::crypto_aead_chacha20poly1305_ietf_encrypt_detached(ctext.data(),
1✔
309
                                                                           mac.data(),
310
                                                                           &maclen,
311
                                                                           in.data(),
312
                                                                           in.size(),
313
                                                                           ad.data(),
314
                                                                           ad.size(),
315
                                                                           nullptr,
316
                                                                           nonce.data(),
317
                                                                           key.data());
318

319
         result.test_eq("maclen", size_t(maclen), 16);
1✔
320
         result.test_eq("mac", mac, "3679F1FB9843FD81E26D962888296954");
1✔
321
         result.test_eq("ctext", ctext, "9F07E7BE5551387A98BA977C732D08");
1✔
322

323
         std::vector<uint8_t> recovered(ctext.size());
1✔
324
         result.test_rc_ok("decrypt",
1✔
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
         result.test_eq("plaintext", recovered, in);
1✔
336

337
         mac[0] ^= 1;
1✔
338
         result.test_rc_fail("decrypt",
2✔
339
                             "invalid ciphertext",
340
                             Botan::Sodium::crypto_aead_chacha20poly1305_ietf_decrypt_detached(recovered.data(),
341
                                                                                               nullptr,
342
                                                                                               ctext.data(),
1✔
343
                                                                                               ctext.size(),
344
                                                                                               mac.data(),
1✔
345
                                                                                               ad.data(),
346
                                                                                               ad.size(),
347
                                                                                               nonce.data(),
348
                                                                                               key.data()));
349

350
         ctext.resize(in.size() + mac.size());
1✔
351
         unsigned long long ctext_len;
1✔
352
         result.test_rc_ok("encrypt",
1✔
353
                           Botan::Sodium::crypto_aead_chacha20poly1305_ietf_encrypt(ctext.data(),
354
                                                                                    &ctext_len,
355
                                                                                    in.data(),
356
                                                                                    in.size(),
357
                                                                                    ad.data(),
358
                                                                                    ad.size(),
359
                                                                                    nullptr,
360
                                                                                    nonce.data(),
361
                                                                                    key.data()));
362

363
         result.test_eq("ctext_len", size_t(ctext_len), ctext.size());
1✔
364
         result.test_eq("ctext", ctext, "9F07E7BE5551387A98BA977C732D083679F1FB9843FD81E26D962888296954");
1✔
365

366
         unsigned long long recovered_len = 0;
1✔
367
         result.test_rc_ok("decrypt",
1✔
368
                           Botan::Sodium::crypto_aead_chacha20poly1305_ietf_decrypt(recovered.data(),
369
                                                                                    &recovered_len,
370
                                                                                    nullptr,
371
                                                                                    ctext.data(),
1✔
372
                                                                                    ctext.size(),
373
                                                                                    ad.data(),
374
                                                                                    ad.size(),
375
                                                                                    nonce.data(),
376
                                                                                    key.data()));
377

378
         result.test_eq("recovered", recovered, in);
1✔
379

380
         result.end_timer();
1✔
381
         return result;
1✔
382
      }
5✔
383

384
      static Test::Result aead_xchacha20poly1305() {
1✔
385
         Test::Result result("crypto_aead_xchacha20poly1305");
1✔
386
         result.start_timer();
1✔
387

388
         const std::vector<uint8_t> key =
1✔
389
            Botan::hex_decode("0000000000000000000000000000000000000000000000000000000000000000");
1✔
390
         const std::vector<uint8_t> ad;
1✔
391
         const std::vector<uint8_t> nonce = Botan::hex_decode("000000000000000000000000000000000000000000000000");
1✔
392
         const std::vector<uint8_t> in = Botan::hex_decode("000000000000000000000000000000");
1✔
393

394
         result.test_eq("key len", Botan::Sodium::crypto_aead_xchacha20poly1305_ietf_keybytes(), key.size());
1✔
395
         result.test_eq("nonce len", Botan::Sodium::crypto_aead_xchacha20poly1305_ietf_npubbytes(), nonce.size());
1✔
396

397
         std::vector<uint8_t> ctext(in.size());
1✔
398
         std::vector<uint8_t> mac(16);
1✔
399
         unsigned long long maclen = 0;
1✔
400
         Botan::Sodium::crypto_aead_xchacha20poly1305_ietf_encrypt_detached(ctext.data(),
1✔
401
                                                                            mac.data(),
402
                                                                            &maclen,
403
                                                                            in.data(),
404
                                                                            in.size(),
405
                                                                            ad.data(),
406
                                                                            ad.size(),
407
                                                                            nullptr,
408
                                                                            nonce.data(),
409
                                                                            key.data());
410

411
         result.test_eq("maclen", size_t(maclen), 16);
1✔
412
         result.test_eq("mac", mac, "b2f7033812ac9ebd3745e2c99c7bbfeb");
1✔
413
         result.test_eq("ctext", ctext, "789e9689e5208d7fd9e1f3c5b5341f");
1✔
414

415
         std::vector<uint8_t> recovered(ctext.size());
1✔
416
         result.test_rc_ok("decrypt",
1✔
417
                           Botan::Sodium::crypto_aead_xchacha20poly1305_ietf_decrypt_detached(recovered.data(),
418
                                                                                              nullptr,
419
                                                                                              ctext.data(),
1✔
420
                                                                                              ctext.size(),
421
                                                                                              mac.data(),
1✔
422
                                                                                              ad.data(),
423
                                                                                              ad.size(),
424
                                                                                              nonce.data(),
425
                                                                                              key.data()));
426

427
         result.test_eq("plaintext", recovered, in);
1✔
428

429
         mac[0] ^= 1;
1✔
430
         result.test_rc_fail("decrypt",
2✔
431
                             "invalid ciphertext",
432
                             Botan::Sodium::crypto_aead_xchacha20poly1305_ietf_decrypt_detached(recovered.data(),
433
                                                                                                nullptr,
434
                                                                                                ctext.data(),
1✔
435
                                                                                                ctext.size(),
436
                                                                                                mac.data(),
1✔
437
                                                                                                ad.data(),
438
                                                                                                ad.size(),
439
                                                                                                nonce.data(),
440
                                                                                                key.data()));
441

442
         ctext.resize(in.size() + mac.size());
1✔
443
         unsigned long long ctext_len;
1✔
444
         result.test_rc_ok("encrypt",
1✔
445
                           Botan::Sodium::crypto_aead_xchacha20poly1305_ietf_encrypt(ctext.data(),
446
                                                                                     &ctext_len,
447
                                                                                     in.data(),
448
                                                                                     in.size(),
449
                                                                                     ad.data(),
450
                                                                                     ad.size(),
451
                                                                                     nullptr,
452
                                                                                     nonce.data(),
453
                                                                                     key.data()));
454

455
         result.test_eq("ctext_len", size_t(ctext_len), ctext.size());
1✔
456
         result.test_eq("ctext", ctext, "789e9689e5208d7fd9e1f3c5b5341fb2f7033812ac9ebd3745e2c99c7bbfeb");
1✔
457

458
         unsigned long long recovered_len = 0;
1✔
459
         result.test_rc_ok("decrypt",
1✔
460
                           Botan::Sodium::crypto_aead_xchacha20poly1305_ietf_decrypt(recovered.data(),
461
                                                                                     &recovered_len,
462
                                                                                     nullptr,
463
                                                                                     ctext.data(),
1✔
464
                                                                                     ctext.size(),
465
                                                                                     ad.data(),
466
                                                                                     ad.size(),
467
                                                                                     nonce.data(),
468
                                                                                     key.data()));
469

470
         result.test_eq("recovered", recovered, in);
1✔
471

472
         result.end_timer();
1✔
473
         return result;
1✔
474
      }
5✔
475

476
      static Test::Result auth_hmacsha512() {
1✔
477
         Test::Result result("crypto_auth_hmacsha512");
1✔
478
         result.start_timer();
1✔
479

480
         const std::vector<uint8_t> key =
1✔
481
            Botan::hex_decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
1✔
482
         const std::vector<uint8_t> in = Botan::hex_decode("616263");
1✔
483

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

486
         std::vector<uint8_t> mac(64);
1✔
487
         Botan::Sodium::crypto_auth_hmacsha512(mac.data(), in.data(), in.size(), key.data());
1✔
488

489
         result.test_eq(
1✔
490
            "expected mac",
491
            mac,
492
            "69D4A21E226BF0D348CB9A847C01CF24E93E8AC30D7C951704B936F82F795A624B470E23ABD33AC8700E797F0F2A499B932BAC7D283BBBB37D8FECF70D5E08A7");
493

494
         result.test_rc_ok("verify",
1✔
495
                           Botan::Sodium::crypto_auth_hmacsha512_verify(mac.data(), in.data(), in.size(), key.data()));
1✔
496

497
         mac[0] ^= 1;
1✔
498
         result.test_rc_fail(
2✔
499
            "verify",
500
            "invalid mac",
501
            Botan::Sodium::crypto_auth_hmacsha512_verify(mac.data(), in.data(), in.size(), key.data()));
1✔
502

503
         result.end_timer();
1✔
504
         return result;
1✔
505
      }
3✔
506

507
      static Test::Result auth_hmacsha512256() {
1✔
508
         Test::Result result("crypto_auth_hmacsha512256");
1✔
509
         result.start_timer();
1✔
510

511
         const std::vector<uint8_t> key =
1✔
512
            Botan::hex_decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
1✔
513
         const std::vector<uint8_t> in = Botan::hex_decode("616263");
1✔
514

515
         std::vector<uint8_t> mac(32);
1✔
516
         Botan::Sodium::crypto_auth_hmacsha512256(mac.data(), in.data(), in.size(), key.data());
1✔
517

518
         result.test_eq("expected mac", mac, "69D4A21E226BF0D348CB9A847C01CF24E93E8AC30D7C951704B936F82F795A62");
1✔
519

520
         result.test_rc_ok(
1✔
521
            "verify", Botan::Sodium::crypto_auth_hmacsha512256_verify(mac.data(), in.data(), in.size(), key.data()));
1✔
522

523
         mac[0] ^= 1;
1✔
524
         result.test_rc_fail(
2✔
525
            "verify",
526
            "invalid mac",
527
            Botan::Sodium::crypto_auth_hmacsha512256_verify(mac.data(), in.data(), in.size(), key.data()));
1✔
528

529
         result.end_timer();
1✔
530
         return result;
1✔
531
      }
3✔
532

533
      static Test::Result auth_hmacsha256() {
1✔
534
         Test::Result result("crypto_auth_hmacsha256");
1✔
535
         result.start_timer();
1✔
536

537
         const std::vector<uint8_t> key =
1✔
538
            Botan::hex_decode("0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20");
1✔
539
         const std::vector<uint8_t> in = Botan::hex_decode("616263");
1✔
540

541
         std::vector<uint8_t> mac(32);
1✔
542
         Botan::Sodium::crypto_auth_hmacsha256(mac.data(), in.data(), in.size(), key.data());
1✔
543

544
         result.test_eq("expected mac", mac, "A21B1F5D4CF4F73A4DD939750F7A066A7F98CC131CB16A6692759021CFAB8181");
1✔
545

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

549
         mac[0] ^= 1;
1✔
550
         result.test_rc_fail(
2✔
551
            "verify",
552
            "invalid mac",
553
            Botan::Sodium::crypto_auth_hmacsha256_verify(mac.data(), in.data(), in.size(), key.data()));
1✔
554

555
         result.end_timer();
1✔
556
         return result;
1✔
557
      }
3✔
558

559
      static Test::Result auth_poly1305() {
1✔
560
         Test::Result result("crypto_onetimeauth_poly1305");
1✔
561
         result.start_timer();
1✔
562

563
         const std::vector<uint8_t> key(Botan::Sodium::crypto_onetimeauth_keybytes(), 0x42);
1✔
564
         const std::vector<uint8_t> in(15);
1✔
565

566
         std::vector<uint8_t> mac(16);
1✔
567

568
         result.test_rc_ok("poly1305",
1✔
569
                           Botan::Sodium::crypto_onetimeauth_poly1305(mac.data(), in.data(), in.size(), key.data()));
570

571
         result.test_eq("expected mac", mac, "12154512151545121515451215154584");
1✔
572

573
         result.test_rc_ok(
1✔
574
            "poly1305 verify",
575
            Botan::Sodium::crypto_onetimeauth_poly1305_verify(mac.data(), in.data(), in.size(), key.data()));
1✔
576

577
         mac[0] ^= 1;
1✔
578
         result.test_rc_fail(
2✔
579
            "poly1305 verify",
580
            "invalid mac",
581
            Botan::Sodium::crypto_onetimeauth_poly1305_verify(mac.data(), in.data(), in.size(), key.data()));
1✔
582

583
         result.end_timer();
1✔
584
         return result;
1✔
585
      }
3✔
586

587
      static Test::Result shorthash_siphash24() {
1✔
588
         Test::Result result("crypto_shorthash_siphash24");
1✔
589
         result.start_timer();
1✔
590

591
         const std::vector<uint8_t> key = Botan::hex_decode("000102030405060708090A0B0C0D0E0F");
1✔
592
         const std::vector<uint8_t> in = Botan::hex_decode("000102030405060708090A0B0C0D0E");
1✔
593

594
         std::vector<uint8_t> mac(8);
1✔
595
         Botan::Sodium::crypto_shorthash_siphash24(mac.data(), in.data(), in.size(), key.data());
1✔
596

597
         result.test_eq("expected mac", mac, "E545BE4961CA29A1");
1✔
598

599
         result.end_timer();
1✔
600
         return result;
1✔
601
      }
3✔
602

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

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

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

616
         result.test_eq("ctext", ctext, "0000000000000000000000000000000042E45EB764A1B706D4776A849BC2526BC6");
1✔
617

618
         std::vector<uint8_t> recovered(33);
1✔
619
         result.test_rc_ok("decrypt",
1✔
620
                           Botan::Sodium::crypto_secretbox_xsalsa20poly1305_open(
621
                              recovered.data(), ctext.data(), ctext.size(), nonce.data(), key.data()));
1✔
622

623
         result.test_eq("decrypted", recovered, ptext);
1✔
624

625
         result.end_timer();
1✔
626
         return result;
1✔
627
      }
5✔
628

629
      static Test::Result secretbox_xsalsa20poly1305_detached() {
1✔
630
         Test::Result result("secretbox_xsalsa20poly1305");
1✔
631
         result.start_timer();
1✔
632

633
         const std::vector<uint8_t> ptext(33);
1✔
634
         const std::vector<uint8_t> nonce(Botan::Sodium::crypto_secretbox_xsalsa20poly1305_noncebytes());
1✔
635
         const std::vector<uint8_t> key(Botan::Sodium::crypto_secretbox_xsalsa20poly1305_keybytes());
1✔
636
         std::vector<uint8_t> ctext(33);
1✔
637
         std::vector<uint8_t> mac(16);
1✔
638

639
         result.test_rc_ok("encrypt detached",
1✔
640
                           Botan::Sodium::crypto_secretbox_detached(
641
                              ctext.data(), mac.data(), ptext.data(), ptext.size(), nonce.data(), key.data()));
642

643
         result.test_eq("ctext", ctext, "C63EBBFFFE85CE2CEBDEF7DC42F494576D05BDD7B929EBB045F2A793F740277D05");
1✔
644
         result.test_eq("mac", mac, "0D6681DCED740667C699F0AC71BFD1BD");
1✔
645

646
         std::vector<uint8_t> recovered(ctext.size());
1✔
647

648
         result.test_rc_ok("open detached",
1✔
649
                           Botan::Sodium::crypto_secretbox_open_detached(
650
                              recovered.data(), ctext.data(), mac.data(), ctext.size(), nonce.data(), key.data()));
1✔
651

652
         result.test_eq("recovered", recovered, ptext);
1✔
653

654
         result.end_timer();
1✔
655
         return result;
1✔
656
      }
6✔
657

658
      static Test::Result sign_ed25519() {
1✔
659
         Test::Result result("crypto_sign_ed25519");
1✔
660
         result.start_timer();
1✔
661

662
         const std::vector<uint8_t> seed(32);
1✔
663
         std::vector<uint8_t> pk(32), sk(64);
1✔
664

665
         result.test_rc_ok("seed_keypair",
1✔
666
                           Botan::Sodium::crypto_sign_ed25519_seed_keypair(pk.data(), sk.data(), seed.data()));
667

668
         result.test_eq("pk", pk, "3B6A27BCCEB6A42D62A3A8D02A6F0D73653215771DE243A63AC048A18B59DA29");
1✔
669
         result.test_eq(
1✔
670
            "sk",
671
            sk,
672
            "00000000000000000000000000000000000000000000000000000000000000003B6A27BCCEB6A42D62A3A8D02A6F0D73653215771DE243A63AC048A18B59DA29");
673

674
         const std::vector<uint8_t> msg = {1, 2, 3};
1✔
675
         std::vector<uint8_t> sig(64);
1✔
676
         unsigned long long sig_len = 0;
1✔
677
         result.test_rc_ok(
1✔
678
            "sign_detached",
679
            Botan::Sodium::crypto_sign_ed25519_detached(sig.data(), &sig_len, msg.data(), msg.size(), sk.data()));
1✔
680
         result.confirm("sig len", sig_len == 64);
2✔
681

682
         result.test_eq(
1✔
683
            "sig",
684
            sig,
685
            "2A26779BA6CBB5E54292257F725AF112B273C38728329682D99ED81BA6D7670350AE4CC53C5456FA437128D19298A5D949AB46E3D41AB3DBCFB0B35C895E9304");
686

687
         result.test_rc_ok(
1✔
688
            "verify",
689
            Botan::Sodium::crypto_sign_ed25519_verify_detached(sig.data(), msg.data(), msg.size(), pk.data()));
1✔
690

691
         sig[0] ^= 1;
1✔
692
         result.test_rc_fail(
2✔
693
            "verify",
694
            "reject invalid",
695
            Botan::Sodium::crypto_sign_ed25519_verify_detached(sig.data(), msg.data(), msg.size(), pk.data()));
1✔
696

697
         result.end_timer();
1✔
698
         return result;
1✔
699
      }
5✔
700

701
      static Test::Result stream_salsa20() {
1✔
702
         Test::Result result("crypto_stream_salsa20");
1✔
703
         result.start_timer();
1✔
704

705
         const std::vector<uint8_t> key =
1✔
706
            Botan::hex_decode("0F62B5085BAE0154A7FA4DA0F34699EC3F92E5388BDE3184D72A7DD02376C91C");
1✔
707
         const std::vector<uint8_t> nonce = Botan::hex_decode("288FF65DC42B92F9");
1✔
708
         const std::vector<uint8_t> expected =
1✔
709
            Botan::hex_decode("5E5E71F90199340304ABB22A37B6625BF883FB89CE3B21F54A10B81066EF87DA");
1✔
710

711
         std::vector<uint8_t> output(32);
1✔
712
         Botan::Sodium::crypto_stream_salsa20(output.data(), output.size(), nonce.data(), key.data());
1✔
713
         result.test_eq("stream", output, expected);
1✔
714

715
         std::vector<uint8_t> xor_output(32);
1✔
716
         Botan::Sodium::crypto_stream_salsa20_xor(
1✔
717
            xor_output.data(), output.data(), output.size(), nonce.data(), key.data());
1✔
718
         result.test_eq("stream", xor_output, std::vector<uint8_t>(32));  // all zeros
2✔
719

720
         result.end_timer();
1✔
721
         return result;
1✔
722
      }
5✔
723

724
      static Test::Result stream_xsalsa20() {
1✔
725
         Test::Result result("crypto_stream_xsalsa20");
1✔
726
         result.start_timer();
1✔
727

728
         const std::vector<uint8_t> key =
1✔
729
            Botan::hex_decode("1B27556473E985D462CD51197A9A46C76009549EAC6474F206C4EE0844F68389");
1✔
730
         const std::vector<uint8_t> nonce = Botan::hex_decode("69696EE955B62B73CD62BDA875FC73D68219E0036B7A0B37");
1✔
731
         const std::vector<uint8_t> expected =
1✔
732
            Botan::hex_decode("EEA6A7251C1E72916D11C2CB214D3C252539121D8E234E652D651FA4C8CFF880");
1✔
733

734
         std::vector<uint8_t> output(32);
1✔
735
         Botan::Sodium::crypto_stream_xsalsa20(output.data(), output.size(), nonce.data(), key.data());
1✔
736
         result.test_eq("stream", output, expected);
1✔
737

738
         std::vector<uint8_t> xor_output(32);
1✔
739
         Botan::Sodium::crypto_stream_xsalsa20_xor(
1✔
740
            xor_output.data(), output.data(), output.size(), nonce.data(), key.data());
1✔
741
         result.test_eq("stream", xor_output, std::vector<uint8_t>(32));  // all zeros
2✔
742

743
         result.end_timer();
1✔
744
         return result;
1✔
745
      }
5✔
746

747
      static Test::Result stream_chacha20() {
1✔
748
         Test::Result result("crypto_stream_chacha20");
1✔
749
         result.start_timer();
1✔
750

751
         const std::vector<uint8_t> key =
1✔
752
            Botan::hex_decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
1✔
753
         const std::vector<uint8_t> nonce = Botan::hex_decode("0001020304050607");
1✔
754
         const std::vector<uint8_t> expected =
1✔
755
            Botan::hex_decode("F798A189F195E66982105FFB640BB7757F579DA31602FC93EC01AC56F85AC3C1");
1✔
756

757
         std::vector<uint8_t> output(32);
1✔
758
         Botan::Sodium::crypto_stream_chacha20(output.data(), output.size(), nonce.data(), key.data());
1✔
759
         result.test_eq("stream", output, expected);
1✔
760

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

766
         result.end_timer();
1✔
767
         return result;
1✔
768
      }
5✔
769

770
      static Test::Result stream_chacha20_ietf() {
1✔
771
         Test::Result result("crypto_stream_chacha20");
1✔
772
         result.start_timer();
1✔
773

774
         const std::vector<uint8_t> key =
1✔
775
            Botan::hex_decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
1✔
776
         const std::vector<uint8_t> nonce = Botan::hex_decode("000102030405060708090A0B0C");
1✔
777
         const std::vector<uint8_t> expected =
1✔
778
            Botan::hex_decode("103AF111C18B549D39248FB07D60C29A95D1DB88D892F7B4AF709A5FD47A9E4B");
1✔
779

780
         std::vector<uint8_t> output(32);
1✔
781
         Botan::Sodium::crypto_stream_chacha20_ietf(output.data(), output.size(), nonce.data(), key.data());
1✔
782
         result.test_eq("stream", output, expected);
1✔
783

784
         std::vector<uint8_t> xor_output(32);
1✔
785
         Botan::Sodium::crypto_stream_chacha20_ietf_xor(
1✔
786
            xor_output.data(), output.data(), output.size(), nonce.data(), key.data());
1✔
787
         result.test_eq("stream", xor_output, std::vector<uint8_t>(32));  // all zeros
2✔
788

789
         result.end_timer();
1✔
790
         return result;
1✔
791
      }
5✔
792

793
      static Test::Result stream_xchacha20() {
1✔
794
         Test::Result result("crypto_stream_xchacha20");
1✔
795
         result.start_timer();
1✔
796

797
         const std::vector<uint8_t> key =
1✔
798
            Botan::hex_decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
1✔
799
         const std::vector<uint8_t> nonce = Botan::hex_decode("000102030405060708090a0b0c0d0e0f1011121314151617");
1✔
800
         const std::vector<uint8_t> expected =
1✔
801
            Botan::hex_decode("e53a61cef151e81401067de33adfc02e90ab205361b49b539fda7f0e63b1bc7d");
1✔
802

803
         std::vector<uint8_t> output(32);
1✔
804
         Botan::Sodium::crypto_stream_xchacha20(output.data(), output.size(), nonce.data(), key.data());
1✔
805
         result.test_eq("stream", output, expected);
1✔
806

807
         std::vector<uint8_t> xor_output(32);
1✔
808
         Botan::Sodium::crypto_stream_xchacha20_xor(
1✔
809
            xor_output.data(), output.data(), output.size(), nonce.data(), key.data());
1✔
810
         result.test_eq("stream", xor_output, std::vector<uint8_t>(32));  // all zeros
2✔
811

812
         result.end_timer();
1✔
813
         return result;
1✔
814
      }
5✔
815
};
816

817
BOTAN_REGISTER_TEST("compat", "sodium", Sodium_API_Tests);
818

819
#endif
820

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