crypto/weakcryptospi/test/tcryptospi/src/signerverifierstep.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 "signerverifierstep.h"
       
    25 
       
    26 #include "keypair.h"
       
    27 #include "cryptosignatureapi.h"
       
    28 #include "cryptokeypairgeneratorapi.h"
       
    29 
       
    30 using namespace CryptoSpi;
       
    31 
       
    32 
       
    33 CSignerVerifierStep::~CSignerVerifierStep()
       
    34 	{
       
    35 	}
       
    36 
       
    37 CSignerVerifierStep::CSignerVerifierStep()
       
    38 	{
       
    39 	SetTestStepName(KSignerVerifierStep);
       
    40 	}
       
    41 
       
    42 TVerdict CSignerVerifierStep::doTestStepPreambleL()
       
    43 	{
       
    44 	SetTestStepResult(EPass);
       
    45 	return TestStepResult();
       
    46 	}
       
    47 
       
    48 
       
    49 TVerdict CSignerVerifierStep::doTestStepL()
       
    50 	{
       
    51 	INFO_PRINTF1(_L("*** Signer/Verifier - Signature and Verification ***"));
       
    52 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
       
    53 	
       
    54   	if (TestStepResult()==EPass)
       
    55 		{
       
    56 		//Assume faliure, unless all is successful
       
    57 		SetTestStepResult(EFail);
       
    58 		
       
    59 		TVariantPtrC testVariant;
       
    60 		TVariantPtrC typeVariant;
       
    61 		TVariantPtrC dataVariant;
       
    62 
       
    63 		if(	!GetStringFromConfig(ConfigSection(),KConfigSignVerifyType, typeVariant) 
       
    64 			|| !GetStringFromConfig(ConfigSection(),KConfigSignVerifyData, dataVariant))
       
    65 			{
       
    66 			// Leave if there's any error.
       
    67 			User::Leave(KErrNotFound);
       
    68 			}
       
    69 		else
       
    70 			{
       
    71 			TUid sigType = typeVariant;
       
    72 						
       
    73 			//Construct a Crypto Parameters object to store the necessary key pair generator parameters
       
    74 			CCryptoParams* keyParams = CCryptoParams::NewL();
       
    75 			CleanupStack::PushL(keyParams);
       
    76 
       
    77 			//Create Key Pair Generator Object
       
    78 			CKeyPairGenerator * keypairImpl = NULL;
       
    79 			
       
    80 			//Define error code as first TRAP is beyond scope
       
    81 			TInt err = 0;
       
    82 			
       
    83 			if (sigType == KRsaSignerUid)			
       
    84 				{
       
    85 				INFO_PRINTF1(_L("Creating Key Pair Generator (RSA)..."));
       
    86 
       
    87 				keyParams->AddL(KKeyExponent, KRsaKeyParameterEUid);
       
    88 				keyParams->AddL(KRsaPrivateKeyStandard, KRsaKeyTypeUid);
       
    89 				
       
    90 				//Construct an 'RSA' Key Pair Generator Implementation Object
       
    91 				TRAP_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(keypairImpl, 
       
    92 																KRSAKeyPairGeneratorUid, 
       
    93 																keyParams));
       
    94 				}
       
    95 			else
       
    96 				{
       
    97 				INFO_PRINTF1(_L("Creating Key Pair Generator (DSA)..."));
       
    98 				
       
    99 				//Construct a 'DSA' Key Pair Generator Implementation Object
       
   100 				TRAP_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(keypairImpl,
       
   101                                             					KDSAKeyPairGeneratorUid,
       
   102                                             					NULL));
       
   103 				}
       
   104 				
       
   105 			CleanupStack::PushL(keypairImpl);
       
   106 	
       
   107 			// Generate a Key Pair 
       
   108 			INFO_PRINTF1(_L("Generating Key Pair..."));
       
   109 			
       
   110 			CKeyPair* keyPair = NULL;
       
   111 				
       
   112 			TRAP_LOG(err,keypairImpl->GenerateKeyPairL(1024, 
       
   113 													*keyParams, 
       
   114 													keyPair));
       
   115 
       
   116 			CleanupStack::PushL(keyPair);
       
   117 			
       
   118 			
       
   119 			// Create a Signer Object	
       
   120 			INFO_PRINTF1(_L("Creating Signer Object..."));
       
   121 			CCryptoParams* svParams = CCryptoParams::NewL();
       
   122 			CleanupStack::PushL(svParams);
       
   123 
       
   124 			CSigner * implsig = NULL;	
       
   125 		    TRAP_LOG(err,CSignatureFactory::CreateSignerL(implsig,
       
   126 													   sigType,
       
   127 													   keyPair->PublicKey(),
       
   128 													   KPaddingModePkcs1_v1_5_SignatureUid,
       
   129 													   svParams));
       
   130 										   
       
   131 			CleanupStack::PushL(implsig);
       
   132 		
       
   133 		
       
   134 			// Create a Verifier
       
   135 			INFO_PRINTF1(_L("Creating Verifier Object..."));
       
   136 			
       
   137 			CVerifier * implver = NULL;	
       
   138 		    TRAP_LOG(err,CSignatureFactory::CreateVerifierL(implver,
       
   139 										   				sigType == KRsaSignerUid ? KRsaVerifierUid : KDsaVerifierUid,
       
   140 										   				keyPair->PrivateKey(),
       
   141 										   				KPaddingModePkcs1_v1_5_SignatureUid,
       
   142 										   				svParams));
       
   143 										   				
       
   144 			CleanupStack::PushL(implver);
       
   145 										   
       
   146 			//Define a new signature object
       
   147 			CCryptoParams *signature = CCryptoParams::NewL();
       
   148 			CleanupStack::PushL(signature);
       
   149 
       
   150 			//Convert the source data to an 8 Bit Descriptor
       
   151 			HBufC8* sourceData = HBufC8::NewLC(dataVariant.Length());
       
   152 			TPtr8 sourceDataPtr = sourceData->Des();
       
   153 			
       
   154 			sourceDataPtr.Copy(dataVariant);
       
   155 			
       
   156 			//Set the signer object to use the 'Private' Key of the key pair
       
   157 			INFO_PRINTF1(_L("Setting Signer Private Key..."));
       
   158 			TRAP_LOG(err,implsig->SetKeyL(keyPair->PrivateKey()));
       
   159 			
       
   160 			//Generate a signature for the source data
       
   161 			INFO_PRINTF1(_L("Signing..."));
       
   162 			TRAP_LOG(err,implsig->SignL(*sourceData, *signature));
       
   163     	
       
   164     		TBool bResult = EFalse;	
       
   165     		
       
   166     		//Set the verifier object to use the 'Public' Key of the key pair
       
   167     		INFO_PRINTF1(_L("Setting Verifier Public Key..."));
       
   168 			TRAP_LOG(err,implver->SetKeyL(keyPair->PublicKey()));
       
   169 			
       
   170 			//Verify that the generated signature is valid for the given data
       
   171 			INFO_PRINTF1(_L("Verifying..."));
       
   172 			TRAP_LOG(err,implver->VerifyL(*sourceData, *signature, bResult));
       
   173 
       
   174 			if (bResult)
       
   175 				{
       
   176 				// verifier agrees signature is valid
       
   177 				INFO_PRINTF1(_L("PASS: Verification of Generated Signature Successful"));
       
   178 				SetTestStepResult(EPass);
       
   179 				}
       
   180 			else
       
   181 				{
       
   182 				//verification failure, signature is invalid
       
   183 				ERR_PRINTF2(_L("FAIL: Verification Failure of Generated Signature - %d"), err);
       
   184 				SetTestStepResult(EFail);
       
   185 				}
       
   186 			
       
   187 			CleanupStack::PopAndDestroy(sourceData);	
       
   188 			CleanupStack::PopAndDestroy(signature);
       
   189 			CleanupStack::PopAndDestroy(implver);
       
   190 			CleanupStack::PopAndDestroy(implsig);
       
   191 			CleanupStack::PopAndDestroy(svParams);
       
   192 			
       
   193 			CleanupStack::PopAndDestroy(keyPair);
       
   194 			CleanupStack::PopAndDestroy(keypairImpl);
       
   195 			CleanupStack::PopAndDestroy(keyParams);
       
   196 			}
       
   197 
       
   198 		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
       
   199 		
       
   200 		}
       
   201 	  return TestStepResult();
       
   202 	}
       
   203 
       
   204 
       
   205 
       
   206 TVerdict CSignerVerifierStep::doTestStepPostambleL()
       
   207 	{
       
   208 	return TestStepResult();
       
   209 	}