os/security/crypto/weakcrypto/test/tasymmetric/tdsaprimegen.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
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
	iPrimeCert = CDSAPrimeCertificate::NewL(*seed, counter);
sl@0
    81
	CleanupStack::PopAndDestroy(seed);
sl@0
    82
	}
sl@0
    83
sl@0
    84
void CDSAPrimeGen::DoPerformPrerequisite(TRequestStatus& aStatus)
sl@0
    85
	{
sl@0
    86
	TRequestStatus* status = &aStatus;
sl@0
    87
	User::RequestComplete(status, KErrNone);
sl@0
    88
	iActionState = CTestAction::EAction;
sl@0
    89
	}
sl@0
    90
sl@0
    91
void CDSAPrimeGen::DoPerformPostrequisite(TRequestStatus& aStatus)
sl@0
    92
	{
sl@0
    93
	TRequestStatus* status = &aStatus;
sl@0
    94
sl@0
    95
	iFinished = ETrue;
sl@0
    96
	User::RequestComplete(status, KErrNone);
sl@0
    97
	}
sl@0
    98
sl@0
    99
void CDSAPrimeGen::DoReportAction(void)
sl@0
   100
	{
sl@0
   101
	}
sl@0
   102
sl@0
   103
void CDSAPrimeGen::DoCheckResult(TInt)
sl@0
   104
	{
sl@0
   105
	if (iResult)
sl@0
   106
		iConsole.Printf(_L("."));
sl@0
   107
	else
sl@0
   108
		iConsole.Printf(_L("X"));
sl@0
   109
	}
sl@0
   110
sl@0
   111
void CDSAPrimeGen::PerformAction(TRequestStatus& aStatus)
sl@0
   112
	{
sl@0
   113
	__UHEAP_MARK;
sl@0
   114
	TRequestStatus* status = &aStatus;
sl@0
   115
	iResult = EFalse;
sl@0
   116
sl@0
   117
	CRandomSetSource* rng = new(ELeave)CRandomSetSource(iPrimeCert->Seed());
sl@0
   118
	SetThreadRandomLC(rng);
sl@0
   119
	CDSAKeyPair* dsaPair = CDSAKeyPair::NewLC(iKeyBits);
sl@0
   120
	
sl@0
   121
	//this is more or less just calling the prime generation routine over again
sl@0
   122
	//but it does test if ValidatePrimesL and the NewLC generate the same primes
sl@0
   123
	iResult = dsaPair->PublicKey().ValidatePrimesL(*iPrimeCert);
sl@0
   124
sl@0
   125
	//we've already checked that the ValidatePrime + NewLC give same results
sl@0
   126
	//this now checks against the vectors in the test scripts
sl@0
   127
	if(iP != dsaPair->PublicKey().P() ||
sl@0
   128
		iQ != dsaPair->PublicKey().Q() || 
sl@0
   129
		iP != dsaPair->PrivateKey().P() ||
sl@0
   130
		iQ != dsaPair->PrivateKey().Q() )
sl@0
   131
		{
sl@0
   132
		iResult = EFalse;
sl@0
   133
		}
sl@0
   134
sl@0
   135
	CleanupStack::PopAndDestroy(dsaPair); 
sl@0
   136
	CleanupStack::PopAndDestroy(); //SetThreadRandomLC
sl@0
   137
	User::RequestComplete(status, KErrNone);
sl@0
   138
	iActionState = CTestAction::EPostrequisite;
sl@0
   139
	__UHEAP_MARKEND;
sl@0
   140
	}
sl@0
   141