os/security/crypto/weakcryptospi/source/random/random.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2005-2010 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 /**
    20  @file
    21 */
    22 
    23 #include <e32std.h>
    24 #include <e32math.h>
    25 #include <random.h>
    26 #include <hash.h>
    27 #include "randomshim.h"
    28 
    29 extern "C" {
    30 EXPORT_C void RAND_bytes(unsigned char* buf,int bytes)
    31 	{
    32 	TPtr8 ptr(buf,bytes,bytes);
    33 	buf[0]++;
    34 	TRandom::Random(ptr);
    35 	}
    36 }
    37 
    38 EXPORT_C CRandom::CRandom(void)
    39 	{
    40 	}
    41 
    42 EXPORT_C CSystemRandom* CSystemRandom::NewL(void)
    43 	{
    44 	CSystemRandom* self = NewLC();
    45 	CleanupStack::Pop(self);
    46 	return self;
    47 	}
    48 
    49 EXPORT_C CSystemRandom* CSystemRandom::NewLC(void)
    50 	{
    51 	CSystemRandom* self = new(ELeave)CSystemRandom();
    52 	CleanupStack::PushL(self);
    53 	self->ConstructL();
    54 	return self;
    55 	}
    56 
    57 void CSystemRandom::GenerateBytesL(TDes8& aDest)
    58 	{
    59 	TRAPD(error, iShim->GenerateBytesL(aDest));
    60     // This method must leave on low memory conditions.
    61     if(error == KErrNoMemory)
    62         {
    63         User::Leave(error); 
    64         }
    65     }
    66 
    67 CSystemRandom::CSystemRandom(void)
    68 	{
    69 	}
    70 
    71 CSystemRandom::~CSystemRandom()
    72 	{
    73 	delete iShim;
    74 	}
    75 
    76 void CSystemRandom::ConstructL()
    77 	{
    78 	iShim = CRandomShim::NewL();
    79 	}
    80 
    81 // Methods replace by shim are excluded from coverage.
    82 #ifdef _BullseyeCoverage
    83 #pragma suppress_warnings on
    84 #pragma BullseyeCoverage off
    85 #pragma suppress_warnings off
    86 #endif
    87 EXPORT_C void TRandom::Random(TDes8& aDestination)
    88 	{
    89 	// Method replaced by shim
    90 	TRandomShim::Random(aDestination);
    91 	}
    92 
    93 EXPORT_C void TRandom::RandomL(TDes8& aDestination)
    94 	{
    95 	// Method replaced by shim
    96 	TRandomShim::RandomL(aDestination);
    97 	}
    98 	
    99 EXPORT_C void TRandom::SecureRandomL(TDes8& aDestination)
   100 	{
   101 	// Method replaced by shim
   102 	TRandomShim::SecureRandomL(aDestination);
   103 	}
   104 
   105 EXPORT_C RRandomSession::RRandomSession(void)
   106 	{
   107 	}
   108 
   109 EXPORT_C void RRandomSession::ConnectL(void)
   110 	{
   111 	// No action required
   112 	}
   113 
   114 EXPORT_C TInt RRandomSession::GetRandom(TDes8& aDestination)
   115 	{
   116 	// Method replaced by shim
   117 	TRandomShim::Random(aDestination);
   118 	return KErrNone;
   119 	}