2 // Rfc2898DeriveBytes.cs: RFC2898 (PKCS#5 v2) Key derivation for Password Based Encryption
5 // Sebastien Pouliot (sebastien@ximian.com)
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 using System.Runtime.InteropServices;
35 using Mono.Security.Cryptography;
37 namespace System.Security.Cryptography {
40 public class Rfc2898DeriveBytes : DeriveBytes {
42 private const int defaultIterations = 1000;
44 private int _iteration;
46 private HMACSHA1 _hmac;
47 private byte[] _buffer;
53 public Rfc2898DeriveBytes (string password, byte[] salt)
54 : this (password, salt, defaultIterations)
58 public Rfc2898DeriveBytes (string password, byte[] salt, int iterations)
61 throw new ArgumentNullException ("password");
64 IterationCount = iterations;
65 _hmac = new HMACSHA1 (Encoding.UTF8.GetBytes (password));
68 public Rfc2898DeriveBytes (byte[] password, byte[] salt, int iterations)
71 throw new ArgumentNullException ("password");
74 IterationCount = iterations;
75 _hmac = new HMACSHA1 (password);
78 public Rfc2898DeriveBytes (string password, int saltSize)
79 : this (password, saltSize, defaultIterations)
83 public Rfc2898DeriveBytes (string password, int saltSize, int iterations)
86 throw new ArgumentNullException ("password");
88 throw new ArgumentOutOfRangeException ("invalid salt length");
90 Salt = KeyBuilder.Key (saltSize);
91 IterationCount = iterations;
92 _hmac = new HMACSHA1 (Encoding.UTF8.GetBytes (password));
97 public int IterationCount {
98 get { return _iteration; }
101 throw new ArgumentOutOfRangeException ("IterationCount < 1");
108 get { return (byte[]) _salt.Clone (); }
111 throw new ArgumentNullException ("Salt");
112 if (value.Length < 8)
113 throw new ArgumentException ("Salt < 8 bytes");
115 _salt = (byte[])value.Clone ();
121 private byte[] F (byte[] s, int c, int i)
123 byte[] data = new byte [s.Length + 4];
124 Buffer.BlockCopy (s, 0, data, 0, s.Length);
125 byte[] int4 = BitConverter.GetBytes (i);
126 Array.Reverse (int4, 0, 4);
127 Buffer.BlockCopy (int4, 0, data, s.Length, 4);
130 byte[] u1 = _hmac.ComputeHash (data);
132 // so we start at j=1
133 for (int j=1; j < c; j++) {
134 byte[] un = _hmac.ComputeHash (data);
136 for (int k=0; k < 20; k++)
137 u1 [k] = (byte)(u1 [k] ^ un [k]);
143 public override byte[] GetBytes (int cb)
146 throw new ArgumentOutOfRangeException ("cb");
148 int l = cb / 20; // HMACSHA1 == 160 bits == 20 bytes
149 int r = cb % 20; // remainder
153 byte[] result = new byte [cb];
156 int count = Math.Min (20 - _pos, cb);
157 Buffer.BlockCopy (_buffer, _pos, result, 0, count);
165 for (int i=1; i <= l; i++) {
166 _buffer = F (_salt, _iteration, ++_f);
167 int count = ((i == l) ? r : 20);
168 Buffer.BlockCopy (_buffer, _pos, result, rpos, count);
169 rpos += _pos + count;
170 _pos = ((count == 20) ? 0 : count);
176 public override void Reset ()