password.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Anthony Ferrara <ircmaxell@php.net> |
  16. | Charles R. Portwood II <charlesportwoodii@erianna.com> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include <stdlib.h>
  20. #include "php.h"
  21. #include "fcntl.h"
  22. #include "php_password.h"
  23. #include "php_rand.h"
  24. #include "php_crypt.h"
  25. #include "base64.h"
  26. #include "zend_interfaces.h"
  27. #include "info.h"
  28. #include "php_random.h"
  29. #if HAVE_ARGON2LIB
  30. #include "argon2.h"
  31. #endif
  32. #ifdef PHP_WIN32
  33. #include "win32/winutil.h"
  34. #endif
  35. PHP_MINIT_FUNCTION(password) /* {{{ */
  36. {
  37. REGISTER_LONG_CONSTANT("PASSWORD_DEFAULT", PHP_PASSWORD_DEFAULT, CONST_CS | CONST_PERSISTENT);
  38. REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT", PHP_PASSWORD_BCRYPT, CONST_CS | CONST_PERSISTENT);
  39. #if HAVE_ARGON2LIB
  40. REGISTER_LONG_CONSTANT("PASSWORD_ARGON2I", PHP_PASSWORD_ARGON2I, CONST_CS | CONST_PERSISTENT);
  41. REGISTER_LONG_CONSTANT("PASSWORD_ARGON2ID", PHP_PASSWORD_ARGON2ID, CONST_CS | CONST_PERSISTENT);
  42. #endif
  43. REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT_DEFAULT_COST", PHP_PASSWORD_BCRYPT_COST, CONST_CS | CONST_PERSISTENT);
  44. #if HAVE_ARGON2LIB
  45. REGISTER_LONG_CONSTANT("PASSWORD_ARGON2_DEFAULT_MEMORY_COST", PHP_PASSWORD_ARGON2_MEMORY_COST, CONST_CS | CONST_PERSISTENT);
  46. REGISTER_LONG_CONSTANT("PASSWORD_ARGON2_DEFAULT_TIME_COST", PHP_PASSWORD_ARGON2_TIME_COST, CONST_CS | CONST_PERSISTENT);
  47. REGISTER_LONG_CONSTANT("PASSWORD_ARGON2_DEFAULT_THREADS", PHP_PASSWORD_ARGON2_THREADS, CONST_CS | CONST_PERSISTENT);
  48. #endif
  49. return SUCCESS;
  50. }
  51. /* }}} */
  52. static zend_string* php_password_get_algo_name(const php_password_algo algo)
  53. {
  54. switch (algo) {
  55. case PHP_PASSWORD_BCRYPT:
  56. return zend_string_init("bcrypt", sizeof("bcrypt") - 1, 0);
  57. #if HAVE_ARGON2LIB
  58. case PHP_PASSWORD_ARGON2I:
  59. return zend_string_init("argon2i", sizeof("argon2i") - 1, 0);
  60. case PHP_PASSWORD_ARGON2ID:
  61. return zend_string_init("argon2id", sizeof("argon2id") - 1, 0);
  62. #endif
  63. case PHP_PASSWORD_UNKNOWN:
  64. default:
  65. return zend_string_init("unknown", sizeof("unknown") - 1, 0);
  66. }
  67. }
  68. static php_password_algo php_password_determine_algo(const zend_string *hash)
  69. {
  70. const char *h = ZSTR_VAL(hash);
  71. const size_t len = ZSTR_LEN(hash);
  72. if (len == 60 && h[0] == '$' && h[1] == '2' && h[2] == 'y') {
  73. return PHP_PASSWORD_BCRYPT;
  74. }
  75. #if HAVE_ARGON2LIB
  76. if (len >= sizeof("$argon2id$")-1 && !memcmp(h, "$argon2id$", sizeof("$argon2id$")-1)) {
  77. return PHP_PASSWORD_ARGON2ID;
  78. }
  79. if (len >= sizeof("$argon2i$")-1 && !memcmp(h, "$argon2i$", sizeof("$argon2i$")-1)) {
  80. return PHP_PASSWORD_ARGON2I;
  81. }
  82. #endif
  83. return PHP_PASSWORD_UNKNOWN;
  84. }
  85. static int php_password_salt_is_alphabet(const char *str, const size_t len) /* {{{ */
  86. {
  87. size_t i = 0;
  88. for (i = 0; i < len; i++) {
  89. if (!((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= '0' && str[i] <= '9') || str[i] == '.' || str[i] == '/')) {
  90. return FAILURE;
  91. }
  92. }
  93. return SUCCESS;
  94. }
  95. /* }}} */
  96. static int php_password_salt_to64(const char *str, const size_t str_len, const size_t out_len, char *ret) /* {{{ */
  97. {
  98. size_t pos = 0;
  99. zend_string *buffer;
  100. if ((int) str_len < 0) {
  101. return FAILURE;
  102. }
  103. buffer = php_base64_encode((unsigned char*) str, str_len);
  104. if (ZSTR_LEN(buffer) < out_len) {
  105. /* Too short of an encoded string generated */
  106. zend_string_release_ex(buffer, 0);
  107. return FAILURE;
  108. }
  109. for (pos = 0; pos < out_len; pos++) {
  110. if (ZSTR_VAL(buffer)[pos] == '+') {
  111. ret[pos] = '.';
  112. } else if (ZSTR_VAL(buffer)[pos] == '=') {
  113. zend_string_free(buffer);
  114. return FAILURE;
  115. } else {
  116. ret[pos] = ZSTR_VAL(buffer)[pos];
  117. }
  118. }
  119. zend_string_free(buffer);
  120. return SUCCESS;
  121. }
  122. /* }}} */
  123. static zend_string* php_password_make_salt(size_t length) /* {{{ */
  124. {
  125. zend_string *ret, *buffer;
  126. if (length > (INT_MAX / 3)) {
  127. php_error_docref(NULL, E_WARNING, "Length is too large to safely generate");
  128. return NULL;
  129. }
  130. buffer = zend_string_alloc(length * 3 / 4 + 1, 0);
  131. if (FAILURE == php_random_bytes_silent(ZSTR_VAL(buffer), ZSTR_LEN(buffer))) {
  132. php_error_docref(NULL, E_WARNING, "Unable to generate salt");
  133. zend_string_release_ex(buffer, 0);
  134. return NULL;
  135. }
  136. ret = zend_string_alloc(length, 0);
  137. if (php_password_salt_to64(ZSTR_VAL(buffer), ZSTR_LEN(buffer), length, ZSTR_VAL(ret)) == FAILURE) {
  138. php_error_docref(NULL, E_WARNING, "Generated salt too short");
  139. zend_string_release_ex(buffer, 0);
  140. zend_string_release_ex(ret, 0);
  141. return NULL;
  142. }
  143. zend_string_release_ex(buffer, 0);
  144. ZSTR_VAL(ret)[length] = 0;
  145. return ret;
  146. }
  147. /* }}} */
  148. #if HAVE_ARGON2LIB
  149. static void extract_argon2_parameters(const php_password_algo algo, const zend_string *hash,
  150. zend_long *v, zend_long *memory_cost,
  151. zend_long *time_cost, zend_long *threads) /* {{{ */
  152. {
  153. if (algo == PHP_PASSWORD_ARGON2ID) {
  154. sscanf(ZSTR_VAL(hash), "$%*[argon2id]$v=" ZEND_LONG_FMT "$m=" ZEND_LONG_FMT ",t=" ZEND_LONG_FMT ",p=" ZEND_LONG_FMT, v, memory_cost, time_cost, threads);
  155. } else if (algo == PHP_PASSWORD_ARGON2I) {
  156. sscanf(ZSTR_VAL(hash), "$%*[argon2i]$v=" ZEND_LONG_FMT "$m=" ZEND_LONG_FMT ",t=" ZEND_LONG_FMT ",p=" ZEND_LONG_FMT, v, memory_cost, time_cost, threads);
  157. }
  158. return;
  159. }
  160. #endif
  161. /* {{{ proto array password_get_info(string $hash)
  162. Retrieves information about a given hash */
  163. PHP_FUNCTION(password_get_info)
  164. {
  165. php_password_algo algo;
  166. zend_string *hash, *algo_name;
  167. zval options;
  168. ZEND_PARSE_PARAMETERS_START(1, 1)
  169. Z_PARAM_STR(hash)
  170. ZEND_PARSE_PARAMETERS_END();
  171. array_init(&options);
  172. algo = php_password_determine_algo(hash);
  173. algo_name = php_password_get_algo_name(algo);
  174. switch (algo) {
  175. case PHP_PASSWORD_BCRYPT:
  176. {
  177. zend_long cost = PHP_PASSWORD_BCRYPT_COST;
  178. sscanf(ZSTR_VAL(hash), "$2y$" ZEND_LONG_FMT "$", &cost);
  179. add_assoc_long(&options, "cost", cost);
  180. }
  181. break;
  182. #if HAVE_ARGON2LIB
  183. case PHP_PASSWORD_ARGON2I:
  184. case PHP_PASSWORD_ARGON2ID:
  185. {
  186. zend_long v = 0;
  187. zend_long memory_cost = PHP_PASSWORD_ARGON2_MEMORY_COST;
  188. zend_long time_cost = PHP_PASSWORD_ARGON2_TIME_COST;
  189. zend_long threads = PHP_PASSWORD_ARGON2_THREADS;
  190. extract_argon2_parameters(algo, hash, &v, &memory_cost, &time_cost, &threads);
  191. add_assoc_long(&options, "memory_cost", memory_cost);
  192. add_assoc_long(&options, "time_cost", time_cost);
  193. add_assoc_long(&options, "threads", threads);
  194. }
  195. break;
  196. #endif
  197. case PHP_PASSWORD_UNKNOWN:
  198. default:
  199. break;
  200. }
  201. array_init(return_value);
  202. add_assoc_long(return_value, "algo", algo);
  203. add_assoc_str(return_value, "algoName", algo_name);
  204. add_assoc_zval(return_value, "options", &options);
  205. }
  206. /** }}} */
  207. /* {{{ proto bool password_needs_rehash(string $hash, int $algo[, array $options])
  208. Determines if a given hash requires re-hashing based upon parameters */
  209. PHP_FUNCTION(password_needs_rehash)
  210. {
  211. zend_long new_algo = 0;
  212. php_password_algo algo;
  213. zend_string *hash;
  214. HashTable *options = 0;
  215. zval *option_buffer;
  216. ZEND_PARSE_PARAMETERS_START(2, 3)
  217. Z_PARAM_STR(hash)
  218. Z_PARAM_LONG(new_algo)
  219. Z_PARAM_OPTIONAL
  220. Z_PARAM_ARRAY_OR_OBJECT_HT(options)
  221. ZEND_PARSE_PARAMETERS_END();
  222. algo = php_password_determine_algo(hash);
  223. if ((zend_long)algo != new_algo) {
  224. RETURN_TRUE;
  225. }
  226. switch (algo) {
  227. case PHP_PASSWORD_BCRYPT:
  228. {
  229. zend_long new_cost = PHP_PASSWORD_BCRYPT_COST, cost = 0;
  230. if (options && (option_buffer = zend_hash_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
  231. new_cost = zval_get_long(option_buffer);
  232. }
  233. sscanf(ZSTR_VAL(hash), "$2y$" ZEND_LONG_FMT "$", &cost);
  234. if (cost != new_cost) {
  235. RETURN_TRUE;
  236. }
  237. }
  238. break;
  239. #if HAVE_ARGON2LIB
  240. case PHP_PASSWORD_ARGON2I:
  241. case PHP_PASSWORD_ARGON2ID:
  242. {
  243. zend_long v = 0;
  244. zend_long new_memory_cost = PHP_PASSWORD_ARGON2_MEMORY_COST, memory_cost = 0;
  245. zend_long new_time_cost = PHP_PASSWORD_ARGON2_TIME_COST, time_cost = 0;
  246. zend_long new_threads = PHP_PASSWORD_ARGON2_THREADS, threads = 0;
  247. if (options && (option_buffer = zend_hash_str_find(options, "memory_cost", sizeof("memory_cost")-1)) != NULL) {
  248. new_memory_cost = zval_get_long(option_buffer);
  249. }
  250. if (options && (option_buffer = zend_hash_str_find(options, "time_cost", sizeof("time_cost")-1)) != NULL) {
  251. new_time_cost = zval_get_long(option_buffer);
  252. }
  253. if (options && (option_buffer = zend_hash_str_find(options, "threads", sizeof("threads")-1)) != NULL) {
  254. new_threads = zval_get_long(option_buffer);
  255. }
  256. extract_argon2_parameters(algo, hash, &v, &memory_cost, &time_cost, &threads);
  257. if (new_time_cost != time_cost || new_memory_cost != memory_cost || new_threads != threads) {
  258. RETURN_TRUE;
  259. }
  260. }
  261. break;
  262. #endif
  263. case PHP_PASSWORD_UNKNOWN:
  264. default:
  265. break;
  266. }
  267. RETURN_FALSE;
  268. }
  269. /* }}} */
  270. /* {{{ proto bool password_verify(string password, string hash)
  271. Verify a hash created using crypt() or password_hash() */
  272. PHP_FUNCTION(password_verify)
  273. {
  274. zend_string *password, *hash;
  275. php_password_algo algo;
  276. ZEND_PARSE_PARAMETERS_START(2, 2)
  277. Z_PARAM_STR(password)
  278. Z_PARAM_STR(hash)
  279. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  280. algo = php_password_determine_algo(hash);
  281. switch(algo) {
  282. #if HAVE_ARGON2LIB
  283. case PHP_PASSWORD_ARGON2I:
  284. case PHP_PASSWORD_ARGON2ID:
  285. {
  286. argon2_type type;
  287. if (algo == PHP_PASSWORD_ARGON2ID) {
  288. type = Argon2_id;
  289. } else if (algo == PHP_PASSWORD_ARGON2I) {
  290. type = Argon2_i;
  291. }
  292. RETURN_BOOL(ARGON2_OK == argon2_verify(ZSTR_VAL(hash), ZSTR_VAL(password), ZSTR_LEN(password), type));
  293. }
  294. break;
  295. #endif
  296. case PHP_PASSWORD_BCRYPT:
  297. case PHP_PASSWORD_UNKNOWN:
  298. default:
  299. {
  300. size_t i;
  301. int status = 0;
  302. zend_string *ret = php_crypt(ZSTR_VAL(password), (int)ZSTR_LEN(password), ZSTR_VAL(hash), (int)ZSTR_LEN(hash), 1);
  303. if (!ret) {
  304. RETURN_FALSE;
  305. }
  306. if (ZSTR_LEN(ret) != ZSTR_LEN(hash) || ZSTR_LEN(hash) < 13) {
  307. zend_string_free(ret);
  308. RETURN_FALSE;
  309. }
  310. /* We're using this method instead of == in order to provide
  311. * resistance towards timing attacks. This is a constant time
  312. * equality check that will always check every byte of both
  313. * values. */
  314. for (i = 0; i < ZSTR_LEN(hash); i++) {
  315. status |= (ZSTR_VAL(ret)[i] ^ ZSTR_VAL(hash)[i]);
  316. }
  317. zend_string_free(ret);
  318. RETURN_BOOL(status == 0);
  319. }
  320. }
  321. RETURN_FALSE;
  322. }
  323. /* }}} */
  324. static zend_string* php_password_get_salt(zval *return_value, size_t required_salt_len, HashTable *options) {
  325. zend_string *buffer;
  326. zval *option_buffer;
  327. if (!options || !(option_buffer = zend_hash_str_find(options, "salt", sizeof("salt") - 1))) {
  328. buffer = php_password_make_salt(required_salt_len);
  329. if (!buffer) {
  330. RETVAL_FALSE;
  331. }
  332. return buffer;
  333. }
  334. php_error_docref(NULL, E_DEPRECATED, "Use of the 'salt' option to password_hash is deprecated");
  335. switch (Z_TYPE_P(option_buffer)) {
  336. case IS_STRING:
  337. buffer = zend_string_copy(Z_STR_P(option_buffer));
  338. break;
  339. case IS_LONG:
  340. case IS_DOUBLE:
  341. case IS_OBJECT:
  342. buffer = zval_get_string(option_buffer);
  343. break;
  344. case IS_FALSE:
  345. case IS_TRUE:
  346. case IS_NULL:
  347. case IS_RESOURCE:
  348. case IS_ARRAY:
  349. default:
  350. php_error_docref(NULL, E_WARNING, "Non-string salt parameter supplied");
  351. return NULL;
  352. }
  353. /* XXX all the crypt related APIs work with int for string length.
  354. That should be revised for size_t and then we maybe don't require
  355. the > INT_MAX check. */
  356. if (ZEND_SIZE_T_INT_OVFL(ZSTR_LEN(buffer))) {
  357. php_error_docref(NULL, E_WARNING, "Supplied salt is too long");
  358. zend_string_release_ex(buffer, 0);
  359. return NULL;
  360. }
  361. if (ZSTR_LEN(buffer) < required_salt_len) {
  362. php_error_docref(NULL, E_WARNING, "Provided salt is too short: %zd expecting %zd", ZSTR_LEN(buffer), required_salt_len);
  363. zend_string_release_ex(buffer, 0);
  364. return NULL;
  365. }
  366. if (php_password_salt_is_alphabet(ZSTR_VAL(buffer), ZSTR_LEN(buffer)) == FAILURE) {
  367. zend_string *salt = zend_string_alloc(required_salt_len, 0);
  368. if (php_password_salt_to64(ZSTR_VAL(buffer), ZSTR_LEN(buffer), required_salt_len, ZSTR_VAL(salt)) == FAILURE) {
  369. php_error_docref(NULL, E_WARNING, "Provided salt is too short: %zd", ZSTR_LEN(buffer));
  370. zend_string_release_ex(salt, 0);
  371. zend_string_release_ex(buffer, 0);
  372. return NULL;
  373. }
  374. zend_string_release_ex(buffer, 0);
  375. return salt;
  376. } else {
  377. zend_string *salt = zend_string_alloc(required_salt_len, 0);
  378. memcpy(ZSTR_VAL(salt), ZSTR_VAL(buffer), required_salt_len);
  379. zend_string_release_ex(buffer, 0);
  380. return salt;
  381. }
  382. }
  383. /* {{{ proto string password_hash(string password, int algo[, array options = array()])
  384. Hash a password */
  385. PHP_FUNCTION(password_hash)
  386. {
  387. zend_string *password;
  388. zend_long algo = PHP_PASSWORD_DEFAULT;
  389. HashTable *options = NULL;
  390. ZEND_PARSE_PARAMETERS_START(2, 3)
  391. Z_PARAM_STR(password)
  392. Z_PARAM_LONG(algo)
  393. Z_PARAM_OPTIONAL
  394. Z_PARAM_ARRAY_OR_OBJECT_HT(options)
  395. ZEND_PARSE_PARAMETERS_END();
  396. switch (algo) {
  397. case PHP_PASSWORD_BCRYPT:
  398. {
  399. char hash_format[10];
  400. size_t hash_format_len;
  401. zend_string *result, *hash, *salt;
  402. zval *option_buffer;
  403. zend_long cost = PHP_PASSWORD_BCRYPT_COST;
  404. if (options && (option_buffer = zend_hash_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
  405. cost = zval_get_long(option_buffer);
  406. }
  407. if (cost < 4 || cost > 31) {
  408. php_error_docref(NULL, E_WARNING, "Invalid bcrypt cost parameter specified: " ZEND_LONG_FMT, cost);
  409. RETURN_NULL();
  410. }
  411. hash_format_len = snprintf(hash_format, sizeof(hash_format), "$2y$%02" ZEND_LONG_FMT_SPEC "$", cost);
  412. if (!(salt = php_password_get_salt(return_value, Z_UL(22), options))) {
  413. return;
  414. }
  415. ZSTR_VAL(salt)[ZSTR_LEN(salt)] = 0;
  416. hash = zend_string_alloc(ZSTR_LEN(salt) + hash_format_len, 0);
  417. sprintf(ZSTR_VAL(hash), "%s%s", hash_format, ZSTR_VAL(salt));
  418. ZSTR_VAL(hash)[hash_format_len + ZSTR_LEN(salt)] = 0;
  419. zend_string_release_ex(salt, 0);
  420. /* This cast is safe, since both values are defined here in code and cannot overflow */
  421. result = php_crypt(ZSTR_VAL(password), (int)ZSTR_LEN(password), ZSTR_VAL(hash), (int)ZSTR_LEN(hash), 1);
  422. zend_string_release_ex(hash, 0);
  423. if (!result) {
  424. RETURN_FALSE;
  425. }
  426. if (ZSTR_LEN(result) < 13) {
  427. zend_string_free(result);
  428. RETURN_FALSE;
  429. }
  430. RETURN_STR(result);
  431. }
  432. break;
  433. #if HAVE_ARGON2LIB
  434. case PHP_PASSWORD_ARGON2I:
  435. case PHP_PASSWORD_ARGON2ID:
  436. {
  437. zval *option_buffer;
  438. zend_string *salt, *out, *encoded;
  439. size_t time_cost = PHP_PASSWORD_ARGON2_TIME_COST;
  440. size_t memory_cost = PHP_PASSWORD_ARGON2_MEMORY_COST;
  441. size_t threads = PHP_PASSWORD_ARGON2_THREADS;
  442. argon2_type type;
  443. if (algo == PHP_PASSWORD_ARGON2ID) {
  444. type = Argon2_id;
  445. } else if (algo == PHP_PASSWORD_ARGON2I) {
  446. type = Argon2_i;
  447. }
  448. size_t encoded_len;
  449. int status = 0;
  450. if (options && (option_buffer = zend_hash_str_find(options, "memory_cost", sizeof("memory_cost")-1)) != NULL) {
  451. memory_cost = zval_get_long(option_buffer);
  452. }
  453. if (memory_cost > ARGON2_MAX_MEMORY || memory_cost < ARGON2_MIN_MEMORY) {
  454. php_error_docref(NULL, E_WARNING, "Memory cost is outside of allowed memory range");
  455. RETURN_NULL();
  456. }
  457. if (options && (option_buffer = zend_hash_str_find(options, "time_cost", sizeof("time_cost")-1)) != NULL) {
  458. time_cost = zval_get_long(option_buffer);
  459. }
  460. if (time_cost > ARGON2_MAX_TIME || time_cost < ARGON2_MIN_TIME) {
  461. php_error_docref(NULL, E_WARNING, "Time cost is outside of allowed time range");
  462. RETURN_NULL();
  463. }
  464. if (options && (option_buffer = zend_hash_str_find(options, "threads", sizeof("threads")-1)) != NULL) {
  465. threads = zval_get_long(option_buffer);
  466. }
  467. if (threads > ARGON2_MAX_LANES || threads == 0) {
  468. php_error_docref(NULL, E_WARNING, "Invalid number of threads");
  469. RETURN_NULL();
  470. }
  471. if (!(salt = php_password_get_salt(return_value, Z_UL(16), options))) {
  472. return;
  473. }
  474. out = zend_string_alloc(32, 0);
  475. encoded_len = argon2_encodedlen(
  476. time_cost,
  477. memory_cost,
  478. threads,
  479. (uint32_t)ZSTR_LEN(salt),
  480. ZSTR_LEN(out),
  481. type
  482. );
  483. encoded = zend_string_alloc(encoded_len - 1, 0);
  484. status = argon2_hash(
  485. time_cost,
  486. memory_cost,
  487. threads,
  488. ZSTR_VAL(password),
  489. ZSTR_LEN(password),
  490. ZSTR_VAL(salt),
  491. ZSTR_LEN(salt),
  492. ZSTR_VAL(out),
  493. ZSTR_LEN(out),
  494. ZSTR_VAL(encoded),
  495. encoded_len,
  496. type,
  497. ARGON2_VERSION_NUMBER
  498. );
  499. zend_string_release_ex(out, 0);
  500. zend_string_release_ex(salt, 0);
  501. if (status != ARGON2_OK) {
  502. zend_string_efree(encoded);
  503. php_error_docref(NULL, E_WARNING, "%s", argon2_error_message(status));
  504. RETURN_FALSE;
  505. }
  506. ZSTR_VAL(encoded)[ZSTR_LEN(encoded)] = 0;
  507. RETURN_NEW_STR(encoded);
  508. }
  509. break;
  510. #endif
  511. case PHP_PASSWORD_UNKNOWN:
  512. default:
  513. php_error_docref(NULL, E_WARNING, "Unknown password hashing algorithm: " ZEND_LONG_FMT, algo);
  514. RETURN_NULL();
  515. }
  516. }
  517. /* }}} */
  518. /*
  519. * Local variables:
  520. * tab-width: 4
  521. * c-basic-offset: 4
  522. * End:
  523. * vim600: sw=4 ts=4 fdm=marker
  524. * vim<600: sw=4 ts=4
  525. */