Preserve random seed across reboots

At first I tried to just use systemd's systemd-random-seed, but that one
does not actually use an appropriate ioctl for persuading kernel that
entropy is there. There's a patch [1] for this, but its fate is far from
certain, and even with it, I am not completely sure that I got
everything working correctly (some boots were quick, others waited for
systemd-resolved in the same manner as before this patch).

In the end, just seeding stuff from the initrd [2] is much easier. A
downside is that the systemd unit systemd-random-seed.service will add
the contents of that file once again -- but that doesn't matter much
because we do not patch it, and therefore it does not increase kernel's
idea about available entropy.

Changes from that upstream rndaddentropy:

- larger buffer
- more conservative entropy contribution

[1] https://github.com/systemd/systemd/pull/10621 .
[2] https://github.com/rfinnie/twuewand/blob/master/rndaddentropy/rndaddentropy.c

Change-Id: Ibe3c926c241d75fb7d7c40f3df2a96813931971b
diff --git a/package/czechlight-cfg-fs/czechlight-random-seed.c b/package/czechlight-cfg-fs/czechlight-random-seed.c
new file mode 100644
index 0000000..ac5c94a
--- /dev/null
+++ b/package/czechlight-cfg-fs/czechlight-random-seed.c
@@ -0,0 +1,70 @@
+/* rndaddentropy, an RNDADDENTROPY ioctl wrapper
+ * Copyright (C) 2012 Ryan Finnie <ryan@finnie.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/fcntl.h>
+#include <sys/ioctl.h>
+#include <linux/random.h>
+
+int main(int argc, char *argv[]) {
+  struct {
+    int entropy_count;
+    int buf_size;
+    char buf[8192];
+  } entropy;
+
+  int i;
+  for(i=1; i < argc; i++) {
+    if(strcmp(argv[i], "--help") == 0) {
+      fprintf(stderr, "rndaddentropy, an RNDADDENTROPY ioctl wrapper\n");
+      fprintf(stderr, "Copyright (C) 2012 Ryan Finnie <ryan@finnie.org>\n");
+      fprintf(stderr, "\n");
+      fprintf(stderr, "Usage: $ENTROPY_GENERATOR | rndaddentropy\n");
+      fprintf(stderr, "\n");
+      fprintf(stderr, "WARNING!  This program is dangerous, and relies on your entropy\n");
+      fprintf(stderr, "generator producing adequate output.  Inadequate entropy generation\n");
+      fprintf(stderr, "fed to the primary pool is a security risk to the system.\n");
+      return(1);
+    }
+  }
+
+  int randfd;
+  if((randfd = open("/dev/random", O_WRONLY)) < 0) {
+    perror("/dev/random");
+    return(1);
+  }
+
+  int count;
+  while((count = fread(entropy.buf, 1, sizeof(entropy.buf), stdin)) > 0) {
+    // Jan Kundrat: be more conservative -- one bit of entropy per 16 bytes of randomness
+    entropy.entropy_count = count / 16;
+    if (entropy.entropy_count < 1) {
+      entropy.entropy_count = 1;
+    }
+    entropy.buf_size = count;
+    if(ioctl(randfd, RNDADDENTROPY, &entropy) < 0) {
+      perror("RNDADDENTROPY");
+      return(1);
+    }
+  }
+
+  return(0);
+}
+