annotate src/c/mhash.c @ 1144:bafb6a5a52a9

A litter more cautious about NULL applications; plug a memory leak in some Postgres functions
author Adam Chlipala <adamc@hcoop.net>
date Sun, 31 Jan 2010 15:28:50 -0500
parents 72670131dace
children 6249df767d4c
rev   line source
adamc@1104 1 #include <mhash.h>
adamc@1104 2
adamc@1104 3 #define KEYSIZE 16
adamc@1104 4 #define PASSSIZE 4
adamc@1104 5
adamc@1104 6 #define HASH_ALGORITHM MHASH_SHA256
adamc@1104 7 #define HASH_BLOCKSIZE 32
adamc@1104 8 #define KEYGEN_ALGORITHM KEYGEN_MCRYPT
adamc@1104 9
adamc@1104 10 int uw_hash_blocksize = HASH_BLOCKSIZE;
adamc@1104 11
adamc@1104 12 static int password[PASSSIZE];
adamc@1104 13 static unsigned char private_key[KEYSIZE];
adamc@1104 14
adamc@1104 15 void uw_init_crypto() {
adamc@1104 16 KEYGEN kg = {{HASH_ALGORITHM, HASH_ALGORITHM}};
adamc@1104 17 int i;
adamc@1104 18
adamc@1104 19 assert(mhash_get_block_size(HASH_ALGORITHM) == HASH_BLOCKSIZE);
adamc@1104 20
adamc@1104 21 for (i = 0; i < PASSSIZE; ++i)
adamc@1104 22 password[i] = rand();
adamc@1104 23
adamc@1104 24 if (mhash_keygen_ext(KEYGEN_ALGORITHM, kg,
adamc@1104 25 private_key, sizeof(private_key),
adamc@1104 26 (unsigned char*)password, sizeof(password)) < 0) {
adamc@1104 27 fprintf(stderr, "Key generation failed\n");
adamc@1104 28 exit(1);
adamc@1104 29 }
adamc@1104 30 }
adamc@1104 31
adamc@1104 32 void uw_sign(const char *in, char *out) {
adamc@1104 33 MHASH td;
adamc@1104 34
adamc@1104 35 td = mhash_hmac_init(HASH_ALGORITHM, private_key, sizeof(private_key),
adamc@1104 36 mhash_get_hash_pblock(HASH_ALGORITHM));
adamc@1104 37
adamc@1104 38 mhash(td, in, strlen(in));
adamc@1104 39 if (mhash_hmac_deinit(td, out) < 0)
adamc@1104 40 fprintf(stderr, "Signing failed\n");
adamc@1104 41 }