diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | go.mod | 5 | ||||
| -rw-r--r-- | go.sum | 2 | ||||
| -rw-r--r-- | main.go | 74 |
4 files changed, 82 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..783cf17 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +entropyd @@ -0,0 +1,5 @@ +module entropyd + +go 1.16 + +require golang.org/x/sys v0.0.0-20210415045647-66c3f260301c @@ -0,0 +1,2 @@ +golang.org/x/sys v0.0.0-20210415045647-66c3f260301c h1:6L+uOeS3OQt/f4eFHXZcTxeZrGCuz+CLElgEBjbcTA4= +golang.org/x/sys v0.0.0-20210415045647-66c3f260301c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -0,0 +1,74 @@ +package main + +import ( + "fmt" + "golang.org/x/sys/unix" + "log" + "math/rand" + "os" + "time" + "unsafe" +) + +const BUFSIZE = 512 + +type rand_pool_info struct { + entropy_count int + buf_size int + buf [512]byte +} + +var pid int + +func init() { + log.SetFlags(0) + pid = os.Getpid() +} + +func ioctl(fd int, req uint, ptr unsafe.Pointer) error { + _, _, errno := unix.Syscall(unix.SYS_IOCTL, + uintptr(fd), + uintptr(req), + uintptr(ptr)) + + if errno != 0 { + return fmt.Errorf("ioctl: %w", errno) + } + + return nil +} + +func feed(fd int) error { + seed := pid * time.Now().Nanosecond() + rand.Seed(int64(seed)) + + output := &rand_pool_info{ + entropy_count: BUFSIZE * 8, + buf_size: BUFSIZE, + } + + rand.Read(output.buf[:]) + + err := ioctl(fd, unix.RNDADDENTROPY, unsafe.Pointer(output)) + if err != nil { + return err + } + + return nil +} + +func main() { + fd, err := unix.Open("/dev/random", unix.O_RDWR, 0) + if err != nil { + log.Fatal(err) + } + defer unix.Close(fd) + + for { + if err := feed(fd); err != nil { + log.Fatalf("feed failed: %s", err) + } + + time.Sleep(time.Second) + } +} |
