os/security/crypto/weakcryptospi/test/tasymmetric/tdsaprimegen.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/test/tasymmetric/tdsaprimegen.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,170 @@
     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 "tdsaprimegen.h"
    1.23 +#include "t_input.h"
    1.24 +#include <asymmetric.h>
    1.25 +#include <random.h>
    1.26 +#include <padding.h>
    1.27 +#include <bigint.h>
    1.28 +#include "tbrokenrandom.h"
    1.29 +
    1.30 +CTestAction* CDSAPrimeGen::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut, 
    1.31 +	const TTestActionSpec& aTestActionSpec)
    1.32 +	{
    1.33 +	CTestAction* self = CDSAPrimeGen::NewLC(aFs, aConsole,
    1.34 +		aOut, aTestActionSpec);
    1.35 +	CleanupStack::Pop();
    1.36 +	return self;
    1.37 +	}
    1.38 +
    1.39 +CTestAction* CDSAPrimeGen::NewLC(RFs& aFs, CConsoleBase& aConsole, Output& aOut, 
    1.40 +	const TTestActionSpec& aTestActionSpec)
    1.41 +	{
    1.42 +	CDSAPrimeGen* self = new(ELeave) CDSAPrimeGen(aFs, aConsole, aOut);
    1.43 +	CleanupStack::PushL(self);
    1.44 +	self->ConstructL(aTestActionSpec);
    1.45 +	return self;
    1.46 +	}
    1.47 +
    1.48 +CDSAPrimeGen::~CDSAPrimeGen()
    1.49 +	{
    1.50 +	delete iBody;
    1.51 +	iP.Close();
    1.52 +	iQ.Close();
    1.53 +	delete const_cast<CDSAPrimeCertificate*>(iPrimeCert);
    1.54 +	}
    1.55 +
    1.56 +CDSAPrimeGen::CDSAPrimeGen(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
    1.57 +	: CTestAction(aConsole, aOut), iFs(aFs)
    1.58 +	{
    1.59 +	}
    1.60 +
    1.61 +void CDSAPrimeGen::ConstructL(const TTestActionSpec& aTestActionSpec)
    1.62 +	{
    1.63 +	CTestAction::ConstructL(aTestActionSpec);
    1.64 +	iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
    1.65 +	iBody->Des().Copy(aTestActionSpec.iActionBody);
    1.66 +	
    1.67 +
    1.68 +	iKeyBits = Input::ParseIntElement(*iBody, _L8("<keybits>"), _L8("</keybits>"));
    1.69 +
    1.70 +	HBufC8* p = Input::ParseElementHexL(*iBody, _L8("<p>"));
    1.71 +	CleanupStack::PushL(p);
    1.72 +	iP = RInteger::NewL(*p);
    1.73 +	CleanupStack::PopAndDestroy(p);
    1.74 +
    1.75 +	HBufC8* q = Input::ParseElementHexL(*iBody, _L8("<q>"));
    1.76 +	CleanupStack::PushL(q);
    1.77 +	iQ = RInteger::NewL(*q);
    1.78 +	CleanupStack::PopAndDestroy(q);
    1.79 +
    1.80 +	TUint counter = Input::ParseIntElement(*iBody, _L8("<c>"), _L8("</c>"));
    1.81 +	HBufC8* seed = Input::ParseElementHexL(*iBody, _L8("<seed>"));
    1.82 +	CleanupStack::PushL(seed);
    1.83 +	
    1.84 +	iExpectedResult = Input::ParseIntElement(*iBody, _L8("<result>"), _L8("</result>"));
    1.85 +	
    1.86 +	iPrimeCert = CDSAPrimeCertificate::NewL(*seed, counter);
    1.87 +	CleanupStack::PopAndDestroy(seed);
    1.88 +	}
    1.89 +
    1.90 +void CDSAPrimeGen::DoPerformPrerequisite(TRequestStatus& aStatus)
    1.91 +	{
    1.92 +	TRequestStatus* status = &aStatus;
    1.93 +	User::RequestComplete(status, KErrNone);
    1.94 +	iActionState = CTestAction::EAction;
    1.95 +	}
    1.96 +
    1.97 +void CDSAPrimeGen::DoPerformPostrequisite(TRequestStatus& aStatus)
    1.98 +	{
    1.99 +	TRequestStatus* status = &aStatus;
   1.100 +
   1.101 +	iFinished = ETrue;
   1.102 +	User::RequestComplete(status, KErrNone);
   1.103 +	}
   1.104 +
   1.105 +void CDSAPrimeGen::DoReportAction(void)
   1.106 +	{
   1.107 +	}
   1.108 +
   1.109 +void CDSAPrimeGen::DoCheckResult(TInt)
   1.110 +	{
   1.111 +	if (iResult)
   1.112 +		{
   1.113 +		iConsole.Printf(_L("."));
   1.114 +		}
   1.115 +	else if (iResult == iExpectedResult)
   1.116 +		{
   1.117 +		iKnownFailure = ETrue;
   1.118 +		iConsole.Printf(_L("."));
   1.119 +		}
   1.120 +	else
   1.121 +		{
   1.122 +		iConsole.Printf(_L("X"));
   1.123 +		}
   1.124 +	}
   1.125 +
   1.126 +void CDSAPrimeGen::PerformAction(TRequestStatus& aStatus)
   1.127 +	{
   1.128 +	__UHEAP_MARK;
   1.129 +	TRequestStatus* status = &aStatus;
   1.130 +	iResult = EFalse;
   1.131 +
   1.132 +	CRandomSetSource* rng = new(ELeave)CRandomSetSource(iPrimeCert->Seed());
   1.133 +	SetThreadRandomLC(rng);
   1.134 +	CDSAKeyPair* dsaPair = CDSAKeyPair::NewLC(iKeyBits);
   1.135 +	
   1.136 +	//this is more or less just calling the prime generation routine over again
   1.137 +	//but it does test if ValidatePrimesL and the NewLC generate the same primes
   1.138 +	iResult = dsaPair->PublicKey().ValidatePrimesL(*iPrimeCert);
   1.139 +
   1.140 +	//we've already checked that the ValidatePrime + NewLC give same results
   1.141 +	//this now checks against the vectors in the test scripts
   1.142 +	if(iP != dsaPair->PublicKey().P() ||
   1.143 +		iQ != dsaPair->PublicKey().Q() || 
   1.144 +		iP != dsaPair->PrivateKey().P() ||
   1.145 +		iQ != dsaPair->PrivateKey().Q() )
   1.146 +		{
   1.147 +		iResult = EFalse;
   1.148 +		}
   1.149 +	
   1.150 +	// Get the prime certificate and compare the seed and counter values with the initial value supplied.
   1.151 +	const CDSAPrimeCertificate& primeCert = dsaPair->PrimeCertificate();
   1.152 +
   1.153 +	if (((iPrimeCert->Seed()).Compare(primeCert.Seed()) != 0) ||
   1.154 +		(iPrimeCert->Counter() != primeCert.Counter()))
   1.155 +		{
   1.156 +		iResult = EFalse;
   1.157 +		}
   1.158 +	
   1.159 +	// Validate the prime length values. 
   1.160 +	if (CDSAParameters::ValidPrimeLength(256) ||  /* for <512 */
   1.161 +		CDSAParameters::ValidPrimeLength(1088) || /* for >1024 */
   1.162 +		CDSAParameters::ValidPrimeLength(775))    /* for non multiple of 64 */
   1.163 +		{
   1.164 +		iResult = EFalse;
   1.165 +		}
   1.166 +
   1.167 +	CleanupStack::PopAndDestroy(dsaPair); 
   1.168 +	CleanupStack::PopAndDestroy(); //SetThreadRandomLC
   1.169 +	User::RequestComplete(status, KErrNone);
   1.170 +	iActionState = CTestAction::EPostrequisite;
   1.171 +	__UHEAP_MARKEND;
   1.172 +	}
   1.173 +