Skip to content

Commit

Permalink
Add speedtest (#52)
Browse files Browse the repository at this point in the history
* add speed test
* delete using namespace std
  • Loading branch information
SergeyBel authored Jan 5, 2022
1 parent 4e5595f commit be9a93d
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 80 deletions.
14 changes: 10 additions & 4 deletions .github/workflows/aes-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ jobs:
sudo make
sudo cp lib/*.a /usr/lib
- name: Build
- name: Build test
run: |
mkdir bin
make workflow_build
make workflow_build_test
- name: Test
- name: Run test
run: ./bin/test

- name: Build speed test
run: |
make workflow_build_speed_test
- name: Run speed test
run: ./bin/speedtest



20 changes: 15 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FLAGS = -Wall -Wextra

build_all: clean build_test build_debug build_profile build_release
build_all: clean build_test build_debug build_profile build_release build_speed_test

build_test:
docker-compose exec aes g++ $(FLAGS) -g -pthread ./src/AES.cpp ./tests/tests.cpp /usr/lib/libgtest.a -o bin/test
Expand All @@ -11,9 +11,14 @@ build_debug:
build_profile:
docker-compose exec aes g++ $(FLAGS) -pg ./src/AES.cpp ./dev/main.cpp -o bin/profile

build_speed_test:
docker-compose exec aes g++ $(FLAGS) -O2 ./src/AES.cpp ./speedtest/main.cpp -o bin/speedtest

build_release:
docker-compose exec aes g++ $(FLAGS) -O2 ./src/AES.cpp ./dev/main.cpp -o bin/release



test:
docker-compose exec aes bin/test

Expand All @@ -26,11 +31,16 @@ profile:
release:
docker-compose exec aes bin/release

speed_test:
docker-compose exec aes bin/speedtest

clean:
docker-compose exec aes rm -rf bin
docker-compose exec aes mkdir bin -p
docker-compose exec aes rm -rf bin/*


workflow_build_test:
g++ $(FLAGS) -g -pthread ./src/AES.cpp ./tests/tests.cpp /usr/lib/libgtest.a -o bin/test

workflow_build:
g++ $(FLAGS) -g -pthread ./src/AES.cpp ./tests/tests.cpp /usr/lib/libgtest.a -o bin/test
workflow_build_speed_test:
g++ $(FLAGS) -O2 ./src/AES.cpp ./speedtest/main.cpp -o bin/speedtest

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ There are four executables in `bin` folder:
* `test` - run tests
* `debug` - version for debugging (main code will be taken from dev/main.cpp)
* `profile` - version for profiling with gprof (main code will be taken from dev/main.cpp)
* `speedtest` - performance speed test (main code will be taken from speedtest/main.cpp)
* `release` - version with optimization (main code will be taken from dev/main.cpp)


Expand All @@ -58,9 +59,11 @@ Build commands:
* `make build_test` - build `test` target
* `make build_debug` - build `debug` target
* `make build_profile` - build `profile` target
* `make build_speed_test` - build `speedtest` target
* `make build_release` - build `release` target
* `make test` - run tests
* `make debug` - run debug version
* `make profile` - run profile version
* `make speedtest` - run performance speed test
* `make release` - run `release` version
* `make clean` - clean `bin` directory
Empty file added bin/.gitkeep
Empty file.
3 changes: 1 addition & 2 deletions dev/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include <iostream>
#include "../src/AES.h"
using namespace std;

int main()
{
cout << "Aes dev" << endl;
std::cout << "Aes dev" << std::endl;
return 0;
}
52 changes: 52 additions & 0 deletions speedtest/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <iostream>
#include <sys/time.h>
#include <ctime>
#include "../src/AES.h"

const unsigned int MICROSECONDS = 1000000;
unsigned long getMicroseconds()
{
struct timeval tv;
gettimeofday(&tv,NULL);
return MICROSECONDS * tv.tv_sec + tv.tv_usec;
}

unsigned char * getRandomPlain(unsigned int length)
{
unsigned char *plain = new unsigned char[length];
for (unsigned int i = 0; i < length; i++) {
plain[i] = rand() % 256;
}

return plain;

}

int main()
{
const unsigned int MEGABYTE = 1024 * 1024 * sizeof(unsigned char);

unsigned int len = 0;
unsigned int megabytesCount = 10;
unsigned int plainLength = megabytesCount * MEGABYTE;
unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };

std::cout << "Start speedtest" << std::endl;
srand(std::time(nullptr));

unsigned char *plain = getRandomPlain(plainLength);

AES aes(AESKeyLength::AES_256);
unsigned long start = getMicroseconds();
unsigned char *out = aes.EncryptECB(plain, plainLength, key, len);
unsigned long delta = getMicroseconds() - start;

double speed = (double)megabytesCount / delta * MICROSECONDS;

printf("%.2f Mb/s\n", speed);

delete[] plain;
delete[] out;

return 0;
}
32 changes: 16 additions & 16 deletions src/AES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,73 +486,73 @@ void AES::printHexArray (unsigned char a[], unsigned int n)
}
}

void AES::printHexVector (vector<unsigned char> a)
void AES::printHexVector (std::vector<unsigned char> a)
{
for (unsigned int i = 0; i < a.size(); i++) {
printf("%02x ", a[i]);
}
}

vector<unsigned char> AES::ArrayToVector(unsigned char *a, unsigned char len)
std::vector<unsigned char> AES::ArrayToVector(unsigned char *a, unsigned char len)
{
vector<unsigned char> v(a, a + len * sizeof(unsigned char));
std::vector<unsigned char> v(a, a + len * sizeof(unsigned char));
return v;
}

unsigned char *AES::VectorToArray(vector<unsigned char> a)
unsigned char *AES::VectorToArray(std::vector<unsigned char> a)
{
return a.data();
}


vector<unsigned char> AES::EncryptECB(vector<unsigned char> in, vector<unsigned char> key)
std::vector<unsigned char> AES::EncryptECB(std::vector<unsigned char> in, std::vector<unsigned char> key)
{
unsigned int outLen = 0;;
unsigned char *out = EncryptECB(VectorToArray(in), (unsigned int)in.size(), VectorToArray(key), outLen);
vector<unsigned char> v = ArrayToVector(out, outLen);
std::vector<unsigned char> v = ArrayToVector(out, outLen);
delete []out;
return v;
}

vector<unsigned char> AES::DecryptECB(vector<unsigned char> in, vector<unsigned char> key)
std::vector<unsigned char> AES::DecryptECB(std::vector<unsigned char> in, std::vector<unsigned char> key)
{
unsigned char *out = DecryptECB(VectorToArray(in), (unsigned int)in.size(), VectorToArray(key));
vector<unsigned char> v = ArrayToVector(out, (unsigned int)in.size());
std::vector<unsigned char> v = ArrayToVector(out, (unsigned int)in.size());
delete []out;
return v;
}


vector<unsigned char> AES::EncryptCBC(vector<unsigned char> in, vector<unsigned char> key, vector<unsigned char> iv)
std::vector<unsigned char> AES::EncryptCBC(std::vector<unsigned char> in, std::vector<unsigned char> key, std::vector<unsigned char> iv)
{
unsigned int outLen = 0;
unsigned char *out = EncryptCBC(VectorToArray(in), (unsigned int)in.size(), VectorToArray(key), VectorToArray(iv), outLen);
vector<unsigned char> v = ArrayToVector(out, outLen);
std::vector<unsigned char> v = ArrayToVector(out, outLen);
delete [] out;
return v;
}

vector<unsigned char> AES::DecryptCBC(vector<unsigned char> in, vector<unsigned char> key, vector<unsigned char> iv)
std::vector<unsigned char> AES::DecryptCBC(std::vector<unsigned char> in, std::vector<unsigned char> key, std::vector<unsigned char> iv)
{
unsigned char *out = DecryptCBC(VectorToArray(in), (unsigned int)in.size(), VectorToArray(key), VectorToArray(iv));
vector<unsigned char> v = ArrayToVector(out, (unsigned int)in.size());
std::vector<unsigned char> v = ArrayToVector(out, (unsigned int)in.size());
delete [] out;
return v;
}

vector<unsigned char> AES::EncryptCFB(vector<unsigned char> in, vector<unsigned char> key, vector<unsigned char> iv)
std::vector<unsigned char> AES::EncryptCFB(std::vector<unsigned char> in, std::vector<unsigned char> key, std::vector<unsigned char> iv)
{
unsigned int outLen = 0;
unsigned char *out = EncryptCFB(VectorToArray(in), (unsigned int)in.size(), VectorToArray(key), VectorToArray(iv), outLen);
vector<unsigned char> v = ArrayToVector(out, outLen);
std::vector<unsigned char> v = ArrayToVector(out, outLen);
delete [] out;
return v;
}

vector<unsigned char> AES::DecryptCFB(vector<unsigned char> in, vector<unsigned char> key, vector<unsigned char> iv)
std::vector<unsigned char> AES::DecryptCFB(std::vector<unsigned char> in, std::vector<unsigned char> key, std::vector<unsigned char> iv)
{
unsigned char *out = DecryptCFB(VectorToArray(in), (unsigned int)in.size(), VectorToArray(key), VectorToArray(iv));
vector<unsigned char> v = ArrayToVector(out, (unsigned int)in.size());
std::vector<unsigned char> v = ArrayToVector(out, (unsigned int)in.size());
delete [] out;
return v;

Expand Down
20 changes: 9 additions & 11 deletions src/AES.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include <stdio.h>
#include <vector>

using namespace std;

enum class AESKeyLength {
AES_128,
AES_192,
Expand Down Expand Up @@ -61,9 +59,9 @@ class AES

void XorBlocks(unsigned char *a, unsigned char * b, unsigned char *c, unsigned int len);

vector<unsigned char> ArrayToVector(unsigned char *a, unsigned char len);
std::vector<unsigned char> ArrayToVector(unsigned char *a, unsigned char len);

unsigned char *VectorToArray(vector<unsigned char> a);
unsigned char *VectorToArray(std::vector<unsigned char> a);

public:
explicit AES(AESKeyLength keyLength = AESKeyLength::AES_256);
Expand All @@ -82,22 +80,22 @@ class AES



vector<unsigned char> EncryptECB(vector<unsigned char> in, vector<unsigned char> key);
std::vector<unsigned char> EncryptECB(std::vector<unsigned char> in, std::vector<unsigned char> key);

vector<unsigned char> DecryptECB(vector<unsigned char> in, vector<unsigned char> key);
std::vector<unsigned char> DecryptECB(std::vector<unsigned char> in, std::vector<unsigned char> key);

vector<unsigned char> EncryptCBC(vector<unsigned char> in, vector<unsigned char> key, vector<unsigned char> iv);
std::vector<unsigned char> EncryptCBC(std::vector<unsigned char> in, std::vector<unsigned char> key, std::vector<unsigned char> iv);

vector<unsigned char> DecryptCBC(vector<unsigned char> in, vector<unsigned char> key, vector<unsigned char> iv);
std::vector<unsigned char> DecryptCBC(std::vector<unsigned char> in, std::vector<unsigned char> key, std::vector<unsigned char> iv);

vector<unsigned char> EncryptCFB(vector<unsigned char> in, vector<unsigned char> key, vector<unsigned char> iv);
std::vector<unsigned char> EncryptCFB(std::vector<unsigned char> in, std::vector<unsigned char> key, std::vector<unsigned char> iv);

vector<unsigned char> DecryptCFB(vector<unsigned char> in, vector<unsigned char> key, vector<unsigned char> iv);
std::vector<unsigned char> DecryptCFB(std::vector<unsigned char> in, std::vector<unsigned char> key, std::vector<unsigned char> iv);


void printHexArray(unsigned char a[], unsigned int n);

void printHexVector(vector<unsigned char> a);
void printHexVector(std::vector<unsigned char> a);
};

const unsigned char sbox[16][16] = {
Expand Down
Loading

0 comments on commit be9a93d

Please sign in to comment.