First public contribution.
2 * Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * random shim implementation
16 * random shim implementation
25 #include "randomshim.h"
26 #include <cryptospi/cryptospidef.h>
27 #include <cryptospi/cryptorandomapi.h>
28 #include <cryptospi/plugincharacteristics.h>
29 #include <cryptospi/keys.h>
31 #include "securityerr.h"
33 using namespace CryptoSpi;
35 _LIT(KRandomFail, "Cannot obtain randomness");
38 // Random shim implementation
40 CRandomShim* CRandomShim::NewL()
42 CRandomShim* self = CRandomShim::NewLC();
47 CRandomShim* CRandomShim::NewLC()
49 CRandomShim* self = new(ELeave) CRandomShim();
50 CleanupStack::PushL(self);
55 void CRandomShim::GenerateBytesL(TDes8& aDest)
57 iRandomImpl->GenerateRandomBytesL(aDest);
60 CRandomShim::CRandomShim()
64 CRandomShim::~CRandomShim()
69 void CRandomShim::ConstructL()
71 CRandomFactory::CreateRandomL(iRandomImpl, KRandomUid, NULL);
75 * @deprecated Use RandomL() instead
76 * @panic This function can panic under low memory conditions
77 * See PDEF097319: TRandom::Random panics during OOM
78 * This method is preserved only for BC reasons
80 void TRandomShim::Random(TDes8& aDest)
82 CRandomShim* rand = NULL;
83 TRAPD(ret, rand = CRandomShim::NewL());
86 User::Panic(KRandomFail, ret);
88 TRAPD(ret2, rand->GenerateBytesL(aDest));
90 if ((ret2 != KErrNone) && (ret2 != KErrNotSecure))
92 // this method can't leave so the cleanup stack can't be used (because of PushL())
93 // so we just delete the randon shim here if GenerateBytesL() leaves
94 User::Panic(KRandomFail, ret);
98 void TRandomShim::RandomL(TDes8& aDest)
100 CRandomShim* rand = CRandomShim::NewL();
101 CleanupStack::PushL(rand);
103 TRAPD(error, rand->GenerateBytesL(aDest));
104 CleanupStack::PopAndDestroy(rand); // Use a singleton, avoid new overhead?
106 // This method should leave on low memory conditions.
107 if(error == KErrNoMemory)
113 void TRandomShim::SecureRandomL(TDes8& aDest)
115 CRandomShim* rand = CRandomShim::NewLC();
117 rand->GenerateBytesL(aDest);
118 CleanupStack::PopAndDestroy(rand);