1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/inc/bigint.h Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,336 @@
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 +* ** IMPORTANT ** PublishedPartner API's in this file are published to 3rd party developers via the
1.19 +* Symbian website. Changes to these API's should be treated as PublishedAll API changes and the Security TA should be consulted.
1.20 +*
1.21 +*/
1.22 +
1.23 +
1.24 +/**
1.25 + @file
1.26 + @publishedAll
1.27 + @released
1.28 +*/
1.29 +
1.30 +#ifndef __BIGINT_H__
1.31 +#define __BIGINT_H__
1.32 +
1.33 +#include <e32base.h>
1.34 +
1.35 +
1.36 +const TUint KSignMask = 0x1L;
1.37 +const TUint KHeapBasedMask = 0x2L;
1.38 +const TUint KPtrMask = 0xFFFFFFFCL;
1.39 +
1.40 +class RInteger;
1.41 +
1.42 +/**
1.43 + * Abstract base class defining the interface for handling and manipulating big
1.44 + * integers.
1.45 + *
1.46 + * TInteger is capable of representing both negative and positive integers
1.47 + * with an absolute value of less than 2^32^(2^32). To create an integer
1.48 + * look at RInteger.
1.49 + * TInteger defines an interface for the RInteger implementation - it is not
1.50 + * intended that TIntegers be copied or assigned from other TIntegers. On EKA1
1.51 + * platforms, this is possible, but it should be avoided.
1.52 + *
1.53 + *
1.54 + * @see RInteger
1.55 + */
1.56 +class TInteger
1.57 + {
1.58 +public:
1.59 + /** @internalComponent */
1.60 + enum TRandomAttribute {EAllBitsRandom=0, ETopBitSet=1, ETop2BitsSet=2};
1.61 +
1.62 + IMPORT_C HBufC8* BufferLC() const;
1.63 + IMPORT_C TUint WordCount(void) const;
1.64 + IMPORT_C TUint ByteCount(void) const;
1.65 + IMPORT_C TUint BitCount(void) const;
1.66 +
1.67 + IMPORT_C static const TInteger& Zero(void);
1.68 + IMPORT_C static const TInteger& One(void);
1.69 + IMPORT_C static const TInteger& Two(void);
1.70 +
1.71 + IMPORT_C RInteger PlusL(const TInteger& aOperand) const;
1.72 + IMPORT_C RInteger MinusL(const TInteger& aOperand) const;
1.73 + IMPORT_C RInteger TimesL(const TInteger& aOperand) const;
1.74 + IMPORT_C RInteger DividedByL(const TInteger& aOperand) const;
1.75 + IMPORT_C RInteger DividedByL(TUint aOperand) const;
1.76 + IMPORT_C RInteger ModuloL(const TInteger& aOperand) const;
1.77 + IMPORT_C TUint ModuloL(TUint aOperand) const;
1.78 +
1.79 + IMPORT_C RInteger SquaredL(void) const;
1.80 + IMPORT_C RInteger ExponentiateL(const TInteger& aExponent) const;
1.81 + IMPORT_C static RInteger ModularMultiplyL(const TInteger& aA, const TInteger& aB,
1.82 + const TInteger& aModulus);
1.83 + IMPORT_C static RInteger ModularExponentiateL(const TInteger& aBase,
1.84 + const TInteger& aExp, const TInteger& aMod);
1.85 + IMPORT_C RInteger GCDL(const TInteger& aOperand) const;
1.86 + IMPORT_C RInteger InverseModL(const TInteger& aMod) const;
1.87 +
1.88 + // These overloaded operator functions leave
1.89 + IMPORT_C TInteger& operator += (const TInteger& aOperand);
1.90 + IMPORT_C TInteger& operator -= (const TInteger& aOperand);
1.91 + IMPORT_C TInteger& operator *= (const TInteger& aOperand);
1.92 + IMPORT_C TInteger& operator /= (const TInteger& aOperand);
1.93 + IMPORT_C TInteger& operator %= (const TInteger& aOperand);
1.94 +
1.95 + IMPORT_C TInteger& operator += (TInt aOperand);
1.96 + IMPORT_C TInteger& operator -= (TInt aOperand);
1.97 + IMPORT_C TInteger& operator *= (TInt aOperand);
1.98 + IMPORT_C TInteger& operator /= (TInt aOperand);
1.99 + IMPORT_C TInteger& operator %= (TInt aOperand);
1.100 + IMPORT_C TInteger& operator -- ();
1.101 + IMPORT_C TInteger& operator ++ ();
1.102 +
1.103 + IMPORT_C TInteger& operator <<= (TUint aBits);
1.104 + // End of leaving overloaded operator functions
1.105 + IMPORT_C TInteger& operator >>= (TUint aBits);
1.106 +
1.107 + IMPORT_C TInt UnsignedCompare(const TInteger& aThat) const;
1.108 + IMPORT_C TInt SignedCompare(const TInteger& aThat) const;
1.109 + IMPORT_C TBool operator ! () const;
1.110 + inline TBool operator == (const TInteger& aInteger) const;
1.111 + inline TBool operator != (const TInteger& aInteger) const;
1.112 + inline TBool operator <= (const TInteger& aInteger) const;
1.113 + inline TBool operator >= (const TInteger& aInteger) const;
1.114 + inline TBool operator < (const TInteger& aInteger) const;
1.115 + inline TBool operator > (const TInteger& aInteger) const;
1.116 +
1.117 + IMPORT_C TInt SignedCompare(TInt aThat) const;
1.118 + inline TBool operator == (TInt aInteger) const;
1.119 + inline TBool operator != (TInt aInteger) const;
1.120 + inline TBool operator <= (TInt aInteger) const;
1.121 + inline TBool operator >= (TInt aInteger) const;
1.122 + inline TBool operator < (TInt aInteger) const;
1.123 + inline TBool operator > (TInt aInteger) const;
1.124 +
1.125 + inline TBool IsZero() const {return !*this;}
1.126 + inline TBool NotZero() const {return !IsZero();}
1.127 + inline TBool IsNegative() const {return Sign() == ENegative;}
1.128 + inline TBool NotNegative() const {return !IsNegative();}
1.129 + inline TBool IsPositive() const {return NotNegative() && NotZero();}
1.130 + inline TBool NotPositive() const {return !IsPositive();}
1.131 + inline TBool IsEven() const {return Bit(0) == EFalse;}
1.132 + inline TBool IsOdd() const {return Bit(0);}
1.133 +
1.134 + IMPORT_C TBool IsPrimeL(void) const;
1.135 +
1.136 + IMPORT_C TBool Bit(TUint aBitPos) const;
1.137 + IMPORT_C void SetBit(TUint aBitPos);
1.138 + IMPORT_C void Negate(void);
1.139 +
1.140 + IMPORT_C TInt ConvertToLongL(void) const;
1.141 +
1.142 + IMPORT_C void CopyL(const TInteger& aInteger, TBool aAllowShrink=ETrue);
1.143 + IMPORT_C void CopyL(const TInt aInteger, TBool aAllowShrink=ETrue);
1.144 + IMPORT_C void Set(const RInteger& aInteger);
1.145 + IMPORT_C HBufC8* BufferWithNoTruncationLC() const;
1.146 +
1.147 +protected: //Interface functions to algorthims
1.148 + /** @internalComponent */
1.149 + RInteger PositiveAddL(const TInteger& aA, const TInteger& aB) const;
1.150 + /** @internalComponent */
1.151 + RInteger PositiveSubtractL(const TInteger& aA, const TInteger& aB) const;
1.152 + /** @internalComponent */
1.153 + RInteger PositiveMultiplyL(const TInteger& aA, const TInteger& aB) const;
1.154 + /** @internalComponent */
1.155 + void PositiveDivideL(RInteger& aRemainder, RInteger& aQuotient,
1.156 + const TInteger& aDividend, const TInteger& aDivisor) const;
1.157 + /** @internalComponent */
1.158 + void DivideL(RInteger& aRemainder, RInteger& aQuotient,
1.159 + const TInteger& aDividend, const TInteger& aDivisor) const;
1.160 + /** @internalComponent */
1.161 + void PositiveDivide(TUint& aRemainder, TInteger& aQoutient,
1.162 + const TInteger& aDividend, TUint aDivisor) const;
1.163 + /** @internalComponent */
1.164 + void DivideL(TUint& aRemainder, RInteger& aQoutient,
1.165 + const TInteger& aDividend, TUint aDivisor) const;
1.166 + /** @internalComponent */
1.167 + TUint Modulo(const TInteger& aDividend, TUint aDivisor) const;
1.168 +
1.169 +protected: //Utility functions
1.170 + /** @internalComponent */
1.171 + TInt ConvertToLong(void) const;
1.172 + inline TUint ConvertToUnsignedLong(void) const {return Ptr()[0];}
1.173 + /** @internalComponent */
1.174 + TBool IsConvertableToLong(void) const;
1.175 + /** @internalComponent */
1.176 + void RandomizeL(TUint aBits, TRandomAttribute aAttr);
1.177 + /** @internalComponent */
1.178 + void RandomizeL(const TInteger& aMin, const TInteger& aMax);
1.179 + /** @internalComponent */
1.180 + void PrimeRandomizeL(TUint aBits, TRandomAttribute aAttr);
1.181 + /** @internalComponent */
1.182 + TBool SmallPrimeRandomizeL(void);
1.183 +
1.184 +protected: //Memory Handling
1.185 + /** @internalComponent */
1.186 + void CreateNewL(TUint aNewSize);
1.187 + /** @internalComponent */
1.188 + void CleanNewL(TUint aNewSize);
1.189 + /** @internalComponent */
1.190 + void CleanGrowL(TUint aNewSize);
1.191 + /** @internalComponent */
1.192 + void CleanResizeL(TUint aNewSize);
1.193 +
1.194 +protected: //Construction functions
1.195 + IMPORT_C TInteger(void);
1.196 + /** @internalComponent */
1.197 + void Construct(const TDesC8& aValue);
1.198 + /** @internalComponent */
1.199 + void Construct(const TInteger& aInteger);
1.200 + /** @internalComponent */
1.201 + void Construct(TInt aInteger);
1.202 + /** @internalComponent */
1.203 + void Construct(TUint aInteger);
1.204 +protected: //Construction functions for stack based integers
1.205 + /** @internalComponent */
1.206 + void ConstructStack(TUint aWords, TUint aInteger);
1.207 + /** @internalComponent */
1.208 + void ConstructStack(TUint aWords, const TInteger& aInteger);
1.209 +
1.210 +
1.211 +protected: //Member data
1.212 + enum TSign {EPositive=0, ENegative=1};
1.213 + TUint iSize;
1.214 + TUint iPtr;
1.215 +
1.216 +protected: //Hackish functions
1.217 + /* Memory Layout
1.218 + * Word 0: Size -- Must be power of 2 (algorithms assume this)
1.219 + * Word 1: Pointer to storage location plus 2 flag bits in lsb
1.220 + * Word 1 (bit 0): Sign of the integer (+ve == 0, -ve == 1)
1.221 + * Word 1 (bit 1): Heap based flag bit (stack== 0, heap==1)
1.222 + */
1.223 + inline TSign Sign(void) const {return (TSign)(iPtr&KSignMask);}
1.224 + inline TUint Size(void) const {return iSize;}
1.225 + inline void SetSize(TUint aSize) {iSize = aSize;}
1.226 + inline TUint* const Ptr(void) const {return (TUint*)(iPtr&KPtrMask);}
1.227 + inline TBool IsStackBased(void) const {return !IsHeapBased();}
1.228 + inline TBool IsHeapBased(void) const {return iPtr&KHeapBasedMask;}
1.229 + inline void SetPtr(TUint* aPtr) {iPtr &= ~KPtrMask; iPtr |= (TUint)aPtr;}
1.230 + inline void SetHeapBased(void) {iPtr |= KHeapBasedMask;}
1.231 + inline void SetStackBased(void) {iPtr &= ~KHeapBasedMask;}
1.232 + inline void SetSign(TSign aSign) {iPtr &= ~KSignMask; iPtr |= (TUint)aSign;}
1.233 +
1.234 +private:
1.235 + // disable default copy constructor and assignment operator
1.236 + TInteger(const TInteger& aInteger);
1.237 + TInteger& operator=(const TInteger& aInteger);
1.238 +
1.239 + friend class CMontgomeryStructure;
1.240 + friend class RInteger; //in order to have access to Size() for an argument
1.241 + };
1.242 +
1.243 +// Inline methods for TInteger
1.244 +
1.245 +inline TBool TInteger::operator == (const TInteger& aInteger) const
1.246 + {
1.247 + return SignedCompare(aInteger) == 0;
1.248 + }
1.249 +
1.250 +inline TBool TInteger::operator != (const TInteger& aInteger) const
1.251 + {
1.252 + return SignedCompare(aInteger) != 0;
1.253 + }
1.254 +
1.255 +inline TBool TInteger::operator <= (const TInteger& aInteger) const
1.256 + {
1.257 + return SignedCompare(aInteger) <= 0;
1.258 + }
1.259 +
1.260 +inline TBool TInteger::operator >= (const TInteger& aInteger) const
1.261 + {
1.262 + return SignedCompare(aInteger) >= 0;
1.263 + }
1.264 +
1.265 +inline TBool TInteger::operator < (const TInteger& aInteger) const
1.266 + {
1.267 + return SignedCompare(aInteger) < 0;
1.268 + }
1.269 +
1.270 +TBool TInteger::operator > (const TInteger& aInteger) const
1.271 + {
1.272 + return SignedCompare(aInteger) > 0;
1.273 + }
1.274 +
1.275 +inline TBool TInteger::operator == (TInt aInteger) const
1.276 + {
1.277 + return SignedCompare(aInteger) == 0;
1.278 + }
1.279 +
1.280 +inline TBool TInteger::operator != (TInt aInteger) const
1.281 + {
1.282 + return SignedCompare(aInteger) != 0;
1.283 + }
1.284 +
1.285 +inline TBool TInteger::operator <= (TInt aInteger) const
1.286 + {
1.287 + return SignedCompare(aInteger) <= 0;
1.288 + }
1.289 +
1.290 +inline TBool TInteger::operator >= (TInt aInteger) const
1.291 + {
1.292 + return SignedCompare(aInteger) >= 0;
1.293 + }
1.294 +
1.295 +inline TBool TInteger::operator < (TInt aInteger) const
1.296 + {
1.297 + return SignedCompare(aInteger) < 0;
1.298 + }
1.299 +
1.300 +inline TBool TInteger::operator > (TInt aInteger) const
1.301 + {
1.302 + return SignedCompare(aInteger) > 0;
1.303 + }
1.304 +
1.305 +
1.306 +/**
1.307 + * A TInteger derived class allowing the construction of variable length big integers.
1.308 + * See the Cryptography API guide for further information.
1.309 + *
1.310 + *
1.311 + * @see TInteger
1.312 + */
1.313 +class RInteger : public TInteger
1.314 + {
1.315 +public:
1.316 + IMPORT_C static RInteger NewL(void);
1.317 + IMPORT_C static RInteger NewL(const TDesC8& aValue);
1.318 + IMPORT_C static RInteger NewL(const TInteger& aInteger);
1.319 + IMPORT_C static RInteger NewL(TInt aInteger);
1.320 + IMPORT_C static RInteger NewL(TUint aInteger);
1.321 + IMPORT_C static RInteger NewEmptyL(TUint aNumWords);
1.322 +
1.323 + IMPORT_C static RInteger NewRandomL(TUint aBits,
1.324 + TRandomAttribute aAttr=EAllBitsRandom);
1.325 + IMPORT_C static RInteger NewRandomL(const TInteger& aMin,
1.326 + const TInteger& aMax);
1.327 + IMPORT_C static RInteger NewPrimeL(TUint aBits,
1.328 + TRandomAttribute aAttr=EAllBitsRandom);
1.329 +
1.330 + IMPORT_C RInteger(void);
1.331 + IMPORT_C RInteger(const RInteger& aInteger);
1.332 + IMPORT_C RInteger& operator=(const RInteger& aInteger);
1.333 +
1.334 + IMPORT_C operator TCleanupItem();
1.335 + IMPORT_C static void CallClose(TAny* aPtr);
1.336 + IMPORT_C void Close(void);
1.337 + };
1.338 +
1.339 +#endif // __BIGINT_H__