2007-03-22 Sebastien Pouliot <sebastien@ximian.com>
[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-2007 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.Runtime.InteropServices;
33 using System.Text;
34
35 using Mono.Security.Cryptography;
36
37 namespace System.Security.Cryptography { 
38
39         [ComVisible (true)]
40         public class Rfc2898DeriveBytes : DeriveBytes {
41
42                 private const int defaultIterations = 1000;
43
44                 private int _iteration;
45                 private byte[] _salt;
46                 private HMACSHA1 _hmac;
47                 private byte[] _buffer;
48                 private int _pos;
49                 private int _f;
50
51                 // constructors
52
53                 public Rfc2898DeriveBytes (string password, byte[] salt) 
54                         : this (password, salt, defaultIterations)
55                 {
56                 }
57                 
58                 public Rfc2898DeriveBytes (string password, byte[] salt, int iterations) 
59                 {
60                         if (password == null)
61                                 throw new ArgumentNullException ("password");
62
63                         Salt = salt;
64                         IterationCount = iterations;
65                         _hmac = new HMACSHA1 (Encoding.UTF8.GetBytes (password));
66                 }
67
68                 public Rfc2898DeriveBytes (byte[] password, byte[] salt, int iterations) 
69                 {
70                         if (password == null)
71                                 throw new ArgumentNullException ("password");
72
73                         Salt = salt;
74                         IterationCount = iterations;
75                         _hmac = new HMACSHA1 (password);
76                 }
77                 
78                 public Rfc2898DeriveBytes (string password, int saltSize)
79                         : this (password, saltSize, defaultIterations)
80                 {
81                 }
82                 
83                 public Rfc2898DeriveBytes (string password, int saltSize, int iterations)
84                 {
85                         if (password == null)
86                                 throw new ArgumentNullException ("password");
87                         if (saltSize < 0)
88                                 throw new ArgumentOutOfRangeException ("invalid salt length");
89
90                         Salt = KeyBuilder.Key (saltSize);
91                         IterationCount = iterations;
92                         _hmac = new HMACSHA1 (Encoding.UTF8.GetBytes (password));
93                 }
94
95                 // properties
96
97                 public int IterationCount { 
98                         get { return _iteration; }
99                         set {
100                                 if (value < 1)
101                                         throw new ArgumentOutOfRangeException ("IterationCount < 1");
102
103                                 _iteration = value; 
104                         }
105                 }
106
107                 public byte[] Salt { 
108                         get { return (byte[]) _salt.Clone (); }
109                         set {
110                                 if (value == null)
111                                         throw new ArgumentNullException ("Salt");
112                                 if (value.Length < 8)
113                                         throw new ArgumentException ("Salt < 8 bytes");
114
115                                 _salt = (byte[])value.Clone (); 
116                         }
117                 }
118
119                 // methods
120
121                 private byte[] F (byte[] s, int c, int i) 
122                 {
123                         s [s.Length - 4] = (byte)(i >> 24);
124                         s [s.Length - 3] = (byte)(i >> 16);
125                         s [s.Length - 2] = (byte)(i >> 8);
126                         s [s.Length - 1] = (byte)i;
127
128                         // this is like j=0
129                         byte[] u1 = _hmac.ComputeHash (s);
130                         byte[] data = u1;
131                         // so we start at j=1
132                         for (int j=1; j < c; j++) {
133                                 byte[] un = _hmac.ComputeHash (data);
134                                 // xor
135                                 for (int k=0; k < 20; k++)
136                                         u1 [k] = (byte)(u1 [k] ^ un [k]);
137                                 data = un;
138                         }
139                         return u1;
140                 }
141
142                 public override byte[] GetBytes (int cb) 
143                 {
144                         if (cb < 1)
145                                 throw new ArgumentOutOfRangeException ("cb");
146
147                         int l = cb / 20;        // HMACSHA1 == 160 bits == 20 bytes
148                         int r = cb % 20;        // remainder
149                         if (r != 0)
150                                 l++;            // rounding up
151
152                         byte[] result = new byte [cb];
153                         int rpos = 0;
154                         if (_pos > 0) {
155                                 int count = Math.Min (20 - _pos, cb);
156                                 Buffer.BlockCopy (_buffer, _pos, result, 0, count);
157                                 if (count >= cb)
158                                         return result;
159                                 _pos = 0;
160                                 rpos = 20 - cb;
161                                 r = cb - rpos;
162                         }
163
164                         byte[] data = new byte [_salt.Length + 4];
165                         Buffer.BlockCopy (_salt, 0, data, 0, _salt.Length);
166
167                         for (int i=1; i <= l; i++) {
168                                 _buffer = F (data, _iteration, ++_f);
169                                 int count = ((i == l) ? r : 20);
170                                 Buffer.BlockCopy (_buffer, _pos, result, rpos, count);
171                                 rpos += _pos + count;
172                                 _pos = ((count == 20) ? 0 : count);
173                         }
174
175                         return result;
176                 }
177                 
178                 public override void Reset () 
179                 {
180                         _buffer = null;
181                         _pos = 0;
182                         _f = 0;
183                 }
184         } 
185 }
186
187 #endif