sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
* Example CTestStep derived implementation
|
sl@0
|
16 |
*
|
sl@0
|
17 |
*/
|
sl@0
|
18 |
|
sl@0
|
19 |
|
sl@0
|
20 |
/**
|
sl@0
|
21 |
@file
|
sl@0
|
22 |
@internalTechnology
|
sl@0
|
23 |
*/
|
sl@0
|
24 |
#include "symmetriccipherobjectreusestep.h"
|
sl@0
|
25 |
|
sl@0
|
26 |
#include "filewriter.h"
|
sl@0
|
27 |
#include "filecompare.h"
|
sl@0
|
28 |
|
sl@0
|
29 |
using namespace CryptoSpi;
|
sl@0
|
30 |
|
sl@0
|
31 |
|
sl@0
|
32 |
CSymmetricCipherObjectReuseStep::CSymmetricCipherObjectReuseStep(TInt aOffset) : iOffset(aOffset)
|
sl@0
|
33 |
{
|
sl@0
|
34 |
}
|
sl@0
|
35 |
|
sl@0
|
36 |
|
sl@0
|
37 |
CSymmetricCipherObjectReuseStep::~CSymmetricCipherObjectReuseStep()
|
sl@0
|
38 |
{
|
sl@0
|
39 |
}
|
sl@0
|
40 |
|
sl@0
|
41 |
|
sl@0
|
42 |
TVerdict CSymmetricCipherObjectReuseStep::doTestStepPreambleL()
|
sl@0
|
43 |
{
|
sl@0
|
44 |
SetTestStepResult(EPass);
|
sl@0
|
45 |
return TestStepResult();
|
sl@0
|
46 |
}
|
sl@0
|
47 |
|
sl@0
|
48 |
|
sl@0
|
49 |
TVerdict CSymmetricCipherObjectReuseStep::doTestStepL()
|
sl@0
|
50 |
{
|
sl@0
|
51 |
INFO_PRINTF1(_L("*** Symmetric Cipher - Object Reuse ***"));
|
sl@0
|
52 |
INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
|
sl@0
|
53 |
if (TestStepResult()==EPass)
|
sl@0
|
54 |
{
|
sl@0
|
55 |
|
sl@0
|
56 |
//Assume failure, unless all is successful
|
sl@0
|
57 |
SetTestStepResult(EFail);
|
sl@0
|
58 |
|
sl@0
|
59 |
TPtrC keyPath;
|
sl@0
|
60 |
TPtrC srcPath;
|
sl@0
|
61 |
TVariantPtrC algorithm;
|
sl@0
|
62 |
TVariantPtrC operationMode;
|
sl@0
|
63 |
TVariantPtrC paddingMode;
|
sl@0
|
64 |
|
sl@0
|
65 |
if( !GetStringFromConfig(ConfigSection(),KConfigEncryptKey, keyPath) ||
|
sl@0
|
66 |
!GetStringFromConfig(ConfigSection(),KConfigSourcePath, srcPath) ||
|
sl@0
|
67 |
!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid, algorithm) ||
|
sl@0
|
68 |
!GetStringFromConfig(ConfigSection(),KConfigOperationMode, operationMode) ||
|
sl@0
|
69 |
!GetStringFromConfig(ConfigSection(),KConfigPaddingMode, paddingMode ))
|
sl@0
|
70 |
{
|
sl@0
|
71 |
User::Leave(KErrNotFound);
|
sl@0
|
72 |
}
|
sl@0
|
73 |
else
|
sl@0
|
74 |
{
|
sl@0
|
75 |
|
sl@0
|
76 |
//Create an instance of TKeyProperty
|
sl@0
|
77 |
TKeyProperty keyProperty;
|
sl@0
|
78 |
|
sl@0
|
79 |
//Load the key data using the
|
sl@0
|
80 |
CFileReader* keyData = CFileReader::NewLC(keyPath);
|
sl@0
|
81 |
|
sl@0
|
82 |
CCryptoParams* params = CCryptoParams::NewLC();
|
sl@0
|
83 |
params->AddL( *keyData, KSymmetricKeyParameterUid);
|
sl@0
|
84 |
|
sl@0
|
85 |
CKey* key=CKey::NewL(keyProperty, *params);
|
sl@0
|
86 |
CleanupStack::PushL(key);
|
sl@0
|
87 |
|
sl@0
|
88 |
CCryptoParams* xparams = NULL;
|
sl@0
|
89 |
|
sl@0
|
90 |
if (TUid(algorithm) == KArc4Uid)
|
sl@0
|
91 |
{
|
sl@0
|
92 |
//Set the RC4 DiscardBytes to 0
|
sl@0
|
93 |
xparams = CCryptoParams::NewL();
|
sl@0
|
94 |
xparams->AddL(NULL, KARC4DiscardBytes);
|
sl@0
|
95 |
CleanupStack::PushL(xparams);
|
sl@0
|
96 |
}
|
sl@0
|
97 |
|
sl@0
|
98 |
if (TUid(algorithm) == KRc2Uid)
|
sl@0
|
99 |
{
|
sl@0
|
100 |
TInt keylen = TPtrC8(*keyData).Length() * 8;
|
sl@0
|
101 |
xparams = CCryptoParams::NewLC();
|
sl@0
|
102 |
|
sl@0
|
103 |
//Set the RC2 EffectiveKeyLen according to the input key size
|
sl@0
|
104 |
xparams->AddL( keylen, KRC2EffectiveKeyLenBits);
|
sl@0
|
105 |
}
|
sl@0
|
106 |
|
sl@0
|
107 |
INFO_PRINTF1(_L("Creating Symmetric Cipher Object..."));
|
sl@0
|
108 |
|
sl@0
|
109 |
// Create a Symmetric Cipher with the values from the ini config file
|
sl@0
|
110 |
CryptoSpi::CSymmetricCipher * impl = NULL;
|
sl@0
|
111 |
TRAPD(err,CSymmetricCipherFactory::CreateSymmetricCipherL
|
sl@0
|
112 |
(
|
sl@0
|
113 |
impl,
|
sl@0
|
114 |
algorithm,
|
sl@0
|
115 |
*key,
|
sl@0
|
116 |
KCryptoModeEncryptUid,
|
sl@0
|
117 |
operationMode,
|
sl@0
|
118 |
paddingMode,
|
sl@0
|
119 |
xparams));
|
sl@0
|
120 |
|
sl@0
|
121 |
if(impl && (err == KErrNone))
|
sl@0
|
122 |
{
|
sl@0
|
123 |
CleanupStack::PushL(impl);
|
sl@0
|
124 |
|
sl@0
|
125 |
const TInt KObjectReuseItterations = 5; // 5 iterations should be enough to check the object reuse feature
|
sl@0
|
126 |
// the no of iteration is reduced, to reduce the time taken for execution
|
sl@0
|
127 |
|
sl@0
|
128 |
//Boolean to denote the state
|
sl@0
|
129 |
TBool testPass = ETrue;
|
sl@0
|
130 |
|
sl@0
|
131 |
/*************** Encrypt/Decrypt Reuse Loop ****************/
|
sl@0
|
132 |
for(TInt index = 0; index < KObjectReuseItterations; index++)
|
sl@0
|
133 |
{
|
sl@0
|
134 |
INFO_PRINTF3(_L("i=%d : START HEAP CELLS: %d"),index, User::CountAllocCells());
|
sl@0
|
135 |
|
sl@0
|
136 |
//-----RESET IMPLEMENTATION OBJECT (NORMAL LOGGING)----------
|
sl@0
|
137 |
|
sl@0
|
138 |
impl->Reset();
|
sl@0
|
139 |
|
sl@0
|
140 |
TRAP(err,impl->SetKeyL(*key));
|
sl@0
|
141 |
|
sl@0
|
142 |
if(err != KErrNone)
|
sl@0
|
143 |
{
|
sl@0
|
144 |
ERR_PRINTF3(_L("*** ERROR:%d - SetKeyL() i=%d ***"),err,index);
|
sl@0
|
145 |
User::Leave(err);
|
sl@0
|
146 |
}
|
sl@0
|
147 |
|
sl@0
|
148 |
TRAP(err,impl->SetCryptoModeL(KCryptoModeEncryptUid));
|
sl@0
|
149 |
|
sl@0
|
150 |
if(err != KErrNone)
|
sl@0
|
151 |
{
|
sl@0
|
152 |
ERR_PRINTF3(_L("*** ERROR:%d - SetCryptoModeL() i=%d ***"),err,index);
|
sl@0
|
153 |
User::Leave(err);
|
sl@0
|
154 |
}
|
sl@0
|
155 |
|
sl@0
|
156 |
if(TUid(algorithm) != KArc4Uid)
|
sl@0
|
157 |
{
|
sl@0
|
158 |
|
sl@0
|
159 |
impl->SetOperationModeL(operationMode);
|
sl@0
|
160 |
|
sl@0
|
161 |
if(err != KErrNone)
|
sl@0
|
162 |
{
|
sl@0
|
163 |
ERR_PRINTF3(_L("*** ERROR:%d - SetOperationModeL() i=%d ***"),err,index);
|
sl@0
|
164 |
User::Leave(err);
|
sl@0
|
165 |
}
|
sl@0
|
166 |
|
sl@0
|
167 |
TRAP(err,impl->SetPaddingModeL(paddingMode));
|
sl@0
|
168 |
|
sl@0
|
169 |
if(err != KErrNone)
|
sl@0
|
170 |
{
|
sl@0
|
171 |
ERR_PRINTF3(_L("*** ERROR:%d - SetPaddingModeL() i=%d ***"),err,index);
|
sl@0
|
172 |
User::Leave(err);
|
sl@0
|
173 |
}
|
sl@0
|
174 |
|
sl@0
|
175 |
}
|
sl@0
|
176 |
|
sl@0
|
177 |
//------------------------------------------------------
|
sl@0
|
178 |
|
sl@0
|
179 |
//find out the block size for this algorithm
|
sl@0
|
180 |
TInt blockSize(0);
|
sl@0
|
181 |
|
sl@0
|
182 |
if (TUid(operationMode) == KOperationModeCTRUid)
|
sl@0
|
183 |
{
|
sl@0
|
184 |
blockSize = CtrModeCalcBlockSizeL(*impl);
|
sl@0
|
185 |
}
|
sl@0
|
186 |
else
|
sl@0
|
187 |
{
|
sl@0
|
188 |
blockSize = impl->BlockSize();
|
sl@0
|
189 |
}
|
sl@0
|
190 |
|
sl@0
|
191 |
HBufC8* iv = NULL;
|
sl@0
|
192 |
|
sl@0
|
193 |
if ((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
|
sl@0
|
194 |
{
|
sl@0
|
195 |
// block size is in bits so to allocate the correct number of bytes divide by 8
|
sl@0
|
196 |
// iv is left on the cleanup stack for the duration of the test and deleted in a conditional at the end of the outer block.
|
sl@0
|
197 |
// If this conditional block changes, take care to update the condition for deleting this allocated IV, near the end of this function.
|
sl@0
|
198 |
iv = HBufC8::NewLC(blockSize/8);
|
sl@0
|
199 |
|
sl@0
|
200 |
// blocksize is in bits so to allocate the correct number of 8 byte chunks divide by 64
|
sl@0
|
201 |
for(TInt i = 0 ; i <blockSize/64 ; i++)
|
sl@0
|
202 |
{
|
sl@0
|
203 |
iv->Des().Append(_L8("12345678"));
|
sl@0
|
204 |
}
|
sl@0
|
205 |
|
sl@0
|
206 |
TRAP_LOG(err,impl->SetIvL(iv->Des()));
|
sl@0
|
207 |
}
|
sl@0
|
208 |
|
sl@0
|
209 |
// convert to bytesize
|
sl@0
|
210 |
blockSize/=8;
|
sl@0
|
211 |
blockSize += iOffset;
|
sl@0
|
212 |
|
sl@0
|
213 |
//read from src file
|
sl@0
|
214 |
CFileReader* srcData = CFileReader::NewLC(srcPath,blockSize);
|
sl@0
|
215 |
|
sl@0
|
216 |
// first step is to read from the src file one block
|
sl@0
|
217 |
// at a time, encrypt that block and then write
|
sl@0
|
218 |
// the encrypted block out to a temporary file.
|
sl@0
|
219 |
CFileWriter* encryptedDataWriter = CFileWriter::NewLC(TPtrC(KTempEncryptedFilePath));
|
sl@0
|
220 |
|
sl@0
|
221 |
TInt numBlocks = srcData->NumBlocks();
|
sl@0
|
222 |
|
sl@0
|
223 |
INFO_PRINTF1(_L("Starting Incremental Encryption..."));
|
sl@0
|
224 |
|
sl@0
|
225 |
for(TInt i = 1 ; i <= numBlocks ; i++)
|
sl@0
|
226 |
{
|
sl@0
|
227 |
TRAP_LOG(err,srcData->ReadBlockL());
|
sl@0
|
228 |
|
sl@0
|
229 |
//Create buffer for encrypted data
|
sl@0
|
230 |
TInt maxOutputLength = impl->MaxFinalOutputLength(TPtrC8(*srcData).Length());
|
sl@0
|
231 |
HBufC8* encrypted = HBufC8::NewLC(maxOutputLength);
|
sl@0
|
232 |
TPtr8 encryptedPtr = encrypted->Des();
|
sl@0
|
233 |
|
sl@0
|
234 |
if(i == numBlocks)
|
sl@0
|
235 |
{
|
sl@0
|
236 |
TRAP(err,impl->ProcessFinalL(*srcData, encryptedPtr));
|
sl@0
|
237 |
|
sl@0
|
238 |
if(err != KErrNone)
|
sl@0
|
239 |
{
|
sl@0
|
240 |
ERR_PRINTF3(_L("*** ERROR:%d - ProcessFinalL() Block=%d ***"),err,i);
|
sl@0
|
241 |
User::Leave(err);
|
sl@0
|
242 |
}
|
sl@0
|
243 |
}
|
sl@0
|
244 |
else
|
sl@0
|
245 |
{
|
sl@0
|
246 |
TRAP(err,impl->ProcessL(*srcData, encryptedPtr));
|
sl@0
|
247 |
|
sl@0
|
248 |
if(err != KErrNone)
|
sl@0
|
249 |
{
|
sl@0
|
250 |
ERR_PRINTF3(_L("*** ERROR:%d - ProcessL() Block=%d ***"),err,i);
|
sl@0
|
251 |
User::Leave(err);
|
sl@0
|
252 |
}
|
sl@0
|
253 |
}
|
sl@0
|
254 |
|
sl@0
|
255 |
TRAP_LOG(err,encryptedDataWriter->WriteBlockL(encryptedPtr));
|
sl@0
|
256 |
|
sl@0
|
257 |
CleanupStack::PopAndDestroy(encrypted);
|
sl@0
|
258 |
}
|
sl@0
|
259 |
|
sl@0
|
260 |
CleanupStack::PopAndDestroy(encryptedDataWriter);
|
sl@0
|
261 |
|
sl@0
|
262 |
//Switch to Decrypt Crypto Mode
|
sl@0
|
263 |
TRAP(err,impl->SetCryptoModeL(KCryptoModeDecryptUid));
|
sl@0
|
264 |
|
sl@0
|
265 |
if(err != KErrNone)
|
sl@0
|
266 |
{
|
sl@0
|
267 |
ERR_PRINTF3(_L("*** ERROR:%d - SetCryptoModeL() i=%d ***"),err,index);
|
sl@0
|
268 |
User::Leave(err);
|
sl@0
|
269 |
}
|
sl@0
|
270 |
|
sl@0
|
271 |
//If in CTR mode need to reset the keystream to the start of the sequence used for encryption.
|
sl@0
|
272 |
if(TUid(operationMode) == KOperationModeCTRUid)
|
sl@0
|
273 |
{
|
sl@0
|
274 |
impl->SetIvL(iv->Des());
|
sl@0
|
275 |
}
|
sl@0
|
276 |
|
sl@0
|
277 |
|
sl@0
|
278 |
// the next step is to read the previously encrypted data
|
sl@0
|
279 |
// from the temporary file decrypting this one block
|
sl@0
|
280 |
// at a time and outputing this to a temporary file.
|
sl@0
|
281 |
CFileReader* encryptedDataReader = CFileReader::NewLC(TPtrC(KTempEncryptedFilePath),blockSize);
|
sl@0
|
282 |
CFileWriter* decryptedDataWriter = CFileWriter::NewLC(TPtrC(KTempDecryptedFilePath));
|
sl@0
|
283 |
|
sl@0
|
284 |
numBlocks = encryptedDataReader->NumBlocks();
|
sl@0
|
285 |
|
sl@0
|
286 |
INFO_PRINTF1(_L("Starting Incremental Decryption..."));
|
sl@0
|
287 |
|
sl@0
|
288 |
for(TInt i = 1 ; i <= numBlocks ; i++)
|
sl@0
|
289 |
{
|
sl@0
|
290 |
encryptedDataReader->ReadBlockL();
|
sl@0
|
291 |
//Create buffer for encrypted data
|
sl@0
|
292 |
TInt maxOutputLength = impl->MaxFinalOutputLength(TPtrC8(*encryptedDataReader).Length());
|
sl@0
|
293 |
HBufC8* decrypted = HBufC8::NewLC(maxOutputLength);
|
sl@0
|
294 |
TPtr8 decryptedPtr = decrypted->Des();
|
sl@0
|
295 |
|
sl@0
|
296 |
//Perform the decryption operation
|
sl@0
|
297 |
if(i == numBlocks)
|
sl@0
|
298 |
{
|
sl@0
|
299 |
TRAP(err,impl->ProcessFinalL(*encryptedDataReader, decryptedPtr));
|
sl@0
|
300 |
|
sl@0
|
301 |
if(err != KErrNone)
|
sl@0
|
302 |
{
|
sl@0
|
303 |
ERR_PRINTF3(_L("*** ERROR:%d - ProcessFinalL() Block=%d ***"),err,i);
|
sl@0
|
304 |
User::Leave(err);
|
sl@0
|
305 |
}
|
sl@0
|
306 |
}
|
sl@0
|
307 |
else
|
sl@0
|
308 |
{
|
sl@0
|
309 |
TRAP(err,impl->ProcessL(*encryptedDataReader, decryptedPtr));
|
sl@0
|
310 |
|
sl@0
|
311 |
if(err != KErrNone)
|
sl@0
|
312 |
{
|
sl@0
|
313 |
ERR_PRINTF3(_L("*** ERROR:%d - ProcessL() Block=%d ***"),err,i);
|
sl@0
|
314 |
User::Leave(err);
|
sl@0
|
315 |
}
|
sl@0
|
316 |
}
|
sl@0
|
317 |
|
sl@0
|
318 |
TRAP_LOG(err,decryptedDataWriter->WriteBlockL(decryptedPtr));
|
sl@0
|
319 |
|
sl@0
|
320 |
CleanupStack::PopAndDestroy(decrypted);
|
sl@0
|
321 |
}
|
sl@0
|
322 |
|
sl@0
|
323 |
CleanupStack::PopAndDestroy(decryptedDataWriter);
|
sl@0
|
324 |
CleanupStack::PopAndDestroy(encryptedDataReader);
|
sl@0
|
325 |
CleanupStack::PopAndDestroy(srcData);
|
sl@0
|
326 |
|
sl@0
|
327 |
if((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
|
sl@0
|
328 |
{
|
sl@0
|
329 |
// Iv is left on the cleanupstack at creation.
|
sl@0
|
330 |
// If it becomes possible for operationMode to be modified during
|
sl@0
|
331 |
// the test this needs to be re-engineered.
|
sl@0
|
332 |
CleanupStack::PopAndDestroy(iv);
|
sl@0
|
333 |
}
|
sl@0
|
334 |
|
sl@0
|
335 |
|
sl@0
|
336 |
// compare the src with the file thats been
|
sl@0
|
337 |
// encrypted then decrypted
|
sl@0
|
338 |
// Note: Returning 0 means that the files match
|
sl@0
|
339 |
if(!TFileCompare::CompareL(srcPath,TPtrC(KTempDecryptedFilePath)))
|
sl@0
|
340 |
{
|
sl@0
|
341 |
INFO_PRINTF2(_L("*** PASS = Source File and Decrypted Data Match - i=%d ***"),index);
|
sl@0
|
342 |
}
|
sl@0
|
343 |
else
|
sl@0
|
344 |
{
|
sl@0
|
345 |
testPass = EFalse;
|
sl@0
|
346 |
ERR_PRINTF2(_L("*** ERROR: Source File and Decrypted Data Mismatch - i=%d ***"),index);
|
sl@0
|
347 |
}
|
sl@0
|
348 |
|
sl@0
|
349 |
RFs rFs;
|
sl@0
|
350 |
rFs.Connect();
|
sl@0
|
351 |
rFs.Delete( KTempDecryptedFilePath );
|
sl@0
|
352 |
rFs.Delete( KTempEncryptedFilePath );
|
sl@0
|
353 |
rFs.Close();
|
sl@0
|
354 |
|
sl@0
|
355 |
INFO_PRINTF3(_L("*** i=%d : END HEAP CELLS: %d ***"),index, User::CountAllocCells());
|
sl@0
|
356 |
}
|
sl@0
|
357 |
|
sl@0
|
358 |
/*************** END OF LOOP ****************/
|
sl@0
|
359 |
|
sl@0
|
360 |
CleanupStack::PopAndDestroy(impl);
|
sl@0
|
361 |
|
sl@0
|
362 |
if(testPass == EFalse)
|
sl@0
|
363 |
{
|
sl@0
|
364 |
ERR_PRINTF1(_L("*** TEST FAIL : Symmetric Cipher - Object Reuse ***"));
|
sl@0
|
365 |
}
|
sl@0
|
366 |
else
|
sl@0
|
367 |
{
|
sl@0
|
368 |
INFO_PRINTF1(_L("*** TEST PASS : Symmetric Cipher - Object Reuse ***"));
|
sl@0
|
369 |
SetTestStepResult(EPass);
|
sl@0
|
370 |
}
|
sl@0
|
371 |
|
sl@0
|
372 |
}
|
sl@0
|
373 |
else
|
sl@0
|
374 |
{
|
sl@0
|
375 |
ERR_PRINTF2(_L("*** FAIL: Failed to Create Symmetric Object - %d ***"), err);
|
sl@0
|
376 |
User::Leave(err);
|
sl@0
|
377 |
}
|
sl@0
|
378 |
|
sl@0
|
379 |
if (TUid(algorithm) == KArc4Uid || TUid(algorithm) == KRc2Uid)
|
sl@0
|
380 |
{
|
sl@0
|
381 |
CleanupStack::PopAndDestroy(xparams);
|
sl@0
|
382 |
}
|
sl@0
|
383 |
|
sl@0
|
384 |
CleanupStack::PopAndDestroy(key);
|
sl@0
|
385 |
CleanupStack::PopAndDestroy(params);
|
sl@0
|
386 |
CleanupStack::PopAndDestroy(keyData);
|
sl@0
|
387 |
}
|
sl@0
|
388 |
}
|
sl@0
|
389 |
INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
|
sl@0
|
390 |
|
sl@0
|
391 |
return TestStepResult();
|
sl@0
|
392 |
|
sl@0
|
393 |
}
|
sl@0
|
394 |
|
sl@0
|
395 |
|
sl@0
|
396 |
TVerdict CSymmetricCipherObjectReuseStep::doTestStepPostambleL()
|
sl@0
|
397 |
{
|
sl@0
|
398 |
return TestStepResult();
|
sl@0
|
399 |
}
|