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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
namespace Devolutions.Cryptography.Argon2
{
using System;
using System.IO;
/// <summary>
/// Used to define the Argon2 parameters when deriving data.
/// </summary>
public class Argon2Parameters
{
/// <summary>
/// Initializes a new instance of the <see cref="Argon2Parameters"/> class.
/// </summary>
public Argon2Parameters()
{
this.InitializeParameters();
}
/// <summary>
/// Initializes a new instance of the <see cref="Argon2Parameters"/> class.
/// </summary>
/// <param name="defaultParameters">Use the standard Argon2 parameters.</param>
internal Argon2Parameters(bool defaultParameters = true)
{
if (defaultParameters)
{
this.InitializeParameters();
}
}
/// <summary>
/// Gets the size of the raw Argon2 data.
/// </summary>
public static long NativeSize
{
get
{
return Native.GetDefaultArgon2ParametersSizeNative();
}
}
/// <summary>
/// Gets or sets the number of iterations over the memory.
/// </summary>
public uint Iterations { get; set; }
/// <summary>
/// Gets or sets the number of threads to use.
/// </summary>
public uint Lanes { get; set; }
/// <summary>
/// Gets or sets the hash length.
/// </summary>
public uint Length { get; set; }
/// <summary>
/// Gets or sets the memory used by the algorithm.
/// </summary>
public uint Memory { get; set; }
/// <summary>
/// Gets or sets the associated data.
/// </summary>
internal byte[] AssociatedData { get; set; }
/// <summary>
/// Gets or sets the devolutions crypto version.
/// </summary>
internal uint DevolutionsCryptoVersion { get; set; }
/// <summary>
/// Gets or sets the salt used by the algorithm.
/// </summary>
internal byte[] Salt { get; set; }
/// <summary>
/// Gets or sets the Argon2 variant used by the algorithm.
/// </summary>
internal Variant Variant { get; set; }
/// <summary>
/// Gets or sets the Argon2 version.
/// </summary>
internal Version Version { get; set; }
/// <summary>
/// Deserialize the Argon2Parameters class from a little endian byte array.
/// </summary>
/// <param name="data">The data to deserialize.</param>
/// <returns>Returns the deserialized parameters.</returns>
public static Argon2Parameters FromByteArray(byte[] data)
{
if (data == null)
{
return null;
}
try
{
MemoryStream stream = new MemoryStream(data);
Argon2Parameters parameters = new Argon2Parameters(false);
// ==== Devolutions Crypto Version ====
byte[] buffer = new byte[4];
stream.Read(buffer, 0, 4);
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(buffer);
}
parameters.DevolutionsCryptoVersion = BitConverter.ToUInt32(buffer, 0);
// ==== Length ====
stream.Read(buffer, 0, 4);
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(buffer);
}
parameters.Length = BitConverter.ToUInt32(buffer, 0);
// ==== Lanes ====
stream.Read(buffer, 0, 4);
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(buffer);
}
parameters.Lanes = BitConverter.ToUInt32(buffer, 0);
// ==== Memory ====
stream.Read(buffer, 0, 4);
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(buffer);
}
parameters.Memory = BitConverter.ToUInt32(buffer, 0);
// ==== Iterations ====
stream.Read(buffer, 0, 4);
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(buffer);
}
parameters.Iterations = BitConverter.ToUInt32(buffer, 0);
// ==== Variant ====
stream.Read(buffer, 0, 1);
parameters.Variant = (Variant)buffer[0];
// ==== Version ====
stream.Read(buffer, 0, 1);
parameters.Version = (Version)buffer[0];
// ==== Associated Data ====
stream.Read(buffer, 0, 4);
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(buffer);
}
uint associatedDataLength = BitConverter.ToUInt32(buffer, 0);
byte[] associatedData = new byte[associatedDataLength];
stream.Read(associatedData, 0, (int)associatedDataLength);
parameters.AssociatedData = associatedData;
// ==== Salt ====
stream.Read(buffer, 0, 4);
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(buffer);
}
uint saltLength = BitConverter.ToUInt32(buffer, 0);
byte[] salt = new byte[saltLength];
stream.Read(salt, 0, (int)saltLength);
parameters.Salt = salt;
stream.Close();
return parameters;
}
catch (Exception ex)
{
DevolutionsCryptoException exception = new DevolutionsCryptoException(ManagedError.Error, exception: ex);
throw exception;
}
}
/// <summary>
/// Serialize the Argon2Parameters class to a little endian byte array.
/// </summary>
/// <returns>Returns Argon2Parameters class to a little endian byte array.</returns>
public byte[] ToByteArray()
{
try
{
// === Devolutions Crypto Version ===
byte[] devolutionsCryptoVersion = BitConverter.GetBytes(this.DevolutionsCryptoVersion);
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(devolutionsCryptoVersion);
}
// === Length ===
byte[] length = BitConverter.GetBytes(this.Length);
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(length);
}
// === Lanes ===
byte[] lanes = BitConverter.GetBytes(this.Lanes);
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(lanes);
}
// === Memory ===
byte[] memory = BitConverter.GetBytes(this.Memory);
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(memory);
}
// === Iterations ===
byte[] iterations = BitConverter.GetBytes(this.Iterations);
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(iterations);
}
// === Variant ===
byte[] variant = new byte[1];
variant[0] = (byte)this.Variant;
// === Version ===
byte[] version = new byte[1];
version[0] = (byte)this.Version;
// === Associated Data Length ===
if (this.AssociatedData == null)
{
this.AssociatedData = Array.Empty<byte>();
}
byte[] associatedDataLength = BitConverter.GetBytes(this.AssociatedData.Length);
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(associatedDataLength);
}
// === Salt Length ===
if (this.Salt == null)
{
this.Salt = Array.Empty<byte>();
}
byte[] saltLength = BitConverter.GetBytes(this.Salt.Length);
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(saltLength);
}
return Utils.ConcatArrays(
devolutionsCryptoVersion,
length,
lanes,
memory,
iterations,
variant,
version,
associatedDataLength,
this.AssociatedData,
saltLength,
this.Salt);
}
catch (Exception ex)
{
DevolutionsCryptoException exception = new DevolutionsCryptoException(ManagedError.Error, exception: ex);
throw exception;
}
}
/// <summary>
/// Initialise the default recommended parameters.
/// </summary>
private void InitializeParameters()
{
Argon2Parameters parameters = Managed.GetDefaultArgon2Parameters();
this.Iterations = parameters.Iterations;
this.Lanes = parameters.Lanes;
this.Length = parameters.Length;
this.Memory = parameters.Memory;
this.AssociatedData = parameters.AssociatedData;
this.DevolutionsCryptoVersion = parameters.DevolutionsCryptoVersion;
this.Salt = parameters.Salt;
this.Variant = parameters.Variant;
this.Version = parameters.Version;
}
}
}