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