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