Merge "Chrome Origin Trial support"
[lhc/web/wiklou.git] / includes / password / Argon2Password.php
1 <?php
2
3 use Wikimedia\Assert\Assert;
4
5 /**
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 */
23
24 /**
25 * Implements Argon2, a modern key derivation algorithm designed to resist GPU cracking and
26 * side-channel attacks.
27 *
28 * @see https://en.wikipedia.org/wiki/Argon2
29 */
30 class Argon2Password extends Password {
31 /**
32 * @var null[] Array with known password_hash() option names as keys
33 */
34 private static $knownOptions = [
35 'memory_cost' => null,
36 'time_cost' => null,
37 'threads' => null,
38 ];
39
40 /**
41 * @inheritDoc
42 */
43 protected function isSupported() {
44 // It is actually possible to have a PHP build with Argon2i but not Argon2id
45 return defined( 'PASSWORD_ARGON2I' ) || defined( 'PASSWORD_ARGON2ID' );
46 }
47
48 /**
49 * @return mixed[] Array of 2nd and third parmeters to password_hash()
50 */
51 private function prepareParams() {
52 switch ( $this->config['algo'] ) {
53 case 'argon2i':
54 $algo = PASSWORD_ARGON2I;
55 break;
56 case 'argon2id':
57 $algo = PASSWORD_ARGON2ID;
58 break;
59 case 'auto':
60 $algo = defined( 'PASSWORD_ARGON2ID' ) ? PASSWORD_ARGON2ID : PASSWORD_ARGON2I;
61 break;
62 default:
63 throw new LogicException( "Unexpected algo: {$this->config['algo']}" );
64
65 }
66
67 $params = array_intersect_key( $this->config, self::$knownOptions );
68
69 return [ $algo, $params ];
70 }
71
72 /**
73 * @inheritDoc
74 */
75 public function crypt( $password ) {
76 list( $algo, $params ) = $this->prepareParams();
77 $this->hash = password_hash( $password, $algo, $params );
78 }
79
80 /**
81 * @inheritDoc
82 */
83 public function equals( $other ) {
84 if ( is_string( $other ) ) {
85 return $this->verify( $other );
86 }
87
88 // Argon2 key derivation is not deterministic, can't pass objects to equals()
89 return false;
90 }
91
92 /**
93 * @inheritDoc
94 */
95 public function verify( $password ) {
96 Assert::parameterType( 'string', $password, '$password' );
97
98 return password_verify( $password, $this->hash );
99 }
100
101 /**
102 * @inheritDoc
103 */
104 public function toString() {
105 $res = ":argon2:{$this->hash}";
106 $this->assertIsSafeSize( $res );
107 return $res;
108 }
109
110 /**
111 * @inheritDoc
112 */
113 public function needsUpdate() {
114 list( $algo, $params ) = $this->prepareParams();
115 return password_needs_rehash( $this->hash, $algo, $params );
116 }
117 }