Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

arc4.cpp

00001 // arc4.cpp - written and placed in the public domain by Wei Dai
00002 
00003 // The ARC4 algorithm was first revealed in an anonymous email to the
00004 // cypherpunks mailing list. This file originally contained some
00005 // code copied from this email. The code has since been rewritten in order
00006 // to clarify the copyright status of this file. It should now be
00007 // completely in the public domain.
00008 
00009 #include "pch.h"
00010 #include "arc4.h"
00011 
00012 NAMESPACE_BEGIN(CryptoPP)
00013 
00014 ARC4::ARC4(const byte *key, unsigned int keyLen)
00015         : m_state(256), m_x(0), m_y(0)
00016 {
00017         unsigned int i;
00018         for (i=0; i<256; i++)
00019                 m_state[i] = i;
00020 
00021         unsigned int keyIndex = 0, stateIndex = 0;
00022         for (i=0; i<256; i++)
00023         {
00024                 unsigned int a = m_state[i];
00025                 stateIndex += key[keyIndex] + a;
00026                 stateIndex &= 0xff;
00027                 m_state[i] = m_state[stateIndex];
00028                 m_state[stateIndex] = a;
00029                 if (++keyIndex >= keyLen)
00030                         keyIndex = 0;
00031         }
00032 }
00033 
00034 ARC4::~ARC4()
00035 {
00036         m_x=0;
00037         m_y=0;
00038 }
00039 
00040 byte ARC4::GenerateByte()
00041 {
00042         m_x = (m_x+1) & 0xff;
00043         unsigned int a = m_state[m_x];
00044         m_y = (m_y+a) & 0xff;
00045         unsigned int b = m_state[m_y];
00046         m_state[m_x] = b;
00047         m_state[m_y] = a;
00048         return m_state[(a+b) & 0xff];
00049 }
00050 
00051 byte ARC4::ProcessByte(byte input)
00052 {
00053         return input ^ ARC4::GenerateByte();
00054 }
00055 
00056 void ARC4::ProcessString(byte *outString, const byte *inString, unsigned int length)
00057 {
00058         byte *const s=m_state;
00059         unsigned int x = m_x;
00060         unsigned int y = m_y;
00061 
00062         while(length--)
00063         {
00064                 x = (x+1) & 0xff;
00065                 unsigned int a = s[x];
00066                 y = (y+a) & 0xff;
00067                 unsigned int b = s[y];
00068                 s[x] = b;
00069                 s[y] = a;
00070                 *outString++ = *inString++ ^ s[(a+b) & 0xff];
00071         }
00072 
00073         m_x = x;
00074         m_y = y;
00075 }
00076 
00077 void ARC4::ProcessString(byte *inoutString, unsigned int length)
00078 {
00079         byte *const s=m_state;
00080         unsigned int x = m_x;
00081         unsigned int y = m_y;
00082 
00083         while(length--)
00084         {
00085                 x = (x+1) & 0xff;
00086                 unsigned int a = s[x];
00087                 y = (y+a) & 0xff;
00088                 unsigned int b = s[y];
00089                 s[x] = b;
00090                 s[y] = a;
00091                 *inoutString++ ^= s[(a+b) & 0xff];
00092         }
00093 
00094         m_x = x;
00095         m_y = y;
00096 }
00097 
00098 MARC4::MARC4(const byte *userKey, unsigned int keyLength, unsigned int discardBytes)
00099         : ARC4(userKey, keyLength)
00100 {
00101         while (discardBytes--)
00102                 MARC4::GenerateByte();
00103 }
00104 
00105 NAMESPACE_END

Generated at Mon Jan 15 01:16:29 2001 for Crypto++ by doxygen1.2.4 written by Dimitri van Heesch, © 1997-2000