1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcrypto/test/tasymmetric/trsavector.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,806 @@
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 "trsavector.h"
1.23 +#include "tvectorutils.h"
1.24 +#include "t_input.h"
1.25 +#include <bigint.h>
1.26 +#include "performancetest.h"
1.27 +#include "tbrokenrandom.h"
1.28 +_LIT8(KPlaintextStart, "<plaintext>");
1.29 +_LIT8(KPlaintextEnd, "</plaintext>");
1.30 +_LIT8(KCiphertextStart, "<ciphertext>");
1.31 +_LIT8(KCiphertextEnd, "</ciphertext>");
1.32 +_LIT8(KSignatureStart, "<signature>");
1.33 +_LIT8(KSignatureEnd, "</signature>");
1.34 +_LIT8(KDigestInfoStart, "<digestInfo>");
1.35 +_LIT8(KDigestInfoEnd, "</digestInfo>");
1.36 +
1.37 +////////////////////////////////////////////////////////////////////////////////
1.38 +// CRSAEncryptVector
1.39 +////////////////////////////////////////////////////////////////////////////////
1.40 +
1.41 +CTestAction* CRSAEncryptVector::NewL(RFs& aFs,
1.42 + CConsoleBase& aConsole,
1.43 + Output& aOut,
1.44 + const TTestActionSpec& aTestActionSpec)
1.45 + {
1.46 + CTestAction* self = CRSAEncryptVector::NewLC(aFs, aConsole,
1.47 + aOut, aTestActionSpec);
1.48 + CleanupStack::Pop();
1.49 + return self;
1.50 + }
1.51 +
1.52 +CTestAction* CRSAEncryptVector::NewLC(RFs& aFs,
1.53 + CConsoleBase& aConsole,
1.54 + Output& aOut,
1.55 + const TTestActionSpec& aTestActionSpec)
1.56 + {
1.57 + CRSAEncryptVector* self = new(ELeave) CRSAEncryptVector(aFs, aConsole, aOut);
1.58 + CleanupStack::PushL(self);
1.59 + self->ConstructL(aTestActionSpec);
1.60 + return self;
1.61 + }
1.62 +
1.63 +CRSAEncryptVector::~CRSAEncryptVector()
1.64 + {
1.65 + delete iPubKey;
1.66 + delete iPlaintext;
1.67 + delete iCiphertext;
1.68 + }
1.69 +
1.70 +CRSAEncryptVector::CRSAEncryptVector(RFs& /*aFs*/,
1.71 + CConsoleBase& aConsole,
1.72 + Output& aOut)
1.73 +: CVectorTest(aConsole, aOut)
1.74 + {
1.75 + }
1.76 +
1.77 +void CRSAEncryptVector::ConstructL(const TTestActionSpec& aTestActionSpec)
1.78 + {
1.79 + CVectorTest::ConstructL(aTestActionSpec);
1.80 +
1.81 + iPubKey = VectorUtils::ReadRSAPublicKeyL(aTestActionSpec.iActionBody);
1.82 +
1.83 + TPtrC8 ptextIn = Input::ParseElement(aTestActionSpec.iActionBody, KPlaintextStart, KPlaintextEnd);
1.84 + iPlaintext = VectorUtils::ParseBinaryL(ptextIn);
1.85 +
1.86 + TPtrC8 ctextIn = Input::ParseElement(aTestActionSpec.iActionBody, KCiphertextStart, KCiphertextEnd);
1.87 + iCiphertext = VectorUtils::ParseBinaryL(ctextIn);
1.88 + }
1.89 +
1.90 +void CRSAEncryptVector::DoPerformanceTestActionL()
1.91 +{
1.92 + __UHEAP_MARK;
1.93 +
1.94 + CRSAPKCS1v15Encryptor* encryptor = CRSAPKCS1v15Encryptor::NewL(*iPubKey);
1.95 + CleanupStack::PushL(encryptor);
1.96 +
1.97 + TTimeIntervalMicroSeconds encryptTime(0);
1.98 + TTime start, end;
1.99 + TTimeIntervalSeconds diff(0);
1.100 + const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
1.101 +
1.102 + TInt noEncryptions = 0;
1.103 + HBufC8 *eResult = HBufC8::NewLC(encryptor->MaxOutputLength());
1.104 + TPtr8 ePtr = eResult->Des();
1.105 + TPtr8* eResultPtr = &ePtr;
1.106 +
1.107 +// Time encryption
1.108 + CRandomIncrementing* brokenRandom = new(ELeave)CRandomIncrementing(1);
1.109 + SetThreadRandomLC(brokenRandom);
1.110 + start.UniversalTime();
1.111 + while (diff < KIterationTime)
1.112 + {
1.113 + eResultPtr->Zero();
1.114 + encryptor->EncryptL(*iPlaintext, *eResultPtr);
1.115 + noEncryptions++;
1.116 + end.UniversalTime();
1.117 + end.SecondsFrom(start, diff);
1.118 + }
1.119 + end.UniversalTime();
1.120 + encryptTime = end.MicroSecondsFrom(start);
1.121 + TReal encrypttime = I64REAL(encryptTime.Int64());
1.122 + CleanupStack::PopAndDestroy(1); //SetThreadRandomLC
1.123 +
1.124 + iResult = ETrue;
1.125 + if (*iCiphertext!=*eResult)
1.126 + {
1.127 + iResult = EFalse;
1.128 + }
1.129 +
1.130 + CleanupStack::PopAndDestroy(2); // encryptor, eResult
1.131 +
1.132 + __UHEAP_MARKEND;
1.133 +
1.134 + if (iResult)
1.135 + {
1.136 + TReal rate = I64REAL(encryptTime.Int64()) / noEncryptions;
1.137 + TBuf<256> buf;
1.138 + _LIT(KEncryptTime, "\tEncrypt Time: %f us/encryption (%i encryptions in %f us)\r\n");
1.139 + buf.Format(KEncryptTime, rate, noEncryptions, encrypttime);
1.140 + iOut.writeString(buf);
1.141 + }
1.142 + else
1.143 + {
1.144 + _LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
1.145 + iOut.writeString(KNoTimingInfo);
1.146 + }
1.147 +}
1.148 +
1.149 +void CRSAEncryptVector::DoPerformActionL()
1.150 + {
1.151 + __UHEAP_MARK;
1.152 +
1.153 + CRSAPKCS1v15Encryptor* encryptor = CRSAPKCS1v15Encryptor::NewL(*iPubKey);
1.154 + CleanupStack::PushL(encryptor);
1.155 + HBufC8* encryptBuf = HBufC8::NewLC(encryptor->MaxOutputLength());
1.156 + TPtr8 encryptPtr = encryptBuf->Des();
1.157 + CRandomIncrementing* brokenRandom = new(ELeave)CRandomIncrementing(1);
1.158 + SetThreadRandomLC(brokenRandom);
1.159 + encryptor->EncryptL(*iPlaintext, encryptPtr);
1.160 + CleanupStack::PopAndDestroy(1); //threadrandom;
1.161 + iResult = (*iCiphertext == *encryptBuf);
1.162 +
1.163 + CleanupStack::PopAndDestroy(encryptBuf);
1.164 + CleanupStack::PopAndDestroy(encryptor);
1.165 +
1.166 + __UHEAP_MARKEND;
1.167 + }
1.168 +
1.169 +////////////////////////////////////////////////////////////////////////////////
1.170 +// CRSADecryptVector
1.171 +////////////////////////////////////////////////////////////////////////////////
1.172 +
1.173 +CTestAction* CRSADecryptVector::NewL(RFs& aFs,
1.174 + CConsoleBase& aConsole,
1.175 + Output& aOut,
1.176 + const TTestActionSpec& aTestActionSpec)
1.177 + {
1.178 + CTestAction* self = CRSADecryptVector::NewLC(aFs, aConsole,
1.179 + aOut, aTestActionSpec);
1.180 + CleanupStack::Pop();
1.181 + return self;
1.182 + }
1.183 +
1.184 +CTestAction* CRSADecryptVector::NewLC(RFs& aFs,
1.185 + CConsoleBase& aConsole,
1.186 + Output& aOut,
1.187 + const TTestActionSpec& aTestActionSpec)
1.188 + {
1.189 + CRSADecryptVector* self = new(ELeave) CRSADecryptVector(aFs, aConsole, aOut);
1.190 + CleanupStack::PushL(self);
1.191 + self->ConstructL(aTestActionSpec);
1.192 + return self;
1.193 + }
1.194 +
1.195 +CRSADecryptVector::~CRSADecryptVector()
1.196 + {
1.197 + delete iPrivKey;
1.198 + delete iPlaintext;
1.199 + delete iCiphertext;
1.200 + }
1.201 +
1.202 +CRSADecryptVector::CRSADecryptVector(RFs& /*aFs*/,
1.203 + CConsoleBase& aConsole,
1.204 + Output& aOut)
1.205 +: CVectorTest(aConsole, aOut)
1.206 + {
1.207 + }
1.208 +
1.209 +void CRSADecryptVector::ConstructL(const TTestActionSpec& aTestActionSpec)
1.210 + {
1.211 + CVectorTest::ConstructL(aTestActionSpec);
1.212 +
1.213 + iPrivKey = VectorUtils::ReadRSAPrivateKeyL(aTestActionSpec.iActionBody);
1.214 +
1.215 + TPtrC8 ctextIn = Input::ParseElement(aTestActionSpec.iActionBody, KCiphertextStart, KCiphertextEnd);
1.216 + iCiphertext = VectorUtils::ParseBinaryL(ctextIn);
1.217 +
1.218 + TPtrC8 ptextIn = Input::ParseElement(aTestActionSpec.iActionBody, KPlaintextStart, KPlaintextEnd);
1.219 + iPlaintext = VectorUtils::ParseBinaryL(ptextIn);
1.220 + }
1.221 +
1.222 +void CRSADecryptVector::DoPerformanceTestActionL()
1.223 +{
1.224 + __UHEAP_MARK;
1.225 +
1.226 + CRSAPKCS1v15Decryptor* decryptor = CRSAPKCS1v15Decryptor::NewL(*iPrivKey);
1.227 + CleanupStack::PushL(decryptor);
1.228 +
1.229 + TTimeIntervalMicroSeconds decryptTime(0);
1.230 + TTime start, end;
1.231 + TTimeIntervalSeconds diff(0);
1.232 + const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
1.233 +
1.234 + HBufC8 *dResult = HBufC8::NewLC(decryptor->MaxOutputLength());
1.235 + TPtr8 dPtr = dResult->Des();
1.236 + TPtr8* dResultPtr = &dPtr;
1.237 + TInt noDecryptions = 0;
1.238 +
1.239 +// Time decryption
1.240 + start.UniversalTime();
1.241 + while (diff < KIterationTime)
1.242 + {
1.243 + decryptor->DecryptL(*iCiphertext, *dResultPtr);
1.244 + noDecryptions++;
1.245 + end.UniversalTime();
1.246 + end.SecondsFrom(start, diff);
1.247 + }
1.248 + end.UniversalTime();
1.249 + decryptTime = end.MicroSecondsFrom(start);
1.250 + TReal decrypttime = I64REAL(decryptTime.Int64());
1.251 + iResult = ETrue;
1.252 + if (*iPlaintext!=*dResult)
1.253 + {
1.254 + iResult = EFalse;
1.255 + }
1.256 +
1.257 + CleanupStack::PopAndDestroy(2); // decryptor, dResult
1.258 +
1.259 + __UHEAP_MARKEND;
1.260 +
1.261 + if (iResult)
1.262 + {
1.263 + TReal rate = I64REAL(decryptTime.Int64()) / noDecryptions;
1.264 + TBuf<256> buf;
1.265 + _LIT(KDecryptTime, "\tDecrypt Time: %f us/decryption (%i decryptions in %f us)\r\n");
1.266 + buf.Format(KDecryptTime, rate, noDecryptions, decrypttime);
1.267 + iOut.writeString(buf);
1.268 + }
1.269 + else
1.270 + {
1.271 + _LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
1.272 + iOut.writeString(KNoTimingInfo);
1.273 + }
1.274 +}
1.275 +
1.276 +void CRSADecryptVector::DoPerformActionL()
1.277 + {
1.278 + __UHEAP_MARK;
1.279 +
1.280 + CRSAPKCS1v15Decryptor* decryptor = CRSAPKCS1v15Decryptor::NewL(*iPrivKey);
1.281 + CleanupStack::PushL(decryptor);
1.282 +
1.283 + HBufC8* decryptBuf = HBufC8::NewLC(decryptor->MaxOutputLength());
1.284 + TPtr8 decryptPtr = decryptBuf->Des();
1.285 + TRAPD(err, decryptor->DecryptL(*iCiphertext, decryptPtr));
1.286 + iResult = (err == KErrNone) && (*iPlaintext == *decryptBuf);
1.287 +
1.288 + CleanupStack::PopAndDestroy(decryptBuf);
1.289 + CleanupStack::PopAndDestroy(decryptor);
1.290 +
1.291 + __UHEAP_MARKEND;
1.292 + }
1.293 +
1.294 +
1.295 +////////////////////////////////////////////////////////////////////////////////
1.296 +// CRSADecryptVectorCRT
1.297 +////////////////////////////////////////////////////////////////////////////////
1.298 +CTestAction* CRSADecryptVectorCRT::NewL(RFs& aFs,
1.299 + CConsoleBase& aConsole,
1.300 + Output& aOut,
1.301 + const TTestActionSpec& aTestActionSpec)
1.302 + {
1.303 + CTestAction* self = CRSADecryptVectorCRT::NewLC(aFs, aConsole,
1.304 + aOut, aTestActionSpec);
1.305 + CleanupStack::Pop();
1.306 + return self;
1.307 + }
1.308 +
1.309 +CTestAction* CRSADecryptVectorCRT::NewLC(RFs& aFs,
1.310 + CConsoleBase& aConsole,
1.311 + Output& aOut,
1.312 + const TTestActionSpec& aTestActionSpec)
1.313 + {
1.314 + CRSADecryptVectorCRT* self = new(ELeave) CRSADecryptVectorCRT(aFs, aConsole, aOut);
1.315 + CleanupStack::PushL(self);
1.316 + self->ConstructL(aTestActionSpec);
1.317 + return self;
1.318 + }
1.319 +
1.320 +CRSADecryptVectorCRT::~CRSADecryptVectorCRT()
1.321 + {
1.322 + delete iPrivKey;
1.323 + delete iPlaintext;
1.324 + delete iCiphertext;
1.325 + }
1.326 +
1.327 +CRSADecryptVectorCRT::CRSADecryptVectorCRT(RFs& /*aFs*/,
1.328 + CConsoleBase& aConsole,
1.329 + Output& aOut)
1.330 +: CVectorTest(aConsole, aOut)
1.331 + {
1.332 + }
1.333 +
1.334 +void CRSADecryptVectorCRT::ConstructL(const TTestActionSpec& aTestActionSpec)
1.335 + {
1.336 + CVectorTest::ConstructL(aTestActionSpec);
1.337 +
1.338 + iPrivKey = VectorUtils::ReadRSAPrivateKeyCRTL(aTestActionSpec.iActionBody);
1.339 +
1.340 + TPtrC8 ctextIn = Input::ParseElement(aTestActionSpec.iActionBody, KCiphertextStart, KCiphertextEnd);
1.341 + iCiphertext = VectorUtils::ParseBinaryL(ctextIn);
1.342 +
1.343 + TPtrC8 ptextIn = Input::ParseElement(aTestActionSpec.iActionBody, KPlaintextStart, KPlaintextEnd);
1.344 + iPlaintext = VectorUtils::ParseBinaryL(ptextIn);
1.345 + }
1.346 +
1.347 +void CRSADecryptVectorCRT::DoPerformanceTestActionL()
1.348 +{
1.349 + __UHEAP_MARK;
1.350 +
1.351 + CRSAPKCS1v15Decryptor* decryptor = CRSAPKCS1v15Decryptor::NewL(*iPrivKey);
1.352 + CleanupStack::PushL(decryptor);
1.353 +
1.354 + TTimeIntervalMicroSeconds decryptTime(0);
1.355 + TTime start, end;
1.356 + TTimeIntervalSeconds diff(0);
1.357 + const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
1.358 +
1.359 + TInt noDecryptions = 0;
1.360 + HBufC8 *dResult = HBufC8::NewLC(decryptor->MaxOutputLength());
1.361 + TPtr8 dPtr = dResult->Des();
1.362 + TPtr8* dResultPtr = &dPtr;
1.363 +
1.364 +// Time decryption
1.365 + start.UniversalTime();
1.366 + while (diff < KIterationTime)
1.367 + {
1.368 + decryptor->DecryptL(*iCiphertext, *dResultPtr);
1.369 + noDecryptions++;
1.370 + end.UniversalTime();
1.371 + end.SecondsFrom(start, diff);
1.372 + }
1.373 + end.UniversalTime();
1.374 + decryptTime = end.MicroSecondsFrom(start);
1.375 + TReal decrypttime = I64REAL(decryptTime.Int64());
1.376 +
1.377 + iResult = ETrue;
1.378 + if (*iPlaintext!=*dResult)
1.379 + {
1.380 + iResult = EFalse;
1.381 + }
1.382 +
1.383 +
1.384 + CleanupStack::PopAndDestroy(2); // decryptor, dResult
1.385 +
1.386 + __UHEAP_MARKEND;
1.387 +
1.388 + if (iResult)
1.389 + {
1.390 + TReal rate = I64REAL(decryptTime.Int64()) / noDecryptions;
1.391 + TBuf<256> buf;
1.392 + _LIT(KDecryptTime, "\tDecrypt Time: %f us/decryption (%i decryptions in %f us)\r\n");
1.393 + buf.Format(KDecryptTime, rate, noDecryptions, decrypttime);
1.394 + iOut.writeString(buf);
1.395 + }
1.396 + else
1.397 + {
1.398 + _LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
1.399 + iOut.writeString(KNoTimingInfo);
1.400 + }
1.401 +}
1.402 +
1.403 +void CRSADecryptVectorCRT::DoPerformActionL()
1.404 + {
1.405 + __UHEAP_MARK;
1.406 +
1.407 + CRSAPKCS1v15Decryptor* decryptor = CRSAPKCS1v15Decryptor::NewL(*iPrivKey);
1.408 + CleanupStack::PushL(decryptor);
1.409 +
1.410 + HBufC8* decryptBuf = HBufC8::NewLC(decryptor->MaxOutputLength());
1.411 + TPtr8 decryptPtr = decryptBuf->Des();
1.412 + TRAPD(err, decryptor->DecryptL(*iCiphertext, decryptPtr));
1.413 + iResult = (err == KErrNone) && (*iPlaintext == *decryptBuf);
1.414 +
1.415 + CleanupStack::PopAndDestroy(decryptBuf);
1.416 + CleanupStack::PopAndDestroy(decryptor);
1.417 +
1.418 + __UHEAP_MARKEND;
1.419 + }
1.420 +
1.421 +////////////////////////////////////////////////////////////////////////////////
1.422 +// CRSASignVector
1.423 +////////////////////////////////////////////////////////////////////////////////
1.424 +
1.425 +CTestAction* CRSASignVector::NewL(RFs& aFs,
1.426 + CConsoleBase& aConsole,
1.427 + Output& aOut,
1.428 + const TTestActionSpec& aTestActionSpec)
1.429 + {
1.430 + CTestAction* self = CRSASignVector::NewLC(aFs, aConsole, aOut, aTestActionSpec);
1.431 + CleanupStack::Pop();
1.432 + return self;
1.433 + }
1.434 +
1.435 +CTestAction* CRSASignVector::NewLC(RFs& aFs,
1.436 + CConsoleBase& aConsole,
1.437 + Output& aOut,
1.438 + const TTestActionSpec& aTestActionSpec)
1.439 + {
1.440 + CRSASignVector* self = new(ELeave) CRSASignVector(aFs, aConsole, aOut);
1.441 + CleanupStack::PushL(self);
1.442 + self->ConstructL(aTestActionSpec);
1.443 + return self;
1.444 + }
1.445 +
1.446 +CRSASignVector::~CRSASignVector()
1.447 + {
1.448 + delete iPrivKey;
1.449 + delete iDigestInfo;
1.450 + delete iSignature;
1.451 + }
1.452 +
1.453 +CRSASignVector::CRSASignVector(RFs& /*aFs*/,
1.454 + CConsoleBase& aConsole,
1.455 + Output& aOut)
1.456 + : CVectorTest(aConsole, aOut)
1.457 + {
1.458 + }
1.459 +
1.460 +void CRSASignVector::ConstructL(const TTestActionSpec& aTestActionSpec)
1.461 + {
1.462 + CVectorTest::ConstructL(aTestActionSpec);
1.463 +
1.464 + iPrivKey = VectorUtils::ReadRSAPrivateKeyL(aTestActionSpec.iActionBody);
1.465 +
1.466 + TPtrC8 digestInfoIn = Input::ParseElement(aTestActionSpec.iActionBody, KDigestInfoStart, KDigestInfoEnd);
1.467 + iDigestInfo = VectorUtils::ParseBinaryL(digestInfoIn);
1.468 +
1.469 + TPtrC8 signatureIn = Input::ParseElement(aTestActionSpec.iActionBody, KSignatureStart, KSignatureEnd);
1.470 + RInteger integer = VectorUtils::ParseIntegerL(signatureIn);
1.471 + CleanupStack::PushL(integer);
1.472 + iSignature = CRSASignature::NewL(integer);
1.473 + CleanupStack::Pop(&integer);
1.474 + }
1.475 +
1.476 +void CRSASignVector::DoPerformanceTestActionL()
1.477 +{
1.478 + __UHEAP_MARK;
1.479 +
1.480 + CRSAPKCS1v15Signer* signer = CRSAPKCS1v15Signer::NewL(*iPrivKey);
1.481 + CleanupStack::PushL(signer);
1.482 +
1.483 + TPtrC8 digestPtr = iDigestInfo->Des();
1.484 +
1.485 + TTimeIntervalMicroSeconds signTime(0);
1.486 + TTime start, end;
1.487 + TTimeIntervalSeconds diff(0);
1.488 + const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
1.489 +
1.490 + TInt noSignings = 0;
1.491 +
1.492 + const CRSASignature *testSig = 0;
1.493 +
1.494 +// Time signing
1.495 + start.UniversalTime();
1.496 + while (diff < KIterationTime)
1.497 + {
1.498 + testSig = signer->SignL(digestPtr);
1.499 + delete testSig;
1.500 + noSignings++;
1.501 + end.UniversalTime();
1.502 + end.SecondsFrom(start, diff);
1.503 + }
1.504 + end.UniversalTime();
1.505 + signTime = end.MicroSecondsFrom(start);
1.506 + TReal signtime = I64REAL(signTime.Int64());
1.507 +
1.508 + iResult = ETrue;
1.509 + testSig = signer->SignL(digestPtr);
1.510 + if (!(*testSig==*iSignature))
1.511 + {
1.512 + iResult = EFalse;
1.513 + }
1.514 + delete testSig;
1.515 +
1.516 + CleanupStack::PopAndDestroy(); // signer
1.517 +
1.518 + __UHEAP_MARKEND;
1.519 +
1.520 + if (iResult)
1.521 + {
1.522 + TReal rate = I64REAL(signTime.Int64()) / noSignings;
1.523 + TBuf<256> buf;
1.524 + _LIT(KSignTime, "\tSign Time: %f us/signing (%i signings in %f us)\r\n");
1.525 + buf.Format(KSignTime, rate, noSignings, signtime);
1.526 + iOut.writeString(buf);
1.527 + }
1.528 + else
1.529 + {
1.530 + _LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
1.531 + iOut.writeString(KNoTimingInfo);
1.532 + }
1.533 +}
1.534 +
1.535 +void CRSASignVector::DoPerformActionL()
1.536 + {
1.537 + __UHEAP_MARK;
1.538 +
1.539 + CRSAPKCS1v15Signer* digestSigner = CRSAPKCS1v15Signer::NewL(*iPrivKey);
1.540 + CleanupStack::PushL(digestSigner);
1.541 +
1.542 + TPtrC8 digestPtr2 = iDigestInfo->Des();
1.543 + const CRSASignature* testSig2 = digestSigner->SignL(digestPtr2);
1.544 + CleanupStack::PushL(const_cast<CRSASignature*>(testSig2));
1.545 + iResult = (*testSig2 == *iSignature);
1.546 +
1.547 + CleanupStack::PopAndDestroy(const_cast<CRSASignature*>(testSig2));
1.548 + CleanupStack::PopAndDestroy(digestSigner);
1.549 +
1.550 + __UHEAP_MARKEND;
1.551 + }
1.552 +
1.553 +////////////////////////////////////////////////////////////////////////////////
1.554 +// CRSASignVectorCRT
1.555 +////////////////////////////////////////////////////////////////////////////////
1.556 +
1.557 +CTestAction* CRSASignVectorCRT::NewL(RFs& aFs,
1.558 + CConsoleBase& aConsole,
1.559 + Output& aOut,
1.560 + const TTestActionSpec& aTestActionSpec)
1.561 + {
1.562 + CTestAction* self = CRSASignVectorCRT::NewLC(aFs, aConsole, aOut, aTestActionSpec);
1.563 + CleanupStack::Pop();
1.564 + return self;
1.565 + }
1.566 +
1.567 +CTestAction* CRSASignVectorCRT::NewLC(RFs& aFs,
1.568 + CConsoleBase& aConsole,
1.569 + Output& aOut,
1.570 + const TTestActionSpec& aTestActionSpec)
1.571 + {
1.572 + CRSASignVectorCRT* self = new(ELeave) CRSASignVectorCRT(aFs, aConsole, aOut);
1.573 + CleanupStack::PushL(self);
1.574 + self->ConstructL(aTestActionSpec);
1.575 + return self;
1.576 + }
1.577 +
1.578 +CRSASignVectorCRT::~CRSASignVectorCRT()
1.579 + {
1.580 + delete iPrivKey;
1.581 + delete iDigestInfo;
1.582 + delete iSignature;
1.583 + }
1.584 +
1.585 +CRSASignVectorCRT::CRSASignVectorCRT(RFs& /*aFs*/,
1.586 + CConsoleBase& aConsole,
1.587 + Output& aOut)
1.588 + : CVectorTest(aConsole, aOut)
1.589 + {
1.590 + }
1.591 +
1.592 +void CRSASignVectorCRT::ConstructL(const TTestActionSpec& aTestActionSpec)
1.593 + {
1.594 + CVectorTest::ConstructL(aTestActionSpec);
1.595 +
1.596 + iPrivKey = VectorUtils::ReadRSAPrivateKeyCRTL(aTestActionSpec.iActionBody);
1.597 +
1.598 + TPtrC8 digestInfoIn = Input::ParseElement(aTestActionSpec.iActionBody, KDigestInfoStart, KDigestInfoEnd);
1.599 + iDigestInfo = VectorUtils::ParseBinaryL(digestInfoIn);
1.600 +
1.601 + TPtrC8 signatureIn = Input::ParseElement(aTestActionSpec.iActionBody, KSignatureStart, KSignatureEnd);
1.602 + RInteger integer = VectorUtils::ParseIntegerL(signatureIn);
1.603 + CleanupStack::PushL(integer);
1.604 + iSignature = CRSASignature::NewL(integer);
1.605 + CleanupStack::Pop(&integer);
1.606 + }
1.607 +
1.608 +void CRSASignVectorCRT::DoPerformanceTestActionL()
1.609 +{
1.610 + __UHEAP_MARK;
1.611 +
1.612 + CRSAPKCS1v15Signer* signer = CRSAPKCS1v15Signer::NewL(*iPrivKey);
1.613 + CleanupStack::PushL(signer);
1.614 +
1.615 + TPtrC8 digestPtr = iDigestInfo->Des();
1.616 +
1.617 + TTimeIntervalMicroSeconds signTime(0);
1.618 + TTime start, end;
1.619 + TTimeIntervalSeconds diff(0);
1.620 + const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
1.621 + TInt noSignings = 0;
1.622 +
1.623 + const CRSASignature *testSig = NULL;
1.624 +
1.625 +// Time signing
1.626 + start.UniversalTime();
1.627 + while (diff < KIterationTime)
1.628 + {
1.629 + testSig = signer->SignL(digestPtr);
1.630 + delete testSig;
1.631 + noSignings++;
1.632 + end.UniversalTime();
1.633 + end.SecondsFrom(start, diff);
1.634 + }
1.635 + end.UniversalTime();
1.636 + signTime = end.MicroSecondsFrom(start);
1.637 + TReal signtime = I64REAL(signTime.Int64());
1.638 +
1.639 + testSig = signer->SignL(digestPtr);
1.640 + iResult = ETrue;
1.641 + if (!(*testSig==*iSignature))
1.642 + {
1.643 + iResult = EFalse;
1.644 + }
1.645 + delete testSig;
1.646 +
1.647 + CleanupStack::PopAndDestroy(); // signer
1.648 +
1.649 + __UHEAP_MARKEND;
1.650 +
1.651 + if (iResult)
1.652 + {
1.653 + TReal rate = I64REAL(signTime.Int64()) / noSignings;
1.654 + TBuf<256> buf;
1.655 + _LIT(KSignTime, "\tSign Time: %f us/signing (%i signings in %f us)\r\n");
1.656 + buf.Format(KSignTime, rate, noSignings, signtime);
1.657 + iOut.writeString(buf);
1.658 + }
1.659 + else
1.660 + {
1.661 + _LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
1.662 + iOut.writeString(KNoTimingInfo);
1.663 + }
1.664 +}
1.665 +
1.666 +void CRSASignVectorCRT::DoPerformActionL()
1.667 + {
1.668 + __UHEAP_MARK;
1.669 +
1.670 + CRSAPKCS1v15Signer* signer = CRSAPKCS1v15Signer::NewL(*iPrivKey);
1.671 + CleanupStack::PushL(signer);
1.672 +
1.673 + TPtrC8 digestPtr = iDigestInfo->Des();
1.674 + const CRSASignature* testSig = signer->SignL(digestPtr);
1.675 + CleanupStack::PushL(const_cast<CRSASignature*>(testSig));
1.676 +
1.677 +#ifdef _DEBUG
1.678 + HBufC8* theResultSig = testSig->S().BufferLC();
1.679 + HBufC8* theExpectedSig = iSignature->S().BufferLC();
1.680 + iResult = (theResultSig->Compare(*theExpectedSig)) ==0 ? ETrue : EFalse;
1.681 + CleanupStack::PopAndDestroy(2);
1.682 +#endif
1.683 +
1.684 + iResult = (*testSig == *iSignature);
1.685 +
1.686 + CleanupStack::PopAndDestroy(const_cast<CRSASignature*>(testSig));
1.687 + CleanupStack::PopAndDestroy(signer);
1.688 +
1.689 + __UHEAP_MARKEND;
1.690 + }
1.691 +
1.692 +
1.693 +////////////////////////////////////////////////////////////////////////////////
1.694 +// CRSAVerifyVector
1.695 +////////////////////////////////////////////////////////////////////////////////
1.696 +
1.697 +CTestAction* CRSAVerifyVector::NewL(RFs& aFs,
1.698 + CConsoleBase& aConsole,
1.699 + Output& aOut,
1.700 + const TTestActionSpec& aTestActionSpec)
1.701 + {
1.702 + CTestAction* self = CRSAVerifyVector::NewLC(aFs, aConsole, aOut, aTestActionSpec);
1.703 + CleanupStack::Pop();
1.704 + return self;
1.705 + }
1.706 +
1.707 +CTestAction* CRSAVerifyVector::NewLC(RFs& aFs,
1.708 + CConsoleBase& aConsole,
1.709 + Output& aOut,
1.710 + const TTestActionSpec& aTestActionSpec)
1.711 + {
1.712 + CRSAVerifyVector* self = new(ELeave) CRSAVerifyVector(aFs, aConsole, aOut);
1.713 + CleanupStack::PushL(self);
1.714 + self->ConstructL(aTestActionSpec);
1.715 + return self;
1.716 + }
1.717 +
1.718 +CRSAVerifyVector::~CRSAVerifyVector()
1.719 + {
1.720 + delete iPubKey;
1.721 + delete iDigestInfo;
1.722 + delete iSignature;
1.723 + }
1.724 +
1.725 +CRSAVerifyVector::CRSAVerifyVector(RFs& /*aFs*/,
1.726 + CConsoleBase& aConsole,
1.727 + Output& aOut)
1.728 + : CVectorTest(aConsole, aOut)
1.729 + {
1.730 + }
1.731 +
1.732 +void CRSAVerifyVector::ConstructL(const TTestActionSpec& aTestActionSpec)
1.733 + {
1.734 + CVectorTest::ConstructL(aTestActionSpec);
1.735 +
1.736 + iPubKey = VectorUtils::ReadRSAPublicKeyL(aTestActionSpec.iActionBody);
1.737 +
1.738 + TPtrC8 digestInfoIn = Input::ParseElement(aTestActionSpec.iActionBody, KDigestInfoStart, KDigestInfoEnd);
1.739 + iDigestInfo = VectorUtils::ParseBinaryL(digestInfoIn);
1.740 +
1.741 + TPtrC8 signatureIn = Input::ParseElement(aTestActionSpec.iActionBody, KSignatureStart, KSignatureEnd);
1.742 + RInteger integer = VectorUtils::ParseIntegerL(signatureIn);
1.743 + CleanupStack::PushL(integer);
1.744 + iSignature = CRSASignature::NewL(integer);
1.745 + CleanupStack::Pop(&integer);
1.746 + }
1.747 +
1.748 +void CRSAVerifyVector::DoPerformanceTestActionL()
1.749 +{
1.750 + __UHEAP_MARK;
1.751 +
1.752 +
1.753 + TTimeIntervalMicroSeconds verifyTime(0);
1.754 + TTime start, end;
1.755 + TTimeIntervalSeconds diff(0);
1.756 + const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
1.757 +
1.758 + TInt noVerifies = 0;
1.759 +
1.760 + CRSAPKCS1v15Verifier *verifier = CRSAPKCS1v15Verifier::NewLC(*iPubKey);
1.761 +
1.762 +// Time verification
1.763 + start.UniversalTime();
1.764 + while (diff < KIterationTime)
1.765 + {
1.766 + iResult = verifier->VerifyL(*iDigestInfo, *iSignature);
1.767 + if (!iResult)
1.768 + {
1.769 + break;
1.770 + }
1.771 + noVerifies++;
1.772 + end.UniversalTime();
1.773 + end.SecondsFrom(start, diff);
1.774 + }
1.775 + end.UniversalTime();
1.776 + verifyTime = end.MicroSecondsFrom(start);
1.777 + TReal verifytime = I64REAL(verifyTime.Int64());
1.778 +
1.779 + CleanupStack::PopAndDestroy(verifier);
1.780 +
1.781 + __UHEAP_MARKEND;
1.782 +
1.783 + if (iResult)
1.784 + {
1.785 + TReal rate = I64REAL(verifyTime.Int64()) / noVerifies;
1.786 + TBuf<256> buf;
1.787 + _LIT(KVerifyTime, "\tVerify Time: %f us/verify (%i verifies in %f us)\r\n");
1.788 + buf.Format(KVerifyTime, rate, noVerifies, verifytime);
1.789 + iOut.writeString(buf);
1.790 + }
1.791 + else
1.792 + {
1.793 + _LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
1.794 + iOut.writeString(KNoTimingInfo);
1.795 + }
1.796 +}
1.797 +
1.798 +void CRSAVerifyVector::DoPerformActionL()
1.799 + {
1.800 + __UHEAP_MARK;
1.801 +
1.802 + CRSAPKCS1v15Verifier* verifier = CRSAPKCS1v15Verifier::NewL(*iPubKey);
1.803 + CleanupStack::PushL(verifier);
1.804 + iResult = verifier->VerifyL(*iDigestInfo, *iSignature);
1.805 +
1.806 + CleanupStack::PopAndDestroy(verifier);
1.807 +
1.808 + __UHEAP_MARKEND;
1.809 + }