Esempio n. 1
0
void CTLogger::LogL(const TDesC& aString)
	{
	// Open the file server and file
	RFs fs;
	User::LeaveIfError(fs.Connect());
	CleanupClosePushL(fs);
	
	// Open the file or create it if doesn't exist, create it
	RFile file;
	TDriveUnit sysDrive (fs.GetSystemDrive());
	TBuf<128> logFile (sysDrive.Name());
	logFile.Append(KLogFilename);
	
	TInt error = file.Open(fs, logFile, EFileWrite|EFileShareAny);
	if (error == KErrNotFound)
		{
		error = file.Create(fs, logFile, EFileWrite|EFileShareAny);
		}
	User::LeaveIfError(error);
	CleanupClosePushL(file);
	
	// Seek to the end of the file
	TInt tmp = 0;
	file.Seek(ESeekEnd, tmp);

	// And do some logging
	TBuf8<MAX_LEN> buf;
	buf.Copy(aString);
	file.Write(buf);
	file.Write(KNewLine);

	// Close and tidy up
	CleanupStack::PopAndDestroy(2, &fs);
	}
Esempio n. 2
0
//Get DSC database name
void CDscDatabase::GetDatabaseNameL(TDes& aDatabaseName)
	{
	RFs fs;
	CleanupClosePushL(fs);
	User::LeaveIfError(fs.Connect());
	
	// retrieve system drive
	TDriveNumber driveNumber = fs.GetSystemDrive();
 
	// convert to char
    TChar driveChar;
    User::LeaveIfError(RFs::DriveToChar(driveNumber, driveChar));
 
	aDatabaseName.Format(KDatabaseName, (TUint)driveChar);
	CleanupStack::PopAndDestroy(&fs);
	}
/*
HELPER:
The actual step thta does the encryption and decryption

*/
TVerdict CTlsProvTestActive::EncryptAndDecryptL(CTLSSession* aPtrTlsSession, CTlsProvStep* /*aStep*/)
	{
	
	RFs filesys;
	filesys.Connect();
	RFile fileTmp_t;
	
	TBuf8<1024> TempPrint;
	TDriveUnit sysDrive (filesys.GetSystemDrive());
	TDriveName sysDriveName (sysDrive.Name());
	
	TBuf<128> fileName (sysDriveName);
	fileName.Append(_L("\\data\\ActualAppData.bin"));
	
	TInt result_t = fileTmp_t.Open(filesys, fileName, EFileRead);
		
  	INFO_PRINTF1(_L("EncryptAndDecryptL 1"));
	if(!result_t)
		fileTmp_t.Read( TempPrint );
	fileTmp_t.Close();	

	
	iStatus = KRequestPending;
	HBufC8* Output = NULL;
	TRecordProtocol RecType;
	RecType= EHandshake;
	TInt64 ASeqNumber = 0;
	Output = HBufC8::NewL(TempPrint.Length() + 24);
	aPtrTlsSession->EncryptL( 
		TempPrint, 
		Output, 
		ASeqNumber,RecType) ;
	
  	INFO_PRINTF1(_L("EncryptAndDecryptL 2"));
  	
  	fileName.Copy(sysDriveName);
  	fileName.Append(_L("\\data\\EncryptOutput.bin"));
	
	result_t = fileTmp_t.Open(filesys, fileName, EFileRead);
	
	TBuf8<1024> ActualOutput;	
	if(!result_t)
		fileTmp_t.Read(ActualOutput);
	fileTmp_t.Close();
	if(ActualOutput.Compare(Output->Des()) != 0)
		return EFail;

	//Test Decryption here

  	INFO_PRINTF1(_L("EncryptAndDecryptL 3"));
  	
  	fileName.Copy(sysDriveName);
  	fileName.Append(_L("\\data\\DecryptionInput.bin"));
	
	result_t = fileTmp_t.Open(filesys, fileName, EFileRead);
	
	if(!result_t)
		fileTmp_t.Read( ActualOutput );
	fileTmp_t.Close();


	iStatus = KRequestPending;
	HBufC8* OutputDe = NULL;
	
	RecType= EHandshake;
	TInt64 ASeqNumberDe = 0;
	OutputDe = HBufC8::NewL(ActualOutput.Length());
	aPtrTlsSession->DecryptAndVerifyL( 
		ActualOutput, 
		OutputDe, 
		ASeqNumberDe,RecType) ;

	fileName.Copy (sysDriveName);
	fileName.Append(_L("\\data\\DecryptionOutputNoMac.bin"));
	
	result_t = fileTmp_t.Open(filesys, fileName, EFileRead);
	
	if(!result_t)
		fileTmp_t.Read(ActualOutput);
	fileTmp_t.Close();
	
  	INFO_PRINTF1(_L("EncryptAndDecryptL 4"));
	if(ActualOutput.Compare(OutputDe->Des()) != 0)
   {
	   iLogInfo.Copy(_L("	EncryptDecr Fail"));
		return EFail;
   }
	else
   {
	   iLogInfo.Copy(_L("	EncryptDecr Success"));
		return EPass;
   }
	}
Esempio n. 4
0
LOCAL_C void mainL() // initialize and call example code under cleanup stack
    {
	test.Title();
	CTestConsole *con = CTestConsole::NewL(test.Console());

	RFs fs;
	User::LeaveIfError(fs.Connect());
	CleanupClosePushL(fs);

	TDriveUnit sysDrive (fs.GetSystemDrive());
	TBuf<24> logFile (sysDrive.Name());
	logFile.Append(_L("\\temblog.txt"));

	RFile file;
	User::LeaveIfError(file.Replace(fs, logFile, EFileShareAny|EFileWrite));
	CleanupClosePushL(file);

	con->SetLogFile(file);
	test.SetConsole(con);

    TInt r;
	RDebug::Printf("Hello from user side\n");
	
    test.Start(_L(" @SYMTestCaseID:SEC-CRYPTOSPI-TEMB-0001 Load driver "));

    test.Next(_L("Loading Physical Device"));
    r=User::LoadPhysicalDevice(KPddFileName);
    test(r==KErrNone || r==KErrAlreadyExists);

    test.Next(_L("Loading Logical Device"));
    r=User::LoadLogicalDevice(KLddFileName);
    test(r==KErrNone || r==KErrAlreadyExists);

	//
	// Generate key and IV
	//
    test.Start(_L("Random - Generating key & IV for AES tests"));
	test.Printf(_L("\tGenerating random key\n"));
	// Generate random 16 byte key
	TBuf8<KEYLEN> key;
	key.SetLength(key.MaxLength());
	TRandom::RandomL(key);
	key[0] = 'K';
	key[1] = 'E';
	key[2] = 'Y';
	key[3] = '*';
	for(int z=4; z<KEYLEN; ++z) key[z] = z;

	test.Printf(_L("\tGenerating random IV\n"));
	// Generate random 16 byte IV
	TBuf8<16> iv;
	iv.SetLength(iv.MaxLength());
	TRandom::RandomL(iv);
	iv[0] = 'I';
	iv[1] = 'V';
	iv[2] = '*';
	iv[3] = '*';

	TBuf8<BUFLEN> plaintext;
	plaintext.FillZ();
	plaintext.SetLength(BUFLEN);
	plaintext[0] = 'P';
	plaintext[1] = 'L';
	plaintext[2] = 'A';
	plaintext[3] = 'I';
	plaintext[4] = 'N';
	for(int i=0; i<BUFLEN; ++i)
		{
		plaintext[i] = i;
		}


	//
	// KMS tests
	//
    test.Next(_L("KMS - Store key"));
	TBuf8<BUFLEN+16> kmsData;
	kmsData.FillZ();
	kmsData.SetLength(0);
	do
		{
		RKeyMgmtSession kms;
		User::LeaveIfError(kms.Connect());
		CleanupClosePushL(kms);
		
		TKeyHandle keyHandle;
		User::LeaveIfError(kms.StoreKey(key, keyHandle));
		_LIT_SECURITY_POLICY_PASS(KAlwaysPass);
		User::LeaveIfError(kms.AddUsage(keyHandle, 0 /* operation */, KAlwaysPass));
		
		test.Next(_L("KMS - Attempt to use key via embedded key handle"));
		TPckgBuf<TKeyHandle> keyHandlePkg;
		keyHandlePkg() = keyHandle;

		TKeyProperty keyProperty = {KAesUid, KNullUid, KSymmetricKeyUid, KNonExtractableKey};
		CCryptoParams* keyParam =CCryptoParams::NewLC();
		keyParam->AddL(keyHandlePkg, KSymmetricKeyParameterUid);
		CKey *ckey=CKey::NewL(keyProperty, *keyParam);
		CleanupStack::PopAndDestroy(keyParam);
		CleanupStack::PushL(ckey);
		CryptoSpi::CSymmetricCipher *aes = 0;
		CSymmetricCipherFactory::CreateSymmetricCipherL(aes,
														KAesUid,
														*ckey,
														KCryptoModeEncryptUid,
														KOperationModeCBCUid,
														KPaddingModePKCS7Uid,
														NULL);
		CleanupStack::PopAndDestroy(ckey);
		CleanupStack::PushL(aes);

		aes->SetOperationModeL(CryptoSpi::KOperationModeCBCUid);
		aes->SetIvL(iv);		

		aes->ProcessFinalL(plaintext, kmsData);

		CleanupStack::PopAndDestroy(aes);
		CleanupStack::PopAndDestroy(&kms);
		} while(false);


	//
	// Encrypt using legacy API
	//
	TBuf8<BUFLEN+16> sw;
	sw.FillZ();
	sw.SetLength(0);
	do 
		{ 
		test.Next(_L("Encrypt using key directly (non-KMS)"));
		
		// ECB
		test.Printf(_L("    CBC\n"));
		CAESEncryptor *rawaes = CAESEncryptor::NewLC(key); // rawaes
		CModeCBCEncryptor *cbc = CModeCBCEncryptor::NewL(rawaes, iv);
		CleanupStack::Pop(rawaes); //
		CleanupStack::PushL(cbc);  // cbc
		
#ifdef PKCS7PAD
		CPadding *pad = CPaddingPKCS7::NewLC(16); // cbc, pad
#else
		CPadding *pad = CPaddingNone::NewLC(16); // cbc, pad
#endif
		CBufferedEncryptor *aes = CBufferedEncryptor::NewL(cbc, pad);
		CleanupStack::Pop(pad); // cbc
		CleanupStack::Pop(cbc);
		CleanupStack::PushL(aes); // aes
		
		test.Printf(_L("About to s/w encrypt (old api)\n"));
		aes->ProcessFinalL(plaintext, sw);
		
		CleanupStack::PopAndDestroy(aes);
		} while(false);

	test.Printf(_L("Checking KMS encrypt and direct encrypt had the same result\n"));
	test(kmsData == sw);
    test.End();
	
	test.Printf(_L("\r\n0 tests failed out of 1\r\n"));
		
	// test.Printf(KTxtPressAnyKey);
	// test.Getch(); // get and ignore character
	test.Close();

	CleanupStack::PopAndDestroy(&file);
	CleanupStack::PopAndDestroy(&fs);
    }