From: Simon Josefsson Date: Mon, 17 Oct 2005 13:00:51 +0000 (+0000) Subject: Add ciphers. X-Git-Tag: cvs-readonly~2803 X-Git-Url: http://erislabs.org.uk/gitweb/?a=commitdiff_plain;h=1ded427a5e0dba47b32257e484a29418cf789653;p=gnulib.git Add ciphers. --- diff --git a/lib/ChangeLog b/lib/ChangeLog index 188464590..c87f9dee1 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,5 +1,9 @@ 2005-10-17 Simon Josefsson + * gc.h, gc-libgcrypt.c: Add ciphers. + +2005-10-17 Simon Josefsson + * sha1.c: Use uint32_t instead of md5_uint32.t * sha1.h: Use stdint.h and uint32_t instead of md5_uint32 from diff --git a/lib/gc-libgcrypt.c b/lib/gc-libgcrypt.c index 349d5619b..a7d2c17d9 100644 --- a/lib/gc-libgcrypt.c +++ b/lib/gc-libgcrypt.c @@ -94,6 +94,124 @@ gc_set_allocators (gc_malloc_t func_malloc, func_realloc, func_free); } +/* Ciphers. */ + +Gc_rc +gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode, + gc_cipher_handle * outhandle) +{ + int gcryalg, gcrymode; + gcry_error_t err; + + switch (alg) + { + case GC_AES128: + gcryalg = GCRY_CIPHER_RIJNDAEL; + break; + + case GC_AES192: + gcryalg = GCRY_CIPHER_RIJNDAEL; + break; + + case GC_AES256: + gcryalg = GCRY_CIPHER_RIJNDAEL256; + break; + + case GC_3DES: + gcryalg = GCRY_CIPHER_3DES; + break; + + case GC_DES: + gcryalg = GCRY_CIPHER_DES; + break; + + case GC_ARCFOUR128: + case GC_ARCFOUR40: + gcryalg = GCRY_CIPHER_ARCFOUR; + break; + + case GC_ARCTWO40: + gcryalg = GCRY_CIPHER_RFC2268_40; + break; + + default: + return GC_INVALID_CIPHER; + } + + switch (mode) + { + case GC_CBC: + gcrymode = GCRY_CIPHER_MODE_CBC; + break; + + case GC_STREAM: + gcrymode = GCRY_CIPHER_MODE_STREAM; + break; + + default: + return GC_INVALID_CIPHER; + } + + err = gcry_cipher_open ((gcry_cipher_hd_t *) outhandle, + gcryalg, gcrymode, 0); + if (gcry_err_code (err)) + return GC_INVALID_CIPHER; + + return GC_OK; +} + +Gc_rc +gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key) +{ + gcry_error_t err; + + err = gcry_cipher_setkey ((gcry_cipher_hd_t) handle, key, keylen); + if (gcry_err_code (err)) + return GC_INVALID_CIPHER; + + return GC_OK; +} + +Gc_rc +gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv) +{ + gcry_error_t err; + + err = gcry_cipher_setiv ((gcry_cipher_hd_t) handle, iv, ivlen); + if (gcry_err_code (err)) + return GC_INVALID_CIPHER; + + return GC_OK; +} + +Gc_rc +gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data) +{ + if (gcry_cipher_encrypt ((gcry_cipher_hd_t) handle, + data, len, NULL, len) != 0) + return GC_INVALID_CIPHER; + + return GC_OK; +} + +Gc_rc +gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data) +{ + if (gcry_cipher_decrypt ((gcry_cipher_hd_t) handle, + data, len, NULL, len) != 0) + return GC_INVALID_CIPHER; + + return GC_OK; +} + +Gc_rc +gc_cipher_close (gc_cipher_handle handle) +{ + gcry_cipher_close (handle); + + return GC_OK; +} + /* Hashes. */ Gc_rc diff --git a/lib/gc.h b/lib/gc.h index 69e56a02e..e8bb299a4 100644 --- a/lib/gc.h +++ b/lib/gc.h @@ -49,6 +49,29 @@ typedef enum Gc_hash Gc_hash; #define GC_MD5_DIGEST_SIZE 16 #define GC_SHA1_DIGEST_SIZE 20 +/* Cipher types. */ +enum Gc_cipher + { + GC_AES128, + GC_AES192, + GC_AES256, + GC_3DES, + GC_DES, + GC_ARCFOUR128, + GC_ARCFOUR40, + GC_ARCTWO40 + }; +typedef enum Gc_cipher Gc_cipher; + +enum Gc_cipher_mode + { + GC_CBC, + GC_STREAM + }; +typedef enum Gc_cipher_mode Gc_cipher_mode; + +typedef void *gc_cipher_handle; + /* Call before respectively after any other functions. */ extern Gc_rc gc_init (void); extern void gc_done (void); @@ -64,6 +87,19 @@ extern void gc_set_allocators (gc_malloc_t func_malloc, gc_realloc_t func_realloc, gc_free_t func_free); +/* Ciphers. */ +extern Gc_rc gc_cipher_open (Gc_cipher cipher, Gc_cipher_mode mode, + gc_cipher_handle * outhandle); +extern Gc_rc gc_cipher_setkey (gc_cipher_handle handle, + size_t keylen, const char *key); +extern Gc_rc gc_cipher_setiv (gc_cipher_handle handle, + size_t ivlen, const char *iv); +extern Gc_rc gc_cipher_encrypt_inline (gc_cipher_handle handle, + size_t len, char *data); +extern Gc_rc gc_cipher_decrypt_inline (gc_cipher_handle handle, + size_t len, char *data); +extern Gc_rc gc_cipher_close (gc_cipher_handle handle); + /* Hashes. */ /* Compute a hash value over buffer IN of INLEN bytes size using the