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