crypto/weakcryptospi/source/random/randomshim.cpp
branchRCL_3
changeset 41 9b5a3a9fddf8
parent 19 cd501b96611d
child 43 2f10d260163b
equal deleted inserted replaced
37:721a5e5fe251 41:9b5a3a9fddf8
    26 #include <cryptospi/cryptospidef.h>
    26 #include <cryptospi/cryptospidef.h>
    27 #include <cryptospi/cryptorandomapi.h>
    27 #include <cryptospi/cryptorandomapi.h>
    28 #include <cryptospi/plugincharacteristics.h>
    28 #include <cryptospi/plugincharacteristics.h>
    29 #include "keys.h"
    29 #include "keys.h"
    30 #include <e32debug.h>
    30 #include <e32debug.h>
    31 
    31 #include "securityerr.h"
    32 
    32 
    33 using namespace CryptoSpi;
    33 using namespace CryptoSpi;
    34 
    34 
    35 _LIT(KRandomFail, "Cannot obtain randomness");
    35 _LIT(KRandomFail, "Cannot obtain randomness");
    36 
    36 
    51 	self->ConstructL();
    51 	self->ConstructL();
    52 	return self;
    52 	return self;
    53 	}
    53 	}
    54 
    54 
    55 void CRandomShim::GenerateBytesL(TDes8& aDest)
    55 void CRandomShim::GenerateBytesL(TDes8& aDest)
    56 	{
    56 	{	
    57 	iRandomImpl->GenerateRandomBytesL(aDest);
    57 	iRandomImpl->GenerateRandomBytesL(aDest);
    58 	}
    58 	}
    59 
    59 
    60 CRandomShim::CRandomShim()
    60 CRandomShim::CRandomShim()
    61 	{
    61 	{
    63 
    63 
    64 CRandomShim::~CRandomShim()
    64 CRandomShim::~CRandomShim()
    65 	{
    65 	{
    66 	delete iRandomImpl;
    66 	delete iRandomImpl;
    67 	}
    67 	}
    68 
    68 	
    69 void CRandomShim::ConstructL()
    69 void CRandomShim::ConstructL()
    70 	{
    70 	{
    71 	CRandomFactory::CreateRandomL(iRandomImpl, KRandomUid, NULL);
    71 	CRandomFactory::CreateRandomL(iRandomImpl, KRandomUid, NULL);	
    72 	}
    72 	}
    73 
    73 
    74 /**
    74 /**
    75  * @deprecated Use RandomL() instead
    75  * @deprecated Use RandomL() instead
    76  * @panic This function can panic under low memory conditions
    76  * @panic This function can panic under low memory conditions
    85 		{
    85 		{
    86 		User::Panic(KRandomFail, ret);
    86 		User::Panic(KRandomFail, ret);
    87 		}
    87 		}
    88 	TRAPD(ret2, rand->GenerateBytesL(aDest));
    88 	TRAPD(ret2, rand->GenerateBytesL(aDest));
    89 	delete rand;
    89 	delete rand;
    90 	if (ret2 != KErrNone)
    90 	if ((ret2 != KErrNone) && (ret2 != KErrNotSecure))
    91 		{
    91 		{
    92 		// this method can't leave so the cleanup stack can't be used (because of PushL()) 
    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
    93 		// so we just delete the randon shim here if GenerateBytesL() leaves
    94 		User::Panic(KRandomFail, ret);
    94 		User::Panic(KRandomFail, ret);
    95 		}
    95 		}
    97 
    97 
    98 void TRandomShim::RandomL(TDes8& aDest)
    98 void TRandomShim::RandomL(TDes8& aDest)
    99 	{
    99 	{
   100 	CRandomShim* rand = CRandomShim::NewL();
   100 	CRandomShim* rand = CRandomShim::NewL();
   101 	CleanupStack::PushL(rand);
   101 	CleanupStack::PushL(rand);
       
   102 	
       
   103 	TRAPD(error, rand->GenerateBytesL(aDest));
       
   104 	CleanupStack::PopAndDestroy(rand); // Use a singleton, avoid new overhead?
       
   105 	
       
   106 	// This method should leave on low memory conditions.
       
   107 	if(error == KErrNoMemory)
       
   108 		{
       
   109 		User::Leave(error);	
       
   110 		}
       
   111 	}	
       
   112 
       
   113 void TRandomShim::SecureRandomL(TDes8& aDest)
       
   114 	{
       
   115 	CRandomShim* rand = CRandomShim::NewLC();	
       
   116 	
   102 	rand->GenerateBytesL(aDest);
   117 	rand->GenerateBytesL(aDest);
   103 	CleanupStack::PopAndDestroy(rand); // Use a singleton, avoid new overhead?
   118 	CleanupStack::PopAndDestroy(rand);	
   104 	}
   119 	}
   105