1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/types.h>
#include <osl/thread.h>
#include <osl/thread.hxx>
#include <osl/time.h>
#include <rtl/alloc.h>
#include <rtl/digest.h>
#include <rtl/random.h>
/*========================================================================
*
* rtlRandom internals.
*
*======================================================================*/
#define RTL_RANDOM_RNG_1(a) ((a) * 16807L)
#define RTL_RANDOM_RNG_2(a) ((a) * 65539L)
#define RTL_RANDOM_RNG(x, y, z) \
{ \
(x) = 170 * ((x) % 178) - 63 * ((x) / 178); \
if ((x) < 0) (x) += 30328L; \
\
(y) = 171 * ((y) % 177) - 2 * ((y) / 177); \
if ((y) < 0) (y) += 30269L; \
\
(z) = 172 * ((z) % 176) - 35 * ((z) / 176); \
if ((z) < 0) (z) += 30307L; \
}
/** RandomData_Impl.
*/
struct RandomData_Impl
{
sal_Int16 m_nX;
sal_Int16 m_nY;
sal_Int16 m_nZ;
};
/** __rtl_random_data.
*/
static double __rtl_random_data (RandomData_Impl *pImpl);
/** RandomPool_Impl.
*/
#define RTL_RANDOM_DIGEST rtl_Digest_AlgorithmMD5
#define RTL_RANDOM_SIZE_DIGEST RTL_DIGEST_LENGTH_MD5
#define RTL_RANDOM_SIZE_POOL 1023
struct RandomPool_Impl
{
rtlDigest m_hDigest;
sal_uInt8 m_pDigest[RTL_RANDOM_SIZE_DIGEST];
sal_uInt8 m_pData[RTL_RANDOM_SIZE_POOL + 1];
sal_uInt32 m_nData;
sal_uInt32 m_nIndex;
sal_uInt32 m_nCount;
};
/** __rtl_random_initPool.
*/
static bool __rtl_random_initPool (
RandomPool_Impl *pImpl);
/** __rtl_random_seedPool.
*/
static void __rtl_random_seedPool (
RandomPool_Impl *pImpl, const sal_uInt8 *pBuffer, sal_Size nBufLen);
/** __rtl_random_readPool.
*/
static void __rtl_random_readPool (
RandomPool_Impl *pImpl, sal_uInt8 *pBuffer, sal_Size nBufLen);
/*
* __rtl_random_data.
*/
static double __rtl_random_data (RandomData_Impl *pImpl)
{
double random;
RTL_RANDOM_RNG (pImpl->m_nX, pImpl->m_nY, pImpl->m_nZ);
random = (((double)(pImpl->m_nX) / 30328.0) +
((double)(pImpl->m_nY) / 30269.0) +
((double)(pImpl->m_nZ) / 30307.0) );
random -= ((double)((sal_uInt32)(random)));
return (random);
}
/*
* __rtl_random_initPool.
*/
static bool __rtl_random_initPool (RandomPool_Impl *pImpl)
{
pImpl->m_hDigest = rtl_digest_create (RTL_RANDOM_DIGEST);
if (pImpl->m_hDigest)
{
oslThreadIdentifier tid;
TimeValue tv;
RandomData_Impl rd;
double seed;
/* The use of uninitialized stack variables as a way to
* enhance the entropy of the random pool triggers
* memory checkers like purify and valgrind.
*/
/*
__rtl_random_seedPool (pImpl, (sal_uInt8*)&tid, sizeof(tid));
__rtl_random_seedPool (pImpl, (sal_uInt8*)&tv, sizeof(tv));
__rtl_random_seedPool (pImpl, (sal_uInt8*)&rd, sizeof(rd));
*/
tid = osl::Thread::getCurrentIdentifier();
tid = RTL_RANDOM_RNG_2(RTL_RANDOM_RNG_1(tid));
__rtl_random_seedPool (pImpl, (sal_uInt8*)&tid, sizeof(tid));
osl_getSystemTime (&tv);
tv.Seconds = RTL_RANDOM_RNG_2(tv.Seconds);
tv.Nanosec = RTL_RANDOM_RNG_2(tv.Nanosec);
__rtl_random_seedPool (pImpl, (sal_uInt8*)&tv, sizeof(tv));
rd.m_nX = (sal_Int16)(((tid >> 1) << 1) + 1);
rd.m_nY = (sal_Int16)(((tv.Seconds >> 1) << 1) + 1);
rd.m_nZ = (sal_Int16)(((tv.Nanosec >> 1) << 1) + 1);
__rtl_random_seedPool (pImpl, (sal_uInt8*)&rd, sizeof(rd));
while (pImpl->m_nData < RTL_RANDOM_SIZE_POOL)
{
seed = __rtl_random_data (&rd);
__rtl_random_seedPool (pImpl, (sal_uInt8*)&seed, sizeof(seed));
}
return true;
}
return false;
}
/*
* __rtl_random_seedPool.
*/
static void __rtl_random_seedPool (
RandomPool_Impl *pImpl, const sal_uInt8 *pBuffer, sal_Size nBufLen)
{
sal_Size i;
sal_sSize j, k;
for (i = 0; i < nBufLen; i += RTL_RANDOM_SIZE_DIGEST)
{
j = nBufLen - i;
if (j > RTL_RANDOM_SIZE_DIGEST)
j = RTL_RANDOM_SIZE_DIGEST;
rtl_digest_update (
pImpl->m_hDigest, pImpl->m_pDigest, RTL_RANDOM_SIZE_DIGEST);
k = (pImpl->m_nIndex + j) - RTL_RANDOM_SIZE_POOL;
if (k > 0)
{
rtl_digest_update (
pImpl->m_hDigest, &(pImpl->m_pData[pImpl->m_nIndex]), j - k);
rtl_digest_update (
pImpl->m_hDigest, &(pImpl->m_pData[0]), k);
}
else
{
rtl_digest_update (
pImpl->m_hDigest, &(pImpl->m_pData[pImpl->m_nIndex]), j);
}
rtl_digest_update (pImpl->m_hDigest, pBuffer, j);
pBuffer += j;
rtl_digest_get (
pImpl->m_hDigest, pImpl->m_pDigest, RTL_RANDOM_SIZE_DIGEST);
for (k = 0; k < j; k++)
{
pImpl->m_pData[pImpl->m_nIndex++] ^= pImpl->m_pDigest[k];
if (pImpl->m_nIndex >= RTL_RANDOM_SIZE_POOL)
{
pImpl->m_nData = RTL_RANDOM_SIZE_POOL;
pImpl->m_nIndex = 0;
}
}
}
if (pImpl->m_nIndex > pImpl->m_nData)
pImpl->m_nData = pImpl->m_nIndex;
}
/*
* __rtl_random_readPool.
*/
static void __rtl_random_readPool (
RandomPool_Impl *pImpl, sal_uInt8 *pBuffer, sal_Size nBufLen)
{
sal_Int32 j, k;
while (nBufLen > 0)
{
j = nBufLen;
if (j > RTL_RANDOM_SIZE_DIGEST/2)
j = RTL_RANDOM_SIZE_DIGEST/2;
nBufLen -= j;
rtl_digest_update (
pImpl->m_hDigest,
&(pImpl->m_pDigest[RTL_RANDOM_SIZE_DIGEST/2]),
RTL_RANDOM_SIZE_DIGEST/2);
k = (pImpl->m_nIndex + j) - pImpl->m_nData;
if (k > 0)
{
rtl_digest_update (
pImpl->m_hDigest, &(pImpl->m_pData[pImpl->m_nIndex]), j - k);
rtl_digest_update (
pImpl->m_hDigest, &(pImpl->m_pData[0]), k);
}
else
{
rtl_digest_update (
pImpl->m_hDigest, &(pImpl->m_pData[pImpl->m_nIndex]), j);
}
rtl_digest_get (
pImpl->m_hDigest, pImpl->m_pDigest, RTL_RANDOM_SIZE_DIGEST);
for (k = 0; k < j; k++)
{
if (pImpl->m_nIndex >= pImpl->m_nData) pImpl->m_nIndex = 0;
pImpl->m_pData[pImpl->m_nIndex++] ^= pImpl->m_pDigest[k];
*pBuffer++ = pImpl->m_pDigest[k + RTL_RANDOM_SIZE_DIGEST/2];
}
}
pImpl->m_nCount++;
rtl_digest_update (
pImpl->m_hDigest, &(pImpl->m_nCount), sizeof(pImpl->m_nCount));
rtl_digest_update (
pImpl->m_hDigest, pImpl->m_pDigest, RTL_RANDOM_SIZE_DIGEST);
rtl_digest_get (
pImpl->m_hDigest, pImpl->m_pDigest, RTL_RANDOM_SIZE_DIGEST);
}
/*========================================================================
*
* rtlRandom implementation.
*
*======================================================================*/
/*
* rtl_random_createPool.
*/
rtlRandomPool SAL_CALL rtl_random_createPool() SAL_THROW_EXTERN_C()
{
RandomPool_Impl *pImpl = (RandomPool_Impl*)NULL;
pImpl = (RandomPool_Impl*)rtl_allocateZeroMemory (sizeof(RandomPool_Impl));
if (pImpl)
{
if (!__rtl_random_initPool (pImpl))
{
rtl_freeZeroMemory (pImpl, sizeof(RandomPool_Impl));
pImpl = (RandomPool_Impl*)NULL;
}
}
return ((rtlRandomPool)pImpl);
}
/*
* rtl_random_destroyPool.
*/
void SAL_CALL rtl_random_destroyPool (rtlRandomPool Pool) SAL_THROW_EXTERN_C()
{
RandomPool_Impl *pImpl = (RandomPool_Impl *)Pool;
if (pImpl)
{
rtl_digest_destroy (pImpl->m_hDigest);
rtl_freeZeroMemory (pImpl, sizeof (RandomPool_Impl));
}
}
/*
* rtl_random_addBytes.
*/
rtlRandomError SAL_CALL rtl_random_addBytes (
rtlRandomPool Pool, const void *Buffer, sal_Size Bytes) SAL_THROW_EXTERN_C()
{
RandomPool_Impl *pImpl = (RandomPool_Impl *)Pool;
const sal_uInt8 *pBuffer = (const sal_uInt8 *)Buffer;
if ((pImpl == NULL) || (pBuffer == NULL))
return rtl_Random_E_Argument;
__rtl_random_seedPool (pImpl, pBuffer, Bytes);
return rtl_Random_E_None;
}
/*
* rtl_random_getBytes.
*/
rtlRandomError SAL_CALL rtl_random_getBytes (
rtlRandomPool Pool, void *Buffer, sal_Size Bytes) SAL_THROW_EXTERN_C()
{
RandomPool_Impl *pImpl = (RandomPool_Impl *)Pool;
sal_uInt8 *pBuffer = (sal_uInt8 *)Buffer;
if ((pImpl == NULL) || (pBuffer == NULL))
return rtl_Random_E_Argument;
__rtl_random_readPool (pImpl, pBuffer, Bytes);
return rtl_Random_E_None;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|