diff -r 721a5e5fe251 -r 581b7c2ef978 cryptoplugins/cryptospiplugins/source/softwarecrypto/randomimpl.cpp --- a/cryptoplugins/cryptospiplugins/source/softwarecrypto/randomimpl.cpp Tue Jan 26 13:18:49 2010 +0200 +++ b/cryptoplugins/cryptospiplugins/source/softwarecrypto/randomimpl.cpp Fri Mar 19 10:03:04 2010 +0200 @@ -21,26 +21,16 @@ */ #include +#include #include #include "randomimpl.h" #include "pluginentry.h" #include "pluginconfig.h" - -#include "randsvr.h" -#include "randcliserv.h" -#include "randsvrimpl.h" - -_LIT(KRandomServerImg,"z:\\sys\\bin\\randsvr.exe"); // DLL/EXE name -_LIT(KRandomServerConnect, "Randsvr connect"); -_LIT(KRandomServerGet, "Randsvr get"); - -const TUid KServerUid3={0x100066dc}; - +#include "securityerr.h" using namespace SoftwareCrypto; - CRandomImpl* CRandomImpl::NewL(void) { CRandomImpl* self = new(ELeave)CRandomImpl(); @@ -54,31 +44,23 @@ return self; } -void CRandomImpl::GenerateRandomBytesL(TDes8& aDest) +void CRandomImpl::GenerateRandomBytesL(TDes8& aDestination) { - TRandomImpl::Random(aDest); + // Call the Math library to populate the buffer with random data. + TRAPD(err, Math::RandomL(aDestination)); + if(err != KErrNone) + { + // As the end users are interested only in the security aspect of the output but not + // the internal states, accordingly translate the kernel side error code if required. + err = (err == KErrNotReady) ? KErrNotSecure : err; + User::Leave(err); + } } CRandomImpl::CRandomImpl(void) { } -void TRandomImpl::Random(TDes8& aDestination) - { - RRandomSessionImpl rs; - TRAPD(ret,rs.ConnectL()); - if (ret != KErrNone) - { - User::Panic(KRandomServerConnect, ret); - } - TInt err=rs.GetRandom(aDestination); - if (err != KErrNone) - { - User::Panic(KRandomServerGet, err); - } - rs.Close(); - } - void CRandomImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics) { TInt randomNum = sizeof(KRandomCharacteristics)/sizeof(TRandomCharacteristics*); @@ -124,81 +106,6 @@ { } -RRandomSessionImpl::RRandomSessionImpl(void) - { - } - -static TInt StartServer() -// Borrowed from AndrewT's server startup code. -// Start the server process/thread which lives in an EPOCEXE object -// - { - - const TUidType serverUid(KNullUid,KNullUid,KServerUid3); - - // - // EPOC and EKA2 is easy, we just create a new server process. Simultaneous - // launching of two such processes should be detected when the second one - // attempts to create the server object, failing with KErrAlreadyExists. - // - RProcess server; - TInt r=server.Create(KRandomServerImg, KNullDesC, serverUid); - - if (r!=KErrNone) - return r; - TRequestStatus stat; - server.Rendezvous(stat); - if (stat!=KRequestPending) - server.Kill(0); // abort startup - else - server.Resume(); // logon OK - start the server - User::WaitForRequest(stat); // wait for start or death - // we can't use the 'exit reason' if the server panicked as this - // is the panic 'reason' and may be '0' which cannot be distinguished - // from KErrNone - r=(server.ExitType()==EExitPanic) ? KErrGeneral : stat.Int(); - server.Close(); - return r; - - } - -void RRandomSessionImpl::ConnectL(void) - { - TInt retry=2; - for (;;) - { - // Magic number 1 below is the number of asynchronous message slots - TInt r = CreateSession(KRandomServerName,TVersion(0,0,0), 1); - if (r == KErrNone) return; - // We used to leave with KErrNone, but this is inefficient and - // provokes an emulator problem in User::Leave which causes tpbe to crash - // if (r == KErrNone) User::Leave(r); // Connected okay - if (r != KErrNotFound && r != KErrServerTerminated) - User::Leave(r); // Something else happened - if (--retry == 0) - User::Leave(r); // Give up after a while - r = StartServer(); // Try starting again - if (r != KErrNone && r != KErrAlreadyExists) - User::Leave(r); - } - } - -TInt RRandomSessionImpl::GetRandom(TDes8& aDestination) - { - TInt desclength = aDestination.Length(); - for ( TInt i = 0; i < desclength; i += KRandomBlockSize) - { - TInt getlen = Min(KRandomBlockSize, desclength - i); - TPtr8 buffer(&aDestination[i], KRandomBlockSize, KRandomBlockSize); - TInt err = SendReceive(CRandomSession::KRandomRequest, TIpcArgs(&buffer, getlen)); - if (err != KErrNone) - { - return err; - } - } - return KErrNone; - } - // Methods which are not supported can be excluded from the coverage. #ifdef _BullseyeCoverage #pragma suppress_warnings on