Botan 3.14.1
Crypto and TLS for C&
sodium_aead.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
9#include <botan/aead.h>
10#include <botan/exceptn.h>
11#include <botan/mem_ops.h>
12
13namespace Botan {
14
15namespace {
16
17int sodium_aead_chacha20poly1305_encrypt(uint8_t ctext[],
18 unsigned long long* ctext_len,
19 const uint8_t ptext[],
20 size_t ptext_len,
21 const uint8_t ad[],
22 size_t ad_len,
23 const uint8_t nonce[],
24 size_t nonce_len,
25 const uint8_t key[]) {
26 auto chacha20poly1305 = AEAD_Mode::create_or_throw("ChaCha20Poly1305", Cipher_Dir::Encryption);
27
28 chacha20poly1305->set_key(key, 32);
29 chacha20poly1305->set_associated_data(ad, ad_len);
30 chacha20poly1305->start(nonce, nonce_len);
31
32 // FIXME do this in-place
34 buf.reserve(ptext_len + 16);
35 buf.assign(ptext, ptext + ptext_len);
36
37 chacha20poly1305->finish(buf);
38
39 copy_mem(ctext, buf.data(), buf.size());
40 if(ctext_len != nullptr) {
41 *ctext_len = buf.size();
42 }
43 return 0;
44}
45
46int sodium_aead_chacha20poly1305_decrypt(uint8_t ptext[],
47 unsigned long long* ptext_len,
48 const uint8_t ctext[],
49 size_t ctext_len,
50 const uint8_t ad[],
51 size_t ad_len,
52 const uint8_t nonce[],
53 size_t nonce_len,
54 const uint8_t key[]) {
55 if(ptext_len != nullptr) {
56 *ptext_len = 0;
57 }
58
59 if(ctext_len < 16) {
60 return -1;
61 }
62
63 auto chacha20poly1305 = AEAD_Mode::create_or_throw("ChaCha20Poly1305", Cipher_Dir::Decryption);
64
65 chacha20poly1305->set_key(key, 32);
66 chacha20poly1305->set_associated_data(ad, ad_len);
67 chacha20poly1305->start(nonce, nonce_len);
68
69 // FIXME do this in-place
71 buf.assign(ctext, ctext + ctext_len);
72
73 try {
74 chacha20poly1305->finish(buf);
76 if(ptext != nullptr) {
77 clear_mem(ptext, ctext_len - 16);
78 }
79 return -1;
80 }
81
82 if(ptext_len != nullptr) {
83 *ptext_len = ctext_len - 16;
84 }
85
86 if(ptext != nullptr) {
87 copy_mem(ptext, buf.data(), buf.size());
88 }
89 return 0;
90}
91
92int sodium_aead_chacha20poly1305_encrypt_detached(uint8_t ctext[],
93 uint8_t mac[],
94 const uint8_t ptext[],
95 size_t ptext_len,
96 const uint8_t ad[],
97 size_t ad_len,
98 const uint8_t nonce[],
99 size_t nonce_len,
100 const uint8_t key[]) {
101 auto chacha20poly1305 = AEAD_Mode::create_or_throw("ChaCha20Poly1305", Cipher_Dir::Encryption);
102
103 chacha20poly1305->set_key(key, 32);
104 chacha20poly1305->set_associated_data(ad, ad_len);
105 chacha20poly1305->start(nonce, nonce_len);
106
107 // FIXME do this in-place
109 buf.reserve(ptext_len + 16);
110 buf.assign(ptext, ptext + ptext_len);
111
112 chacha20poly1305->finish(buf);
113
114 copy_mem(ctext, buf.data(), ptext_len);
115 copy_mem(mac, buf.data() + ptext_len, 16);
116 return 0;
117}
118
119int sodium_aead_chacha20poly1305_decrypt_detached(uint8_t ptext[],
120 const uint8_t ctext[],
121 size_t ctext_len,
122 const uint8_t mac[],
123 const uint8_t ad[],
124 size_t ad_len,
125 const uint8_t nonce[],
126 size_t nonce_len,
127 const uint8_t key[]) {
128 auto chacha20poly1305 = AEAD_Mode::create_or_throw("ChaCha20Poly1305", Cipher_Dir::Decryption);
129
130 chacha20poly1305->set_key(key, 32);
131 chacha20poly1305->set_associated_data(ad, ad_len);
132 chacha20poly1305->start(nonce, nonce_len);
133
134 // FIXME do this in-place
136 buf.reserve(ctext_len + 16);
137 buf.assign(ctext, ctext + ctext_len);
138 buf.insert(buf.end(), mac, mac + 16);
139
140 try {
141 chacha20poly1305->finish(buf);
143 if(ptext != nullptr) {
144 clear_mem(ptext, ctext_len);
145 }
146 return -1;
147 }
148
149 if(ptext != nullptr) {
150 copy_mem(ptext, buf.data(), buf.size());
151 }
152 return 0;
153}
154
155} // namespace
156
158 unsigned long long* ctext_len,
159 const uint8_t ptext[],
160 size_t ptext_len,
161 const uint8_t ad[],
162 size_t ad_len,
163 const uint8_t unused_secret_nonce[],
164 const uint8_t nonce[],
165 const uint8_t key[]) {
166 BOTAN_UNUSED(unused_secret_nonce);
167
168 return sodium_aead_chacha20poly1305_encrypt(
169 ctext, ctext_len, ptext, ptext_len, ad, ad_len, nonce, crypto_aead_chacha20poly1305_ietf_npubbytes(), key);
170}
171
173 unsigned long long* ptext_len,
174 uint8_t unused_secret_nonce[],
175 const uint8_t ctext[],
176 size_t ctext_len,
177 const uint8_t ad[],
178 size_t ad_len,
179 const uint8_t nonce[],
180 const uint8_t key[]) {
181 BOTAN_UNUSED(unused_secret_nonce);
182
183 return sodium_aead_chacha20poly1305_decrypt(
184 ptext, ptext_len, ctext, ctext_len, ad, ad_len, nonce, crypto_aead_chacha20poly1305_ietf_npubbytes(), key);
185}
186
188 uint8_t mac[],
189 unsigned long long* mac_len,
190 const uint8_t ptext[],
191 size_t ptext_len,
192 const uint8_t ad[],
193 size_t ad_len,
194 const uint8_t unused_secret_nonce[],
195 const uint8_t nonce[],
196 const uint8_t key[]) {
197 BOTAN_UNUSED(unused_secret_nonce);
198
199 if(mac_len != nullptr) {
200 *mac_len = 16;
201 }
202
203 return sodium_aead_chacha20poly1305_encrypt_detached(
204 ctext, mac, ptext, ptext_len, ad, ad_len, nonce, crypto_aead_chacha20poly1305_ietf_npubbytes(), key);
205}
206
208 uint8_t unused_secret_nonce[],
209 const uint8_t ctext[],
210 size_t ctext_len,
211 const uint8_t mac[],
212 const uint8_t ad[],
213 size_t ad_len,
214 const uint8_t nonce[],
215 const uint8_t key[]) {
216 BOTAN_UNUSED(unused_secret_nonce);
217
218 return sodium_aead_chacha20poly1305_decrypt_detached(
219 ptext, ctext, ctext_len, mac, ad, ad_len, nonce, crypto_aead_chacha20poly1305_ietf_npubbytes(), key);
220}
221
223 unsigned long long* ctext_len,
224 const uint8_t ptext[],
225 size_t ptext_len,
226 const uint8_t ad[],
227 size_t ad_len,
228 const uint8_t unused_secret_nonce[],
229 const uint8_t nonce[],
230 const uint8_t key[]) {
231 BOTAN_UNUSED(unused_secret_nonce);
232 return sodium_aead_chacha20poly1305_encrypt(
233 ctext, ctext_len, ptext, ptext_len, ad, ad_len, nonce, crypto_aead_chacha20poly1305_npubbytes(), key);
234}
235
237 unsigned long long* ptext_len,
238 uint8_t unused_secret_nonce[],
239 const uint8_t ctext[],
240 size_t ctext_len,
241 const uint8_t ad[],
242 size_t ad_len,
243 const uint8_t nonce[],
244 const uint8_t key[]) {
245 BOTAN_UNUSED(unused_secret_nonce);
246 return sodium_aead_chacha20poly1305_decrypt(
247 ptext, ptext_len, ctext, ctext_len, ad, ad_len, nonce, crypto_aead_chacha20poly1305_npubbytes(), key);
248}
249
251 uint8_t mac[],
252 unsigned long long* mac_len,
253 const uint8_t ptext[],
254 size_t ptext_len,
255 const uint8_t ad[],
256 size_t ad_len,
257 const uint8_t unused_secret_nonce[],
258 const uint8_t nonce[],
259 const uint8_t key[]) {
260 BOTAN_UNUSED(unused_secret_nonce);
261 if(mac_len != nullptr) {
262 *mac_len = 16;
263 }
264
265 return sodium_aead_chacha20poly1305_encrypt_detached(
266 ctext, mac, ptext, ptext_len, ad, ad_len, nonce, crypto_aead_chacha20poly1305_npubbytes(), key);
267}
268
270 uint8_t unused_secret_nonce[],
271 const uint8_t ctext[],
272 size_t ctext_len,
273 const uint8_t mac[],
274 const uint8_t ad[],
275 size_t ad_len,
276 const uint8_t nonce[],
277 const uint8_t key[]) {
278 BOTAN_UNUSED(unused_secret_nonce);
279
280 return sodium_aead_chacha20poly1305_decrypt_detached(
281 ptext, ctext, ctext_len, mac, ad, ad_len, nonce, crypto_aead_chacha20poly1305_npubbytes(), key);
282}
283
285 unsigned long long* ctext_len,
286 const uint8_t ptext[],
287 size_t ptext_len,
288 const uint8_t ad[],
289 size_t ad_len,
290 const uint8_t unused_secret_nonce[],
291 const uint8_t nonce[],
292 const uint8_t key[]) {
293 BOTAN_UNUSED(unused_secret_nonce);
294
295 return sodium_aead_chacha20poly1305_encrypt(
296 ctext, ctext_len, ptext, ptext_len, ad, ad_len, nonce, crypto_aead_xchacha20poly1305_ietf_npubbytes(), key);
297}
298
300 unsigned long long* ptext_len,
301 uint8_t unused_secret_nonce[],
302 const uint8_t ctext[],
303 size_t ctext_len,
304 const uint8_t ad[],
305 size_t ad_len,
306 const uint8_t nonce[],
307 const uint8_t key[]) {
308 BOTAN_UNUSED(unused_secret_nonce);
309
310 return sodium_aead_chacha20poly1305_decrypt(
311 ptext, ptext_len, ctext, ctext_len, ad, ad_len, nonce, crypto_aead_xchacha20poly1305_ietf_npubbytes(), key);
312}
313
315 uint8_t mac[],
316 unsigned long long* mac_len,
317 const uint8_t ptext[],
318 size_t ptext_len,
319 const uint8_t ad[],
320 size_t ad_len,
321 const uint8_t unused_secret_nonce[],
322 const uint8_t nonce[],
323 const uint8_t key[]) {
324 BOTAN_UNUSED(unused_secret_nonce);
325 if(mac_len != nullptr) {
326 *mac_len = 16;
327 }
328
329 return sodium_aead_chacha20poly1305_encrypt_detached(
330 ctext, mac, ptext, ptext_len, ad, ad_len, nonce, crypto_aead_xchacha20poly1305_ietf_npubbytes(), key);
331}
332
334 uint8_t unused_secret_nonce[],
335 const uint8_t ctext[],
336 size_t ctext_len,
337 const uint8_t mac[],
338 const uint8_t ad[],
339 size_t ad_len,
340 const uint8_t nonce[],
341 const uint8_t key[]) {
342 BOTAN_UNUSED(unused_secret_nonce);
343 return sodium_aead_chacha20poly1305_decrypt_detached(
344 ptext, ctext, ctext_len, mac, ad, ad_len, nonce, crypto_aead_xchacha20poly1305_ietf_npubbytes(), key);
345}
346
347} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
static std::unique_ptr< AEAD_Mode > create_or_throw(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
Definition aead.cpp:51
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()
Definition sodium.h:371
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()
Definition sodium.h:511
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()
Definition sodium.h:440
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)
Definition mem_ops.h:144
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:118