2 // ARC4Managed.cs: Alleged RC4(tm) compatible symmetric stream cipher
3 // RC4 is a trademark of RSA Security
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System.Globalization;
29 using System.Security.Cryptography;
31 namespace Mono.Security.Cryptography {
34 // a. Usenet 1994 - RC4 Algorithm revealed
35 // http://www.qrst.de/html/dsds/rc4.htm
42 class ARC4Managed : RC4, ICryptoTransform {
48 private bool m_disposed;
50 public ARC4Managed () : base ()
52 state = new byte [256];
61 protected override void Dispose (bool disposing)
67 Array.Clear (key, 0, key.Length);
70 Array.Clear (state, 0, state.Length);
72 GC.SuppressFinalize (this);
77 public override byte[] Key {
81 return (byte[]) KeyValue.Clone ();
85 throw new ArgumentNullException ("Key");
86 KeyValue = key = (byte[]) value.Clone ();
91 public bool CanReuseTransform {
95 public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgvIV)
98 return (ICryptoTransform) this;
101 public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgvIV)
104 return CreateEncryptor ();
107 public override void GenerateIV ()
109 // not used for a stream cipher
113 public override void GenerateKey ()
115 KeyValue = KeyBuilder.Key (KeySizeValue >> 3);
118 public bool CanTransformMultipleBlocks {
122 public int InputBlockSize {
126 public int OutputBlockSize {
130 private void KeySetup (byte[] key)
135 for (int counter = 0; counter < 256; counter++)
136 state [counter] = (byte) counter;
139 for (int counter = 0; counter < 256; counter++) {
140 index2 = (byte) (key [index1] + state [counter] + index2);
142 byte tmp = state [counter];
143 state [counter] = state [index2];
144 state [index2] = tmp;
145 index1 = (byte) ((index1 + 1) % key.Length);
149 private void CheckInput (byte[] inputBuffer, int inputOffset, int inputCount)
151 if (inputBuffer == null)
152 throw new ArgumentNullException ("inputBuffer");
154 throw new ArgumentOutOfRangeException ("inputOffset", "< 0");
156 throw new ArgumentOutOfRangeException ("inputCount", "< 0");
157 // ordered to avoid possible integer overflow
158 if (inputOffset > inputBuffer.Length - inputCount)
159 throw new ArgumentException ("inputBuffer", Locale.GetText ("Overflow"));
162 public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
164 CheckInput (inputBuffer, inputOffset, inputCount);
165 // check output parameters
166 if (outputBuffer == null)
167 throw new ArgumentNullException ("outputBuffer");
168 if (outputOffset < 0)
169 throw new ArgumentOutOfRangeException ("outputOffset", "< 0");
170 // ordered to avoid possible integer overflow
171 if (outputOffset > outputBuffer.Length - inputCount)
172 throw new ArgumentException ("outputBuffer", Locale.GetText ("Overflow"));
174 return InternalTransformBlock (inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
177 private int InternalTransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
180 for (int counter = 0; counter < inputCount; counter ++) {
182 y = (byte) (state [x] + y);
184 byte tmp = state [x];
185 state [x] = state [y];
188 xorIndex = (byte) (state [x] + state [y]);
189 outputBuffer [outputOffset + counter] = (byte) (inputBuffer [inputOffset + counter] ^ state [xorIndex]);
194 public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
196 CheckInput (inputBuffer, inputOffset, inputCount);
198 byte[] output = new byte [inputCount];
199 InternalTransformBlock (inputBuffer, inputOffset, inputCount, output, 0);