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