Index: includes/password.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/password.inc,v
retrieving revision 1.8
diff -u -p -r1.8 password.inc
--- includes/password.inc	4 May 2010 15:47:03 -0000	1.8
+++ includes/password.inc	8 Dec 2010 22:00:20 -0000
@@ -99,18 +99,38 @@ function _password_base64_encode($input,
  */
 function _password_generate_salt($count_log2) {
   $output = '$S$';
-  // Minimum log2 iterations is DRUPAL_MIN_HASH_COUNT.
-  $count_log2 = max($count_log2, DRUPAL_MIN_HASH_COUNT);
-  // Maximum log2 iterations is DRUPAL_MAX_HASH_COUNT.
+  // Ensure that $count_log2 is within set bounds.
+  $count_log2 = _password_enforce_log2_boundaries($count_log2);
   // We encode the final log2 iteration count in base 64.
   $itoa64 = _password_itoa64();
-  $output .= $itoa64[min($count_log2, DRUPAL_MAX_HASH_COUNT)];
+  $output .= $itoa64[$count_log2];
   // 6 bytes is the standard salt for a portable phpass hash.
   $output .= _password_base64_encode(drupal_random_bytes(6), 6);
   return $output;
 }
 
 /**
+ * Ensures that $count_log2 is within set bounds.
+ *
+ * @param $count_log2
+ *   Integer that determines the number of iterations used in the hashing
+ *   process. A larger value is more secure, but takes more time to complete.
+ *
+ * @return
+ *   Integer within set bounds that is closest to $count_log2.
+ */
+function _password_enforce_log2_boundaries($count_log2) {
+  if ($count_log2 < DRUPAL_MIN_HASH_COUNT) {
+    return DRUPAL_MIN_HASH_COUNT;
+  }
+  elseif ($count_log2 > DRUPAL_MAX_HASH_COUNT) {
+    return DRUPAL_MAX_HASH_COUNT;
+  }
+
+  return (int) $count_log2;
+}
+
+/**
  * Hash a password using a secure stretched hash.
  *
  * By using a salt and repeated hashing the password is "stretched". Its
@@ -261,7 +281,9 @@ function user_needs_new_hash($account) {
   if ((substr($account->pass, 0, 3) != '$S$') || (strlen($account->pass) != DRUPAL_HASH_LENGTH)) {
     return TRUE;
   }
+  // Ensure that $count_log2 is within set bounds.
+  $count_log2 = _password_enforce_log2_boundaries(variable_get('password_count_log2', DRUPAL_HASH_COUNT));
   // Check whether the iteration count used differs from the standard number.
-  return (_password_get_count_log2($account->pass) != variable_get('password_count_log2', DRUPAL_HASH_COUNT));
+  return (_password_get_count_log2($account->pass) !== $count_log2);
 }
 
Index: modules/simpletest/simpletest.info
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/simpletest.info,v
retrieving revision 1.26
diff -u -p -r1.26 simpletest.info
--- modules/simpletest/simpletest.info	22 Oct 2010 16:36:14 -0000	1.26
+++ modules/simpletest/simpletest.info	8 Dec 2010 22:00:20 -0000
@@ -31,6 +31,7 @@ files[] = tests/lock.test
 files[] = tests/mail.test
 files[] = tests/menu.test
 files[] = tests/module.test
+files[] = tests/password.test
 files[] = tests/path.test
 files[] = tests/registry.test
 files[] = tests/schema.test
Index: modules/simpletest/tests/password.test
===================================================================
RCS file: modules/simpletest/tests/password.test
diff -N modules/simpletest/tests/password.test
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ modules/simpletest/tests/password.test	8 Dec 2010 22:00:20 -0000
@@ -0,0 +1,61 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Provides unit tests for password.inc.
+ */
+
+/**
+ * Unit tests for password hashing API.
+ */
+class PasswordHashingTest extends DrupalWebTestCase {
+  protected $profile = 'testing';
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Password hashing',
+      'description' => 'Password hashing unit tests.',
+      'group' => 'System',
+    );
+  }
+
+  function setUp() {
+    require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
+    parent::setUp();
+  }
+
+  /**
+   * Test password hashing.
+   */
+  function testPasswordHashing() {
+    // Set a log2 iteration count that is deliberately out of bounds to test
+    // that it is corrected to be within bounds.
+    variable_set('password_count_log2', 1);
+    // Set up a fake $account with a password 'baz', hashed with md5.
+    $password = 'baz';
+    $account = (object) array('name' => 'foo', 'pass' => md5($password));
+    // The md5 password should be flagged as needing an update.
+    $this->assertTrue(user_needs_new_hash($account), t('User with md5 password needs a new hash.'));
+    // Re-hash the password.
+    $old_hash = $account->pass;
+    $account->pass = user_hash_password($password);
+    $this->assertIdentical(_password_get_count_log2($account->pass), DRUPAL_MIN_HASH_COUNT, t('Re-hashed password has the minimum number of log2 iterations.'));
+    $this->assertTrue($account->pass != $old_hash, t('Password hash changed.'));
+    $this->assertTrue(user_check_password($password, $account), t('Password check succeeds.'));
+    // Since the log2 setting hasn't changed and the user has a valid password,
+    // user_needs_new_hash() should return FALSE.
+    $this->assertFalse(user_needs_new_hash($account), t('User does not need a new hash.'));
+    // Increment the log2 iteration to MIN + 1.
+    variable_set('password_count_log2', DRUPAL_MIN_HASH_COUNT + 1);
+    $this->assertTrue(user_needs_new_hash($account), t('User needs a new hash after incrementing the log2 count.'));
+    // Re-hash the password.
+    $old_hash = $account->pass;
+    $account->pass = user_hash_password($password);
+    $this->assertIdentical(_password_get_count_log2($account->pass), DRUPAL_MIN_HASH_COUNT + 1, t('Re-hashed password has the correct number of log2 iterations.'));
+    $this->assertTrue($account->pass != $old_hash, t('Password hash changed again.'));
+    // Now the hash should be OK.
+    $this->assertFalse(user_needs_new_hash($account), t('Re-hashed password does not need a new hash.'));
+    $this->assertTrue(user_check_password($password, $account), t('Password check succeeds with re-hashed password.'));
+  }
+}
Index: modules/user/user.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/user/user.module,v
retrieving revision 1.1224
diff -u -p -r1.1224 user.module
--- modules/user/user.module	30 Nov 2010 23:55:11 -0000	1.1224
+++ modules/user/user.module	8 Dec 2010 22:00:20 -0000
@@ -2154,7 +2154,7 @@ function user_login_final_validate($form
 function user_authenticate($name, $password) {
   $uid = FALSE;
   if (!empty($name) && !empty($password)) {
-    $account = db_query("SELECT * FROM {users} WHERE name = :name AND status = 1", array(':name' => $name))->fetchObject();
+    $account = user_load_by_name($name);
     if ($account) {
       // Allow alternate password hashing schemes.
       require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
@@ -2166,10 +2166,7 @@ function user_authenticate($name, $passw
         if (user_needs_new_hash($account)) {
           $new_hash = user_hash_password($password);
           if ($new_hash) {
-            db_update('users')
-              ->fields(array('pass' => $new_hash))
-              ->condition('uid', $account->uid)
-              ->execute();
+            user_save($account, array('pass' => $new_hash));
           }
         }
       }
Index: modules/user/user.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/user/user.test,v
retrieving revision 1.108
diff -u -p -r1.108 user.test
--- modules/user/user.test	30 Nov 2010 23:55:11 -0000	1.108
+++ modules/user/user.test	8 Dec 2010 22:00:21 -0000
@@ -367,6 +367,31 @@ class UserLoginTestCase extends DrupalWe
   }
 
   /**
+   * Test that user password is re-hashed upon login after changing $count_log2.
+   */
+  function testPasswordRehashOnLogin() {
+    // Load password hashing API.
+    require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
+    // Set initial $count_log2 to the default, DRUPAL_HASH_COUNT.
+    variable_set('password_count_log2', DRUPAL_HASH_COUNT);
+    // Create a new user and authenticate.
+    $account = $this->drupalCreateUser(array());
+    $password = $account->pass_raw;
+    $this->drupalLogin($account);
+    $this->drupalLogout();
+    // Load the stored user. The password hash should reflect $count_log2.
+    $account = user_load($account->uid);
+    $this->assertIdentical(_password_get_count_log2($account->pass), DRUPAL_HASH_COUNT);
+    // Change $count_log2 and log in again.
+    variable_set('password_count_log2', DRUPAL_HASH_COUNT + 1);
+    $account->pass_raw = $password;
+    $this->drupalLogin($account);
+    // Load the stored user, which should have a different password hash now.
+    $account = user_load($account->uid, TRUE);
+    $this->assertIdentical(_password_get_count_log2($account->pass), DRUPAL_HASH_COUNT + 1);
+  }
+
+  /**
    * Make an unsuccessful login attempt.
    *
    * @param $account
