1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tasymmetric/trsaencryptfb.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,402 @@
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 "trsaencryptfb.h"
1.23 +#include "t_input.h"
1.24 +#include <asymmetric.h>
1.25 +#include <padding.h>
1.26 +#include <bigint.h>
1.27 +#include "performancetest.h"
1.28 +#include <securityerr.h>
1.29 +
1.30 +_LIT8(KInputStart, "<input>");
1.31 +_LIT8(KInputEnd, "</input>");
1.32 +_LIT8(KKeyBitsStart, "<keybits>");
1.33 +_LIT8(KKeyBitsEnd, "</keybits>");
1.34 +
1.35 +CRSAEncryptFB::~CRSAEncryptFB()
1.36 + {
1.37 + delete iBody;
1.38 + }
1.39 +
1.40 +CRSAEncryptFB::CRSAEncryptFB(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.41 + : CTestAction(aConsole, aOut), iFs(aFs), iKeyBits(512) // Default key size = 512
1.42 + {
1.43 + }
1.44 +
1.45 +CTestAction* CRSAEncryptFB::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
1.46 + const TTestActionSpec& aTestActionSpec)
1.47 + {
1.48 + CTestAction* self = CRSAEncryptFB::NewLC(aFs, aConsole,
1.49 + aOut, aTestActionSpec);
1.50 + CleanupStack::Pop();
1.51 + return self;
1.52 + }
1.53 +
1.54 +CTestAction* CRSAEncryptFB::NewLC(RFs& aFs, CConsoleBase& aConsole,Output& aOut,
1.55 + const TTestActionSpec& aTestActionSpec)
1.56 + {
1.57 + CRSAEncryptFB* self = new(ELeave) CRSAEncryptFB(aFs, aConsole, aOut);
1.58 + CleanupStack::PushL(self);
1.59 + self->ConstructL(aTestActionSpec);
1.60 + return self;
1.61 + }
1.62 +
1.63 +void CRSAEncryptFB::ConstructL(const TTestActionSpec& aTestActionSpec)
1.64 +{
1.65 + CTestAction::ConstructL(aTestActionSpec);
1.66 + iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
1.67 + iBody->Des().Copy(aTestActionSpec.iActionBody);
1.68 +
1.69 + if (CPerformance::PerformanceTester()->IsTestingPerformance())
1.70 + {// Number of test iterations
1.71 + TPtrC8 itsPtr = Input::ParseElement(aTestActionSpec.iActionBody, KIterationsStart, KIterationsEnd);
1.72 + if (itsPtr!=KNullDesC8)
1.73 + {
1.74 + TLex8 lex;
1.75 + lex.Assign(itsPtr);
1.76 + User::LeaveIfError(lex.Val(iPerfTestIterations));
1.77 + if (iPerfTestIterations > KMaxIterations)
1.78 + User::Panic(_L("AsymmetricPerformance.exe"), KErrArgument);
1.79 + }
1.80 + TPtrC8 cryptoPtr = Input::ParseElement(aTestActionSpec.iActionBody, KTypeOfCryptoStart, KTypeOfCryptoEnd);
1.81 + if (cryptoPtr.CompareF(KRSAStandard) == 0)
1.82 + {
1.83 + iCryptoType = EStandard;
1.84 + }
1.85 + else if (cryptoPtr.CompareF(KRSAStandardCRT) == 0)
1.86 + {
1.87 + iCryptoType = EStandardCRT;
1.88 + }
1.89 + else
1.90 + {
1.91 + User::Panic(_L("AsymmetricPerformance.exe"), KErrArgument);
1.92 + }
1.93 + }
1.94 +
1.95 + TPtrC8 keyBitsPtr = Input::ParseElement(*iBody, KKeyBitsStart, KKeyBitsEnd);
1.96 + if (keyBitsPtr!=KNullDesC8)
1.97 + {
1.98 + TLex8 lex;
1.99 + lex.Assign(keyBitsPtr);
1.100 + User::LeaveIfError(lex.Val(iKeyBits));
1.101 + }
1.102 +}
1.103 +
1.104 +void CRSAEncryptFB::DoPerformPrerequisite(TRequestStatus& aStatus)
1.105 + {
1.106 + TRequestStatus* status = &aStatus;
1.107 +
1.108 + TPtrC8 inputTemp = Input::ParseElement(*iBody, KInputStart, KInputEnd);
1.109 + iInput = HBufC8::NewL(inputTemp.Length());
1.110 + *iInput = inputTemp;
1.111 + Hex(*iInput);
1.112 +
1.113 + User::RequestComplete(status, KErrNone);
1.114 + iActionState = CTestAction::EAction;
1.115 + }
1.116 +
1.117 +void CRSAEncryptFB::DoPerformPostrequisite(TRequestStatus& aStatus)
1.118 + {
1.119 + TRequestStatus* status = &aStatus;
1.120 + delete iInput;
1.121 +
1.122 + iFinished = ETrue;
1.123 + User::RequestComplete(status, iActionResult);
1.124 + }
1.125 +
1.126 +void CRSAEncryptFB::DoReportAction(void)
1.127 + {
1.128 + }
1.129 +
1.130 +void CRSAEncryptFB::DoCheckResult(TInt)
1.131 + {
1.132 + if (iResult)
1.133 + iConsole.Printf(_L("."));
1.134 + else
1.135 + iConsole.Printf(_L("X"));
1.136 + }
1.137 +
1.138 +void CRSAEncryptFB::Hex(HBufC8& aString)
1.139 + {
1.140 + TPtr8 ptr=aString.Des();
1.141 + if (aString.Length()%2)
1.142 + {
1.143 + ptr.SetLength(0);
1.144 + return;
1.145 + }
1.146 + TInt i;
1.147 + for (i=0;i<aString.Length();i+=2)
1.148 + {
1.149 + TUint8 tmp;
1.150 + tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
1.151 + tmp*=16;
1.152 + tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
1.153 + ptr[i/2]=tmp;
1.154 + }
1.155 + ptr.SetLength(aString.Length()/2);
1.156 + }
1.157 +
1.158 +
1.159 +void CRSAEncryptFB::PerformAction(TRequestStatus& aStatus)
1.160 +{
1.161 + TRequestStatus* status = &aStatus;
1.162 +
1.163 + if (CPerformance::PerformanceTester()->IsTestingPerformance())
1.164 + {
1.165 + iConsole.Printf(_L(">")); // Indicates start of test
1.166 + TRAP(iActionResult, DoPerformanceTestActionL());
1.167 + }
1.168 + else
1.169 + {
1.170 + TRAP(iActionResult, DoPerformActionL());
1.171 + }
1.172 +
1.173 + if (iActionResult==KErrNoMemory)
1.174 + {
1.175 + User::Leave(iActionResult); // For OOM testing
1.176 + }
1.177 + if (iActionResult==KErrKeyNotWeakEnough)
1.178 + {
1.179 + iResult = ETrue;
1.180 + iConsole.Printf(_L("Crypto libraries returned KErrKeyNotWeakEnough! Passing test automatically.\n\r"));
1.181 + iOut.writeString(_L("Crypto libraries returned KErrKeyNotWeakEnough! Passing test automatically.\r\n"));
1.182 + }
1.183 +
1.184 + User::RequestComplete(status, iActionResult);
1.185 +
1.186 + iActionState = CTestAction::EPostrequisite;
1.187 +}
1.188 +
1.189 +void CRSAEncryptFB::DoPerformanceTestActionL()
1.190 +{
1.191 + __UHEAP_MARK;
1.192 + iResult = EFalse;
1.193 +
1.194 + TTimeIntervalMicroSeconds keyCreateTime(0);
1.195 + TTimeIntervalMicroSeconds encryptTime(0);
1.196 + TTimeIntervalMicroSeconds decryptTime(0);
1.197 + TTime start, end;
1.198 + TTimeIntervalSeconds diff(0);
1.199 + const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
1.200 +
1.201 +// Time key pair creation
1.202 + CRSAKeyPair *rsaPair = NULL;
1.203 + TInt noKeyPairCreations = 0;
1.204 +
1.205 + start.UniversalTime();
1.206 + while (diff < KIterationTime)
1.207 + {
1.208 + rsaPair = CRSAKeyPair::NewLC(iKeyBits, TypeOfCrypto());
1.209 + CleanupStack::PopAndDestroy();
1.210 + noKeyPairCreations++;
1.211 + end.UniversalTime();
1.212 + end.SecondsFrom(start, diff);
1.213 + }
1.214 + end.UniversalTime();
1.215 + keyCreateTime = end.MicroSecondsFrom(start);
1.216 + TReal keycreatetime = I64REAL(keyCreateTime.Int64());
1.217 +
1.218 + rsaPair = CRSAKeyPair::NewLC(iKeyBits, TypeOfCrypto()); // Create one keypair for operations
1.219 +
1.220 + CRSAPKCS1v15Encryptor* encryptor = CRSAPKCS1v15Encryptor::NewL(rsaPair->PublicKey());
1.221 + CleanupStack::PushL(encryptor);
1.222 +
1.223 + CRSAPKCS1v15Decryptor* decryptor = CRSAPKCS1v15Decryptor::NewL(rsaPair->PrivateKey());
1.224 + CleanupStack::PushL(decryptor);
1.225 +
1.226 + HBufC8 *eResult = HBufC8::NewLC(encryptor->MaxOutputLength());
1.227 + TPtr8 ePtr = eResult->Des();
1.228 + TPtr8* eResultPtr = &ePtr;
1.229 +
1.230 +
1.231 +// Time encryption
1.232 + diff = 0;
1.233 + TInt noEncryptions = 0;
1.234 + start.UniversalTime();
1.235 + while (diff < KIterationTime)
1.236 + {
1.237 + eResultPtr->Zero();
1.238 + encryptor->EncryptL(*iInput, *eResultPtr);
1.239 + noEncryptions++;
1.240 + end.UniversalTime();
1.241 + end.SecondsFrom(start, diff);
1.242 + }
1.243 + end.UniversalTime();
1.244 + encryptTime = end.MicroSecondsFrom(start);
1.245 + TReal encrypttime = I64REAL(encryptTime.Int64());
1.246 +
1.247 +// Time decryption
1.248 + diff = 0;
1.249 + HBufC8 *dResult = HBufC8::NewLC(decryptor->MaxOutputLength());
1.250 + ePtr = eResult->Des();
1.251 + eResultPtr = &ePtr;
1.252 + TPtr8 dPtr = dResult->Des();
1.253 + TPtr8* dResultPtr = &dPtr;
1.254 + TInt noDecryptions = 0;
1.255 + start.UniversalTime();
1.256 + while (diff < KIterationTime)
1.257 + {
1.258 + decryptor->DecryptL(*eResultPtr, *dResultPtr);
1.259 + noDecryptions++;
1.260 + end.UniversalTime();
1.261 + end.SecondsFrom(start, diff);
1.262 + }
1.263 + end.UniversalTime();
1.264 + decryptTime = end.MicroSecondsFrom(start);
1.265 + TReal decrypttime = I64REAL(decryptTime.Int64());
1.266 +
1.267 + iResult = ETrue;
1.268 + if (*iInput != *dResult)
1.269 + {
1.270 + iResult = EFalse;
1.271 + }
1.272 +
1.273 + CleanupStack::PopAndDestroy(5); // dResult,eResult,decryptor,encryptor,rsaPair
1.274 + __UHEAP_MARKEND;
1.275 +
1.276 + if (iResult)
1.277 + {
1.278 + TReal rate = I64REAL(keyCreateTime.Int64()) / noKeyPairCreations;
1.279 + TBuf<256> buf;
1.280 + _LIT(KKeyCreateTime, "\tKeyCreate\t%f");
1.281 + buf.Format(KKeyCreateTime, rate);
1.282 + iOut.writeString(buf);
1.283 +
1.284 + rate = I64REAL(encryptTime.Int64()) / noEncryptions;
1.285 + _LIT(KEncryptTime, "\tEncrypt\t%f");
1.286 + buf.Format(KEncryptTime, rate);
1.287 + iOut.writeString(buf);
1.288 +
1.289 + rate = I64REAL(decryptTime.Int64()) / noDecryptions;
1.290 + _LIT(KDecryptTime, "\tDecrypt\t%f");
1.291 + buf.Format(KDecryptTime, rate);
1.292 + iOut.writeString(buf);
1.293 + }
1.294 + else
1.295 + {
1.296 + _LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
1.297 + iOut.writeString(KNoTimingInfo);
1.298 + }
1.299 +}
1.300 +
1.301 +void CRSAEncryptFB::DoPerformActionL()
1.302 + {
1.303 + __UHEAP_MARK;
1.304 +
1.305 + iResult = EFalse;
1.306 +
1.307 + CRSAKeyPair* rsaPair = CRSAKeyPair::NewLC(iKeyBits, TypeOfCrypto());
1.308 + //LogKeyData(static_cast<const CRSAPublicKey*>(&rsaPair->PublicKey()), static_cast<const CRSAPrivateKeyCRT*>(&rsaPair->PrivateKey()), KNullDesC8);
1.309 +
1.310 + CRSAPKCS1v15Encryptor* encryptor = CRSAPKCS1v15Encryptor::NewL(rsaPair->PublicKey());
1.311 + CleanupStack::PushL(encryptor);
1.312 +
1.313 + CRSAPKCS1v15Decryptor* decryptor = CRSAPKCS1v15Decryptor::NewL(rsaPair->PrivateKey());
1.314 + CleanupStack::PushL(decryptor);
1.315 +
1.316 + HBufC8* eResult = HBufC8::NewLC(encryptor->MaxOutputLength());
1.317 + HBufC8* dResult = HBufC8::NewLC(decryptor->MaxOutputLength());
1.318 +
1.319 + TPtr8 eResultPtr = eResult->Des();
1.320 + encryptor->EncryptL(*iInput, eResultPtr);
1.321 +
1.322 + TPtr8 dResultPtr = dResult->Des();
1.323 + if (TypeOfCrypto() == EStandardCRT)
1.324 + {
1.325 + TRAPD(res, decryptor->DecryptL(eResultPtr, dResultPtr));
1.326 + if (res==KErrCorrupt) // Log all the key data for this failure
1.327 + LogKeyData(static_cast<const CRSAPublicKey*>(&rsaPair->PublicKey()), static_cast<const CRSAPrivateKeyCRT*>(&rsaPair->PrivateKey()), *eResult);
1.328 +
1.329 + User::LeaveIfError(res);
1.330 + }
1.331 + else
1.332 + {
1.333 + decryptor->DecryptL(eResultPtr, dResultPtr);
1.334 + }
1.335 +
1.336 + if(*iInput == *dResult)
1.337 + {
1.338 + iResult = ETrue;
1.339 + }
1.340 +
1.341 + // Check the input and output lenghts.
1.342 + if ((encryptor->MaxInputLength() == decryptor->MaxOutputLength()) && (encryptor->MaxOutputLength() == decryptor->MaxInputLength()))
1.343 + {
1.344 + iResult = ETrue;
1.345 + }
1.346 + else
1.347 + {
1.348 + iResult = EFalse;
1.349 + iOut.writeString(_L("\n\t RSA PKCS1 v15 Encryptor-Decryptor Max Input/Output length mismatch. \n\r"));
1.350 + }
1.351 +
1.352 + CleanupStack::PopAndDestroy(dResult);
1.353 + CleanupStack::PopAndDestroy(eResult);
1.354 + CleanupStack::PopAndDestroy(decryptor);
1.355 + CleanupStack::PopAndDestroy(encryptor);
1.356 + CleanupStack::PopAndDestroy(rsaPair);
1.357 +
1.358 + __UHEAP_MARKEND;
1.359 + }
1.360 +
1.361 +void CRSAEncryptFB::LogKeyData(const CRSAPublicKey* aPublicKey, const CRSAPrivateKeyCRT* aPrivateKeyCRT, const TDesC8& aCipherText)
1.362 +{
1.363 + iOut.writeString(_L("\n\t ----------- e -----------\n\r"));
1.364 + const TInteger& keyE = aPublicKey->E();
1.365 + HBufC8* theE = keyE.BufferLC();
1.366 + iOut.writeOctetString(*theE);
1.367 +
1.368 + iOut.writeString(_L("\n\t ----------- P -----------\n\r"));
1.369 + const TInteger& keyP = aPrivateKeyCRT->P();
1.370 + HBufC8* theP = keyP.BufferLC();
1.371 + iOut.writeOctetString(*theP);
1.372 +
1.373 + iOut.writeString(_L("\n\t ----------- Q -----------\n\r"));
1.374 + const TInteger& keyQ = aPrivateKeyCRT->Q();
1.375 + HBufC8* theQ = keyQ.BufferLC();
1.376 + iOut.writeOctetString(*theQ);
1.377 +
1.378 + iOut.writeString(_L("\n\t ----------- DP -----------\n\r"));
1.379 + const TInteger& keyDP = aPrivateKeyCRT->DP();
1.380 + HBufC8* theDP = keyDP.BufferLC();
1.381 + iOut.writeOctetString(*theDP);
1.382 +
1.383 + iOut.writeString(_L("\n\t ----------- DQ -----------\n\r"));
1.384 + const TInteger& keyDQ = aPrivateKeyCRT->DQ();
1.385 + HBufC8* theDQ = keyDQ.BufferLC();
1.386 + iOut.writeOctetString(*theDQ);
1.387 +
1.388 + iOut.writeString(_L("\n\t ----------- QInv -----------\n\r"));
1.389 + const TInteger& keyQInv = aPrivateKeyCRT->QInv();
1.390 + HBufC8* theQInv = keyQInv.BufferLC();
1.391 + iOut.writeOctetString(*theQInv);
1.392 +
1.393 + iOut.writeString(_L("\n\t ----------- N -----------\n\r"));
1.394 + const TInteger& keyN = aPrivateKeyCRT->N();
1.395 + HBufC8* theN = keyN.BufferLC();
1.396 + iOut.writeOctetString(*theN);
1.397 +
1.398 + iOut.writeString(_L("\n\t ----------- ciphertext -----------\n\r"));
1.399 + iOut.writeOctetString(aCipherText);
1.400 +
1.401 + iOut.writeString(_L("\n\t -------------------------\n\r"));
1.402 +
1.403 + CleanupStack::PopAndDestroy(6);
1.404 +
1.405 +}