Debian Codegraph @

src/lib/crypto/test/AESTests.cpp

raw
1 /*
2 * Copyright (c) 2010 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 AESTests.cpp
29
30 Contains test cases to test the AES implementation
31 *****************************************************************************/
32
33 #include <stdlib.h>
34 #include <cppunit/extensions/HelperMacros.h>
35 #include "AESTests.h"
36 #include "CryptoFactory.h"
37 #include "AESKey.h"
38 #include <stdio.h>
39
40 CPPUNIT_TEST_SUITE_REGISTRATION(AESTests);
41
42 void AESTests::setUp()
43 {
44 aes = NULL;
45
46 aes = CryptoFactory::i()->getSymmetricAlgorithm(SymAlgo::AES);
47
48 // Check the return value
49 CPPUNIT_ASSERT(aes != NULL);
50 }
51
52 void AESTests::tearDown()
53 {
54 if (aes != NULL)
55 {
56 CryptoFactory::i()->recycleSymmetricAlgorithm(aes);
57 }
58
59 fflush(stdout);
60 }
61
62 void AESTests::testBlockSize()
63 {
64 CPPUNIT_ASSERT(aes->getBlockSize() == 16);
65 }
66
67 void AESTests::testCBC()
68 {
69 char testKeys128[][33] =
70 {
71 "00000000000000000000000000000000",
72 "0102030405060708090A0B0C0D0E0F10",
73 "404142434445464748494A4B4C4D4E4F",
74 "89436760984679018453504364534464",
75 "49587346983643545706904580436731"
76 };
77
78 char testKeys192[][49] =
79 {
80 "000000000000000000000000000000000000000000000000",
81 "0102030405060708090A0B0C0D0E0F101213141516171819",
82 "404142434445464748494A4B4C4D4E4F5051525354555657",
83 "096874395874290867409857496743857632098479834634",
84 "439867439058743095864395348375043296845094854983"
85 };
86
87 char testKeys256[][65] =
88 {
89 "0000000000000000000000000000000000000000000000000000000000000000",
90 "0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20",
91 "404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F",
92 "4394398576098257436095746985679043867498572406874967416846341641",
93 "4369006859867098670492857409386741095643756930847023587048579014"
94 };
95
96 char testData[][256] =
97 {
98 "4938673409687134684698438657403986439058740935874395813968496846",
99 "549813644389670948567490687546098245665626527788",
100 "64398769586792586795867965624526",
101 "468376458463264536"
102 };
103
104 char testResult[5][4][3][256] = {
105 {
106 {
107 "6CAEC72F5E101C66550215ACAB6B874C62E7BD074C0A09A8EE4562EFCB4E560A3E90FA0F50391087824FC27F57618E5C",
108 "E20E3123AC64FCA5536E0A2DC48DBEBCECB3F260EFF4A0EB99D72F57EF38DED336EB9DD0B968D24C91E63974E7445A21",
109 "C7910B1634DB493998608875A4652B20C64202ED507D9DBA06F62EB20A63C32FB6C9669D42A0AC29D773E6D40A63A2AC"
110 },
111 {
112 "8F48A65BF638FEDB7E6F59BAC8C110FEBA933F106D564119B88569E758B7FB83",
113 "125E1D93DC2C43A6FAFC508DB6F9A4A9F390D102C2300F0A3617CE95027BFAA3",
114 "FACB8DEF1B476400DE9796D5058E9086ECF04C927F5C160161C7A34D8288EB3C"
115 },
116 {
117 "C810E96482F109C9A05D2B1BEBAC7966BB7784F58A5478C1A07EC0DB39F6D87B",
118 "2385391BB8F2DD97280B1FAEFACB6B5C4FE12A2274D6B967509CF18500A640D6",
119 "47549520EADA1A5D931EACCC922F88BA2E386089BF97C790FD2CD38553334AE4"
120 },
121 {
122 "A7D9EAE80224624188CAA7012140E946",
123 "6ECD5C71ECA4AB9C3B71E91721CA2043",
124 "2F77AA438E9259F268985668B00650E5"
125 }
126 },
127 {
128 {
129 "FCB2FB6BF8ED8910F023A934EB9DA550E4D5B469D75B9390F4A207E54F29412450E52E980862DC80B89F6D1D10B68AA5",
130 "7EF0F65513CFE3E0D21305E2ECCBB3554B0DE119720C5A86337E57F74795BC23ED9CB82A951DE3D00D7A0DC8997319DD",
131 "5B83BFDB6EF3AEA5191F2EE3366EDE10480E9459C0DE2994DD9C6408A377DFFF8121A38CFD1AA864559B9A435A3BDD6D"
132 },
133 {
134 "81D667193D42BF19C456F4A1F7070C047D94C7EE8136FA315F938162FDDA20C1",
135 "2EFBA2B689C0F775097F98B569A1F20004F1A75F0C53473969DBE586ABCAE04D",
136 "447326913AA4565951D987F59B48870DD9285EEFCF64B429C2220E4F3E0D9DE2"
137 },
138 {
139 "891DF30BAEA2D24408A9C788D59DBAC7A6F34311813216311E18E9ED7122DB1A",
140 "F1629B62ECDF3CDAA3DA0EBB31EE37691AF4EB2B6F9CF04A9861935B2C167D02",
141 "C7BD348D5E6696CB8BA813B96EA5C42C5C3C3629D18FC9DAF1B50A0AE4843C5B"
142 },
143 {
144 "91C44D109D46C8E8656793680D43BE94",
145 "E68D8E49A19F155B7ED7253120B0D117",
146 "FAED8666F695C85283ECF51C96DB41CE"
147 }
148 },
149 {
150 {
151 "0CCFB49FE2B7E93A556E56B2C616885FBB0515F55A4210FE2F492A4775F078655CB21691CA6A54819C2D885954809D00",
152 "2FC6C785D683FC35304DD161A21FA1B256F9FBB2817F1F3BEBCE7C1E292EC6999641AA6953C0FAB6DFC2942CABD32DFB",
153 "31FAF4E3DA19D2372666AC635FFE361E33AD7865AEF616273D8F3B471F77A0998C6A41497168A65F621D912C54A4AF28"
154 },
155 {
156 "0B3842152A6365ED14AD952ABBBAF0EED2E8F36250DD25DDA301490FDE05219B",
157 "696BF21A887A04E194DCC18719E1BD623D8BD25A0CEF5EC2E21312ACE6C81F40",
158 "A2C8E61471EC80FE39AC0D8F720FEA8F2D23D04596A751C755E51CD357BCA5E0"
159 },
160 {
161 "5E0EA3AFBE191A16854C7960F087958F577EA4F80160F521A12D2211FAC25E16",
162 "F5475B4FD48F969123C9F7FB08C7E902CCA282F167BBAFF1A7C7EDDB7BCBAC76",
163 "380CB860EC6DEF4F9329F4BE826DE1FE61A71629DD978F00BEFB349ACBD0BAD8"
164 },
165 {
166 "BA1452E755E6A43E43B10DD2C1530093",
167 "07CA52926D4E8F2F6055E6E0251CB9E5",
168 "AB99E5FEE195B4433667AD3074A9322E"
169 }
170 },
171 {
172 {
173 "D2D46E577723B30E6B5FC96DC18B2C55E0EACCB07CE07C7F30FD113A987E2A2059AB7DF8985C1AE525EFAD9CE111893C",
174 "CC5B220688AC0231DBC03C8886C0D0109840B9E58FBB1A6B6C261ED9E7979E951818033A25778FF328786D1777790078",
175 "4B4A0B3D6D4E770BECC574BF66CD401942DC4D0DCD0EC65F99B2925B688BB217FCB5C946BE986C440C93279F4670CD43"
176 },
177 {
178 "29C76D62D3C4F7FECCBFD7A73B06E2ECA7AA3B2D4BE79EC945B0B88C813264D8",
179 "B898DCE11F3D6BB2182208E0BBBA7F404FE415D4D6D0772960E7CE3549B9899C",
180 "11E9552009836B51F241E972D680A9F397260163D9D5369BFC1B136FB4206966"
181 },
182 {
183 "539FC6EDAE21EDDC1CC4650367F527467916A6990E540146238AA9CD6B3B4ADE",
184 "2D9DE2BC47DAFCF7867134110C541EBCD72D67B1B23DEF6805DBFF4A4D90EF91",
185 "BF484690835FF61C4A7873A996EEB91F553978A40360E192273D3923E04DFE1E"
186 },
187 {
188 "4910EFFFBF571C98D51802F04A42213E",
189 "EDD1880FC4D41293BC74B98AF3D8A010",
190 "F8BCC258A6CD7FAA4EDF16A3CF5573C4"
191 }
192 },
193 {
194 {
195 "C2061BD0B4274B5CB4E408B492991F4195FDBFCDED1BC57442151B182BA6E8075AAF858F357C262E0034B9B5F839D823",
196 "6EEFEBCC9F8C607D21A158E23980EFA6EC234DC6EA668A446F467F4AE87521F18DC1800D87A5EBC63C444F810557B61D",
197 "00C952BA54614A1F11B0D59F3F469A859F62CCE0D35073B91B461302A7F37BD0B23401482DFCEE66ABD12C05615C9862"
198 },
199 {
200 "B36553D93EEF04AE247DBCBDDB8C039FFAC8AC1B0EF14C2E4BA653F089924451",
201 "90670C9DE58F95431591FA2BE8EA1B4B3F5BDBFC0B5199F94A41E4FC7B6B1645",
202 "A065415413D3A08E4B42A3F2681B8D122167A1E3F92D38C305761D9BF80131F1"
203 },
204 {
205 "12A758F161543F2842138B8C2453C3A05A90BE9F92CB3DD10C40AB9D1D746B49",
206 "DF1F4DDFFE1032C812FC6F35AB2B3A7B0E8D26DA49DEC8F5E08D108DB1283BEA",
207 "FDCB66159E1B5CF1BE9F7271EF2C35D5E9F7485E32D16C6AC865E64619DB8724"
208 },
209 {
210 "1FC224DCB64848B5E8F9FB91C542991F",
211 "1F8C5F65F9205098B47E26894B9154D9",
212 "19763CAC206EDFDEBEDAD9C274DEE1C1"
213 }
214 }
215 };
216
217 char testIV[][33] =
218 {
219 "00000000000000000000000000000000",
220 "0102030405060708090A0B0C0D0E0F10",
221 "404142434445464748494A4B4C4D4E4F",
222 "69836472094875029486750948672066",
223 "48670943876904867104398574908554"
224 };
225
226 for (int i = 0; i < 5; i++)
227 {
228 ByteString keyData128(testKeys128[i]);
229 ByteString keyData192(testKeys192[i]);
230 ByteString keyData256(testKeys256[i]);
231
232 AESKey aesKey128(128);
233 CPPUNIT_ASSERT(aesKey128.setKeyBits(keyData128));
234 AESKey aesKey192(192);
235 CPPUNIT_ASSERT(aesKey192.setKeyBits(keyData192));
236 AESKey aesKey256(256);
237 CPPUNIT_ASSERT(aesKey256.setKeyBits(keyData256));
238
239 ByteString IV(testIV[i]);
240
241 for (int j = 0; j < 4; j++)
242 {
243 ByteString plainText(testData[j]), shsmPlainText;
244 ByteString cipherText;
245 ByteString shsmCipherText, OB;
246
247 // Test 128-bit key
248 cipherText = ByteString(testResult[i][j][0]);
249
250 // Now, do the same thing using our AES implementation
251 shsmCipherText.wipe();
252 CPPUNIT_ASSERT(aes->encryptInit(&aesKey128, SymMode::CBC, IV));
253
254 CPPUNIT_ASSERT(aes->encryptUpdate(plainText, OB));
255 shsmCipherText += OB;
256
257 CPPUNIT_ASSERT(aes->encryptFinal(OB));
258 shsmCipherText += OB;
259
260 CPPUNIT_ASSERT(shsmCipherText == cipherText);
261
262 // Check that we can get the plain text
263 shsmPlainText.wipe();
264 CPPUNIT_ASSERT(aes->decryptInit(&aesKey128, SymMode::CBC, IV));
265
266 CPPUNIT_ASSERT(aes->decryptUpdate(shsmCipherText, OB));
267 shsmPlainText += OB;
268
269 CPPUNIT_ASSERT(aes->decryptFinal(OB));
270 shsmPlainText += OB;
271
272 CPPUNIT_ASSERT(shsmPlainText == plainText);
273
274 // Test 192-bit key
275 cipherText = ByteString(testResult[i][j][1]);
276
277 // Now, do the same thing using our AES implementation
278 shsmCipherText.wipe();
279 CPPUNIT_ASSERT(aes->encryptInit(&aesKey192, SymMode::CBC, IV));
280
281 CPPUNIT_ASSERT(aes->encryptUpdate(plainText, OB));
282 shsmCipherText += OB;
283
284 CPPUNIT_ASSERT(aes->encryptFinal(OB));
285 shsmCipherText += OB;
286
287 CPPUNIT_ASSERT(shsmCipherText == cipherText);
288
289 // Check that we can get the plain text
290 shsmPlainText.wipe();
291 CPPUNIT_ASSERT(aes->decryptInit(&aesKey192, SymMode::CBC, IV));
292
293 CPPUNIT_ASSERT(aes->decryptUpdate(shsmCipherText, OB));
294 shsmPlainText += OB;
295
296 CPPUNIT_ASSERT(aes->decryptFinal(OB));
297 shsmPlainText += OB;
298
299 CPPUNIT_ASSERT(shsmPlainText == plainText);
300
301 // Test 256-bit key
302 cipherText = ByteString(testResult[i][j][2]);
303
304 // Now, do the same thing using our AES implementation
305 shsmCipherText.wipe();
306 CPPUNIT_ASSERT(aes->encryptInit(&aesKey256, SymMode::CBC, IV));
307
308 CPPUNIT_ASSERT(aes->encryptUpdate(plainText, OB));
309 shsmCipherText += OB;
310
311 CPPUNIT_ASSERT(aes->encryptFinal(OB));
312 shsmCipherText += OB;
313
314 CPPUNIT_ASSERT(shsmCipherText == cipherText);
315
316 // Check that we can get the plain text
317 shsmPlainText.wipe();
318 CPPUNIT_ASSERT(aes->decryptInit(&aesKey256, SymMode::CBC, IV));
319
320 CPPUNIT_ASSERT(aes->decryptUpdate(shsmCipherText, OB));
321 shsmPlainText += OB;
322
323 CPPUNIT_ASSERT(aes->decryptFinal(OB));
324 shsmPlainText += OB;
325
326 CPPUNIT_ASSERT(shsmPlainText == plainText);
327 }
328 }
329 }
330
331 void AESTests::testECB()
332 {
333 char testKeys128[][33] =
334 {
335 "00000000000000000000000000000000",
336 "0102030405060708090A0B0C0D0E0F10",
337 "404142434445464748494A4B4C4D4E4F",
338 "89436760984679018453504364534464",
339 "49587346983643545706904580436731"
340 };
341
342 char testKeys192[][49] =
343 {
344 "000000000000000000000000000000000000000000000000",
345 "0102030405060708090A0B0C0D0E0F101213141516171819",
346 "404142434445464748494A4B4C4D4E4F5051525354555657",
347 "096874395874290867409857496743857632098479834634",
348 "439867439058743095864395348375043296845094854983"
349 };
350
351 char testKeys256[][65] =
352 {
353 "0000000000000000000000000000000000000000000000000000000000000000",
354 "0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20",
355 "404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F",
356 "4394398576098257436095746985679043867498572406874967416846341641",
357 "4369006859867098670492857409386741095643756930847023587048579014"
358 };
359
360 char testData[][256] =
361 {
362 "4938673409687134684698438657403986439058740935874395813968496846",
363 "549813644389670948567490687546098245665626527788",
364 "64398769586792586795867965624526",
365 "468376458463264536"
366 };
367
368 char testResult[5][4][3][256] = {
369 {
370 {
371 "6CAEC72F5E101C66550215ACAB6B874CD26479922B98D9839D7DA729B557ABA00143DB63EE66B0CDFF9F69917680151E",
372 "E20E3123AC64FCA5536E0A2DC48DBEBC0E4E53BDA45FFC97C677951A891A6B7502BB292527E726FD51EB29894D6F0AAD",
373 "C7910B1634DB493998608875A4652B205750B3B761DA7718E120C23A575F7D821F788FE6D86C317549697FBF0C07FA43"
374 },
375 {
376 "8F48A65BF638FEDB7E6F59BAC8C110FE0E5E6370CBEFACFA0D7A5744030A481B",
377 "125E1D93DC2C43A6FAFC508DB6F9A4A933738D14C219340D5F4D7203DEDCA7E1",
378 "FACB8DEF1B476400DE9796D5058E90863A9A8C8BB2420B9A85BA3E8F87BB48F2"
379 },
380 {
381 "C810E96482F109C9A05D2B1BEBAC79660143DB63EE66B0CDFF9F69917680151E",
382 "2385391BB8F2DD97280B1FAEFACB6B5C02BB292527E726FD51EB29894D6F0AAD",
383 "47549520EADA1A5D931EACCC922F88BA1F788FE6D86C317549697FBF0C07FA43"
384 },
385 {
386 "A7D9EAE80224624188CAA7012140E946",
387 "6ECD5C71ECA4AB9C3B71E91721CA2043",
388 "2F77AA438E9259F268985668B00650E5"
389 }
390 },
391 {
392 {
393 "8947CE273536C8A4D1E878F38371B9A8D2B3B45496779386CBA32CA70001D6AA6CC00A66D2AD83FFD76E9A2BCAD89A01",
394 "B151340CFECADA3AE176637D0A78686E2063E1A602C85D03AE648BDF4FA57C36F7F1878D088644BD5FB43D3C0FE1C30C",
395 "C19AE024C8F6B8E3383F675DF463512E273AAD7D0B88F22D5225EF09D2E37118D45D7C5AA26BCA9D6B1D5DDBF68F9EF6"
396 },
397 {
398 "16C30BBAE7CDB2EE1E02275B79A064F6EE69FB37C8E039400435782F550CF86A",
399 "F6C869D28D2D167C50BEE8F605D33021CB9173567B8B4AB3EEC68F0298324B78",
400 "07DB563F7E31F1E670A02F97E8D120C7EE3FCEBBF2FDC2D37FC17D93ED1A778B"
401 },
402 {
403 "52902B599686234833C4D420A9BF17FF6CC00A66D2AD83FFD76E9A2BCAD89A01",
404 "B17FA9EA89D5578A844B3D82891330B1F7F1878D088644BD5FB43D3C0FE1C30C",
405 "74BC55BE85291E0D1FA4A4444051CF65D45D7C5AA26BCA9D6B1D5DDBF68F9EF6"
406 },
407 {
408 "3F00CAAC6FA432A7C1826CA4DA7C55D6",
409 "9C2DBF449FF2C4AC1CFD7C43D200D33A",
410 "E2183AB600A986806D86ADA4EE38E562"
411 }
412 },
413 {
414 {
415 "23741EF993CBA04E5C67B42A16CA4D100BA6DF745E6D90818500DEC1CFC9811DBD3ACBFC853ED5DE825266C3B1883EC4",
416 "F14D0EB7DFDB9B8960B0E47D7F4828E8756C38BA83655AAC466986ECB229A66FA390265A4BF5F50A8DFFAD253701E418",
417 "0AAE579A796C94AF4FFB9D7C71381CB5E68E15465F30D7085A72D0CCEC7030BBC9CB7B3859E1A550BCBF11B624022C56"
418 },
419 {
420 "AFCD6801459845C88548CC337BDD4D8B87E81D9D6AC945E14E3C4E0AC976A4B9",
421 "117129A4775FC84E703F2F2C54B1B55DC4A79241F6CB0A37A8D551D71983D944",
422 "1128250DA7C9A1BBE6A61AC01F28D4D9E3027C3625BD5514AE5DFE4B9132DAFA"
423 },
424 {
425 "B275BDAF14AE286643C533258343F822BD3ACBFC853ED5DE825266C3B1883EC4",
426 "93ACFCCFE2C4736B6492A673A59DACE2A390265A4BF5F50A8DFFAD253701E418",
427 "2C2165E6491662A855FD7A20CDFF23BDC9CB7B3859E1A550BCBF11B624022C56"
428 },
429 {
430 "30BBF52D760BAEA653FD03E5E84E583A",
431 "F5F55DF3FC4D9CF2A2829BACA774A51A",
432 "7EE196D148C11FA4998A90C6C7932395"
433 }
434 },
435 {
436 {
437 "44C2B3344B002BB7A6994E1C74CC7BA70CE55BF44FD96506B553F0EFD3FE02B28329D59D480B0C1714A0DC60EB9FA8CE",
438 "568C7627FD2519BE6031F052DE8F680860F7680460E92A524EA912174BE17B1337D593DEF15FB5BA64F03D1AAE276775",
439 "99E36C0097BCFE41945C064EFA476FF4AA6048F909BEDE32A649F1035A2FB83CD601D09C3FDB36BC61B8CDE5BDD73804"
440 },
441 {
442 "D748E8E93D29775BD8831E3BC1E1ABF75F484C7CC693521A66A5AB1637822E40",
443 "010103778E123E5140F8D0356DA831E1587DB7E416AE9FAD14C6E2F78DC83148",
444 "A8B8926E8FB762A1AB8CD9FA08507D77872C8EAAAFF3527572F49497B8B366C9"
445 },
446 {
447 "CFF3212C7E94C2DD65EC1CDC998D6C4B8329D59D480B0C1714A0DC60EB9FA8CE",
448 "324D16FEEC9DCA75A80F4B80175F8A7537D593DEF15FB5BA64F03D1AAE276775",
449 "850567F4F0200CFBF88F1A6D35CAF6D9D601D09C3FDB36BC61B8CDE5BDD73804"
450 },
451 {
452 "417F142D7609AE701B3D263FFECE4502",
453 "E0DC994D8DC01C4EB2ECD19AD120C3D1",
454 "FB26CC4E48B40EAB755FF65164EFB406"
455 }
456 },
457 {
458 {
459 "048AEE75741BC60D01B512A53FEE97238F294743E7A351FAF589DD9E040BB8AF0F59D8F60E9C700F10025B5E69828819",
460 "67FB10E52640B1E060F3D7868524721AC375DC76628B0D79C5F40ADC653FC001B50C33356548289D3E70EB7FBC0E2B56",
461 "31953FF249D7519D3C39FB21D70A41033286A320193CC938C5ACBBCA2B25340829799212E3CABACE0BFD4424427705AB"
462 },
463 {
464 "8D8D50FA4619F0E1B821DCA4ACBDFD46AE92D76A0B95A0331D61C4A7032D9705",
465 "10685E8F632AC41D92E3A0403BB20C79868BD6F94691226EC54D7220C45E7233",
466 "C5FF99FD1BC2BFDBEA62894B279DE6CBF51CCD00362A557DB9D9102DAF623A50"
467 },
468 {
469 "0330B8FF58E4E6E956B4F81F7A4770200F59D8F60E9C700F10025B5E69828819",
470 "C79BABE5B34B305B05E38013DC5568F7B50C33356548289D3E70EB7FBC0E2B56",
471 "98BEE460FF803288C898A900DD08CE2529799212E3CABACE0BFD4424427705AB"
472 },
473 {
474 "9A4FD3A26DD0D3A12F224E5E7A06EB76",
475 "B86FE6F088C3A6497F21BCB29DB703D1",
476 "313CC604B301DACA48CDB6F405AA7938"
477 }
478 }
479 };
480
481 char testIV[][33] =
482 {
483 "00000000000000000000000000000000",
484 "0102030405060708090A0B0C0D0E0F10",
485 "404142434445464748494A4B4C4D4E4F",
486 "69836472094875029486750948672066",
487 "48670943876904867104398574908554"
488 };
489
490 for (int i = 0; i < 5; i++)
491 {
492 ByteString keyData128(testKeys128[i]);
493 ByteString keyData192(testKeys192[i]);
494 ByteString keyData256(testKeys256[i]);
495
496 AESKey aesKey128(128);
497 CPPUNIT_ASSERT(aesKey128.setKeyBits(keyData128));
498 AESKey aesKey192(192);
499 CPPUNIT_ASSERT(aesKey192.setKeyBits(keyData192));
500 AESKey aesKey256(256);
501 CPPUNIT_ASSERT(aesKey256.setKeyBits(keyData256));
502
503 ByteString IV(testIV[i]);
504
505 for (int j = 0; j < 4; j++)
506 {
507 ByteString plainText(testData[j]), shsmPlainText;
508 ByteString cipherText;
509 ByteString shsmCipherText, OB;
510
511 // Test 128-bit key
512
513 // Get the reference for the encrypted data
514 cipherText = ByteString(testResult[i][j][0]);
515
516 // Now, do the same thing using our AES implementation
517 shsmCipherText.wipe();
518 CPPUNIT_ASSERT(aes->encryptInit(&aesKey128, SymMode::ECB, IV));
519
520 CPPUNIT_ASSERT(aes->encryptUpdate(plainText, OB));
521 shsmCipherText += OB;
522
523 CPPUNIT_ASSERT(aes->encryptFinal(OB));
524 shsmCipherText += OB;
525
526 CPPUNIT_ASSERT(shsmCipherText == cipherText);
527
528 // Check that we can get the plain text
529 shsmPlainText.wipe();
530 CPPUNIT_ASSERT(aes->decryptInit(&aesKey128, SymMode::ECB, IV));
531
532 CPPUNIT_ASSERT(aes->decryptUpdate(shsmCipherText, OB));
533 shsmPlainText += OB;
534
535 CPPUNIT_ASSERT(aes->decryptFinal(OB));
536 shsmPlainText += OB;
537
538 CPPUNIT_ASSERT(shsmPlainText == plainText);
539
540 // Test 192-bit key
541 cipherText = ByteString(testResult[i][j][1]);
542
543 // Now, do the same thing using our AES implementation
544 shsmCipherText.wipe();
545 CPPUNIT_ASSERT(aes->encryptInit(&aesKey192, SymMode::ECB, IV));
546
547 CPPUNIT_ASSERT(aes->encryptUpdate(plainText, OB));
548 shsmCipherText += OB;
549
550 CPPUNIT_ASSERT(aes->encryptFinal(OB));
551 shsmCipherText += OB;
552
553 CPPUNIT_ASSERT(shsmCipherText == cipherText);
554
555 // Check that we can get the plain text
556 shsmPlainText.wipe();
557 CPPUNIT_ASSERT(aes->decryptInit(&aesKey192, SymMode::ECB, IV));
558
559 CPPUNIT_ASSERT(aes->decryptUpdate(shsmCipherText, OB));
560 shsmPlainText += OB;
561
562 CPPUNIT_ASSERT(aes->decryptFinal(OB));
563 shsmPlainText += OB;
564
565 CPPUNIT_ASSERT(shsmPlainText == plainText);
566
567 // Test 256-bit key
568 cipherText = ByteString(testResult[i][j][2]);
569
570 // Now, do the same thing using our AES implementation
571 shsmCipherText.wipe();
572 CPPUNIT_ASSERT(aes->encryptInit(&aesKey256, SymMode::ECB, IV));
573
574 CPPUNIT_ASSERT(aes->encryptUpdate(plainText, OB));
575 shsmCipherText += OB;
576
577 CPPUNIT_ASSERT(aes->encryptFinal(OB));
578 shsmCipherText += OB;
579
580 CPPUNIT_ASSERT(shsmCipherText == cipherText);
581
582 // Check that we can get the plain text
583 shsmPlainText.wipe();
584 CPPUNIT_ASSERT(aes->decryptInit(&aesKey256, SymMode::ECB, IV));
585
586 CPPUNIT_ASSERT(aes->decryptUpdate(shsmCipherText, OB));
587 shsmPlainText += OB;
588
589 CPPUNIT_ASSERT(aes->decryptFinal(OB));
590 shsmPlainText += OB;
591
592 CPPUNIT_ASSERT(shsmPlainText == plainText);
593 }
594 }
595 }
596
597 void AESTests::testCTR()
598 {
599 // Test vectors from RFC3686
600
601 char testKeys128[][33] =
602 {
603 "AE6852F8121067CC4BF7A5765577F39E",
604 "7E24067817FAE0D743D6CE1F32539163",
605 "7691BE035E5020A8AC6E618529F9A0DC"
606 };
607
608 char testKeys192[][49] =
609 {
610 "16AF5B145FC9F579C175F93E3BFB0EED863D06CCFDB78515",
611 "7C5CB2401B3DC33C19E7340819E0F69C678C3DB8E6F6A91A",
612 "02BF391EE8ECB159B959617B0965279BF59B60A786D3E0FE"
613 };
614
615 char testKeys256[][65] =
616 {
617 "776BEFF2851DB06F4C8A0542C8696F6C6A81AF1EEC96B4D37FC1D689E6C1C104",
618 "F6D66D6BD52D59BB0796365879EFF886C66DD51A5B6A99744B50590C87A23884",
619 "FF7A617CE69148E4F1726E2F43581DE2AA62D9F805532EDFF1EED687FB54153D"
620 };
621
622 char testData[][256] =
623 {
624 "53696E676C6520626C6F636B206D7367",
625 "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F",
626 "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20212223"
627 };
628
629 char testResult[3][3][256] =
630 {
631 {
632 "E4095D4FB7A7B3792D6175A3261311B8",
633 "4B55384FE259C9C84E7935A003CBE928",
634 "145AD01DBF824EC7560863DC71E3E0C0"
635 },
636 {
637 "5104A106168A72D9790D41EE8EDAD388EB2E1EFC46DA57C8FCE630DF9141BE28",
638 "453243FC609B23327EDFAAFA7131CD9F8490701C5AD4A79CFC1FE0FF42F4FB00",
639 "F05E231B3894612C49EE000B804EB2A9B8306B508F839D6A5530831D9344AF1C"
640 },
641 {
642 "C1CF48A89F2FFDD9CF4652E9EFDB72D74540A42BDE6D7836D59A5CEAAEF3105325B2072F",
643 "96893FC55E5C722F540B7DD1DDF7E758D288BC95C69165884536C811662F2188ABEE0935",
644 "EB6C52821D0BBBF7CE7594462ACA4FAAB407DF866569FD07F48CC0B583D6071F1EC0E6B8"
645 }
646 };
647
648 char testCB[3][3][33] =
649 {
650 {
651 "00000030000000000000000000000001",
652 "0000004836733C147D6D93CB00000001",
653 "00000060DB5672C97AA8F0B200000001"
654 },
655 {
656 "006CB6DBC0543B59DA48D90B00000001",
657 "0096B03B020C6EADC2CB500D00000001",
658 "00FAAC24C1585EF15A43D87500000001"
659 },
660 {
661 "00E0017B27777F3F4A1786F000000001",
662 "0007BDFD5CBD60278DCC091200000001",
663 "001CC5B751A51D70A1C1114800000001"
664 }
665 };
666
667 for (int i = 0; i < 3; i++)
668 {
669 ByteString keyData128(testKeys128[i]);
670 ByteString keyData192(testKeys192[i]);
671 ByteString keyData256(testKeys256[i]);
672
673 AESKey aesKey128(128);
674 CPPUNIT_ASSERT(aesKey128.setKeyBits(keyData128));
675 AESKey aesKey192(192);
676 CPPUNIT_ASSERT(aesKey192.setKeyBits(keyData192));
677 AESKey aesKey256(256);
678 CPPUNIT_ASSERT(aesKey256.setKeyBits(keyData256));
679
680
681 ByteString plainText(testData[i]), shsmPlainText;
682 ByteString CB;
683 ByteString cipherText;
684 ByteString shsmCipherText, OB;
685
686 // Test 128-bit key
687 CB = ByteString(testCB[i][0]);
688 cipherText = ByteString(testResult[i][0]);
689
690 // Now, do the same thing using our AES implementation
691 shsmCipherText.wipe();
692 CPPUNIT_ASSERT(aes->encryptInit(&aesKey128, SymMode::CTR, CB));
693
694 CPPUNIT_ASSERT(aes->encryptUpdate(plainText, OB));
695 shsmCipherText += OB;
696
697 CPPUNIT_ASSERT(aes->encryptFinal(OB));
698 shsmCipherText += OB;
699
700 CPPUNIT_ASSERT(shsmCipherText == cipherText);
701
702 // Check that we can get the plain text
703 shsmPlainText.wipe();
704 CPPUNIT_ASSERT(aes->decryptInit(&aesKey128, SymMode::CTR, CB));
705
706 CPPUNIT_ASSERT(aes->decryptUpdate(shsmCipherText, OB));
707 shsmPlainText += OB;
708
709 CPPUNIT_ASSERT(aes->decryptFinal(OB));
710 shsmPlainText += OB;
711
712 CPPUNIT_ASSERT(shsmPlainText == plainText);
713
714 // Test 192-bit key
715 CB = ByteString(testCB[i][1]);
716 cipherText = ByteString(testResult[i][1]);
717
718 // Now, do the same thing using our AES implementation
719 shsmCipherText.wipe();
720 CPPUNIT_ASSERT(aes->encryptInit(&aesKey192, SymMode::CTR, CB));
721
722 CPPUNIT_ASSERT(aes->encryptUpdate(plainText, OB));
723 shsmCipherText += OB;
724
725 CPPUNIT_ASSERT(aes->encryptFinal(OB));
726 shsmCipherText += OB;
727
728 CPPUNIT_ASSERT(shsmCipherText == cipherText);
729
730 // Check that we can get the plain text
731 shsmPlainText.wipe();
732 CPPUNIT_ASSERT(aes->decryptInit(&aesKey192, SymMode::CTR, CB));
733
734 CPPUNIT_ASSERT(aes->decryptUpdate(shsmCipherText, OB));
735 shsmPlainText += OB;
736
737 CPPUNIT_ASSERT(aes->decryptFinal(OB));
738 shsmPlainText += OB;
739
740 CPPUNIT_ASSERT(shsmPlainText == plainText);
741
742 // Test 256-bit key
743 CB = ByteString(testCB[i][2]);
744 cipherText = ByteString(testResult[i][2]);
745
746 // Now, do the same thing using our AES implementation
747 shsmCipherText.wipe();
748 CPPUNIT_ASSERT(aes->encryptInit(&aesKey256, SymMode::CTR, CB));
749
750 CPPUNIT_ASSERT(aes->encryptUpdate(plainText, OB));
751 shsmCipherText += OB;
752
753 CPPUNIT_ASSERT(aes->encryptFinal(OB));
754 shsmCipherText += OB;
755
756 CPPUNIT_ASSERT(shsmCipherText == cipherText);
757
758 // Check that we can get the plain text
759 shsmPlainText.wipe();
760 CPPUNIT_ASSERT(aes->decryptInit(&aesKey256, SymMode::CTR, CB));
761
762 CPPUNIT_ASSERT(aes->decryptUpdate(shsmCipherText, OB));
763 shsmPlainText += OB;
764
765 CPPUNIT_ASSERT(aes->decryptFinal(OB));
766 shsmPlainText += OB;
767
768 CPPUNIT_ASSERT(shsmPlainText == plainText);
769 }
770 }
771
772 void AESTests::testGCM()
773 {
774 // Test vectors from NIST via Botan
775
776 char test128[8][6][256] =
777 {
778 {
779 "00000000000000000000000000000000",
780 "000000000000000000000000",
781 "",
782 "",
783 "10",
784 "58E2FCCEFA7E3061367F1D57A4E7455A"
785 },
786 {
787 "00000000000000000000000000000000",
788 "000000000000000000000000",
789 "00000000000000000000000000000000",
790 "",
791 "10",
792 "0388DACE60B6A392F328C2B971B2FE78AB6E47D42CEC13BDF53A67B21257BDDF"
793 },
794 {
795 "FEFFE9928665731C6D6A8F9467308308",
796 "CAFEBABEFACEDBADDECAF888",
797 "D9313225F88406E5A55909C5AFF5269A86A7A9531534F7DA2E4C303D8A318A721C3C0C95956809532FCF0E2449A6B525B16AEDF5AA0DE657BA637B391AAFD255",
798 "",
799 "10",
800 "42831EC2217774244B7221B784D0D49CE3AA212F2C02A4E035C17E2329ACA12E21D514B25466931C7D8F6A5AAC84AA051BA30B396A0AAC973D58E091473F59854D5C2AF327CD64A62CF35ABD2BA6FAB4"
801 },
802 {
803 "FEFFE9928665731C6D6A8F9467308308",
804 "CAFEBABEFACEDBADDECAF888",
805 "D9313225F88406E5A55909C5AFF5269A86A7A9531534F7DA2E4C303D8A318A721C3C0C95956809532FCF0E2449A6B525B16AEDF5AA0DE657BA637B39",
806 "FEEDFACEDEADBEEFFEEDFACEDEADBEEFABADDAD2",
807 "10",
808 "42831EC2217774244B7221B784D0D49CE3AA212F2C02A4E035C17E2329ACA12E21D514B25466931C7D8F6A5AAC84AA051BA30B396A0AAC973D58E0915BC94FBC3221A5DB94FAE95AE7121A47"
809 },
810 {
811 "FEFFE9928665731C6D6A8F9467308308",
812 "CAFEBABEFACEDBAD",
813 "D9313225F88406E5A55909C5AFF5269A86A7A9531534F7DA2E4C303D8A318A721C3C0C95956809532FCF0E2449A6B525B16AEDF5AA0DE657BA637B39",
814 "FEEDFACEDEADBEEFFEEDFACEDEADBEEFABADDAD2",
815 "10",
816 "61353B4C2806934A777FF51FA22A4755699B2A714FCDC6F83766E5F97B6C742373806900E49F24B22B097544D4896B424989B5E1EBAC0F07C23F45983612D2E79E3B0785561BE14AACA2FCCB"
817 },
818 {
819 "FEFFE9928665731C6D6A8F9467308308",
820 "9313225DF88406E555909C5AFF5269AA6A7A9538534F7DA1E4C303D2A318A728C3C0C95156809539FCF0E2429A6B525416AEDBF5A0DE6A57A637B39B",
821 "D9313225F88406E5A55909C5AFF5269A86A7A9531534F7DA2E4C303D8A318A721C3C0C95956809532FCF0E2449A6B525B16AEDF5AA0DE657BA637B39",
822 "FEEDFACEDEADBEEFFEEDFACEDEADBEEFABADDAD2",
823 "10",
824 "8CE24998625615B603A033ACA13FB894BE9112A5C3A211A8BA262A3CCA7E2CA701E4A9A4FBA43C90CCDCB281D48C7C6FD62875D2ACA417034C34AEE5619CC5AEFFFE0BFA462AF43C1699D050"
825 },
826 {
827 "FEFFE9928665731C6D6A8F9467308308",
828 "CAFEBABEFACEDBAD",
829 "D9313225F88406E5A55909C5AFF5269A86A7A9531534F7DA2E4C303D8A318A721C3C0C95956809532FCF0E2449A6B525B16AEDF5AA0DE657BA637B39",
830 "FEEDFACEDEADBEEFFEEDFACEDEADBEEFABADDAD2",
831 "C",
832 "61353B4C2806934A777FF51FA22A4755699B2A714FCDC6F83766E5F97B6C742373806900E49F24B22B097544D4896B424989B5E1EBAC0F07C23F45983612D2E79E3B0785561BE14A"
833 },
834 {
835 "FEFFE9928665731C6D6A8F9467308308",
836 "9313225DF88406E555909C5AFF5269AA6A7A9538534F7DA1E4C303D2A318A728C3C0C95156809539FCF0E2429A6B525416AEDBF5A0DE6A57A637B39B",
837 "D9313225F88406E5A55909C5AFF5269A86A7A9531534F7DA2E4C303D8A318A721C3C0C95956809532FCF0E2449A6B525B16AEDF5AA0DE657BA637B39",
838 "FEEDFACEDEADBEEFFEEDFACEDEADBEEFABADDAD2",
839 "C",
840 "8CE24998625615B603A033ACA13FB894BE9112A5C3A211A8BA262A3CCA7E2CA701E4A9A4FBA43C90CCDCB281D48C7C6FD62875D2ACA417034C34AEE5619CC5AEFFFE0BFA462AF43C"
841 }
842 };
843
844 char test192[8][6][256] =
845 {
846 {
847 "000000000000000000000000000000000000000000000000",
848 "000000000000000000000000",
849 "",
850 "",
851 "10",
852 "cd33b28ac773f74ba00ed1f312572435"
853 },
854 {
855 "000000000000000000000000000000000000000000000000",
856 "000000000000000000000000",
857 "00000000000000000000000000000000",
858 "",
859 "10",
860 "98e7247c07f0fe411c267e4384b0f6002ff58d80033927ab8ef4d4587514f0fb"
861 },
862 {
863 "feffe9928665731c6d6a8f9467308308feffe9928665731c",
864 "cafebabefacedbaddecaf888",
865 "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255",
866 "",
867 "10",
868 "3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710acade2569924a7c8587336bfb118024db8674a14"
869 },
870 {
871 "feffe9928665731c6d6a8f9467308308feffe9928665731c",
872 "cafebabefacedbaddecaf888",
873 "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
874 "feedfacedeadbeeffeedfacedeadbeefabaddad2",
875 "10",
876 "3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda27102519498e80f1478f37ba55bd6d27618c"
877 },
878 {
879 "feffe9928665731c6d6a8f9467308308feffe9928665731c",
880 "cafebabefacedbad",
881 "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
882 "feedfacedeadbeeffeedfacedeadbeefabaddad2",
883 "10",
884 "0f10f599ae14a154ed24b36e25324db8c566632ef2bbb34f8347280fc4507057fddc29df9a471f75c66541d4d4dad1c9e93a19a58e8b473fa0f062f765dcc57fcf623a24094fcca40d3533f8"
885 },
886 {
887 "feffe9928665731c6d6a8f9467308308feffe9928665731c",
888 "9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
889 "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
890 "feedfacedeadbeeffeedfacedeadbeefabaddad2",
891 "10",
892 "d27e88681ce3243c4830165a8fdcf9ff1de9a1d8e6b447ef6ef7b79828666e4581e79012af34ddd9e2f037589b292db3e67c036745fa22e7e9b7373bdcf566ff291c25bbb8568fc3d376a6d9"
893 },
894 {
895 "feffe9928665731c6d6a8f9467308308feffe9928665731c",
896 "cafebabefacedbaddecaf888",
897 "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
898 "feedfacedeadbeeffeedfacedeadbeefabaddad2",
899 "C",
900 "3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda27102519498e80f1478f37ba55bd"
901 },
902 {
903 "feffe9928665731c6d6a8f9467308308feffe9928665731c",
904 "cafebabefacedbad",
905 "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
906 "feedfacedeadbeeffeedfacedeadbeefabaddad2",
907 "C",
908 "0f10f599ae14a154ed24b36e25324db8c566632ef2bbb34f8347280fc4507057fddc29df9a471f75c66541d4d4dad1c9e93a19a58e8b473fa0f062f765dcc57fcf623a24094fcca4"
909 }
910 };
911
912 char test256[8][6][256] =
913 {
914 {
915 "0000000000000000000000000000000000000000000000000000000000000000",
916 "000000000000000000000000",
917 "",
918 "",
919 "10",
920 "530f8afbc74536b9a963b4f1c4cb738b"
921 },
922 {
923 "0000000000000000000000000000000000000000000000000000000000000000",
924 "000000000000000000000000",
925 "00000000000000000000000000000000",
926 "",
927 "10",
928 "cea7403d4d606b6e074ec5d3baf39d18d0d1c8a799996bf0265b98b5d48ab919"
929 },
930 {
931 "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
932 "cafebabefacedbaddecaf888",
933 "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255",
934 "",
935 "10",
936 "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662898015adb094dac5d93471bdec1a502270e3cc6c"
937 },
938 {
939 "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
940 "cafebabefacedbaddecaf888",
941 "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
942 "feedfacedeadbeeffeedfacedeadbeefabaddad2",
943 "10",
944 "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f66276fc6ece0f4e1768cddf8853bb2d551b"
945 },
946 {
947 "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
948 "cafebabefacedbad",
949 "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
950 "feedfacedeadbeeffeedfacedeadbeefabaddad2",
951 "10",
952 "c3762df1ca787d32ae47c13bf19844cbaf1ae14d0b976afac52ff7d79bba9de0feb582d33934a4f0954cc2363bc73f7862ac430e64abe499f47c9b1f3a337dbf46a792c45e454913fe2ea8f2",
953 },
954 {
955 "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
956 "9313225df88406e555909c5aff5269aa6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b525416aedbf5a0de6a57a637b39b",
957 "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
958 "feedfacedeadbeeffeedfacedeadbeefabaddad2",
959 "10",
960 "5a8def2f0c9e53f1f75d7853659e2a20eeb2b22aafde6419a058ab4f6f746bf40fc0c3b780f244452da3ebf1c5d82cdea2418997200ef82e44ae7e3fa44a8266ee1c8eb0c8b5d4cf5ae9f19a"
961 },
962 {
963 "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
964 "cafebabefacedbaddecaf888",
965 "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
966 "feedfacedeadbeeffeedfacedeadbeefabaddad2",
967 "C",
968 "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f66276fc6ece0f4e1768cddf8853"
969 },
970 {
971 "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
972 "cafebabefacedbad",
973 "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
974 "feedfacedeadbeeffeedfacedeadbeefabaddad2",
975 "C",
976 "c3762df1ca787d32ae47c13bf19844cbaf1ae14d0b976afac52ff7d79bba9de0feb582d33934a4f0954cc2363bc73f7862ac430e64abe499f47c9b1f3a337dbf46a792c45e454913"
977 }
978 };
979
980 for (int i = 0; i < 8; i++)
981 {
982 ByteString keyData128(test128[i][0]);
983 ByteString keyData192(test192[i][0]);
984 ByteString keyData256(test256[i][0]);
985
986 AESKey aesKey128(128);
987 CPPUNIT_ASSERT(aesKey128.setKeyBits(keyData128));
988 AESKey aesKey192(192);
989 CPPUNIT_ASSERT(aesKey192.setKeyBits(keyData192));
990 AESKey aesKey256(256);
991 CPPUNIT_ASSERT(aesKey256.setKeyBits(keyData256));
992
993 ByteString IV;
994 ByteString plainText;
995 ByteString AAD;
996 size_t tagBits;
997 ByteString cipherText;
998
999 ByteString shsmPlainText;
1000 ByteString shsmCipherText;
1001 ByteString OB;
1002
1003 // Test 128-bit key
1004 IV = ByteString(test128[i][1]);
1005 plainText = ByteString(test128[i][2]);
1006 AAD = ByteString(test128[i][3]);
1007 tagBits = ByteString(test128[i][4]).long_val();
1008 cipherText = ByteString(test128[i][5]);
1009
1010 // Now, do the same thing using our AES implementation
1011 shsmCipherText.wipe();
1012 CPPUNIT_ASSERT(aes->encryptInit(&aesKey128, SymMode::GCM, IV, true, 0, AAD, tagBits));
1013
1014 CPPUNIT_ASSERT(aes->encryptUpdate(plainText, OB));
1015 shsmCipherText += OB;
1016
1017 CPPUNIT_ASSERT(aes->encryptFinal(OB));
1018 shsmCipherText += OB;
1019
1020 CPPUNIT_ASSERT(shsmCipherText == cipherText);
1021
1022 // Check that we can get the plain text
1023 shsmPlainText.wipe();
1024 CPPUNIT_ASSERT(aes->decryptInit(&aesKey128, SymMode::GCM, IV, true, 0, AAD, tagBits));
1025
1026 CPPUNIT_ASSERT(aes->decryptUpdate(shsmCipherText, OB));
1027 CPPUNIT_ASSERT(OB.size() == 0);
1028
1029 CPPUNIT_ASSERT(aes->decryptFinal(OB));
1030 shsmPlainText += OB;
1031
1032 CPPUNIT_ASSERT(shsmPlainText == plainText);
1033
1034 // Test 192-bit key
1035 IV = ByteString(test192[i][1]);
1036 plainText = ByteString(test192[i][2]);
1037 AAD = ByteString(test192[i][3]);
1038 tagBits = ByteString(test192[i][4]).long_val();
1039 cipherText = ByteString(test192[i][5]);
1040
1041 // Now, do the same thing using our AES implementation
1042 shsmCipherText.wipe();
1043 CPPUNIT_ASSERT(aes->encryptInit(&aesKey192, SymMode::GCM, IV, true, 0, AAD, tagBits));
1044
1045 CPPUNIT_ASSERT(aes->encryptUpdate(plainText, OB));
1046 shsmCipherText += OB;
1047
1048 CPPUNIT_ASSERT(aes->encryptFinal(OB));
1049 shsmCipherText += OB;
1050 CPPUNIT_ASSERT(shsmCipherText == cipherText);
1051
1052 // Check that we can get the plain text
1053 shsmPlainText.wipe();
1054 CPPUNIT_ASSERT(aes->decryptInit(&aesKey192, SymMode::GCM, IV, true, 0, AAD, tagBits));
1055
1056 CPPUNIT_ASSERT(aes->decryptUpdate(shsmCipherText, OB));
1057 CPPUNIT_ASSERT(OB.size() == 0);
1058
1059 CPPUNIT_ASSERT(aes->decryptFinal(OB));
1060 shsmPlainText += OB;
1061
1062 CPPUNIT_ASSERT(shsmPlainText == plainText);
1063
1064 // Test 256-bit key
1065 IV = ByteString(test256[i][1]);
1066 plainText = ByteString(test256[i][2]);
1067 AAD = ByteString(test256[i][3]);
1068 tagBits = ByteString(test256[i][4]).long_val();
1069 cipherText = ByteString(test256[i][5]);
1070
1071 // Now, do the same thing using our AES implementation
1072 shsmCipherText.wipe();
1073 CPPUNIT_ASSERT(aes->encryptInit(&aesKey256, SymMode::GCM, IV, true, 0, AAD, tagBits));
1074
1075 CPPUNIT_ASSERT(aes->encryptUpdate(plainText, OB));
1076 shsmCipherText += OB;
1077
1078 CPPUNIT_ASSERT(aes->encryptFinal(OB));
1079 shsmCipherText += OB;
1080
1081 CPPUNIT_ASSERT(shsmCipherText == cipherText);
1082
1083 // Check that we can get the plain text
1084 shsmPlainText.wipe();
1085 CPPUNIT_ASSERT(aes->decryptInit(&aesKey256, SymMode::GCM, IV, true, 0, AAD, tagBits));
1086
1087 CPPUNIT_ASSERT(aes->decryptUpdate(shsmCipherText, OB));
1088 CPPUNIT_ASSERT(OB.size() == 0);
1089
1090 CPPUNIT_ASSERT(aes->decryptFinal(OB));
1091 shsmPlainText += OB;
1092
1093 CPPUNIT_ASSERT(shsmPlainText == plainText);
1094 }
1095 }
1096
1097 void AESTests::testWrap(const char testKeK[][128], const char testKey[][128], const char testCt[][128], const int testCnt, SymWrap::Type mode)
1098 {
1099 for (int i = 0; i < testCnt; i++)
1100 {
1101 ByteString kekData(testKeK[i]);
1102 ByteString keyData(testKey[i]);
1103
1104 AESKey aesKeK(kekData.size() * 8);
1105 CPPUNIT_ASSERT(aesKeK.setKeyBits(kekData));
1106
1107 ByteString wrapped;
1108 ByteString expectedCt(testCt[i]);
1109 CPPUNIT_ASSERT(aes->wrapKey(&aesKeK, mode, keyData, wrapped));
1110 CPPUNIT_ASSERT(wrapped.size() == expectedCt.size());
1111 CPPUNIT_ASSERT(wrapped == expectedCt);
1112
1113 ByteString unwrapped;
1114 CPPUNIT_ASSERT(aes->unwrapKey(&aesKeK, mode, wrapped, unwrapped));
1115 CPPUNIT_ASSERT(unwrapped.size() == keyData.size());
1116 CPPUNIT_ASSERT(unwrapped == keyData);
1117 /*
1118 #ifdef HAVE_AES_KEY_WRAP_PAD
1119 keyData.resize(20);
1120 ByteString padwrapped;
1121 CPPUNIT_ASSERT(aes->wrapKey(&aesKeK, SymWrap::AES_KEYWRAP_PAD, keyData, padwrapped));
1122 CPPUNIT_ASSERT(padwrapped.size() == 32);
1123
1124 ByteString padunwrapped;
1125 CPPUNIT_ASSERT(aes->unwrapKey(&aesKeK, SymWrap::AES_KEYWRAP_PAD, padwrapped, padunwrapped));
1126 CPPUNIT_ASSERT(padunwrapped == keyData);
1127 #endif
1128 */
1129 }
1130 }
1131
1132 // RFC 3394 tests
1133 void AESTests::testWrapWoPad()
1134 {
1135 char testKeK[][128] = {
1136 "000102030405060708090A0B0C0D0E0F", // section 4.1
1137 "000102030405060708090A0B0C0D0E0F1011121314151617", // section 4.2
1138 "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F", // section 4.3
1139 "000102030405060708090A0B0C0D0E0F1011121314151617", // section 4.4
1140 "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F", // section 4.5
1141 "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F", // section 4.6
1142 };
1143 char testKey[][128] = {
1144 "00112233445566778899AABBCCDDEEFF",
1145 "00112233445566778899AABBCCDDEEFF",
1146 "00112233445566778899AABBCCDDEEFF",
1147 "00112233445566778899AABBCCDDEEFF0001020304050607",
1148 "00112233445566778899AABBCCDDEEFF0001020304050607",
1149 "00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F"
1150 };
1151 char testCt[][128] = {
1152 "1FA68B0A8112B447AEF34BD8FB5A7B829D3E862371D2CFE5",
1153 "96778B25AE6CA435F92B5B97C050AED2468AB8A17AD84E5D",
1154 "64E8C3F9CE0F5BA263E9777905818A2A93C8191E7D6E8AE7",
1155 "031D33264E15D33268F24EC260743EDCE1C6C7DDEE725A936BA814915C6762D2",
1156 "A8F9BC1612C68B3FF6E6F4FBE30E71E4769C8B80A32CB8958CD5D17D6B254DA1",
1157 "28C9F404C4B810F4CBCCB35CFB87F8263F5786E2D80ED326CBC7F0E71A99F43BFB988B9B7A02DD21"
1158 };
1159
1160 testWrap(testKeK, testKey, testCt, sizeof(testKeK) / 128, SymWrap::AES_KEYWRAP);
1161 }
1162
1163 // RFC 5649 tests
1164 void AESTests::testWrapPad()
1165 {
1166 char testKeK[][128] = {
1167 "5840DF6E29B02AF1AB493B705BF16EA1AE8338F4DCC176A8", // section 6 example 1
1168 "5840DF6E29B02AF1AB493B705BF16EA1AE8338F4DCC176A8", // section 6 example 2
1169 };
1170 char testKey[][128] = {
1171 "C37B7E6492584340BED12207808941155068F738",
1172 "466F7250617369"
1173 };
1174 char testCt[][128] = {
1175 "138BDEAA9B8FA7FC61F97742E72248EE5AE6AE5360D1AE6A5F54F373FA543B6A",
1176 "AFBEB0F07DFBF5419200F2CCB50BB24F"
1177 };
1178
1179 testWrap(testKeK, testKey, testCt, sizeof(testKeK) / 128, SymWrap::AES_KEYWRAP_PAD);
1180 }