1
// arc4.cpp - written and placed in the public domain by Wei Dai
3
// The ARC4 algorithm was first revealed in an anonymous email to the
4
// cypherpunks mailing list. This file originally contained some
5
// code copied from this email. The code has since been rewritten in order
6
// to clarify the copyright status of this file. It should now be
7
// completely in the public domain.
10
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
13
NAMESPACE_BEGIN(CryptoPP)
16
void ARC4_TestInstantiations()
21
ARC4_Base::~ARC4_Base()
26
void ARC4_Base::UncheckedSetKey(const byte *key, unsigned int keyLen, const NameValuePairs ¶ms)
28
AssertValidKeyLength(keyLen);
37
unsigned int keyIndex = 0, stateIndex = 0;
40
unsigned int a = m_state[i];
41
stateIndex += key[keyIndex] + a;
43
m_state[i] = m_state[stateIndex];
44
m_state[stateIndex] = a;
45
if (++keyIndex >= keyLen)
49
int discardBytes = params.GetIntValueWithDefault("DiscardBytes", GetDefaultDiscardBytes());
50
DiscardBytes(discardBytes);
54
static inline unsigned int MakeByte(T &x, T &y, byte *s)
56
unsigned int a = s[x];
58
unsigned int b = s[y];
62
return s[(a+b) & 0xff];
65
void ARC4_Base::GenerateBlock(byte *output, size_t size)
68
*output++ = MakeByte(m_x, m_y, m_state);
71
void ARC4_Base::ProcessData(byte *outString, const byte *inString, size_t length)
76
byte *const s = m_state;
80
if (inString == outString)
84
*outString++ ^= MakeByte(x, y, s);
91
*outString++ = *inString++ ^ MakeByte(x, y, s);
100
void ARC4_Base::DiscardBytes(size_t length)
105
byte *const s = m_state;
106
unsigned int x = m_x;
107
unsigned int y = m_y;