X-Git-Url: https://www.tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fgcrypt%2Fdigest.c;h=ac5dc744fa152c7b9de65658f2d0f38d645d42e9;hb=f6e87ab476a0faf8b124ecaaa27f967d825e6457;hp=34f9d2800d38ccc71f4499280b72adef8bf4e821;hpb=d6c50eb73ad49bd2eac67214995dff76b7a20661;p=tinc diff --git a/src/gcrypt/digest.c b/src/gcrypt/digest.c index 34f9d280..ac5dc744 100644 --- a/src/gcrypt/digest.c +++ b/src/gcrypt/digest.c @@ -1,6 +1,6 @@ /* digest.c -- Digest handling - Copyright (C) 2007 Guus Sliepen + Copyright (C) 2007-2012 Guus Sliepen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -37,7 +37,7 @@ static struct { static bool nametodigest(const char *name, int *algo) { int i; - for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) { + for(i = 0; i < sizeof(digesttable) / sizeof * digesttable; i++) { if(digesttable[i].name && !strcasecmp(name, digesttable[i].name)) { *algo = digesttable[i].algo; return true; @@ -50,7 +50,7 @@ static bool nametodigest(const char *name, int *algo) { static bool nidtodigest(int nid, int *algo) { int i; - for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) { + for(i = 0; i < sizeof(digesttable) / sizeof * digesttable; i++) { if(nid == digesttable[i].nid) { *algo = digesttable[i].algo; return true; @@ -63,7 +63,7 @@ static bool nidtodigest(int nid, int *algo) { static bool digesttonid(int algo, int *nid) { int i; - for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) { + for(i = 0; i < sizeof(digesttable) / sizeof * digesttable; i++) { if(algo == digesttable[i].algo) { *nid = digesttable[i].nid; return true; @@ -75,18 +75,20 @@ static bool digesttonid(int algo, int *nid) { static bool digest_open(digest_t *digest, int algo, int maclength) { if(!digesttonid(algo, &digest->nid)) { - logger(LOG_DEBUG, "Digest %d has no corresponding nid!", algo); + logger(DEBUG_ALWAYS, LOG_DEBUG, "Digest %d has no corresponding nid!", algo); return false; } unsigned int len = gcry_md_get_algo_dlen(algo); - if(maclength > len || maclength < 0) + if(maclength > len || maclength < 0) { digest->maclength = len; - else + } else { digest->maclength = maclength; - + } + digest->algo = algo; + digest->hmac = NULL; return true; } @@ -95,7 +97,7 @@ bool digest_open_by_name(digest_t *digest, const char *name, int maclength) { int algo; if(!nametodigest(name, &algo)) { - logger(LOG_DEBUG, "Unknown digest name '%s'!", name); + logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest name '%s'!", name); return false; } @@ -106,7 +108,7 @@ bool digest_open_by_nid(digest_t *digest, int nid, int maclength) { int algo; if(!nidtodigest(nid, &algo)) { - logger(LOG_DEBUG, "Unknown digest ID %d!", nid); + logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest ID %d!", nid); return false; } @@ -118,24 +120,53 @@ bool digest_open_sha1(digest_t *digest, int maclength) { } void digest_close(digest_t *digest) { + if(digest->hmac) { + gcry_md_close(digest->hmac); + } + + digest->hmac = NULL; +} + +bool digest_set_key(digest_t *digest, const void *key, size_t len) { + if(!digest->hmac) { + gcry_md_open(&digest->hmac, digest->algo, GCRY_MD_FLAG_HMAC); + } + + if(!digest->hmac) { + return false; + } + + return !gcry_md_setkey(digest->hmac, key, len); } bool digest_create(digest_t *digest, const void *indata, size_t inlen, void *outdata) { unsigned int len = gcry_md_get_algo_dlen(digest->algo); - char tmpdata[len]; - gcry_md_hash_buffer(digest->algo, tmpdata, indata, inlen); - memcpy(outdata, tmpdata, digest->maclength); + if(digest->hmac) { + char *tmpdata; + gcry_md_reset(digest->hmac); + gcry_md_write(digest->hmac, indata, inlen); + tmpdata = gcry_md_read(digest->hmac, digest->algo); + + if(!tmpdata) { + return false; + } + + memcpy(outdata, tmpdata, digest->maclength); + } else { + char tmpdata[len]; + gcry_md_hash_buffer(digest->algo, tmpdata, indata, inlen); + memcpy(outdata, tmpdata, digest->maclength); + } return true; } bool digest_verify(digest_t *digest, const void *indata, size_t inlen, const void *cmpdata) { - unsigned int len = gcry_md_get_algo_dlen(digest->algo); + unsigned int len = digest->maclength; char outdata[len]; - gcry_md_hash_buffer(digest->algo, outdata, indata, inlen); - return !memcmp(cmpdata, outdata, digest->maclength); + return digest_create(digest, indata, inlen, outdata) && !memcmp(cmpdata, outdata, len); } int digest_get_nid(const digest_t *digest) {