19
|
1 |
/*
|
|
2 |
* Copyright (c) 2006-2009 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 |
* random shim implementation
|
|
16 |
* random shim implementation
|
|
17 |
*
|
|
18 |
*/
|
|
19 |
|
|
20 |
|
|
21 |
/**
|
|
22 |
@file
|
|
23 |
*/
|
|
24 |
|
|
25 |
#include "randomshim.h"
|
|
26 |
#include <cryptospi/cryptospidef.h>
|
|
27 |
#include <cryptospi/cryptorandomapi.h>
|
|
28 |
#include <cryptospi/plugincharacteristics.h>
|
|
29 |
#include "keys.h"
|
|
30 |
#include <e32debug.h>
|
|
31 |
|
|
32 |
|
|
33 |
using namespace CryptoSpi;
|
|
34 |
|
|
35 |
_LIT(KRandomFail, "Cannot obtain randomness");
|
|
36 |
|
|
37 |
//
|
|
38 |
// Random shim implementation
|
|
39 |
//
|
|
40 |
CRandomShim* CRandomShim::NewL()
|
|
41 |
{
|
|
42 |
CRandomShim* self = CRandomShim::NewLC();
|
|
43 |
CleanupStack::Pop();
|
|
44 |
return self;
|
|
45 |
}
|
|
46 |
|
|
47 |
CRandomShim* CRandomShim::NewLC()
|
|
48 |
{
|
|
49 |
CRandomShim* self = new(ELeave) CRandomShim();
|
|
50 |
CleanupStack::PushL(self);
|
|
51 |
self->ConstructL();
|
|
52 |
return self;
|
|
53 |
}
|
|
54 |
|
|
55 |
void CRandomShim::GenerateBytesL(TDes8& aDest)
|
|
56 |
{
|
|
57 |
iRandomImpl->GenerateRandomBytesL(aDest);
|
|
58 |
}
|
|
59 |
|
|
60 |
CRandomShim::CRandomShim()
|
|
61 |
{
|
|
62 |
}
|
|
63 |
|
|
64 |
CRandomShim::~CRandomShim()
|
|
65 |
{
|
|
66 |
delete iRandomImpl;
|
|
67 |
}
|
|
68 |
|
|
69 |
void CRandomShim::ConstructL()
|
|
70 |
{
|
|
71 |
CRandomFactory::CreateRandomL(iRandomImpl, KRandomUid, NULL);
|
|
72 |
}
|
|
73 |
|
|
74 |
/**
|
|
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
|
|
79 |
*/
|
|
80 |
void TRandomShim::Random(TDes8& aDest)
|
|
81 |
{
|
|
82 |
CRandomShim* rand = NULL;
|
|
83 |
TRAPD(ret, rand = CRandomShim::NewL());
|
|
84 |
if (ret != KErrNone)
|
|
85 |
{
|
|
86 |
User::Panic(KRandomFail, ret);
|
|
87 |
}
|
|
88 |
TRAPD(ret2, rand->GenerateBytesL(aDest));
|
|
89 |
delete rand;
|
|
90 |
if (ret2 != KErrNone)
|
|
91 |
{
|
|
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);
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
|
98 |
void TRandomShim::RandomL(TDes8& aDest)
|
|
99 |
{
|
|
100 |
CRandomShim* rand = CRandomShim::NewL();
|
|
101 |
CleanupStack::PushL(rand);
|
|
102 |
rand->GenerateBytesL(aDest);
|
|
103 |
CleanupStack::PopAndDestroy(rand); // Use a singleton, avoid new overhead?
|
|
104 |
}
|
|
105 |
|