From 42a58ba9efc7afe5f359f9e5992d15651843c92c Mon Sep 17 00:00:00 2001 From: Simon Josefsson Date: Mon, 17 Oct 2005 13:13:08 +0000 Subject: [PATCH] Add more hash functions. --- lib/ChangeLog | 4 ++ lib/gc-libgcrypt.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/gc.h | 23 ++++++++++- 3 files changed, 144 insertions(+), 1 deletion(-) diff --git a/lib/ChangeLog b/lib/ChangeLog index c87f9dee1..22176e385 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,5 +1,9 @@ 2005-10-17 Simon Josefsson + * gc.h, gc-libgcrypt.c: Add more hash types/functions. + +2005-10-17 Simon Josefsson + * gc.h, gc-libgcrypt.c: Add ciphers. 2005-10-17 Simon Josefsson diff --git a/lib/gc-libgcrypt.c b/lib/gc-libgcrypt.c index a7d2c17d9..5eb14e644 100644 --- a/lib/gc-libgcrypt.c +++ b/lib/gc-libgcrypt.c @@ -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; @@ -233,6 +345,12 @@ gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf) break; #endif +#ifdef GC_USE_RMD160 + case GC_RMD160: + gcryalg = GCRY_MD_RMD160; + break; +#endif + default: return GC_INVALID_HASH; } diff --git a/lib/gc.h b/lib/gc.h index e8bb299a4..cde3f155e 100644 --- a/lib/gc.h +++ b/lib/gc.h @@ -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 @@ -102,6 +112,17 @@ 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. The required size of OUT depends on HASH, and is generally -- 2.11.0