Botan 2.19.5 Crypto and TLS for C&

sodium_box.cpp
Go to the documentation of this file.
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#include <botan/secmem.h>
9
10 namespace Botan {
11
12 int Sodium::crypto_box_curve25519xsalsa20poly1305_seed_keypair(uint8_t pk[32],
13 uint8_t sk[32],
14 const uint8_t seed[32])
15 {
16 secure_vector<uint8_t> digest(64);
17 crypto_hash_sha512(digest.data(), seed, 32);
18 copy_mem(sk, digest.data(), 32);
19 return crypto_scalarmult_curve25519_base(pk, sk);
20 }
21
22 int Sodium::crypto_box_curve25519xsalsa20poly1305_keypair(uint8_t pk[32],
23 uint8_t sk[32])
24 {
25 randombytes_buf(sk, 32);
26 return crypto_scalarmult_curve25519_base(pk, sk);
27 }
28
29 int Sodium::crypto_box_curve25519xsalsa20poly1305_beforenm(uint8_t key[],
30 const uint8_t pk[32],
31 const uint8_t sk[32])
32 {
33 const uint8_t zero[16] = { 0 };
34 secure_vector<uint8_t> shared(32);
35
36 if(crypto_scalarmult_curve25519(shared.data(), sk, pk) != 0)
37 return -1;
38
39 return crypto_core_hsalsa20(key, zero, shared.data(), nullptr);
40 }
41
42 int Sodium::crypto_box_curve25519xsalsa20poly1305(uint8_t ctext[],
43 const uint8_t ptext[],
44 size_t ptext_len,
45 const uint8_t nonce[],
46 const uint8_t pk[32],
47 const uint8_t sk[32])
48 {
49 secure_vector<uint8_t> shared(32);
50
51 if(crypto_box_curve25519xsalsa20poly1305_beforenm(shared.data(), pk, sk) != 0)
52 return -1;
53
54 return crypto_box_curve25519xsalsa20poly1305_afternm(ctext, ptext, ptext_len, nonce, shared.data());
55 }
56
57 int Sodium::crypto_box_curve25519xsalsa20poly1305_open(uint8_t ptext[],
58 const uint8_t ctext[],
59 size_t ctext_len,
60 const uint8_t nonce[],
61 const uint8_t pk[32],
62 const uint8_t sk[32])
63 {
64 secure_vector<uint8_t> shared(32);
65
66 if(crypto_box_curve25519xsalsa20poly1305_beforenm(shared.data(), pk, sk) != 0)
67 return -1;
68
69 return crypto_box_curve25519xsalsa20poly1305_open_afternm(ptext, ctext, ctext_len, nonce, shared.data());
70 }
71
72 int Sodium::crypto_box_detached(uint8_t ctext[], uint8_t mac[],
73 const uint8_t ptext[], size_t ptext_len,
74 const uint8_t nonce[], const uint8_t pk[32],
75 const uint8_t sk[32])
76 {
77 secure_vector<uint8_t> shared(32);
78
79 if(crypto_box_beforenm(shared.data(), pk, sk) != 0)
80 return -1;
81
82 return crypto_box_detached_afternm(ctext, mac, ptext, ptext_len, nonce, shared.data());
83 }
84
85 int Sodium::crypto_box_open_detached(uint8_t ptext[], const uint8_t ctext[],
86 const uint8_t mac[],
87 size_t ctext_len,
88 const uint8_t nonce[],
89 const uint8_t pk[32],
90 const uint8_t sk[32])
91 {
92 secure_vector<uint8_t> shared(32);
93
94 if(crypto_box_beforenm(shared.data(), pk, sk) != 0)
95 return -1;
96
97 return crypto_box_open_detached_afternm(ptext, ctext, mac, ctext_len, nonce, shared.data());
98 }
99
100}
Botan::Sodium::crypto_box_curve25519xsalsa20poly1305_seed_keypair
int crypto_box_curve25519xsalsa20poly1305_seed_keypair(uint8_t pk[32], uint8_t sk[32], const uint8_t seed[32])
Definition: sodium_box.cpp:12
Botan::Sodium::crypto_box_curve25519xsalsa20poly1305_beforenm
int crypto_box_curve25519xsalsa20poly1305_beforenm(uint8_t key[], const uint8_t pk[32], const uint8_t sk[32])
Definition: sodium_box.cpp:29
Botan::Sodium::crypto_box_curve25519xsalsa20poly1305_open_afternm
int crypto_box_curve25519xsalsa20poly1305_open_afternm(uint8_t ptext[], const uint8_t ctext[], size_t ctext_len, const uint8_t nonce[], const uint8_t key[])
Definition: sodium.h:685
Botan::Sodium::crypto_box_curve25519xsalsa20poly1305_keypair
int crypto_box_curve25519xsalsa20poly1305_keypair(uint8_t pk[32], uint8_t sk[32])
Definition: sodium_box.cpp:22
Botan::Sodium::crypto_box_open_detached
int crypto_box_open_detached(uint8_t ptext[], const uint8_t ctext[], const uint8_t mac[], size_t ctext_len, const uint8_t nonce[], const uint8_t pk[32], const uint8_t sk[32])
Definition: sodium_box.cpp:85
Botan::Sodium::crypto_box_curve25519xsalsa20poly1305_open
int crypto_box_curve25519xsalsa20poly1305_open(uint8_t ptext[], const uint8_t ctext[], size_t ctext_len, const uint8_t nonce[], const uint8_t pk[32], const uint8_t sk[32])
Definition: sodium_box.cpp:57
Botan::Sodium::crypto_box_open_detached_afternm
int crypto_box_open_detached_afternm(uint8_t ptext[], const uint8_t ctext[], const uint8_t mac[], size_t ctext_len, const uint8_t nonce[], const uint8_t key[])
Definition: sodium.h:799
Botan::Sodium::crypto_box_detached
int crypto_box_detached(uint8_t ctext[], uint8_t mac[], const uint8_t ptext[], size_t ptext_len, const uint8_t nonce[], const uint8_t pk[32], const uint8_t sk[32])
Definition: sodium_box.cpp:72
Botan::Sodium::crypto_hash_sha512
int crypto_hash_sha512(uint8_t out[64], const uint8_t in[], size_t in_len)
Definition: sodium_auth.cpp:13
Botan::Sodium::crypto_scalarmult_curve25519
int crypto_scalarmult_curve25519(uint8_t out[32], const uint8_t scalar[32], const uint8_t basepoint[32])
Definition: sodium_25519.cpp:13
Botan::Sodium::crypto_box_curve25519xsalsa20poly1305
int crypto_box_curve25519xsalsa20poly1305(uint8_t ctext[], const uint8_t ptext[], size_t ptext_len, const uint8_t nonce[], const uint8_t pk[32], const uint8_t sk[32])
Definition: sodium_box.cpp:42
Botan::Sodium::crypto_box_beforenm
int crypto_box_beforenm(uint8_t key[], const uint8_t pk[32], const uint8_t sk[32])
Definition: sodium.h:779
Botan::Sodium::crypto_scalarmult_curve25519_base
int crypto_scalarmult_curve25519_base(uint8_t out[32], const uint8_t scalar[32])
Definition: sodium_25519.cpp:19
Botan::Sodium::crypto_core_hsalsa20
int crypto_core_hsalsa20(uint8_t out[], const uint8_t in[], const uint8_t key[], const uint8_t c[])
Definition: sodium_salsa.cpp:13
Botan::Sodium::crypto_box_curve25519xsalsa20poly1305_afternm
int crypto_box_curve25519xsalsa20poly1305_afternm(uint8_t ctext[], const uint8_t ptext[], size_t ptext_len, const uint8_t nonce[], const uint8_t key[])
Definition: sodium.h:676
Botan::Sodium::crypto_box_detached_afternm
int crypto_box_detached_afternm(uint8_t ctext[], uint8_t mac[], const uint8_t ptext[], size_t ptext_len, const uint8_t nonce[], const uint8_t key[])
Definition: sodium.h:821
Botan::Sodium::randombytes_buf
void randombytes_buf(void *buf, size_t size)
Definition: sodium_utils.cpp:18
Botan
Definition: alg_id.cpp:13
Botan::copy_mem
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:133
Botan::secure_vector
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65