sl@0: /* sl@0: * Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * random shim implementation sl@0: * random shim implementation sl@0: * sl@0: */ sl@0: sl@0: sl@0: /** sl@0: @file sl@0: */ sl@0: sl@0: #include "randomshim.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "securityerr.h" sl@0: sl@0: using namespace CryptoSpi; sl@0: sl@0: _LIT(KRandomFail, "Cannot obtain randomness"); sl@0: sl@0: // sl@0: // Random shim implementation sl@0: // sl@0: CRandomShim* CRandomShim::NewL() sl@0: { sl@0: CRandomShim* self = CRandomShim::NewLC(); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: CRandomShim* CRandomShim::NewLC() sl@0: { sl@0: CRandomShim* self = new(ELeave) CRandomShim(); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(); sl@0: return self; sl@0: } sl@0: sl@0: void CRandomShim::GenerateBytesL(TDes8& aDest) sl@0: { sl@0: iRandomImpl->GenerateRandomBytesL(aDest); sl@0: } sl@0: sl@0: CRandomShim::CRandomShim() sl@0: { sl@0: } sl@0: sl@0: CRandomShim::~CRandomShim() sl@0: { sl@0: delete iRandomImpl; sl@0: } sl@0: sl@0: void CRandomShim::ConstructL() sl@0: { sl@0: CRandomFactory::CreateRandomL(iRandomImpl, KRandomUid, NULL); sl@0: } sl@0: sl@0: /** sl@0: * @deprecated Use RandomL() instead sl@0: * @panic This function can panic under low memory conditions sl@0: * See PDEF097319: TRandom::Random panics during OOM sl@0: * This method is preserved only for BC reasons sl@0: */ sl@0: void TRandomShim::Random(TDes8& aDest) sl@0: { sl@0: CRandomShim* rand = NULL; sl@0: TRAPD(ret, rand = CRandomShim::NewL()); sl@0: if (ret != KErrNone) sl@0: { sl@0: User::Panic(KRandomFail, ret); sl@0: } sl@0: TRAPD(ret2, rand->GenerateBytesL(aDest)); sl@0: delete rand; sl@0: if ((ret2 != KErrNone) && (ret2 != KErrNotSecure)) sl@0: { sl@0: // this method can't leave so the cleanup stack can't be used (because of PushL()) sl@0: // so we just delete the randon shim here if GenerateBytesL() leaves sl@0: User::Panic(KRandomFail, ret); sl@0: } sl@0: } sl@0: sl@0: void TRandomShim::RandomL(TDes8& aDest) sl@0: { sl@0: CRandomShim* rand = CRandomShim::NewL(); sl@0: CleanupStack::PushL(rand); sl@0: sl@0: TRAPD(error, rand->GenerateBytesL(aDest)); sl@0: CleanupStack::PopAndDestroy(rand); // Use a singleton, avoid new overhead? sl@0: sl@0: // This method should leave on low memory conditions. sl@0: if(error == KErrNoMemory) sl@0: { sl@0: User::Leave(error); sl@0: } sl@0: } sl@0: sl@0: void TRandomShim::SecureRandomL(TDes8& aDest) sl@0: { sl@0: CRandomShim* rand = CRandomShim::NewLC(); sl@0: sl@0: rand->GenerateBytesL(aDest); sl@0: CleanupStack::PopAndDestroy(rand); sl@0: }