crypto/weakcryptospi/test/tplugins/src/randomimpl.cpp
changeset 8 35751d3474b7
child 43 9b5a3a9fddf8
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     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 <e32std.h>
       
    24 #include <e32debug.h>
       
    25 
       
    26 #include "randomimpl.h"
       
    27 #include "pluginentry.h"
       
    28 #include "pluginconfig.h"
       
    29 
       
    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 
       
    41 using namespace SoftwareCrypto;
       
    42 
       
    43 
       
    44 CRandomImpl* CRandomImpl::NewL(TUid aImplementationUid)
       
    45 	{
       
    46 	CRandomImpl* self = new(ELeave)CRandomImpl(aImplementationUid);
       
    47 	return self;
       
    48 	}
       
    49 
       
    50 CRandomImpl* CRandomImpl::NewLC(TUid aImplementationUid)
       
    51 	{
       
    52 	CRandomImpl* self = NewL(aImplementationUid);
       
    53 	CleanupStack::PushL(self);
       
    54 	return self;
       
    55 	}
       
    56 
       
    57 void CRandomImpl::GenerateRandomBytesL(TDes8& aDest)
       
    58 	{
       
    59 	TRandomImpl::Random(aDest);
       
    60 	}
       
    61 
       
    62 CRandomImpl::CRandomImpl(TUid aImplementationUid) : iImplementationUid(aImplementationUid)
       
    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 	}
       
    81 
       
    82 void CRandomImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
       
    83 	{
       
    84 	TInt randomNum = sizeof(KRandomCharacteristics)/sizeof(TRandomCharacteristics*);
       
    85 	for (TInt i = 0; i < randomNum; i++)
       
    86 		{
       
    87 		if (KRandomCharacteristics[i]->cmn.iImplementationUID == ImplementationUid().iUid)
       
    88 			{
       
    89 			aPluginCharacteristics = KRandomCharacteristics[i];
       
    90 			break;
       
    91 			}
       
    92 		}
       
    93 	}
       
    94 	
       
    95 TAny* CRandomImpl::GetExtension(TUid /*aExtensionId*/)
       
    96 	{
       
    97 	return NULL;
       
    98 	}
       
    99 	
       
   100 CExtendedCharacteristics* CRandomImpl::CreateExtendedCharacteristicsL()
       
   101 	{
       
   102 	// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
       
   103 	// for exclusive use and are not CERTIFIED to be standards compliant.
       
   104 	return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
       
   105 	}
       
   106 
       
   107 const CExtendedCharacteristics* CRandomImpl::GetExtendedCharacteristicsL()
       
   108 	{
       
   109 	return CRandomImpl::CreateExtendedCharacteristicsL();
       
   110 	}
       
   111 
       
   112 TUid CRandomImpl::ImplementationUid() const
       
   113 	{
       
   114 	return iImplementationUid;
       
   115 	}
       
   116 
       
   117 CRandomImpl::~CRandomImpl()
       
   118 	{
       
   119 	}
       
   120 
       
   121 void CRandomImpl::Close()
       
   122 	{
       
   123 	delete this;
       
   124 	}
       
   125 
       
   126 // All crypto plugins must implement this, to reset
       
   127 // hardware if required. Do nothing in this version
       
   128 void CRandomImpl::Reset()
       
   129 	{
       
   130 	}
       
   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 	}