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