forked from dbhurley/MauticOutlookPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnixCrypt.cs
418 lines (332 loc) · 13.9 KB
/
UnixCrypt.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Security.Cryptography;
using System.IO;
using System.Management;
namespace MauticOutlookPlugin {
class UnixCrypt
{
public static string crypt(string key, string salt)
{
ArrayPointer<byte> key_ptr = new ArrayPointer<byte>(
Encoding.UTF8.GetBytes(key + "\0"));
ArrayPointer<byte> salt_ptr = new ArrayPointer<byte>(
Encoding.UTF8.GetBytes(salt + "\0"));
/* we have to use MD5 encryption replacement. */
return ExtractString(__md5_crypt(key_ptr, salt_ptr));
}
/* Table with characters for base64 transformation. */
static char[] b64t =
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray();
/* Define our magic string to mark salt for MD5 "encryption"
replacement. This is meant to be the same as for other MD5 based
encryption implementations. */
static ArrayPointer<byte> md5_salt_prefix = new ArrayPointer<byte>(new byte[] { (byte)'$', (byte)'1', (byte)'$', 0 });
static ArrayPointer<byte> dollar_sign = new ArrayPointer<byte>(new byte[] { (byte)'$', 0 });
/* Maximum salt string length. */
const int SALT_LEN_MAX = 16;
/* Default number of rounds if not explicitly specified. */
const int ROUNDS_DEFAULT = 5000;
/* Minimum number of rounds. */
const int ROUNDS_MIN = 1000;
/* Maximum number of rounds. */
const int ROUNDS_MAX = 999999999;
private static ArrayPointer<byte> __md5_crypt(ArrayPointer<byte> key, ArrayPointer<byte> salt)
{
/* We don't want to have an arbitrary limit in the size of the
password. We can compute the size of the result in advance and
so we can prepare the buffer we pass to `md5_crypt_r'. */
ArrayPointer<byte> buffer;
int buflen = 0;
int needed = 3 + strlen(salt) + 1 + 26 + 1;
ArrayPointer<byte> new_buffer = new ArrayPointer<byte>(new byte[needed]);
buffer = new_buffer;
buflen = needed;
return __md5_crypt_r(key, salt, buffer, buflen);
}
public static int strlen(ArrayPointer<byte> str)
{
if(str == null)
{
return 0;
}
for(int i = 0; ; i++, str++)
{
if(str.Value == 0)
return i;
}
}
public static int strncmp(ArrayPointer<byte> str1, ArrayPointer<byte> str2, int length)
{
for(int i = 0; i < length; i++, str1++, str2++)
{
if(str1.Value > str2.Value)
{
return 1;
}
else if (str2.Value > str1.Value)
{
return -1;
}
}
return 0;
}
private static int strcspn(ArrayPointer<byte> str1, ArrayPointer<byte> str2)
{
int location = 0;
ArrayPointer<byte> i;
ArrayPointer<byte> j;
int str1_len = strlen(str1);
int str2_len = strlen(str2);
for(i = str1; i.Value != 0; i++, location++)
{
for(j = str2; j.Value != 0; j++)
{
if(i.Value == j.Value)
return location;
}
}
return str1_len;
}
private class md5_ctx
{
public MemoryStream stream;
}
private class sha256_ctx
{
public MemoryStream stream;
}
private static ArrayPointer<byte> __md5_crypt_r(ArrayPointer<byte> key, ArrayPointer<byte> salt, ArrayPointer<byte> buffer, int buflen)
{
ArrayPointer<byte> alt_result = new ArrayPointer<byte>(new byte[16]);
md5_ctx ctx = new md5_ctx();
md5_ctx alt_ctx = new md5_ctx();
int salt_len;
int key_len;
int cnt;
ArrayPointer<byte> cp;
ArrayPointer<byte> copied_key;
ArrayPointer<byte> copied_salt;
/* Find beginning of salt string. The prefix should normally always
be present. Just in case it is not. */
if (strncmp(md5_salt_prefix, salt, strlen(md5_salt_prefix)) == 0)
{
salt += strlen(md5_salt_prefix);
}
salt_len = Math.Min(strcspn(salt, dollar_sign), 8);
key_len = strlen(key);
byte[] temp = new byte[key.SourceArray.Length];
key.SourceArray.CopyTo(temp, 0);
copied_key = new ArrayPointer<byte>(temp);
copied_key.Address = key.Address;
key = copied_key;
temp = new byte[salt.SourceArray.Length];
salt.SourceArray.CopyTo(temp, 0);
copied_salt = new ArrayPointer<byte>(temp);
copied_salt.Address = salt.Address;
salt = copied_salt;
/* Prepare for the real work. */
__md5_init_ctx(ctx);
/* Add the key string. */
__md5_process_bytes(key, key_len, ctx);
/* Because the SALT argument need not always have the salt prefix we
add it separately. */
__md5_process_bytes(md5_salt_prefix, strlen(md5_salt_prefix), ctx);
/* The last part is the salt string. This must be at most 8
characters and it ends at the first `$' character (for
compatibility with existing implementations). */
__md5_process_bytes(salt, salt_len, ctx);
/* Compute alternate MD5 sum with input KEY, SALT, and KEY. The
final result will be added to the first context. */
__md5_init_ctx (alt_ctx);
/* Add key. */
__md5_process_bytes(key, key_len, alt_ctx);
/* Add salt. */
__md5_process_bytes(salt, salt_len, alt_ctx);
/* Add key again. */
__md5_process_bytes(key, key_len, alt_ctx);
/* Now get result of this (16 bytes) and add it to the other
context. */
__md5_finish_ctx(alt_ctx, alt_result);
/* Add for any character in the key one byte of the alternate sum. */
for (cnt = key_len; cnt > 16; cnt -= 16)
__md5_process_bytes (alt_result, 16, ctx);
__md5_process_bytes(alt_result, cnt, ctx);
/* For the following code we need a NUL byte. */
alt_result.Value = 0;
/* The original implementation now does something weird: for every 1
bit in the key the first 0 is added to the buffer, for every 0
bit the first character of the key. This does not seem to be
what was intended but we have to follow this to be compatible. */
for (cnt = key_len; cnt > 0; cnt >>= 1)
__md5_process_bytes((cnt & 1) != 0 ? alt_result : key, 1, ctx);
/* Create intermediate result. */
__md5_finish_ctx (ctx, alt_result);
/* Now comes another weirdness. In fear of password crackers here
comes a quite long loop which just processes the output of the
previous round again. We cannot ignore this here. */
for (cnt = 0; cnt < 1000; ++cnt)
{
/* New context. */
__md5_init_ctx (ctx);
/* Add key or last result. */
if ((cnt & 1) != 0)
__md5_process_bytes (key, key_len, ctx);
else
__md5_process_bytes (alt_result, 16, ctx);
/* Add salt for numbers not divisible by 3. */
if (cnt % 3 != 0)
__md5_process_bytes (salt, salt_len, ctx);
/* Add key for numbers not divisible by 7. */
if (cnt % 7 != 0)
__md5_process_bytes (key, key_len, ctx);
/* Add key or last result. */
if ((cnt & 1) != 0)
__md5_process_bytes (alt_result, 16, ctx);
else
__md5_process_bytes (key, key_len, ctx);
/* Create intermediate result. */
__md5_finish_ctx (ctx, alt_result);
}
/* Now we can construct the result string. It consists of three
parts. */
cp = __stpncpy(buffer, md5_salt_prefix, Math.Max(0, buflen));
buflen -= strlen(md5_salt_prefix);
cp = __stpncpy (cp, salt, Math.Min(Math.Max(0, buflen), salt_len));
buflen -= Math.Min(Math.Max(0, buflen), salt_len);
if (buflen > 0)
{
cp.Value = (byte)'$';
cp++;
buflen--;
}
cp = b64_from_24bit(alt_result[0], alt_result[6], alt_result[12], 4, cp, buflen, out buflen);
cp = b64_from_24bit(alt_result[1], alt_result[7], alt_result[13], 4, cp, buflen, out buflen);
cp = b64_from_24bit(alt_result[2], alt_result[8], alt_result[14], 4, cp, buflen, out buflen);
cp = b64_from_24bit(alt_result[3], alt_result[9], alt_result[15], 4, cp, buflen, out buflen);
cp = b64_from_24bit(alt_result[4], alt_result[10], alt_result[5], 4, cp, buflen, out buflen);
cp = b64_from_24bit(0, 0, alt_result[11], 2, cp, buflen, out buflen);
if (buflen <= 0)
{
throw new IndexOutOfRangeException();
}
else
cp.Value = 0; /* Terminate the string. */
/* Clear the buffer for the intermediate result so that people
attaching to processes or reading core dumps cannot get any
information. We do it in this way to clear correct_words[]
inside the MD5 implementation as well. */
__md5_init_ctx(ctx);
__md5_finish_ctx(ctx, alt_result);
return buffer;
}
private static ArrayPointer<byte> memcpy(ArrayPointer<byte> dest, ArrayPointer<byte> src, int n)
{
for (int i = 0; i < n; i++)
{
dest[i] = src[i];
}
return dest;
}
private static ArrayPointer<byte> mempcpy(ArrayPointer<byte> dest, ArrayPointer<byte> src, int n)
{
for (int i = 0; i < n; i++)
{
dest[i] = src[i];
}
return dest + n;
}
private static ulong strtoul(ArrayPointer<byte> str, out ArrayPointer<byte> endptr, int numberbase)
{
if (numberbase != 10) throw new ArgumentOutOfRangeException("numberbase");
string num = "";
for (int i = 0; str.Value >= '0' && str.Value <= '9'; i++, str++)
{
num += (char)str.Value;
}
endptr = str;
ulong value = 0;
ulong.TryParse(num, out value);
return value;
}
private static ArrayPointer<byte> b64_from_24bit(uint B2, uint B1, uint B0, int N, ArrayPointer<byte> cp, int buflen, out int buflen_out)
{
uint w = ((B2) << 16) | ((B1) << 8) | (B0);
int n = (N);
while (n-- > 0 && buflen > 0)
{
cp.Value = (byte)b64t[w & 0x3f];
cp++;
buflen--;
w >>= 6;
}
buflen_out = buflen;
return cp;
}
private static void __md5_init_ctx(md5_ctx ctx)
{
ctx.stream = new MemoryStream();
}
private static void __md5_process_bytes(ArrayPointer<byte> buffer, int count, md5_ctx ctx)
{
ctx.stream.Write(buffer.SourceArray, buffer.Address, count);
}
private static void __md5_finish_ctx(md5_ctx ctx, ArrayPointer<byte> buffer)
{
byte[] temp = new byte[ctx.stream.Length];
ctx.stream.Seek(0, SeekOrigin.Begin);
ctx.stream.Read(temp, 0, temp.Length);
temp = MD5.Create().ComputeHash(temp);
for (int i = 0; i < temp.Length; i++, buffer++)
{
buffer.Value = temp[i];
}
}
private static void __sha256_init_ctx(sha256_ctx ctx)
{
ctx.stream = new MemoryStream();
}
private static void __sha256_process_bytes(ArrayPointer<byte> buffer, int count, sha256_ctx ctx)
{
ctx.stream.Write(buffer.SourceArray, buffer.Address, count);
}
private static void __sha256_finish_ctx(sha256_ctx ctx, ArrayPointer<byte> buffer)
{
byte[] temp = new byte[ctx.stream.Length];
ctx.stream.Seek(0, SeekOrigin.Begin);
ctx.stream.Read(temp, 0, temp.Length);
temp = SHA256.Create().ComputeHash(temp);
for (int i = 0; i < temp.Length; i++, buffer++)
{
buffer.Value = temp[i];
}
}
private static ArrayPointer<byte> __stpncpy(ArrayPointer<byte> buffer, ArrayPointer<byte> source, int max)
{
int i;
for (i = 0; i < max; i++, buffer++)
{
if (source[i] == 0)
{
break;
}
buffer.Value = source[i];
}
return buffer;
}
private static string ExtractString(ArrayPointer<byte> str)
{
StringBuilder sb = new StringBuilder(strlen(str));
for (int i = 0; ; i++)
{
if (str[i] == 0)
break;
sb.Append((char)str[i]);
}
return sb.ToString();
}
}
}