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