[PATCH] RSA RC4

Rüdiger Sonderfeld cplusplushelp@gmx.net
Fri, 6 Dec 2002 23:13:38 +0100


ups, the rc4.c code was a little bit old here is the newest version :)

I didn't know if I'm allowed to use inline because it is a very new C feature 
(but very useful)

/* rc4.c - 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

static void
burn_stack (int bytes)
{
  char buf[64];
    
  memset (buf, 0, sizeof buf);
  bytes -= sizeof buf;
  if (bytes > 0)
    burn_stack (bytes);
}

static /*inline*/ 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);
  }
  return 0;
}

static int
rc4_setkey (RC4_context *ctx, const byte *key, unsigned int keylen)
{
  int rc = do_rc4_setkey (ctx, key, keylen);
  burn_stack (2*sizeof(u32)+256);
  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 (256+4*sizeof(u32));
}

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;
}