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 "tactionvector.h"
|
sl@0
|
20 |
#include "symmetric.h"
|
sl@0
|
21 |
|
sl@0
|
22 |
CTestAction* CActionVector::NewL(RFs& aFs,
|
sl@0
|
23 |
CConsoleBase& aConsole,
|
sl@0
|
24 |
Output& aOut,
|
sl@0
|
25 |
const TTestActionSpec& aTestActionSpec)
|
sl@0
|
26 |
{
|
sl@0
|
27 |
CTestAction* self = CActionVector::NewLC(aFs, aConsole,
|
sl@0
|
28 |
aOut, aTestActionSpec);
|
sl@0
|
29 |
CleanupStack::Pop();
|
sl@0
|
30 |
return self;
|
sl@0
|
31 |
}
|
sl@0
|
32 |
|
sl@0
|
33 |
CTestAction* CActionVector::NewLC(RFs& aFs,
|
sl@0
|
34 |
CConsoleBase& aConsole,
|
sl@0
|
35 |
Output& aOut,
|
sl@0
|
36 |
const TTestActionSpec& aTestActionSpec)
|
sl@0
|
37 |
{
|
sl@0
|
38 |
CActionVector* self = new(ELeave) CActionVector(aFs, aConsole, aOut);
|
sl@0
|
39 |
CleanupStack::PushL(self);
|
sl@0
|
40 |
self->ConstructL(aTestActionSpec);
|
sl@0
|
41 |
return self;
|
sl@0
|
42 |
}
|
sl@0
|
43 |
|
sl@0
|
44 |
CActionVector::~CActionVector()
|
sl@0
|
45 |
{
|
sl@0
|
46 |
delete iEncryptor;
|
sl@0
|
47 |
delete iDecryptor;
|
sl@0
|
48 |
}
|
sl@0
|
49 |
|
sl@0
|
50 |
CActionVector::CActionVector(RFs& aFs,
|
sl@0
|
51 |
CConsoleBase& aConsole,
|
sl@0
|
52 |
Output& aOut)
|
sl@0
|
53 |
|
sl@0
|
54 |
: CCryptoTestAction(aFs, aConsole, aOut)
|
sl@0
|
55 |
{}
|
sl@0
|
56 |
|
sl@0
|
57 |
|
sl@0
|
58 |
void CActionVector::DoPerformPrerequisiteL()
|
sl@0
|
59 |
{
|
sl@0
|
60 |
TInt err = KErrNone;
|
sl@0
|
61 |
TInt pos = 0;
|
sl@0
|
62 |
TPtrC8 vector = Input::ParseElement(*iBody, KVectorStart, KVectorEnd, pos, err);
|
sl@0
|
63 |
|
sl@0
|
64 |
DoInputParseL(vector);
|
sl@0
|
65 |
|
sl@0
|
66 |
CBlockTransformation* encryptor = NULL;
|
sl@0
|
67 |
CBlockTransformation* decryptor = NULL;
|
sl@0
|
68 |
iEncryptor = 0;
|
sl@0
|
69 |
iDecryptor = 0;
|
sl@0
|
70 |
|
sl@0
|
71 |
switch (iCipherType)
|
sl@0
|
72 |
{
|
sl@0
|
73 |
case (EDESECB):
|
sl@0
|
74 |
{
|
sl@0
|
75 |
encryptor = CDESEncryptor::NewLC(iKey->Des());
|
sl@0
|
76 |
decryptor = CDESDecryptor::NewL(iKey->Des());
|
sl@0
|
77 |
CleanupStack::Pop(encryptor);
|
sl@0
|
78 |
}
|
sl@0
|
79 |
break;
|
sl@0
|
80 |
case(EDESCBC):
|
sl@0
|
81 |
{
|
sl@0
|
82 |
CBlockTransformation* desEncryptor = NULL;
|
sl@0
|
83 |
CBlockTransformation* desDecryptor = NULL;
|
sl@0
|
84 |
|
sl@0
|
85 |
desEncryptor = CDESEncryptor::NewLC(iKey->Des());
|
sl@0
|
86 |
desDecryptor = CDESDecryptor::NewLC(iKey->Des());
|
sl@0
|
87 |
|
sl@0
|
88 |
encryptor = CModeCBCEncryptor::NewL(desEncryptor, iIV->Des());
|
sl@0
|
89 |
CleanupStack::PushL(encryptor);
|
sl@0
|
90 |
decryptor = CModeCBCDecryptor::NewL(desDecryptor, iIV->Des());
|
sl@0
|
91 |
|
sl@0
|
92 |
CleanupStack::Pop(3, desEncryptor);
|
sl@0
|
93 |
}
|
sl@0
|
94 |
break;
|
sl@0
|
95 |
case (E3DESECB):
|
sl@0
|
96 |
{
|
sl@0
|
97 |
encryptor = C3DESEncryptor::NewLC(iKey->Des());
|
sl@0
|
98 |
decryptor = C3DESDecryptor::NewL(iKey->Des());
|
sl@0
|
99 |
CleanupStack::Pop(encryptor);
|
sl@0
|
100 |
}
|
sl@0
|
101 |
break;
|
sl@0
|
102 |
case (E3DESCBC):
|
sl@0
|
103 |
{
|
sl@0
|
104 |
CBlockTransformation* the3DESencryptor = NULL;
|
sl@0
|
105 |
CBlockTransformation* the3DESdecryptor = NULL;
|
sl@0
|
106 |
|
sl@0
|
107 |
the3DESencryptor = C3DESEncryptor::NewLC(iKey->Des());
|
sl@0
|
108 |
the3DESdecryptor = C3DESDecryptor::NewLC(iKey->Des());
|
sl@0
|
109 |
|
sl@0
|
110 |
encryptor = CModeCBCEncryptor::NewL(the3DESencryptor, iIV->Des());
|
sl@0
|
111 |
CleanupStack::PushL(encryptor);
|
sl@0
|
112 |
decryptor = CModeCBCDecryptor::NewL(the3DESdecryptor, iIV->Des());
|
sl@0
|
113 |
|
sl@0
|
114 |
CleanupStack::Pop(3, the3DESencryptor);
|
sl@0
|
115 |
}
|
sl@0
|
116 |
break;
|
sl@0
|
117 |
case (EAESECB):
|
sl@0
|
118 |
{
|
sl@0
|
119 |
encryptor = CAESEncryptor::NewLC(iKey->Des());
|
sl@0
|
120 |
decryptor = CAESDecryptor::NewL(iKey->Des());
|
sl@0
|
121 |
|
sl@0
|
122 |
CleanupStack::Pop(encryptor);
|
sl@0
|
123 |
}
|
sl@0
|
124 |
break;
|
sl@0
|
125 |
case (ERC2ECB):
|
sl@0
|
126 |
{
|
sl@0
|
127 |
encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
|
sl@0
|
128 |
decryptor = CRC2Decryptor::NewL(iKey->Des(), iEffectiveKeyLen);
|
sl@0
|
129 |
CleanupStack::Pop(encryptor);
|
sl@0
|
130 |
}
|
sl@0
|
131 |
break;
|
sl@0
|
132 |
case (ERC2CBC):
|
sl@0
|
133 |
{
|
sl@0
|
134 |
CBlockTransformation* theRC2encryptor = NULL;
|
sl@0
|
135 |
CBlockTransformation* theRC2decryptor = NULL;
|
sl@0
|
136 |
|
sl@0
|
137 |
theRC2encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
|
sl@0
|
138 |
theRC2decryptor = CRC2Decryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
|
sl@0
|
139 |
|
sl@0
|
140 |
encryptor = CModeCBCEncryptor::NewL(theRC2encryptor, iIV->Des());
|
sl@0
|
141 |
CleanupStack::PushL(encryptor);
|
sl@0
|
142 |
decryptor = CModeCBCDecryptor::NewL(theRC2decryptor, iIV->Des());
|
sl@0
|
143 |
|
sl@0
|
144 |
CleanupStack::Pop(3, theRC2encryptor);
|
sl@0
|
145 |
}
|
sl@0
|
146 |
break;
|
sl@0
|
147 |
case (ERC4):
|
sl@0
|
148 |
{
|
sl@0
|
149 |
iEncryptor = CARC4::NewL(*iKey,0);
|
sl@0
|
150 |
iDecryptor = CARC4::NewL(*iKey,0);
|
sl@0
|
151 |
}
|
sl@0
|
152 |
break;
|
sl@0
|
153 |
case (ECipherNull):
|
sl@0
|
154 |
{
|
sl@0
|
155 |
iEncryptor = CNullCipher::NewL();
|
sl@0
|
156 |
iDecryptor = CNullCipher::NewL();
|
sl@0
|
157 |
}
|
sl@0
|
158 |
break;
|
sl@0
|
159 |
|
sl@0
|
160 |
default:
|
sl@0
|
161 |
{
|
sl@0
|
162 |
ASSERT(0);
|
sl@0
|
163 |
User::Leave(KErrNotSupported);
|
sl@0
|
164 |
}
|
sl@0
|
165 |
}
|
sl@0
|
166 |
|
sl@0
|
167 |
CleanupStack::PushL(encryptor);
|
sl@0
|
168 |
CleanupStack::PushL(decryptor);
|
sl@0
|
169 |
|
sl@0
|
170 |
if(!iEncryptor && !iDecryptor)
|
sl@0
|
171 |
{
|
sl@0
|
172 |
CPaddingSSLv3* dPadding = CPaddingSSLv3::NewLC(decryptor->BlockSize());
|
sl@0
|
173 |
CPaddingSSLv3* ePadding = CPaddingSSLv3::NewLC(encryptor->BlockSize());
|
sl@0
|
174 |
iEncryptor = CBufferedEncryptor::NewL(encryptor, ePadding);
|
sl@0
|
175 |
CleanupStack::Pop(ePadding);
|
sl@0
|
176 |
iDecryptor = CBufferedDecryptor::NewL(decryptor, dPadding);
|
sl@0
|
177 |
CleanupStack::Pop(dPadding);
|
sl@0
|
178 |
}
|
sl@0
|
179 |
|
sl@0
|
180 |
iEResult = HBufC8::NewMaxL(iEncryptor->MaxOutputLength(iInput->Length()));
|
sl@0
|
181 |
iDResult = HBufC8::NewMaxL(iDecryptor->MaxOutputLength(iEResult->Size()));
|
sl@0
|
182 |
|
sl@0
|
183 |
CleanupStack::Pop(2, encryptor);
|
sl@0
|
184 |
}
|
sl@0
|
185 |
|
sl@0
|
186 |
void CActionVector::DoPerformActionL()
|
sl@0
|
187 |
{
|
sl@0
|
188 |
// First perform the test blockwise (ie passing in one block at a time)
|
sl@0
|
189 |
TUint blockSize = iEncryptor->BlockSize();
|
sl@0
|
190 |
TUint index = 0;
|
sl@0
|
191 |
while (index <= (iInput->Size() - blockSize))
|
sl@0
|
192 |
{
|
sl@0
|
193 |
TPtr8 theEncryptResult((TUint8*)&(iEResult->operator[](index)), blockSize, blockSize);
|
sl@0
|
194 |
theEncryptResult.FillZ(theEncryptResult.MaxLength());
|
sl@0
|
195 |
theEncryptResult.SetLength(0);
|
sl@0
|
196 |
|
sl@0
|
197 |
TPtrC8 theEncryptInput(iInput->Mid(index, blockSize));
|
sl@0
|
198 |
iEncryptor->Process(theEncryptInput, theEncryptResult);
|
sl@0
|
199 |
|
sl@0
|
200 |
if (iOutput->Mid(index, blockSize) == theEncryptResult)
|
sl@0
|
201 |
iResult = ETrue;
|
sl@0
|
202 |
else
|
sl@0
|
203 |
break; // No point doing any more
|
sl@0
|
204 |
|
sl@0
|
205 |
index+=blockSize;
|
sl@0
|
206 |
}
|
sl@0
|
207 |
|
sl@0
|
208 |
if (*iOutput==*iEResult)
|
sl@0
|
209 |
{
|
sl@0
|
210 |
iResult = ETrue;
|
sl@0
|
211 |
|
sl@0
|
212 |
index = 0;
|
sl@0
|
213 |
while (index <= (iEResult->Size()- blockSize))
|
sl@0
|
214 |
{
|
sl@0
|
215 |
TPtr8 theDecryptResult((TUint8*)&(iDResult->operator[](index)), blockSize, blockSize);
|
sl@0
|
216 |
theDecryptResult.FillZ(theDecryptResult.MaxLength());
|
sl@0
|
217 |
theDecryptResult.SetLength(0);
|
sl@0
|
218 |
|
sl@0
|
219 |
TPtrC8 theDecryptInput(iEResult->Mid(index, blockSize));
|
sl@0
|
220 |
iDecryptor->Process(theDecryptInput, theDecryptResult);
|
sl@0
|
221 |
|
sl@0
|
222 |
if(iInput->Mid(index, blockSize) != theDecryptResult)
|
sl@0
|
223 |
{
|
sl@0
|
224 |
iResult = EFalse;
|
sl@0
|
225 |
break; // No point doing any more
|
sl@0
|
226 |
}
|
sl@0
|
227 |
|
sl@0
|
228 |
index+=blockSize;
|
sl@0
|
229 |
}
|
sl@0
|
230 |
|
sl@0
|
231 |
if (*iInput!=*iDResult)
|
sl@0
|
232 |
iResult = EFalse;
|
sl@0
|
233 |
}
|
sl@0
|
234 |
|
sl@0
|
235 |
|
sl@0
|
236 |
iEncryptor->Reset();
|
sl@0
|
237 |
iDecryptor->Reset();
|
sl@0
|
238 |
// Now, if input is longer than a single block, repeat passing all the data
|
sl@0
|
239 |
if ((TUint)iInput->Size() > blockSize)
|
sl@0
|
240 |
{
|
sl@0
|
241 |
TPtr8 theEncryptResult(iEResult->Des());
|
sl@0
|
242 |
theEncryptResult.FillZ(theEncryptResult.MaxLength());
|
sl@0
|
243 |
theEncryptResult.SetLength(0);
|
sl@0
|
244 |
|
sl@0
|
245 |
TPtrC8 theInput(*iInput);
|
sl@0
|
246 |
iEncryptor->Process(theInput, theEncryptResult);
|
sl@0
|
247 |
|
sl@0
|
248 |
if (*iOutput!=*iEResult)
|
sl@0
|
249 |
{
|
sl@0
|
250 |
iResult = EFalse;
|
sl@0
|
251 |
}
|
sl@0
|
252 |
else
|
sl@0
|
253 |
{
|
sl@0
|
254 |
TPtr8 theDecryptResult(iDResult->Des());
|
sl@0
|
255 |
theDecryptResult.FillZ(theDecryptResult.MaxLength());
|
sl@0
|
256 |
theDecryptResult.SetLength(0);
|
sl@0
|
257 |
|
sl@0
|
258 |
iDecryptor->Process(*iEResult, theDecryptResult);
|
sl@0
|
259 |
if (*iInput!=*iDResult)
|
sl@0
|
260 |
iResult = EFalse;
|
sl@0
|
261 |
}
|
sl@0
|
262 |
}
|
sl@0
|
263 |
}
|
sl@0
|
264 |
|