19 /** |
19 /** |
20 @file |
20 @file |
21 */ |
21 */ |
22 |
22 |
23 #include <e32std.h> |
23 #include <e32std.h> |
|
24 #include <e32math.h> |
24 #include <e32debug.h> |
25 #include <e32debug.h> |
25 |
26 |
26 #include "randomimpl.h" |
27 #include "randomimpl.h" |
27 #include "pluginentry.h" |
28 #include "pluginentry.h" |
28 #include "pluginconfig.h" |
29 #include "pluginconfig.h" |
29 |
30 #include "securityerr.h" |
30 #include "randsvr.h" |
|
31 #include "randcliserv.h" |
|
32 #include "randsvrimpl.h" |
|
33 |
|
34 _LIT(KRandomServerImg,"z:\\sys\\bin\\randsvr.exe"); // DLL/EXE name |
|
35 _LIT(KRandomServerConnect, "Randsvr connect"); |
|
36 _LIT(KRandomServerGet, "Randsvr get"); |
|
37 |
|
38 const TUid KServerUid3={0x100066dc}; |
|
39 |
|
40 |
31 |
41 using namespace SoftwareCrypto; |
32 using namespace SoftwareCrypto; |
42 |
33 |
43 |
34 |
44 CRandomImpl* CRandomImpl::NewL(TUid aImplementationUid) |
35 CRandomImpl* CRandomImpl::NewL(TUid aImplementationUid) |
52 CRandomImpl* self = NewL(aImplementationUid); |
43 CRandomImpl* self = NewL(aImplementationUid); |
53 CleanupStack::PushL(self); |
44 CleanupStack::PushL(self); |
54 return self; |
45 return self; |
55 } |
46 } |
56 |
47 |
57 void CRandomImpl::GenerateRandomBytesL(TDes8& aDest) |
48 void CRandomImpl::GenerateRandomBytesL(TDes8& aDestination) |
58 { |
49 { |
59 TRandomImpl::Random(aDest); |
50 // Call the Math library to populate the buffer with random data. |
|
51 TRAPD(err, Math::RandomL(aDestination)); |
|
52 if(err != KErrNone) |
|
53 { |
|
54 // As the end users are interested only in the security aspect of the output but not |
|
55 // the internal states, accordingly translate the kernel side error code if required. |
|
56 err = (err == KErrNotReady) ? KErrNotSecure : err; |
|
57 |
|
58 User::Leave(err); |
|
59 } |
60 } |
60 } |
61 |
61 |
62 CRandomImpl::CRandomImpl(TUid aImplementationUid) : iImplementationUid(aImplementationUid) |
62 CRandomImpl::CRandomImpl(TUid aImplementationUid) : iImplementationUid(aImplementationUid) |
63 { |
63 { |
64 } |
|
65 |
|
66 void TRandomImpl::Random(TDes8& aDestination) |
|
67 { |
|
68 RRandomSessionImpl rs; |
|
69 TRAPD(ret,rs.ConnectL()); |
|
70 if (ret != KErrNone) |
|
71 { |
|
72 User::Panic(KRandomServerConnect, ret); |
|
73 } |
|
74 TInt err=rs.GetRandom(aDestination); |
|
75 if (err != KErrNone) |
|
76 { |
|
77 User::Panic(KRandomServerGet, err); |
|
78 } |
|
79 rs.Close(); |
|
80 } |
64 } |
81 |
65 |
82 void CRandomImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics) |
66 void CRandomImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics) |
83 { |
67 { |
84 TInt randomNum = sizeof(KRandomCharacteristics)/sizeof(TRandomCharacteristics*); |
68 TInt randomNum = sizeof(KRandomCharacteristics)/sizeof(TRandomCharacteristics*); |
126 // All crypto plugins must implement this, to reset |
110 // All crypto plugins must implement this, to reset |
127 // hardware if required. Do nothing in this version |
111 // hardware if required. Do nothing in this version |
128 void CRandomImpl::Reset() |
112 void CRandomImpl::Reset() |
129 { |
113 { |
130 } |
114 } |
131 |
|
132 RRandomSessionImpl::RRandomSessionImpl(void) |
|
133 { |
|
134 } |
|
135 |
|
136 static TInt StartServer() |
|
137 // Borrowed from AndrewT's server startup code. |
|
138 // Start the server process/thread which lives in an EPOCEXE object |
|
139 // |
|
140 { |
|
141 |
|
142 const TUidType serverUid(KNullUid,KNullUid,KServerUid3); |
|
143 |
|
144 // |
|
145 // EPOC and EKA2 is easy, we just create a new server process. Simultaneous |
|
146 // launching of two such processes should be detected when the second one |
|
147 // attempts to create the server object, failing with KErrAlreadyExists. |
|
148 // |
|
149 RProcess server; |
|
150 TInt r=server.Create(KRandomServerImg, KNullDesC, serverUid); |
|
151 |
|
152 if (r!=KErrNone) |
|
153 return r; |
|
154 TRequestStatus stat; |
|
155 server.Rendezvous(stat); |
|
156 if (stat!=KRequestPending) |
|
157 server.Kill(0); // abort startup |
|
158 else |
|
159 server.Resume(); // logon OK - start the server |
|
160 User::WaitForRequest(stat); // wait for start or death |
|
161 // we can't use the 'exit reason' if the server panicked as this |
|
162 // is the panic 'reason' and may be '0' which cannot be distinguished |
|
163 // from KErrNone |
|
164 r=(server.ExitType()==EExitPanic) ? KErrGeneral : stat.Int(); |
|
165 server.Close(); |
|
166 return r; |
|
167 |
|
168 } |
|
169 |
|
170 void RRandomSessionImpl::ConnectL(void) |
|
171 { |
|
172 TInt retry=2; |
|
173 for (;;) |
|
174 { |
|
175 // Magic number 1 below is the number of asynchronous message slots |
|
176 TInt r = CreateSession(KRandomServerName,TVersion(0,0,0), 1); |
|
177 if (r == KErrNone) |
|
178 User::Leave(r); // Connected okay |
|
179 if (r != KErrNotFound && r != KErrServerTerminated) |
|
180 User::Leave(r); // Something else happened |
|
181 if (--retry == 0) |
|
182 User::Leave(r); // Give up after a while |
|
183 r = StartServer(); // Try starting again |
|
184 if (r != KErrNone && r != KErrAlreadyExists) |
|
185 User::Leave(r); |
|
186 } |
|
187 } |
|
188 |
|
189 TInt RRandomSessionImpl::GetRandom(TDes8& aDestination) |
|
190 { |
|
191 TInt desclength = aDestination.Length(); |
|
192 for ( TInt i = 0; i < desclength; i += KRandomBlockSize) |
|
193 { |
|
194 TInt getlen = Min(KRandomBlockSize, desclength - i); |
|
195 TPtr8 buffer(&aDestination[i], KRandomBlockSize, KRandomBlockSize); |
|
196 TInt err = SendReceive(CRandomSession::KRandomRequest, TIpcArgs(&buffer, getlen)); |
|
197 if (err != KErrNone) |
|
198 { |
|
199 return err; |
|
200 } |
|
201 } |
|
202 return KErrNone; |
|
203 } |
|