Update contrib.
2 * Copyright (c) 1998-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.
19 #include "tactionmontecarlo.h"
20 #include "bufferedtransformation.h"
25 const TInt KAESBlockSizeBytes = 16; // 128 bits
27 CTestAction* CActionMonteCarlo::NewL(RFs& aFs,
28 CConsoleBase& aConsole,
30 const TTestActionSpec& aTestActionSpec)
32 CTestAction* self = CActionMonteCarlo::NewLC(aFs, aConsole,
33 aOut, aTestActionSpec);
38 CTestAction* CActionMonteCarlo::NewLC(RFs& aFs,
39 CConsoleBase& aConsole,
41 const TTestActionSpec& aTestActionSpec)
43 CActionMonteCarlo* self = new(ELeave) CActionMonteCarlo(aFs, aConsole, aOut);
44 CleanupStack::PushL(self);
45 self->ConstructL(aTestActionSpec);
49 CActionMonteCarlo::~CActionMonteCarlo()
55 CActionMonteCarlo::CActionMonteCarlo(RFs& aFs,
56 CConsoleBase& aConsole,
59 : CCryptoTestAction(aFs, aConsole, aOut)
63 void CActionMonteCarlo::DoPerformPrerequisiteL()
67 TPtrC8 monteCarlo = Input::ParseElement(*iBody, KMonteCarloStart, KMonteCarloEnd, pos, err);
69 DoInputParseL(monteCarlo);
71 CBlockTransformation* encryptor = NULL;
72 CBlockTransformation* decryptor = NULL;
76 case (EAESMonteCarloEncryptECB):
78 encryptor = CAESEncryptor::NewLC(iKey->Des());
81 case (EAESMonteCarloDecryptECB):
83 decryptor = CAESDecryptor::NewLC(iKey->Des());
86 case (EAESMonteCarloEncryptCBC):
88 CBlockTransformation* aesEncryptor = NULL;
89 aesEncryptor = CAESEncryptor::NewLC(iKey->Des());
91 encryptor = CModeCBCEncryptor::NewL(aesEncryptor, iIV->Des());
92 CleanupStack::Pop(aesEncryptor);
93 CleanupStack::PushL(encryptor);
96 case (EAESMonteCarloDecryptCBC):
98 CBlockTransformation* aesDecryptor = NULL;
99 aesDecryptor = CAESDecryptor::NewLC(iKey->Des());
101 decryptor = CModeCBCDecryptor::NewL(aesDecryptor, iIV->Des());
102 CleanupStack::Pop(aesDecryptor);
103 CleanupStack::PushL(decryptor);
109 User::Leave(KErrNotSupported);
114 CPaddingSSLv3* padding = 0;
117 padding = CPaddingSSLv3::NewLC(encryptor->BlockSize());
118 iEncrypt = CBufferedEncryptor::NewL(encryptor, padding);
119 iEResult = HBufC8::NewMaxL(iEncrypt->MaxOutputLength(iInput->Length()));
123 padding = CPaddingSSLv3::NewLC(decryptor->BlockSize());
124 iDecrypt = CBufferedDecryptor::NewL(decryptor, padding);
125 iDResult = HBufC8::NewMaxL(iDecrypt->MaxOutputLength(iInput->Size()));
128 CleanupStack::Pop(2); // padding, encryptor/decryptor
133 void CActionMonteCarlo::DoPerformActionL()
137 __ASSERT_DEBUG(iInput->Size()==KAESBlockSizeBytes, User::Panic(_L("tsymmetric"), KErrNotSupported));
139 if (iCipherType==EAESMonteCarloEncryptECB)
141 else if (iCipherType==EAESMonteCarloDecryptECB)
143 else if (iCipherType==EAESMonteCarloEncryptCBC)
145 else if (iCipherType==EAESMonteCarloDecryptCBC)
148 User::Leave(KErrNotSupported);
151 void CActionMonteCarlo::DoAESEncryptECB()
153 TPtr8 theEncryptResult(iEResult->Des());
154 theEncryptResult.FillZ(theEncryptResult.MaxLength());
155 theEncryptResult.SetLength(0);
158 TPtr8 theInput(iInput->Des());
159 for (; index < KMonteCarloIterations; index++)
161 iEncrypt->Process(theInput, theEncryptResult);
162 theInput.Copy(theEncryptResult);
163 theEncryptResult.FillZ(theEncryptResult.MaxLength());
164 theEncryptResult.SetLength(0);
167 if (*iOutput==*iEResult)
173 void CActionMonteCarlo::DoAESDecryptECB()
175 TPtr8 theDecryptResult(iDResult->Des());
176 theDecryptResult.FillZ(theDecryptResult.MaxLength());
177 theDecryptResult.SetLength(0);
180 TPtr8 theInput(iInput->Des());
181 for (; index < KMonteCarloIterations; index++)
183 iDecrypt->Process(theInput, theDecryptResult);
184 theInput.Copy(theDecryptResult);
185 theDecryptResult.FillZ(theDecryptResult.MaxLength());
186 theDecryptResult.SetLength(0);
189 if (*iOutput==*iInput)
195 void CActionMonteCarlo::DoAESEncryptCBC()
197 TPtr8 theEncryptResult(iEResult->Des());
198 theEncryptResult.FillZ(theEncryptResult.MaxLength());
199 theEncryptResult.SetLength(0);
202 TPtr8 theInput(iInput->Des());
204 TBuf8<KAESBlockSizeBytes> nextBuf;
205 nextBuf.FillZ(KAESBlockSizeBytes);
207 for (; index < KMonteCarloIterations-1; index++)
209 iEncrypt->Process(theInput, theEncryptResult);
212 theInput.Copy(*iIV); // First loop, use the original IV as next PT block
214 theInput.Copy(nextBuf); // Use previous CT block as next PT block
216 // Save CT block for next loop when it'll become the PT block
217 nextBuf.Copy(theEncryptResult);
218 // Reset for next encryption
219 theEncryptResult.FillZ(theEncryptResult.MaxLength());
220 theEncryptResult.SetLength(0);
223 iEncrypt->Process(theInput, theEncryptResult);
225 if (theEncryptResult.Compare(*iOutput)==KErrNone)
232 void CActionMonteCarlo::DoAESDecryptCBC()
234 TPtr8 theDecryptResult(iDResult->Des());
235 theDecryptResult.FillZ(theDecryptResult.MaxLength());
236 theDecryptResult.SetLength(0);
239 TPtr8 theInput(iInput->Des());
241 for (; index < KMonteCarloIterations-1; index++)
243 iDecrypt->Process(theInput, theDecryptResult);
245 // Use previous PT block as next CT block
246 theInput.Copy(theDecryptResult);
248 // Reset for next decryption
249 theDecryptResult.FillZ(theDecryptResult.MaxLength());
250 theDecryptResult.SetLength(0);
254 iDecrypt->Process(theInput, theDecryptResult);
256 if (theDecryptResult.Compare(*iOutput)==KErrNone)