Skip to content

Commit

Permalink
Merge pull request #41 from SergeyBel/add-vector-support
Browse files Browse the repository at this point in the history
Add vector support
  • Loading branch information
SergeyBel authored Nov 14, 2021
2 parents 6c4a3e3 + 1412f57 commit b3772e4
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 28 deletions.
26 changes: 19 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
FLAGS = -Wall -Wextra

all: clean build_test build_debug build_profile build_release
build_all: clean build_test build_debug build_profile build_release

build_test:
g++ $(FLAGS) -g -pthread ./src/AES.cpp ./tests/tests.cpp /usr/lib/libgtest.a -o bin/test
docker-compose exec aes g++ $(FLAGS) -g -pthread ./src/AES.cpp ./tests/tests.cpp /usr/lib/libgtest.a -o bin/test

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

build_profile:
g++ $(FLAGS) -pg ./src/AES.cpp ./dev/main.cpp -o bin/profile
docker-compose exec aes g++ $(FLAGS) -pg ./src/AES.cpp ./dev/main.cpp -o bin/profile

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

test:
docker-compose exec aes bin/test

debug:
docker-compose exec aes bin/debug

profile:
docker-compose exec aes bin/profile

release:
docker-compose exec aes bin/release

clean:
rm -rf bin
mkdir bin -p
docker-compose exec aes rm -rf bin
docker-compose exec aes mkdir bin -p
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ c = aes.EncryptECB(plain, plainLen, key, outLen);
//now variable c contains outLen bytes - ciphertext
...
```
Or for vectors:
```c++
...
vector<unsigned char> plain = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; //plaintext example
vector<unsigned char> key = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; //key example
AES aes(128);
c = aes.EncryptECB(plain, key);
//now vector c contains ciphertext
...
```
ECB, CBC, CFB modes are supported.


Expand All @@ -33,7 +46,7 @@ http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197.pdf
1. `git clone https://github.com/SergeyBel/AES.git`
1. `docker-compose build`
1. `docker-compose up -d`
1. `docker-compose exec aes bash`
1. use make commands

There are four executables in `bin` folder:
* `test` - run tests
Expand All @@ -43,9 +56,13 @@ There are four executables in `bin` folder:


Build commands:
* `make all` - build all targets
* `make build_all` - build all targets
* `make build_test` - build `test` target
* `make build_debug` - build `debug` target
* `make build_profile` - build `profile` target
* `make build_release` - build `release` target
* `make test` - run tests
* `make debug` - run debug version
* `make profile` - run profile version
* `make release` - run `release` version
* `make clean` - clean `bin` directory
67 changes: 67 additions & 0 deletions src/AES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ AES::AES(int keyLen)
blockBytesLen = 4 * this->Nb * sizeof(unsigned char);
}


unsigned char * AES::EncryptECB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned int &outLen)
{
outLen = GetPaddingLength(inLen);
Expand Down Expand Up @@ -524,9 +525,75 @@ void AES::printHexArray (unsigned char a[], unsigned int n)
}
}

void AES::printHexVector (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)
{
vector<unsigned char> v(a, a + len * sizeof(unsigned char));
return v;
}

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


vector<unsigned char> AES::EncryptECB(vector<unsigned char> in, 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);
delete []out;
return v;
}

vector<unsigned char> AES::DecryptECB(vector<unsigned char> in, 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());
delete []out;
return v;
}


vector<unsigned char> AES::EncryptCBC(vector<unsigned char> in, vector<unsigned char> key, 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);
delete [] out;
return v;
}

vector<unsigned char> AES::DecryptCBC(vector<unsigned char> in, vector<unsigned char> key, 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());
delete [] out;
return v;
}

vector<unsigned char> AES::EncryptCFB(vector<unsigned char> in, vector<unsigned char> key, 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);
delete [] out;
return v;
}

vector<unsigned char> AES::DecryptCFB(vector<unsigned char> in, vector<unsigned char> key, 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());
delete [] out;
return v;

}

38 changes: 29 additions & 9 deletions src/AES.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#ifndef _AES_H_
#define _AES_H_

#include<cstring>
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

Expand Down Expand Up @@ -58,24 +59,43 @@ 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);

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

public:
AES(int keyLen = 256);

unsigned char *EncryptECB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned int &outLen);
unsigned char *EncryptECB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned int &outLen);

unsigned char *DecryptECB(unsigned char in[], unsigned int inLen, unsigned char key[]);
unsigned char *DecryptECB(unsigned char in[], unsigned int inLen, unsigned char key[]);

unsigned char *EncryptCBC(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv, unsigned int &outLen);
unsigned char *EncryptCBC(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char *iv, unsigned int &outLen);

unsigned char *DecryptCBC(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv);
unsigned char *DecryptCBC(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char *iv);

unsigned char *EncryptCFB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv, unsigned int &outLen);
unsigned char *EncryptCFB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char *iv, unsigned int &outLen);

unsigned char *DecryptCFB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char *iv);



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

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

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

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

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

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

unsigned char *DecryptCFB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv);

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

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

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

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

0 comments on commit b3772e4

Please sign in to comment.