os/security/cryptoplugins/cryptospiplugins/test/h4drv/crypto_h4/tcrypto.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 <e32def_private.h>
    35 
    36 _LIT(KTxtEPOC32EX,"tcrypto: mainL failed");
    37 _LIT(KTxtPressAnyKey," [press any key]");
    38 
    39 #define RNG_TESTS
    40 //#define RNG_USE_SVR
    41 //#define KEYLEN 16
    42 #define KEYLEN 24
    43 //#define KEYLEN 32
    44 
    45 #define USE_CBCMODE
    46 //#define DISABLE_AES_CHECKS
    47 
    48 //#define PADDING_PKCS7
    49 
    50 //#define BUFLEN 256
    51 #define BUFLEN (256*16)
    52 #define LOOPCOUNT 10000
    53 
    54 
    55 // public
    56 CConsoleBase* console; // write all your messages to this
    57 
    58 // private
    59 LOCAL_C void mainL();
    60 
    61 GLDEF_C TInt E32Main() // main function called by E32
    62     {
    63 	__UHEAP_MARK;
    64 	CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
    65 	TRAPD(error,mainL()); // more initialization, then do example
    66 	__ASSERT_ALWAYS(!error,User::Panic(KTxtEPOC32EX,error));
    67 	delete cleanup; // destroy clean-up stack
    68 	__UHEAP_MARKEND;
    69 	return 0; // and return
    70     }
    71 
    72 LOCAL_D RTest test(_L("tcrypto"));
    73 
    74 _LIT(KLddFileName,"cryptoldd.ldd");
    75 _LIT(KPddFileName,"crypto.h4.pdd");
    76 
    77 LOCAL_C void mainL() // initialize and call example code under cleanup stack
    78     {
    79 #if 0
    80 	test.Printf(KTxtPressAnyKey);
    81 	test.Getch(); // get and ignore character
    82 	test.Printf(_L("\n"));
    83 #endif
    84 
    85 	test.Title();
    86 
    87 	RDebug::Printf("Hello from user side\n");
    88 	
    89 
    90     TInt r;
    91 
    92     test.Start(_L(" @SYMTestCaseID:SEC-CRYPTOSPI-CRYPTO-0001 Loading Physical Device "));
    93     r=User::LoadPhysicalDevice(KPddFileName);
    94     test(r==KErrNone || r==KErrAlreadyExists);
    95 
    96 	test.Next(_L("Re-Loading Physical Device"));
    97 	r=User::LoadPhysicalDevice(KPddFileName);
    98 	test(r==KErrNone || r==KErrAlreadyExists);
    99 
   100     test.Next(_L("Loading Logical Device"));
   101     r=User::LoadLogicalDevice(KLddFileName);
   102     test(r==KErrNone || r==KErrAlreadyExists);
   103 
   104 	test.Next(_L("Re-Loading Logical Device"));
   105 	r=User::LoadLogicalDevice(KLddFileName);
   106 	test(r==KErrNone || r==KErrAlreadyExists);
   107 	
   108     test.Next(_L("Open Device"));
   109     RDevice device;
   110     r=device.Open(RCryptoDriver::Name());
   111     test(r==KErrNone);
   112 
   113     test.Next(_L("Get Device Capabilities"));
   114     RCryptoDriver::TCaps caps;
   115     TPckg<RCryptoDriver::TCaps>capsPckg(caps);
   116     capsPckg.FillZ(); // Zero 'caps' so we can tell if GetCaps has really filled it
   117     device.GetCaps(capsPckg);
   118     TVersion expectedVer(RCryptoDriver::VersionRequired());
   119     test(caps.iVersion.iMajor==expectedVer.iMajor);
   120     test(caps.iVersion.iMinor==expectedVer.iMinor);
   121     test(caps.iVersion.iBuild==expectedVer.iBuild);
   122 
   123 	test.Next(_L("Close Device"));
   124     device.Close();
   125 
   126     test.Next(_L("Open Logical Channel"));
   127     RCryptoDriver ldd;
   128     r=ldd.Open();
   129     test(r==KErrNone);
   130 
   131 	test.Next(_L("GetHwVersions"));
   132     RCryptoDriver::THwVersionsBuf hwVersionsBuf;
   133     hwVersionsBuf.FillZ(); // Zero 'config' so we can tell if GetConfig has really filled it
   134     r=ldd.GetHwVersions(hwVersionsBuf);
   135     test(r==KErrNone);
   136 	test.Printf(_L("RNG h/w version 0x%x\n"), hwVersionsBuf().iRngHwVersion);
   137 	test.Printf(_L("DES/3DES h/w version 0x%x\n"), hwVersionsBuf().iDes3DesHwVersion);
   138 	test.Printf(_L("SHA1/MD5 h/w version 0x%x\n"), hwVersionsBuf().iSha1Md5HwVersion);
   139 	test.Printf(_L("AES h/w version 0x%x\n"), hwVersionsBuf().iAesHwVersion);
   140 	test.Printf(_L("PKA h/w version 0x%x\n"), hwVersionsBuf().iPkaHwVersion);
   141 
   142 	test.Next(_L("GetConfig"));
   143     RCryptoDriver::TConfigBuf configBuf;
   144     configBuf.FillZ(); // Zero 'config' so we can tell if GetConfig has really filled it
   145     r=ldd.GetConfig(configBuf);
   146     test(r==KErrNone);
   147 	
   148 	{
   149 	test.Next(_L("Open Logical Channel 2nd time"));
   150 	RCryptoDriver lddB;
   151 	r=lddB.Open();
   152 	test(r==KErrNone);
   153 	
   154 	test.Next(_L("GetConfig"));
   155 	RCryptoDriver::TConfigBuf configBuf;
   156 	configBuf.FillZ(); // Zero 'config' so we can tell if GetConfig has really filled it
   157 	r=lddB.GetConfig(configBuf);
   158 	test(r==KErrNone);
   159 	lddB.Close();
   160 	}
   161 	
   162     RCryptoDriver::TConfig& config=configBuf();
   163 
   164     test.Next(_L("SetConfig"));
   165     TInt setting = configBuf().iFakeDriverSetting+1;
   166     configBuf().iFakeDriverSetting = setting;
   167     r=ldd.SetConfig(configBuf); // Use SetConfig to change setting
   168     test(r==KErrNone);
   169 
   170     configBuf.FillZ();
   171     r=ldd.GetConfig(configBuf);
   172     test(r==KErrNone);
   173     test(configBuf().iFakeDriverSetting==setting);
   174 
   175     test.Next(_L("Check access by wrong client"));
   176     RCryptoDriver ldd2=ldd;
   177     r=ldd2.Duplicate(RThread(),EOwnerProcess);
   178     test(r==KErrAccessDenied);
   179 
   180     test.Next(_L("Check handle duplication"));
   181     ldd2=ldd;
   182     r=ldd2.Duplicate(RThread(),EOwnerThread);
   183     test(r==KErrNone);
   184     ldd2.Close();
   185 
   186 	
   187     TRequestStatus status;
   188     TRequestStatus status2;
   189 	TInt startCount;
   190 	TInt endCount;
   191 
   192 
   193 	{
   194     test.Next(_L("Random h/w use 2nd chan"));
   195 	HBufC8 *hbuf = HBufC8::NewLC(20);
   196 	TPtr8 buf = hbuf->Des();
   197 	buf.SetLength(buf.MaxLength());
   198 	ldd.Random(status,buf);
   199 	User::WaitForRequest(status);
   200 
   201 	test.Printf(_L("    Try 2nd chan"));
   202 	RCryptoDriver lddB;
   203     r=lddB.Open();
   204     test(r==KErrNone);
   205 	buf.SetLength(buf.MaxLength());
   206 	lddB.Random(status,buf);
   207 	User::WaitForRequest(status);
   208 
   209 	test.Printf(_L("    Try re-using 1st chan\n"));
   210 	buf.SetLength(buf.MaxLength());
   211 	ldd.Random(status,buf);
   212 	User::WaitForRequest(status);
   213 
   214 	lddB.Close();
   215 	CleanupStack::PopAndDestroy(hbuf);
   216 	}
   217 	
   218 
   219 
   220 
   221 #ifdef RNG_TESTS
   222     test.Next(_L("Random (h/w)"));
   223 #ifdef __MARM__
   224 	HBufC8 *hbuf = HBufC8::NewLC(32768);
   225 #else
   226 	HBufC8 *hbuf = HBufC8::NewLC(1024);
   227 #endif
   228 	TPtr8 buf = hbuf->Des();
   229 	buf.SetLength(buf.MaxLength());
   230 	startCount = User::NTickCount();
   231 	for(TInt i=0; i<100; ++i)
   232 		{
   233 		// Should discard existing contents of buf
   234 		ldd.Random(status,buf);
   235 		//	User::After(20);
   236 		//	return;
   237 		User::WaitForRequest(status);
   238 		}
   239 	endCount = User::NTickCount();
   240 	test.Printf(_L("Duration in ticks (h/w) = %d\n"), endCount - startCount);
   241 	
   242 #ifndef __MARM__
   243 	{
   244 	TUint32 *p = (TUint32 *) &buf[0];
   245 	test.Printf(_L("buf =\n    "));
   246 	for(TInt i = 0 ; i<256; ++i)
   247 		{
   248 		if(i && (i%16 == 0))
   249 			{
   250 			test.Printf(_L("\n    "));
   251 			}
   252 		test.Printf(_L("%08x "), p[i]);
   253 		}
   254 	test.Printf(_L("\n"));
   255 	}
   256 	
   257 #endif
   258 	
   259 	{
   260     test.Next(_L("Random (s/w)"));
   261 #ifdef RNG_USE_SVR
   262  	RRandomSession rs;
   263 	TRAPD(ret,rs.ConnectL());
   264 	User::LeaveIfError(ret);
   265 	CleanupClosePushL(rs);
   266 #endif
   267 
   268 	TInt startCount = User::NTickCount();
   269 	for(TInt i=0; i<100; ++i)
   270 		{
   271 		// Should discard existing contents of buf
   272 #ifdef RNG_USE_SVR
   273 		User::LeaveIfError(rs.GetRandom(buf));
   274 #else
   275 		TRandom::RandomL(buf);
   276 #endif
   277 		}
   278 	TInt endCount = User::NTickCount();
   279 	test.Printf(_L("Duration in ticks (s/w) = %d\n"), endCount - startCount);
   280 	
   281 #ifdef RNG_USE_SVR
   282 	CleanupStack::PopAndDestroy(&rs);
   283 #endif
   284 	}
   285 	
   286 
   287 #ifdef __MARM__
   288 	const TInt end = 10;
   289 #else
   290 	const TInt end = 1;
   291 #endif
   292 
   293 	for(TInt i = 0 ; i<end; ++i)
   294 		{
   295 		test.Printf(_L("buf[%d] = 0x%x\n"), i, buf[i]);
   296 		}
   297     r=status.Int();
   298     test(r==KErrNone);
   299 
   300 	CleanupStack::PopAndDestroy(hbuf);
   301 
   302 #endif
   303 
   304     test.Next(_L("Random - Generating key & IV for AES tests"));
   305 	test.Printf(_L("Generating random key\n"));
   306 	// Generate random 16 byte key
   307 	TBuf8<KEYLEN> key;
   308 	key.SetLength(key.MaxLength());
   309 	ldd.Random(status, key);
   310 	User::WaitForRequest(status);
   311 	key[0] = 'K';
   312 	key[1] = 'E';
   313 	key[2] = 'Y';
   314 	key[3] = '*';
   315 	for(int z=4; z<KEYLEN; ++z) key[z] = z;
   316 
   317 	test.Printf(_L("Generating random IV\n"));
   318 	// Generate random 16 byte IV
   319 	TBuf8<16> iv;
   320 	iv.SetLength(iv.MaxLength());
   321 	ldd.Random(status, iv);
   322 	User::WaitForRequest(status);
   323 	iv[0] = 'I';
   324 	iv[1] = 'V';
   325 	iv[2] = '*';
   326 	iv[3] = '*';
   327 
   328 	TBuf8<BUFLEN> plaintext;
   329 	plaintext.SetLength(BUFLEN);
   330 	plaintext[0] = 'P';
   331 	plaintext[1] = 'L';
   332 	plaintext[2] = 'A';
   333 	plaintext[3] = 'I';
   334 	plaintext[4] = 'N';
   335 	plaintext.SetLength(BUFLEN);
   336 	for(int i=0; i<BUFLEN; ++i)
   337 		{
   338 		plaintext[i] = i;
   339 		}
   340 	
   341 
   342 
   343     test.Next(_L("AES - S/W"));
   344 
   345 #ifdef PADDING_PKCS7
   346 	CPadding *pad = CPaddingPKCS7::NewLC(16);
   347 #else
   348 	CPadding *pad = CPaddingNone::NewLC(16);
   349 #endif
   350 
   351 #ifdef USE_CBCMODE
   352 	// CBC
   353 	test.Printf(_L("    CBC\n"));
   354 	CAESEncryptor *rawaes = CAESEncryptor::NewLC(key); // pad, rawaes
   355 	CModeCBCEncryptor *cbc = CModeCBCEncryptor::NewL(rawaes, iv);
   356 	CleanupStack::Pop(rawaes); // pad
   357 	CleanupStack::PushL(cbc);  // pad, cbc
   358 
   359 	CBufferedEncryptor *aes = CBufferedEncryptor::NewL(cbc, pad);
   360 	CleanupStack::Pop(cbc); // pad
   361 	CleanupStack::Pop(pad); //
   362 	CleanupStack::PushL(aes); //aes
   363 #else
   364 	// ECB
   365 	test.Printf(_L("    ECB\n"));
   366 	CAESEncryptor *rawaes = CAESEncryptor::NewLC(key); // pad, rawaes
   367 	CBufferedEncryptor *aes = CBufferedEncryptor::NewL(rawaes, pad);
   368 	CleanupStack::Pop(rawaes); // pad
   369 	CleanupStack::Pop(pad); // 
   370 	CleanupStack::PushL(aes); // aes
   371 #endif	
   372 	
   373 	{
   374 	TBuf8<10> b1;
   375 	b1 = _L8("0123456789");
   376 	TBuf8<6> b2;
   377 	b2 = _L8("012345");
   378 	TBuf8<64> b3;
   379 	
   380 	test.Printf(_L("Test of Process/ProcessFinal\n"));
   381 	aes->Reset();
   382 	aes->Process(b1,b3);
   383 	aes->ProcessFinalL(b2, b3);
   384 	aes->Reset();
   385 	}
   386 	
   387 	test.Printf(_L("About to s/w encrypt\n"));
   388 	TBuf8<BUFLEN> sw;
   389 	sw.SetLength(0);
   390 	aes->ProcessFinalL(plaintext, sw);
   391 
   392 	test.Printf(_L("sw =\n    "));
   393 	for(TInt i = 0 ; i<256; ++i)
   394 		{
   395 		if(i && (i%16 == 0))
   396 			{
   397 			test.Printf(_L("\n    "));
   398 			}
   399 		test.Printf(_L("%02x "), sw[i]);
   400 		}
   401 	test.Printf(_L("\n"));
   402 
   403 	aes->Reset();
   404 	test.Printf(_L("S/W - Encrypt %d bytes %d times\n"), BUFLEN, LOOPCOUNT);
   405 	startCount = User::NTickCount();
   406 	for(int z=0; z < LOOPCOUNT; ++z)
   407 		{
   408 		//aes->Reset();
   409 		sw.SetLength(0);
   410 		//aes->ProcessFinalL(plaintext, sw);
   411 		aes->Process(plaintext, sw);
   412 		}
   413 	endCount = User::NTickCount();
   414 	test.Printf(_L("Duration in ticks (s/w) = %d\n"), endCount - startCount);
   415 
   416 
   417 	CleanupStack::PopAndDestroy(aes);
   418 
   419     test.Next(_L("AES - H/W"));
   420 
   421 	
   422 	test.Printf(_L("Setting config\n"));
   423 #ifdef USE_CBCMODE
   424 	r = ldd.SetAesConfig(ETrue, RCryptoDriver::ECbcMode, key, iv);
   425 	test.Printf(_L("    CBC\n"));
   426 #else
   427 	r = ldd.SetAesConfig(ETrue, RCryptoDriver::EEcbMode, key, iv);
   428 	test.Printf(_L("    ECB\n"));
   429 #endif
   430 	test(r==KErrNone);
   431 	
   432 
   433 	test.Printf(_L("H/W - Encrypt %d bytes %d times\n"), BUFLEN, LOOPCOUNT);
   434 	TBuf8<BUFLEN> tmp2;
   435 	startCount = User::NTickCount();
   436 	for(int z=0; z < LOOPCOUNT; ++z)
   437 		{
   438 		tmp2.SetLength(0);
   439 		ldd.AesRead(status2, tmp2, tmp2.MaxLength());
   440 		ldd.AesWrite(status, plaintext);
   441 		User::WaitForRequest(status);	
   442 		User::WaitForRequest(status2);
   443 #ifdef DISABLE_AES_CHECKS
   444 		tmp2.SetLength(BUFLEN);
   445 #endif
   446 		}
   447 	endCount = User::NTickCount();
   448 	test.Printf(_L("Duration in ticks (h/w) = %d\n"), endCount - startCount);
   449 
   450 	
   451 #ifndef DISABLE_AES_CHECKS
   452 #ifdef __MARM__
   453 	test.Printf(_L("Compare tmp2 and sw\n"));
   454 	test(tmp2==sw);
   455 #else
   456 	test.Printf(_L("Compare tmp2 and plaintext\n"));
   457     test(tmp2==plaintext);
   458 #endif
   459 #endif
   460 
   461 
   462 
   463 
   464 
   465 //	User::Panic(_L("Fake app crash"), 42);
   466 
   467     test.Next(_L("Close Logical Channel"));
   468     ldd.Close();
   469 
   470     __KHEAP_MARKEND;
   471 
   472     test.Next(_L("Unload Logical Device"));
   473     r=User::FreeLogicalDevice(RCryptoDriver::Name());
   474     test(r==KErrNone);
   475 
   476     test.Next(_L("Unload Physical Device"));
   477     TName pddName(RCryptoDriver::Name());
   478     _LIT(KVariantExtension,".h4");
   479     pddName.Append(KVariantExtension);
   480     r=User::FreePhysicalDevice(pddName);
   481     test(r==KErrNone);
   482 
   483     test.End();
   484 
   485 	test.Printf(KTxtPressAnyKey);
   486 	test.Getch(); // get and ignore character
   487 	test.Close();
   488     }
   489 
   490 
   491 // End of file