crypto/weakcryptospi/test/tcryptospi/src/asymmetriccipherencrypteddatacheckstep.cpp
changeset 8 35751d3474b7
child 49 2f10d260163b
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 * Example CTestStep derived implementation
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  @file
       
    22  @internalTechnology
       
    23 */
       
    24 #include "asymmetriccipherencrypteddatacheckstep.h"
       
    25 
       
    26 #include "cryptoasymmetriccipherapi.h"
       
    27 #include "cryptokeypairgeneratorapi.h"
       
    28 #include "keypair.h"
       
    29 #include "filereader.h"
       
    30 
       
    31 using namespace CryptoSpi;
       
    32 
       
    33 CASymmetricCipherEncryptedDataCheckStep::~CASymmetricCipherEncryptedDataCheckStep()
       
    34 	{
       
    35 	}
       
    36 
       
    37 
       
    38 CASymmetricCipherEncryptedDataCheckStep::CASymmetricCipherEncryptedDataCheckStep()
       
    39 	{
       
    40 	SetTestStepName(KASymmetricCipherEncryptedDataCheckStep);
       
    41 	}
       
    42 
       
    43 
       
    44 TVerdict CASymmetricCipherEncryptedDataCheckStep::doTestStepPreambleL()
       
    45 	{
       
    46 	SetTestStepResult(EPass);
       
    47 	return TestStepResult();
       
    48 	}
       
    49 
       
    50 
       
    51 TVerdict CASymmetricCipherEncryptedDataCheckStep::doTestStepL()
       
    52 	{
       
    53 
       
    54 	INFO_PRINTF1(_L("*** Asymmetric Cipher - Encrypted Data Check ***"));
       
    55 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
       
    56 	
       
    57   	if (TestStepResult()==EPass)
       
    58 		{
       
    59 		
       
    60 		//Assume faliure, unless all is successful
       
    61 		SetTestStepResult(EFail);
       
    62 		
       
    63 		TVariantPtrC algorithm;
       
    64 		TVariantPtrC srcPath;
       
    65 		TVariantPtrC encrypted;
       
    66 		TVariantPtrC paddingMode;
       
    67 		
       
    68 		
       
    69 		if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid, algorithm) || 
       
    70 			!GetStringFromConfig(ConfigSection(),KConfigPaddingMode, paddingMode) ||
       
    71 			!GetStringFromConfig(ConfigSection(),KConfigSourcePath, srcPath) || 
       
    72 			!GetStringFromConfig(ConfigSection(),KConfigExEncryptedData, encrypted))
       
    73 			{
       
    74 			User::Leave(KErrNotFound);
       
    75 			}
       
    76 		else
       
    77 			{
       
    78 			//assume failure, unless all is successful
       
    79 			SetTestStepResult(EFail);
       
    80 			
       
    81 			CCryptoParams* keyParams = CCryptoParams::NewLC(); 
       
    82 			
       
    83 			//****************************************************
       
    84 			//Create Key Pair and Key Pair Generator Objects
       
    85    			CKeyPair* keyPair = NULL;
       
    86 			CKeyPairGenerator * keypairImpl = NULL;
       
    87 			
       
    88 			// create an RSA key pair
       
    89 			INFO_PRINTF1(_L("Generating RSA keys"));
       
    90 
       
    91 			keyParams->AddL(KKeyExponent, KRsaKeyParameterEUid);
       
    92 			keyParams->AddL(KRsaPrivateKeyStandard, KRsaKeyTypeUid);
       
    93 
       
    94 			// create a key pair generator implementation interface
       
    95 			TRAPD_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(keypairImpl, 
       
    96 													KRSAKeyPairGeneratorUid, 
       
    97 													keyParams));
       
    98 													
       
    99 			CleanupStack::PushL(keypairImpl);
       
   100 
       
   101 			// Create a Key Pair	
       
   102 			TRAP_LOG(err,keypairImpl->GenerateKeyPairL(1024, *keyParams, keyPair));
       
   103 			
       
   104 			CleanupStack::PushL(keyPair);
       
   105 			
       
   106 			//*****************************************************
       
   107 			
       
   108 			INFO_PRINTF1(_L("Creating Asymmetric Cipher Object..."));
       
   109 
       
   110 			// Create a Symmetric Cipher 
       
   111 			CryptoSpi::CAsymmetricCipher * impl = NULL;	
       
   112 			
       
   113 			TRAP(err,CAsymmetricCipherFactory::CreateAsymmetricCipherL(impl,
       
   114 														algorithm,
       
   115 														keyPair->PublicKey(),
       
   116 														KCryptoModeEncryptUid,
       
   117 														paddingMode,
       
   118 														NULL));
       
   119 	
       
   120 			if(impl && (err == KErrNone))
       
   121 				{
       
   122 				CleanupStack::PushL(impl);
       
   123 				
       
   124 				INFO_PRINTF1(_L("*** Asymmetric Cipher Implementation Created... ***"));
       
   125 			
       
   126 				//read from src file
       
   127 				CFileReader* srcData = CFileReader::NewLC(srcPath);		
       
   128 			
       
   129 				//Create buffer for encrypted data
       
   130 				TInt maxOutputLength = impl->GetMaximumOutputLengthL();
       
   131 				HBufC8* encrypted =	HBufC8::NewLC(maxOutputLength);
       
   132 				TPtr8 encryptedPtr = encrypted->Des();
       
   133 				
       
   134 				INFO_PRINTF1(_L("Encrypting Source Data..."));
       
   135 
       
   136 				//Perform the encryption operation
       
   137 				TRAP_LOG(err,impl->ProcessL(TPtrC8(*srcData), encryptedPtr));
       
   138 				
       
   139 				if(err == KErrNone)
       
   140 					{
       
   141 					INFO_PRINTF1(_L("*** DATA ENCRYPTED ***"));
       
   142 					
       
   143 					//Check that the source data matches the data thats 
       
   144 					//been encrypted then decrypted
       
   145 					if(	!encryptedPtr.Compare(TPtrC8(*srcData)))
       
   146 						{
       
   147 						INFO_PRINTF1(_L("PASS : Encrypted Data Matches Expected"));
       
   148 						SetTestStepResult(EPass);
       
   149 						}
       
   150 					else
       
   151 						{
       
   152 						INFO_PRINTF1(_L("FAIL : Encrypted Data and Expected Mismatch"));	
       
   153 						SetTestStepResult(EFail);
       
   154 						}
       
   155 					}
       
   156 	
       
   157 				CleanupStack::PopAndDestroy(encrypted); 
       
   158 				CleanupStack::PopAndDestroy(srcData);
       
   159 				CleanupStack::PopAndDestroy(impl);
       
   160 				}
       
   161 			else
       
   162 				{
       
   163 				ERR_PRINTF2(_L("*** FAIL: Failed to Create Asymmetric Object - %d ***"), err);
       
   164 				User::Leave(err);	
       
   165 				}
       
   166 				
       
   167 			CleanupStack::PopAndDestroy(keyPair);
       
   168 			CleanupStack::PopAndDestroy(keypairImpl);
       
   169 			CleanupStack::PopAndDestroy(keyParams);
       
   170 			}
       
   171 			
       
   172 		}
       
   173 		
       
   174 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
       
   175 
       
   176 	return TestStepResult();
       
   177 	}
       
   178 
       
   179 
       
   180 TVerdict CASymmetricCipherEncryptedDataCheckStep::doTestStepPostambleL()
       
   181 	{
       
   182 	return TestStepResult();
       
   183 	}