7#include <botan/sodium.h>
10#include <botan/mem_ops.h>
16int sodium_aead_chacha20poly1305_encrypt(uint8_t ctext[],
17 unsigned long long* ctext_len,
18 const uint8_t ptext[],
22 const uint8_t nonce[],
24 const uint8_t key[]) {
27 chacha20poly1305->set_key(key, 32);
28 chacha20poly1305->set_associated_data(ad, ad_len);
29 chacha20poly1305->start(nonce, nonce_len);
33 buf.reserve(ptext_len + 16);
34 buf.assign(ptext, ptext + ptext_len);
36 chacha20poly1305->finish(buf);
38 copy_mem(ctext, buf.data(), buf.size());
40 *ctext_len = buf.size();
45int sodium_aead_chacha20poly1305_decrypt(uint8_t ptext[],
46 unsigned long long* ptext_len,
47 const uint8_t ctext[],
51 const uint8_t nonce[],
53 const uint8_t key[]) {
62 chacha20poly1305->set_key(key, 32);
63 chacha20poly1305->set_associated_data(ad, ad_len);
64 chacha20poly1305->start(nonce, nonce_len);
68 buf.assign(ctext, ctext + ctext_len);
71 chacha20poly1305->finish(buf);
72 }
catch(Invalid_Authentication_Tag&) {
76 *ptext_len = ctext_len - 16;
78 copy_mem(ptext, buf.data(), buf.size());
82int sodium_aead_chacha20poly1305_encrypt_detached(uint8_t ctext[],
84 const uint8_t ptext[],
88 const uint8_t nonce[],
90 const uint8_t key[]) {
93 chacha20poly1305->set_key(key, 32);
94 chacha20poly1305->set_associated_data(ad, ad_len);
95 chacha20poly1305->start(nonce, nonce_len);
99 buf.reserve(ptext_len + 16);
100 buf.assign(ptext, ptext + ptext_len);
102 chacha20poly1305->finish(buf);
104 copy_mem(ctext, buf.data(), ptext_len);
105 copy_mem(mac, buf.data() + ptext_len, 16);
109int sodium_aead_chacha20poly1305_decrypt_detached(uint8_t ptext[],
110 const uint8_t ctext[],
115 const uint8_t nonce[],
117 const uint8_t key[]) {
120 chacha20poly1305->set_key(key, 32);
121 chacha20poly1305->set_associated_data(ad, ad_len);
122 chacha20poly1305->start(nonce, nonce_len);
126 buf.reserve(ctext_len + 16);
127 buf.assign(ctext, ctext + ctext_len);
128 buf.insert(buf.end(), mac, mac + 16);
131 chacha20poly1305->finish(buf);
132 }
catch(Invalid_Authentication_Tag&) {
136 copy_mem(ptext, buf.data(), buf.size());
143 unsigned long long* ctext_len,
144 const uint8_t ptext[],
148 const uint8_t unused_secret_nonce[],
149 const uint8_t nonce[],
150 const uint8_t key[]) {
153 return sodium_aead_chacha20poly1305_encrypt(
158 unsigned long long* ptext_len,
159 uint8_t unused_secret_nonce[],
160 const uint8_t ctext[],
164 const uint8_t nonce[],
165 const uint8_t key[]) {
168 return sodium_aead_chacha20poly1305_decrypt(
174 unsigned long long* mac_len,
175 const uint8_t ptext[],
179 const uint8_t unused_secret_nonce[],
180 const uint8_t nonce[],
181 const uint8_t key[]) {
188 return sodium_aead_chacha20poly1305_encrypt_detached(
193 uint8_t unused_secret_nonce[],
194 const uint8_t ctext[],
199 const uint8_t nonce[],
200 const uint8_t key[]) {
203 return sodium_aead_chacha20poly1305_decrypt_detached(
208 unsigned long long* ctext_len,
209 const uint8_t ptext[],
213 const uint8_t unused_secret_nonce[],
214 const uint8_t nonce[],
215 const uint8_t key[]) {
217 return sodium_aead_chacha20poly1305_encrypt(
222 unsigned long long* ptext_len,
223 uint8_t unused_secret_nonce[],
224 const uint8_t ctext[],
228 const uint8_t nonce[],
229 const uint8_t key[]) {
231 return sodium_aead_chacha20poly1305_decrypt(
237 unsigned long long* mac_len,
238 const uint8_t ptext[],
242 const uint8_t unused_secret_nonce[],
243 const uint8_t nonce[],
244 const uint8_t key[]) {
250 return sodium_aead_chacha20poly1305_encrypt_detached(
255 uint8_t unused_secret_nonce[],
256 const uint8_t ctext[],
261 const uint8_t nonce[],
262 const uint8_t key[]) {
265 return sodium_aead_chacha20poly1305_decrypt_detached(
270 unsigned long long* ctext_len,
271 const uint8_t ptext[],
275 const uint8_t unused_secret_nonce[],
276 const uint8_t nonce[],
277 const uint8_t key[]) {
280 return sodium_aead_chacha20poly1305_encrypt(
285 unsigned long long* ptext_len,
286 uint8_t unused_secret_nonce[],
287 const uint8_t ctext[],
291 const uint8_t nonce[],
292 const uint8_t key[]) {
295 return sodium_aead_chacha20poly1305_decrypt(
301 unsigned long long* mac_len,
302 const uint8_t ptext[],
306 const uint8_t unused_secret_nonce[],
307 const uint8_t nonce[],
308 const uint8_t key[]) {
314 return sodium_aead_chacha20poly1305_encrypt_detached(
319 uint8_t unused_secret_nonce[],
320 const uint8_t ctext[],
325 const uint8_t nonce[],
326 const uint8_t key[]) {
328 return sodium_aead_chacha20poly1305_decrypt_detached(
static std::unique_ptr< AEAD_Mode > create_or_throw(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
int crypto_aead_xchacha20poly1305_ietf_decrypt(uint8_t ptext[], unsigned long long *ptext_len, uint8_t unused_secret_nonce[], const uint8_t ctext[], size_t ctext_len, const uint8_t ad[], size_t ad_len, const uint8_t nonce[], const uint8_t key[])
int crypto_aead_chacha20poly1305_decrypt(uint8_t m[], unsigned long long *ptext_len, uint8_t unused_secret_nonce[], const uint8_t ctext[], size_t ctext_len, const uint8_t ad[], size_t ad_len, const uint8_t nonce[], const uint8_t key[])
int crypto_aead_chacha20poly1305_decrypt_detached(uint8_t m[], uint8_t unused_secret_nonce[], const uint8_t ctext[], size_t ctext_len, const uint8_t mac[], const uint8_t ad[], size_t ad_len, const uint8_t nonce[], const uint8_t key[])
int crypto_aead_chacha20poly1305_ietf_encrypt_detached(uint8_t ctext[], uint8_t mac[], unsigned long long *mac_len, const uint8_t ptext[], size_t ptext_len, const uint8_t ad[], size_t ad_len, const uint8_t unused_secret_nonce[], const uint8_t nonce[], const uint8_t key[])
size_t crypto_aead_chacha20poly1305_ietf_npubbytes()
int crypto_aead_chacha20poly1305_encrypt(uint8_t ctext[], unsigned long long *ctext_len, const uint8_t ptext[], size_t ptext_len, const uint8_t ad[], size_t ad_len, const uint8_t unused_secret_nonce[], const uint8_t nonce[], const uint8_t key[])
size_t crypto_aead_xchacha20poly1305_ietf_npubbytes()
int crypto_aead_chacha20poly1305_ietf_encrypt(uint8_t ctext[], unsigned long long *ctext_len, const uint8_t ptext[], size_t ptext_len, const uint8_t ad[], size_t ad_len, const uint8_t unused_secret_nonce[], const uint8_t nonce[], const uint8_t key[])
int crypto_aead_xchacha20poly1305_ietf_encrypt(uint8_t ctext[], unsigned long long *ctext_len, const uint8_t ptext[], size_t ptext_len, const uint8_t ad[], size_t ad_len, const uint8_t unused_secret_nonce[], const uint8_t nonce[], const uint8_t key[])
int crypto_aead_chacha20poly1305_ietf_decrypt(uint8_t ptext[], unsigned long long *ptext_len, uint8_t unused_secret_nonce[], const uint8_t ctext[], size_t ctext_len, const uint8_t ad[], size_t ad_len, const uint8_t nonce[], const uint8_t key[])
int crypto_aead_chacha20poly1305_encrypt_detached(uint8_t ctext[], uint8_t mac[], unsigned long long *mac_len, const uint8_t ptext[], size_t ptext_len, const uint8_t ad[], size_t ad_len, const uint8_t unused_secret_nonce[], const uint8_t nonce[], const uint8_t key[])
int crypto_aead_xchacha20poly1305_ietf_encrypt_detached(uint8_t ctext[], uint8_t mac[], unsigned long long *mac_len, const uint8_t ptext[], size_t ptext_len, const uint8_t ad[], size_t ad_len, const uint8_t unused_secret_nonce[], const uint8_t nonce[], const uint8_t key[])
size_t crypto_aead_chacha20poly1305_npubbytes()
int crypto_aead_chacha20poly1305_ietf_decrypt_detached(uint8_t m[], uint8_t unused_secret_nonce[], const uint8_t ctext[], size_t ctext_len, const uint8_t mac[], const uint8_t ad[], size_t ad_len, const uint8_t nonce[], const uint8_t key[])
int crypto_aead_xchacha20poly1305_ietf_decrypt_detached(uint8_t ptext[], uint8_t unused_secret_nonce[], const uint8_t ctext[], size_t ctext_len, const uint8_t mac[], const uint8_t ad[], size_t ad_len, const uint8_t nonce[], const uint8_t key[])
std::vector< T, secure_allocator< T > > secure_vector
constexpr void copy_mem(T *out, const T *in, size_t n)