os/security/crypto/weakcrypto/source/random/random.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
* (c) 1999-2003 Symbian Ltd
sl@0
    16
*
sl@0
    17
*/
sl@0
    18
sl@0
    19
sl@0
    20
/**
sl@0
    21
 @file
sl@0
    22
*/
sl@0
    23
sl@0
    24
#include <e32std.h>
sl@0
    25
#include <random.h>
sl@0
    26
#include <hash.h>
sl@0
    27
#include <e32math.h>
sl@0
    28
#include "randsvr.h"
sl@0
    29
#include "randcliserv.h"
sl@0
    30
sl@0
    31
_LIT(KRandomServerImg,"z:\\system\\libs\\randsvr.exe");		// DLL/EXE name
sl@0
    32
_LIT(KRandomServerConnect, "Randsvr connect");
sl@0
    33
_LIT(KRandomServerGet, "Randsvr get");
sl@0
    34
sl@0
    35
const TUid KServerUid3={0x100066dc};
sl@0
    36
sl@0
    37
extern "C" {
sl@0
    38
EXPORT_C void RAND_bytes(unsigned char* buf,int bytes)
sl@0
    39
	{
sl@0
    40
	TPtr8 ptr(buf,bytes,bytes);
sl@0
    41
	buf[0]++;
sl@0
    42
	TRandom::Random(ptr);
sl@0
    43
	}
sl@0
    44
}
sl@0
    45
sl@0
    46
EXPORT_C CRandom::CRandom(void)
sl@0
    47
	{
sl@0
    48
	}
sl@0
    49
sl@0
    50
EXPORT_C CSystemRandom* CSystemRandom::NewL(void)
sl@0
    51
	{
sl@0
    52
	CSystemRandom* self = new(ELeave)CSystemRandom();
sl@0
    53
	return self;
sl@0
    54
	}
sl@0
    55
sl@0
    56
EXPORT_C CSystemRandom* CSystemRandom::NewLC(void)
sl@0
    57
	{
sl@0
    58
	CSystemRandom* self = NewL();
sl@0
    59
	CleanupStack::PushL(self);
sl@0
    60
	return self;
sl@0
    61
	}
sl@0
    62
sl@0
    63
void CSystemRandom::GenerateBytesL(TDes8& aDest)
sl@0
    64
	{
sl@0
    65
	TRandom::RandomL(aDest);
sl@0
    66
	}
sl@0
    67
sl@0
    68
CSystemRandom::CSystemRandom(void)
sl@0
    69
	{
sl@0
    70
	}
sl@0
    71
sl@0
    72
EXPORT_C void TRandom::Random(TDes8& aDestination)
sl@0
    73
	{
sl@0
    74
	RRandomSession rs;
sl@0
    75
	TRAPD(ret,rs.ConnectL());
sl@0
    76
	if (ret)
sl@0
    77
		{
sl@0
    78
		User::Panic(KRandomServerConnect, ret);
sl@0
    79
		}
sl@0
    80
	TInt err=rs.GetRandom(aDestination);
sl@0
    81
	if (err)
sl@0
    82
		{
sl@0
    83
		User::Panic(KRandomServerGet, err);
sl@0
    84
		}
sl@0
    85
	rs.Close();
sl@0
    86
	}
sl@0
    87
sl@0
    88
EXPORT_C void TRandom::RandomL(TDes8& aDestination)
sl@0
    89
	{
sl@0
    90
	RRandomSession rs;
sl@0
    91
	TRAPD(ret,rs.ConnectL());
sl@0
    92
	User::LeaveIfError(ret);
sl@0
    93
	CleanupClosePushL(rs);
sl@0
    94
sl@0
    95
	TInt err=rs.GetRandom(aDestination);
sl@0
    96
	User::LeaveIfError(err);
sl@0
    97
sl@0
    98
	CleanupStack::PopAndDestroy(); // rs
sl@0
    99
	}
sl@0
   100
sl@0
   101
EXPORT_C RRandomSession::RRandomSession(void)
sl@0
   102
	{
sl@0
   103
	}
sl@0
   104
sl@0
   105
static TInt StartServer()
sl@0
   106
// Borrowed from AndrewT's server startup code.
sl@0
   107
// Start the server process/thread which lives in an EPOCEXE object
sl@0
   108
//
sl@0
   109
	{
sl@0
   110
	
sl@0
   111
	const TUidType serverUid(KNullUid,KNullUid,KServerUid3);
sl@0
   112
sl@0
   113
	//
sl@0
   114
	// EPOC and EKA2 is easy, we just create a new server process. Simultaneous
sl@0
   115
	// launching of two such processes should be detected when the second one
sl@0
   116
	// attempts to create the server object, failing with KErrAlreadyExists.
sl@0
   117
	//
sl@0
   118
	RProcess server;
sl@0
   119
	TInt r=server.Create(KRandomServerImg, KNullDesC, serverUid);
sl@0
   120
sl@0
   121
	if (r!=KErrNone)
sl@0
   122
		return r;
sl@0
   123
	TRequestStatus stat;
sl@0
   124
	server.Rendezvous(stat);
sl@0
   125
	if (stat!=KRequestPending)
sl@0
   126
		server.Kill(0);		// abort startup
sl@0
   127
	else
sl@0
   128
		server.Resume();	// logon OK - start the server
sl@0
   129
	User::WaitForRequest(stat);		// wait for start or death
sl@0
   130
	// we can't use the 'exit reason' if the server panicked as this
sl@0
   131
	// is the panic 'reason' and may be '0' which cannot be distinguished
sl@0
   132
	// from KErrNone
sl@0
   133
	r=(server.ExitType()==EExitPanic) ? KErrGeneral : stat.Int();
sl@0
   134
	server.Close();
sl@0
   135
	return r;
sl@0
   136
sl@0
   137
	}
sl@0
   138
sl@0
   139
EXPORT_C void RRandomSession::ConnectL(void)
sl@0
   140
	{
sl@0
   141
	TInt retry=2;
sl@0
   142
	for (;;)
sl@0
   143
		{
sl@0
   144
		TInt r=CreateSession(KRandomServerName,TVersion(0,0,0),1);
sl@0
   145
		if (r!=KErrNotFound && r!=KErrServerTerminated)
sl@0
   146
			   User::Leave(r);
sl@0
   147
		if (--retry==0)
sl@0
   148
			User::Leave(r);
sl@0
   149
		r=StartServer();
sl@0
   150
		if (r!=KErrNone && r!=KErrAlreadyExists)
sl@0
   151
			User::Leave(r);
sl@0
   152
		}
sl@0
   153
	}
sl@0
   154
sl@0
   155
EXPORT_C TInt RRandomSession::GetRandom(TDes8& aDestination)
sl@0
   156
	{
sl@0
   157
	if (aDestination.Length()<KRandomBlockSize)
sl@0
   158
		{
sl@0
   159
		return SendReceive(CRandomSession::KRandomRequest,
sl@0
   160
		                   TIpcArgs(&aDestination, aDestination.Length()));
sl@0
   161
		}
sl@0
   162
	else
sl@0
   163
		{
sl@0
   164
		TInt i;
sl@0
   165
		TInt err=KErrNone;
sl@0
   166
		TInt length=aDestination.Length();
sl@0
   167
		for (i=0;(i+KRandomBlockSize)<length;i+=KRandomBlockSize)
sl@0
   168
			{
sl@0
   169
			TPtr8 buffer(&aDestination[i],KRandomBlockSize,KRandomBlockSize);
sl@0
   170
			err=SendReceive(CRandomSession::KRandomRequest,
sl@0
   171
			 				TIpcArgs(&buffer, KRandomBlockSize));
sl@0
   172
			if (err)
sl@0
   173
				{
sl@0
   174
				return err;
sl@0
   175
				}
sl@0
   176
			}
sl@0
   177
		TPtr8 buffer(&aDestination[i],length%KRandomBlockSize,KRandomBlockSize);
sl@0
   178
		err=SendReceive(CRandomSession::KRandomRequest,
sl@0
   179
						TIpcArgs(&buffer, length-i));
sl@0
   180
		return err;
sl@0
   181
		}
sl@0
   182
	}
sl@0
   183