crypto/weakcryptospi/test/tplugins/src/randomimpl.cpp
branchRCL_3
changeset 41 9b5a3a9fddf8
parent 8 35751d3474b7
--- a/crypto/weakcryptospi/test/tplugins/src/randomimpl.cpp	Tue Jan 26 13:18:49 2010 +0200
+++ b/crypto/weakcryptospi/test/tplugins/src/randomimpl.cpp	Sat Feb 20 00:36:18 2010 +0200
@@ -21,22 +21,13 @@
 */
 
 #include <e32std.h>
+#include <e32math.h>
 #include <e32debug.h>
 
 #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;
 
@@ -54,31 +45,24 @@
 	return self;
 	}
 
-void CRandomImpl::GenerateRandomBytesL(TDes8& aDest)
-	{
-	TRandomImpl::Random(aDest);
+void CRandomImpl::GenerateRandomBytesL(TDes8& aDestination)
+	{	
+    // 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(TUid aImplementationUid) : iImplementationUid(aImplementationUid)
 	{
 	}
 
-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*);
@@ -128,76 +112,3 @@
 void CRandomImpl::Reset()
 	{
 	}
-
-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)
-			   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;
-	}