Skip to content

Commit ddcc40f

Browse files
committed
Cleanup and CS
1 parent 3b67b4e commit ddcc40f

File tree

4 files changed

+51
-56
lines changed

4 files changed

+51
-56
lines changed

ext/openssl/openssl_pwhash.c

+5-10
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
#define PHP_OPENSSL_SALT_SIZE 16
4646
#define PHP_OPENSSL_HASH_SIZE 32
47-
#define PHP_OPENSSL_DIGEST_SIZE 128
47+
#define PHP_OPENSSL_DIGEST_SIZE 128
4848

4949
static inline zend_result get_options(zend_array *options, uint32_t *memlimit, uint32_t *iterlimit, uint32_t *threads)
5050
{
@@ -93,16 +93,12 @@ static bool php_openssl_argon2_compute_hash(
9393
unsigned char *hash, size_t hash_len)
9494
{
9595
OSSL_PARAM params[7], *p = params;
96-
OSSL_LIB_CTX *ctx = NULL;
9796
EVP_KDF *kdf = NULL;
9897
EVP_KDF_CTX *kctx = NULL;
9998
bool ret = false;
10099

101100
if (threads > 1) {
102-
if ((ctx = OSSL_LIB_CTX_new()) == NULL) {
103-
goto fail;
104-
}
105-
if (OSSL_set_max_threads(ctx, threads) != 1) {
101+
if (OSSL_set_max_threads(NULL, threads) != 1) {
106102
goto fail;
107103
}
108104
}
@@ -121,7 +117,7 @@ static bool php_openssl_argon2_compute_hash(
121117
(void *)pass, pass_len);
122118
*p++ = OSSL_PARAM_construct_end();
123119

124-
if ((kdf = EVP_KDF_fetch(ctx, algo, NULL)) == NULL) {
120+
if ((kdf = EVP_KDF_fetch(NULL, algo, NULL)) == NULL) {
125121
goto fail;
126122
}
127123
if ((kctx = EVP_KDF_CTX_new(kdf)) == NULL) {
@@ -139,8 +135,7 @@ static bool php_openssl_argon2_compute_hash(
139135
EVP_KDF_CTX_free(kctx);
140136

141137
if (threads > 1) {
142-
OSSL_set_max_threads(ctx, 0);
143-
OSSL_LIB_CTX_free(ctx);
138+
OSSL_set_max_threads(NULL, 0);
144139
}
145140
return ret;
146141
}
@@ -200,7 +195,7 @@ static int php_openssl_argon2_extract(
200195
return FAILURE;
201196
}
202197
if (sscanf(p, "v=%" PRIu32 "$m=%" PRIu32 ",t=%" PRIu32 ",p=%" PRIu32,
203-
version, memlimit, iterlimit, threads) != 4) {
198+
version, memlimit, iterlimit, threads) != 4) {
204199
return FAILURE;
205200
}
206201
if (salt && hash) {

ext/openssl/tests/openssl_password.phpt

+12-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ openssl
55
--SKIPIF--
66
<?php
77
if (!function_exists('openssl_password_hash')) {
8-
echo "skip - No openssl_password_hash";
8+
echo "skip - No openssl_password_hash";
99
}
1010
?>
1111
--FILE--
@@ -15,18 +15,18 @@ echo 'Argon2 provider: ';
1515
var_dump(PASSWORD_ARGON2_PROVIDER);
1616

1717
foreach([1, 2] as $mem) {
18-
foreach([1, 2] as $time) {
19-
$opts = [
20-
'memory_cost' => PASSWORD_ARGON2_DEFAULT_MEMORY_COST / $mem,
21-
'time_cost' => PASSWORD_ARGON2_DEFAULT_TIME_COST / $time,
22-
'threads' => PASSWORD_ARGON2_DEFAULT_THREADS,
23-
];
24-
foreach(['argon2i', 'argon2id'] as $algo) {
25-
$pass = "secret$mem$time$algo";
26-
$hash = openssl_password_hash($algo, $pass, $opts);
27-
var_dump(openssl_password_verify($algo, $pass, $hash));
18+
foreach([1, 2] as $time) {
19+
$opts = [
20+
'memory_cost' => PASSWORD_ARGON2_DEFAULT_MEMORY_COST / $mem,
21+
'time_cost' => PASSWORD_ARGON2_DEFAULT_TIME_COST / $time,
22+
'threads' => PASSWORD_ARGON2_DEFAULT_THREADS,
23+
];
24+
foreach(['argon2i', 'argon2id'] as $algo) {
25+
$pass = "secret$mem$time$algo";
26+
$hash = openssl_password_hash($algo, $pass, $opts);
27+
var_dump(openssl_password_verify($algo, $pass, $hash));
28+
}
2829
}
29-
}
3030
}
3131
?>
3232
--EXPECTF--

ext/openssl/tests/openssl_password_compat.phpt

+17-17
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ sodium
66
--SKIPIF--
77
<?php
88
if (!function_exists('sodium_crypto_pwhash_str_verify')) {
9-
echo "skip - No crypto_pwhash_str_verify";
9+
echo "skip - No crypto_pwhash_str_verify";
1010
}
1111

1212
if (!function_exists('openssl_password_hash')) {
13-
echo "skip - No crypto_pwhash_str_verify";
13+
echo "skip - No crypto_pwhash_str_verify";
1414
}
1515
?>
1616
--FILE--
@@ -20,23 +20,23 @@ echo 'Argon2 provider: ';
2020
var_dump(PASSWORD_ARGON2_PROVIDER);
2121

2222
foreach([1, 2] as $mem) {
23-
foreach([1, 2] as $time) {
24-
$opts = [
25-
'memory_cost' => PASSWORD_ARGON2_DEFAULT_MEMORY_COST / $mem,
26-
'time_cost' => PASSWORD_ARGON2_DEFAULT_TIME_COST / $time,
27-
'threads' => PASSWORD_ARGON2_DEFAULT_THREADS,
28-
];
29-
$algo = 'argon2id';
30-
$pass = "secret$mem$time$algo";
23+
foreach([1, 2] as $time) {
24+
$opts = [
25+
'memory_cost' => PASSWORD_ARGON2_DEFAULT_MEMORY_COST / $mem,
26+
'time_cost' => PASSWORD_ARGON2_DEFAULT_TIME_COST / $time,
27+
'threads' => PASSWORD_ARGON2_DEFAULT_THREADS,
28+
];
29+
$algo = 'argon2id';
30+
$pass = "secret$mem$time$algo";
3131

32-
/* hash with libsodium / verify with openssl */
33-
$hash = sodium_crypto_pwhash_str($pass, PASSWORD_ARGON2_DEFAULT_TIME_COST / $time, PASSWORD_ARGON2_DEFAULT_MEMORY_COST / $mem);
34-
var_dump(openssl_password_verify($algo, $pass, $hash));
32+
/* hash with libsodium / verify with openssl */
33+
$hash = sodium_crypto_pwhash_str($pass, PASSWORD_ARGON2_DEFAULT_TIME_COST / $time, PASSWORD_ARGON2_DEFAULT_MEMORY_COST / $mem);
34+
var_dump(openssl_password_verify($algo, $pass, $hash));
3535

36-
/* hash with openssl / verify with libsodium */
37-
$hash = openssl_password_hash($algo, $pass, $opts);
38-
var_dump(sodium_crypto_pwhash_str_verify($hash, $pass));
39-
}
36+
/* hash with openssl / verify with libsodium */
37+
$hash = openssl_password_hash($algo, $pass, $opts);
38+
var_dump(sodium_crypto_pwhash_str_verify($hash, $pass));
39+
}
4040
}
4141
?>
4242
--EXPECTF--

ext/openssl/tests/openssl_password_compat2.phpt

+17-17
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ sodium
66
--SKIPIF--
77
<?php
88
if (PASSWORD_ARGON2_PROVIDER != "standard") {
9-
echo "skip - libargon2 not available";
9+
echo "skip - libargon2 not available";
1010
}
1111

1212
if (!function_exists('openssl_password_hash')) {
13-
echo "skip - No crypto_pwhash_str_verify";
13+
echo "skip - No crypto_pwhash_str_verify";
1414
}
1515
?>
1616
--FILE--
@@ -20,23 +20,23 @@ echo 'Argon2 provider: ';
2020
var_dump(PASSWORD_ARGON2_PROVIDER);
2121

2222
foreach([1, 2] as $mem) {
23-
foreach([1, 2] as $time) {
24-
$opts = [
25-
'memory_cost' => PASSWORD_ARGON2_DEFAULT_MEMORY_COST / $mem,
26-
'time_cost' => PASSWORD_ARGON2_DEFAULT_TIME_COST / $time,
27-
'threads' => PASSWORD_ARGON2_DEFAULT_THREADS,
28-
];
29-
$algo = 'argon2id';
30-
$pass = "secret$mem$time$algo";
23+
foreach([1, 2] as $time) {
24+
$opts = [
25+
'memory_cost' => PASSWORD_ARGON2_DEFAULT_MEMORY_COST / $mem,
26+
'time_cost' => PASSWORD_ARGON2_DEFAULT_TIME_COST / $time,
27+
'threads' => PASSWORD_ARGON2_DEFAULT_THREADS,
28+
];
29+
$algo = 'argon2id';
30+
$pass = "secret$mem$time$algo";
3131

32-
/* hash with libargon2 / verify with openssl */
33-
$hash = password_hash($pass, PASSWORD_ARGON2ID, $opts);
34-
var_dump(openssl_password_verify($algo, $pass, $hash));
32+
/* hash with libargon2 / verify with openssl */
33+
$hash = password_hash($pass, PASSWORD_ARGON2ID, $opts);
34+
var_dump(openssl_password_verify($algo, $pass, $hash));
3535

36-
/* hash with openssl / verify with libargon2 */
37-
$hash = openssl_password_hash($algo, $pass, $opts);
38-
var_dump(password_verify($pass, $hash));
39-
}
36+
/* hash with openssl / verify with libargon2 */
37+
$hash = openssl_password_hash($algo, $pass, $opts);
38+
var_dump(password_verify($pass, $hash));
39+
}
4040
}
4141
?>
4242
--EXPECT--

0 commit comments

Comments
 (0)