os/security/crypto/weakcryptospi/test/tbigint/tmontgomeryfb.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) 2002-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 "tmontgomeryfb.h"
sl@0
    20
#include "t_input.h"
sl@0
    21
#include "t_output.h"
sl@0
    22
#include "tutils.h"
sl@0
    23
#include <bigint.h>
sl@0
    24
#include <random.h>
sl@0
    25
#include "../../source/bigint/mont.h"
sl@0
    26
sl@0
    27
CTestAction* CMontgomeryFB::NewL(RFs& aFs, CConsoleBase& aConsole, 
sl@0
    28
	Output& aOut, const TTestActionSpec& aTestActionSpec)
sl@0
    29
	{
sl@0
    30
	CTestAction* self = CMontgomeryFB::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* CMontgomeryFB::NewLC(RFs& aFs, CConsoleBase& aConsole, 
sl@0
    37
	Output& aOut, const TTestActionSpec& aTestActionSpec)
sl@0
    38
	{
sl@0
    39
	CMontgomeryFB* self = new(ELeave) CMontgomeryFB(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
CMontgomeryFB::~CMontgomeryFB()
sl@0
    46
	{
sl@0
    47
	delete iBody;
sl@0
    48
	}
sl@0
    49
sl@0
    50
CMontgomeryFB::CMontgomeryFB(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
sl@0
    51
	: CTestAction(aConsole, aOut), iFs(aFs)
sl@0
    52
	{
sl@0
    53
	}
sl@0
    54
sl@0
    55
void CMontgomeryFB::ConstructL(const TTestActionSpec& aTestActionSpec)
sl@0
    56
	{
sl@0
    57
	CTestAction::ConstructL(aTestActionSpec);
sl@0
    58
sl@0
    59
	iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
sl@0
    60
	iBody->Des().Copy(aTestActionSpec.iActionBody);
sl@0
    61
sl@0
    62
	HBufC8* length = Input::ParseElementHexL(*iBody, _L8("<bits>"));
sl@0
    63
	CleanupStack::PushL(length);
sl@0
    64
	RInteger clength = RInteger::NewL(*length);
sl@0
    65
	CleanupStack::PopAndDestroy(length);
sl@0
    66
	CleanupStack::PushL(clength);
sl@0
    67
	TUint bits = clength.ConvertToLongL();
sl@0
    68
	CleanupStack::PopAndDestroy();//clength
sl@0
    69
	// the final /7 gives the number of times we have to increment by 7 to get
sl@0
    70
	// to that number of bytes and hence bits.
sl@0
    71
	iIterations = ((bits+7)/8)/7 + 1;
sl@0
    72
	}
sl@0
    73
sl@0
    74
void CMontgomeryFB::DoPerformPrerequisite(TRequestStatus& aStatus)
sl@0
    75
	{
sl@0
    76
	TRequestStatus* status = &aStatus;
sl@0
    77
	User::RequestComplete(status, KErrNone);
sl@0
    78
	iActionState = CTestAction::EAction;
sl@0
    79
	}
sl@0
    80
sl@0
    81
void CMontgomeryFB::DoPerformPostrequisite(TRequestStatus& aStatus)
sl@0
    82
	{
sl@0
    83
	TRequestStatus* status = &aStatus;
sl@0
    84
	iFinished = ETrue;
sl@0
    85
	User::RequestComplete(status, KErrNone);
sl@0
    86
	}
sl@0
    87
sl@0
    88
void CMontgomeryFB::DoReportAction(void)
sl@0
    89
	{
sl@0
    90
	}
sl@0
    91
sl@0
    92
void CMontgomeryFB::DoCheckResult(TInt)
sl@0
    93
	{
sl@0
    94
	}
sl@0
    95
sl@0
    96
void CMontgomeryFB::PerformAction(TRequestStatus& aStatus)
sl@0
    97
	{
sl@0
    98
	TRAPD(res, PerformActionL());
sl@0
    99
	__ASSERT_ALWAYS(!res, User::Panic(_L("CMontgomeryFB::PerformAction"), res));
sl@0
   100
	TRequestStatus* status = &aStatus;
sl@0
   101
	User::RequestComplete(status, KErrNone);
sl@0
   102
	iActionState = CTestAction::EPostrequisite;
sl@0
   103
	}
sl@0
   104
sl@0
   105
void CMontgomeryFB::PerformActionL()
sl@0
   106
	{
sl@0
   107
	__UHEAP_MARK;
sl@0
   108
	
sl@0
   109
	iResult = ETrue;
sl@0
   110
sl@0
   111
	//Generate iIterations*7 byte random sequences we are using 7 as it's a
sl@0
   112
	//generator mod 8.  Thus we'll cycle through every value (0-7) every 8
sl@0
   113
	//iterations.  This gives us a better feeling that certain byte lengths
sl@0
   114
	//(and thus bit lengths as the byte is chosen randomly) don't have errors.
sl@0
   115
	for(TUint i=1; i<=iIterations; i++)
sl@0
   116
		{ 
sl@0
   117
		//generate a prime of roughly i*7*8 bits
sl@0
   118
		RInteger prime = RInteger::NewPrimeL(i*7*8);
sl@0
   119
		CleanupStack::PushL(prime);
sl@0
   120
		CMontgomeryStructure* mont = CMontgomeryStructure::NewLC(prime);
sl@0
   121
		
sl@0
   122
		//generate a random number of x | 2 < x < prime
sl@0
   123
		RInteger base = RInteger::NewRandomL(TInteger::Two(), prime);
sl@0
   124
		CleanupStack::PushL(base);
sl@0
   125
		
sl@0
   126
		//This is using Fermat's Little Theorem
sl@0
   127
		//  (base ^ prime) % prime == base or
sl@0
   128
		//  (base ^ prime-1) % prime == 1
sl@0
   129
		const TInteger& y = mont->ExponentiateL(base, prime);
sl@0
   130
		if( y != base )
sl@0
   131
			{
sl@0
   132
			iResult = EFalse;
sl@0
   133
			iConsole.Printf(_L("X"));
sl@0
   134
			iOut.writeString(_L("Failure exponentiating:"));
sl@0
   135
			iOut.writeNewLine();
sl@0
   136
			Utils::DumpInteger(iOut, _L("base: "), base);
sl@0
   137
			Utils::DumpInteger(iOut, _L("prime: "), prime);
sl@0
   138
			Utils::DumpInteger(iOut, _L("output: "), (const RInteger&)y);
sl@0
   139
			Utils::DumpInteger(iOut, _L("expected: "), base);
sl@0
   140
			}
sl@0
   141
sl@0
   142
		CleanupStack::PopAndDestroy(3, &prime);//base,mont,prime
sl@0
   143
sl@0
   144
		iConsole.Printf(_L("."));
sl@0
   145
		}
sl@0
   146
sl@0
   147
	__UHEAP_MARKEND;
sl@0
   148
	}
sl@0
   149