7#include <botan/sodium.h>
10#include <botan/exceptn.h>
11#include <botan/mem_ops.h>
17int sodium_aead_chacha20poly1305_encrypt(uint8_t ctext[],
18 unsigned long long* ctext_len,
19 const uint8_t ptext[],
23 const uint8_t nonce[],
25 const uint8_t key[]) {
28 chacha20poly1305->set_key(key, 32);
29 chacha20poly1305->set_associated_data(ad, ad_len);
30 chacha20poly1305->start(nonce, nonce_len);
34 buf.reserve(ptext_len + 16);
35 buf.assign(ptext, ptext + ptext_len);
37 chacha20poly1305->finish(buf);
39 copy_mem(ctext, buf.data(), buf.size());
40 if(ctext_len !=
nullptr) {
41 *ctext_len = buf.size();
46int sodium_aead_chacha20poly1305_decrypt(uint8_t ptext[],
47 unsigned long long* ptext_len,
48 const uint8_t ctext[],
52 const uint8_t nonce[],
54 const uint8_t key[]) {
55 if(ptext_len !=
nullptr) {
65 chacha20poly1305->set_key(key, 32);
66 chacha20poly1305->set_associated_data(ad, ad_len);
67 chacha20poly1305->start(nonce, nonce_len);
71 buf.assign(ctext, ctext + ctext_len);
74 chacha20poly1305->finish(buf);
76 if(ptext !=
nullptr) {
82 if(ptext_len !=
nullptr) {
83 *ptext_len = ctext_len - 16;
86 if(ptext !=
nullptr) {
87 copy_mem(ptext, buf.data(), buf.size());
92int sodium_aead_chacha20poly1305_encrypt_detached(uint8_t ctext[],
94 const uint8_t ptext[],
98 const uint8_t nonce[],
100 const uint8_t key[]) {
103 chacha20poly1305->set_key(key, 32);
104 chacha20poly1305->set_associated_data(ad, ad_len);
105 chacha20poly1305->start(nonce, nonce_len);
109 buf.reserve(ptext_len + 16);
110 buf.assign(ptext, ptext + ptext_len);
112 chacha20poly1305->finish(buf);
114 copy_mem(ctext, buf.data(), ptext_len);
115 copy_mem(mac, buf.data() + ptext_len, 16);
119int sodium_aead_chacha20poly1305_decrypt_detached(uint8_t ptext[],
120 const uint8_t ctext[],
125 const uint8_t nonce[],
127 const uint8_t key[]) {
130 chacha20poly1305->set_key(key, 32);
131 chacha20poly1305->set_associated_data(ad, ad_len);
132 chacha20poly1305->start(nonce, nonce_len);
136 buf.reserve(ctext_len + 16);
137 buf.assign(ctext, ctext + ctext_len);
138 buf.insert(buf.end(), mac, mac + 16);
141 chacha20poly1305->finish(buf);
143 if(ptext !=
nullptr) {
149 if(ptext !=
nullptr) {
150 copy_mem(ptext, buf.data(), buf.size());
158 unsigned long long* ctext_len,
159 const uint8_t ptext[],
163 const uint8_t unused_secret_nonce[],
164 const uint8_t nonce[],
165 const uint8_t key[]) {
168 return sodium_aead_chacha20poly1305_encrypt(
173 unsigned long long* ptext_len,
174 uint8_t unused_secret_nonce[],
175 const uint8_t ctext[],
179 const uint8_t nonce[],
180 const uint8_t key[]) {
183 return sodium_aead_chacha20poly1305_decrypt(
189 unsigned long long* mac_len,
190 const uint8_t ptext[],
194 const uint8_t unused_secret_nonce[],
195 const uint8_t nonce[],
196 const uint8_t key[]) {
199 if(mac_len !=
nullptr) {
203 return sodium_aead_chacha20poly1305_encrypt_detached(
208 uint8_t unused_secret_nonce[],
209 const uint8_t ctext[],
214 const uint8_t nonce[],
215 const uint8_t key[]) {
218 return sodium_aead_chacha20poly1305_decrypt_detached(
223 unsigned long long* ctext_len,
224 const uint8_t ptext[],
228 const uint8_t unused_secret_nonce[],
229 const uint8_t nonce[],
230 const uint8_t key[]) {
232 return sodium_aead_chacha20poly1305_encrypt(
237 unsigned long long* ptext_len,
238 uint8_t unused_secret_nonce[],
239 const uint8_t ctext[],
243 const uint8_t nonce[],
244 const uint8_t key[]) {
246 return sodium_aead_chacha20poly1305_decrypt(
252 unsigned long long* mac_len,
253 const uint8_t ptext[],
257 const uint8_t unused_secret_nonce[],
258 const uint8_t nonce[],
259 const uint8_t key[]) {
261 if(mac_len !=
nullptr) {
265 return sodium_aead_chacha20poly1305_encrypt_detached(
270 uint8_t unused_secret_nonce[],
271 const uint8_t ctext[],
276 const uint8_t nonce[],
277 const uint8_t key[]) {
280 return sodium_aead_chacha20poly1305_decrypt_detached(
285 unsigned long long* ctext_len,
286 const uint8_t ptext[],
290 const uint8_t unused_secret_nonce[],
291 const uint8_t nonce[],
292 const uint8_t key[]) {
295 return sodium_aead_chacha20poly1305_encrypt(
300 unsigned long long* ptext_len,
301 uint8_t unused_secret_nonce[],
302 const uint8_t ctext[],
306 const uint8_t nonce[],
307 const uint8_t key[]) {
310 return sodium_aead_chacha20poly1305_decrypt(
316 unsigned long long* mac_len,
317 const uint8_t ptext[],
321 const uint8_t unused_secret_nonce[],
322 const uint8_t nonce[],
323 const uint8_t key[]) {
325 if(mac_len !=
nullptr) {
329 return sodium_aead_chacha20poly1305_encrypt_detached(
334 uint8_t unused_secret_nonce[],
335 const uint8_t ctext[],
340 const uint8_t nonce[],
341 const uint8_t key[]) {
343 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[])
constexpr void copy_mem(T *out, const T *in, size_t n)
std::vector< T, secure_allocator< T > > secure_vector
constexpr void clear_mem(T *ptr, size_t n)