crypto/weakcrypto/test/tasymmetric/tdsaprimegen.cpp
author hgs
Thu, 12 Aug 2010 21:07:10 +0530
changeset 90 8c545fea2798
parent 72 de46a57f75fb
permissions -rw-r--r--
201031_01
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     1
/*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     2
* Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     3
* All rights reserved.
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     8
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     9
* Initial Contributors:
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    11
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    12
* Contributors:
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    13
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    14
* Description: 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    15
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    16
*/
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    17
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    18
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    19
#include "tdsaprimegen.h"
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    20
#include "t_input.h"
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    21
#include <asymmetric.h>
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    22
#include <random.h>
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    23
#include <padding.h>
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    24
#include <bigint.h>
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    25
#include "tbrokenrandom.h"
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    26
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    27
CTestAction* CDSAPrimeGen::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut, 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    28
	const TTestActionSpec& aTestActionSpec)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    29
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    30
	CTestAction* self = CDSAPrimeGen::NewLC(aFs, aConsole,
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    31
		aOut, aTestActionSpec);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    32
	CleanupStack::Pop();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    33
	return self;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    34
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    35
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    36
CTestAction* CDSAPrimeGen::NewLC(RFs& aFs, CConsoleBase& aConsole, Output& aOut, 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    37
	const TTestActionSpec& aTestActionSpec)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    38
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    39
	CDSAPrimeGen* self = new(ELeave) CDSAPrimeGen(aFs, aConsole, aOut);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    40
	CleanupStack::PushL(self);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    41
	self->ConstructL(aTestActionSpec);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    42
	return self;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    43
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    44
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    45
CDSAPrimeGen::~CDSAPrimeGen()
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    46
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    47
	delete iBody;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    48
	iP.Close();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    49
	iQ.Close();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    50
	delete const_cast<CDSAPrimeCertificate*>(iPrimeCert);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    51
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    52
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    53
CDSAPrimeGen::CDSAPrimeGen(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    54
	: CTestAction(aConsole, aOut), iFs(aFs)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    55
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    56
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    57
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    58
void CDSAPrimeGen::ConstructL(const TTestActionSpec& aTestActionSpec)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    59
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    60
	CTestAction::ConstructL(aTestActionSpec);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    61
	iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    62
	iBody->Des().Copy(aTestActionSpec.iActionBody);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    63
	
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    64
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    65
	iKeyBits = Input::ParseIntElement(*iBody, _L8("<keybits>"), _L8("</keybits>"));
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    66
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    67
	HBufC8* p = Input::ParseElementHexL(*iBody, _L8("<p>"));
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    68
	CleanupStack::PushL(p);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    69
	iP = RInteger::NewL(*p);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    70
	CleanupStack::PopAndDestroy(p);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    71
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    72
	HBufC8* q = Input::ParseElementHexL(*iBody, _L8("<q>"));
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    73
	CleanupStack::PushL(q);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    74
	iQ = RInteger::NewL(*q);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    75
	CleanupStack::PopAndDestroy(q);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    76
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    77
	TUint counter = Input::ParseIntElement(*iBody, _L8("<c>"), _L8("</c>"));
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    78
	HBufC8* seed = Input::ParseElementHexL(*iBody, _L8("<seed>"));
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    79
	CleanupStack::PushL(seed);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    80
	iPrimeCert = CDSAPrimeCertificate::NewL(*seed, counter);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    81
	CleanupStack::PopAndDestroy(seed);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    82
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    83
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    84
void CDSAPrimeGen::DoPerformPrerequisite(TRequestStatus& aStatus)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    85
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    86
	TRequestStatus* status = &aStatus;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    87
	User::RequestComplete(status, KErrNone);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    88
	iActionState = CTestAction::EAction;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    89
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    90
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    91
void CDSAPrimeGen::DoPerformPostrequisite(TRequestStatus& aStatus)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    92
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    93
	TRequestStatus* status = &aStatus;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    94
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    95
	iFinished = ETrue;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    96
	User::RequestComplete(status, KErrNone);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    97
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    98
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    99
void CDSAPrimeGen::DoReportAction(void)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   100
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   101
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   102
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   103
void CDSAPrimeGen::DoCheckResult(TInt)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   104
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   105
	if (iResult)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   106
		iConsole.Printf(_L("."));
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   107
	else
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   108
		iConsole.Printf(_L("X"));
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   109
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   110
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   111
void CDSAPrimeGen::PerformAction(TRequestStatus& aStatus)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   112
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   113
	__UHEAP_MARK;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   114
	TRequestStatus* status = &aStatus;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   115
	iResult = EFalse;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   116
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   117
	CRandomSetSource* rng = new(ELeave)CRandomSetSource(iPrimeCert->Seed());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   118
	SetThreadRandomLC(rng);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   119
	CDSAKeyPair* dsaPair = CDSAKeyPair::NewLC(iKeyBits);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   120
	
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   121
	//this is more or less just calling the prime generation routine over again
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   122
	//but it does test if ValidatePrimesL and the NewLC generate the same primes
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   123
	iResult = dsaPair->PublicKey().ValidatePrimesL(*iPrimeCert);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   124
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   125
	//we've already checked that the ValidatePrime + NewLC give same results
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   126
	//this now checks against the vectors in the test scripts
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   127
	if(iP != dsaPair->PublicKey().P() ||
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   128
		iQ != dsaPair->PublicKey().Q() || 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   129
		iP != dsaPair->PrivateKey().P() ||
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   130
		iQ != dsaPair->PrivateKey().Q() )
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   131
		{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   132
		iResult = EFalse;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   133
		}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   134
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   135
	CleanupStack::PopAndDestroy(dsaPair); 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   136
	CleanupStack::PopAndDestroy(); //SetThreadRandomLC
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   137
	User::RequestComplete(status, KErrNone);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   138
	iActionState = CTestAction::EPostrequisite;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   139
	__UHEAP_MARKEND;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   140
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   141