Add flags to disable the different random sources

This makes testing the different options much easier.

Signed-off-by: Justin Cormack <justin@specialbusservice.com>
This commit is contained in:
Justin Cormack 2017-08-02 20:55:36 +01:00
parent 63798997f6
commit f768ac4bfd
2 changed files with 13 additions and 7 deletions

View File

@ -3,6 +3,7 @@
package main
import (
"flag"
"log"
"os"
@ -10,10 +11,11 @@ import (
)
func main() {
oneshot := len(os.Args) > 1 && os.Args[1] == "-1"
oneshot := flag.Bool("1", false, "Enable oneshot mode")
flag.Parse()
timeout := -1
if oneshot {
if *oneshot {
timeout = 0
}
@ -61,7 +63,7 @@ func main() {
if nevents == 1 && events[0].Events&unix.EPOLLOUT == unix.EPOLLOUT {
continue
}
if oneshot {
if *oneshot {
log.Printf("Wrote %d bytes of entropy, exiting as oneshot\n", count)
break
}

View File

@ -36,12 +36,16 @@ import "C"
import (
"errors"
"flag"
"os"
"unsafe"
"golang.org/x/sys/unix"
)
var disableRdrand = flag.Bool("disable-rdrand", false, "Disable use of RDRAND")
var disableRdseed = flag.Bool("disable-rdseed", false, "Disable use of RDSEED")
var hasRdrand, hasRdseed bool
type randInfo struct {
@ -51,19 +55,19 @@ type randInfo struct {
}
func initRand() bool {
hasRdrand = C.hasrdrand() == 1
hasRdseed = C.hasrdseed() == 1
hasRdrand = C.hasrdrand() == 1 && !*disableRdrand
hasRdseed = C.hasrdseed() == 1 && !*disableRdseed
return hasRdrand || hasRdseed
}
func rand() (uint64, error) {
var x C.uint64_t
// prefer rdseed as that is correct seed
if hasRdseed && C.rdseed(&x) == 1 {
if hasRdseed && C.rdseed(&x) == 1 && !*disableRdseed {
return uint64(x), nil
}
// failed rdseed, rdrand better than nothing
if hasRdrand && C.rdrand(&x) == 1 {
if hasRdrand && C.rdrand(&x) == 1 && !*disableRdrand {
return uint64(x), nil
}
return 0, errors.New("No randomness available")