os/security/crypto/weakcryptospi/source/bigint/mont.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2003-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 <bigint.h>
sl@0
    20
#include <euserext.h>
sl@0
    21
#include "algorithms.h"
sl@0
    22
#include "words.h"
sl@0
    23
#include "windowslider.h"
sl@0
    24
#include "mont.h"
sl@0
    25
sl@0
    26
EXPORT_C CMontgomeryStructure* CMontgomeryStructure::NewLC(
sl@0
    27
	const TInteger& aModulus)
sl@0
    28
	{
sl@0
    29
	CMontgomeryStructure* self = new(ELeave) CMontgomeryStructure;
sl@0
    30
	CleanupStack::PushL(self);
sl@0
    31
	self->ConstructL(aModulus);
sl@0
    32
	return self;
sl@0
    33
	}
sl@0
    34
sl@0
    35
CMontgomeryStructure::~CMontgomeryStructure()
sl@0
    36
	{
sl@0
    37
	iModulus.Close();
sl@0
    38
	iModulusInv.Close();
sl@0
    39
	iWorkspace.Close();
sl@0
    40
	iResult.Close();
sl@0
    41
	}
sl@0
    42
sl@0
    43
void CMontgomeryStructure::ConstructL(const TInteger& aModulus)
sl@0
    44
	{
sl@0
    45
	User::LeaveIfError(aModulus.IsOdd() ? KErrNone : KErrArgument);
sl@0
    46
	
sl@0
    47
	iModulusInv = RInteger::NewEmptyL(aModulus.Size());
sl@0
    48
	iWorkspace = RInteger::NewEmptyL(5*aModulus.Size());
sl@0
    49
	iModulus = RInteger::NewL(aModulus);
sl@0
    50
	iResult = RInteger::NewEmptyL(aModulus.Size());
sl@0
    51
	RecursiveInverseModPower2(iModulusInv.Ptr(), iWorkspace.Ptr(), 
sl@0
    52
		iModulus.Ptr(), iModulus.Size());
sl@0
    53
	}
sl@0
    54
sl@0
    55
CMontgomeryStructure::CMontgomeryStructure()
sl@0
    56
	{
sl@0
    57
	}
sl@0
    58
sl@0
    59
TInteger& CMontgomeryStructure::ConvertInL(TInteger& aInteger) const
sl@0
    60
	{
sl@0
    61
	aInteger <<= WordsToBits(iModulus.Size());
sl@0
    62
	aInteger %= iModulus;
sl@0
    63
	return aInteger;
sl@0
    64
	}
sl@0
    65
sl@0
    66
TInteger& CMontgomeryStructure::ConvertOutL(TInteger& aInteger) const
sl@0
    67
	{
sl@0
    68
	TUint* const T = iWorkspace.Ptr();
sl@0
    69
	TUint* const R = aInteger.Ptr();
sl@0
    70
	const TUint N = iModulus.Size();
sl@0
    71
	User::LeaveIfError((aInteger.Size() <= N) ? KErrNone : KErrArgument);
sl@0
    72
sl@0
    73
	CopyWords(T, aInteger.Ptr(), aInteger.Size());
sl@0
    74
	SetWords(T + aInteger.Size(), 0, 2*N - aInteger.Size());
sl@0
    75
	MontgomeryReduce(R, T+2*N, T, iModulus.Ptr(), iModulusInv.Ptr(), N);
sl@0
    76
	return aInteger;
sl@0
    77
	}
sl@0
    78
sl@0
    79
void CMontgomeryStructure::DoMultiplyL(TInteger& aResult, const TInteger& aA,
sl@0
    80
	const TInteger& aB) const
sl@0
    81
	{
sl@0
    82
	User::LeaveIfError((aResult.Size() == iModulus.Size()) ? KErrNone : KErrArgument);
sl@0
    83
					   
sl@0
    84
	TUint* const T = iWorkspace.Ptr();
sl@0
    85
	TUint* const R = aResult.Ptr();
sl@0
    86
	const TUint N = iModulus.Size();
sl@0
    87
	const TUint* const aReg = aA.Ptr();
sl@0
    88
	const TUint* const bReg = aB.Ptr();
sl@0
    89
	const TUint aSize = aA.Size();
sl@0
    90
	const TUint bSize = aB.Size();
sl@0
    91
	User::LeaveIfError((aSize <= N && bSize <= N) ? KErrNone : KErrArgument);
sl@0
    92
sl@0
    93
	AsymmetricMultiply(T, T+2*N, aReg, aSize, bReg, bSize);
sl@0
    94
	SetWords(T+aSize+bSize, 0, 2*N - aSize - bSize);
sl@0
    95
	MontgomeryReduce(R, T+2*N, T, iModulus.Ptr(), iModulusInv.Ptr(), N);
sl@0
    96
	}
sl@0
    97
sl@0
    98
const TInteger& CMontgomeryStructure::SquareL(const TInteger& aA) const
sl@0
    99
	{
sl@0
   100
	RInteger a = RInteger::NewL(aA);
sl@0
   101
	CleanupStack::PushL(a);
sl@0
   102
	DoSquareL(iResult, ConvertInL(a));
sl@0
   103
	ConvertOutL(iResult);
sl@0
   104
	CleanupStack::PopAndDestroy(&a);
sl@0
   105
	return iResult;
sl@0
   106
	}
sl@0
   107
sl@0
   108
void CMontgomeryStructure::DoSquareL(TInteger& aResult, const TInteger& aA) const
sl@0
   109
	{
sl@0
   110
	User::LeaveIfError((aResult.Size() == iModulus.Size()) ? KErrNone : KErrArgument);
sl@0
   111
	TUint* const T = iWorkspace.Ptr();
sl@0
   112
	TUint* const R = aResult.Ptr();
sl@0
   113
	const TUint N = iModulus.Size();
sl@0
   114
	const TUint* const aReg = aA.Ptr();
sl@0
   115
	const TUint aSize = aA.Size();
sl@0
   116
sl@0
   117
	User::LeaveIfError((aSize <= N) ? KErrNone : KErrArgument);
sl@0
   118
sl@0
   119
	RecursiveSquare(T, T+2*N, aReg, aSize);
sl@0
   120
	SetWords(T+2*aSize, 0, 2*N-2*aSize);
sl@0
   121
	MontgomeryReduce(R, T+2*N, T, iModulus.Ptr(), iModulusInv.Ptr(), N);
sl@0
   122
	}
sl@0
   123
sl@0
   124
EXPORT_C const TInteger& CMontgomeryStructure::ExponentiateL(
sl@0
   125
	const TInteger& aBase, const TInteger& aExponent) const
sl@0
   126
	{
sl@0
   127
	//See HAC 14.85
sl@0
   128
	if ((iResult.Size() != iModulus.Size()) ||
sl@0
   129
		(aBase >= iModulus) ||
sl@0
   130
		(!aBase.IsPositive()) ||
sl@0
   131
		(!aExponent.IsPositive()))
sl@0
   132
		{
sl@0
   133
		User::Leave(KErrArgument);
sl@0
   134
		}
sl@0
   135
sl@0
   136
	// 1.1 Precomputation
sl@0
   137
	// g1 <- g
sl@0
   138
	// g2 <- g^2
sl@0
   139
	RInteger g2 = RInteger::NewL(aBase);
sl@0
   140
	CleanupStack::PushL(g2);
sl@0
   141
	ConvertInL(g2);
sl@0
   142
	//ConvertInL can shrink g2, because we call DoSquare on g2, g2 must be the same size as the modulus
sl@0
   143
	g2.CleanGrowL(iModulus.Size());
sl@0
   144
	RInteger g1 = RInteger::NewL(g2);
sl@0
   145
	CleanupStack::PushL(g1);
sl@0
   146
	DoSquareL(g2, g2);
sl@0
   147
sl@0
   148
	TWindowSlider slider(aExponent);
sl@0
   149
sl@0
   150
	// 1.2 
sl@0
   151
	// For i from 1 to (2^(k-1) -1) do g2i+1 <- g2i-1 * g2
sl@0
   152
	TUint count = (1 << (slider.WindowSize()-1)) - 1; //2^(k-1) -1
sl@0
   153
	RRArray<RInteger> powerArray(count+1); //+1 because we append g1
sl@0
   154
	User::LeaveIfError(powerArray.Append(g1));
sl@0
   155
	CleanupStack::Pop(&g1); 
sl@0
   156
	CleanupClosePushL(powerArray);
sl@0
   157
	for(TUint k=1; k <= count; k++)
sl@0
   158
		{
sl@0
   159
		RInteger gi = RInteger::NewEmptyL(iModulus.Size());
sl@0
   160
		DoMultiplyL(gi, g2, powerArray[k-1]);
sl@0
   161
		User::LeaveIfError(powerArray.Append(gi));
sl@0
   162
		}
sl@0
   163
	
sl@0
   164
	// 2 A <- 1, i <- t
sl@0
   165
	RInteger temp = RInteger::NewL(TInteger::One());
sl@0
   166
	CleanupStack::PushL(temp);
sl@0
   167
	ConvertInL(temp);
sl@0
   168
sl@0
   169
	RInteger& A = iResult;
sl@0
   170
	//Set A to one converted in for this modulus without changing the memory size of A (iResult)
sl@0
   171
	A.CopyL(temp, EFalse); 
sl@0
   172
	CleanupStack::PopAndDestroy(&temp);
sl@0
   173
sl@0
   174
	TInt i = aExponent.BitCount() - 1;
sl@0
   175
sl@0
   176
	// 3 While i>=0 do:
sl@0
   177
	while( i>=0 )
sl@0
   178
		{
sl@0
   179
		// 3.1 If ei == 0 then A <- A^2
sl@0
   180
		if(!aExponent.Bit(i))
sl@0
   181
			{
sl@0
   182
			DoSquareL(A, A);
sl@0
   183
			i--;
sl@0
   184
			}
sl@0
   185
		// 3.2 Find longest bitstring ei,ei-1,...,el s.t. i-l+1<=k and el==1
sl@0
   186
		// and do:
sl@0
   187
		// A <- (A^2^(i-l+1)) * g[the index indicated by the bitstring value]
sl@0
   188
		else
sl@0
   189
			{
sl@0
   190
			slider.FindNextWindow(i);
sl@0
   191
			assert(slider.Length() >= 1);
sl@0
   192
			for(TUint j=0; j<slider.Length(); j++)
sl@0
   193
				{
sl@0
   194
				DoSquareL(A, A);
sl@0
   195
				}
sl@0
   196
			DoMultiplyL(A, A, powerArray[slider.Value()>>1]);
sl@0
   197
			i -= slider.Length();
sl@0
   198
			}
sl@0
   199
		}
sl@0
   200
	CleanupStack::PopAndDestroy(2, &g2); //powerArray, g2
sl@0
   201
	return ConvertOutL(A); // A == iResult
sl@0
   202
	}
sl@0
   203
sl@0
   204
// Methods are excluded from coverage due to the problem with BullsEye on ONB.
sl@0
   205
// Manually verified that these methods are functionally covered.
sl@0
   206
#ifdef _BullseyeCoverage
sl@0
   207
#pragma suppress_warnings on
sl@0
   208
#pragma BullseyeCoverage off
sl@0
   209
#pragma suppress_warnings off
sl@0
   210
#endif
sl@0
   211
sl@0
   212
const TInteger& CMontgomeryStructure::ReduceL(
sl@0
   213
	const TInteger& aInteger) const
sl@0
   214
	{
sl@0
   215
	RInteger temp = RInteger::NewL(aInteger);
sl@0
   216
	CleanupStack::PushL(temp);
sl@0
   217
	ConvertInL(temp);
sl@0
   218
	iResult.CopyL(ConvertOutL(temp), EFalse);
sl@0
   219
	CleanupStack::PopAndDestroy(&temp);
sl@0
   220
	return iResult;
sl@0
   221
	}
sl@0
   222
sl@0
   223
CMontgomeryStructure* CMontgomeryStructure::NewL(
sl@0
   224
	const TInteger& aModulus)
sl@0
   225
	{
sl@0
   226
	CMontgomeryStructure* self = CMontgomeryStructure::NewLC(aModulus);
sl@0
   227
	CleanupStack::Pop(self);
sl@0
   228
	return self;
sl@0
   229
	}
sl@0
   230
sl@0
   231
const TInteger& CMontgomeryStructure::MultiplyL(const TInteger& aA, 
sl@0
   232
	const TInteger& aB) const
sl@0
   233
	{
sl@0
   234
	RInteger a = RInteger::NewL(aA);
sl@0
   235
	CleanupStack::PushL(a);
sl@0
   236
	RInteger b = RInteger::NewL(aB);
sl@0
   237
	CleanupStack::PushL(b);
sl@0
   238
	DoMultiplyL(iResult, ConvertInL(a), ConvertInL(b));
sl@0
   239
	ConvertOutL(iResult);
sl@0
   240
	CleanupStack::PopAndDestroy(&b); 
sl@0
   241
	CleanupStack::PopAndDestroy(&a); 
sl@0
   242
	return iResult;
sl@0
   243
	}