Search in sources :

Example 1 with SignalPreKeyStore

use of com.toshi.crypto.signal.store.SignalPreKeyStore in project toshi-android-client by toshiapp.

the class PreKeyUtil method generateSignedPreKey.

public static SignedPreKeyRecord generateSignedPreKey(Context context, IdentityKeyPair identityKeyPair, boolean active) {
    try {
        SignedPreKeyStore signedPreKeyStore = new SignalPreKeyStore();
        int signedPreKeyId = getNextSignedPreKeyId(context);
        ECKeyPair keyPair = Curve.generateKeyPair();
        byte[] signature = Curve.calculateSignature(identityKeyPair.getPrivateKey(), keyPair.getPublicKey().serialize());
        SignedPreKeyRecord record = new SignedPreKeyRecord(signedPreKeyId, System.currentTimeMillis(), keyPair, signature);
        signedPreKeyStore.storeSignedPreKey(signedPreKeyId, record);
        setNextSignedPreKeyId(context, (signedPreKeyId + 1) % Medium.MAX_VALUE);
        if (active) {
            setActiveSignedPreKeyId(context, signedPreKeyId);
        }
        return record;
    } catch (InvalidKeyException e) {
        LogUtil.exception("Error while generating signed pre key", e);
        throw new AssertionError(e);
    }
}
Also used : SignalPreKeyStore(com.toshi.crypto.signal.store.SignalPreKeyStore) SignedPreKeyStore(org.whispersystems.libsignal.state.SignedPreKeyStore) ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord)

Example 2 with SignalPreKeyStore

use of com.toshi.crypto.signal.store.SignalPreKeyStore in project toshi-android-client by toshiapp.

the class PreKeyUtil method generatePreKeys.

public static List<PreKeyRecord> generatePreKeys(Context context) {
    PreKeyStore preKeyStore = new SignalPreKeyStore();
    List<PreKeyRecord> records = new LinkedList<>();
    int preKeyIdOffset = getNextPreKeyId(context);
    for (int i = 0; i < BATCH_SIZE; i++) {
        int preKeyId = (preKeyIdOffset + i) % Medium.MAX_VALUE;
        ECKeyPair keyPair = Curve.generateKeyPair();
        PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair);
        preKeyStore.storePreKey(preKeyId, record);
        records.add(record);
    }
    setNextPreKeyId(context, (preKeyIdOffset + BATCH_SIZE + 1) % Medium.MAX_VALUE);
    return records;
}
Also used : SignalPreKeyStore(com.toshi.crypto.signal.store.SignalPreKeyStore) ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair) PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) SignalPreKeyStore(com.toshi.crypto.signal.store.SignalPreKeyStore) PreKeyStore(org.whispersystems.libsignal.state.PreKeyStore) SignedPreKeyStore(org.whispersystems.libsignal.state.SignedPreKeyStore) LinkedList(java.util.LinkedList)

Example 3 with SignalPreKeyStore

use of com.toshi.crypto.signal.store.SignalPreKeyStore in project toshi-android-client by toshiapp.

the class PreKeyUtil method generateLastResortKey.

public static PreKeyRecord generateLastResortKey(Context context) {
    PreKeyStore preKeyStore = new SignalPreKeyStore();
    if (preKeyStore.containsPreKey(Medium.MAX_VALUE)) {
        try {
            return preKeyStore.loadPreKey(Medium.MAX_VALUE);
        } catch (InvalidKeyIdException e) {
            LogUtil.exception("Error while generating last resort key", e);
            preKeyStore.removePreKey(Medium.MAX_VALUE);
        }
    }
    ECKeyPair keyPair = Curve.generateKeyPair();
    PreKeyRecord record = new PreKeyRecord(Medium.MAX_VALUE, keyPair);
    preKeyStore.storePreKey(Medium.MAX_VALUE, record);
    return record;
}
Also used : SignalPreKeyStore(com.toshi.crypto.signal.store.SignalPreKeyStore) ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair) PreKeyRecord(org.whispersystems.libsignal.state.PreKeyRecord) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) InvalidKeyIdException(org.whispersystems.libsignal.InvalidKeyIdException) SignalPreKeyStore(com.toshi.crypto.signal.store.SignalPreKeyStore) PreKeyStore(org.whispersystems.libsignal.state.PreKeyStore) SignedPreKeyStore(org.whispersystems.libsignal.state.SignedPreKeyStore)

Aggregations

SignalPreKeyStore (com.toshi.crypto.signal.store.SignalPreKeyStore)3 ECKeyPair (org.whispersystems.libsignal.ecc.ECKeyPair)3 SignedPreKeyRecord (org.whispersystems.libsignal.state.SignedPreKeyRecord)3 SignedPreKeyStore (org.whispersystems.libsignal.state.SignedPreKeyStore)3 PreKeyRecord (org.whispersystems.libsignal.state.PreKeyRecord)2 PreKeyStore (org.whispersystems.libsignal.state.PreKeyStore)2 LinkedList (java.util.LinkedList)1 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)1 InvalidKeyIdException (org.whispersystems.libsignal.InvalidKeyIdException)1