crypto/weakcryptospi/inc/bigint.h
changeset 8 35751d3474b7
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     1 /*
       
     2 * Copyright (c) 2003-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 * ** IMPORTANT ** PublishedPartner API's in this file are published to 3rd party developers via the 
       
    16 * Symbian website. Changes to these API's should be treated as PublishedAll API changes and the Security TA should be consulted.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 /**
       
    22  @file
       
    23  @publishedAll
       
    24  @released
       
    25 */
       
    26 
       
    27 #ifndef __BIGINT_H__
       
    28 #define __BIGINT_H__
       
    29 
       
    30 #include <e32base.h>
       
    31 
       
    32 
       
    33 const TUint KSignMask = 0x1L;
       
    34 const TUint KHeapBasedMask = 0x2L;
       
    35 const TUint KPtrMask = 0xFFFFFFFCL;
       
    36 
       
    37 class RInteger;
       
    38 
       
    39 /**
       
    40  * Abstract base class defining the interface for handling and manipulating big
       
    41  * integers.
       
    42  *
       
    43  * TInteger is capable of representing both negative and positive integers 
       
    44  * with an absolute value of less than 2^32^(2^32).  To create an integer 
       
    45  * look at RInteger.
       
    46  * TInteger defines an interface for the RInteger implementation - it is not
       
    47  * intended that TIntegers be copied or assigned from other TIntegers.  On EKA1
       
    48  * platforms, this is possible, but it should be avoided.
       
    49  * 
       
    50  * 
       
    51  * @see RInteger
       
    52  */
       
    53 class TInteger
       
    54 	{
       
    55 public:
       
    56 	/** @internalComponent */
       
    57 	enum TRandomAttribute {EAllBitsRandom=0, ETopBitSet=1, ETop2BitsSet=2};
       
    58 
       
    59 	IMPORT_C HBufC8* BufferLC() const;
       
    60 	IMPORT_C TUint WordCount(void) const;
       
    61 	IMPORT_C TUint ByteCount(void) const;
       
    62 	IMPORT_C TUint BitCount(void) const;
       
    63 
       
    64 	IMPORT_C static const TInteger& Zero(void);
       
    65 	IMPORT_C static const TInteger& One(void);
       
    66 	IMPORT_C static const TInteger& Two(void);
       
    67 
       
    68 	IMPORT_C RInteger PlusL(const TInteger& aOperand) const;
       
    69 	IMPORT_C RInteger MinusL(const TInteger& aOperand) const;
       
    70 	IMPORT_C RInteger TimesL(const TInteger& aOperand) const;
       
    71 	IMPORT_C RInteger DividedByL(const TInteger& aOperand) const;
       
    72 	IMPORT_C RInteger DividedByL(TUint aOperand) const;
       
    73 	IMPORT_C RInteger ModuloL(const TInteger& aOperand) const;
       
    74 	IMPORT_C TUint ModuloL(TUint aOperand) const;
       
    75 	
       
    76 	IMPORT_C RInteger SquaredL(void) const;
       
    77 	IMPORT_C RInteger ExponentiateL(const TInteger& aExponent) const;
       
    78 	IMPORT_C static RInteger ModularMultiplyL(const TInteger& aA, const TInteger& aB,
       
    79 		const TInteger& aModulus);
       
    80 	IMPORT_C static RInteger ModularExponentiateL(const TInteger& aBase, 
       
    81 		const TInteger& aExp, const TInteger& aMod);
       
    82 	IMPORT_C RInteger GCDL(const TInteger& aOperand) const;
       
    83 	IMPORT_C RInteger InverseModL(const TInteger& aMod) const;
       
    84 	
       
    85 	// These overloaded operator functions leave 
       
    86 	IMPORT_C TInteger& operator += (const TInteger& aOperand);
       
    87 	IMPORT_C TInteger& operator -= (const TInteger& aOperand);
       
    88 	IMPORT_C TInteger& operator *= (const TInteger& aOperand);
       
    89 	IMPORT_C TInteger& operator /= (const TInteger& aOperand);
       
    90 	IMPORT_C TInteger& operator %= (const TInteger& aOperand);
       
    91 
       
    92 	IMPORT_C TInteger& operator += (TInt aOperand);
       
    93 	IMPORT_C TInteger& operator -= (TInt aOperand);
       
    94 	IMPORT_C TInteger& operator *= (TInt aOperand);
       
    95 	IMPORT_C TInteger& operator /= (TInt aOperand);
       
    96 	IMPORT_C TInteger& operator %= (TInt aOperand);
       
    97 	IMPORT_C TInteger& operator -- ();
       
    98 	IMPORT_C TInteger& operator ++ ();
       
    99 
       
   100 	IMPORT_C TInteger& operator <<= (TUint aBits);
       
   101 	// End of leaving overloaded operator functions 
       
   102 	IMPORT_C TInteger& operator >>= (TUint aBits);
       
   103 
       
   104 	IMPORT_C TInt UnsignedCompare(const TInteger& aThat) const;
       
   105 	IMPORT_C TInt SignedCompare(const TInteger& aThat) const;
       
   106 	IMPORT_C TBool operator ! () const;
       
   107 	inline TBool operator == (const TInteger& aInteger) const;
       
   108 	inline TBool operator != (const TInteger& aInteger) const;
       
   109 	inline TBool operator <= (const TInteger& aInteger) const;
       
   110 	inline TBool operator >= (const TInteger& aInteger) const;
       
   111 	inline TBool operator < (const TInteger& aInteger) const;
       
   112 	inline TBool operator > (const TInteger& aInteger) const;
       
   113 	
       
   114 	IMPORT_C TInt SignedCompare(TInt aThat) const;
       
   115 	inline TBool operator == (TInt aInteger) const;
       
   116 	inline TBool operator != (TInt aInteger) const;
       
   117 	inline TBool operator <= (TInt aInteger) const;
       
   118 	inline TBool operator >= (TInt aInteger) const;
       
   119 	inline TBool operator < (TInt aInteger) const;
       
   120 	inline TBool operator > (TInt aInteger) const;
       
   121 
       
   122 	inline TBool IsZero() const {return !*this;}
       
   123 	inline TBool NotZero() const {return !IsZero();}
       
   124 	inline TBool IsNegative() const {return Sign() == ENegative;}
       
   125 	inline TBool NotNegative() const {return !IsNegative();}
       
   126 	inline TBool IsPositive() const {return NotNegative() && NotZero();}
       
   127 	inline TBool NotPositive() const {return !IsPositive();}
       
   128 	inline TBool IsEven() const {return Bit(0) == EFalse;}
       
   129 	inline TBool IsOdd() const {return Bit(0);}
       
   130 
       
   131 	IMPORT_C TBool IsPrimeL(void) const;
       
   132 
       
   133 	IMPORT_C TBool Bit(TUint aBitPos) const;
       
   134 	IMPORT_C void SetBit(TUint aBitPos);
       
   135 	IMPORT_C void Negate(void);
       
   136 
       
   137 	IMPORT_C TInt ConvertToLongL(void) const;
       
   138 
       
   139 	IMPORT_C void CopyL(const TInteger& aInteger, TBool aAllowShrink=ETrue);
       
   140 	IMPORT_C void CopyL(const TInt aInteger, TBool aAllowShrink=ETrue);
       
   141 	IMPORT_C void Set(const RInteger& aInteger);
       
   142 	IMPORT_C HBufC8* BufferWithNoTruncationLC() const;
       
   143 
       
   144 protected: //Interface functions to algorthims
       
   145 	/** @internalComponent */
       
   146 	RInteger PositiveAddL(const TInteger& aA, const TInteger& aB) const;
       
   147 	/** @internalComponent */
       
   148 	RInteger PositiveSubtractL(const TInteger& aA, const TInteger& aB) const;
       
   149 	/** @internalComponent */
       
   150 	RInteger PositiveMultiplyL(const TInteger& aA, const TInteger& aB) const;
       
   151 	/** @internalComponent */
       
   152 	void PositiveDivideL(RInteger& aRemainder, RInteger& aQuotient, 
       
   153 		const TInteger& aDividend, const TInteger& aDivisor) const;
       
   154 	/** @internalComponent */
       
   155 	void DivideL(RInteger& aRemainder, RInteger& aQuotient, 
       
   156 		const TInteger& aDividend, const TInteger& aDivisor) const;
       
   157 	/** @internalComponent */
       
   158 	void PositiveDivide(TUint& aRemainder, TInteger& aQoutient, 
       
   159 		const TInteger& aDividend, TUint aDivisor) const;
       
   160 	/** @internalComponent */
       
   161 	void DivideL(TUint& aRemainder, RInteger& aQoutient, 
       
   162 		const TInteger& aDividend, TUint aDivisor) const;
       
   163 	/** @internalComponent */
       
   164 	TUint Modulo(const TInteger& aDividend, TUint aDivisor) const;
       
   165 
       
   166 protected: //Utility functions
       
   167 	/** @internalComponent */
       
   168 	TInt ConvertToLong(void) const;
       
   169 	inline TUint ConvertToUnsignedLong(void) const {return Ptr()[0];}
       
   170 	/** @internalComponent */
       
   171 	TBool IsConvertableToLong(void) const;
       
   172 	/** @internalComponent */
       
   173 	void RandomizeL(TUint aBits, TRandomAttribute aAttr);
       
   174 	/** @internalComponent */
       
   175 	void RandomizeL(const TInteger& aMin, const TInteger& aMax);
       
   176 	/** @internalComponent */
       
   177 	void PrimeRandomizeL(TUint aBits, TRandomAttribute aAttr);
       
   178 	/** @internalComponent */
       
   179 	TBool SmallPrimeRandomizeL(void);
       
   180 
       
   181 protected: //Memory Handling
       
   182 	/** @internalComponent */
       
   183 	void CreateNewL(TUint aNewSize);
       
   184 	/** @internalComponent */
       
   185 	void CleanNewL(TUint aNewSize);
       
   186 	/** @internalComponent */
       
   187 	void CleanGrowL(TUint aNewSize);
       
   188 	/** @internalComponent */
       
   189 	void CleanResizeL(TUint aNewSize);
       
   190 
       
   191 protected: //Construction functions
       
   192 	IMPORT_C TInteger(void);
       
   193 	/** @internalComponent */
       
   194 	void Construct(const TDesC8& aValue);
       
   195 	/** @internalComponent */
       
   196 	void Construct(const TInteger& aInteger);
       
   197 	/** @internalComponent */
       
   198 	void Construct(TInt aInteger);
       
   199 	/** @internalComponent */
       
   200 	void Construct(TUint aInteger);
       
   201 protected: //Construction functions for stack based integers
       
   202 	/** @internalComponent */
       
   203 	void ConstructStack(TUint aWords, TUint aInteger);
       
   204 	/** @internalComponent */
       
   205 	void ConstructStack(TUint aWords, const TInteger& aInteger);
       
   206 
       
   207 
       
   208 protected: //Member data
       
   209 	enum TSign {EPositive=0, ENegative=1};
       
   210 	TUint iSize;
       
   211 	TUint iPtr;
       
   212 
       
   213 protected: //Hackish functions
       
   214 	/* Memory Layout
       
   215 	 * Word 0: Size -- Must be power of 2 (algorithms assume this)
       
   216 	 * Word 1: Pointer to storage location plus 2 flag bits in lsb
       
   217 	 * Word 1 (bit 0): Sign of the integer (+ve == 0, -ve == 1)
       
   218 	 * Word 1 (bit 1): Heap based flag bit (stack== 0, heap==1)
       
   219 	 */
       
   220 	inline TSign Sign(void) const {return (TSign)(iPtr&KSignMask);}
       
   221 	inline TUint Size(void) const {return iSize;}
       
   222 	inline void SetSize(TUint aSize) {iSize = aSize;}
       
   223 	inline TUint* const Ptr(void) const {return (TUint*)(iPtr&KPtrMask);}
       
   224 	inline TBool IsStackBased(void) const {return !IsHeapBased();}
       
   225 	inline TBool IsHeapBased(void) const {return iPtr&KHeapBasedMask;}
       
   226 	inline void SetPtr(TUint* aPtr) {iPtr &= ~KPtrMask; iPtr |= (TUint)aPtr;}
       
   227 	inline void SetHeapBased(void) {iPtr |= KHeapBasedMask;}
       
   228 	inline void SetStackBased(void) {iPtr &= ~KHeapBasedMask;}
       
   229 	inline void SetSign(TSign aSign) {iPtr &= ~KSignMask; iPtr |= (TUint)aSign;}
       
   230 
       
   231 private:
       
   232 	// disable default copy constructor and assignment operator
       
   233 	TInteger(const TInteger& aInteger);
       
   234 	TInteger& operator=(const TInteger& aInteger);
       
   235 
       
   236 	friend class CMontgomeryStructure;
       
   237 	friend class RInteger; //in order to have access to Size() for an argument
       
   238 	};
       
   239 
       
   240 // Inline methods for TInteger
       
   241 
       
   242 inline TBool TInteger::operator == (const TInteger& aInteger) const
       
   243 	{
       
   244 	return SignedCompare(aInteger) == 0;
       
   245 	}
       
   246 
       
   247 inline TBool TInteger::operator != (const TInteger& aInteger) const
       
   248 	{
       
   249 	return SignedCompare(aInteger) != 0;
       
   250 	}
       
   251 
       
   252 inline TBool TInteger::operator <= (const TInteger& aInteger) const
       
   253 	{
       
   254 	return SignedCompare(aInteger) <= 0;
       
   255 	}
       
   256 
       
   257 inline TBool TInteger::operator >= (const TInteger& aInteger) const
       
   258 	{
       
   259 	return SignedCompare(aInteger) >= 0;
       
   260 	}
       
   261 
       
   262 inline TBool TInteger::operator < (const TInteger& aInteger) const
       
   263 	{
       
   264 	return SignedCompare(aInteger) < 0;
       
   265 	}
       
   266 
       
   267 TBool TInteger::operator > (const TInteger& aInteger) const
       
   268 	{
       
   269 	return SignedCompare(aInteger) > 0;
       
   270 	}
       
   271 
       
   272 inline TBool TInteger::operator == (TInt aInteger) const
       
   273 	{
       
   274 	return SignedCompare(aInteger) == 0;
       
   275 	}
       
   276 
       
   277 inline TBool TInteger::operator != (TInt aInteger) const
       
   278 	{
       
   279 	return SignedCompare(aInteger) != 0;
       
   280 	}
       
   281 
       
   282 inline TBool TInteger::operator <= (TInt aInteger) const
       
   283 	{
       
   284 	return SignedCompare(aInteger) <= 0;
       
   285 	}
       
   286 
       
   287 inline TBool TInteger::operator >= (TInt aInteger) const
       
   288 	{
       
   289 	return SignedCompare(aInteger) >= 0;
       
   290 	}
       
   291 
       
   292 inline TBool TInteger::operator < (TInt aInteger) const
       
   293 	{
       
   294 	return SignedCompare(aInteger) < 0;
       
   295 	}
       
   296 
       
   297 inline TBool TInteger::operator > (TInt aInteger) const
       
   298 	{
       
   299 	return SignedCompare(aInteger) > 0;
       
   300 	}
       
   301 
       
   302 
       
   303 /** 
       
   304  * A TInteger derived class allowing the construction of variable length big integers.
       
   305  * See the Cryptography API guide for further information.
       
   306  *
       
   307  *
       
   308  * @see TInteger
       
   309  */
       
   310 class RInteger : public TInteger
       
   311 	{
       
   312 public:
       
   313 	IMPORT_C static RInteger NewL(void);
       
   314 	IMPORT_C static RInteger NewL(const TDesC8& aValue);
       
   315 	IMPORT_C static RInteger NewL(const TInteger& aInteger);
       
   316 	IMPORT_C static RInteger NewL(TInt aInteger);
       
   317 	IMPORT_C static RInteger NewL(TUint aInteger);
       
   318 	IMPORT_C static RInteger NewEmptyL(TUint aNumWords);
       
   319 
       
   320 	IMPORT_C static RInteger NewRandomL(TUint aBits, 
       
   321 		TRandomAttribute aAttr=EAllBitsRandom);
       
   322 	IMPORT_C static RInteger NewRandomL(const TInteger& aMin,
       
   323 		const TInteger& aMax);
       
   324 	IMPORT_C static RInteger NewPrimeL(TUint aBits, 
       
   325 		TRandomAttribute aAttr=EAllBitsRandom);
       
   326 
       
   327 	IMPORT_C RInteger(void);
       
   328 	IMPORT_C RInteger(const RInteger& aInteger);
       
   329 	IMPORT_C RInteger& operator=(const RInteger& aInteger);
       
   330 
       
   331 	IMPORT_C operator TCleanupItem();
       
   332 	IMPORT_C static void CallClose(TAny* aPtr);
       
   333 	IMPORT_C void Close(void);
       
   334 	};
       
   335 
       
   336 #endif // __BIGINT_H__