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 "cryptotestaction.h"
20 #include <securityerr.h>
21 #include <cryptostrength.h>
22 #include "../common/inlines.h"
25 CCryptoTestAction::CCryptoTestAction(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
26 : CTestAction(aConsole, aOut), iFs(aFs), iEffectiveKeyLen(128)
29 CCryptoTestAction::~CCryptoTestAction()
34 void CCryptoTestAction::ConstructL(const TTestActionSpec& aTestActionSpec)
36 CTestAction::ConstructL(aTestActionSpec);
37 iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
38 iBody->Des().Copy(aTestActionSpec.iActionBody);
41 void CCryptoTestAction::DoPerformPrerequisite(TRequestStatus& aStatus)
43 TRequestStatus* status = &aStatus;
45 TRAPD(r, DoPerformPrerequisiteL());
46 if ((r != iExpectedWeakResult) && (TCrypto::Strength() == TCrypto::EWeak))
50 User::RequestComplete(status, r);
53 iActionState = CTestAction::EAction;
55 else if (r==KErrKeyNotWeakEnough)
58 iConsole.Printf(_L("Crypto libraries returned KErrKeyNotWeakEnough! Passing test automatically.\n\r"));
59 iOut.writeString(_L("Crypto libraries returned KErrKeyNotWeakEnough! Passing test automatically.\r\n"));
60 iActionState = CTestAction::EPostrequisite;
62 else if (r==KErrWeakKey)
65 iConsole.Printf(_L("Crypto libraries returned KErrWeakKey! Passing test automatically.\n\r"));
66 iOut.writeString(_L("Crypto libraries returned KErrWeakKey! Passing test automatically.\r\n"));
67 iActionState = CTestAction::EPostrequisite;
71 iActionState = CTestAction::EPostrequisite;
75 void CCryptoTestAction::DoPerformPostrequisite(TRequestStatus& aStatus)
77 TRequestStatus* status = &aStatus;
78 TRAPD(r, DoPerformPostrequisiteL());
87 User::RequestComplete(status, r);
90 void CCryptoTestAction::DoReportAction(void)
93 void CCryptoTestAction::DoCheckResult(TInt)
96 iConsole.Printf(_L("X"));
98 iConsole.Printf(_L("."));
101 void CCryptoTestAction::PerformAction(TRequestStatus& aStatus)
103 TRequestStatus* status = &aStatus;
105 TRAPD(res, DoPerformActionL());
106 if (res==KErrNoMemory)
108 User::Leave(res); // For OOM testing
110 iActionState = CTestAction::EPostrequisite;
111 User::RequestComplete(status, res);
115 void CCryptoTestAction::DoInputParseL(TDesC8& aScriptBuffer)
120 TPtrC8 keyTemp = Input::ParseElement(aScriptBuffer, KKeyStart, KKeyEnd, pos, err);
121 iKey = HBufC8::NewL(keyTemp.Length());
126 TPtrC8 inputTemp = Input::ParseElement(aScriptBuffer, KInputStart, KInputEnd, pos, err);
127 iInput = HBufC8::NewL(inputTemp.Length());
132 TPtrC8 ivTemp = Input::ParseElement(aScriptBuffer, KIVStart, KIVEnd, pos, err);
133 iIV = HBufC8::NewL(ivTemp.Length());
138 TPtrC8 outputTemp = Input::ParseElement(aScriptBuffer, KOutputStart, KOutputEnd, pos, err);
139 iOutput = HBufC8::NewL(outputTemp.Length());
140 *iOutput = outputTemp;
144 TPtrC8 cipherType = Input::ParseElement(aScriptBuffer, KCipherTypeStart, KCipherTypeEnd, pos, err);
145 iExpectedWeakResult = KErrNone;
146 if( cipherType == _L8("DESECB") )
148 iCipherType = EDESECB;
150 else if( cipherType == _L8("DESCBC") )
152 iCipherType = EDESCBC;
154 else if( cipherType == _L8("3DESECB") )
156 iCipherType = E3DESECB;
157 iExpectedWeakResult = KErrKeyNotWeakEnough;
159 else if( cipherType == _L8("3DESCBC") )
161 iCipherType = E3DESCBC;
162 iExpectedWeakResult = KErrKeyNotWeakEnough;
164 else if( cipherType == _L8("AESECB") )
166 iCipherType = EAESECB;
167 iExpectedWeakResult = KErrKeyNotWeakEnough;
169 else if (cipherType==_L8("AESENC_ECB") )
171 iCipherType = EAESMonteCarloEncryptECB;
172 iExpectedWeakResult = KErrKeyNotWeakEnough;
174 else if (cipherType==_L8("AESDEC_ECB") )
176 iCipherType = EAESMonteCarloDecryptECB;
177 iExpectedWeakResult = KErrKeyNotWeakEnough;
179 else if (cipherType==_L8("AESENC_CBC") )
181 iCipherType = EAESMonteCarloEncryptCBC;
182 iExpectedWeakResult = KErrKeyNotWeakEnough;
184 else if (cipherType==_L8("AESDEC_CBC") )
186 iCipherType = EAESMonteCarloDecryptCBC;
187 iExpectedWeakResult = KErrKeyNotWeakEnough;
189 else if( cipherType == _L8("RC2ECB") )
191 iCipherType = ERC2ECB;
192 // weak enough if either aKey or aEffectiveKeyLen is weak
193 TInt minKeySize = Min(iEffectiveKeyLen, BytesToBits(iKey->Size()));
194 TRAP(iExpectedWeakResult, TCrypto::IsSymmetricWeakEnoughL(minKeySize));
196 else if( cipherType == _L8("RC2CBC") )
198 iCipherType = ERC2CBC;
199 // weak enough if either aKey or aEffectiveKeyLen is weak
200 TInt minKeySize = Min(iEffectiveKeyLen, BytesToBits(iKey->Size()));
201 TRAP(iExpectedWeakResult, TCrypto::IsSymmetricWeakEnoughL(minKeySize));
203 else if( cipherType == _L8("RC4") )
206 TRAP(iExpectedWeakResult, TCrypto::IsSymmetricWeakEnoughL(BytesToBits(iKey->Size())));
208 else if( cipherType == _L8("NULL") )
210 iCipherType = ECipherNull;
212 else if( cipherType == _L8("DESECB_WKT"))
214 iCipherType = EDESECB;
215 iExpectedWeakResult = KErrWeakKey;
217 else if( cipherType == _L8("DESCBC_WKT"))
219 iCipherType = EDESCBC;
220 iExpectedWeakResult = KErrWeakKey;
224 TBuf<64> cipherName(0);
225 cipherName.Copy(cipherType);
226 TBuf<256> formattable;
227 formattable.Format(_L("\nBad Cipher Type: %S"), &cipherName);
228 iConsole.Printf(formattable);
230 iActionState = CTestAction::EPostrequisite;
231 User::Leave(KErrNotSupported);
234 if ( (iCipherType==ERC2ECB) || (iCipherType==ERC2CBC) )
237 TPtrC8 effKeyLenTemp = Input::ParseElement(aScriptBuffer, KEffKeyLenStart, KEffKeyLenEnd, pos, err);
239 if (effKeyLenTemp.Length() != 0)
242 lex.Assign(effKeyLenTemp);
243 User::LeaveIfError(lex.Val(iEffectiveKeyLen));
248 TPtrC8 iterationsTemp = Input::ParseElement(aScriptBuffer, KIterationsStart, KIterationsEnd, pos, err);
250 lex.Assign(iterationsTemp);
251 err = lex.Val(iIterationTime);
254 TPtrC8 randTemp = Input::ParseElement(aScriptBuffer, KRandDataSizeStart, KRandDataSizeEnd, pos, err);
256 randlex.Assign(randTemp);
257 err = randlex.Val(iRandDataSize);
263 void CCryptoTestAction::Hex(HBufC8& aString)
265 TPtr8 ptr=aString.Des();
266 if (aString.Length()%2)
272 for (i=0;i<aString.Length();i+=2)
275 tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
277 tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
280 ptr.SetLength(aString.Length()/2);