1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tsymmetric/cryptotestaction.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,275 @@
1.4 +/*
1.5 +* Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include "cryptotestaction.h"
1.23 +#include <securityerr.h>
1.24 +#include <cryptostrength.h>
1.25 +#include "../common/inlines.h"
1.26 +#include "t_output.h"
1.27 +
1.28 +CCryptoTestAction::CCryptoTestAction(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.29 +: CTestAction(aConsole, aOut), iFs(aFs), iEffectiveKeyLen(128)
1.30 +{}
1.31 +
1.32 +CCryptoTestAction::~CCryptoTestAction()
1.33 +{
1.34 + delete iBody;
1.35 +}
1.36 +
1.37 +void CCryptoTestAction::ConstructL(const TTestActionSpec& aTestActionSpec)
1.38 +{
1.39 + CTestAction::ConstructL(aTestActionSpec);
1.40 + iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
1.41 + iBody->Des().Copy(aTestActionSpec.iActionBody);
1.42 +}
1.43 +
1.44 +void CCryptoTestAction::DoPerformPrerequisite(TRequestStatus& aStatus)
1.45 +{
1.46 + TRequestStatus* status = &aStatus;
1.47 +
1.48 + TRAPD(r, DoPerformPrerequisiteL());
1.49 + if ((r != iExpectedWeakResult) && (TCrypto::Strength() == TCrypto::EWeak))
1.50 + {
1.51 + r = KErrUnknown;
1.52 + }
1.53 + User::RequestComplete(status, r);
1.54 + if (KErrNone==r)
1.55 + {
1.56 + iActionState = CTestAction::EAction;
1.57 + }
1.58 + else if (r==KErrKeyNotWeakEnough)
1.59 + {
1.60 + iResult = ETrue;
1.61 + iConsole.Printf(_L("Crypto libraries returned KErrKeyNotWeakEnough! Passing test automatically.\n\r"));
1.62 + iOut.writeString(_L("Crypto libraries returned KErrKeyNotWeakEnough! Passing test automatically.\r\n"));
1.63 + iActionState = CTestAction::EPostrequisite;
1.64 + }
1.65 + else if (r==KErrWeakKey)
1.66 + {
1.67 + iResult = ETrue;
1.68 + iConsole.Printf(_L("Crypto libraries returned KErrWeakKey! Passing test automatically.\n\r"));
1.69 + iOut.writeString(_L("Crypto libraries returned KErrWeakKey! Passing test automatically.\r\n"));
1.70 + iActionState = CTestAction::EPostrequisite;
1.71 + }
1.72 + else
1.73 + {
1.74 + iActionState = CTestAction::EPostrequisite;
1.75 + }
1.76 +}
1.77 +
1.78 +void CCryptoTestAction::DoPerformPostrequisite(TRequestStatus& aStatus)
1.79 +{
1.80 + TRequestStatus* status = &aStatus;
1.81 + TRAPD(r, DoPerformPostrequisiteL());
1.82 + delete iKey;
1.83 + delete iInput;
1.84 + delete iOutput;
1.85 + delete iIV;
1.86 + delete iEResult;
1.87 + delete iDResult;
1.88 +
1.89 + iFinished = ETrue;
1.90 + User::RequestComplete(status, r);
1.91 +}
1.92 +
1.93 +void CCryptoTestAction::DoReportAction(void)
1.94 +{}
1.95 +
1.96 +void CCryptoTestAction::DoCheckResult(TInt)
1.97 +{
1.98 + if (iResult==EFalse)
1.99 + iConsole.Printf(_L("X"));
1.100 + else
1.101 + iConsole.Printf(_L("."));
1.102 +}
1.103 +
1.104 +void CCryptoTestAction::PerformAction(TRequestStatus& aStatus)
1.105 +{
1.106 + TRequestStatus* status = &aStatus;
1.107 +
1.108 + TRAPD(res, DoPerformActionL());
1.109 + if (res==KErrNoMemory)
1.110 + {
1.111 + User::Leave(res); // For OOM testing
1.112 + }
1.113 + iActionState = CTestAction::EPostrequisite;
1.114 + User::RequestComplete(status, res);
1.115 +}
1.116 +
1.117 +
1.118 +void CCryptoTestAction::DoInputParseL(TDesC8& aScriptBuffer)
1.119 +{
1.120 + TInt err = KErrNone;
1.121 + TInt pos = 0;
1.122 +
1.123 + TPtrC8 keyTemp = Input::ParseElement(aScriptBuffer, KKeyStart, KKeyEnd, pos=0, err);
1.124 + iKey = HBufC8::NewL(keyTemp.Length());
1.125 + *iKey = keyTemp;
1.126 + Hex(*iKey);
1.127 +
1.128 + TPtrC8 inputTemp = Input::ParseElement(aScriptBuffer, KInputStart, KInputEnd, pos=0, err);
1.129 + iInput = HBufC8::NewL(inputTemp.Length());
1.130 + *iInput = inputTemp;
1.131 + Hex(*iInput);
1.132 +
1.133 + TPtrC8 ivTemp = Input::ParseElement(aScriptBuffer, KIVStart, KIVEnd, pos=0, err);
1.134 + iIV = HBufC8::NewL(ivTemp.Length());
1.135 + *iIV = ivTemp;
1.136 + Hex(*iIV);
1.137 +
1.138 + TPtrC8 outputTemp = Input::ParseElement(aScriptBuffer, KOutputStart, KOutputEnd, pos=0, err);
1.139 + iOutput = HBufC8::NewL(outputTemp.Length());
1.140 + *iOutput = outputTemp;
1.141 + Hex(*iOutput);
1.142 +
1.143 + TPtrC8 cipherType = Input::ParseElement(aScriptBuffer, KCipherTypeStart, KCipherTypeEnd, pos=0, err);
1.144 + iExpectedWeakResult = KErrNone;
1.145 + if( cipherType == _L8("DESECB") )
1.146 + {
1.147 + iCipherType = EDESECB;
1.148 + }
1.149 + else if( cipherType == _L8("DESCBC") )
1.150 + {
1.151 + iCipherType = EDESCBC;
1.152 + }
1.153 + else if( cipherType == _L8("3DESECB") )
1.154 + {
1.155 + iCipherType = E3DESECB;
1.156 + iExpectedWeakResult = KErrKeyNotWeakEnough;
1.157 + }
1.158 + else if( cipherType == _L8("3DESCBC") )
1.159 + {
1.160 + iCipherType = E3DESCBC;
1.161 + iExpectedWeakResult = KErrKeyNotWeakEnough;
1.162 + }
1.163 + else if( cipherType == _L8("AESECB") )
1.164 + {
1.165 + iCipherType = EAESECB;
1.166 + iExpectedWeakResult = KErrKeyNotWeakEnough;
1.167 + }
1.168 + else if (cipherType==_L8("AESENC_ECB") )
1.169 + {
1.170 + iCipherType = EAESMonteCarloEncryptECB;
1.171 + iExpectedWeakResult = KErrKeyNotWeakEnough;
1.172 + }
1.173 + else if (cipherType==_L8("AESDEC_ECB") )
1.174 + {
1.175 + iCipherType = EAESMonteCarloDecryptECB;
1.176 + iExpectedWeakResult = KErrKeyNotWeakEnough;
1.177 + }
1.178 + else if (cipherType==_L8("AESENC_CBC") )
1.179 + {
1.180 + iCipherType = EAESMonteCarloEncryptCBC;
1.181 + iExpectedWeakResult = KErrKeyNotWeakEnough;
1.182 + }
1.183 + else if (cipherType==_L8("AESDEC_CBC") )
1.184 + {
1.185 + iCipherType = EAESMonteCarloDecryptCBC;
1.186 + iExpectedWeakResult = KErrKeyNotWeakEnough;
1.187 + }
1.188 + else if( cipherType == _L8("RC2ECB") )
1.189 + {
1.190 + iCipherType = ERC2ECB;
1.191 + // weak enough if either aKey or aEffectiveKeyLen is weak
1.192 + TInt minKeySize = Min(iEffectiveKeyLen, BytesToBits(iKey->Size()));
1.193 + TRAP(iExpectedWeakResult, TCrypto::IsSymmetricWeakEnoughL(minKeySize));
1.194 + }
1.195 + else if( cipherType == _L8("RC2CBC") )
1.196 + {
1.197 + iCipherType = ERC2CBC;
1.198 + // weak enough if either aKey or aEffectiveKeyLen is weak
1.199 + TInt minKeySize = Min(iEffectiveKeyLen, BytesToBits(iKey->Size()));
1.200 + TRAP(iExpectedWeakResult, TCrypto::IsSymmetricWeakEnoughL(minKeySize));
1.201 + }
1.202 + else if( cipherType == _L8("RC4") )
1.203 + {
1.204 + iCipherType = ERC4;
1.205 + TRAP(iExpectedWeakResult, TCrypto::IsSymmetricWeakEnoughL(BytesToBits(iKey->Size())));
1.206 + }
1.207 + else if( cipherType == _L8("NULL") )
1.208 + {
1.209 + iCipherType = ECipherNull;
1.210 + }
1.211 + else if( cipherType == _L8("DESECB_WKT"))
1.212 + {
1.213 + iCipherType = EDESECB;
1.214 + iExpectedWeakResult = KErrWeakKey;
1.215 + }
1.216 + else if( cipherType == _L8("DESCBC_WKT"))
1.217 + {
1.218 + iCipherType = EDESCBC;
1.219 + iExpectedWeakResult = KErrWeakKey;
1.220 + }
1.221 + else
1.222 + {
1.223 + TBuf<64> cipherName(0);
1.224 + cipherName.Copy(cipherType);
1.225 + TBuf<256> formattable;
1.226 + formattable.Format(_L("\nBad Cipher Type: %S"), &cipherName);
1.227 + iConsole.Printf(formattable);
1.228 +
1.229 + iActionState = CTestAction::EPostrequisite;
1.230 + User::Leave(KErrNotSupported);
1.231 + }
1.232 +
1.233 + if ( (iCipherType==ERC2ECB) || (iCipherType==ERC2CBC) )
1.234 + {
1.235 + TPtrC8 effKeyLenTemp = Input::ParseElement(aScriptBuffer, KEffKeyLenStart, KEffKeyLenEnd, pos=0, err);
1.236 +
1.237 + if (effKeyLenTemp.Length() != 0)
1.238 + {
1.239 + TLex8 lex;
1.240 + lex.Assign(effKeyLenTemp);
1.241 + User::LeaveIfError(lex.Val(iEffectiveKeyLen));
1.242 + }
1.243 + }
1.244 +
1.245 + TPtrC8 iterationsTemp = Input::ParseElement(aScriptBuffer, KIterationsStart, KIterationsEnd, pos=0, err);
1.246 + TLex8 lex;
1.247 + lex.Assign(iterationsTemp);
1.248 + err = lex.Val(iIterationTime);
1.249 +
1.250 + TPtrC8 randTemp = Input::ParseElement(aScriptBuffer, KRandDataSizeStart, KRandDataSizeEnd, pos=0, err);
1.251 + TLex8 randlex;
1.252 + randlex.Assign(randTemp);
1.253 + err = randlex.Val(iRandDataSize);
1.254 +
1.255 + delete iBody;
1.256 + iBody = NULL;
1.257 +}
1.258 +
1.259 +void CCryptoTestAction::Hex(HBufC8& aString)
1.260 + {
1.261 + TPtr8 ptr=aString.Des();
1.262 + if (aString.Length()%2)
1.263 + {
1.264 + ptr.SetLength(0);
1.265 + return;
1.266 + }
1.267 + TInt i;
1.268 + for (i=0;i<aString.Length();i+=2)
1.269 + {
1.270 + TUint8 tmp;
1.271 + tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
1.272 + tmp*=16;
1.273 + tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
1.274 + ptr[i/2]=tmp;
1.275 + }
1.276 + ptr.SetLength(aString.Length()/2);
1.277 + }
1.278 +