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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/randomgen.h"
#include "gate/results.h"
#if defined(GATE_SYS_WIN16)
# define GATE_CORE_RANDOM_GATE_IMPL 1
#elif defined(GATE_SYS_WIN)
# define GATE_CORE_RANDOM_WINAPI_IMPL 1
#elif defined(GATE_SYS_POSIX)
# define GATE_CORE_RANDOM_POSIX_IMPL 1
#else
# define GATE_CORE_RANDOM_GATE_IMPL 1
#endif
#if defined(GATE_CORE_RANDOM_WINAPI_IMPL)
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
#endif
#include "gate/platforms.h"
#include "gate/debugging.h"
#include <wincrypt.h>
#if defined(GATE_SYS_WINSTORE)
# include <bcrypt.h>
# pragma comment(lib, "bcrypt.lib")
#endif
#if defined(GATE_WIN32_ANSI)
# include <stdlib.h>
#endif
typedef LONG(WINAPI* BCryptGenRandomProc)(PVOID hAlgorithm, PUCHAR pbBuffer, ULONG cbBuffer, ULONG dwFlags);
static BCryptGenRandomProc BCryptGenRandomImpl = NULL;
#ifndef STATUS_SUCCESS
# define STATUS_SUCCESS ((LONG)0x00000000L)
#endif
#ifndef BCRYPT_RNG_USE_ENTROPY_IN_BUFFER
# define BCRYPT_RNG_USE_ENTROPY_IN_BUFFER 0x00000001
#endif
#ifndef BCRYPT_USE_SYSTEM_PREFERRED_RNG
# define BCRYPT_USE_SYSTEM_PREFERRED_RNG 0x00000002
#endif
static gate_bool_t load_bcrypt()
{
static HMODULE hmod = NULL;
LONG ntstatus;
UCHAR test_buffer[sizeof(gate_uint32_t)];
if (!BCryptGenRandomImpl)
{
#if defined(GATE_SYS_WINSTORE)
BCryptGenRandomImpl = &BCryptGenRandom;
#else
if (!hmod)
{
hmod = gate_win32_load_library(_T("bcrypt.dll"), 0);
}
if (!hmod)
{
return false;
}
if (!gate_win32_get_proc_address(hmod, "BCryptGenRandom", &BCryptGenRandomImpl))
{
return false;
}
#endif
}
ntstatus = BCryptGenRandomImpl(NULL, test_buffer, sizeof(test_buffer), BCRYPT_USE_SYSTEM_PREFERRED_RNG);
if (ntstatus != STATUS_SUCCESS)
{
return false;
}
return true;
}
#ifndef CRYPT_SILENT
#define CRYPT_SILENT 0x0040
#endif
gate_result_t gate_randomgenerator_create(gate_randomsession_t* session)
{
gate_result_t ret = GATE_RESULT_FAILED;
HCRYPTPROV provider = 0;
DWORD error_code;
do
{
if (load_bcrypt())
{
*session = &BCryptGenRandomImpl;
ret = GATE_RESULT_OK;
break;
}
#if defined(GATE_SYS_WINCE)
if (!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
{
error_code = GetLastError();
GATE_DEBUG_TRACE_MSG_VALUE("CryptAcquireContext() failed", error_code);
ret = GATE_RESULT_FAILED;
break;
}
#else
if (!gate_platform.AdvCryptAcquireContext)
{
ret = GATE_RESULT_NOTSUPPORTED;
# if defined(GATE_WIN32_ANSI)
/* WinNT 3 does not support Crypt API
use CRT fallback */
srand(GetTickCount());
*session = NULL;
ret = GATE_RESULT_OK;
# endif
break;
}
if (!gate_platform.AdvCryptAcquireContext(&provider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
{
/* Win9x has no SILENT flag */
if (!gate_platform.AdvCryptAcquireContext(&provider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
{
error_code = GetLastError();
GATE_DEBUG_TRACE_MSG_VALUE("CryptAcquireContext() failed", error_code);
ret = GATE_RESULT_FAILED;
break;
}
}
#endif
* session = (gate_randomsession_t)(gate_uintptr_t)provider;
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
union gate_randomgen_number
{
gate_uint64_t num;
BYTE buffer[sizeof(gate_uint64_t)];
BYTE dummy[32];
};
gate_result_t gate_randomgenerator_get_num(gate_randomsession_t* session, gate_uint64_t* number)
{
gate_result_t ret = GATE_RESULT_FAILED;
union gate_randomgen_number tmp;
ret = gate_randomgenerator_get_buffer(session, &tmp.buffer[0], sizeof(tmp.buffer));
if (GATE_SUCCEEDED(ret))
{
*number = tmp.num;
}
return ret;
}
gate_result_t gate_randomgenerator_get_buffer(gate_randomsession_t* session, void* buffer, gate_size_t bufferlen)
{
gate_result_t ret = GATE_RESULT_FAILED;
HCRYPTPROV hProv = (HCRYPTPROV)(gate_uintptr_t)*session;
LONG ntstatus;
BYTE* byte_buffer = (BYTE*)buffer;
do
{
if (!session)
{
ret = GATE_RESULT_INVALIDARG;
break;
}
if ((*session == &BCryptGenRandomImpl) && (BCryptGenRandomImpl != NULL))
{
ntstatus = BCryptGenRandomImpl(NULL, byte_buffer, (ULONG)bufferlen, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
if (STATUS_SUCCESS == ntstatus)
{
ret = GATE_RESULT_OK;
}
else
{
ret = GATE_RESULT_FAILED;
}
break;
}
#if defined(GATE_SYS_WINCE)
if (!CryptGenRandom(hProv, (DWORD)bufferlen, byte_buffer))
{
ret = GATE_RESULT_FAILED;
break;
}
#else
if (!gate_platform.AdvCryptGenRandom)
{
ret = GATE_RESULT_NOTSUPPORTED;
# if defined(GATE_WIN32_ANSI)
/* CRT fallback: */
while (bufferlen-- > 0)
{
*byte_buffer = (BYTE)(rand() % 256);
++byte_buffer;
}
ret = GATE_RESULT_OK;
# endif
break;
}
if (!gate_platform.AdvCryptGenRandom(hProv, (DWORD)bufferlen, byte_buffer))
{
ret = GATE_RESULT_FAILED;
break;
}
#endif
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
gate_result_t gate_randomgenerator_destroy(gate_randomsession_t* session)
{
gate_result_t ret = GATE_RESULT_FAILED;
HCRYPTPROV hProv;
do
{
if (!session)
{
ret = GATE_RESULT_INVALIDARG;
break;
}
if (*session == &BCryptGenRandomImpl)
{
/* nothing to release */
ret = GATE_RESULT_OK;
break;
}
hProv = (HCRYPTPROV)(gate_uintptr_t)*session;
if (hProv)
{
#if defined(GATE_SYS_WINCE)
if (!CryptReleaseContext(hProv, 0))
{
ret = GATE_RESULT_FAILED;
}
#else
if (!gate_platform.AdvCryptReleaseContext)
{
ret = GATE_RESULT_NOTSUPPORTED;
break;
}
if (!gate_platform.AdvCryptReleaseContext(hProv, 0))
{
ret = GATE_RESULT_FAILED;<--- Variable 'ret' is reassigned a value before the old one has been used.
}
#endif
}
*session = NULL;
ret = GATE_RESULT_OK;<--- Variable 'ret' is reassigned a value before the old one has been used.
} while (0);
return ret;
}
#endif /* GATE_CORE_RANDOM_WINAPI_IMPL */
#if defined(GATE_CORE_RANDOM_POSIX_IMPL)
#include "gate/platforms.h"
#include <fcntl.h>
gate_result_t gate_randomgenerator_create(gate_randomsession_t* session)
{
int file = open("/dev/random", O_RDONLY, 0);
if (file == -1)
{
return GATE_RESULT_FAILED;
}
else
{
*session = (gate_randomsession_t)(gate_intptr_t)file;
return GATE_RESULT_OK;
}
}
union gate_randomgen_number
{
gate_uint64_t num;
char buffer[sizeof(gate_uint64_t)];
};
gate_result_t gate_randomgenerator_get_num(gate_randomsession_t* session, gate_uint64_t* number)
{
int file = (int)(gate_intptr_t)*session;<--- Assigning a pointer to an integer is not portable. [+]Assigning a pointer to an integer (int/long/etc) is not portable across different platforms and compilers. For example in 32-bit Windows and linux they are same width, but in 64-bit Windows and linux they are of different width. In worst case you end up assigning 64-bit address to 32-bit integer. The safe way is to store addresses only in pointer types (or typedefs like uintptr_t).
union gate_randomgen_number tmp;
gate_size_t count = 0;
ssize_t readresult;<--- The scope of the variable 'readresult' can be reduced. [+]The scope of the variable 'readresult' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
while (count < sizeof(tmp.buffer))
{
readresult = read(file, &tmp.buffer[count], sizeof(tmp.buffer) - count);
if (readresult == -1)
{
return GATE_RESULT_FAILED;
}
count += (gate_size_t)readresult;
}
*number = tmp.num;
return GATE_RESULT_OK;
}
gate_result_t gate_randomgenerator_get_buffer(gate_randomsession_t* session, void* buffer, gate_size_t bufferlen)
{
int file = (int)(gate_intptr_t)*session;<--- Assigning a pointer to an integer is not portable. [+]Assigning a pointer to an integer (int/long/etc) is not portable across different platforms and compilers. For example in 32-bit Windows and linux they are same width, but in 64-bit Windows and linux they are of different width. In worst case you end up assigning 64-bit address to 32-bit integer. The safe way is to store addresses only in pointer types (or typedefs like uintptr_t).
char* ptr = (char*)buffer;
gate_size_t count = 0;
ssize_t readresult;<--- The scope of the variable 'readresult' can be reduced. [+]The scope of the variable 'readresult' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
while (count < bufferlen)
{
readresult = read(file, &ptr[count], bufferlen - count);
if (readresult == -1)
{
return GATE_RESULT_FAILED;
}
count += (gate_size_t)readresult;
}
return GATE_RESULT_OK;
}
gate_result_t gate_randomgenerator_destroy(gate_randomsession_t* session)
{
int file = (int)(gate_intptr_t)*session;<--- Assigning a pointer to an integer is not portable. [+]Assigning a pointer to an integer (int/long/etc) is not portable across different platforms and compilers. For example in 32-bit Windows and linux they are same width, but in 64-bit Windows and linux they are of different width. In worst case you end up assigning 64-bit address to 32-bit integer. The safe way is to store addresses only in pointer types (or typedefs like uintptr_t).
if (-1 == close(file))
{
return GATE_RESULT_FAILED;
}
*session = NULL;
return GATE_RESULT_OK;
}
#endif /* GATE_CORE_RANDOM_POSIX_IMPL */
#if defined(GATE_CORE_RANDOM_GATE_IMPL)
#include "gate/memalloc.h"
#include "gate/times.h"
/* GATE uses KISS from George Marsaglia's implementation */
/* http://www.cse.yorku.ca/~oz/marsaglia-rng.html */
typedef unsigned long rnd_num_t;
typedef struct
{
rnd_num_t z;
rnd_num_t w;
rnd_num_t jsr;
rnd_num_t jcong;
rnd_num_t a;
rnd_num_t b;
rnd_num_t t[256];
rnd_num_t x;
rnd_num_t y;
rnd_num_t bro;
unsigned char c;
} gate_randomgen_impl_t;
static gate_randomgen_impl_t* get_global_random_impl()
{
#if defined(GATE_SYS_DOS)
static gate_randomgen_impl_t near global_random_impl;
#else
static gate_randomgen_impl_t global_random_impl;
#endif
return &global_random_impl;
}
static rnd_num_t random_znew(gate_randomgen_impl_t* impl)
{
return (impl->z = 36969 * (impl->z & 65535) + (impl->z >> 16));
}
static rnd_num_t random_wnew(gate_randomgen_impl_t* impl)
{
return (impl->w = 18000 * (impl->w & 65535) + (impl->w >> 16));
}
static rnd_num_t random_MWC(gate_randomgen_impl_t* impl)
{
return ((random_znew(impl) << 16) + random_wnew(impl));
}
static rnd_num_t random_CONG(gate_randomgen_impl_t* impl)
{
return (impl->jcong = 69069 * impl->jcong + 1234567);
}
static rnd_num_t random_SHR3(gate_randomgen_impl_t* impl)
{
return (impl->jsr ^= (impl->jsr << 17), impl->jsr ^= (impl->jsr >> 13), impl->jsr ^= (impl->jsr << 5));
}
static rnd_num_t random_KISS(gate_randomgen_impl_t* impl)
{
return ((random_MWC(impl) ^ random_CONG(impl)) + random_SHR3(impl));
}
static rnd_num_t random_LFIB4(gate_randomgen_impl_t* impl)
{
return (impl->c++,
impl->t[impl->c] = impl->t[impl->c]
+ impl->t[(unsigned char)(impl->c + 58)]
+ impl->t[(unsigned char)(impl->c + 119)]
+ impl->t[(unsigned char)(impl->c + 178)]
);
}
static void random_init(gate_randomgen_impl_t* impl, unsigned long seed)
{
unsigned int i;
impl->z = 12345 + ((seed) | 0xff);
impl->w = 65435 + ((seed >> 4) | 0xff);
impl->jsr = 34221 + ((seed >> 8) | 0xff);
impl->jcong = 12345 + ((seed >> 12) | 0xff);
impl->a = 9983651 + ((seed >> 16) | 0xff);
impl->b = 95746118 + ((seed >> 20) | 0xff);
impl->x = 0;
impl->y = 0;
impl->bro = 0;
impl->c = 0;
for (i = 0; i != sizeof(impl->t) / sizeof(impl->t[0]); ++i)
{
impl->t[i] = random_KISS(impl);
}
}
gate_result_t gate_randomgenerator_create(gate_randomsession_t* session)
{
gate_result_t ret = GATE_RESULT_OK;
gate_randomgen_impl_t* ptr_impl;
gate_time_t tm = GATE_INIT_EMPTY;
if (session)
{
ptr_impl = (gate_randomgen_impl_t*)gate_mem_alloc(sizeof(gate_randomgen_impl_t));
if (ptr_impl == NULL)
{
ptr_impl = get_global_random_impl();
}
*session = (void*)ptr_impl;
}
else
{
ptr_impl = get_global_random_impl();
}
gate_time_now(&tm);
random_init(ptr_impl, (unsigned long)(tm.timestamp & 0xffffffff));
return ret;
}
gate_result_t gate_randomgenerator_get_num(gate_randomsession_t* session, gate_uint64_t* number)
{
gate_result_t ret = GATE_RESULT_OK;
gate_randomgen_impl_t* ptr_impl;
gate_uint64_t num[4];
if (session)
{
ptr_impl = (gate_randomgen_impl_t*)*session;
}
else
{
ptr_impl = get_global_random_impl();
}
num[0] = random_LFIB4(ptr_impl);
num[1] = random_LFIB4(ptr_impl);
num[2] = random_LFIB4(ptr_impl);
num[3] = random_LFIB4(ptr_impl);
if (number)
{
*number = (num[0] & 0xffff)
| ((num[1] & 0xffff) << 16)
| ((num[2] & 0xffff) << 32)
| ((num[3] & 0xffff) << 48)
;
}
return ret;
}
gate_result_t gate_randomgenerator_get_buffer(gate_randomsession_t* session, void* buffer, gate_size_t bufferlen)
{
gate_result_t ret = GATE_RESULT_OK;
gate_randomgen_impl_t* ptr_impl;
unsigned char* ptr_buffer = (unsigned char*)buffer;
if (session)
{
ptr_impl = (gate_randomgen_impl_t*)*session;
}
else
{
ptr_impl = get_global_random_impl();
}
while (bufferlen-- != 0)
{
*ptr_buffer = (unsigned char)((random_LFIB4(ptr_impl) >> 4) & 0xff);
++ptr_buffer;
}
return ret;
}
gate_result_t gate_randomgenerator_destroy(gate_randomsession_t* session)
{
gate_randomgen_impl_t* ptr_impl;<--- The scope of the variable 'ptr_impl' can be reduced. [+]The scope of the variable 'ptr_impl' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
if (session)
{
ptr_impl = (gate_randomgen_impl_t*)*session;
if (ptr_impl && (ptr_impl != get_global_random_impl()))
{
gate_mem_dealloc(ptr_impl);
}
}
return GATE_RESULT_OK;
}
#endif /* GATE_CORE_RANDOM_GATE_IMPL */
|