os/security/crypto/weakcryptospi/test/tcryptospi/src/symmetriccipherctrmodeoutoforderstep.cpp
Update contrib.
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
24 #include "symmetriccipherctrmodeoutoforderstep.h"
26 using namespace CryptoSpi;
28 CSymmetricCipherCtrModeOutOfOrderStep::CSymmetricCipherCtrModeOutOfOrderStep()
30 SetTestStepName(KSymmetricCipherCtrModeOutOfOrderStep);
34 TVerdict CSymmetricCipherCtrModeOutOfOrderStep::doTestStepPreambleL()
36 SetTestStepResult(EPass);
37 return TestStepResult();
41 TVerdict CSymmetricCipherCtrModeOutOfOrderStep::doTestStepL()
43 INFO_PRINTF1(_L("*** Symmetric Cipher - Counter mode out of order operation ***"));
44 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
46 if (TestStepResult() != EPass)
48 return TestStepResult();
51 //Assume failure, unless all is successful
52 SetTestStepResult(EFail);
54 TVariantPtrC operationMode;
56 CSymmetricCipher* impl = NULL;
58 SetupCipherL(EFalse, EFalse, operationMode, impl, key);
60 INFO_PRINTF1(_L("Plugin loaded."));
62 CleanupStack::PushL(key);
63 CleanupStack::PushL(impl);
66 if (TUid(operationMode) != KOperationModeCTRUid)
68 ERR_PRINTF2(_L("*** FAIL: This test supports CTR operation mode only and not mode id: %d ***"), (TUid(operationMode)).iUid);
69 CleanupStack::PopAndDestroy(2, key);
70 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
71 return TestStepResult();
75 HBufC8* plaintext = ReadInPlaintextL();
76 CleanupStack::PushL(plaintext);
79 TInt blockSize = CtrModeCalcBlockSizeL(*impl)/8;
81 if (plaintext->Length() < ((blockSize * 2) + 1))
83 ERR_PRINTF2(_L("*** FAIL: Plaintext argument is not long enough for this test, length(B) = %d ***"), plaintext->Length());
84 CleanupStack::PopAndDestroy(3, key);
85 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
86 return TestStepResult();
89 // This will store our calculated version of the ciphertext to compare with that in the .ini file
90 TUint8* calculatedCiphertext = new (ELeave) TUint8[plaintext->Length()];
91 CleanupStack::PushL(calculatedCiphertext);
92 // Ptr to the first block of the calculated ciphertext
93 TPtr8 calcCipherPtr1(calculatedCiphertext, blockSize);
94 // Ptr to the remaining blocks of the calculated ciphertext
95 TPtr8 calcCipherPtr2((calculatedCiphertext + blockSize), (plaintext->Length() - blockSize));
97 // Ptr to the first block of the .ini file's plaintext
98 TPtrC8 knownPlainPtr1(plaintext->Ptr(), blockSize);
99 // Ptr to the remaining blocks of the .ini file's plaintext
100 TPtrC8 knownPlainPtr2((plaintext->Ptr() + blockSize), (plaintext->Length() - blockSize));
103 HBufC8* iv = ReadInIvL();
104 CleanupStack::PushL(iv);
106 // Increment IV to the value for the second block so we can encrypt blocks 2+ first
107 HBufC8* incrementedIv1 = CtrModeIncrementCounterL((*iv));
108 CleanupStack::PushL(incrementedIv1);
110 impl->SetIvL(*incrementedIv1);
113 INFO_PRINTF1(_L("Setup complete. Encrypting blocks 2+."));
114 impl->ProcessL(knownPlainPtr2, calcCipherPtr2);
116 INFO_PRINTF1(_L("Blocks 2+ encrypted. Reseting and encrypting block 1."));
118 impl->ProcessL(knownPlainPtr1, calcCipherPtr1);
121 HBufC8* knownCiphertext = ReadInCiphertextL();
122 CleanupStack::PushL(knownCiphertext);
124 // Check that calculated ciphertext matches the expected value
125 TPtrC8 wholeCalcCiphertext(calculatedCiphertext, (plaintext->Length()));
126 if (wholeCalcCiphertext.Compare((*knownCiphertext)) != 0)
128 ERR_PRINTF1(_L("*** FAIL: Calculated ciphertext does not match expected value ***"));
129 CleanupStack::PopAndDestroy(7, key);
130 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
131 return TestStepResult();
135 INFO_PRINTF1(_L("Calculated ciphertext matches the expected value."));
139 // **** SWITCH TO DECRYPTION NOW ****
141 // This will store our calculated version of the plaintext to compare with that in the .ini file
142 TUint8* calculatedPlaintext = new (ELeave) TUint8[plaintext->Length()];
143 CleanupStack::PushL(calculatedPlaintext);
144 // Ptr to the first block of the calculated plaintext
145 TPtr8 calcPlainPtr1(calculatedPlaintext, (blockSize * 2));
146 // Ptr to the remaining blocks of the calculated plaintext
147 TPtr8 calcPlainPtr2((calculatedPlaintext + (blockSize * 2)), (plaintext->Length() - (blockSize * 2)));
149 // Ptr to the first 2 blocks of the ciphertext
150 TPtrC8 knownCipherPtr1(wholeCalcCiphertext.Ptr(), (blockSize * 2));
151 // Ptr to the remaining blocks of the ciphertext
152 TPtrC8 knownCipherPtr2((wholeCalcCiphertext.Ptr() + (blockSize * 2)), (wholeCalcCiphertext.Length() - (blockSize * 2)));
155 // Increment IV to the value for the third block so we can decrypt blocks 3+ first
156 HBufC8* incrementedIv2 = CtrModeIncrementCounterL(*incrementedIv1);
157 CleanupStack::PushL(incrementedIv2);
158 impl->SetIvL(*incrementedIv2);
160 INFO_PRINTF1(_L("Setup complete. Decrypting blocks 3+."));
161 impl->ProcessL(knownCipherPtr2, calcPlainPtr2);
163 INFO_PRINTF1(_L("Blocks 3+ encrypted. Reseting and encrypting blocks 1 and 2."));
165 impl->ProcessL(knownCipherPtr1, calcPlainPtr1);
167 TPtrC8 wholeCalcPlaintext(calculatedPlaintext, (plaintext->Length()));
169 if (wholeCalcPlaintext.Compare((*plaintext)) != 0)
171 ERR_PRINTF1(_L("*** FAIL: Calculated plaintext does not match expected value ***"));
175 INFO_PRINTF1(_L("*** PASS: Calculated plaintext matches the original one ***"));
176 SetTestStepResult(EPass);
179 CleanupStack::PopAndDestroy(9, key);
181 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
183 return TestStepResult();
187 TVerdict CSymmetricCipherCtrModeOutOfOrderStep::doTestStepPostambleL()
189 return TestStepResult();