Hmac-SIV encryption/decryption.

This commit is contained in:
Alan Evans
2020-01-17 13:31:30 -05:00
committed by Greyson Parrelli
parent 3907ec8b51
commit 7d70ea78cd
18 changed files with 462 additions and 28 deletions

View File

@@ -0,0 +1,58 @@
package org.thoughtcrime.securesms.registration.v2;
import org.junit.Test;
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.internal.util.JsonUtil;
import java.io.IOException;
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.thoughtcrime.securesms.testutil.SecureRandomTestUtil.mockRandom;
public final class HashedPinKbsDataTest {
@Test
public void vectors_createNewKbsData() throws IOException {
for (KbsTestVector vector : getKbsTestVectorList().getVectors()) {
HashedPin hashedPin = HashedPin.fromArgon2Hash(vector.getArgon2Hash());
KbsData kbsData = hashedPin.createNewKbsData(mockRandom(vector.getMasterKey()));
assertArrayEquals(vector.getMasterKey(), kbsData.getMasterKey().serialize());
assertArrayEquals(vector.getIvAndCipher(), kbsData.getCipherText());
assertArrayEquals(vector.getKbsAccessKey(), kbsData.getKbsAccessKey());
assertEquals(vector.getRegistrationLock(), kbsData.getMasterKey().deriveRegistrationLock());
}
}
@Test
public void vectors_decryptKbsDataIVCipherText() throws IOException, InvalidCiphertextException {
for (KbsTestVector vector : getKbsTestVectorList().getVectors()) {
HashedPin hashedPin = HashedPin.fromArgon2Hash(vector.getArgon2Hash());
KbsData kbsData = hashedPin.decryptKbsDataIVCipherText(vector.getIvAndCipher());
assertArrayEquals(vector.getMasterKey(), kbsData.getMasterKey().serialize());
assertArrayEquals(vector.getIvAndCipher(), kbsData.getCipherText());
assertArrayEquals(vector.getKbsAccessKey(), kbsData.getKbsAccessKey());
assertEquals(vector.getRegistrationLock(), kbsData.getMasterKey().deriveRegistrationLock());
}
}
private static KbsTestVectorList getKbsTestVectorList() throws IOException {
try (InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("data/kbs_vectors.json")) {
KbsTestVectorList data = JsonUtil.fromJson(Util.readFullyAsString(resourceAsStream), KbsTestVectorList.class);
assertFalse(data.getVectors().isEmpty());
return data;
}
}
}

View File

@@ -0,0 +1,63 @@
package org.thoughtcrime.securesms.registration.v2;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.thoughtcrime.securesms.testutil.HexDeserializer;
public final class KbsTestVector {
@JsonProperty("backup_id")
@JsonDeserialize(using = HexDeserializer.class)
private byte[] backupId;
@JsonProperty("argon2_hash")
@JsonDeserialize(using = HexDeserializer.class)
private byte[] argon2Hash;
@JsonProperty("pin")
private String pin;
@JsonProperty("registration_lock")
private String registrationLock;
@JsonProperty("master_key")
@JsonDeserialize(using = HexDeserializer.class)
private byte[] masterKey;
@JsonProperty("kbs_access_key")
@JsonDeserialize(using = HexDeserializer.class)
private byte[] kbsAccessKey;
@JsonProperty("iv_and_cipher")
@JsonDeserialize(using = HexDeserializer.class)
private byte[] ivAndCipher;
public byte[] getBackupId() {
return backupId;
}
public byte[] getArgon2Hash() {
return argon2Hash;
}
public String getPin() {
return pin;
}
public String getRegistrationLock() {
return registrationLock;
}
public byte[] getMasterKey() {
return masterKey;
}
public byte[] getKbsAccessKey() {
return kbsAccessKey;
}
public byte[] getIvAndCipher() {
return ivAndCipher;
}
}

View File

@@ -0,0 +1,15 @@
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;
}
}