Skip to content

Commit 0e3b3b0

Browse files
Changing m_cost and t_cost to memory_cost and time_cost
- Updating tests - Adjusting cost factors: - memory_cost = 1 MiB - time_cost = 2 - threads = 2
1 parent d883f65 commit 0e3b3b0

File tree

7 files changed

+44
-50
lines changed

7 files changed

+44
-50
lines changed

ext/standard/password.c

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ PHP_MINIT_FUNCTION(password) /* {{{ */
4545
REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT", PHP_PASSWORD_BCRYPT, CONST_CS | CONST_PERSISTENT);
4646
#if HAVE_ARGON2LIB
4747
REGISTER_LONG_CONSTANT("PASSWORD_ARGON2I", PHP_PASSWORD_ARGON2I, CONST_CS | CONST_PERSISTENT);
48-
REGISTER_LONG_CONSTANT("PASSWORD_ARGON2", PHP_PASSWORD_ARGON2, CONST_CS | CONST_PERSISTENT);
4948
#endif
5049

5150
REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT_DEFAULT_COST", PHP_PASSWORD_BCRYPT_COST, CONST_CS | CONST_PERSISTENT);
@@ -195,13 +194,13 @@ PHP_FUNCTION(password_get_info)
195194
case PHP_PASSWORD_ARGON2I:
196195
{
197196
zend_long v = 0;
198-
zend_long m_cost = PHP_PASSWORD_ARGON2_MEMORY_COST;
199-
zend_long t_cost = PHP_PASSWORD_ARGON2_TIME_COST;
197+
zend_long memory_cost = PHP_PASSWORD_ARGON2_MEMORY_COST;
198+
zend_long time_cost = PHP_PASSWORD_ARGON2_TIME_COST;
200199
zend_long threads = PHP_PASSWORD_ARGON2_THREADS;
201200

202-
sscanf(hash, "$%*[argon2i]$v=" ZEND_LONG_FMT "$m=" ZEND_LONG_FMT ",t=" ZEND_LONG_FMT ",p=" ZEND_LONG_FMT, &v, &m_cost, &t_cost, &threads);
203-
add_assoc_long(&options, "m_cost", m_cost);
204-
add_assoc_long(&options, "t_cost", t_cost);
201+
sscanf(hash, "$%*[argon2i]$v=" ZEND_LONG_FMT "$m=" ZEND_LONG_FMT ",t=" ZEND_LONG_FMT ",p=" ZEND_LONG_FMT, &v, &memory_cost, &time_cost, &threads);
202+
add_assoc_long(&options, "memory_cost", memory_cost);
203+
add_assoc_long(&options, "time_cost", time_cost);
205204
add_assoc_long(&options, "threads", threads);
206205
}
207206
break;
@@ -259,25 +258,25 @@ PHP_FUNCTION(password_needs_rehash)
259258
case PHP_PASSWORD_ARGON2I:
260259
{
261260
zend_long v = 0;
262-
zend_long new_m_cost = PHP_PASSWORD_ARGON2_MEMORY_COST, m_cost = 0;
263-
zend_long new_t_cost = PHP_PASSWORD_ARGON2_TIME_COST, t_cost = 0;
261+
zend_long new_memory_cost = PHP_PASSWORD_ARGON2_MEMORY_COST, memory_cost = 0;
262+
zend_long new_time_cost = PHP_PASSWORD_ARGON2_TIME_COST, time_cost = 0;
264263
zend_long new_threads = PHP_PASSWORD_ARGON2_THREADS, threads = 0;
265264

266-
if (options && (option_buffer = zend_hash_str_find(options, "m_cost", sizeof("m_cost")-1)) != NULL) {
267-
new_m_cost = zval_get_long(option_buffer);
265+
if (options && (option_buffer = zend_hash_str_find(options, "memory_cost", sizeof("memory_cost")-1)) != NULL) {
266+
new_memory_cost = zval_get_long(option_buffer);
268267
}
269268

270-
if (options && (option_buffer = zend_hash_str_find(options, "t_cost", sizeof("t_cost")-1)) != NULL) {
271-
new_t_cost = zval_get_long(option_buffer);
269+
if (options && (option_buffer = zend_hash_str_find(options, "time_cost", sizeof("time_cost")-1)) != NULL) {
270+
new_time_cost = zval_get_long(option_buffer);
272271
}
273272

274273
if (options && (option_buffer = zend_hash_str_find(options, "threads", sizeof("threads")-1)) != NULL) {
275274
new_threads = zval_get_long(option_buffer);
276275
}
277276

278-
sscanf(hash, "$%*[argon2i]$v=" ZEND_LONG_FMT "$m=" ZEND_LONG_FMT ",t=" ZEND_LONG_FMT ",p=" ZEND_LONG_FMT, &v, &m_cost, &t_cost, &threads);
277+
sscanf(hash, "$%*[argon2i]$v=" ZEND_LONG_FMT "$m=" ZEND_LONG_FMT ",t=" ZEND_LONG_FMT ",p=" ZEND_LONG_FMT, &v, &memory_cost, &time_cost, &threads);
279278

280-
if (new_t_cost != t_cost || new_m_cost != m_cost || new_threads != threads) {
279+
if (new_time_cost != time_cost || new_memory_cost != memory_cost || new_threads != threads) {
281280
RETURN_TRUE;
282281
}
283282
}
@@ -367,8 +366,8 @@ PHP_FUNCTION(password_hash)
367366
zval *option_buffer;
368367

369368
#if HAVE_ARGON2LIB
370-
size_t t_cost = PHP_PASSWORD_ARGON2_TIME_COST;
371-
size_t m_cost = PHP_PASSWORD_ARGON2_MEMORY_COST;
369+
size_t time_cost = PHP_PASSWORD_ARGON2_TIME_COST;
370+
size_t memory_cost = PHP_PASSWORD_ARGON2_MEMORY_COST;
372371
size_t threads = PHP_PASSWORD_ARGON2_THREADS;
373372
argon2_type type = Argon2_i;
374373
#endif
@@ -399,21 +398,21 @@ PHP_FUNCTION(password_hash)
399398
#if HAVE_ARGON2LIB
400399
case PHP_PASSWORD_ARGON2I:
401400
{
402-
if (options && (option_buffer = zend_hash_str_find(options, "m_cost", sizeof("m_cost")-1)) != NULL) {
403-
m_cost = zval_get_long(option_buffer);
401+
if (options && (option_buffer = zend_hash_str_find(options, "memory_cost", sizeof("memory_cost")-1)) != NULL) {
402+
memory_cost = zval_get_long(option_buffer);
404403
}
405404

406-
if (m_cost > ARGON2_MAX_MEMORY || m_cost < ARGON2_MIN_MEMORY) {
407-
php_error_docref(NULL, E_WARNING, "Memory cost is outside of allowed memory range", m_cost);
405+
if (memory_cost > ARGON2_MAX_MEMORY || memory_cost < ARGON2_MIN_MEMORY) {
406+
php_error_docref(NULL, E_WARNING, "Memory cost is outside of allowed memory range", memory_cost);
408407
RETURN_NULL();
409408
}
410409

411-
if (options && (option_buffer = zend_hash_str_find(options, "t_cost", sizeof("t_cost")-1)) != NULL) {
412-
t_cost = zval_get_long(option_buffer);
410+
if (options && (option_buffer = zend_hash_str_find(options, "time_cost", sizeof("time_cost")-1)) != NULL) {
411+
time_cost = zval_get_long(option_buffer);
413412
}
414413

415-
if (t_cost > ARGON2_MAX_TIME || t_cost < ARGON2_MIN_TIME) {
416-
php_error_docref(NULL, E_WARNING, "Time cost is outside of allowed time range", t_cost);
414+
if (time_cost > ARGON2_MAX_TIME || time_cost < ARGON2_MIN_TIME) {
415+
php_error_docref(NULL, E_WARNING, "Time cost is outside of allowed time range", time_cost);
417416
RETURN_NULL();
418417
}
419418

@@ -532,8 +531,8 @@ PHP_FUNCTION(password_hash)
532531
int status = 0;
533532

534533
encoded_len = argon2_encodedlen(
535-
t_cost,
536-
m_cost,
534+
time_cost,
535+
memory_cost,
537536
threads,
538537
(uint32_t)salt_len,
539538
out_len
@@ -543,8 +542,8 @@ PHP_FUNCTION(password_hash)
543542
zend_string *encoded = zend_string_alloc(encoded_len, 0);
544543

545544
status = argon2_hash(
546-
t_cost,
547-
m_cost,
545+
time_cost,
546+
memory_cost,
548547
threads,
549548
password,
550549
password_len,

ext/standard/php_password.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ PHP_MINIT_FUNCTION(password);
3333
#define PHP_PASSWORD_BCRYPT_COST 10
3434

3535
#if HAVE_ARGON2LIB
36-
#define PHP_PASSWORD_ARGON2 PHP_PASSWORD_ARGON2I
37-
#define PHP_PASSWORD_ARGON2_MEMORY_COST 1<<16
38-
#define PHP_PASSWORD_ARGON2_TIME_COST 3
39-
#define PHP_PASSWORD_ARGON2_THREADS 1
36+
#define PHP_PASSWORD_ARGON2_MEMORY_COST 1<<10
37+
#define PHP_PASSWORD_ARGON2_TIME_COST 2
38+
#define PHP_PASSWORD_ARGON2_THREADS 2
4039
#endif
4140

4241
typedef enum {

ext/standard/tests/password/password_get_info_argon2.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Test normal operation of password_get_info() with Argon2
33
--SKIPIF--
44
<?php
5-
if (!defined('PASSWORD_ARGON2')) die('Skipped: password_get_info not built with Argon2');
5+
if (!defined('PASSWORD_ARGON2I')) die('Skipped: password_get_info not built with Argon2');
66
?>
77
--FILE--
88
<?php
@@ -18,9 +18,9 @@ array(3) {
1818
string(7) "argon2i"
1919
["options"]=>
2020
array(3) {
21-
["m_cost"]=>
21+
["memory_cost"]=>
2222
int(65536)
23-
["t_cost"]=>
23+
["time_cost"]=>
2424
int(3)
2525
["threads"]=>
2626
int(1)

ext/standard/tests/password/password_hash_argon2.phpt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
22
Test normal operation of password_hash() with argon2
33
--SKIPIF--
44
<?php
5-
if (!defined('PASSWORD_ARGON2')) die('Skipped: password_get_info not built with Argon2');
5+
if (!defined('PASSWORD_ARGON2I')) die('Skipped: password_get_info not built with Argon2');
66
--FILE--
77
<?php
88

99
$password = "the password for testing 12345!";
1010

11-
$hash = password_hash($password, PASSWORD_ARGON2);
12-
var_dump(password_verify($password, $hash));
13-
1411
$hash = password_hash($password, PASSWORD_ARGON2I);
1512
var_dump(password_verify($password, $hash));
1613

1714
echo "OK!";
1815
?>
1916
--EXPECT--
2017
bool(true)
21-
bool(true)
2218
OK!

ext/standard/tests/password/password_hash_error_argon2.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
Test error operation of password_hash() with argon2
33
--SKIPIF--
44
<?php
5-
if (!defined('PASSWORD_ARGON2')) die('Skipped: password_get_info not built with Argon2');
5+
if (!defined('PASSWORD_ARGON2I')) die('Skipped: password_get_info not built with Argon2');
66
?>
77
--FILE--
88
<?php
9-
var_dump(password_hash('test', PASSWORD_ARGON2, ['m_cost' => 0]));
10-
var_dump(password_hash('test', PASSWORD_ARGON2, ['t_cost' => 0]));
11-
var_dump(password_hash('test', PASSWORD_ARGON2, ['threads' => 0]));
9+
var_dump(password_hash('test', PASSWORD_ARGON2I, ['memory_cost' => 0]));
10+
var_dump(password_hash('test', PASSWORD_ARGON2I, ['time_cost' => 0]));
11+
var_dump(password_hash('test', PASSWORD_ARGON2I, ['threads' => 0]));
1212
?>
1313
--EXPECTF--
1414
Warning: password_hash(): Memory cost is outside of allowed memory range in %s on line %d

ext/standard/tests/password/password_needs_rehash_argon2.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
Test normal operation of password_needs_rehash() with argon2
33
--SKIPIF--
44
<?php
5-
if (!defined('PASSWORD_ARGON2')) die('Skipped: password_get_info not built with Argon2');
5+
if (!defined('PASSWORD_ARGON2I')) die('Skipped: password_get_info not built with Argon2');
66
?>
77
--FILE--
88
<?php
99

1010
$hash = '$argon2i$v=19$m=65536,t=3,p=1$YkprUktYN0lHQTd2bWRFeA$79aA+6IvgclpDAJVoezProlqzIPy7do/P0sBDXS9Nn0';
11-
var_dump(password_needs_rehash($hash, PASSWORD_ARGON2));
12-
var_dump(password_needs_rehash($hash, PASSWORD_ARGON2, ['m_cost' => 1<<17]));
13-
var_dump(password_needs_rehash($hash, PASSWORD_ARGON2, ['t_cost' => 2]));
14-
var_dump(password_needs_rehash($hash, PASSWORD_ARGON2, ['threads' => 2]));
11+
var_dump(password_needs_rehash($hash, PASSWORD_ARGON2I));
12+
var_dump(password_needs_rehash($hash, PASSWORD_ARGON2I, ['memory_cost' => 1<<17]));
13+
var_dump(password_needs_rehash($hash, PASSWORD_ARGON2I, ['time_cost' => 2]));
14+
var_dump(password_needs_rehash($hash, PASSWORD_ARGON2I, ['threads' => 2]));
1515
echo "OK!";
1616
?>
1717
--EXPECT--

ext/standard/tests/password/password_verify_argon2.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Test normal operation of password_verify() with argon2
33
--SKIPIF--
44
<?php
5-
if (!defined('PASSWORD_ARGON2')) die('Skipped: password_get_info not built with Argon2');
5+
if (!defined('PASSWORD_ARGON2I')) die('Skipped: password_get_info not built with Argon2');
66
?>
77
--FILE--
88
<?php

0 commit comments

Comments
 (0)