1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcrypto/source/bigint/mont.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,235 @@
1.4 +/*
1.5 +* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include <bigint.h>
1.23 +#include <euserext.h>
1.24 +#include "algorithms.h"
1.25 +#include "words.h"
1.26 +#include "windowslider.h"
1.27 +#include "mont.h"
1.28 +
1.29 +CMontgomeryStructure* CMontgomeryStructure::NewL(
1.30 + const TInteger& aModulus)
1.31 + {
1.32 + CMontgomeryStructure* self = CMontgomeryStructure::NewLC(aModulus);
1.33 + CleanupStack::Pop(self);
1.34 + return self;
1.35 + }
1.36 +
1.37 +CMontgomeryStructure* CMontgomeryStructure::NewLC(
1.38 + const TInteger& aModulus)
1.39 + {
1.40 + CMontgomeryStructure* self = new(ELeave) CMontgomeryStructure;
1.41 + CleanupStack::PushL(self);
1.42 + self->ConstructL(aModulus);
1.43 + return self;
1.44 + }
1.45 +
1.46 +CMontgomeryStructure::~CMontgomeryStructure()
1.47 + {
1.48 + iModulus.Close();
1.49 + iModulusInv.Close();
1.50 + iWorkspace.Close();
1.51 + iResult.Close();
1.52 + }
1.53 +
1.54 +void CMontgomeryStructure::ConstructL(const TInteger& aModulus)
1.55 + {
1.56 + User::LeaveIfError(aModulus.IsOdd() ? KErrNone : KErrArgument);
1.57 +
1.58 + iModulusInv = RInteger::NewEmptyL(aModulus.Size());
1.59 + iWorkspace = RInteger::NewEmptyL(5*aModulus.Size());
1.60 + iModulus = RInteger::NewL(aModulus);
1.61 + iResult = RInteger::NewEmptyL(aModulus.Size());
1.62 + RecursiveInverseModPower2(iModulusInv.Ptr(), iWorkspace.Ptr(),
1.63 + iModulus.Ptr(), iModulus.Size());
1.64 + }
1.65 +
1.66 +CMontgomeryStructure::CMontgomeryStructure()
1.67 + {
1.68 + }
1.69 +
1.70 +TInteger& CMontgomeryStructure::ConvertInL(TInteger& aInteger) const
1.71 + {
1.72 + aInteger <<= WordsToBits(iModulus.Size());
1.73 + aInteger %= iModulus;
1.74 + return aInteger;
1.75 + }
1.76 +
1.77 +TInteger& CMontgomeryStructure::ConvertOutL(TInteger& aInteger) const
1.78 + {
1.79 + TUint* const T = iWorkspace.Ptr();
1.80 + TUint* const R = aInteger.Ptr();
1.81 + const TUint N = iModulus.Size();
1.82 + User::LeaveIfError((aInteger.Size() <= N) ? KErrNone : KErrArgument);
1.83 +
1.84 + CopyWords(T, aInteger.Ptr(), aInteger.Size());
1.85 + SetWords(T + aInteger.Size(), 0, 2*N - aInteger.Size());
1.86 + MontgomeryReduce(R, T+2*N, T, iModulus.Ptr(), iModulusInv.Ptr(), N);
1.87 + return aInteger;
1.88 + }
1.89 +
1.90 +const TInteger& CMontgomeryStructure::ReduceL(
1.91 + const TInteger& aInteger) const
1.92 + {
1.93 + RInteger temp = RInteger::NewL(aInteger);
1.94 + CleanupStack::PushL(temp);
1.95 + ConvertInL(temp);
1.96 + iResult.CopyL(ConvertOutL(temp), EFalse);
1.97 + CleanupStack::PopAndDestroy(&temp);
1.98 + return iResult;
1.99 + }
1.100 +
1.101 +const TInteger& CMontgomeryStructure::MultiplyL(const TInteger& aA,
1.102 + const TInteger& aB) const
1.103 + {
1.104 + RInteger a = RInteger::NewL(aA);
1.105 + CleanupStack::PushL(a);
1.106 + RInteger b = RInteger::NewL(aB);
1.107 + CleanupStack::PushL(b);
1.108 + DoMultiplyL(iResult, ConvertInL(a), ConvertInL(b));
1.109 + ConvertOutL(iResult);
1.110 + CleanupStack::PopAndDestroy(&b);
1.111 + CleanupStack::PopAndDestroy(&a);
1.112 + return iResult;
1.113 + }
1.114 +
1.115 +void CMontgomeryStructure::DoMultiplyL(TInteger& aResult, const TInteger& aA,
1.116 + const TInteger& aB) const
1.117 + {
1.118 + User::LeaveIfError((aResult.Size() == iModulus.Size()) ? KErrNone : KErrArgument);
1.119 +
1.120 + TUint* const T = iWorkspace.Ptr();
1.121 + TUint* const R = aResult.Ptr();
1.122 + const TUint N = iModulus.Size();
1.123 + const TUint* const aReg = aA.Ptr();
1.124 + const TUint* const bReg = aB.Ptr();
1.125 + const TUint aSize = aA.Size();
1.126 + const TUint bSize = aB.Size();
1.127 + User::LeaveIfError((aSize <= N && bSize <= N) ? KErrNone : KErrArgument);
1.128 +
1.129 + AsymmetricMultiply(T, T+2*N, aReg, aSize, bReg, bSize);
1.130 + SetWords(T+aSize+bSize, 0, 2*N - aSize - bSize);
1.131 + MontgomeryReduce(R, T+2*N, T, iModulus.Ptr(), iModulusInv.Ptr(), N);
1.132 + }
1.133 +
1.134 +const TInteger& CMontgomeryStructure::SquareL(const TInteger& aA) const
1.135 + {
1.136 + RInteger a = RInteger::NewL(aA);
1.137 + CleanupStack::PushL(a);
1.138 + DoSquareL(iResult, ConvertInL(a));
1.139 + ConvertOutL(iResult);
1.140 + CleanupStack::PopAndDestroy(&a);
1.141 + return iResult;
1.142 + }
1.143 +
1.144 +void CMontgomeryStructure::DoSquareL(TInteger& aResult, const TInteger& aA) const
1.145 + {
1.146 + User::LeaveIfError((aResult.Size() == iModulus.Size()) ? KErrNone : KErrArgument);
1.147 + TUint* const T = iWorkspace.Ptr();
1.148 + TUint* const R = aResult.Ptr();
1.149 + const TUint N = iModulus.Size();
1.150 + const TUint* const aReg = aA.Ptr();
1.151 + const TUint aSize = aA.Size();
1.152 +
1.153 + User::LeaveIfError((aSize <= N) ? KErrNone : KErrArgument);
1.154 +
1.155 + RecursiveSquare(T, T+2*N, aReg, aSize);
1.156 + SetWords(T+2*aSize, 0, 2*N-2*aSize);
1.157 + MontgomeryReduce(R, T+2*N, T, iModulus.Ptr(), iModulusInv.Ptr(), N);
1.158 + }
1.159 +
1.160 +const TInteger& CMontgomeryStructure::ExponentiateL(
1.161 + const TInteger& aBase, const TInteger& aExponent) const
1.162 + {
1.163 + //See HAC 14.85
1.164 + if ((iResult.Size() != iModulus.Size()) ||
1.165 + (aBase >= iModulus) ||
1.166 + (!aBase.IsPositive()) ||
1.167 + (!aExponent.IsPositive()))
1.168 + {
1.169 + User::Leave(KErrArgument);
1.170 + }
1.171 +
1.172 + // 1.1 Precomputation
1.173 + // g1 <- g
1.174 + // g2 <- g^2
1.175 + RInteger g2 = RInteger::NewL(aBase);
1.176 + CleanupStack::PushL(g2);
1.177 + ConvertInL(g2);
1.178 + //ConvertIn can shrink g2, because we call DoSquare on g2, g2 must be the same size as the modulus
1.179 + g2.CleanGrowL(iModulus.Size());
1.180 + RInteger g1 = RInteger::NewL(g2);
1.181 + CleanupStack::PushL(g1);
1.182 + DoSquareL(g2, g2);
1.183 +
1.184 + TWindowSlider slider(aExponent);
1.185 +
1.186 + // 1.2
1.187 + // For i from 1 to (2^(k-1) -1) do g2i+1 <- g2i-1 * g2
1.188 + TUint count = (1 << (slider.WindowSize()-1)) - 1; //2^(k-1) -1
1.189 + RRArray<RInteger> powerArray(count+1); //+1 because we append g1
1.190 + User::LeaveIfError(powerArray.Append(g1));
1.191 + CleanupStack::Pop(&g1);
1.192 + CleanupClosePushL(powerArray);
1.193 + for(TUint k=1; k <= count; k++)
1.194 + {
1.195 + RInteger gi = RInteger::NewEmptyL(iModulus.Size());
1.196 + DoMultiplyL(gi, g2, powerArray[k-1]);
1.197 + User::LeaveIfError(powerArray.Append(gi));
1.198 + }
1.199 +
1.200 + // 2 A <- 1, i <- t
1.201 + RInteger temp = RInteger::NewL(TInteger::One());
1.202 + CleanupStack::PushL(temp);
1.203 + ConvertInL(temp);
1.204 +
1.205 + RInteger& A = iResult;
1.206 + //Set A to one converted in for this modulus without changing the memory size of A (iResult)
1.207 + A.CopyL(temp, EFalse);
1.208 + CleanupStack::PopAndDestroy(&temp);
1.209 +
1.210 + TInt i = aExponent.BitCount() - 1;
1.211 +
1.212 + // 3 While i>=0 do:
1.213 + while( i>=0 )
1.214 + {
1.215 + // 3.1 If ei == 0 then A <- A^2
1.216 + if(!aExponent.Bit(i))
1.217 + {
1.218 + DoSquareL(A, A);
1.219 + i--;
1.220 + }
1.221 + // 3.2 Find longest bitstring ei,ei-1,...,el s.t. i-l+1<=k and el==1
1.222 + // and do:
1.223 + // A <- (A^2^(i-l+1)) * g[the index indicated by the bitstring value]
1.224 + else
1.225 + {
1.226 + slider.FindNextWindow(i);
1.227 + assert(slider.Length() >= 1);
1.228 + for(TUint j=0; j<slider.Length(); j++)
1.229 + {
1.230 + DoSquareL(A, A);
1.231 + }
1.232 + DoMultiplyL(A, A, powerArray[slider.Value()>>1]);
1.233 + i -= slider.Length();
1.234 + }
1.235 + }
1.236 + CleanupStack::PopAndDestroy(2, &g2); //powerArray, g2
1.237 + return ConvertOutL(A); // A == iResult
1.238 + }