-
Notifications
You must be signed in to change notification settings - Fork 18
Add support for Hacl_AES_128_GCM_NI and Hacl_AES_128_GCM_M32 #418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 4 commits
0b35736
28e382e
5d40ece
bcf5962
ff14158
d67a8b4
d93469e
720a2a7
e2db26a
4b5f3ed
4abdd02
943cad6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,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)); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There needs to be an alloc function such that the caller/user doesn't need to know the magic numbers here.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't think of a convenient way to add such function. I don't seem to be able to alloc/dealloc memory buffers at Low*-level, these functionalities are specific to this library which only utilizes NI and CT64 function from inside EverCrypt functions that the user are supposed to use instead.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can look at other alloc functions like hacl-packages/src/Hacl_Hash_SHA3.c Line 175 in 81303b8
hacl-packages/src/Hacl_Hash_Blake2s_128.c Line 480 in 81303b8
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I implemented malloc/free for AES_GCM context on hacl side mamonet/hacl-star@c9880f1 |
||||||
| 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); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly to the alloc, do we need a free function here. |
||||||
| } | ||||||
| } | ||||||
|
|
||||||
| 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(); | ||||||
Uh oh!
There was an error while loading. Please reload this page.