os/security/crypto/weakcrypto/source/random/randsvr.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
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
/**
sl@0
    20
 @file
sl@0
    21
*/
sl@0
    22
sl@0
    23
#include "randsvr.h"
sl@0
    24
#include "randcliserv.h"
sl@0
    25
#include <e32math.h>
sl@0
    26
#include "randsvrimpl.h"
sl@0
    27
sl@0
    28
sl@0
    29
//const TInt KFastTickTimer=1000000;			// These are testing numbers!
sl@0
    30
//const TInt KSlowTickTimer=30000000;
sl@0
    31
const TInt KThreshold=1024;
sl@0
    32
sl@0
    33
const TInt KFastTickTimer=30000000;			// These are the real numbers!
sl@0
    34
const TInt KSlowTickTimer=0x7fffffff;
sl@0
    35
sl@0
    36
sl@0
    37
void SignalClient()
sl@0
    38
//
sl@0
    39
// Signal the owning thread that the server has started successfully
sl@0
    40
// This may itself fail
sl@0
    41
//
sl@0
    42
	{
sl@0
    43
	RProcess::Rendezvous(KErrNone);
sl@0
    44
	}
sl@0
    45
sl@0
    46
sl@0
    47
EXPORT_C TInt RunRandomServer(TAny* /*aUnused*/)
sl@0
    48
	{
sl@0
    49
	CTrapCleanup* cleanup=CTrapCleanup::New();
sl@0
    50
	if (!cleanup)
sl@0
    51
		{
sl@0
    52
		return KErrNoMemory;
sl@0
    53
		}
sl@0
    54
sl@0
    55
	TInt ret = User::RenameThread(KRandomServerName);
sl@0
    56
sl@0
    57
	__ASSERT_ALWAYS(ret==KErrNone,User::Panic(KRandomServerName,KErrServerTerminated));
sl@0
    58
sl@0
    59
	if (CRandomScheduler::New())
sl@0
    60
		return KErrNoMemory;
sl@0
    61
	ret = CRandomServer::New();
sl@0
    62
	if (ret != KErrNone )
sl@0
    63
		return ret;
sl@0
    64
	// Initialisation complete, now signal the client
sl@0
    65
	SignalClient();
sl@0
    66
	
sl@0
    67
	CRandomScheduler::Start();
sl@0
    68
	delete cleanup;
sl@0
    69
	return KErrNone;
sl@0
    70
	}
sl@0
    71
sl@0
    72
TBool CRandomScheduler::New(void)
sl@0
    73
	{
sl@0
    74
	CRandomScheduler* rs;
sl@0
    75
	rs=new CRandomScheduler;
sl@0
    76
	CRandomScheduler::Install(rs);
sl@0
    77
	return (rs == NULL);
sl@0
    78
	}
sl@0
    79
sl@0
    80
void CRandomScheduler::Error(TInt /*aError*/) const 
sl@0
    81
	{
sl@0
    82
	User::Panic(KRandomServerName, 3);
sl@0
    83
	}
sl@0
    84
sl@0
    85
CRandomServer::CRandomServer(void) : CServer2(EPriorityLow)
sl@0
    86
	{
sl@0
    87
	}
sl@0
    88
sl@0
    89
CRandomServer::~CRandomServer(void)
sl@0
    90
	{
sl@0
    91
	// This should never happen....but in case it does:
sl@0
    92
	delete iHash;
sl@0
    93
	delete iTicker;
sl@0
    94
	delete iPool;
sl@0
    95
	}
sl@0
    96
sl@0
    97
TInt CRandomServer::New(void)
sl@0
    98
	{
sl@0
    99
	CRandomServer* self;
sl@0
   100
	self=new CRandomServer;
sl@0
   101
	if (!self)
sl@0
   102
		{
sl@0
   103
		return KErrNoMemory;
sl@0
   104
		}
sl@0
   105
	TRAPD(ret,self->ConstructL());
sl@0
   106
	if (ret)
sl@0
   107
		{
sl@0
   108
		return ret;
sl@0
   109
		}
sl@0
   110
	
sl@0
   111
	return self->Start(KRandomServerName);
sl@0
   112
	}
sl@0
   113
sl@0
   114
void CRandomServer::ConstructL(void)
sl@0
   115
	{
sl@0
   116
	iPool=new (ELeave) TUint8[KRandomPoolSize];
sl@0
   117
	iHash=CSHA1::NewL();
sl@0
   118
	iPoolIn=0;
sl@0
   119
	iPoolOut=0;
sl@0
   120
	iTicker=CPeriodic::NewL(EPriorityLow);
sl@0
   121
	TCallBack callback(Tick,this);
sl@0
   122
	iTicker->Start(KFastTickTimer,KFastTickTimer,callback);		// **** these figures might need tweaking!
sl@0
   123
	iQuality=0;
sl@0
   124
	iFast=ETrue;
sl@0
   125
	}
sl@0
   126
sl@0
   127
TInt CRandomServer::Tick(TAny* aServer)
sl@0
   128
	{
sl@0
   129
	CRandomServer* svr=(CRandomServer*)aServer;
sl@0
   130
	svr->Stir();
sl@0
   131
	svr->iQuality+=30;
sl@0
   132
	if (svr->iFast)
sl@0
   133
		{
sl@0
   134
		if (svr->iQuality>KThreshold)
sl@0
   135
			{
sl@0
   136
			TCallBack callback(Tick,svr);
sl@0
   137
			svr->iTicker->Cancel();
sl@0
   138
			svr->iTicker->Start(KSlowTickTimer,KSlowTickTimer,callback);		// **** these figures might need tweaking!
sl@0
   139
			svr->iFast=EFalse;
sl@0
   140
			}
sl@0
   141
		}
sl@0
   142
	if (svr->iQuality>(KRandomPoolSize<<3))
sl@0
   143
		{
sl@0
   144
		svr->iQuality=(KRandomPoolSize<<3);
sl@0
   145
		}
sl@0
   146
	return ETrue;
sl@0
   147
	}
sl@0
   148
sl@0
   149
CSession2* CRandomServer::NewSessionL(const TVersion& /*aVersion*/, const RMessage2& /*aMessage*/) const
sl@0
   150
	{
sl@0
   151
	return CRandomSession::NewL(const_cast<CRandomServer*>(this));
sl@0
   152
	//CRandomSession::NewL(CONST_CAST(CRandomServer*,this),Message().Client());
sl@0
   153
	}
sl@0
   154
sl@0
   155
TPtrC8 CRandomServer::GetRandom(void)
sl@0
   156
	{
sl@0
   157
	TPtr8 res(&iPool[iPoolOut],iHash->HashSize(),iHash->HashSize());
sl@0
   158
	iPoolOut+=iHash->HashSize();
sl@0
   159
	if ((iPoolOut+iHash->HashSize())>KRandomPoolSize)
sl@0
   160
		{
sl@0
   161
		iPoolOut=0;
sl@0
   162
		}
sl@0
   163
	return iHash->Hash(res);
sl@0
   164
	}
sl@0
   165
sl@0
   166
CMessageDigest* CRandomServer::Hash(void) const
sl@0
   167
	{
sl@0
   168
	return iHash;
sl@0
   169
	}
sl@0
   170
sl@0
   171
void CRandomServer::Stir(void) 
sl@0
   172
	{
sl@0
   173
	TInt rnd;
sl@0
   174
	rnd=Math::Random();
sl@0
   175
	TPtrC8 r((TUint8*)&rnd,sizeof(TInt));
sl@0
   176
	iHash->Hash(r);
sl@0
   177
	TPtr8 dest(&iPool[iPoolIn],iHash->HashSize());
sl@0
   178
	dest.Copy(iHash->Hash(dest));
sl@0
   179
	iPoolIn+=iHash->HashSize();
sl@0
   180
	if ((iPoolIn+iHash->HashSize())>KRandomPoolSize)
sl@0
   181
		{
sl@0
   182
		iPoolIn=0;
sl@0
   183
		}
sl@0
   184
	}
sl@0
   185
sl@0
   186
CRandomSession* CRandomSession::NewL(CRandomServer* aServer)
sl@0
   187
	{
sl@0
   188
	CRandomSession* self;
sl@0
   189
	self=new (ELeave) CRandomSession(aServer);
sl@0
   190
	return self;
sl@0
   191
	}
sl@0
   192
sl@0
   193
CRandomSession::CRandomSession(CRandomServer* aServer) : CSession2(), iServer(aServer)
sl@0
   194
	{
sl@0
   195
	}
sl@0
   196
sl@0
   197
CRandomSession::~CRandomSession(void)
sl@0
   198
	{
sl@0
   199
	}
sl@0
   200
sl@0
   201
void CRandomSession::ServiceL(const RMessage2& aMessage)
sl@0
   202
	{
sl@0
   203
	switch (aMessage.Function())
sl@0
   204
		{
sl@0
   205
	case KRandomRequest:
sl@0
   206
		{
sl@0
   207
		TInt ret = FillBuffer(aMessage);
sl@0
   208
		aMessage.Complete(ret);
sl@0
   209
		break;
sl@0
   210
		}
sl@0
   211
	default:
sl@0
   212
		aMessage.Complete(KErrNotSupported);
sl@0
   213
		break;
sl@0
   214
		};
sl@0
   215
	}
sl@0
   216
sl@0
   217
TInt CRandomSession::FillBuffer(const RMessage2& aMessage)
sl@0
   218
	{
sl@0
   219
	TInt length = aMessage.Int1();
sl@0
   220
	if (length < 0 || length > KRandomBlockSize)
sl@0
   221
		return KErrArgument;
sl@0
   222
	iServer->iQuality -= length;
sl@0
   223
	if (iServer->iQuality<0)
sl@0
   224
		{
sl@0
   225
		iServer->iQuality=0;
sl@0
   226
		}
sl@0
   227
	if (!iServer->iFast)
sl@0
   228
		{
sl@0
   229
		if (iServer->iQuality<KThreshold)
sl@0
   230
			{
sl@0
   231
			TCallBack callback(CRandomServer::Tick,iServer);
sl@0
   232
			iServer->iTicker->Cancel();
sl@0
   233
			iServer->iTicker->Start(KFastTickTimer,KFastTickTimer,callback);		// **** these figures might need tweaking!
sl@0
   234
			iServer->iFast=ETrue;
sl@0
   235
			}
sl@0
   236
		}
sl@0
   237
	TBuf8<KRandomBlockSize> buf(0);
sl@0
   238
	iServer->Stir();
sl@0
   239
	TInt i;
sl@0
   240
	TInt hashsize=iServer->Hash()->HashSize();
sl@0
   241
	for (i=0; i+hashsize < length; i+=hashsize)
sl@0
   242
		{
sl@0
   243
		buf.Append(iServer->GetRandom());
sl@0
   244
		iServer->Stir();
sl@0
   245
		}
sl@0
   246
	TPtrC8 ptr(iServer->GetRandom().Ptr(), length-i);
sl@0
   247
	buf.Append(ptr);
sl@0
   248
	
sl@0
   249
	TRAPD(ret, aMessage.WriteL(0, buf));
sl@0
   250
	return ret;
sl@0
   251
	}
sl@0
   252
sl@0
   253
GLDEF_C TInt E32Main(void)
sl@0
   254
	{
sl@0
   255
	return RunRandomServer(NULL);
sl@0
   256
	}