Commit 6327a693210ce3bac92f6a066655e4e20a3ee925

Steffen Jaeckel 2019-05-13T10:22:08

Merge pull request #256 from libtom/rand-cleanup Use Jenkins rand in test suite

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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
diff --git a/.travis.yml b/.travis.yml
index bde3d08..a6a097d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -86,7 +86,7 @@ matrix:
     # GCC for the x64_32 architecture (32-bit longs and 32-bit pointers)
     # TODO: Probably not possible to run anything in x32 in Travis
     #       but needs to be checked to be sure.
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --with-mx32'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --with-mx32'
       addons:
         apt:
           packages:
@@ -94,7 +94,7 @@ matrix:
             - gcc-multilib
 
     # GCC for the x86-64 architecture (64-bit longs and 64-bit pointers)
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --with-m64 --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --with-m64 --with-valgrind'
     - env: BUILDOPTIONS='--with-cc=gcc-4.7 --with-m64 --with-valgrind'
       addons:
         apt:
@@ -112,13 +112,13 @@ matrix:
             - gcc-4.9
 
     # clang for x86-64 architecture (64-bit longs and 64-bit pointers)
-    - env: CONV_WARNINGS=1 BUILDOPTIONS='--with-cc=clang-7 --with-m64 --with-valgrind'
-    - env: BUILDOPTIONS='--with-cc=clang-6.0 --with-m64 --with-valgrind'
+    - env: SANITIZER=1 CONV_WARNINGS=1 BUILDOPTIONS='--with-cc=clang-7 --with-m64 --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-6.0 --with-m64 --with-valgrind'
       addons:
         apt:
           packages:
             - clang-6.0
-    - env: BUILDOPTIONS='--with-cc=clang-5.0 --with-m64 --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-5.0 --with-m64 --with-valgrind'
       addons:
         apt:
           packages:
@@ -132,24 +132,24 @@ matrix:
     # GCC for the x86-64 architecture with restricted limb sizes
     # formerly started with the option "--with-low-mp" to testme.sh
     # but testing all three in one run took to long and timed out.
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_8BIT  --with-valgrind'
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_16BIT --with-valgrind'
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_32BIT --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_8BIT  --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_16BIT --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_32BIT --with-valgrind'
 
     # clang for the x86-64 architecture with restricted limb sizes
-    - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_8BIT  --with-valgrind'
-    - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_16BIT --with-valgrind'
-    - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_32BIT --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_8BIT  --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_16BIT --with-valgrind'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_32BIT --with-valgrind'
 
     # Test "autotuning", the automatic evaluation and setting of the Toom-Cook cut-offs.
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_8BIT  --with-valgrind --make-option=tune'
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_16BIT --with-valgrind --make-option=tune'
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_32BIT --with-valgrind --make-option=tune'
-    - env: BUILDOPTIONS='--with-cc=gcc-5 --with-valgrind --make-option=tune'
-    - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_8BIT  --with-valgrind --make-option=tune'
-    - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_16BIT --with-valgrind --make-option=tune'
-    - env: BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_32BIT --with-valgrind --make-option=tune'
-    - env: BUILDOPTIONS='--with-cc=clang-7 --with-valgrind --make-option=tune'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_8BIT  --with-valgrind --make-option=tune'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_16BIT --with-valgrind --make-option=tune'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --cflags=-DMP_32BIT --with-valgrind --make-option=tune'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --with-valgrind --make-option=tune'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_8BIT  --with-valgrind --make-option=tune'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_16BIT --with-valgrind --make-option=tune'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_32BIT --with-valgrind --make-option=tune'
+    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --with-valgrind --make-option=tune'
 
     # GCC for the x86-64 architecture testing against a different Bigint-implementation
     # with 333333 different inputs.
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..db0ff48 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_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_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_jenkins.c b/bn_s_mp_rand_jenkins.c
new file mode 100644
index 0000000..d6946f8
--- /dev/null
+++ b/bn_s_mp_rand_jenkins.c
@@ -0,0 +1,52 @@
+#include "tommath_private.h"
+#ifdef BN_S_MP_RAND_JENKINS_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* Bob Jenkins' http://burtleburtle.net/bob/rand/smallprng.html */
+/* Chosen for speed and a good "mix" */
+typedef struct ranctx {
+   uint64_t a;
+   uint64_t b;
+   uint64_t c;
+   uint64_t d;
+} ranctx;
+
+static ranctx jenkins_x;
+
+#define rot(x,k) (((x)<<(k))|((x)>>(64-(k))))
+static uint64_t s_rand_jenkins_val(void)
+{
+   uint64_t e = jenkins_x.a - rot(jenkins_x.b, 7);
+   jenkins_x.a = jenkins_x.b ^ rot(jenkins_x.c, 13);
+   jenkins_x.b = jenkins_x.c + rot(jenkins_x.d, 37);
+   jenkins_x.c = jenkins_x.d + e;
+   jenkins_x.d = e + jenkins_x.a;
+   return jenkins_x.d;
+}
+
+void s_mp_rand_jenkins_init(uint64_t seed)
+{
+   uint64_t i;
+   jenkins_x.a = 0xf1ea5eed;
+   jenkins_x.b = jenkins_x.c = jenkins_x.d = seed;
+   for (i = 0; i < 20; ++i) {
+      (void)s_rand_jenkins_val();
+   }
+}
+
+int s_mp_rand_jenkins(void *p, size_t n)
+{
+   char *q = (char *)p;
+   while (n > 0) {
+      int i;
+      uint64_t x = s_rand_jenkins_val();
+      for (i = 0; i < 8 && n > 0; ++i, --n) {
+         *q++ = (char)(x & 0xFF);
+         x >>= 8;
+      }
+   }
+   return MP_OKAY;
+}
+
+#endif
diff --git a/bn_s_mp_rand_platform.c b/bn_s_mp_rand_platform.c
new file mode 100644
index 0000000..6e04ed5
--- /dev/null
+++ b/bn_s_mp_rand_platform.c
@@ -0,0 +1,157 @@
+#include "tommath_private.h"
+#ifdef BN_S_MP_RAND_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 int s_read_win_csp(void *p, size_t n)
+{
+   static HCRYPTPROV hProv = 0;
+   if (hProv == 0) {
+      HCRYPTPROV h = 0;
+      if (!CryptAcquireContext(&h, NULL, MS_DEF_PROV, PROV_RSA_FULL,
+                               (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) &&
+          !CryptAcquireContext(&h, NULL, MS_DEF_PROV, PROV_RSA_FULL,
+                               CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET)) {
+         return MP_ERR;
+      }
+      hProc = h;
+   }
+   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_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..be0ec9a 100644
--- a/callgraph.txt
+++ b/callgraph.txt
@@ -15360,6 +15360,12 @@ BN_S_MP_MUL_HIGH_DIGS_FAST_C
 +--->BN_MP_CLAMP_C
 
 
+BN_S_MP_RAND_JENKINS_C
+
+
+BN_S_MP_RAND_PLATFORM_C
+
+
 BN_S_MP_REVERSE_C
 
 
diff --git a/demo/main.c b/demo/main.c
index aa58c20..068e31a 100644
--- a/demo/main.c
+++ b/demo/main.c
@@ -26,8 +26,6 @@ void ndraw(mp_int *a, const char *name)
 
 int main(int argc, char **argv)
 {
-   srand(LTM_DEMO_RAND_SEED);
-
 #ifdef MP_8BIT
    printf("Digit size 8 Bit \n");
 #endif
diff --git a/demo/opponent.c b/demo/opponent.c
index 7f02252..69c1c85 100644
--- a/demo/opponent.c
+++ b/demo/opponent.c
@@ -1,5 +1,11 @@
 #include "shared.h"
 
+#ifdef LTM_MTEST_REAL_RAND
+#define LTM_MTEST_RAND_SEED  time(NULL)
+#else
+#define LTM_MTEST_RAND_SEED  23
+#endif
+
 static void draw(mp_int *a)
 {
    ndraw(a, "");
@@ -21,6 +27,8 @@ int mtest_opponent(void)
    unsigned long expt_n, add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n,
             gcd_n, lcm_n, inv_n, div2_n, mul2_n, add_d_n, sub_d_n;
 
+   srand(LTM_MTEST_RAND_SEED);
+
    if (mp_init_multi(&a, &b, &c, &d, &e, &f, NULL)!= MP_OKAY)
       return EXIT_FAILURE;
 
diff --git a/demo/shared.h b/demo/shared.h
index 006f4cf..b32def7 100644
--- a/demo/shared.h
+++ b/demo/shared.h
@@ -18,12 +18,6 @@
 #define LTM_DEMO_TEST_REDUCE_2K_L 0
 #endif
 
-#ifdef LTM_DEMO_REAL_RAND
-#define LTM_DEMO_RAND_SEED  time(NULL)
-#else
-#define LTM_DEMO_RAND_SEED  23
-#endif
-
 #define MP_WUR /* TODO: result checks disabled for now */
 #include "tommath.h"
 
diff --git a/demo/test.c b/demo/test.c
index 2d0348f..c4314d0 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -1,4 +1,30 @@
 #include "shared.h"
+#include "tommath_private.h"
+
+static long rand_long(void)
+{
+   long x;
+   if (s_mp_rand_source(&x, sizeof(x)) != MP_OKAY) {
+      fprintf(stderr, "s_mp_rand_source failed\n");
+      exit(EXIT_FAILURE);
+   }
+   return x;
+}
+
+static int rand_int(void)
+{
+   int x;
+   if (s_mp_rand_source(&x, sizeof(x)) != MP_OKAY) {
+      fprintf(stderr, "s_mp_rand_source failed\n");
+      exit(EXIT_FAILURE);
+   }
+   return x;
+}
+
+static unsigned long ulabs(long x)
+{
+   return x > 0 ? (unsigned long)x : -(unsigned long)x;
+}
 
 static int test_trivial_stuff(void)
 {
@@ -260,14 +286,14 @@ static int test_mp_complement(void)
    }
 
    for (i = 0; i < 1000; ++i) {
-      long l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
-      mp_set_long(&a, (unsigned long)labs(l));
+      long l = rand_long();
+      mp_set_long(&a, ulabs(l));
       if (l < 0)
          mp_neg(&a, &a);
       mp_complement(&a, &b);
 
       l = ~l;
-      mp_set_long(&c, (unsigned long)labs(l));
+      mp_set_long(&c, ulabs(l));
       if (l < 0)
          mp_neg(&c, &c);
 
@@ -297,14 +323,14 @@ static int test_mp_tc_div_2d(void)
       long l;
       int em;
 
-      l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
-      mp_set_long(&a, (unsigned long)labs(l));
+      l = rand_long();
+      mp_set_long(&a, ulabs(l));
       if (l < 0)
          mp_neg(&a, &a);
 
-      em = rand() % 32;
+      em = abs(rand_int()) % 32;
 
-      mp_set_long(&d, (unsigned long)labs(l >> em));
+      mp_set_long(&d, ulabs(l >> em));
       if ((l >> em) < 0)
          mp_neg(&d, &d);
 
@@ -333,19 +359,19 @@ static int test_mp_tc_xor(void)
    }
 
    for (i = 0; i < 1000; ++i) {
-      int l, em;
+      long l, em;
 
-      l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
-      mp_set_int(&a, (unsigned long)labs(l));
+      l = rand_long();
+      mp_set_int(&a, ulabs(l));
       if (l < 0)
          mp_neg(&a, &a);
 
-      em = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
-      mp_set_int(&b, (unsigned long)labs(em));
+      em = rand_long();
+      mp_set_int(&b, ulabs(em));
       if (em < 0)
          mp_neg(&b, &b);
 
-      mp_set_int(&d, (unsigned long)labs(l ^ em));
+      mp_set_int(&d, ulabs(l ^ em));
       if ((l ^ em) < 0)
          mp_neg(&d, &d);
 
@@ -376,17 +402,17 @@ static int test_mp_tc_or(void)
    for (i = 0; i < 1000; ++i) {
       long l, em;
 
-      l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
-      mp_set_long(&a, (unsigned long)labs(l));
+      l = rand_long();
+      mp_set_long(&a, ulabs(l));
       if (l < 0)
          mp_neg(&a, &a);
 
-      em = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
-      mp_set_long(&b, (unsigned long)labs(em));
+      em = rand_long();
+      mp_set_long(&b, ulabs(em));
       if (em < 0)
          mp_neg(&b, &b);
 
-      mp_set_long(&d, (unsigned long)labs(l | em));
+      mp_set_long(&d, ulabs(l | em));
       if ((l | em) < 0)
          mp_neg(&d, &d);
 
@@ -416,17 +442,17 @@ static int test_mp_tc_and(void)
    for (i = 0; i < 1000; ++i) {
       long l, em;
 
-      l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
-      mp_set_long(&a, (unsigned long)labs(l));
+      l = rand_long();
+      mp_set_long(&a, ulabs(l));
       if (l < 0)
          mp_neg(&a, &a);
 
-      em = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
-      mp_set_long(&b, (unsigned long)labs(em));
+      em = rand_long();
+      mp_set_long(&b, ulabs(em));
       if (em < 0)
          mp_neg(&b, &b);
 
-      mp_set_long(&d, (unsigned long)labs(l & em));
+      mp_set_long(&d, ulabs(l & em));
       if ((l & em) < 0)
          mp_neg(&d, &d);
 
@@ -518,8 +544,8 @@ static int test_mp_set_double(void)
    }
 
    for (i = 0; i < 1000; ++i) {
-      int tmp = rand();
-      double dbl = (double)tmp * rand() + 1;
+      int tmp = rand_int();
+      double dbl = (double)tmp * rand_int() + 1;
       if (mp_set_double(&a, dbl) != MP_OKAY) {
          printf("\nmp_set_double() failed");
          goto LBL_ERR;
@@ -558,7 +584,7 @@ static int test_mp_get_int(void)
    }
 
    for (i = 0; i < 1000; ++i) {
-      t = (unsigned long)(rand() * rand() + 1) & 0xFFFFFFFFuL;
+      t = (unsigned long)rand_long() & 0xFFFFFFFFuL;
       mp_set_int(&a, t);
       if (t != mp_get_int(&a)) {
          printf("\nmp_get_int() bad result!");
@@ -662,7 +688,7 @@ static int test_mp_sqrt(void)
    for (i = 0; i < 1000; ++i) {
       printf("%6d\r", i);
       fflush(stdout);
-      n = (rand() & 15) + 1;
+      n = (rand_int() & 15) + 1;
       mp_rand(&a, n);
       if (mp_sqrt(&a, &b) != MP_OKAY) {
          printf("\nmp_sqrt() error!");
@@ -701,7 +727,7 @@ static int test_mp_is_square(void)
       fflush(stdout);
 
       /* test mp_is_square false negatives */
-      n = (rand() & 7) + 1;
+      n = (rand_int() & 7) + 1;
       mp_rand(&a, n);
       mp_sqr(&a, &a);
       if (mp_is_square(&a, &n) != MP_OKAY) {
@@ -789,7 +815,7 @@ static int test_mp_prime_rand(void)
    for (ix = 10; ix < 128; ix++) {
       printf("Testing (not safe-prime): %9d bits    \r", ix);
       fflush(stdout);
-      err = mp_prime_rand(&a, 8, ix, (rand() & 1) ? 0 : MP_PRIME_2MSB_ON);
+      err = mp_prime_rand(&a, 8, ix, (rand_int() & 1) ? 0 : MP_PRIME_2MSB_ON);
       if (err != MP_OKAY) {
          printf("\nfailed with error: %s\n", mp_error_to_string(err));
          goto LBL_ERR;
@@ -850,7 +876,7 @@ static int test_mp_prime_is_prime(void)
    for (ix = 16; ix < 128; ix++) {
       printf("Testing (    safe-prime): %9d bits    \r", ix);
       fflush(stdout);
-      err = mp_prime_rand(&a, 8, ix, ((rand() & 1) ? 0 : MP_PRIME_2MSB_ON) | MP_PRIME_SAFE);
+      err = mp_prime_rand(&a, 8, ix, ((rand_int() & 1) ? 0 : MP_PRIME_2MSB_ON) | MP_PRIME_SAFE);
       if (err != MP_OKAY) {
          printf("\nfailed with error: %s\n", mp_error_to_string(err));
          goto LBL_ERR;
@@ -941,7 +967,7 @@ static int test_mp_montgomery_reduce(void)
 
          /* now test a random reduction */
          for (ix = 0; ix < 100; ix++) {
-            mp_rand(&c, 1 + abs(rand()) % (2*i));
+            mp_rand(&c, 1 + abs(rand_int()) % (2*i));
             mp_copy(&c, &d);
             mp_copy(&c, &e);
 
@@ -1096,7 +1122,7 @@ static int test_mp_div_3(void)
          printf("%9d\r", cnt);
          fflush(stdout);
       }
-      mp_rand(&a, abs(rand()) % 128 + 1);
+      mp_rand(&a, abs(rand_int()) % 128 + 1);
       mp_div(&a, &d, &b, &e);
       mp_div_3(&a, &c, &r2);
 
@@ -1853,6 +1879,9 @@ int unit_tests(int argc, char **argv)
    unsigned long i;
    int res = EXIT_SUCCESS, j;
 
+   s_mp_rand_jenkins_init((uint64_t)time(NULL));
+   mp_rand_source(s_mp_rand_jenkins);
+
    for (i = 0; i < sizeof(test) / sizeof(test[0]); ++i) {
       if (argc > 1) {
          for (j = 1; j < argc; ++j) {
diff --git a/etc/tune.c b/etc/tune.c
index 399ad3d..06fb0d6 100644
--- a/etc/tune.c
+++ b/etc/tune.c
@@ -10,15 +10,6 @@
 #include <limits.h>
 #include <errno.h>
 
-static uint64_t s_ranval(void);
-static void s_raninit(uint64_t seed);
-static uint64_t s_timer_function(void);
-static void s_timer_start(void);
-static uint64_t s_timer_stop(void);
-static uint64_t s_time_mul(int size);
-static uint64_t s_time_sqr(int size);
-static void s_usage(char *s);
-
 /*
    Please take in mind that both multiplicands are of the same size. The balancing
    mechanism in mp_balance works well but has some overhead itself. You can test
@@ -26,56 +17,12 @@ static void s_usage(char *s);
    to generate ratios of the form 1:x.
 */
 
-/* Bob Jenkins' http://burtleburtle.net/bob/rand/smallprng.html */
-/* Chosen for speed and a good "mix" */
-typedef struct ranctx {
-   uint64_t a;
-   uint64_t b;
-   uint64_t c;
-   uint64_t d;
-} ranctx;
-
-static ranctx burtle_x;
-
-#   define rot(x,k) (((x)<<(k))|((x)>>(64-(k))))
-static uint64_t s_ranval(void)
-{
-   uint64_t e = burtle_x.a - rot(burtle_x.b, 7);
-   burtle_x.a = burtle_x.b ^ rot(burtle_x.c, 13);
-   burtle_x.b = burtle_x.c + rot(burtle_x.d, 37);
-   burtle_x.c = burtle_x.d + e;
-   burtle_x.d = e + burtle_x.a;
-   return burtle_x.d;
-}
-
-static void s_raninit(uint64_t seed)
-{
-   uint64_t i;
-   burtle_x.a = 0xf1ea5eed;
-   burtle_x.b = burtle_x.c = burtle_x.d = seed;
-   for (i = 0; i < 20; ++i) {
-      (void) s_ranval();
-   }
-}
-
-/*
-   The original used LTM's mp_rand which uses the cryptographically secure
-   source of the OS for its purpose. That is too expensive, too slow and
-   most important for a benchmark: it is not repeatable.
-*/
-static int s_ranbuf(void *p, size_t n)
-{
-   char *q = (char *)p;
-   while (n > 0) {
-      int i;
-      uint64_t x = s_ranval();
-      for (i = 0; i < 8 && n > 0; ++i, --n) {
-         *q++ = (char)(x & 0xFF);
-         x >>= 8;
-      }
-   }
-   return MP_OKAY;
-}
+static uint64_t s_timer_function(void);
+static void s_timer_start(void);
+static uint64_t s_timer_stop(void);
+static uint64_t s_time_mul(int size);
+static uint64_t s_time_sqr(int size);
+static void s_usage(char *s);
 
 static uint64_t s_timer_function(void)
 {
@@ -263,8 +210,6 @@ int main(int argc, char **argv)
    s_number_of_test_loops = 64;
    s_stabilization_extra = 3;
 
-   mp_rand_source(s_ranbuf);
-
    /* Very simple option parser, please treat it nicely. */
    if (argc != 1) {
       for (opt = 1; (opt < argc) && (argv[opt][0] == '-'); opt++) {
@@ -504,7 +449,13 @@ int main(int argc, char **argv)
       }
    }
 
-   s_raninit(seed);
+   /*
+     mp_rand uses the cryptographically secure
+     source of the OS by default. That is too expensive, too slow and
+     most important for a benchmark: it is not repeatable.
+   */
+   s_mp_rand_jenkins_init(seed);
+   mp_rand_source(s_mp_rand_jenkins);
 
    ksm  = KARATSUBA_MUL_CUTOFF;
    kss  = KARATSUBA_SQR_CUTOFF;
diff --git a/libtommath_VS2008.vcproj b/libtommath_VS2008.vcproj
index 2ca8525..6e4fb35 100644
--- a/libtommath_VS2008.vcproj
+++ b/libtommath_VS2008.vcproj
@@ -873,6 +873,14 @@
 			>
 		</File>
 		<File
+			RelativePath="bn_s_mp_rand_jenkins.c"
+			>
+		</File>
+		<File
+			RelativePath="bn_s_mp_rand_platform.c"
+			>
+		</File>
+		<File
 			RelativePath="bn_s_mp_reverse.c"
 			>
 		</File>
diff --git a/makefile b/makefile
index 8c3f992..a77373f 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_jenkins.o bn_s_mp_rand_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..a1f72bc 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_jenkins.o bn_s_mp_rand_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..6e5ee71 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_jenkins.obj bn_s_mp_rand_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..1479793 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_jenkins.o bn_s_mp_rand_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..e43745a 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_jenkins.o bn_s_mp_rand_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_include.mk b/makefile_include.mk
index 161321a..d7d4cba 100644
--- a/makefile_include.mk
+++ b/makefile_include.mk
@@ -49,6 +49,10 @@ endif
 
 CFLAGS += -I./ -Wall -Wsign-compare -Wextra -Wshadow
 
+ifdef SANITIZER
+CFLAGS += -fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=float-divide-by-zero
+endif
+
 ifndef NO_ADDTL_WARNINGS
 # additional warnings
 CFLAGS += -Wdeclaration-after-statement -Wbad-function-cast -Wcast-align
diff --git a/tommath_class.h b/tommath_class.h
index 25e5462..2e62a64 100644
--- a/tommath_class.h
+++ b/tommath_class.h
@@ -150,6 +150,8 @@
 #   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_JENKINS_C
+#   define BN_S_MP_RAND_PLATFORM_C
 #   define BN_S_MP_REVERSE_C
 #   define BN_S_MP_SQR_C
 #   define BN_S_MP_SQR_FAST_C
@@ -720,6 +722,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 +765,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 +1174,13 @@
 #   define BN_MP_CLAMP_C
 #endif
 
+#if defined(BN_S_MP_RAND_JENKINS_C)
+#   define BN_S_MP_RAND_JENKINS_INIT_C
+#endif
+
+#if defined(BN_S_MP_RAND_PLATFORM_C)
+#endif
+
 #if defined(BN_S_MP_REVERSE_C)
 #endif
 
diff --git a/tommath_private.h b/tommath_private.h
index 7ef3582..2fa3e0e 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,8 +130,13 @@ 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_platform(void *p, size_t n);
 void s_mp_reverse(unsigned char *s, int len);
 
+/* TODO: jenkins prng is not thread safe as of now */
+MP_WUR int s_mp_rand_jenkins(void *p, size_t n);
+void s_mp_rand_jenkins_init(uint64_t);
+
 extern const char *const mp_s_rmap;
 extern const uint8_t mp_s_rmap_reverse[];
 extern const size_t mp_s_rmap_reverse_sz;