• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 6704830136

31 Oct 2023 09:55AM UTC coverage: 91.722% (+0.001%) from 91.721%
6704830136

push

github

web-flow
Merge pull request #3752 from randombit/jack/allocator-helper

Split out allocator helpers to allocator.h

80150 of 87384 relevant lines covered (91.72%)

8593301.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.37
/src/lib/compat/sodium/sodium_aead.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/aead.h>
10
#include <botan/mem_ops.h>
11

12
namespace Botan {
13

14
namespace {
15

16
int sodium_aead_chacha20poly1305_encrypt(uint8_t ctext[],
3✔
17
                                         unsigned long long* ctext_len,
18
                                         const uint8_t ptext[],
19
                                         size_t ptext_len,
20
                                         const uint8_t ad[],
21
                                         size_t ad_len,
22
                                         const uint8_t nonce[],
23
                                         size_t nonce_len,
24
                                         const uint8_t key[]) {
25
   auto chacha20poly1305 = AEAD_Mode::create_or_throw("ChaCha20Poly1305", Cipher_Dir::Encryption);
3✔
26

27
   chacha20poly1305->set_key(key, 32);
3✔
28
   chacha20poly1305->set_associated_data(ad, ad_len);
3✔
29
   chacha20poly1305->start(nonce, nonce_len);
3✔
30

31
   // FIXME do this in-place
32
   secure_vector<uint8_t> buf;
3✔
33
   buf.reserve(ptext_len + 16);
3✔
34
   buf.assign(ptext, ptext + ptext_len);
3✔
35

36
   chacha20poly1305->finish(buf);
3✔
37

38
   copy_mem(ctext, buf.data(), buf.size());
3✔
39
   if(ctext_len) {
3✔
40
      *ctext_len = buf.size();
3✔
41
   }
42
   return 0;
3✔
43
}
6✔
44

45
int sodium_aead_chacha20poly1305_decrypt(uint8_t ptext[],
3✔
46
                                         unsigned long long* ptext_len,
47
                                         const uint8_t ctext[],
48
                                         size_t ctext_len,
49
                                         const uint8_t ad[],
50
                                         size_t ad_len,
51
                                         const uint8_t nonce[],
52
                                         size_t nonce_len,
53
                                         const uint8_t key[]) {
54
   if(ctext_len < 16) {
3✔
55
      return -1;
56
   }
57

58
   *ptext_len = 0;
3✔
59

60
   auto chacha20poly1305 = AEAD_Mode::create_or_throw("ChaCha20Poly1305", Cipher_Dir::Decryption);
3✔
61

62
   chacha20poly1305->set_key(key, 32);
3✔
63
   chacha20poly1305->set_associated_data(ad, ad_len);
3✔
64
   chacha20poly1305->start(nonce, nonce_len);
3✔
65

66
   // FIXME do this in-place
67
   secure_vector<uint8_t> buf;
3✔
68
   buf.assign(ctext, ctext + ctext_len);
3✔
69

70
   try {
3✔
71
      chacha20poly1305->finish(buf);
3✔
72
   } catch(Invalid_Authentication_Tag&) {
×
73
      return -1;
×
74
   }
×
75

76
   *ptext_len = ctext_len - 16;
3✔
77

78
   copy_mem(ptext, buf.data(), buf.size());
3✔
79
   return 0;
80
}
6✔
81

82
int sodium_aead_chacha20poly1305_encrypt_detached(uint8_t ctext[],
3✔
83
                                                  uint8_t mac[],
84
                                                  const uint8_t ptext[],
85
                                                  size_t ptext_len,
86
                                                  const uint8_t ad[],
87
                                                  size_t ad_len,
88
                                                  const uint8_t nonce[],
89
                                                  size_t nonce_len,
90
                                                  const uint8_t key[]) {
91
   auto chacha20poly1305 = AEAD_Mode::create_or_throw("ChaCha20Poly1305", Cipher_Dir::Encryption);
3✔
92

93
   chacha20poly1305->set_key(key, 32);
3✔
94
   chacha20poly1305->set_associated_data(ad, ad_len);
3✔
95
   chacha20poly1305->start(nonce, nonce_len);
3✔
96

97
   // FIXME do this in-place
98
   secure_vector<uint8_t> buf;
3✔
99
   buf.reserve(ptext_len + 16);
3✔
100
   buf.assign(ptext, ptext + ptext_len);
3✔
101

102
   chacha20poly1305->finish(buf);
3✔
103

104
   copy_mem(ctext, buf.data(), ptext_len);
3✔
105
   copy_mem(mac, buf.data() + ptext_len, 16);
3✔
106
   return 0;
3✔
107
}
6✔
108

109
int sodium_aead_chacha20poly1305_decrypt_detached(uint8_t ptext[],
6✔
110
                                                  const uint8_t ctext[],
111
                                                  size_t ctext_len,
112
                                                  const uint8_t mac[],
113
                                                  const uint8_t ad[],
114
                                                  size_t ad_len,
115
                                                  const uint8_t nonce[],
116
                                                  size_t nonce_len,
117
                                                  const uint8_t key[]) {
118
   auto chacha20poly1305 = AEAD_Mode::create_or_throw("ChaCha20Poly1305", Cipher_Dir::Decryption);
6✔
119

120
   chacha20poly1305->set_key(key, 32);
6✔
121
   chacha20poly1305->set_associated_data(ad, ad_len);
6✔
122
   chacha20poly1305->start(nonce, nonce_len);
6✔
123

124
   // FIXME do this in-place
125
   secure_vector<uint8_t> buf;
6✔
126
   buf.reserve(ctext_len + 16);
6✔
127
   buf.assign(ctext, ctext + ctext_len);
6✔
128
   buf.insert(buf.end(), mac, mac + 16);
6✔
129

130
   try {
6✔
131
      chacha20poly1305->finish(buf);
6✔
132
   } catch(Invalid_Authentication_Tag&) {
3✔
133
      return -1;
3✔
134
   }
3✔
135

136
   copy_mem(ptext, buf.data(), buf.size());
3✔
137
   return 0;
138
}
12✔
139

140
}  // namespace
141

142
int Sodium::crypto_aead_chacha20poly1305_ietf_encrypt(uint8_t ctext[],
1✔
143
                                                      unsigned long long* ctext_len,
144
                                                      const uint8_t ptext[],
145
                                                      size_t ptext_len,
146
                                                      const uint8_t ad[],
147
                                                      size_t ad_len,
148
                                                      const uint8_t unused_secret_nonce[],
149
                                                      const uint8_t nonce[],
150
                                                      const uint8_t key[]) {
151
   BOTAN_UNUSED(unused_secret_nonce);
1✔
152

153
   return sodium_aead_chacha20poly1305_encrypt(
1✔
154
      ctext, ctext_len, ptext, ptext_len, ad, ad_len, nonce, crypto_aead_chacha20poly1305_ietf_npubbytes(), key);
1✔
155
}
156

157
int Sodium::crypto_aead_chacha20poly1305_ietf_decrypt(uint8_t ptext[],
1✔
158
                                                      unsigned long long* ptext_len,
159
                                                      uint8_t unused_secret_nonce[],
160
                                                      const uint8_t ctext[],
161
                                                      size_t ctext_len,
162
                                                      const uint8_t ad[],
163
                                                      size_t ad_len,
164
                                                      const uint8_t nonce[],
165
                                                      const uint8_t key[]) {
166
   BOTAN_UNUSED(unused_secret_nonce);
1✔
167

168
   return sodium_aead_chacha20poly1305_decrypt(
1✔
169
      ptext, ptext_len, ctext, ctext_len, ad, ad_len, nonce, crypto_aead_chacha20poly1305_ietf_npubbytes(), key);
1✔
170
}
171

172
int Sodium::crypto_aead_chacha20poly1305_ietf_encrypt_detached(uint8_t ctext[],
1✔
173
                                                               uint8_t mac[],
174
                                                               unsigned long long* mac_len,
175
                                                               const uint8_t ptext[],
176
                                                               size_t ptext_len,
177
                                                               const uint8_t ad[],
178
                                                               size_t ad_len,
179
                                                               const uint8_t unused_secret_nonce[],
180
                                                               const uint8_t nonce[],
181
                                                               const uint8_t key[]) {
182
   BOTAN_UNUSED(unused_secret_nonce);
1✔
183

184
   if(mac_len) {
1✔
185
      *mac_len = 16;
1✔
186
   }
187

188
   return sodium_aead_chacha20poly1305_encrypt_detached(
1✔
189
      ctext, mac, ptext, ptext_len, ad, ad_len, nonce, crypto_aead_chacha20poly1305_ietf_npubbytes(), key);
1✔
190
}
191

192
int Sodium::crypto_aead_chacha20poly1305_ietf_decrypt_detached(uint8_t ptext[],
2✔
193
                                                               uint8_t unused_secret_nonce[],
194
                                                               const uint8_t ctext[],
195
                                                               size_t ctext_len,
196
                                                               const uint8_t mac[],
197
                                                               const uint8_t ad[],
198
                                                               size_t ad_len,
199
                                                               const uint8_t nonce[],
200
                                                               const uint8_t key[]) {
201
   BOTAN_UNUSED(unused_secret_nonce);
2✔
202

203
   return sodium_aead_chacha20poly1305_decrypt_detached(
2✔
204
      ptext, ctext, ctext_len, mac, ad, ad_len, nonce, crypto_aead_chacha20poly1305_ietf_npubbytes(), key);
2✔
205
}
206

207
int Sodium::crypto_aead_chacha20poly1305_encrypt(uint8_t ctext[],
1✔
208
                                                 unsigned long long* ctext_len,
209
                                                 const uint8_t ptext[],
210
                                                 size_t ptext_len,
211
                                                 const uint8_t ad[],
212
                                                 size_t ad_len,
213
                                                 const uint8_t unused_secret_nonce[],
214
                                                 const uint8_t nonce[],
215
                                                 const uint8_t key[]) {
216
   BOTAN_UNUSED(unused_secret_nonce);
1✔
217
   return sodium_aead_chacha20poly1305_encrypt(
1✔
218
      ctext, ctext_len, ptext, ptext_len, ad, ad_len, nonce, crypto_aead_chacha20poly1305_npubbytes(), key);
1✔
219
}
220

221
int Sodium::crypto_aead_chacha20poly1305_decrypt(uint8_t ptext[],
1✔
222
                                                 unsigned long long* ptext_len,
223
                                                 uint8_t unused_secret_nonce[],
224
                                                 const uint8_t ctext[],
225
                                                 size_t ctext_len,
226
                                                 const uint8_t ad[],
227
                                                 size_t ad_len,
228
                                                 const uint8_t nonce[],
229
                                                 const uint8_t key[]) {
230
   BOTAN_UNUSED(unused_secret_nonce);
1✔
231
   return sodium_aead_chacha20poly1305_decrypt(
1✔
232
      ptext, ptext_len, ctext, ctext_len, ad, ad_len, nonce, crypto_aead_chacha20poly1305_npubbytes(), key);
1✔
233
}
234

235
int Sodium::crypto_aead_chacha20poly1305_encrypt_detached(uint8_t ctext[],
1✔
236
                                                          uint8_t mac[],
237
                                                          unsigned long long* mac_len,
238
                                                          const uint8_t ptext[],
239
                                                          size_t ptext_len,
240
                                                          const uint8_t ad[],
241
                                                          size_t ad_len,
242
                                                          const uint8_t unused_secret_nonce[],
243
                                                          const uint8_t nonce[],
244
                                                          const uint8_t key[]) {
245
   BOTAN_UNUSED(unused_secret_nonce);
1✔
246
   if(mac_len) {
1✔
247
      *mac_len = 16;
1✔
248
   }
249

250
   return sodium_aead_chacha20poly1305_encrypt_detached(
1✔
251
      ctext, mac, ptext, ptext_len, ad, ad_len, nonce, crypto_aead_chacha20poly1305_npubbytes(), key);
1✔
252
}
253

254
int Sodium::crypto_aead_chacha20poly1305_decrypt_detached(uint8_t ptext[],
2✔
255
                                                          uint8_t unused_secret_nonce[],
256
                                                          const uint8_t ctext[],
257
                                                          size_t ctext_len,
258
                                                          const uint8_t mac[],
259
                                                          const uint8_t ad[],
260
                                                          size_t ad_len,
261
                                                          const uint8_t nonce[],
262
                                                          const uint8_t key[]) {
263
   BOTAN_UNUSED(unused_secret_nonce);
2✔
264

265
   return sodium_aead_chacha20poly1305_decrypt_detached(
2✔
266
      ptext, ctext, ctext_len, mac, ad, ad_len, nonce, crypto_aead_chacha20poly1305_npubbytes(), key);
2✔
267
}
268

269
int Sodium::crypto_aead_xchacha20poly1305_ietf_encrypt(uint8_t ctext[],
1✔
270
                                                       unsigned long long* ctext_len,
271
                                                       const uint8_t ptext[],
272
                                                       size_t ptext_len,
273
                                                       const uint8_t ad[],
274
                                                       size_t ad_len,
275
                                                       const uint8_t unused_secret_nonce[],
276
                                                       const uint8_t nonce[],
277
                                                       const uint8_t key[]) {
278
   BOTAN_UNUSED(unused_secret_nonce);
1✔
279

280
   return sodium_aead_chacha20poly1305_encrypt(
1✔
281
      ctext, ctext_len, ptext, ptext_len, ad, ad_len, nonce, crypto_aead_xchacha20poly1305_ietf_npubbytes(), key);
1✔
282
}
283

284
int Sodium::crypto_aead_xchacha20poly1305_ietf_decrypt(uint8_t ptext[],
1✔
285
                                                       unsigned long long* ptext_len,
286
                                                       uint8_t unused_secret_nonce[],
287
                                                       const uint8_t ctext[],
288
                                                       size_t ctext_len,
289
                                                       const uint8_t ad[],
290
                                                       size_t ad_len,
291
                                                       const uint8_t nonce[],
292
                                                       const uint8_t key[]) {
293
   BOTAN_UNUSED(unused_secret_nonce);
1✔
294

295
   return sodium_aead_chacha20poly1305_decrypt(
1✔
296
      ptext, ptext_len, ctext, ctext_len, ad, ad_len, nonce, crypto_aead_xchacha20poly1305_ietf_npubbytes(), key);
1✔
297
}
298

299
int Sodium::crypto_aead_xchacha20poly1305_ietf_encrypt_detached(uint8_t ctext[],
1✔
300
                                                                uint8_t mac[],
301
                                                                unsigned long long* mac_len,
302
                                                                const uint8_t ptext[],
303
                                                                size_t ptext_len,
304
                                                                const uint8_t ad[],
305
                                                                size_t ad_len,
306
                                                                const uint8_t unused_secret_nonce[],
307
                                                                const uint8_t nonce[],
308
                                                                const uint8_t key[]) {
309
   BOTAN_UNUSED(unused_secret_nonce);
1✔
310
   if(mac_len) {
1✔
311
      *mac_len = 16;
1✔
312
   }
313

314
   return sodium_aead_chacha20poly1305_encrypt_detached(
1✔
315
      ctext, mac, ptext, ptext_len, ad, ad_len, nonce, crypto_aead_xchacha20poly1305_ietf_npubbytes(), key);
1✔
316
}
317

318
int Sodium::crypto_aead_xchacha20poly1305_ietf_decrypt_detached(uint8_t ptext[],
2✔
319
                                                                uint8_t unused_secret_nonce[],
320
                                                                const uint8_t ctext[],
321
                                                                size_t ctext_len,
322
                                                                const uint8_t mac[],
323
                                                                const uint8_t ad[],
324
                                                                size_t ad_len,
325
                                                                const uint8_t nonce[],
326
                                                                const uint8_t key[]) {
327
   BOTAN_UNUSED(unused_secret_nonce);
2✔
328
   return sodium_aead_chacha20poly1305_decrypt_detached(
2✔
329
      ptext, ctext, ctext_len, mac, ad, ad_len, nonce, crypto_aead_xchacha20poly1305_ietf_npubbytes(), key);
2✔
330
}
331

332
}  // 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

© 2026 Coveralls, Inc