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