3405
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
Object subclass:#RandomRDRand
|
|
4 |
instanceVariableNames:'x y z c'
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Magnitude-Numbers'
|
|
8 |
!
|
|
9 |
|
|
10 |
!RandomRDRand class methodsFor:'documentation'!
|
|
11 |
|
|
12 |
documentation
|
|
13 |
"
|
|
14 |
Warning: there have been discussions about the security of the intel rdgen instruction
|
|
15 |
and whether there are NSA backdoors built into it.
|
|
16 |
Linus Torwalds refuses to use it for /dev/urandom in the linux kernel, for that very reason.
|
|
17 |
Be sure you know what you are doing, if you use this generator for sensitive cryptographic stuff.
|
|
18 |
We recommend using one of the libcrypt-based generators and use this only to get additional
|
|
19 |
entropy for the seed.
|
|
20 |
|
|
21 |
NO WARRANTY
|
|
22 |
|
|
23 |
This generator uses the rdgen random generator which is built into modern intel chips.
|
|
24 |
Before using, you should check via the isSupported query.
|
|
25 |
|
|
26 |
RandomRDGen new nextInteger
|
|
27 |
|
|
28 |
[see also:]
|
|
29 |
RandomGenerator - the default; uses the machine's /dev/random if available
|
|
30 |
Random - fast, but generates less quality random numbers
|
|
31 |
RandomTT800 - another random generator
|
|
32 |
RandomParkMiller - another random generator
|
|
33 |
RandomMT19937 - another random generator
|
|
34 |
RandomKISS - another random generator
|
|
35 |
exept:libcrypt - a library containing more stuff based on hashes and cyphers
|
|
36 |
|
|
37 |
[author:]
|
|
38 |
Claus Gittinger.
|
|
39 |
"
|
|
40 |
! !
|
|
41 |
|
|
42 |
!RandomRDRand class methodsFor:'instance creation'!
|
|
43 |
|
|
44 |
new
|
|
45 |
self isSupported ifFalse:[ self error:'this generator needs a cpu with rdgen instruction' ].
|
|
46 |
^ self basicNew
|
|
47 |
initialize
|
|
48 |
!
|
|
49 |
|
|
50 |
new:seed
|
|
51 |
"seed is actualy ignored"
|
|
52 |
|
|
53 |
self isSupported ifFalse:[ self error:'this generator needs a cpu with rdgen instruction' ].
|
|
54 |
^ self basicNew
|
|
55 |
initialize;
|
|
56 |
seed:seed
|
|
57 |
! !
|
|
58 |
|
|
59 |
!RandomRDRand class methodsFor:'queries'!
|
|
60 |
|
|
61 |
isSupported
|
|
62 |
"true if this architecture supports hardware random numbers"
|
|
63 |
|
|
64 |
^ OperatingSystem getCPUType = 'x86_64'
|
|
65 |
and:[ (OperatingSystem getSystemInfo at:#extendedInstructions ifAbsent:#())
|
|
66 |
includes:#aes ]
|
|
67 |
|
|
68 |
"
|
|
69 |
self isSupported
|
|
70 |
"
|
|
71 |
! !
|
|
72 |
|
|
73 |
!RandomRDRand methodsFor:'initialization'!
|
|
74 |
|
|
75 |
initialize
|
|
76 |
!
|
|
77 |
|
|
78 |
seed:seed
|
|
79 |
"/ ignored
|
|
80 |
! !
|
|
81 |
|
|
82 |
!RandomRDRand methodsFor:'random numbers'!
|
|
83 |
|
|
84 |
nextBoolean
|
3416
|
85 |
"generates a boolean random"
|
3405
|
86 |
|
|
87 |
^ self nextInteger > 16r7FFFFFFF
|
|
88 |
!
|
|
89 |
|
|
90 |
nextInteger
|
|
91 |
"generates the next integer in 0..FFFFFFFF.
|
|
92 |
Notice, it may raise an illegal instruction exception on some cpu chips,
|
|
93 |
even though the cpuid instruction says that it is available"
|
|
94 |
|
|
95 |
%{
|
|
96 |
unsigned int r = 0;
|
|
97 |
int cf;
|
|
98 |
|
|
99 |
do {
|
|
100 |
#if defined(__i386__) && defined(__GNUC__) && (__GNUC__ >= 2)
|
|
101 |
// Encoding of rdrand %eax
|
|
102 |
asm(".byte 0x0F, 0xC7, 0xF0; adcl $0,%1" :
|
|
103 |
"=a" (r), "=r" (cf) : "0" (r), "1" (cf) : "cc");
|
|
104 |
|
|
105 |
#else
|
|
106 |
# ifdef USE_DRAND32
|
|
107 |
cf = _rdrand32_step(&r);
|
|
108 |
# else
|
|
109 |
goto unsupported;
|
|
110 |
# endif
|
|
111 |
#endif
|
|
112 |
} while (cf != 0);
|
|
113 |
RETURN (__MKUINT(r));
|
|
114 |
|
|
115 |
unsupported: ;
|
|
116 |
%}.
|
|
117 |
self primitiveFailed:'unsupported on this architecture'
|
|
118 |
|
|
119 |
"
|
|
120 |
self new nextInteger
|
|
121 |
"
|
|
122 |
! !
|
|
123 |
|
|
124 |
!RandomRDRand class methodsFor:'documentation'!
|
|
125 |
|
|
126 |
version
|
3416
|
127 |
^ '$Header: /cvs/stx/stx/libbasic2/RandomRDRand.st,v 1.2 2014-10-02 16:23:16 cg Exp $'
|
3405
|
128 |
!
|
|
129 |
|
|
130 |
version_CVS
|
3416
|
131 |
^ '$Header: /cvs/stx/stx/libbasic2/RandomRDRand.st,v 1.2 2014-10-02 16:23:16 cg Exp $'
|
3405
|
132 |
! !
|
|
133 |
|