os/security/crypto/weakcryptospi/source/random/randomshim.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
* random shim implementation
sl@0
    16
* random shim implementation
sl@0
    17
*
sl@0
    18
*/
sl@0
    19
sl@0
    20
sl@0
    21
/**
sl@0
    22
 @file
sl@0
    23
*/
sl@0
    24
sl@0
    25
#include "randomshim.h"
sl@0
    26
#include <cryptospi/cryptospidef.h>
sl@0
    27
#include <cryptospi/cryptorandomapi.h>
sl@0
    28
#include <cryptospi/plugincharacteristics.h>
sl@0
    29
#include <cryptospi/keys.h>
sl@0
    30
#include <e32debug.h>
sl@0
    31
#include "securityerr.h"
sl@0
    32
sl@0
    33
using namespace CryptoSpi;
sl@0
    34
sl@0
    35
_LIT(KRandomFail, "Cannot obtain randomness");
sl@0
    36
sl@0
    37
//
sl@0
    38
// Random shim implementation
sl@0
    39
//
sl@0
    40
CRandomShim* CRandomShim::NewL()
sl@0
    41
	{
sl@0
    42
	CRandomShim* self = CRandomShim::NewLC();
sl@0
    43
	CleanupStack::Pop();
sl@0
    44
	return self;
sl@0
    45
	}
sl@0
    46
sl@0
    47
CRandomShim* CRandomShim::NewLC()
sl@0
    48
	{
sl@0
    49
	CRandomShim* self = new(ELeave) CRandomShim();
sl@0
    50
	CleanupStack::PushL(self);
sl@0
    51
	self->ConstructL();
sl@0
    52
	return self;
sl@0
    53
	}
sl@0
    54
sl@0
    55
void CRandomShim::GenerateBytesL(TDes8& aDest)
sl@0
    56
	{	
sl@0
    57
	iRandomImpl->GenerateRandomBytesL(aDest);
sl@0
    58
	}
sl@0
    59
sl@0
    60
CRandomShim::CRandomShim()
sl@0
    61
	{
sl@0
    62
	}
sl@0
    63
sl@0
    64
CRandomShim::~CRandomShim()
sl@0
    65
	{
sl@0
    66
	delete iRandomImpl;
sl@0
    67
	}
sl@0
    68
	
sl@0
    69
void CRandomShim::ConstructL()
sl@0
    70
	{
sl@0
    71
	CRandomFactory::CreateRandomL(iRandomImpl, KRandomUid, NULL);	
sl@0
    72
	}
sl@0
    73
sl@0
    74
/**
sl@0
    75
 * @deprecated Use RandomL() instead
sl@0
    76
 * @panic This function can panic under low memory conditions
sl@0
    77
 * See PDEF097319: TRandom::Random panics during OOM
sl@0
    78
 * This method is preserved only for BC reasons
sl@0
    79
 */
sl@0
    80
void TRandomShim::Random(TDes8& aDest)
sl@0
    81
	{
sl@0
    82
	CRandomShim* rand = NULL;
sl@0
    83
	TRAPD(ret, rand = CRandomShim::NewL());
sl@0
    84
	if (ret != KErrNone)
sl@0
    85
		{
sl@0
    86
		User::Panic(KRandomFail, ret);
sl@0
    87
		}
sl@0
    88
	TRAPD(ret2, rand->GenerateBytesL(aDest));
sl@0
    89
	delete rand;
sl@0
    90
	if ((ret2 != KErrNone) && (ret2 != KErrNotSecure))
sl@0
    91
		{
sl@0
    92
		// this method can't leave so the cleanup stack can't be used (because of PushL()) 
sl@0
    93
		// so we just delete the randon shim here if GenerateBytesL() leaves
sl@0
    94
		User::Panic(KRandomFail, ret);
sl@0
    95
		}
sl@0
    96
	}
sl@0
    97
sl@0
    98
void TRandomShim::RandomL(TDes8& aDest)
sl@0
    99
	{
sl@0
   100
	CRandomShim* rand = CRandomShim::NewL();
sl@0
   101
	CleanupStack::PushL(rand);
sl@0
   102
	
sl@0
   103
	TRAPD(error, rand->GenerateBytesL(aDest));
sl@0
   104
	CleanupStack::PopAndDestroy(rand); // Use a singleton, avoid new overhead?
sl@0
   105
	
sl@0
   106
	// This method should leave on low memory conditions.
sl@0
   107
	if(error == KErrNoMemory)
sl@0
   108
		{
sl@0
   109
		User::Leave(error);	
sl@0
   110
		}
sl@0
   111
	}	
sl@0
   112
sl@0
   113
void TRandomShim::SecureRandomL(TDes8& aDest)
sl@0
   114
	{
sl@0
   115
	CRandomShim* rand = CRandomShim::NewLC();	
sl@0
   116
	
sl@0
   117
	rand->GenerateBytesL(aDest);
sl@0
   118
	CleanupStack::PopAndDestroy(rand);	
sl@0
   119
	}