Rename static transport keys to handshake keys.

parent e91a7c64
package org.briarproject.bramble.api.crypto;
import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.transport.StaticTransportKeys;
import org.briarproject.bramble.api.transport.HandshakeKeys;
import org.briarproject.bramble.api.transport.TransportKeys;
/**
......@@ -27,21 +27,19 @@ public interface TransportCrypto {
TransportKeys rotateTransportKeys(TransportKeys k, long timePeriod);
/**
* Derives static transport keys for the given transport in the given time
* period from the given root key.
* Derives handshake keys for the given transport in the given time period
* from the given root key.
*
* @param alice whether the keys are for use by Alice or Bob.
*/
StaticTransportKeys deriveStaticTransportKeys(TransportId t,
SecretKey rootKey, long timePeriod, boolean alice);
HandshakeKeys deriveHandshakeKeys(TransportId t, SecretKey rootKey,
long timePeriod, boolean alice);
/**
* Updates the given static transport keys to the given time period. If
* the keys are for the given period or any later period they are not
* updated.
* Updates the given handshake keys to the given time period. If the keys
* are for the given period or any later period they are not updated.
*/
StaticTransportKeys updateStaticTransportKeys(StaticTransportKeys k,
long timePeriod);
HandshakeKeys updateHandshakeKeys(HandshakeKeys k, long timePeriod);
/**
* Encodes the pseudo-random tag that is used to recognise a stream.
......
......@@ -21,9 +21,9 @@ import org.briarproject.bramble.api.sync.MessageStatus;
import org.briarproject.bramble.api.sync.Offer;
import org.briarproject.bramble.api.sync.Request;
import org.briarproject.bramble.api.sync.validation.MessageState;
import org.briarproject.bramble.api.transport.StaticTransportKeySet;
import org.briarproject.bramble.api.transport.StaticTransportKeySetId;
import org.briarproject.bramble.api.transport.StaticTransportKeys;
import org.briarproject.bramble.api.transport.HandshakeKeySet;
import org.briarproject.bramble.api.transport.HandshakeKeySetId;
import org.briarproject.bramble.api.transport.HandshakeKeys;
import org.briarproject.bramble.api.transport.TransportKeySet;
import org.briarproject.bramble.api.transport.TransportKeySetId;
import org.briarproject.bramble.api.transport.TransportKeys;
......@@ -113,29 +113,29 @@ public interface DatabaseComponent {
void addGroup(Transaction txn, Group g) throws DbException;
/**
* Stores a local pseudonym.
* Stores the given handshake keys for the given contact and returns a
* key set ID.
*/
void addLocalAuthor(Transaction txn, LocalAuthor a) throws DbException;
HandshakeKeySetId addHandshakeKeys(Transaction txn, ContactId c,
HandshakeKeys k) throws DbException;
/**
* Stores a local message.
* Stores the given handshake keys for the given pending contact and
* returns a key set ID.
*/
void addLocalMessage(Transaction txn, Message m, Metadata meta,
boolean shared) throws DbException;
HandshakeKeySetId addHandshakeKeys(Transaction txn, PendingContactId p,
HandshakeKeys k) throws DbException;
/**
* Stores the given static transport keys for the given contact and returns
* a key set ID.
* Stores a local pseudonym.
*/
StaticTransportKeySetId addStaticTransportKeys(Transaction txn, ContactId c,
StaticTransportKeys k) throws DbException;
void addLocalAuthor(Transaction txn, LocalAuthor a) throws DbException;
/**
* Stores the given static transport keys for the given pending contact and
* returns a key set ID.
* Stores a local message.
*/
StaticTransportKeySetId addStaticTransportKeys(Transaction txn,
PendingContactId p, StaticTransportKeys k) throws DbException;
void addLocalMessage(Transaction txn, Message m, Metadata meta,
boolean shared) throws DbException;
/**
* Stores a transport.
......@@ -287,6 +287,14 @@ public interface DatabaseComponent {
Visibility getGroupVisibility(Transaction txn, ContactId c, GroupId g)
throws DbException;
/**
* Returns all handshake keys for the given transport.
* <p/>
* Read-only.
*/
Collection<HandshakeKeySet> getHandshakeKeys(Transaction txn,
TransportId t) throws DbException;
/**
* Returns the local pseudonym with the given ID.
* <p/>
......@@ -442,14 +450,6 @@ public interface DatabaseComponent {
*/
Settings getSettings(Transaction txn, String namespace) throws DbException;
/**
* Returns all static transport keys for the given transport.
* <p/>
* Read-only.
*/
Collection<StaticTransportKeySet> getStaticTransportKeys(Transaction txn,
TransportId t) throws DbException;
/**
* Returns all transport keys for the given transport.
* <p/>
......@@ -459,11 +459,10 @@ public interface DatabaseComponent {
throws DbException;
/**
* Increments the outgoing stream counter for the given static transport
* keys.
* Increments the outgoing stream counter for the given handshake keys.
*/
void incrementStreamCounter(Transaction txn, TransportId t,
StaticTransportKeySetId k) throws DbException;
HandshakeKeySetId k) throws DbException;
/**
* Increments the outgoing stream counter for the given transport keys.
......@@ -524,6 +523,12 @@ public interface DatabaseComponent {
*/
void removeGroup(Transaction txn, Group g) throws DbException;
/**
* Removes the given handshake keys from the database.
*/
void removeHandshakeKeys(Transaction txn, TransportId t,
HandshakeKeySetId k) throws DbException;
/**
* Removes a local pseudonym (and all associated state) from the database.
*/
......@@ -534,12 +539,6 @@ public interface DatabaseComponent {
*/
void removeMessage(Transaction txn, MessageId m) throws DbException;
/**
* Removes the given static transport keys from the database.
*/
void removeStaticTransportKeys(Transaction txn, TransportId t,
StaticTransportKeySetId k) throws DbException;
/**
* Removes a transport (and all associated state) from the database.
*/
......@@ -606,11 +605,10 @@ public interface DatabaseComponent {
TransportKeySetId k) throws DbException;
/**
* Stores the given static transport keys, deleting any keys they have
* replaced.
* Stores the given handshake keys, deleting any keys they have replaced.
*/
void updateStaticTransportKeys(Transaction txn,
Collection<StaticTransportKeySet> keys) throws DbException;
void updateHandshakeKeys(Transaction txn, Collection<HandshakeKeySet> keys)
throws DbException;
/**
* Stores the given transport keys, deleting any keys they have replaced.
......
......@@ -6,8 +6,7 @@ import org.briarproject.bramble.api.plugin.TransportId;
import javax.annotation.concurrent.Immutable;
/**
* Abstract superclass for {@link TransportKeys} and
* {@link StaticTransportKeys}.
* Abstract superclass for {@link TransportKeys} and {@link HandshakeKeys}.
*/
@Immutable
@NotNullByDefault
......
......@@ -8,37 +8,38 @@ import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
/**
* A set of transport keys for communicating with a contact or pending contact.
* Unlike a {@link TransportKeySet} these keys do not provide forward secrecy.
* A set of keys for handshaking with a given contact or pending contact over a
* given transport. Unlike a {@link TransportKeySet} these keys do not provide
* forward secrecy.
*/
@Immutable
@NotNullByDefault
public class StaticTransportKeySet {
public class HandshakeKeySet {
private final StaticTransportKeySetId keySetId;
private final HandshakeKeySetId keySetId;
@Nullable
private final ContactId contactId;
@Nullable
private final PendingContactId pendingContactId;
private final StaticTransportKeys keys;
private final HandshakeKeys keys;
public StaticTransportKeySet(StaticTransportKeySetId keySetId,
ContactId contactId, StaticTransportKeys keys) {
public HandshakeKeySet(HandshakeKeySetId keySetId, ContactId contactId,
HandshakeKeys keys) {
this.keySetId = keySetId;
this.contactId = contactId;
this.keys = keys;
pendingContactId = null;
}
public StaticTransportKeySet(StaticTransportKeySetId keySetId,
PendingContactId pendingContactId, StaticTransportKeys keys) {
public HandshakeKeySet(HandshakeKeySetId keySetId,
PendingContactId pendingContactId, HandshakeKeys keys) {
this.keySetId = keySetId;
this.pendingContactId = pendingContactId;
this.keys = keys;
contactId = null;
}
public StaticTransportKeySetId getKeySetId() {
public HandshakeKeySetId getKeySetId() {
return keySetId;
}
......@@ -52,7 +53,7 @@ public class StaticTransportKeySet {
return pendingContactId;
}
public StaticTransportKeys getKeys() {
public HandshakeKeys getKeys() {
return keys;
}
......@@ -63,7 +64,7 @@ public class StaticTransportKeySet {
@Override
public boolean equals(Object o) {
return o instanceof StaticTransportKeySet &&
keySetId.equals(((StaticTransportKeySet) o).keySetId);
return o instanceof HandshakeKeySet &&
keySetId.equals(((HandshakeKeySet) o).keySetId);
}
}
......@@ -6,16 +6,16 @@ import javax.annotation.concurrent.Immutable;
/**
* Type-safe wrapper for an integer that uniquely identifies a
* {@link StaticTransportKeySet set of static transport keys} within the scope
* of the local device.
* {@link HandshakeKeySet set of handshake keys} within the scope of the local
* device.
*/
@Immutable
@NotNullByDefault
public class StaticTransportKeySetId {
public class HandshakeKeySetId {
private final int id;
public StaticTransportKeySetId(int id) {
public HandshakeKeySetId(int id) {
this.id = id;
}
......@@ -30,7 +30,7 @@ public class StaticTransportKeySetId {
@Override
public boolean equals(Object o) {
return o instanceof StaticTransportKeySetId &&
id == ((StaticTransportKeySetId) o).id;
return o instanceof HandshakeKeySetId &&
id == ((HandshakeKeySetId) o).id;
}
}
......@@ -7,17 +7,18 @@ import org.briarproject.bramble.api.plugin.TransportId;
import javax.annotation.concurrent.Immutable;
/**
* Keys for communicating with a given contact or pending contact over a given
* transport. Unlike {@link TransportKeys} these do not provide forward secrecy.
* Keys for handshaking with a given contact or pending contact over a given
* transport. Unlike {@link TransportKeys} these keys do not provide forward
* secrecy.
*/
@Immutable
@NotNullByDefault
public class StaticTransportKeys extends AbstractTransportKeys {
public class HandshakeKeys extends AbstractTransportKeys {
private final SecretKey rootKey;
private final boolean alice;
public StaticTransportKeys(TransportId transportId, IncomingKeys inPrev,
public HandshakeKeys(TransportId transportId, IncomingKeys inPrev,
IncomingKeys inCurr, IncomingKeys inNext, OutgoingKeys outCurr,
SecretKey rootKey, boolean alice) {
super(transportId, inPrev, inCurr, inNext, outCurr);
......
......@@ -109,26 +109,26 @@ public interface TransportConstants {
String ROTATE_LABEL = "org.briarproject.bramble.transport/ROTATE";
/**
* Label for deriving Alice's static tag key from the root key.
* Label for deriving Alice's handshake tag key from the root key.
*/
String ALICE_STATIC_TAG_LABEL =
String ALICE_HANDSHAKE_TAG_LABEL =
"org.briarproject.bramble.transport/ALICE_STATIC_TAG_KEY";
/**
* Label for deriving Bob's static tag key from the root key.
* Label for deriving Bob's handshake tag key from the root key.
*/
String BOB_STATIC_TAG_LABEL =
String BOB_HANDSHAKE_TAG_LABEL =
"org.briarproject.bramble.transport/BOB_STATIC_TAG_KEY";
/**
* Label for deriving Alice's static header key from the root key.
* Label for deriving Alice's handshake header key from the root key.
*/
String ALICE_STATIC_HEADER_LABEL =
String ALICE_HANDSHAKE_HEADER_LABEL =
"org.briarproject.bramble.transport/ALICE_STATIC_HEADER_KEY";
/**
* Label for deriving Bob's static header key from the root key.
* Label for deriving Bob's handshake header key from the root key.
*/
String BOB_STATIC_HEADER_LABEL =
String BOB_HANDSHAKE_HEADER_LABEL =
"org.briarproject.bramble.transport/BOB_STATIC_HEADER_KEY";
}
......@@ -6,7 +6,8 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import javax.annotation.concurrent.Immutable;
/**
* A set of transport keys for communicating with a contact.
* A set of keys for communicating with a given contact over a given transport.
* Unlike a {@link HandshakeKeySet} these keys provide forward secrecy.
*/
@Immutable
@NotNullByDefault
......
......@@ -7,7 +7,7 @@ import javax.annotation.concurrent.Immutable;
/**
* Keys for communicating with a given contact over a given transport. Unlike
* {@link StaticTransportKeys}, these keys provide forward secrecy.
* {@link HandshakeKeys} these keys provide forward secrecy.
*/
@Immutable
@NotNullByDefault
......
......@@ -4,9 +4,9 @@ import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.crypto.TransportCrypto;
import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.transport.HandshakeKeys;
import org.briarproject.bramble.api.transport.IncomingKeys;
import org.briarproject.bramble.api.transport.OutgoingKeys;
import org.briarproject.bramble.api.transport.StaticTransportKeys;
import org.briarproject.bramble.api.transport.TransportKeys;
import org.spongycastle.crypto.Digest;
import org.spongycastle.crypto.digests.Blake2bDigest;
......@@ -14,13 +14,13 @@ import org.spongycastle.crypto.digests.Blake2bDigest;
import javax.inject.Inject;
import static java.lang.System.arraycopy;
import static org.briarproject.bramble.api.transport.TransportConstants.ALICE_HANDSHAKE_HEADER_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.ALICE_HANDSHAKE_TAG_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.ALICE_HEADER_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.ALICE_STATIC_HEADER_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.ALICE_STATIC_TAG_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.ALICE_TAG_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.BOB_HANDSHAKE_HEADER_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.BOB_HANDSHAKE_TAG_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.BOB_HEADER_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.BOB_STATIC_HEADER_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.BOB_STATIC_TAG_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.BOB_TAG_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.ROTATE_LABEL;
import static org.briarproject.bramble.api.transport.TransportConstants.TAG_LENGTH;
......@@ -115,49 +115,51 @@ class TransportCryptoImpl implements TransportCrypto {
}
@Override
public StaticTransportKeys deriveStaticTransportKeys(TransportId t,
SecretKey rootKey, long timePeriod, boolean alice) {
public HandshakeKeys deriveHandshakeKeys(TransportId t, SecretKey rootKey,
long timePeriod, boolean alice) {
if (timePeriod < 1) throw new IllegalArgumentException();
IncomingKeys inPrev = deriveStaticIncomingKeys(t, rootKey, alice,
IncomingKeys inPrev = deriveIncomingHandshakeKeys(t, rootKey, alice,
timePeriod - 1);
IncomingKeys inCurr = deriveStaticIncomingKeys(t, rootKey, alice,
IncomingKeys inCurr = deriveIncomingHandshakeKeys(t, rootKey, alice,
timePeriod);
IncomingKeys inNext = deriveStaticIncomingKeys(t, rootKey, alice,
IncomingKeys inNext = deriveIncomingHandshakeKeys(t, rootKey, alice,
timePeriod + 1);
OutgoingKeys outCurr = deriveStaticOutgoingKeys(t, rootKey, alice,
OutgoingKeys outCurr = deriveOutgoingHandshakeKeys(t, rootKey, alice,
timePeriod);
return new StaticTransportKeys(t, inPrev, inCurr, inNext, outCurr,
rootKey, alice);
return new HandshakeKeys(t, inPrev, inCurr, inNext, outCurr, rootKey,
alice);
}
private IncomingKeys deriveStaticIncomingKeys(TransportId t,
private IncomingKeys deriveIncomingHandshakeKeys(TransportId t,
SecretKey rootKey, boolean alice, long timePeriod) {
SecretKey tag = deriveStaticTagKey(t, rootKey, !alice, timePeriod);
SecretKey header = deriveStaticHeaderKey(t, rootKey, !alice,
SecretKey tag = deriveHandshakeTagKey(t, rootKey, !alice, timePeriod);
SecretKey header = deriveHandshakeHeaderKey(t, rootKey, !alice,
timePeriod);
return new IncomingKeys(tag, header, timePeriod);
}
private OutgoingKeys deriveStaticOutgoingKeys(TransportId t,
private OutgoingKeys deriveOutgoingHandshakeKeys(TransportId t,
SecretKey rootKey, boolean alice, long timePeriod) {
SecretKey tag = deriveStaticTagKey(t, rootKey, alice, timePeriod);
SecretKey header = deriveStaticHeaderKey(t, rootKey, alice, timePeriod);
SecretKey tag = deriveHandshakeTagKey(t, rootKey, alice, timePeriod);
SecretKey header = deriveHandshakeHeaderKey(t, rootKey, alice,
timePeriod);
return new OutgoingKeys(tag, header, timePeriod, true);
}
private SecretKey deriveStaticTagKey(TransportId t, SecretKey rootKey,
private SecretKey deriveHandshakeTagKey(TransportId t, SecretKey rootKey,
boolean alice, long timePeriod) {
String label = alice ? ALICE_STATIC_TAG_LABEL : BOB_STATIC_TAG_LABEL;
String label = alice ? ALICE_HANDSHAKE_TAG_LABEL :
BOB_HANDSHAKE_TAG_LABEL;
byte[] id = toUtf8(t.getString());
byte[] period = new byte[INT_64_BYTES];
writeUint64(timePeriod, period, 0);
return crypto.deriveKey(label, rootKey, id, period);
}
private SecretKey deriveStaticHeaderKey(TransportId t, SecretKey rootKey,
private SecretKey deriveHandshakeHeaderKey(TransportId t, SecretKey rootKey,
boolean alice, long timePeriod) {
String label =
alice ? ALICE_STATIC_HEADER_LABEL : BOB_STATIC_HEADER_LABEL;
String label = alice ? ALICE_HANDSHAKE_HEADER_LABEL :
BOB_HANDSHAKE_HEADER_LABEL;
byte[] id = toUtf8(t.getString());
byte[] period = new byte[INT_64_BYTES];
writeUint64(timePeriod, period, 0);
......@@ -165,8 +167,7 @@ class TransportCryptoImpl implements TransportCrypto {
}
@Override
public StaticTransportKeys updateStaticTransportKeys(StaticTransportKeys k,
long timePeriod) {
public HandshakeKeys updateHandshakeKeys(HandshakeKeys k, long timePeriod) {
long elapsed = timePeriod - k.getTimePeriod();
TransportId t = k.getTransportId();
SecretKey rootKey = k.getRootKey();
......@@ -178,26 +179,26 @@ class TransportCryptoImpl implements TransportCrypto {
// The keys are one period old - shift by one period
IncomingKeys inPrev = k.getCurrentIncomingKeys();
IncomingKeys inCurr = k.getNextIncomingKeys();
IncomingKeys inNext = deriveStaticIncomingKeys(t, rootKey, alice,
timePeriod + 1);
OutgoingKeys outCurr = deriveStaticOutgoingKeys(t, rootKey, alice,
timePeriod);
return new StaticTransportKeys(t, inPrev, inCurr, inNext, outCurr,
IncomingKeys inNext = deriveIncomingHandshakeKeys(t, rootKey,
alice, timePeriod + 1);
OutgoingKeys outCurr = deriveOutgoingHandshakeKeys(t, rootKey,
alice, timePeriod);
return new HandshakeKeys(t, inPrev, inCurr, inNext, outCurr,
rootKey, alice);
} else if (elapsed == 2) {
// The keys are two periods old - shift by two periods
IncomingKeys inPrev = k.getNextIncomingKeys();
IncomingKeys inCurr = deriveStaticIncomingKeys(t, rootKey, alice,
timePeriod);
IncomingKeys inNext = deriveStaticIncomingKeys(t, rootKey, alice,
timePeriod + 1);
OutgoingKeys outCurr = deriveStaticOutgoingKeys(t, rootKey, alice,
timePeriod);
return new StaticTransportKeys(t, inPrev, inCurr, inNext, outCurr,
IncomingKeys inCurr = deriveIncomingHandshakeKeys(t, rootKey,
alice, timePeriod);
IncomingKeys inNext = deriveIncomingHandshakeKeys(t, rootKey,
alice, timePeriod + 1);
OutgoingKeys outCurr = deriveOutgoingHandshakeKeys(t, rootKey,
alice, timePeriod);
return new HandshakeKeys(t, inPrev, inCurr, inNext, outCurr,
rootKey, alice);
} else {
// The keys are more than two periods old - derive fresh keys
return deriveStaticTransportKeys(t, rootKey, timePeriod, alice);
return deriveHandshakeKeys(t, rootKey, timePeriod, alice);
}
}
......
......@@ -27,9 +27,9 @@ import org.briarproject.bramble.api.sync.Message;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.api.sync.MessageStatus;
import org.briarproject.bramble.api.sync.validation.MessageState;
import org.briarproject.bramble.api.transport.StaticTransportKeySet;
import org.briarproject.bramble.api.transport.StaticTransportKeySetId;
import org.briarproject.bramble.api.transport.StaticTransportKeys;
import org.briarproject.bramble.api.transport.HandshakeKeySet;
import org.briarproject.bramble.api.transport.HandshakeKeySetId;
import org.briarproject.bramble.api.transport.HandshakeKeys;
import org.briarproject.bramble.api.transport.TransportKeySet;
import org.briarproject.bramble.api.transport.TransportKeySetId;
import org.briarproject.bramble.api.transport.TransportKeys;
......@@ -105,6 +105,20 @@ interface Database<T> {
void addGroupVisibility(T txn, ContactId c, GroupId g, boolean shared)
throws DbException;
/**
* Stores the given handshake keys for the given contact and returns a
* key set ID.
*/
HandshakeKeySetId addHandshakeKeys(T txn, ContactId c, HandshakeKeys k)
throws DbException;
/**
* Stores the given handshake keys for the given pending contact and
* returns a key set ID.
*/
HandshakeKeySetId addHandshakeKeys(T txn, PendingContactId p,
HandshakeKeys k) throws DbException;
/**
* Stores a local pseudonym.
*/
......@@ -136,20 +150,6 @@ interface Database<T> {
*/
void addPendingContact(T txn, PendingContact p) throws DbException;
/**
* Stores the given static transport keys for the given contact and returns
* a key set ID.
*/
StaticTransportKeySetId addStaticTransportKeys(T txn, ContactId c,
StaticTransportKeys k) throws DbException;
/**
* Stores the given static transport keys for the given pending contact and
* returns a key set ID.
*/
StaticTransportKeySetId addStaticTransportKeys(T txn, PendingContactId p,
StaticTransportKeys k) throws DbException;
/**
* Stores a transport.
*/
......@@ -314,6 +314,14 @@ interface Database<T> {
Map<ContactId, Boolean> getGroupVisibility(T txn, GroupId g)
throws DbException;
/**
* Returns all handshake keys for the given transport.
* <p/>
* Read-only.
*/
Collection<HandshakeKeySet> getHandshakeKeys(T txn, TransportId t)
throws DbException;
/**
* Returns the local pseudonym with the given ID.
* <p/>
......@@ -528,14 +536,6 @@ interface Database<T> {
*/
Settings getSettings(T txn, String namespace) throws DbException;
/**
* Returns all static transport keys for the given transport.
* <p/>
* Read-only.
*/
Collection<StaticTransportKeySet> getStaticTransportKeys(T txn,
TransportId t) throws DbException;
/**