First public contribution.
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=0, err);
121 iKey = HBufC8::NewL(keyTemp.Length());
125 TPtrC8 inputTemp = Input::ParseElement(aScriptBuffer, KInputStart, KInputEnd, pos=0, err);
126 iInput = HBufC8::NewL(inputTemp.Length());
130 TPtrC8 ivTemp = Input::ParseElement(aScriptBuffer, KIVStart, KIVEnd, pos=0, err);
131 iIV = HBufC8::NewL(ivTemp.Length());
135 TPtrC8 outputTemp = Input::ParseElement(aScriptBuffer, KOutputStart, KOutputEnd, pos=0, err);
136 iOutput = HBufC8::NewL(outputTemp.Length());
137 *iOutput = outputTemp;
140 TPtrC8 cipherType = Input::ParseElement(aScriptBuffer, KCipherTypeStart, KCipherTypeEnd, pos=0, err);
141 iExpectedWeakResult = KErrNone;
142 if( cipherType == _L8("DESECB") )
144 iCipherType = EDESECB;
146 else if( cipherType == _L8("DESCBC") )
148 iCipherType = EDESCBC;
150 else if( cipherType == _L8("3DESECB") )
152 iCipherType = E3DESECB;
153 iExpectedWeakResult = KErrKeyNotWeakEnough;
155 else if( cipherType == _L8("3DESCBC") )
157 iCipherType = E3DESCBC;
158 iExpectedWeakResult = KErrKeyNotWeakEnough;
160 else if( cipherType == _L8("AESECB") )
162 iCipherType = EAESECB;
163 iExpectedWeakResult = KErrKeyNotWeakEnough;
165 else if (cipherType==_L8("AESENC_ECB") )
167 iCipherType = EAESMonteCarloEncryptECB;
168 iExpectedWeakResult = KErrKeyNotWeakEnough;
170 else if (cipherType==_L8("AESDEC_ECB") )
172 iCipherType = EAESMonteCarloDecryptECB;
173 iExpectedWeakResult = KErrKeyNotWeakEnough;
175 else if (cipherType==_L8("AESENC_CBC") )
177 iCipherType = EAESMonteCarloEncryptCBC;
178 iExpectedWeakResult = KErrKeyNotWeakEnough;
180 else if (cipherType==_L8("AESDEC_CBC") )
182 iCipherType = EAESMonteCarloDecryptCBC;
183 iExpectedWeakResult = KErrKeyNotWeakEnough;
185 else if( cipherType == _L8("RC2ECB") )
187 iCipherType = ERC2ECB;
188 // weak enough if either aKey or aEffectiveKeyLen is weak
189 TInt minKeySize = Min(iEffectiveKeyLen, BytesToBits(iKey->Size()));
190 TRAP(iExpectedWeakResult, TCrypto::IsSymmetricWeakEnoughL(minKeySize));
192 else if( cipherType == _L8("RC2CBC") )
194 iCipherType = ERC2CBC;
195 // weak enough if either aKey or aEffectiveKeyLen is weak
196 TInt minKeySize = Min(iEffectiveKeyLen, BytesToBits(iKey->Size()));
197 TRAP(iExpectedWeakResult, TCrypto::IsSymmetricWeakEnoughL(minKeySize));
199 else if( cipherType == _L8("RC4") )
202 TRAP(iExpectedWeakResult, TCrypto::IsSymmetricWeakEnoughL(BytesToBits(iKey->Size())));
204 else if( cipherType == _L8("NULL") )
206 iCipherType = ECipherNull;
208 else if( cipherType == _L8("DESECB_WKT"))
210 iCipherType = EDESECB;
211 iExpectedWeakResult = KErrWeakKey;
213 else if( cipherType == _L8("DESCBC_WKT"))
215 iCipherType = EDESCBC;
216 iExpectedWeakResult = KErrWeakKey;
220 TBuf<64> cipherName(0);
221 cipherName.Copy(cipherType);
222 TBuf<256> formattable;
223 formattable.Format(_L("\nBad Cipher Type: %S"), &cipherName);
224 iConsole.Printf(formattable);
226 iActionState = CTestAction::EPostrequisite;
227 User::Leave(KErrNotSupported);
230 if ( (iCipherType==ERC2ECB) || (iCipherType==ERC2CBC) )
232 TPtrC8 effKeyLenTemp = Input::ParseElement(aScriptBuffer, KEffKeyLenStart, KEffKeyLenEnd, pos=0, err);
234 if (effKeyLenTemp.Length() != 0)
237 lex.Assign(effKeyLenTemp);
238 User::LeaveIfError(lex.Val(iEffectiveKeyLen));
242 TPtrC8 iterationsTemp = Input::ParseElement(aScriptBuffer, KIterationsStart, KIterationsEnd, pos=0, err);
244 lex.Assign(iterationsTemp);
245 err = lex.Val(iIterationTime);
247 TPtrC8 randTemp = Input::ParseElement(aScriptBuffer, KRandDataSizeStart, KRandDataSizeEnd, pos=0, err);
249 randlex.Assign(randTemp);
250 err = randlex.Val(iRandDataSize);
256 void CCryptoTestAction::Hex(HBufC8& aString)
258 TPtr8 ptr=aString.Des();
259 if (aString.Length()%2)
265 for (i=0;i<aString.Length();i+=2)
268 tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
270 tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
273 ptr.SetLength(aString.Length()/2);