Back to index

scribus-ng  1.3.4.dfsg+svn20071115
rc4.c
Go to the documentation of this file.
00001 /*
00002  *  RC4 functions for HTMLDOC.
00003  *
00004  *   Original code by Tim Martin
00005  *   Copyright 1999 by Carnegie Mellon University, All Rights Reserved
00006  *
00007  *   Permission to use, copy, modify, and distribute this software and its
00008  *   documentation for any purpose and without fee is hereby granted,
00009  *   provided that the above copyright notice appear in all copies and that
00010  *   both that copyright notice and this permission notice appear in
00011  *   supporting documentation, and that the name of Carnegie Mellon
00012  *   University not be used in advertising or publicity pertaining to
00013  *   distribution of the software without specific, written prior
00014  *   permission.
00015  *
00016  *   CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
00017  *   THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
00018  *   FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR
00019  *   ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00020  *   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00021  *   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
00022  *   OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00023  *
00024  * Contents:
00025  *
00026  *   rc4_init()    - Initialize an RC4 context with the specified key.
00027  *   rc4_encrypt() - Encrypt the given buffer.
00028  */
00029 
00030 #include "rc4.h"
00031 
00032 
00033 /*
00034  * 'rc4_init()' - Initialize an RC4 context with the specified key.
00035  */
00036 
00037 void
00038 rc4_init(rc4_context_t       *text,       /* IO - Context */
00039          const unsigned char *key, /* I - Key */
00040          unsigned            keylen)      /* I - Length of key */
00041 {
00042   int         i, j;                /* Looping vars */
00043   unsigned char      tmp;                 /* Temporary variable */
00044 
00045 
00046  /*
00047   * Fill in linearly s0=0, s1=1, ...
00048   */
00049 
00050   for (i = 0; i < 256; i ++)
00051     text->sbox[i] = i;
00052 
00053   for (i = 0, j = 0; i < 256; i ++)
00054   {
00055    /*
00056     * j = (j + Si + Ki) mod 256
00057     */
00058 
00059     j = (j + text->sbox[i] + key[i % keylen]) & 255;
00060 
00061    /*
00062     * Swap Si and Sj...
00063     */
00064 
00065     tmp           = text->sbox[i];
00066     text->sbox[i] = text->sbox[j];
00067     text->sbox[j] = tmp;
00068   }
00069 
00070  /*
00071   * Initialized counters to 0 and return...
00072   */
00073 
00074   text->i = 0;
00075   text->j = 0;
00076 }
00077 
00078 
00079 /*
00080  * 'rc4_encrypt()' - Encrypt the given buffer.
00081  */
00082 
00083 void
00084 rc4_encrypt(rc4_context_t       *text,           /* I - Context */
00085            const unsigned char *input,           /* I - Input buffer */
00086            unsigned char       *output,   /* O - Output buffer */
00087            unsigned            len)              /* I - Size of buffers */
00088 {
00089   unsigned char             tmp;                 /* Swap variable */
00090   int                i, j;                /* Looping vars */
00091   int                t;                   /* Current S box */
00092 
00093 
00094  /*
00095   * Loop through the entire buffer...
00096   */
00097 
00098   i = text->i;
00099   j = text->j;
00100 
00101   while (len > 0)
00102   {
00103    /*
00104     * Get the next S box indices...
00105     */
00106 
00107     i = (i + 1) & 255;
00108     j = (j + text->sbox[i]) & 255;
00109 
00110    /*
00111     * Swap Si and Sj...
00112     */
00113 
00114     tmp           = text->sbox[i];
00115     text->sbox[i] = text->sbox[j];
00116     text->sbox[j] = tmp;
00117 
00118    /*
00119     * Get the S box index for this byte...
00120     */
00121 
00122     t = (text->sbox[i] + text->sbox[j]) & 255;
00123 
00124    /*
00125     * Encrypt using the S box...
00126     */
00127 
00128     *output++ = *input++ ^ text->sbox[t];
00129     len --;
00130   }
00131 
00132  /*
00133   * Copy current S box indices back to context...
00134   */
00135 
00136   text->i = i;
00137   text->j = j;
00138 }
00139 
00140 
00141 /*
00142  * End of "$Id: rc4.c 485 2004-10-20 21:05:49Z fschmid $".
00143  */