os/security/crypto/weakcrypto/inc/random.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     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 securely generated random data up to the 
    44 	 * current length, discarding any current contents.
    45 	 *
    46 	 * @param aDest The buffer in to which to write random data.
    47 	 *
    48 	 */
    49 	virtual void GenerateBytesL(TDes8& aDest) = 0;
    50 protected:
    51 	IMPORT_C CRandom(void);
    52 private:
    53 	CRandom(const CRandom&);
    54 	CRandom& operator=(const CRandom&);
    55 	};
    56 
    57 /**
    58  *
    59  * Sets a secure random number generator implementation to use
    60  * for this thread.
    61  * 
    62  * @param aRNG The secure random number generator to use.
    63  *
    64  */
    65 IMPORT_C void SetThreadRandomL(CRandom* aRNG);
    66 
    67 /**
    68  *
    69  * Sets a secure random number generator implementation to use
    70  * for this thread, placing it on the cleanup stack.
    71  * 
    72  * @param aRNG The secure random number generator to use.
    73  *
    74  */
    75 IMPORT_C void SetThreadRandomLC(CRandom* aRNG);
    76 
    77 /** @internalAll */
    78 void DeleteThreadRandom(TAny* aPtr);
    79 
    80 /**
    81  *
    82  * Destroys the currently installed random number generator
    83  * that is in use for this thread.
    84  *
    85  */
    86 IMPORT_C void DestroyThreadRandom(void);
    87 
    88 /**
    89  *
    90  * Generates cryptographically secure random data, filling
    91  * the provided buffer up to its current length, discarding
    92  * any data that it may currently contain.
    93  *
    94  * @param aDest The buffer to fill with random data
    95  *
    96  */
    97 IMPORT_C void GenerateRandomBytesL(TDes8& aDest);
    98 
    99 class CSystemRandom : public CRandom
   100 /**
   101  *
   102  * This default secure random number generator uses
   103  * system state to generate entropy for the generation
   104  * of cryptographically secure random numbers.
   105  *
   106  * @publishedAll
   107  * @released
   108  *
   109  */
   110 
   111 	{
   112 public:
   113 
   114 	/**
   115 	 *
   116 	 * Constructs a new system random number generator.
   117 	 *
   118 	 * @return A ready-to-use random number generator.
   119 	 */
   120 	IMPORT_C static CSystemRandom* NewL(void);
   121 	
   122 	/**
   123 	 *
   124 	 * Constructs a new system random number generator,
   125 	 * and places it on the cleanup stack.
   126 	 *
   127 	 * @return A ready-to-use random number generator.
   128 	 *
   129 	 */
   130 	IMPORT_C static CSystemRandom* NewLC(void);
   131 	
   132 	/**
   133 	 *
   134 	 * Implements the contract as specified in the base
   135 	 * class, CRandom, filling the buffer supplied with
   136 	 * cryptographically secure random data up to its
   137 	 * current length, discarding its current content.
   138 	 *
   139 	 * @param aDest The buffer to which to write random data
   140 	 *
   141 	 */
   142 	virtual void GenerateBytesL(TDes8& aDest);
   143 private:
   144 	CSystemRandom(void);
   145 	CSystemRandom(const CSystemRandom&);
   146 	CSystemRandom& operator=(const CSystemRandom&);
   147 	};
   148 
   149 class TRandom
   150 /**
   151  *
   152  * The user interface to the system cryptographically 
   153  * secure random number generator.
   154  *
   155  * @publishedAll
   156  * @released
   157  */
   158 	{
   159 public:
   160 
   161 	/**
   162 	 * 
   163 	 * Fills the provided buffer with secure random data up to its
   164 	 * current length, discarding any current content.
   165 	 *
   166 	 * @param aDestination The buffer in which to write the random data.
   167 	 * @deprecated Use RandomL() instead
   168 	 * @panic This function can panic under low memory conditions
   169 	 *
   170 	 */
   171 	IMPORT_C static void Random(TDes8& aDestination);
   172 
   173 	/**
   174 	 * 
   175 	 * Fills the provided buffer with secure random data up to its
   176 	 * current length, discarding any current content.
   177 	 *
   178 	 * @param aDestination The buffer in which to write the random data.
   179 	 * @leave This function can leave under low memory conditions
   180 	 *
   181 	 */
   182 	IMPORT_C static void RandomL(TDes8& aDestination);
   183 	};
   184 
   185 class RRandomSession:public RSessionBase
   186 /**
   187  *
   188  * The client interface to the system random number generator. End
   189  * users should use TRandom instead of this interface.
   190  *
   191  * @publishedAll
   192  * @released
   193  */
   194 	{
   195 public:
   196 
   197 	IMPORT_C RRandomSession(void);
   198 	
   199 	/**
   200 	 * 
   201 	 * Fills the provided buffer with secure random data up to its
   202 	 * current length, discarding any current content.
   203 	 *
   204 	 * @param aDestination The buffer in to which to write the random data 
   205 	 *
   206 	 */
   207 	IMPORT_C TInt GetRandom(TDes8& aDestination);
   208 	
   209 	/**
   210 	 *
   211 	 * Opens a new session with the random number server.
   212 	 *
   213 	 */
   214 	IMPORT_C void ConnectL(void);
   215 	};
   216 
   217 #endif // __RANDOM_H__