|
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 */ |
|
25 |
|
26 #include <e32std.h> |
|
27 #include <random.h> |
|
28 #include <hash.h> |
|
29 #include <e32math.h> |
|
30 #include "randsvr.h" |
|
31 #include "randcliserv.h" |
|
32 |
|
33 _LIT(KRandomServerImg,"z:\\system\\libs\\randsvr.exe"); // DLL/EXE name |
|
34 _LIT(KRandomServerConnect, "Randsvr connect"); |
|
35 _LIT(KRandomServerGet, "Randsvr get"); |
|
36 |
|
37 const TUid KServerUid3={0x100066dc}; |
|
38 |
|
39 extern "C" { |
|
40 EXPORT_C void RAND_bytes(unsigned char* buf,int bytes) |
|
41 { |
|
42 TPtr8 ptr(buf,bytes,bytes); |
|
43 buf[0]++; |
|
44 TRandom::Random(ptr); |
|
45 } |
|
46 } |
|
47 |
|
48 EXPORT_C CRandom::CRandom(void) |
|
49 { |
|
50 } |
|
51 |
|
52 EXPORT_C CSystemRandom* CSystemRandom::NewL(void) |
|
53 { |
|
54 CSystemRandom* self = new(ELeave)CSystemRandom(); |
|
55 return self; |
|
56 } |
|
57 |
|
58 EXPORT_C CSystemRandom* CSystemRandom::NewLC(void) |
|
59 { |
|
60 CSystemRandom* self = NewL(); |
|
61 CleanupStack::PushL(self); |
|
62 return self; |
|
63 } |
|
64 |
|
65 void CSystemRandom::GenerateBytesL(TDes8& aDest) |
|
66 { |
|
67 TRandom::RandomL(aDest); |
|
68 } |
|
69 |
|
70 CSystemRandom::CSystemRandom(void) |
|
71 { |
|
72 } |
|
73 |
|
74 EXPORT_C void TRandom::Random(TDes8& aDestination) |
|
75 { |
|
76 RRandomSession rs; |
|
77 TRAPD(ret,rs.ConnectL()); |
|
78 if (ret) |
|
79 { |
|
80 User::Panic(KRandomServerConnect, ret); |
|
81 } |
|
82 TInt err=rs.GetRandom(aDestination); |
|
83 if (err) |
|
84 { |
|
85 User::Panic(KRandomServerGet, err); |
|
86 } |
|
87 rs.Close(); |
|
88 } |
|
89 |
|
90 EXPORT_C void TRandom::RandomL(TDes8& aDestination) |
|
91 { |
|
92 RRandomSession rs; |
|
93 TRAPD(ret,rs.ConnectL()); |
|
94 User::LeaveIfError(ret); |
|
95 CleanupClosePushL(rs); |
|
96 |
|
97 TInt err=rs.GetRandom(aDestination); |
|
98 User::LeaveIfError(err); |
|
99 |
|
100 CleanupStack::PopAndDestroy(); // rs |
|
101 } |
|
102 |
|
103 EXPORT_C RRandomSession::RRandomSession(void) |
|
104 { |
|
105 } |
|
106 |
|
107 static TInt StartServer() |
|
108 // Borrowed from AndrewT's server startup code. |
|
109 // Start the server process/thread which lives in an EPOCEXE object |
|
110 // |
|
111 { |
|
112 |
|
113 const TUidType serverUid(KNullUid,KNullUid,KServerUid3); |
|
114 |
|
115 // |
|
116 // EPOC and EKA2 is easy, we just create a new server process. Simultaneous |
|
117 // launching of two such processes should be detected when the second one |
|
118 // attempts to create the server object, failing with KErrAlreadyExists. |
|
119 // |
|
120 RProcess server; |
|
121 TInt r=server.Create(KRandomServerImg, KNullDesC, serverUid); |
|
122 |
|
123 if (r!=KErrNone) |
|
124 return r; |
|
125 TRequestStatus stat; |
|
126 server.Rendezvous(stat); |
|
127 if (stat!=KRequestPending) |
|
128 server.Kill(0); // abort startup |
|
129 else |
|
130 server.Resume(); // logon OK - start the server |
|
131 User::WaitForRequest(stat); // wait for start or death |
|
132 // we can't use the 'exit reason' if the server panicked as this |
|
133 // is the panic 'reason' and may be '0' which cannot be distinguished |
|
134 // from KErrNone |
|
135 r=(server.ExitType()==EExitPanic) ? KErrGeneral : stat.Int(); |
|
136 server.Close(); |
|
137 return r; |
|
138 |
|
139 } |
|
140 |
|
141 EXPORT_C void RRandomSession::ConnectL(void) |
|
142 { |
|
143 TInt retry=2; |
|
144 for (;;) |
|
145 { |
|
146 TInt r=CreateSession(KRandomServerName,TVersion(0,0,0),1); |
|
147 if (r!=KErrNotFound && r!=KErrServerTerminated) |
|
148 User::Leave(r); |
|
149 if (--retry==0) |
|
150 User::Leave(r); |
|
151 r=StartServer(); |
|
152 if (r!=KErrNone && r!=KErrAlreadyExists) |
|
153 User::Leave(r); |
|
154 } |
|
155 } |
|
156 |
|
157 EXPORT_C TInt RRandomSession::GetRandom(TDes8& aDestination) |
|
158 { |
|
159 if (aDestination.Length()<KRandomBlockSize) |
|
160 { |
|
161 return SendReceive(CRandomSession::KRandomRequest, |
|
162 TIpcArgs(&aDestination, aDestination.Length())); |
|
163 } |
|
164 else |
|
165 { |
|
166 TInt i; |
|
167 TInt err=KErrNone; |
|
168 TInt length=aDestination.Length(); |
|
169 for (i=0;(i+KRandomBlockSize)<length;i+=KRandomBlockSize) |
|
170 { |
|
171 TPtr8 buffer(&aDestination[i],KRandomBlockSize,KRandomBlockSize); |
|
172 err=SendReceive(CRandomSession::KRandomRequest, |
|
173 TIpcArgs(&buffer, KRandomBlockSize)); |
|
174 if (err) |
|
175 { |
|
176 return err; |
|
177 } |
|
178 } |
|
179 TPtr8 buffer(&aDestination[i],length%KRandomBlockSize,KRandomBlockSize); |
|
180 err=SendReceive(CRandomSession::KRandomRequest, |
|
181 TIpcArgs(&buffer, length-i)); |
|
182 return err; |
|
183 } |
|
184 } |