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>
35 #include <cryptospi/cryptospidef.h>
36 #include "cryptosymmetriccipherapi.h"
37 #include "kmsclient.h"
39 //#include "kmsservercommon.h"
40 using namespace CryptoSpi;
42 _LIT(KTxtEPOC32EX,"temb: mainL failed");
43 //_LIT(KTxtPressAnyKey," [press any key]");
53 #define BUFLEN (256*16)
54 #define LOOPCOUNT 10000
56 LOCAL_D RTest test(_L("Embedded Key Tests"));
59 class CTestConsole:public CConsoleBase
63 static CTestConsole* NewL(CConsoleBase* aCon);
64 TInt Create(const TDesC16& aTitle,TSize aSize) {return iCon->Create(aTitle,aSize);};
65 void Read(TRequestStatus& aStatus) {iCon->Read(aStatus);};
66 void ReadCancel(void) {iCon->ReadCancel();};
67 void Write(const TDesC16& aString);
68 TPoint CursorPos(void) const {return iCon->CursorPos();};
69 void SetCursorPosAbs(const TPoint& aPos) {iCon->SetCursorPosAbs(aPos);};
70 void SetCursorPosRel(const TPoint& aPos) {iCon->SetCursorPosRel(aPos);};
71 void SetCursorHeight(TInt aHeight) {iCon->SetCursorHeight(aHeight);};
72 void SetTitle(const TDesC16& aTitle) {iCon->SetTitle(aTitle);};
73 void ClearScreen(void) {iCon->ClearScreen();};
74 void ClearToEndOfLine(void) {iCon->ClearToEndOfLine();};
75 TSize ScreenSize(void) const {return iCon->ScreenSize();};
76 TKeyCode KeyCode(void) const {return iCon->KeyCode();};
77 TUint KeyModifiers(void) const {return iCon->KeyModifiers();};
79 void SetLogFile(RFile &aFile);
86 CTestConsole* CTestConsole::NewL(CConsoleBase* aCon)
89 self=new (ELeave) CTestConsole;
95 CTestConsole::CTestConsole(void):CConsoleBase()
100 CTestConsole::~CTestConsole(void)
110 void CTestConsole::Write(const TDesC16& aString)
113 iCon->Write(aString);
117 TPtr8 ptr(space,200);
123 void CTestConsole::SetLogFile(RFile &aFile)
130 LOCAL_C void mainL();
132 GLDEF_C TInt E32Main() // main function called by E32
134 CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
140 TRAPD(error,mainL()); // more initialization, then do example
141 __ASSERT_ALWAYS(!error,User::Panic(KTxtEPOC32EX,error));
144 delete cleanup; // destroy clean-up stack
145 return 0; // and return
149 _LIT(KLddFileName,"cryptoldd.ldd");
150 _LIT(KPddFileName,"crypto.h4.pdd");
152 LOCAL_C void mainL() // initialize and call example code under cleanup stack
155 CTestConsole *con = CTestConsole::NewL(test.Console());
158 User::LeaveIfError(fs.Connect());
159 CleanupClosePushL(fs);
161 TDriveUnit sysDrive (fs.GetSystemDrive());
162 TBuf<24> logFile (sysDrive.Name());
163 logFile.Append(_L("\\temblog.txt"));
166 User::LeaveIfError(file.Replace(fs, logFile, EFileShareAny|EFileWrite));
167 CleanupClosePushL(file);
169 con->SetLogFile(file);
170 test.SetConsole(con);
173 RDebug::Printf("Hello from user side\n");
175 test.Start(_L(" @SYMTestCaseID:SEC-CRYPTOSPI-TEMB-0001 Load driver "));
177 test.Next(_L("Loading Physical Device"));
178 r=User::LoadPhysicalDevice(KPddFileName);
179 test(r==KErrNone || r==KErrAlreadyExists);
181 test.Next(_L("Loading Logical Device"));
182 r=User::LoadLogicalDevice(KLddFileName);
183 test(r==KErrNone || r==KErrAlreadyExists);
186 // Generate key and IV
188 test.Start(_L("Random - Generating key & IV for AES tests"));
189 test.Printf(_L("\tGenerating random key\n"));
190 // Generate random 16 byte key
192 key.SetLength(key.MaxLength());
193 TRandom::RandomL(key);
198 for(int z=4; z<KEYLEN; ++z) key[z] = z;
200 test.Printf(_L("\tGenerating random IV\n"));
201 // Generate random 16 byte IV
203 iv.SetLength(iv.MaxLength());
204 TRandom::RandomL(iv);
210 TBuf8<BUFLEN> plaintext;
212 plaintext.SetLength(BUFLEN);
218 for(int i=0; i<BUFLEN; ++i)
227 test.Next(_L("KMS - Store key"));
228 TBuf8<BUFLEN+16> kmsData;
230 kmsData.SetLength(0);
234 User::LeaveIfError(kms.Connect());
235 CleanupClosePushL(kms);
237 TKeyHandle keyHandle;
238 User::LeaveIfError(kms.StoreKey(key, keyHandle));
239 _LIT_SECURITY_POLICY_PASS(KAlwaysPass);
240 User::LeaveIfError(kms.AddUsage(keyHandle, 0 /* operation */, KAlwaysPass));
242 test.Next(_L("KMS - Attempt to use key via embedded key handle"));
243 TPckgBuf<TKeyHandle> keyHandlePkg;
244 keyHandlePkg() = keyHandle;
246 TKeyProperty keyProperty = {KAesUid, KNullUid, KSymmetricKeyUid, KNonExtractableKey};
247 CCryptoParams* keyParam =CCryptoParams::NewLC();
248 keyParam->AddL(keyHandlePkg, KSymmetricKeyParameterUid);
249 CKey *ckey=CKey::NewL(keyProperty, *keyParam);
250 CleanupStack::PopAndDestroy(keyParam);
251 CleanupStack::PushL(ckey);
252 CryptoSpi::CSymmetricCipher *aes = 0;
253 CSymmetricCipherFactory::CreateSymmetricCipherL(aes,
256 KCryptoModeEncryptUid,
257 KOperationModeCBCUid,
258 KPaddingModePKCS7Uid,
260 CleanupStack::PopAndDestroy(ckey);
261 CleanupStack::PushL(aes);
263 aes->SetOperationModeL(CryptoSpi::KOperationModeCBCUid);
266 aes->ProcessFinalL(plaintext, kmsData);
268 CleanupStack::PopAndDestroy(aes);
269 CleanupStack::PopAndDestroy(&kms);
274 // Encrypt using legacy API
281 test.Next(_L("Encrypt using key directly (non-KMS)"));
284 test.Printf(_L(" CBC\n"));
285 CAESEncryptor *rawaes = CAESEncryptor::NewLC(key); // rawaes
286 CModeCBCEncryptor *cbc = CModeCBCEncryptor::NewL(rawaes, iv);
287 CleanupStack::Pop(rawaes); //
288 CleanupStack::PushL(cbc); // cbc
291 CPadding *pad = CPaddingPKCS7::NewLC(16); // cbc, pad
293 CPadding *pad = CPaddingNone::NewLC(16); // cbc, pad
295 CBufferedEncryptor *aes = CBufferedEncryptor::NewL(cbc, pad);
296 CleanupStack::Pop(pad); // cbc
297 CleanupStack::Pop(cbc);
298 CleanupStack::PushL(aes); // aes
300 test.Printf(_L("About to s/w encrypt (old api)\n"));
301 aes->ProcessFinalL(plaintext, sw);
303 CleanupStack::PopAndDestroy(aes);
306 test.Printf(_L("Checking KMS encrypt and direct encrypt had the same result\n"));
310 test.Printf(_L("\r\n0 tests failed out of 1\r\n"));
312 // test.Printf(KTxtPressAnyKey);
313 // test.Getch(); // get and ignore character
316 CleanupStack::PopAndDestroy(&file);
317 CleanupStack::PopAndDestroy(&fs);