]> WPIA git - gigi.git/commitdiff
fix: short files in PasswordHashChecker
authorLucas Werkmeister <mail@lucaswerkmeister.de>
Tue, 19 Jun 2018 21:23:34 +0000 (23:23 +0200)
committerLucas Werkmeister <mail@lucaswerkmeister.de>
Tue, 19 Jun 2018 21:25:10 +0000 (23:25 +0200)
For short files (or, presumably, for very rare hashes on all files),
PasswordHashChecker would occasionally attempt to read before the start
or past the end of a file; avoid this with clamping (in two cases where
there is no potentially infinite iteration) or aborting (in the one
other case, where clamping might yield an infinite loop).

Change-Id: Ia1d4f527a2b8589ec43732e0e1a1cf80cb3e2bac

src/club/wpia/gigi/passwords/PasswordHashChecker.java

index 32eda623a8164bcffc3725fa4c99f531c6584308..5c6e3a06ea97ce31486bc92f8e00935d3fd85f9e 100644 (file)
@@ -76,6 +76,7 @@ public class PasswordHashChecker implements PasswordChecker {
     private boolean knownPasswordHash(byte[] passwordHash) throws IOException {
         long targetEstimate = estimateHashOffset(passwordHash);
         long bestGuess = targetEstimate;
+        bestGuess = clampOffset(bestGuess);
 
         hashBuffer.clear();
         database.read(hashBuffer, bestGuess);
@@ -86,6 +87,7 @@ public class PasswordHashChecker implements PasswordChecker {
                 break;
             }
             bestGuess = bestGuess + targetEstimate - bestGuessEstimate;
+            bestGuess = clampOffset(bestGuess);
             hashBuffer.clear();
             database.read(hashBuffer, bestGuess);
         }
@@ -97,6 +99,9 @@ public class PasswordHashChecker implements PasswordChecker {
         int newSearchDirection = searchDirection;
         while (searchDirection == newSearchDirection) {
             bestGuess += digestLength * searchDirection;
+            if (bestGuess < 0 || bestGuess >= database.size()) {
+                break;
+            }
             hashBuffer.clear();
             database.read(hashBuffer, bestGuess);
             newSearchDirection = compareHashes(passwordHash, hashBuffer.array());
@@ -127,4 +132,14 @@ public class PasswordHashChecker implements PasswordChecker {
                 / (1L << 32);
         return (pos / digestLength) * digestLength;
     }
+
+    private long clampOffset(long offset) throws IOException {
+        if (offset < 0) {
+            return 0;
+        }
+        if (offset >= database.size()) {
+            return database.size() - 1;
+        }
+        return offset;
+    }
 }