mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-23 10:05:15 +00:00
clean device consistency
This commit is contained in:
parent
075e265a46
commit
a11173676f
@ -1,54 +0,0 @@
|
||||
package org.session.libsignal.libsignal.devices;
|
||||
|
||||
import org.session.libsignal.libsignal.util.ByteArrayComparator;
|
||||
import org.session.libsignal.libsignal.util.ByteUtil;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class DeviceConsistencyCodeGenerator {
|
||||
|
||||
private static final int CODE_VERSION = 0;
|
||||
|
||||
public static String generateFor(DeviceConsistencyCommitment commitment,
|
||||
List<DeviceConsistencySignature> signatures)
|
||||
{
|
||||
try {
|
||||
ArrayList<DeviceConsistencySignature> sortedSignatures = new ArrayList<DeviceConsistencySignature>(signatures);
|
||||
Collections.sort(sortedSignatures, new SignatureComparator());
|
||||
|
||||
MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
|
||||
messageDigest.update(ByteUtil.shortToByteArray(CODE_VERSION));
|
||||
messageDigest.update(commitment.toByteArray());
|
||||
|
||||
for (DeviceConsistencySignature signature : sortedSignatures) {
|
||||
messageDigest.update(signature.getVrfOutput());
|
||||
}
|
||||
|
||||
byte[] hash = messageDigest.digest();
|
||||
|
||||
String digits = getEncodedChunk(hash, 0) + getEncodedChunk(hash, 5);
|
||||
return digits.substring(0, 6);
|
||||
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String getEncodedChunk(byte[] hash, int offset) {
|
||||
long chunk = ByteUtil.byteArray5ToLong(hash, offset) % 100000;
|
||||
return String.format("%05d", chunk);
|
||||
}
|
||||
|
||||
|
||||
private static class SignatureComparator extends ByteArrayComparator implements Comparator<DeviceConsistencySignature> {
|
||||
@Override
|
||||
public int compare(DeviceConsistencySignature first, DeviceConsistencySignature second) {
|
||||
return compare(first.getVrfOutput(), second.getVrfOutput());
|
||||
}
|
||||
}
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
package org.session.libsignal.libsignal.devices;
|
||||
|
||||
import org.session.libsignal.libsignal.IdentityKey;
|
||||
import org.session.libsignal.libsignal.util.ByteUtil;
|
||||
import org.session.libsignal.libsignal.util.IdentityKeyComparator;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class DeviceConsistencyCommitment {
|
||||
|
||||
private static final String VERSION = "DeviceConsistencyCommitment_V0";
|
||||
|
||||
private final int generation;
|
||||
private final byte[] serialized;
|
||||
|
||||
public DeviceConsistencyCommitment(int generation, List<IdentityKey> identityKeys) {
|
||||
try {
|
||||
ArrayList<IdentityKey> sortedIdentityKeys = new ArrayList<IdentityKey>(identityKeys);
|
||||
Collections.sort(sortedIdentityKeys, new IdentityKeyComparator());
|
||||
|
||||
MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
|
||||
messageDigest.update(VERSION.getBytes());
|
||||
messageDigest.update(ByteUtil.intToByteArray(generation));
|
||||
|
||||
for (IdentityKey commitment : sortedIdentityKeys) {
|
||||
messageDigest.update(commitment.getPublicKey().serialize());
|
||||
}
|
||||
|
||||
this.generation = generation;
|
||||
this.serialized = messageDigest.digest();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] toByteArray() {
|
||||
return serialized;
|
||||
}
|
||||
|
||||
public int getGeneration() {
|
||||
return generation;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package org.session.libsignal.libsignal.devices;
|
||||
|
||||
public class DeviceConsistencySignature {
|
||||
|
||||
private final byte[] signature;
|
||||
private final byte[] vrfOutput;
|
||||
|
||||
public DeviceConsistencySignature(byte[] signature, byte[] vrfOutput) {
|
||||
this.signature = signature;
|
||||
this.vrfOutput = vrfOutput;
|
||||
}
|
||||
|
||||
public byte[] getVrfOutput() {
|
||||
return vrfOutput;
|
||||
}
|
||||
|
||||
public byte[] getSignature() {
|
||||
return signature;
|
||||
}
|
||||
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
package org.session.libsignal.libsignal.protocol;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
|
||||
import org.whispersystems.curve25519.VrfSignatureVerificationFailedException;
|
||||
import org.session.libsignal.libsignal.IdentityKey;
|
||||
import org.session.libsignal.libsignal.IdentityKeyPair;
|
||||
import org.session.libsignal.libsignal.InvalidKeyException;
|
||||
import org.session.libsignal.libsignal.InvalidMessageException;
|
||||
import org.session.libsignal.libsignal.devices.DeviceConsistencyCommitment;
|
||||
import org.session.libsignal.libsignal.devices.DeviceConsistencySignature;
|
||||
import org.session.libsignal.libsignal.ecc.Curve;
|
||||
|
||||
public class DeviceConsistencyMessage {
|
||||
|
||||
private final DeviceConsistencySignature signature;
|
||||
private final int generation;
|
||||
private final byte[] serialized;
|
||||
|
||||
public DeviceConsistencyMessage(DeviceConsistencyCommitment commitment, IdentityKeyPair identityKeyPair) {
|
||||
try {
|
||||
byte[] signatureBytes = Curve.calculateVrfSignature(identityKeyPair.getPrivateKey(), commitment.toByteArray());
|
||||
byte[] vrfOutputBytes = Curve.verifyVrfSignature(identityKeyPair.getPublicKey().getPublicKey(), commitment.toByteArray(), signatureBytes);
|
||||
|
||||
this.generation = commitment.getGeneration();
|
||||
this.signature = new DeviceConsistencySignature(signatureBytes, vrfOutputBytes);
|
||||
this.serialized = SignalProtos.DeviceConsistencyCodeMessage.newBuilder()
|
||||
.setGeneration(commitment.getGeneration())
|
||||
.setSignature(ByteString.copyFrom(signature.getSignature()))
|
||||
.build()
|
||||
.toByteArray();
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new AssertionError(e);
|
||||
} catch (VrfSignatureVerificationFailedException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
public DeviceConsistencyMessage(DeviceConsistencyCommitment commitment, byte[] serialized, IdentityKey identityKey) throws InvalidMessageException {
|
||||
try {
|
||||
SignalProtos.DeviceConsistencyCodeMessage message = SignalProtos.DeviceConsistencyCodeMessage.parseFrom(serialized);
|
||||
byte[] vrfOutputBytes = Curve.verifyVrfSignature(identityKey.getPublicKey(), commitment.toByteArray(), message.getSignature().toByteArray());
|
||||
|
||||
this.generation = message.getGeneration();
|
||||
this.signature = new DeviceConsistencySignature(message.getSignature().toByteArray(), vrfOutputBytes);
|
||||
this.serialized = serialized;
|
||||
} catch (InvalidProtocolBufferException e) {
|
||||
throw new InvalidMessageException(e);
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new InvalidMessageException(e);
|
||||
} catch (VrfSignatureVerificationFailedException e) {
|
||||
throw new InvalidMessageException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] getSerialized() {
|
||||
return serialized;
|
||||
}
|
||||
|
||||
public DeviceConsistencySignature getSignature() {
|
||||
return signature;
|
||||
}
|
||||
|
||||
public int getGeneration() {
|
||||
return generation;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user