comparison src/c/mhash.c @ 1145:6249df767d4c

mhash will use saved signature
author Adam Chlipala <adamc@hcoop.net>
date Thu, 04 Feb 2010 13:07:12 -0500
parents 72670131dace
children 042f618f7c77
comparison
equal deleted inserted replaced
1144:bafb6a5a52a9 1145:6249df767d4c
1 #include <mhash.h> 1 #include <mhash.h>
2 #include <fcntl.h>
2 3
3 #define KEYSIZE 16 4 #define KEYSIZE 16
4 #define PASSSIZE 4 5 #define PASSSIZE 4
5 6
6 #define HASH_ALGORITHM MHASH_SHA256 7 #define HASH_ALGORITHM MHASH_SHA256
10 int uw_hash_blocksize = HASH_BLOCKSIZE; 11 int uw_hash_blocksize = HASH_BLOCKSIZE;
11 12
12 static int password[PASSSIZE]; 13 static int password[PASSSIZE];
13 static unsigned char private_key[KEYSIZE]; 14 static unsigned char private_key[KEYSIZE];
14 15
16 char *uw_sig_file = NULL;
17
18 static void random_password() {
19 int i;
20
21 for (i = 0; i < PASSSIZE; ++i)
22 password[i] = rand();
23 }
24
15 void uw_init_crypto() { 25 void uw_init_crypto() {
16 KEYGEN kg = {{HASH_ALGORITHM, HASH_ALGORITHM}}; 26 KEYGEN kg = {{HASH_ALGORITHM, HASH_ALGORITHM}};
17 int i;
18 27
19 assert(mhash_get_block_size(HASH_ALGORITHM) == HASH_BLOCKSIZE); 28 assert(mhash_get_block_size(HASH_ALGORITHM) == HASH_BLOCKSIZE);
20 29
21 for (i = 0; i < PASSSIZE; ++i) 30 if (uw_sig_file) {
22 password[i] = rand(); 31 int fd;
32
33 if (access(uw_sig_file, 0)) {
34 random_password();
35
36 if ((fd = open(uw_sig_file, O_WRONLY | O_CREAT, 0700)) < 0) {
37 fprintf(stderr, "Can't open signature file %s\n", uw_sig_file);
38 perror("open");
39 exit(1);
40 }
41
42 if (write(fd, &password, sizeof password) != sizeof password) {
43 fprintf(stderr, "Error writing signature file\n");
44 exit(1);
45 }
46
47 close(fd);
48 } else {
49 if ((fd = open(uw_sig_file, O_RDONLY)) < 0) {
50 fprintf(stderr, "Can't open signature file %s\n", uw_sig_file);
51 perror("open");
52 exit(1);
53 }
54
55 if (read(fd, &password, sizeof password) != sizeof password) {
56 fprintf(stderr, "Error reading signature file\n");
57 exit(1);
58 }
59
60 close(fd);
61 }
62 } else
63 random_password();
23 64
24 if (mhash_keygen_ext(KEYGEN_ALGORITHM, kg, 65 if (mhash_keygen_ext(KEYGEN_ALGORITHM, kg,
25 private_key, sizeof(private_key), 66 private_key, sizeof(private_key),
26 (unsigned char*)password, sizeof(password)) < 0) { 67 (unsigned char*)password, sizeof(password)) < 0) {
27 fprintf(stderr, "Key generation failed\n"); 68 fprintf(stderr, "Key generation failed\n");