Skip to content

Commit 7e7fa77

Browse files
committed
Validate interoperability between libsodium and libargon for argon2i(d) hashing
1 parent d124640 commit 7e7fa77

File tree

3 files changed

+291
-0
lines changed

3 files changed

+291
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
--TEST--
2+
Test interoperability of password_hash('argon2i')
3+
--SKIPIF--
4+
<?php
5+
if (!function_exits('sodium_crypto_pwhash_str_verify')) {
6+
echo "skip - No crypto_pwhash_str_verify";
7+
}
8+
if (!in_array('argon2i', password_algos(), true /* strict */)) {
9+
echo "skip - No argon2i support in password_hash()";
10+
}
11+
--FILE--
12+
<?php
13+
14+
foreach([1, 2, 4] as $mem) {
15+
foreach([1, 2, 4] as $time) {
16+
foreach([1, 2, 4] as $threads) {
17+
$opts = [
18+
'memory_cost' => PASSWORD_ARGON2_DEFAULT_MEMORY_COST * $mem,
19+
'time_cost' => PASSWORD_ARGON2_DEFAULT_TIME_COST * $time,
20+
'threads' => PASSWORD_ARGON2_DEFAULT_THREADS * $threads,
21+
];
22+
$password = random_bytes(32);
23+
echo "Using password: ";
24+
var_dump(base64_encode($password));
25+
$hash = password_hash($password, 'argon2i', $opts);
26+
echo "Hash: "; var_dump($hash);
27+
var_dump(sodium_crypto_pwhash_str_verify($hash, $password));
28+
}
29+
}
30+
}
31+
--EXPECTF--
32+
Using password: string(44) "%s"
33+
Hash: string(95) "$argon2i$v=19$m=1024,t=3,p=2$%s"
34+
bool(true)
35+
Using password: string(44) "%s"
36+
Hash: string(95) "$argon2i$v=19$m=1024,t=3,p=4$%s"
37+
bool(true)
38+
Using password: string(44) "%s"
39+
Hash: string(95) "$argon2i$v=19$m=1024,t=3,p=8$%s"
40+
bool(true)
41+
Using password: string(44) "%s"
42+
Hash: string(95) "$argon2i$v=19$m=1024,t=6,p=2$%s"
43+
bool(true)
44+
Using password: string(44) "%s"
45+
Hash: string(95) "$argon2i$v=19$m=1024,t=6,p=4$%s"
46+
bool(true)
47+
Using password: string(44) "%s"
48+
Hash: string(95) "$argon2i$v=19$m=1024,t=6,p=8$%s"
49+
bool(true)
50+
Using password: string(44) "%s"
51+
Hash: string(96) "$argon2i$v=19$m=1024,t=12,p=2$%s"
52+
bool(true)
53+
Using password: string(44) "%s"
54+
Hash: string(96) "$argon2i$v=19$m=1024,t=12,p=4$%s"
55+
bool(true)
56+
Using password: string(44) "%s"
57+
Hash: string(96) "$argon2i$v=19$m=1024,t=12,p=8$%s"
58+
bool(true)
59+
Using password: string(44) "%s"
60+
Hash: string(95) "$argon2i$v=19$m=2048,t=3,p=2$%s"
61+
bool(true)
62+
Using password: string(44) "%s"
63+
Hash: string(95) "$argon2i$v=19$m=2048,t=3,p=4$%s"
64+
bool(true)
65+
Using password: string(44) "%s"
66+
Hash: string(95) "$argon2i$v=19$m=2048,t=3,p=8$%s"
67+
bool(true)
68+
Using password: string(44) "%s"
69+
Hash: string(95) "$argon2i$v=19$m=2048,t=6,p=2$%s"
70+
bool(true)
71+
Using password: string(44) "%s"
72+
Hash: string(95) "$argon2i$v=19$m=2048,t=6,p=4$%s"
73+
bool(true)
74+
Using password: string(44) "%s"
75+
Hash: string(95) "$argon2i$v=19$m=2048,t=6,p=8$%s"
76+
bool(true)
77+
Using password: string(44) "%s"
78+
Hash: string(96) "$argon2i$v=19$m=2048,t=12,p=2$%s"
79+
bool(true)
80+
Using password: string(44) "%s"
81+
Hash: string(96) "$argon2i$v=19$m=2048,t=12,p=4$%s"
82+
bool(true)
83+
Using password: string(44) "%s"
84+
Hash: string(96) "$argon2i$v=19$m=2048,t=12,p=8$%s"
85+
bool(true)
86+
Using password: string(44) "%s"
87+
Hash: string(95) "$argon2i$v=19$m=4096,t=3,p=2$%s"
88+
bool(true)
89+
Using password: string(44) "%s"
90+
Hash: string(95) "$argon2i$v=19$m=4096,t=3,p=4$%s"
91+
bool(true)
92+
Using password: string(44) "%s"
93+
Hash: string(95) "$argon2i$v=19$m=4096,t=3,p=8$%s"
94+
bool(true)
95+
Using password: string(44) "%s"
96+
Hash: string(95) "$argon2i$v=19$m=4096,t=6,p=2$%s"
97+
bool(true)
98+
Using password: string(44) "%s"
99+
Hash: string(95) "$argon2i$v=19$m=4096,t=6,p=4$%s"
100+
bool(true)
101+
Using password: string(44) "%s"
102+
Hash: string(95) "$argon2i$v=19$m=4096,t=6,p=8$%s"
103+
bool(true)
104+
Using password: string(44) "%s"
105+
Hash: string(96) "$argon2i$v=19$m=4096,t=12,p=2$%s"
106+
bool(true)
107+
Using password: string(44) "%s"
108+
Hash: string(96) "$argon2i$v=19$m=4096,t=12,p=4$%s"
109+
bool(true)
110+
Using password: string(44) "%s"
111+
Hash: string(96) "$argon2i$v=19$m=4096,t=12,p=8$%s"
112+
bool(true)
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
--TEST--
2+
Test interoperability of password_hash('argon2id')
3+
--SKIPIF--
4+
<?php
5+
if (!function_exits('sodium_crypto_pwhash_str_verify')) {
6+
echo "skip - No crypto_pwhash_str_verify";
7+
}
8+
if (!in_array('argon2id', password_algos(), true /* strict */)) {
9+
echo "skip - No argon2id support in password_hash()";
10+
}
11+
--FILE--
12+
<?php
13+
14+
foreach([1, 2, 4] as $mem) {
15+
foreach([1, 2, 4] as $time) {
16+
foreach([1, 2, 4] as $threads) {
17+
$opts = [
18+
'memory_cost' => PASSWORD_ARGON2_DEFAULT_MEMORY_COST * $mem,
19+
'time_cost' => PASSWORD_ARGON2_DEFAULT_TIME_COST * $time,
20+
'threads' => PASSWORD_ARGON2_DEFAULT_THREADS * $threads,
21+
];
22+
$password = random_bytes(32);
23+
echo "Using password: ";
24+
var_dump(base64_encode($password));
25+
$hash = password_hash($password, 'argon2id', $opts);
26+
echo "Hash: "; var_dump($hash);
27+
var_dump(sodium_crypto_pwhash_str_verify($hash, $password));
28+
}
29+
}
30+
}
31+
--EXPECTF--
32+
Using password: string(44) "%s"
33+
Hash: string(96) "$argon2id$v=19$m=1024,t=3,p=2$%s"
34+
bool(true)
35+
Using password: string(44) "%s"
36+
Hash: string(96) "$argon2id$v=19$m=1024,t=3,p=4$%s"
37+
bool(true)
38+
Using password: string(44) "%s"
39+
Hash: string(96) "$argon2id$v=19$m=1024,t=3,p=8$%s"
40+
bool(true)
41+
Using password: string(44) "%s"
42+
Hash: string(96) "$argon2id$v=19$m=1024,t=6,p=2$%s"
43+
bool(true)
44+
Using password: string(44) "%s"
45+
Hash: string(96) "$argon2id$v=19$m=1024,t=6,p=4$%s"
46+
bool(true)
47+
Using password: string(44) "%s"
48+
Hash: string(96) "$argon2id$v=19$m=1024,t=6,p=8$%s"
49+
bool(true)
50+
Using password: string(44) "%s"
51+
Hash: string(97) "$argon2id$v=19$m=1024,t=12,p=2$%s"
52+
bool(true)
53+
Using password: string(44) "%s"
54+
Hash: string(97) "$argon2id$v=19$m=1024,t=12,p=4$%s"
55+
bool(true)
56+
Using password: string(44) "%s"
57+
Hash: string(97) "$argon2id$v=19$m=1024,t=12,p=8$%s"
58+
bool(true)
59+
Using password: string(44) "%s"
60+
Hash: string(96) "$argon2id$v=19$m=2048,t=3,p=2$%s"
61+
bool(true)
62+
Using password: string(44) "%s"
63+
Hash: string(96) "$argon2id$v=19$m=2048,t=3,p=4$%s"
64+
bool(true)
65+
Using password: string(44) "%s"
66+
Hash: string(96) "$argon2id$v=19$m=2048,t=3,p=8$%s"
67+
bool(true)
68+
Using password: string(44) "%s"
69+
Hash: string(96) "$argon2id$v=19$m=2048,t=6,p=2$%s"
70+
bool(true)
71+
Using password: string(44) "%s"
72+
Hash: string(96) "$argon2id$v=19$m=2048,t=6,p=4$%s"
73+
bool(true)
74+
Using password: string(44) "%s"
75+
Hash: string(96) "$argon2id$v=19$m=2048,t=6,p=8$%s"
76+
bool(true)
77+
Using password: string(44) "%s"
78+
Hash: string(97) "$argon2id$v=19$m=2048,t=12,p=2$%s"
79+
bool(true)
80+
Using password: string(44) "%s"
81+
Hash: string(97) "$argon2id$v=19$m=2048,t=12,p=4$%s"
82+
bool(true)
83+
Using password: string(44) "%s"
84+
Hash: string(97) "$argon2id$v=19$m=2048,t=12,p=8$%s"
85+
bool(true)
86+
Using password: string(44) "%s"
87+
Hash: string(96) "$argon2id$v=19$m=4096,t=3,p=2$%s"
88+
bool(true)
89+
Using password: string(44) "%s"
90+
Hash: string(96) "$argon2id$v=19$m=4096,t=3,p=4$%s"
91+
bool(true)
92+
Using password: string(44) "%s"
93+
Hash: string(96) "$argon2id$v=19$m=4096,t=3,p=8$%s"
94+
bool(true)
95+
Using password: string(44) "%s"
96+
Hash: string(96) "$argon2id$v=19$m=4096,t=6,p=2$%s"
97+
bool(true)
98+
Using password: string(44) "%s"
99+
Hash: string(96) "$argon2id$v=19$m=4096,t=6,p=4$%s"
100+
bool(true)
101+
Using password: string(44) "%s"
102+
Hash: string(96) "$argon2id$v=19$m=4096,t=6,p=8$%s"
103+
bool(true)
104+
Using password: string(44) "%s"
105+
Hash: string(97) "$argon2id$v=19$m=4096,t=12,p=2$%s"
106+
bool(true)
107+
Using password: string(44) "%s"
108+
Hash: string(97) "$argon2id$v=19$m=4096,t=12,p=4$%s"
109+
bool(true)
110+
Using password: string(44) "%s"
111+
Hash: string(97) "$argon2id$v=19$m=4096,t=12,p=8$%s"
112+
bool(true)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
--TEST--
2+
Test interoperability of password_verify()
3+
--SKIPIF--
4+
<?php
5+
if (!function_exits('sodium_crypto_pwhash_str')) {
6+
echo "skip - No crypto_pwhash_str_verify";
7+
}
8+
9+
// Depending on library version, libsodium may provide argon2i or argon2id hashes.
10+
$hash = sodium_crypto_pwhash_str("test", SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE, SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE);
11+
list(, $algo) = explode('$', $hash, 3);
12+
13+
if (!in_array($algo, password_algos(), true /* strict */)) {
14+
echo "skip - No $algo support in password_verify()";
15+
}
16+
--FILE--
17+
<?php
18+
19+
$opsSet = [
20+
SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE,
21+
SODIUM_CRYPTO_PWHASH_OPSLIMIT_MODERATE,
22+
SODIUM_CRYPTO_PWHASH_OPSLIMIT_SENSITIVE,
23+
];
24+
$memSet = [
25+
SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE,
26+
SODIUM_CRYPTO_PWHASH_MEMLIMIT_MODERATE,
27+
SODIUM_CRYPTO_PWHASH_MEMLIMIT_SENSITIVE,
28+
];
29+
30+
foreach($opsSet as $ops) {
31+
foreach($memSet as $mem) {
32+
$password = random_bytes(32);
33+
echo "Using password: ";
34+
var_dump(base64_encode($password));
35+
$hash = sodium_crypto_pwhash_str($password, $ops, $mem);
36+
echo "Hash: "; var_dump($hash);
37+
var_dump(password_verify($password, $hash));
38+
}
39+
}
40+
--EXPECTF--
41+
Using password: string(44) "%s"
42+
Hash: string(%d) "$argon2i%s"
43+
bool(true)
44+
Using password: string(44) "%s"
45+
Hash: string(%d) "$argon2i%s"
46+
bool(true)
47+
Using password: string(44) "%s"
48+
Hash: string(%d) "$argon2i%s"
49+
bool(true)
50+
Using password: string(44) "%s"
51+
Hash: string(%d) "$argon2i%s"
52+
bool(true)
53+
Using password: string(44) "%s"
54+
Hash: string(%d) "$argon2i%s"
55+
bool(true)
56+
Using password: string(44) "%s"
57+
Hash: string(%d) "$argon2i%s"
58+
bool(true)
59+
Using password: string(44) "%s"
60+
Hash: string(%d) "$argon2i%s"
61+
bool(true)
62+
Using password: string(44) "%s"
63+
Hash: string(%d) "$argon2i%s"
64+
bool(true)
65+
Using password: string(44) "%s"
66+
Hash: string(%d) "$argon2i%s"
67+
bool(true)

0 commit comments

Comments
 (0)