|
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 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 /** |
|
22 @file |
|
23 */ |
|
24 |
|
25 #include "randsvr.h" |
|
26 #include "randcliserv.h" |
|
27 #include <e32math.h> |
|
28 |
|
29 //const TInt KFastTickTimer=1000000; // These are testing numbers! |
|
30 //const TInt KSlowTickTimer=30000000; |
|
31 const TInt KThreshold=1024; |
|
32 |
|
33 const TInt KFastTickTimer=30000000; // These are the real numbers! |
|
34 const TInt KSlowTickTimer=0x7fffffff; |
|
35 |
|
36 |
|
37 void SignalClient() |
|
38 // |
|
39 // Signal the owning thread that the server has started successfully |
|
40 // This may itself fail |
|
41 // |
|
42 { |
|
43 RProcess::Rendezvous(KErrNone); |
|
44 } |
|
45 |
|
46 |
|
47 EXPORT_C TInt RunRandomServer(TAny* /*aUnused*/) |
|
48 { |
|
49 CTrapCleanup* cleanup=CTrapCleanup::New(); |
|
50 if (!cleanup) |
|
51 { |
|
52 return KErrNoMemory; |
|
53 } |
|
54 |
|
55 TInt ret = User::RenameThread(KRandomServerName); |
|
56 |
|
57 __ASSERT_ALWAYS(ret==KErrNone,User::Panic(KRandomServerName,KErrServerTerminated)); |
|
58 |
|
59 if (CRandomScheduler::New()) |
|
60 return KErrNoMemory; |
|
61 ret = CRandomServer::New(); |
|
62 if (ret != KErrNone ) |
|
63 return ret; |
|
64 // Initialisation complete, now signal the client |
|
65 SignalClient(); |
|
66 |
|
67 CRandomScheduler::Start(); |
|
68 delete cleanup; |
|
69 return KErrNone; |
|
70 } |
|
71 |
|
72 TBool CRandomScheduler::New(void) |
|
73 { |
|
74 CRandomScheduler* rs; |
|
75 rs=new CRandomScheduler; |
|
76 CRandomScheduler::Install(rs); |
|
77 return (rs == NULL); |
|
78 } |
|
79 |
|
80 void CRandomScheduler::Error(TInt /*aError*/) const |
|
81 { |
|
82 User::Panic(KRandomServerName, 3); |
|
83 } |
|
84 |
|
85 CRandomServer::CRandomServer(void) : CServer2(EPriorityLow) |
|
86 { |
|
87 } |
|
88 |
|
89 CRandomServer::~CRandomServer(void) |
|
90 { |
|
91 // This should never happen....but in case it does: |
|
92 delete iHash; |
|
93 delete iTicker; |
|
94 delete iPool; |
|
95 } |
|
96 |
|
97 TInt CRandomServer::New(void) |
|
98 { |
|
99 CRandomServer* self; |
|
100 self=new CRandomServer; |
|
101 if (!self) |
|
102 { |
|
103 return KErrNoMemory; |
|
104 } |
|
105 TRAPD(ret,self->ConstructL()); |
|
106 if (ret) |
|
107 { |
|
108 return ret; |
|
109 } |
|
110 |
|
111 return self->Start(KRandomServerName); |
|
112 } |
|
113 |
|
114 void CRandomServer::ConstructL(void) |
|
115 { |
|
116 iPool=new (ELeave) TUint8[KRandomPoolSize]; |
|
117 iHash=CSHA1::NewL(); |
|
118 iPoolIn=0; |
|
119 iPoolOut=0; |
|
120 iTicker=CPeriodic::NewL(EPriorityLow); |
|
121 TCallBack callback(Tick,this); |
|
122 iTicker->Start(KFastTickTimer,KFastTickTimer,callback); // **** these figures might need tweaking! |
|
123 iQuality=0; |
|
124 iFast=ETrue; |
|
125 } |
|
126 |
|
127 TInt CRandomServer::Tick(TAny* aServer) |
|
128 { |
|
129 CRandomServer* svr=(CRandomServer*)aServer; |
|
130 svr->Stir(); |
|
131 svr->iQuality+=30; |
|
132 if (svr->iFast) |
|
133 { |
|
134 if (svr->iQuality>KThreshold) |
|
135 { |
|
136 TCallBack callback(Tick,svr); |
|
137 svr->iTicker->Cancel(); |
|
138 svr->iTicker->Start(KSlowTickTimer,KSlowTickTimer,callback); // **** these figures might need tweaking! |
|
139 svr->iFast=EFalse; |
|
140 } |
|
141 } |
|
142 if (svr->iQuality>(KRandomPoolSize<<3)) |
|
143 { |
|
144 svr->iQuality=(KRandomPoolSize<<3); |
|
145 } |
|
146 return ETrue; |
|
147 } |
|
148 |
|
149 CSession2* CRandomServer::NewSessionL(const TVersion& /*aVersion*/, const RMessage2& /*aMessage*/) const |
|
150 { |
|
151 return CRandomSession::NewL(const_cast<CRandomServer*>(this)); |
|
152 //CRandomSession::NewL(CONST_CAST(CRandomServer*,this),Message().Client()); |
|
153 } |
|
154 |
|
155 TPtrC8 CRandomServer::GetRandom(void) |
|
156 { |
|
157 TPtr8 res(&iPool[iPoolOut],iHash->HashSize(),iHash->HashSize()); |
|
158 iPoolOut+=iHash->HashSize(); |
|
159 if ((iPoolOut+iHash->HashSize())>KRandomPoolSize) |
|
160 { |
|
161 iPoolOut=0; |
|
162 } |
|
163 return iHash->Hash(res); |
|
164 } |
|
165 |
|
166 CMessageDigest* CRandomServer::Hash(void) const |
|
167 { |
|
168 return iHash; |
|
169 } |
|
170 |
|
171 void CRandomServer::Stir(void) |
|
172 { |
|
173 TInt rnd; |
|
174 rnd=Math::Random(); |
|
175 TPtrC8 r((TUint8*)&rnd,sizeof(TInt)); |
|
176 iHash->Hash(r); |
|
177 TPtr8 dest(&iPool[iPoolIn],iHash->HashSize()); |
|
178 dest.Copy(iHash->Hash(dest)); |
|
179 iPoolIn+=iHash->HashSize(); |
|
180 if ((iPoolIn+iHash->HashSize())>KRandomPoolSize) |
|
181 { |
|
182 iPoolIn=0; |
|
183 } |
|
184 } |
|
185 |
|
186 CRandomSession* CRandomSession::NewL(CRandomServer* aServer) |
|
187 { |
|
188 CRandomSession* self; |
|
189 self=new (ELeave) CRandomSession(aServer); |
|
190 return self; |
|
191 } |
|
192 |
|
193 CRandomSession::CRandomSession(CRandomServer* aServer) : CSession2(), iServer(aServer) |
|
194 { |
|
195 } |
|
196 |
|
197 CRandomSession::~CRandomSession(void) |
|
198 { |
|
199 } |
|
200 |
|
201 void CRandomSession::ServiceL(const RMessage2& aMessage) |
|
202 { |
|
203 switch (aMessage.Function()) |
|
204 { |
|
205 case KRandomRequest: |
|
206 { |
|
207 TInt ret = FillBuffer(aMessage); |
|
208 aMessage.Complete(ret); |
|
209 break; |
|
210 } |
|
211 default: |
|
212 aMessage.Complete(KErrNotSupported); |
|
213 break; |
|
214 }; |
|
215 } |
|
216 |
|
217 TInt CRandomSession::FillBuffer(const RMessage2& aMessage) |
|
218 { |
|
219 TInt length = aMessage.Int1(); |
|
220 if (length < 0 || length > KRandomBlockSize) |
|
221 return KErrArgument; |
|
222 iServer->iQuality -= length; |
|
223 if (iServer->iQuality<0) |
|
224 { |
|
225 iServer->iQuality=0; |
|
226 } |
|
227 if (!iServer->iFast) |
|
228 { |
|
229 if (iServer->iQuality<KThreshold) |
|
230 { |
|
231 TCallBack callback(CRandomServer::Tick,iServer); |
|
232 iServer->iTicker->Cancel(); |
|
233 iServer->iTicker->Start(KFastTickTimer,KFastTickTimer,callback); // **** these figures might need tweaking! |
|
234 iServer->iFast=ETrue; |
|
235 } |
|
236 } |
|
237 TBuf8<KRandomBlockSize> buf(0); |
|
238 iServer->Stir(); |
|
239 TInt i; |
|
240 TInt hashsize=iServer->Hash()->HashSize(); |
|
241 for (i=0; i+hashsize < length; i+=hashsize) |
|
242 { |
|
243 buf.Append(iServer->GetRandom()); |
|
244 iServer->Stir(); |
|
245 } |
|
246 TPtrC8 ptr(iServer->GetRandom().Ptr(), length-i); |
|
247 buf.Append(ptr); |
|
248 |
|
249 TRAPD(ret, aMessage.WriteL(0, buf)); |
|
250 return ret; |
|
251 } |
|
252 |
|
253 GLDEF_C TInt E32Main(void) |
|
254 { |
|
255 return RunRandomServer(NULL); |
|
256 } |