Handle ENETDOWN error if defined.
[mono.git] / mcs / class / Mono.Security / Mono.Security.Cryptography / ARC4Managed.cs
1 //
2 // ARC4Managed.cs: Alleged RC4(tm) compatible symmetric stream cipher
3 //      RC4 is a trademark of RSA Security
4 //
5
6 //
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:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
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.
25 //
26
27 using System;
28 using System.Globalization;
29 using System.Security.Cryptography;
30
31 namespace Mono.Security.Cryptography {
32
33         // References:
34         // a.   Usenet 1994 - RC4 Algorithm revealed
35         //      http://www.qrst.de/html/dsds/rc4.htm
36
37         public class ARC4Managed : RC4, ICryptoTransform {
38
39                 private byte[] key;
40                 private byte[] state;
41                 private byte x;
42                 private byte y;
43                 private bool m_disposed;
44
45                 public ARC4Managed () : base () 
46                 {
47                         state = new byte [256];
48                         m_disposed = false;
49                 }
50
51                 ~ARC4Managed () 
52                 {
53                         Dispose (true);
54                 }
55                 
56                 protected override void Dispose (bool disposing) 
57                 {
58                         if (!m_disposed) {
59                                 x = 0;
60                                 y = 0;
61                                 if (key != null) {
62                                         Array.Clear (key, 0, key.Length);
63                                         key = null;
64                                 }
65                                 Array.Clear (state, 0, state.Length);
66                                 state = null;
67                                 GC.SuppressFinalize (this);
68                                 m_disposed = true;
69                         }
70                 }
71
72                 public override byte[] Key {
73                         get {
74                                 if (KeyValue == null)
75                                         GenerateKey ();
76                                 return (byte[]) KeyValue.Clone (); 
77                         }
78                         set { 
79                                 if (value == null)
80                                         throw new ArgumentNullException ("Key");
81                                 KeyValue = key = (byte[]) value.Clone ();
82                                 KeySetup (key);
83                         }
84                 }
85
86                 public bool CanReuseTransform {
87                         get { return false; }
88                 }
89
90                 public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgvIV)
91                 {
92                         Key = rgbKey;
93                         return (ICryptoTransform) this;
94                 }
95
96                 public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgvIV) 
97                 {
98                         Key = rgbKey;
99                         return CreateEncryptor ();
100                 }
101
102                 public override void GenerateIV () 
103                 {
104                         // not used for a stream cipher
105                         IV = new byte [0];
106                 }
107
108                 public override void GenerateKey () 
109                 {
110                         KeyValue = KeyBuilder.Key (KeySizeValue >> 3);
111                 }
112
113                 public bool CanTransformMultipleBlocks {
114                         get { return true; }
115                 }
116
117                 public int InputBlockSize {
118                         get { return 1; }
119                 }
120
121                 public int OutputBlockSize {
122                         get { return 1; }
123                 }
124
125                 private void KeySetup (byte[] key) 
126                 {
127                         byte index1 = 0;
128                         byte index2 = 0;
129
130                         for (int counter = 0; counter < 256; counter++)
131                                 state [counter] = (byte) counter;    
132                         x = 0;
133                         y = 0;
134                         for (int counter = 0; counter < 256; counter++) {
135                                 index2 = (byte) (key [index1] + state [counter] + index2);
136                                 // swap byte
137                                 byte tmp = state [counter];
138                                 state [counter] = state [index2];
139                                 state [index2] = tmp;
140                                 index1 = (byte) ((index1 + 1) % key.Length);
141                         }
142                 }
143
144                 private void CheckInput (byte[] inputBuffer, int inputOffset, int inputCount)
145                 {
146                         if (inputBuffer == null)
147                                 throw new ArgumentNullException ("inputBuffer");
148                         if (inputOffset < 0)
149                                 throw new ArgumentOutOfRangeException ("inputOffset", "< 0");
150                         if (inputCount < 0)
151                                 throw new ArgumentOutOfRangeException ("inputCount", "< 0");
152                         // ordered to avoid possible integer overflow
153                         if (inputOffset > inputBuffer.Length - inputCount)
154                                 throw new ArgumentException ("inputBuffer", Locale.GetText ("Overflow"));
155                 }
156
157                 public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) 
158                 {
159                         CheckInput (inputBuffer, inputOffset, inputCount);
160                         // check output parameters
161                         if (outputBuffer == null)
162                                 throw new ArgumentNullException ("outputBuffer");
163                         if (outputOffset < 0)
164                                 throw new ArgumentOutOfRangeException ("outputOffset", "< 0");
165                         // ordered to avoid possible integer overflow
166                         if (outputOffset > outputBuffer.Length - inputCount)
167                                 throw new ArgumentException ("outputBuffer", Locale.GetText ("Overflow"));
168
169                         return InternalTransformBlock (inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
170                 }
171
172                 private int InternalTransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) 
173                 {
174                         byte xorIndex;
175                         for (int counter = 0; counter < inputCount; counter ++) {               
176                                 x = (byte) (x + 1);
177                                 y = (byte) (state [x] + y);
178                                 // swap byte
179                                 byte tmp = state [x];
180                                 state [x] = state [y];
181                                 state [y] = tmp;
182
183                                 xorIndex = (byte) (state [x] + state [y]);
184                                 outputBuffer [outputOffset + counter] = (byte) (inputBuffer [inputOffset + counter] ^ state [xorIndex]);
185                         }
186                         return inputCount;
187                 }
188
189                 public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount) 
190                 {
191                         CheckInput (inputBuffer, inputOffset, inputCount);
192
193                         byte[] output = new byte [inputCount];
194                         InternalTransformBlock (inputBuffer, inputOffset, inputCount, output, 0);
195                         return output;
196                 }
197         }
198 }