os/security/crypto/weakcrypto/test/tasymmetric/tdsavector.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcrypto/test/tasymmetric/tdsavector.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,213 @@
     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 "tdsavector.h"
    1.23 +#include "tvectorutils.h"
    1.24 +#include "t_input.h"
    1.25 +#include <bigint.h>
    1.26 +#include "tbrokenrandom.h"
    1.27 +
    1.28 +////////////////////////////////////////////////////////////////////////////////
    1.29 +// CDSASignVector
    1.30 +////////////////////////////////////////////////////////////////////////////////
    1.31 +
    1.32 +CTestAction* CDSASignVector::NewL(RFs& aFs,
    1.33 +                                  CConsoleBase& aConsole,
    1.34 +                                  Output& aOut, 
    1.35 +                                  const TTestActionSpec& aTestActionSpec)
    1.36 +	{
    1.37 +	CTestAction* self = CDSASignVector::NewLC(aFs, aConsole, aOut, aTestActionSpec);
    1.38 +	CleanupStack::Pop();
    1.39 +	return self;
    1.40 +	}
    1.41 +
    1.42 +CTestAction* CDSASignVector::NewLC(RFs& aFs,
    1.43 +                                   CConsoleBase& aConsole,
    1.44 +                                   Output& aOut, 
    1.45 +                                   const TTestActionSpec& aTestActionSpec)
    1.46 +	{
    1.47 +	CDSASignVector* self = new(ELeave) CDSASignVector(aFs, aConsole, aOut);
    1.48 +	CleanupStack::PushL(self);
    1.49 +	self->ConstructL(aTestActionSpec);
    1.50 +	return self;
    1.51 +	}
    1.52 +
    1.53 +CDSASignVector::~CDSASignVector()
    1.54 +	{
    1.55 +    delete iPrivKey;
    1.56 +	delete iSignature;
    1.57 +	delete iK;
    1.58 +	delete iSigInput;
    1.59 +	}
    1.60 +
    1.61 +CDSASignVector::CDSASignVector(RFs& /*aFs*/, 
    1.62 +                               CConsoleBase& aConsole,
    1.63 +                               Output& aOut)					 
    1.64 +    : CVectorTest(aConsole, aOut)
    1.65 +	{
    1.66 +	}
    1.67 +
    1.68 +void CDSASignVector::ConstructL(const TTestActionSpec& aTestActionSpec)
    1.69 +	{
    1.70 +	CVectorTest::ConstructL(aTestActionSpec);
    1.71 +
    1.72 +    iPrivKey = VectorUtils::ReadDSAPrivateKeyL(aTestActionSpec.iActionBody);
    1.73 +
    1.74 +	iMessage.Set(Input::ParseElement(aTestActionSpec.iActionBody, _L8("<m>")));
    1.75 +	iK = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<k>"));
    1.76 +
    1.77 +	iSignature = VectorUtils::ReadDSASignatureL(aTestActionSpec.iActionBody);
    1.78 +	iSigInput = CHashingSignatureInput::NewL(CMessageDigest::ESHA1);
    1.79 +	iSigInput->Update(iMessage);
    1.80 +	}
    1.81 +
    1.82 +void CDSASignVector::DoPerformActionL()
    1.83 +	{
    1.84 +	__UHEAP_MARK;
    1.85 +
    1.86 +	CRandomSetSource* random = new(ELeave)CRandomSetSource(*iK);
    1.87 +	SetThreadRandomLC(random);
    1.88 +
    1.89 +    CDSASigner* signer = CDSASigner::NewLC(*iPrivKey);
    1.90 +	const CDSASignature* testSig = signer->SignL(iSigInput->Final());
    1.91 +    iResult = (*testSig == *iSignature);
    1.92 +
    1.93 +	delete testSig;
    1.94 +    CleanupStack::PopAndDestroy(signer);
    1.95 +	CleanupStack::PopAndDestroy(); //SetThreadRandomLC
    1.96 +
    1.97 +	__UHEAP_MARKEND;
    1.98 +	}
    1.99 +
   1.100 +void CDSASignVector::DoPerformanceTestActionL()
   1.101 +	{
   1.102 +	iResult = ETrue;
   1.103 +	}
   1.104 +
   1.105 +void CDSASignVector::DoCheckResult(TInt /*aError*/)
   1.106 +	{
   1.107 +//	If using Keith's fixed up testframework for testing failures, iResult will 
   1.108 +//	have already been corrected for a deliberate fail result.
   1.109 +//	If not using Keith's testframework iResult is still a fail result at this
   1.110 +//	point, so now's the time to adjust for deliberate failure.
   1.111 +
   1.112 +	if (!iResult)
   1.113 +		iResult = (iResult && iExpectedResult) || (!iResult && !iExpectedResult);
   1.114 +	
   1.115 +	if( iResult == EFalse )
   1.116 +		{
   1.117 +		iConsole.Printf(_L("X"));
   1.118 +		}
   1.119 +	else 
   1.120 +		{
   1.121 +		iConsole.Printf(_L("."));
   1.122 +		}
   1.123 +	}
   1.124 +
   1.125 +////////////////////////////////////////////////////////////////////////////////
   1.126 +// CDSAVerifyVector
   1.127 +////////////////////////////////////////////////////////////////////////////////
   1.128 +
   1.129 +CTestAction* CDSAVerifyVector::NewL(RFs& aFs,
   1.130 +                                  CConsoleBase& aConsole,
   1.131 +                                  Output& aOut, 
   1.132 +                                  const TTestActionSpec& aTestActionSpec)
   1.133 +	{
   1.134 +	CTestAction* self = CDSAVerifyVector::NewLC(aFs, aConsole, aOut, aTestActionSpec);
   1.135 +	CleanupStack::Pop();
   1.136 +	return self;
   1.137 +	}
   1.138 +
   1.139 +CTestAction* CDSAVerifyVector::NewLC(RFs& aFs,
   1.140 +                                   CConsoleBase& aConsole,
   1.141 +                                   Output& aOut, 
   1.142 +                                   const TTestActionSpec& aTestActionSpec)
   1.143 +	{
   1.144 +	CDSAVerifyVector* self = new(ELeave) CDSAVerifyVector(aFs, aConsole, aOut);
   1.145 +	CleanupStack::PushL(self);
   1.146 +	self->ConstructL(aTestActionSpec);
   1.147 +	return self;
   1.148 +	}
   1.149 +
   1.150 +CDSAVerifyVector::~CDSAVerifyVector()
   1.151 +	{
   1.152 +    delete iPubKey;
   1.153 +	delete iSignature;
   1.154 +	delete iMessage;
   1.155 +	delete iSigInput;
   1.156 +	}
   1.157 +
   1.158 +CDSAVerifyVector::CDSAVerifyVector(RFs& /*aFs*/, 
   1.159 +                                   CConsoleBase& aConsole,
   1.160 +                                   Output& aOut)					 
   1.161 +    : CVectorTest(aConsole, aOut)
   1.162 +	{
   1.163 +	}
   1.164 +
   1.165 +void CDSAVerifyVector::ConstructL(const TTestActionSpec& aTestActionSpec)
   1.166 +	{
   1.167 +	CVectorTest::ConstructL(aTestActionSpec);
   1.168 +
   1.169 +    iPubKey = VectorUtils::ReadDSAPublicKeyL(aTestActionSpec.iActionBody);
   1.170 +
   1.171 +	TPtrC8 message(Input::ParseElement(aTestActionSpec.iActionBody, _L8("<m>")));
   1.172 +	if (message.Length()==0)	
   1.173 +		iMessage = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<hexm>"));
   1.174 +	else
   1.175 +		iMessage = message.AllocL();
   1.176 +		
   1.177 +	iSignature = VectorUtils::ReadDSASignatureL(aTestActionSpec.iActionBody);
   1.178 +	iSigInput = CHashingSignatureInput::NewL(CMessageDigest::ESHA1);
   1.179 +	iSigInput->Update(*iMessage);
   1.180 +	}	
   1.181 +
   1.182 +void CDSAVerifyVector::DoPerformActionL()
   1.183 +	{
   1.184 +	__UHEAP_MARK;
   1.185 +
   1.186 +    CDSAVerifier* verifier = CDSAVerifier::NewLC(*iPubKey);
   1.187 +    iResult = verifier->VerifyL(iSigInput->Final(), *iSignature);
   1.188 +
   1.189 +    CleanupStack::PopAndDestroy(verifier);
   1.190 +	__UHEAP_MARKEND;
   1.191 +	}
   1.192 +
   1.193 +void CDSAVerifyVector::DoPerformanceTestActionL()
   1.194 +	{
   1.195 +	iResult = ETrue;
   1.196 +	}
   1.197 +
   1.198 +void CDSAVerifyVector::DoCheckResult(TInt /*aError*/)
   1.199 +	{
   1.200 +//	If using Keith's fixed up testframework for testing failures, iResult will 
   1.201 +//	have already been corrected for a deliberate fail result.
   1.202 +//	If not using Keith's testframework iResult is still a fail result at this
   1.203 +//	point, so now's the time to adjust for deliberate failure.
   1.204 +
   1.205 +	if (!iResult)
   1.206 +		iResult = (iResult && iExpectedResult) || (!iResult && !iExpectedResult);
   1.207 +	
   1.208 +	if( iResult == EFalse )
   1.209 +		{
   1.210 +		iConsole.Printf(_L("X"));
   1.211 +		}
   1.212 +	else 
   1.213 +		{
   1.214 +		iConsole.Printf(_L("."));
   1.215 +		}
   1.216 +	}