[PATCH] RSA RC4
Rüdiger Sonderfeld
cplusplushelp@gmx.net
Fri, 6 Dec 2002 23:04:36 +0100
I wrote a implementation of RSA RC4 for the libgcrypt. I hope the
implementation is usefull and bug free :)
There are some security problems with RSA RC4
http://www.wisdom.weizmann.ac.il/~itsik/RC4/rc4.html
But RSA RC4 (was) used in a lot of software!
BTW.
What do you think about a Modul for IBM MARS? It was an AES Final Candidate
and after the XSL problem of Rijndael it could be interessting to use it
cipher/ChangeLog
2002-12-06 Ruediger Sonderfeld <cplusplushelp@gmx.net>
* rc4.c: New
* rc4.h: New
* Makefile.am (EXTRA_PROGRAMS): Add rc4.c and rc4.h
* cipher.c (setup_cipher_table): load rc4 modul
src/ChangeLog
2002-12-06 Ruediger Sonderfeld <cplusplushelp@gmx.net>
* gcrypt.h: Added rc4 number
Index: cipher/Makefile.am
===================================================================
RCS file: /cvs/gnupg/libgcrypt/cipher/Makefile.am,v
retrieving revision 1.67
diff -r1.67 Makefile.am
70c70,71
< construct.c
---
> construct.c \
> rc4.c rc4.h
Index: cipher/cipher.c
===================================================================
RCS file: /cvs/gnupg/libgcrypt/cipher/cipher.c,v
retrieving revision 1.47
diff -r1.47 cipher.c
29a30
> #include "rc4.h"
37c38,39
< #define TABLE_SIZE 14
---
> #define TABLE_SIZE 15
133c135,146
< i = 0;
---
> i = 0;
> cipher_table[i].algo = GCRY_CIPHER_RC4;
> cipher_table[i].name = _gcry_rc4_get_info( cipher_table[i].algo,
> &cipher_table[i].keylen,
> &cipher_table[i].blocksize,
> &cipher_table[i].contextsize,
> &cipher_table[i].setkey,
> &cipher_table[i].stencrypt,
> &cipher_table[i].stdecrypt );
> if( !cipher_table[i].name )
> BUG();
> i++;
Index: src/gcrypt.h
===================================================================
RCS file: /cvs/gnupg/libgcrypt/src/gcrypt.h,v
retrieving revision 1.64
diff -r1.64 gcrypt.h
40c40
< #define GCRYPT_VERSION "1.1.10"
---
> #define GCRYPT_VERSION "1.1.7"
485c485,487
< GCRY_CIPHER_DES = 302
---
> GCRY_CIPHER_DES = 302,
> GCRY_CIPHER_RC4 = 303
rc4.c
/* rc4.h - RSA Security RC4 (Ron's Code #4)
* Copyright (C) 2002 Free Software Foundation, Inc.
*
* This file is part of Libgcrypt.
*
* Libgcrypt is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser general Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* Libgcrypt 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
* This implementation is based on the RC4 Description from
* http://www.ncat.edu/~grogans/main.htm
*
* It is written by Ruediger Sonderfeld <cplusplushelp@gmx.net>
*/
#include <config.h>
#include <string.h>
#include "types.h"
#include "g10lib.h"
#include "dynload.h"
#include "rc4.h"
#define CIPHER_ALGO_RC4 303
typedef struct {
byte sbox[256];
} RC4_context;
static void
burn_stack (int bytes)
{
char buf[64];
memset (buf, 0, sizeof buf);
bytes -= sizeof buf;
if (bytes > 0)
burn_stack (bytes);
}
static void
swap_bytes (byte *a, byte *b)
{
byte tmp=*b;
*b=*a;
*a=tmp;
}
/* Perform the key setup. */
static int
do_rc4_setkey (RC4_context *ctx, const byte *key, const unsigned keylen)
{
u32 i,j; /*iterators*/
byte k[256];/*key*/
for(i=0; i<=255 ; ++i) {
ctx->sbox[i]=i; /*fill sbox*/
k[i]=key[i%keylen]; /*fill key*/
}
for(i=0; i<=255 ; ++i) {
j = ( ctx->sbox[i] + k[i] + j ) % 256;
swap_bytes(ctx->sbox+i,ctx->sbox+j);
}
}
static int
rc4_setkey (RC4_context *ctx, const byte *key, unsigned int keylen)
{
int rc = do_rc4_setkey (ctx, key, keylen);
burn_stack (/*[TODO] ...*/1);
return rc;
}
static void
do_rc4_stcrypt (const RC4_context *ctx, byte *out, const byte *in, unsigned
int length)
{
u32 i=0,j=0,l,t=0; /*iterators*/
byte sbox[256];
memcpy(sbox,ctx->sbox,256);
for(; t<length ; ++t) {
i=(i + 1) % 256;
j=(j + sbox[i]) % 256;
swap_bytes(sbox+i,sbox+j);
l=(ctx->sbox[i] + ctx->sbox[j]) % 256;
out[t] = in[t] ^ ctx->sbox[l];
}
}
static void
rc4_stcrypt (const RC4_context *ctx, byte *out, const byte *in, unsigned int
length)
{
do_rc4_stcrypt (ctx, out, in, length);
burn_stack (/*[TODO] ...*/1);
}
const char *
_gcry_rc4_get_info( int algo, size_t *keylen,
size_t *blocksize, size_t *contextsize,
int (**r_setkey)( void *c, byte *key, unsigned keylen ),
void (**r_stencrypt)( void *c, byte *outbuf, byte *inbuf,
unsigned ),
void (**r_stdecrypt)( void *c, byte *outbuf, byte *inbuf,
unsigned )
)
{
*keylen = 2048; /*max!*/
*blocksize = 1;
*contextsize = sizeof(RC4_context);
*(int (**)(RC4_context*, const byte*, unsigned))r_setkey
= rc4_setkey;
*(void (**)(const RC4_context*, byte*, const byte*, unsigned))r_stencrypt
= rc4_stcrypt;
*(void (**)(const RC4_context*, byte*, const byte*, unsigned))r_stdecrypt
= rc4_stcrypt;
if(algo==CIPHER_ALGO_RC4)
return "RC4";
return NULL;
}
rc4.h
/* rc4.h - RSA Security RC4 (Ron's Code #4)
* Copyright (C) 2002 Free Software Foundation, Inc.
*
* This file is part of Libgcrypt.
*
* Libgcrypt is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser general Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* Libgcrypt 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
* This implementation is based on the RC4 Description from
* http://www.ncat.edu/~grogans/main.htm
*
* It is written by Ruediger Sonderfeld <cplusplushelp@gmx.net>
*/
#ifndef G10_RC4_H
#define G10_RC4_H
#include "types.h"
const char *
_gcry_rc4_get_info( int algo, size_t *keylen,
size_t *blocksize, size_t *contextsize,
int (**setkeyf)( void *c, byte *key, unsigned keylen ),
void (**stencryptf)( void *c, byte *outbuf, byte *inbuf,
unsigned ),
void (**stdecryptf)( void *c, byte *outbuf, byte *inbuf,
unsigned )
);
#endif /*G10_RC4_H*/