updating to the latest module.
[mono.git] / mcs / class / corlib / System.Security.Cryptography / Rfc2898DeriveBytes.cs
1 //
2 // Rfc2898DeriveBytes.cs: RFC2898 (PKCS#5 v2) Key derivation for Password Based Encryption 
3 //
4 // Author:
5 //      Sebastien Pouliot (sebastien@ximian.com)
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if NET_2_0
31
32 using System.Text;
33
34 using Mono.Security.Cryptography;
35
36 namespace System.Security.Cryptography { 
37
38         public class Rfc2898DeriveBytes : DeriveBytes {
39
40                 private const int defaultIterations = 1000;
41
42                 private int _iteration;
43                 private byte[] _salt;
44                 private HMACSHA1 _hmac;
45                 private byte[] _buffer;
46                 private int _pos;
47                 private int _f;
48
49                 // constructors
50
51                 public Rfc2898DeriveBytes (string password, byte[] salt) 
52                         : this (password, salt, defaultIterations)
53                 {
54                 }
55                 
56                 public Rfc2898DeriveBytes (string password, byte[] salt, int iterations) 
57                 {
58                         if (password == null)
59                                 throw new ArgumentNullException ("password");
60
61                         Salt = salt;
62                         IterationCount = iterations;
63                         _hmac = new HMACSHA1 (Encoding.UTF8.GetBytes (password));
64                 }
65
66                 public Rfc2898DeriveBytes (byte[] password, byte[] salt, int iterations) 
67                 {
68                         if (password == null)
69                                 throw new ArgumentNullException ("password");
70
71                         Salt = salt;
72                         IterationCount = iterations;
73                         _hmac = new HMACSHA1 (password);
74                 }
75                 
76                 public Rfc2898DeriveBytes (string password, int saltSize)
77                         : this (password, saltSize, defaultIterations)
78                 {
79                 }
80                 
81                 public Rfc2898DeriveBytes (string password, int saltSize, int iterations)
82                 {
83                         if (password == null)
84                                 throw new ArgumentNullException ("password");
85                         if (saltSize < 0)
86                                 throw new ArgumentOutOfRangeException ("invalid salt length");
87
88                         Salt = KeyBuilder.Key (saltSize);
89                         IterationCount = iterations;
90                         _hmac = new HMACSHA1 (Encoding.UTF8.GetBytes (password));
91                 }
92
93                 // properties
94
95                 public int IterationCount { 
96                         get { return _iteration; }
97                         set {
98                                 if (value < 1)
99                                         throw new ArgumentOutOfRangeException ("IterationCount < 1");
100
101                                 _iteration = value; 
102                         }
103                 }
104
105                 public byte[] Salt { 
106                         get { return (byte[]) _salt.Clone (); }
107                         set {
108                                 if (value == null)
109                                         throw new ArgumentNullException ("Salt");
110                                 if (value.Length < 8)
111                                         throw new ArgumentException ("Salt < 8 bytes");
112
113                                 _salt = (byte[])value.Clone (); 
114                         }
115                 }
116
117                 // methods
118
119                 private byte[] F (byte[] s, int c, int i) 
120                 {
121                         byte[] data = new byte [s.Length + 4];
122                         Buffer.BlockCopy (s, 0, data, 0, s.Length);
123                         byte[] int4 = BitConverter.GetBytes (i);
124                         Array.Reverse (int4, 0, 4);
125                         Buffer.BlockCopy (int4, 0, data, s.Length, 4);
126
127                         // this is like j=0
128                         byte[] u1 = _hmac.ComputeHash (data);
129                         data = u1;
130                         // so we start at j=1
131                         for (int j=1; j < c; j++) {
132                                 byte[] un = _hmac.ComputeHash (data);
133                                 // xor
134                                 for (int k=0; k < 20; k++)
135                                         u1 [k] = (byte)(u1 [k] ^ un [k]);
136                                 data = un;
137                         }
138                         return u1;
139                 }
140
141                 public override byte[] GetBytes (int cb) 
142                 {
143                         if (cb < 1)
144                                 throw new ArgumentOutOfRangeException ("cb");
145
146                         int l = cb / 20;        // HMACSHA1 == 160 bits == 20 bytes
147                         int r = cb % 20;        // remainder
148                         if (r != 0)
149                                 l++;            // rounding up
150
151                         byte[] result = new byte [cb];
152                         int rpos = 0;
153                         if (_pos > 0) {
154                                 int count = Math.Min (20 - _pos, cb);
155                                 Buffer.BlockCopy (_buffer, _pos, result, 0, count);
156                                 if (count >= cb)
157                                         return result;
158                                 _pos = 0;
159                                 rpos = 20 - cb;
160                                 r = cb - rpos;
161                         }
162
163                         for (int i=1; i <= l; i++) {
164                                 _buffer = F (_salt, _iteration, ++_f);
165                                 int count = ((i == l) ? r : 20);
166                                 Buffer.BlockCopy (_buffer, _pos, result, rpos, count);
167                                 rpos += _pos + count;
168                                 _pos = ((count == 20) ? 0 : count);
169                         }
170
171                         return result;
172                 }
173                 
174                 public override void Reset () 
175                 {
176                         _buffer = null;
177                         _pos = 0;
178                         _f = 0;
179                 }
180         } 
181 }
182
183 #endif