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