[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
gc: more hash functions
From: |
Simon Josefsson |
Subject: |
gc: more hash functions |
Date: |
Mon, 17 Oct 2005 15:11:17 +0200 |
User-agent: |
Gnus/5.110004 (No Gnus v0.4) Emacs/22.0.50 (gnu/linux) |
I have installed this. Again, the gnulib part will follow later on.
My goal now is to make GnuTLS be able to use libgcrypt through the
gnulib "gc" module.
2005-10-17 Simon Josefsson <address@hidden>
* gc.h, gc-libgcrypt.c: Add more hash types/functions.
Index: lib/gc.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/gc.h,v
retrieving revision 1.9
diff -u -p -r1.9 gc.h
--- lib/gc.h 17 Oct 2005 13:00:51 -0000 1.9
+++ lib/gc.h 17 Oct 2005 13:11:02 -0000
@@ -42,10 +42,20 @@ typedef enum Gc_rc Gc_rc;
enum Gc_hash
{
GC_MD5,
- GC_SHA1
+ GC_SHA1,
+ GC_MD2,
+ GC_RMD160
};
typedef enum Gc_hash Gc_hash;
+enum Gc_hash_mode
+ {
+ GC_HMAC = 1
+ };
+typedef enum Gc_hash_mode Gc_hash_mode;
+
+typedef void *gc_hash_handle;
+
#define GC_MD5_DIGEST_SIZE 16
#define GC_SHA1_DIGEST_SIZE 20
@@ -101,6 +111,17 @@ extern Gc_rc gc_cipher_decrypt_inline (g
extern Gc_rc gc_cipher_close (gc_cipher_handle handle);
/* Hashes. */
+
+extern Gc_rc gc_hash_open (Gc_hash hash, Gc_hash_mode mode,
+ gc_hash_handle * outhandle);
+extern Gc_rc gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle);
+extern size_t gc_hash_digest_length (Gc_hash hash);
+extern void gc_hash_hmac_setkey (gc_hash_handle handle,
+ size_t len, const char *key);
+extern void gc_hash_write (gc_hash_handle handle,
+ size_t len, const char *data);
+extern const char *gc_hash_read (gc_hash_handle handle);
+extern void gc_hash_close (gc_hash_handle handle);
/* Compute a hash value over buffer IN of INLEN bytes size using the
algorithm HASH, placing the result in the pre-allocated buffer OUT.
Index: lib/gc-libgcrypt.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/gc-libgcrypt.c,v
retrieving revision 1.8
diff -u -p -r1.8 gc-libgcrypt.c
--- lib/gc-libgcrypt.c 17 Oct 2005 13:00:51 -0000 1.8
+++ lib/gc-libgcrypt.c 17 Oct 2005 13:11:02 -0000
@@ -215,6 +215,118 @@ gc_cipher_close (gc_cipher_handle handle
/* Hashes. */
Gc_rc
+gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
+{
+ int gcryalg, gcrymode;
+ gcry_error_t err;
+
+ switch (hash)
+ {
+ case GC_MD5:
+ gcryalg = GCRY_MD_MD5;
+ break;
+
+ case GC_SHA1:
+ gcryalg = GCRY_MD_SHA1;
+ break;
+
+ case GC_RMD160:
+ gcryalg = GCRY_MD_RMD160;
+ break;
+
+ default:
+ return GC_INVALID_HASH;
+ }
+
+ switch (mode)
+ {
+ case 0:
+ gcrymode = 0;
+ break;
+
+ case GC_HMAC:
+ gcrymode = GCRY_MD_FLAG_HMAC;
+ break;
+
+ default:
+ return GC_INVALID_HASH;
+ }
+
+ err = gcry_md_open ((gcry_md_hd_t *) outhandle, gcryalg, gcrymode);
+ if (gcry_err_code (err))
+ return GC_INVALID_HASH;
+
+ return GC_OK;
+}
+
+Gc_rc
+gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
+{
+ int err;
+
+ err = gcry_md_copy ((gcry_md_hd_t *) outhandle, (gcry_md_hd_t) handle);
+ if (err)
+ return GC_INVALID_HASH;
+
+ return GC_OK;
+}
+
+size_t
+gc_hash_digest_length (Gc_hash hash)
+{
+ int gcryalg;
+
+ switch (hash)
+ {
+ case GC_MD5:
+ gcryalg = GCRY_MD_MD5;
+ break;
+
+ case GC_SHA1:
+ gcryalg = GCRY_MD_SHA1;
+ break;
+
+ case GC_RMD160:
+ gcryalg = GCRY_MD_RMD160;
+ break;
+
+ default:
+ return 0;
+ }
+
+ return gcry_md_get_algo_dlen (gcryalg);
+}
+
+void
+gc_hash_hmac_setkey (gc_hash_handle handle, size_t len, const char *key)
+{
+ gcry_md_setkey ((gcry_md_hd_t) handle, key, len);
+}
+
+void
+gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
+{
+ gcry_md_write ((gcry_md_hd_t) handle, data, len);
+}
+
+const char *
+gc_hash_read (gc_hash_handle handle)
+{
+ const char *digest;
+
+ gcry_md_final ((gcry_md_hd_t) handle);
+ digest = gcry_md_read ((gcry_md_hd_t) handle, 0);
+
+ return digest;
+}
+
+void
+gc_hash_close (gc_hash_handle handle)
+{
+ gcry_md_close ((gcry_md_hd_t) handle);
+}
+
+Gc_rc
gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
{
int gcryalg;
@@ -230,6 +342,12 @@ gc_hash_buffer (Gc_hash hash, const void
#ifdef GC_USE_SHA1
case GC_SHA1:
gcryalg = GCRY_MD_SHA1;
+ break;
+#endif
+
+#ifdef GC_USE_RMD160
+ case GC_RMD160:
+ gcryalg = GCRY_MD_RMD160;
break;
#endif
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- gc: more hash functions,
Simon Josefsson <=