0
|
1 |
// Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
2 |
// All rights reserved.
|
|
3 |
// This component and the accompanying materials are made available
|
|
4 |
// under the terms of the License "Eclipse Public License v1.0"
|
|
5 |
// which accompanies this distribution, and is available
|
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
// e32\kernel\random.cpp
|
|
15 |
//
|
|
16 |
//
|
|
17 |
|
|
18 |
#include <kernel/kern_priv.h>
|
|
19 |
|
|
20 |
//
|
|
21 |
// Generate nth random number using the following algorithm
|
|
22 |
//
|
|
23 |
// X[n] = ((X[n-j] rotl r1) + (X[n-k] rotl r2)) modulo 2^b
|
|
24 |
//
|
|
25 |
// k=17, j=10, r1=5, r2=3
|
|
26 |
//
|
|
27 |
|
|
28 |
const TInt KBufferSize=17;
|
|
29 |
const TInt KBufferSizeInBits=KBufferSize*32;
|
|
30 |
const TInt KPointer2Offset=10; // Must be less than KBufferSize
|
|
31 |
|
|
32 |
TUint32 RandomBuffer[KBufferSize];
|
|
33 |
TInt RandomPointer=1;
|
|
34 |
TInt RandomSaltPointer=KBufferSizeInBits-1;
|
|
35 |
|
|
36 |
inline TUint32 RotateLeft5(TUint32 aVal)
|
|
37 |
{ return (aVal << 5) | (aVal >> 27); }
|
|
38 |
|
|
39 |
inline TUint32 RotateLeft3(TUint32 aVal)
|
|
40 |
{ return (aVal << 3) | (aVal >> 29); }
|
|
41 |
|
|
42 |
void K::Randomize()
|
|
43 |
//
|
|
44 |
// Initialise the random pool
|
|
45 |
//
|
|
46 |
{
|
|
47 |
TUint64 seed=K::TickQ->iRtc;
|
|
48 |
TInt i;
|
|
49 |
for (i=0; i<KBufferSize; i++)
|
|
50 |
{
|
|
51 |
RandomBuffer[i] = TUint32(seed);
|
|
52 |
seed= ((seed<<5) + (seed>>59)) + 97;
|
|
53 |
}
|
|
54 |
NKern::LockSystem();
|
|
55 |
for (i=0; i<50; i++)
|
|
56 |
Kern::Random();
|
|
57 |
NKern::UnlockSystem();
|
|
58 |
}
|
|
59 |
|
|
60 |
|
|
61 |
/**
|
|
62 |
Generate the next random number.
|
|
63 |
|
|
64 |
@return The generated random number.
|
|
65 |
|
|
66 |
@pre Kernel Lock must not be held.
|
|
67 |
@pre No fast mutex should be held
|
|
68 |
@pre Interrupts should be enabled
|
|
69 |
@pre Can be used in a device driver.
|
|
70 |
*/
|
|
71 |
EXPORT_C TUint32 Kern::Random()
|
|
72 |
{
|
|
73 |
TBool alreadyLocked = TheScheduler.iLock.HeldByCurrentThread();
|
|
74 |
if (!alreadyLocked)
|
|
75 |
NKern::LockSystem();
|
|
76 |
TInt p1 = RandomPointer;
|
|
77 |
if(--p1<0)
|
|
78 |
p1 = KBufferSize-1;
|
|
79 |
RandomPointer = p1;
|
|
80 |
TInt p2 = p1+KPointer2Offset;
|
|
81 |
if(p2>KBufferSize-1)
|
|
82 |
p2 -= KBufferSize-1;
|
|
83 |
TUint32 r = RandomBuffer[p1] = RotateLeft5(RandomBuffer[p2])+RotateLeft3(RandomBuffer[p1]);
|
|
84 |
if (!alreadyLocked)
|
|
85 |
NKern::UnlockSystem();
|
|
86 |
return r;
|
|
87 |
}
|
|
88 |
|
|
89 |
/**
|
|
90 |
Adds a bit to the random pool used to generate random numbers.
|
|
91 |
This method should be used by any sources of entropy to improve the quality of
|
|
92 |
random number generation.
|
|
93 |
|
|
94 |
@param aBitOfSalt The least significant bit of this value is added to the random pool.
|
|
95 |
|
|
96 |
@pre Can be used in a device driver.
|
|
97 |
*/
|
|
98 |
EXPORT_C void Kern::RandomSalt(TUint32 aBitOfSalt)
|
|
99 |
{
|
|
100 |
TInt p=RandomSaltPointer; // protect RandomSaltPointer from re-entrantrancy
|
|
101 |
if (aBitOfSalt&=1)
|
|
102 |
{
|
|
103 |
TInt word=p >> 5;
|
|
104 |
TInt bit=p & 0x1f;
|
|
105 |
RandomBuffer[word] ^= aBitOfSalt << bit;
|
|
106 |
}
|
|
107 |
if (--p<0)
|
|
108 |
p=KBufferSizeInBits-1;
|
|
109 |
RandomSaltPointer=p;
|
|
110 |
}
|
|
111 |
|