Skip to content

Commit

Permalink
fix ArrayToVector length type (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyBel authored Feb 18, 2022
1 parent a8070f0 commit e4ea9ae
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ ECB, CBC, CFB modes are supported.


# Padding
This library do not provide any padding because padding is not part of AES standard. Plaintext and ciphertext length in bytes must be divisible by 16. If length doesn't satisfy this condition exception will be thrown
This library does not provide any padding because padding is not part of AES standard. Plaintext and ciphertext length in bytes must be divisible by 16. If length doesn't satisfy this condition exception will be thrown


# Links
Expand Down Expand Up @@ -70,6 +70,6 @@ Build commands:
* `make test` - run tests
* `make debug` - run debug version
* `make profile` - run profile version
* `make speedtest` - run performance speed test
* `make speed_test` - run performance speed test
* `make release` - run `release` version
* `make clean` - clean `bin` directory
2 changes: 1 addition & 1 deletion src/AES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ void AES::printHexVector (std::vector<unsigned char> a)
}
}

std::vector<unsigned char> AES::ArrayToVector(unsigned char *a, unsigned char len)
std::vector<unsigned char> AES::ArrayToVector(unsigned char *a, unsigned int len)
{
std::vector<unsigned char> v(a, a + len * sizeof(unsigned char));
return v;
Expand Down
2 changes: 1 addition & 1 deletion src/AES.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class AES

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

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

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

Expand Down
43 changes: 41 additions & 2 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ TEST(KeyLengths, KeyLength256)



TEST(ECB, EncryptDecrypt)
TEST(ECB, EncryptDecryptOneBlock)
{
AES aes(AESKeyLength::AES_256);
unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
Expand All @@ -60,7 +60,7 @@ TEST(ECB, EncryptDecrypt)
delete[] innew;
}

TEST(ECB, EncryptDecryptVector)
TEST(ECB, EncryptDecryptVectorOneBlock)
{
AES aes(AESKeyLength::AES_256);
std::vector<unsigned char> plain = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
Expand Down Expand Up @@ -366,6 +366,45 @@ TEST(CFB, DecryptTwoBlocksVector)
}


TEST(LongData, EncryptDecryptOneKb)
{
AES aes(AESKeyLength::AES_256);
unsigned int kbSize = 1024 * sizeof(unsigned char);
unsigned char* plain = new unsigned char[kbSize];
for (unsigned int i = 0; i < kbSize; i++) {
plain[i] = i % 256;
}

unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };

unsigned char *out = aes.EncryptECB(plain, kbSize, key);
unsigned char *innew = aes.DecryptECB(out, kbSize, key);
ASSERT_FALSE(memcmp(innew, plain, kbSize));
delete[] plain;
delete[] out;
delete[] innew;
}

TEST(LongData, EncryptDecryptVectorOneKb)
{
AES aes(AESKeyLength::AES_256);
unsigned int kbSize = 1024 * sizeof(unsigned char);
std::vector<unsigned char> plain(kbSize);
for (unsigned int i = 0; i < kbSize; i++) {
plain[i] = i % 256;
}


std::vector<unsigned char> key = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };

std::vector<unsigned char> out = aes.EncryptECB(plain, key);
std::vector<unsigned char> innew = aes.DecryptECB(out, key);
ASSERT_EQ(innew, plain);
}


int main(int argc, char *argv[])
{
::testing::InitGoogleTest(&argc, argv);
Expand Down

0 comments on commit e4ea9ae

Please sign in to comment.