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