40
40
#define PHP_SODIUM_PWHASH_OPSLIMIT 4
41
41
#define PHP_SODIUM_PWHASH_THREADS 1
42
42
43
+ static inline int get_options (zend_array * options , size_t * memlimit , size_t * opslimit ) {
44
+ zval * opt ;
45
+
46
+ * opslimit = PHP_SODIUM_PWHASH_OPSLIMIT ;
47
+ * memlimit = PHP_SODIUM_PWHASH_MEMLIMIT << 10 ;
48
+ if (!options ) {
49
+ return SUCCESS ;
50
+ }
51
+ if ((opt = zend_hash_str_find (options , "memory_cost" , strlen ("memory_cost" )))) {
52
+ zend_long smemlimit = zval_get_long (opt );
53
+
54
+ if ((smemlimit < 0 ) || (smemlimit < crypto_pwhash_MEMLIMIT_MIN >> 10 ) || (smemlimit > (crypto_pwhash_MEMLIMIT_MAX >> 10 ))) {
55
+ php_error_docref (NULL , E_WARNING , "Memory cost is outside of allowed memory range" );
56
+ return FAILURE ;
57
+ }
58
+ * memlimit = smemlimit << 10 ;
59
+ }
60
+ if ((opt = zend_hash_str_find (options , "time_cost" , strlen ("time_cost" )))) {
61
+ * opslimit = zval_get_long (opt );
62
+ if ((* opslimit < crypto_pwhash_OPSLIMIT_MIN ) || (* opslimit > crypto_pwhash_OPSLIMIT_MAX )) {
63
+ php_error_docref (NULL , E_WARNING , "Time cost is outside of allowed time range" );
64
+ return FAILURE ;
65
+ }
66
+ }
67
+ if ((opt = zend_hash_str_find (options , "threads" , strlen ("threads" ))) && (zval_get_long (opt ) != 1 )) {
68
+ php_error_docref (NULL , E_WARNING , "A thread value other than 1 is not supported by this implementation" );
69
+ return FAILURE ;
70
+ }
71
+ return SUCCESS ;
72
+ }
73
+
43
74
static zend_string * php_sodium_argon2_hash (const zend_string * password , zend_array * options , int alg ) {
44
- size_t opslimit = PHP_SODIUM_PWHASH_OPSLIMIT ;
45
- size_t memlimit = PHP_SODIUM_PWHASH_MEMLIMIT ;
75
+ size_t opslimit , memlimit ;
46
76
zend_string * ret ;
47
77
48
78
if ((ZSTR_LEN (password ) >= 0xffffffff )) {
49
79
php_error_docref (NULL , E_WARNING , "Password is too long" );
50
80
return NULL ;
51
81
}
52
82
53
- if (options ) {
54
- zval * opt ;
55
- if ((opt = zend_hash_str_find (options , "memory_cost" , strlen ("memory_cost" )))) {
56
- memlimit = zval_get_long (opt );
57
- if ((memlimit < crypto_pwhash_MEMLIMIT_MIN ) || (memlimit > crypto_pwhash_MEMLIMIT_MAX )) {
58
- php_error_docref (NULL , E_WARNING , "Memory cost is outside of allowed memory range" );
59
- return NULL ;
60
- }
61
- }
62
- if ((opt = zend_hash_str_find (options , "time_cost" , strlen ("time_cost" )))) {
63
- opslimit = zval_get_long (opt );
64
- if ((opslimit < crypto_pwhash_OPSLIMIT_MIN ) || (opslimit > crypto_pwhash_OPSLIMIT_MAX )) {
65
- php_error_docref (NULL , E_WARNING , "Time cost is outside of allowed time range" );
66
- return NULL ;
67
- }
68
- }
69
- if ((opt = zend_hash_str_find (options , "threads" , strlen ("threads" ))) && (zval_get_long (opt ) != 1 )) {
70
- php_error_docref (NULL , E_WARNING , "A thread value other than 1 is not supported by this implementation" );
71
- return NULL ;
72
- }
83
+ if (get_options (options , & memlimit , & opslimit ) == FAILURE ) {
84
+ return NULL ;
73
85
}
74
86
75
87
ret = zend_string_alloc (crypto_pwhash_STRBYTES - 1 , 0 );
76
- if (crypto_pwhash_str_alg (ZSTR_VAL (ret ), ZSTR_VAL (password ), ZSTR_LEN (password ), opslimit , memlimit << 10 , alg )) {
88
+ if (crypto_pwhash_str_alg (ZSTR_VAL (ret ), ZSTR_VAL (password ), ZSTR_LEN (password ), opslimit , memlimit , alg )) {
77
89
php_error_docref (NULL , E_WARNING , "Unexpected failure hashing password" );
78
90
zend_string_release (ret );
79
91
return NULL ;
@@ -93,32 +105,12 @@ static zend_bool php_sodium_argon2_verify(const zend_string *password, const zen
93
105
}
94
106
95
107
static zend_bool php_sodium_argon2_needs_rehash (const zend_string * hash , zend_array * options ) {
96
- size_t opslimit = PHP_SODIUM_PWHASH_OPSLIMIT ;
97
- size_t memlimit = PHP_SODIUM_PWHASH_MEMLIMIT ;
98
-
99
- if (options ) {
100
- zval * opt ;
101
- if ((opt = zend_hash_str_find (options , "memory_cost" , strlen ("memory_cost" )))) {
102
- memlimit = zval_get_long (opt );
103
- if ((memlimit < crypto_pwhash_MEMLIMIT_MIN ) || (memlimit > crypto_pwhash_MEMLIMIT_MAX )) {
104
- php_error_docref (NULL , E_WARNING , "Memory cost is outside of allowed memory range" );
105
- return 1 ;
106
- }
107
- }
108
- if ((opt = zend_hash_str_find (options , "time_cost" , strlen ("time_cost" )))) {
109
- opslimit = zval_get_long (opt );
110
- if ((opslimit < crypto_pwhash_OPSLIMIT_MIN ) || (opslimit > crypto_pwhash_OPSLIMIT_MAX )) {
111
- php_error_docref (NULL , E_WARNING , "Time cost is outside of allowed time range" );
112
- return 1 ;
113
- }
114
- }
115
- if ((opt = zend_hash_str_find (options , "threads" , strlen ("threads" ))) && (zval_get_long (opt ) != 1 )) {
116
- php_error_docref (NULL , E_WARNING , "A thread value other than 1 is not supported by this implementation" );
117
- return 1 ;
118
- }
119
- }
108
+ size_t opslimit , memlimit ;
120
109
121
- return crypto_pwhash_str_needs_rehash (ZSTR_VAL (hash ), opslimit , memlimit << 10 );
110
+ if (get_options (options , & memlimit , & opslimit ) == FAILURE ) {
111
+ return 1 ;
112
+ }
113
+ return crypto_pwhash_str_needs_rehash (ZSTR_VAL (hash ), opslimit , memlimit );
122
114
}
123
115
124
116
static int php_sodium_argon2_get_info (zval * return_value , const zend_string * hash ) {
0 commit comments