forked from besser82/libxcrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-badsalt.c
428 lines (401 loc) · 17.3 KB
/
test-badsalt.c
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
419
420
421
422
423
424
425
426
427
428
/* Test rejection of ill-formed password hashes.
Copyright (C) 2012-2018 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
#include "crypt-port.h"
#include <crypt.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
static const char phrase[] = "values of β will give rise to dom!";
struct testcase
{
const char *label;
size_t plen;
const char *setting;
};
static const struct testcase testcases[] =
{
/* These strings are invalid regardless of the algorithm. */
{ "*too short", 1, "/" },
{ "*invalid char :", 1, ":" },
{ "*invalid char ;", 1, ";" },
{ "*invalid char *", 1, "*" },
{ "*invalid char !", 1, "!" },
{ "*invalid char \\", 1, "\\" },
{ "*invalid white 1", 1, " " },
{ "*invalid white 2", 1, "\t" },
{ "*invalid white 3", 1, "\r" },
{ "*invalid white 4", 1, "\n" },
{ "*invalid white 5", 1, "\f" },
{ "*invalid ctrl 1", 1, "\1" },
{ "*invalid ctrl 2", 1, "\177" },
{ "*failure token 1", 2, "*0" },
{ "*failure token 2", 2, "*1" },
{ "*bcrypt invalid salt", 3, "$2$" },
{ "*unsupported algorithm", 13, "$un$upp0rt3d$" },
{ "*empty string", 1, "\0" },
/* Each of these is a valid setting string for some algorithm,
from which we will derive many invalid setting strings.
This is an expensive test, so where possible, the number of
"rounds" of the hash function has been set abnormally low. */
#if INCLUDE_des
{ "DES (trad.)", 2, "Mp" },
{ "*DES (trad.), 1st char invalid -", 2, "-p" },
{ "*DES (trad.), 2nd char invalid -", 2, "M-" },
{ "*DES (trad.), 1st char invalid :", 2, ":p" },
{ "*DES (trad.), 2nd char invalid :", 2, "M:" },
{ "*DES (trad.), 1st char invalid [", 2, "[p" },
{ "*DES (trad.), 2nd char invalid [", 2, "M[" },
{ "*DES (trad.), 1st char invalid {", 2, "{p" },
{ "*DES (trad.), 2nd char invalid {", 2, "M{" },
#endif
#if INCLUDE_des_big
{ "DES (bigcrypt)", 2, "Mp............" },
{ "*DES (bigcrypt), 1st char invalid -", 2, "-p............" },
{ "*DES (bigcrypt), 2nd char invalid -", 2, "M-............" },
{ "*DES (bigcrypt), 1st char invalid :", 2, ":p............" },
{ "*DES (bigcrypt), 2nd char invalid :", 2, "M:............" },
{ "*DES (bigcrypt), 1st char invalid [", 2, "[p............" },
{ "*DES (bigcrypt), 2nd char invalid [", 2, "M[............" },
{ "*DES (bigcrypt), 1st char invalid {", 2, "{p............" },
{ "*DES (bigcrypt), 2nd char invalid {", 2, "M{............" },
#endif
#if INCLUDE_des_xbsd
{ "DES (BSDi)", 9, "_J9..MJHn" },
{ "*DES (BSDi), 1st char invalid -", 9, "_-9..MJHn" },
{ "*DES (BSDi), 2nd char invalid -", 9, "_J-..MJHn" },
{ "*DES (BSDi), 3rd char invalid -", 9, "_J9-.MJHn" },
{ "*DES (BSDi), 4th char invalid -", 9, "_J9.-MJHn" },
{ "*DES (BSDi), 5th char invalid -", 9, "_J9..-JHn" },
{ "*DES (BSDi), 6th char invalid -", 9, "_J9..M-Hn" },
{ "*DES (BSDi), 7th char invalid -", 9, "_J9..MJ-n" },
{ "*DES (BSDi), 8th char invalid -", 9, "_J9..MJH-" },
{ "*DES (BSDi), 1st char invalid :", 9, "_:9..MJHn" },
{ "*DES (BSDi), 2nd char invalid :", 9, "_J:..MJHn" },
{ "*DES (BSDi), 3rd char invalid :", 9, "_J9:.MJHn" },
{ "*DES (BSDi), 4th char invalid :", 9, "_J9.:MJHn" },
{ "*DES (BSDi), 5th char invalid :", 9, "_J9..:JHn" },
{ "*DES (BSDi), 6th char invalid :", 9, "_J9..M:Hn" },
{ "*DES (BSDi), 7th char invalid :", 9, "_J9..MJ:n" },
{ "*DES (BSDi), 8th char invalid :", 9, "_J9..MJH:" },
{ "*DES (BSDi), 1st char invalid [", 9, "_[9..MJHn" },
{ "*DES (BSDi), 2nd char invalid [", 9, "_J[..MJHn" },
{ "*DES (BSDi), 3rd char invalid [", 9, "_J9[.MJHn" },
{ "*DES (BSDi), 4th char invalid [", 9, "_J9.[MJHn" },
{ "*DES (BSDi), 5th char invalid [", 9, "_J9..[JHn" },
{ "*DES (BSDi), 6th char invalid [", 9, "_J9..M[Hn" },
{ "*DES (BSDi), 7th char invalid [", 9, "_J9..MJ[n" },
{ "*DES (BSDi), 8th char invalid [", 9, "_J9..MJH[" },
{ "*DES (BSDi), 1st char invalid {", 9, "_{9..MJHn" },
{ "*DES (BSDi), 2nd char invalid {", 9, "_J{..MJHn" },
{ "*DES (BSDi), 3rd char invalid {", 9, "_J9{.MJHn" },
{ "*DES (BSDi), 4th char invalid {", 9, "_J9.{MJHn" },
{ "*DES (BSDi), 5th char invalid {", 9, "_J9..{JHn" },
{ "*DES (BSDi), 6th char invalid {", 9, "_J9..M{Hn" },
{ "*DES (BSDi), 7th char invalid {", 9, "_J9..MJ{n" },
{ "*DES (BSDi), 8th char invalid {", 9, "_J9..MJH{" },
#endif
#if INCLUDE_md5
{ "MD5 (FreeBSD)", 12, "$1$MJHnaAke$" },
{ "*MD5 (FreeBSD) invalid char", 12, "$1$:JHnaAke$" },
#endif
#if INCLUDE_sunmd5
{ "MD5 (Sun, plain)", 14, "$md5$1xMeE.at$" },
{ "*MD5 (Sun, plain) invalid char", 14, "$md5$:xMeE.at$" },
{ "MD5 (Sun, rounds)", 25, "$md5,rounds=123$1xMeE.at$" },
{ "*MD5 (Sun, rounds) invalid char", 25, "$md5,rounds=123$:xMeE.at$" },
{ "*MD5 (Sun, rounds) invalid rounds 1", 25, "$md5,rounds=:23$1xMeE.at$" },
{ "*MD5 (Sun, rounds) invalid rounds 2", 25, "$md5,rounds=12:$1xMeE.at$" },
{ "*MD5 (Sun, rounds) invalid rounds 3", 25, "$md5,rounds:123$1xMeE.at$" },
{ "*MD5 (Sun, rounds) invalid rounds 4", 22, "$md5,rounds=$1xMeE.at$" },
{ "*MD5 (Sun, rounds) invalid rounds 5", 23, "$md5,rounds=0$1xMeE.at$" },
{ "*MD5 (Sun, rounds) invalid rounds 6", 25, "$md5,rounds=012$1xMeE.at$" },
{ "*MD5 (Sun, rounds) invalid rounds 7", 32, "$md5,rounds=4294967295$1xMeE.at$" },
#endif
#if INCLUDE_nthash
{ "NTHASH (bare)", 3, "$3$" },
{ "NTHASH (fake salt)", 3, "$3$__not_used__c809a450df09a3" },
#endif
#if INCLUDE_sha1
{ "HMAC-SHA1", 27, "$sha1$123$GGXpNqoJvglVTkGU$" },
{ "*HMAC-SHA1 invalid char", 27, "$sha1$123$:GXpNqoJvglVTkGU$" },
{ "*HMAC-SHA1 invalid rounds 1", 27, "$sha1$:23$GGXpNqoJvglVTkGU$" },
{ "*HMAC-SHA1 invalid rounds 2", 27, "$sha1$12:$GGXpNqoJvglVTkGU$" },
{ "*HMAC-SHA1 invalid rounds 3", 27, "$sha1$12:$GGXpNqoJvglVTkGU$" },
#endif
#if INCLUDE_sha256
{ "SHA-256 (plain)", 20, "$5$MJHnaAkegEVYHsFK$" },
{ "*SHA-256 (plain) invalid char", 20, "$5$:JHnaAkegEVYHsFK$" },
{ "SHA-256 (rounds)", 32, "$5$rounds=1000$MJHnaAkegEVYHsFK$" },
{ "*SHA-256 (rounds) invalid rounds 1", 32, "$5$rounds=:000$MJHnaAkegEVYHsFK$" },
{ "*SHA-256 (rounds) invalid rounds 2", 32, "$5$rounds=100:$MJHnaAkegEVYHsFK$" },
{ "*SHA-256 (rounds) invalid rounds 3", 32, "$5$rounds:1000$MJHnaAkegEVYHsFK$" },
{ "*SHA-256 (rounds) invalid rounds 4", 28, "$5$rounds=$MJHnaAkegEVYHsFK$" },
{ "*SHA-256 (rounds) invalid rounds 5", 29, "$5$rounds=0$MJHnaAkegEVYHsFK$" },
{ "*SHA-256 (rounds) invalid rounds 6", 32, "$5$rounds=0100$MJHnaAkegEVYHsFK$" },
{ "*SHA-256 (rounds) invalid rounds 7", 38, "$5$rounds=4294967295$MJHnaAkegEVYHsFK$" },
#endif
#if INCLUDE_sha512
{ "SHA-512 (plain)", 20, "$6$MJHnaAkegEVYHsFK$" },
{ "*SHA-512 (plain) invalid char", 20, "$6$:JHnaAkegEVYHsFK$" },
{ "SHA-512 (rounds)", 32, "$6$rounds=1000$MJHnaAkegEVYHsFK$" },
{ "*SHA-512 (rounds) invalid rounds 1", 32, "$6$rounds=:000$MJHnaAkegEVYHsFK$" },
{ "*SHA-512 (rounds) invalid rounds 2", 32, "$6$rounds=100:$MJHnaAkegEVYHsFK$" },
{ "*SHA-512 (rounds) invalid rounds 3", 32, "$6$rounds:1000$MJHnaAkegEVYHsFK$" },
{ "*SHA-512 (rounds) invalid rounds 4", 28, "$6$rounds=$MJHnaAkegEVYHsFK$" },
{ "*SHA-512 (rounds) invalid rounds 5", 29, "$6$rounds=0$MJHnaAkegEVYHsFK$" },
{ "*SHA-512 (rounds) invalid rounds 6", 32, "$6$rounds=0100$MJHnaAkegEVYHsFK$" },
{ "*SHA-512 (rounds) invalid rounds 6", 38, "$6$rounds=4294967295$MJHnaAkegEVYHsFK$" },
#endif
#if INCLUDE_bcrypt
{ "bcrypt (a04)", 29, "$2a$04$UBVLHeMpJ/QQCv3XqJx8zO" },
{ "*bcrypt (a04) invalid char", 29, "$2a$04$:BVLHeMpJ/QQCv3XqJx8zO" },
{ "*bcrypt (a04) invalid rounds 1", 29, "$2a$:4$UBVLHeMpJ/QQCv3XqJx8zO" },
{ "*bcrypt (a04) invalid rounds 2", 29, "$2a$0:$UBVLHeMpJ/QQCv3XqJx8zO" },
{ "bcrypt (b04)", 29, "$2b$04$UBVLHeMpJ/QQCv3XqJx8zO" },
{ "*bcrypt (b04) invalid char", 29, "$2b$04$:BVLHeMpJ/QQCv3XqJx8zO" },
{ "*bcrypt (b04) invalid rounds 1", 29, "$2b$:4$UBVLHeMpJ/QQCv3XqJx8zO" },
{ "*bcrypt (b04) invalid rounds 2", 29, "$2b$0:$UBVLHeMpJ/QQCv3XqJx8zO" },
{ "bcrypt (x04)", 29, "$2x$04$UBVLHeMpJ/QQCv3XqJx8zO" },
{ "*bcrypt (x04) invalid char", 29, "$2x$04$:BVLHeMpJ/QQCv3XqJx8zO" },
{ "*bcrypt (x04) invalid rounds 1", 29, "$2x$:4$UBVLHeMpJ/QQCv3XqJx8zO" },
{ "*bcrypt (x04) invalid rounds 2", 29, "$2x$0:$UBVLHeMpJ/QQCv3XqJx8zO" },
{ "bcrypt (y04)", 29, "$2y$04$UBVLHeMpJ/QQCv3XqJx8zO" },
{ "*bcrypt (y04) invalid char", 29, "$2y$04$:BVLHeMpJ/QQCv3XqJx8zO" },
{ "*bcrypt (y04) invalid rounds 1", 29, "$2y$:4$UBVLHeMpJ/QQCv3XqJx8zO" },
{ "*bcrypt (y04) invalid rounds 2", 29, "$2y$0:$UBVLHeMpJ/QQCv3XqJx8zO" },
#endif
};
static bool
check_results (const char *label, const char *fn,
const char *retval, const char *setting,
bool expected_to_succeed)
{
size_t l_setting = strlen (setting);
if (expected_to_succeed)
{
if (retval[0] == '*' ||
strncmp (retval, setting,
(setting[l_setting - 1] == '*') ? l_setting - 1 : l_setting))
{
printf ("FAIL: %s/%s/%s: expected success, got non-matching %s\n",
label, setting, fn, retval);
return false;
}
}
else
{
if (retval[0] != '*' ||
(l_setting >= 2 && !strncmp (retval, setting, l_setting)))
{
printf ("FAIL: %s/%s/%s: expected failure, got %s\n",
label, setting, fn, retval);
return false;
}
}
return true;
}
static bool
check_crypt (const char *label, const char *fn,
const char *retval, const char *setting,
bool expected_to_succeed)
{
#if ENABLE_FAILURE_TOKENS
/* crypt/crypt_r never return null when failure tokens are enabled */
if (!retval)
{
printf ("FAIL: %s/%s/%s: returned NULL\n", label, setting, fn);
return false;
}
#else
if (expected_to_succeed && !retval)
{
printf ("FAIL: %s/%s/%s: returned NULL\n", label, setting, fn);
return false;
}
else if (!expected_to_succeed && retval)
{
printf ("FAIL: %s/%s/%s: returned %p, should be NULL\n",
label, setting, fn, (const void *)retval);
return false;
}
else if (!expected_to_succeed && !retval)
return true;
#endif
if (!check_results (label, fn, retval, setting,
expected_to_succeed))
return false;
return true;
}
static bool
check_crypt_rn (const char *label, const char *fn,
const char *retval, const char *output,
const char *setting, bool expected_to_succeed)
{
bool ok = true;
if (expected_to_succeed)
{
if (!retval)
{
printf ("FAIL: %s/%s/%s: returned NULL\n", label, setting, fn);
ok = false;
}
else if (retval != output)
{
printf ("FAIL: %s/%s/%s: returned %p but output is %p\n",
label, setting, fn,
(const void *)retval, (const void *)output);
ok = false;
}
}
else
{
if (retval)
{
printf ("FAIL: %s/%s/%s: returned %p (output is %p), "
"should be NULL\n",
label, setting, fn,
(const void *)retval, (const void *)output);
ok = false;
}
}
if (!check_results (label, fn, output, setting,
expected_to_succeed))
ok = false;
return ok;
}
static bool
test_one_setting (const char *label, const char *setting,
struct crypt_data *cd, bool expected_to_succeed)
{
bool ok = true;
const char *retval;
int cdsize = (int) sizeof (struct crypt_data);
#ifdef VERBOSE
printf ("%s: testing %s (expect: %s)\n", label, setting,
expected_to_succeed ? "succeed" : "fail");
#endif
retval = crypt (phrase, setting);
if (!check_crypt (label, "crypt", retval, setting, expected_to_succeed))
ok = false;
retval = crypt_r (phrase, setting, cd);
if (!check_crypt (label, "crypt_r", retval, setting, expected_to_succeed))
ok = false;
retval = crypt_rn (phrase, setting, cd, cdsize);
if (!check_crypt_rn (label, "crypt_rn", retval, cd->output,
setting, expected_to_succeed))
ok = false;
retval = crypt_ra (phrase, setting, (void **)&cd, &cdsize);
if (!check_crypt_rn (label, "crypt_ra", retval, cd->output,
setting, expected_to_succeed))
ok = false;
return ok;
}
static bool
test_one_case (const struct testcase *t,
char *page, size_t pagesize,
struct crypt_data *cd)
{
memset (page, 'a', pagesize);
size_t l_setting = strlen (t->setting);
assert (l_setting <= pagesize);
if (t->label[0] == '*')
{
/* Hashing with this setting is expected to fail already.
We still want to verify that we do not read past the end of
the string. */
char *p = page + pagesize - (l_setting + 1);
memcpy (p, t->setting, l_setting + 1);
if (!test_one_setting (t->label + 1, p, cd, false))
return false;
printf ("PASS: %s\n", t->label + 1);
return true;
}
else
{
/* Hashing with this setting is expected to succeed. */
char goodhash[CRYPT_OUTPUT_SIZE];
char *result = crypt_rn (phrase, t->setting, cd,
sizeof (struct crypt_data));
if (!result)
{
printf ("FAIL: %s: initial hash returned NULL/%s (%s)\n",
t->label, cd->output, strerror (errno));
return false;
}
size_t l_hash = strlen (result);
assert (l_hash + 1 <= CRYPT_OUTPUT_SIZE);
memcpy (goodhash, result, l_hash + 1);
char *p = page + pagesize - (l_hash + 1);
memcpy (p, goodhash, l_hash + 1);
/* Rechecking the hash with the full output should succeed. */
if (!test_one_setting (t->label, p, cd, true))
return false;
/* Recomputing the hash with its own prefix should produce a
hash with the same prefix. */
p = page + pagesize - (t->plen + 1);
memcpy (p, goodhash, t->plen);
p[t->plen] = '\0';
if (!test_one_setting (t->label, p, cd, true))
return false;
/* An invalid character after the prefix should not affect the
result of the hash computation. */
p = page + pagesize - (t->plen + 2);
memcpy (p, goodhash, t->plen);
p[t->plen] = '*';
p[t->plen+1] = '\0';
if (!test_one_setting (t->label, p, cd, true))
return false;
/* However, an invalid character anywhere within the prefix should
cause hashing to fail. */
for (size_t i = 1; i < t->plen; i++)
{
p = page + pagesize - (t->plen + 2 - i);
memcpy (p, goodhash, t->plen - i);
if (!test_one_setting (t->label, p, cd, false))
return false;
}
printf ("PASS: %s\n", t->label);
return true;
}
}
int
main (void)
{
/* Set up a two-page region whose first page is read-write and
whose second page is inaccessible. */
size_t pagesize = (size_t) sysconf (_SC_PAGESIZE);
char *page = mmap (0, pagesize * 2, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANON, -1, 0);
if (page == MAP_FAILED)
{
perror ("mmap");
return 1;
}
memset (page, 'x', pagesize * 2);
if (mprotect (page + pagesize, pagesize, PROT_NONE))
{
perror ("mprotect");
return 1;
}
struct crypt_data cd;
memset (&cd, 0, sizeof cd);
bool ok = true;
for (size_t i = 0; i < ARRAY_SIZE (testcases); i++)
if (!test_one_case (&testcases[i], page, pagesize, &cd))
ok = false;
return ok ? 0 : 1;
}