os/security/cryptoplugins/cryptospiplugins/test/h4drv/crypto_h4_plugin/temb.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     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 #include <random.h>
    34 #include "keys.h"
    35 #include <cryptospi/cryptospidef.h>
    36 #include "cryptosymmetriccipherapi.h"
    37 #include "kmsclient.h"
    38 #include <f32file.h>
    39 //#include "kmsservercommon.h"
    40 using namespace CryptoSpi;
    41 
    42 _LIT(KTxtEPOC32EX,"temb: mainL failed");
    43 //_LIT(KTxtPressAnyKey," [press any key]");
    44 
    45 //#define KEYLEN 16
    46 #define KEYLEN 24
    47 //#define KEYLEN 32
    48 
    49 #define PKCS7PAD
    50 
    51 
    52 //#define BUFLEN 256
    53 #define BUFLEN (256*16)
    54 #define LOOPCOUNT 10000
    55 
    56 LOCAL_D RTest test(_L("Embedded Key Tests"));
    57 
    58 
    59 class CTestConsole:public CConsoleBase
    60 
    61 	{
    62 	public:
    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();};
    78 		~CTestConsole(void);
    79 		void SetLogFile(RFile &aFile);
    80 	private:
    81 		CTestConsole(void);
    82 		CConsoleBase* iCon;
    83 		RFile* iFile;
    84 	};
    85 
    86 CTestConsole* CTestConsole::NewL(CConsoleBase* aCon)
    87 	{
    88 	CTestConsole* self;
    89 	self=new (ELeave) CTestConsole;
    90 	self->iCon=aCon;
    91 	self->iFile=NULL;
    92 	return self;
    93 	}
    94 
    95 CTestConsole::CTestConsole(void):CConsoleBase()
    96 
    97 	{
    98 	}
    99 
   100 CTestConsole::~CTestConsole(void)
   101 
   102 	{
   103 	delete iCon;
   104 	if (iFile)
   105 		{
   106 		iFile->Close();
   107 		}
   108 	}
   109 
   110 void CTestConsole::Write(const TDesC16& aString)
   111 
   112 	{
   113 	iCon->Write(aString);
   114 	if (iFile)
   115 		{
   116 		TUint8 space[200];
   117 		TPtr8 ptr(space,200);
   118 		ptr.Copy(aString);
   119 		iFile->Write(ptr);
   120 		}
   121 	}
   122 
   123 void CTestConsole::SetLogFile(RFile &aFile)
   124 
   125 	{
   126 	iFile = &aFile;
   127 	}
   128 
   129 // private
   130 LOCAL_C void mainL();
   131 
   132 GLDEF_C TInt E32Main() // main function called by E32
   133     {
   134 	CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
   135 	
   136 	//
   137 	// Run the tests
   138 	//
   139 	__UHEAP_MARK;
   140 	TRAPD(error,mainL()); // more initialization, then do example
   141 	__ASSERT_ALWAYS(!error,User::Panic(KTxtEPOC32EX,error));
   142 	__UHEAP_MARKEND;
   143 
   144 	delete cleanup; // destroy clean-up stack
   145 	return 0; // and return
   146     }
   147 
   148 
   149 _LIT(KLddFileName,"cryptoldd.ldd");
   150 _LIT(KPddFileName,"crypto.h4.pdd");
   151 
   152 LOCAL_C void mainL() // initialize and call example code under cleanup stack
   153     {
   154 	test.Title();
   155 	CTestConsole *con = CTestConsole::NewL(test.Console());
   156 
   157 	RFs fs;
   158 	User::LeaveIfError(fs.Connect());
   159 	CleanupClosePushL(fs);
   160 
   161 	TDriveUnit sysDrive (fs.GetSystemDrive());
   162 	TBuf<24> logFile (sysDrive.Name());
   163 	logFile.Append(_L("\\temblog.txt"));
   164 
   165 	RFile file;
   166 	User::LeaveIfError(file.Replace(fs, logFile, EFileShareAny|EFileWrite));
   167 	CleanupClosePushL(file);
   168 
   169 	con->SetLogFile(file);
   170 	test.SetConsole(con);
   171 
   172     TInt r;
   173 	RDebug::Printf("Hello from user side\n");
   174 	
   175     test.Start(_L(" @SYMTestCaseID:SEC-CRYPTOSPI-TEMB-0001 Load driver "));
   176 
   177     test.Next(_L("Loading Physical Device"));
   178     r=User::LoadPhysicalDevice(KPddFileName);
   179     test(r==KErrNone || r==KErrAlreadyExists);
   180 
   181     test.Next(_L("Loading Logical Device"));
   182     r=User::LoadLogicalDevice(KLddFileName);
   183     test(r==KErrNone || r==KErrAlreadyExists);
   184 
   185 	//
   186 	// Generate key and IV
   187 	//
   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
   191 	TBuf8<KEYLEN> key;
   192 	key.SetLength(key.MaxLength());
   193 	TRandom::RandomL(key);
   194 	key[0] = 'K';
   195 	key[1] = 'E';
   196 	key[2] = 'Y';
   197 	key[3] = '*';
   198 	for(int z=4; z<KEYLEN; ++z) key[z] = z;
   199 
   200 	test.Printf(_L("\tGenerating random IV\n"));
   201 	// Generate random 16 byte IV
   202 	TBuf8<16> iv;
   203 	iv.SetLength(iv.MaxLength());
   204 	TRandom::RandomL(iv);
   205 	iv[0] = 'I';
   206 	iv[1] = 'V';
   207 	iv[2] = '*';
   208 	iv[3] = '*';
   209 
   210 	TBuf8<BUFLEN> plaintext;
   211 	plaintext.FillZ();
   212 	plaintext.SetLength(BUFLEN);
   213 	plaintext[0] = 'P';
   214 	plaintext[1] = 'L';
   215 	plaintext[2] = 'A';
   216 	plaintext[3] = 'I';
   217 	plaintext[4] = 'N';
   218 	for(int i=0; i<BUFLEN; ++i)
   219 		{
   220 		plaintext[i] = i;
   221 		}
   222 
   223 
   224 	//
   225 	// KMS tests
   226 	//
   227     test.Next(_L("KMS - Store key"));
   228 	TBuf8<BUFLEN+16> kmsData;
   229 	kmsData.FillZ();
   230 	kmsData.SetLength(0);
   231 	do
   232 		{
   233 		RKeyMgmtSession kms;
   234 		User::LeaveIfError(kms.Connect());
   235 		CleanupClosePushL(kms);
   236 		
   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));
   241 		
   242 		test.Next(_L("KMS - Attempt to use key via embedded key handle"));
   243 		TPckgBuf<TKeyHandle> keyHandlePkg;
   244 		keyHandlePkg() = keyHandle;
   245 
   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,
   254 														KAesUid,
   255 														*ckey,
   256 														KCryptoModeEncryptUid,
   257 														KOperationModeCBCUid,
   258 														KPaddingModePKCS7Uid,
   259 														NULL);
   260 		CleanupStack::PopAndDestroy(ckey);
   261 		CleanupStack::PushL(aes);
   262 
   263 		aes->SetOperationModeL(CryptoSpi::KOperationModeCBCUid);
   264 		aes->SetIvL(iv);		
   265 
   266 		aes->ProcessFinalL(plaintext, kmsData);
   267 
   268 		CleanupStack::PopAndDestroy(aes);
   269 		CleanupStack::PopAndDestroy(&kms);
   270 		} while(false);
   271 
   272 
   273 	//
   274 	// Encrypt using legacy API
   275 	//
   276 	TBuf8<BUFLEN+16> sw;
   277 	sw.FillZ();
   278 	sw.SetLength(0);
   279 	do 
   280 		{ 
   281 		test.Next(_L("Encrypt using key directly (non-KMS)"));
   282 		
   283 		// ECB
   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
   289 		
   290 #ifdef PKCS7PAD
   291 		CPadding *pad = CPaddingPKCS7::NewLC(16); // cbc, pad
   292 #else
   293 		CPadding *pad = CPaddingNone::NewLC(16); // cbc, pad
   294 #endif
   295 		CBufferedEncryptor *aes = CBufferedEncryptor::NewL(cbc, pad);
   296 		CleanupStack::Pop(pad); // cbc
   297 		CleanupStack::Pop(cbc);
   298 		CleanupStack::PushL(aes); // aes
   299 		
   300 		test.Printf(_L("About to s/w encrypt (old api)\n"));
   301 		aes->ProcessFinalL(plaintext, sw);
   302 		
   303 		CleanupStack::PopAndDestroy(aes);
   304 		} while(false);
   305 
   306 	test.Printf(_L("Checking KMS encrypt and direct encrypt had the same result\n"));
   307 	test(kmsData == sw);
   308     test.End();
   309 	
   310 	test.Printf(_L("\r\n0 tests failed out of 1\r\n"));
   311 		
   312 	// test.Printf(KTxtPressAnyKey);
   313 	// test.Getch(); // get and ignore character
   314 	test.Close();
   315 
   316 	CleanupStack::PopAndDestroy(&file);
   317 	CleanupStack::PopAndDestroy(&fs);
   318     }
   319 
   320 
   321 // End of file