Update contrib.
2 * Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Example CTestStep derived implementation
24 #include "signerverifierstep.h"
26 #include <cryptospi/keypair.h>
27 #include <cryptospi/cryptosignatureapi.h>
28 #include <cryptospi/cryptokeypairgeneratorapi.h>
30 using namespace CryptoSpi;
33 CSignerVerifierStep::~CSignerVerifierStep()
37 CSignerVerifierStep::CSignerVerifierStep()
39 SetTestStepName(KSignerVerifierStep);
42 TVerdict CSignerVerifierStep::doTestStepPreambleL()
44 SetTestStepResult(EPass);
45 return TestStepResult();
49 TVerdict CSignerVerifierStep::doTestStepL()
51 INFO_PRINTF1(_L("*** Signer/Verifier - Signature and Verification ***"));
52 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
54 if (TestStepResult()==EPass)
56 //Assume faliure, unless all is successful
57 SetTestStepResult(EFail);
59 TVariantPtrC testVariant;
60 TVariantPtrC typeVariant;
61 TVariantPtrC dataVariant;
63 if( !GetStringFromConfig(ConfigSection(),KConfigSignVerifyType, typeVariant)
64 || !GetStringFromConfig(ConfigSection(),KConfigSignVerifyData, dataVariant))
66 // Leave if there's any error.
67 User::Leave(KErrNotFound);
71 TUid sigType = typeVariant;
73 //Construct a Crypto Parameters object to store the necessary key pair generator parameters
74 CCryptoParams* keyParams = CCryptoParams::NewL();
75 CleanupStack::PushL(keyParams);
77 //Create Key Pair Generator Object
78 CKeyPairGenerator * keypairImpl = NULL;
80 //Define error code as first TRAP is beyond scope
83 if (sigType == KRsaSignerUid)
85 INFO_PRINTF1(_L("Creating Key Pair Generator (RSA)..."));
87 keyParams->AddL(KKeyExponent, KRsaKeyParameterEUid);
88 keyParams->AddL(KRsaPrivateKeyStandard, KRsaKeyTypeUid);
90 //Construct an 'RSA' Key Pair Generator Implementation Object
91 TRAP_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(keypairImpl,
92 KRSAKeyPairGeneratorUid,
97 INFO_PRINTF1(_L("Creating Key Pair Generator (DSA)..."));
99 //Construct a 'DSA' Key Pair Generator Implementation Object
100 TRAP_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(keypairImpl,
101 KDSAKeyPairGeneratorUid,
105 CleanupStack::PushL(keypairImpl);
107 // Generate a Key Pair
108 INFO_PRINTF1(_L("Generating Key Pair..."));
110 CKeyPair* keyPair = NULL;
112 TRAP_LOG(err,keypairImpl->GenerateKeyPairL(1024,
116 CleanupStack::PushL(keyPair);
119 // Create a Signer Object
120 INFO_PRINTF1(_L("Creating Signer Object..."));
121 CCryptoParams* svParams = CCryptoParams::NewL();
122 CleanupStack::PushL(svParams);
124 CSigner * implsig = NULL;
125 TRAP_LOG(err,CSignatureFactory::CreateSignerL(implsig,
127 keyPair->PublicKey(),
128 KPaddingModePkcs1_v1_5_SignatureUid,
131 CleanupStack::PushL(implsig);
135 INFO_PRINTF1(_L("Creating Verifier Object..."));
137 CVerifier * implver = NULL;
138 TRAP_LOG(err,CSignatureFactory::CreateVerifierL(implver,
139 sigType == KRsaSignerUid ? KRsaVerifierUid : KDsaVerifierUid,
140 keyPair->PrivateKey(),
141 KPaddingModePkcs1_v1_5_SignatureUid,
144 CleanupStack::PushL(implver);
146 //Define a new signature object
147 CCryptoParams *signature = CCryptoParams::NewL();
148 CleanupStack::PushL(signature);
150 //Convert the source data to an 8 Bit Descriptor
151 HBufC8* sourceData = HBufC8::NewLC(dataVariant.Length());
152 TPtr8 sourceDataPtr = sourceData->Des();
154 sourceDataPtr.Copy(dataVariant);
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()));
160 //Generate a signature for the source data
161 INFO_PRINTF1(_L("Signing..."));
162 TRAP_LOG(err,implsig->SignL(*sourceData, *signature));
164 TBool bResult = EFalse;
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()));
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));
176 // verifier agrees signature is valid
177 INFO_PRINTF1(_L("PASS: Verification of Generated Signature Successful"));
178 SetTestStepResult(EPass);
182 //verification failure, signature is invalid
183 ERR_PRINTF2(_L("FAIL: Verification Failure of Generated Signature - %d"), err);
184 SetTestStepResult(EFail);
187 CleanupStack::PopAndDestroy(sourceData);
188 CleanupStack::PopAndDestroy(signature);
189 CleanupStack::PopAndDestroy(implver);
190 CleanupStack::PopAndDestroy(implsig);
191 CleanupStack::PopAndDestroy(svParams);
193 CleanupStack::PopAndDestroy(keyPair);
194 CleanupStack::PopAndDestroy(keypairImpl);
195 CleanupStack::PopAndDestroy(keyParams);
198 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
201 return TestStepResult();
206 TVerdict CSignerVerifierStep::doTestStepPostambleL()
208 return TestStepResult();