From 6706f9250779391005cfe0563330310a449c6e0e Mon Sep 17 00:00:00 2001 From: SergeyBel Date: Fri, 27 Sep 2019 22:58:49 +0300 Subject: [PATCH] Issue10 (#12) * padding tests * add print function --- AES.cpp | 20 +++++++++++-- AES.h | 10 +++++-- tests.cpp | 85 +++++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 88 insertions(+), 27 deletions(-) diff --git a/AES.cpp b/AES.cpp index cf25bd3..e79e250 100644 --- a/AES.cpp +++ b/AES.cpp @@ -139,14 +139,22 @@ unsigned char *AES::DecryptCFB(unsigned char in[], unsigned int inLen, unsigned unsigned char * AES::PaddingNulls(unsigned char in[], unsigned int inLen, unsigned int alignLen) { - unsigned char * alignIn = new unsigned char[alignLen]; + unsigned char *alignIn = new unsigned char[alignLen]; memcpy(alignIn, in, inLen); + memset(alignIn + inLen, 0x00, alignLen - inLen); return alignIn; } unsigned int AES::GetPaddingLength(unsigned int len) { - return (len / blockBytesLen) * blockBytesLen; + unsigned int lengthWithPadding = (len / blockBytesLen); + if (len % blockBytesLen) { + lengthWithPadding++; + } + + lengthWithPadding *= blockBytesLen; + + return lengthWithPadding; } void AES::EncryptBlock(unsigned char in[], unsigned char out[], unsigned char key[]) @@ -487,6 +495,14 @@ void AES::XorBlocks(unsigned char *a, unsigned char * b, unsigned char *c, unsig } } +void AES::printHexArray (unsigned char a[], unsigned int n) +{ + for (int i = 0; i < n; i++) { + printf("%02x ", a[i]); + } + + } + diff --git a/AES.h b/AES.h index 96ad6c8..e5bebca 100644 --- a/AES.h +++ b/AES.h @@ -1,9 +1,11 @@ #ifndef _AES_H_ #define _AES_H_ -#include +#include +#include +#include -using namespace std; +using namespace std; class AES { @@ -65,9 +67,11 @@ class AES unsigned char *DecryptCBC(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 *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, unsigned int &outLen); + + void printHexArray (unsigned char a[], unsigned int n); }; diff --git a/tests.cpp b/tests.cpp index 51dd326..fd280d7 100644 --- a/tests.cpp +++ b/tests.cpp @@ -2,16 +2,19 @@ #include #include "AES.h" -TEST(KeyLengths, KeyLength128) { +const unsigned int BLOCK_BYTES_LENGTH = 16 * sizeof(unsigned char); + +TEST(KeyLengths, KeyLength128) +{ AES aes(128); unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; unsigned char right[] = { 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30, 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a }; unsigned int len = 0; - unsigned char *out = aes.EncryptECB(plain, 16 * sizeof(unsigned char), key, len); + unsigned char *out = aes.EncryptECB(plain, 16, key, len); - ASSERT_FALSE(memcmp(right, out, 16 * sizeof(unsigned char))); - ASSERT_EQ(16, len); + ASSERT_FALSE(memcmp(right, out, BLOCK_BYTES_LENGTH)); + ASSERT_EQ(BLOCK_BYTES_LENGTH, len); delete[] out; } @@ -19,13 +22,13 @@ TEST(KeyLengths, KeyLength192) { AES aes(192); unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; - unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x011, + 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}; unsigned char right[] = {0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0, 0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91 }; unsigned int len; - unsigned char *out = aes.EncryptECB(plain, 16 * sizeof(unsigned char), key, len); - ASSERT_FALSE(memcmp(right, out, 16 * sizeof(unsigned char))); + unsigned char *out = aes.EncryptECB(plain, 16, key, len); + ASSERT_FALSE(memcmp(right, out, BLOCK_BYTES_LENGTH)); ASSERT_EQ(16, len); delete[] out; } @@ -35,13 +38,13 @@ TEST(KeyLengths, KeyLength256) { AES aes(256); unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; - unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x011, + 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 right[] = { 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf, 0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89 }; unsigned int len = 0; - unsigned char *out = aes.EncryptECB(plain, 16 * sizeof(unsigned char), key, len); - ASSERT_FALSE(memcmp(right, out, 16 * sizeof(unsigned char))); + unsigned char *out = aes.EncryptECB(plain, 16, key, len); + ASSERT_FALSE(memcmp(right, out, BLOCK_BYTES_LENGTH)); ASSERT_EQ(16, len); delete[] out; } @@ -51,29 +54,65 @@ TEST(ECB, EncryptDecrypt) AES aes(256); unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; - unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x011, + 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 int len = 0; - unsigned char *out = aes.EncryptECB(plain, 16 * sizeof(unsigned char), key, len); - unsigned char *innew = aes.DecryptECB(out, 16 * sizeof(unsigned char), key, len); - ASSERT_FALSE(memcmp(innew, plain, 16 * sizeof(unsigned char))); + unsigned char *out = aes.EncryptECB(plain, BLOCK_BYTES_LENGTH, key, len); + unsigned char *innew = aes.DecryptECB(out, BLOCK_BYTES_LENGTH, key, len); + ASSERT_FALSE(memcmp(innew, plain, BLOCK_BYTES_LENGTH)); delete[] out; delete[] innew; } +TEST(ECB, OneBlockWIthoutByteEncrypt) +{ + AES aes(128); + unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xaa}; + unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; + unsigned char right[] = { 0x7c, 0x99, 0xf4, 0x2b, 0x6e, 0xe5, 0x03, 0x30, 0x9c, 0x6c, 0x1a, 0x67, 0xe9, 0x7a, 0xc2, 0x42 }; + unsigned int len = 0; + unsigned char *out = aes.EncryptECB(plain, (BLOCK_BYTES_LENGTH - 1) * sizeof(unsigned char), key, len); + + + ASSERT_EQ(BLOCK_BYTES_LENGTH, len); + ASSERT_FALSE(memcmp(right, out, len)); + + delete[] out; +} + +TEST(ECB, OneBlockPlusOneByteEncrypt) +{ + AES aes(128); + unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0xaa}; + unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; + unsigned char right[] = { 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30, 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a, + 0x08, 0x69, 0xe8, 0x81, 0x8a, 0x81, 0x78, 0x9a, 0x72, 0x3d, 0x26, 0x16, 0x40, 0x0d, 0x31, 0xa9 + }; + unsigned int len = 0; + unsigned char *out = aes.EncryptECB(plain, (BLOCK_BYTES_LENGTH + 1) * sizeof(unsigned char), key, len); + + + ASSERT_EQ(2 * BLOCK_BYTES_LENGTH, len); + ASSERT_FALSE(memcmp(right, out, len)); + + delete[] out; +} + + + TEST(CBC, EncryptDecrypt) { AES aes(256); unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; unsigned char iv[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; - unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x011, + 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 int len; - unsigned char *out = aes.EncryptCBC(plain, 16 * sizeof(unsigned char), key, iv, len); - unsigned char *innew = aes.DecryptCBC(out, 16 * sizeof(unsigned char), key, iv, len); - ASSERT_FALSE(memcmp(innew, plain, 16 * sizeof(unsigned char))); + unsigned char *out = aes.EncryptCBC(plain, BLOCK_BYTES_LENGTH, key, iv, len); + unsigned char *innew = aes.DecryptCBC(out, BLOCK_BYTES_LENGTH, key, iv, len); + ASSERT_FALSE(memcmp(innew, plain, BLOCK_BYTES_LENGTH)); delete[] out; delete[] innew; } @@ -83,18 +122,20 @@ TEST(CFB, EncryptDecrypt) AES aes(256); unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; unsigned char iv[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; - unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x011, + 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 int len; - unsigned char *out = aes.EncryptCFB(plain, 16 * sizeof(unsigned char), key, iv, len); - unsigned char *innew = aes.DecryptCFB(out, 16 * sizeof(unsigned char), key, iv, len); - ASSERT_FALSE(memcmp(innew, plain, 16 * sizeof(unsigned char))); + unsigned char *out = aes.EncryptCFB(plain, BLOCK_BYTES_LENGTH, key, iv, len); + unsigned char *innew = aes.DecryptCFB(out, BLOCK_BYTES_LENGTH, key, iv, len); + ASSERT_FALSE(memcmp(innew, plain, BLOCK_BYTES_LENGTH)); delete[] out; delete[] innew; } + + int main(int argc, char *argv[]) { ::testing::InitGoogleTest(&argc, argv);