os/security/crypto/weakcrypto/source/random/threadrandom.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/threadrandom.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,65 @@
     1.4 +/*
     1.5 +* Copyright (c) 2003-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 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include <random.h>
    1.23 +
    1.24 +_LIT(KThreadRandom, "threadrandom.cpp");
    1.25 +
    1.26 +EXPORT_C void SetThreadRandomL(CRandom* aRNG)
    1.27 +	{
    1.28 +	User::LeaveIfError(Dll::SetTls(aRNG));	
    1.29 +	}
    1.30 +
    1.31 +EXPORT_C void SetThreadRandomLC(CRandom* aRNG)
    1.32 +	{
    1.33 +	CleanupStack::PushL(aRNG);
    1.34 +	SetThreadRandomL(aRNG);
    1.35 +	//This pop before the push isn't a problem as the PushL can't fail. 
    1.36 +	//We just did a push before this and thus there is enough room on the
    1.37 +	//cleanupstack such that OOM is not possible.
    1.38 +	CleanupStack::Pop(aRNG);
    1.39 +	CleanupStack::PushL(TCleanupItem(&DeleteThreadRandom, Dll::Tls()));
    1.40 +	}
    1.41 +
    1.42 +void DeleteThreadRandom(TAny* aPtr)
    1.43 +	{
    1.44 +	CRandom* random = reinterpret_cast<CRandom*>(aPtr);
    1.45 +	delete random;
    1.46 +	TInt result = Dll::SetTls(0);
    1.47 +	__ASSERT_ALWAYS(result == 0, User::Panic(KThreadRandom, 1));
    1.48 +	}
    1.49 +
    1.50 +EXPORT_C void DestroyThreadRandom(void)
    1.51 +	{
    1.52 +	delete (CRandom*)(Dll::Tls());
    1.53 +	TInt result = Dll::SetTls(0);
    1.54 +	__ASSERT_ALWAYS(result == 0, User::Panic(KThreadRandom, 1));
    1.55 +	}
    1.56 +
    1.57 +EXPORT_C void GenerateRandomBytesL(TDes8& aDest)
    1.58 +	{
    1.59 +	TAny* tls = Dll::Tls();
    1.60 +	if(tls)
    1.61 +		{
    1.62 +		((CRandom*)tls)->GenerateBytesL(aDest);
    1.63 +		}
    1.64 +	else
    1.65 +		{
    1.66 +		TRandom::RandomL(aDest);
    1.67 +		}
    1.68 +	}