1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tsymmetric/tactionmontecarlo.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,261 @@
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 "tactionmontecarlo.h"
1.23 +#include "bufferedtransformation.h"
1.24 +#include "rijndael.h"
1.25 +#include "cbcmode.h"
1.26 +#include "padding.h"
1.27 +
1.28 +const TInt KAESBlockSizeBytes = 16; // 128 bits
1.29 +
1.30 +CTestAction* CActionMonteCarlo::NewL(RFs& aFs,
1.31 + CConsoleBase& aConsole,
1.32 + Output& aOut,
1.33 + const TTestActionSpec& aTestActionSpec)
1.34 + {
1.35 + CTestAction* self = CActionMonteCarlo::NewLC(aFs, aConsole,
1.36 + aOut, aTestActionSpec);
1.37 + CleanupStack::Pop();
1.38 + return self;
1.39 + }
1.40 +
1.41 +CTestAction* CActionMonteCarlo::NewLC(RFs& aFs,
1.42 + CConsoleBase& aConsole,
1.43 + Output& aOut,
1.44 + const TTestActionSpec& aTestActionSpec)
1.45 + {
1.46 + CActionMonteCarlo* self = new(ELeave) CActionMonteCarlo(aFs, aConsole, aOut);
1.47 + CleanupStack::PushL(self);
1.48 + self->ConstructL(aTestActionSpec);
1.49 + return self;
1.50 + }
1.51 +
1.52 +CActionMonteCarlo::~CActionMonteCarlo()
1.53 +{
1.54 + delete iEncrypt;
1.55 + delete iDecrypt;
1.56 +}
1.57 +
1.58 +CActionMonteCarlo::CActionMonteCarlo(RFs& aFs,
1.59 + CConsoleBase& aConsole,
1.60 + Output& aOut)
1.61 +
1.62 +: CCryptoTestAction(aFs, aConsole, aOut)
1.63 +{}
1.64 +
1.65 +
1.66 +void CActionMonteCarlo::DoPerformPrerequisiteL()
1.67 +{
1.68 + TInt err = KErrNone;
1.69 + TInt pos = 0;
1.70 + TPtrC8 monteCarlo = Input::ParseElement(*iBody, KMonteCarloStart, KMonteCarloEnd, pos, err);
1.71 +
1.72 + DoInputParseL(monteCarlo);
1.73 +
1.74 + CBlockTransformation* encryptor = NULL;
1.75 + CBlockTransformation* decryptor = NULL;
1.76 +
1.77 + switch (iCipherType)
1.78 + {
1.79 + case (EAESMonteCarloEncryptECB):
1.80 + {
1.81 + encryptor = CAESEncryptor::NewLC(iKey->Des());
1.82 + }
1.83 + break;
1.84 + case (EAESMonteCarloDecryptECB):
1.85 + {
1.86 + decryptor = CAESDecryptor::NewLC(iKey->Des());
1.87 + }
1.88 + break;
1.89 + case (EAESMonteCarloEncryptCBC):
1.90 + {
1.91 + CBlockTransformation* aesEncryptor = NULL;
1.92 + aesEncryptor = CAESEncryptor::NewLC(iKey->Des());
1.93 +
1.94 + encryptor = CModeCBCEncryptor::NewL(aesEncryptor, iIV->Des());
1.95 + CleanupStack::Pop(aesEncryptor);
1.96 + CleanupStack::PushL(encryptor);
1.97 + }
1.98 + break;
1.99 + case (EAESMonteCarloDecryptCBC):
1.100 + {
1.101 + CBlockTransformation* aesDecryptor = NULL;
1.102 + aesDecryptor = CAESDecryptor::NewLC(iKey->Des());
1.103 +
1.104 + decryptor = CModeCBCDecryptor::NewL(aesDecryptor, iIV->Des());
1.105 + CleanupStack::Pop(aesDecryptor);
1.106 + CleanupStack::PushL(decryptor);
1.107 + }
1.108 + break;
1.109 + default:
1.110 + {
1.111 + ASSERT(0);
1.112 + User::Leave(KErrNotSupported);
1.113 + }
1.114 + }
1.115 +
1.116 +
1.117 + CPaddingSSLv3* padding = 0;
1.118 + if (encryptor)
1.119 + {
1.120 + padding = CPaddingSSLv3::NewLC(encryptor->BlockSize());
1.121 + iEncrypt = CBufferedEncryptor::NewL(encryptor, padding);
1.122 + iEResult = HBufC8::NewMaxL(iEncrypt->MaxOutputLength(iInput->Length()));
1.123 + }
1.124 + else if (decryptor)
1.125 + {
1.126 + padding = CPaddingSSLv3::NewLC(decryptor->BlockSize());
1.127 + iDecrypt = CBufferedDecryptor::NewL(decryptor, padding);
1.128 + iDResult = HBufC8::NewMaxL(iDecrypt->MaxOutputLength(iInput->Size()));
1.129 + }
1.130 +
1.131 + CleanupStack::Pop(2); // padding, encryptor/decryptor
1.132 +
1.133 +}
1.134 +
1.135 +
1.136 +void CActionMonteCarlo::DoPerformActionL()
1.137 +{
1.138 + iResult = EFalse;
1.139 +
1.140 + __ASSERT_DEBUG(iInput->Size()==KAESBlockSizeBytes, User::Panic(_L("tsymmetric"), KErrNotSupported));
1.141 +
1.142 + if (iCipherType==EAESMonteCarloEncryptECB)
1.143 + DoAESEncryptECB();
1.144 + else if (iCipherType==EAESMonteCarloDecryptECB)
1.145 + DoAESDecryptECB();
1.146 + else if (iCipherType==EAESMonteCarloEncryptCBC)
1.147 + DoAESEncryptCBC();
1.148 + else if (iCipherType==EAESMonteCarloDecryptCBC)
1.149 + DoAESDecryptCBC();
1.150 + else
1.151 + User::Leave(KErrNotSupported);
1.152 +}
1.153 +
1.154 +void CActionMonteCarlo::DoAESEncryptECB()
1.155 +{
1.156 + TPtr8 theEncryptResult(iEResult->Des());
1.157 + theEncryptResult.FillZ(theEncryptResult.MaxLength());
1.158 + theEncryptResult.SetLength(0);
1.159 +
1.160 + TInt index = 0;
1.161 + TPtr8 theInput(iInput->Des());
1.162 + for (; index < KMonteCarloIterations; index++)
1.163 + {
1.164 + iEncrypt->Process(theInput, theEncryptResult);
1.165 + theInput.Copy(theEncryptResult);
1.166 + theEncryptResult.FillZ(theEncryptResult.MaxLength());
1.167 + theEncryptResult.SetLength(0);
1.168 + }
1.169 +
1.170 + if (*iOutput==*iEResult)
1.171 + {
1.172 + iResult = ETrue;
1.173 + }
1.174 +}
1.175 +
1.176 +void CActionMonteCarlo::DoAESDecryptECB()
1.177 +{
1.178 + TPtr8 theDecryptResult(iDResult->Des());
1.179 + theDecryptResult.FillZ(theDecryptResult.MaxLength());
1.180 + theDecryptResult.SetLength(0);
1.181 +
1.182 + TInt index = 0;
1.183 + TPtr8 theInput(iInput->Des());
1.184 + for (; index < KMonteCarloIterations; index++)
1.185 + {
1.186 + iDecrypt->Process(theInput, theDecryptResult);
1.187 + theInput.Copy(theDecryptResult);
1.188 + theDecryptResult.FillZ(theDecryptResult.MaxLength());
1.189 + theDecryptResult.SetLength(0);
1.190 + }
1.191 +
1.192 + if (*iOutput==*iInput)
1.193 + {
1.194 + iResult = ETrue;
1.195 + }
1.196 +}
1.197 +
1.198 +void CActionMonteCarlo::DoAESEncryptCBC()
1.199 + {
1.200 + TPtr8 theEncryptResult(iEResult->Des());
1.201 + theEncryptResult.FillZ(theEncryptResult.MaxLength());
1.202 + theEncryptResult.SetLength(0);
1.203 +
1.204 + TInt index = 0;
1.205 + TPtr8 theInput(iInput->Des());
1.206 +
1.207 + TBuf8<KAESBlockSizeBytes> nextBuf;
1.208 + nextBuf.FillZ(KAESBlockSizeBytes);
1.209 +
1.210 + for (; index < KMonteCarloIterations-1; index++)
1.211 + {
1.212 + iEncrypt->Process(theInput, theEncryptResult);
1.213 +
1.214 + if (index==0)
1.215 + theInput.Copy(*iIV); // First loop, use the original IV as next PT block
1.216 + else
1.217 + theInput.Copy(nextBuf); // Use previous CT block as next PT block
1.218 +
1.219 + // Save CT block for next loop when it'll become the PT block
1.220 + nextBuf.Copy(theEncryptResult);
1.221 + // Reset for next encryption
1.222 + theEncryptResult.FillZ(theEncryptResult.MaxLength());
1.223 + theEncryptResult.SetLength(0);
1.224 + }
1.225 +
1.226 + iEncrypt->Process(theInput, theEncryptResult);
1.227 +
1.228 + if (theEncryptResult.Compare(*iOutput)==KErrNone)
1.229 + {
1.230 + iResult = ETrue;
1.231 + }
1.232 +
1.233 + }
1.234 +
1.235 +void CActionMonteCarlo::DoAESDecryptCBC()
1.236 + {
1.237 + TPtr8 theDecryptResult(iDResult->Des());
1.238 + theDecryptResult.FillZ(theDecryptResult.MaxLength());
1.239 + theDecryptResult.SetLength(0);
1.240 +
1.241 + TInt index = 0;
1.242 + TPtr8 theInput(iInput->Des());
1.243 +
1.244 + for (; index < KMonteCarloIterations-1; index++)
1.245 + {
1.246 + iDecrypt->Process(theInput, theDecryptResult);
1.247 +
1.248 + // Use previous PT block as next CT block
1.249 + theInput.Copy(theDecryptResult);
1.250 +
1.251 + // Reset for next decryption
1.252 + theDecryptResult.FillZ(theDecryptResult.MaxLength());
1.253 + theDecryptResult.SetLength(0);
1.254 + }
1.255 +
1.256 + // Last loop
1.257 + iDecrypt->Process(theInput, theDecryptResult);
1.258 +
1.259 + if (theDecryptResult.Compare(*iOutput)==KErrNone)
1.260 + {
1.261 + iResult = ETrue;
1.262 + }
1.263 +
1.264 + }