2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * (c) 1999-2003 Symbian Ltd
33 class CRandom : public CBase
42 * Implementations of this method should fill the passed
43 * buffer with the generated pseudo-random data up to the
44 * current length, discarding any current contents. The
45 * implementations should leave with KErrNotSecure when
46 * the generated random data is not secure enough.
48 * @param aDest The buffer to fill with random data
49 * @leave KErrNotSecure Random data generated is not
50 * secure enough for crytographic operations
51 * otherwise, leaves with any other system wide error code.
54 virtual void GenerateBytesL(TDes8& aDest) = 0;
56 IMPORT_C CRandom(void);
58 CRandom(const CRandom&);
59 CRandom& operator=(const CRandom&);
64 * Sets a pseudo-random number generator implementation to use for this thread.
66 * @param aRNG The pseudo-random number generator to use.
69 IMPORT_C void SetThreadRandomL(CRandom* aRNG);
73 * Sets a pseudo-random number generator implementation to use
74 * for this thread, placing it on the cleanup stack.
76 * @param aRNG The pseudo-random number generator to use.
79 IMPORT_C void SetThreadRandomLC(CRandom* aRNG);
82 void DeleteThreadRandom(TAny* aPtr);
86 * Destroys the currently installed random number generator
87 * that is in use for this thread.
90 IMPORT_C void DestroyThreadRandom(void);
94 * Generates pseudo-random data.
95 * Fills the provided buffer up to its current length,
96 * discarding any data that it may currently contain.
98 * @param aDest The buffer to fill with random data
99 * @leave KErrNotSecure The random data generated is
100 * not secure enough for cryptographic operations
101 * otherwise, leaves with any other system wide error codes.
104 IMPORT_C void GenerateRandomBytesL(TDes8& aDest);
107 class CSystemRandom : public CRandom
110 * This default pseudo-random number generator uses system state
111 * to generate entropy for the generation of random numbers.
123 * Constructs a new pseudo-random number generator.
125 * @return A ready-to-use random number generator.
127 IMPORT_C static CSystemRandom* NewL(void);
131 * Constructs a new pseudo-random number generator,
132 * and places it on the cleanup stack.
134 * @return A ready-to-use random number generator.
137 IMPORT_C static CSystemRandom* NewLC(void);
141 * Implements the contract as specified in the base class, CRandom, filling the buffer
142 * supplied with random data up to its current length, discarding its current content.
143 * It will leave with KErrNotSecure when the generated random data is not secure enough.
145 * @param aDest The buffer to which to write random data
146 * @leave KErrNotSecure The generated random data is not secure enough for cryptographic operations
147 * otherwise, leaves with any other system wide error codes.
150 virtual void GenerateBytesL(TDes8& aDest);
155 CSystemRandom(const CSystemRandom&);
156 CSystemRandom& operator=(const CSystemRandom&);
166 * The user interface to the random number generator.
176 * Fills the provided buffer with pseudo-random data up to its current length,
177 * discarding any current content.
179 * This method will not return secure random numbers for some time after the phone boot-up. Because,
180 * pseudo-random number generator will take some time to attain a secure state by collecting enough
181 * entropy samples after the boot-up. Till that time, the pseudo-random numbers generated may not be
182 * cryptographically secure and there is no way to get to know about it with this API.
183 * So, if explcit notification on the strength of the random numbers is necessary, use TRandom::SecureRandomL.
185 * @param aDestination The buffer in which to write the random data.
186 * @deprecated Use RandomL() instead
187 * @panic This function can panic under low memory conditions
190 IMPORT_C static void Random(TDes8& aDestination);
194 * Fills the provided buffer with pseudo-random data up to its current length,
195 * discarding any current content.
197 * This method will not return secure random numbers for some time after the phone boot-up. Because,
198 * pseudo-random number generator will take some time to attain a secure state by collecting enough
199 * entropy samples after the boot-up. Till that time, the pseudo-random numbers generated may not be
200 * cryptographically secure and there is no way to get to know about it with this API.
201 * So, if explcit notification on the strength of the random numbers is necessary, use TRandom::SecureRandomL.
203 * @param aDestination The buffer in which to write the random data.
204 * @leave This function can leave under low memory conditions
207 IMPORT_C static void RandomL(TDes8& aDestination);
211 * Fills the provided buffer with the pseudo-random data up to its current length, discarding any current
212 * content of the descriptor. When this method returns normally (with out leave), the system state is secure
213 * and hence the random numbers generated are cryptographically secure as well. When this method leaves with
214 * the error code KErrNotSecure, the system internal state is not secure and hence the random numbers too.
216 * Though this method leaves when the system internal state is not secure, still the descriptor will be filled
217 * with pseudo-random bytes. This random data may or may not be secure enough. Recommended to treat these numbers
220 * @param aDestination The buffer in which to write the random data.
221 * @leave KErrNotSecure The generated random numbers is not secure enough for cryptographic operations.
222 * Otherwise, leaves with some other system wide error codes.
225 IMPORT_C static void SecureRandomL(TDes8& aDestination);
228 class RRandomSession:public RSessionBase
231 * The client interface to the system random number generator. End
232 * users should use TRandom instead of this interface.
240 IMPORT_C RRandomSession(void);
244 * Fills the provided buffer with pseudo-random data up to its
245 * current length, discarding any current content.
247 * @param aDestination The buffer in to which to write the random data
250 IMPORT_C TInt GetRandom(TDes8& aDestination);
254 * Opens a new session with the random number generator.
257 IMPORT_C void ConnectL(void);
260 #endif // __RANDOM_H__