bug-gnulib
[Top][All Lists]
Advanced

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

gc-rijndael and gc-rijndael-tests


From: Simon Josefsson
Subject: gc-rijndael and gc-rijndael-tests
Date: Wed, 19 Oct 2005 16:52:21 +0200
User-agent: Gnus/5.110004 (No Gnus v0.4) Emacs/22.0.50 (gnu/linux)

I have installed this patch.

I realize gc-gnulib.c will end up with plenty of #ifdef's with this
approach.  I dislike that, but I don't have a good solution right now.
I believe it would be easiest if I continue to install patches until
GnuTLS work in gnulib-native mode (libgcrypt mode already works).
This is pretty close to working now.  Then we can re-think the
structure, and I will have a test case for the new ideas.

Index: m4/ChangeLog
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/ChangeLog,v
retrieving revision 1.748
diff -u -p -r1.748 ChangeLog
--- m4/ChangeLog        19 Oct 2005 08:37:44 -0000      1.748
+++ m4/ChangeLog        19 Oct 2005 14:51:19 -0000
@@ -1,5 +1,9 @@
 2005-10-19  Simon Josefsson  <address@hidden>
 
+       * gc-rijndael.m4: New file.
+
+2005-10-19  Simon Josefsson  <address@hidden>
+
        * m4/gc-hmac-md5.m4, m4/gc-hmac-sha1.m4, m4/gc-md4.m4,
        m4/gc-md5.m4, m4/gc-sha1.m4: Fix typo, suggested by Stepan Kasal
        <address@hidden>.
Index: m4/gc-rijndael.m4
===================================================================
RCS file: m4/gc-rijndael.m4
diff -N m4/gc-rijndael.m4
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ m4/gc-rijndael.m4   19 Oct 2005 14:51:19 -0000
@@ -0,0 +1,15 @@
+# gc-rijndael.m4 serial 1
+dnl Copyright (C) 2005 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_GC_RIJNDAEL],
+[
+  AC_REQUIRE([gl_GC])
+  AC_DEFINE(GC_USE_RIJNDAEL, 1,
+    [Define if you want to support RIJNDAEL through GC.])
+  if test "$ac_cv_libgcrypt" != yes; then
+    gl_RIJNDAEL
+  fi
+])
Index: lib/ChangeLog
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/ChangeLog,v
retrieving revision 1.1022
diff -u -p -r1.1022 ChangeLog
--- lib/ChangeLog       18 Oct 2005 23:37:20 -0000      1.1022
+++ lib/ChangeLog       19 Oct 2005 14:51:20 -0000
@@ -1,5 +1,10 @@
 2005-10-19  Simon Josefsson  <address@hidden>
 
+       * gc-gnulib.c: Implement gc_cipher_* API, currently only with AES
+       support.
+
+       * gc.h: Add ECB enum type.
+
        * hmac-md5.c, hmac-sha1.c: Include memxor.h.
 
 2005-10-19  Simon Josefsson  <address@hidden>
Index: lib/gc.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/gc.h,v
retrieving revision 1.13
diff -u -p -r1.13 gc.h
--- lib/gc.h    18 Oct 2005 23:35:51 -0000      1.13
+++ lib/gc.h    19 Oct 2005 14:51:20 -0000
@@ -77,6 +77,7 @@ typedef enum Gc_cipher Gc_cipher;
 
 enum Gc_cipher_mode
 {
+  GC_ECB,
   GC_CBC,
   GC_STREAM
 };
Index: lib/gc-gnulib.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/gc-gnulib.c,v
retrieving revision 1.8
diff -u -p -r1.8 gc-gnulib.c
--- lib/gc-gnulib.c     18 Oct 2005 23:35:51 -0000      1.8
+++ lib/gc-gnulib.c     19 Oct 2005 14:51:20 -0000
@@ -49,6 +49,9 @@
 #ifdef GC_USE_HMAC_MD5
 # include "hmac.h"
 #endif
+#ifdef GC_USE_RIJNDAEL
+# include "rijndael-api-fst.h"
+#endif
 
 Gc_rc
 gc_init (void)
@@ -143,6 +146,217 @@ gc_set_allocators (gc_malloc_t func_mall
                   gc_realloc_t func_realloc, gc_free_t func_free)
 {
   return;
+}
+/* Ciphers. */
+
+typedef struct _gc_cipher_ctx {
+  Gc_cipher alg;
+  Gc_cipher_mode mode;
+#ifdef GC_USE_RIJNDAEL
+  rijndaelKeyInstance aesEncKey;
+  rijndaelKeyInstance aesDecKey;
+  rijndaelCipherInstance aesContext;
+#endif
+} _gc_cipher_ctx;
+
+Gc_rc
+gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
+               gc_cipher_handle * outhandle)
+{
+  _gc_cipher_ctx *ctx;
+  Gc_rc rc = GC_OK;
+
+  ctx = calloc (sizeof (*ctx), 1);
+
+  ctx->alg = alg;
+  ctx->mode = mode;
+
+  switch (alg)
+    {
+#ifdef GC_USE_RIJNDAEL
+    case GC_AES128:
+    case GC_AES192:
+    case GC_AES256:
+      switch (mode)
+       {
+       case GC_ECB:
+       case GC_CBC:
+         break;
+
+       default:
+         rc = GC_INVALID_CIPHER;
+       }
+      break;
+#endif
+
+    default:
+      rc = GC_INVALID_CIPHER;
+    }
+
+  if (rc == GC_OK)
+    *outhandle = ctx;
+  else
+    free (ctx);
+
+  return rc;
+}
+
+Gc_rc
+gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
+{
+  _gc_cipher_ctx *ctx = handle;
+
+  switch (ctx->alg)
+    {
+#ifdef GC_USE_RIJNDAEL
+    case GC_AES128:
+    case GC_AES192:
+    case GC_AES256:
+      {
+       rijndael_rc rc;
+       size_t i;
+       char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1];
+
+       for (i = 0; i < keylen; i++)
+         sprintf (&keyMaterial[2*i], "%02x", key[i] & 0xFF);
+
+       rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT,
+                             keylen * 8, keyMaterial);
+       if (rc < 0)
+         return GC_INVALID_CIPHER;
+
+       rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT,
+                             keylen * 8, keyMaterial);
+       if (rc < 0)
+         return GC_INVALID_CIPHER;
+
+       rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL);
+       if (rc < 0)
+         return GC_INVALID_CIPHER;
+      }
+      break;
+#endif
+
+    default:
+      return GC_INVALID_CIPHER;
+    }
+
+  return GC_OK;
+}
+
+Gc_rc
+gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
+{
+  _gc_cipher_ctx *ctx = handle;
+
+  switch (ctx->alg)
+    {
+#ifdef GC_USE_RIJNDAEL
+    case GC_AES128:
+    case GC_AES192:
+    case GC_AES256:
+      switch (ctx->mode)
+       {
+       case GC_ECB:
+         /* Doesn't use IV. */
+         break;
+
+       case GC_CBC:
+         {
+           rijndael_rc rc;
+           size_t i;
+           char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1];
+
+           for (i = 0; i < ivlen; i++)
+             sprintf (&ivMaterial[2*i], "%02x", iv[i] & 0xFF);
+
+           rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC,
+                                    ivMaterial);
+           if (rc < 0)
+             return GC_INVALID_CIPHER;
+         }
+         break;
+
+       default:
+         return GC_INVALID_CIPHER;
+       }
+      break;
+#endif
+
+    default:
+      return GC_INVALID_CIPHER;
+    }
+
+  return GC_OK;
+}
+
+Gc_rc
+gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
+{
+  _gc_cipher_ctx *ctx = handle;
+
+  switch (ctx->alg)
+    {
+#ifdef GC_USE_RIJNDAEL
+    case GC_AES128:
+    case GC_AES192:
+    case GC_AES256:
+      {
+       int nblocks;
+
+       nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey,
+                                       data, 8 * len, data);
+       if (nblocks < 0)
+         return GC_INVALID_CIPHER;
+      }
+      break;
+#endif
+
+    default:
+      return GC_INVALID_CIPHER;
+    }
+
+  return GC_OK;
+}
+
+Gc_rc
+gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
+{
+  _gc_cipher_ctx *ctx = handle;
+
+  switch (ctx->alg)
+    {
+#ifdef GC_USE_RIJNDAEL
+    case GC_AES128:
+    case GC_AES192:
+    case GC_AES256:
+      {
+       int nblocks;
+
+       nblocks = rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey,
+                                       data, 8 * len, data);
+       if (nblocks < 0)
+         return GC_INVALID_CIPHER;
+      }
+      break;
+#endif
+
+    default:
+      return GC_INVALID_CIPHER;
+    }
+
+  return GC_OK;
+}
+
+Gc_rc
+gc_cipher_close (gc_cipher_handle handle)
+{
+  _gc_cipher_ctx *ctx = handle;
+
+  if (ctx)
+    free (ctx);
+
+  return GC_OK;
 }
 
 /* Hashes. */
Index: ChangeLog
===================================================================
RCS file: /cvsroot/gnulib/gnulib/ChangeLog,v
retrieving revision 1.430
diff -u -p -r1.430 ChangeLog
--- ChangeLog   18 Oct 2005 23:45:03 -0000      1.430
+++ ChangeLog   19 Oct 2005 14:51:20 -0000
@@ -1,5 +1,11 @@
 2005-10-19  Simon Josefsson  <address@hidden>
 
+       * tests/test-gc-rijndael.c: New file.
+
+       * modules/gc-rijndael, modules/gc-rijndael-test: New files.
+
+2005-10-19  Simon Josefsson  <address@hidden>
+
        * tests/test-gc-md4.c, tests/test-gc-md5.c: Test gc_hash_buffer
        interface too.
 
Index: modules/gc-rijndael
===================================================================
RCS file: modules/gc-rijndael
diff -N modules/gc-rijndael
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ modules/gc-rijndael 19 Oct 2005 14:51:20 -0000
@@ -0,0 +1,28 @@
+Description:
+Generic crypto wrappers for rijndael block cipher.
+
+Files:
+m4/gc-rijndael.m4
+lib/rijndael-alg-fst.c
+lib/rijndael-alg-fst.h
+lib/rijndael-api-fst.c
+lib/rijndael-api-fst.h
+m4/rijndael.m4
+
+Depends-on:
+stdint
+gc
+
+configure.ac:
+gl_GC_RIJNDAEL
+
+Makefile.am:
+
+Include:
+"gc.h"
+
+License:
+LGPL
+
+Maintainer:
+Simon Josefsson
Index: modules/gc-rijndael-tests
===================================================================
RCS file: modules/gc-rijndael-tests
diff -N modules/gc-rijndael-tests
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ modules/gc-rijndael-tests   19 Oct 2005 14:51:20 -0000
@@ -0,0 +1,11 @@
+Files:
+tests/test-gc-rijndael.c
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-gc-rijndael
+noinst_PROGRAMS += test-gc-rijndael
+test_gc_rijndael_SOURCES = test-gc-rijndael.c
Index: tests/test-gc-rijndael.c
===================================================================
RCS file: tests/test-gc-rijndael.c
diff -N tests/test-gc-rijndael.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ tests/test-gc-rijndael.c    19 Oct 2005 14:51:20 -0000
@@ -0,0 +1,167 @@
+/*
+ * Copyright (C) 2005 Free Software Foundation
+ * Written by Simon Josefsson
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdio.h>
+#include <string.h>
+#include "gc.h"
+
+int
+main (int argc, char *argv[])
+{
+  Gc_rc rc;
+
+  rc = gc_init ();
+  if (rc != GC_OK)
+    {
+      printf ("gc_init() failed\n");
+      return 1;
+    }
+
+  {
+    char buf[16];
+    char key[] = "\x00\x00\x00\x00\x00\x00\x00\x00"
+      "\x00\x00\x00\x00\x00\x00\x00\x00";
+    char pt[] = "\x00\x00\x00\x00\x00\x00\x00\x00"
+      "\x00\x00\x00\x00\x00\x00\x00\x00";
+    char ct[] = "\xC3\x4C\x05\x2C\xC0\xDA\x8D\x73"
+      "\x45\x1A\xFE\x5F\x03\xBE\x29\x7F";
+    gc_cipher_handle ctx;
+    size_t i;
+
+    rc = gc_cipher_open (GC_AES128, GC_ECB, &ctx);
+    if (rc != GC_OK)
+      return 1;
+
+    rc = gc_cipher_setkey (ctx, 16, key);
+    if (rc != GC_OK)
+      return 1;
+
+    memcpy (buf, pt, 16);
+
+    for (i = 0; i < 10000; i++)
+      {
+       rc = gc_cipher_encrypt_inline (ctx, 16, buf);
+       if (rc != GC_OK)
+         {
+           printf ("encrypt failed %d\n", rc);
+           return 1;
+         }
+      }
+
+    if (memcmp (buf, ct, 16) != 0)
+      {
+       size_t i;
+       printf ("expected:\n");
+       for (i = 0; i < 16; i++)
+         printf ("%02x ", ct[i] & 0xFF);
+       printf ("\ncomputed:\n");
+       for (i = 0; i < 16; i++)
+         printf ("%02x ", buf[i] & 0xFF);
+       printf ("\n");
+       return 1;
+      }
+
+    for (i = 0; i < 10000; i++)
+      {
+       rc = gc_cipher_decrypt_inline (ctx, 16, buf);
+       if (rc != GC_OK)
+         {
+           printf ("decrypt failed %d\n", rc);
+           return 1;
+         }
+      }
+
+    if (memcmp (buf, pt, 16) != 0)
+      {
+       size_t i;
+       printf ("expected:\n");
+       for (i = 0; i < 16; i++)
+         printf ("%02x ", pt[i] & 0xFF);
+       printf ("\ncomputed:\n");
+       for (i = 0; i < 16; i++)
+         printf ("%02x ", buf[i] & 0xFF);
+       printf ("\n");
+       return 1;
+      }
+
+    gc_cipher_close (ctx);
+  }
+
+
+  {
+    char buf[16];
+    char iv[] = "\x00\x00\x00\x00\x00\x00\x00\x00"
+      "\x00\x00\x00\x00\x00\x00\x00\x00";
+    char key[] = "\x00\x00\x00\x00\x00\x00\x00\x00"
+      "\x00\x00\x00\x00\x00\x00\x00\x00";
+    char pt[] = "\x00\x00\x00\x00\x00\x00\x00\x00"
+      "\x00\x00\x00\x00\x00\x00\x00\x00";
+    char ct[] = "\x66\xe9\x4b\xd4\xef\x8a\x2c\x3b"
+      "\x88\x4c\xfa\x59\xca\x34\x2b\x2e";
+    gc_cipher_handle ctx;
+    size_t i;
+
+    rc = gc_cipher_open (GC_AES128, GC_CBC, &ctx);
+    if (rc != GC_OK)
+      return 1;
+
+    rc = gc_cipher_setkey (ctx, 16, key);
+    if (rc != GC_OK)
+      return 1;
+
+    rc = gc_cipher_setiv (ctx, 16, iv);
+    if (rc != GC_OK)
+      return 1;
+
+    memcpy (buf, pt, 16);
+
+    for (i = 0; i < 10000; i++)
+      {
+       rc = gc_cipher_encrypt_inline (ctx, 16, buf);
+       if (rc != GC_OK)
+         {
+           printf ("encrypt failed %d\n", rc);
+           return 1;
+         }
+      }
+
+    if (memcmp (buf, ct, 16) != 0)
+      {
+       size_t i;
+       printf ("expected:\n");
+       for (i = 0; i < 16; i++)
+         printf ("%02x ", ct[i] & 0xFF);
+       printf ("\ncomputed:\n");
+       for (i = 0; i < 16; i++)
+         printf ("%02x ", buf[i] & 0xFF);
+       printf ("\n");
+       return 1;
+      }
+
+    gc_cipher_close (ctx);
+  }
+
+  gc_done ();
+
+  return 0;
+}




reply via email to

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