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