crypto/weakcryptospi/test/tsymmetric/cryptotestaction.cpp
changeset 8 35751d3474b7
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     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=0, err);
       
   121 	iKey = HBufC8::NewL(keyTemp.Length());
       
   122 	*iKey = keyTemp;
       
   123 	Hex(*iKey);
       
   124 
       
   125 	TPtrC8 inputTemp = Input::ParseElement(aScriptBuffer, KInputStart, KInputEnd, pos=0, err);
       
   126 	iInput = HBufC8::NewL(inputTemp.Length());
       
   127 	*iInput = inputTemp;
       
   128 	Hex(*iInput);
       
   129 
       
   130 	TPtrC8 ivTemp = Input::ParseElement(aScriptBuffer, KIVStart, KIVEnd, pos=0, err);
       
   131 	iIV = HBufC8::NewL(ivTemp.Length());
       
   132 	*iIV = ivTemp;
       
   133 	Hex(*iIV);
       
   134 
       
   135 	TPtrC8 outputTemp = Input::ParseElement(aScriptBuffer, KOutputStart, KOutputEnd, pos=0, err);
       
   136 	iOutput = HBufC8::NewL(outputTemp.Length());
       
   137 	*iOutput = outputTemp;
       
   138 	Hex(*iOutput);
       
   139 
       
   140 	TPtrC8 cipherType = Input::ParseElement(aScriptBuffer, KCipherTypeStart, KCipherTypeEnd, pos=0, err);
       
   141 	iExpectedWeakResult = KErrNone;
       
   142 	if( cipherType == _L8("DESECB") )
       
   143 	{
       
   144 		iCipherType = EDESECB;
       
   145 	}
       
   146 	else if( cipherType == _L8("DESCBC") )
       
   147 	{
       
   148 		iCipherType = EDESCBC;
       
   149 	}
       
   150 	else if( cipherType == _L8("3DESECB") )
       
   151 	{
       
   152 		iCipherType = E3DESECB;
       
   153 		iExpectedWeakResult = KErrKeyNotWeakEnough;
       
   154 	}
       
   155 	else if( cipherType == _L8("3DESCBC") )
       
   156 	{
       
   157 		iCipherType = E3DESCBC;
       
   158 		iExpectedWeakResult = KErrKeyNotWeakEnough;
       
   159 	}
       
   160 	else if( cipherType == _L8("AESECB") )
       
   161 	{
       
   162 		iCipherType = EAESECB;
       
   163 		iExpectedWeakResult = KErrKeyNotWeakEnough;
       
   164 	}
       
   165 	else if (cipherType==_L8("AESENC_ECB") )
       
   166 	{
       
   167 		iCipherType = EAESMonteCarloEncryptECB;	
       
   168 		iExpectedWeakResult = KErrKeyNotWeakEnough;
       
   169 	}
       
   170 	else if (cipherType==_L8("AESDEC_ECB") )
       
   171 	{
       
   172 		iCipherType = EAESMonteCarloDecryptECB;	
       
   173 		iExpectedWeakResult = KErrKeyNotWeakEnough;
       
   174 	}
       
   175 	else if (cipherType==_L8("AESENC_CBC") )
       
   176 	{
       
   177 		iCipherType = EAESMonteCarloEncryptCBC;	
       
   178 		iExpectedWeakResult = KErrKeyNotWeakEnough;
       
   179 	}
       
   180 	else if (cipherType==_L8("AESDEC_CBC") )
       
   181 	{
       
   182 		iCipherType = EAESMonteCarloDecryptCBC;		
       
   183 		iExpectedWeakResult = KErrKeyNotWeakEnough;
       
   184 	}
       
   185 	else if( cipherType == _L8("RC2ECB") )
       
   186 	{
       
   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));
       
   191 	}
       
   192 	else if( cipherType == _L8("RC2CBC") )
       
   193 	{
       
   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));
       
   198 	}
       
   199 	else if( cipherType == _L8("RC4") )
       
   200 	{
       
   201 		iCipherType = ERC4;
       
   202 		TRAP(iExpectedWeakResult, TCrypto::IsSymmetricWeakEnoughL(BytesToBits(iKey->Size())));
       
   203 	}
       
   204 	else if( cipherType == _L8("NULL") )
       
   205 	{
       
   206 		iCipherType = ECipherNull;
       
   207 	}
       
   208  	else if( cipherType == _L8("DESECB_WKT"))
       
   209  	{
       
   210  		iCipherType = EDESECB;	
       
   211  		iExpectedWeakResult = KErrWeakKey;
       
   212  	}
       
   213  	else if( cipherType == _L8("DESCBC_WKT"))
       
   214  	{
       
   215  		iCipherType = EDESCBC;	
       
   216  		iExpectedWeakResult = KErrWeakKey;
       
   217  	}
       
   218 	else 
       
   219 	{
       
   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);
       
   225 
       
   226 		iActionState = CTestAction::EPostrequisite;
       
   227 		User::Leave(KErrNotSupported);
       
   228 	}
       
   229 
       
   230 	if ( (iCipherType==ERC2ECB) || (iCipherType==ERC2CBC) )
       
   231 	{
       
   232 		TPtrC8 effKeyLenTemp = Input::ParseElement(aScriptBuffer, KEffKeyLenStart, KEffKeyLenEnd, pos=0, err);
       
   233 
       
   234 		if (effKeyLenTemp.Length() != 0)
       
   235 		{
       
   236 			TLex8 lex;
       
   237 			lex.Assign(effKeyLenTemp);
       
   238 			User::LeaveIfError(lex.Val(iEffectiveKeyLen));
       
   239 		}
       
   240 	}
       
   241 
       
   242 	TPtrC8 iterationsTemp = Input::ParseElement(aScriptBuffer, KIterationsStart, KIterationsEnd, pos=0, err);	
       
   243 	TLex8 lex;
       
   244 	lex.Assign(iterationsTemp);
       
   245 	err = lex.Val(iIterationTime);
       
   246 
       
   247 	TPtrC8 randTemp = Input::ParseElement(aScriptBuffer, KRandDataSizeStart, KRandDataSizeEnd, pos=0, err);	
       
   248 	TLex8 randlex;
       
   249 	randlex.Assign(randTemp);
       
   250 	err = randlex.Val(iRandDataSize);	
       
   251 
       
   252 	delete iBody;
       
   253 	iBody = NULL;
       
   254 }
       
   255 
       
   256 void CCryptoTestAction::Hex(HBufC8& aString)
       
   257     {
       
   258     TPtr8 ptr=aString.Des();
       
   259     if (aString.Length()%2)
       
   260         {
       
   261         ptr.SetLength(0);
       
   262         return;
       
   263         }
       
   264     TInt i;
       
   265     for (i=0;i<aString.Length();i+=2)
       
   266         {
       
   267         TUint8 tmp;
       
   268         tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
       
   269         tmp*=16;
       
   270         tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
       
   271         ptr[i/2]=tmp;
       
   272         }
       
   273     ptr.SetLength(aString.Length()/2);
       
   274     }
       
   275