1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/src/signerverifierstep.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,209 @@
1.4 +/*
1.5 +* Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* Example CTestStep derived implementation
1.19 +*
1.20 +*/
1.21 +
1.22 +
1.23 +/**
1.24 + @file
1.25 + @internalTechnology
1.26 +*/
1.27 +#include "signerverifierstep.h"
1.28 +
1.29 +#include <cryptospi/keypair.h>
1.30 +#include <cryptospi/cryptosignatureapi.h>
1.31 +#include <cryptospi/cryptokeypairgeneratorapi.h>
1.32 +
1.33 +using namespace CryptoSpi;
1.34 +
1.35 +
1.36 +CSignerVerifierStep::~CSignerVerifierStep()
1.37 + {
1.38 + }
1.39 +
1.40 +CSignerVerifierStep::CSignerVerifierStep()
1.41 + {
1.42 + SetTestStepName(KSignerVerifierStep);
1.43 + }
1.44 +
1.45 +TVerdict CSignerVerifierStep::doTestStepPreambleL()
1.46 + {
1.47 + SetTestStepResult(EPass);
1.48 + return TestStepResult();
1.49 + }
1.50 +
1.51 +
1.52 +TVerdict CSignerVerifierStep::doTestStepL()
1.53 + {
1.54 + INFO_PRINTF1(_L("*** Signer/Verifier - Signature and Verification ***"));
1.55 + INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
1.56 +
1.57 + if (TestStepResult()==EPass)
1.58 + {
1.59 + //Assume faliure, unless all is successful
1.60 + SetTestStepResult(EFail);
1.61 +
1.62 + TVariantPtrC testVariant;
1.63 + TVariantPtrC typeVariant;
1.64 + TVariantPtrC dataVariant;
1.65 +
1.66 + if( !GetStringFromConfig(ConfigSection(),KConfigSignVerifyType, typeVariant)
1.67 + || !GetStringFromConfig(ConfigSection(),KConfigSignVerifyData, dataVariant))
1.68 + {
1.69 + // Leave if there's any error.
1.70 + User::Leave(KErrNotFound);
1.71 + }
1.72 + else
1.73 + {
1.74 + TUid sigType = typeVariant;
1.75 +
1.76 + //Construct a Crypto Parameters object to store the necessary key pair generator parameters
1.77 + CCryptoParams* keyParams = CCryptoParams::NewL();
1.78 + CleanupStack::PushL(keyParams);
1.79 +
1.80 + //Create Key Pair Generator Object
1.81 + CKeyPairGenerator * keypairImpl = NULL;
1.82 +
1.83 + //Define error code as first TRAP is beyond scope
1.84 + TInt err = 0;
1.85 +
1.86 + if (sigType == KRsaSignerUid)
1.87 + {
1.88 + INFO_PRINTF1(_L("Creating Key Pair Generator (RSA)..."));
1.89 +
1.90 + keyParams->AddL(KKeyExponent, KRsaKeyParameterEUid);
1.91 + keyParams->AddL(KRsaPrivateKeyStandard, KRsaKeyTypeUid);
1.92 +
1.93 + //Construct an 'RSA' Key Pair Generator Implementation Object
1.94 + TRAP_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(keypairImpl,
1.95 + KRSAKeyPairGeneratorUid,
1.96 + keyParams));
1.97 + }
1.98 + else
1.99 + {
1.100 + INFO_PRINTF1(_L("Creating Key Pair Generator (DSA)..."));
1.101 +
1.102 + //Construct a 'DSA' Key Pair Generator Implementation Object
1.103 + TRAP_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(keypairImpl,
1.104 + KDSAKeyPairGeneratorUid,
1.105 + NULL));
1.106 + }
1.107 +
1.108 + CleanupStack::PushL(keypairImpl);
1.109 +
1.110 + // Generate a Key Pair
1.111 + INFO_PRINTF1(_L("Generating Key Pair..."));
1.112 +
1.113 + CKeyPair* keyPair = NULL;
1.114 +
1.115 + TRAP_LOG(err,keypairImpl->GenerateKeyPairL(1024,
1.116 + *keyParams,
1.117 + keyPair));
1.118 +
1.119 + CleanupStack::PushL(keyPair);
1.120 +
1.121 +
1.122 + // Create a Signer Object
1.123 + INFO_PRINTF1(_L("Creating Signer Object..."));
1.124 + CCryptoParams* svParams = CCryptoParams::NewL();
1.125 + CleanupStack::PushL(svParams);
1.126 +
1.127 + CSigner * implsig = NULL;
1.128 + TRAP_LOG(err,CSignatureFactory::CreateSignerL(implsig,
1.129 + sigType,
1.130 + keyPair->PublicKey(),
1.131 + KPaddingModePkcs1_v1_5_SignatureUid,
1.132 + svParams));
1.133 +
1.134 + CleanupStack::PushL(implsig);
1.135 +
1.136 +
1.137 + // Create a Verifier
1.138 + INFO_PRINTF1(_L("Creating Verifier Object..."));
1.139 +
1.140 + CVerifier * implver = NULL;
1.141 + TRAP_LOG(err,CSignatureFactory::CreateVerifierL(implver,
1.142 + sigType == KRsaSignerUid ? KRsaVerifierUid : KDsaVerifierUid,
1.143 + keyPair->PrivateKey(),
1.144 + KPaddingModePkcs1_v1_5_SignatureUid,
1.145 + svParams));
1.146 +
1.147 + CleanupStack::PushL(implver);
1.148 +
1.149 + //Define a new signature object
1.150 + CCryptoParams *signature = CCryptoParams::NewL();
1.151 + CleanupStack::PushL(signature);
1.152 +
1.153 + //Convert the source data to an 8 Bit Descriptor
1.154 + HBufC8* sourceData = HBufC8::NewLC(dataVariant.Length());
1.155 + TPtr8 sourceDataPtr = sourceData->Des();
1.156 +
1.157 + sourceDataPtr.Copy(dataVariant);
1.158 +
1.159 + //Set the signer object to use the 'Private' Key of the key pair
1.160 + INFO_PRINTF1(_L("Setting Signer Private Key..."));
1.161 + TRAP_LOG(err,implsig->SetKeyL(keyPair->PrivateKey()));
1.162 +
1.163 + //Generate a signature for the source data
1.164 + INFO_PRINTF1(_L("Signing..."));
1.165 + TRAP_LOG(err,implsig->SignL(*sourceData, *signature));
1.166 +
1.167 + TBool bResult = EFalse;
1.168 +
1.169 + //Set the verifier object to use the 'Public' Key of the key pair
1.170 + INFO_PRINTF1(_L("Setting Verifier Public Key..."));
1.171 + TRAP_LOG(err,implver->SetKeyL(keyPair->PublicKey()));
1.172 +
1.173 + //Verify that the generated signature is valid for the given data
1.174 + INFO_PRINTF1(_L("Verifying..."));
1.175 + TRAP_LOG(err,implver->VerifyL(*sourceData, *signature, bResult));
1.176 +
1.177 + if (bResult)
1.178 + {
1.179 + // verifier agrees signature is valid
1.180 + INFO_PRINTF1(_L("PASS: Verification of Generated Signature Successful"));
1.181 + SetTestStepResult(EPass);
1.182 + }
1.183 + else
1.184 + {
1.185 + //verification failure, signature is invalid
1.186 + ERR_PRINTF2(_L("FAIL: Verification Failure of Generated Signature - %d"), err);
1.187 + SetTestStepResult(EFail);
1.188 + }
1.189 +
1.190 + CleanupStack::PopAndDestroy(sourceData);
1.191 + CleanupStack::PopAndDestroy(signature);
1.192 + CleanupStack::PopAndDestroy(implver);
1.193 + CleanupStack::PopAndDestroy(implsig);
1.194 + CleanupStack::PopAndDestroy(svParams);
1.195 +
1.196 + CleanupStack::PopAndDestroy(keyPair);
1.197 + CleanupStack::PopAndDestroy(keypairImpl);
1.198 + CleanupStack::PopAndDestroy(keyParams);
1.199 + }
1.200 +
1.201 + INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
1.202 +
1.203 + }
1.204 + return TestStepResult();
1.205 + }
1.206 +
1.207 +
1.208 +
1.209 +TVerdict CSignerVerifierStep::doTestStepPostambleL()
1.210 + {
1.211 + return TestStepResult();
1.212 + }