changeset 1145:6249df767d4c

mhash will use saved signature
author Adam Chlipala <adamc@hcoop.net>
date Thu, 04 Feb 2010 13:07:12 -0500
parents bafb6a5a52a9
children 7fdea74b1dd9
files src/c/mhash.c
diffstat 1 files changed, 44 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/c/mhash.c	Sun Jan 31 15:28:50 2010 -0500
+++ b/src/c/mhash.c	Thu Feb 04 13:07:12 2010 -0500
@@ -1,4 +1,5 @@
 #include <mhash.h>
+#include <fcntl.h>
 
 #define KEYSIZE 16
 #define PASSSIZE 4
@@ -12,14 +13,54 @@
 static int password[PASSSIZE];
 static unsigned char private_key[KEYSIZE];
 
+char *uw_sig_file = NULL;
+
+static void random_password() {
+  int i;
+
+  for (i = 0; i < PASSSIZE; ++i)
+    password[i] = rand();
+}
+
 void uw_init_crypto() {
   KEYGEN kg = {{HASH_ALGORITHM, HASH_ALGORITHM}};
-  int i;
 
   assert(mhash_get_block_size(HASH_ALGORITHM) == HASH_BLOCKSIZE);
 
-  for (i = 0; i < PASSSIZE; ++i)
-    password[i] = rand();
+  if (uw_sig_file) {
+    int fd;
+
+    if (access(uw_sig_file, 0)) {
+      random_password();
+      
+      if ((fd = open(uw_sig_file, O_WRONLY | O_CREAT, 0700)) < 0) {
+        fprintf(stderr, "Can't open signature file %s\n", uw_sig_file);
+        perror("open");
+        exit(1);
+      }
+
+      if (write(fd, &password, sizeof password) != sizeof password) {
+        fprintf(stderr, "Error writing signature file\n");
+        exit(1);
+      }
+
+      close(fd);
+    } else {
+      if ((fd = open(uw_sig_file, O_RDONLY)) < 0) {
+        fprintf(stderr, "Can't open signature file %s\n", uw_sig_file);
+        perror("open");
+        exit(1);
+      }
+
+      if (read(fd, &password, sizeof password) != sizeof password) {
+        fprintf(stderr, "Error reading signature file\n");
+        exit(1);
+      }
+
+      close(fd);
+    }
+  } else
+    random_password();
 
   if (mhash_keygen_ext(KEYGEN_ALGORITHM, kg,
                        private_key, sizeof(private_key),