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 "tdsavector.h"
sl@0: #include "tvectorutils.h"
sl@0: #include "t_input.h"
sl@0: #include <bigint.h>
sl@0: #include "tbrokenrandom.h"
sl@0: 
sl@0: ////////////////////////////////////////////////////////////////////////////////
sl@0: // CDSASignVector
sl@0: ////////////////////////////////////////////////////////////////////////////////
sl@0: 
sl@0: CTestAction* CDSASignVector::NewL(RFs& aFs,
sl@0:                                   CConsoleBase& aConsole,
sl@0:                                   Output& aOut, 
sl@0:                                   const TTestActionSpec& aTestActionSpec)
sl@0: 	{
sl@0: 	CTestAction* self = CDSASignVector::NewLC(aFs, aConsole, aOut, aTestActionSpec);
sl@0: 	CleanupStack::Pop();
sl@0: 	return self;
sl@0: 	}
sl@0: 
sl@0: CTestAction* CDSASignVector::NewLC(RFs& aFs,
sl@0:                                    CConsoleBase& aConsole,
sl@0:                                    Output& aOut, 
sl@0:                                    const TTestActionSpec& aTestActionSpec)
sl@0: 	{
sl@0: 	CDSASignVector* self = new(ELeave) CDSASignVector(aFs, aConsole, aOut);
sl@0: 	CleanupStack::PushL(self);
sl@0: 	self->ConstructL(aTestActionSpec);
sl@0: 	return self;
sl@0: 	}
sl@0: 
sl@0: CDSASignVector::~CDSASignVector()
sl@0: 	{
sl@0:     delete iPrivKey;
sl@0: 	delete iSignature;
sl@0: 	delete iK;
sl@0: 	delete iSigInput;
sl@0: 	}
sl@0: 
sl@0: CDSASignVector::CDSASignVector(RFs& /*aFs*/, 
sl@0:                                CConsoleBase& aConsole,
sl@0:                                Output& aOut)					 
sl@0:     : CVectorTest(aConsole, aOut)
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: void CDSASignVector::ConstructL(const TTestActionSpec& aTestActionSpec)
sl@0: 	{
sl@0: 	CVectorTest::ConstructL(aTestActionSpec);
sl@0: 
sl@0:     iPrivKey = VectorUtils::ReadDSAPrivateKeyL(aTestActionSpec.iActionBody);
sl@0: 
sl@0: 	iMessage.Set(Input::ParseElement(aTestActionSpec.iActionBody, _L8("<m>")));
sl@0: 	iK = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<k>"));
sl@0: 
sl@0: 	iSignature = VectorUtils::ReadDSASignatureL(aTestActionSpec.iActionBody);
sl@0: 	iSigInput = CHashingSignatureInput::NewL(CMessageDigest::ESHA1);
sl@0: 	iSigInput->Update(iMessage);
sl@0: 	}
sl@0: 
sl@0: void CDSASignVector::DoPerformActionL()
sl@0: 	{
sl@0: 	__UHEAP_MARK;
sl@0: 
sl@0: 	CRandomSetSource* random = new(ELeave)CRandomSetSource(*iK);
sl@0: 	SetThreadRandomLC(random);
sl@0: 
sl@0:     CDSASigner* signer = CDSASigner::NewLC(*iPrivKey);
sl@0: 	const CDSASignature* testSig = signer->SignL(iSigInput->Final());
sl@0:     iResult = (*testSig == *iSignature);
sl@0: 
sl@0: 	delete testSig;
sl@0:     CleanupStack::PopAndDestroy(signer);
sl@0: 	CleanupStack::PopAndDestroy(); //SetThreadRandomLC
sl@0: 
sl@0: 	__UHEAP_MARKEND;
sl@0: 	}
sl@0: 
sl@0: void CDSASignVector::DoPerformanceTestActionL()
sl@0: 	{
sl@0: 	iResult = ETrue;
sl@0: 	}
sl@0: 
sl@0: void CDSASignVector::DoCheckResult(TInt /*aError*/)
sl@0: 	{
sl@0: //	If using Keith's fixed up testframework for testing failures, iResult will 
sl@0: //	have already been corrected for a deliberate fail result.
sl@0: //	If not using Keith's testframework iResult is still a fail result at this
sl@0: //	point, so now's the time to adjust for deliberate failure.
sl@0: 
sl@0: 	if (!iResult)
sl@0: 		iResult = (iResult && iExpectedResult) || (!iResult && !iExpectedResult);
sl@0: 	
sl@0: 	if( iResult == EFalse )
sl@0: 		{
sl@0: 		iConsole.Printf(_L("X"));
sl@0: 		}
sl@0: 	else 
sl@0: 		{
sl@0: 		iConsole.Printf(_L("."));
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: ////////////////////////////////////////////////////////////////////////////////
sl@0: // CDSAVerifyVector
sl@0: ////////////////////////////////////////////////////////////////////////////////
sl@0: 
sl@0: CTestAction* CDSAVerifyVector::NewL(RFs& aFs,
sl@0:                                   CConsoleBase& aConsole,
sl@0:                                   Output& aOut, 
sl@0:                                   const TTestActionSpec& aTestActionSpec)
sl@0: 	{
sl@0: 	CTestAction* self = CDSAVerifyVector::NewLC(aFs, aConsole, aOut, aTestActionSpec);
sl@0: 	CleanupStack::Pop();
sl@0: 	return self;
sl@0: 	}
sl@0: 
sl@0: CTestAction* CDSAVerifyVector::NewLC(RFs& aFs,
sl@0:                                    CConsoleBase& aConsole,
sl@0:                                    Output& aOut, 
sl@0:                                    const TTestActionSpec& aTestActionSpec)
sl@0: 	{
sl@0: 	CDSAVerifyVector* self = new(ELeave) CDSAVerifyVector(aFs, aConsole, aOut);
sl@0: 	CleanupStack::PushL(self);
sl@0: 	self->ConstructL(aTestActionSpec);
sl@0: 	return self;
sl@0: 	}
sl@0: 
sl@0: CDSAVerifyVector::~CDSAVerifyVector()
sl@0: 	{
sl@0:     delete iPubKey;
sl@0: 	delete iSignature;
sl@0: 	delete iMessage;
sl@0: 	delete iSigInput;
sl@0: 	}
sl@0: 
sl@0: CDSAVerifyVector::CDSAVerifyVector(RFs& /*aFs*/, 
sl@0:                                    CConsoleBase& aConsole,
sl@0:                                    Output& aOut)					 
sl@0:     : CVectorTest(aConsole, aOut)
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: void CDSAVerifyVector::ConstructL(const TTestActionSpec& aTestActionSpec)
sl@0: 	{
sl@0: 	CVectorTest::ConstructL(aTestActionSpec);
sl@0: 
sl@0:     iPubKey = VectorUtils::ReadDSAPublicKeyL(aTestActionSpec.iActionBody);
sl@0: 
sl@0: 	TPtrC8 message(Input::ParseElement(aTestActionSpec.iActionBody, _L8("<m>")));
sl@0: 	if (message.Length()==0)	
sl@0: 		iMessage = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<hexm>"));
sl@0: 	else
sl@0: 		iMessage = message.AllocL();
sl@0: 		
sl@0: 	iSignature = VectorUtils::ReadDSASignatureL(aTestActionSpec.iActionBody);
sl@0: 	iSigInput = CHashingSignatureInput::NewL(CMessageDigest::ESHA1);
sl@0: 	iSigInput->Update(*iMessage);
sl@0: 	}	
sl@0: 
sl@0: void CDSAVerifyVector::DoPerformActionL()
sl@0: 	{
sl@0: 	__UHEAP_MARK;
sl@0: 
sl@0:     CDSAVerifier* verifier = CDSAVerifier::NewLC(*iPubKey);
sl@0:     iResult = verifier->VerifyL(iSigInput->Final(), *iSignature);
sl@0: 
sl@0:     CleanupStack::PopAndDestroy(verifier);
sl@0: 	__UHEAP_MARKEND;
sl@0: 	}
sl@0: 
sl@0: void CDSAVerifyVector::DoPerformanceTestActionL()
sl@0: 	{
sl@0: 	iResult = ETrue;
sl@0: 	}
sl@0: 
sl@0: void CDSAVerifyVector::DoCheckResult(TInt /*aError*/)
sl@0: 	{
sl@0: //	If using Keith's fixed up testframework for testing failures, iResult will 
sl@0: //	have already been corrected for a deliberate fail result.
sl@0: //	If not using Keith's testframework iResult is still a fail result at this
sl@0: //	point, so now's the time to adjust for deliberate failure.
sl@0: 
sl@0: 	if (!iResult)
sl@0: 		iResult = (iResult && iExpectedResult) || (!iResult && !iExpectedResult);
sl@0: 	
sl@0: 	if( iResult == EFalse )
sl@0: 		{
sl@0: 		iConsole.Printf(_L("X"));
sl@0: 		}
sl@0: 	else 
sl@0: 		{
sl@0: 		iConsole.Printf(_L("."));
sl@0: 		}
sl@0: 	}