/
PBKDF2.cs
270 lines (249 loc) · 6.92 KB
/
PBKDF2.cs
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
using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
namespace SecurityDriven.Inferno.Kdf
{
using Mac;
using Extensions;
public class PBKDF2 : DeriveBytes
{
// Fields
static readonly CryptoRandom rng = new CryptoRandom();
int BlockSize, endIndex, startIndex, iterations;
uint block;
byte[] buffer, salt;
HMAC hmac;
HMAC2 hmac2;
/// <summary>
/// Default iteration count.
/// </summary>
public const int DefaultIterations = 10000;
/// <summary>
/// ctor
/// </summary>
/// <param name="hmacFactory">hmacFactory</param>
/// <param name="password">password</param>
/// <param name="saltSize">saltSize</param>
public PBKDF2(Func<HMAC> hmacFactory, string password, int saltSize)
: this(hmacFactory, password, saltSize, DefaultIterations)
{
}//ctor
/// <summary>
/// ctor
/// </summary>
/// <param name="hmacFactory">hmacFactory</param>
/// <param name="password">password</param>
/// <param name="salt">salt</param>
public PBKDF2(Func<HMAC> hmacFactory, string password, byte[] salt)
: this(hmacFactory, password, salt, DefaultIterations)
{
}//ctor
/// <summary>
/// ctor
/// </summary>
/// <param name="hmacFactory">hmacFactory</param>
/// <param name="password">password</param>
/// <param name="salt">salt</param>
/// <param name="iterations">iterations</param>
public PBKDF2(Func<HMAC> hmacFactory, string password, byte[] salt, int iterations)
: this(hmacFactory, password.ToBytes(), salt, iterations)
{
}//ctor
/// <summary>
/// ctor
/// </summary>
/// <param name="hmacFactory">hmacFactory</param>
/// <param name="password">password</param>
/// <param name="saltSize">saltSize</param>
/// <param name="iterations">iterations</param>
public PBKDF2(Func<HMAC> hmacFactory, string password, int saltSize, int iterations)
: this(hmacFactory, password.ToBytes(), GenerateSalt(saltSize), iterations)
{
}//ctor
/// <summary>
/// ctor
/// </summary>
/// <param name="hmacFactory">hmacFactory</param>
/// <param name="password">password</param>
/// <param name="salt">salt</param>
/// <param name="iterations">iterations</param>
public PBKDF2(Func<HMAC> hmacFactory, byte[] password, byte[] salt, int iterations)
{
this.Salt = salt;
this.IterationCount = iterations;
this.hmac = hmacFactory();
this.hmac2 = hmac as HMAC2;
this.hmac.Key = password;
this.BlockSize = hmac.HashSize / 8;
this.Initialize();
}//ctor
static byte[] GenerateSalt(int saltSize)
{
if (saltSize < 0)
throw new ArgumentOutOfRangeException(nameof(saltSize));
byte[] data = new byte[saltSize];
rng.NextBytes(data, 0, data.Length);
return data;
}//GenerateSalt()
/// <summary>
/// Releases the unmanaged resources used, and optionally releases the managed resources.
/// </summary>
/// <param name="disposing">true to release both managed and unmanaged resources; false to release only managed resources.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (this.hmac != null)
{
this.hmac.Dispose();
}
if (this.buffer != null)
{
Array.Clear(this.buffer, 0, this.buffer.Length);
}
if (this.salt != null)
{
Array.Clear(this.salt, 0, this.salt.Length);
}
}
}//Dispose()
byte[] inputBuffer = new byte[4];
byte[] Func()
{
// localize members
var _inputBuffer = this.inputBuffer;
var _hmac = this.hmac;
var _hmac2 = this.hmac2;
new Utils.IntStruct { UintValue = this.block }.ToBEBytes(_inputBuffer);
this.hmac.TransformBlock(inputBuffer: this.salt, inputOffset: 0, inputCount: this.salt.Length, outputBuffer: null, outputOffset: 0);
this.hmac.TransformBlock(inputBuffer: _inputBuffer, inputOffset: 0, inputCount: _inputBuffer.Length, outputBuffer: null, outputOffset: 0);
this.hmac.TransformFinalBlock(inputBuffer: _inputBuffer, inputOffset: 0, inputCount: 0);
byte[] hash = this.hmac.Hash; // creates a copy
this.hmac.Initialize();
byte[] buffer3 = hash;
for (int i = 2, blockSize = BlockSize, j = this.iterations; i <= j; i++)
{
if (_hmac2 != null)
{
_hmac2.TransformBlock(hash, 0, blockSize, null, 0);
_hmac2.TransformFinalBlock(hash, 0, 0);
hash = _hmac2.HashInner;
}
else hash = _hmac.ComputeHash(hash);
Utils.Xor(dest: buffer3, destOffset: 0, left: hash, leftOffset: 0, byteCount: blockSize);
}
this.block++;
return buffer3;
}
// Properties
/// <summary>
/// Gets or sets the number of iterations for the operation.
/// </summary>
public int IterationCount
{
get
{
return this.iterations;
}
set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
this.iterations = value;
this.Initialize();
}
}
/// <summary>
/// Gets or sets the key salt value for the operation.
/// </summary>
public byte[] Salt
{
get
{
return this.salt.CloneBytes();
}
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
if (value.Length < 8)
{
throw new ArgumentException("Salt is not at least 8 bytes.");
}
this.salt = value.CloneBytes();
this.Initialize();
}
}
// Methods
/// <summary>
/// Returns pseudo-random bytes.
/// </summary>
/// <param name="cb">The number of pseudo-random bytes to generate.</param>
/// <returns></returns>
public override byte[] GetBytes(int cb)
{
if (cb <= 0)
{
throw new ArgumentOutOfRangeException(nameof(cb), "Positive number required.");
}
byte[] dst = new byte[cb];
int dstOffsetBytes = 0;
int byteCount = this.endIndex - this.startIndex;
if (byteCount > 0)
{
if (cb < byteCount)
{
Buffer.BlockCopy(this.buffer, this.startIndex, dst, 0, cb);
this.startIndex += cb;
return dst;
}
Buffer.BlockCopy(this.buffer, this.startIndex, dst, 0, byteCount);
this.startIndex = this.endIndex = 0;
dstOffsetBytes += byteCount;
}//if
while (dstOffsetBytes < cb)
{
byte[] src = this.Func();
int num3 = cb - dstOffsetBytes;
if (num3 > BlockSize)
{
Buffer.BlockCopy(src, 0, dst, dstOffsetBytes, BlockSize);
dstOffsetBytes += BlockSize;
}
else
{
Buffer.BlockCopy(src, 0, dst, dstOffsetBytes, num3);
dstOffsetBytes += num3;
Buffer.BlockCopy(src, num3, this.buffer, this.startIndex, BlockSize - num3);
this.endIndex += BlockSize - num3;
return dst;
}
}//while
return dst;
}//GetBytes()
void Initialize()
{
if (this.buffer != null)
{
Array.Clear(this.buffer, 0, this.buffer.Length);
}
this.buffer = new byte[BlockSize];
this.block = 1;
this.startIndex = this.endIndex = 0;
}
/// <summary>
/// Resets the state.
/// </summary>
/// <remarks>
/// This method is automatically called if the salt or iteration count is modified.
/// </remarks>
public override void Reset()
{
this.Initialize();
}
}//class PBKDF2
}//ns