4
4
#include < QCryptographicHash>
5
5
#include " qaesencryption.h"
6
6
7
- void AesTest::ECB128Crypt ()
7
+ void AesTest::initTestCase ()
8
8
{
9
- QByteArray hexText, keyHex, outputHex;
10
- QAESEncryption encryption (QAESEncryption::AES_128, QAESEncryption::ECB);
9
+ quint8 key_16[16 ] = {0x2b , 0x7e , 0x15 , 0x16 , 0x28 , 0xae , 0xd2 , 0xa6 , 0xab , 0xf7 , 0x15 , 0x88 , 0x09 , 0xcf , 0x4f , 0x3c };
10
+ for (int i=0 ; i<16 ; i++)
11
+ key16.append (key_16[i]);
11
12
12
- const quint8 key[16 ] = {0x2b , 0x7e , 0x15 , 0x16 , 0x28 , 0xae , 0xd2 , 0xa6 , 0xab , 0xf7 , 0x15 , 0x88 , 0x09 , 0xcf , 0x4f , 0x3c };
13
+ quint8 key_24[24 ] = { 0x8e , 0x73 , 0xb0 , 0xf7 , 0xda , 0x0e , 0x64 , 0x52 , 0xc8 , 0x10 , 0xf3 , 0x2b , 0x80 , 0x90 , 0x79 , 0xe5 , 0x62 , 0xf8 ,
14
+ 0xea , 0xd2 , 0x52 , 0x2c , 0x6b , 0x7b };
15
+ for (int i=0 ; i<24 ; i++)
16
+ key24.append (key_24[i]);
13
17
14
- const quint8 text[16 ] = {0x6b , 0xc1 , 0xbe , 0xe2 , 0x2e , 0x40 , 0x9f , 0x96 , 0xe9 , 0x3d , 0x7e , 0x11 , 0x73 , 0x93 , 0x17 , 0x2a };
15
- const quint8 output[16 ] = {0x3a , 0xd7 , 0x7b , 0xb4 , 0x0d , 0x7a , 0x36 , 0x60 , 0xa8 , 0x9e , 0xca , 0xf3 , 0x24 , 0x66 , 0xef , 0x97 };
18
+ quint8 key_32[32 ]= { 0x60 , 0x3d , 0xeb , 0x10 , 0x15 , 0xca , 0x71 , 0xbe , 0x2b , 0x73 , 0xae , 0xf0 , 0x85 , 0x7d , 0x77 , 0x81 ,
19
+ 0x1f , 0x35 , 0x2c , 0x07 , 0x3b , 0x61 , 0x08 , 0xd7 , 0x2d , 0x98 , 0x10 , 0xa3 , 0x09 , 0x14 , 0xdf , 0xf4 };
20
+ for (int i=0 ; i<32 ; i++)
21
+ key32.append (key_32[i]);
16
22
23
+ quint8 iv_16[16 ] = {0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f };
24
+ for (int i=0 ; i<16 ; i++)
25
+ iv.append (iv_16[i]);
26
+
27
+ quint8 in_text[16 ] = { 0x6b , 0xc1 , 0xbe , 0xe2 , 0x2e , 0x40 , 0x9f , 0x96 , 0xe9 , 0x3d , 0x7e , 0x11 , 0x73 , 0x93 , 0x17 , 0x2a };
28
+ quint8 out_text[16 ] = { 0x3a , 0xd7 , 0x7b , 0xb4 , 0x0d , 0x7a , 0x36 , 0x60 , 0xa8 , 0x9e , 0xca , 0xf3 , 0x24 , 0x66 , 0xef , 0x97 };
29
+ quint8 out_text_2[16 ] = { 0xbd , 0x33 , 0x4f , 0x1d , 0x6e , 0x45 , 0xf2 , 0x5f , 0xf7 , 0x12 , 0xa2 , 0x14 , 0x57 , 0x1f , 0xa5 , 0xcc };
30
+ quint8 out_text_3[16 ] = { 0xf3 , 0xee , 0xd1 , 0xbd , 0xb5 , 0xd2 , 0xa0 , 0x3c , 0x06 , 0x4b , 0x5a , 0x7e , 0x3d , 0xb1 , 0x81 , 0xf8 };
17
31
18
32
for (int i=0 ; i<16 ; i++){
19
- keyHex.append (key[i]);
20
- hexText.append (text[i]);
21
- outputHex.append (output[i]);
33
+ in.append (in_text[i]);
34
+ outECB128.append (out_text[i]);
35
+ outECB192.append (out_text_2[i]);
36
+ outECB256.append (out_text_3[i]);
22
37
}
23
38
24
- QCOMPARE (encryption.encode (hexText, keyHex), outputHex);
39
+ quint8 text_cbc[64 ] = { 0x6b , 0xc1 , 0xbe , 0xe2 , 0x2e , 0x40 , 0x9f , 0x96 , 0xe9 , 0x3d , 0x7e , 0x11 , 0x73 , 0x93 , 0x17 , 0x2a ,
40
+ 0xae , 0x2d , 0x8a , 0x57 , 0x1e , 0x03 , 0xac , 0x9c , 0x9e , 0xb7 , 0x6f , 0xac , 0x45 , 0xaf , 0x8e , 0x51 ,
41
+ 0x30 , 0xc8 , 0x1c , 0x46 , 0xa3 , 0x5c , 0xe4 , 0x11 , 0xe5 , 0xfb , 0xc1 , 0x19 , 0x1a , 0x0a , 0x52 , 0xef ,
42
+ 0xf6 , 0x9f , 0x24 , 0x45 , 0xdf , 0x4f , 0x9b , 0x17 , 0xad , 0x2b , 0x41 , 0x7b , 0xe6 , 0x6c , 0x37 , 0x10 };
43
+
44
+ quint8 output_cbc[64 ] = { 0x76 , 0x49 , 0xab , 0xac , 0x81 , 0x19 , 0xb2 , 0x46 , 0xce , 0xe9 , 0x8e , 0x9b , 0x12 , 0xe9 , 0x19 , 0x7d ,
45
+ 0x50 , 0x86 , 0xcb , 0x9b , 0x50 , 0x72 , 0x19 , 0xee , 0x95 , 0xdb , 0x11 , 0x3a , 0x91 , 0x76 , 0x78 , 0xb2 ,
46
+ 0x73 , 0xbe , 0xd6 , 0xb8 , 0xe3 , 0xc1 , 0x74 , 0x3b , 0x71 , 0x16 , 0xe6 , 0x9e , 0x22 , 0x22 , 0x95 , 0x16 ,
47
+ 0x3f , 0xf1 , 0xca , 0xa1 , 0x68 , 0x1f , 0xac , 0x09 , 0x12 , 0x0e , 0xca , 0x30 , 0x75 , 0x86 , 0xe1 , 0xa7 };
48
+
49
+ for (int i=0 ; i<64 ; i++){
50
+ inCBC128.append (text_cbc[i]);
51
+ outCBC128.append (output_cbc[i]);
52
+ }
25
53
}
26
54
27
- void AesTest::ECB128Decrypt ()
55
+
56
+ // ==================ECB TESTING=========================
57
+
58
+ void AesTest::ECB128Crypt ()
28
59
{
29
- QByteArray hexText, keyHex, outputHex;
60
+ QByteArray hexText, outputHex;
30
61
QAESEncryption encryption (QAESEncryption::AES_128, QAESEncryption::ECB);
31
62
32
- const quint8 key[16 ] = {0x2b , 0x7e , 0x15 , 0x16 , 0x28 , 0xae , 0xd2 , 0xa6 , 0xab , 0xf7 , 0x15 , 0x88 , 0x09 , 0xcf , 0x4f , 0x3c };
33
- const quint8 text[16 ] = {0x43 , 0xb1 , 0xcd , 0x7f , 0x59 , 0x8e , 0xce , 0x23 , 0x88 , 0x1b , 0x00 , 0xe3 , 0xed , 0x03 , 0x06 , 0x88 };
34
- const quint8 output[16 ] = {0x30 , 0xc8 , 0x1c , 0x46 , 0xa3 , 0x5c , 0xe4 , 0x11 , 0xe5 , 0xfb , 0xc1 , 0x19 , 0x1a , 0x0a , 0x52 , 0xef };
63
+ QCOMPARE (encryption.encode (in, key16), outECB128);
64
+ }
35
65
36
- for (int i=0 ; i<16 ; i++){
37
- keyHex.append (key[i]);
38
- hexText.append (text[i]);
39
- outputHex.append (output[i]);
40
- }
66
+ void AesTest::ECB128Decrypt ()
67
+ {
68
+ QByteArray hexText, outputHex;
69
+ QAESEncryption encryption (QAESEncryption::AES_128, QAESEncryption::ECB);
41
70
42
- QCOMPARE (encryption.decode (hexText, keyHex ), outputHex );
71
+ QCOMPARE (encryption.decode (outECB128, key16 ), in );
43
72
}
44
73
45
74
void AesTest::ECB192Crypt ()
46
75
{
47
- QByteArray hexText, keyHex, outputHex;
76
+ QByteArray outputHex;
48
77
QAESEncryption encryption (QAESEncryption::AES_192, QAESEncryption::ECB);
49
78
50
- uint8_t key[24 ] = { 0x8e , 0x73 , 0xb0 , 0xf7 , 0xda , 0x0e , 0x64 , 0x52 , 0xc8 , 0x10 , 0xf3 , 0x2b , 0x80 , 0x90 , 0x79 , 0xe5 , 0x62 , 0xf8 ,
51
- 0xea , 0xd2 , 0x52 , 0x2c , 0x6b , 0x7b };
52
- uint8_t text[16 ] = {0x6b , 0xc1 , 0xbe , 0xe2 , 0x2e , 0x40 , 0x9f , 0x96 , 0xe9 , 0x3d , 0x7e , 0x11 , 0x73 , 0x93 , 0x17 , 0x2a };
53
- uint8_t output[16 ] = { 0xbd , 0x33 , 0x4f , 0x1d , 0x6e , 0x45 , 0xf2 , 0x5f , 0xf7 , 0x12 , 0xa2 , 0x14 , 0x57 , 0x1f , 0xa5 , 0xcc };
54
-
55
- for (int i=0 ; i<16 ; i++){
56
- hexText.append (text[i]);
57
- outputHex.append (output[i]);
58
- }
59
- for (int i=0 ; i<24 ; i++)
60
- keyHex.append (key[i]);
61
-
62
- QCOMPARE (encryption.encode (hexText, keyHex), outputHex);
79
+ QCOMPARE (encryption.encode (in, key24), outECB192);
63
80
}
64
81
65
82
void AesTest::ECB192Decrypt ()
66
83
{
67
- QByteArray hexText, keyHex, outputHex ;
84
+ QByteArray hexText;
68
85
QAESEncryption encryption (QAESEncryption::AES_192, QAESEncryption::ECB);
69
86
70
- uint8_t key[24 ] = { 0x8e , 0x73 , 0xb0 , 0xf7 , 0xda , 0x0e , 0x64 , 0x52 , 0xc8 , 0x10 , 0xf3 , 0x2b , 0x80 , 0x90 , 0x79 , 0xe5 , 0x62 , 0xf8 ,
71
- 0xea , 0xd2 , 0x52 , 0x2c , 0x6b , 0x7b };
72
- uint8_t text[16 ] = { 0xbd , 0x33 , 0x4f , 0x1d , 0x6e , 0x45 , 0xf2 , 0x5f , 0xf7 , 0x12 , 0xa2 , 0x14 , 0x57 , 0x1f , 0xa5 , 0xcc };
73
- uint8_t output[16 ] = {0x6b , 0xc1 , 0xbe , 0xe2 , 0x2e , 0x40 , 0x9f , 0x96 , 0xe9 , 0x3d , 0x7e , 0x11 , 0x73 , 0x93 , 0x17 , 0x2a };
74
-
75
- for (int i=0 ; i<16 ; i++){
76
- hexText.append (text[i]);
77
- outputHex.append (output[i]);
78
- }
79
- for (int i=0 ; i<24 ; i++)
80
- keyHex.append (key[i]);
81
-
82
- QCOMPARE (encryption.decode (hexText, keyHex), outputHex);
87
+ QCOMPARE (encryption.decode (outECB192, key24), in);
83
88
}
84
89
85
90
void AesTest::ECB256Crypt ()
86
91
{
87
- QByteArray hexText, keyHex, outputHex;
88
92
QAESEncryption encryption (QAESEncryption::AES_256, QAESEncryption::ECB);
89
93
90
- uint8_t key[32 ] = { 0x60 , 0x3d , 0xeb , 0x10 , 0x15 , 0xca , 0x71 , 0xbe , 0x2b , 0x73 , 0xae , 0xf0 , 0x85 , 0x7d , 0x77 , 0x81 ,
91
- 0x1f , 0x35 , 0x2c , 0x07 , 0x3b , 0x61 , 0x08 , 0xd7 , 0x2d , 0x98 , 0x10 , 0xa3 , 0x09 , 0x14 , 0xdf , 0xf4 };
92
- uint8_t text[16 ] = {0x6b , 0xc1 , 0xbe , 0xe2 , 0x2e , 0x40 , 0x9f , 0x96 , 0xe9 , 0x3d , 0x7e , 0x11 , 0x73 , 0x93 , 0x17 , 0x2a };
93
- uint8_t output[16 ] = { 0xf3 , 0xee , 0xd1 , 0xbd , 0xb5 , 0xd2 , 0xa0 , 0x3c , 0x06 , 0x4b , 0x5a , 0x7e , 0x3d , 0xb1 , 0x81 , 0xf8 };
94
-
95
- for (int i=0 ; i<16 ; i++){
96
- hexText.append (text[i]);
97
- outputHex.append (output[i]);
98
- }
99
- for (int i=0 ; i<32 ; i++)
100
- keyHex.append (key[i]);
101
-
102
- QCOMPARE (encryption.encode (hexText, keyHex), outputHex);
94
+ QCOMPARE (encryption.encode (in, key32), outECB256);
103
95
}
104
96
105
97
void AesTest::ECB256Decrypt ()
106
98
{
107
- QByteArray hexText, keyHex, outputHex;
108
99
QAESEncryption encryption (QAESEncryption::AES_256, QAESEncryption::ECB);
109
100
110
- uint8_t key[32 ] = { 0x60 , 0x3d , 0xeb , 0x10 , 0x15 , 0xca , 0x71 , 0xbe , 0x2b , 0x73 , 0xae , 0xf0 , 0x85 , 0x7d , 0x77 , 0x81 ,
111
- 0x1f , 0x35 , 0x2c , 0x07 , 0x3b , 0x61 , 0x08 , 0xd7 , 0x2d , 0x98 , 0x10 , 0xa3 , 0x09 , 0x14 , 0xdf , 0xf4 };
112
- uint8_t output[16 ] = {0x6b , 0xc1 , 0xbe , 0xe2 , 0x2e , 0x40 , 0x9f , 0x96 , 0xe9 , 0x3d , 0x7e , 0x11 , 0x73 , 0x93 , 0x17 , 0x2a };
113
- uint8_t text[16 ] = { 0xf3 , 0xee , 0xd1 , 0xbd , 0xb5 , 0xd2 , 0xa0 , 0x3c , 0x06 , 0x4b , 0x5a , 0x7e , 0x3d , 0xb1 , 0x81 , 0xf8 };
114
-
115
- for (int i=0 ; i<16 ; i++){
116
- hexText.append (text[i]);
117
- outputHex.append (output[i]);
118
- }
119
- for (int i=0 ; i<32 ; i++)
120
- keyHex.append (key[i]);
121
-
122
- QCOMPARE (encryption.decode (hexText, keyHex), outputHex);
101
+ QCOMPARE (encryption.decode (outECB256, key32), in);
123
102
}
124
103
125
104
void AesTest::ECB256String ()
@@ -138,65 +117,19 @@ void AesTest::ECB256String()
138
117
QCOMPARE (QString (encryption.decode (encodeText, hashKey)), inputStr);
139
118
}
140
119
120
+
121
+ // ==================CBC TESTING=========================
122
+
141
123
void AesTest::CBC128Crypt ()
142
124
{
143
- QByteArray hexText, keyHex, outputHex, ivHex;
144
125
QAESEncryption encryption (QAESEncryption::AES_128, QAESEncryption::CBC);
145
126
146
- const quint8 key[16 ] = {0x2b , 0x7e , 0x15 , 0x16 , 0x28 , 0xae , 0xd2 , 0xa6 , 0xab , 0xf7 , 0x15 , 0x88 , 0x09 , 0xcf , 0x4f , 0x3c };
147
- const quint8 iv[16 ] = {0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f };
148
-
149
- const quint8 text[64 ] = {0x6b , 0xc1 , 0xbe , 0xe2 , 0x2e , 0x40 , 0x9f , 0x96 , 0xe9 , 0x3d , 0x7e , 0x11 , 0x73 , 0x93 , 0x17 , 0x2a ,
150
- 0xae , 0x2d , 0x8a , 0x57 , 0x1e , 0x03 , 0xac , 0x9c , 0x9e , 0xb7 , 0x6f , 0xac , 0x45 , 0xaf , 0x8e , 0x51 ,
151
- 0x30 , 0xc8 , 0x1c , 0x46 , 0xa3 , 0x5c , 0xe4 , 0x11 , 0xe5 , 0xfb , 0xc1 , 0x19 , 0x1a , 0x0a , 0x52 , 0xef ,
152
- 0xf6 , 0x9f , 0x24 , 0x45 , 0xdf , 0x4f , 0x9b , 0x17 , 0xad , 0x2b , 0x41 , 0x7b , 0xe6 , 0x6c , 0x37 , 0x10 };
153
-
154
- const quint8 output[64 ] = {0x76 , 0x49 , 0xab , 0xac , 0x81 , 0x19 , 0xb2 , 0x46 , 0xce , 0xe9 , 0x8e , 0x9b , 0x12 , 0xe9 , 0x19 , 0x7d ,
155
- 0x50 , 0x86 , 0xcb , 0x9b , 0x50 , 0x72 , 0x19 , 0xee , 0x95 , 0xdb , 0x11 , 0x3a , 0x91 , 0x76 , 0x78 , 0xb2 ,
156
- 0x73 , 0xbe , 0xd6 , 0xb8 , 0xe3 , 0xc1 , 0x74 , 0x3b , 0x71 , 0x16 , 0xe6 , 0x9e , 0x22 , 0x22 , 0x95 , 0x16 ,
157
- 0x3f , 0xf1 , 0xca , 0xa1 , 0x68 , 0x1f , 0xac , 0x09 , 0x12 , 0x0e , 0xca , 0x30 , 0x75 , 0x86 , 0xe1 , 0xa7 };
158
-
159
- for (int i=0 ; i<64 ; i++){
160
- if (i<16 ) {
161
- keyHex.append (key[i]);
162
- ivHex.append (iv[i]);
163
- }
164
- hexText.append (text[i]);
165
- outputHex.append (output[i]);
166
-
167
- }
168
-
169
- QCOMPARE (encryption.encode (hexText, keyHex, ivHex), outputHex);
127
+ QCOMPARE (encryption.encode (inCBC128, key16, iv), outCBC128);
170
128
}
171
129
172
130
void AesTest::CBC128Decrypt ()
173
131
{
174
- QByteArray hexText, keyHex, outputHex, ivHex;
175
132
QAESEncryption encryption (QAESEncryption::AES_128, QAESEncryption::CBC);
176
133
177
- const quint8 key[16 ] = { 0x2b , 0x7e , 0x15 , 0x16 , 0x28 , 0xae , 0xd2 , 0xa6 , 0xab , 0xf7 , 0x15 , 0x88 , 0x09 , 0xcf , 0x4f , 0x3c };
178
- const quint8 iv[16 ] = { 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f };
179
-
180
- const quint8 text[64 ] = { 0x76 , 0x49 , 0xab , 0xac , 0x81 , 0x19 , 0xb2 , 0x46 , 0xce , 0xe9 , 0x8e , 0x9b , 0x12 , 0xe9 , 0x19 , 0x7d ,
181
- 0x50 , 0x86 , 0xcb , 0x9b , 0x50 , 0x72 , 0x19 , 0xee , 0x95 , 0xdb , 0x11 , 0x3a , 0x91 , 0x76 , 0x78 , 0xb2 ,
182
- 0x73 , 0xbe , 0xd6 , 0xb8 , 0xe3 , 0xc1 , 0x74 , 0x3b , 0x71 , 0x16 , 0xe6 , 0x9e , 0x22 , 0x22 , 0x95 , 0x16 ,
183
- 0x3f , 0xf1 , 0xca , 0xa1 , 0x68 , 0x1f , 0xac , 0x09 , 0x12 , 0x0e , 0xca , 0x30 , 0x75 , 0x86 , 0xe1 , 0xa7 };
184
-
185
- const quint8 output[64 ] = { 0x6b , 0xc1 , 0xbe , 0xe2 , 0x2e , 0x40 , 0x9f , 0x96 , 0xe9 , 0x3d , 0x7e , 0x11 , 0x73 , 0x93 , 0x17 , 0x2a ,
186
- 0xae , 0x2d , 0x8a , 0x57 , 0x1e , 0x03 , 0xac , 0x9c , 0x9e , 0xb7 , 0x6f , 0xac , 0x45 , 0xaf , 0x8e , 0x51 ,
187
- 0x30 , 0xc8 , 0x1c , 0x46 , 0xa3 , 0x5c , 0xe4 , 0x11 , 0xe5 , 0xfb , 0xc1 , 0x19 , 0x1a , 0x0a , 0x52 , 0xef ,
188
- 0xf6 , 0x9f , 0x24 , 0x45 , 0xdf , 0x4f , 0x9b , 0x17 , 0xad , 0x2b , 0x41 , 0x7b , 0xe6 , 0x6c , 0x37 , 0x10 };
189
-
190
-
191
- for (int i=0 ; i<64 ; i++){
192
- if (i<16 ) {
193
- keyHex.append (key[i]);
194
- ivHex.append (iv[i]);
195
- }
196
- hexText.append (text[i]);
197
- outputHex.append (output[i]);
198
-
199
- }
200
-
201
- QCOMPARE (encryption.decode (hexText, keyHex, ivHex), outputHex);
134
+ QCOMPARE (encryption.decode (outCBC128, key16, iv), inCBC128);
202
135
}
0 commit comments