-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace pinstretcher with Argon2 and new PIN encryption.
- Loading branch information
1 parent
f7a3bb2
commit e37c4b1
Showing
32 changed files
with
634 additions
and
577 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
app/src/androidTest/java/org/thoughtcrime/securesms/TextSecureTestCase.java
This file was deleted.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
app/src/androidTest/java/org/thoughtcrime/securesms/lock/PinHashing_hashPin_Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.thoughtcrime.securesms.lock; | ||
|
||
import org.junit.Test; | ||
import org.thoughtcrime.securesms.util.Hex; | ||
import org.whispersystems.signalservice.api.kbs.HashedPin; | ||
import org.whispersystems.signalservice.api.kbs.KbsData; | ||
import org.whispersystems.signalservice.api.kbs.MasterKey; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public final class PinHashing_hashPin_Test { | ||
|
||
@Test | ||
public void argon2_hashed_pin_password() throws IOException { | ||
byte[] backupId = Hex.fromStringCondensed("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"); | ||
MasterKey masterKey = new MasterKey(Hex.fromStringCondensed("202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f")); | ||
|
||
HashedPin hashedPin = PinHashing.hashPin("password", () -> backupId); | ||
KbsData kbsData = hashedPin.createNewKbsData(masterKey); | ||
|
||
assertArrayEquals(Hex.fromStringCondensed("ab7e8499d21f80a6600b3b9ee349ac6d72c07e3359fe885a934ba7aa844429f8"), hashedPin.getKbsAccessKey()); | ||
assertArrayEquals(Hex.fromStringCondensed("ab7e8499d21f80a6600b3b9ee349ac6d72c07e3359fe885a934ba7aa844429f8"), kbsData.getKbsAccessKey()); | ||
assertArrayEquals(Hex.fromStringCondensed("3f33ce58eb25b40436592a30eae2a8fabab1899095f4e2fba6e2d0dc43b4a2d9cac5a3931748522393951e0e54dec769"), kbsData.getCipherText()); | ||
assertEquals(masterKey, kbsData.getMasterKey()); | ||
} | ||
|
||
@Test | ||
public void argon2_hashed_pin_another_password() throws IOException { | ||
byte[] backupId = Hex.fromStringCondensed("202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"); | ||
MasterKey masterKey = new MasterKey(Hex.fromStringCondensed("88a787415a2ecd79da0d1016a82a27c5c695c9a19b88b0aa1d35683280aa9a67")); | ||
|
||
HashedPin hashedPin = PinHashing.hashPin("anotherpassword ", () -> backupId); | ||
KbsData kbsData = hashedPin.createNewKbsData(masterKey); | ||
|
||
assertArrayEquals(Hex.fromStringCondensed("301d9dd1e96f20ce51083f67d3298fd37b97525de8324d5e12ed2d407d3d927b"), hashedPin.getKbsAccessKey()); | ||
assertArrayEquals(Hex.fromStringCondensed("301d9dd1e96f20ce51083f67d3298fd37b97525de8324d5e12ed2d407d3d927b"), kbsData.getKbsAccessKey()); | ||
assertArrayEquals(Hex.fromStringCondensed("9d9b05402ea39c17ff1c9298c8a0e86784a352aa02a74943bf8bcf07ec0f4b574a5b786ad0182c8d308d9eb06538b8c9"), kbsData.getCipherText()); | ||
assertEquals(masterKey, kbsData.getMasterKey()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 69 additions & 42 deletions
111
app/src/main/java/org/thoughtcrime/securesms/keyvalue/KbsValues.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
app/src/main/java/org/thoughtcrime/securesms/lock/PinHashing.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.thoughtcrime.securesms.lock; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import org.signal.argon2.Argon2; | ||
import org.signal.argon2.Argon2Exception; | ||
import org.signal.argon2.MemoryCost; | ||
import org.signal.argon2.Type; | ||
import org.signal.argon2.Version; | ||
import org.thoughtcrime.securesms.util.Util; | ||
import org.whispersystems.signalservice.api.KeyBackupService; | ||
import org.whispersystems.signalservice.api.kbs.HashedPin; | ||
import org.whispersystems.signalservice.internal.registrationpin.PinHasher; | ||
|
||
public final class PinHashing { | ||
|
||
private static final Type KBS_PIN_ARGON_TYPE = Type.Argon2id; | ||
private static final Type LOCAL_PIN_ARGON_TYPE = Type.Argon2i; | ||
|
||
private PinHashing() { | ||
} | ||
|
||
public static HashedPin hashPin(@NonNull String pin, @NonNull KeyBackupService.HashSession hashSession) { | ||
return PinHasher.hashPin(PinHasher.normalize(pin), password -> { | ||
try { | ||
return new Argon2.Builder(Version.V13) | ||
.type(KBS_PIN_ARGON_TYPE) | ||
.memoryCost(MemoryCost.MiB(16)) | ||
.parallelism(1) | ||
.iterations(32) | ||
.hashLength(64) | ||
.build() | ||
.hash(password, hashSession.hashSalt()) | ||
.getHash(); | ||
} catch (Argon2Exception e) { | ||
throw new AssertionError(e); | ||
} | ||
}); | ||
} | ||
|
||
public static String localPinHash(@NonNull String pin) { | ||
byte[] normalized = PinHasher.normalize(pin); | ||
try { | ||
return new Argon2.Builder(Version.V13) | ||
.type(LOCAL_PIN_ARGON_TYPE) | ||
.memoryCost(MemoryCost.KiB(256)) | ||
.parallelism(1) | ||
.iterations(50) | ||
.hashLength(32) | ||
.build() | ||
.hash(normalized, Util.getSecretBytes(16)) | ||
.getEncoded(); | ||
} catch (Argon2Exception e) { | ||
throw new AssertionError(e); | ||
} | ||
} | ||
|
||
public static boolean verifyLocalPinHash(@NonNull String localPinHash, @NonNull String pin) { | ||
byte[] normalized = PinHasher.normalize(pin); | ||
return Argon2.verify(localPinHash, normalized, LOCAL_PIN_ARGON_TYPE); | ||
} | ||
} |
Oops, something went wrong.