# HG changeset patch # User Adam Chlipala # Date 1265306832 18000 # Node ID 6249df767d4c52eae09a603de588393eb201afbc # Parent bafb6a5a52a9e6b49c2c58563fa07d0a535449c1 mhash will use saved signature diff -r bafb6a5a52a9 -r 6249df767d4c src/c/mhash.c --- 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 +#include #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),