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