1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcrypto/test/tbigint/tprimegenvector.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,142 @@
1.4 +/*
1.5 +* Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* tprimeproblem.cpp
1.19 +*
1.20 +*/
1.21 +
1.22 +
1.23 +#include <bigint.h>
1.24 +#include <random.h>
1.25 +#include "tprimegenvector.h"
1.26 +#include "t_input.h"
1.27 +#include "t_output.h"
1.28 +#include "tutils.h"
1.29 +#include "../../source/bigint/mont.h"
1.30 +
1.31 +CTestAction* CPrimeGenVector::NewL(RFs& aFs, CConsoleBase& aConsole,
1.32 + Output& aOut, const TTestActionSpec& aTestActionSpec)
1.33 + {
1.34 + CTestAction* self = CPrimeGenVector::NewLC(aFs, aConsole,
1.35 + aOut, aTestActionSpec);
1.36 + CleanupStack::Pop();
1.37 + return self;
1.38 + }
1.39 +
1.40 +CTestAction* CPrimeGenVector::NewLC(RFs& aFs, CConsoleBase& aConsole,
1.41 + Output& aOut, const TTestActionSpec& aTestActionSpec)
1.42 + {
1.43 + CPrimeGenVector* self = new(ELeave) CPrimeGenVector(aFs, aConsole, aOut);
1.44 + CleanupStack::PushL(self);
1.45 + self->ConstructL(aTestActionSpec);
1.46 + return self;
1.47 + }
1.48 +
1.49 +CPrimeGenVector::~CPrimeGenVector()
1.50 + {
1.51 + delete iBody;
1.52 + delete iRandomDes;
1.53 + delete iPrimeDes;
1.54 + iRandom.Close();
1.55 + iPrime.Close();
1.56 + }
1.57 +
1.58 +CPrimeGenVector::CPrimeGenVector(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.59 + : CTestAction(aConsole, aOut), iFs(aFs)
1.60 + {
1.61 + }
1.62 +
1.63 +void CPrimeGenVector::ConstructL(const TTestActionSpec& aTestActionSpec)
1.64 + {
1.65 + CTestAction::ConstructL(aTestActionSpec);
1.66 +
1.67 + iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
1.68 + iBody->Des().Copy(aTestActionSpec.iActionBody);
1.69 +
1.70 + iBits = Input::ParseIntElement(*iBody, _L8("<bits>"), _L8("</bits>"));
1.71 + iRandomDes = Input::ParseElementHexL(*iBody, _L8("<random>"));
1.72 + iPrimeDes = Input::ParseElementHexL(*iBody, _L8("<prime>"));
1.73 + }
1.74 +
1.75 +void CPrimeGenVector::DoPerformPrerequisite(TRequestStatus& aStatus)
1.76 + {
1.77 + iRandom = RInteger::NewL(*iRandomDes);
1.78 + iPrime = RInteger::NewL(*iPrimeDes);
1.79 + TRequestStatus* status = &aStatus;
1.80 + User::RequestComplete(status, KErrNone);
1.81 + iActionState = CTestAction::EAction;
1.82 + }
1.83 +
1.84 +void CPrimeGenVector::DoPerformPostrequisite(TRequestStatus& aStatus)
1.85 + {
1.86 + TRequestStatus* status = &aStatus;
1.87 + iFinished = ETrue;
1.88 + User::RequestComplete(status, KErrNone);
1.89 + }
1.90 +
1.91 +void CPrimeGenVector::DoReportAction(void)
1.92 + {
1.93 + }
1.94 +
1.95 +void CPrimeGenVector::DoCheckResult(TInt)
1.96 + {
1.97 + iResult = (iResult && iExpectedResult) || (!iResult && !iExpectedResult);
1.98 + if (iResult)
1.99 + iConsole.Printf(_L("."));
1.100 + else
1.101 + iConsole.Printf(_L("X"));
1.102 + }
1.103 +
1.104 +void CPrimeGenVector::PerformAction(TRequestStatus& aStatus)
1.105 + {
1.106 + TRAPD(res, PerformActionL());
1.107 + __ASSERT_ALWAYS(!res, User::Panic(_L("CPrimeGenVector::PerformAction"), res));
1.108 + TRequestStatus* status = &aStatus;
1.109 + User::RequestComplete(status, KErrNone);
1.110 + iActionState = CTestAction::EPostrequisite;
1.111 + }
1.112 +
1.113 +void CPrimeGenVector::PerformActionL()
1.114 + {
1.115 + __UHEAP_MARK;
1.116 +
1.117 + iResult = EFalse;;
1.118 +
1.119 + //Returned number should be the next prime after our initial "random" number
1.120 +
1.121 + //This CRandomSetSource stuff is a little dodgy as:
1.122 + // 1) it has no NewL's with is fine as long as it doesn't allocate memory
1.123 + // 2) more importantly, the prime generation routines use random numbers to
1.124 + // determine whether or not the numbers it generates are prime.
1.125 + // Since CRandomSetSource has already exhausted its given list of "random"
1.126 + // numbers it returns 0's for use with anything else that calls it. It
1.127 + // makes the primality testing a little bogus if you were using it for real
1.128 + // things, but the test vectors that are there have already been verified
1.129 + // and are known to be prime.
1.130 + CRandomSetSource* incrementingRandom = new(ELeave)CRandomSetSource(*iRandomDes);
1.131 + SetThreadRandomLC(incrementingRandom);
1.132 +
1.133 + RInteger prime = RInteger::NewPrimeL(iBits);
1.134 +
1.135 + if(prime == iPrime)
1.136 + {
1.137 + iResult = ETrue;
1.138 + }
1.139 + prime.Close();
1.140 +
1.141 + CleanupStack::PopAndDestroy(1); //SetThreadRandomLC
1.142 +
1.143 + __UHEAP_MARKEND;
1.144 + }
1.145 +