|
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 "asymmetriccipherencryptdecryptstep.h" |
|
25 |
|
26 #include "cryptoasymmetriccipherapi.h" |
|
27 #include "cryptokeypairgeneratorapi.h" |
|
28 #include "keypair.h" |
|
29 #include "filereader.h" |
|
30 |
|
31 using namespace CryptoSpi; |
|
32 |
|
33 CASymmetricCipherEncryptDecryptStep::~CASymmetricCipherEncryptDecryptStep() |
|
34 { |
|
35 } |
|
36 |
|
37 |
|
38 CASymmetricCipherEncryptDecryptStep::CASymmetricCipherEncryptDecryptStep() |
|
39 { |
|
40 SetTestStepName(KASymmetricCipherEncryptDecryptStep); |
|
41 } |
|
42 |
|
43 |
|
44 TVerdict CASymmetricCipherEncryptDecryptStep::doTestStepPreambleL() |
|
45 { |
|
46 SetTestStepResult(EPass); |
|
47 return TestStepResult(); |
|
48 } |
|
49 |
|
50 |
|
51 TVerdict CASymmetricCipherEncryptDecryptStep::doTestStepL() |
|
52 { |
|
53 INFO_PRINTF1(_L("*** Asymmetric Cipher - Encrypt/Decrypt ***")); |
|
54 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); |
|
55 |
|
56 if (TestStepResult()==EPass) |
|
57 { |
|
58 |
|
59 //Assume faliure, unless all is successful |
|
60 SetTestStepResult(EFail); |
|
61 |
|
62 TPtrC srcPath; |
|
63 TVariantPtrC paddingMode; |
|
64 |
|
65 if(!GetStringFromConfig(ConfigSection(),KConfigSourcePath, srcPath) || |
|
66 !GetStringFromConfig(ConfigSection(),KConfigPaddingMode, paddingMode )) |
|
67 { |
|
68 User::Leave(KErrNotFound); |
|
69 } |
|
70 else |
|
71 { |
|
72 |
|
73 CCryptoParams* keyParams = CCryptoParams::NewLC(); |
|
74 |
|
75 //**************************************************** |
|
76 //Create Key Pair and Key Pair Generator Objects |
|
77 CKeyPair* keyPair = NULL; |
|
78 CKeyPairGenerator * keypairImpl = NULL; |
|
79 |
|
80 // create an RSA key pair |
|
81 INFO_PRINTF1(_L("Generating RSA keys")); |
|
82 |
|
83 keyParams->AddL(KKeyExponent, KRsaKeyParameterEUid); |
|
84 keyParams->AddL(KRsaPrivateKeyStandard, KRsaKeyTypeUid); |
|
85 |
|
86 // create a key pair generator implementation interface |
|
87 TRAPD_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL( |
|
88 keypairImpl, |
|
89 KRSAKeyPairGeneratorUid, |
|
90 keyParams)); |
|
91 |
|
92 CleanupStack::PushL(keypairImpl); |
|
93 |
|
94 // Create a Key Pair |
|
95 TRAP_LOG(err,keypairImpl->GenerateKeyPairL(1024, *keyParams, keyPair)); |
|
96 |
|
97 CleanupStack::PushL(keyPair); |
|
98 |
|
99 //***************************************************** |
|
100 |
|
101 INFO_PRINTF1(_L("Creating Asymmetric Cipher Object...")); |
|
102 |
|
103 // Create an Asymmetric Cipher with the values fromt he ini |
|
104 CryptoSpi::CAsymmetricCipher * impl = NULL; |
|
105 |
|
106 TRAP(err,CAsymmetricCipherFactory::CreateAsymmetricCipherL |
|
107 ( |
|
108 impl, |
|
109 KRsaCipherUid, |
|
110 keyPair->PublicKey(), |
|
111 KCryptoModeEncryptUid, |
|
112 paddingMode, |
|
113 NULL)); |
|
114 |
|
115 if(impl && (err == KErrNone)) |
|
116 { |
|
117 INFO_PRINTF1(_L("*** Asymmetric Cipher Implementation Created... ***")); |
|
118 |
|
119 CleanupStack::PushL(impl); |
|
120 |
|
121 //read from src file |
|
122 CFileReader* srcData = CFileReader::NewLC(srcPath); |
|
123 |
|
124 //Create buffer for encrypted data |
|
125 TInt maxOutputLength = impl->GetMaximumOutputLengthL(); |
|
126 HBufC8* encrypted = HBufC8::NewLC(maxOutputLength); |
|
127 TPtr8 encryptedPtr = encrypted->Des(); |
|
128 |
|
129 INFO_PRINTF1(_L("Encrypting Source Data...")); |
|
130 |
|
131 //Perform the encryption operation |
|
132 TRAP_LOG(err, impl->ProcessL(static_cast<TPtrC8>(*srcData), encryptedPtr)); |
|
133 |
|
134 if(err == KErrNone) |
|
135 { |
|
136 |
|
137 if(encryptedPtr.Compare(TPtrC8(*srcData)) != 0) |
|
138 { |
|
139 |
|
140 INFO_PRINTF1(_L("*** DATA ENCRYPTED ***")); |
|
141 |
|
142 //Switch to decrypt |
|
143 TRAP_LOG(err,impl->SetCryptoModeL(KCryptoModeDecryptUid)); |
|
144 |
|
145 TRAP_LOG(err,impl->SetKeyL(keyPair->PrivateKey())); |
|
146 |
|
147 //Create a buffer for the decrypted data |
|
148 maxOutputLength = encryptedPtr.Length(); |
|
149 |
|
150 TInt bufSize = impl->GetMaximumOutputLengthL(); |
|
151 |
|
152 HBufC8* output = HBufC8::NewLC(bufSize); |
|
153 TPtr8 outputPtr = output->Des(); |
|
154 |
|
155 INFO_PRINTF1(_L("Decrypting Data...")); |
|
156 |
|
157 //Perform the decryption operation |
|
158 TRAP_LOG(err, impl->ProcessL(encryptedPtr, outputPtr)); |
|
159 |
|
160 if(err == KErrNone) |
|
161 { |
|
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)) |
|
166 { |
|
167 INFO_PRINTF1(_L("PASS : Decrypted Data matches expected Source")); |
|
168 SetTestStepResult(EPass); |
|
169 } |
|
170 else |
|
171 { |
|
172 INFO_PRINTF1(_L("FAIL : Decrypted Data and expected Mismatch")); |
|
173 SetTestStepResult(EFail); |
|
174 } |
|
175 } |
|
176 |
|
177 CleanupStack::PopAndDestroy(output); |
|
178 } |
|
179 else |
|
180 { |
|
181 INFO_PRINTF1(_L("FAIL : Encrpyted Data and Source Data length and content is the same")); |
|
182 SetTestStepResult(EFail); |
|
183 } |
|
184 } |
|
185 |
|
186 |
|
187 CleanupStack::PopAndDestroy(encrypted); |
|
188 CleanupStack::PopAndDestroy(srcData); |
|
189 CleanupStack::PopAndDestroy(impl); |
|
190 } |
|
191 else |
|
192 { |
|
193 ERR_PRINTF2(_L("*** FAIL: Failed to Create Asymmetric Object - %d ***"), err); |
|
194 User::Leave(err); |
|
195 } |
|
196 |
|
197 CleanupStack::PopAndDestroy(keyPair); |
|
198 CleanupStack::PopAndDestroy(keypairImpl); |
|
199 CleanupStack::PopAndDestroy(keyParams); |
|
200 } |
|
201 } |
|
202 |
|
203 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); |
|
204 |
|
205 return TestStepResult(); |
|
206 } |
|
207 |
|
208 |
|
209 TVerdict CASymmetricCipherEncryptDecryptStep::doTestStepPostambleL() |
|
210 { |
|
211 return TestStepResult(); |
|
212 } |