os/security/crypto/weakcrypto/source/random/random.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcrypto/source/random/random.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,183 @@
     1.4 +/*
     1.5 +* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +* (c) 1999-2003 Symbian Ltd
    1.19 +*
    1.20 +*/
    1.21 +
    1.22 +
    1.23 +/**
    1.24 + @file
    1.25 +*/
    1.26 +
    1.27 +#include <e32std.h>
    1.28 +#include <random.h>
    1.29 +#include <hash.h>
    1.30 +#include <e32math.h>
    1.31 +#include "randsvr.h"
    1.32 +#include "randcliserv.h"
    1.33 +
    1.34 +_LIT(KRandomServerImg,"z:\\system\\libs\\randsvr.exe");		// DLL/EXE name
    1.35 +_LIT(KRandomServerConnect, "Randsvr connect");
    1.36 +_LIT(KRandomServerGet, "Randsvr get");
    1.37 +
    1.38 +const TUid KServerUid3={0x100066dc};
    1.39 +
    1.40 +extern "C" {
    1.41 +EXPORT_C void RAND_bytes(unsigned char* buf,int bytes)
    1.42 +	{
    1.43 +	TPtr8 ptr(buf,bytes,bytes);
    1.44 +	buf[0]++;
    1.45 +	TRandom::Random(ptr);
    1.46 +	}
    1.47 +}
    1.48 +
    1.49 +EXPORT_C CRandom::CRandom(void)
    1.50 +	{
    1.51 +	}
    1.52 +
    1.53 +EXPORT_C CSystemRandom* CSystemRandom::NewL(void)
    1.54 +	{
    1.55 +	CSystemRandom* self = new(ELeave)CSystemRandom();
    1.56 +	return self;
    1.57 +	}
    1.58 +
    1.59 +EXPORT_C CSystemRandom* CSystemRandom::NewLC(void)
    1.60 +	{
    1.61 +	CSystemRandom* self = NewL();
    1.62 +	CleanupStack::PushL(self);
    1.63 +	return self;
    1.64 +	}
    1.65 +
    1.66 +void CSystemRandom::GenerateBytesL(TDes8& aDest)
    1.67 +	{
    1.68 +	TRandom::RandomL(aDest);
    1.69 +	}
    1.70 +
    1.71 +CSystemRandom::CSystemRandom(void)
    1.72 +	{
    1.73 +	}
    1.74 +
    1.75 +EXPORT_C void TRandom::Random(TDes8& aDestination)
    1.76 +	{
    1.77 +	RRandomSession rs;
    1.78 +	TRAPD(ret,rs.ConnectL());
    1.79 +	if (ret)
    1.80 +		{
    1.81 +		User::Panic(KRandomServerConnect, ret);
    1.82 +		}
    1.83 +	TInt err=rs.GetRandom(aDestination);
    1.84 +	if (err)
    1.85 +		{
    1.86 +		User::Panic(KRandomServerGet, err);
    1.87 +		}
    1.88 +	rs.Close();
    1.89 +	}
    1.90 +
    1.91 +EXPORT_C void TRandom::RandomL(TDes8& aDestination)
    1.92 +	{
    1.93 +	RRandomSession rs;
    1.94 +	TRAPD(ret,rs.ConnectL());
    1.95 +	User::LeaveIfError(ret);
    1.96 +	CleanupClosePushL(rs);
    1.97 +
    1.98 +	TInt err=rs.GetRandom(aDestination);
    1.99 +	User::LeaveIfError(err);
   1.100 +
   1.101 +	CleanupStack::PopAndDestroy(); // rs
   1.102 +	}
   1.103 +
   1.104 +EXPORT_C RRandomSession::RRandomSession(void)
   1.105 +	{
   1.106 +	}
   1.107 +
   1.108 +static TInt StartServer()
   1.109 +// Borrowed from AndrewT's server startup code.
   1.110 +// Start the server process/thread which lives in an EPOCEXE object
   1.111 +//
   1.112 +	{
   1.113 +	
   1.114 +	const TUidType serverUid(KNullUid,KNullUid,KServerUid3);
   1.115 +
   1.116 +	//
   1.117 +	// EPOC and EKA2 is easy, we just create a new server process. Simultaneous
   1.118 +	// launching of two such processes should be detected when the second one
   1.119 +	// attempts to create the server object, failing with KErrAlreadyExists.
   1.120 +	//
   1.121 +	RProcess server;
   1.122 +	TInt r=server.Create(KRandomServerImg, KNullDesC, serverUid);
   1.123 +
   1.124 +	if (r!=KErrNone)
   1.125 +		return r;
   1.126 +	TRequestStatus stat;
   1.127 +	server.Rendezvous(stat);
   1.128 +	if (stat!=KRequestPending)
   1.129 +		server.Kill(0);		// abort startup
   1.130 +	else
   1.131 +		server.Resume();	// logon OK - start the server
   1.132 +	User::WaitForRequest(stat);		// wait for start or death
   1.133 +	// we can't use the 'exit reason' if the server panicked as this
   1.134 +	// is the panic 'reason' and may be '0' which cannot be distinguished
   1.135 +	// from KErrNone
   1.136 +	r=(server.ExitType()==EExitPanic) ? KErrGeneral : stat.Int();
   1.137 +	server.Close();
   1.138 +	return r;
   1.139 +
   1.140 +	}
   1.141 +
   1.142 +EXPORT_C void RRandomSession::ConnectL(void)
   1.143 +	{
   1.144 +	TInt retry=2;
   1.145 +	for (;;)
   1.146 +		{
   1.147 +		TInt r=CreateSession(KRandomServerName,TVersion(0,0,0),1);
   1.148 +		if (r!=KErrNotFound && r!=KErrServerTerminated)
   1.149 +			   User::Leave(r);
   1.150 +		if (--retry==0)
   1.151 +			User::Leave(r);
   1.152 +		r=StartServer();
   1.153 +		if (r!=KErrNone && r!=KErrAlreadyExists)
   1.154 +			User::Leave(r);
   1.155 +		}
   1.156 +	}
   1.157 +
   1.158 +EXPORT_C TInt RRandomSession::GetRandom(TDes8& aDestination)
   1.159 +	{
   1.160 +	if (aDestination.Length()<KRandomBlockSize)
   1.161 +		{
   1.162 +		return SendReceive(CRandomSession::KRandomRequest,
   1.163 +		                   TIpcArgs(&aDestination, aDestination.Length()));
   1.164 +		}
   1.165 +	else
   1.166 +		{
   1.167 +		TInt i;
   1.168 +		TInt err=KErrNone;
   1.169 +		TInt length=aDestination.Length();
   1.170 +		for (i=0;(i+KRandomBlockSize)<length;i+=KRandomBlockSize)
   1.171 +			{
   1.172 +			TPtr8 buffer(&aDestination[i],KRandomBlockSize,KRandomBlockSize);
   1.173 +			err=SendReceive(CRandomSession::KRandomRequest,
   1.174 +			 				TIpcArgs(&buffer, KRandomBlockSize));
   1.175 +			if (err)
   1.176 +				{
   1.177 +				return err;
   1.178 +				}
   1.179 +			}
   1.180 +		TPtr8 buffer(&aDestination[i],length%KRandomBlockSize,KRandomBlockSize);
   1.181 +		err=SendReceive(CRandomSession::KRandomRequest,
   1.182 +						TIpcArgs(&buffer, length-i));
   1.183 +		return err;
   1.184 +		}
   1.185 +	}
   1.186 +