sl@0: /* sl@0: * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: /** sl@0: @file sl@0: @internalComponent sl@0: @released sl@0: */ sl@0: #include "cryptodriver.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: _LIT(KTxtEPOC32EX,"tasync: mainL failed"); sl@0: _LIT(KTxtPressAnyKey," [press any key]"); sl@0: sl@0: //#define KEYLEN 16 sl@0: #define KEYLEN 24 sl@0: //#define KEYLEN 32 sl@0: sl@0: //#define BUFLEN 256 sl@0: //#define BUFLEN (256*16) sl@0: #define BUFLEN 32 sl@0: #define LOOPCOUNT 10000 sl@0: sl@0: sl@0: // public sl@0: CConsoleBase* console; // write all your messages to this sl@0: sl@0: // private sl@0: LOCAL_C void mainL(); sl@0: sl@0: GLDEF_C TInt E32Main() // main function called by E32 sl@0: { sl@0: __UHEAP_MARK; sl@0: CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack sl@0: TRAPD(error,mainL()); // more initialization, then do example sl@0: __ASSERT_ALWAYS(!error,User::Panic(KTxtEPOC32EX,error)); sl@0: delete cleanup; // destroy clean-up stack sl@0: __UHEAP_MARKEND; sl@0: return 0; // and return sl@0: } sl@0: sl@0: LOCAL_D RTest test(_L("tasync")); sl@0: sl@0: LOCAL_D void dumpBuffer(const TDesC &aName, const TDesC8 &aBuf); sl@0: sl@0: LOCAL_C void encryptL(const TDesC8 &aKey, const TDesC8 &aIV, const TDesC8 &aPlainText, sl@0: TDes8 &aCipherText); sl@0: LOCAL_C void decryptL(const TDesC8 &aKey, const TDesC8 &aIV, const TDesC8 &aCiphertext, sl@0: TDes8 &aPlaintext); sl@0: sl@0: LOCAL_C void mainL() // initialize and call example code under cleanup stack sl@0: { sl@0: test.Title(); sl@0: // sl@0: // Generate key and IV sl@0: // sl@0: test.Start(_L(" @SYMTestCaseID:SEC-CRYPTOSPI-ASYNC-0001 Generating key & IV for AES tests ")); sl@0: test.Next(_L("Generating key\n")); sl@0: // Generate 16 byte key sl@0: TBuf8 key; sl@0: key.SetLength(KEYLEN); sl@0: key[0] = 'K'; sl@0: key[1] = 'E'; sl@0: key[2] = 'Y'; sl@0: key[3] = '*'; sl@0: for(int z=4; z iv; sl@0: iv.SetLength(iv.MaxLength()); sl@0: iv[0] = 'I'; sl@0: iv[1] = 'V'; sl@0: iv[2] = '*'; sl@0: iv[3] = '*'; sl@0: sl@0: TBuf8 plaintext; sl@0: plaintext.FillZ(); sl@0: plaintext.SetLength(plaintext.MaxLength()); sl@0: plaintext[0] = 'P'; sl@0: plaintext[1] = 'L'; sl@0: plaintext[2] = 'A'; sl@0: plaintext[3] = 'I'; sl@0: plaintext[4] = 'N'; sl@0: plaintext[5] = '1'; sl@0: for(int i=6; i ciphertext; sl@0: encryptL(key, iv, plaintext, ciphertext); sl@0: sl@0: TBuf8 plaintext2; sl@0: decryptL(key, iv, ciphertext, plaintext2); sl@0: sl@0: test.Printf(_L("Checking plaintext2 == plaintext\n")); sl@0: test(plaintext2 == plaintext); sl@0: sl@0: test.Printf(KTxtPressAnyKey); sl@0: test.Getch(); // get and ignore character sl@0: test.Close(); sl@0: sl@0: } sl@0: sl@0: LOCAL_C void encryptL(const TDesC8 &aKey, const TDesC8 &aIV, const TDesC8 &aPlaintext, sl@0: TDes8 &aCiphertext) sl@0: { sl@0: RDebug::Printf("Starting tasync encryption tests\n"); sl@0: sl@0: TPtrC8 chunk1(aPlaintext.Left(16)); sl@0: TPtrC8 chunk2(aPlaintext.Mid(16, aPlaintext.Length()-16)); sl@0: sl@0: sl@0: // sl@0: // Encrypt using legacy API - Reference sl@0: // sl@0: TBuf8 sw; sl@0: sw.FillZ(); sl@0: sw.SetLength(0); sl@0: sl@0: test.Start(_L("AES - S/W - Reference encrypt")); sl@0: sl@0: test.Printf(_L(" CBC\n")); sl@0: CAESEncryptor *rawaes = CAESEncryptor::NewLC(aKey); // rawaes sl@0: CModeCBCEncryptor *cbc = CModeCBCEncryptor::NewL(rawaes, aIV); sl@0: CleanupStack::Pop(rawaes); // sl@0: CleanupStack::PushL(cbc); // cbc sl@0: sl@0: CPadding *pad = CPaddingPKCS7::NewLC(16); // cbc, pad sl@0: CBufferedEncryptor *aes = CBufferedEncryptor::NewL(cbc, pad); sl@0: CleanupStack::Pop(pad); // cbc sl@0: CleanupStack::Pop(cbc); sl@0: CleanupStack::PushL(aes); // aes sl@0: sl@0: test.Printf(_L("About to s/w encrypt (old api)\n")); sl@0: aes->Process(chunk1, sw); sl@0: aes->ProcessFinalL(chunk2, sw); sl@0: dumpBuffer(_L("sw"), sw); sl@0: sl@0: CleanupStack::PopAndDestroy(aes); sl@0: sl@0: // sl@0: // Encrypt using legacy API sl@0: // sl@0: test.Start(_L("AES - S/W - Legacy API - Parallel encrypt")); sl@0: sl@0: test.Printf(_L(" CBC2\n")); sl@0: TBuf8 sw2; sl@0: sw2.FillZ(); sl@0: sw2.SetLength(0); sl@0: CAESEncryptor *rawaes2 = CAESEncryptor::NewLC(aKey); // rawaes2 sl@0: CModeCBCEncryptor *cbc2 = CModeCBCEncryptor::NewL(rawaes2, aIV); sl@0: CleanupStack::Pop(rawaes2); // sl@0: CleanupStack::PushL(cbc2); // cbc2 sl@0: sl@0: CPadding *pad2 = CPaddingPKCS7::NewLC(16); // cbc2, pad2 sl@0: CBufferedEncryptor *aes2 = CBufferedEncryptor::NewL(cbc2, pad2); sl@0: CleanupStack::Pop(pad2); // cbc2 sl@0: CleanupStack::Pop(cbc2); sl@0: CleanupStack::PushL(aes2); // aes2 sl@0: sl@0: test.Printf(_L(" CBC3\n")); sl@0: TBuf8 sw3; sl@0: sw3.FillZ(); sl@0: sw3.SetLength(0); sl@0: CAESEncryptor *rawaes3 = CAESEncryptor::NewLC(aKey); // rawaes3 sl@0: CModeCBCEncryptor *cbc3 = CModeCBCEncryptor::NewL(rawaes3, aIV); sl@0: CleanupStack::Pop(rawaes3); // sl@0: CleanupStack::PushL(cbc3); // cbc3 sl@0: sl@0: CPadding *pad3 = CPaddingPKCS7::NewLC(16); // cbc3, pad3 sl@0: CBufferedEncryptor *aes3 = CBufferedEncryptor::NewL(cbc3, pad3); sl@0: CleanupStack::Pop(pad3); // cbc3 sl@0: CleanupStack::Pop(cbc3); sl@0: CleanupStack::PushL(aes3); // aes3 sl@0: sl@0: test.Printf(_L("About to parallel encrypt\n")); sl@0: #if 1 sl@0: // Parallel sl@0: aes2->Process(chunk1, sw2); sl@0: aes3->Process(chunk1, sw3); sl@0: aes2->ProcessFinalL(chunk2, sw2); sl@0: aes3->ProcessFinalL(chunk2, sw3); sl@0: #else sl@0: // Sequential sl@0: aes2->Process(chunk1, sw2); sl@0: aes2->ProcessFinalL(chunk2, sw2); sl@0: aes3->Process(chunk1, sw3); sl@0: aes3->ProcessFinalL(chunk2, sw3); sl@0: #endif sl@0: sl@0: dumpBuffer(_L("sw2"), sw2); sl@0: dumpBuffer(_L("sw3"), sw3); sl@0: sl@0: sl@0: CleanupStack::PopAndDestroy(aes3); sl@0: CleanupStack::PopAndDestroy(aes2); sl@0: sl@0: test.Printf(_L("Checking sw2 == sw3\n")); sl@0: test(sw2 == sw3); sl@0: sl@0: test.Printf(_L("Checking sw2 == sw\n")); sl@0: test(sw2 == sw); sl@0: sl@0: aCiphertext = sw; sl@0: sl@0: test.End(); sl@0: } sl@0: sl@0: LOCAL_C void decryptL(const TDesC8 &aKey, const TDesC8 &aIV, const TDesC8 &aCiphertext, sl@0: TDes8 &aPlaintext) sl@0: { sl@0: RDebug::Printf("Starting tasync decryption tests\n"); sl@0: sl@0: TPtrC8 chunk1(aCiphertext.Left(16)); sl@0: TPtrC8 chunk2(aCiphertext.Mid(16, aCiphertext.Length()-16)); sl@0: sl@0: // sl@0: // Decrypt using legacy API - Reference sl@0: // sl@0: TBuf8 sw; sl@0: sw.FillZ(); sl@0: sw.SetLength(0); sl@0: sl@0: test.Start(_L("AES - S/W - Reference decrypt")); sl@0: sl@0: test.Printf(_L(" CBC\n")); sl@0: CAESDecryptor *rawaes = CAESDecryptor::NewLC(aKey); // rawaes sl@0: CModeCBCDecryptor *cbc = CModeCBCDecryptor::NewL(rawaes, aIV); sl@0: CleanupStack::Pop(rawaes); // sl@0: CleanupStack::PushL(cbc); // cbc sl@0: sl@0: CPadding *pad = CPaddingPKCS7::NewLC(16); // cbc, pad sl@0: CBufferedDecryptor *aes = CBufferedDecryptor::NewL(cbc, pad); sl@0: CleanupStack::Pop(pad); // cbc sl@0: CleanupStack::Pop(cbc); sl@0: CleanupStack::PushL(aes); // aes sl@0: sl@0: test.Printf(_L("About to s/w decrypt (old api)\n")); sl@0: aes->Process(chunk1, sw); sl@0: aes->ProcessFinalL(chunk2, sw); sl@0: dumpBuffer(_L("sw"), sw); sl@0: sl@0: CleanupStack::PopAndDestroy(aes); sl@0: sl@0: // sl@0: // Decrypt using legacy API sl@0: // sl@0: test.Start(_L("AES - S/W - Legacy API - Parallel decrypt")); sl@0: sl@0: test.Printf(_L(" CBC2\n")); sl@0: TBuf8 sw2; sl@0: sw2.FillZ(); sl@0: sw2.SetLength(0); sl@0: CAESDecryptor *rawaes2 = CAESDecryptor::NewLC(aKey); // rawaes2 sl@0: CModeCBCDecryptor *cbc2 = CModeCBCDecryptor::NewL(rawaes2, aIV); sl@0: CleanupStack::Pop(rawaes2); // sl@0: CleanupStack::PushL(cbc2); // cbc2 sl@0: sl@0: CPadding *pad2 = CPaddingPKCS7::NewLC(16); // cbc2, pad2 sl@0: CBufferedDecryptor *aes2 = CBufferedDecryptor::NewL(cbc2, pad2); sl@0: CleanupStack::Pop(pad2); // cbc2 sl@0: CleanupStack::Pop(cbc2); sl@0: CleanupStack::PushL(aes2); // aes2 sl@0: sl@0: test.Printf(_L(" CBC3\n")); sl@0: TBuf8 sw3; sl@0: sw3.FillZ(); sl@0: sw3.SetLength(0); sl@0: CAESDecryptor *rawaes3 = CAESDecryptor::NewLC(aKey); // rawaes3 sl@0: CModeCBCDecryptor *cbc3 = CModeCBCDecryptor::NewL(rawaes3, aIV); sl@0: CleanupStack::Pop(rawaes3); // sl@0: CleanupStack::PushL(cbc3); // cbc3 sl@0: sl@0: CPadding *pad3 = CPaddingPKCS7::NewLC(16); // cbc3, pad3 sl@0: CBufferedDecryptor *aes3 = CBufferedDecryptor::NewL(cbc3, pad3); sl@0: CleanupStack::Pop(pad3); // cbc3 sl@0: CleanupStack::Pop(cbc3); sl@0: CleanupStack::PushL(aes3); // aes3 sl@0: sl@0: test.Printf(_L("About to parallel decrypt\n")); sl@0: sl@0: // Parallel sl@0: aes2->Process(chunk1, sw2); sl@0: aes3->Process(chunk1, sw3); sl@0: aes2->ProcessFinalL(chunk2, sw2); sl@0: aes3->ProcessFinalL(chunk2, sw3); sl@0: sl@0: dumpBuffer(_L("sw2"), sw2); sl@0: dumpBuffer(_L("sw3"), sw3); sl@0: sl@0: sl@0: CleanupStack::PopAndDestroy(aes3); sl@0: CleanupStack::PopAndDestroy(aes2); sl@0: sl@0: test.Printf(_L("Checking sw2 == sw3\n")); sl@0: test(sw2 == sw3); sl@0: sl@0: test.Printf(_L("Checking sw2 == sw\n")); sl@0: test(sw2 == sw); sl@0: sl@0: aPlaintext = sw; sl@0: sl@0: test.End(); sl@0: } sl@0: sl@0: sl@0: sl@0: LOCAL_D void dumpBuffer(const TDesC &aName, const TDesC8 &aBuf) sl@0: { sl@0: test.Printf(_L("%S ="), &aName); sl@0: TInt len = aBuf.Length(); sl@0: for(TInt i = 0 ; i < len; ++i) sl@0: { sl@0: if(i%16 == 0) sl@0: { sl@0: test.Printf(_L("\n ")); sl@0: } sl@0: test.Printf(_L("%02x "), aBuf[i]); sl@0: } sl@0: test.Printf(_L("\n")); sl@0: } sl@0: sl@0: sl@0: // End of file