AES done
This commit is contained in:
parent
d7785ed520
commit
f15cc5c838
|
@ -1,2 +1,2 @@
|
||||||
test: aes.cpp test.cpp
|
test: aes_4.cpp aes_5a.cpp aes_5b.cpp aes_6.cpp test.cpp
|
||||||
g++ test.cpp -lcrypto
|
g++ test.cpp -lcrypto -march=native -maes -msse2 -msse -Ofast
|
|
@ -11,7 +11,7 @@ http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* AES Constants */
|
/* AES Constants */
|
||||||
|
namespace aes128_4{
|
||||||
// forward sbox
|
// forward sbox
|
||||||
const uint8_t SBOX[256] = {
|
const uint8_t SBOX[256] = {
|
||||||
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
|
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
|
||||||
|
@ -208,3 +208,4 @@ void aes128_4(uint8_t *in, uint8_t *out, uint32_t * expKey) {
|
||||||
out[15] = wbyte(state[3], 3);
|
out[15] = wbyte(state[3], 3);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
|
@ -1,10 +1,8 @@
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <immintrin.h>
|
#include <immintrin.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Author: Ondrej Hladuvka, hladuond@fit.cvut.cz
|
Author: Ondrej Hladuvka, hladuond@fit.cvut.cz
|
||||||
Template: Jiri Bucek 2017
|
|
||||||
AES specification:
|
AES specification:
|
||||||
http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
|
http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -16,9 +16,8 @@
|
||||||
constexpr size_t blockSize = 16;
|
constexpr size_t blockSize = 16;
|
||||||
constexpr size_t keySize = 16;
|
constexpr size_t keySize = 16;
|
||||||
|
|
||||||
// Test constants
|
size_t numTests = 1'000'000;
|
||||||
constexpr size_t numTests = 1'000'000;
|
size_t payloadSize = numTests * blockSize;
|
||||||
constexpr size_t payloadSize = numTests * blockSize;
|
|
||||||
|
|
||||||
using TestTimeUnit = std::milli;
|
using TestTimeUnit = std::milli;
|
||||||
using CycleTimeUnit = std::nano;
|
using CycleTimeUnit = std::nano;
|
||||||
|
@ -32,7 +31,6 @@ void xor_into_128bit_u(uint8_t *a, uint8_t *b) {
|
||||||
_mm_storeu_si128((__m128i*)a, vec_result);
|
_mm_storeu_si128((__m128i*)a, vec_result);
|
||||||
}
|
}
|
||||||
|
|
||||||
// aligned version
|
|
||||||
void xor_128bit(__m128i *a, __m128i *b, __m128i *c) {
|
void xor_128bit(__m128i *a, __m128i *b, __m128i *c) {
|
||||||
__m128i vec_a = _mm_load_si128(a);
|
__m128i vec_a = _mm_load_si128(a);
|
||||||
__m128i vec_b = _mm_load_si128(b);
|
__m128i vec_b = _mm_load_si128(b);
|
||||||
|
@ -48,8 +46,7 @@ void mov_128bit(__m128i *a, __m128i *b) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void test(void (*aes)(uint8_t *in, uint8_t *out, uint32_t *expKey), uint8_t *in, uint8_t *refOut,
|
void test(void (*aes)(uint8_t *in, uint8_t *out, uint32_t *expKey), uint8_t *in, uint8_t *refOut,
|
||||||
uint32_t *expandedKey, uint8_t *iv,
|
uint32_t *expandedKey, uint8_t *iv, const std::string& name) {
|
||||||
const std::string& name) {
|
|
||||||
std::cout << "\n\ntesting: " << name << '\n';
|
std::cout << "\n\ntesting: " << name << '\n';
|
||||||
|
|
||||||
uint8_t* tmpBlock(static_cast<uint8_t*>(std::aligned_alloc(blockSize, blockSize)));
|
uint8_t* tmpBlock(static_cast<uint8_t*>(std::aligned_alloc(blockSize, blockSize)));
|
||||||
|
@ -73,26 +70,37 @@ void test(void (*aes)(uint8_t *in, uint8_t *out, uint32_t *expKey), uint8_t *in,
|
||||||
std::chrono::duration<double, CycleTimeUnit> time = end - start;
|
std::chrono::duration<double, CycleTimeUnit> time = end - start;
|
||||||
double timeAVG = time.count() / numTests;
|
double timeAVG = time.count() / numTests;
|
||||||
|
|
||||||
std::cout << "time: " << time.count()/std::ratio_divide<CycleTimeUnit, TestTimeUnit>::den << "ms\navg time: " << timeAVG << "ns\navg cpu cycles: " << cycles/numTests << std::endl;
|
std::cout << "total time: " << time.count()/std::ratio_divide<CycleTimeUnit, TestTimeUnit>::den << "ms\n"
|
||||||
|
<< "avg time pro block: " << timeAVG << "ns\n"
|
||||||
|
<< "avg cpu cycles per block: " << cycles/numTests << std::endl;
|
||||||
std::free(tmpBlock);
|
std::free(tmpBlock);
|
||||||
std::free(outBuf);
|
std::free(outBuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main(int argc, char *argv[]) {
|
||||||
uint8_t key[keySize];
|
uint8_t key[keySize];
|
||||||
uint8_t iv[blockSize];
|
uint8_t iv[blockSize];
|
||||||
uint32_t expandedKey[44];
|
uint32_t expandedKey[44];
|
||||||
AES_KEY opensslKey;
|
AES_KEY opensslKey;
|
||||||
|
|
||||||
uint8_t* input(static_cast<uint8_t*>(std::aligned_alloc(blockSize, payloadSize)));
|
if (argc > 1) {
|
||||||
|
numTests = strtoull(argv[1], nullptr, 10);
|
||||||
|
if ((int64_t)numTests < 1) {
|
||||||
|
std::cout << "ivalid param" << std::endl;
|
||||||
|
__builtin_trap();
|
||||||
|
}
|
||||||
|
payloadSize = numTests * blockSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t* input( static_cast<uint8_t*>(std::aligned_alloc(blockSize, payloadSize)));
|
||||||
uint8_t* opensslOutput(static_cast<uint8_t*>(std::aligned_alloc(blockSize, payloadSize)));
|
uint8_t* opensslOutput(static_cast<uint8_t*>(std::aligned_alloc(blockSize, payloadSize)));
|
||||||
|
|
||||||
RAND_bytes(key, keySize);
|
RAND_bytes(key, keySize);
|
||||||
RAND_bytes(iv, blockSize);
|
RAND_bytes(iv, blockSize);
|
||||||
RAND_bytes(input, payloadSize);
|
RAND_bytes(input, payloadSize);
|
||||||
|
|
||||||
// OpenSSL
|
// OpenSSL beg #############################################################################
|
||||||
std::cout << "testing: OpenSSL\n";
|
std::cout << "measuring reference: OpenSSL\n";
|
||||||
#pragma GCC diagnostic push
|
#pragma GCC diagnostic push
|
||||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||||
AES_set_encrypt_key(key, 128, &opensslKey);
|
AES_set_encrypt_key(key, 128, &opensslKey);
|
||||||
|
@ -103,7 +111,7 @@ int main() {
|
||||||
for (int test = 0; test < numTests; ++test) {
|
for (int test = 0; test < numTests; ++test) {
|
||||||
AES_encrypt(tmpBlock, tmpBlock, &opensslKey);
|
AES_encrypt(tmpBlock, tmpBlock, &opensslKey);
|
||||||
xor_128bit(reinterpret_cast<__m128i*>(tmpBlock),
|
xor_128bit(reinterpret_cast<__m128i*>(tmpBlock),
|
||||||
reinterpret_cast<__m128i*>(input + blockSize * test),
|
reinterpret_cast<__m128i*>(input + blockSize * test),
|
||||||
reinterpret_cast<__m128i*>(opensslOutput + blockSize * test));
|
reinterpret_cast<__m128i*>(opensslOutput + blockSize * test));
|
||||||
}
|
}
|
||||||
opensslCycles = __rdtsc() - opensslCycles;
|
opensslCycles = __rdtsc() - opensslCycles;
|
||||||
|
@ -113,16 +121,17 @@ int main() {
|
||||||
std::cout << "time: " << opensslTime.count()/std::ratio_divide<CycleTimeUnit, TestTimeUnit>::den << "ms\navg time: " << timeAVG << "ns\navg cycles: " << opensslCycles/numTests << std::endl;
|
std::cout << "time: " << opensslTime.count()/std::ratio_divide<CycleTimeUnit, TestTimeUnit>::den << "ms\navg time: " << timeAVG << "ns\navg cycles: " << opensslCycles/numTests << std::endl;
|
||||||
std::free(tmpBlock);
|
std::free(tmpBlock);
|
||||||
#pragma GCC diagnostic pop
|
#pragma GCC diagnostic pop
|
||||||
|
// OpenSSL end #############################################################################
|
||||||
expandKey(key, expandedKey);
|
|
||||||
|
|
||||||
test(aes128_4, input, opensslOutput, expandedKey, iv, "My original implementation (4)");
|
aes128_4::expandKey(key, expandedKey);
|
||||||
|
|
||||||
|
test(aes128_4::aes128_4, input, opensslOutput, expandedKey, iv, "Naive implementation (4)");
|
||||||
test(aes128_5a::aes128_5, input, opensslOutput, expandedKey, iv, "With macro (5a)");
|
test(aes128_5a::aes128_5, input, opensslOutput, expandedKey, iv, "With macro (5a)");
|
||||||
test(aes128_5b::aes128_5, input, opensslOutput, expandedKey, iv, "With T-Box (5b)");
|
test(aes128_5b::aes128_5, input, opensslOutput, expandedKey, iv, "With T-Box (5b)");
|
||||||
|
|
||||||
__m128i expandedKey_128[10];
|
__m128i expandedKey_128[10];
|
||||||
aes128_6::expandKey(key, expandedKey_128);
|
aes128_6::expandKey(key, expandedKey_128);
|
||||||
test(aes128_6::aes128_6, input, opensslOutput, (uint32_t *)expandedKey_128, iv, "asm (6)");
|
test(aes128_6::aes128_6, input, opensslOutput, (uint32_t *)expandedKey_128, iv, "Intrinsics (6)");
|
||||||
|
|
||||||
std::free(input);
|
std::free(input);
|
||||||
std::free(opensslOutput);
|
std::free(opensslOutput);
|
||||||
|
|
Loading…
Reference in New Issue