|
1 /* |
|
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 /** |
|
20 @file |
|
21 @internalComponent |
|
22 @released |
|
23 */ |
|
24 #include <cryptodriver.h> |
|
25 #include <e32base.h> |
|
26 #include <e32cons.h> |
|
27 #include <e32test.h> |
|
28 #include <e32debug.h> |
|
29 #include <rijndael.h> |
|
30 #include <cbcmode.h> |
|
31 #include <padding.h> |
|
32 #include <bufferedtransformation.h> |
|
33 |
|
34 _LIT(KTxtEPOC32EX,"tasync: mainL failed"); |
|
35 _LIT(KTxtPressAnyKey," [press any key]"); |
|
36 |
|
37 //#define KEYLEN 16 |
|
38 #define KEYLEN 24 |
|
39 //#define KEYLEN 32 |
|
40 |
|
41 //#define BUFLEN 256 |
|
42 //#define BUFLEN (256*16) |
|
43 #define BUFLEN 32 |
|
44 #define LOOPCOUNT 10000 |
|
45 |
|
46 |
|
47 // public |
|
48 CConsoleBase* console; // write all your messages to this |
|
49 |
|
50 // private |
|
51 LOCAL_C void mainL(); |
|
52 |
|
53 GLDEF_C TInt E32Main() // main function called by E32 |
|
54 { |
|
55 __UHEAP_MARK; |
|
56 CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack |
|
57 TRAPD(error,mainL()); // more initialization, then do example |
|
58 __ASSERT_ALWAYS(!error,User::Panic(KTxtEPOC32EX,error)); |
|
59 delete cleanup; // destroy clean-up stack |
|
60 __UHEAP_MARKEND; |
|
61 return 0; // and return |
|
62 } |
|
63 |
|
64 LOCAL_D RTest test(_L("tasync")); |
|
65 |
|
66 LOCAL_D void dumpBuffer(const TDesC &aName, const TDesC8 &aBuf); |
|
67 |
|
68 LOCAL_C void encryptL(const TDesC8 &aKey, const TDesC8 &aIV, const TDesC8 &aPlainText, |
|
69 TDes8 &aCipherText); |
|
70 LOCAL_C void decryptL(const TDesC8 &aKey, const TDesC8 &aIV, const TDesC8 &aCiphertext, |
|
71 TDes8 &aPlaintext); |
|
72 |
|
73 LOCAL_C void mainL() // initialize and call example code under cleanup stack |
|
74 { |
|
75 test.Title(); |
|
76 // |
|
77 // Generate key and IV |
|
78 // |
|
79 test.Start(_L(" @SYMTestCaseID:SEC-CRYPTOSPI-ASYNC-0001 Generating key & IV for AES tests ")); |
|
80 test.Next(_L("Generating key\n")); |
|
81 // Generate 16 byte key |
|
82 TBuf8<KEYLEN> key; |
|
83 key.SetLength(KEYLEN); |
|
84 key[0] = 'K'; |
|
85 key[1] = 'E'; |
|
86 key[2] = 'Y'; |
|
87 key[3] = '*'; |
|
88 for(int z=4; z<KEYLEN; ++z) key[z] = z; |
|
89 |
|
90 |
|
91 test.Next(_L("Generating IV\n")); |
|
92 TBuf8<16> iv; |
|
93 iv.SetLength(iv.MaxLength()); |
|
94 iv[0] = 'I'; |
|
95 iv[1] = 'V'; |
|
96 iv[2] = '*'; |
|
97 iv[3] = '*'; |
|
98 |
|
99 TBuf8<BUFLEN> plaintext; |
|
100 plaintext.FillZ(); |
|
101 plaintext.SetLength(plaintext.MaxLength()); |
|
102 plaintext[0] = 'P'; |
|
103 plaintext[1] = 'L'; |
|
104 plaintext[2] = 'A'; |
|
105 plaintext[3] = 'I'; |
|
106 plaintext[4] = 'N'; |
|
107 plaintext[5] = '1'; |
|
108 for(int i=6; i<BUFLEN/2; ++i) |
|
109 { |
|
110 plaintext[i] = i; |
|
111 } |
|
112 plaintext[BUFLEN/2] = 'P'; |
|
113 plaintext[BUFLEN/2+1] = 'L'; |
|
114 plaintext[BUFLEN/2+2] = 'A'; |
|
115 plaintext[BUFLEN/2+3] = 'I'; |
|
116 plaintext[BUFLEN/2+4] = 'N'; |
|
117 plaintext[BUFLEN/2+5] = '2'; |
|
118 for(int i=BUFLEN/2+6; i<BUFLEN; ++i) |
|
119 { |
|
120 plaintext[i] = i; |
|
121 } |
|
122 test.End(); |
|
123 |
|
124 TBuf8<BUFLEN+16> ciphertext; |
|
125 encryptL(key, iv, plaintext, ciphertext); |
|
126 |
|
127 TBuf8<BUFLEN> plaintext2; |
|
128 decryptL(key, iv, ciphertext, plaintext2); |
|
129 |
|
130 test.Printf(_L("Checking plaintext2 == plaintext\n")); |
|
131 test(plaintext2 == plaintext); |
|
132 |
|
133 test.Printf(KTxtPressAnyKey); |
|
134 test.Getch(); // get and ignore character |
|
135 test.Close(); |
|
136 |
|
137 } |
|
138 |
|
139 LOCAL_C void encryptL(const TDesC8 &aKey, const TDesC8 &aIV, const TDesC8 &aPlaintext, |
|
140 TDes8 &aCiphertext) |
|
141 { |
|
142 RDebug::Printf("Starting tasync encryption tests\n"); |
|
143 |
|
144 TPtrC8 chunk1(aPlaintext.Left(16)); |
|
145 TPtrC8 chunk2(aPlaintext.Mid(16, aPlaintext.Length()-16)); |
|
146 |
|
147 |
|
148 // |
|
149 // Encrypt using legacy API - Reference |
|
150 // |
|
151 TBuf8<BUFLEN+16> sw; |
|
152 sw.FillZ(); |
|
153 sw.SetLength(0); |
|
154 |
|
155 test.Start(_L("AES - S/W - Reference encrypt")); |
|
156 |
|
157 test.Printf(_L(" CBC\n")); |
|
158 CAESEncryptor *rawaes = CAESEncryptor::NewLC(aKey); // rawaes |
|
159 CModeCBCEncryptor *cbc = CModeCBCEncryptor::NewL(rawaes, aIV); |
|
160 CleanupStack::Pop(rawaes); // |
|
161 CleanupStack::PushL(cbc); // cbc |
|
162 |
|
163 CPadding *pad = CPaddingPKCS7::NewLC(16); // cbc, pad |
|
164 CBufferedEncryptor *aes = CBufferedEncryptor::NewL(cbc, pad); |
|
165 CleanupStack::Pop(pad); // cbc |
|
166 CleanupStack::Pop(cbc); |
|
167 CleanupStack::PushL(aes); // aes |
|
168 |
|
169 test.Printf(_L("About to s/w encrypt (old api)\n")); |
|
170 aes->Process(chunk1, sw); |
|
171 aes->ProcessFinalL(chunk2, sw); |
|
172 dumpBuffer(_L("sw"), sw); |
|
173 |
|
174 CleanupStack::PopAndDestroy(aes); |
|
175 |
|
176 // |
|
177 // Encrypt using legacy API |
|
178 // |
|
179 test.Start(_L("AES - S/W - Legacy API - Parallel encrypt")); |
|
180 |
|
181 test.Printf(_L(" CBC2\n")); |
|
182 TBuf8<BUFLEN+16> sw2; |
|
183 sw2.FillZ(); |
|
184 sw2.SetLength(0); |
|
185 CAESEncryptor *rawaes2 = CAESEncryptor::NewLC(aKey); // rawaes2 |
|
186 CModeCBCEncryptor *cbc2 = CModeCBCEncryptor::NewL(rawaes2, aIV); |
|
187 CleanupStack::Pop(rawaes2); // |
|
188 CleanupStack::PushL(cbc2); // cbc2 |
|
189 |
|
190 CPadding *pad2 = CPaddingPKCS7::NewLC(16); // cbc2, pad2 |
|
191 CBufferedEncryptor *aes2 = CBufferedEncryptor::NewL(cbc2, pad2); |
|
192 CleanupStack::Pop(pad2); // cbc2 |
|
193 CleanupStack::Pop(cbc2); |
|
194 CleanupStack::PushL(aes2); // aes2 |
|
195 |
|
196 test.Printf(_L(" CBC3\n")); |
|
197 TBuf8<BUFLEN+16> sw3; |
|
198 sw3.FillZ(); |
|
199 sw3.SetLength(0); |
|
200 CAESEncryptor *rawaes3 = CAESEncryptor::NewLC(aKey); // rawaes3 |
|
201 CModeCBCEncryptor *cbc3 = CModeCBCEncryptor::NewL(rawaes3, aIV); |
|
202 CleanupStack::Pop(rawaes3); // |
|
203 CleanupStack::PushL(cbc3); // cbc3 |
|
204 |
|
205 CPadding *pad3 = CPaddingPKCS7::NewLC(16); // cbc3, pad3 |
|
206 CBufferedEncryptor *aes3 = CBufferedEncryptor::NewL(cbc3, pad3); |
|
207 CleanupStack::Pop(pad3); // cbc3 |
|
208 CleanupStack::Pop(cbc3); |
|
209 CleanupStack::PushL(aes3); // aes3 |
|
210 |
|
211 test.Printf(_L("About to parallel encrypt\n")); |
|
212 #if 1 |
|
213 // Parallel |
|
214 aes2->Process(chunk1, sw2); |
|
215 aes3->Process(chunk1, sw3); |
|
216 aes2->ProcessFinalL(chunk2, sw2); |
|
217 aes3->ProcessFinalL(chunk2, sw3); |
|
218 #else |
|
219 // Sequential |
|
220 aes2->Process(chunk1, sw2); |
|
221 aes2->ProcessFinalL(chunk2, sw2); |
|
222 aes3->Process(chunk1, sw3); |
|
223 aes3->ProcessFinalL(chunk2, sw3); |
|
224 #endif |
|
225 |
|
226 dumpBuffer(_L("sw2"), sw2); |
|
227 dumpBuffer(_L("sw3"), sw3); |
|
228 |
|
229 |
|
230 CleanupStack::PopAndDestroy(aes3); |
|
231 CleanupStack::PopAndDestroy(aes2); |
|
232 |
|
233 test.Printf(_L("Checking sw2 == sw3\n")); |
|
234 test(sw2 == sw3); |
|
235 |
|
236 test.Printf(_L("Checking sw2 == sw\n")); |
|
237 test(sw2 == sw); |
|
238 |
|
239 aCiphertext = sw; |
|
240 |
|
241 test.End(); |
|
242 } |
|
243 |
|
244 LOCAL_C void decryptL(const TDesC8 &aKey, const TDesC8 &aIV, const TDesC8 &aCiphertext, |
|
245 TDes8 &aPlaintext) |
|
246 { |
|
247 RDebug::Printf("Starting tasync decryption tests\n"); |
|
248 |
|
249 TPtrC8 chunk1(aCiphertext.Left(16)); |
|
250 TPtrC8 chunk2(aCiphertext.Mid(16, aCiphertext.Length()-16)); |
|
251 |
|
252 // |
|
253 // Decrypt using legacy API - Reference |
|
254 // |
|
255 TBuf8<BUFLEN+16> sw; |
|
256 sw.FillZ(); |
|
257 sw.SetLength(0); |
|
258 |
|
259 test.Start(_L("AES - S/W - Reference decrypt")); |
|
260 |
|
261 test.Printf(_L(" CBC\n")); |
|
262 CAESDecryptor *rawaes = CAESDecryptor::NewLC(aKey); // rawaes |
|
263 CModeCBCDecryptor *cbc = CModeCBCDecryptor::NewL(rawaes, aIV); |
|
264 CleanupStack::Pop(rawaes); // |
|
265 CleanupStack::PushL(cbc); // cbc |
|
266 |
|
267 CPadding *pad = CPaddingPKCS7::NewLC(16); // cbc, pad |
|
268 CBufferedDecryptor *aes = CBufferedDecryptor::NewL(cbc, pad); |
|
269 CleanupStack::Pop(pad); // cbc |
|
270 CleanupStack::Pop(cbc); |
|
271 CleanupStack::PushL(aes); // aes |
|
272 |
|
273 test.Printf(_L("About to s/w decrypt (old api)\n")); |
|
274 aes->Process(chunk1, sw); |
|
275 aes->ProcessFinalL(chunk2, sw); |
|
276 dumpBuffer(_L("sw"), sw); |
|
277 |
|
278 CleanupStack::PopAndDestroy(aes); |
|
279 |
|
280 // |
|
281 // Decrypt using legacy API |
|
282 // |
|
283 test.Start(_L("AES - S/W - Legacy API - Parallel decrypt")); |
|
284 |
|
285 test.Printf(_L(" CBC2\n")); |
|
286 TBuf8<BUFLEN+16> sw2; |
|
287 sw2.FillZ(); |
|
288 sw2.SetLength(0); |
|
289 CAESDecryptor *rawaes2 = CAESDecryptor::NewLC(aKey); // rawaes2 |
|
290 CModeCBCDecryptor *cbc2 = CModeCBCDecryptor::NewL(rawaes2, aIV); |
|
291 CleanupStack::Pop(rawaes2); // |
|
292 CleanupStack::PushL(cbc2); // cbc2 |
|
293 |
|
294 CPadding *pad2 = CPaddingPKCS7::NewLC(16); // cbc2, pad2 |
|
295 CBufferedDecryptor *aes2 = CBufferedDecryptor::NewL(cbc2, pad2); |
|
296 CleanupStack::Pop(pad2); // cbc2 |
|
297 CleanupStack::Pop(cbc2); |
|
298 CleanupStack::PushL(aes2); // aes2 |
|
299 |
|
300 test.Printf(_L(" CBC3\n")); |
|
301 TBuf8<BUFLEN+16> sw3; |
|
302 sw3.FillZ(); |
|
303 sw3.SetLength(0); |
|
304 CAESDecryptor *rawaes3 = CAESDecryptor::NewLC(aKey); // rawaes3 |
|
305 CModeCBCDecryptor *cbc3 = CModeCBCDecryptor::NewL(rawaes3, aIV); |
|
306 CleanupStack::Pop(rawaes3); // |
|
307 CleanupStack::PushL(cbc3); // cbc3 |
|
308 |
|
309 CPadding *pad3 = CPaddingPKCS7::NewLC(16); // cbc3, pad3 |
|
310 CBufferedDecryptor *aes3 = CBufferedDecryptor::NewL(cbc3, pad3); |
|
311 CleanupStack::Pop(pad3); // cbc3 |
|
312 CleanupStack::Pop(cbc3); |
|
313 CleanupStack::PushL(aes3); // aes3 |
|
314 |
|
315 test.Printf(_L("About to parallel decrypt\n")); |
|
316 |
|
317 // Parallel |
|
318 aes2->Process(chunk1, sw2); |
|
319 aes3->Process(chunk1, sw3); |
|
320 aes2->ProcessFinalL(chunk2, sw2); |
|
321 aes3->ProcessFinalL(chunk2, sw3); |
|
322 |
|
323 dumpBuffer(_L("sw2"), sw2); |
|
324 dumpBuffer(_L("sw3"), sw3); |
|
325 |
|
326 |
|
327 CleanupStack::PopAndDestroy(aes3); |
|
328 CleanupStack::PopAndDestroy(aes2); |
|
329 |
|
330 test.Printf(_L("Checking sw2 == sw3\n")); |
|
331 test(sw2 == sw3); |
|
332 |
|
333 test.Printf(_L("Checking sw2 == sw\n")); |
|
334 test(sw2 == sw); |
|
335 |
|
336 aPlaintext = sw; |
|
337 |
|
338 test.End(); |
|
339 } |
|
340 |
|
341 |
|
342 |
|
343 LOCAL_D void dumpBuffer(const TDesC &aName, const TDesC8 &aBuf) |
|
344 { |
|
345 test.Printf(_L("%S ="), &aName); |
|
346 TInt len = aBuf.Length(); |
|
347 for(TInt i = 0 ; i < len; ++i) |
|
348 { |
|
349 if(i%16 == 0) |
|
350 { |
|
351 test.Printf(_L("\n ")); |
|
352 } |
|
353 test.Printf(_L("%02x "), aBuf[i]); |
|
354 } |
|
355 test.Printf(_L("\n")); |
|
356 } |
|
357 |
|
358 |
|
359 // End of file |