os/security/crypto/weakcryptospi/test/tasymmetric/tdsaprimegen.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 *
    16 */
    17 
    18 
    19 #include "tdsaprimegen.h"
    20 #include "t_input.h"
    21 #include <asymmetric.h>
    22 #include <random.h>
    23 #include <padding.h>
    24 #include <bigint.h>
    25 #include "tbrokenrandom.h"
    26 
    27 CTestAction* CDSAPrimeGen::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut, 
    28 	const TTestActionSpec& aTestActionSpec)
    29 	{
    30 	CTestAction* self = CDSAPrimeGen::NewLC(aFs, aConsole,
    31 		aOut, aTestActionSpec);
    32 	CleanupStack::Pop();
    33 	return self;
    34 	}
    35 
    36 CTestAction* CDSAPrimeGen::NewLC(RFs& aFs, CConsoleBase& aConsole, Output& aOut, 
    37 	const TTestActionSpec& aTestActionSpec)
    38 	{
    39 	CDSAPrimeGen* self = new(ELeave) CDSAPrimeGen(aFs, aConsole, aOut);
    40 	CleanupStack::PushL(self);
    41 	self->ConstructL(aTestActionSpec);
    42 	return self;
    43 	}
    44 
    45 CDSAPrimeGen::~CDSAPrimeGen()
    46 	{
    47 	delete iBody;
    48 	iP.Close();
    49 	iQ.Close();
    50 	delete const_cast<CDSAPrimeCertificate*>(iPrimeCert);
    51 	}
    52 
    53 CDSAPrimeGen::CDSAPrimeGen(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
    54 	: CTestAction(aConsole, aOut), iFs(aFs)
    55 	{
    56 	}
    57 
    58 void CDSAPrimeGen::ConstructL(const TTestActionSpec& aTestActionSpec)
    59 	{
    60 	CTestAction::ConstructL(aTestActionSpec);
    61 	iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
    62 	iBody->Des().Copy(aTestActionSpec.iActionBody);
    63 	
    64 
    65 	iKeyBits = Input::ParseIntElement(*iBody, _L8("<keybits>"), _L8("</keybits>"));
    66 
    67 	HBufC8* p = Input::ParseElementHexL(*iBody, _L8("<p>"));
    68 	CleanupStack::PushL(p);
    69 	iP = RInteger::NewL(*p);
    70 	CleanupStack::PopAndDestroy(p);
    71 
    72 	HBufC8* q = Input::ParseElementHexL(*iBody, _L8("<q>"));
    73 	CleanupStack::PushL(q);
    74 	iQ = RInteger::NewL(*q);
    75 	CleanupStack::PopAndDestroy(q);
    76 
    77 	TUint counter = Input::ParseIntElement(*iBody, _L8("<c>"), _L8("</c>"));
    78 	HBufC8* seed = Input::ParseElementHexL(*iBody, _L8("<seed>"));
    79 	CleanupStack::PushL(seed);
    80 	
    81 	iExpectedResult = Input::ParseIntElement(*iBody, _L8("<result>"), _L8("</result>"));
    82 	
    83 	iPrimeCert = CDSAPrimeCertificate::NewL(*seed, counter);
    84 	CleanupStack::PopAndDestroy(seed);
    85 	}
    86 
    87 void CDSAPrimeGen::DoPerformPrerequisite(TRequestStatus& aStatus)
    88 	{
    89 	TRequestStatus* status = &aStatus;
    90 	User::RequestComplete(status, KErrNone);
    91 	iActionState = CTestAction::EAction;
    92 	}
    93 
    94 void CDSAPrimeGen::DoPerformPostrequisite(TRequestStatus& aStatus)
    95 	{
    96 	TRequestStatus* status = &aStatus;
    97 
    98 	iFinished = ETrue;
    99 	User::RequestComplete(status, KErrNone);
   100 	}
   101 
   102 void CDSAPrimeGen::DoReportAction(void)
   103 	{
   104 	}
   105 
   106 void CDSAPrimeGen::DoCheckResult(TInt)
   107 	{
   108 	if (iResult)
   109 		{
   110 		iConsole.Printf(_L("."));
   111 		}
   112 	else if (iResult == iExpectedResult)
   113 		{
   114 		iKnownFailure = ETrue;
   115 		iConsole.Printf(_L("."));
   116 		}
   117 	else
   118 		{
   119 		iConsole.Printf(_L("X"));
   120 		}
   121 	}
   122 
   123 void CDSAPrimeGen::PerformAction(TRequestStatus& aStatus)
   124 	{
   125 	__UHEAP_MARK;
   126 	TRequestStatus* status = &aStatus;
   127 	iResult = EFalse;
   128 
   129 	CRandomSetSource* rng = new(ELeave)CRandomSetSource(iPrimeCert->Seed());
   130 	SetThreadRandomLC(rng);
   131 	CDSAKeyPair* dsaPair = CDSAKeyPair::NewLC(iKeyBits);
   132 	
   133 	//this is more or less just calling the prime generation routine over again
   134 	//but it does test if ValidatePrimesL and the NewLC generate the same primes
   135 	iResult = dsaPair->PublicKey().ValidatePrimesL(*iPrimeCert);
   136 
   137 	//we've already checked that the ValidatePrime + NewLC give same results
   138 	//this now checks against the vectors in the test scripts
   139 	if(iP != dsaPair->PublicKey().P() ||
   140 		iQ != dsaPair->PublicKey().Q() || 
   141 		iP != dsaPair->PrivateKey().P() ||
   142 		iQ != dsaPair->PrivateKey().Q() )
   143 		{
   144 		iResult = EFalse;
   145 		}
   146 	
   147 	// Get the prime certificate and compare the seed and counter values with the initial value supplied.
   148 	const CDSAPrimeCertificate& primeCert = dsaPair->PrimeCertificate();
   149 
   150 	if (((iPrimeCert->Seed()).Compare(primeCert.Seed()) != 0) ||
   151 		(iPrimeCert->Counter() != primeCert.Counter()))
   152 		{
   153 		iResult = EFalse;
   154 		}
   155 	
   156 	// Validate the prime length values. 
   157 	if (CDSAParameters::ValidPrimeLength(256) ||  /* for <512 */
   158 		CDSAParameters::ValidPrimeLength(1088) || /* for >1024 */
   159 		CDSAParameters::ValidPrimeLength(775))    /* for non multiple of 64 */
   160 		{
   161 		iResult = EFalse;
   162 		}
   163 
   164 	CleanupStack::PopAndDestroy(dsaPair); 
   165 	CleanupStack::PopAndDestroy(); //SetThreadRandomLC
   166 	User::RequestComplete(status, KErrNone);
   167 	iActionState = CTestAction::EPostrequisite;
   168 	__UHEAP_MARKEND;
   169 	}
   170