]> granicus.if.org Git - php/commitdiff
Implement php.ini setting password.bcrypt_cost
authorAnthony Ferrara <ircmaxell@ircmaxell.com>
Wed, 27 Jun 2012 01:15:56 +0000 (21:15 -0400)
committerAnthony Ferrara <ircmaxell@ircmaxell.com>
Wed, 27 Jun 2012 01:15:56 +0000 (21:15 -0400)
ext/standard/basic_functions.c
ext/standard/password.c
ext/standard/php_password.h
main/main.c
php.ini-development
php.ini-production

index 9e35a5e020b924a6250282baddab7e0edbe55ebe..5dc86ab097857a64df6309c748a510bca7c05545 100644 (file)
@@ -3846,6 +3846,7 @@ PHP_MINFO_FUNCTION(basic) /* {{{ */
        php_info_print_table_start();
        BASIC_MINFO_SUBMODULE(dl)
        BASIC_MINFO_SUBMODULE(mail)
+       BASIC_MINFO_SUBMODULE(password)
        php_info_print_table_end();
        BASIC_MINFO_SUBMODULE(assert)
 }
index f049fbcbf1e8c5a086ea219538347f66f627946f..94aa4dc3e3ec3d088d54b94a0d81ad4854a701e1 100644 (file)
@@ -43,6 +43,11 @@ PHP_MINIT_FUNCTION(password) /* {{{ */
 }
 /* }}} */
 
+PHP_MINFO_FUNCTION(password) /* {{{ */
+{
+       php_info_print_table_row(2, "Default Password BCrypt Cost", INI_STR("password.bcrypt_cost"));
+}
+/* }}} */
 
 static int php_password_salt_is_alphabet(const char *str, const int len)
 {
@@ -169,7 +174,11 @@ PHP_FUNCTION(password_verify)
                zval_ptr_dtor(&ret);
                RETURN_FALSE;
        }
-
+       
+       /* We're using this method instead of == in order to provide
+        * resistence towards timing attacks. This is a constant time
+        * equality check that will always check every byte of both
+        * values. */
        for (i = 0; i < Z_STRLEN_P(ret); i++) {
                status |= (Z_STRVAL_P(ret)[i] ^ Z_STRVAL_P(hash)[i]);
        }
@@ -231,16 +240,20 @@ PHP_FUNCTION(password_hash)
         }
 
         if (strcmp(algo, PHP_PASSWORD_BCRYPT) == 0) {
-               int cost = PHP_PASSWORD_BCRYPT_DEFAULT_COST;
+               int cost = 0;
+               cost = (int) INI_INT("password.bcrypt_cost");
+
                if (options && zend_symtable_find(options, "cost", 5, (void **) &option_buffer) == SUCCESS) {
                        convert_to_long_ex(option_buffer);
                        cost = Z_LVAL_PP(option_buffer);
                        zval_ptr_dtor(option_buffer);
-                       if (cost < 4 || cost > 31) {
-                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid bcrypt cost parameter specified: %d", cost);
-                               RETURN_FALSE;
-                       }
                }
+
+               if (cost < 4 || cost > 31) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid bcrypt cost parameter specified: %d", cost);
+                       RETURN_FALSE;
+               }
+               
                 required_salt_len = 22;
                hash_format = emalloc(8);
                sprintf(hash_format, "$2y$%02d$", cost);
index 830d31ce64b2fe3103da291c800023f790fdf5c8..81fe41f529f2676d2cf7b56eaa28c458b4880b17 100644 (file)
@@ -26,13 +26,11 @@ PHP_FUNCTION(password_verify);
 PHP_FUNCTION(password_make_salt);
 
 PHP_MINIT_FUNCTION(password);
+PHP_MINFO_FUNCTION(password);
 
 #define PHP_PASSWORD_DEFAULT   "2y"
 #define PHP_PASSWORD_BCRYPT    "2y"
 
-#define PHP_PASSWORD_BCRYPT_DEFAULT_COST 12;
-
-
 #endif
 
 
index cc04b1317e991913a9bd4f5b2baf44cb2b3c00b0..e52c32c57dce8767dda34d41ff29378a80722aac 100644 (file)
@@ -540,6 +540,8 @@ PHP_INI_BEGIN()
        STD_PHP_INI_ENTRY("error_append_string",        NULL,           PHP_INI_ALL,            OnUpdateString,                 error_append_string,    php_core_globals,       core_globals)
        STD_PHP_INI_ENTRY("error_prepend_string",       NULL,           PHP_INI_ALL,            OnUpdateString,                 error_prepend_string,   php_core_globals,       core_globals)
 
+       PHP_INI_ENTRY("password.bcrypt_cost",                           "11",           PHP_INI_ALL,            NULL)
+
        PHP_INI_ENTRY("SMTP",                                           "localhost",PHP_INI_ALL,                NULL)
        PHP_INI_ENTRY("smtp_port",                                      "25",           PHP_INI_ALL,            NULL)
        STD_PHP_INI_BOOLEAN("mail.add_x_header",                        "0",            PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateBool,                   mail_x_header,                  php_core_globals,       core_globals)
index a5a7a4a81f818ab046f1127df117c673b2f09ab1..5f1205e6a1de44959b086bca10bbafa8d43f2907 100644 (file)
@@ -1359,6 +1359,15 @@ bcmath.scale = 0
 ; http://php.net/browscap
 ;browscap = extra/browscap.ini
 
+[password]
+; The default cost of a bcrypt hash created using password_hash()
+; Note that this is only the default, and can be overriden by the
+; options argument to password_hash(). Additionally, it only affects
+; newly created hashes. A higher value will make the generated
+; hash more resistent to brute forcing, but will also use more CPU
+; Default: 11
+; password.bcrypt_cost = 11
+
 [Session]
 ; Handler used to store/retrieve data.
 ; http://php.net/session.save-handler
index 5d8f26e0fd3b764cb4bca531a153036d9c788bca..927f305cde87b33f16609aec3d204306d24df9af 100644 (file)
@@ -1359,6 +1359,15 @@ bcmath.scale = 0
 ; http://php.net/browscap
 ;browscap = extra/browscap.ini
 
+[password]
+; The default cost of a bcrypt hash created using password_hash()
+; Note that this is only the default, and can be overriden by the
+; options argument to password_hash(). Additionally, it only affects
+; newly created hashes. A higher value will make the generated
+; hash more resistent to brute forcing, but will also use more CPU
+; Default: 11
+; password.bcrypt_cost = 11
+
 [Session]
 ; Handler used to store/retrieve data.
 ; http://php.net/session.save-handler