X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fgcrypt%2Fdigest.c;h=50446f6e7309f29ee775a2e9f7b90a9da47eb83f;hb=90cde91141ec61be4354d8deab21edb8fdf01022;hp=c8d4b3149b36f54e7cf31b4e4ec30719dd310099;hpb=77cd819058de43a5fcea54300dde50e03088c318;p=tinc diff --git a/src/gcrypt/digest.c b/src/gcrypt/digest.c index c8d4b314..50446f6e 100644 --- a/src/gcrypt/digest.c +++ b/src/gcrypt/digest.c @@ -1,6 +1,6 @@ /* digest.c -- Digest handling - Copyright (C) 2007-2012 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 @@ -17,27 +17,26 @@ 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; - enum gcry_md_algos 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, enum gcry_md_algos *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; @@ -47,8 +46,8 @@ static bool nametodigest(const char *name, enum gcry_md_algos *algo) { return false; } -static bool nidtodigest(int nid, enum gcry_md_algos *algo) { - for(int 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; @@ -58,8 +57,8 @@ static bool nidtodigest(int nid, enum gcry_md_algos *algo) { return false; } -static bool digesttonid(enum gcry_md_algos algo, int *nid) { - for(int 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; @@ -69,15 +68,15 @@ static bool digesttonid(enum gcry_md_algos algo, int *nid) { return false; } -static bool digest_open(digest_t *digest, enum gcry_md_algos algo, size_t maclength) { - if(!digesttonid(algo, &digest->nid)) { +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; } unsigned int len = gcry_md_get_algo_dlen(algo); - if(maclength > len || maclength < 0) { + if(maclength > len) { digest->maclength = len; } else { digest->maclength = maclength; @@ -90,9 +89,9 @@ static bool digest_open(digest_t *digest, enum gcry_md_algos algo, size_t maclen } bool digest_open_by_name(digest_t *digest, const char *name, size_t maclength) { - enum gcry_md_algos algo; + md_algo_t algo; - if(!nametodigest(name, &algo)) { + if(!nametodigest(&algo, name)) { logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest name '%s'!", name); return false; } @@ -100,10 +99,10 @@ bool digest_open_by_name(digest_t *digest, const char *name, size_t maclength) { return digest_open(digest, algo, maclength); } -bool digest_open_by_nid(digest_t *digest, int nid, size_t maclength) { - enum gcry_md_algos algo; +bool digest_open_by_nid(digest_t *digest, nid_t nid, size_t maclength) { + md_algo_t algo; - if(!nidtodigest(nid, &algo)) { + if(!nidtodigest(&algo, nid)) { logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest ID %d!", nid); return false; } @@ -111,10 +110,6 @@ bool digest_open_by_nid(digest_t *digest, int nid, size_t maclength) { return digest_open(digest, algo, maclength); } -bool digest_open_sha1(digest_t *digest, size_t maclength) { - return digest_open(digest, GCRY_MD_SHA1, maclength); -} - void digest_close(digest_t *digest) { if(digest->hmac) { gcry_md_close(digest->hmac); @@ -165,7 +160,7 @@ bool digest_verify(digest_t *digest, const void *indata, size_t inlen, const voi 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; }