1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/source/bigint/rinteger.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,262 @@
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 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include <bigint.h>
1.23 +#include <e32std.h>
1.24 +#include <random.h>
1.25 +#include "../common/inlines.h"
1.26 +
1.27 +
1.28 +/**
1.29 + * Copy constructor
1.30 + *
1.31 + * This function performs a shallow copy,
1.32 + * i.e. the memory holding the integer is not copied.
1.33 + */
1.34 +EXPORT_C RInteger::RInteger(const RInteger& aInteger)
1.35 + {
1.36 + *this = aInteger;
1.37 + }
1.38 +
1.39 +/**
1.40 + * Assignment operator
1.41 + *
1.42 + * This function performs a shallow copy,
1.43 + * i.e. the memory holding the integer is not copied.
1.44 + */
1.45 +EXPORT_C RInteger& RInteger::operator=(const RInteger& aInteger)
1.46 + {
1.47 + iSize = aInteger.iSize;
1.48 + iPtr = aInteger.iPtr;
1.49 + return *this;
1.50 + }
1.51 +
1.52 +
1.53 +/**
1.54 + * Creates a new integer representing 0.
1.55 + *
1.56 + * @return An RInteger by value.
1.57 + */
1.58 +EXPORT_C RInteger RInteger::NewL(void)
1.59 + {
1.60 + return NewL(TInteger::Zero());
1.61 + }
1.62 +
1.63 +/**
1.64 + * Creates a new integer object representing a specified value.
1.65 + *
1.66 + * @param aValue A descriptor containing the big-endian binary
1.67 + * representation of the value.
1.68 + * @return An RInteger object representing the value.
1.69 + */
1.70 +EXPORT_C RInteger RInteger::NewL(const TDesC8& aValue)
1.71 + {
1.72 + RInteger self;
1.73 + //Construct zero's memory beyond the size of aValue after construction
1.74 + self.CreateNewL(BytesToWords(aValue.Size()));
1.75 + self.Construct(aValue);
1.76 + return self;
1.77 + }
1.78 +
1.79 +/**
1.80 + * Creates an exact copy of an \c aInteger object.
1.81 + *
1.82 + * @param aInteger The integer you wish to copy
1.83 + * @return An RInteger object representing an exact copy of
1.84 + * aInteger by value.
1.85 + */
1.86 +EXPORT_C RInteger RInteger::NewL(const TInteger& aInteger)
1.87 + {
1.88 + RInteger self;
1.89 + //don't need to CleanNewL as we'll copy straight from aInteger
1.90 + self.CreateNewL(aInteger.Size());
1.91 + self.Construct(aInteger);
1.92 + return self;
1.93 + }
1.94 +
1.95 +/**
1.96 + * Creates a random integer uniformly distributed over [0, 2^aBits].
1.97 + *
1.98 + * @param aBits The number of bits you wish to randomly select.
1.99 + * @param aAttr Enum specifying whether specific bits in the random number should
1.100 + * be set. See TRandomAttribute for more information.
1.101 + * @return A random RInteger object in the range specified.
1.102 + */
1.103 +EXPORT_C RInteger RInteger::NewRandomL(TUint aBits, TRandomAttribute aAttr)
1.104 + {
1.105 + RInteger self;
1.106 + self.CleanNewL(BitsToWords(aBits));
1.107 + CleanupStack::PushL(self);
1.108 + self.RandomizeL(aBits, aAttr);
1.109 + CleanupStack::Pop(&self);
1.110 + return self;
1.111 + }
1.112 +
1.113 +/**
1.114 + * Creates a random integer uniformly distributed over [x | min <= x <= max].
1.115 + *
1.116 + * @param aMin The smallest possible value for the random integer (inclusive).
1.117 + * @param aMax The largest possible value for the random integer (inclusive).
1.118 + * @return A random RInteger object in the range specified.
1.119 + */
1.120 +EXPORT_C RInteger RInteger::NewRandomL(const TInteger& aMin,
1.121 + const TInteger& aMax)
1.122 + {
1.123 + RInteger self;
1.124 + self.CleanNewL(aMax.Size());
1.125 + CleanupStack::PushL(self);
1.126 + self.RandomizeL(aMin, aMax);
1.127 + CleanupStack::Pop(&self);
1.128 + return self;
1.129 + }
1.130 +
1.131 +/**
1.132 + * Finds a random prime integer in the range of [2, 2^aBits].
1.133 + *
1.134 + * This is done by picking a random integer and using that as a starting point
1.135 + * for a sequential search for a prime. To verify the primality of number,
1.136 + * this algorithm uses a probablistic primality test. This means that it is
1.137 + * possible, although extremely improbable, that the number returned is a pseudoprime.
1.138 + *
1.139 + * @param aBits The number of bits you wish to randomly select your prime from.
1.140 + * @param aAttr Enum specifying whether specific bits in the random number should
1.141 + * be set. See TRandomAttribute for more information.
1.142 + * @return A random RInteger representing a probable prime (with very high
1.143 + * probablity) in the range specified.
1.144 + */
1.145 +EXPORT_C RInteger RInteger::NewPrimeL(TUint aBits, TRandomAttribute aAttr)
1.146 + {
1.147 + RInteger self;
1.148 + self.CleanNewL(BitsToWords(aBits));
1.149 + CleanupStack::PushL(self);
1.150 + self.PrimeRandomizeL(aBits, aAttr);
1.151 + CleanupStack::Pop(&self);
1.152 + return self;
1.153 + }
1.154 +
1.155 +/**
1.156 + * Creates a new integer from the value represented by \c aInteger.
1.157 + *
1.158 + * @param aInteger A signed word sized integer.
1.159 + * @return An RInteger representation of aInteger by value.
1.160 + */
1.161 +EXPORT_C RInteger RInteger::NewL(TInt aInteger)
1.162 + {
1.163 + RInteger self;
1.164 + self.CreateNewL(2);
1.165 + self.Construct(aInteger);
1.166 + return self;
1.167 + }
1.168 +
1.169 +/**
1.170 + * Creates a new integer with a preallocated internal storage of \c aNumWords all
1.171 + * initialised to zero.
1.172 + *
1.173 + * The resulting RInteger object is logically equivalent to RInteger::NewL(0).
1.174 + * The only difference is that the internal storage requirements have been
1.175 + * specified to be larger than the default. This is useful if you are about
1.176 + * to perform an operation on this integer, that you know the resulting size
1.177 + * requirements of, and wish to avoid a heap resize.
1.178 + *
1.179 + * @param aNumWords The number of words for to preallocated and zero fill.
1.180 + * @return An RInteger object representing 0 with a preallocated
1.181 + * zero-filled internal storage of aNumWords.
1.182 + */
1.183 +EXPORT_C RInteger RInteger::NewEmptyL(TUint aNumWords)
1.184 + {
1.185 + RInteger self;
1.186 + self.CleanNewL(aNumWords);
1.187 + //There's no construct as there isn't anything to do
1.188 + return self;
1.189 + }
1.190 +
1.191 +/**
1.192 + * Creates an RInteger object with no associated internal (heap) storage.
1.193 + *
1.194 + * All data members are initialised to zero. It is safe (although not strictly necessary)
1.195 + * to push such an RInteger object onto the CleanupStack. This is useful, for example, if
1.196 + * you want to pass an RInteger object by reference into a function and have it create
1.197 + * the representation of the actual integer for you.
1.198 + *
1.199 + * Note that performing any operation on such an RInteger object other than the default
1.200 + * assignment operator or copy constructor will panic your code.
1.201 + *
1.202 + * @return A stack based class that has no associated internal storage and thus
1.203 + * does not represent any number.
1.204 + */
1.205 +EXPORT_C RInteger::RInteger(void)
1.206 + {
1.207 + }
1.208 +
1.209 +/**
1.210 + * An overloaded TCleanupItem() allowing the RIntegers to be pushed,
1.211 + * popped, and destroyed via the CleanupStack like any other CBase derived object.
1.212 + */
1.213 +EXPORT_C RInteger::operator TCleanupItem(void)
1.214 + {
1.215 + return TCleanupItem(&RInteger::CallClose, this);
1.216 + }
1.217 +
1.218 +/**
1.219 + * Helper function registered with the cleanup stack that just calls Close() for
1.220 + * this RInteger object.
1.221 + *
1.222 + * @param aPtr A pointer to the object for which clean-up is to be performed.
1.223 + */
1.224 +EXPORT_C void RInteger::CallClose(TAny* aPtr)
1.225 + {
1.226 + ((RInteger*)aPtr)->Close();
1.227 + }
1.228 +
1.229 +/**
1.230 + * Zeros and then frees any memory owned by this RInteger object.
1.231 + *
1.232 + * An RInteger object that has been closed can safely fall off the stack.
1.233 + */
1.234 +EXPORT_C void RInteger::Close(void)
1.235 + {
1.236 + if (iPtr)
1.237 + {
1.238 + Mem::FillZ(Ptr(), Size()*4);
1.239 + User::Free(Ptr());
1.240 + iSize = 0;
1.241 + iPtr = NULL;
1.242 + }
1.243 + }
1.244 +
1.245 +// Method is excluded from coverage due to the problem with BullsEye on ONB.
1.246 +// Manually verified that this method is functionally covered.
1.247 +#ifdef _BullseyeCoverage
1.248 +#pragma suppress_warnings on
1.249 +#pragma BullseyeCoverage off
1.250 +#pragma suppress_warnings off
1.251 +#endif
1.252 +
1.253 +/**
1.254 + * Creates a new integer from the value represented by \c aInteger.
1.255 + *
1.256 + * @param aInteger An unsigned word sized integer.
1.257 + * @return An RInteger representation of aInteger by value.
1.258 + */
1.259 +EXPORT_C RInteger RInteger::NewL(TUint aInteger)
1.260 + {
1.261 + RInteger self;
1.262 + self.CreateNewL(2);
1.263 + self.Construct(aInteger);
1.264 + return self;
1.265 + }