From: Simon Josefsson Date: Sat, 21 Aug 2004 21:46:55 +0000 (+0000) Subject: (gc_hmac_sha1): Add (for PKCS5 KDF). X-Git-Tag: gnutls_1_1_18~71 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=144a9904471718f758ae6d12abef1bf3da482bdd;p=thirdparty%2Fgnutls.git (gc_hmac_sha1): Add (for PKCS5 KDF). --- diff --git a/crypto/gc-libgcrypt.c b/crypto/gc-libgcrypt.c index 9cf6a1ddc2..5884734b34 100644 --- a/crypto/gc-libgcrypt.c +++ b/crypto/gc-libgcrypt.c @@ -408,3 +408,36 @@ gc_hmac_md5 (const char *key, size_t keylen, return GC_OK; } + +int +gc_hmac_sha1 (const char *key, size_t keylen, + const char *in, size_t inlen, + char outhash[GC_SHA1_LEN]) +{ + size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1); + gcry_md_hd_t mdh; + unsigned char *hash; + gpg_error_t err; + + assert (hlen == GC_SHA1_LEN); + + err = gcry_md_open (&mdh, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC); + if (err != GPG_ERR_NO_ERROR) + return GC_INVALID_HASH; + + err = gcry_md_setkey (mdh, key, keylen); + if (err != GPG_ERR_NO_ERROR) + return GC_INVALID_HASH; + + gcry_md_write (mdh, in, inlen); + + hash = gcry_md_read (mdh, GCRY_MD_SHA1); + if (hash == NULL) + return GC_INVALID_HASH; + + memcpy (outhash, hash, hlen); + + gcry_md_close (mdh); + + return GC_OK; +} diff --git a/crypto/gc-nettle.c b/crypto/gc-nettle.c index 6a7d5e0767..5ea26246a1 100644 --- a/crypto/gc-nettle.c +++ b/crypto/gc-nettle.c @@ -543,10 +543,9 @@ gc_hash_buffer (int hash, const char *in, size_t inlen, char *out) * gc_md5: * @in: input character array of data to hash. * @inlen: length of input character array of data to hash. - * @out: newly allocated character array with hash of data. + * @out: pre-allocated character array with hash of data. * - * Compute hash of data using MD5. The @out buffer must be - * deallocated by the caller. + * Compute hash of data using MD5. * * Return value: Returns %GC_OK iff successful. **/ @@ -568,10 +567,9 @@ gc_md5 (const char *in, size_t inlen, char out[GC_MD5_LEN]) * @keylen: length of input character array with key to use. * @in: input character array of data to hash. * @inlen: length of input character array of data to hash. - * @outhash: newly allocated character array with keyed hash of data. + * @outhash: pre-allocated character array with keyed hash of data. * - * Compute keyed checksum of data using HMAC-MD5. The @outhash buffer - * must be deallocated by the caller. + * Compute keyed checksum of data using HMAC-MD5. * * Return value: Returns %GC_OK iff successful. **/ @@ -588,3 +586,29 @@ gc_hmac_md5 (const char *key, size_t keylen, return GC_OK; } + +/** + * gc_hmac_sha1: + * @key: input character array with key to use. + * @keylen: length of input character array with key to use. + * @in: input character array of data to hash. + * @inlen: length of input character array of data to hash. + * @outhash: pre-allocated character array with keyed hash of data. + * + * Compute keyed checksum of data using HMAC-SHA1. + * + * Return value: Returns %GC_OK iff successful. + **/ +int +gc_hmac_sha1 (const char *key, size_t keylen, + const char *in, size_t inlen, + char outhash[SHA1_DIGEST_SIZE]) +{ + struct hmac_sha1_ctx ctx; + + hmac_sha1_set_key (&ctx, keylen, key); + hmac_sha1_update (&ctx, inlen, in); + hmac_sha1_digest (&ctx, SHA1_DIGEST_SIZE, outhash); + + return GC_OK; +} diff --git a/crypto/gc.h b/crypto/gc.h index 893be528bf..a47890688e 100644 --- a/crypto/gc.h +++ b/crypto/gc.h @@ -121,5 +121,8 @@ extern int gc_md5 (const char *in, size_t inlen, char out[GC_MD5_LEN]); extern int gc_hmac_md5 (const char *key, size_t keylen, const char *in, size_t inlen, char outhash[GC_MD5_LEN]); +extern int gc_hmac_sha1 (const char *key, size_t keylen, + const char *in, size_t inlen, + char outhash[GC_SHA1_LEN]); #endif /* GC_H */