-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathaesgcm.cc
More file actions
262 lines (220 loc) · 7.41 KB
/
Copy pathaesgcm.cc
File metadata and controls
262 lines (220 loc) · 7.41 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
/*
* Copyright 2023 Cryspen Sarl
*
* Licensed under the Apache License, Version 2.0 or MIT.
* - http://www.apache.org/licenses/LICENSE-2.0
* - http://opensource.org/licenses/MIT
*/
#include "util.h"
#include "krml/internal/target.h"
#ifdef HACL_CAN_COMPILE_AESNI_PCLMUL
#include "Hacl_AES_128_GCM_NI.h"
#endif
#include "Hacl_AES_128_GCM_M32.h"
#include "EverCrypt_AEAD.h"
#include "../third-party/bearssl/bearssl_block.h"
#include "../third-party/bearssl/bearssl_hash.h"
#include "../third-party/bearssl/bearssl_aead.h"
static bytes key(16, 7);
static bytes nonce(12, 9);
static bytes mac(16, 0);
#ifdef HACL_CAN_COMPILE_AESNI_PCLMUL
static void
HACL_AES_128_GCM_NI_encrypt(benchmark::State& state)
{
bytes plaintext(state.range(0), 0x37);
bytes ciphertext(state.range(0) + 16, 0);
for (auto _ : state) {
Lib_IntVector_Intrinsics_vec128 *ctx = (Lib_IntVector_Intrinsics_vec128 *)KRML_HOST_CALLOC((uint32_t)352U, sizeof (uint8_t));
Hacl_AES_128_GCM_NI_aes128_gcm_init(ctx, key.data());
Hacl_AES_128_GCM_NI_aes128_gcm_encrypt(ctx, plaintext.size(), ciphertext.data(), plaintext.data(), 0, NULL, nonce.size(), nonce.data());
KRML_HOST_FREE(ctx);
}
}
BENCHMARK(HACL_AES_128_GCM_NI_encrypt)->Setup(DoSetup)->Apply(Range);
static void
HACL_AES_128_GCM_NI_aad(benchmark::State& state)
{
bytes aad(state.range(0), 0x37);
for (auto _ : state) {
Lib_IntVector_Intrinsics_vec128 *ctx = (Lib_IntVector_Intrinsics_vec128 *)KRML_HOST_CALLOC((uint32_t)352U, sizeof (uint8_t));
Hacl_AES_128_GCM_NI_aes128_gcm_init(ctx, key.data());
Hacl_AES_128_GCM_NI_aes128_gcm_encrypt(ctx, 0, mac.data(), NULL, aad.size(), aad.data(), nonce.size(), nonce.data());
KRML_HOST_FREE(ctx);
}
}
BENCHMARK(HACL_AES_128_GCM_NI_aad)->Setup(DoSetup)->Apply(Range);
#endif
static void
HACL_AES_128_GCM_M32_encrypt(benchmark::State& state)
{
bytes plaintext(state.range(0), 0x37);
bytes ciphertext(state.range(0) + 16, 0);
for (auto _ : state) {
uint64_t *ctx = (uint64_t *)KRML_HOST_CALLOC((uint32_t)3168U, sizeof (uint8_t));
Hacl_AES_128_GCM_M32_aes128_gcm_init(ctx, key.data());
Hacl_AES_128_GCM_M32_aes128_gcm_encrypt(ctx, plaintext.size(), ciphertext.data(), plaintext.data(), 0, NULL, nonce.size(), nonce.data());
KRML_HOST_FREE(ctx);
}
}
BENCHMARK(HACL_AES_128_GCM_M32_encrypt)->Setup(DoSetup)->Apply(Range);
static void
HACL_AES_128_GCM_M32_aad(benchmark::State& state)
{
bytes aad(state.range(0), 0x37);
for (auto _ : state) {
uint64_t *ctx = (uint64_t *)KRML_HOST_CALLOC((uint32_t)3168U, sizeof (uint8_t));
Hacl_AES_128_GCM_M32_aes128_gcm_init(ctx, key.data());
Hacl_AES_128_GCM_M32_aes128_gcm_encrypt(ctx, 0, mac.data(), NULL, aad.size(), aad.data(), nonce.size(), nonce.data());
KRML_HOST_FREE(ctx);
}
}
BENCHMARK(HACL_AES_128_GCM_M32_aad)->Setup(DoSetup)->Apply(Range);
static void
EverCrypt_AES128_GCM_encrypt(benchmark::State& state)
{
bytes plaintext(state.range(0), 0x37);
bytes ciphertext(state.range(0), 0);
for (auto _ : state) {
EverCrypt_AEAD_state_s* ctx;
EverCrypt_Error_error_code res = EverCrypt_AEAD_create_in(
Spec_Agile_AEAD_AES128_GCM, &ctx, key.data());
if (res != EverCrypt_Error_Success) {
state.SkipWithError("Could not allocate AEAD state.");
break;
}
EverCrypt_AEAD_encrypt(ctx,
nonce.data(),
nonce.size(),
NULL,
0,
plaintext.data(),
plaintext.size(),
ciphertext.data(),
mac.data());
EverCrypt_AEAD_free(ctx);
}
}
BENCHMARK(EverCrypt_AES128_GCM_encrypt)->Setup(DoSetup)->Apply(Range);
static void
EverCrypt_AES128_GCM_aad(benchmark::State& state)
{
bytes aad(state.range(0), 0x37);
for (auto _ : state) {
EverCrypt_AEAD_state_s* ctx;
EverCrypt_Error_error_code res = EverCrypt_AEAD_create_in(
Spec_Agile_AEAD_AES128_GCM, &ctx, key.data());
if (res != EverCrypt_Error_Success) {
state.SkipWithError("Could not allocate AEAD state.");
break;
}
EverCrypt_AEAD_encrypt(ctx,
nonce.data(),
nonce.size(),
aad.data(),
aad.size(),
NULL,
0,
NULL,
mac.data());
EverCrypt_AEAD_free(ctx);
}
}
BENCHMARK(EverCrypt_AES128_GCM_aad)->Setup(DoSetup)->Apply(Range);
#ifndef NO_OPENSSL
static void
OpenSSL_aes_128_gcm_encrypt(benchmark::State& state)
{
bytes plaintext(state.range(0), 0x37);
bytes ciphertext(state.range(0), 0);
for (auto _ : state) {
int out_len, unused_len;
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
int result = EVP_EncryptInit_ex2(
ctx, EVP_aes_128_gcm(), key.data(), nonce.data(), NULL);
if (result != 1) {
state.SkipWithError("");
EVP_CIPHER_CTX_free(ctx);
break;
}
result = EVP_EncryptUpdate(
ctx, ciphertext.data(), &out_len, plaintext.data(), plaintext.size());
if (result != 1) {
state.SkipWithError("");
EVP_CIPHER_CTX_free(ctx);
break;
}
result = EVP_EncryptFinal_ex(ctx, mac.data(), &unused_len);
if (result != 1 || unused_len != 0) {
state.SkipWithError("");
EVP_CIPHER_CTX_free(ctx);
break;
}
EVP_CIPHER_CTX_free(ctx);
}
}
BENCHMARK(OpenSSL_aes_128_gcm_encrypt)->Setup(DoSetup)->Apply(Range);
static void
OpenSSL_aes_128_gcm_aad(benchmark::State& state)
{
bytes aad(state.range(0), 0x37);
for (auto _ : state) {
int out_len, unused_len;
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
int result = EVP_EncryptInit_ex2(
ctx, EVP_aes_128_gcm(), key.data(), nonce.data(), NULL);
if (result != 1) {
state.SkipWithError("");
EVP_CIPHER_CTX_free(ctx);
break;
}
result = EVP_EncryptUpdate(
ctx, NULL, &out_len, aad.data(), aad.size());
if (result != 1) {
state.SkipWithError("");
EVP_CIPHER_CTX_free(ctx);
break;
}
result = EVP_EncryptFinal_ex(ctx, mac.data(), &unused_len);
if (result != 1 || unused_len != 0) {
state.SkipWithError("");
EVP_CIPHER_CTX_free(ctx);
break;
}
EVP_CIPHER_CTX_free(ctx);
}
}
BENCHMARK(OpenSSL_aes_128_gcm_aad)->Setup(DoSetup)->Apply(Range);
#endif
static void
BearSSL_CT64_AES128_GCM_encrypt(benchmark::State& state)
{
bytes plaintext(state.range(0), 0x37);
for (auto _ : state) {
br_aes_ct64_ctr_keys bc;
br_gcm_context gc;
br_aes_ct64_ctr_init(&bc, key.data(), key.size());
br_gcm_init(&gc, &bc.vtable, br_ghash_ctmul64);
br_gcm_reset(&gc, nonce.data(), nonce.size());
br_gcm_flip(&gc);
br_gcm_run(&gc, 1, plaintext.data(), plaintext.size());
br_gcm_get_tag(&gc, mac.data());
}
}
BENCHMARK(BearSSL_CT64_AES128_GCM_encrypt)->Setup(DoSetup)->Apply(Range);
static void
BearSSL_CT64_AES128_GCM_aad(benchmark::State& state)
{
bytes aad(state.range(0), 0x37);
for (auto _ : state) {
br_aes_ct64_ctr_keys bc;
br_gcm_context gc;
br_aes_ct64_ctr_init(&bc, key.data(), key.size());
br_gcm_init(&gc, &bc.vtable, br_ghash_ctmul64);
br_gcm_reset(&gc, nonce.data(), nonce.size());
br_gcm_aad_inject(&gc, aad.data(), aad.size());
br_gcm_get_tag(&gc, mac.data());
}
}
BENCHMARK(BearSSL_CT64_AES128_GCM_aad)->Setup(DoSetup)->Apply(Range);
BENCHMARK_MAIN();