1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcrypto/test/tasymmetric/trsasignfb.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,314 @@
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 "trsasignfb.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 +
1.29 +_LIT8(KInputStart, "<input>");
1.30 +_LIT8(KKeyBitsStart, "<keybits>");
1.31 +_LIT8(KKeyBitsEnd, "</keybits>");
1.32 +
1.33 +CRSASignFB::~CRSASignFB()
1.34 + {
1.35 + delete iBody;
1.36 + }
1.37 +
1.38 +CRSASignFB::CRSASignFB(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.39 + : CTestAction(aConsole, aOut), iFs(aFs), iPerfTestIterations(100)
1.40 + {
1.41 + }
1.42 +
1.43 +CTestAction* CRSASignFB::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
1.44 + const TTestActionSpec& aTestActionSpec)
1.45 + {
1.46 + CTestAction* self = CRSASignFB::NewLC(aFs, aConsole,
1.47 + aOut, aTestActionSpec);
1.48 + CleanupStack::Pop();
1.49 + return self;
1.50 + }
1.51 +
1.52 +CTestAction* CRSASignFB::NewLC(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
1.53 + const TTestActionSpec& aTestActionSpec)
1.54 + {
1.55 + CRSASignFB* self = new(ELeave) CRSASignFB(aFs, aConsole, aOut);
1.56 + CleanupStack::PushL(self);
1.57 + self->ConstructL(aTestActionSpec);
1.58 + return self;
1.59 + }
1.60 +
1.61 +
1.62 +TRSAPrivateKeyType CRSASignFB::TypeOfCrypto()
1.63 + {
1.64 + return iCryptoType;
1.65 + }
1.66 +
1.67 +void CRSASignFB::ConstructL(const TTestActionSpec& aTestActionSpec)
1.68 +{
1.69 + CTestAction::ConstructL(aTestActionSpec);
1.70 + iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
1.71 + iBody->Des().Copy(aTestActionSpec.iActionBody);
1.72 +
1.73 + if (CPerformance::PerformanceTester()->IsTestingPerformance())
1.74 + {// Number of test iterations
1.75 + TPtrC8 itsPtr = Input::ParseElement(aTestActionSpec.iActionBody, KIterationsStart, KIterationsEnd);
1.76 + if (itsPtr!=KNullDesC8)
1.77 + {
1.78 + TLex8 lex;
1.79 + lex.Assign(itsPtr);
1.80 + User::LeaveIfError(lex.Val(iPerfTestIterations));
1.81 + if (iPerfTestIterations > KMaxIterations)
1.82 + User::Panic(_L("AsymmetricPerformance.exe"), KErrArgument);
1.83 +
1.84 + }
1.85 + TPtrC8 cryptoPtr = Input::ParseElement(aTestActionSpec.iActionBody, KTypeOfCryptoStart, KTypeOfCryptoEnd);
1.86 + if (cryptoPtr.CompareF(KRSAStandard) == 0)
1.87 + {
1.88 + iCryptoType = EStandard;
1.89 + }
1.90 + else if (cryptoPtr.CompareF(KRSAStandardCRT) == 0)
1.91 + {
1.92 + iCryptoType = EStandardCRT;
1.93 + }
1.94 + else
1.95 + {
1.96 + User::Panic(_L("AsymmetricPerformance.exe"), KErrArgument);
1.97 + }
1.98 + }
1.99 +
1.100 + TPtrC8 keyBitsPtr = Input::ParseElement(*iBody, KKeyBitsStart, KKeyBitsEnd);
1.101 + if (keyBitsPtr!=KNullDesC8)
1.102 + {
1.103 + TLex8 lex;
1.104 + lex.Assign(keyBitsPtr);
1.105 + User::LeaveIfError(lex.Val(iKeyBits));
1.106 + }
1.107 +}
1.108 +
1.109 +void CRSASignFB::DoPerformPrerequisite(TRequestStatus& aStatus)
1.110 + {
1.111 + TRequestStatus* status = &aStatus;
1.112 +
1.113 + HBufC8* input = Input::ParseElementHexL(*iBody, KInputStart);
1.114 + CleanupStack::PushL(input);
1.115 + iSigInput = CHashingSignatureInput::NewL(CMessageDigest::ESHA1);
1.116 + iSigInput->Update(*input);
1.117 + iInput = iSigInput->Final().AllocL();
1.118 + CleanupStack::PopAndDestroy(input);
1.119 +
1.120 + User::RequestComplete(status, KErrNone);
1.121 + iActionState = CTestAction::EAction;
1.122 + }
1.123 +
1.124 +void CRSASignFB::DoPerformPostrequisite(TRequestStatus& aStatus)
1.125 + {
1.126 + TRequestStatus* status = &aStatus;
1.127 + delete iInput;
1.128 + delete iSigInput;
1.129 +
1.130 + iFinished = ETrue;
1.131 + User::RequestComplete(status, iActionResult);
1.132 + }
1.133 +
1.134 +void CRSASignFB::DoReportAction(void)
1.135 + {
1.136 + }
1.137 +
1.138 +void CRSASignFB::DoCheckResult(TInt)
1.139 + {
1.140 + if (iResult)
1.141 + iConsole.Printf(_L("."));
1.142 + else
1.143 + iConsole.Printf(_L("X"));
1.144 + }
1.145 +
1.146 +void CRSASignFB::Hex(HBufC8& aString)
1.147 + {
1.148 + TPtr8 ptr=aString.Des();
1.149 + if (aString.Length()%2)
1.150 + {
1.151 + ptr.SetLength(0);
1.152 + return;
1.153 + }
1.154 + TInt i;
1.155 + for (i=0;i<aString.Length();i+=2)
1.156 + {
1.157 + TUint8 tmp;
1.158 + tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
1.159 + tmp*=16;
1.160 + tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
1.161 + ptr[i/2]=tmp;
1.162 + }
1.163 + ptr.SetLength(aString.Length()/2);
1.164 + }
1.165 +
1.166 +void CRSASignFB::PerformAction(TRequestStatus& aStatus)
1.167 + {
1.168 + TRequestStatus* status = &aStatus;
1.169 +
1.170 + if (CPerformance::PerformanceTester()->IsTestingPerformance())
1.171 + {
1.172 + iConsole.Printf(_L(">")); // Indicates start of test
1.173 + TRAP(iActionResult, DoPerformanceTestActionL());
1.174 + }
1.175 + else
1.176 + {
1.177 + TRAP(iActionResult, DoPerformActionL());
1.178 + }
1.179 +
1.180 + if (iActionResult==KErrNoMemory)
1.181 + User::Leave(iActionResult); // For OOM testing
1.182 +
1.183 + User::RequestComplete(status, iActionResult);
1.184 + iActionState = CTestAction::EPostrequisite;
1.185 + }
1.186 +
1.187 +
1.188 +void CRSASignFB::DoPerformanceTestActionL()
1.189 +{
1.190 + __UHEAP_MARK;
1.191 + iResult = EFalse;
1.192 +
1.193 + TTimeIntervalMicroSeconds keyCreateTime(0);
1.194 + TTimeIntervalMicroSeconds signTime(0);
1.195 + TTimeIntervalMicroSeconds verifyTime(0);
1.196 + TTime start, end;
1.197 + TTimeIntervalSeconds diff(0);
1.198 + const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
1.199 +
1.200 + // Time key pair creation
1.201 + CRSAKeyPair *rsaPair = NULL;
1.202 + TUint noRSAPairs = 0;
1.203 + start.UniversalTime();
1.204 + while (diff < KIterationTime)
1.205 + {
1.206 + rsaPair = CRSAKeyPair::NewLC(iKeyBits, TypeOfCrypto());
1.207 + CleanupStack::PopAndDestroy();
1.208 + noRSAPairs++;
1.209 + end.UniversalTime();
1.210 + end.SecondsFrom(start, diff);
1.211 + }
1.212 + end.UniversalTime();
1.213 + keyCreateTime = end.MicroSecondsFrom(start);
1.214 + TReal keycreatetime = I64REAL(keyCreateTime.Int64());
1.215 +
1.216 + rsaPair=CRSAKeyPair::NewLC(iKeyBits, TypeOfCrypto()); // Create one keypair for operations
1.217 +
1.218 + CRSAPKCS1v15Signer* signer = CRSAPKCS1v15Signer::NewL(rsaPair->PrivateKey());
1.219 + CleanupStack::PushL(signer);
1.220 + CRSAPKCS1v15Verifier* verifier = CRSAPKCS1v15Verifier::NewL(rsaPair->PublicKey());
1.221 + CleanupStack::PushL(verifier);
1.222 +
1.223 + const CRSASignature *testSig;
1.224 +
1.225 + // Time signing
1.226 + diff = 0;
1.227 + TInt noSignings = 0;
1.228 + start.UniversalTime();
1.229 + while (diff < KIterationTime)
1.230 + {
1.231 + testSig = signer->SignL(*iInput);
1.232 + delete testSig;
1.233 + noSignings++;
1.234 + end.UniversalTime();
1.235 + end.SecondsFrom(start, diff);
1.236 + }
1.237 + end.UniversalTime();
1.238 + signTime = end.MicroSecondsFrom(start);
1.239 + TReal signtime = I64REAL(signTime.Int64());
1.240 + // Time verification
1.241 + TInt noVerifies = 0;
1.242 + diff = 0;
1.243 + testSig = signer->SignL(*iInput);
1.244 + CleanupStack::PushL(const_cast<CRSASignature*>(testSig));
1.245 + start.UniversalTime();
1.246 + while (diff < KIterationTime)
1.247 + {
1.248 + iResult = verifier->VerifyL(*iInput, *testSig);
1.249 +
1.250 + // do as many verfies as possible
1.251 + if (!iResult)
1.252 + {
1.253 + break;
1.254 + }
1.255 + noVerifies++;
1.256 + end.UniversalTime();
1.257 + end.SecondsFrom(start, diff);
1.258 + }
1.259 + end.UniversalTime();
1.260 + verifyTime = end.MicroSecondsFrom(start);
1.261 + TReal verifytime = I64REAL(verifyTime.Int64());
1.262 + CleanupStack::PopAndDestroy(4); // verifier,signer,testSig,rsaPairs
1.263 + __UHEAP_MARKEND;
1.264 +
1.265 + if (iResult)
1.266 + {
1.267 + TBuf<256> buf;
1.268 + TReal rate = I64REAL(keyCreateTime.Int64()) / noRSAPairs;
1.269 +
1.270 + _LIT(KKeyCreateTime, "\n\tKeyCreate: %f us/key creation (no creations: %i in %f us)\r\n");
1.271 + buf.Format(KKeyCreateTime, rate, noRSAPairs, keycreatetime);
1.272 + iOut.writeString(buf);
1.273 +
1.274 + rate = I64REAL(signTime.Int64()) / noSignings;
1.275 + _LIT(KSignTime, "\tSigning: %f us/signing (no signings: %i in %f us)\r\n");
1.276 + buf.Format(KSignTime, rate, noSignings, signtime);
1.277 + iOut.writeString(buf);
1.278 +
1.279 + rate = I64REAL(verifyTime.Int64()) / noVerifies;
1.280 + _LIT(KVerifyTime, "\tVerifying: %f us/verify (no verfies: %i in %f us)\r\n");
1.281 + buf.Format(KVerifyTime, rate, noVerifies, verifytime);
1.282 + iOut.writeString(buf);
1.283 + }
1.284 + else
1.285 + {
1.286 + _LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
1.287 + iOut.writeString(KNoTimingInfo);
1.288 + }
1.289 +}
1.290 +
1.291 +void CRSASignFB::DoPerformActionL()
1.292 + {
1.293 + __UHEAP_MARK;
1.294 +
1.295 + iResult = EFalse;
1.296 +
1.297 + CRSAKeyPair* rsaPair = CRSAKeyPair::NewLC(512, TypeOfCrypto());
1.298 +
1.299 + CRSAPKCS1v15Signer* signer = CRSAPKCS1v15Signer::NewL(rsaPair->PrivateKey());
1.300 + CleanupStack::PushL(signer);
1.301 +
1.302 + CRSAPKCS1v15Verifier* verifier = CRSAPKCS1v15Verifier::NewL(rsaPair->PublicKey());
1.303 + CleanupStack::PushL(verifier);
1.304 +
1.305 + const CRSASignature* signature = signer->SignL(*iInput);
1.306 + CleanupStack::PushL(const_cast<CRSASignature*>(signature));
1.307 +
1.308 + if(verifier->VerifyL(*iInput, *signature))
1.309 + {
1.310 + iResult = ETrue;
1.311 + }
1.312 +
1.313 + CleanupStack::PopAndDestroy(4, rsaPair); //signature, verifier, signer, rsaPair
1.314 +
1.315 + __UHEAP_MARKEND;
1.316 + }
1.317 +