summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--go.mod5
-rw-r--r--go.sum2
-rw-r--r--main.go74
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
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..ddc3c0f
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,5 @@
+module entropyd
+
+go 1.16
+
+require golang.org/x/sys v0.0.0-20210415045647-66c3f260301c
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..0c07c25
--- /dev/null
+++ b/go.sum
@@ -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=
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..be05e28
--- /dev/null
+++ b/main.go
@@ -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)
+ }
+}