Skip to content

Commit

Permalink
Remove unnecessary classes, up prekey limit to 100
Browse files Browse the repository at this point in the history
  • Loading branch information
moxie0 committed Jan 6, 2014
1 parent 1ab4e7e commit 327ee4f
Show file tree
Hide file tree
Showing 11 changed files with 546 additions and 164 deletions.
6 changes: 6 additions & 0 deletions library/protobuf/LocalStorageProtocol.proto
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ message SessionStructure {

optional PendingKeyExchange pendingKeyExchange = 8;
optional PendingPreKey pendingPreKey = 9;
}

message PreKeyRecordStructure {
optional uint32 id = 1;
optional bytes publicKey = 2;
optional bytes privateKey = 3;
}
59 changes: 0 additions & 59 deletions library/src/org/whispersystems/textsecure/crypto/PreKeyPair.java

This file was deleted.

49 changes: 0 additions & 49 deletions library/src/org/whispersystems/textsecure/crypto/PreKeyPublic.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.thoughtcrimegson.Gson;

import org.whispersystems.textsecure.crypto.ecc.Curve25519;
import org.whispersystems.textsecure.crypto.ecc.ECKeyPair;
import org.whispersystems.textsecure.storage.InvalidKeyIdException;
import org.whispersystems.textsecure.storage.PreKeyRecord;
import org.whispersystems.textsecure.util.Medium;
Expand All @@ -40,15 +41,15 @@

public class PreKeyUtil {

public static final int BATCH_SIZE = 20;
public static final int BATCH_SIZE = 100;

public static List<PreKeyRecord> generatePreKeys(Context context, MasterSecret masterSecret) {
List<PreKeyRecord> records = new LinkedList<PreKeyRecord>();
int preKeyIdOffset = getNextPreKeyId(context);

for (int i=0;i<BATCH_SIZE;i++) {
int preKeyId = (preKeyIdOffset + i) % Medium.MAX_VALUE;
PreKeyPair keyPair = new PreKeyPair(masterSecret, Curve25519.generateKeyPair());
ECKeyPair keyPair = Curve25519.generateKeyPair();
PreKeyRecord record = new PreKeyRecord(context, masterSecret, preKeyId, keyPair);

record.save();
Expand All @@ -69,7 +70,7 @@ public static PreKeyRecord generateLastResortKey(Context context, MasterSecret m
}
}

PreKeyPair keyPair = new PreKeyPair(masterSecret, Curve25519.generateKeyPair());
ECKeyPair keyPair = Curve25519.generateKeyPair();
PreKeyRecord record = new PreKeyRecord(context, masterSecret, Medium.MAX_VALUE, keyPair);

record.save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ public PublicKey(int id, ECPublicKey publicKey) {
this.id = id;
}

public PublicKey(int preKeyId, PreKeyPublic publicKey) {
this.id = preKeyId;
this.publicKey = publicKey.getPublicKey();
}

public PublicKey(byte[] bytes, int offset) throws InvalidKeyException {
Log.w("PublicKey", "PublicKey Length: " + (bytes.length - offset));

Expand Down
26 changes: 14 additions & 12 deletions library/src/org/whispersystems/textsecure/push/PreKeyEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@
import com.google.thoughtcrimegson.JsonPrimitive;
import com.google.thoughtcrimegson.JsonSerializationContext;
import com.google.thoughtcrimegson.JsonSerializer;

import org.whispersystems.textsecure.crypto.IdentityKey;
import org.whispersystems.textsecure.crypto.InvalidKeyException;
import org.whispersystems.textsecure.crypto.PreKeyPublic;
import org.whispersystems.textsecure.crypto.ecc.Curve;
import org.whispersystems.textsecure.crypto.ecc.ECPublicKey;
import org.whispersystems.textsecure.util.Base64;

import java.io.IOException;
import java.lang.reflect.Type;

public class PreKeyEntity {

private int keyId;
private PreKeyPublic publicKey;
private IdentityKey identityKey;
private int keyId;
private ECPublicKey publicKey;
private IdentityKey identityKey;

public PreKeyEntity(int keyId, PreKeyPublic publicKey, IdentityKey identityKey) {
public PreKeyEntity(int keyId, ECPublicKey publicKey, IdentityKey identityKey) {
this.keyId = keyId;
this.publicKey = publicKey;
this.identityKey = identityKey;
Expand All @@ -32,7 +34,7 @@ public int getKeyId() {
return keyId;
}

public PreKeyPublic getPublicKey() {
public ECPublicKey getPublicKey() {
return publicKey;
}

Expand All @@ -50,29 +52,29 @@ public static PreKeyEntity fromJson(String encoded) {

public static GsonBuilder getBuilder() {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(PreKeyPublic.class, new PreKeyPublicJsonAdapter());
builder.registerTypeAdapter(ECPublicKey.class, new ECPublicKeyJsonAdapter());
builder.registerTypeAdapter(IdentityKey.class, new IdentityKeyJsonAdapter());

return builder;
}

private static class PreKeyPublicJsonAdapter
implements JsonSerializer<PreKeyPublic>, JsonDeserializer<PreKeyPublic>
private static class ECPublicKeyJsonAdapter
implements JsonSerializer<ECPublicKey>, JsonDeserializer<ECPublicKey>
{
@Override
public JsonElement serialize(PreKeyPublic preKeyPublic, Type type,
public JsonElement serialize(ECPublicKey preKeyPublic, Type type,
JsonSerializationContext jsonSerializationContext)
{
return new JsonPrimitive(Base64.encodeBytesWithoutPadding(preKeyPublic.serialize()));
}

@Override
public PreKeyPublic deserialize(JsonElement jsonElement, Type type,
public ECPublicKey deserialize(JsonElement jsonElement, Type type,
JsonDeserializationContext jsonDeserializationContext)
throws JsonParseException
{
try {
return new PreKeyPublic(Base64.decodeWithoutPadding(jsonElement.getAsJsonPrimitive().getAsString()), 0);
return Curve.decodePoint(Base64.decodeWithoutPadding(jsonElement.getAsJsonPrimitive().getAsString()), 0);
} catch (InvalidKeyException e) {
throw new JsonParseException(e);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public static String toJson(PreKeyList entity) {
return PreKeyEntity.getBuilder().create().toJson(entity);
}

public static PreKeyList fromJson(String serialized) {
return PreKeyEntity.getBuilder().create().fromJson(serialized, PreKeyList.class);
}

public PreKeyEntity getLastResortKey() {
return lastResortKey;
}
Expand Down
71 changes: 40 additions & 31 deletions library/src/org/whispersystems/textsecure/storage/PreKeyRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
import android.content.Context;
import android.util.Log;

import com.google.protobuf.ByteString;

import org.whispersystems.textsecure.crypto.InvalidKeyException;
import org.whispersystems.textsecure.crypto.InvalidMessageException;
import org.whispersystems.textsecure.crypto.MasterCipher;
import org.whispersystems.textsecure.crypto.MasterSecret;
import org.whispersystems.textsecure.crypto.PreKeyPair;
import org.whispersystems.textsecure.crypto.ecc.Curve;
import org.whispersystems.textsecure.crypto.ecc.ECKeyPair;
import org.whispersystems.textsecure.crypto.ecc.ECPrivateKey;
import org.whispersystems.textsecure.crypto.ecc.ECPublicKey;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand All @@ -19,36 +26,46 @@ public class PreKeyRecord extends Record {
private static final int CURRENT_VERSION_MARKER = 1;

private final MasterSecret masterSecret;

private PreKeyPair keyPair;
private int id;
private StorageProtos.PreKeyRecordStructure structure;

public PreKeyRecord(Context context, MasterSecret masterSecret, int id)
throws InvalidKeyIdException
{
super(context, PREKEY_DIRECTORY, id+"");

this.id = id;
this.structure = StorageProtos.PreKeyRecordStructure.newBuilder().setId(id).build();
this.masterSecret = masterSecret;

loadData();
}

public PreKeyRecord(Context context, MasterSecret masterSecret,
int id, PreKeyPair keyPair)
int id, ECKeyPair keyPair)
{
super(context, PREKEY_DIRECTORY, id+"");
this.id = id;
this.keyPair = keyPair;
this.masterSecret = masterSecret;
this.structure = StorageProtos.PreKeyRecordStructure.newBuilder()
.setId(id)
.setPublicKey(ByteString.copyFrom(keyPair.getPublicKey()
.serialize()))
.setPrivateKey(ByteString.copyFrom(keyPair.getPrivateKey()
.serialize()))
.build();
}

public int getId() {
return id;
return this.structure.getId();
}

public PreKeyPair getKeyPair() {
return keyPair;
public ECKeyPair getKeyPair() {
try {
ECPublicKey publicKey = Curve.decodePoint(this.structure.getPublicKey().toByteArray(), 0);
ECPrivateKey privateKey = Curve.decodePrivatePoint(publicKey.getType(), this.structure.getPrivateKey().toByteArray());

return new ECKeyPair(publicKey, privateKey);
} catch (InvalidKeyException e) {
throw new AssertionError(e);
}
}

public static boolean hasRecord(Context context, long id) {
Expand All @@ -67,8 +84,10 @@ public void save() {
FileChannel out = file.getChannel();
out.position(0);

MasterCipher masterCipher = new MasterCipher(masterSecret);

writeInteger(CURRENT_VERSION_MARKER, out);
writeKeyPair(keyPair, out);
writeBlob(masterCipher.encryptBytes(structure.toByteArray()), out);

out.force(true);
out.truncate(out.position());
Expand All @@ -83,39 +102,29 @@ public void save() {
private void loadData() throws InvalidKeyIdException {
synchronized (FILE_LOCK) {
try {
FileInputStream in = this.openInputStream();
int recordVersion = readInteger(in);
MasterCipher masterCipher = new MasterCipher(masterSecret);
FileInputStream in = this.openInputStream();
int recordVersion = readInteger(in);

if (recordVersion != CURRENT_VERSION_MARKER) {
Log.w("PreKeyRecord", "Invalid version: " + recordVersion);
return;
}

keyPair = readKeyPair(in);
this.structure =
StorageProtos.PreKeyRecordStructure.parseFrom(masterCipher.decryptBytes(readBlob(in)));

in.close();
} catch (FileNotFoundException e) {
Log.w("PreKeyRecord", e);
throw new InvalidKeyIdException(e);
} catch (IOException ioe) {
Log.w("PreKeyRecord", ioe);
throw new InvalidKeyIdException(ioe);
} catch (InvalidKeyException ike) {
Log.w("LocalKeyRecord", ike);
throw new InvalidKeyIdException(ike);
} catch (InvalidMessageException ime) {
Log.w("PreKeyRecord", ime);
throw new InvalidKeyIdException(ime);
}
}
}

private void writeKeyPair(PreKeyPair keyPair, FileChannel out) throws IOException {
byte[] serialized = keyPair.serialize();
writeBlob(serialized, out);
}

private PreKeyPair readKeyPair(FileInputStream in)
throws IOException, InvalidKeyException
{
byte[] keyPairBytes = readBlob(in);
return new PreKeyPair(masterSecret, keyPairBytes);
}

}
Loading

0 comments on commit 327ee4f

Please sign in to comment.