Fix XMM scanning on Mac x86.
[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 #if !INSIDE_CORLIB
38         public
39 #endif
40         class ARC4Managed : RC4, ICryptoTransform {
41
42                 private byte[] key;
43                 private byte[] state;
44                 private byte x;
45                 private byte y;
46                 private bool m_disposed;
47
48                 public ARC4Managed () : base () 
49                 {
50                         state = new byte [256];
51                         m_disposed = false;
52                 }
53
54                 ~ARC4Managed () 
55                 {
56                         Dispose (true);
57                 }
58                 
59                 protected override void Dispose (bool disposing) 
60                 {
61                         if (!m_disposed) {
62                                 x = 0;
63                                 y = 0;
64                                 if (key != null) {
65                                         Array.Clear (key, 0, key.Length);
66                                         key = null;
67                                 }
68                                 Array.Clear (state, 0, state.Length);
69                                 state = null;
70                                 GC.SuppressFinalize (this);
71                                 m_disposed = true;
72                         }
73                 }
74
75                 public override byte[] Key {
76                         get {
77                                 if (KeyValue == null)
78                                         GenerateKey ();
79                                 return (byte[]) KeyValue.Clone (); 
80                         }
81                         set { 
82                                 if (value == null)
83                                         throw new ArgumentNullException ("Key");
84                                 KeyValue = key = (byte[]) value.Clone ();
85                                 KeySetup (key);
86                         }
87                 }
88
89                 public bool CanReuseTransform {
90                         get { return false; }
91                 }
92
93                 public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgvIV)
94                 {
95                         Key = rgbKey;
96                         return (ICryptoTransform) this;
97                 }
98
99                 public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgvIV) 
100                 {
101                         Key = rgbKey;
102                         return CreateEncryptor ();
103                 }
104
105                 public override void GenerateIV () 
106                 {
107                         // not used for a stream cipher
108                         IV = new byte [0];
109                 }
110
111                 public override void GenerateKey () 
112                 {
113                         KeyValue = KeyBuilder.Key (KeySizeValue >> 3);
114                 }
115
116                 public bool CanTransformMultipleBlocks {
117                         get { return true; }
118                 }
119
120                 public int InputBlockSize {
121                         get { return 1; }
122                 }
123
124                 public int OutputBlockSize {
125                         get { return 1; }
126                 }
127
128                 private void KeySetup (byte[] key) 
129                 {
130                         byte index1 = 0;
131                         byte index2 = 0;
132
133                         for (int counter = 0; counter < 256; counter++)
134                                 state [counter] = (byte) counter;    
135                         x = 0;
136                         y = 0;
137                         for (int counter = 0; counter < 256; counter++) {
138                                 index2 = (byte) (key [index1] + state [counter] + index2);
139                                 // swap byte
140                                 byte tmp = state [counter];
141                                 state [counter] = state [index2];
142                                 state [index2] = tmp;
143                                 index1 = (byte) ((index1 + 1) % key.Length);
144                         }
145                 }
146
147                 private void CheckInput (byte[] inputBuffer, int inputOffset, int inputCount)
148                 {
149                         if (inputBuffer == null)
150                                 throw new ArgumentNullException ("inputBuffer");
151                         if (inputOffset < 0)
152                                 throw new ArgumentOutOfRangeException ("inputOffset", "< 0");
153                         if (inputCount < 0)
154                                 throw new ArgumentOutOfRangeException ("inputCount", "< 0");
155                         // ordered to avoid possible integer overflow
156                         if (inputOffset > inputBuffer.Length - inputCount)
157                                 throw new ArgumentException (Locale.GetText ("Overflow"), "inputBuffer");
158                 }
159
160                 public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) 
161                 {
162                         CheckInput (inputBuffer, inputOffset, inputCount);
163                         // check output parameters
164                         if (outputBuffer == null)
165                                 throw new ArgumentNullException ("outputBuffer");
166                         if (outputOffset < 0)
167                                 throw new ArgumentOutOfRangeException ("outputOffset", "< 0");
168                         // ordered to avoid possible integer overflow
169                         if (outputOffset > outputBuffer.Length - inputCount)
170                                 throw new ArgumentException (Locale.GetText ("Overflow"), "outputBuffer");
171
172                         return InternalTransformBlock (inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
173                 }
174
175                 private int InternalTransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) 
176                 {
177                         byte xorIndex;
178                         for (int counter = 0; counter < inputCount; counter ++) {               
179                                 x = (byte) (x + 1);
180                                 y = (byte) (state [x] + y);
181                                 // swap byte
182                                 byte tmp = state [x];
183                                 state [x] = state [y];
184                                 state [y] = tmp;
185
186                                 xorIndex = (byte) (state [x] + state [y]);
187                                 outputBuffer [outputOffset + counter] = (byte) (inputBuffer [inputOffset + counter] ^ state [xorIndex]);
188                         }
189                         return inputCount;
190                 }
191
192                 public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount) 
193                 {
194                         CheckInput (inputBuffer, inputOffset, inputCount);
195
196                         byte[] output = new byte [inputCount];
197                         InternalTransformBlock (inputBuffer, inputOffset, inputCount, output, 0);
198                         return output;
199                 }
200         }
201 }