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 "symmetriccipherincrementalencryptdecryptstep.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 |
CSymmetricCipherIncrementalEncryptDecryptStep::~CSymmetricCipherIncrementalEncryptDecryptStep()
|
sl@0
|
32 |
{
|
sl@0
|
33 |
}
|
sl@0
|
34 |
|
sl@0
|
35 |
|
sl@0
|
36 |
CSymmetricCipherIncrementalEncryptDecryptStep::CSymmetricCipherIncrementalEncryptDecryptStep(TInt aOffset) : iOffset(aOffset)
|
sl@0
|
37 |
{
|
sl@0
|
38 |
SetTestStepName(KSymmetricCipherIncrementalEncryptDecryptStep);
|
sl@0
|
39 |
}
|
sl@0
|
40 |
|
sl@0
|
41 |
|
sl@0
|
42 |
TVerdict CSymmetricCipherIncrementalEncryptDecryptStep::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 CSymmetricCipherIncrementalEncryptDecryptStep::doTestStepL()
|
sl@0
|
50 |
{
|
sl@0
|
51 |
INFO_PRINTF1(_L("*** Symmetric Cipher - Incremental Encrypt/Decrypt ***"));
|
sl@0
|
52 |
INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
|
sl@0
|
53 |
|
sl@0
|
54 |
if (TestStepResult()==EPass)
|
sl@0
|
55 |
{
|
sl@0
|
56 |
|
sl@0
|
57 |
//Assume faliure, unless all is successful
|
sl@0
|
58 |
SetTestStepResult(EFail);
|
sl@0
|
59 |
|
sl@0
|
60 |
TPtrC srcPath;
|
sl@0
|
61 |
if (!GetStringFromConfig(ConfigSection(),KConfigSourcePath, srcPath))
|
sl@0
|
62 |
{
|
sl@0
|
63 |
User::Leave(KErrNotFound);
|
sl@0
|
64 |
}
|
sl@0
|
65 |
|
sl@0
|
66 |
TVariantPtrC operationMode;
|
sl@0
|
67 |
|
sl@0
|
68 |
// Create a Symmetric Cipher with the values from the ini file
|
sl@0
|
69 |
CryptoSpi::CSymmetricCipher * impl = NULL;
|
sl@0
|
70 |
|
sl@0
|
71 |
CKey* key = NULL;
|
sl@0
|
72 |
SetupCipherL(ETrue, EFalse, operationMode, impl, key);
|
sl@0
|
73 |
|
sl@0
|
74 |
INFO_PRINTF1(_L("Plugin loaded."));
|
sl@0
|
75 |
|
sl@0
|
76 |
CleanupStack::PushL(key);
|
sl@0
|
77 |
CleanupStack::PushL(impl);
|
sl@0
|
78 |
|
sl@0
|
79 |
HBufC8* iv = NULL;
|
sl@0
|
80 |
TInt err(0);
|
sl@0
|
81 |
|
sl@0
|
82 |
TInt blockSize(0);
|
sl@0
|
83 |
if (TUid(operationMode) == KOperationModeCTRUid)
|
sl@0
|
84 |
{
|
sl@0
|
85 |
blockSize = CtrModeCalcBlockSizeL(*impl);
|
sl@0
|
86 |
}
|
sl@0
|
87 |
else
|
sl@0
|
88 |
{
|
sl@0
|
89 |
blockSize = impl->BlockSize();
|
sl@0
|
90 |
}
|
sl@0
|
91 |
|
sl@0
|
92 |
if((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
|
sl@0
|
93 |
{
|
sl@0
|
94 |
// blocksize is in bits so to allocate the correct number of bytes divide by 8
|
sl@0
|
95 |
// 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
|
96 |
// If this conditional block changes, take care to update the condition for deleting this allocated IV, near the end of this function.
|
sl@0
|
97 |
iv = HBufC8::NewLC(blockSize/8);
|
sl@0
|
98 |
|
sl@0
|
99 |
// blocksize is in bits so to allocate the correct number of 8 byte chunks divide by 64
|
sl@0
|
100 |
for(TInt i = 0 ; i <blockSize/64 ; i++)
|
sl@0
|
101 |
{
|
sl@0
|
102 |
iv->Des().Append(_L8("12345678"));
|
sl@0
|
103 |
}
|
sl@0
|
104 |
|
sl@0
|
105 |
TRAP_LOG(err,impl->SetIvL(iv->Des()));
|
sl@0
|
106 |
}
|
sl@0
|
107 |
|
sl@0
|
108 |
// convert to bytesize
|
sl@0
|
109 |
blockSize/=8;
|
sl@0
|
110 |
blockSize += iOffset;
|
sl@0
|
111 |
|
sl@0
|
112 |
//read from src file
|
sl@0
|
113 |
CFileReader* srcData = CFileReader::NewLC(srcPath,blockSize);
|
sl@0
|
114 |
|
sl@0
|
115 |
|
sl@0
|
116 |
// first step is to read from the src file one block
|
sl@0
|
117 |
// at a time, encrypt that block and then write
|
sl@0
|
118 |
// the encrypted block out to a temporary file.
|
sl@0
|
119 |
CFileWriter* encryptedDataWriter = CFileWriter::NewLC(TPtrC(KEncryptedFilePath));
|
sl@0
|
120 |
|
sl@0
|
121 |
TInt numBlocks = srcData->NumBlocks();
|
sl@0
|
122 |
|
sl@0
|
123 |
INFO_PRINTF1(_L("Encrypting Source Data..."));
|
sl@0
|
124 |
|
sl@0
|
125 |
for(TInt i = 1 ; i <= numBlocks ; i++)
|
sl@0
|
126 |
{
|
sl@0
|
127 |
TRAP_LOG(err,srcData->ReadBlockL());
|
sl@0
|
128 |
|
sl@0
|
129 |
//Create buffer for encrypted data
|
sl@0
|
130 |
TInt maxOutputLength = impl->MaxFinalOutputLength(TPtrC8(*srcData).Length());
|
sl@0
|
131 |
HBufC8* encrypted = HBufC8::NewLC(maxOutputLength);
|
sl@0
|
132 |
TPtr8 encryptedPtr = encrypted->Des();
|
sl@0
|
133 |
|
sl@0
|
134 |
if(i == numBlocks)
|
sl@0
|
135 |
{
|
sl@0
|
136 |
TRAP_LOG(err,impl->ProcessFinalL(*srcData, encryptedPtr));
|
sl@0
|
137 |
}
|
sl@0
|
138 |
else
|
sl@0
|
139 |
{
|
sl@0
|
140 |
TRAP_LOG(err,impl->ProcessL(*srcData, encryptedPtr));
|
sl@0
|
141 |
}
|
sl@0
|
142 |
|
sl@0
|
143 |
TRAP_LOG(err,encryptedDataWriter->WriteBlockL(encryptedPtr));
|
sl@0
|
144 |
|
sl@0
|
145 |
CleanupStack::PopAndDestroy(encrypted);
|
sl@0
|
146 |
}
|
sl@0
|
147 |
CleanupStack::PopAndDestroy(encryptedDataWriter);
|
sl@0
|
148 |
|
sl@0
|
149 |
if(err == KErrNone)
|
sl@0
|
150 |
{
|
sl@0
|
151 |
//Switch to decrypt
|
sl@0
|
152 |
TRAP_LOG(err,impl->SetCryptoModeL(KCryptoModeDecryptUid));
|
sl@0
|
153 |
|
sl@0
|
154 |
//If in CTR mode need to reset the keystream to the start of the sequence used for encryption.
|
sl@0
|
155 |
if(TUid(operationMode) == KOperationModeCTRUid)
|
sl@0
|
156 |
{
|
sl@0
|
157 |
impl->SetIvL(iv->Des());
|
sl@0
|
158 |
}
|
sl@0
|
159 |
|
sl@0
|
160 |
// the next step is to read the previously encrypted data
|
sl@0
|
161 |
// from the temporary file decrypting this one block
|
sl@0
|
162 |
// at a time and outputing this to a temporary file.
|
sl@0
|
163 |
CFileReader* encryptedDataReader = CFileReader::NewLC(TPtrC(KEncryptedFilePath),blockSize);
|
sl@0
|
164 |
CFileWriter* decryptedDataWriter = CFileWriter::NewLC(TPtrC(KDecryptedFilePath));
|
sl@0
|
165 |
|
sl@0
|
166 |
numBlocks = encryptedDataReader->NumBlocks();
|
sl@0
|
167 |
|
sl@0
|
168 |
INFO_PRINTF1(_L("Decrypting Data..."));
|
sl@0
|
169 |
|
sl@0
|
170 |
for(TInt i = 1 ; i <= numBlocks ; i++)
|
sl@0
|
171 |
{
|
sl@0
|
172 |
TRAP_LOG(err,encryptedDataReader->ReadBlockL());
|
sl@0
|
173 |
|
sl@0
|
174 |
//Create buffer for encrypted data
|
sl@0
|
175 |
TInt maxOutputLength = impl->MaxFinalOutputLength(TPtrC8(*encryptedDataReader).Length());
|
sl@0
|
176 |
HBufC8* decrypted = HBufC8::NewLC(maxOutputLength);
|
sl@0
|
177 |
TPtr8 decryptedPtr = decrypted->Des();
|
sl@0
|
178 |
|
sl@0
|
179 |
//Perform the decryption operation
|
sl@0
|
180 |
if(i == numBlocks)
|
sl@0
|
181 |
{
|
sl@0
|
182 |
TRAP_LOG(err,impl->ProcessFinalL(*encryptedDataReader, decryptedPtr));
|
sl@0
|
183 |
}
|
sl@0
|
184 |
else
|
sl@0
|
185 |
{
|
sl@0
|
186 |
TRAP_LOG(err,impl->ProcessL(*encryptedDataReader, decryptedPtr));
|
sl@0
|
187 |
}
|
sl@0
|
188 |
|
sl@0
|
189 |
TRAP_LOG(err,decryptedDataWriter->WriteBlockL(decryptedPtr));
|
sl@0
|
190 |
|
sl@0
|
191 |
CleanupStack::PopAndDestroy(decrypted);
|
sl@0
|
192 |
}
|
sl@0
|
193 |
|
sl@0
|
194 |
CleanupStack::PopAndDestroy(decryptedDataWriter);
|
sl@0
|
195 |
CleanupStack::PopAndDestroy(encryptedDataReader);
|
sl@0
|
196 |
}
|
sl@0
|
197 |
|
sl@0
|
198 |
CleanupStack::PopAndDestroy(srcData);
|
sl@0
|
199 |
if((TUid(operationMode) == KOperationModeCBCUid) || (TUid(operationMode) == KOperationModeCTRUid))
|
sl@0
|
200 |
{
|
sl@0
|
201 |
// Iv is left on the cleanupstack at creation.
|
sl@0
|
202 |
// If it becomes possible for operationMode to be modified during
|
sl@0
|
203 |
// the test this needs to be re-engineered.
|
sl@0
|
204 |
CleanupStack::PopAndDestroy(iv);
|
sl@0
|
205 |
}
|
sl@0
|
206 |
CleanupStack::PopAndDestroy(impl);
|
sl@0
|
207 |
CleanupStack::PopAndDestroy(key);
|
sl@0
|
208 |
|
sl@0
|
209 |
// compare the src with the file thats been
|
sl@0
|
210 |
// encrypted then decrypted
|
sl@0
|
211 |
if(!TFileCompare::CompareL(srcPath,TPtrC(KDecryptedFilePath)))
|
sl@0
|
212 |
{
|
sl@0
|
213 |
INFO_PRINTF1(_L("PASS : Source File and Decrypted Data Match"));
|
sl@0
|
214 |
SetTestStepResult(EPass);
|
sl@0
|
215 |
}
|
sl@0
|
216 |
else
|
sl@0
|
217 |
{
|
sl@0
|
218 |
INFO_PRINTF1(_L("FAIL : Source File and Decrypted Data Mismatch"));
|
sl@0
|
219 |
SetTestStepResult(EFail);
|
sl@0
|
220 |
}
|
sl@0
|
221 |
|
sl@0
|
222 |
RFs rFs;
|
sl@0
|
223 |
rFs.Connect();
|
sl@0
|
224 |
rFs.Delete( KDecryptedFilePath );
|
sl@0
|
225 |
rFs.Delete( KEncryptedFilePath );
|
sl@0
|
226 |
rFs.Close();
|
sl@0
|
227 |
}
|
sl@0
|
228 |
INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
|
sl@0
|
229 |
|
sl@0
|
230 |
return TestStepResult();
|
sl@0
|
231 |
}
|
sl@0
|
232 |
|
sl@0
|
233 |
|
sl@0
|
234 |
TVerdict CSymmetricCipherIncrementalEncryptDecryptStep::doTestStepPostambleL()
|
sl@0
|
235 |
{
|
sl@0
|
236 |
return TestStepResult();
|
sl@0
|
237 |
}
|
sl@0
|
238 |
|