Mercurial > hg > octave-lyh > gnulib-hg
changeset 6383:1cc68a6a8398
Add ciphers.
author | Simon Josefsson <simon@josefsson.org> |
---|---|
date | Mon, 17 Oct 2005 13:00:51 +0000 |
parents | d817a01950f3 |
children | a4b7c6c3f5bc |
files | lib/ChangeLog lib/gc-libgcrypt.c lib/gc.h |
diffstat | 3 files changed, 158 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,7 @@ +2005-10-17 Simon Josefsson <jas@extundo.com> + + * gc.h, gc-libgcrypt.c: Add ciphers. + 2005-10-17 Simon Josefsson <jas@extundo.com> * sha1.c: Use uint32_t instead of md5_uint32.t
--- a/lib/gc-libgcrypt.c +++ b/lib/gc-libgcrypt.c @@ -94,6 +94,124 @@ 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
--- a/lib/gc.h +++ b/lib/gc.h @@ -49,6 +49,29 @@ #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 @@ 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