• 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

62.86
/src/lib/compat/sodium/sodium_box.cpp
1
/*
2
* (C) 2019 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include <botan/sodium.h>
8

9
#include <botan/secmem.h>
10

11
namespace Botan {
12

13
int Sodium::crypto_box_curve25519xsalsa20poly1305_seed_keypair(uint8_t pk[32], uint8_t sk[32], const uint8_t seed[32]) {
2✔
14
   secure_vector<uint8_t> digest(64);
2✔
15
   crypto_hash_sha512(digest.data(), seed, 32);
2✔
16
   copy_mem(sk, digest.data(), 32);
2✔
17
   return crypto_scalarmult_curve25519_base(pk, sk);
2✔
18
}
2✔
19

20
int Sodium::crypto_box_curve25519xsalsa20poly1305_keypair(uint8_t pk[32], uint8_t sk[32]) {
×
21
   randombytes_buf(sk, 32);
×
22
   return crypto_scalarmult_curve25519_base(pk, sk);
×
23
}
24

25
int Sodium::crypto_box_curve25519xsalsa20poly1305_beforenm(uint8_t key[], const uint8_t pk[32], const uint8_t sk[32]) {
4✔
26
   const uint8_t zero[16] = {0};
4✔
27
   secure_vector<uint8_t> shared(32);
4✔
28

29
   if(crypto_scalarmult_curve25519(shared.data(), sk, pk) != 0)
4✔
30
      return -1;
31

32
   return crypto_core_hsalsa20(key, zero, shared.data(), nullptr);
4✔
33
}
4✔
34

35
int Sodium::crypto_box_curve25519xsalsa20poly1305(uint8_t ctext[],
×
36
                                                  const uint8_t ptext[],
37
                                                  size_t ptext_len,
38
                                                  const uint8_t nonce[],
39
                                                  const uint8_t pk[32],
40
                                                  const uint8_t sk[32]) {
41
   secure_vector<uint8_t> shared(32);
×
42

43
   if(crypto_box_curve25519xsalsa20poly1305_beforenm(shared.data(), pk, sk) != 0)
×
44
      return -1;
45

46
   return crypto_box_curve25519xsalsa20poly1305_afternm(ctext, ptext, ptext_len, nonce, shared.data());
×
47
}
×
48

49
int Sodium::crypto_box_curve25519xsalsa20poly1305_open(uint8_t ptext[],
×
50
                                                       const uint8_t ctext[],
51
                                                       size_t ctext_len,
52
                                                       const uint8_t nonce[],
53
                                                       const uint8_t pk[32],
54
                                                       const uint8_t sk[32]) {
55
   secure_vector<uint8_t> shared(32);
×
56

57
   if(crypto_box_curve25519xsalsa20poly1305_beforenm(shared.data(), pk, sk) != 0)
×
58
      return -1;
59

60
   return crypto_box_curve25519xsalsa20poly1305_open_afternm(ptext, ctext, ctext_len, nonce, shared.data());
×
61
}
×
62

63
int Sodium::crypto_box_detached(uint8_t ctext[],
2✔
64
                                uint8_t mac[],
65
                                const uint8_t ptext[],
66
                                size_t ptext_len,
67
                                const uint8_t nonce[],
68
                                const uint8_t pk[32],
69
                                const uint8_t sk[32]) {
70
   secure_vector<uint8_t> shared(32);
2✔
71

72
   if(crypto_box_beforenm(shared.data(), pk, sk) != 0)
2✔
73
      return -1;
74

75
   return crypto_box_detached_afternm(ctext, mac, ptext, ptext_len, nonce, shared.data());
2✔
76
}
2✔
77

78
int Sodium::crypto_box_open_detached(uint8_t ptext[],
2✔
79
                                     const uint8_t ctext[],
80
                                     const uint8_t mac[],
81
                                     size_t ctext_len,
82
                                     const uint8_t nonce[],
83
                                     const uint8_t pk[32],
84
                                     const uint8_t sk[32]) {
85
   secure_vector<uint8_t> shared(32);
2✔
86

87
   if(crypto_box_beforenm(shared.data(), pk, sk) != 0)
2✔
88
      return -1;
89

90
   return crypto_box_open_detached_afternm(ptext, ctext, mac, ctext_len, nonce, shared.data());
2✔
91
}
2✔
92

93
}  // namespace Botan
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