sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 1998-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 |
*
|
sl@0
|
16 |
*/
|
sl@0
|
17 |
|
sl@0
|
18 |
|
sl@0
|
19 |
#include "tactionincremental.h"
|
sl@0
|
20 |
#include "symmetric.h"
|
sl@0
|
21 |
#include "des.h"
|
sl@0
|
22 |
#include "securityerr.h"
|
sl@0
|
23 |
|
sl@0
|
24 |
CTestAction* CActionIncremental::NewL(RFs& aFs,
|
sl@0
|
25 |
CConsoleBase& aConsole,
|
sl@0
|
26 |
Output& aOut,
|
sl@0
|
27 |
const TTestActionSpec& aTestActionSpec)
|
sl@0
|
28 |
{
|
sl@0
|
29 |
CTestAction* self = CActionIncremental::NewLC(aFs, aConsole,
|
sl@0
|
30 |
aOut, aTestActionSpec);
|
sl@0
|
31 |
CleanupStack::Pop();
|
sl@0
|
32 |
return self;
|
sl@0
|
33 |
}
|
sl@0
|
34 |
|
sl@0
|
35 |
CTestAction* CActionIncremental::NewLC(RFs& aFs,
|
sl@0
|
36 |
CConsoleBase& aConsole,
|
sl@0
|
37 |
Output& aOut,
|
sl@0
|
38 |
const TTestActionSpec& aTestActionSpec)
|
sl@0
|
39 |
{
|
sl@0
|
40 |
CActionIncremental* self = new(ELeave) CActionIncremental(aFs, aConsole, aOut);
|
sl@0
|
41 |
CleanupStack::PushL(self);
|
sl@0
|
42 |
self->ConstructL(aTestActionSpec);
|
sl@0
|
43 |
return self;
|
sl@0
|
44 |
}
|
sl@0
|
45 |
|
sl@0
|
46 |
CActionIncremental::~CActionIncremental()
|
sl@0
|
47 |
{
|
sl@0
|
48 |
}
|
sl@0
|
49 |
|
sl@0
|
50 |
CActionIncremental::CActionIncremental(RFs& aFs,
|
sl@0
|
51 |
CConsoleBase& aConsole,
|
sl@0
|
52 |
Output& aOut)
|
sl@0
|
53 |
: CCryptoTestAction(aFs, aConsole, aOut)
|
sl@0
|
54 |
{}
|
sl@0
|
55 |
|
sl@0
|
56 |
void CActionIncremental::DoPerformPostrequisiteL()
|
sl@0
|
57 |
{
|
sl@0
|
58 |
if (iEncryptor)
|
sl@0
|
59 |
{
|
sl@0
|
60 |
iEncryptor->Reset();
|
sl@0
|
61 |
delete iEncryptor;
|
sl@0
|
62 |
}
|
sl@0
|
63 |
|
sl@0
|
64 |
if (iDecryptor)
|
sl@0
|
65 |
{
|
sl@0
|
66 |
iDecryptor->Reset();
|
sl@0
|
67 |
delete iDecryptor;
|
sl@0
|
68 |
}
|
sl@0
|
69 |
}
|
sl@0
|
70 |
|
sl@0
|
71 |
void CActionIncremental::DoPerformPrerequisiteL()
|
sl@0
|
72 |
{
|
sl@0
|
73 |
iResult = ETrue;
|
sl@0
|
74 |
TInt err = KErrNone;
|
sl@0
|
75 |
TInt pos = 0;
|
sl@0
|
76 |
TPtrC8 incremental = Input::ParseElement(*iBody, KIncrementalStart, KIncrementalEnd, pos, err);
|
sl@0
|
77 |
|
sl@0
|
78 |
DoInputParseL(incremental);
|
sl@0
|
79 |
|
sl@0
|
80 |
CBlockTransformation* encryptor = NULL;
|
sl@0
|
81 |
CBlockTransformation* decryptor = NULL;
|
sl@0
|
82 |
|
sl@0
|
83 |
iEncryptor =0;
|
sl@0
|
84 |
iDecryptor =0;
|
sl@0
|
85 |
|
sl@0
|
86 |
switch (iCipherType)
|
sl@0
|
87 |
{
|
sl@0
|
88 |
case (EDESECB):
|
sl@0
|
89 |
{
|
sl@0
|
90 |
//If the test is weak key test
|
sl@0
|
91 |
if(iExpectedWeakResult == KErrWeakKey)
|
sl@0
|
92 |
{
|
sl@0
|
93 |
//we expect to leave with KErrWeakKey reason
|
sl@0
|
94 |
if(CDES::IsWeakKey(iKey->Des()))
|
sl@0
|
95 |
{
|
sl@0
|
96 |
User::Leave(KErrWeakKey);
|
sl@0
|
97 |
}
|
sl@0
|
98 |
else //test is unsuccessful, leave with a different reason
|
sl@0
|
99 |
{
|
sl@0
|
100 |
User::Leave(KErrGeneral);
|
sl@0
|
101 |
}
|
sl@0
|
102 |
}
|
sl@0
|
103 |
encryptor = CDESEncryptor::NewLC(iKey->Des());
|
sl@0
|
104 |
decryptor = CDESDecryptor::NewL(iKey->Des());
|
sl@0
|
105 |
CleanupStack::Pop(encryptor);
|
sl@0
|
106 |
}
|
sl@0
|
107 |
break;
|
sl@0
|
108 |
case (EDESCBC):
|
sl@0
|
109 |
{
|
sl@0
|
110 |
CBlockTransformation* desEncryptor = NULL;
|
sl@0
|
111 |
CBlockTransformation* desDecryptor = NULL;
|
sl@0
|
112 |
|
sl@0
|
113 |
if(iExpectedWeakResult == KErrWeakKey)
|
sl@0
|
114 |
{
|
sl@0
|
115 |
if(CDES::IsWeakKey(iKey->Des()))
|
sl@0
|
116 |
{
|
sl@0
|
117 |
User::Leave(KErrWeakKey);
|
sl@0
|
118 |
}
|
sl@0
|
119 |
else
|
sl@0
|
120 |
{
|
sl@0
|
121 |
User::Leave(KErrGeneral);
|
sl@0
|
122 |
}
|
sl@0
|
123 |
}
|
sl@0
|
124 |
|
sl@0
|
125 |
desEncryptor = CDESEncryptor::NewLC(iKey->Des());
|
sl@0
|
126 |
desDecryptor = CDESDecryptor::NewLC(iKey->Des());
|
sl@0
|
127 |
|
sl@0
|
128 |
encryptor = CModeCBCEncryptor::NewL(desEncryptor, iIV->Des());
|
sl@0
|
129 |
CleanupStack::PushL(encryptor);
|
sl@0
|
130 |
decryptor = CModeCBCDecryptor::NewL(desDecryptor, iIV->Des());
|
sl@0
|
131 |
CleanupStack::Pop(3);
|
sl@0
|
132 |
}
|
sl@0
|
133 |
break;
|
sl@0
|
134 |
case (E3DESECB):
|
sl@0
|
135 |
{
|
sl@0
|
136 |
encryptor = C3DESEncryptor::NewLC(iKey->Des());
|
sl@0
|
137 |
decryptor = C3DESDecryptor::NewL(iKey->Des());
|
sl@0
|
138 |
CleanupStack::Pop(encryptor);
|
sl@0
|
139 |
}
|
sl@0
|
140 |
break;
|
sl@0
|
141 |
case (E3DESCBC):
|
sl@0
|
142 |
{
|
sl@0
|
143 |
CBlockTransformation* the3DESencryptor = NULL;
|
sl@0
|
144 |
CBlockTransformation* the3DESdecryptor = NULL;
|
sl@0
|
145 |
|
sl@0
|
146 |
the3DESencryptor = C3DESEncryptor::NewLC(iKey->Des());
|
sl@0
|
147 |
the3DESdecryptor = C3DESDecryptor::NewLC(iKey->Des());
|
sl@0
|
148 |
|
sl@0
|
149 |
encryptor = CModeCBCEncryptor::NewL(the3DESencryptor, iIV->Des());
|
sl@0
|
150 |
CleanupStack::PushL(encryptor);
|
sl@0
|
151 |
decryptor = CModeCBCDecryptor::NewL(the3DESdecryptor, iIV->Des());
|
sl@0
|
152 |
CleanupStack::Pop(3);
|
sl@0
|
153 |
}
|
sl@0
|
154 |
break;
|
sl@0
|
155 |
case (EAESECB):
|
sl@0
|
156 |
{
|
sl@0
|
157 |
encryptor = CAESEncryptor::NewLC(iKey->Des());
|
sl@0
|
158 |
decryptor = CAESDecryptor::NewL(iKey->Des());
|
sl@0
|
159 |
CleanupStack::Pop(encryptor);
|
sl@0
|
160 |
}
|
sl@0
|
161 |
break;
|
sl@0
|
162 |
case (ERC2ECB):
|
sl@0
|
163 |
{
|
sl@0
|
164 |
encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
|
sl@0
|
165 |
decryptor = CRC2Decryptor::NewL(iKey->Des(), iEffectiveKeyLen);
|
sl@0
|
166 |
CleanupStack::Pop(encryptor);
|
sl@0
|
167 |
}
|
sl@0
|
168 |
break;
|
sl@0
|
169 |
case (ERC2CBC):
|
sl@0
|
170 |
{
|
sl@0
|
171 |
encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
|
sl@0
|
172 |
decryptor = CRC2Decryptor::NewL(iKey->Des(), iEffectiveKeyLen);
|
sl@0
|
173 |
CleanupStack::Pop(encryptor);
|
sl@0
|
174 |
}
|
sl@0
|
175 |
break;
|
sl@0
|
176 |
case (ERC4):
|
sl@0
|
177 |
{
|
sl@0
|
178 |
iEncryptor = CARC4::NewL(*iKey, 0);
|
sl@0
|
179 |
iDecryptor = CARC4::NewL(*iKey, 0);
|
sl@0
|
180 |
}
|
sl@0
|
181 |
break;
|
sl@0
|
182 |
case (ECipherNull):
|
sl@0
|
183 |
{
|
sl@0
|
184 |
iEncryptor = CNullCipher::NewL();
|
sl@0
|
185 |
iDecryptor = CNullCipher::NewL();
|
sl@0
|
186 |
}
|
sl@0
|
187 |
break;
|
sl@0
|
188 |
default:
|
sl@0
|
189 |
ASSERT(0);
|
sl@0
|
190 |
User::Leave(KErrNotSupported);
|
sl@0
|
191 |
}
|
sl@0
|
192 |
|
sl@0
|
193 |
CleanupStack::PushL(encryptor);
|
sl@0
|
194 |
CleanupStack::PushL(decryptor);
|
sl@0
|
195 |
if(!iEncryptor && !iDecryptor)
|
sl@0
|
196 |
{
|
sl@0
|
197 |
CPaddingSSLv3* dPadding = CPaddingSSLv3::NewLC(decryptor->BlockSize());
|
sl@0
|
198 |
CPaddingSSLv3* ePadding = CPaddingSSLv3::NewLC(encryptor->BlockSize());
|
sl@0
|
199 |
iEncryptor = CBufferedEncryptor::NewL(encryptor, ePadding);
|
sl@0
|
200 |
CleanupStack::Pop(ePadding);
|
sl@0
|
201 |
iDecryptor = CBufferedDecryptor::NewL(decryptor, dPadding);
|
sl@0
|
202 |
CleanupStack::Pop(dPadding);
|
sl@0
|
203 |
|
sl@0
|
204 |
TInt desEBlockSize = encryptor->BlockSize();
|
sl@0
|
205 |
TInt desDBlockSize = decryptor->BlockSize();
|
sl@0
|
206 |
TInt bufEBlockSize = iEncryptor->BlockSize();
|
sl@0
|
207 |
TInt bufDBlockSize = iDecryptor->BlockSize();
|
sl@0
|
208 |
if ((desEBlockSize/desDBlockSize) != (bufEBlockSize/bufDBlockSize))
|
sl@0
|
209 |
{
|
sl@0
|
210 |
iResult = EFalse;
|
sl@0
|
211 |
}
|
sl@0
|
212 |
|
sl@0
|
213 |
TInt desEKeySize = encryptor->KeySize();
|
sl@0
|
214 |
TInt desDKeySize = decryptor->KeySize();
|
sl@0
|
215 |
if (desEKeySize != desDKeySize)
|
sl@0
|
216 |
{
|
sl@0
|
217 |
iResult = EFalse;
|
sl@0
|
218 |
}
|
sl@0
|
219 |
|
sl@0
|
220 |
encryptor->Reset();
|
sl@0
|
221 |
decryptor->Reset();
|
sl@0
|
222 |
}
|
sl@0
|
223 |
|
sl@0
|
224 |
TInt encryptorKeySize = iEncryptor->KeySize();
|
sl@0
|
225 |
TInt decryptorKeySize = iDecryptor->KeySize();
|
sl@0
|
226 |
if (encryptorKeySize != decryptorKeySize)
|
sl@0
|
227 |
{
|
sl@0
|
228 |
iResult = EFalse;
|
sl@0
|
229 |
}
|
sl@0
|
230 |
|
sl@0
|
231 |
CleanupStack::Pop(2, encryptor);
|
sl@0
|
232 |
|
sl@0
|
233 |
iEResult = HBufC8::NewMaxL(iEncryptor->MaxFinalOutputLength(iInput->Length()));
|
sl@0
|
234 |
iDResult = HBufC8::NewMaxL(iDecryptor->MaxFinalOutputLength(iEResult->Size()));
|
sl@0
|
235 |
}
|
sl@0
|
236 |
|
sl@0
|
237 |
void CActionIncremental::DoPerformActionL()
|
sl@0
|
238 |
{
|
sl@0
|
239 |
TRAPD(res, DoDoPerformActionL())
|
sl@0
|
240 |
if(res == KErrNoMemory)
|
sl@0
|
241 |
{
|
sl@0
|
242 |
iEncryptor->Reset();
|
sl@0
|
243 |
iDecryptor->Reset();
|
sl@0
|
244 |
}
|
sl@0
|
245 |
}
|
sl@0
|
246 |
|
sl@0
|
247 |
void CActionIncremental::DoDoPerformActionL()
|
sl@0
|
248 |
{
|
sl@0
|
249 |
__UHEAP_MARK;
|
sl@0
|
250 |
TPtr8 eResultActual = iEResult->Des();
|
sl@0
|
251 |
eResultActual.FillZ(eResultActual.MaxLength());
|
sl@0
|
252 |
eResultActual.SetLength(0);
|
sl@0
|
253 |
|
sl@0
|
254 |
TPtr8 dResultActual = iDResult->Des();
|
sl@0
|
255 |
dResultActual.FillZ(dResultActual.MaxLength());
|
sl@0
|
256 |
dResultActual.SetLength(0);
|
sl@0
|
257 |
|
sl@0
|
258 |
TInt len = iInput->Length();
|
sl@0
|
259 |
for(TInt i=1; i<len; i++)
|
sl@0
|
260 |
{
|
sl@0
|
261 |
TInt j = 0;
|
sl@0
|
262 |
for(; j+i<len; j+=i)
|
sl@0
|
263 |
{// encryption in blocks of size i
|
sl@0
|
264 |
iEncryptor->Process(iInput->Mid(j,i), eResultActual);
|
sl@0
|
265 |
}
|
sl@0
|
266 |
|
sl@0
|
267 |
iEncryptor->ProcessFinalL(iInput->Mid(j), eResultActual);
|
sl@0
|
268 |
|
sl@0
|
269 |
j=0;
|
sl@0
|
270 |
for(; (j+(len-i))<len; j+=(len-i))
|
sl@0
|
271 |
{// Decryption in blocks of size (len - i)
|
sl@0
|
272 |
iDecryptor->Process(eResultActual.Mid(j, len-i), dResultActual);
|
sl@0
|
273 |
}
|
sl@0
|
274 |
|
sl@0
|
275 |
iDecryptor->ProcessFinalL(eResultActual.Mid(j), dResultActual);
|
sl@0
|
276 |
|
sl@0
|
277 |
if(dResultActual != *iInput)
|
sl@0
|
278 |
{
|
sl@0
|
279 |
iResult = EFalse;
|
sl@0
|
280 |
}
|
sl@0
|
281 |
|
sl@0
|
282 |
eResultActual.FillZ(eResultActual.MaxLength());
|
sl@0
|
283 |
dResultActual.FillZ(dResultActual.MaxLength());
|
sl@0
|
284 |
eResultActual.SetLength(0);
|
sl@0
|
285 |
dResultActual.SetLength(0);
|
sl@0
|
286 |
}
|
sl@0
|
287 |
__UHEAP_MARKEND;
|
sl@0
|
288 |
|
sl@0
|
289 |
}
|
sl@0
|
290 |
|