comparison AES/unit_test.cpp @ 1:0b3630a29ad8

Initial version based on previous repository. Project was ported to QT6 and in now cmake based.
author Ideenmodellierer <tiefenrauscher@web.de>
date Thu, 27 Nov 2025 18:40:28 +0100
parents
children
comparison
equal deleted inserted replaced
0:76ccd6ce50c0 1:0b3630a29ad8
1 ////////////////////////////////////////////////////////////////////////
2 /// \file AES/unit_test.cpp
3 /// \brief Test AES block mode, and CFB mode.
4 /// \author (c) jD Gascuel
5 ///
6 /// $Id$
7 ////////////////////////////////////////////////////////////////////////
8 // File History:
9 // 2015-03-14 [jDG] Creation.
10
11 #include "Rijndael.h"
12 #include "gtest/gtest.h"
13
14 #include <set>
15
16 /// Vectors from http://www.inconteam.com/software-development/41-encryption/55-aes-test-vectors#aes-ecb-128
17
18 static const Rijndael::Block plain[4] = {
19 {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a},
20 {0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51},
21 {0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef},
22 {0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10}
23 };
24
25 static const Rijndael::ECB<128>::Key key128 = {
26 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
27 };
28
29 static const Rijndael::Block cipher128[4] = {
30 {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97},
31 {0xf5, 0xd3, 0xd5, 0x85, 0x03, 0xb9, 0x69, 0x9d, 0xe7, 0x85, 0x89, 0x5a, 0x96, 0xfd, 0xba, 0xaf},
32 {0x43, 0xb1, 0xcd, 0x7f, 0x59, 0x8e, 0xce, 0x23, 0x88, 0x1b, 0x00, 0xe3, 0xed, 0x03, 0x06, 0x88},
33 {0x7b, 0x0c, 0x78, 0x5e, 0x27, 0xe8, 0xad, 0x3f, 0x82, 0x23, 0x20, 0x71, 0x04, 0x72, 0x5d, 0xd4}
34 };
35
36 static const Rijndael::ECB<256>::Key key256 = {
37 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
38 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
39 };
40
41 static const Rijndael::Block cipher256[4] = {
42 {0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c, 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8},
43 {0x59, 0x1c, 0xcb, 0x10, 0xd4, 0x10, 0xed, 0x26, 0xdc, 0x5b, 0xa7, 0x4a, 0x31, 0x36, 0x28, 0x70},
44 {0xb6, 0xed, 0x21, 0xb9, 0x9c, 0xa6, 0xf4, 0xf9, 0xf1, 0x53, 0xe7, 0xb1, 0xbe, 0xaf, 0xed, 0x1d},
45 {0x23, 0x30, 0x4b, 0x7a, 0x39, 0xf9, 0xf3, 0xff, 0x06, 0x7d, 0x8d, 0x8f, 0x9e, 0x24, 0xec, 0xc7}
46 };
47
48 //////////////////////////////////////////////////////////////////////////////
49
50 static const Rijndael::Block IV[4] = {
51 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
52 {0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20, 0x33, 0x34, 0x49, 0xF8, 0xE8, 0x3C, 0xFB, 0x4A},
53 {0xC8, 0xA6, 0x45, 0x37, 0xA0, 0xB3, 0xA9, 0x3F, 0xCD, 0xE3, 0xCD, 0xAD, 0x9F, 0x1C, 0xE5, 0x8B},
54 {0x26, 0x75, 0x1F, 0x67, 0xA3, 0xCB, 0xB1, 0x40, 0xB1, 0x80, 0x8C, 0xF1, 0x87, 0xA4, 0xF4, 0xDF}
55 };
56
57 static const Rijndael::Block cfb_cipher128[4] = {
58 {0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20, 0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a},
59 {0xc8, 0xa6, 0x45, 0x37, 0xa0, 0xb3, 0xa9, 0x3f, 0xcd, 0xe3, 0xcd, 0xad, 0x9f, 0x1c, 0xe5, 0x8b},
60 {0x26, 0x75, 0x1f, 0x67, 0xa3, 0xcb, 0xb1, 0x40, 0xb1, 0x80, 0x8c, 0xf1, 0x87, 0xa4, 0xf4, 0xdf},
61 {0xc0, 0x4b, 0x05, 0x35, 0x7c, 0x5d, 0x1c, 0x0e, 0xea, 0xc4, 0xc6, 0x6f, 0x9f, 0xf7, 0xf2, 0xe6}
62 };
63
64 //////////////////////////////////////////////////////////////////////////////
65
66 TEST(Rijndael, ECB_PRNG) {
67 Rijndael::ECB<128> enc;
68 enc.setupEncrypt(key128);
69
70 const int samples = 100*1000;
71 std::set<Rijndael::Word32> dict;
72
73 int collisions = 0;
74 for(int i=0; i<samples; ++i) {
75 Rijndael::Word32 x = enc.get_random();
76
77 // Should not hit the zero value:
78 ASSERT_NE(0, x);
79
80 // Should scarcely produce the same value twice
81 if(dict.find(x) != dict.end())
82 ++collisions;
83 else
84 dict.insert(x);
85 }
86 // Observed: 250e-6
87 ASSERT_LE(float(collisions)/float(samples), 500e-6f);
88 }
89
90 //////////////////////////////////////////////////////////////////////////////
91
92 TEST(Rijndael, ECB_encrypt_128) {
93 Rijndael::ECB<128> enc;
94 enc.setupEncrypt(key128);
95
96 for(int test=0; test<4; ++test) {
97 Rijndael::Block result = {0};
98 enc.encrypt(plain[test], result);
99 for(int c=0; c<16; ++c)
100 EXPECT_EQ(cipher128[test][c], result[c])
101 << "Test " << test << ": byte " << c;
102 }
103 }
104
105 //////////////////////////////////////////////////////////////////////////////
106
107 TEST(Rijndael, ECB_decrypt_128) {
108 Rijndael::ECB<128> dec;
109 dec.setupDecrypt(key128);
110
111 for(int test=0; test<4; ++test) {
112 Rijndael::Block result = {0};
113 dec.decrypt(cipher128[test], result);
114 for(int c=0; c<16; ++c)
115 EXPECT_EQ(plain[test][c], result[c])
116 << "Test " << test << ": byte " << c;
117 }
118 }
119
120 //////////////////////////////////////////////////////////////////////////////
121
122 TEST(Rijndael, ECB_encrypt_256) {
123 Rijndael::ECB<256> enc;
124 enc.setupEncrypt(key256);
125
126 for(int test=0; test<4; ++test) {
127 Rijndael::Block result = {0};
128 enc.encrypt(plain[test], result);
129 for(int c=0; c<16; ++c)
130 ASSERT_EQ(cipher256[test][c], result[c])
131 << "Test " << test << ": byte " << c;
132 }
133 }
134
135 //////////////////////////////////////////////////////////////////////////////
136
137 TEST(Rijndael, ECB_decrypt_256) {
138 Rijndael::ECB<256> dec;
139 dec.setupDecrypt(key256);
140
141 for(int test=0; test<4; ++test) {
142 Rijndael::Block result = {0};
143 dec.decrypt(cipher256[test], result);
144 for(int c=0; c<16; ++c)
145 ASSERT_EQ(plain[test][c], result[c])
146 << "Test " << test << ": byte " << c;
147 }
148 }
149
150 //////////////////////////////////////////////////////////////////////////////
151
152 TEST(Rijndael, CFB_encrypt_128) {
153 for(int test=0; test<4; ++test) {
154 Rijndael::CFB<128> enc(key128, IV[test]);
155
156 Rijndael::Block result = {0};
157 enc.encrypt(plain[test], result);
158 for(int c=0; c<16; ++c)
159 {
160 ASSERT_EQ(cfb_cipher128[test][c], result[c])
161 << "Test " << test << ": byte " << c;
162 }
163 }
164 }
165
166 //////////////////////////////////////////////////////////////////////////////
167
168 TEST(Rijndael, CFB_decrypt_128) {
169 for(int test=0; test<4; ++test) {
170 Rijndael::CFB<128> dec(key128, IV[test]);
171
172 Rijndael::Block result = {0};
173 dec.decrypt(cfb_cipher128[test], result);
174 for(int c=0; c<16; ++c)
175 {
176 ASSERT_EQ(plain[test][c], result[c])
177 << "Test " << test << ": byte " << c;
178 }
179 }
180 }
181
182 //////////////////////////////////////////////////////////////////////////////
183
184 TEST(Rijndael, CFB_encrypt_128_chained) {
185 Rijndael::CFB<128> enc(key128, IV[0]);
186
187 for(int test=0; test<4; ++test) {
188 Rijndael::Block result = {0};
189 enc.encrypt(plain[test], result);
190 for(int c=0; c<16; ++c)
191 {
192 ASSERT_EQ(cfb_cipher128[test][c], result[c])
193 << "Test " << test << ": byte " << c;
194 }
195 }
196 }
197
198
199 //////////////////////////////////////////////////////////////////////////////
200
201 TEST(Rijndael, CFB_decrypt_128_chained) {
202 Rijndael::CFB<128> dec(key128, IV[0]);
203
204 for(int test=0; test<4; ++test) {
205 Rijndael::Block result = {0};
206 dec.decrypt(cfb_cipher128[test], result);
207 for(int c=0; c<16; ++c)
208 {
209 ASSERT_EQ(plain[test][c], result[c])
210 << "Test " << test << ": byte " << c;
211 }
212 }
213 }
214
215 //////////////////////////////////////////////////////////////////////////////