Commit c8cc3657e71646191580c78dc7a710f88d83603d

Daniel Mendler 2019-05-11T09:09:12

mov s_mp_rand_source_platform to separate file

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
diff --git a/bn_mp_prime_rand.c b/bn_mp_prime_rand.c
index e4bc1a4..cd520ff 100644
--- a/bn_mp_prime_rand.c
+++ b/bn_mp_prime_rand.c
@@ -118,13 +118,13 @@ error:
    return err;
 }
 
-static int s_rand_cb(unsigned char *dst, int len, void *dat)
+static int s_mp_rand_cb(unsigned char *dst, int len, void *dat)
 {
    (void)dat;
    if (len <= 0) {
       return len;
    }
-   if (s_rand_source(dst, (size_t)len) != MP_OKAY) {
+   if (s_mp_rand_source(dst, (size_t)len) != MP_OKAY) {
       return 0;
    }
    return len;
@@ -137,7 +137,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_mp_prime_c
 
 int mp_prime_rand(mp_int *a, int t, int size, int flags)
 {
-   return s_mp_prime_random_ex(a, t, size, flags, s_rand_cb, NULL);
+   return s_mp_prime_random_ex(a, t, size, flags, s_mp_rand_cb, NULL);
 }
 
 #endif
diff --git a/bn_mp_rand.c b/bn_mp_rand.c
index c0fc2cd..29bca68 100644
--- a/bn_mp_rand.c
+++ b/bn_mp_rand.c
@@ -3,175 +3,17 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-/* First the OS-specific special cases
- * - *BSD
- * - Windows
- */
-#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
-#  define MP_ARC4RANDOM
-#endif
-
-#if defined(_WIN32) || defined(_WIN32_WCE)
-#define MP_WIN_CSP
-
-#ifndef _WIN32_WINNT
-#define _WIN32_WINNT 0x0400
-#endif
-#ifdef _WIN32_WCE
-#define UNDER_CE
-#define ARM
-#endif
-
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-#include <wincrypt.h>
-
-static HCRYPTPROV hProv = 0;
-
-static void s_cleanup_win_csp(void)
-{
-   CryptReleaseContext(hProv, 0);
-   hProv = 0;
-}
-
-static int s_read_win_csp(void *p, size_t n)
-{
-   if (hProv == 0) {
-      if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
-                               (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) &&
-          !CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
-                               CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET)) {
-         hProv = 0;
-         return MP_ERR;
-      }
-      atexit(s_cleanup_win_csp);
-   }
-   return CryptGenRandom(hProv, (DWORD)n, (BYTE *)p) == TRUE ? MP_OKAY : MP_ERR;
-}
-#endif /* WIN32 */
-
-#if !defined(MP_WIN_CSP) && defined(__linux__) && defined(__GLIBC_PREREQ)
-#if __GLIBC_PREREQ(2, 25)
-#define MP_GETRANDOM
-#include <sys/random.h>
-#include <errno.h>
-
-static int s_read_getrandom(void *p, size_t n)
-{
-   char *q = (char *)p;
-   while (n > 0) {
-      ssize_t ret = getrandom(q, n, 0);
-      if (ret < 0) {
-         if (errno == EINTR) {
-            continue;
-         }
-         return MP_ERR;
-      }
-      q += ret;
-      n -= (size_t)ret;
-   }
-   return MP_OKAY;
-}
-#endif
-#endif
-
-/* We assume all platforms besides windows provide "/dev/urandom".
- * In case yours doesn't, define MP_NO_DEV_URANDOM at compile-time.
- */
-#if !defined(MP_WIN_CSP) && !defined(MP_NO_DEV_URANDOM)
-#ifndef MP_DEV_URANDOM
-#define MP_DEV_URANDOM "/dev/urandom"
-#endif
-#include <fcntl.h>
-#include <errno.h>
-#include <unistd.h>
-
-static int s_read_dev_urandom(void *p, size_t n)
-{
-   int fd;
-   char *q = (char *)p;
-
-   do {
-      fd = open(MP_DEV_URANDOM, O_RDONLY);
-   } while ((fd == -1) && (errno == EINTR));
-   if (fd == -1) return MP_ERR;
-
-   while (n > 0) {
-      ssize_t ret = read(fd, p, n);
-      if (ret < 0) {
-         if (errno == EINTR) {
-            continue;
-         }
-         close(fd);
-         return MP_ERR;
-      }
-      q += ret;
-      n -= (size_t)ret;
-   }
-
-   close(fd);
-   return MP_OKAY;
-}
-#endif
-
-#if defined(MP_PRNG_ENABLE_LTM_RNG)
-unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void));
-void (*ltm_rng_callback)(void);
-
-static int s_read_ltm_rng(void *p, size_t n)
-{
-   unsigned long ret;
-   if (ltm_rng == NULL) return MP_ERR;
-   ret = ltm_rng(p, n, ltm_rng_callback);
-   if (ret != n) return MP_ERR;
-   return MP_OKAY;
-}
-#endif
-
-static int s_mp_rand_source_platform(void *p, size_t n)
-{
-#if defined(MP_ARC4RANDOM)
-   arc4random_buf(p, n);
-   return MP_OKAY;
-#else
-
-   int ret = MP_ERR;
-
-#if defined(MP_WIN_CSP)
-   ret = s_read_win_csp(p, n);
-   if (ret == MP_OKAY) return ret;
-#endif
-
-#if defined(MP_GETRANDOM)
-   ret = s_read_getrandom(p, n);
-   if (ret == MP_OKAY) return ret;
-#endif
-
-#if defined(MP_DEV_URANDOM)
-   ret = s_read_dev_urandom(p, n);
-   if (ret == MP_OKAY) return ret;
-#endif
-
-#if defined(MP_PRNG_ENABLE_LTM_RNG)
-   ret = s_read_ltm_rng(p, n);
-   if (ret == MP_OKAY) return ret;
-#endif
-
-   return ret;
-#endif
-}
-
-int (*s_rand_source)(void *out, size_t size) = s_mp_rand_source_platform;
+int (*s_mp_rand_source)(void *, size_t) = s_mp_rand_source_platform;
 
 void mp_rand_source(int (*source)(void *out, size_t size))
 {
-   s_rand_source = (source == NULL) ? s_mp_rand_source_platform : source;
+   s_mp_rand_source = (source == NULL) ? s_mp_rand_source_platform : source;
 }
 
 /* makes a pseudo-random int of a given size */
 int mp_rand_digit(mp_digit *r)
 {
-   int ret = s_rand_source(r, sizeof(mp_digit));
+   int ret = s_mp_rand_source(r, sizeof(mp_digit));
    *r &= MP_MASK;
    return ret;
 }
@@ -190,13 +32,13 @@ int mp_rand(mp_int *a, int digits)
       return ret;
    }
 
-   if ((ret = s_rand_source(a->dp, (size_t)digits * sizeof(mp_digit))) != MP_OKAY) {
+   if ((ret = s_mp_rand_source(a->dp, (size_t)digits * sizeof(mp_digit))) != MP_OKAY) {
       return ret;
    }
 
    /* TODO: We ensure that the highest digit is nonzero. Should this be removed? */
    while ((a->dp[digits - 1] & MP_MASK) == 0) {
-      if ((ret = s_rand_source(a->dp + digits - 1, sizeof(mp_digit))) != MP_OKAY) {
+      if ((ret = s_mp_rand_source(a->dp + digits - 1, sizeof(mp_digit))) != MP_OKAY) {
          return ret;
       }
    }
diff --git a/bn_s_mp_rand_source_platform.c b/bn_s_mp_rand_source_platform.c
new file mode 100644
index 0000000..7121f14
--- /dev/null
+++ b/bn_s_mp_rand_source_platform.c
@@ -0,0 +1,164 @@
+#include "tommath_private.h"
+#ifdef BN_S_MP_RAND_SOURCE_PLATFORM_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* First the OS-specific special cases
+ * - *BSD
+ * - Windows
+ */
+#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
+#  define MP_ARC4RANDOM
+#endif
+
+#if defined(_WIN32) || defined(_WIN32_WCE)
+#define MP_WIN_CSP
+
+#ifndef _WIN32_WINNT
+#define _WIN32_WINNT 0x0400
+#endif
+#ifdef _WIN32_WCE
+#define UNDER_CE
+#define ARM
+#endif
+
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#include <wincrypt.h>
+
+static HCRYPTPROV hProv = 0;
+
+static void s_cleanup_win_csp(void)
+{
+   CryptReleaseContext(hProv, 0);
+   hProv = 0;
+}
+
+static int s_read_win_csp(void *p, size_t n)
+{
+   if (hProv == 0) {
+      if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
+                               (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) &&
+          !CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
+                               CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET)) {
+         hProv = 0;
+         return MP_ERR;
+      }
+      atexit(s_cleanup_win_csp);
+   }
+   return CryptGenRandom(hProv, (DWORD)n, (BYTE *)p) == TRUE ? MP_OKAY : MP_ERR;
+}
+#endif /* WIN32 */
+
+#if !defined(MP_WIN_CSP) && defined(__linux__) && defined(__GLIBC_PREREQ)
+#if __GLIBC_PREREQ(2, 25)
+#define MP_GETRANDOM
+#include <sys/random.h>
+#include <errno.h>
+
+static int s_read_getrandom(void *p, size_t n)
+{
+   char *q = (char *)p;
+   while (n > 0) {
+      ssize_t ret = getrandom(q, n, 0);
+      if (ret < 0) {
+         if (errno == EINTR) {
+            continue;
+         }
+         return MP_ERR;
+      }
+      q += ret;
+      n -= (size_t)ret;
+   }
+   return MP_OKAY;
+}
+#endif
+#endif
+
+/* We assume all platforms besides windows provide "/dev/urandom".
+ * In case yours doesn't, define MP_NO_DEV_URANDOM at compile-time.
+ */
+#if !defined(MP_WIN_CSP) && !defined(MP_NO_DEV_URANDOM)
+#ifndef MP_DEV_URANDOM
+#define MP_DEV_URANDOM "/dev/urandom"
+#endif
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
+
+static int s_read_dev_urandom(void *p, size_t n)
+{
+   int fd;
+   char *q = (char *)p;
+
+   do {
+      fd = open(MP_DEV_URANDOM, O_RDONLY);
+   } while ((fd == -1) && (errno == EINTR));
+   if (fd == -1) return MP_ERR;
+
+   while (n > 0) {
+      ssize_t ret = read(fd, p, n);
+      if (ret < 0) {
+         if (errno == EINTR) {
+            continue;
+         }
+         close(fd);
+         return MP_ERR;
+      }
+      q += ret;
+      n -= (size_t)ret;
+   }
+
+   close(fd);
+   return MP_OKAY;
+}
+#endif
+
+#if defined(MP_PRNG_ENABLE_LTM_RNG)
+unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void));
+void (*ltm_rng_callback)(void);
+
+static int s_read_ltm_rng(void *p, size_t n)
+{
+   unsigned long ret;
+   if (ltm_rng == NULL) return MP_ERR;
+   ret = ltm_rng(p, n, ltm_rng_callback);
+   if (ret != n) return MP_ERR;
+   return MP_OKAY;
+}
+#endif
+
+int s_mp_rand_source_platform(void *p, size_t n)
+{
+#if defined(MP_ARC4RANDOM)
+   arc4random_buf(p, n);
+   return MP_OKAY;
+#else
+
+   int ret = MP_ERR;
+
+#if defined(MP_WIN_CSP)
+   ret = s_read_win_csp(p, n);
+   if (ret == MP_OKAY) return ret;
+#endif
+
+#if defined(MP_GETRANDOM)
+   ret = s_read_getrandom(p, n);
+   if (ret == MP_OKAY) return ret;
+#endif
+
+#if defined(MP_DEV_URANDOM)
+   ret = s_read_dev_urandom(p, n);
+   if (ret == MP_OKAY) return ret;
+#endif
+
+#if defined(MP_PRNG_ENABLE_LTM_RNG)
+   ret = s_read_ltm_rng(p, n);
+   if (ret == MP_OKAY) return ret;
+#endif
+
+   return ret;
+#endif
+}
+
+#endif
diff --git a/callgraph.txt b/callgraph.txt
index b38072d..ea615f9 100644
--- a/callgraph.txt
+++ b/callgraph.txt
@@ -15360,6 +15360,9 @@ BN_S_MP_MUL_HIGH_DIGS_FAST_C
 +--->BN_MP_CLAMP_C
 
 
+BN_S_MP_RAND_SOURCE_PLATFORM_C
+
+
 BN_S_MP_REVERSE_C
 
 
diff --git a/libtommath_VS2008.vcproj b/libtommath_VS2008.vcproj
index 2ca8525..b630ce8 100644
--- a/libtommath_VS2008.vcproj
+++ b/libtommath_VS2008.vcproj
@@ -873,6 +873,10 @@
 			>
 		</File>
 		<File
+			RelativePath="bn_s_mp_rand_source_platform.c"
+			>
+		</File>
+		<File
 			RelativePath="bn_s_mp_reverse.c"
 			>
 		</File>
diff --git a/makefile b/makefile
index 8c3f992..8b6f9af 100644
--- a/makefile
+++ b/makefile
@@ -52,7 +52,8 @@ bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_ze
 bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_invmod_fast.o \
 bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o \
 bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o \
-bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
+bn_s_mp_rand_source_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o \
+bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
 
 #END_INS
 
diff --git a/makefile.mingw b/makefile.mingw
index 965e877..dd67ff8 100644
--- a/makefile.mingw
+++ b/makefile.mingw
@@ -55,7 +55,8 @@ bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_ze
 bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_invmod_fast.o \
 bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o \
 bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o \
-bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
+bn_s_mp_rand_source_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o \
+bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
 
 HEADERS_PUB=tommath.h tommath_class.h tommath_superclass.h
 
diff --git a/makefile.msvc b/makefile.msvc
index f5975c0..00706e0 100644
--- a/makefile.msvc
+++ b/makefile.msvc
@@ -47,7 +47,8 @@ bn_mp_toradix.obj bn_mp_toradix_n.obj bn_mp_unsigned_bin_size.obj bn_mp_xor.obj 
 bn_s_mp_add.obj bn_s_mp_balance_mul.obj bn_s_mp_exptmod.obj bn_s_mp_exptmod_fast.obj bn_s_mp_invmod_fast.obj \
 bn_s_mp_invmod_slow.obj bn_s_mp_karatsuba_mul.obj bn_s_mp_karatsuba_sqr.obj bn_s_mp_montgomery_reduce_fast.obj \
 bn_s_mp_mul_digs.obj bn_s_mp_mul_digs_fast.obj bn_s_mp_mul_high_digs.obj bn_s_mp_mul_high_digs_fast.obj \
-bn_s_mp_reverse.obj bn_s_mp_sqr.obj bn_s_mp_sqr_fast.obj bn_s_mp_sub.obj bn_s_mp_toom_mul.obj bn_s_mp_toom_sqr.obj
+bn_s_mp_rand_source_platform.obj bn_s_mp_reverse.obj bn_s_mp_sqr.obj bn_s_mp_sqr_fast.obj bn_s_mp_sub.obj \
+bn_s_mp_toom_mul.obj bn_s_mp_toom_sqr.obj
 
 HEADERS_PUB=tommath.h tommath_class.h tommath_superclass.h
 
diff --git a/makefile.shared b/makefile.shared
index 1605f75..7b0cffd 100644
--- a/makefile.shared
+++ b/makefile.shared
@@ -49,7 +49,8 @@ bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_ze
 bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_invmod_fast.o \
 bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o \
 bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o \
-bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
+bn_s_mp_rand_source_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o \
+bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
 
 #END_INS
 
diff --git a/makefile.unix b/makefile.unix
index 2e1a5fe..9aed117 100644
--- a/makefile.unix
+++ b/makefile.unix
@@ -56,7 +56,8 @@ bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_ze
 bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_invmod_fast.o \
 bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o \
 bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o \
-bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
+bn_s_mp_rand_source_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o \
+bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
 
 HEADERS_PUB=tommath.h tommath_class.h tommath_superclass.h
 
diff --git a/tommath_class.h b/tommath_class.h
index 25e5462..7738a3c 100644
--- a/tommath_class.h
+++ b/tommath_class.h
@@ -150,6 +150,7 @@
 #   define BN_S_MP_MUL_DIGS_FAST_C
 #   define BN_S_MP_MUL_HIGH_DIGS_C
 #   define BN_S_MP_MUL_HIGH_DIGS_FAST_C
+#   define BN_S_MP_RAND_SOURCE_PLATFORM_C
 #   define BN_S_MP_REVERSE_C
 #   define BN_S_MP_SQR_C
 #   define BN_S_MP_SQR_FAST_C
@@ -720,6 +721,8 @@
 #   define BN_MP_DIV_2_C
 #   define BN_MP_MUL_2_C
 #   define BN_MP_ADD_D_C
+#   define BN_S_MP_RAND_CB_C
+#   define BN_S_MP_RAND_SOURCE_C
 #   define BN_MP_PRIME_RANDOM_EX_C
 #endif
 
@@ -761,9 +764,9 @@
 #endif
 
 #if defined(BN_MP_RAND_C)
-#   define BN_S_MP_RAND_SOURCE_PLATFORM_C
 #   define BN_MP_RAND_SOURCE_C
 #   define BN_MP_RAND_DIGIT_C
+#   define BN_S_MP_RAND_SOURCE_C
 #   define BN_MP_ZERO_C
 #   define BN_MP_GROW_C
 #endif
@@ -1170,6 +1173,9 @@
 #   define BN_MP_CLAMP_C
 #endif
 
+#if defined(BN_S_MP_RAND_SOURCE_PLATFORM_C)
+#endif
+
 #if defined(BN_S_MP_REVERSE_C)
 #endif
 
diff --git a/tommath_private.h b/tommath_private.h
index 7ef3582..0cf5d61 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -106,7 +106,7 @@ extern void MP_FREE(void *mem, size_t size);
 #define MP_MAXFAST              (int)(1u << (MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT)))
 
 /* random number source */
-extern int (*s_rand_source)(void *out, size_t size);
+extern int (*s_mp_rand_source)(void *out, size_t size);
 
 /* Minimum number of available digits in mp_int, MP_PREC >= MP_MIN_PREC */
 #define MP_MIN_PREC ((CHAR_BIT * (int)sizeof(long long) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)
@@ -130,6 +130,7 @@ MP_WUR int s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c);
 MP_WUR int s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_digit rho);
 MP_WUR int s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode);
 MP_WUR int s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode);
+MP_WUR int s_mp_rand_source_platform(void *p, size_t n);
 void s_mp_reverse(unsigned char *s, int len);
 
 extern const char *const mp_s_rmap;