From 3b91d656df5247a4e085991d69ffca2dcb73c312 Mon Sep 17 00:00:00 2001 From: SergeyBel Date: Fri, 27 Sep 2019 22:47:10 +0300 Subject: [PATCH 1/6] padding tests --- AES.cpp | 20 +++++++++++-- AES.h | 9 ++++-- tests.cpp | 85 +++++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 87 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..f95d883 100644 --- a/AES.h +++ b/AES.h @@ -1,9 +1,10 @@ #ifndef _AES_H_ #define _AES_H_ -#include +#include +#include -using namespace std; +using namespace std; class AES { @@ -65,9 +66,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); From 56642552492ed01d3e1e53ab25a89ff4c7e36784 Mon Sep 17 00:00:00 2001 From: SergeyBel Date: Fri, 27 Sep 2019 22:50:29 +0300 Subject: [PATCH 2/6] add print function --- AES.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/AES.h b/AES.h index f95d883..e5bebca 100644 --- a/AES.h +++ b/AES.h @@ -2,7 +2,8 @@ #define _AES_H_ #include -#include +#include +#include using namespace std; From 7d5f620039fc83ee517aa0317ccc1a05448756c4 Mon Sep 17 00:00:00 2001 From: SergeyBel Date: Tue, 1 Oct 2019 23:03:24 +0300 Subject: [PATCH 3/6] ECB mode tests --- AES.cpp | 16 +++++--------- AES.h | 2 +- tests.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 67 insertions(+), 17 deletions(-) diff --git a/AES.cpp b/AES.cpp index e79e250..3aacf5b 100644 --- a/AES.cpp +++ b/AES.cpp @@ -39,18 +39,14 @@ unsigned char * AES::EncryptECB(unsigned char in[], unsigned int inLen, unsigned return out; } -unsigned char * AES::DecryptECB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned int &outLen) +unsigned char * AES::DecryptECB(unsigned char in[], unsigned int inLen, unsigned char key[]) { - outLen = GetPaddingLength(inLen); - unsigned char *alignIn = PaddingNulls(in, inLen, outLen); - unsigned char *out = new unsigned char[outLen]; - for (unsigned int i = 0; i < outLen; i+= blockBytesLen) + unsigned char *out = new unsigned char[inLen]; + for (unsigned int i = 0; i < inLen; i+= blockBytesLen) { - DecryptBlock(alignIn + i, out + i, key); + DecryptBlock(in + i, out + i, key); } - delete[] alignIn; - return out; } @@ -76,7 +72,7 @@ unsigned char *AES::EncryptCBC(unsigned char in[], unsigned int inLen, unsigned unsigned char *AES::DecryptCBC(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv, unsigned int &outLen) { - outLen = GetPaddingLength(inLen); + outLen = GetPaddingLength(inLen); unsigned char *alignIn = PaddingNulls(in, inLen, outLen); unsigned char *out = new unsigned char[outLen]; unsigned char *block = new unsigned char[blockBytesLen]; @@ -501,7 +497,7 @@ void AES::printHexArray (unsigned char a[], unsigned int n) printf("%02x ", a[i]); } - } +} diff --git a/AES.h b/AES.h index e5bebca..8db268f 100644 --- a/AES.h +++ b/AES.h @@ -61,7 +61,7 @@ class AES 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 int &outLen); + 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); diff --git a/tests.cpp b/tests.cpp index fd280d7..028ddb6 100644 --- a/tests.cpp +++ b/tests.cpp @@ -33,7 +33,6 @@ TEST(KeyLengths, KeyLength192) delete[] out; } - TEST(KeyLengths, KeyLength256) { AES aes(256); @@ -49,6 +48,8 @@ TEST(KeyLengths, KeyLength256) delete[] out; } + + TEST(ECB, EncryptDecrypt) { AES aes(256); @@ -59,20 +60,20 @@ TEST(ECB, EncryptDecrypt) unsigned int len = 0; unsigned char *out = aes.EncryptECB(plain, BLOCK_BYTES_LENGTH, key, len); - unsigned char *innew = aes.DecryptECB(out, BLOCK_BYTES_LENGTH, key, len); + unsigned char *innew = aes.DecryptECB(out, BLOCK_BYTES_LENGTH, key); ASSERT_FALSE(memcmp(innew, plain, BLOCK_BYTES_LENGTH)); delete[] out; delete[] innew; } -TEST(ECB, OneBlockWIthoutByteEncrypt) +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 plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee}; 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); + unsigned char *out = aes.EncryptECB(plain, (BLOCK_BYTES_LENGTH - 1 * sizeof(unsigned char)) , key, len); ASSERT_EQ(BLOCK_BYTES_LENGTH, len); @@ -81,6 +82,8 @@ TEST(ECB, OneBlockWIthoutByteEncrypt) delete[] out; } + + TEST(ECB, OneBlockPlusOneByteEncrypt) { AES aes(128); @@ -99,6 +102,57 @@ TEST(ECB, OneBlockPlusOneByteEncrypt) delete[] out; } +TEST(ECB, TwoBlocksEncrypt) +{ + AES aes(128); + unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}; + 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, + 0x07, 0xfe, 0xef, 0x74, 0xe1, 0xd5, 0x03, 0x6e, 0x90, 0x0e, 0xee, 0x11, 0x8e, 0x94, 0x92, 0x93, + }; + unsigned int len = 0; + unsigned char *out = aes.EncryptECB(plain, (BLOCK_BYTES_LENGTH * 2) * sizeof(unsigned char), key, len); + + + ASSERT_EQ(2 * BLOCK_BYTES_LENGTH, len); + ASSERT_FALSE(memcmp(right, out, len)); + + delete[] out; +} + +TEST(ECB, OneBlockDecrypt) +{ + AES aes(128); + unsigned char encrypted[] = { 0x7c, 0x99, 0xf4, 0x2b, 0x6e, 0xe5, 0x03, 0x30, 0x9c, 0x6c, 0x1a, 0x67, 0xe9, 0x7a, 0xc2, 0x42 }; + unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; + unsigned char right[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}; + unsigned int len = 0; + unsigned char *out = aes.DecryptECB(encrypted, BLOCK_BYTES_LENGTH, key); + + ASSERT_FALSE(memcmp(right, out, len)); + + delete[] out; +} + +TEST(ECB, TwoBlocksDecrypt) +{ + AES aes(128); + unsigned char encrypted[] = { 0x7c, 0x99, 0xf4, 0x2b, 0x6e, 0xe5, 0x03, 0x30, 0x9c, 0x6c, 0x1a, 0x67, 0xe9, 0x7a, 0xc2, 0x42, + 0x7c, 0x99, 0xf4, 0x2b, 0x6e, 0xe5, 0x03, 0x30, 0x9c, 0x6c, 0x1a, 0x67, 0xe9, 0x7a, 0xc2, 0x42}; + unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; + unsigned char right[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, + 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0x00}; + unsigned int len = 0; + unsigned char *out = aes.DecryptECB(encrypted, BLOCK_BYTES_LENGTH, key); + + ASSERT_FALSE(memcmp(right, out, len)); + + delete[] out; +} + + + TEST(CBC, EncryptDecrypt) @@ -117,6 +171,7 @@ TEST(CBC, EncryptDecrypt) delete[] innew; } + TEST(CFB, EncryptDecrypt) { AES aes(256); @@ -135,7 +190,6 @@ TEST(CFB, EncryptDecrypt) - int main(int argc, char *argv[]) { ::testing::InitGoogleTest(&argc, argv); From 4a88790ea12c3ca7fc142992dc40339de37159d6 Mon Sep 17 00:00:00 2001 From: SergeyBel Date: Thu, 3 Oct 2019 22:27:40 +0300 Subject: [PATCH 4/6] CBC mode tests --- AES.cpp | 2 ++ tests.cpp | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/AES.cpp b/AES.cpp index 3aacf5b..54d6b5b 100644 --- a/AES.cpp +++ b/AES.cpp @@ -62,6 +62,7 @@ unsigned char *AES::EncryptCBC(unsigned char in[], unsigned int inLen, unsigned { XorBlocks(block, alignIn + i, block, blockBytesLen); EncryptBlock(block, out + i, key); + memcpy(block, out + i, blockBytesLen); } delete[] block; @@ -81,6 +82,7 @@ unsigned char *AES::DecryptCBC(unsigned char in[], unsigned int inLen, unsigned { DecryptBlock(alignIn + i, out + i, key); XorBlocks(block, out + i, out + i, blockBytesLen); + memcpy(block, alignIn + i, blockBytesLen); } delete[] block; diff --git a/tests.cpp b/tests.cpp index 028ddb6..e2db407 100644 --- a/tests.cpp +++ b/tests.cpp @@ -82,8 +82,6 @@ TEST(ECB, OneBlockWithoutByteEncrypt) delete[] out; } - - TEST(ECB, OneBlockPlusOneByteEncrypt) { AES aes(128); @@ -171,6 +169,43 @@ TEST(CBC, EncryptDecrypt) delete[] innew; } +TEST(CBC, TwoBlocksEncrypt) +{ + AES aes(128); + unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, + 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}; + unsigned char right[] = {0x1b, 0x87, 0x23, 0x78, 0x79, 0x5f, 0x4f, 0xfd, 0x77, 0x28, 0x55, 0xfc, 0x87, 0xca, 0x96, 0x4d, + 0x4c, 0x5b, 0xca, 0x1c, 0x48, 0xcd, 0x88, 0x00, 0x3a, 0x10, 0x52, 0x11, 0x88, 0x12, 0x5e, 0x00}; + unsigned int len; + + unsigned char *out = aes.EncryptCBC(plain, BLOCK_BYTES_LENGTH * 2, key, iv, len); + ASSERT_EQ(BLOCK_BYTES_LENGTH * 2, len); + ASSERT_FALSE(memcmp(out, right, BLOCK_BYTES_LENGTH * 2)); + delete[] out; +} + +TEST(CBC, TwoBlocksDecrypt) +{ + AES aes(128); + unsigned char encrypted[] = {0x1b, 0x87, 0x23, 0x78, 0x79, 0x5f, 0x4f, 0xfd, 0x77, 0x28, 0x55, 0xfc, 0x87, 0xca, 0x96, 0x4d, + 0x4c, 0x5b, 0xca, 0x1c, 0x48, 0xcd, 0x88, 0x00, 0x3a, 0x10, 0x52, 0x11, 0x88, 0x12, 0x5e, 0x00}; + + 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}; + unsigned char right[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, + 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; + + unsigned int len; + + unsigned char *out = aes.DecryptCBC(encrypted, BLOCK_BYTES_LENGTH * 2, key, iv, len); + + ASSERT_FALSE(memcmp(out, right, BLOCK_BYTES_LENGTH * 2)); + delete[] out; +} + + TEST(CFB, EncryptDecrypt) { From d25d1e0bfcf212bccc40810c270dbbffd6eef497 Mon Sep 17 00:00:00 2001 From: SergeyBel Date: Thu, 3 Oct 2019 23:04:23 +0300 Subject: [PATCH 5/6] CFB mode tests --- tests.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests.cpp b/tests.cpp index e2db407..dabb565 100644 --- a/tests.cpp +++ b/tests.cpp @@ -223,6 +223,40 @@ TEST(CFB, EncryptDecrypt) delete[] innew; } +TEST(CFB, EncryptTwoBlocks) +{ + AES aes(128); + unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, + 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}; + unsigned char right[] = {0x3c, 0x55, 0x3d, 0x01, 0x8a, 0x52, 0xe4, 0x54, 0xec, 0x4e, 0x08, 0x22, 0xc2, 0x8d, 0x55, 0xec, + 0xe3, 0x5a, 0x40, 0xab, 0x30, 0x29, 0xf3, 0x0c, 0xe1, 0xdb, 0x30, 0x6c, 0xa1, 0x05, 0xcb, 0xa9}; + unsigned int len; + + unsigned char *out = aes.EncryptCFB(plain, BLOCK_BYTES_LENGTH * 2, key, iv, len); + ASSERT_EQ(2 * BLOCK_BYTES_LENGTH, len); + ASSERT_FALSE(memcmp(right, out, BLOCK_BYTES_LENGTH * 2)); + delete[] out; +} + + +TEST(CFB, DecryptTwoBlocks) +{ + AES aes(128); + unsigned char encrypted[] = {0x3c, 0x55, 0x3d, 0x01, 0x8a, 0x52, 0xe4, 0x54, 0xec, 0x4e, 0x08, 0x22, 0xc2, 0x8d, 0x55, 0xec, + 0xe3, 0x5a, 0x40, 0xab, 0x30, 0x29, 0xf3, 0x0c, 0xe1, 0xdb, 0x30, 0x6c, 0xa1, 0x05, 0xcb, 0xa9}; + 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}; + unsigned char right[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, + 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; + + unsigned int len; + + unsigned char *out = aes.DecryptCFB(encrypted, BLOCK_BYTES_LENGTH * 2, key, iv, len); + ASSERT_FALSE(memcmp(right, out, BLOCK_BYTES_LENGTH * 2)); + delete[] out; +} int main(int argc, char *argv[]) From 64b1259fac486b52f6858710ab9430f55f1fccff Mon Sep 17 00:00:00 2001 From: SergeyBel Date: Thu, 3 Oct 2019 23:12:00 +0300 Subject: [PATCH 6/6] simplify decrypt functions --- AES.cpp | 26 ++++++++++---------------- AES.h | 4 ++-- tests.cpp | 11 ++++------- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/AES.cpp b/AES.cpp index 54d6b5b..54ac103 100644 --- a/AES.cpp +++ b/AES.cpp @@ -71,22 +71,19 @@ unsigned char *AES::EncryptCBC(unsigned char in[], unsigned int inLen, unsigned return out; } -unsigned char *AES::DecryptCBC(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv, unsigned int &outLen) +unsigned char *AES::DecryptCBC(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv) { - outLen = GetPaddingLength(inLen); - unsigned char *alignIn = PaddingNulls(in, inLen, outLen); - unsigned char *out = new unsigned char[outLen]; + unsigned char *out = new unsigned char[inLen]; unsigned char *block = new unsigned char[blockBytesLen]; memcpy(block, iv, blockBytesLen); - for (unsigned int i = 0; i < outLen; i+= blockBytesLen) + for (unsigned int i = 0; i < inLen; i+= blockBytesLen) { - DecryptBlock(alignIn + i, out + i, key); + DecryptBlock(in + i, out + i, key); XorBlocks(block, out + i, out + i, blockBytesLen); - memcpy(block, alignIn + i, blockBytesLen); + memcpy(block, in + i, blockBytesLen); } delete[] block; - delete[] alignIn; return out; } @@ -113,24 +110,21 @@ unsigned char *AES::EncryptCFB(unsigned char in[], unsigned int inLen, unsigned return out; } -unsigned char *AES::DecryptCFB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv, unsigned int &outLen) +unsigned char *AES::DecryptCFB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv) { - outLen = GetPaddingLength(inLen); - unsigned char *alignIn = PaddingNulls(in, inLen, outLen); - unsigned char *out = new unsigned char[outLen]; + unsigned char *out = new unsigned char[inLen]; unsigned char *block = new unsigned char[blockBytesLen]; unsigned char *encryptedBlock = new unsigned char[blockBytesLen]; memcpy(block, iv, blockBytesLen); - for (unsigned int i = 0; i < outLen; i+= blockBytesLen) + for (unsigned int i = 0; i < inLen; i+= blockBytesLen) { EncryptBlock(block, encryptedBlock, key); - XorBlocks(alignIn + i, encryptedBlock, out + i, blockBytesLen); - memcpy(block, alignIn + i, blockBytesLen); + XorBlocks(in + i, encryptedBlock, out + i, blockBytesLen); + memcpy(block, in + i, blockBytesLen); } delete[] block; delete[] encryptedBlock; - delete[] alignIn; return out; } diff --git a/AES.h b/AES.h index 8db268f..b1bf393 100644 --- a/AES.h +++ b/AES.h @@ -65,11 +65,11 @@ class AES 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 int &outLen); + 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 *DecryptCFB(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); void printHexArray (unsigned char a[], unsigned int n); diff --git a/tests.cpp b/tests.cpp index dabb565..ca7476e 100644 --- a/tests.cpp +++ b/tests.cpp @@ -163,7 +163,7 @@ TEST(CBC, EncryptDecrypt) unsigned int len; unsigned char *out = aes.EncryptCBC(plain, BLOCK_BYTES_LENGTH, key, iv, len); - unsigned char *innew = aes.DecryptCBC(out, BLOCK_BYTES_LENGTH, key, iv, len); + unsigned char *innew = aes.DecryptCBC(out, BLOCK_BYTES_LENGTH, key, iv); ASSERT_FALSE(memcmp(innew, plain, BLOCK_BYTES_LENGTH)); delete[] out; delete[] innew; @@ -197,9 +197,8 @@ TEST(CBC, TwoBlocksDecrypt) unsigned char right[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; - unsigned int len; - unsigned char *out = aes.DecryptCBC(encrypted, BLOCK_BYTES_LENGTH * 2, key, iv, len); + unsigned char *out = aes.DecryptCBC(encrypted, BLOCK_BYTES_LENGTH * 2, key, iv); ASSERT_FALSE(memcmp(out, right, BLOCK_BYTES_LENGTH * 2)); delete[] out; @@ -217,7 +216,7 @@ TEST(CFB, EncryptDecrypt) unsigned int len; unsigned char *out = aes.EncryptCFB(plain, BLOCK_BYTES_LENGTH, key, iv, len); - unsigned char *innew = aes.DecryptCFB(out, BLOCK_BYTES_LENGTH, key, iv, len); + unsigned char *innew = aes.DecryptCFB(out, BLOCK_BYTES_LENGTH, key, iv); ASSERT_FALSE(memcmp(innew, plain, BLOCK_BYTES_LENGTH)); delete[] out; delete[] innew; @@ -250,10 +249,8 @@ TEST(CFB, DecryptTwoBlocks) unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}; unsigned char right[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; - - unsigned int len; - unsigned char *out = aes.DecryptCFB(encrypted, BLOCK_BYTES_LENGTH * 2, key, iv, len); + unsigned char *out = aes.DecryptCFB(encrypted, BLOCK_BYTES_LENGTH * 2, key, iv); ASSERT_FALSE(memcmp(right, out, BLOCK_BYTES_LENGTH * 2)); delete[] out; }