First public contribution.
2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include "tdsaprimegen.h"
21 #include <asymmetric.h>
25 #include "tbrokenrandom.h"
27 CTestAction* CDSAPrimeGen::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
28 const TTestActionSpec& aTestActionSpec)
30 CTestAction* self = CDSAPrimeGen::NewLC(aFs, aConsole,
31 aOut, aTestActionSpec);
36 CTestAction* CDSAPrimeGen::NewLC(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
37 const TTestActionSpec& aTestActionSpec)
39 CDSAPrimeGen* self = new(ELeave) CDSAPrimeGen(aFs, aConsole, aOut);
40 CleanupStack::PushL(self);
41 self->ConstructL(aTestActionSpec);
45 CDSAPrimeGen::~CDSAPrimeGen()
50 delete const_cast<CDSAPrimeCertificate*>(iPrimeCert);
53 CDSAPrimeGen::CDSAPrimeGen(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
54 : CTestAction(aConsole, aOut), iFs(aFs)
58 void CDSAPrimeGen::ConstructL(const TTestActionSpec& aTestActionSpec)
60 CTestAction::ConstructL(aTestActionSpec);
61 iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
62 iBody->Des().Copy(aTestActionSpec.iActionBody);
65 iKeyBits = Input::ParseIntElement(*iBody, _L8("<keybits>"), _L8("</keybits>"));
67 HBufC8* p = Input::ParseElementHexL(*iBody, _L8("<p>"));
68 CleanupStack::PushL(p);
69 iP = RInteger::NewL(*p);
70 CleanupStack::PopAndDestroy(p);
72 HBufC8* q = Input::ParseElementHexL(*iBody, _L8("<q>"));
73 CleanupStack::PushL(q);
74 iQ = RInteger::NewL(*q);
75 CleanupStack::PopAndDestroy(q);
77 TUint counter = Input::ParseIntElement(*iBody, _L8("<c>"), _L8("</c>"));
78 HBufC8* seed = Input::ParseElementHexL(*iBody, _L8("<seed>"));
79 CleanupStack::PushL(seed);
81 iExpectedResult = Input::ParseIntElement(*iBody, _L8("<result>"), _L8("</result>"));
83 iPrimeCert = CDSAPrimeCertificate::NewL(*seed, counter);
84 CleanupStack::PopAndDestroy(seed);
87 void CDSAPrimeGen::DoPerformPrerequisite(TRequestStatus& aStatus)
89 TRequestStatus* status = &aStatus;
90 User::RequestComplete(status, KErrNone);
91 iActionState = CTestAction::EAction;
94 void CDSAPrimeGen::DoPerformPostrequisite(TRequestStatus& aStatus)
96 TRequestStatus* status = &aStatus;
99 User::RequestComplete(status, KErrNone);
102 void CDSAPrimeGen::DoReportAction(void)
106 void CDSAPrimeGen::DoCheckResult(TInt)
110 iConsole.Printf(_L("."));
112 else if (iResult == iExpectedResult)
114 iKnownFailure = ETrue;
115 iConsole.Printf(_L("."));
119 iConsole.Printf(_L("X"));
123 void CDSAPrimeGen::PerformAction(TRequestStatus& aStatus)
126 TRequestStatus* status = &aStatus;
129 CRandomSetSource* rng = new(ELeave)CRandomSetSource(iPrimeCert->Seed());
130 SetThreadRandomLC(rng);
131 CDSAKeyPair* dsaPair = CDSAKeyPair::NewLC(iKeyBits);
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);
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() )
147 // Get the prime certificate and compare the seed and counter values with the initial value supplied.
148 const CDSAPrimeCertificate& primeCert = dsaPair->PrimeCertificate();
150 if (((iPrimeCert->Seed()).Compare(primeCert.Seed()) != 0) ||
151 (iPrimeCert->Counter() != primeCert.Counter()))
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 */
164 CleanupStack::PopAndDestroy(dsaPair);
165 CleanupStack::PopAndDestroy(); //SetThreadRandomLC
166 User::RequestComplete(status, KErrNone);
167 iActionState = CTestAction::EPostrequisite;