diff --git a/lab04-06_AES_PC/Makefile b/lab04-06_AES_PC/Makefile index c05bb08..9dce7ba 100644 --- a/lab04-06_AES_PC/Makefile +++ b/lab04-06_AES_PC/Makefile @@ -1,2 +1,2 @@ -test: aes.cpp test.cpp - g++ test.cpp -lcrypto \ No newline at end of file +test: aes_4.cpp aes_5a.cpp aes_5b.cpp aes_6.cpp test.cpp + g++ test.cpp -lcrypto -march=native -maes -msse2 -msse -Ofast \ No newline at end of file diff --git a/lab04-06_AES_PC/aes_4.cpp b/lab04-06_AES_PC/aes_4.cpp index ef409e7..824402f 100644 --- a/lab04-06_AES_PC/aes_4.cpp +++ b/lab04-06_AES_PC/aes_4.cpp @@ -11,7 +11,7 @@ http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf */ /* AES Constants */ - +namespace aes128_4{ // forward sbox const uint8_t SBOX[256] = { // 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); } +} \ No newline at end of file diff --git a/lab04-06_AES_PC/aes_6.cpp b/lab04-06_AES_PC/aes_6.cpp index e3a4f52..8ac402b 100644 --- a/lab04-06_AES_PC/aes_6.cpp +++ b/lab04-06_AES_PC/aes_6.cpp @@ -1,10 +1,8 @@ -#include #include #include /* Author: Ondrej Hladuvka, hladuond@fit.cvut.cz -Template: Jiri Bucek 2017 AES specification: http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf */ diff --git a/lab04-06_AES_PC/test.cpp b/lab04-06_AES_PC/test.cpp index e6645b9..1fbd398 100644 --- a/lab04-06_AES_PC/test.cpp +++ b/lab04-06_AES_PC/test.cpp @@ -16,9 +16,8 @@ constexpr size_t blockSize = 16; constexpr size_t keySize = 16; -// Test constants -constexpr size_t numTests = 1'000'000; -constexpr size_t payloadSize = numTests * blockSize; +size_t numTests = 1'000'000; +size_t payloadSize = numTests * blockSize; using TestTimeUnit = std::milli; 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); } -// aligned version void xor_128bit(__m128i *a, __m128i *b, __m128i *c) { __m128i vec_a = _mm_load_si128(a); __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, - uint32_t *expandedKey, uint8_t *iv, - const std::string& name) { + uint32_t *expandedKey, uint8_t *iv, const std::string& name) { std::cout << "\n\ntesting: " << name << '\n'; uint8_t* tmpBlock(static_cast(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 time = end - start; double timeAVG = time.count() / numTests; - std::cout << "time: " << time.count()/std::ratio_divide::den << "ms\navg time: " << timeAVG << "ns\navg cpu cycles: " << cycles/numTests << std::endl; + std::cout << "total time: " << time.count()/std::ratio_divide::den << "ms\n" + << "avg time pro block: " << timeAVG << "ns\n" + << "avg cpu cycles per block: " << cycles/numTests << std::endl; std::free(tmpBlock); std::free(outBuf); } -int main() { +int main(int argc, char *argv[]) { uint8_t key[keySize]; uint8_t iv[blockSize]; uint32_t expandedKey[44]; AES_KEY opensslKey; - uint8_t* input(static_cast(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(std::aligned_alloc(blockSize, payloadSize))); uint8_t* opensslOutput(static_cast(std::aligned_alloc(blockSize, payloadSize))); RAND_bytes(key, keySize); RAND_bytes(iv, blockSize); RAND_bytes(input, payloadSize); - // OpenSSL - std::cout << "testing: OpenSSL\n"; + // OpenSSL beg ############################################################################# + std::cout << "measuring reference: OpenSSL\n"; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" AES_set_encrypt_key(key, 128, &opensslKey); @@ -103,7 +111,7 @@ int main() { for (int test = 0; test < numTests; ++test) { AES_encrypt(tmpBlock, tmpBlock, &opensslKey); xor_128bit(reinterpret_cast<__m128i*>(tmpBlock), - reinterpret_cast<__m128i*>(input + blockSize * test), + reinterpret_cast<__m128i*>(input + blockSize * test), reinterpret_cast<__m128i*>(opensslOutput + blockSize * test)); } opensslCycles = __rdtsc() - opensslCycles; @@ -113,16 +121,17 @@ int main() { std::cout << "time: " << opensslTime.count()/std::ratio_divide::den << "ms\navg time: " << timeAVG << "ns\navg cycles: " << opensslCycles/numTests << std::endl; std::free(tmpBlock); #pragma GCC diagnostic pop - - expandKey(key, expandedKey); + // OpenSSL end ############################################################################# - 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_5b::aes128_5, input, opensslOutput, expandedKey, iv, "With T-Box (5b)"); __m128i expandedKey_128[10]; 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(opensslOutput);