os/security/cryptoplugins/cryptospiplugins/test/h4drv/crypto_h4_plugin/tasync.cpp
First public contribution.
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
24 #include "cryptodriver.h"
32 #include <bufferedtransformation.h>
34 _LIT(KTxtEPOC32EX,"tasync: mainL failed");
35 _LIT(KTxtPressAnyKey," [press any key]");
42 //#define BUFLEN (256*16)
44 #define LOOPCOUNT 10000
48 CConsoleBase* console; // write all your messages to this
53 GLDEF_C TInt E32Main() // main function called by E32
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
61 return 0; // and return
64 LOCAL_D RTest test(_L("tasync"));
66 LOCAL_D void dumpBuffer(const TDesC &aName, const TDesC8 &aBuf);
68 LOCAL_C void encryptL(const TDesC8 &aKey, const TDesC8 &aIV, const TDesC8 &aPlainText,
70 LOCAL_C void decryptL(const TDesC8 &aKey, const TDesC8 &aIV, const TDesC8 &aCiphertext,
73 LOCAL_C void mainL() // initialize and call example code under cleanup stack
77 // Generate key and IV
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
83 key.SetLength(KEYLEN);
88 for(int z=4; z<KEYLEN; ++z) key[z] = z;
91 test.Next(_L("Generating IV\n"));
93 iv.SetLength(iv.MaxLength());
99 TBuf8<BUFLEN> plaintext;
101 plaintext.SetLength(plaintext.MaxLength());
108 for(int i=6; i<BUFLEN/2; ++i)
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)
124 TBuf8<BUFLEN+16> ciphertext;
125 encryptL(key, iv, plaintext, ciphertext);
127 TBuf8<BUFLEN> plaintext2;
128 decryptL(key, iv, ciphertext, plaintext2);
130 test.Printf(_L("Checking plaintext2 == plaintext\n"));
131 test(plaintext2 == plaintext);
133 test.Printf(KTxtPressAnyKey);
134 test.Getch(); // get and ignore character
139 LOCAL_C void encryptL(const TDesC8 &aKey, const TDesC8 &aIV, const TDesC8 &aPlaintext,
142 RDebug::Printf("Starting tasync encryption tests\n");
144 TPtrC8 chunk1(aPlaintext.Left(16));
145 TPtrC8 chunk2(aPlaintext.Mid(16, aPlaintext.Length()-16));
149 // Encrypt using legacy API - Reference
155 test.Start(_L("AES - S/W - Reference encrypt"));
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
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
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);
174 CleanupStack::PopAndDestroy(aes);
177 // Encrypt using legacy API
179 test.Start(_L("AES - S/W - Legacy API - Parallel encrypt"));
181 test.Printf(_L(" CBC2\n"));
182 TBuf8<BUFLEN+16> sw2;
185 CAESEncryptor *rawaes2 = CAESEncryptor::NewLC(aKey); // rawaes2
186 CModeCBCEncryptor *cbc2 = CModeCBCEncryptor::NewL(rawaes2, aIV);
187 CleanupStack::Pop(rawaes2); //
188 CleanupStack::PushL(cbc2); // cbc2
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
196 test.Printf(_L(" CBC3\n"));
197 TBuf8<BUFLEN+16> sw3;
200 CAESEncryptor *rawaes3 = CAESEncryptor::NewLC(aKey); // rawaes3
201 CModeCBCEncryptor *cbc3 = CModeCBCEncryptor::NewL(rawaes3, aIV);
202 CleanupStack::Pop(rawaes3); //
203 CleanupStack::PushL(cbc3); // cbc3
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
211 test.Printf(_L("About to parallel encrypt\n"));
214 aes2->Process(chunk1, sw2);
215 aes3->Process(chunk1, sw3);
216 aes2->ProcessFinalL(chunk2, sw2);
217 aes3->ProcessFinalL(chunk2, sw3);
220 aes2->Process(chunk1, sw2);
221 aes2->ProcessFinalL(chunk2, sw2);
222 aes3->Process(chunk1, sw3);
223 aes3->ProcessFinalL(chunk2, sw3);
226 dumpBuffer(_L("sw2"), sw2);
227 dumpBuffer(_L("sw3"), sw3);
230 CleanupStack::PopAndDestroy(aes3);
231 CleanupStack::PopAndDestroy(aes2);
233 test.Printf(_L("Checking sw2 == sw3\n"));
236 test.Printf(_L("Checking sw2 == sw\n"));
244 LOCAL_C void decryptL(const TDesC8 &aKey, const TDesC8 &aIV, const TDesC8 &aCiphertext,
247 RDebug::Printf("Starting tasync decryption tests\n");
249 TPtrC8 chunk1(aCiphertext.Left(16));
250 TPtrC8 chunk2(aCiphertext.Mid(16, aCiphertext.Length()-16));
253 // Decrypt using legacy API - Reference
259 test.Start(_L("AES - S/W - Reference decrypt"));
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
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
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);
278 CleanupStack::PopAndDestroy(aes);
281 // Decrypt using legacy API
283 test.Start(_L("AES - S/W - Legacy API - Parallel decrypt"));
285 test.Printf(_L(" CBC2\n"));
286 TBuf8<BUFLEN+16> sw2;
289 CAESDecryptor *rawaes2 = CAESDecryptor::NewLC(aKey); // rawaes2
290 CModeCBCDecryptor *cbc2 = CModeCBCDecryptor::NewL(rawaes2, aIV);
291 CleanupStack::Pop(rawaes2); //
292 CleanupStack::PushL(cbc2); // cbc2
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
300 test.Printf(_L(" CBC3\n"));
301 TBuf8<BUFLEN+16> sw3;
304 CAESDecryptor *rawaes3 = CAESDecryptor::NewLC(aKey); // rawaes3
305 CModeCBCDecryptor *cbc3 = CModeCBCDecryptor::NewL(rawaes3, aIV);
306 CleanupStack::Pop(rawaes3); //
307 CleanupStack::PushL(cbc3); // cbc3
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
315 test.Printf(_L("About to parallel decrypt\n"));
318 aes2->Process(chunk1, sw2);
319 aes3->Process(chunk1, sw3);
320 aes2->ProcessFinalL(chunk2, sw2);
321 aes3->ProcessFinalL(chunk2, sw3);
323 dumpBuffer(_L("sw2"), sw2);
324 dumpBuffer(_L("sw3"), sw3);
327 CleanupStack::PopAndDestroy(aes3);
328 CleanupStack::PopAndDestroy(aes2);
330 test.Printf(_L("Checking sw2 == sw3\n"));
333 test.Printf(_L("Checking sw2 == sw\n"));
343 LOCAL_D void dumpBuffer(const TDesC &aName, const TDesC8 &aBuf)
345 test.Printf(_L("%S ="), &aName);
346 TInt len = aBuf.Length();
347 for(TInt i = 0 ; i < len; ++i)
351 test.Printf(_L("\n "));
353 test.Printf(_L("%02x "), aBuf[i]);
355 test.Printf(_L("\n"));