diff options
author | Simon Josefsson <simon@josefsson.org> | 2004-08-17 17:34:54 +0000 |
---|---|---|
committer | Simon Josefsson <simon@josefsson.org> | 2004-08-17 17:34:54 +0000 |
commit | b939d79bf762f3ee71451213f87077d480caadd5 (patch) | |
tree | 25205acf19b46fd5ba2766379892c7ccd89f673a | |
parent | 5870597941127a4d0e294616741bd1140b025e33 (diff) | |
download | gnutls-b939d79bf762f3ee71451213f87077d480caadd5.tar.gz |
Add one-call interface.
-rw-r--r-- | crypto/gc-libgcrypt.c | 62 | ||||
-rw-r--r-- | crypto/gc-nettle.c | 50 | ||||
-rw-r--r-- | crypto/gc.h | 10 |
3 files changed, 122 insertions, 0 deletions
diff --git a/crypto/gc-libgcrypt.c b/crypto/gc-libgcrypt.c index 480e2432f4..e581960d11 100644 --- a/crypto/gc-libgcrypt.c +++ b/crypto/gc-libgcrypt.c @@ -32,6 +32,8 @@ /* Get libgcrypt API. */ #include <gcrypt.h> +#include <assert.h> + /* Initialization. */ int @@ -318,3 +320,63 @@ gc_hash_close (gc_hash handle) { gcry_md_close ((gcry_md_hd_t) handle); } + +int +gc_md5 (const char *in, size_t inlen, char out[GC_MD5_LEN]) +{ + size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD5); + gcry_md_hd_t hd; + gpg_error_t err; + unsigned char *p; + + assert (outlen == GC_MD5_LEN); + + err = gcry_md_open (&hd, GCRY_MD_MD5, 0); + if (err != GPG_ERR_NO_ERROR) + return GC_INVALID_HASH; + + gcry_md_write (hd, in, inlen); + + p = gcry_md_read (hd, GCRY_MD_MD5); + if (p == NULL) + return GC_INVALID_HASH; + + memcpy (out, p, outlen); + + gcry_md_close (hd); + + return GC_OK; +} + +int +gc_hmac_md5 (const char *key, size_t keylen, + const char *in, size_t inlen, + char outhash[GC_MD5_LEN]) +{ + size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_MD5); + gcry_md_hd_t mdh; + unsigned char *hash; + gpg_error_t err; + + assert (hlen == GC_MD5_LEN); + + err = gcry_md_open (&mdh, GCRY_MD_MD5, 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_MD5); + 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 a56f90f17c..ab8fc2700c 100644 --- a/crypto/gc-nettle.c +++ b/crypto/gc-nettle.c @@ -502,3 +502,53 @@ gc_hash_close (gc_hash handle) free (hinf->context); free (hinf); } + +/** + * 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. + * + * Compute hash of data using MD5. The @out buffer must be + * deallocated by the caller. + * + * Return value: Returns %GC_OK iff successful. + **/ +int +gc_md5 (const char *in, size_t inlen, char out[GC_MD5_LEN]) +{ + struct md5_ctx md5; + + md5_init (&md5); + md5_update (&md5, inlen, in); + md5_digest (&md5, GC_MD5_LEN, out); + + return GC_OK; +} + +/** + * gc_hmac_md5: + * @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: newly 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. + * + * Return value: Returns %GC_OK iff successful. + **/ +int +gc_hmac_md5 (const char *key, size_t keylen, + const char *in, size_t inlen, + char outhash[GC_MD5_LEN]) +{ + struct hmac_md5_ctx ctx; + + hmac_md5_set_key (&ctx, keylen, key); + hmac_md5_update (&ctx, inlen, in); + hmac_md5_digest (&ctx, GC_MD5_LEN, outhash); + + return GC_OK; +} diff --git a/crypto/gc.h b/crypto/gc.h index 6e255b7a06..f8a9d79ec0 100644 --- a/crypto/gc.h +++ b/crypto/gc.h @@ -26,6 +26,8 @@ /* Get size_t. */ #include <stddef.h> +#define GC_MD5_LEN 16 + enum Gc_rc { GC_OK = 0, @@ -76,6 +78,7 @@ typedef void *gc_cipher; extern int gc_init (void); extern void gc_done (void); +/* Randomness. */ extern int gc_nonce (char *data, size_t datalen); extern int gc_pseudo_random (char *data, size_t datalen); extern int gc_random (char *data, size_t datalen); @@ -111,4 +114,11 @@ extern void gc_hash_write (gc_hash handle, size_t len, const char *data); extern const char *gc_hash_read (gc_hash handle); extern void gc_hash_close (gc_hash handle); +/* One-call interface. */ +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]); + #endif /* GC_H */ |