|
30 | 30 | #ifdef HAVE_ARGON2LIB |
31 | 31 | #include "argon2.h" |
32 | 32 | #endif |
| 33 | +#include "yescrypt/yescrypt.h" |
33 | 34 |
|
34 | 35 | #ifdef PHP_WIN32 |
35 | 36 | #include "win32/winutil.h" |
@@ -151,7 +152,8 @@ static bool php_password_bcrypt_needs_rehash(const zend_string *hash, zend_array |
151 | 152 | return old_cost != new_cost; |
152 | 153 | } |
153 | 154 |
|
154 | | -static bool php_password_bcrypt_verify(const zend_string *password, const zend_string *hash) { |
| 155 | +/* Password verification using the crypt() API, works for both bcrypt and yescrypt. */ |
| 156 | +static bool php_password_crypt_verify(const zend_string *password, const zend_string *hash) { |
155 | 157 | int status = 0; |
156 | 158 | zend_string *ret = php_crypt(ZSTR_VAL(password), (int)ZSTR_LEN(password), ZSTR_VAL(hash), (int)ZSTR_LEN(hash), 1); |
157 | 159 |
|
@@ -224,12 +226,204 @@ static zend_string* php_password_bcrypt_hash(const zend_string *password, zend_a |
224 | 226 | const php_password_algo php_password_algo_bcrypt = { |
225 | 227 | "bcrypt", |
226 | 228 | php_password_bcrypt_hash, |
227 | | - php_password_bcrypt_verify, |
| 229 | + php_password_crypt_verify, |
228 | 230 | php_password_bcrypt_needs_rehash, |
229 | 231 | php_password_bcrypt_get_info, |
230 | 232 | php_password_bcrypt_valid, |
231 | 233 | }; |
232 | 234 |
|
| 235 | +/* yescrypt implementation */ |
| 236 | + |
| 237 | +static zend_string *php_password_yescrypt_hash(const zend_string *password, zend_array *options) { |
| 238 | + zend_long block_count = PHP_PASSWORD_YESCRYPT_DEFAULT_BLOCK_COUNT; |
| 239 | + zend_long block_size = PHP_PASSWORD_YESCRYPT_DEFAULT_BLOCK_SIZE; |
| 240 | + zend_long parallelism = PHP_PASSWORD_YESCRYPT_DEFAULT_PARALLELISM; |
| 241 | + zend_long time = PHP_PASSWORD_YESCRYPT_DEFAULT_TIME; |
| 242 | + |
| 243 | + if (UNEXPECTED(ZEND_LONG_INT_OVFL(ZSTR_LEN(password)))) { |
| 244 | + zend_value_error("Password is too long"); |
| 245 | + return NULL; |
| 246 | + } |
| 247 | + |
| 248 | + if (options) { |
| 249 | + bool failed; |
| 250 | + const zval *option; |
| 251 | + |
| 252 | + option = zend_hash_str_find(options, ZEND_STRL("block_count")); |
| 253 | + if (option) { |
| 254 | + block_count = zval_try_get_long(option, &failed); |
| 255 | + if (UNEXPECTED(failed)) { |
| 256 | + return NULL; |
| 257 | + } |
| 258 | + |
| 259 | + if (block_count < 4 || block_count > UINT32_MAX) { |
| 260 | + zend_value_error("Parameter \"block_count\" must be between 4 and %u", UINT32_MAX); |
| 261 | + return NULL; |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + option = zend_hash_str_find(options, ZEND_STRL("block_size")); |
| 266 | + if (option) { |
| 267 | + block_size = zval_try_get_long(option, &failed); |
| 268 | + if (UNEXPECTED(failed)) { |
| 269 | + return NULL; |
| 270 | + } |
| 271 | + |
| 272 | + if (block_size < 1) { |
| 273 | + zend_value_error("Parameter \"block_size\" must be greater than 0"); |
| 274 | + return NULL; |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + option = zend_hash_str_find(options, ZEND_STRL("parallelism")); |
| 279 | + if (option) { |
| 280 | + parallelism = zval_try_get_long(option, &failed); |
| 281 | + if (UNEXPECTED(failed)) { |
| 282 | + return NULL; |
| 283 | + } |
| 284 | + |
| 285 | + if (parallelism < 1) { |
| 286 | + zend_value_error("Parameter \"parallelism\" must be greater than 0"); |
| 287 | + return NULL; |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + option = zend_hash_str_find(options, ZEND_STRL("time")); |
| 292 | + if (option) { |
| 293 | + time = zval_try_get_long(option, &failed); |
| 294 | + if (UNEXPECTED(failed)) { |
| 295 | + return NULL; |
| 296 | + } |
| 297 | + |
| 298 | + if (time < 0) { |
| 299 | + zend_value_error("Parameter \"time\" must be greater than or equal to 0"); |
| 300 | + return NULL; |
| 301 | + } |
| 302 | + } |
| 303 | + |
| 304 | + if ((uint64_t) block_size * (uint64_t) parallelism >= (1U << 30)) { |
| 305 | + zend_value_error("Parameter \"block_size\" * parameter \"parallelism\" must be less than 2**30"); |
| 306 | + return NULL; |
| 307 | + } |
| 308 | + } |
| 309 | + |
| 310 | + zend_string *salt = php_password_get_salt(NULL, Z_UL(22), options); |
| 311 | + if (UNEXPECTED(!salt)) { |
| 312 | + return NULL; |
| 313 | + } |
| 314 | + ZSTR_VAL(salt)[ZSTR_LEN(salt)] = 0; |
| 315 | + |
| 316 | + uint8_t prefix_buffer[PREFIX_LEN + 1]; |
| 317 | + yescrypt_params_t params = { |
| 318 | + .flags = YESCRYPT_DEFAULTS, |
| 319 | + .N = block_count, .r = block_size, .p = parallelism, .t = time, |
| 320 | + .g = 0, .NROM = 0 |
| 321 | + }; |
| 322 | + uint8_t *prefix = yescrypt_encode_params_r( |
| 323 | + ¶ms, |
| 324 | + (const uint8_t *) ZSTR_VAL(salt), |
| 325 | + ZSTR_LEN(salt), |
| 326 | + prefix_buffer, |
| 327 | + sizeof(prefix_buffer) |
| 328 | + ); |
| 329 | + |
| 330 | + zend_string_release_ex(salt, false); |
| 331 | + |
| 332 | + if (UNEXPECTED(prefix == NULL)) { |
| 333 | + return NULL; |
| 334 | + } |
| 335 | + |
| 336 | + return php_crypt( |
| 337 | + ZSTR_VAL(password), |
| 338 | + /* This cast is safe because we check that the password length fits in an int at the start. */ |
| 339 | + (int) ZSTR_LEN(password), |
| 340 | + (const char *) prefix_buffer, |
| 341 | + /* The following cast is safe because the prefix buffer size is always below INT_MAX. */ |
| 342 | + (int) strlen((const char *) prefix_buffer), |
| 343 | + true |
| 344 | + ); |
| 345 | +} |
| 346 | + |
| 347 | +static bool php_password_yescrypt_valid(const zend_string *hash) { |
| 348 | + const char *h = ZSTR_VAL(hash); |
| 349 | + return (ZSTR_LEN(hash) >= 3 /* "$y$" */ + 3 /* 3 parameters that must be encoded */ + 2 /* $salt$ */ + HASH_LEN |
| 350 | + && ZSTR_LEN(hash) <= PREFIX_LEN + 1 + HASH_LEN) |
| 351 | + && (h[0] == '$') && (h[1] == 'y' || h[1] == '7') && (h[2] == '$'); |
| 352 | +} |
| 353 | + |
| 354 | +static bool php_password_yescrypt_needs_rehash(const zend_string *hash, zend_array *options) { |
| 355 | + zend_long block_count = PHP_PASSWORD_YESCRYPT_DEFAULT_BLOCK_COUNT; |
| 356 | + zend_long block_size = PHP_PASSWORD_YESCRYPT_DEFAULT_BLOCK_SIZE; |
| 357 | + zend_long parallelism = PHP_PASSWORD_YESCRYPT_DEFAULT_PARALLELISM; |
| 358 | + zend_long time = PHP_PASSWORD_YESCRYPT_DEFAULT_TIME; |
| 359 | + |
| 360 | + if (!php_password_yescrypt_valid(hash)) { |
| 361 | + /* Should never get called this way. */ |
| 362 | + return true; |
| 363 | + } |
| 364 | + |
| 365 | + yescrypt_params_t params = { .p = 1 }; |
| 366 | + const uint8_t *src = yescrypt_parse_settings((const uint8_t *) ZSTR_VAL(hash), ¶ms, NULL); |
| 367 | + if (!src) { |
| 368 | + return true; |
| 369 | + } |
| 370 | + |
| 371 | + if (options) { |
| 372 | + const zval *option; |
| 373 | + |
| 374 | + option = zend_hash_str_find(options, ZEND_STRL("block_count")); |
| 375 | + if (option) { |
| 376 | + block_count = zval_get_long(option); |
| 377 | + } |
| 378 | + |
| 379 | + option = zend_hash_str_find(options, ZEND_STRL("block_size")); |
| 380 | + if (option) { |
| 381 | + block_size = zval_get_long(option); |
| 382 | + } |
| 383 | + |
| 384 | + option = zend_hash_str_find(options, ZEND_STRL("parallelism")); |
| 385 | + if (option) { |
| 386 | + parallelism = zval_get_long(option); |
| 387 | + } |
| 388 | + |
| 389 | + option = zend_hash_str_find(options, ZEND_STRL("time")); |
| 390 | + if (option) { |
| 391 | + time = zval_get_long(option); |
| 392 | + } |
| 393 | + } |
| 394 | + |
| 395 | + return block_count != params.N || block_size != params.r || parallelism != params.p || time != params.t; |
| 396 | +} |
| 397 | + |
| 398 | +static int php_password_yescrypt_get_info(zval *return_value, const zend_string *hash) { |
| 399 | + if (!php_password_yescrypt_valid(hash)) { |
| 400 | + /* Should never get called this way. */ |
| 401 | + return FAILURE; |
| 402 | + } |
| 403 | + |
| 404 | + yescrypt_params_t params = { .p = 1 }; |
| 405 | + const uint8_t *src = yescrypt_parse_settings((const uint8_t *) ZSTR_VAL(hash), ¶ms, NULL); |
| 406 | + if (!src) { |
| 407 | + return FAILURE; |
| 408 | + } |
| 409 | + |
| 410 | + add_assoc_long(return_value, "block_count", (zend_long) params.N); |
| 411 | + add_assoc_long(return_value, "block_size", (zend_long) params.r); |
| 412 | + add_assoc_long(return_value, "parallelism", (zend_long) params.p); |
| 413 | + add_assoc_long(return_value, "time", (zend_long) params.t); |
| 414 | + |
| 415 | + return SUCCESS; |
| 416 | +} |
| 417 | + |
| 418 | +const php_password_algo php_password_algo_yescrypt = { |
| 419 | + "yescrypt", |
| 420 | + php_password_yescrypt_hash, |
| 421 | + php_password_crypt_verify, |
| 422 | + php_password_yescrypt_needs_rehash, |
| 423 | + php_password_yescrypt_get_info, |
| 424 | + php_password_yescrypt_valid, |
| 425 | +}; |
| 426 | + |
233 | 427 |
|
234 | 428 | #ifdef HAVE_ARGON2LIB |
235 | 429 | /* argon2i/argon2id shared implementation */ |
@@ -427,6 +621,10 @@ PHP_MINIT_FUNCTION(password) /* {{{ */ |
427 | 621 | return FAILURE; |
428 | 622 | } |
429 | 623 |
|
| 624 | + if (FAILURE == php_password_algo_register("y", &php_password_algo_yescrypt)) { |
| 625 | + return FAILURE; |
| 626 | + } |
| 627 | + |
430 | 628 | #ifdef HAVE_ARGON2LIB |
431 | 629 | if (FAILURE == php_password_algo_register("argon2i", &php_password_algo_argon2i)) { |
432 | 630 | return FAILURE; |
|
0 commit comments