Mercurial > public > ostc_companion
view AES/unit_test.cpp @ 4:e30f00f760d3 default tip
Cleanup OSTC label and removed url
The computer type will now show OSTC 4/5 instead of only 4. The url has
been removed because it is no longer maintained. The ui header have been
deleted because they are generated files shich should not be under
version controll. Delete locally if you want to force an update of the
dialog layout.
| author | Ideenmodellierer |
|---|---|
| date | Sun, 30 Nov 2025 18:37:32 +0100 |
| parents | 0b3630a29ad8 |
| children |
line wrap: on
line source
//////////////////////////////////////////////////////////////////////// /// \file AES/unit_test.cpp /// \brief Test AES block mode, and CFB mode. /// \author (c) jD Gascuel /// /// $Id$ //////////////////////////////////////////////////////////////////////// // File History: // 2015-03-14 [jDG] Creation. #include "Rijndael.h" #include "gtest/gtest.h" #include <set> /// Vectors from http://www.inconteam.com/software-development/41-encryption/55-aes-test-vectors#aes-ecb-128 static const Rijndael::Block plain[4] = { {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a}, {0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51}, {0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef}, {0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10} }; static const Rijndael::ECB<128>::Key key128 = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }; static const Rijndael::Block cipher128[4] = { {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97}, {0xf5, 0xd3, 0xd5, 0x85, 0x03, 0xb9, 0x69, 0x9d, 0xe7, 0x85, 0x89, 0x5a, 0x96, 0xfd, 0xba, 0xaf}, {0x43, 0xb1, 0xcd, 0x7f, 0x59, 0x8e, 0xce, 0x23, 0x88, 0x1b, 0x00, 0xe3, 0xed, 0x03, 0x06, 0x88}, {0x7b, 0x0c, 0x78, 0x5e, 0x27, 0xe8, 0xad, 0x3f, 0x82, 0x23, 0x20, 0x71, 0x04, 0x72, 0x5d, 0xd4} }; static const Rijndael::ECB<256>::Key key256 = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 }; static const Rijndael::Block cipher256[4] = { {0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c, 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8}, {0x59, 0x1c, 0xcb, 0x10, 0xd4, 0x10, 0xed, 0x26, 0xdc, 0x5b, 0xa7, 0x4a, 0x31, 0x36, 0x28, 0x70}, {0xb6, 0xed, 0x21, 0xb9, 0x9c, 0xa6, 0xf4, 0xf9, 0xf1, 0x53, 0xe7, 0xb1, 0xbe, 0xaf, 0xed, 0x1d}, {0x23, 0x30, 0x4b, 0x7a, 0x39, 0xf9, 0xf3, 0xff, 0x06, 0x7d, 0x8d, 0x8f, 0x9e, 0x24, 0xec, 0xc7} }; ////////////////////////////////////////////////////////////////////////////// static const Rijndael::Block IV[4] = { {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, {0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20, 0x33, 0x34, 0x49, 0xF8, 0xE8, 0x3C, 0xFB, 0x4A}, {0xC8, 0xA6, 0x45, 0x37, 0xA0, 0xB3, 0xA9, 0x3F, 0xCD, 0xE3, 0xCD, 0xAD, 0x9F, 0x1C, 0xE5, 0x8B}, {0x26, 0x75, 0x1F, 0x67, 0xA3, 0xCB, 0xB1, 0x40, 0xB1, 0x80, 0x8C, 0xF1, 0x87, 0xA4, 0xF4, 0xDF} }; static const Rijndael::Block cfb_cipher128[4] = { {0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20, 0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a}, {0xc8, 0xa6, 0x45, 0x37, 0xa0, 0xb3, 0xa9, 0x3f, 0xcd, 0xe3, 0xcd, 0xad, 0x9f, 0x1c, 0xe5, 0x8b}, {0x26, 0x75, 0x1f, 0x67, 0xa3, 0xcb, 0xb1, 0x40, 0xb1, 0x80, 0x8c, 0xf1, 0x87, 0xa4, 0xf4, 0xdf}, {0xc0, 0x4b, 0x05, 0x35, 0x7c, 0x5d, 0x1c, 0x0e, 0xea, 0xc4, 0xc6, 0x6f, 0x9f, 0xf7, 0xf2, 0xe6} }; ////////////////////////////////////////////////////////////////////////////// TEST(Rijndael, ECB_PRNG) { Rijndael::ECB<128> enc; enc.setupEncrypt(key128); const int samples = 100*1000; std::set<Rijndael::Word32> dict; int collisions = 0; for(int i=0; i<samples; ++i) { Rijndael::Word32 x = enc.get_random(); // Should not hit the zero value: ASSERT_NE(0, x); // Should scarcely produce the same value twice if(dict.find(x) != dict.end()) ++collisions; else dict.insert(x); } // Observed: 250e-6 ASSERT_LE(float(collisions)/float(samples), 500e-6f); } ////////////////////////////////////////////////////////////////////////////// TEST(Rijndael, ECB_encrypt_128) { Rijndael::ECB<128> enc; enc.setupEncrypt(key128); for(int test=0; test<4; ++test) { Rijndael::Block result = {0}; enc.encrypt(plain[test], result); for(int c=0; c<16; ++c) EXPECT_EQ(cipher128[test][c], result[c]) << "Test " << test << ": byte " << c; } } ////////////////////////////////////////////////////////////////////////////// TEST(Rijndael, ECB_decrypt_128) { Rijndael::ECB<128> dec; dec.setupDecrypt(key128); for(int test=0; test<4; ++test) { Rijndael::Block result = {0}; dec.decrypt(cipher128[test], result); for(int c=0; c<16; ++c) EXPECT_EQ(plain[test][c], result[c]) << "Test " << test << ": byte " << c; } } ////////////////////////////////////////////////////////////////////////////// TEST(Rijndael, ECB_encrypt_256) { Rijndael::ECB<256> enc; enc.setupEncrypt(key256); for(int test=0; test<4; ++test) { Rijndael::Block result = {0}; enc.encrypt(plain[test], result); for(int c=0; c<16; ++c) ASSERT_EQ(cipher256[test][c], result[c]) << "Test " << test << ": byte " << c; } } ////////////////////////////////////////////////////////////////////////////// TEST(Rijndael, ECB_decrypt_256) { Rijndael::ECB<256> dec; dec.setupDecrypt(key256); for(int test=0; test<4; ++test) { Rijndael::Block result = {0}; dec.decrypt(cipher256[test], result); for(int c=0; c<16; ++c) ASSERT_EQ(plain[test][c], result[c]) << "Test " << test << ": byte " << c; } } ////////////////////////////////////////////////////////////////////////////// TEST(Rijndael, CFB_encrypt_128) { for(int test=0; test<4; ++test) { Rijndael::CFB<128> enc(key128, IV[test]); Rijndael::Block result = {0}; enc.encrypt(plain[test], result); for(int c=0; c<16; ++c) { ASSERT_EQ(cfb_cipher128[test][c], result[c]) << "Test " << test << ": byte " << c; } } } ////////////////////////////////////////////////////////////////////////////// TEST(Rijndael, CFB_decrypt_128) { for(int test=0; test<4; ++test) { Rijndael::CFB<128> dec(key128, IV[test]); Rijndael::Block result = {0}; dec.decrypt(cfb_cipher128[test], result); for(int c=0; c<16; ++c) { ASSERT_EQ(plain[test][c], result[c]) << "Test " << test << ": byte " << c; } } } ////////////////////////////////////////////////////////////////////////////// TEST(Rijndael, CFB_encrypt_128_chained) { Rijndael::CFB<128> enc(key128, IV[0]); for(int test=0; test<4; ++test) { Rijndael::Block result = {0}; enc.encrypt(plain[test], result); for(int c=0; c<16; ++c) { ASSERT_EQ(cfb_cipher128[test][c], result[c]) << "Test " << test << ": byte " << c; } } } ////////////////////////////////////////////////////////////////////////////// TEST(Rijndael, CFB_decrypt_128_chained) { Rijndael::CFB<128> dec(key128, IV[0]); for(int test=0; test<4; ++test) { Rijndael::Block result = {0}; dec.decrypt(cfb_cipher128[test], result); for(int c=0; c<16; ++c) { ASSERT_EQ(plain[test][c], result[c]) << "Test " << test << ": byte " << c; } } } //////////////////////////////////////////////////////////////////////////////
