Replace pinstretcher with Argon2 and new PIN encryption.

This commit is contained in:
Alan Evans
2020-01-22 15:02:06 -05:00
committed by Greyson Parrelli
parent f7a3bb2ae8
commit e37c4b1f87
32 changed files with 635 additions and 578 deletions

View File

@@ -1,10 +1,12 @@
package org.thoughtcrime.securesms.registration.v2;
import org.junit.Test;
import org.thoughtcrime.securesms.registration.v2.testdata.KbsTestVector;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.signalservice.api.crypto.InvalidCiphertextException;
import org.whispersystems.signalservice.api.kbs.HashedPin;
import org.whispersystems.signalservice.api.kbs.KbsData;
import org.whispersystems.signalservice.api.kbs.MasterKey;
import org.whispersystems.signalservice.internal.util.JsonUtil;
import java.io.IOException;
@@ -12,17 +14,17 @@ import java.io.InputStream;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.thoughtcrime.securesms.testutil.SecureRandomTestUtil.mockRandom;
public final class HashedPinKbsDataTest {
@Test
public void vectors_createNewKbsData() throws IOException {
for (KbsTestVector vector : getKbsTestVectorList().getVectors()) {
for (KbsTestVector vector : getKbsTestVectorList()) {
HashedPin hashedPin = HashedPin.fromArgon2Hash(vector.getArgon2Hash());
KbsData kbsData = hashedPin.createNewKbsData(mockRandom(vector.getMasterKey()));
KbsData kbsData = hashedPin.createNewKbsData(MasterKey.createNew(mockRandom(vector.getMasterKey())));
assertArrayEquals(vector.getMasterKey(), kbsData.getMasterKey().serialize());
assertArrayEquals(vector.getIvAndCipher(), kbsData.getCipherText());
@@ -33,7 +35,7 @@ public final class HashedPinKbsDataTest {
@Test
public void vectors_decryptKbsDataIVCipherText() throws IOException, InvalidCiphertextException {
for (KbsTestVector vector : getKbsTestVectorList().getVectors()) {
for (KbsTestVector vector : getKbsTestVectorList()) {
HashedPin hashedPin = HashedPin.fromArgon2Hash(vector.getArgon2Hash());
KbsData kbsData = hashedPin.decryptKbsDataIVCipherText(vector.getIvAndCipher());
@@ -45,12 +47,12 @@ public final class HashedPinKbsDataTest {
}
}
private static KbsTestVectorList getKbsTestVectorList() throws IOException {
private static KbsTestVector[] getKbsTestVectorList() throws IOException {
try (InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("data/kbs_vectors.json")) {
KbsTestVectorList data = JsonUtil.fromJson(Util.readFullyAsString(resourceAsStream), KbsTestVectorList.class);
KbsTestVector[] data = JsonUtil.fromJson(Util.readFullyAsString(resourceAsStream), KbsTestVector[].class);
assertFalse(data.getVectors().isEmpty());
assertTrue(data.length > 0);
return data;
}

View File

@@ -1,15 +0,0 @@
package org.thoughtcrime.securesms.registration.v2;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
public final class KbsTestVectorList {
@JsonProperty("vectors")
private List<KbsTestVector> vectors;
public List<KbsTestVector> getVectors() {
return vectors;
}
}

View File

@@ -0,0 +1,42 @@
package org.thoughtcrime.securesms.registration.v2;
import org.junit.Test;
import org.thoughtcrime.securesms.registration.v2.testdata.PinSanitationVector;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.signalservice.internal.registrationpin.PinHasher;
import org.whispersystems.signalservice.internal.util.Hex;
import org.whispersystems.signalservice.internal.util.JsonUtil;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public final class PinHasher_normalize_Test {
@Test
public void vectors_normalize() throws IOException {
for (PinSanitationVector vector : getKbsPinSanitationTestVectorList()) {
byte[] normalized = PinHasher.normalize(vector.getPin());
if (!Arrays.equals(vector.getBytes(), normalized)) {
assertEquals(String.format("%s [%s]", vector.getName(), vector.getPin()),
Hex.toStringCondensed(vector.getBytes()),
Hex.toStringCondensed(normalized));
}
}
}
private static PinSanitationVector[] getKbsPinSanitationTestVectorList() throws IOException {
try (InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("data/kbs_pin_normalization_vectors.json")) {
PinSanitationVector[] data = JsonUtil.fromJson(Util.readFullyAsString(resourceAsStream), PinSanitationVector[].class);
assertTrue(data.length > 0);
return data;
}
}
}

View File

@@ -1,4 +1,4 @@
package org.thoughtcrime.securesms.registration.v2;
package org.thoughtcrime.securesms.registration.v2.testdata;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

View File

@@ -0,0 +1,31 @@
package org.thoughtcrime.securesms.registration.v2.testdata;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.thoughtcrime.securesms.testutil.HexDeserializer;
public class PinSanitationVector {
@JsonProperty("name")
private String name;
@JsonProperty("pin")
private String pin;
@JsonProperty("bytes")
@JsonDeserialize(using = HexDeserializer.class)
private byte[] bytes;
public String getName() {
return name;
}
public String getPin() {
return pin;
}
public byte[] getBytes() {
return bytes;
}
}