annotate src/c/mhash.c @ 1277:1e6a4f9d3e4a

More generous wildification, covering map-records
author Adam Chlipala <adamc@hcoop.net>
date Sun, 13 Jun 2010 10:55:20 -0400
parents 236dc296c32d
children
rev   line source
adamc@1268 1 #include "config.h"
adamc@1268 2
adamc@1104 3 #include <mhash.h>
adamc@1145 4 #include <fcntl.h>
adamc@1104 5
adamc@1104 6 #define KEYSIZE 16
adamc@1104 7 #define PASSSIZE 4
adamc@1104 8
adamc@1104 9 #define HASH_ALGORITHM MHASH_SHA256
adamc@1104 10 #define HASH_BLOCKSIZE 32
adamc@1104 11 #define KEYGEN_ALGORITHM KEYGEN_MCRYPT
adamc@1104 12
adamc@1104 13 int uw_hash_blocksize = HASH_BLOCKSIZE;
adamc@1104 14
adamc@1104 15 static int password[PASSSIZE];
adamc@1104 16 static unsigned char private_key[KEYSIZE];
adamc@1104 17
adamc@1145 18 char *uw_sig_file = NULL;
adamc@1145 19
adamc@1145 20 static void random_password() {
adamc@1145 21 int i;
adamc@1145 22
adamc@1145 23 for (i = 0; i < PASSSIZE; ++i)
adamc@1145 24 password[i] = rand();
adamc@1145 25 }
adamc@1145 26
adamc@1104 27 void uw_init_crypto() {
adamc@1104 28 KEYGEN kg = {{HASH_ALGORITHM, HASH_ALGORITHM}};
adamc@1104 29
adamc@1104 30 assert(mhash_get_block_size(HASH_ALGORITHM) == HASH_BLOCKSIZE);
adamc@1104 31
adamc@1145 32 if (uw_sig_file) {
adamc@1145 33 int fd;
adamc@1145 34
adamc@1155 35 if (access(uw_sig_file, F_OK)) {
adamc@1145 36 random_password();
adamc@1145 37
adamc@1145 38 if ((fd = open(uw_sig_file, O_WRONLY | O_CREAT, 0700)) < 0) {
adamc@1145 39 fprintf(stderr, "Can't open signature file %s\n", uw_sig_file);
adamc@1145 40 perror("open");
adamc@1145 41 exit(1);
adamc@1145 42 }
adamc@1145 43
adamc@1145 44 if (write(fd, &password, sizeof password) != sizeof password) {
adamc@1145 45 fprintf(stderr, "Error writing signature file\n");
adamc@1145 46 exit(1);
adamc@1145 47 }
adamc@1145 48
adamc@1145 49 close(fd);
adamc@1145 50 } else {
adamc@1145 51 if ((fd = open(uw_sig_file, O_RDONLY)) < 0) {
adamc@1145 52 fprintf(stderr, "Can't open signature file %s\n", uw_sig_file);
adamc@1145 53 perror("open");
adamc@1145 54 exit(1);
adamc@1145 55 }
adamc@1145 56
adamc@1145 57 if (read(fd, &password, sizeof password) != sizeof password) {
adamc@1145 58 fprintf(stderr, "Error reading signature file\n");
adamc@1145 59 exit(1);
adamc@1145 60 }
adamc@1145 61
adamc@1145 62 close(fd);
adamc@1145 63 }
adamc@1145 64 } else
adamc@1145 65 random_password();
adamc@1104 66
adamc@1104 67 if (mhash_keygen_ext(KEYGEN_ALGORITHM, kg,
adamc@1104 68 private_key, sizeof(private_key),
adamc@1104 69 (unsigned char*)password, sizeof(password)) < 0) {
adamc@1104 70 fprintf(stderr, "Key generation failed\n");
adamc@1104 71 exit(1);
adamc@1104 72 }
adamc@1104 73 }
adamc@1104 74
adamc@1104 75 void uw_sign(const char *in, char *out) {
adamc@1104 76 MHASH td;
adamc@1104 77
adamc@1104 78 td = mhash_hmac_init(HASH_ALGORITHM, private_key, sizeof(private_key),
adamc@1104 79 mhash_get_hash_pblock(HASH_ALGORITHM));
adamc@1104 80
adamc@1104 81 mhash(td, in, strlen(in));
adamc@1104 82 if (mhash_hmac_deinit(td, out) < 0)
adamc@1104 83 fprintf(stderr, "Signing failed\n");
adamc@1104 84 }