epoc32/include/random.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
parent 2 2fe1408b6811
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
     1 /*
     2 * Copyright (c) 2005-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 * (c) 1999-2003 Symbian Ltd
    16 *
    17 */
    18 
    19 
    20 
    21 
    22 /**
    23  @file
    24  @publishedAll
    25  @released
    26 */
    27 
    28 #ifndef __RANDOM_H__
    29 #define __RANDOM_H__
    30 
    31 #include <e32base.h>
    32 
    33 class CRandom : public CBase
    34 /**
    35  * @publishedAll
    36  * @released
    37  */
    38 	{
    39 public:
    40 
    41 	/**
    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. 
    47 	 *
    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.
    52 	 *
    53 	 */
    54 	virtual void GenerateBytesL(TDes8& aDest) = 0;
    55 protected:
    56 	IMPORT_C CRandom(void);
    57 private:
    58 	CRandom(const CRandom&);
    59 	CRandom& operator=(const CRandom&);
    60 	};
    61 
    62 /**
    63  *
    64  * Sets a pseudo-random number generator implementation to use for this thread.
    65  * 
    66  * @param aRNG The pseudo-random number generator to use.
    67  *
    68  */
    69 IMPORT_C void SetThreadRandomL(CRandom* aRNG);
    70 
    71 /**
    72  *
    73  * Sets a pseudo-random number generator implementation to use
    74  * for this thread, placing it on the cleanup stack.
    75  * 
    76  * @param aRNG The pseudo-random number generator to use.
    77  *
    78  */
    79 IMPORT_C void SetThreadRandomLC(CRandom* aRNG);
    80 
    81 /** @internalAll */
    82 void DeleteThreadRandom(TAny* aPtr);
    83 
    84 /**
    85  *
    86  * Destroys the currently installed random number generator
    87  * that is in use for this thread.
    88  *
    89  */
    90 IMPORT_C void DestroyThreadRandom(void);
    91 
    92 /**
    93  *
    94  * Generates pseudo-random data.
    95  * Fills the provided buffer up to its current length,
    96  * discarding any data that it may currently contain.
    97  *
    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. 
   102  *
   103  */
   104 IMPORT_C void GenerateRandomBytesL(TDes8& aDest);
   105 
   106 class CRandomShim;
   107 class CSystemRandom : public CRandom
   108 /**
   109  *
   110  * This default pseudo-random number generator uses system state 
   111  * to generate entropy for the generation of random numbers.
   112  *
   113  * @publishedAll
   114  * @released
   115  *
   116  */
   117 
   118 	{
   119 public:
   120 
   121 	/**
   122 	 *
   123 	 * Constructs a new pseudo-random number generator.
   124 	 *
   125 	 * @return A ready-to-use random number generator.
   126 	 */
   127 	IMPORT_C static CSystemRandom* NewL(void);
   128 	
   129 	/**
   130 	 *
   131 	 * Constructs a new pseudo-random number generator,
   132 	 * and places it on the cleanup stack.
   133 	 *
   134 	 * @return A ready-to-use random number generator.
   135 	 *
   136 	 */
   137 	IMPORT_C static CSystemRandom* NewLC(void);
   138 	
   139 	/**
   140 	 *
   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.
   144 	 *
   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.
   148 	 *        
   149 	 */
   150 	virtual void GenerateBytesL(TDes8& aDest);
   151 	
   152 	~CSystemRandom();
   153 private:
   154 	CSystemRandom(void);
   155 	CSystemRandom(const CSystemRandom&);
   156 	CSystemRandom& operator=(const CSystemRandom&);
   157 	
   158 	void ConstructL();
   159 	
   160 	CRandomShim *iShim;
   161 	};
   162 
   163 class TRandom
   164 /**
   165  *
   166  * The user interface to the random number generator.
   167  *
   168  * @publishedAll
   169  * @released
   170  */
   171 	{
   172 public:
   173 
   174 	/**
   175 	 * 
   176 	 * Fills the provided buffer with pseudo-random data up to its current length, 
   177 	 * discarding any current content.
   178 	 *
   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.
   184 	 *
   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
   188 	 *
   189 	 */
   190 	IMPORT_C static void Random(TDes8& aDestination);
   191 
   192 	/**	
   193 	 * 
   194 	 * Fills the provided buffer with pseudo-random data up to its current length,
   195 	 * discarding any current content.
   196 	 *
   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.
   202 	 *
   203 	 * @param aDestination The buffer in which to write the random data.
   204 	 * @leave This function can leave under low memory conditions
   205 	 *
   206 	 */
   207 	IMPORT_C static void RandomL(TDes8& aDestination);
   208 	
   209 	/**
   210 	 * 
   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.
   215 	 * 
   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 
   218 	 * as not secure.
   219 	 *
   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.
   223 	 *
   224 	 */
   225 	IMPORT_C static void SecureRandomL(TDes8& aDestination);
   226 	};
   227 
   228 class RRandomSession:public RSessionBase
   229 /**
   230  *
   231  * The client interface to the system random number generator. End
   232  * users should use TRandom instead of this interface.
   233  *
   234  * @publishedAll
   235  * @released
   236  */
   237 	{
   238 public:
   239 
   240 	IMPORT_C RRandomSession(void);
   241 	
   242 	/**
   243 	 * 
   244 	 * Fills the provided buffer with pseudo-random data up to its
   245 	 * current length, discarding any current content.
   246 	 *
   247 	 * @param aDestination The buffer in to which to write the random data 
   248 	 *
   249 	 */
   250 	IMPORT_C TInt GetRandom(TDes8& aDestination);
   251 	
   252 	/**
   253 	 *
   254 	 * Opens a new session with the random number generator.
   255 	 *
   256 	 */
   257 	IMPORT_C void ConnectL(void);
   258 	};
   259 
   260 #endif // __RANDOM_H__