os/security/crypto/weakcryptospi/test/tcryptospi/src/asymmetriccipherencryptdecryptstep.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 "asymmetriccipherencryptdecryptstep.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 CASymmetricCipherEncryptDecryptStep::~CASymmetricCipherEncryptDecryptStep()
38 CASymmetricCipherEncryptDecryptStep::CASymmetricCipherEncryptDecryptStep()
40 SetTestStepName(KASymmetricCipherEncryptDecryptStep);
44 TVerdict CASymmetricCipherEncryptDecryptStep::doTestStepPreambleL()
46 SetTestStepResult(EPass);
47 return TestStepResult();
51 TVerdict CASymmetricCipherEncryptDecryptStep::doTestStepL()
53 INFO_PRINTF1(_L("*** Asymmetric Cipher - Encrypt/Decrypt ***"));
54 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
56 if (TestStepResult()==EPass)
59 //Assume faliure, unless all is successful
60 SetTestStepResult(EFail);
63 TVariantPtrC paddingMode;
65 if(!GetStringFromConfig(ConfigSection(),KConfigSourcePath, srcPath) ||
66 !GetStringFromConfig(ConfigSection(),KConfigPaddingMode, paddingMode ))
68 User::Leave(KErrNotFound);
73 CCryptoParams* keyParams = CCryptoParams::NewLC();
75 //****************************************************
76 //Create Key Pair and Key Pair Generator Objects
77 CKeyPair* keyPair = NULL;
78 CKeyPairGenerator * keypairImpl = NULL;
80 // create an RSA key pair
81 INFO_PRINTF1(_L("Generating RSA keys"));
83 keyParams->AddL(KKeyExponent, KRsaKeyParameterEUid);
84 keyParams->AddL(KRsaPrivateKeyStandard, KRsaKeyTypeUid);
86 // create a key pair generator implementation interface
87 TRAPD_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(
89 KRSAKeyPairGeneratorUid,
92 CleanupStack::PushL(keypairImpl);
95 TRAP_LOG(err,keypairImpl->GenerateKeyPairL(1024, *keyParams, keyPair));
97 CleanupStack::PushL(keyPair);
99 //*****************************************************
101 INFO_PRINTF1(_L("Creating Asymmetric Cipher Object..."));
103 // Create an Asymmetric Cipher with the values fromt he ini
104 CryptoSpi::CAsymmetricCipher * impl = NULL;
106 TRAP(err,CAsymmetricCipherFactory::CreateAsymmetricCipherL
110 keyPair->PublicKey(),
111 KCryptoModeEncryptUid,
115 if(impl && (err == KErrNone))
117 INFO_PRINTF1(_L("*** Asymmetric Cipher Implementation Created... ***"));
119 CleanupStack::PushL(impl);
122 CFileReader* srcData = CFileReader::NewLC(srcPath);
124 //Create buffer for encrypted data
125 TInt maxOutputLength = impl->GetMaximumOutputLengthL();
126 HBufC8* encrypted = HBufC8::NewLC(maxOutputLength);
127 TPtr8 encryptedPtr = encrypted->Des();
129 INFO_PRINTF1(_L("Encrypting Source Data..."));
131 //Perform the encryption operation
132 TRAP_LOG(err, impl->ProcessL(static_cast<TPtrC8>(*srcData), encryptedPtr));
137 if(encryptedPtr.Compare(TPtrC8(*srcData)) != 0)
140 INFO_PRINTF1(_L("*** DATA ENCRYPTED ***"));
143 TRAP_LOG(err,impl->SetCryptoModeL(KCryptoModeDecryptUid));
145 TRAP_LOG(err,impl->SetKeyL(keyPair->PrivateKey()));
147 //Create a buffer for the decrypted data
148 maxOutputLength = encryptedPtr.Length();
150 TInt bufSize = impl->GetMaximumOutputLengthL();
152 HBufC8* output = HBufC8::NewLC(bufSize);
153 TPtr8 outputPtr = output->Des();
155 INFO_PRINTF1(_L("Decrypting Data..."));
157 //Perform the decryption operation
158 TRAP_LOG(err, impl->ProcessL(encryptedPtr, outputPtr));
162 INFO_PRINTF1(_L("*** DATA DECRYPTED ***"));
163 //Check that the source data matches the data thats
164 //been encrypted then decrypted
165 if(!outputPtr.Compare(*srcData))
167 INFO_PRINTF1(_L("PASS : Decrypted Data matches expected Source"));
168 SetTestStepResult(EPass);
172 INFO_PRINTF1(_L("FAIL : Decrypted Data and expected Mismatch"));
173 SetTestStepResult(EFail);
177 CleanupStack::PopAndDestroy(output);
181 INFO_PRINTF1(_L("FAIL : Encrpyted Data and Source Data length and content is the same"));
182 SetTestStepResult(EFail);
187 CleanupStack::PopAndDestroy(encrypted);
188 CleanupStack::PopAndDestroy(srcData);
189 CleanupStack::PopAndDestroy(impl);
193 ERR_PRINTF2(_L("*** FAIL: Failed to Create Asymmetric Object - %d ***"), err);
197 CleanupStack::PopAndDestroy(keyPair);
198 CleanupStack::PopAndDestroy(keypairImpl);
199 CleanupStack::PopAndDestroy(keyParams);
203 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
205 return TestStepResult();
209 TVerdict CASymmetricCipherEncryptDecryptStep::doTestStepPostambleL()
211 return TestStepResult();