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: #include sl@0: #include "keys.h" sl@0: #include sl@0: #include "cryptosymmetriccipherapi.h" sl@0: #include "kmsclient.h" sl@0: #include sl@0: //#include "kmsservercommon.h" sl@0: using namespace CryptoSpi; sl@0: sl@0: _LIT(KTxtEPOC32EX,"temb: 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 PKCS7PAD sl@0: sl@0: sl@0: //#define BUFLEN 256 sl@0: #define BUFLEN (256*16) sl@0: #define LOOPCOUNT 10000 sl@0: sl@0: LOCAL_D RTest test(_L("Embedded Key Tests")); sl@0: sl@0: sl@0: class CTestConsole:public CConsoleBase sl@0: sl@0: { sl@0: public: sl@0: static CTestConsole* NewL(CConsoleBase* aCon); sl@0: TInt Create(const TDesC16& aTitle,TSize aSize) {return iCon->Create(aTitle,aSize);}; sl@0: void Read(TRequestStatus& aStatus) {iCon->Read(aStatus);}; sl@0: void ReadCancel(void) {iCon->ReadCancel();}; sl@0: void Write(const TDesC16& aString); sl@0: TPoint CursorPos(void) const {return iCon->CursorPos();}; sl@0: void SetCursorPosAbs(const TPoint& aPos) {iCon->SetCursorPosAbs(aPos);}; sl@0: void SetCursorPosRel(const TPoint& aPos) {iCon->SetCursorPosRel(aPos);}; sl@0: void SetCursorHeight(TInt aHeight) {iCon->SetCursorHeight(aHeight);}; sl@0: void SetTitle(const TDesC16& aTitle) {iCon->SetTitle(aTitle);}; sl@0: void ClearScreen(void) {iCon->ClearScreen();}; sl@0: void ClearToEndOfLine(void) {iCon->ClearToEndOfLine();}; sl@0: TSize ScreenSize(void) const {return iCon->ScreenSize();}; sl@0: TKeyCode KeyCode(void) const {return iCon->KeyCode();}; sl@0: TUint KeyModifiers(void) const {return iCon->KeyModifiers();}; sl@0: ~CTestConsole(void); sl@0: void SetLogFile(RFile &aFile); sl@0: private: sl@0: CTestConsole(void); sl@0: CConsoleBase* iCon; sl@0: RFile* iFile; sl@0: }; sl@0: sl@0: CTestConsole* CTestConsole::NewL(CConsoleBase* aCon) sl@0: { sl@0: CTestConsole* self; sl@0: self=new (ELeave) CTestConsole; sl@0: self->iCon=aCon; sl@0: self->iFile=NULL; sl@0: return self; sl@0: } sl@0: sl@0: CTestConsole::CTestConsole(void):CConsoleBase() sl@0: sl@0: { sl@0: } sl@0: sl@0: CTestConsole::~CTestConsole(void) sl@0: sl@0: { sl@0: delete iCon; sl@0: if (iFile) sl@0: { sl@0: iFile->Close(); sl@0: } sl@0: } sl@0: sl@0: void CTestConsole::Write(const TDesC16& aString) sl@0: sl@0: { sl@0: iCon->Write(aString); sl@0: if (iFile) sl@0: { sl@0: TUint8 space[200]; sl@0: TPtr8 ptr(space,200); sl@0: ptr.Copy(aString); sl@0: iFile->Write(ptr); sl@0: } sl@0: } sl@0: sl@0: void CTestConsole::SetLogFile(RFile &aFile) sl@0: sl@0: { sl@0: iFile = &aFile; sl@0: } 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: CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack sl@0: sl@0: // sl@0: // Run the tests sl@0: // sl@0: __UHEAP_MARK; sl@0: TRAPD(error,mainL()); // more initialization, then do example sl@0: __ASSERT_ALWAYS(!error,User::Panic(KTxtEPOC32EX,error)); sl@0: __UHEAP_MARKEND; sl@0: sl@0: delete cleanup; // destroy clean-up stack sl@0: return 0; // and return sl@0: } sl@0: sl@0: sl@0: _LIT(KLddFileName,"cryptoldd.ldd"); sl@0: _LIT(KPddFileName,"crypto.h4.pdd"); sl@0: sl@0: LOCAL_C void mainL() // initialize and call example code under cleanup stack sl@0: { sl@0: test.Title(); sl@0: CTestConsole *con = CTestConsole::NewL(test.Console()); sl@0: sl@0: RFs fs; sl@0: User::LeaveIfError(fs.Connect()); sl@0: CleanupClosePushL(fs); sl@0: sl@0: TDriveUnit sysDrive (fs.GetSystemDrive()); sl@0: TBuf<24> logFile (sysDrive.Name()); sl@0: logFile.Append(_L("\\temblog.txt")); sl@0: sl@0: RFile file; sl@0: User::LeaveIfError(file.Replace(fs, logFile, EFileShareAny|EFileWrite)); sl@0: CleanupClosePushL(file); sl@0: sl@0: con->SetLogFile(file); sl@0: test.SetConsole(con); sl@0: sl@0: TInt r; sl@0: RDebug::Printf("Hello from user side\n"); sl@0: sl@0: test.Start(_L(" @SYMTestCaseID:SEC-CRYPTOSPI-TEMB-0001 Load driver ")); sl@0: sl@0: test.Next(_L("Loading Physical Device")); sl@0: r=User::LoadPhysicalDevice(KPddFileName); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: sl@0: test.Next(_L("Loading Logical Device")); sl@0: r=User::LoadLogicalDevice(KLddFileName); sl@0: test(r==KErrNone || r==KErrAlreadyExists); sl@0: sl@0: // sl@0: // Generate key and IV sl@0: // sl@0: test.Start(_L("Random - Generating key & IV for AES tests")); sl@0: test.Printf(_L("\tGenerating random key\n")); sl@0: // Generate random 16 byte key sl@0: TBuf8 key; sl@0: key.SetLength(key.MaxLength()); sl@0: TRandom::RandomL(key); 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: TRandom::RandomL(iv); 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(BUFLEN); 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: for(int i=0; i kmsData; sl@0: kmsData.FillZ(); sl@0: kmsData.SetLength(0); sl@0: do sl@0: { sl@0: RKeyMgmtSession kms; sl@0: User::LeaveIfError(kms.Connect()); sl@0: CleanupClosePushL(kms); sl@0: sl@0: TKeyHandle keyHandle; sl@0: User::LeaveIfError(kms.StoreKey(key, keyHandle)); sl@0: _LIT_SECURITY_POLICY_PASS(KAlwaysPass); sl@0: User::LeaveIfError(kms.AddUsage(keyHandle, 0 /* operation */, KAlwaysPass)); sl@0: sl@0: test.Next(_L("KMS - Attempt to use key via embedded key handle")); sl@0: TPckgBuf keyHandlePkg; sl@0: keyHandlePkg() = keyHandle; sl@0: sl@0: TKeyProperty keyProperty = {KAesUid, KNullUid, KSymmetricKeyUid, KNonExtractableKey}; sl@0: CCryptoParams* keyParam =CCryptoParams::NewLC(); sl@0: keyParam->AddL(keyHandlePkg, KSymmetricKeyParameterUid); sl@0: CKey *ckey=CKey::NewL(keyProperty, *keyParam); sl@0: CleanupStack::PopAndDestroy(keyParam); sl@0: CleanupStack::PushL(ckey); sl@0: CryptoSpi::CSymmetricCipher *aes = 0; sl@0: CSymmetricCipherFactory::CreateSymmetricCipherL(aes, sl@0: KAesUid, sl@0: *ckey, sl@0: KCryptoModeEncryptUid, sl@0: KOperationModeCBCUid, sl@0: KPaddingModePKCS7Uid, sl@0: NULL); sl@0: CleanupStack::PopAndDestroy(ckey); sl@0: CleanupStack::PushL(aes); sl@0: sl@0: aes->SetOperationModeL(CryptoSpi::KOperationModeCBCUid); sl@0: aes->SetIvL(iv); sl@0: sl@0: aes->ProcessFinalL(plaintext, kmsData); sl@0: sl@0: CleanupStack::PopAndDestroy(aes); sl@0: CleanupStack::PopAndDestroy(&kms); sl@0: } while(false); sl@0: sl@0: sl@0: // sl@0: // Encrypt using legacy API sl@0: // sl@0: TBuf8 sw; sl@0: sw.FillZ(); sl@0: sw.SetLength(0); sl@0: do sl@0: { sl@0: test.Next(_L("Encrypt using key directly (non-KMS)")); sl@0: sl@0: // ECB sl@0: test.Printf(_L(" CBC\n")); sl@0: CAESEncryptor *rawaes = CAESEncryptor::NewLC(key); // rawaes sl@0: CModeCBCEncryptor *cbc = CModeCBCEncryptor::NewL(rawaes, iv); sl@0: CleanupStack::Pop(rawaes); // sl@0: CleanupStack::PushL(cbc); // cbc sl@0: sl@0: #ifdef PKCS7PAD sl@0: CPadding *pad = CPaddingPKCS7::NewLC(16); // cbc, pad sl@0: #else sl@0: CPadding *pad = CPaddingNone::NewLC(16); // cbc, pad sl@0: #endif 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->ProcessFinalL(plaintext, sw); sl@0: sl@0: CleanupStack::PopAndDestroy(aes); sl@0: } while(false); sl@0: sl@0: test.Printf(_L("Checking KMS encrypt and direct encrypt had the same result\n")); sl@0: test(kmsData == sw); sl@0: test.End(); sl@0: sl@0: test.Printf(_L("\r\n0 tests failed out of 1\r\n")); sl@0: sl@0: // test.Printf(KTxtPressAnyKey); sl@0: // test.Getch(); // get and ignore character sl@0: test.Close(); sl@0: sl@0: CleanupStack::PopAndDestroy(&file); sl@0: CleanupStack::PopAndDestroy(&fs); sl@0: } sl@0: sl@0: sl@0: // End of file