author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Sat, 20 Feb 2010 00:36:18 +0200 | |
branch | RCL_3 |
changeset 43 | 9b5a3a9fddf8 |
parent 17 | cd501b96611d |
child 49 | 2f10d260163b |
permissions | -rw-r--r-- |
17 | 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> |
|
43
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
31 |
#include "securityerr.h" |
17 | 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) |
|
43
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
56 |
{ |
17 | 57 |
iRandomImpl->GenerateRandomBytesL(aDest); |
58 |
} |
|
59 |
||
60 |
CRandomShim::CRandomShim() |
|
61 |
{ |
|
62 |
} |
|
63 |
||
64 |
CRandomShim::~CRandomShim() |
|
65 |
{ |
|
66 |
delete iRandomImpl; |
|
67 |
} |
|
43
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
68 |
|
17 | 69 |
void CRandomShim::ConstructL() |
70 |
{ |
|
43
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
71 |
CRandomFactory::CreateRandomL(iRandomImpl, KRandomUid, NULL); |
17 | 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; |
|
43
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
90 |
if ((ret2 != KErrNone) && (ret2 != KErrNotSecure)) |
17 | 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); |
|
43
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
102 |
|
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
103 |
TRAPD(error, rand->GenerateBytesL(aDest)); |
17 | 104 |
CleanupStack::PopAndDestroy(rand); // Use a singleton, avoid new overhead? |
43
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
105 |
|
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
106 |
// This method should leave on low memory conditions. |
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
107 |
if(error == KErrNoMemory) |
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
108 |
{ |
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
109 |
User::Leave(error); |
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
110 |
} |
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
111 |
} |
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
112 |
|
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
113 |
void TRandomShim::SecureRandomL(TDes8& aDest) |
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
114 |
{ |
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
115 |
CRandomShim* rand = CRandomShim::NewLC(); |
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
116 |
|
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
117 |
rand->GenerateBytesL(aDest); |
9b5a3a9fddf8
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
17
diff
changeset
|
118 |
CleanupStack::PopAndDestroy(rand); |
17 | 119 |
} |