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 |
*
|
sl@0
|
16 |
*/
|
sl@0
|
17 |
|
sl@0
|
18 |
|
sl@0
|
19 |
/**
|
sl@0
|
20 |
@file
|
sl@0
|
21 |
@internalTechnology
|
sl@0
|
22 |
*/
|
sl@0
|
23 |
|
sl@0
|
24 |
#include "symmetriccipherctrmodeoutoforderstep.h"
|
sl@0
|
25 |
|
sl@0
|
26 |
using namespace CryptoSpi;
|
sl@0
|
27 |
|
sl@0
|
28 |
CSymmetricCipherCtrModeOutOfOrderStep::CSymmetricCipherCtrModeOutOfOrderStep()
|
sl@0
|
29 |
{
|
sl@0
|
30 |
SetTestStepName(KSymmetricCipherCtrModeOutOfOrderStep);
|
sl@0
|
31 |
}
|
sl@0
|
32 |
|
sl@0
|
33 |
|
sl@0
|
34 |
TVerdict CSymmetricCipherCtrModeOutOfOrderStep::doTestStepPreambleL()
|
sl@0
|
35 |
{
|
sl@0
|
36 |
SetTestStepResult(EPass);
|
sl@0
|
37 |
return TestStepResult();
|
sl@0
|
38 |
}
|
sl@0
|
39 |
|
sl@0
|
40 |
|
sl@0
|
41 |
TVerdict CSymmetricCipherCtrModeOutOfOrderStep::doTestStepL()
|
sl@0
|
42 |
{
|
sl@0
|
43 |
INFO_PRINTF1(_L("*** Symmetric Cipher - Counter mode out of order operation ***"));
|
sl@0
|
44 |
INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
|
sl@0
|
45 |
|
sl@0
|
46 |
if (TestStepResult() != EPass)
|
sl@0
|
47 |
{
|
sl@0
|
48 |
return TestStepResult();
|
sl@0
|
49 |
}
|
sl@0
|
50 |
|
sl@0
|
51 |
//Assume failure, unless all is successful
|
sl@0
|
52 |
SetTestStepResult(EFail);
|
sl@0
|
53 |
|
sl@0
|
54 |
TVariantPtrC operationMode;
|
sl@0
|
55 |
|
sl@0
|
56 |
CSymmetricCipher* impl = NULL;
|
sl@0
|
57 |
CKey* key = NULL;
|
sl@0
|
58 |
SetupCipherL(EFalse, EFalse, operationMode, impl, key);
|
sl@0
|
59 |
|
sl@0
|
60 |
INFO_PRINTF1(_L("Plugin loaded."));
|
sl@0
|
61 |
|
sl@0
|
62 |
CleanupStack::PushL(key);
|
sl@0
|
63 |
CleanupStack::PushL(impl);
|
sl@0
|
64 |
|
sl@0
|
65 |
|
sl@0
|
66 |
if (TUid(operationMode) != KOperationModeCTRUid)
|
sl@0
|
67 |
{
|
sl@0
|
68 |
ERR_PRINTF2(_L("*** FAIL: This test supports CTR operation mode only and not mode id: %d ***"), (TUid(operationMode)).iUid);
|
sl@0
|
69 |
CleanupStack::PopAndDestroy(2, key);
|
sl@0
|
70 |
INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
|
sl@0
|
71 |
return TestStepResult();
|
sl@0
|
72 |
}
|
sl@0
|
73 |
|
sl@0
|
74 |
|
sl@0
|
75 |
HBufC8* plaintext = ReadInPlaintextL();
|
sl@0
|
76 |
CleanupStack::PushL(plaintext);
|
sl@0
|
77 |
|
sl@0
|
78 |
|
sl@0
|
79 |
TInt blockSize = CtrModeCalcBlockSizeL(*impl)/8;
|
sl@0
|
80 |
|
sl@0
|
81 |
if (plaintext->Length() < ((blockSize * 2) + 1))
|
sl@0
|
82 |
{
|
sl@0
|
83 |
ERR_PRINTF2(_L("*** FAIL: Plaintext argument is not long enough for this test, length(B) = %d ***"), plaintext->Length());
|
sl@0
|
84 |
CleanupStack::PopAndDestroy(3, key);
|
sl@0
|
85 |
INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
|
sl@0
|
86 |
return TestStepResult();
|
sl@0
|
87 |
}
|
sl@0
|
88 |
|
sl@0
|
89 |
// This will store our calculated version of the ciphertext to compare with that in the .ini file
|
sl@0
|
90 |
TUint8* calculatedCiphertext = new (ELeave) TUint8[plaintext->Length()];
|
sl@0
|
91 |
CleanupStack::PushL(calculatedCiphertext);
|
sl@0
|
92 |
// Ptr to the first block of the calculated ciphertext
|
sl@0
|
93 |
TPtr8 calcCipherPtr1(calculatedCiphertext, blockSize);
|
sl@0
|
94 |
// Ptr to the remaining blocks of the calculated ciphertext
|
sl@0
|
95 |
TPtr8 calcCipherPtr2((calculatedCiphertext + blockSize), (plaintext->Length() - blockSize));
|
sl@0
|
96 |
|
sl@0
|
97 |
// Ptr to the first block of the .ini file's plaintext
|
sl@0
|
98 |
TPtrC8 knownPlainPtr1(plaintext->Ptr(), blockSize);
|
sl@0
|
99 |
// Ptr to the remaining blocks of the .ini file's plaintext
|
sl@0
|
100 |
TPtrC8 knownPlainPtr2((plaintext->Ptr() + blockSize), (plaintext->Length() - blockSize));
|
sl@0
|
101 |
|
sl@0
|
102 |
|
sl@0
|
103 |
HBufC8* iv = ReadInIvL();
|
sl@0
|
104 |
CleanupStack::PushL(iv);
|
sl@0
|
105 |
|
sl@0
|
106 |
// Increment IV to the value for the second block so we can encrypt blocks 2+ first
|
sl@0
|
107 |
HBufC8* incrementedIv1 = CtrModeIncrementCounterL((*iv));
|
sl@0
|
108 |
CleanupStack::PushL(incrementedIv1);
|
sl@0
|
109 |
|
sl@0
|
110 |
impl->SetIvL(*incrementedIv1);
|
sl@0
|
111 |
|
sl@0
|
112 |
|
sl@0
|
113 |
INFO_PRINTF1(_L("Setup complete. Encrypting blocks 2+."));
|
sl@0
|
114 |
impl->ProcessL(knownPlainPtr2, calcCipherPtr2);
|
sl@0
|
115 |
|
sl@0
|
116 |
INFO_PRINTF1(_L("Blocks 2+ encrypted. Reseting and encrypting block 1."));
|
sl@0
|
117 |
impl->SetIvL((*iv));
|
sl@0
|
118 |
impl->ProcessL(knownPlainPtr1, calcCipherPtr1);
|
sl@0
|
119 |
|
sl@0
|
120 |
|
sl@0
|
121 |
HBufC8* knownCiphertext = ReadInCiphertextL();
|
sl@0
|
122 |
CleanupStack::PushL(knownCiphertext);
|
sl@0
|
123 |
|
sl@0
|
124 |
// Check that calculated ciphertext matches the expected value
|
sl@0
|
125 |
TPtrC8 wholeCalcCiphertext(calculatedCiphertext, (plaintext->Length()));
|
sl@0
|
126 |
if (wholeCalcCiphertext.Compare((*knownCiphertext)) != 0)
|
sl@0
|
127 |
{
|
sl@0
|
128 |
ERR_PRINTF1(_L("*** FAIL: Calculated ciphertext does not match expected value ***"));
|
sl@0
|
129 |
CleanupStack::PopAndDestroy(7, key);
|
sl@0
|
130 |
INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
|
sl@0
|
131 |
return TestStepResult();
|
sl@0
|
132 |
}
|
sl@0
|
133 |
else
|
sl@0
|
134 |
{
|
sl@0
|
135 |
INFO_PRINTF1(_L("Calculated ciphertext matches the expected value."));
|
sl@0
|
136 |
}
|
sl@0
|
137 |
|
sl@0
|
138 |
|
sl@0
|
139 |
// **** SWITCH TO DECRYPTION NOW ****
|
sl@0
|
140 |
|
sl@0
|
141 |
// This will store our calculated version of the plaintext to compare with that in the .ini file
|
sl@0
|
142 |
TUint8* calculatedPlaintext = new (ELeave) TUint8[plaintext->Length()];
|
sl@0
|
143 |
CleanupStack::PushL(calculatedPlaintext);
|
sl@0
|
144 |
// Ptr to the first block of the calculated plaintext
|
sl@0
|
145 |
TPtr8 calcPlainPtr1(calculatedPlaintext, (blockSize * 2));
|
sl@0
|
146 |
// Ptr to the remaining blocks of the calculated plaintext
|
sl@0
|
147 |
TPtr8 calcPlainPtr2((calculatedPlaintext + (blockSize * 2)), (plaintext->Length() - (blockSize * 2)));
|
sl@0
|
148 |
|
sl@0
|
149 |
// Ptr to the first 2 blocks of the ciphertext
|
sl@0
|
150 |
TPtrC8 knownCipherPtr1(wholeCalcCiphertext.Ptr(), (blockSize * 2));
|
sl@0
|
151 |
// Ptr to the remaining blocks of the ciphertext
|
sl@0
|
152 |
TPtrC8 knownCipherPtr2((wholeCalcCiphertext.Ptr() + (blockSize * 2)), (wholeCalcCiphertext.Length() - (blockSize * 2)));
|
sl@0
|
153 |
|
sl@0
|
154 |
|
sl@0
|
155 |
// Increment IV to the value for the third block so we can decrypt blocks 3+ first
|
sl@0
|
156 |
HBufC8* incrementedIv2 = CtrModeIncrementCounterL(*incrementedIv1);
|
sl@0
|
157 |
CleanupStack::PushL(incrementedIv2);
|
sl@0
|
158 |
impl->SetIvL(*incrementedIv2);
|
sl@0
|
159 |
|
sl@0
|
160 |
INFO_PRINTF1(_L("Setup complete. Decrypting blocks 3+."));
|
sl@0
|
161 |
impl->ProcessL(knownCipherPtr2, calcPlainPtr2);
|
sl@0
|
162 |
|
sl@0
|
163 |
INFO_PRINTF1(_L("Blocks 3+ encrypted. Reseting and encrypting blocks 1 and 2."));
|
sl@0
|
164 |
impl->SetIvL((*iv));
|
sl@0
|
165 |
impl->ProcessL(knownCipherPtr1, calcPlainPtr1);
|
sl@0
|
166 |
|
sl@0
|
167 |
TPtrC8 wholeCalcPlaintext(calculatedPlaintext, (plaintext->Length()));
|
sl@0
|
168 |
|
sl@0
|
169 |
if (wholeCalcPlaintext.Compare((*plaintext)) != 0)
|
sl@0
|
170 |
{
|
sl@0
|
171 |
ERR_PRINTF1(_L("*** FAIL: Calculated plaintext does not match expected value ***"));
|
sl@0
|
172 |
}
|
sl@0
|
173 |
else
|
sl@0
|
174 |
{
|
sl@0
|
175 |
INFO_PRINTF1(_L("*** PASS: Calculated plaintext matches the original one ***"));
|
sl@0
|
176 |
SetTestStepResult(EPass);
|
sl@0
|
177 |
}
|
sl@0
|
178 |
|
sl@0
|
179 |
CleanupStack::PopAndDestroy(9, key);
|
sl@0
|
180 |
|
sl@0
|
181 |
INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
|
sl@0
|
182 |
|
sl@0
|
183 |
return TestStepResult();
|
sl@0
|
184 |
}
|
sl@0
|
185 |
|
sl@0
|
186 |
|
sl@0
|
187 |
TVerdict CSymmetricCipherCtrModeOutOfOrderStep::doTestStepPostambleL()
|
sl@0
|
188 |
{
|
sl@0
|
189 |
return TestStepResult();
|
sl@0
|
190 |
}
|