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