summaryrefslogtreecommitdiffstats
path: root/botan/src/stream/arc4
diff options
context:
space:
mode:
Diffstat (limited to 'botan/src/stream/arc4')
-rw-r--r--botan/src/stream/arc4/arc4.cpp105
-rw-r--r--botan/src/stream/arc4/arc4.h41
-rw-r--r--botan/src/stream/arc4/info.txt14
3 files changed, 160 insertions, 0 deletions
diff --git a/botan/src/stream/arc4/arc4.cpp b/botan/src/stream/arc4/arc4.cpp
new file mode 100644
index 0000000..0f78f73
--- /dev/null
+++ b/botan/src/stream/arc4/arc4.cpp
@@ -0,0 +1,105 @@
+/*
+* ARC4
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include <botan/arc4.h>
+#include <botan/xor_buf.h>
+#include <botan/parsing.h>
+
+namespace Botan {
+
+/*
+* Combine cipher stream with message
+*/
+void ARC4::cipher(const byte in[], byte out[], u32bit length)
+ {
+ while(length >= buffer.size() - position)
+ {
+ xor_buf(out, in, buffer.begin() + position, buffer.size() - position);
+ length -= (buffer.size() - position);
+ in += (buffer.size() - position);
+ out += (buffer.size() - position);
+ generate();
+ }
+ xor_buf(out, in, buffer.begin() + position, length);
+ position += length;
+ }
+
+/*
+* Generate cipher stream
+*/
+void ARC4::generate()
+ {
+ u32bit SX, SY;
+ for(u32bit j = 0; j != buffer.size(); j += 4)
+ {
+ SX = state[X+1]; Y = (Y + SX) % 256; SY = state[Y];
+ state[X+1] = SY; state[Y] = SX;
+ buffer[j] = state[(SX + SY) % 256];
+
+ SX = state[X+2]; Y = (Y + SX) % 256; SY = state[Y];
+ state[X+2] = SY; state[Y] = SX;
+ buffer[j+1] = state[(SX + SY) % 256];
+
+ SX = state[X+3]; Y = (Y + SX) % 256; SY = state[Y];
+ state[X+3] = SY; state[Y] = SX;
+ buffer[j+2] = state[(SX + SY) % 256];
+
+ X = (X + 4) % 256;
+ SX = state[X]; Y = (Y + SX) % 256; SY = state[Y];
+ state[X] = SY; state[Y] = SX;
+ buffer[j+3] = state[(SX + SY) % 256];
+ }
+ position = 0;
+ }
+
+/*
+* ARC4 Key Schedule
+*/
+void ARC4::key_schedule(const byte key[], u32bit length)
+ {
+ clear();
+ for(u32bit j = 0; j != 256; ++j)
+ state[j] = j;
+ for(u32bit j = 0, state_index = 0; j != 256; ++j)
+ {
+ state_index = (state_index + key[j % length] + state[j]) % 256;
+ std::swap(state[j], state[state_index]);
+ }
+ for(u32bit j = 0; j <= SKIP; j += buffer.size())
+ generate();
+ position += (SKIP % buffer.size());
+ }
+
+/*
+* Return the name of this type
+*/
+std::string ARC4::name() const
+ {
+ if(SKIP == 0) return "ARC4";
+ if(SKIP == 256) return "MARK-4";
+ else return "RC4_skip(" + to_string(SKIP) + ")";
+ }
+
+/*
+* Clear memory of sensitive data
+*/
+void ARC4::clear() throw()
+ {
+ state.clear();
+ buffer.clear();
+ position = X = Y = 0;
+ }
+
+/*
+* ARC4 Constructor
+*/
+ARC4::ARC4(u32bit s) : StreamCipher(1, 256), SKIP(s)
+ {
+ clear();
+ }
+
+}
diff --git a/botan/src/stream/arc4/arc4.h b/botan/src/stream/arc4/arc4.h
new file mode 100644
index 0000000..aa2cea7
--- /dev/null
+++ b/botan/src/stream/arc4/arc4.h
@@ -0,0 +1,41 @@
+/*
+* ARC4
+* (C) 1999-2008 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_ARC4_H__
+#define BOTAN_ARC4_H__
+
+#include <botan/stream_cipher.h>
+#include <botan/types.h>
+
+namespace Botan {
+
+/*
+* ARC4
+*/
+class BOTAN_DLL ARC4 : public StreamCipher
+ {
+ public:
+ void clear() throw();
+ std::string name() const;
+ StreamCipher* clone() const { return new ARC4(SKIP); }
+ ARC4(u32bit = 0);
+ ~ARC4() { clear(); }
+ private:
+ void cipher(const byte[], byte[], u32bit);
+ void key_schedule(const byte[], u32bit);
+ void generate();
+
+ const u32bit SKIP;
+
+ SecureBuffer<byte, DEFAULT_BUFFERSIZE> buffer;
+ SecureBuffer<u32bit, 256> state;
+ u32bit X, Y, position;
+ };
+
+}
+
+#endif
diff --git a/botan/src/stream/arc4/info.txt b/botan/src/stream/arc4/info.txt
new file mode 100644
index 0000000..e4689cf
--- /dev/null
+++ b/botan/src/stream/arc4/info.txt
@@ -0,0 +1,14 @@
+realname "ARC4"
+
+define ARC4
+
+load_on auto
+
+<requires>
+stream
+</requires>
+
+<add>
+arc4.cpp
+arc4.h
+</add>