From 13083d5caa5989dab4238adce19a2fa55efc4201 Mon Sep 17 00:00:00 2001 From: Simon Josefsson Date: Sat, 15 Oct 2005 18:19:44 +0000 Subject: [PATCH] Add arcfour module. --- ChangeLog | 6 ++++ lib/ChangeLog | 4 +++ lib/arcfour.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/arcfour.h | 50 ++++++++++++++++++++++++++++++++ m4/ChangeLog | 4 +++ m4/arcfour.m4 | 11 +++++++ modules/arcfour | 23 +++++++++++++++ modules/arcfour-tests | 11 +++++++ tests/test-arcfour.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 261 insertions(+) create mode 100644 lib/arcfour.c create mode 100644 lib/arcfour.h create mode 100644 m4/arcfour.m4 create mode 100644 modules/arcfour create mode 100644 modules/arcfour-tests create mode 100644 tests/test-arcfour.c diff --git a/ChangeLog b/ChangeLog index cbcfac02a..0eb6cd303 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2005-10-14 Simon Josefsson + + * tests/test-arcfour.c: New file. + + * modules/arcfour, modules/arcfour-tests: New files. + 2005-10-13 Oskar Liljeblad * modules/human (Depends-on): Depend on xstrtoumax, not xstrtol. diff --git a/lib/ChangeLog b/lib/ChangeLog index d3dce493f..d6ef8a8ce 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,7 @@ +2005-10-14 Simon Josefsson + + * arcfour.h, arcfour.c: New files. + 2005-10-14 Paul Eggert * obstack.c [defined _LIBC && defined USE_IN_LIBIO]: Don't diff --git a/lib/arcfour.c b/lib/arcfour.c new file mode 100644 index 000000000..61b851127 --- /dev/null +++ b/lib/arcfour.c @@ -0,0 +1,79 @@ +/* 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 +#endif + +#include "arcfour.h" + +void +arcfour_stream (arcfour_context * context, const char *inbuf, char *outbuf, + size_t length) +{ + size_t i = context->idx_i; + size_t j = context->idx_j; + char *sbox = context->sbox; + + for (; length > 0; length--) + { + char t; + + i = (i + 1) % ARCFOUR_SBOX_SIZE; + j = (j + sbox[i]) % ARCFOUR_SBOX_SIZE; + t = sbox[i]; + sbox[i] = sbox[j]; + sbox[j] = t; + *outbuf++ = (*inbuf++ + ^ sbox[(0U + sbox[i] + sbox[j]) % ARCFOUR_SBOX_SIZE]); + } + + 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; + char *sbox = context->sbox; + + context->idx_i = context->idx_j = 0; + for (i = 0; i < ARCFOUR_SBOX_SIZE; i++) + sbox[i] = i; + for (i = j = k = 0; i < ARCFOUR_SBOX_SIZE; i++) + { + char t; + j = (j + sbox[i] + key[k]) % ARCFOUR_SBOX_SIZE; + t = sbox[i]; + sbox[i] = sbox[j]; + sbox[j] = t; + if (++k == keylen) + k = 0; + } +} diff --git a/lib/arcfour.h b/lib/arcfour.h new file mode 100644 index 000000000..28ef679d0 --- /dev/null +++ b/lib/arcfour.h @@ -0,0 +1,50 @@ +/* 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 + +#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 */ diff --git a/m4/ChangeLog b/m4/ChangeLog index f51c2c936..192d1521c 100644 --- a/m4/ChangeLog +++ b/m4/ChangeLog @@ -1,3 +1,7 @@ +2005-10-14 Simon Josefsson + + * arcfour.m4: New file. + 2005-10-12 Bruno Haible * stdbool.m4 (gl_STDBOOL_H): Define as an alias of AM_STDBOOL_H. diff --git a/m4/arcfour.m4 b/m4/arcfour.m4 new file mode 100644 index 000000000..17150e616 --- /dev/null +++ b/m4/arcfour.m4 @@ -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]) +]) diff --git a/modules/arcfour b/modules/arcfour new file mode 100644 index 000000000..328fcbd28 --- /dev/null +++ b/modules/arcfour @@ -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 diff --git a/modules/arcfour-tests b/modules/arcfour-tests new file mode 100644 index 000000000..9e9fff7d0 --- /dev/null +++ b/modules/arcfour-tests @@ -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 diff --git a/tests/test-arcfour.c b/tests/test-arcfour.c new file mode 100644 index 000000000..9540d0141 --- /dev/null +++ b/tests/test-arcfour.c @@ -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 +#endif + +#include +#include +#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; +} -- 2.11.0