bug-gnulib
[Top][All Lists]
Advanced

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

Re: arcfour


From: Simon Josefsson
Subject: Re: arcfour
Date: Fri, 14 Oct 2005 16:14:09 +0200
User-agent: Gnus/5.110004 (No Gnus v0.4) Emacs/22.0.50 (gnu/linux)

Ralf Wildenhues <address@hidden> writes:

> * Simon Josefsson wrote on Fri, Oct 14, 2005 at 03:57:43PM CEST:
>> Ralf Wildenhues <address@hidden> writes:
>> 
>> > Furthermore, if I may suggest not to sprinkle hard-coded numbers all
>> > over the place.  How about something like this
>> >   #define ARCFOUR_BLOCKBITS 8
>> >   #define ARCFOUR_BLOCKSIZE (1 << ARCFOUR_BLOCKBITS)
>> >   #define ARCFOUR_BLOCKMASK (ARCFOUR_BLOCKSIZE - 1)
>> 
>> Wasn't that backwards?  Shouldn't it be:
>> 
>>    #define ARCFOUR_BLOCK_SIZE 8
>>    #define ARCFOUR_BLOCK_BITS (1 << ARCFOUR_BLOCKSIZE)
>>    #define ARCFOUR_BLOCK_MASK (ARCFOUR_BLOCKBITS - 1)
>> 
>> ?
>> 
>> Symbols called *SIZE are usually measured in bytes.
>
> Come to think of it, I'd probably call the first one LOGSZ
> (or LOGSIZE, FWIW), and the second SIZE.  Calling the second
> BITS sounds wrong to me, nothing here has 256 bits.

I thought about this more. There are only two defines that I believe
should be present in arcfour.h:

#define ARCFOUR_BLOCK_SIZE 8
#define ARCFOUR_SBOX_SIZE 256

Internally, in arcfour.c, because we are using "X AND 255" instead of
"X AND ARCFOUR_SBOX_SIZE" we can use:

#define ARCFOUR_MOD_MASK (ARCFOUR_SBOX_SIZE - 1)

And then use that symbol in the code.

The ARCFOUR_MOD_MASK symbol doesn't represent an external property of
the cipher, so I don't think it should be in arcfour.h.

Here is the entire revised module again.

What do you think?

Index: ChangeLog
===================================================================
RCS file: /cvsroot/gnulib/gnulib/ChangeLog,v
retrieving revision 1.419
diff -u -p -r1.419 ChangeLog
--- ChangeLog   14 Oct 2005 00:59:45 -0000      1.419
+++ ChangeLog   14 Oct 2005 14:13:11 -0000
@@ -1,3 +1,9 @@
+2005-10-14  Simon Josefsson  <address@hidden>
+
+       * tests/test-arcfour.c: New file.
+
+       * modules/arcfour, modules/arcfour-tests: New files.
+
 2005-10-13  Oskar Liljeblad  <address@hidden>
 
         * modules/human (Depends-on): Depend on xstrtoumax, not xstrtol.
Index: modules/arcfour
===================================================================
RCS file: modules/arcfour
diff -N modules/arcfour
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ modules/arcfour     14 Oct 2005 14:13:11 -0000
@@ -0,0 +1,23 @@
+Description:
+ARCFOUR stream cipher implementation
+
+Files:
+lib/arcfour.h
+lib/arcfour.c
+m4/arcfour.m4
+
+Depends-on:
+
+configure.ac:
+gl_ARCFOUR
+
+Makefile.am:
+
+Include:
+"arcfour.h"
+
+License:
+LGPL
+
+Maintainer:
+Simon Josefsson
Index: modules/arcfour-tests
===================================================================
RCS file: modules/arcfour-tests
diff -N modules/arcfour-tests
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ modules/arcfour-tests       14 Oct 2005 14:13:11 -0000
@@ -0,0 +1,11 @@
+Files:
+tests/test-arcfour.c
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-arcfour
+noinst_PROGRAMS += test-arcfour
+test_arcfour_SOURCES = test-arcfour.c
Index: m4/ChangeLog
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/ChangeLog,v
retrieving revision 1.741
diff -u -p -r1.741 ChangeLog
--- m4/ChangeLog        13 Oct 2005 12:20:32 -0000      1.741
+++ m4/ChangeLog        14 Oct 2005 14:13:12 -0000
@@ -1,3 +1,7 @@
+2005-10-14  Simon Josefsson  <address@hidden>
+
+       * arcfour.m4: New file.
+
 2005-10-12  Bruno Haible  <address@hidden>
 
        * stdbool.m4 (gl_STDBOOL_H): Define as an alias of AM_STDBOOL_H.
Index: m4/arcfour.m4
===================================================================
RCS file: m4/arcfour.m4
diff -N m4/arcfour.m4
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ m4/arcfour.m4       14 Oct 2005 14:13:12 -0000
@@ -0,0 +1,11 @@
+# arcfour.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_ARCFOUR],
+[
+  AC_LIBSOURCES([arcfour.c, arcfour.h])
+  AC_LIBOBJ([arcfour])
+])
Index: lib/ChangeLog
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/ChangeLog,v
retrieving revision 1.1011
diff -u -p -r1.1011 ChangeLog
--- lib/ChangeLog       13 Oct 2005 07:49:05 -0000      1.1011
+++ lib/ChangeLog       14 Oct 2005 14:13:13 -0000
@@ -1,3 +1,7 @@
+2005-10-14  Simon Josefsson  <address@hidden>
+
+       * arcfour.h, arcfour.c: New files.
+
 2005-10-13  Simon Josefsson  <address@hidden>
 
        * gc-pbkdf2-sha1.c (gc_pbkdf2_sha1): Optimize CEIL computation.
Index: lib/arcfour.c
===================================================================
RCS file: lib/arcfour.c
diff -N lib/arcfour.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ lib/arcfour.c       14 Oct 2005 14:13:13 -0000
@@ -0,0 +1,84 @@
+/* arcfour.c --- The arcfour stream cipher
+ * Copyright (C) 2000, 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
+ *
+ * This file 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 file 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 file; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ */
+
+/* Code from Libgcrypt adapted for gnulib by Simon Josefsson. */
+
+/*
+ * For a description of the algorithm, see:
+ *   Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1996.
+ *   ISBN 0-471-11709-9. Pages 397 ff.
+ */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "arcfour.h"
+
+/* The and-op seems to be faster than the mod-op, so use it throughout
+   the code instead of mod-op.  MOD 256 is the same as AND 255, hence
+   this definition. */
+#define ARCFOUR_MOD_MASK (ARCFOUR_SBOX_SIZE - 1)
+
+void
+arcfour_stream (arcfour_context * context, const char *inbuf, char *outbuf,
+               size_t length)
+{
+  register size_t i = context->idx_i;
+  register size_t j = context->idx_j;
+  register char *sbox = context->sbox;
+
+  for (; length > 0; length--)
+    {
+      register char t;
+
+      i++;
+      i &= ARCFOUR_MOD_MASK;
+      j += sbox[i];
+      j &= ARCFOUR_MOD_MASK;
+      t = sbox[i];
+      sbox[i] = sbox[j];
+      sbox[j] = t;
+      *outbuf++ = *inbuf++ ^ sbox[(sbox[i] + sbox[j]) & ARCFOUR_MOD_MASK];
+    }
+
+  context->idx_i = i;
+  context->idx_j = j;
+}
+
+void
+arcfour_setkey (arcfour_context * context, const char *key, size_t keylen)
+{
+  size_t i, j, k;
+
+  context->idx_i = context->idx_j = 0;
+  for (i = 0; i < ARCFOUR_SBOX_SIZE; i++)
+    context->sbox[i] = i;
+  for (i = j = k = 0; i < ARCFOUR_SBOX_SIZE; i++)
+    {
+      char t;
+      j = (j + context->sbox[i] + key[k]) & ARCFOUR_MOD_MASK;
+      t = context->sbox[i];
+      context->sbox[i] = context->sbox[j];
+      context->sbox[j] = t;
+      if (++k == keylen)
+       k = 0;
+    }
+}
Index: lib/arcfour.h
===================================================================
RCS file: lib/arcfour.h
diff -N lib/arcfour.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ lib/arcfour.h       14 Oct 2005 14:13:13 -0000
@@ -0,0 +1,52 @@
+/* arcfour.h --- The arcfour stream cipher
+ * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
+ *    Free Software Foundation, Inc.
+ *
+ * This file 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 file 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 file; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ */
+
+/* Code from Libgcrypt adapted for gnulib by Simon Josefsson. */
+
+#ifndef ARCFOUR_H
+# define ARCFOUR_H
+
+# include <stddef.h>
+
+#define ARCFOUR_BLOCK_SIZE 8
+
+#define ARCFOUR_SBOX_SIZE 256
+
+typedef struct
+{
+  size_t idx_i, idx_j;
+  char sbox[ARCFOUR_SBOX_SIZE];
+} arcfour_context;
+
+/* Apply ARCFOUR stream to INBUF placing the result in OUTBUF, both of
+   LENGTH size.  CONTEXT must be initialized with arcfour_setkey
+   before this function is called. */
+extern void
+arcfour_stream (arcfour_context * context,
+               const char *inbuf, char *outbuf, size_t length);
+
+/* Initialize CONTEXT using encryption KEY of KEYLEN bytes.  KEY
+   should be 40 bits (5 bytes) or longer.  The KEY cannot be zero
+   length.  */
+extern void
+arcfour_setkey (arcfour_context * context, const char *key, size_t keylen);
+
+#endif /* ARCFOUR_H */
Index: tests/test-arcfour.c
===================================================================
RCS file: tests/test-arcfour.c
diff -N tests/test-arcfour.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ tests/test-arcfour.c        14 Oct 2005 14:13:13 -0000
@@ -0,0 +1,73 @@
+/*
+ * 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 "arcfour.h"
+
+int
+main (int argc, char *argv[])
+{
+  arcfour_context ctx;
+  /* Test vector from Cryptlib via Libgcrypt labeled there: "from the
+     State/Commerce Department". */
+  static char key_1[] = { 0x61, 0x8A, 0x63, 0xD2, 0xFB };
+  static char plaintext_1[] = { 0xDC, 0xEE, 0x4C, 0xF9, 0x2C };
+  static const char ciphertext_1[] = { 0xF1, 0x38, 0x29, 0xC9, 0xDE };
+  char scratch[16];
+
+  arcfour_setkey (&ctx, key_1, sizeof (key_1));
+  arcfour_stream (&ctx, plaintext_1, scratch, sizeof (plaintext_1));
+  if (memcmp (scratch, ciphertext_1, sizeof (ciphertext_1)))
+    {
+      size_t i;
+      printf ("expected:\n");
+      for (i = 0; i < 5; i++)
+       printf ("%02x ", scratch[i] & 0xFF);
+      printf ("\ncomputed:\n");
+      for (i = 0; i < 5; i++)
+       printf ("%02x ", ciphertext_1[i] & 0xFF);
+      printf ("\n");
+      return 1;
+    }
+
+  /* decrypt */
+
+  arcfour_setkey (&ctx, key_1, sizeof (key_1));
+  arcfour_stream (&ctx, scratch, scratch, sizeof (plaintext_1));
+  if (memcmp (scratch, plaintext_1, sizeof (plaintext_1)))
+    {
+      size_t i;
+      printf ("expected:\n");
+      for (i = 0; i < 5; i++)
+       printf ("%02x ", plaintext_1[i] & 0xFF);
+      printf ("\ncomputed:\n");
+      for (i = 0; i < 5; i++)
+       printf ("%02x ", scratch[i] & 0xFF);
+      printf ("\n");
+      return 1;
+    }
+
+
+  return 0;
+}




reply via email to

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