crypto/weakcrypto/test/tsymmetric/cryptotestaction.cpp
changeset 0 2c201484c85f
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "cryptotestaction.h"
       
    20 #include <securityerr.h>
       
    21 #include <cryptostrength.h>
       
    22 #include "../common/inlines.h"
       
    23 #include "t_output.h"
       
    24 
       
    25 CCryptoTestAction::CCryptoTestAction(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
       
    26 :	CTestAction(aConsole, aOut), iFs(aFs), iEffectiveKeyLen(128)
       
    27 {}
       
    28 
       
    29 CCryptoTestAction::~CCryptoTestAction()
       
    30 {
       
    31 	delete iBody;
       
    32 }
       
    33 
       
    34 void CCryptoTestAction::ConstructL(const TTestActionSpec& aTestActionSpec)
       
    35 {
       
    36 	CTestAction::ConstructL(aTestActionSpec);
       
    37 	iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
       
    38 	iBody->Des().Copy(aTestActionSpec.iActionBody);	
       
    39 }
       
    40 
       
    41 void CCryptoTestAction::DoPerformPrerequisite(TRequestStatus& aStatus)
       
    42 {
       
    43 	TRequestStatus* status = &aStatus;
       
    44 	
       
    45 	TRAPD(r, DoPerformPrerequisiteL());
       
    46 	if ((r != iExpectedWeakResult) && (TCrypto::Strength() == TCrypto::EWeak))
       
    47 		{
       
    48 		r = KErrUnknown;
       
    49 		}
       
    50 	User::RequestComplete(status, r);
       
    51 	if (KErrNone==r)
       
    52 		{
       
    53 		iActionState = CTestAction::EAction;
       
    54 		}
       
    55 	else if (r==KErrKeyNotWeakEnough)
       
    56 		{
       
    57 		iResult = ETrue;
       
    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;
       
    61 		}
       
    62 	else if (r==KErrWeakKey)
       
    63 		{
       
    64 		iResult = ETrue;
       
    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;
       
    68 		}
       
    69 	else
       
    70 		{
       
    71 		iActionState = CTestAction::EPostrequisite;
       
    72 		}
       
    73 }
       
    74 
       
    75 void CCryptoTestAction::DoPerformPostrequisite(TRequestStatus& aStatus)
       
    76 {
       
    77 	TRequestStatus* status = &aStatus;
       
    78 	TRAPD(r, DoPerformPostrequisiteL());
       
    79 	delete iKey;
       
    80 	delete iInput;
       
    81 	delete iOutput;
       
    82 	delete iIV;
       
    83 	delete iEResult;
       
    84 	delete iDResult;
       
    85 
       
    86 	iFinished = ETrue;
       
    87 	User::RequestComplete(status, r);
       
    88 }
       
    89 
       
    90 void CCryptoTestAction::DoReportAction(void)
       
    91 {}
       
    92 
       
    93 void CCryptoTestAction::DoCheckResult(TInt)
       
    94 {
       
    95 	if (iResult==EFalse)
       
    96 		iConsole.Printf(_L("X"));
       
    97 	else
       
    98 		iConsole.Printf(_L("."));
       
    99 }
       
   100 
       
   101 void CCryptoTestAction::PerformAction(TRequestStatus& aStatus)
       
   102 {
       
   103 	TRequestStatus* status = &aStatus;
       
   104 
       
   105 	TRAPD(res, DoPerformActionL());
       
   106 	if (res==KErrNoMemory)
       
   107 		{
       
   108 		User::Leave(res);	//	For OOM testing
       
   109 		}
       
   110 	iActionState = CTestAction::EPostrequisite;
       
   111 	User::RequestComplete(status, res);
       
   112 }
       
   113 
       
   114 
       
   115 void CCryptoTestAction::DoInputParseL(TDesC8& aScriptBuffer)
       
   116 {
       
   117 	TInt err = KErrNone;
       
   118 	TInt pos = 0;
       
   119 
       
   120 	TPtrC8 keyTemp = Input::ParseElement(aScriptBuffer, KKeyStart, KKeyEnd, pos, err);
       
   121 	iKey = HBufC8::NewL(keyTemp.Length());
       
   122 	*iKey = keyTemp;
       
   123 	Hex(*iKey);
       
   124 
       
   125 	pos = 0;
       
   126 	TPtrC8 inputTemp = Input::ParseElement(aScriptBuffer, KInputStart, KInputEnd, pos, err);
       
   127 	iInput = HBufC8::NewL(inputTemp.Length());
       
   128 	*iInput = inputTemp;
       
   129 	Hex(*iInput);
       
   130 
       
   131 	pos = 0;
       
   132 	TPtrC8 ivTemp = Input::ParseElement(aScriptBuffer, KIVStart, KIVEnd, pos, err);
       
   133 	iIV = HBufC8::NewL(ivTemp.Length());
       
   134 	*iIV = ivTemp;
       
   135 	Hex(*iIV);
       
   136 
       
   137 	pos = 0;
       
   138 	TPtrC8 outputTemp = Input::ParseElement(aScriptBuffer, KOutputStart, KOutputEnd, pos, err);
       
   139 	iOutput = HBufC8::NewL(outputTemp.Length());
       
   140 	*iOutput = outputTemp;
       
   141 	Hex(*iOutput);
       
   142 
       
   143 	pos = 0;
       
   144 	TPtrC8 cipherType = Input::ParseElement(aScriptBuffer, KCipherTypeStart, KCipherTypeEnd, pos, err);
       
   145 	iExpectedWeakResult = KErrNone;
       
   146 	if( cipherType == _L8("DESECB") )
       
   147 	{
       
   148 		iCipherType = EDESECB;
       
   149 	}
       
   150 	else if( cipherType == _L8("DESCBC") )
       
   151 	{
       
   152 		iCipherType = EDESCBC;
       
   153 	}
       
   154 	else if( cipherType == _L8("3DESECB") )
       
   155 	{
       
   156 		iCipherType = E3DESECB;
       
   157 		iExpectedWeakResult = KErrKeyNotWeakEnough;
       
   158 	}
       
   159 	else if( cipherType == _L8("3DESCBC") )
       
   160 	{
       
   161 		iCipherType = E3DESCBC;
       
   162 		iExpectedWeakResult = KErrKeyNotWeakEnough;
       
   163 	}
       
   164 	else if( cipherType == _L8("AESECB") )
       
   165 	{
       
   166 		iCipherType = EAESECB;
       
   167 		iExpectedWeakResult = KErrKeyNotWeakEnough;
       
   168 	}
       
   169 	else if (cipherType==_L8("AESENC_ECB") )
       
   170 	{
       
   171 		iCipherType = EAESMonteCarloEncryptECB;	
       
   172 		iExpectedWeakResult = KErrKeyNotWeakEnough;
       
   173 	}
       
   174 	else if (cipherType==_L8("AESDEC_ECB") )
       
   175 	{
       
   176 		iCipherType = EAESMonteCarloDecryptECB;	
       
   177 		iExpectedWeakResult = KErrKeyNotWeakEnough;
       
   178 	}
       
   179 	else if (cipherType==_L8("AESENC_CBC") )
       
   180 	{
       
   181 		iCipherType = EAESMonteCarloEncryptCBC;	
       
   182 		iExpectedWeakResult = KErrKeyNotWeakEnough;
       
   183 	}
       
   184 	else if (cipherType==_L8("AESDEC_CBC") )
       
   185 	{
       
   186 		iCipherType = EAESMonteCarloDecryptCBC;		
       
   187 		iExpectedWeakResult = KErrKeyNotWeakEnough;
       
   188 	}
       
   189 	else if( cipherType == _L8("RC2ECB") )
       
   190 	{
       
   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));
       
   195 	}
       
   196 	else if( cipherType == _L8("RC2CBC") )
       
   197 	{
       
   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));
       
   202 	}
       
   203 	else if( cipherType == _L8("RC4") )
       
   204 	{
       
   205 		iCipherType = ERC4;
       
   206 		TRAP(iExpectedWeakResult, TCrypto::IsSymmetricWeakEnoughL(BytesToBits(iKey->Size())));
       
   207 	}
       
   208 	else if( cipherType == _L8("NULL") )
       
   209 	{
       
   210 		iCipherType = ECipherNull;
       
   211 	}
       
   212 	else if( cipherType == _L8("DESECB_WKT"))
       
   213 	{
       
   214 		iCipherType = EDESECB;	
       
   215 		iExpectedWeakResult = KErrWeakKey;
       
   216 	}
       
   217 	else if( cipherType == _L8("DESCBC_WKT"))
       
   218 	{
       
   219 		iCipherType = EDESCBC;	
       
   220 		iExpectedWeakResult = KErrWeakKey;
       
   221 	}
       
   222 	else 
       
   223 	{
       
   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);
       
   229 
       
   230 		iActionState = CTestAction::EPostrequisite;
       
   231 		User::Leave(KErrNotSupported);
       
   232 	}
       
   233 
       
   234 	if ( (iCipherType==ERC2ECB) || (iCipherType==ERC2CBC) )
       
   235 	{
       
   236 		pos = 0;
       
   237 		TPtrC8 effKeyLenTemp = Input::ParseElement(aScriptBuffer, KEffKeyLenStart, KEffKeyLenEnd, pos, err);
       
   238 
       
   239 		if (effKeyLenTemp.Length() != 0)
       
   240 		{
       
   241 			TLex8 lex;
       
   242 			lex.Assign(effKeyLenTemp);
       
   243 			User::LeaveIfError(lex.Val(iEffectiveKeyLen));
       
   244 		}
       
   245 	}
       
   246 
       
   247 	pos = 0;
       
   248 	TPtrC8 iterationsTemp = Input::ParseElement(aScriptBuffer, KIterationsStart, KIterationsEnd, pos, err);	
       
   249 	TLex8 lex;
       
   250 	lex.Assign(iterationsTemp);
       
   251 	err = lex.Val(iIterationTime);
       
   252 
       
   253 	pos = 0;
       
   254 	TPtrC8 randTemp = Input::ParseElement(aScriptBuffer, KRandDataSizeStart, KRandDataSizeEnd, pos, err);	
       
   255 	TLex8 randlex;
       
   256 	randlex.Assign(randTemp);
       
   257 	err = randlex.Val(iRandDataSize);	
       
   258 
       
   259 	delete iBody;
       
   260 	iBody = NULL;
       
   261 }
       
   262 
       
   263 void CCryptoTestAction::Hex(HBufC8& aString)
       
   264     {
       
   265     TPtr8 ptr=aString.Des();
       
   266     if (aString.Length()%2)
       
   267         {
       
   268         ptr.SetLength(0);
       
   269         return;
       
   270         }
       
   271     TInt i;
       
   272     for (i=0;i<aString.Length();i+=2)
       
   273         {
       
   274         TUint8 tmp;
       
   275         tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
       
   276         tmp*=16;
       
   277         tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
       
   278         ptr[i/2]=tmp;
       
   279         }
       
   280     ptr.SetLength(aString.Length()/2);
       
   281     }