]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
(gc_hmac_sha1): Add (for PKCS5 KDF).
authorSimon Josefsson <simon@josefsson.org>
Sat, 21 Aug 2004 21:46:55 +0000 (21:46 +0000)
committerSimon Josefsson <simon@josefsson.org>
Sat, 21 Aug 2004 21:46:55 +0000 (21:46 +0000)
crypto/gc-libgcrypt.c
crypto/gc-nettle.c
crypto/gc.h

index 9cf6a1ddc2aaf7a71b9a3f894875b34adb9ebc14..5884734b34469112428d9e9bb50990a6ff46be8e 100644 (file)
@@ -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;
+}
index 6a7d5e0767c0aa46b9659b0667f3726aba9e2939..5ea26246a1b068903587c32852751878f7338c25 100644 (file)
@@ -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;
+}
index 893be528bf710f072ddae4bd4822e77029bb074b..a47890688e0f066925825c3e25b7f82c0479513b 100644 (file)
@@ -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 */