os/security/crypto/weakcryptospi/source/bigint/rinteger.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 *
    16 */
    17 
    18 
    19 #include <bigint.h>
    20 #include <e32std.h>
    21 #include <random.h>
    22 #include "../common/inlines.h"
    23 
    24 
    25 /** 
    26  * Copy constructor
    27  *
    28  * This function performs a shallow copy, 
    29  * i.e. the memory holding the integer is not copied.
    30  */
    31 EXPORT_C RInteger::RInteger(const RInteger& aInteger)
    32 	{
    33 	*this = aInteger;
    34 	}
    35 
    36 /** 
    37  * Assignment operator
    38  * 
    39  * This function performs a shallow copy, 
    40  * i.e. the memory holding the integer is not copied.
    41  */
    42 EXPORT_C RInteger& RInteger::operator=(const RInteger& aInteger)
    43 	{
    44 	iSize = aInteger.iSize;
    45 	iPtr = aInteger.iPtr;
    46 	return *this;
    47 	}
    48 
    49 
    50 /** 
    51  * Creates a new integer representing 0.
    52  * 
    53  * @return	An RInteger by value.
    54  */
    55 EXPORT_C RInteger RInteger::NewL(void)
    56 	{
    57 	return NewL(TInteger::Zero());
    58 	}
    59 
    60 /** 
    61  * Creates a new integer object representing a specified value.
    62  * 
    63  * @param aValue	A descriptor containing the big-endian binary
    64  * 					representation of the value.
    65  * @return			An RInteger object representing the value.
    66  */
    67 EXPORT_C RInteger RInteger::NewL(const TDesC8& aValue)
    68 	{
    69 	RInteger self;
    70 	//Construct zero's memory beyond the size of aValue after construction
    71 	self.CreateNewL(BytesToWords(aValue.Size()));
    72 	self.Construct(aValue);
    73 	return self;
    74 	}
    75 
    76 /** 
    77  * Creates an exact copy of an \c aInteger object.
    78  * 
    79  * @param aInteger	The integer you wish to copy
    80  * @return			An RInteger object representing an exact copy of 
    81  *					aInteger by value.
    82  */
    83 EXPORT_C RInteger RInteger::NewL(const TInteger& aInteger)
    84 	{
    85 	RInteger self;
    86 	//don't need to CleanNewL as we'll copy straight from aInteger
    87 	self.CreateNewL(aInteger.Size());
    88 	self.Construct(aInteger);
    89 	return self;
    90 	}
    91 
    92 /** 
    93  * Creates a random integer uniformly distributed over [0, 2^aBits].
    94  * 
    95  * @param aBits	The number of bits you wish to randomly select.
    96  * @param aAttr	Enum specifying whether specific bits in the random number should
    97  *				be set.  See TRandomAttribute for more information.
    98  * @return		A random RInteger object in the range specified.
    99  */
   100 EXPORT_C RInteger RInteger::NewRandomL(TUint aBits, TRandomAttribute aAttr)
   101 	{
   102 	RInteger self;
   103 	self.CleanNewL(BitsToWords(aBits));
   104 	CleanupStack::PushL(self);
   105 	self.RandomizeL(aBits, aAttr);
   106 	CleanupStack::Pop(&self); 
   107 	return self;
   108 	}
   109 
   110 /** 
   111  * Creates a random integer uniformly distributed over [x | min <= x <= max].
   112  * 
   113  * @param aMin	The smallest possible value for the random integer (inclusive).
   114  * @param aMax	The largest possible value for the random integer (inclusive).
   115  * @return		A random RInteger object in the range specified.
   116  */
   117 EXPORT_C RInteger RInteger::NewRandomL(const TInteger& aMin,
   118 	const TInteger& aMax)
   119 	{
   120 	RInteger self;
   121 	self.CleanNewL(aMax.Size());
   122 	CleanupStack::PushL(self);
   123 	self.RandomizeL(aMin, aMax);
   124 	CleanupStack::Pop(&self);
   125 	return self;
   126 	}
   127 
   128 /** 
   129  * Finds a random prime integer in the range of [2, 2^aBits].
   130  *
   131  * This is done by picking a random integer and using that as a starting point
   132  * for a sequential search for a prime.  To verify the primality of number, 
   133  * this algorithm uses a probablistic primality test.  This means that it is
   134  * possible, although extremely improbable, that the number returned is a pseudoprime.
   135  *
   136  * @param aBits	The number of bits you wish to randomly select your prime from.
   137  * @param aAttr	Enum specifying whether specific bits in the random number should
   138  *				be set.  See TRandomAttribute for more information.
   139  * @return		A random RInteger representing a probable prime (with very high
   140  *				probablity) in the range specified.
   141  */
   142 EXPORT_C RInteger RInteger::NewPrimeL(TUint aBits, TRandomAttribute aAttr)
   143 	{
   144 	RInteger self;
   145 	self.CleanNewL(BitsToWords(aBits));
   146 	CleanupStack::PushL(self);
   147 	self.PrimeRandomizeL(aBits, aAttr);
   148 	CleanupStack::Pop(&self);
   149 	return self;
   150 	}
   151 
   152 /** 
   153  * Creates a new integer from the value represented by \c aInteger.
   154  * 
   155  * @param aInteger	A signed word sized integer.
   156  * @return			An RInteger representation of aInteger by value.
   157  */
   158 EXPORT_C RInteger RInteger::NewL(TInt aInteger)
   159 	{
   160 	RInteger self;
   161 	self.CreateNewL(2);
   162 	self.Construct(aInteger);
   163 	return self;
   164 	}
   165 
   166 /** 
   167  * Creates a new integer with a preallocated internal storage of \c aNumWords all
   168  * initialised to zero.
   169  *
   170  * The resulting RInteger object is logically equivalent to RInteger::NewL(0).
   171  * The only difference is that the internal storage requirements have been 
   172  * specified to be larger than the default. This is useful if you are about 
   173  * to perform an operation on this integer, that you know the resulting size
   174  * requirements of, and wish to avoid a heap resize.
   175  *
   176  * @param aNumWords	The number of words for to preallocated and zero fill.
   177  * @return			An RInteger object representing 0 with a preallocated 
   178  *					zero-filled internal storage of aNumWords.
   179  */
   180 EXPORT_C RInteger RInteger::NewEmptyL(TUint aNumWords)
   181 	{
   182 	RInteger self;
   183 	self.CleanNewL(aNumWords);
   184 	//There's no construct as there isn't anything to do
   185 	return self;
   186 	}
   187 
   188 /**
   189  * Creates an RInteger object with no associated internal (heap) storage.
   190  * 
   191  * All data members are initialised to zero.  It is safe (although not strictly necessary)
   192  * to push such an RInteger object onto the CleanupStack.  This is useful, for example, if
   193  * you want to pass an RInteger object by reference into a function and have it create
   194  * the representation of the actual integer for you.  
   195  *
   196  * Note that performing any operation on such an RInteger object other than the default
   197  * assignment operator or copy constructor will panic your code.
   198  * 
   199  * @return	A stack based class that has no associated internal storage and thus
   200  *			does not represent any number.
   201  */
   202 EXPORT_C RInteger::RInteger(void)  
   203 	{
   204 	}
   205 
   206 /** 
   207  * An overloaded TCleanupItem() allowing the RIntegers to be pushed,
   208  * popped, and destroyed via the CleanupStack like any other CBase derived object.
   209  */
   210 EXPORT_C RInteger::operator TCleanupItem(void)
   211 	{
   212 	return TCleanupItem(&RInteger::CallClose, this);
   213 	}
   214 
   215 /** 
   216  * Helper function registered with the cleanup stack that just calls Close() for
   217  * this RInteger object.
   218  * 
   219  * @param aPtr	A pointer to the object for which clean-up is to be performed. 
   220  */
   221 EXPORT_C void RInteger::CallClose(TAny* aPtr)
   222 	{
   223 	((RInteger*)aPtr)->Close();	
   224 	}
   225 
   226 /** 
   227  * Zeros and then frees any memory owned by this RInteger object.
   228  *  
   229  * An RInteger object that has been closed can safely fall off the stack.
   230  */
   231 EXPORT_C void RInteger::Close(void)
   232 	{
   233 	if (iPtr)
   234 		{
   235 		Mem::FillZ(Ptr(), Size()*4);
   236 		User::Free(Ptr());
   237 		iSize = 0;
   238 		iPtr = NULL;
   239 		}
   240 	}
   241 
   242 // Method is excluded from coverage due to the problem with BullsEye on ONB.
   243 // Manually verified that this method is functionally covered.
   244 #ifdef _BullseyeCoverage
   245 #pragma suppress_warnings on
   246 #pragma BullseyeCoverage off
   247 #pragma suppress_warnings off
   248 #endif
   249 
   250 /** 
   251  * Creates a new integer from the value represented by \c aInteger.
   252  *
   253  * @param aInteger	An unsigned word sized integer.
   254  * @return			An RInteger representation of aInteger by value.
   255  */
   256 EXPORT_C RInteger RInteger::NewL(TUint aInteger)
   257 	{
   258 	RInteger self;
   259 	self.CreateNewL(2);
   260 	self.Construct(aInteger);
   261 	return self;
   262 	}