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.
williamr@2
     1
/*
williamr@2
     2
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
williamr@2
     3
* All rights reserved.
williamr@2
     4
* This component and the accompanying materials are made available
williamr@2
     5
* under the terms of the License "Eclipse Public License v1.0"
williamr@2
     6
* which accompanies this distribution, and is available
williamr@2
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
williamr@2
     8
*
williamr@2
     9
* Initial Contributors:
williamr@2
    10
* Nokia Corporation - initial contribution.
williamr@2
    11
*
williamr@2
    12
* Contributors:
williamr@2
    13
*
williamr@2
    14
* Description: 
williamr@2
    15
* (c) 1999-2003 Symbian Ltd
williamr@2
    16
*
williamr@2
    17
*/
williamr@2
    18
williamr@2
    19
williamr@2
    20
williamr@2
    21
williamr@2
    22
/**
williamr@2
    23
 @file
williamr@2
    24
 @publishedAll
williamr@2
    25
 @released
williamr@2
    26
*/
williamr@2
    27
williamr@2
    28
#ifndef __RANDOM_H__
williamr@2
    29
#define __RANDOM_H__
williamr@2
    30
williamr@2
    31
#include <e32base.h>
williamr@2
    32
williamr@2
    33
class CRandom : public CBase
williamr@2
    34
/**
williamr@2
    35
 * @publishedAll
williamr@2
    36
 * @released
williamr@2
    37
 */
williamr@2
    38
	{
williamr@2
    39
public:
williamr@2
    40
williamr@2
    41
	/**
williamr@2
    42
	 * Implementations of this method should fill the passed
williamr@4
    43
	 * buffer with the generated pseudo-random data up to the
williamr@4
    44
	 * current length, discarding any current contents. The 
williamr@4
    45
	 * implementations should leave with KErrNotSecure when 
williamr@4
    46
	 * the generated random data is not secure enough. 
williamr@2
    47
	 *
williamr@4
    48
	 * @param aDest The buffer to fill with random data
williamr@4
    49
	 * @leave KErrNotSecure Random data generated is not 
williamr@4
    50
	 *        secure enough for crytographic operations
williamr@4
    51
	 *        otherwise, leaves with any other system wide error code.
williamr@2
    52
	 *
williamr@2
    53
	 */
williamr@2
    54
	virtual void GenerateBytesL(TDes8& aDest) = 0;
williamr@2
    55
protected:
williamr@2
    56
	IMPORT_C CRandom(void);
williamr@2
    57
private:
williamr@2
    58
	CRandom(const CRandom&);
williamr@2
    59
	CRandom& operator=(const CRandom&);
williamr@2
    60
	};
williamr@2
    61
williamr@2
    62
/**
williamr@2
    63
 *
williamr@4
    64
 * Sets a pseudo-random number generator implementation to use for this thread.
williamr@2
    65
 * 
williamr@4
    66
 * @param aRNG The pseudo-random number generator to use.
williamr@2
    67
 *
williamr@2
    68
 */
williamr@2
    69
IMPORT_C void SetThreadRandomL(CRandom* aRNG);
williamr@2
    70
williamr@2
    71
/**
williamr@2
    72
 *
williamr@4
    73
 * Sets a pseudo-random number generator implementation to use
williamr@2
    74
 * for this thread, placing it on the cleanup stack.
williamr@2
    75
 * 
williamr@4
    76
 * @param aRNG The pseudo-random number generator to use.
williamr@2
    77
 *
williamr@2
    78
 */
williamr@2
    79
IMPORT_C void SetThreadRandomLC(CRandom* aRNG);
williamr@2
    80
williamr@2
    81
/** @internalAll */
williamr@2
    82
void DeleteThreadRandom(TAny* aPtr);
williamr@2
    83
williamr@2
    84
/**
williamr@2
    85
 *
williamr@2
    86
 * Destroys the currently installed random number generator
williamr@2
    87
 * that is in use for this thread.
williamr@2
    88
 *
williamr@2
    89
 */
williamr@2
    90
IMPORT_C void DestroyThreadRandom(void);
williamr@2
    91
williamr@2
    92
/**
williamr@2
    93
 *
williamr@4
    94
 * Generates pseudo-random data.
williamr@4
    95
 * Fills the provided buffer up to its current length,
williamr@4
    96
 * discarding any data that it may currently contain.
williamr@2
    97
 *
williamr@2
    98
 * @param aDest The buffer to fill with random data
williamr@4
    99
 * @leave KErrNotSecure The random data generated is  
williamr@4
   100
 *        not secure enough for cryptographic operations
williamr@4
   101
 *        otherwise, leaves with any other system wide error codes. 
williamr@2
   102
 *
williamr@2
   103
 */
williamr@2
   104
IMPORT_C void GenerateRandomBytesL(TDes8& aDest);
williamr@2
   105
williamr@4
   106
class CRandomShim;
williamr@2
   107
class CSystemRandom : public CRandom
williamr@2
   108
/**
williamr@2
   109
 *
williamr@4
   110
 * This default pseudo-random number generator uses system state 
williamr@4
   111
 * to generate entropy for the generation of random numbers.
williamr@2
   112
 *
williamr@2
   113
 * @publishedAll
williamr@2
   114
 * @released
williamr@2
   115
 *
williamr@2
   116
 */
williamr@2
   117
williamr@2
   118
	{
williamr@2
   119
public:
williamr@2
   120
williamr@2
   121
	/**
williamr@2
   122
	 *
williamr@4
   123
	 * Constructs a new pseudo-random number generator.
williamr@2
   124
	 *
williamr@2
   125
	 * @return A ready-to-use random number generator.
williamr@2
   126
	 */
williamr@2
   127
	IMPORT_C static CSystemRandom* NewL(void);
williamr@2
   128
	
williamr@2
   129
	/**
williamr@2
   130
	 *
williamr@4
   131
	 * Constructs a new pseudo-random number generator,
williamr@2
   132
	 * and places it on the cleanup stack.
williamr@2
   133
	 *
williamr@2
   134
	 * @return A ready-to-use random number generator.
williamr@2
   135
	 *
williamr@2
   136
	 */
williamr@2
   137
	IMPORT_C static CSystemRandom* NewLC(void);
williamr@2
   138
	
williamr@2
   139
	/**
williamr@2
   140
	 *
williamr@4
   141
	 * Implements the contract as specified in the base class,  CRandom, filling the buffer
williamr@4
   142
	 * supplied with random data  up to its current length, discarding its current content.
williamr@4
   143
	 * It will leave with KErrNotSecure when the generated random data is not secure enough.
williamr@2
   144
	 *
williamr@2
   145
	 * @param aDest The buffer to which to write random data
williamr@4
   146
	 * @leave KErrNotSecure The generated random data is not secure enough for cryptographic operations 
williamr@4
   147
	 *        otherwise, leaves with any other system wide error codes.
williamr@4
   148
	 *        
williamr@2
   149
	 */
williamr@2
   150
	virtual void GenerateBytesL(TDes8& aDest);
williamr@4
   151
	
williamr@4
   152
	~CSystemRandom();
williamr@2
   153
private:
williamr@2
   154
	CSystemRandom(void);
williamr@2
   155
	CSystemRandom(const CSystemRandom&);
williamr@2
   156
	CSystemRandom& operator=(const CSystemRandom&);
williamr@4
   157
	
williamr@4
   158
	void ConstructL();
williamr@4
   159
	
williamr@4
   160
	CRandomShim *iShim;
williamr@2
   161
	};
williamr@2
   162
williamr@2
   163
class TRandom
williamr@2
   164
/**
williamr@2
   165
 *
williamr@4
   166
 * The user interface to the random number generator.
williamr@2
   167
 *
williamr@2
   168
 * @publishedAll
williamr@2
   169
 * @released
williamr@2
   170
 */
williamr@2
   171
	{
williamr@2
   172
public:
williamr@2
   173
williamr@2
   174
	/**
williamr@2
   175
	 * 
williamr@4
   176
	 * Fills the provided buffer with pseudo-random data up to its current length, 
williamr@4
   177
	 * discarding any current content.
williamr@4
   178
	 *
williamr@4
   179
	 * This method will not return secure random numbers for some time after the phone boot-up. Because,
williamr@4
   180
	 * pseudo-random number generator will take some time to attain a secure state by collecting enough 
williamr@4
   181
	 * entropy samples after the boot-up. Till that time, the pseudo-random numbers generated may not be
williamr@4
   182
	 * cryptographically secure and there is no way to get to know about it with this API. 
williamr@4
   183
	 * So, if explcit notification on the strength of the random numbers is necessary, use TRandom::SecureRandomL.
williamr@2
   184
	 *
williamr@2
   185
	 * @param aDestination The buffer in which to write the random data.
williamr@2
   186
	 * @deprecated Use RandomL() instead
williamr@2
   187
	 * @panic This function can panic under low memory conditions
williamr@2
   188
	 *
williamr@2
   189
	 */
williamr@2
   190
	IMPORT_C static void Random(TDes8& aDestination);
williamr@2
   191
williamr@4
   192
	/**	
williamr@2
   193
	 * 
williamr@4
   194
	 * Fills the provided buffer with pseudo-random data up to its current length,
williamr@4
   195
	 * discarding any current content.
williamr@4
   196
	 *
williamr@4
   197
	 * This method will not return secure random numbers for some time after the phone boot-up. Because,
williamr@4
   198
     * pseudo-random number generator will take some time to attain a secure state by collecting enough 
williamr@4
   199
     * entropy samples after the boot-up. Till that time, the pseudo-random numbers generated may not be
williamr@4
   200
     * cryptographically secure and there is no way to get to know about it with this API. 
williamr@4
   201
     * So, if explcit notification on the strength of the random numbers is necessary, use TRandom::SecureRandomL.
williamr@2
   202
	 *
williamr@2
   203
	 * @param aDestination The buffer in which to write the random data.
williamr@2
   204
	 * @leave This function can leave under low memory conditions
williamr@2
   205
	 *
williamr@2
   206
	 */
williamr@2
   207
	IMPORT_C static void RandomL(TDes8& aDestination);
williamr@4
   208
	
williamr@4
   209
	/**
williamr@4
   210
	 * 
williamr@4
   211
	 * Fills the provided buffer with the pseudo-random data up to its current length, discarding any current
williamr@4
   212
	 * content of the descriptor. When this method returns normally (with out leave), the system state is secure
williamr@4
   213
	 * and hence the random numbers generated are cryptographically secure as well. When this method leaves with
williamr@4
   214
	 * the error code KErrNotSecure, the system internal state is not secure and hence the random numbers too.
williamr@4
   215
	 * 
williamr@4
   216
	 * Though this method leaves when the system internal state is not secure, still the descriptor will be filled 
williamr@4
   217
	 * with pseudo-random bytes. This random data may or may not be secure enough. Recommended to treat these numbers 
williamr@4
   218
	 * as not secure.
williamr@4
   219
	 *
williamr@4
   220
	 * @param aDestination The buffer in which to write the random data.
williamr@4
   221
	 * @leave KErrNotSecure The generated random numbers is not secure enough for cryptographic operations.
williamr@4
   222
	 *        Otherwise, leaves with some other system wide error codes.
williamr@4
   223
	 *
williamr@4
   224
	 */
williamr@4
   225
	IMPORT_C static void SecureRandomL(TDes8& aDestination);
williamr@2
   226
	};
williamr@2
   227
williamr@2
   228
class RRandomSession:public RSessionBase
williamr@2
   229
/**
williamr@2
   230
 *
williamr@2
   231
 * The client interface to the system random number generator. End
williamr@2
   232
 * users should use TRandom instead of this interface.
williamr@2
   233
 *
williamr@2
   234
 * @publishedAll
williamr@2
   235
 * @released
williamr@2
   236
 */
williamr@2
   237
	{
williamr@2
   238
public:
williamr@2
   239
williamr@2
   240
	IMPORT_C RRandomSession(void);
williamr@2
   241
	
williamr@2
   242
	/**
williamr@2
   243
	 * 
williamr@4
   244
	 * Fills the provided buffer with pseudo-random data up to its
williamr@2
   245
	 * current length, discarding any current content.
williamr@2
   246
	 *
williamr@2
   247
	 * @param aDestination The buffer in to which to write the random data 
williamr@2
   248
	 *
williamr@2
   249
	 */
williamr@2
   250
	IMPORT_C TInt GetRandom(TDes8& aDestination);
williamr@2
   251
	
williamr@2
   252
	/**
williamr@2
   253
	 *
williamr@4
   254
	 * Opens a new session with the random number generator.
williamr@2
   255
	 *
williamr@2
   256
	 */
williamr@2
   257
	IMPORT_C void ConnectL(void);
williamr@2
   258
	};
williamr@2
   259
williamr@2
   260
#endif // __RANDOM_H__