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

View File

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