Skip to content

Commit

Permalink
Rename 'device key' to 'signed prekey'.
Browse files Browse the repository at this point in the history
  • Loading branch information
moxie0 committed Oct 20, 2014
1 parent 07fd17c commit 0d532af
Show file tree
Hide file tree
Showing 26 changed files with 579 additions and 580 deletions.
8 changes: 4 additions & 4 deletions libaxolotl/protobuf/LocalStorageProtocol.proto
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ message SessionStructure {
}

message PendingPreKey {
optional uint32 preKeyId = 1;
optional int32 deviceKeyId = 3;
optional bytes baseKey = 2;
optional uint32 preKeyId = 1;
optional int32 signedPreKeyId = 3;
optional bytes baseKey = 2;
}

optional uint32 sessionVersion = 1;
Expand Down Expand Up @@ -72,7 +72,7 @@ message PreKeyRecordStructure {
optional bytes privateKey = 3;
}

message DeviceKeyRecordStructure {
message SignedPreKeyRecordStructure {
optional uint32 id = 1;
optional bytes publicKey = 2;
optional bytes privateKey = 3;
Expand Down
2 changes: 1 addition & 1 deletion libaxolotl/protobuf/WhisperTextProtocol.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ message WhisperMessage {
message PreKeyWhisperMessage {
optional uint32 registrationId = 5;
optional uint32 preKeyId = 1;
optional uint32 deviceKeyId = 6;
optional uint32 signedPreKeyId = 6;
optional bytes baseKey = 2;
optional bytes identityKey = 3;
optional bytes verification = 7;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.whispersystems.test;

import org.whispersystems.libaxolotl.InvalidKeyIdException;
import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
import org.whispersystems.libaxolotl.state.SignedPreKeyStore;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class InMemorySignedPreKeyStore implements SignedPreKeyStore {

private final Map<Integer, byte[]> store = new HashMap<>();

@Override
public SignedPreKeyRecord loadSignedPreKey(int signedPreKeyId) throws InvalidKeyIdException {
try {
if (!store.containsKey(signedPreKeyId)) {
throw new InvalidKeyIdException("No such signedprekeyrecord!");
}

return new SignedPreKeyRecord(store.get(signedPreKeyId));
} catch (IOException e) {
throw new AssertionError(e);
}
}

@Override
public List<SignedPreKeyRecord> loadSignedPreKeys() {
try {
List<SignedPreKeyRecord> results = new LinkedList<>();

for (byte[] serialized : store.values()) {
results.add(new SignedPreKeyRecord(serialized));
}

return results;
} catch (IOException e) {
throw new AssertionError(e);
}
}

@Override
public void storeSignedPreKey(int signedPreKeyId, SignedPreKeyRecord record) {
store.put(signedPreKeyId, record.serialize());
}

@Override
public boolean containsSignedPreKey(int signedPreKeyId) {
return store.containsKey(signedPreKeyId);
}

@Override
public void removeSignedPreKey(int signedPreKeyId) {
store.remove(signedPreKeyId);
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.whispersystems.libaxolotl.protocol.KeyExchangeMessage;
import org.whispersystems.libaxolotl.protocol.PreKeyWhisperMessage;
import org.whispersystems.libaxolotl.ratchet.RatchetingSession;
import org.whispersystems.libaxolotl.state.DeviceKeyStore;
import org.whispersystems.libaxolotl.state.SignedPreKeyStore;
import org.whispersystems.libaxolotl.state.IdentityKeyStore;
import org.whispersystems.libaxolotl.state.PreKeyBundle;
import org.whispersystems.libaxolotl.state.PreKeyRecord;
Expand Down Expand Up @@ -43,12 +43,12 @@ public class SessionBuilder {

private static final String TAG = SessionBuilder.class.getSimpleName();

private final SessionStore sessionStore;
private final PreKeyStore preKeyStore;
private final DeviceKeyStore deviceKeyStore;
private final IdentityKeyStore identityKeyStore;
private final long recipientId;
private final int deviceId;
private final SessionStore sessionStore;
private final PreKeyStore preKeyStore;
private final SignedPreKeyStore signedPreKeyStore;
private final IdentityKeyStore identityKeyStore;
private final long recipientId;
private final int deviceId;

/**
* Constructs a SessionBuilder.
Expand All @@ -61,16 +61,16 @@ public class SessionBuilder {
*/
public SessionBuilder(SessionStore sessionStore,
PreKeyStore preKeyStore,
DeviceKeyStore deviceKeyStore,
SignedPreKeyStore signedPreKeyStore,
IdentityKeyStore identityKeyStore,
long recipientId, int deviceId)
{
this.sessionStore = sessionStore;
this.preKeyStore = preKeyStore;
this.deviceKeyStore = deviceKeyStore;
this.identityKeyStore = identityKeyStore;
this.recipientId = recipientId;
this.deviceId = deviceId;
this.sessionStore = sessionStore;
this.preKeyStore = preKeyStore;
this.signedPreKeyStore = signedPreKeyStore;
this.identityKeyStore = identityKeyStore;
this.recipientId = recipientId;
this.deviceId = deviceId;
}

/**
Expand Down Expand Up @@ -109,7 +109,7 @@ private void processV3(PreKeyWhisperMessage message)
{
SessionRecord sessionRecord = sessionStore.loadSession(recipientId, deviceId);
int preKeyId = message.getPreKeyId();
int deviceKeyId = message.getDeviceKeyId();
int signedPreKeyId = message.getSignedPreKeyId();
ECPublicKey theirBaseKey = message.getBaseKey();
ECPublicKey theirEphemeralKey = message.getWhisperMessage().getSenderEphemeral();
IdentityKey theirIdentityKey = message.getIdentityKey();
Expand All @@ -122,10 +122,10 @@ private void processV3(PreKeyWhisperMessage message)
if (preKeyId >=0 && !preKeyStore.containsPreKey(preKeyId))
throw new InvalidKeyIdException("No such prekey: " + preKeyId);

if (!deviceKeyStore.containsDeviceKey(deviceKeyId))
throw new InvalidKeyIdException("No such device key: " + deviceKeyId);
if (!signedPreKeyStore.containsSignedPreKey(signedPreKeyId))
throw new InvalidKeyIdException("No such device key: " + signedPreKeyId);

ECKeyPair ourBaseKey = deviceKeyStore.loadDeviceKey(deviceKeyId).getKeyPair();
ECKeyPair ourBaseKey = signedPreKeyStore.loadSignedPreKey(signedPreKeyId).getKeyPair();
ECKeyPair ourEphemeralKey = ourBaseKey;
ECKeyPair ourPreKey = preKeyId < 0 ? ourBaseKey : preKeyStore.loadPreKey(preKeyId).getKeyPair();
ECPublicKey theirPreKey = theirBaseKey;
Expand Down Expand Up @@ -222,10 +222,10 @@ public void process(PreKeyBundle preKey) throws InvalidKeyException, UntrustedId
throw new UntrustedIdentityException();
}

if (preKey.getDeviceKey() != null &&
if (preKey.getSignedPreKey() != null &&
!Curve.verifySignature(preKey.getIdentityKey().getPublicKey(),
preKey.getDeviceKey().serialize(),
preKey.getDeviceKeySignature()))
preKey.getSignedPreKey().serialize(),
preKey.getSignedPreKeySignature()))
{
throw new InvalidKeyException("Invalid signature on device key!");
}
Expand All @@ -238,19 +238,19 @@ public void process(PreKeyBundle preKey) throws InvalidKeyException, UntrustedId

IdentityKey theirIdentityKey = preKey.getIdentityKey();
ECPublicKey theirPreKey = preKey.getPreKey();
ECPublicKey theirBaseKey = preKey.getDeviceKey() == null ? preKey.getPreKey() : preKey.getDeviceKey();
ECPublicKey theirBaseKey = preKey.getSignedPreKey() == null ? preKey.getPreKey() : preKey.getSignedPreKey();
ECPublicKey theirEphemeralKey = theirBaseKey;

if (sessionRecord.getSessionState().getNeedsRefresh()) sessionRecord.archiveCurrentState();
else sessionRecord.reset();

RatchetingSession.initializeSession(sessionRecord.getSessionState(),
preKey.getDeviceKey() == null ? 2 : 3,
preKey.getSignedPreKey() == null ? 2 : 3,
ourBaseKey, theirBaseKey, ourEphemeralKey,
theirEphemeralKey, ourPreKey, theirPreKey,
ourIdentityKey, theirIdentityKey);

sessionRecord.getSessionState().setPendingPreKey(preKey.getPreKeyId(), preKey.getDeviceKeyId(), ourBaseKey.getPublicKey());
sessionRecord.getSessionState().setPendingPreKey(preKey.getPreKeyId(), preKey.getSignedPreKeyId(), ourBaseKey.getPublicKey());
sessionRecord.getSessionState().setLocalRegistrationId(identityKeyStore.getLocalRegistrationId());
sessionRecord.getSessionState().setRemoteRegistrationId(preKey.getRegistrationId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ public CiphertextMessage encrypt(byte[] paddedMessage) {
previousCounter, ciphertextBody);

if (sessionState.hasPendingPreKey()) {
int pendingPreKeyId = sessionState.getPendingPreKeyId();
int pendingDeviceKeyId = sessionState.getPendingDeviceKeyId();
ECPublicKey pendingBaseKey = sessionState.getPendingBaseKey();
int localRegistrationId = sessionState.getLocalRegistrationId();
int pendingPreKeyId = sessionState.getPendingPreKeyId();
int pendingSignedPreKeyId = sessionState.getPendingSignedPreKeyId();
ECPublicKey pendingBaseKey = sessionState.getPendingBaseKey();
int localRegistrationId = sessionState.getLocalRegistrationId();

ciphertextMessage = new PreKeyWhisperMessage(sessionVersion,
localRegistrationId, pendingPreKeyId,
pendingDeviceKeyId, pendingBaseKey,
pendingSignedPreKeyId, pendingBaseKey,
sessionState.getLocalIdentityKey(),
sessionState.getVerification(),
(WhisperMessage) ciphertextMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class PreKeyWhisperMessage implements CiphertextMessage {
private final int version;
private final int registrationId;
private final int preKeyId;
private final int deviceKeyId;
private final int signedPreKeyId;
private final ECPublicKey baseKey;
private final IdentityKey identityKey;
private final byte[] verification;
Expand All @@ -55,11 +55,11 @@ public PreKeyWhisperMessage(byte[] serialized)
= WhisperProtos.PreKeyWhisperMessage.parseFrom(ByteString.copyFrom(serialized, 1,
serialized.length-1));

if ((version == 2 && !preKeyWhisperMessage.hasPreKeyId()) ||
(version == 3 && !preKeyWhisperMessage.hasDeviceKeyId()) ||
(version == 3 && !preKeyWhisperMessage.hasVerification()) ||
!preKeyWhisperMessage.hasBaseKey() ||
!preKeyWhisperMessage.hasIdentityKey() ||
if ((version == 2 && !preKeyWhisperMessage.hasPreKeyId()) ||
(version == 3 && !preKeyWhisperMessage.hasSignedPreKeyId()) ||
(version == 3 && !preKeyWhisperMessage.hasVerification()) ||
!preKeyWhisperMessage.hasBaseKey() ||
!preKeyWhisperMessage.hasIdentityKey() ||
!preKeyWhisperMessage.hasMessage())
{
throw new InvalidMessageException("Incomplete message.");
Expand All @@ -68,7 +68,7 @@ public PreKeyWhisperMessage(byte[] serialized)
this.serialized = serialized;
this.registrationId = preKeyWhisperMessage.getRegistrationId();
this.preKeyId = preKeyWhisperMessage.hasPreKeyId() ? preKeyWhisperMessage.getPreKeyId() : -1;
this.deviceKeyId = preKeyWhisperMessage.hasDeviceKeyId() ? preKeyWhisperMessage.getDeviceKeyId() : -1;
this.signedPreKeyId = preKeyWhisperMessage.hasSignedPreKeyId() ? preKeyWhisperMessage.getSignedPreKeyId() : -1;
this.baseKey = Curve.decodePoint(preKeyWhisperMessage.getBaseKey().toByteArray(), 0);
this.identityKey = new IdentityKey(Curve.decodePoint(preKeyWhisperMessage.getIdentityKey().toByteArray(), 0));
this.verification = preKeyWhisperMessage.getVerification().toByteArray();
Expand All @@ -78,14 +78,14 @@ public PreKeyWhisperMessage(byte[] serialized)
}
}

public PreKeyWhisperMessage(int messageVersion, int registrationId, int preKeyId, int deviceKeyId,
public PreKeyWhisperMessage(int messageVersion, int registrationId, int preKeyId, int signedPreKeyId,
ECPublicKey baseKey, IdentityKey identityKey, byte[] verification,
WhisperMessage message)
{
this.version = messageVersion;
this.registrationId = registrationId;
this.preKeyId = preKeyId;
this.deviceKeyId = deviceKeyId;
this.signedPreKeyId = signedPreKeyId;
this.baseKey = baseKey;
this.identityKey = identityKey;
this.verification = verification;
Expand All @@ -94,7 +94,7 @@ public PreKeyWhisperMessage(int messageVersion, int registrationId, int preKeyId
byte[] versionBytes = {ByteUtil.intsToByteHighAndLow(this.version, CURRENT_VERSION)};
byte[] messageBytes = WhisperProtos.PreKeyWhisperMessage.newBuilder()
.setPreKeyId(preKeyId)
.setDeviceKeyId(deviceKeyId)
.setSignedPreKeyId(signedPreKeyId)
.setBaseKey(ByteString.copyFrom(baseKey.serialize()))
.setIdentityKey(ByteString.copyFrom(identityKey.serialize()))
.setVerification(ByteString.copyFrom(verification))
Expand All @@ -121,8 +121,8 @@ public int getPreKeyId() {
return preKeyId;
}

public int getDeviceKeyId() {
return deviceKeyId;
public int getSignedPreKeyId() {
return signedPreKeyId;
}

public ECPublicKey getBaseKey() {
Expand Down

0 comments on commit 0d532af

Please sign in to comment.