os/security/crypto/weakcryptospi/test/tbigint/tmontgomeryvector.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 "tmontgomeryvector.h"
sl@0
    20
#include "t_input.h"
sl@0
    21
#include <bigint.h>
sl@0
    22
#include "../../source/bigint/mont.h"
sl@0
    23
sl@0
    24
CTestAction* CMontgomeryVector::NewL(RFs& aFs, CConsoleBase& aConsole, 
sl@0
    25
	Output& aOut, const TTestActionSpec& aTestActionSpec)
sl@0
    26
	{
sl@0
    27
	CTestAction* self = CMontgomeryVector::NewLC(aFs, aConsole,
sl@0
    28
		aOut, aTestActionSpec);
sl@0
    29
	CleanupStack::Pop();
sl@0
    30
	return self;
sl@0
    31
	}
sl@0
    32
sl@0
    33
CTestAction* CMontgomeryVector::NewLC(RFs& aFs, CConsoleBase& aConsole, 
sl@0
    34
	Output& aOut, const TTestActionSpec& aTestActionSpec)
sl@0
    35
	{
sl@0
    36
	CMontgomeryVector* self = new(ELeave) CMontgomeryVector(aFs, aConsole, aOut);
sl@0
    37
	CleanupStack::PushL(self);
sl@0
    38
	self->ConstructL(aTestActionSpec);
sl@0
    39
	return self;
sl@0
    40
	}
sl@0
    41
sl@0
    42
CMontgomeryVector::~CMontgomeryVector()
sl@0
    43
	{
sl@0
    44
	delete iBody;
sl@0
    45
	delete iA;
sl@0
    46
	delete iB;
sl@0
    47
	delete iModulus;
sl@0
    48
	delete iAns;
sl@0
    49
	}
sl@0
    50
sl@0
    51
CMontgomeryVector::CMontgomeryVector(RFs& aFs, CConsoleBase& aConsole, 
sl@0
    52
	Output& aOut) : CTestAction(aConsole, aOut), iFs(aFs)
sl@0
    53
	{
sl@0
    54
	}
sl@0
    55
sl@0
    56
void CMontgomeryVector::ConstructL(const TTestActionSpec& aTestActionSpec)
sl@0
    57
	{
sl@0
    58
	CTestAction::ConstructL(aTestActionSpec);
sl@0
    59
	iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
sl@0
    60
	iBody->Des().Copy(aTestActionSpec.iActionBody);
sl@0
    61
sl@0
    62
	iA = Input::ParseElementHexL(*iBody, _L8("<a>"));
sl@0
    63
	iB = Input::ParseElementHexL(*iBody, _L8("<b>"));
sl@0
    64
	iModulus = Input::ParseElementHexL(*iBody, _L8("<modulus>"));
sl@0
    65
	iAns = Input::ParseElementHexL(*iBody, _L8("<ans>"));
sl@0
    66
	TPtrC8 op = Input::ParseElement(*iBody, _L8("<op>"));
sl@0
    67
	if( op == _L8("multiply") )
sl@0
    68
		{
sl@0
    69
		iOp = EMultiply;
sl@0
    70
		}
sl@0
    71
	else if( op == _L8("square") )
sl@0
    72
		{
sl@0
    73
		iOp = ESquare;
sl@0
    74
		}
sl@0
    75
	else if( op == _L8("reduce") )
sl@0
    76
		{
sl@0
    77
		iOp = EReduce;
sl@0
    78
		}
sl@0
    79
	else if( op == _L8("exponentiate") )
sl@0
    80
		{
sl@0
    81
		iOp = EExponentiate;
sl@0
    82
		}
sl@0
    83
	else 
sl@0
    84
		{
sl@0
    85
		User::Panic(_L("tmontgomeryvector"), 1);
sl@0
    86
		}
sl@0
    87
	}
sl@0
    88
sl@0
    89
void CMontgomeryVector::DoPerformPrerequisite(TRequestStatus& aStatus)
sl@0
    90
	{
sl@0
    91
	TRequestStatus* status = &aStatus;
sl@0
    92
	User::RequestComplete(status, KErrNone);
sl@0
    93
	iActionState = CTestAction::EAction;
sl@0
    94
	}
sl@0
    95
sl@0
    96
void CMontgomeryVector::DoPerformPostrequisite(TRequestStatus& aStatus)
sl@0
    97
	{
sl@0
    98
	TRequestStatus* status = &aStatus;
sl@0
    99
	iFinished = ETrue;
sl@0
   100
	User::RequestComplete(status, KErrNone);
sl@0
   101
	}
sl@0
   102
sl@0
   103
void CMontgomeryVector::DoReportAction(void)
sl@0
   104
	{
sl@0
   105
	}
sl@0
   106
sl@0
   107
void CMontgomeryVector::DoCheckResult(TInt)
sl@0
   108
	{
sl@0
   109
	}
sl@0
   110
sl@0
   111
void CMontgomeryVector::PerformAction(TRequestStatus& aStatus)
sl@0
   112
	{
sl@0
   113
	__UHEAP_MARK;
sl@0
   114
	TRequestStatus* status = &aStatus;
sl@0
   115
	iResult = ETrue;
sl@0
   116
sl@0
   117
 	RInteger a = RInteger::NewL(*iA);
sl@0
   118
	CleanupStack::PushL(a);
sl@0
   119
	RInteger b = RInteger::NewL(*iB);
sl@0
   120
	CleanupStack::PushL(b);
sl@0
   121
	RInteger modulus = RInteger::NewL(*iModulus);
sl@0
   122
	CleanupStack::PushL(modulus);
sl@0
   123
	RInteger ans = RInteger::NewL(*iAns);
sl@0
   124
	CleanupStack::PushL(ans);
sl@0
   125
	CMontgomeryStructure* mont = CMontgomeryStructure::NewL(modulus);
sl@0
   126
	CleanupStack::PushL(mont);
sl@0
   127
	//we don't own out at any point, it remains the propery of mont
sl@0
   128
	const TInteger* out = 0;
sl@0
   129
	switch(iOp)
sl@0
   130
		{
sl@0
   131
		case EMultiply:
sl@0
   132
			out = &(mont->MultiplyL(a, b));
sl@0
   133
			break;
sl@0
   134
		case ESquare:
sl@0
   135
			out = &(mont->SquareL(a));
sl@0
   136
			break;
sl@0
   137
		case EReduce:
sl@0
   138
			out = &(mont->ReduceL(a));
sl@0
   139
			break;
sl@0
   140
		case EExponentiate:
sl@0
   141
			out = &(mont->ExponentiateL(a, b));
sl@0
   142
			break;
sl@0
   143
		default:
sl@0
   144
			User::Panic(_L("tbasicmathsvector"), 2);
sl@0
   145
			break;
sl@0
   146
		}
sl@0
   147
sl@0
   148
	if( *out != ans )
sl@0
   149
		{
sl@0
   150
		iResult = EFalse;
sl@0
   151
		}
sl@0
   152
	CleanupStack::PopAndDestroy(mont);
sl@0
   153
	CleanupStack::PopAndDestroy(4); //ans, modulus, b,a 
sl@0
   154
sl@0
   155
	User::RequestComplete(status, KErrNone);
sl@0
   156
	iActionState = CTestAction::EPostrequisite;
sl@0
   157
	__UHEAP_MARKEND;
sl@0
   158
	}
sl@0
   159