X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fgcrypt%2Fdigest.c;h=9ebffba3dc57bf1895fb7e194be7855e20d6127a;hb=d917c8cb6b69475d568ccbe82389b9f2b3eb5e80;hp=639fa67790131098203e1902713b01644415c171;hpb=1b8f8918360b40a2749d40355266ed7dedbe41b5;p=tinc diff --git a/src/gcrypt/digest.c b/src/gcrypt/digest.c index 639fa677..9ebffba3 100644 --- a/src/gcrypt/digest.c +++ b/src/gcrypt/digest.c @@ -12,11 +12,9 @@ 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" @@ -75,56 +73,91 @@ static bool digesttonid(int algo, int *nid) { return false; } -static bool digest_open(digest_t *digest, int algo) { +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; } - digest->len = gcry_md_get_algo_dlen(algo); + unsigned int len = gcry_md_get_algo_dlen(algo); + + if(maclength > len || maclength < 0) + 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) { +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; } - return digest_open(digest, algo); + return digest_open(digest, algo, maclength); } -bool digest_open_by_nid(digest_t *digest, int nid) { +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; } - 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); +bool digest_open_sha1(digest_t *digest, int maclength) { + return digest_open(digest, GCRY_MD_SHA1, 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) { - gcry_md_hash_buffer(digest->algo, outdata, indata, inlen); + unsigned int len = gcry_md_get_algo_dlen(digest->algo); + + 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) { - char outdata[digest->len]; + unsigned int len = digest->maclength; + char 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) { @@ -132,7 +165,7 @@ int digest_get_nid(const digest_t *digest) { } size_t digest_length(const digest_t *digest) { - return digest->len; + return digest->maclength; } bool digest_active(const digest_t *digest) {