1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
//
// PasswordDeriveBytes.cs: Handles PKCS#5 key derivation using password
//
// Author:
// Sebastien Pouliot (sebastien@ximian.com)
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
namespace System.Security.Cryptography {
// References:
// a. PKCS #5 - Password-Based Cryptography Standard
// http://www.rsasecurity.com/rsalabs/pkcs/pkcs-5/index.html
// b. IETF RFC2898: PKCS #5: Password-Based Cryptography Specification Version 2.0
// http://www.rfc-editor.org/rfc/rfc2898.txt
#if NET_2_0
[ComVisible (true)]
#endif
public class PasswordDeriveBytes : DeriveBytes {
private string HashNameValue;
private byte[] SaltValue;
private int IterationsValue;
private HashAlgorithm hash;
private int state;
private byte[] password;
private byte[] initial;
private byte[] output;
private int position;
private int hashnumber;
public PasswordDeriveBytes (string strPassword, byte[] rgbSalt)
{
Prepare (strPassword, rgbSalt, "SHA1", 100);
}
public PasswordDeriveBytes (string strPassword, byte[] rgbSalt, CspParameters cspParams)
{
Prepare (strPassword, rgbSalt, "SHA1", 100);
if (cspParams != null) {
throw new NotSupportedException (
Locale.GetText ("CspParameters not supported by Mono for PasswordDeriveBytes."));
}
}
public PasswordDeriveBytes (string strPassword, byte[] rgbSalt, string strHashName, int iterations)
{
Prepare (strPassword, rgbSalt, strHashName, iterations);
}
public PasswordDeriveBytes (string strPassword, byte[] rgbSalt, string strHashName, int iterations, CspParameters cspParams)
{
Prepare (strPassword, rgbSalt, strHashName, iterations);
if (cspParams != null) {
throw new NotSupportedException (
Locale.GetText ("CspParameters not supported by Mono for PasswordDeriveBytes."));
}
}
#if NET_2_0
public PasswordDeriveBytes (byte[] password, byte[] salt)
{
Prepare (password, salt, "SHA1", 100);
}
public PasswordDeriveBytes (byte[] password, byte[] salt, CspParameters cspParams)
{
Prepare (password, salt, "SHA1", 100);
if (cspParams != null) {
throw new NotSupportedException (
Locale.GetText ("CspParameters not supported by Mono for PasswordDeriveBytes."));
}
}
public PasswordDeriveBytes (byte[] password, byte[] salt, string hashName, int iterations)
{
Prepare (password, salt, hashName, iterations);
}
public PasswordDeriveBytes (byte[] password, byte[] salt, string hashName, int iterations, CspParameters cspParams)
{
Prepare (password, salt, hashName, iterations);
if (cspParams != null) {
throw new NotSupportedException (
Locale.GetText ("CspParameters not supported by Mono for PasswordDeriveBytes."));
}
}
#endif
~PasswordDeriveBytes ()
{
// zeroize buffer
if (initial != null) {
Array.Clear (initial, 0, initial.Length);
initial = null;
}
// zeroize temporary password storage
Array.Clear (password, 0, password.Length);
}
#if NET_2_0
private void Prepare (string strPassword, byte[] rgbSalt, string strHashName, int iterations)
{
if (strPassword == null)
throw new ArgumentNullException ("strPassword");
byte[] pwd = Encoding.UTF8.GetBytes (strPassword);
Prepare (pwd, rgbSalt, strHashName, iterations);
Array.Clear (pwd, 0, pwd.Length);
}
private void Prepare (byte[] password, byte[] rgbSalt, string strHashName, int iterations)
{
if (password == null)
throw new ArgumentNullException ("password");
this.password = (byte[]) password.Clone ();
Salt = rgbSalt;
HashName = strHashName;
IterationCount = iterations;
state = 0;
}
#else
private void Prepare (string strPassword, byte[] rgbSalt, string strHashName, int iterations)
{
if (strPassword == null)
password = null;
else
password = Encoding.UTF8.GetBytes (strPassword);
if (rgbSalt == null)
SaltValue = null;
else
SaltValue = (byte[]) rgbSalt.Clone ();
HashName = strHashName;
IterationCount = iterations;
state = 0;
}
#endif
public string HashName {
get { return HashNameValue; }
set {
if (value == null)
throw new ArgumentNullException ("HashName");
if (state != 0) {
throw new CryptographicException (
Locale.GetText ("Can't change this property at this stage"));
}
HashNameValue = value;
}
}
public int IterationCount {
get { return IterationsValue; }
set {
if (value < 1)
throw new ArgumentOutOfRangeException ("> 0", "IterationCount");
if (state != 0) {
throw new CryptographicException (
Locale.GetText ("Can't change this property at this stage"));
}
IterationsValue = value;
}
}
public byte[] Salt {
get {
if (SaltValue == null)
return null;
return (byte[]) SaltValue.Clone ();
}
set {
if (state != 0) {
throw new CryptographicException (
Locale.GetText ("Can't change this property at this stage"));
}
#if NET_2_0
if (value != null)
SaltValue = (byte[]) value.Clone ();
else
SaltValue = null;
#else
// this will cause a NullReferenceException if set to null (like 1.0/1.1)
SaltValue = (byte[]) value.Clone ();
#endif
}
}
public byte[] CryptDeriveKey (string algname, string alghashname, int keySize, byte[] rgbIV)
{
if (keySize > 128) {
throw new CryptographicException (
Locale.GetText ("Key Size can't be greater than 128 bits"));
}
throw new NotSupportedException (
Locale.GetText ("CspParameters not supported by Mono"));
}
// note: Key is returned - we can't zeroize it ourselve :-(
#if NET_2_0
[Obsolete ("see Rfc2898DeriveBytes for PKCS#5 v2 support")]
#endif
public override byte[] GetBytes (int cb)
{
#if ! NET_2_0
// 1.0/1.1 was a little late at throwing the argument exception ;-)
if (password == null)
throw new ArgumentNullException ("Password");
#endif
if (cb < 1)
throw new IndexOutOfRangeException ("cb");
if (state == 0) {
state = 1;
// it's now impossible to change the HashName, Salt
// and IterationCount
Reset ();
}
byte[] result = new byte [cb];
int cpos = 0;
// the initial hash (in reset) + at least one iteration
int iter = Math.Max (1, IterationsValue - 1);
// start with the PKCS5 key
if (output == null) {
// calculate the PKCS5 key
output = initial;
// generate new key material
for (int i = 0; i < iter - 1; i++)
output = hash.ComputeHash (output);
}
while (cpos < cb) {
byte[] output2 = null;
if (hashnumber == 0) {
// last iteration on output
output2 = hash.ComputeHash (output);
}
else if (hashnumber < 1000) {
string n = Convert.ToString (hashnumber);
output2 = new byte [output.Length + n.Length];
for (int j=0; j < n.Length; j++)
output2 [j] = (byte)(n [j]);
Buffer.BlockCopy (output, 0, output2, n.Length, output.Length);
// don't update output
output2 = hash.ComputeHash (output2);
}
else {
throw new CryptographicException (
Locale.GetText ("too long"));
}
int rem = output2.Length - position;
int l = Math.Min (cb - cpos, rem);
Buffer.BlockCopy (output2, position, result, cpos, l);
cpos += l;
position += l;
while (position >= output2.Length) {
position -= output2.Length;
hashnumber++;
}
}
return result;
}
public override void Reset ()
{
// note: Reset doesn't change state
position = 0;
hashnumber = 0;
hash = HashAlgorithm.Create (HashNameValue);
if (SaltValue != null) {
hash.TransformBlock (password, 0, password.Length, password, 0);
hash.TransformFinalBlock (SaltValue, 0, SaltValue.Length);
initial = hash.Hash;
}
else
initial = hash.ComputeHash (password);
}
}
}
|