sl@0: /* sl@0: * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "trsavector.h" sl@0: #include "tvectorutils.h" sl@0: #include "t_input.h" sl@0: #include sl@0: #include "performancetest.h" sl@0: #include "tbrokenrandom.h" sl@0: _LIT8(KPlaintextStart, ""); sl@0: _LIT8(KPlaintextEnd, "</plaintext>"); sl@0: _LIT8(KCiphertextStart, "<ciphertext>"); sl@0: _LIT8(KCiphertextEnd, "</ciphertext>"); sl@0: _LIT8(KSignatureStart, "<signature>"); sl@0: _LIT8(KSignatureEnd, "</signature>"); sl@0: _LIT8(KDigestInfoStart, "<digestInfo>"); sl@0: _LIT8(KDigestInfoEnd, "</digestInfo>"); sl@0: sl@0: //////////////////////////////////////////////////////////////////////////////// sl@0: // CRSAEncryptVector sl@0: //////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: CTestAction* CRSAEncryptVector::NewL(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestAction* self = CRSAEncryptVector::NewLC(aFs, aConsole, sl@0: aOut, aTestActionSpec); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: CTestAction* CRSAEncryptVector::NewLC(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CRSAEncryptVector* self = new(ELeave) CRSAEncryptVector(aFs, aConsole, aOut); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aTestActionSpec); sl@0: return self; sl@0: } sl@0: sl@0: CRSAEncryptVector::~CRSAEncryptVector() sl@0: { sl@0: delete iPubKey; sl@0: delete iPlaintext; sl@0: delete iCiphertext; sl@0: } sl@0: sl@0: CRSAEncryptVector::CRSAEncryptVector(RFs& /*aFs*/, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut) sl@0: : CVectorTest(aConsole, aOut) sl@0: { sl@0: } sl@0: sl@0: void CRSAEncryptVector::ConstructL(const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CVectorTest::ConstructL(aTestActionSpec); sl@0: sl@0: iPubKey = VectorUtils::ReadRSAPublicKeyL(aTestActionSpec.iActionBody); sl@0: sl@0: TPtrC8 ptextIn = Input::ParseElement(aTestActionSpec.iActionBody, KPlaintextStart, KPlaintextEnd); sl@0: iPlaintext = VectorUtils::ParseBinaryL(ptextIn); sl@0: sl@0: TPtrC8 ctextIn = Input::ParseElement(aTestActionSpec.iActionBody, KCiphertextStart, KCiphertextEnd); sl@0: iCiphertext = VectorUtils::ParseBinaryL(ctextIn); sl@0: } sl@0: sl@0: void CRSAEncryptVector::DoPerformanceTestActionL() sl@0: { sl@0: __UHEAP_MARK; sl@0: sl@0: CRSAPKCS1v15Encryptor* encryptor = CRSAPKCS1v15Encryptor::NewL(*iPubKey); sl@0: CleanupStack::PushL(encryptor); sl@0: sl@0: TTimeIntervalMicroSeconds encryptTime(0); sl@0: TTime start, end; sl@0: TTimeIntervalSeconds diff(0); sl@0: const TTimeIntervalSeconds KIterationTime(iPerfTestIterations); sl@0: sl@0: TInt noEncryptions = 0; sl@0: HBufC8 *eResult = HBufC8::NewLC(encryptor->MaxOutputLength()); sl@0: TPtr8 ePtr = eResult->Des(); sl@0: TPtr8* eResultPtr = &ePtr; sl@0: sl@0: // Time encryption sl@0: CRandomIncrementing* brokenRandom = new(ELeave)CRandomIncrementing(1); sl@0: SetThreadRandomLC(brokenRandom); sl@0: start.UniversalTime(); sl@0: while (diff < KIterationTime) sl@0: { sl@0: eResultPtr->Zero(); sl@0: encryptor->EncryptL(*iPlaintext, *eResultPtr); sl@0: noEncryptions++; sl@0: end.UniversalTime(); sl@0: end.SecondsFrom(start, diff); sl@0: } sl@0: end.UniversalTime(); sl@0: encryptTime = end.MicroSecondsFrom(start); sl@0: TReal encrypttime = I64REAL(encryptTime.Int64()); sl@0: CleanupStack::PopAndDestroy(1); //SetThreadRandomLC sl@0: sl@0: iResult = ETrue; sl@0: if (*iCiphertext!=*eResult) sl@0: { sl@0: iResult = EFalse; sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(2); // encryptor, eResult sl@0: sl@0: __UHEAP_MARKEND; sl@0: sl@0: if (iResult) sl@0: { sl@0: TReal rate = I64REAL(encryptTime.Int64()) / noEncryptions; sl@0: TBuf<256> buf; sl@0: _LIT(KEncryptTime, "\tEncrypt Time: %f us/encryption (%i encryptions in %f us)\r\n"); sl@0: buf.Format(KEncryptTime, rate, noEncryptions, encrypttime); sl@0: iOut.writeString(buf); sl@0: } sl@0: else sl@0: { sl@0: _LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n"); sl@0: iOut.writeString(KNoTimingInfo); sl@0: } sl@0: } sl@0: sl@0: void CRSAEncryptVector::DoPerformActionL() sl@0: { sl@0: __UHEAP_MARK; sl@0: sl@0: CRSAPKCS1v15Encryptor* encryptor = CRSAPKCS1v15Encryptor::NewL(*iPubKey); sl@0: CleanupStack::PushL(encryptor); sl@0: HBufC8* encryptBuf = HBufC8::NewLC(encryptor->MaxOutputLength()); sl@0: TPtr8 encryptPtr = encryptBuf->Des(); sl@0: CRandomIncrementing* brokenRandom = new(ELeave)CRandomIncrementing(1); sl@0: SetThreadRandomLC(brokenRandom); sl@0: encryptor->EncryptL(*iPlaintext, encryptPtr); sl@0: CleanupStack::PopAndDestroy(1); //threadrandom; sl@0: iResult = (*iCiphertext == *encryptBuf); sl@0: sl@0: CleanupStack::PopAndDestroy(encryptBuf); sl@0: CleanupStack::PopAndDestroy(encryptor); sl@0: sl@0: __UHEAP_MARKEND; sl@0: } sl@0: sl@0: //////////////////////////////////////////////////////////////////////////////// sl@0: // CRSADecryptVector sl@0: //////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: CTestAction* CRSADecryptVector::NewL(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestAction* self = CRSADecryptVector::NewLC(aFs, aConsole, sl@0: aOut, aTestActionSpec); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: CTestAction* CRSADecryptVector::NewLC(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CRSADecryptVector* self = new(ELeave) CRSADecryptVector(aFs, aConsole, aOut); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aTestActionSpec); sl@0: return self; sl@0: } sl@0: sl@0: CRSADecryptVector::~CRSADecryptVector() sl@0: { sl@0: delete iPrivKey; sl@0: delete iPlaintext; sl@0: delete iCiphertext; sl@0: } sl@0: sl@0: CRSADecryptVector::CRSADecryptVector(RFs& /*aFs*/, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut) sl@0: : CVectorTest(aConsole, aOut) sl@0: { sl@0: } sl@0: sl@0: void CRSADecryptVector::ConstructL(const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CVectorTest::ConstructL(aTestActionSpec); sl@0: sl@0: iPrivKey = VectorUtils::ReadRSAPrivateKeyL(aTestActionSpec.iActionBody); sl@0: sl@0: TPtrC8 ctextIn = Input::ParseElement(aTestActionSpec.iActionBody, KCiphertextStart, KCiphertextEnd); sl@0: iCiphertext = VectorUtils::ParseBinaryL(ctextIn); sl@0: sl@0: TPtrC8 ptextIn = Input::ParseElement(aTestActionSpec.iActionBody, KPlaintextStart, KPlaintextEnd); sl@0: iPlaintext = VectorUtils::ParseBinaryL(ptextIn); sl@0: } sl@0: sl@0: void CRSADecryptVector::DoPerformanceTestActionL() sl@0: { sl@0: __UHEAP_MARK; sl@0: sl@0: CRSAPKCS1v15Decryptor* decryptor = CRSAPKCS1v15Decryptor::NewL(*iPrivKey); sl@0: CleanupStack::PushL(decryptor); sl@0: sl@0: TTimeIntervalMicroSeconds decryptTime(0); sl@0: TTime start, end; sl@0: TTimeIntervalSeconds diff(0); sl@0: const TTimeIntervalSeconds KIterationTime(iPerfTestIterations); sl@0: sl@0: HBufC8 *dResult = HBufC8::NewLC(decryptor->MaxOutputLength()); sl@0: TPtr8 dPtr = dResult->Des(); sl@0: TPtr8* dResultPtr = &dPtr; sl@0: TInt noDecryptions = 0; sl@0: sl@0: // Time decryption sl@0: start.UniversalTime(); sl@0: while (diff < KIterationTime) sl@0: { sl@0: decryptor->DecryptL(*iCiphertext, *dResultPtr); sl@0: noDecryptions++; sl@0: end.UniversalTime(); sl@0: end.SecondsFrom(start, diff); sl@0: } sl@0: end.UniversalTime(); sl@0: decryptTime = end.MicroSecondsFrom(start); sl@0: TReal decrypttime = I64REAL(decryptTime.Int64()); sl@0: iResult = ETrue; sl@0: if (*iPlaintext!=*dResult) sl@0: { sl@0: iResult = EFalse; sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(2); // decryptor, dResult sl@0: sl@0: __UHEAP_MARKEND; sl@0: sl@0: if (iResult) sl@0: { sl@0: TReal rate = I64REAL(decryptTime.Int64()) / noDecryptions; sl@0: TBuf<256> buf; sl@0: _LIT(KDecryptTime, "\tDecrypt Time: %f us/decryption (%i decryptions in %f us)\r\n"); sl@0: buf.Format(KDecryptTime, rate, noDecryptions, decrypttime); sl@0: iOut.writeString(buf); sl@0: } sl@0: else sl@0: { sl@0: _LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n"); sl@0: iOut.writeString(KNoTimingInfo); sl@0: } sl@0: } sl@0: sl@0: void CRSADecryptVector::DoPerformActionL() sl@0: { sl@0: __UHEAP_MARK; sl@0: sl@0: CRSAPKCS1v15Decryptor* decryptor = CRSAPKCS1v15Decryptor::NewL(*iPrivKey); sl@0: CleanupStack::PushL(decryptor); sl@0: sl@0: HBufC8* decryptBuf = HBufC8::NewLC(decryptor->MaxOutputLength()); sl@0: TPtr8 decryptPtr = decryptBuf->Des(); sl@0: TRAPD(err, decryptor->DecryptL(*iCiphertext, decryptPtr)); sl@0: iResult = (err == KErrNone) && (*iPlaintext == *decryptBuf); sl@0: sl@0: CleanupStack::PopAndDestroy(decryptBuf); sl@0: CleanupStack::PopAndDestroy(decryptor); sl@0: sl@0: __UHEAP_MARKEND; sl@0: } sl@0: sl@0: sl@0: //////////////////////////////////////////////////////////////////////////////// sl@0: // CRSADecryptVectorCRT sl@0: //////////////////////////////////////////////////////////////////////////////// sl@0: CTestAction* CRSADecryptVectorCRT::NewL(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestAction* self = CRSADecryptVectorCRT::NewLC(aFs, aConsole, sl@0: aOut, aTestActionSpec); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: CTestAction* CRSADecryptVectorCRT::NewLC(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CRSADecryptVectorCRT* self = new(ELeave) CRSADecryptVectorCRT(aFs, aConsole, aOut); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aTestActionSpec); sl@0: return self; sl@0: } sl@0: sl@0: CRSADecryptVectorCRT::~CRSADecryptVectorCRT() sl@0: { sl@0: delete iPrivKey; sl@0: delete iPlaintext; sl@0: delete iCiphertext; sl@0: } sl@0: sl@0: CRSADecryptVectorCRT::CRSADecryptVectorCRT(RFs& /*aFs*/, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut) sl@0: : CVectorTest(aConsole, aOut) sl@0: { sl@0: } sl@0: sl@0: void CRSADecryptVectorCRT::ConstructL(const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CVectorTest::ConstructL(aTestActionSpec); sl@0: sl@0: iPrivKey = VectorUtils::ReadRSAPrivateKeyCRTL(aTestActionSpec.iActionBody); sl@0: sl@0: TPtrC8 ctextIn = Input::ParseElement(aTestActionSpec.iActionBody, KCiphertextStart, KCiphertextEnd); sl@0: iCiphertext = VectorUtils::ParseBinaryL(ctextIn); sl@0: sl@0: TPtrC8 ptextIn = Input::ParseElement(aTestActionSpec.iActionBody, KPlaintextStart, KPlaintextEnd); sl@0: iPlaintext = VectorUtils::ParseBinaryL(ptextIn); sl@0: } sl@0: sl@0: void CRSADecryptVectorCRT::DoPerformanceTestActionL() sl@0: { sl@0: __UHEAP_MARK; sl@0: sl@0: CRSAPKCS1v15Decryptor* decryptor = CRSAPKCS1v15Decryptor::NewL(*iPrivKey); sl@0: CleanupStack::PushL(decryptor); sl@0: sl@0: TTimeIntervalMicroSeconds decryptTime(0); sl@0: TTime start, end; sl@0: TTimeIntervalSeconds diff(0); sl@0: const TTimeIntervalSeconds KIterationTime(iPerfTestIterations); sl@0: sl@0: TInt noDecryptions = 0; sl@0: HBufC8 *dResult = HBufC8::NewLC(decryptor->MaxOutputLength()); sl@0: TPtr8 dPtr = dResult->Des(); sl@0: TPtr8* dResultPtr = &dPtr; sl@0: sl@0: // Time decryption sl@0: start.UniversalTime(); sl@0: while (diff < KIterationTime) sl@0: { sl@0: decryptor->DecryptL(*iCiphertext, *dResultPtr); sl@0: noDecryptions++; sl@0: end.UniversalTime(); sl@0: end.SecondsFrom(start, diff); sl@0: } sl@0: end.UniversalTime(); sl@0: decryptTime = end.MicroSecondsFrom(start); sl@0: TReal decrypttime = I64REAL(decryptTime.Int64()); sl@0: sl@0: iResult = ETrue; sl@0: if (*iPlaintext!=*dResult) sl@0: { sl@0: iResult = EFalse; sl@0: } sl@0: sl@0: sl@0: CleanupStack::PopAndDestroy(2); // decryptor, dResult sl@0: sl@0: __UHEAP_MARKEND; sl@0: sl@0: if (iResult) sl@0: { sl@0: TReal rate = I64REAL(decryptTime.Int64()) / noDecryptions; sl@0: TBuf<256> buf; sl@0: _LIT(KDecryptTime, "\tDecrypt Time: %f us/decryption (%i decryptions in %f us)\r\n"); sl@0: buf.Format(KDecryptTime, rate, noDecryptions, decrypttime); sl@0: iOut.writeString(buf); sl@0: } sl@0: else sl@0: { sl@0: _LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n"); sl@0: iOut.writeString(KNoTimingInfo); sl@0: } sl@0: } sl@0: sl@0: void CRSADecryptVectorCRT::DoPerformActionL() sl@0: { sl@0: __UHEAP_MARK; sl@0: sl@0: CRSAPKCS1v15Decryptor* decryptor = CRSAPKCS1v15Decryptor::NewL(*iPrivKey); sl@0: CleanupStack::PushL(decryptor); sl@0: sl@0: HBufC8* decryptBuf = HBufC8::NewLC(decryptor->MaxOutputLength()); sl@0: TPtr8 decryptPtr = decryptBuf->Des(); sl@0: TRAPD(err, decryptor->DecryptL(*iCiphertext, decryptPtr)); sl@0: iResult = (err == KErrNone) && (*iPlaintext == *decryptBuf); sl@0: sl@0: CleanupStack::PopAndDestroy(decryptBuf); sl@0: CleanupStack::PopAndDestroy(decryptor); sl@0: sl@0: __UHEAP_MARKEND; sl@0: } sl@0: sl@0: //////////////////////////////////////////////////////////////////////////////// sl@0: // CRSASignVector sl@0: //////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: CTestAction* CRSASignVector::NewL(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestAction* self = CRSASignVector::NewLC(aFs, aConsole, aOut, aTestActionSpec); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: CTestAction* CRSASignVector::NewLC(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CRSASignVector* self = new(ELeave) CRSASignVector(aFs, aConsole, aOut); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aTestActionSpec); sl@0: return self; sl@0: } sl@0: sl@0: CRSASignVector::~CRSASignVector() sl@0: { sl@0: delete iPrivKey; sl@0: delete iDigestInfo; sl@0: delete iSignature; sl@0: } sl@0: sl@0: CRSASignVector::CRSASignVector(RFs& /*aFs*/, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut) sl@0: : CVectorTest(aConsole, aOut) sl@0: { sl@0: } sl@0: sl@0: void CRSASignVector::ConstructL(const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CVectorTest::ConstructL(aTestActionSpec); sl@0: sl@0: iPrivKey = VectorUtils::ReadRSAPrivateKeyL(aTestActionSpec.iActionBody); sl@0: sl@0: TPtrC8 digestInfoIn = Input::ParseElement(aTestActionSpec.iActionBody, KDigestInfoStart, KDigestInfoEnd); sl@0: iDigestInfo = VectorUtils::ParseBinaryL(digestInfoIn); sl@0: sl@0: TPtrC8 signatureIn = Input::ParseElement(aTestActionSpec.iActionBody, KSignatureStart, KSignatureEnd); sl@0: RInteger integer = VectorUtils::ParseIntegerL(signatureIn); sl@0: CleanupStack::PushL(integer); sl@0: iSignature = CRSASignature::NewL(integer); sl@0: CleanupStack::Pop(&integer); sl@0: } sl@0: sl@0: void CRSASignVector::DoPerformanceTestActionL() sl@0: { sl@0: __UHEAP_MARK; sl@0: sl@0: CRSAPKCS1v15Signer* signer = CRSAPKCS1v15Signer::NewL(*iPrivKey); sl@0: CleanupStack::PushL(signer); sl@0: sl@0: TPtrC8 digestPtr = iDigestInfo->Des(); sl@0: sl@0: TTimeIntervalMicroSeconds signTime(0); sl@0: TTime start, end; sl@0: TTimeIntervalSeconds diff(0); sl@0: const TTimeIntervalSeconds KIterationTime(iPerfTestIterations); sl@0: sl@0: TInt noSignings = 0; sl@0: sl@0: const CRSASignature *testSig = 0; sl@0: sl@0: // Time signing sl@0: start.UniversalTime(); sl@0: while (diff < KIterationTime) sl@0: { sl@0: testSig = signer->SignL(digestPtr); sl@0: delete testSig; sl@0: noSignings++; sl@0: end.UniversalTime(); sl@0: end.SecondsFrom(start, diff); sl@0: } sl@0: end.UniversalTime(); sl@0: signTime = end.MicroSecondsFrom(start); sl@0: TReal signtime = I64REAL(signTime.Int64()); sl@0: sl@0: iResult = ETrue; sl@0: testSig = signer->SignL(digestPtr); sl@0: if (!(*testSig==*iSignature)) sl@0: { sl@0: iResult = EFalse; sl@0: } sl@0: delete testSig; sl@0: sl@0: CleanupStack::PopAndDestroy(); // signer sl@0: sl@0: __UHEAP_MARKEND; sl@0: sl@0: if (iResult) sl@0: { sl@0: TReal rate = I64REAL(signTime.Int64()) / noSignings; sl@0: TBuf<256> buf; sl@0: _LIT(KSignTime, "\tSign Time: %f us/signing (%i signings in %f us)\r\n"); sl@0: buf.Format(KSignTime, rate, noSignings, signtime); sl@0: iOut.writeString(buf); sl@0: } sl@0: else sl@0: { sl@0: _LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n"); sl@0: iOut.writeString(KNoTimingInfo); sl@0: } sl@0: } sl@0: sl@0: void CRSASignVector::DoPerformActionL() sl@0: { sl@0: __UHEAP_MARK; sl@0: sl@0: CRSAPKCS1v15Signer* digestSigner = CRSAPKCS1v15Signer::NewL(*iPrivKey); sl@0: CleanupStack::PushL(digestSigner); sl@0: sl@0: TPtrC8 digestPtr2 = iDigestInfo->Des(); sl@0: const CRSASignature* testSig2 = digestSigner->SignL(digestPtr2); sl@0: CleanupStack::PushL(const_cast<CRSASignature*>(testSig2)); sl@0: iResult = (*testSig2 == *iSignature); sl@0: sl@0: CleanupStack::PopAndDestroy(const_cast<CRSASignature*>(testSig2)); sl@0: CleanupStack::PopAndDestroy(digestSigner); sl@0: sl@0: __UHEAP_MARKEND; sl@0: } sl@0: sl@0: //////////////////////////////////////////////////////////////////////////////// sl@0: // CRSASignVectorCRT sl@0: //////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: CTestAction* CRSASignVectorCRT::NewL(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestAction* self = CRSASignVectorCRT::NewLC(aFs, aConsole, aOut, aTestActionSpec); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: CTestAction* CRSASignVectorCRT::NewLC(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CRSASignVectorCRT* self = new(ELeave) CRSASignVectorCRT(aFs, aConsole, aOut); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aTestActionSpec); sl@0: return self; sl@0: } sl@0: sl@0: CRSASignVectorCRT::~CRSASignVectorCRT() sl@0: { sl@0: delete iPrivKey; sl@0: delete iDigestInfo; sl@0: delete iSignature; sl@0: } sl@0: sl@0: CRSASignVectorCRT::CRSASignVectorCRT(RFs& /*aFs*/, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut) sl@0: : CVectorTest(aConsole, aOut) sl@0: { sl@0: } sl@0: sl@0: void CRSASignVectorCRT::ConstructL(const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CVectorTest::ConstructL(aTestActionSpec); sl@0: sl@0: iPrivKey = VectorUtils::ReadRSAPrivateKeyCRTL(aTestActionSpec.iActionBody); sl@0: sl@0: TPtrC8 digestInfoIn = Input::ParseElement(aTestActionSpec.iActionBody, KDigestInfoStart, KDigestInfoEnd); sl@0: iDigestInfo = VectorUtils::ParseBinaryL(digestInfoIn); sl@0: sl@0: TPtrC8 signatureIn = Input::ParseElement(aTestActionSpec.iActionBody, KSignatureStart, KSignatureEnd); sl@0: RInteger integer = VectorUtils::ParseIntegerL(signatureIn); sl@0: CleanupStack::PushL(integer); sl@0: iSignature = CRSASignature::NewL(integer); sl@0: CleanupStack::Pop(&integer); sl@0: } sl@0: sl@0: void CRSASignVectorCRT::DoPerformanceTestActionL() sl@0: { sl@0: __UHEAP_MARK; sl@0: sl@0: CRSAPKCS1v15Signer* signer = CRSAPKCS1v15Signer::NewL(*iPrivKey); sl@0: CleanupStack::PushL(signer); sl@0: sl@0: TPtrC8 digestPtr = iDigestInfo->Des(); sl@0: sl@0: TTimeIntervalMicroSeconds signTime(0); sl@0: TTime start, end; sl@0: TTimeIntervalSeconds diff(0); sl@0: const TTimeIntervalSeconds KIterationTime(iPerfTestIterations); sl@0: TInt noSignings = 0; sl@0: sl@0: const CRSASignature *testSig = NULL; sl@0: sl@0: // Time signing sl@0: start.UniversalTime(); sl@0: while (diff < KIterationTime) sl@0: { sl@0: testSig = signer->SignL(digestPtr); sl@0: delete testSig; sl@0: noSignings++; sl@0: end.UniversalTime(); sl@0: end.SecondsFrom(start, diff); sl@0: } sl@0: end.UniversalTime(); sl@0: signTime = end.MicroSecondsFrom(start); sl@0: TReal signtime = I64REAL(signTime.Int64()); sl@0: sl@0: testSig = signer->SignL(digestPtr); sl@0: iResult = ETrue; sl@0: if (!(*testSig==*iSignature)) sl@0: { sl@0: iResult = EFalse; sl@0: } sl@0: delete testSig; sl@0: sl@0: CleanupStack::PopAndDestroy(); // signer sl@0: sl@0: __UHEAP_MARKEND; sl@0: sl@0: if (iResult) sl@0: { sl@0: TReal rate = I64REAL(signTime.Int64()) / noSignings; sl@0: TBuf<256> buf; sl@0: _LIT(KSignTime, "\tSign Time: %f us/signing (%i signings in %f us)\r\n"); sl@0: buf.Format(KSignTime, rate, noSignings, signtime); sl@0: iOut.writeString(buf); sl@0: } sl@0: else sl@0: { sl@0: _LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n"); sl@0: iOut.writeString(KNoTimingInfo); sl@0: } sl@0: } sl@0: sl@0: void CRSASignVectorCRT::DoPerformActionL() sl@0: { sl@0: __UHEAP_MARK; sl@0: sl@0: CRSAPKCS1v15Signer* signer = CRSAPKCS1v15Signer::NewL(*iPrivKey); sl@0: CleanupStack::PushL(signer); sl@0: sl@0: TPtrC8 digestPtr = iDigestInfo->Des(); sl@0: const CRSASignature* testSig = signer->SignL(digestPtr); sl@0: CleanupStack::PushL(const_cast<CRSASignature*>(testSig)); sl@0: sl@0: #ifdef _DEBUG sl@0: HBufC8* theResultSig = testSig->S().BufferLC(); sl@0: HBufC8* theExpectedSig = iSignature->S().BufferLC(); sl@0: iResult = (theResultSig->Compare(*theExpectedSig)) ==0 ? ETrue : EFalse; sl@0: CleanupStack::PopAndDestroy(2); sl@0: #endif sl@0: sl@0: iResult = (*testSig == *iSignature); sl@0: sl@0: CleanupStack::PopAndDestroy(const_cast<CRSASignature*>(testSig)); sl@0: CleanupStack::PopAndDestroy(signer); sl@0: sl@0: __UHEAP_MARKEND; sl@0: } sl@0: sl@0: sl@0: //////////////////////////////////////////////////////////////////////////////// sl@0: // CRSAVerifyVector sl@0: //////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: CTestAction* CRSAVerifyVector::NewL(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CTestAction* self = CRSAVerifyVector::NewLC(aFs, aConsole, aOut, aTestActionSpec); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: CTestAction* CRSAVerifyVector::NewLC(RFs& aFs, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut, sl@0: const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CRSAVerifyVector* self = new(ELeave) CRSAVerifyVector(aFs, aConsole, aOut); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aTestActionSpec); sl@0: return self; sl@0: } sl@0: sl@0: CRSAVerifyVector::~CRSAVerifyVector() sl@0: { sl@0: delete iPubKey; sl@0: delete iDigestInfo; sl@0: delete iSignature; sl@0: } sl@0: sl@0: CRSAVerifyVector::CRSAVerifyVector(RFs& /*aFs*/, sl@0: CConsoleBase& aConsole, sl@0: Output& aOut) sl@0: : CVectorTest(aConsole, aOut) sl@0: { sl@0: } sl@0: sl@0: void CRSAVerifyVector::ConstructL(const TTestActionSpec& aTestActionSpec) sl@0: { sl@0: CVectorTest::ConstructL(aTestActionSpec); sl@0: sl@0: iPubKey = VectorUtils::ReadRSAPublicKeyL(aTestActionSpec.iActionBody); sl@0: sl@0: TPtrC8 digestInfoIn = Input::ParseElement(aTestActionSpec.iActionBody, KDigestInfoStart, KDigestInfoEnd); sl@0: iDigestInfo = VectorUtils::ParseBinaryL(digestInfoIn); sl@0: sl@0: TPtrC8 signatureIn = Input::ParseElement(aTestActionSpec.iActionBody, KSignatureStart, KSignatureEnd); sl@0: RInteger integer = VectorUtils::ParseIntegerL(signatureIn); sl@0: CleanupStack::PushL(integer); sl@0: iSignature = CRSASignature::NewL(integer); sl@0: CleanupStack::Pop(&integer); sl@0: } sl@0: sl@0: void CRSAVerifyVector::DoPerformanceTestActionL() sl@0: { sl@0: __UHEAP_MARK; sl@0: sl@0: sl@0: TTimeIntervalMicroSeconds verifyTime(0); sl@0: TTime start, end; sl@0: TTimeIntervalSeconds diff(0); sl@0: const TTimeIntervalSeconds KIterationTime(iPerfTestIterations); sl@0: sl@0: TInt noVerifies = 0; sl@0: sl@0: CRSAPKCS1v15Verifier *verifier = CRSAPKCS1v15Verifier::NewLC(*iPubKey); sl@0: sl@0: // Time verification sl@0: start.UniversalTime(); sl@0: while (diff < KIterationTime) sl@0: { sl@0: iResult = verifier->VerifyL(*iDigestInfo, *iSignature); sl@0: if (!iResult) sl@0: { sl@0: break; sl@0: } sl@0: noVerifies++; sl@0: end.UniversalTime(); sl@0: end.SecondsFrom(start, diff); sl@0: } sl@0: end.UniversalTime(); sl@0: verifyTime = end.MicroSecondsFrom(start); sl@0: TReal verifytime = I64REAL(verifyTime.Int64()); sl@0: sl@0: CleanupStack::PopAndDestroy(verifier); sl@0: sl@0: __UHEAP_MARKEND; sl@0: sl@0: if (iResult) sl@0: { sl@0: TReal rate = I64REAL(verifyTime.Int64()) / noVerifies; sl@0: TBuf<256> buf; sl@0: _LIT(KVerifyTime, "\tVerify Time: %f us/verify (%i verifies in %f us)\r\n"); sl@0: buf.Format(KVerifyTime, rate, noVerifies, verifytime); sl@0: iOut.writeString(buf); sl@0: } sl@0: else sl@0: { sl@0: _LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n"); sl@0: iOut.writeString(KNoTimingInfo); sl@0: } sl@0: } sl@0: sl@0: void CRSAVerifyVector::DoPerformActionL() sl@0: { sl@0: __UHEAP_MARK; sl@0: sl@0: CRSAPKCS1v15Verifier* verifier = CRSAPKCS1v15Verifier::NewL(*iPubKey); sl@0: CleanupStack::PushL(verifier); sl@0: iResult = verifier->VerifyL(*iDigestInfo, *iSignature); sl@0: sl@0: CleanupStack::PopAndDestroy(verifier); sl@0: sl@0: __UHEAP_MARKEND; sl@0: }