-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathaead.cpp
More file actions
302 lines (266 loc) · 9.16 KB
/
aead.cpp
File metadata and controls
302 lines (266 loc) · 9.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// ============================================================================
// AEAD (Authenticated Encryption with Associated Data)
#include "ncrypto.h"
#ifdef OPENSSL_IS_BORINGSSL
#include "ncrypto/aead.h"
namespace ncrypto {
const Aead Aead::FromName(std::string_view name) {
for (const auto& [construct, info] : aeadIndex) {
if (EqualNoCase(info.name, name)) {
return Aead(&info, construct());
}
}
return Aead();
}
const Aead Aead::FromCtx(std::string_view name, const AeadCtxPointer& ctx) {
for (const auto& [_, info] : aeadIndex) {
if (info.name == name) {
return Aead(&info, EVP_AEAD_CTX_aead(ctx.get()));
}
}
return Aead();
}
int Aead::getMode() const {
if (!aead_) return -1;
return info_->mode;
}
std::string_view Aead::getModeLabel() const {
if (!aead_) return {};
switch (getMode()) {
case EVP_CIPH_CCM_MODE:
return "ccm";
case EVP_CIPH_CTR_MODE:
return "ctr";
case EVP_CIPH_GCM_MODE:
return "gcm";
case EVP_CIPH_STREAM_CIPHER:
return "stream";
}
return "{unknown}";
}
int Aead::getNonceLength() const {
if (!aead_) return 0;
return EVP_AEAD_nonce_length(aead_);
}
int Aead::getKeyLength() const {
if (!aead_) return 0;
return EVP_AEAD_key_length(aead_);
}
int Aead::getMaxOverhead() const {
if (!aead_) return 0;
return EVP_AEAD_max_overhead(aead_);
}
int Aead::getMaxTagLength() const {
if (!aead_) return 0;
return EVP_AEAD_max_tag_len(aead_);
}
int Aead::getBlockSize() const {
if (!aead_) return 0;
// EVP_CIPHER_CTX_block_size returns the block size, in bytes, of the cipher
// underlying |ctx|, or one if the cipher is a stream cipher.
return 1;
}
std::string_view Aead::getName() const {
if (!aead_) return "";
return info_->name;
}
int Aead::getNid() const {
if (!aead_) return 0;
return info_->nid;
}
const Aead Aead::FromConstructor(Aead::AeadConstructor construct) {
return Aead(&aeadIndex.at(construct), construct());
}
const std::unordered_map<Aead::AeadConstructor, Aead::AeadInfo>
Aead::aeadIndex = {
{EVP_aead_aes_128_gcm,
{.name = LN_aes_128_gcm,
.mode = EVP_CIPH_GCM_MODE,
.nid = NID_aes_128_gcm}},
{EVP_aead_aes_192_gcm,
{.name = LN_aes_192_gcm,
.mode = EVP_CIPH_GCM_MODE,
.nid = NID_aes_192_gcm}},
{EVP_aead_aes_256_gcm,
{.name = LN_aes_256_gcm,
.mode = EVP_CIPH_GCM_MODE,
.nid = NID_aes_256_gcm}},
{EVP_aead_chacha20_poly1305,
{.name = LN_chacha20_poly1305,
.mode = EVP_CIPH_STREAM_CIPHER,
.nid = NID_chacha20_poly1305}},
{EVP_aead_xchacha20_poly1305,
{
.name = "xchacha20-poly1305",
.mode = EVP_CIPH_STREAM_CIPHER,
}},
{EVP_aead_aes_128_ctr_hmac_sha256,
{
.name = "aes-128-ctr-hmac-sha256",
.mode = EVP_CIPH_CTR_MODE,
}},
{EVP_aead_aes_256_ctr_hmac_sha256,
{
.name = "aes-256-ctr-hmac-sha256",
.mode = EVP_CIPH_CTR_MODE,
}},
{EVP_aead_aes_128_gcm_siv,
{
.name = "aes-128-gcm-siv",
.mode = EVP_CIPH_GCM_MODE,
}},
{EVP_aead_aes_256_gcm_siv,
{
.name = "aes-256-gcm-siv",
.mode = EVP_CIPH_GCM_MODE,
}},
{EVP_aead_aes_128_gcm_randnonce,
{
.name = "aes-128-gcm-randnonce",
.mode = EVP_CIPH_GCM_MODE,
}},
{EVP_aead_aes_256_gcm_randnonce,
{
.name = "aes-256-gcm-randnonce",
.mode = EVP_CIPH_GCM_MODE,
}},
{EVP_aead_aes_128_ccm_bluetooth,
{
.name = "aes-128-ccm-bluetooth",
.mode = EVP_CIPH_CCM_MODE,
}},
{EVP_aead_aes_128_ccm_bluetooth_8,
{
.name = "aes-128-ccm-bluetooth-8",
.mode = EVP_CIPH_CCM_MODE,
}},
{EVP_aead_aes_128_ccm_matter,
{
.name = "aes-128-ccm-matter",
.mode = EVP_CIPH_CCM_MODE,
}},
{EVP_aead_aes_128_eax,
{.name = "aes-128-eax",
// BoringSSL does not define a mode constant for EAX. Using STREAM
// arbitrarily
.mode = EVP_CIPH_STREAM_CIPHER}},
{EVP_aead_aes_256_eax,
{.name = "aes-256-eax",
// BoringSSL does not define a mode constant for EAX. Using STREAM
// arbitrarily
.mode = EVP_CIPH_STREAM_CIPHER}},
};
void Aead::ForEach(AeadNameCallback callback) {
for (const auto& [_, info] : aeadIndex) {
callback(info.name);
}
}
const Aead Aead::EMPTY = Aead();
const Aead Aead::AES_128_GCM = Aead::FromConstructor(EVP_aead_aes_128_gcm);
const Aead Aead::AES_192_GCM = Aead::FromConstructor(EVP_aead_aes_192_gcm);
const Aead Aead::AES_256_GCM = Aead::FromConstructor(EVP_aead_aes_256_gcm);
const Aead Aead::CHACHA20_POLY1305 =
Aead::FromConstructor(EVP_aead_chacha20_poly1305);
const Aead Aead::XCHACHA20_POLY1305 =
Aead::FromConstructor(EVP_aead_xchacha20_poly1305);
const Aead Aead::AES_128_CTR_HMAC_SHA256 =
Aead::FromConstructor(EVP_aead_aes_128_ctr_hmac_sha256);
const Aead Aead::AES_256_CTR_HMAC_SHA256 =
Aead::FromConstructor(EVP_aead_aes_256_ctr_hmac_sha256);
const Aead Aead::AES_128_GCM_SIV =
Aead::FromConstructor(EVP_aead_aes_128_gcm_siv);
const Aead Aead::AES_256_GCM_SIV =
Aead::FromConstructor(EVP_aead_aes_256_gcm_siv);
const Aead Aead::AES_128_GCM_RANDNONCE =
Aead::FromConstructor(EVP_aead_aes_128_gcm_randnonce);
const Aead Aead::AES_256_GCM_RANDNONCE =
Aead::FromConstructor(EVP_aead_aes_256_gcm_randnonce);
const Aead Aead::AES_128_CCM_BLUETOOTH =
Aead::FromConstructor(EVP_aead_aes_128_ccm_bluetooth);
const Aead Aead::AES_128_CCM_BLUETOOTH_8 =
Aead::FromConstructor(EVP_aead_aes_128_ccm_bluetooth_8);
const Aead Aead::AES_128_CCM_MATTER =
Aead::FromConstructor(EVP_aead_aes_128_ccm_matter);
const Aead Aead::AES_128_EAX = Aead::FromConstructor(EVP_aead_aes_128_eax);
const Aead Aead::AES_256_EAX = Aead::FromConstructor(EVP_aead_aes_256_eax);
AeadCtxPointer AeadCtxPointer::New(const Aead& aead,
bool encrypt,
const unsigned char* key,
size_t keyLen,
size_t tagLen) {
// Note: In the EVP_AEAD API new always calls init
auto ret = AeadCtxPointer(EVP_AEAD_CTX_new(aead.get(), key, keyLen, tagLen));
if (!ret) {
return {};
}
return ret;
}
AeadCtxPointer::AeadCtxPointer(EVP_AEAD_CTX* ctx) : ctx_(ctx) {}
AeadCtxPointer::AeadCtxPointer(AeadCtxPointer&& other) noexcept
: ctx_(other.release()) {}
AeadCtxPointer& AeadCtxPointer::operator=(AeadCtxPointer&& other) noexcept {
if (this == &other) return *this;
this->~AeadCtxPointer();
return *new (this) AeadCtxPointer(std::move(other));
}
AeadCtxPointer::~AeadCtxPointer() {
reset();
}
void AeadCtxPointer::reset(EVP_AEAD_CTX* ctx) {
ctx_.reset(ctx);
}
EVP_AEAD_CTX* AeadCtxPointer::release() {
return ctx_.release();
}
bool AeadCtxPointer::init(const Aead& aead,
bool encrypt,
const unsigned char* key,
size_t keyLen,
size_t tagLen) {
return EVP_AEAD_CTX_init_with_direction(
ctx_.get(),
aead,
key,
keyLen,
tagLen,
encrypt ? evp_aead_seal : evp_aead_open);
}
bool AeadCtxPointer::encrypt(const Buffer<const unsigned char>& in,
Buffer<unsigned char>& out,
Buffer<unsigned char>& tag,
const Buffer<const unsigned char>& nonce,
const Buffer<const unsigned char>& aad) {
if (!ctx_) return false;
return EVP_AEAD_CTX_seal_scatter(ctx_.get(),
out.data,
tag.data,
&tag.len,
tag.len,
nonce.data,
nonce.len,
in.data,
in.len,
nullptr /* extra_in */,
0 /* extra_in_len */,
aad.data,
aad.len) == 1;
}
bool AeadCtxPointer::decrypt(const Buffer<const unsigned char>& in,
Buffer<unsigned char>& out,
const Buffer<const unsigned char>& tag,
const Buffer<const unsigned char>& nonce,
const Buffer<const unsigned char>& aad) {
if (!ctx_) return false;
return EVP_AEAD_CTX_open_gather(ctx_.get(),
out.data,
nonce.data,
nonce.len,
in.data,
in.len,
tag.data,
tag.len,
aad.data,
aad.len) == 1;
}
} // namespace ncrypto
#endif