@@ -99,6 +99,10 @@ static struct mhash_bc_entry mhash_to_hash[MHASH_NUM_ALGOS] = {
99
99
{"XXH3" , "xxh3" , 40 },
100
100
{"XXH128" , "xxh128" , 41 },
101
101
};
102
+ static bool php_is_valid_mhash_algo (zend_long hash_algo ) {
103
+ return hash_algo >= 0 && hash_algo < MHASH_NUM_ALGOS && hash_algo != 4 && hash_algo != 6 && hash_algo != 26 ;
104
+ }
105
+
102
106
#endif
103
107
104
108
/* Hash Registry Access */
@@ -1184,10 +1188,11 @@ static void mhash_init(INIT_FUNC_ARGS)
1184
1188
int algo_number = 0 ;
1185
1189
1186
1190
for (algo_number = 0 ; algo_number < MHASH_NUM_ALGOS ; algo_number ++ ) {
1187
- struct mhash_bc_entry algorithm = mhash_to_hash [algo_number ];
1188
- if (algorithm .mhash_name == NULL ) {
1191
+ if (!php_is_valid_mhash_algo (algo_number )) {
1189
1192
continue ;
1190
1193
}
1194
+ struct mhash_bc_entry algorithm = mhash_to_hash [algo_number ];
1195
+ ZEND_ASSERT (algorithm .mhash_name != NULL );
1191
1196
1192
1197
len = slprintf (buf , 127 , "MHASH_%s" , algorithm .mhash_name );
1193
1198
zend_register_long_constant (buf , len , algorithm .value , CONST_CS | CONST_PERSISTENT , module_number );
@@ -1209,11 +1214,12 @@ PHP_FUNCTION(mhash)
1209
1214
}
1210
1215
1211
1216
/* need to convert the first parameter from int constant to string algorithm name */
1212
- if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS ) {
1217
+ if (php_is_valid_mhash_algo ( algorithm ) ) {
1213
1218
struct mhash_bc_entry algorithm_lookup = mhash_to_hash [algorithm ];
1214
- if (algorithm_lookup .hash_name ) {
1215
- algo = zend_string_init (algorithm_lookup .hash_name , strlen (algorithm_lookup .hash_name ), 0 );
1216
- }
1219
+ ZEND_ASSERT (algorithm_lookup .hash_name != NULL );
1220
+ algo = zend_string_init (algorithm_lookup .hash_name , strlen (algorithm_lookup .hash_name ), 0 );
1221
+ } else {
1222
+ RETURN_FALSE ;
1217
1223
}
1218
1224
1219
1225
if (key ) {
@@ -1222,9 +1228,8 @@ PHP_FUNCTION(mhash)
1222
1228
php_hash_do_hash (return_value , algo , data , data_len , 1 , 0 , NULL );
1223
1229
}
1224
1230
1225
- if (algo ) {
1226
- zend_string_release (algo );
1227
- }
1231
+ ZEND_ASSERT (algo != NULL );
1232
+ zend_string_release (algo );
1228
1233
}
1229
1234
/* }}} */
1230
1235
@@ -1237,11 +1242,10 @@ PHP_FUNCTION(mhash_get_hash_name)
1237
1242
RETURN_THROWS ();
1238
1243
}
1239
1244
1240
- if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS ) {
1245
+ if (php_is_valid_mhash_algo ( algorithm ) ) {
1241
1246
struct mhash_bc_entry algorithm_lookup = mhash_to_hash [algorithm ];
1242
- if (algorithm_lookup .mhash_name ) {
1243
- RETURN_STRING (algorithm_lookup .mhash_name );
1244
- }
1247
+ ZEND_ASSERT (algorithm_lookup .mhash_name != NULL );
1248
+ RETURN_STRING (algorithm_lookup .mhash_name );
1245
1249
}
1246
1250
RETURN_FALSE ;
1247
1251
}
@@ -1267,14 +1271,12 @@ PHP_FUNCTION(mhash_get_block_size)
1267
1271
}
1268
1272
RETVAL_FALSE ;
1269
1273
1270
- if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS ) {
1274
+ if (php_is_valid_mhash_algo ( algorithm ) ) {
1271
1275
struct mhash_bc_entry algorithm_lookup = mhash_to_hash [algorithm ];
1272
- if (algorithm_lookup .mhash_name ) {
1273
- const php_hash_ops * ops = zend_hash_str_find_ptr (& php_hash_hashtable , algorithm_lookup .hash_name , strlen (algorithm_lookup .hash_name ));
1274
- if (ops ) {
1275
- RETVAL_LONG (ops -> digest_size );
1276
- }
1277
- }
1276
+ ZEND_ASSERT (algorithm_lookup .hash_name != NULL );
1277
+ const php_hash_ops * ops = zend_hash_str_find_ptr (& php_hash_hashtable , algorithm_lookup .hash_name , strlen (algorithm_lookup .hash_name ));
1278
+ ZEND_ASSERT (ops != NULL );
1279
+ RETVAL_LONG (ops -> digest_size );
1278
1280
}
1279
1281
}
1280
1282
/* }}} */
@@ -1309,47 +1311,46 @@ PHP_FUNCTION(mhash_keygen_s2k)
1309
1311
salt_len = SALT_SIZE ;
1310
1312
1311
1313
RETVAL_FALSE ;
1312
- if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS ) {
1314
+ if (php_is_valid_mhash_algo ( algorithm ) ) {
1313
1315
struct mhash_bc_entry algorithm_lookup = mhash_to_hash [algorithm ];
1314
- if (algorithm_lookup .mhash_name ) {
1315
- const php_hash_ops * ops = zend_hash_str_find_ptr (& php_hash_hashtable , algorithm_lookup .hash_name , strlen (algorithm_lookup .hash_name ));
1316
- if (ops ) {
1317
- unsigned char null = '\0' ;
1318
- void * context ;
1319
- char * key , * digest ;
1320
- int i = 0 , j = 0 ;
1321
- size_t block_size = ops -> digest_size ;
1322
- size_t times = bytes / block_size ;
1323
-
1324
- if ((bytes % block_size ) != 0 ) {
1325
- times ++ ;
1326
- }
1327
-
1328
- context = php_hash_alloc_context (ops );
1329
- ops -> hash_init (context , NULL );
1316
+ ZEND_ASSERT (algorithm_lookup .hash_name != NULL );
1317
+ const php_hash_ops * ops = zend_hash_str_find_ptr (& php_hash_hashtable , algorithm_lookup .hash_name , strlen (algorithm_lookup .hash_name ));
1318
+ ZEND_ASSERT (ops != NULL );
1319
+
1320
+ unsigned char null = '\0' ;
1321
+ void * context ;
1322
+ char * key , * digest ;
1323
+ int i = 0 , j = 0 ;
1324
+ size_t block_size = ops -> digest_size ;
1325
+ size_t times = bytes / block_size ;
1326
+
1327
+ if ((bytes % block_size ) != 0 ) {
1328
+ times ++ ;
1329
+ }
1330
1330
1331
- key = ecalloc ( 1 , times * block_size );
1332
- digest = emalloc ( ops -> digest_size + 1 );
1331
+ context = php_hash_alloc_context ( ops );
1332
+ ops -> hash_init ( context , NULL );
1333
1333
1334
- for ( i = 0 ; i < times ; i ++ ) {
1335
- ops -> hash_init ( context , NULL );
1334
+ key = ecalloc ( 1 , times * block_size );
1335
+ digest = emalloc ( ops -> digest_size + 1 );
1336
1336
1337
- for (j = 0 ;j < i ;j ++ ) {
1338
- ops -> hash_update (context , & null , 1 );
1339
- }
1340
- ops -> hash_update (context , (unsigned char * )padded_salt , salt_len );
1341
- ops -> hash_update (context , (unsigned char * )password , password_len );
1342
- ops -> hash_final ((unsigned char * )digest , context );
1343
- memcpy ( & key [i * block_size ], digest , block_size );
1344
- }
1337
+ for (i = 0 ; i < times ; i ++ ) {
1338
+ ops -> hash_init (context , NULL );
1345
1339
1346
- RETVAL_STRINGL (key , bytes );
1347
- ZEND_SECURE_ZERO (key , bytes );
1348
- efree (digest );
1349
- efree (context );
1350
- efree (key );
1340
+ for (j = 0 ;j < i ;j ++ ) {
1341
+ ops -> hash_update (context , & null , 1 );
1351
1342
}
1343
+ ops -> hash_update (context , (unsigned char * )padded_salt , salt_len );
1344
+ ops -> hash_update (context , (unsigned char * )password , password_len );
1345
+ ops -> hash_final ((unsigned char * )digest , context );
1346
+ memcpy ( & key [i * block_size ], digest , block_size );
1352
1347
}
1348
+
1349
+ RETVAL_STRINGL (key , bytes );
1350
+ ZEND_SECURE_ZERO (key , bytes );
1351
+ efree (digest );
1352
+ efree (context );
1353
+ efree (key );
1353
1354
}
1354
1355
}
1355
1356
/* }}} */
0 commit comments