crypto/weakcrypto/source/random/threadrandom.cpp
changeset 0 2c201484c85f
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2003-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 #include <random.h>
       
    20 
       
    21 _LIT(KThreadRandom, "threadrandom.cpp");
       
    22 
       
    23 EXPORT_C void SetThreadRandomL(CRandom* aRNG)
       
    24 	{
       
    25 	User::LeaveIfError(Dll::SetTls(aRNG));	
       
    26 	}
       
    27 
       
    28 EXPORT_C void SetThreadRandomLC(CRandom* aRNG)
       
    29 	{
       
    30 	CleanupStack::PushL(aRNG);
       
    31 	SetThreadRandomL(aRNG);
       
    32 	//This pop before the push isn't a problem as the PushL can't fail. 
       
    33 	//We just did a push before this and thus there is enough room on the
       
    34 	//cleanupstack such that OOM is not possible.
       
    35 	CleanupStack::Pop(aRNG);
       
    36 	CleanupStack::PushL(TCleanupItem(&DeleteThreadRandom, Dll::Tls()));
       
    37 	}
       
    38 
       
    39 void DeleteThreadRandom(TAny* aPtr)
       
    40 	{
       
    41 	CRandom* random = reinterpret_cast<CRandom*>(aPtr);
       
    42 	delete random;
       
    43 	TInt result = Dll::SetTls(0);
       
    44 	__ASSERT_ALWAYS(result == 0, User::Panic(KThreadRandom, 1));
       
    45 	}
       
    46 
       
    47 EXPORT_C void DestroyThreadRandom(void)
       
    48 	{
       
    49 	delete (CRandom*)(Dll::Tls());
       
    50 	TInt result = Dll::SetTls(0);
       
    51 	__ASSERT_ALWAYS(result == 0, User::Panic(KThreadRandom, 1));
       
    52 	}
       
    53 
       
    54 EXPORT_C void GenerateRandomBytesL(TDes8& aDest)
       
    55 	{
       
    56 	TAny* tls = Dll::Tls();
       
    57 	if(tls)
       
    58 		{
       
    59 		((CRandom*)tls)->GenerateBytesL(aDest);
       
    60 		}
       
    61 	else
       
    62 		{
       
    63 		TRandom::RandomL(aDest);
       
    64 		}
       
    65 	}