| 1 |
#include "crypt-port.h" |
| 2 |
|
| 3 |
#include <errno.h> |
| 4 |
#include <stdio.h> |
| 5 |
#include <stdlib.h> |
| 6 |
|
| 7 |
static const char *const entropy[] = |
| 8 |
{ |
| 9 |
"\x58\x35\xcd\x26\x03\xab\x2c\x14\x92\x13\x1e\x59\xb0\xbc\xfe\xd5", |
| 10 |
"\x9b\x35\xa2\x45\xeb\x68\x9e\x8f\xd9\xa9\x09\x71\xcc\x4d\x21\x44", |
| 11 |
"\x25\x13\xc5\x94\xc3\x93\x1d\xf4\xfd\xd4\x4f\xbd\x10\xe5\x28\x08", |
| 12 |
"\xa0\x2d\x35\x70\xa8\x0b\xc3\xad\xdf\x61\x69\xb3\x19\xda\x7e\x8d", |
| 13 |
0 |
| 14 |
}; |
| 15 |
|
| 16 |
#if INCLUDE_descrypt |
| 17 |
static const char *const des_expected_output[] = { "Mp", "Pp", "ZH", "Uh"}; |
| 18 |
#endif |
| 19 |
#if INCLUDE_bigcrypt && !INCLUDE_descrypt |
| 20 |
static const char *const big_expected_output[] = |
| 21 |
{ |
| 22 |
"Mp............", |
| 23 |
"Pp............", |
| 24 |
"ZH............", |
| 25 |
"Uh............" |
| 26 |
}; |
| 27 |
#endif |
| 28 |
#if INCLUDE_bsdicrypt |
| 29 |
static const char *const bsdi_expected_output[] = |
| 30 |
{ |
| 31 |
"_J9..MJHn", |
| 32 |
"_J9..PKXc", |
| 33 |
"_J9..ZAFl", |
| 34 |
"_J9..UqGB" |
| 35 |
}; |
| 36 |
static const char *const bsdi_expected_output_r[] = |
| 37 |
{ |
| 38 |
"_/.2.MJHn", |
| 39 |
"_/.2.PKXc", |
| 40 |
"_/.2.ZAFl", |
| 41 |
"_/.2.UqGB" |
| 42 |
}; |
| 43 |
static const char *const bsdi_expected_output_l[] = |
| 44 |
{ |
| 45 |
"_/...MJHn", |
| 46 |
"_/...PKXc", |
| 47 |
"_/...ZAFl", |
| 48 |
"_/...UqGB" |
| 49 |
}; |
| 50 |
static const char *const bsdi_expected_output_h[] = |
| 51 |
{ |
| 52 |
"_zzzzMJHn", |
| 53 |
"_zzzzPKXc", |
| 54 |
"_zzzzZAFl", |
| 55 |
"_zzzzUqGB" |
| 56 |
}; |
| 57 |
#endif |
| 58 |
#if INCLUDE_md5crypt |
| 59 |
static const char *const md5_expected_output[] = |
| 60 |
{ |
| 61 |
"$1$MJHnaAke", |
| 62 |
"$1$PKXc3hCO", |
| 63 |
"$1$ZAFlICwY", |
| 64 |
"$1$UqGBkVu0" |
| 65 |
}; |
| 66 |
#endif |
| 67 |
#if INCLUDE_sunmd5 |
| 68 |
static const char *const sunmd5_expected_output[] = |
| 69 |
{ |
| 70 |
"$md5,rounds=55349$BPm.fm03$", |
| 71 |
"$md5,rounds=72501$WKoucttX$", |
| 72 |
"$md5,rounds=42259$3HtkHq/x$", |
| 73 |
"$md5,rounds=73773$p.5e9AQf$", |
| 74 |
}; |
| 75 |
static const char *const sunmd5_expected_output_l[] = |
| 76 |
{ |
| 77 |
"$md5,rounds=55349$BPm.fm03$", |
| 78 |
"$md5,rounds=72501$WKoucttX$", |
| 79 |
"$md5,rounds=42259$3HtkHq/x$", |
| 80 |
"$md5,rounds=73773$p.5e9AQf$", |
| 81 |
}; |
| 82 |
static const char *const sunmd5_expected_output_h[] = |
| 83 |
{ |
| 84 |
"$md5,rounds=4294924340$BPm.fm03$", |
| 85 |
"$md5,rounds=4294941492$WKoucttX$", |
| 86 |
"$md5,rounds=4294911250$3HtkHq/x$", |
| 87 |
"$md5,rounds=4294942764$p.5e9AQf$", |
| 88 |
}; |
| 89 |
#endif |
| 90 |
#if INCLUDE_sha1crypt |
| 91 |
static const char *const sha1_expected_output[] = |
| 92 |
{ |
| 93 |
"$sha1$248488$ggu.H673kaZ5$", |
| 94 |
"$sha1$248421$SWqudaxXA5L0$", |
| 95 |
"$sha1$257243$RAtkIrDxEovH$", |
| 96 |
"$sha1$250464$1j.eVxRfNAPO$", |
| 97 |
}; |
| 98 |
static const char *const sha1_expected_output_l[] = |
| 99 |
{ |
| 100 |
"$sha1$4$ggu.H673kaZ5$", |
| 101 |
"$sha1$4$SWqudaxXA5L0$", |
| 102 |
"$sha1$4$RAtkIrDxEovH$", |
| 103 |
"$sha1$4$1j.eVxRfNAPO$", |
| 104 |
}; |
| 105 |
static const char *const sha1_expected_output_h[] = |
| 106 |
{ |
| 107 |
"$sha1$3643984551$ggu.H673kaZ5$", |
| 108 |
"$sha1$4200450659$SWqudaxXA5L0$", |
| 109 |
"$sha1$3946507480$RAtkIrDxEovH$", |
| 110 |
"$sha1$3486175838$1j.eVxRfNAPO$", |
| 111 |
}; |
| 112 |
#endif |
| 113 |
#if INCLUDE_sha256crypt |
| 114 |
static const char *const sha256_expected_output[] = |
| 115 |
{ |
| 116 |
"$5$MJHnaAkegEVYHsFK", |
| 117 |
"$5$PKXc3hCOSyMqdaEQ", |
| 118 |
"$5$ZAFlICwYRETzIzIj", |
| 119 |
"$5$UqGBkVu01rurVZqg" |
| 120 |
}; |
| 121 |
static const char *const sha256_expected_output_r[] = |
| 122 |
{ |
| 123 |
"$5$rounds=10191$MJHnaAkegEVYHsFK", |
| 124 |
"$5$rounds=10191$PKXc3hCOSyMqdaEQ", |
| 125 |
"$5$rounds=10191$ZAFlICwYRETzIzIj", |
| 126 |
"$5$rounds=10191$UqGBkVu01rurVZqg" |
| 127 |
}; |
| 128 |
static const char *const sha256_expected_output_l[] = |
| 129 |
{ |
| 130 |
"$5$rounds=1000$MJHnaAkegEVYHsFK", |
| 131 |
"$5$rounds=1000$PKXc3hCOSyMqdaEQ", |
| 132 |
"$5$rounds=1000$ZAFlICwYRETzIzIj", |
| 133 |
"$5$rounds=1000$UqGBkVu01rurVZqg" |
| 134 |
}; |
| 135 |
static const char *const sha256_expected_output_h[] = |
| 136 |
{ |
| 137 |
"$5$rounds=999999999$MJHnaAkegEVYHsFK", |
| 138 |
"$5$rounds=999999999$PKXc3hCOSyMqdaEQ", |
| 139 |
"$5$rounds=999999999$ZAFlICwYRETzIzIj", |
| 140 |
"$5$rounds=999999999$UqGBkVu01rurVZqg" |
| 141 |
}; |
| 142 |
#endif |
| 143 |
#if INCLUDE_sha512crypt |
| 144 |
static const char *const sha512_expected_output[] = |
| 145 |
{ |
| 146 |
"$6$MJHnaAkegEVYHsFK", |
| 147 |
"$6$PKXc3hCOSyMqdaEQ", |
| 148 |
"$6$ZAFlICwYRETzIzIj", |
| 149 |
"$6$UqGBkVu01rurVZqg" |
| 150 |
}; |
| 151 |
static const char *const sha512_expected_output_r[] = |
| 152 |
{ |
| 153 |
"$6$rounds=10191$MJHnaAkegEVYHsFK", |
| 154 |
"$6$rounds=10191$PKXc3hCOSyMqdaEQ", |
| 155 |
"$6$rounds=10191$ZAFlICwYRETzIzIj", |
| 156 |
"$6$rounds=10191$UqGBkVu01rurVZqg" |
| 157 |
}; |
| 158 |
static const char *const sha512_expected_output_l[] = |
| 159 |
{ |
| 160 |
"$6$rounds=1000$MJHnaAkegEVYHsFK", |
| 161 |
"$6$rounds=1000$PKXc3hCOSyMqdaEQ", |
| 162 |
"$6$rounds=1000$ZAFlICwYRETzIzIj", |
| 163 |
"$6$rounds=1000$UqGBkVu01rurVZqg" |
| 164 |
}; |
| 165 |
static const char *const sha512_expected_output_h[] = |
| 166 |
{ |
| 167 |
"$6$rounds=999999999$MJHnaAkegEVYHsFK", |
| 168 |
"$6$rounds=999999999$PKXc3hCOSyMqdaEQ", |
| 169 |
"$6$rounds=999999999$ZAFlICwYRETzIzIj", |
| 170 |
"$6$rounds=999999999$UqGBkVu01rurVZqg" |
| 171 |
}; |
| 172 |
#endif |
| 173 |
#if INCLUDE_sm3crypt |
| 174 |
static const char *const sm3_expected_output[] = |
| 175 |
{ |
| 176 |
"$sm3$MJHnaAkegEVYHsFK", |
| 177 |
"$sm3$PKXc3hCOSyMqdaEQ", |
| 178 |
"$sm3$ZAFlICwYRETzIzIj", |
| 179 |
"$sm3$UqGBkVu01rurVZqg" |
| 180 |
}; |
| 181 |
static const char *const sm3_expected_output_r[] = |
| 182 |
{ |
| 183 |
"$sm3$rounds=10191$MJHnaAkegEVYHsFK", |
| 184 |
"$sm3$rounds=10191$PKXc3hCOSyMqdaEQ", |
| 185 |
"$sm3$rounds=10191$ZAFlICwYRETzIzIj", |
| 186 |
"$sm3$rounds=10191$UqGBkVu01rurVZqg" |
| 187 |
}; |
| 188 |
static const char *const sm3_expected_output_l[] = |
| 189 |
{ |
| 190 |
"$sm3$rounds=1000$MJHnaAkegEVYHsFK", |
| 191 |
"$sm3$rounds=1000$PKXc3hCOSyMqdaEQ", |
| 192 |
"$sm3$rounds=1000$ZAFlICwYRETzIzIj", |
| 193 |
"$sm3$rounds=1000$UqGBkVu01rurVZqg" |
| 194 |
}; |
| 195 |
static const char *const sm3_expected_output_h[] = |
| 196 |
{ |
| 197 |
"$sm3$rounds=999999999$MJHnaAkegEVYHsFK", |
| 198 |
"$sm3$rounds=999999999$PKXc3hCOSyMqdaEQ", |
| 199 |
"$sm3$rounds=999999999$ZAFlICwYRETzIzIj", |
| 200 |
"$sm3$rounds=999999999$UqGBkVu01rurVZqg" |
| 201 |
}; |
| 202 |
#endif |
| 203 |
#if INCLUDE_bcrypt |
| 204 |
static const char *const bcrypt_b_expected_output[] = |
| 205 |
{ |
| 206 |
"$2b$05$UBVLHeMpJ/QQCv3XqJx8zO", |
| 207 |
"$2b$05$kxUgPcrmlm9XoOjvxCyfP.", |
| 208 |
"$2b$05$HPNDjKMRFdR7zC87CMSmA.", |
| 209 |
"$2b$05$mAyzaIeJu41dWUkxEbn8hO" |
| 210 |
}; |
| 211 |
static const char *const bcrypt_b_expected_output_l[] = |
| 212 |
{ |
| 213 |
"$2b$04$UBVLHeMpJ/QQCv3XqJx8zO", |
| 214 |
"$2b$04$kxUgPcrmlm9XoOjvxCyfP.", |
| 215 |
"$2b$04$HPNDjKMRFdR7zC87CMSmA.", |
| 216 |
"$2b$04$mAyzaIeJu41dWUkxEbn8hO" |
| 217 |
}; |
| 218 |
static const char *const bcrypt_b_expected_output_h[] = |
| 219 |
{ |
| 220 |
"$2b$31$UBVLHeMpJ/QQCv3XqJx8zO", |
| 221 |
"$2b$31$kxUgPcrmlm9XoOjvxCyfP.", |
| 222 |
"$2b$31$HPNDjKMRFdR7zC87CMSmA.", |
| 223 |
"$2b$31$mAyzaIeJu41dWUkxEbn8hO" |
| 224 |
}; |
| 225 |
#endif |
| 226 |
#if INCLUDE_bcrypt_a |
| 227 |
static const char *const bcrypt_a_expected_output[] = |
| 228 |
{ |
| 229 |
"$2a$05$UBVLHeMpJ/QQCv3XqJx8zO", |
| 230 |
"$2a$05$kxUgPcrmlm9XoOjvxCyfP.", |
| 231 |
"$2a$05$HPNDjKMRFdR7zC87CMSmA.", |
| 232 |
"$2a$05$mAyzaIeJu41dWUkxEbn8hO" |
| 233 |
}; |
| 234 |
#endif |
| 235 |
#if INCLUDE_bcrypt_y |
| 236 |
static const char *const bcrypt_y_expected_output[] = |
| 237 |
{ |
| 238 |
"$2y$05$UBVLHeMpJ/QQCv3XqJx8zO", |
| 239 |
"$2y$05$kxUgPcrmlm9XoOjvxCyfP.", |
| 240 |
"$2y$05$HPNDjKMRFdR7zC87CMSmA.", |
| 241 |
"$2y$05$mAyzaIeJu41dWUkxEbn8hO" |
| 242 |
}; |
| 243 |
#endif |
| 244 |
#if INCLUDE_yescrypt |
| 245 |
static const char *yescrypt_expected_output[] = |
| 246 |
{ |
| 247 |
"$y$j9T$MJHnaAkegEVYHsFKkmfzJ1", |
| 248 |
"$y$j9T$PKXc3hCOSyMqdaEQArI62/", |
| 249 |
"$y$j9T$ZAFlICwYRETzIzIjEIC86.", |
| 250 |
"$y$j9T$UqGBkVu01rurVZqgNchTB0" |
| 251 |
}; |
| 252 |
static const char *yescrypt_expected_output_l[] = |
| 253 |
{ |
| 254 |
"$y$j75$MJHnaAkegEVYHsFKkmfzJ1", |
| 255 |
"$y$j75$PKXc3hCOSyMqdaEQArI62/", |
| 256 |
"$y$j75$ZAFlICwYRETzIzIjEIC86.", |
| 257 |
"$y$j75$UqGBkVu01rurVZqgNchTB0" |
| 258 |
}; |
| 259 |
static const char *yescrypt_expected_output_h[] = |
| 260 |
{ |
| 261 |
"$y$jFT$MJHnaAkegEVYHsFKkmfzJ1", |
| 262 |
"$y$jFT$PKXc3hCOSyMqdaEQArI62/", |
| 263 |
"$y$jFT$ZAFlICwYRETzIzIjEIC86.", |
| 264 |
"$y$jFT$UqGBkVu01rurVZqgNchTB0" |
| 265 |
}; |
| 266 |
#endif |
| 267 |
#if INCLUDE_scrypt |
| 268 |
static const char *scrypt_expected_output[] = |
| 269 |
{ |
| 270 |
"$7$CU..../....MJHnaAkegEVYHsFKkmfzJ1", |
| 271 |
"$7$CU..../....PKXc3hCOSyMqdaEQArI62/", |
| 272 |
"$7$CU..../....ZAFlICwYRETzIzIjEIC86.", |
| 273 |
"$7$CU..../....UqGBkVu01rurVZqgNchTB0" |
| 274 |
}; |
| 275 |
static const char *scrypt_expected_output_l[] = |
| 276 |
{ |
| 277 |
"$7$BU..../....MJHnaAkegEVYHsFKkmfzJ1", |
| 278 |
"$7$BU..../....PKXc3hCOSyMqdaEQArI62/", |
| 279 |
"$7$BU..../....ZAFlICwYRETzIzIjEIC86.", |
| 280 |
"$7$BU..../....UqGBkVu01rurVZqgNchTB0" |
| 281 |
}; |
| 282 |
static const char *scrypt_expected_output_h[] = |
| 283 |
{ |
| 284 |
"$7$GU..../....MJHnaAkegEVYHsFKkmfzJ1", |
| 285 |
"$7$GU..../....PKXc3hCOSyMqdaEQArI62/", |
| 286 |
"$7$GU..../....ZAFlICwYRETzIzIjEIC86.", |
| 287 |
"$7$GU..../....UqGBkVu01rurVZqgNchTB0" |
| 288 |
}; |
| 289 |
#endif |
| 290 |
#if INCLUDE_gost_yescrypt |
| 291 |
static const char *gost_yescrypt_expected_output[] = |
| 292 |
{ |
| 293 |
"$gy$j9T$MJHnaAkegEVYHsFKkmfzJ1", |
| 294 |
"$gy$j9T$PKXc3hCOSyMqdaEQArI62/", |
| 295 |
"$gy$j9T$ZAFlICwYRETzIzIjEIC86.", |
| 296 |
"$gy$j9T$UqGBkVu01rurVZqgNchTB0" |
| 297 |
}; |
| 298 |
static const char *gost_yescrypt_expected_output_l[] = |
| 299 |
{ |
| 300 |
"$gy$j75$MJHnaAkegEVYHsFKkmfzJ1", |
| 301 |
"$gy$j75$PKXc3hCOSyMqdaEQArI62/", |
| 302 |
"$gy$j75$ZAFlICwYRETzIzIjEIC86.", |
| 303 |
"$gy$j75$UqGBkVu01rurVZqgNchTB0" |
| 304 |
}; |
| 305 |
static const char *gost_yescrypt_expected_output_h[] = |
| 306 |
{ |
| 307 |
"$gy$jFT$MJHnaAkegEVYHsFKkmfzJ1", |
| 308 |
"$gy$jFT$PKXc3hCOSyMqdaEQArI62/", |
| 309 |
"$gy$jFT$ZAFlICwYRETzIzIjEIC86.", |
| 310 |
"$gy$jFT$UqGBkVu01rurVZqgNchTB0" |
| 311 |
}; |
| 312 |
#endif |
| 313 |
#if INCLUDE_sm3_yescrypt |
| 314 |
static const char *const sm3_yescrypt_expected_output[] = |
| 315 |
{ |
| 316 |
"$sm3y$j9T$MJHnaAkegEVYHsFKkmfzJ1", |
| 317 |
"$sm3y$j9T$PKXc3hCOSyMqdaEQArI62/", |
| 318 |
"$sm3y$j9T$ZAFlICwYRETzIzIjEIC86.", |
| 319 |
"$sm3y$j9T$UqGBkVu01rurVZqgNchTB0" |
| 320 |
}; |
| 321 |
static const char *const sm3_yescrypt_expected_output_l[] = |
| 322 |
{ |
| 323 |
"$sm3y$j75$MJHnaAkegEVYHsFKkmfzJ1", |
| 324 |
"$sm3y$j75$PKXc3hCOSyMqdaEQArI62/", |
| 325 |
"$sm3y$j75$ZAFlICwYRETzIzIjEIC86.", |
| 326 |
"$sm3y$j75$UqGBkVu01rurVZqgNchTB0" |
| 327 |
}; |
| 328 |
static const char *const sm3_yescrypt_expected_output_h[] = |
| 329 |
{ |
| 330 |
"$sm3y$jFT$MJHnaAkegEVYHsFKkmfzJ1", |
| 331 |
"$sm3y$jFT$PKXc3hCOSyMqdaEQArI62/", |
| 332 |
"$sm3y$jFT$ZAFlICwYRETzIzIjEIC86.", |
| 333 |
"$sm3y$jFT$UqGBkVu01rurVZqgNchTB0" |
| 334 |
}; |
| 335 |
#endif |
| 336 |
|
| 337 |
struct testcase |
| 338 |
{ |
| 339 |
const char *prefix; |
| 340 |
const char *const *expected_output; |
| 341 |
unsigned int expected_len; |
| 342 |
unsigned int expected_auto_len; |
| 343 |
unsigned long rounds; |
| 344 |
}; |
| 345 |
|
| 346 |
// For all hashing methods with a linear cost parameter (that is, |
| 347 |
// DES/BSD, MD5/Sun, SM3, SHA1, SHA256, and SHA512), crypt_gensalt will |
| 348 |
// accept any value in the range of 'unsigned long' and clip it to the |
| 349 |
// actual valid range. |
| 350 |
#define MIN_LINEAR_COST 1 |
| 351 |
#define MAX_LINEAR_COST ULONG_MAX |
| 352 |
|
| 353 |
static const struct testcase testcases[] = |
| 354 |
{ |
| 355 |
#if INCLUDE_descrypt |
| 356 |
{ "", des_expected_output, 2, 0, 0 }, |
| 357 |
// DES doesn't have variable round count. |
| 358 |
#endif |
| 359 |
#if INCLUDE_bigcrypt && !INCLUDE_descrypt |
| 360 |
{ "", big_expected_output, 14, 0, 0 }, |
| 361 |
// bigcrypt doesn't have variable round count. |
| 362 |
#endif |
| 363 |
#if INCLUDE_bsdicrypt |
| 364 |
{ "_", bsdi_expected_output, 9, 0, 0 }, |
| 365 |
// BSDI/DES always emits a round count. |
| 366 |
// The _r expectation is used to verify that even inputs are |
| 367 |
// made odd, rather than rejected. |
| 368 |
{ "_", bsdi_expected_output_r, 9, 0, 16384 }, |
| 369 |
{ "_", bsdi_expected_output_l, 9, 0, MIN_LINEAR_COST }, |
| 370 |
{ "_", bsdi_expected_output_h, 9, 0, MAX_LINEAR_COST }, |
| 371 |
#endif |
| 372 |
#if INCLUDE_md5crypt |
| 373 |
{ "$1$", md5_expected_output, 11, 0, 0 }, |
| 374 |
// MD5/BSD doesn't have variable round count. |
| 375 |
#endif |
| 376 |
#if INCLUDE_sunmd5 |
| 377 |
{ "$md5", sunmd5_expected_output, 27, 0, 0 }, |
| 378 |
// MD5/Sun always emits a round count. |
| 379 |
{ "$md5", sunmd5_expected_output_l, 27, 0, MIN_LINEAR_COST }, |
| 380 |
{ "$md5", sunmd5_expected_output_h, 32, 0, MAX_LINEAR_COST }, |
| 381 |
#endif |
| 382 |
#if INCLUDE_sm3crypt |
| 383 |
{ "$sm3$", sm3_expected_output, 21, 0, 0 }, |
| 384 |
{ "$sm3$", sm3_expected_output_r, 34, 0, 10191 }, |
| 385 |
{ "$sm3$", sm3_expected_output_l, 33, 0, MIN_LINEAR_COST }, |
| 386 |
{ "$sm3$", sm3_expected_output_h, 38, 0, MAX_LINEAR_COST }, |
| 387 |
#endif |
| 388 |
#if INCLUDE_sha1crypt |
| 389 |
{ "$sha1", sha1_expected_output, 26, 34, 0 }, |
| 390 |
// SHA1/PBKDF always emits a round count. |
| 391 |
{ "$sha1", sha1_expected_output_l, 21, 29, MIN_LINEAR_COST }, |
| 392 |
{ "$sha1", sha1_expected_output_h, 30, 38, MAX_LINEAR_COST }, |
| 393 |
#endif |
| 394 |
#if INCLUDE_sha256crypt |
| 395 |
{ "$5$", sha256_expected_output, 19, 0, 0 }, |
| 396 |
{ "$5$", sha256_expected_output_r, 32, 0, 10191 }, |
| 397 |
{ "$5$", sha256_expected_output_l, 31, 0, MIN_LINEAR_COST }, |
| 398 |
{ "$5$", sha256_expected_output_h, 36, 0, MAX_LINEAR_COST }, |
| 399 |
#endif |
| 400 |
#if INCLUDE_sha512crypt |
| 401 |
{ "$6$", sha512_expected_output, 19, 0, 0 }, |
| 402 |
{ "$6$", sha512_expected_output_r, 32, 0, 10191 }, |
| 403 |
{ "$6$", sha512_expected_output_l, 31, 0, MIN_LINEAR_COST }, |
| 404 |
{ "$6$", sha512_expected_output_h, 36, 0, MAX_LINEAR_COST }, |
| 405 |
#endif |
| 406 |
#if INCLUDE_bcrypt |
| 407 |
{ "$2b$", bcrypt_b_expected_output, 29, 0, 0 }, |
| 408 |
// bcrypt always emits a cost parameter. |
| 409 |
// bcrypt's cost parameter is exponential, not linear, and |
| 410 |
// values outside the documented range are errors. |
| 411 |
{ "$2b$", bcrypt_b_expected_output_l, 29, 0, 4 }, |
| 412 |
{ "$2b$", bcrypt_b_expected_output_h, 29, 0, 31 }, |
| 413 |
#endif |
| 414 |
// Salt generation for legacy bcrypt variants uses the same code as |
| 415 |
// the 'b' variant, so we don't bother testing them on non-default |
| 416 |
// rounds. |
| 417 |
#if INCLUDE_bcrypt_a |
| 418 |
{ "$2a$", bcrypt_a_expected_output, 29, 0, 0 }, |
| 419 |
#endif |
| 420 |
#if INCLUDE_bcrypt_y |
| 421 |
{ "$2y$", bcrypt_y_expected_output, 29, 0, 0 }, |
| 422 |
#endif |
| 423 |
#if INCLUDE_yescrypt |
| 424 |
{ "$y$", yescrypt_expected_output, 29, 29, 0 }, |
| 425 |
{ "$y$", yescrypt_expected_output_l, 29, 29, 1 }, |
| 426 |
{ "$y$", yescrypt_expected_output_h, 29, 29, 11 }, |
| 427 |
#endif |
| 428 |
#if INCLUDE_scrypt |
| 429 |
{ "$7$", scrypt_expected_output, 36, 36, 0 }, |
| 430 |
{ "$7$", scrypt_expected_output_l, 36, 36, 6 }, |
| 431 |
{ "$7$", scrypt_expected_output_h, 36, 36, 11 }, |
| 432 |
#endif |
| 433 |
#if INCLUDE_gost_yescrypt |
| 434 |
{ "$gy$", gost_yescrypt_expected_output, 30, 30, 0 }, |
| 435 |
{ "$gy$", gost_yescrypt_expected_output_l, 30, 30, 1 }, |
| 436 |
{ "$gy$", gost_yescrypt_expected_output_h, 30, 30, 11 }, |
| 437 |
#endif |
| 438 |
#if INCLUDE_sm3_yescrypt |
| 439 |
{ "$sm3y$", sm3_yescrypt_expected_output, 32, 32, 0 }, |
| 440 |
{ "$sm3y$", sm3_yescrypt_expected_output_l, 32, 32, 1 }, |
| 441 |
{ "$sm3y$", sm3_yescrypt_expected_output_h, 32, 32, 11 }, |
| 442 |
#endif |
| 443 |
{ 0, 0, 0, 0, 0 } |
| 444 |
}; |
| 445 |
|
| 446 |
/* The "best available" hashing method. */ |
| 447 |
#if INCLUDE_yescrypt |
| 448 |
# define EXPECTED_DEFAULT_PREFIX "$y$" |
| 449 |
#elif INCLUDE_bcrypt |
| 450 |
# define EXPECTED_DEFAULT_PREFIX "$2b$" |
| 451 |
#elif INCLUDE_sha512crypt |
| 452 |
# define EXPECTED_DEFAULT_PREFIX "$6$" |
| 453 |
#endif |
| 454 |
|
| 455 |
#if CRYPT_GENSALT_IMPLEMENTS_DEFAULT_PREFIX |
| 456 |
# ifndef EXPECTED_DEFAULT_PREFIX |
| 457 |
# error "Which hashing algorithm is the default?" |
| 458 |
# endif |
| 459 |
#else |
| 460 |
# ifdef EXPECTED_DEFAULT_PREFIX |
| 461 |
# error "Default hashing algorithm should be available" |
| 462 |
# endif |
| 463 |
#endif |
| 464 |
|
| 465 |
int |
| 466 |
main (void) |
| 467 |
{ |
| 468 |
int status = 0; |
| 469 |
unsigned int ent; |
| 470 |
const struct testcase *tcase; |
| 471 |
char output[CRYPT_GENSALT_OUTPUT_SIZE]; |
| 472 |
char prev_output[CRYPT_GENSALT_OUTPUT_SIZE]; |
| 473 |
|
| 474 |
for (tcase = testcases; tcase->prefix; tcase++) |
| 475 |
{ |
| 476 |
memset (prev_output, 0, CRYPT_GENSALT_OUTPUT_SIZE); |
| 477 |
for (ent = 0; ent < ARRAY_SIZE (entropy); ent++) |
| 478 |
{ |
| 479 |
memset (output, 0, CRYPT_GENSALT_OUTPUT_SIZE); |
| 480 |
char *salt = crypt_gensalt_rn (tcase->prefix, tcase->rounds, |
| 481 |
entropy[ent], 16, |
| 482 |
output, CRYPT_GENSALT_OUTPUT_SIZE); |
| 483 |
if (salt == 0) |
| 484 |
{ |
| 485 |
if (entropy[ent] == 0 && errno == ENOSYS) |
| 486 |
{ |
| 487 |
fprintf (stderr, |
| 488 |
"UNSUPPORTED: %s/%lu/auto-entropy -> ENOSYS\n", |
| 489 |
tcase->prefix, tcase->rounds); |
| 490 |
} |
| 491 |
else |
| 492 |
{ |
| 493 |
fprintf (stderr, "ERROR: %s/%lu/%u -> 0\n", |
| 494 |
tcase->prefix, tcase->rounds, ent); |
| 495 |
status = 1; |
| 496 |
} |
| 497 |
continue; |
| 498 |
} |
| 499 |
size_t slen = strlen (salt); |
| 500 |
unsigned int expected_len = |
| 501 |
(!entropy[ent] && tcase->expected_auto_len) ? |
| 502 |
tcase->expected_auto_len : tcase->expected_len; |
| 503 |
if (slen != expected_len) |
| 504 |
{ |
| 505 |
fprintf (stderr, |
| 506 |
"ERROR: %s/%lu/%u -> %s (expected len=%u got %zu)\n", |
| 507 |
tcase->prefix, tcase->rounds, ent, salt, |
| 508 |
expected_len, slen); |
| 509 |
status = 1; |
| 510 |
} |
| 511 |
else if (strncmp (salt, tcase->prefix, strlen (tcase->prefix))) |
| 512 |
{ |
| 513 |
fprintf (stderr, "ERROR: %s/%lu/%u -> %s (prefix wrong)\n", |
| 514 |
tcase->prefix, tcase->rounds, ent, salt); |
| 515 |
status = 1; |
| 516 |
} |
| 517 |
else if (!strcmp (salt, prev_output)) |
| 518 |
{ |
| 519 |
fprintf (stderr, "ERROR: %s/%lu/%u -> %s (same as prev)\n", |
| 520 |
tcase->prefix, tcase->rounds, ent, salt); |
| 521 |
status = 1; |
| 522 |
} |
| 523 |
else if (entropy[ent] && strcmp (salt, tcase->expected_output[ent])) |
| 524 |
{ |
| 525 |
fprintf (stderr, "ERROR: %s/%lu/%u -> %s (expected %s)\n", |
| 526 |
tcase->prefix, tcase->rounds, ent, salt, |
| 527 |
tcase->expected_output[ent]); |
| 528 |
status = 1; |
| 529 |
} |
| 530 |
else |
| 531 |
fprintf (stderr, " ok: %s/%lu/%u -> %s\n", |
| 532 |
tcase->prefix, tcase->rounds, ent, salt); |
| 533 |
|
| 534 |
strcpy_or_abort (prev_output, CRYPT_GENSALT_OUTPUT_SIZE, salt); |
| 535 |
|
| 536 |
/* Test if crypt works with this salt. */ |
| 537 |
if (!tcase->rounds) |
| 538 |
{ |
| 539 |
#define PASSW "alexander" |
| 540 |
static struct crypt_data a, b; |
| 541 |
if (!crypt_rn (PASSW, salt, &a, sizeof(a))) |
| 542 |
{ |
| 543 |
fprintf (stderr, "ERROR: %s/%u -> crypt(gensalt) fail\n", |
| 544 |
tcase->prefix, ent); |
| 545 |
status = 1; |
| 546 |
} |
| 547 |
else if (!crypt_rn (PASSW, a.output, &b, sizeof(b))) |
| 548 |
{ |
| 549 |
fprintf (stderr, "ERROR: %s/%u -> crypt(crypt(gensalt)) fail\n", |
| 550 |
tcase->prefix, ent); |
| 551 |
status = 1; |
| 552 |
} |
| 553 |
else if (strcmp (a.output, b.output)) |
| 554 |
{ |
| 555 |
fprintf (stderr, "ERROR: %s/%u -> crypt(gensalt) != crypt(crypt(gensalt))\n", |
| 556 |
tcase->prefix, ent); |
| 557 |
status = 1; |
| 558 |
} |
| 559 |
else |
| 560 |
{ |
| 561 |
fprintf (stderr, " ok: %s/%u -> crypt works with this salt\n", |
| 562 |
tcase->prefix, ent); |
| 563 |
} |
| 564 |
} |
| 565 |
} |
| 566 |
} |
| 567 |
#if CRYPT_GENSALT_IMPLEMENTS_DEFAULT_PREFIX |
| 568 |
/* Passing a null pointer as the prefix argument to crypt_gensalt is |
| 569 |
supposed to tell it to use the "best available" hashing method. */ |
| 570 |
{ |
| 571 |
char *setting1, *setting2; |
| 572 |
setting1 = crypt_gensalt_ra (EXPECTED_DEFAULT_PREFIX, 0, entropy[0], 16); |
| 573 |
setting2 = crypt_gensalt_ra (0, 0, entropy[0], 16); |
| 574 |
if ((setting1 == 0 && setting2 != 0) || |
| 575 |
(setting1 != 0 && setting2 == 0) || |
| 576 |
(setting1 != 0 && setting2 != 0 && strcmp (setting1, setting2))) |
| 577 |
{ |
| 578 |
printf ("FAILED: crypt_gensalt defaulting to $y$\n" |
| 579 |
" $y$ -> %s\n" |
| 580 |
" null -> %s\n", |
| 581 |
setting1, setting2); |
| 582 |
status = 1; |
| 583 |
} |
| 584 |
free (setting1); |
| 585 |
free (setting2); |
| 586 |
} |
| 587 |
#else |
| 588 |
{ |
| 589 |
char *setting = crypt_gensalt_ra (0, 0, entropy[0], 16); |
| 590 |
if (setting) |
| 591 |
{ |
| 592 |
printf ("FAILED: crypt_gensalt null -> %s (null expected)\n", setting); |
| 593 |
status = 1; |
| 594 |
} |
| 595 |
free (setting); |
| 596 |
} |
| 597 |
#endif |
| 598 |
return status; |
| 599 |
} |