bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

gc: cipher modes


From: Simon Josefsson
Subject: gc: cipher modes
Date: Mon, 17 Oct 2005 15:00:57 +0200
User-agent: Gnus/5.110004 (No Gnus v0.4) Emacs/22.0.50 (gnu/linux)

For libgcrypt only, the gnulib part is larger but will be added later.
I have installed this.

2005-10-17  Simon Josefsson  <address@hidden>

        * gc.h, gc-libgcrypt.c: Add ciphers.

Index: lib/gc.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/gc.h,v
retrieving revision 1.8
diff -u -p -r1.8 gc.h
--- lib/gc.h    12 Oct 2005 13:13:29 -0000      1.8
+++ lib/gc.h    17 Oct 2005 12:59:44 -0000
@@ -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);
@@ -63,6 +86,19 @@ extern void gc_set_allocators (gc_malloc
                               gc_secure_check_t secure_check,
                               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. */
 
Index: lib/gc-libgcrypt.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/gc-libgcrypt.c,v
retrieving revision 1.7
diff -u -p -r1.7 gc-libgcrypt.c
--- lib/gc-libgcrypt.c  12 Oct 2005 11:57:13 -0000      1.7
+++ lib/gc-libgcrypt.c  17 Oct 2005 12:59:44 -0000
@@ -94,6 +94,124 @@ gc_set_allocators (gc_malloc_t func_mall
                               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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]