1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tsymmetric/tactionincremental.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,290 @@
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 "tactionincremental.h"
1.23 +#include "symmetric.h"
1.24 +#include "des.h"
1.25 +#include "securityerr.h"
1.26 +
1.27 +CTestAction* CActionIncremental::NewL(RFs& aFs,
1.28 + CConsoleBase& aConsole,
1.29 + Output& aOut,
1.30 + const TTestActionSpec& aTestActionSpec)
1.31 + {
1.32 + CTestAction* self = CActionIncremental::NewLC(aFs, aConsole,
1.33 + aOut, aTestActionSpec);
1.34 + CleanupStack::Pop();
1.35 + return self;
1.36 + }
1.37 +
1.38 +CTestAction* CActionIncremental::NewLC(RFs& aFs,
1.39 + CConsoleBase& aConsole,
1.40 + Output& aOut,
1.41 + const TTestActionSpec& aTestActionSpec)
1.42 + {
1.43 + CActionIncremental* self = new(ELeave) CActionIncremental(aFs, aConsole, aOut);
1.44 + CleanupStack::PushL(self);
1.45 + self->ConstructL(aTestActionSpec);
1.46 + return self;
1.47 + }
1.48 +
1.49 +CActionIncremental::~CActionIncremental()
1.50 +{
1.51 +}
1.52 +
1.53 +CActionIncremental::CActionIncremental(RFs& aFs,
1.54 + CConsoleBase& aConsole,
1.55 + Output& aOut)
1.56 +: CCryptoTestAction(aFs, aConsole, aOut)
1.57 +{}
1.58 +
1.59 +void CActionIncremental::DoPerformPostrequisiteL()
1.60 +{
1.61 + if (iEncryptor)
1.62 + {
1.63 + iEncryptor->Reset();
1.64 + delete iEncryptor;
1.65 + }
1.66 +
1.67 + if (iDecryptor)
1.68 + {
1.69 + iDecryptor->Reset();
1.70 + delete iDecryptor;
1.71 + }
1.72 +}
1.73 +
1.74 +void CActionIncremental::DoPerformPrerequisiteL()
1.75 +{
1.76 + iResult = ETrue;
1.77 + TInt err = KErrNone;
1.78 + TInt pos = 0;
1.79 + TPtrC8 incremental = Input::ParseElement(*iBody, KIncrementalStart, KIncrementalEnd, pos, err);
1.80 +
1.81 + DoInputParseL(incremental);
1.82 +
1.83 + CBlockTransformation* encryptor = NULL;
1.84 + CBlockTransformation* decryptor = NULL;
1.85 +
1.86 + iEncryptor =0;
1.87 + iDecryptor =0;
1.88 +
1.89 + switch (iCipherType)
1.90 + {
1.91 + case (EDESECB):
1.92 + {
1.93 + //If the test is weak key test
1.94 + if(iExpectedWeakResult == KErrWeakKey)
1.95 + {
1.96 + //we expect to leave with KErrWeakKey reason
1.97 + if(CDES::IsWeakKey(iKey->Des()))
1.98 + {
1.99 + User::Leave(KErrWeakKey);
1.100 + }
1.101 + else //test is unsuccessful, leave with a different reason
1.102 + {
1.103 + User::Leave(KErrGeneral);
1.104 + }
1.105 + }
1.106 + encryptor = CDESEncryptor::NewLC(iKey->Des());
1.107 + decryptor = CDESDecryptor::NewL(iKey->Des());
1.108 + CleanupStack::Pop(encryptor);
1.109 + }
1.110 + break;
1.111 + case (EDESCBC):
1.112 + {
1.113 + CBlockTransformation* desEncryptor = NULL;
1.114 + CBlockTransformation* desDecryptor = NULL;
1.115 +
1.116 + if(iExpectedWeakResult == KErrWeakKey)
1.117 + {
1.118 + if(CDES::IsWeakKey(iKey->Des()))
1.119 + {
1.120 + User::Leave(KErrWeakKey);
1.121 + }
1.122 + else
1.123 + {
1.124 + User::Leave(KErrGeneral);
1.125 + }
1.126 + }
1.127 +
1.128 + desEncryptor = CDESEncryptor::NewLC(iKey->Des());
1.129 + desDecryptor = CDESDecryptor::NewLC(iKey->Des());
1.130 +
1.131 + encryptor = CModeCBCEncryptor::NewL(desEncryptor, iIV->Des());
1.132 + CleanupStack::PushL(encryptor);
1.133 + decryptor = CModeCBCDecryptor::NewL(desDecryptor, iIV->Des());
1.134 + CleanupStack::Pop(3);
1.135 + }
1.136 + break;
1.137 + case (E3DESECB):
1.138 + {
1.139 + encryptor = C3DESEncryptor::NewLC(iKey->Des());
1.140 + decryptor = C3DESDecryptor::NewL(iKey->Des());
1.141 + CleanupStack::Pop(encryptor);
1.142 + }
1.143 + break;
1.144 + case (E3DESCBC):
1.145 + {
1.146 + CBlockTransformation* the3DESencryptor = NULL;
1.147 + CBlockTransformation* the3DESdecryptor = NULL;
1.148 +
1.149 + the3DESencryptor = C3DESEncryptor::NewLC(iKey->Des());
1.150 + the3DESdecryptor = C3DESDecryptor::NewLC(iKey->Des());
1.151 +
1.152 + encryptor = CModeCBCEncryptor::NewL(the3DESencryptor, iIV->Des());
1.153 + CleanupStack::PushL(encryptor);
1.154 + decryptor = CModeCBCDecryptor::NewL(the3DESdecryptor, iIV->Des());
1.155 + CleanupStack::Pop(3);
1.156 + }
1.157 + break;
1.158 + case (EAESECB):
1.159 + {
1.160 + encryptor = CAESEncryptor::NewLC(iKey->Des());
1.161 + decryptor = CAESDecryptor::NewL(iKey->Des());
1.162 + CleanupStack::Pop(encryptor);
1.163 + }
1.164 + break;
1.165 + case (ERC2ECB):
1.166 + {
1.167 + encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
1.168 + decryptor = CRC2Decryptor::NewL(iKey->Des(), iEffectiveKeyLen);
1.169 + CleanupStack::Pop(encryptor);
1.170 + }
1.171 + break;
1.172 + case (ERC2CBC):
1.173 + {
1.174 + encryptor = CRC2Encryptor::NewLC(iKey->Des(), iEffectiveKeyLen);
1.175 + decryptor = CRC2Decryptor::NewL(iKey->Des(), iEffectiveKeyLen);
1.176 + CleanupStack::Pop(encryptor);
1.177 + }
1.178 + break;
1.179 + case (ERC4):
1.180 + {
1.181 + iEncryptor = CARC4::NewL(*iKey, 0);
1.182 + iDecryptor = CARC4::NewL(*iKey, 0);
1.183 + }
1.184 + break;
1.185 + case (ECipherNull):
1.186 + {
1.187 + iEncryptor = CNullCipher::NewL();
1.188 + iDecryptor = CNullCipher::NewL();
1.189 + }
1.190 + break;
1.191 + default:
1.192 + ASSERT(0);
1.193 + User::Leave(KErrNotSupported);
1.194 + }
1.195 +
1.196 + CleanupStack::PushL(encryptor);
1.197 + CleanupStack::PushL(decryptor);
1.198 + if(!iEncryptor && !iDecryptor)
1.199 + {
1.200 + CPaddingSSLv3* dPadding = CPaddingSSLv3::NewLC(decryptor->BlockSize());
1.201 + CPaddingSSLv3* ePadding = CPaddingSSLv3::NewLC(encryptor->BlockSize());
1.202 + iEncryptor = CBufferedEncryptor::NewL(encryptor, ePadding);
1.203 + CleanupStack::Pop(ePadding);
1.204 + iDecryptor = CBufferedDecryptor::NewL(decryptor, dPadding);
1.205 + CleanupStack::Pop(dPadding);
1.206 +
1.207 + TInt desEBlockSize = encryptor->BlockSize();
1.208 + TInt desDBlockSize = decryptor->BlockSize();
1.209 + TInt bufEBlockSize = iEncryptor->BlockSize();
1.210 + TInt bufDBlockSize = iDecryptor->BlockSize();
1.211 + if ((desEBlockSize/desDBlockSize) != (bufEBlockSize/bufDBlockSize))
1.212 + {
1.213 + iResult = EFalse;
1.214 + }
1.215 +
1.216 + TInt desEKeySize = encryptor->KeySize();
1.217 + TInt desDKeySize = decryptor->KeySize();
1.218 + if (desEKeySize != desDKeySize)
1.219 + {
1.220 + iResult = EFalse;
1.221 + }
1.222 +
1.223 + encryptor->Reset();
1.224 + decryptor->Reset();
1.225 + }
1.226 +
1.227 + TInt encryptorKeySize = iEncryptor->KeySize();
1.228 + TInt decryptorKeySize = iDecryptor->KeySize();
1.229 + if (encryptorKeySize != decryptorKeySize)
1.230 + {
1.231 + iResult = EFalse;
1.232 + }
1.233 +
1.234 + CleanupStack::Pop(2, encryptor);
1.235 +
1.236 + iEResult = HBufC8::NewMaxL(iEncryptor->MaxFinalOutputLength(iInput->Length()));
1.237 + iDResult = HBufC8::NewMaxL(iDecryptor->MaxFinalOutputLength(iEResult->Size()));
1.238 +}
1.239 +
1.240 +void CActionIncremental::DoPerformActionL()
1.241 + {
1.242 + TRAPD(res, DoDoPerformActionL())
1.243 + if(res == KErrNoMemory)
1.244 + {
1.245 + iEncryptor->Reset();
1.246 + iDecryptor->Reset();
1.247 + }
1.248 + }
1.249 +
1.250 +void CActionIncremental::DoDoPerformActionL()
1.251 +{
1.252 + __UHEAP_MARK;
1.253 + TPtr8 eResultActual = iEResult->Des();
1.254 + eResultActual.FillZ(eResultActual.MaxLength());
1.255 + eResultActual.SetLength(0);
1.256 +
1.257 + TPtr8 dResultActual = iDResult->Des();
1.258 + dResultActual.FillZ(dResultActual.MaxLength());
1.259 + dResultActual.SetLength(0);
1.260 +
1.261 + TInt len = iInput->Length();
1.262 + for(TInt i=1; i<len; i++)
1.263 + {
1.264 + TInt j = 0;
1.265 + for(; j+i<len; j+=i)
1.266 + {// encryption in blocks of size i
1.267 + iEncryptor->Process(iInput->Mid(j,i), eResultActual);
1.268 + }
1.269 +
1.270 + iEncryptor->ProcessFinalL(iInput->Mid(j), eResultActual);
1.271 +
1.272 + j=0;
1.273 + for(; (j+(len-i))<len; j+=(len-i))
1.274 + {// Decryption in blocks of size (len - i)
1.275 + iDecryptor->Process(eResultActual.Mid(j, len-i), dResultActual);
1.276 + }
1.277 +
1.278 + iDecryptor->ProcessFinalL(eResultActual.Mid(j), dResultActual);
1.279 +
1.280 + if(dResultActual != *iInput)
1.281 + {
1.282 + iResult = EFalse;
1.283 + }
1.284 +
1.285 + eResultActual.FillZ(eResultActual.MaxLength());
1.286 + dResultActual.FillZ(dResultActual.MaxLength());
1.287 + eResultActual.SetLength(0);
1.288 + dResultActual.SetLength(0);
1.289 + }
1.290 + __UHEAP_MARKEND;
1.291 +
1.292 +}
1.293 +