From 8386bf91f5cda763364eae726d64b035fa7dd40f Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Thu, 4 Feb 2010 13:07:12 -0500 Subject: mhash will use saved signature --- src/c/mhash.c | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file 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 +#include #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), -- cgit v1.2.3