Search in sources :

Example 16 with ECKeyPair

use of org.whispersystems.libsignal.ecc.ECKeyPair in project Signal-Android by signalapp.

the class SignedPreKeyDatabase method getSignedPreKey.

@Nullable
public SignedPreKeyRecord getSignedPreKey(int keyId) {
    SQLiteDatabase database = databaseHelper.getReadableDatabase();
    try (Cursor cursor = database.query(TABLE_NAME, null, KEY_ID + " = ?", new String[] { String.valueOf(keyId) }, null, null, null)) {
        if (cursor != null && cursor.moveToFirst()) {
            try {
                ECPublicKey publicKey = Curve.decodePoint(Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(PUBLIC_KEY))), 0);
                ECPrivateKey privateKey = Curve.decodePrivatePoint(Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(PRIVATE_KEY))));
                byte[] signature = Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(SIGNATURE)));
                long timestamp = cursor.getLong(cursor.getColumnIndexOrThrow(TIMESTAMP));
                return new SignedPreKeyRecord(keyId, timestamp, new ECKeyPair(publicKey, privateKey), signature);
            } catch (InvalidKeyException | IOException e) {
                Log.w(TAG, e);
            }
        }
    }
    return null;
}
Also used : ECPrivateKey(org.whispersystems.libsignal.ecc.ECPrivateKey) ECPublicKey(org.whispersystems.libsignal.ecc.ECPublicKey) SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair) IOException(java.io.IOException) Cursor(android.database.Cursor) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) Nullable(android.support.annotation.Nullable)

Example 17 with ECKeyPair

use of org.whispersystems.libsignal.ecc.ECKeyPair 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 18 with ECKeyPair

use of org.whispersystems.libsignal.ecc.ECKeyPair 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)

Example 19 with ECKeyPair

use of org.whispersystems.libsignal.ecc.ECKeyPair in project Signal-Android by WhisperSystems.

the class MasterSecretUtil method generateAsymmetricMasterSecret.

public static AsymmetricMasterSecret generateAsymmetricMasterSecret(Context context, MasterSecret masterSecret) {
    MasterCipher masterCipher = new MasterCipher(masterSecret);
    ECKeyPair keyPair = Curve.generateKeyPair();
    save(context, ASYMMETRIC_LOCAL_PUBLIC_DJB, keyPair.getPublicKey().serialize());
    save(context, ASYMMETRIC_LOCAL_PRIVATE_DJB, masterCipher.encryptKey(keyPair.getPrivateKey()));
    return new AsymmetricMasterSecret(keyPair.getPublicKey(), keyPair.getPrivateKey());
}
Also used : ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair)

Example 20 with ECKeyPair

use of org.whispersystems.libsignal.ecc.ECKeyPair in project Signal-Android by WhisperSystems.

the class PrimaryProvisioningCipher method encrypt.

public byte[] encrypt(ProvisionMessage message) throws InvalidKeyException {
    ECKeyPair ourKeyPair = Curve.generateKeyPair();
    byte[] sharedSecret = Curve.calculateAgreement(theirPublicKey, ourKeyPair.getPrivateKey());
    byte[] derivedSecret = HKDF.deriveSecrets(sharedSecret, PROVISIONING_MESSAGE.getBytes(), 64);
    byte[][] parts = Util.split(derivedSecret, 32, 32);
    byte[] version = { 0x01 };
    byte[] ciphertext = getCiphertext(parts[0], message.toByteArray());
    byte[] mac = getMac(parts[1], Util.join(version, ciphertext));
    byte[] body = Util.join(version, ciphertext, mac);
    return ProvisionEnvelope.newBuilder().setPublicKey(ByteString.copyFrom(ourKeyPair.getPublicKey().serialize())).setBody(ByteString.copyFrom(body)).build().toByteArray();
}
Also used : ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair)

Aggregations

ECKeyPair (org.whispersystems.libsignal.ecc.ECKeyPair)32 SignedPreKeyRecord (org.whispersystems.libsignal.state.SignedPreKeyRecord)14 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)10 PreKeyRecord (org.whispersystems.libsignal.state.PreKeyRecord)8 SignedPreKeyStore (org.whispersystems.libsignal.state.SignedPreKeyStore)8 ECPrivateKey (org.whispersystems.libsignal.ecc.ECPrivateKey)7 LinkedList (java.util.LinkedList)6 IdentityKey (org.whispersystems.libsignal.IdentityKey)6 TextSecurePreKeyStore (org.thoughtcrime.securesms.crypto.storage.TextSecurePreKeyStore)5 ECPublicKey (org.whispersystems.libsignal.ecc.ECPublicKey)5 PreKeyStore (org.whispersystems.libsignal.state.PreKeyStore)5 NonNull (androidx.annotation.NonNull)4 IdentityKeyPair (org.whispersystems.libsignal.IdentityKeyPair)4 MultiRemoteAttestationResponse (org.whispersystems.signalservice.internal.contacts.entities.MultiRemoteAttestationResponse)4 RemoteAttestationResponse (org.whispersystems.signalservice.internal.contacts.entities.RemoteAttestationResponse)4 Cursor (android.database.Cursor)3 SignalPreKeyStore (com.toshi.crypto.signal.store.SignalPreKeyStore)3 IOException (java.io.IOException)3 SQLiteDatabase (net.sqlcipher.database.SQLiteDatabase)3 Nullable (android.support.annotation.Nullable)2