RandomRDRand.st
author Claus Gittinger <cg@exept.de>
Tue, 18 Dec 2018 12:52:15 +0100
changeset 4777 b22943151ce0
parent 4667 35e1ebb8020e
child 4887 8c49f1189e7c
permissions -rw-r--r--
#DOCUMENTATION by cg class: ZipStream class comment/format in: #compress:into: #uncompress:into:

"{ Encoding: utf8 }"

"
 COPYRIGHT (c) 2014 Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic2' }"

"{ NameSpace: Smalltalk }"

Object subclass:#RandomRDRand
	instanceVariableNames:'x y z c'
	classVariableNames:''
	poolDictionaries:''
	category:'Magnitude-Numbers-Random'
!

!RandomRDRand class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2014 Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    This generator uses the rdgen random generator which is built into modern intel chips.
    Before using, you should check via the isSupported query.

    Warning:
	there have been discussions about the security of the intel rdgen instruction
	and whether there are NSA backdoors built into it.
	Linus Torwalds refuses to use it for /dev/urandom in the linux kernel, for that very reason.
	Be sure you know what you are doing, if you use this generator for sensitive cryptographic stuff.
	We recommend using one of the libcrypt-based generators and use this only to get additional
	entropy for the seed.

    NO WARRANTY

    RandomRDGen new nextInteger

    [see also:]
	RandomGenerator - the default; uses the machine's /dev/random if available
	Random  - fast, but generates less quality random numbers
	RandomTT800 - another random generator
	RandomParkMiller - another random generator
	RandomMT19937 - another random generator
	RandomKISS - another random generator
	exept:libcrypt - a library containing more stuff based on hashes and cyphers

    [author:]
	Claus Gittinger.
"
! !

!RandomRDRand class methodsFor:'instance creation'!

new
    self isSupported ifFalse:[ self error:'this generator needs a cpu with rdgen instruction' ].
    ^ self basicNew initialize

    "
     self new nextInteger
    "
!

new:seed
    "seed is actually ignored"

    self isSupported ifFalse:[ self error:'this generator needs a cpu with rdgen instruction' ].
    ^ self basicNew initialize; seed:seed
! !

!RandomRDRand class methodsFor:'queries'!

isSupported
    "true if this architecture supports hardware random numbers"

    ^ OperatingSystem getCPUType = 'x86_64'
	and:[ (OperatingSystem getSystemInfo at:#extendedInstructions ifAbsent:#())
	      includes:#aes ]

    "
     self isSupported
    "
! !

!RandomRDRand methodsFor:'initialization'!

initialize
!

seed:seed
    "/ ignored
! !

!RandomRDRand methodsFor:'random numbers'!

nextBoolean
    "generates a boolean random"

    ^ self nextInteger > 16r7FFFFFFF
!

nextInteger
    "generates the next integer in 0..FFFFFFFF.
     Notice, it may raise an illegal instruction exception on some cpu chips,
     even though the cpuid instruction says that it is available"

    |cfStillSet|

%{
    unsigned INT r = 0;
    int cf;
    int count = 50;

    do {
#ifdef USE_DRAND64
	cf = _rdrand64_step(&r);
#else
# ifdef USE_DRAND32
	cf = _rdrand32_step(&r);
# else
#  if defined(__x86__) && defined(__GNUC__) && (__GNUC__ >= 2)
	// Encoding of rdrand %eax
	asm(".byte 0x0F, 0xC7, 0xF0; adcl $0,%1"
	    : "=a" (r), "=r" (cf)
	    : "0" (r), "1" (cf)
	    : "cc");

#  else
#   if defined(__x86_64__) && defined(__GNUC__) && (__GNUC__ >= 2)
	// Encoding of rdrand %rax
	asm(".byte 0x48, 0x0F, 0xC7, 0xF0; adcl $0,%1"
	    : "=a" (r), "=r" (cf)
	    : "0" (r), "1" (cf)
	    : "cc");

#   else
	goto unsupported;
#   endif
#  endif
# endif
#endif
    } while ((cf != 0) && (--count > 0));
    if (cf == 0) {
	RETURN (__MKUINT(r));
    }
    cfStillSet = true;
unsupported: ;
%}.
    cfStillSet ifTrue:[
	self primitiveFailed:'carry flag not clear after 50 tries'
    ].
    self primitiveFailed:'unsupported on this architecture'

    "
     self new nextInteger
    "
! !

!RandomRDRand class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !