chiark / gitweb /
Add 'uml/rndaddtoentcnt/' from commit '3cef9b224336ac4147aade20738420193e525fc5'
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 7 Aug 2021 11:14:07 +0000 (12:14 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 7 Aug 2021 11:14:07 +0000 (12:14 +0100)
git-subtree-dir: uml/rndaddtoentcnt
git-subtree-mainline: 488191f4770edfd9990ab847b061e4227f79a091
git-subtree-split: 3cef9b224336ac4147aade20738420193e525fc5

uml/rndaddtoentcnt/.gitignore [new file with mode: 0644]
uml/rndaddtoentcnt/LICENSE [new file with mode: 0644]
uml/rndaddtoentcnt/Makefile [new file with mode: 0644]
uml/rndaddtoentcnt/README.md [new file with mode: 0644]
uml/rndaddtoentcnt/rndaddtoentcnt.c [new file with mode: 0644]

diff --git a/uml/rndaddtoentcnt/.gitignore b/uml/rndaddtoentcnt/.gitignore
new file mode 100644 (file)
index 0000000..a997b67
--- /dev/null
@@ -0,0 +1 @@
+rndaddtoentcnt
diff --git a/uml/rndaddtoentcnt/LICENSE b/uml/rndaddtoentcnt/LICENSE
new file mode 100644 (file)
index 0000000..4479b29
--- /dev/null
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2018 Jumpnow Technologies, LLC
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/uml/rndaddtoentcnt/Makefile b/uml/rndaddtoentcnt/Makefile
new file mode 100644 (file)
index 0000000..7b3c881
--- /dev/null
@@ -0,0 +1,6 @@
+rndaddtoentcnt: rndaddtoentcnt.c
+       $(CC) rndaddtoentcnt.c -o rndaddtoentcnt
+
+.PHONY: clean
+clean:
+       rm -f *.o rndaddtoentcnt
diff --git a/uml/rndaddtoentcnt/README.md b/uml/rndaddtoentcnt/README.md
new file mode 100644 (file)
index 0000000..9f85b29
--- /dev/null
@@ -0,0 +1,15 @@
+### rndaddtoentcnt
+
+Seeding the random number generator by writing to /dev/urandom does not update the entropy count.
+
+This utility makes the RNDADDTOENTCNT ioctl call needed to do this.
+
+Used in startup scripts after initializing /dev/urandom with a presaved seed.
+
+Example:
+
+    dd if=/path/to/some/random-seed-file of=/dev/urandom bs=512 count=1
+
+    /path/to/rdnaddtoentcnt <entropy-bit-count>
+
+where entropy-bit-count is a number between 1 and (8 * 512) depending on how much you trust the seed file.
diff --git a/uml/rndaddtoentcnt/rndaddtoentcnt.c b/uml/rndaddtoentcnt/rndaddtoentcnt.c
new file mode 100644 (file)
index 0000000..929bf4d
--- /dev/null
@@ -0,0 +1,46 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+
+#include <linux/random.h>
+
+
+int main(int argc, char **argv)
+{
+    int count, fd;
+
+    if (argc != 2) {
+        printf("Usage: rndaddtoentcnt <entropy-bit-count>\n");
+        exit(1);
+    }
+
+    count = strtoul(argv[1], NULL, 0);
+
+    if (count < 1 || count > 4096) {
+        printf("Count range is 1 to 4096\n");
+        exit(1);
+    }
+
+    fd = open("/dev/urandom", O_WRONLY);
+
+    if (fd < 0) {
+        perror("open(/dev/urandom)");
+        exit(1);
+    }
+
+
+    if (ioctl(fd, RNDADDTOENTCNT, &count) < 0) {
+        perror("ioctl(RNDADDTOENTCNT)");
+        close(fd);
+        exit(1);
+    }
+
+    close(fd);
+
+    return 0;
+}