os/security/crypto/weakcryptospi/test/tcryptospi/src/asymmetriccipherencrypteddatacheckstep.cpp
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 "asymmetriccipherencrypteddatacheckstep.h"
26 #include <cryptospi/cryptoasymmetriccipherapi.h>
27 #include <cryptospi/cryptokeypairgeneratorapi.h>
28 #include <cryptospi/keypair.h>
29 #include "filereader.h"
31 using namespace CryptoSpi;
33 CASymmetricCipherEncryptedDataCheckStep::~CASymmetricCipherEncryptedDataCheckStep()
38 CASymmetricCipherEncryptedDataCheckStep::CASymmetricCipherEncryptedDataCheckStep()
40 SetTestStepName(KASymmetricCipherEncryptedDataCheckStep);
44 TVerdict CASymmetricCipherEncryptedDataCheckStep::doTestStepPreambleL()
46 SetTestStepResult(EPass);
47 return TestStepResult();
51 TVerdict CASymmetricCipherEncryptedDataCheckStep::doTestStepL()
54 INFO_PRINTF1(_L("*** Asymmetric Cipher - Encrypted Data Check ***"));
55 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
57 if (TestStepResult()==EPass)
60 //Assume faliure, unless all is successful
61 SetTestStepResult(EFail);
63 TVariantPtrC algorithm;
65 TVariantPtrC encrypted;
66 TVariantPtrC paddingMode;
69 if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid, algorithm) ||
70 !GetStringFromConfig(ConfigSection(),KConfigPaddingMode, paddingMode) ||
71 !GetStringFromConfig(ConfigSection(),KConfigSourcePath, srcPath) ||
72 !GetStringFromConfig(ConfigSection(),KConfigExEncryptedData, encrypted))
74 User::Leave(KErrNotFound);
78 //assume failure, unless all is successful
79 SetTestStepResult(EFail);
81 CCryptoParams* keyParams = CCryptoParams::NewLC();
83 //****************************************************
84 //Create Key Pair and Key Pair Generator Objects
85 CKeyPair* keyPair = NULL;
86 CKeyPairGenerator * keypairImpl = NULL;
88 // create an RSA key pair
89 INFO_PRINTF1(_L("Generating RSA keys"));
91 keyParams->AddL(KKeyExponent, KRsaKeyParameterEUid);
92 keyParams->AddL(KRsaPrivateKeyStandard, KRsaKeyTypeUid);
94 // create a key pair generator implementation interface
95 TRAPD_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(keypairImpl,
96 KRSAKeyPairGeneratorUid,
99 CleanupStack::PushL(keypairImpl);
102 TRAP_LOG(err,keypairImpl->GenerateKeyPairL(1024, *keyParams, keyPair));
104 CleanupStack::PushL(keyPair);
106 //*****************************************************
108 INFO_PRINTF1(_L("Creating Asymmetric Cipher Object..."));
110 // Create a Symmetric Cipher
111 CryptoSpi::CAsymmetricCipher * impl = NULL;
113 TRAP(err,CAsymmetricCipherFactory::CreateAsymmetricCipherL(impl,
115 keyPair->PublicKey(),
116 KCryptoModeEncryptUid,
120 if(impl && (err == KErrNone))
122 CleanupStack::PushL(impl);
124 INFO_PRINTF1(_L("*** Asymmetric Cipher Implementation Created... ***"));
127 CFileReader* srcData = CFileReader::NewLC(srcPath);
129 //Create buffer for encrypted data
130 TInt maxOutputLength = impl->GetMaximumOutputLengthL();
131 HBufC8* encrypted = HBufC8::NewLC(maxOutputLength);
132 TPtr8 encryptedPtr = encrypted->Des();
134 INFO_PRINTF1(_L("Encrypting Source Data..."));
136 //Perform the encryption operation
137 TRAP_LOG(err,impl->ProcessL(TPtrC8(*srcData), encryptedPtr));
141 INFO_PRINTF1(_L("*** DATA ENCRYPTED ***"));
143 //Check that the source data matches the data thats
144 //been encrypted then decrypted
145 if( !encryptedPtr.Compare(TPtrC8(*srcData)))
147 INFO_PRINTF1(_L("PASS : Encrypted Data Matches Expected"));
148 SetTestStepResult(EPass);
152 INFO_PRINTF1(_L("FAIL : Encrypted Data and Expected Mismatch"));
153 SetTestStepResult(EFail);
157 CleanupStack::PopAndDestroy(encrypted);
158 CleanupStack::PopAndDestroy(srcData);
159 CleanupStack::PopAndDestroy(impl);
163 ERR_PRINTF2(_L("*** FAIL: Failed to Create Asymmetric Object - %d ***"), err);
167 CleanupStack::PopAndDestroy(keyPair);
168 CleanupStack::PopAndDestroy(keypairImpl);
169 CleanupStack::PopAndDestroy(keyParams);
174 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
176 return TestStepResult();
180 TVerdict CASymmetricCipherEncryptedDataCheckStep::doTestStepPostambleL()
182 return TestStepResult();