X-Git-Url: https://www.tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fgcrypt%2Fdigest.c;h=cfea1f0af82a1acb0f5692e922c35a82c55cc1bd;hb=c44b08613508c993e7fd9f625e0b1b4775efffed;hp=639fa67790131098203e1902713b01644415c171;hpb=075e6828a7533e7daa790225f17aa6bb39703278;p=tinc diff --git a/src/gcrypt/digest.c b/src/gcrypt/digest.c index 639fa677..cfea1f0a 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-2022 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 @@ -12,34 +12,31 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - $Id$ + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "system.h" +#include "../system.h" #include "digest.h" -#include "logger.h" +#include "../digest.h" +#include "../logger.h" static struct { const char *name; - int algo; - int nid; + md_algo_t algo; + nid_t nid; } digesttable[] = { - {"none", GCRY_MD_NONE, 0}, - {"sha1", GCRY_MD_SHA1, 64}, + {"none", GCRY_MD_NONE, 0}, + {"sha1", GCRY_MD_SHA1, 64}, {"sha256", GCRY_MD_SHA256, 672}, {"sha384", GCRY_MD_SHA384, 673}, {"sha512", GCRY_MD_SHA512, 674}, }; -static bool nametodigest(const char *name, int *algo) { - int i; - - for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) { +static bool nametodigest(md_algo_t *algo, const char *name) { + for(size_t i = 0; i < sizeof(digesttable) / sizeof(*digesttable); i++) { if(digesttable[i].name && !strcasecmp(name, digesttable[i].name)) { *algo = digesttable[i].algo; return true; @@ -49,10 +46,8 @@ static bool nametodigest(const char *name, int *algo) { return false; } -static bool nidtodigest(int nid, int *algo) { - int i; - - for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) { +static bool nidtodigest(md_algo_t *algo, nid_t nid) { + for(size_t i = 0; i < sizeof(digesttable) / sizeof(*digesttable); i++) { if(nid == digesttable[i].nid) { *algo = digesttable[i].algo; return true; @@ -62,10 +57,8 @@ static bool nidtodigest(int nid, int *algo) { return false; } -static bool digesttonid(int algo, int *nid) { - int i; - - for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) { +static bool digesttonid(nid_t *nid, md_algo_t algo) { + for(size_t i = 0; i < sizeof(digesttable) / sizeof(*digesttable); i++) { if(algo == digesttable[i].algo) { *nid = digesttable[i].nid; return true; @@ -75,66 +68,118 @@ static bool digesttonid(int algo, int *nid) { return false; } -static bool digest_open(digest_t *digest, int algo) { - if(!digesttonid(algo, &digest->nid)) { - logger(LOG_DEBUG, _("Digest %d has no corresponding nid!"), algo); +static bool digest_open(digest_t *digest, md_algo_t algo, size_t maclength) { + if(!digesttonid(&digest->nid, algo)) { + logger(DEBUG_ALWAYS, LOG_DEBUG, "Digest %d has no corresponding nid!", algo); return false; } - digest->len = gcry_md_get_algo_dlen(algo); + unsigned int len = gcry_md_get_algo_dlen(algo); + + if(maclength > len) { + digest->maclength = len; + } else { + digest->maclength = maclength; + } + + digest->algo = algo; + digest->hmac = NULL; return true; } -bool digest_open_by_name(digest_t *digest, const char *name) { - int algo; +bool digest_open_by_name(digest_t *digest, const char *name, size_t maclength) { + md_algo_t algo; - if(!nametodigest(name, &algo)) { - logger(LOG_DEBUG, _("Unknown digest name '%s'!"), name); + if(!nametodigest(&algo, name)) { + logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest name '%s'!", name); return false; } - return digest_open(digest, algo); + return digest_open(digest, algo, maclength); } -bool digest_open_by_nid(digest_t *digest, int nid) { - int algo; +bool digest_open_by_nid(digest_t *digest, nid_t nid, size_t maclength) { + md_algo_t algo; - if(!nidtodigest(nid, &algo)) { - logger(LOG_DEBUG, _("Unknown digest ID %d!"), nid); + if(!nidtodigest(&algo, nid)) { + logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest ID %d!", nid); return false; } - return digest_open(digest, algo); + return digest_open(digest, algo, maclength); } -bool digest_open_sha1(digest_t *digest) { - return digest_open(digest, GCRY_MD_SHA1); +void digest_close(digest_t *digest) { + if(!digest) { + return; + } + + if(digest->hmac) { + gcry_md_close(digest->hmac); + } + + memset(digest, 0, sizeof(*digest)); } -void digest_close(digest_t *digest) { +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) { - gcry_md_hash_buffer(digest->algo, outdata, indata, inlen); + unsigned int len = gcry_md_get_algo_dlen(digest->algo); + + if(digest->hmac) { + uint8_t *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) { - char outdata[digest->len]; + size_t len = digest->maclength; + uint8_t outdata[len]; - gcry_md_hash_buffer(digest->algo, outdata, indata, inlen); - return !memcmp(cmpdata, outdata, digest->len); + return digest_create(digest, indata, inlen, outdata) && !memcmp(cmpdata, outdata, len); } -int digest_get_nid(const digest_t *digest) { +nid_t digest_get_nid(const digest_t *digest) { + if(!digest || !digest->nid) { + return 0; + } + return digest->nid; } size_t digest_length(const digest_t *digest) { - return digest->len; + if(!digest) { + return 0; + } + + return digest->maclength; } bool digest_active(const digest_t *digest) { - return digest->algo != GCRY_MD_NONE; + return digest && digest->algo != GCRY_MD_NONE; }