Add ciphers.
authorSimon Josefsson <simon@josefsson.org>
Mon, 17 Oct 2005 13:00:51 +0000 (13:00 +0000)
committerSimon Josefsson <simon@josefsson.org>
Mon, 17 Oct 2005 13:00:51 +0000 (13:00 +0000)
lib/ChangeLog
lib/gc-libgcrypt.c
lib/gc.h

index 1884645..c87f9de 100644 (file)
@@ -1,5 +1,9 @@
 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
 
        * sha1.h: Use stdint.h and uint32_t instead of md5_uint32 from
index 349d561..a7d2c17 100644 (file)
@@ -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
index 69e56a0..e8bb299 100644 (file)
--- 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