summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adamc@hcoop.net>2010-02-04 13:07:12 -0500
committerGravatar Adam Chlipala <adamc@hcoop.net>2010-02-04 13:07:12 -0500
commit8386bf91f5cda763364eae726d64b035fa7dd40f (patch)
tree27e70249dba390225e64fdaf18d6c61aa785d549
parent771ec67a7691b5732b4915c6bbaefe9b21f35362 (diff)
mhash will use saved signature
-rw-r--r--src/c/mhash.c47
1 files changed, 44 insertions, 3 deletions
diff --git a/src/c/mhash.c b/src/c/mhash.c
index becb9d97..91a3f78f 100644
--- a/src/c/mhash.c
+++ b/src/c/mhash.c
@@ -1,4 +1,5 @@
#include <mhash.h>
+#include <fcntl.h>
#define KEYSIZE 16
#define PASSSIZE 4
@@ -12,14 +13,54 @@ int uw_hash_blocksize = HASH_BLOCKSIZE;
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),