Search in sources :

Example 1 with ECPrivateKey

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

the class IdentityKeyUtil method generateIdentityKeys.

public static void generateIdentityKeys(Context context) {
    ECKeyPair djbKeyPair = Curve.generateKeyPair();
    IdentityKey djbIdentityKey = new IdentityKey(djbKeyPair.getPublicKey());
    ECPrivateKey djbPrivateKey = djbKeyPair.getPrivateKey();
    save(context, IDENTITY_PUBLIC_KEY_PREF, Base64.encodeBytes(djbIdentityKey.serialize()));
    save(context, IDENTITY_PRIVATE_KEY_PREF, Base64.encodeBytes(djbPrivateKey.serialize()));
}
Also used : ECPrivateKey(org.whispersystems.libsignal.ecc.ECPrivateKey) IdentityKey(org.whispersystems.libsignal.IdentityKey) ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair)

Example 2 with ECPrivateKey

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

the class IdentityKeyUtil method getIdentityKeyPair.

@NonNull
public static IdentityKeyPair getIdentityKeyPair(@NonNull Context context) {
    if (!hasIdentityKey(context))
        throw new AssertionError("There isn't one!");
    try {
        IdentityKey publicKey = getIdentityKey(context);
        ECPrivateKey privateKey = Curve.decodePrivatePoint(Base64.decode(retrieve(context, IDENTITY_PRIVATE_KEY_PREF)));
        return new IdentityKeyPair(publicKey, privateKey);
    } catch (IOException e) {
        throw new AssertionError(e);
    }
}
Also used : ECPrivateKey(org.whispersystems.libsignal.ecc.ECPrivateKey) IdentityKey(org.whispersystems.libsignal.IdentityKey) IOException(java.io.IOException) IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) NonNull(android.support.annotation.NonNull)

Example 3 with ECPrivateKey

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

the class IdentityKeyUtil method getLegacyIdentityKeyPair.

private static IdentityKeyPair getLegacyIdentityKeyPair(@NonNull Context context, @NonNull MasterSecret masterSecret) {
    try {
        MasterCipher masterCipher = new MasterCipher(masterSecret);
        byte[] publicKeyBytes = Base64.decode(retrieve(context, IDENTITY_PUBLIC_KEY_CIPHERTEXT_LEGACY_PREF));
        IdentityKey identityKey = new IdentityKey(publicKeyBytes, 0);
        ECPrivateKey privateKey = masterCipher.decryptKey(Base64.decode(retrieve(context, IDENTITY_PRIVATE_KEY_CIPHERTEXT_LEGACY_PREF)));
        return new IdentityKeyPair(identityKey, privateKey);
    } catch (IOException | InvalidKeyException e) {
        throw new AssertionError(e);
    }
}
Also used : ECPrivateKey(org.whispersystems.libsignal.ecc.ECPrivateKey) IdentityKey(org.whispersystems.libsignal.IdentityKey) IOException(java.io.IOException) IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException)

Example 4 with ECPrivateKey

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

the class MasterSecretUtil method getAsymmetricMasterSecret.

public static AsymmetricMasterSecret getAsymmetricMasterSecret(@NonNull Context context, @Nullable MasterSecret masterSecret) {
    try {
        byte[] djbPublicBytes = retrieve(context, ASYMMETRIC_LOCAL_PUBLIC_DJB);
        byte[] djbPrivateBytes = retrieve(context, ASYMMETRIC_LOCAL_PRIVATE_DJB);
        ECPublicKey djbPublicKey = null;
        ECPrivateKey djbPrivateKey = null;
        if (djbPublicBytes != null) {
            djbPublicKey = Curve.decodePoint(djbPublicBytes, 0);
        }
        if (masterSecret != null) {
            MasterCipher masterCipher = new MasterCipher(masterSecret);
            if (djbPrivateBytes != null) {
                djbPrivateKey = masterCipher.decryptKey(djbPrivateBytes);
            }
        }
        return new AsymmetricMasterSecret(djbPublicKey, djbPrivateKey);
    } catch (InvalidKeyException | IOException ike) {
        throw new AssertionError(ike);
    }
}
Also used : ECPrivateKey(org.whispersystems.libsignal.ecc.ECPrivateKey) ECPublicKey(org.whispersystems.libsignal.ecc.ECPublicKey) IOException(java.io.IOException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException)

Example 5 with ECPrivateKey

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

the class AsymmetricMasterCipher method decryptBytes.

public byte[] decryptBytes(byte[] combined) throws IOException, InvalidMessageException {
    try {
        byte[][] parts = Util.split(combined, PublicKey.KEY_SIZE, combined.length - PublicKey.KEY_SIZE);
        PublicKey theirPublicKey = new PublicKey(parts[0], 0);
        ECPrivateKey ourPrivateKey = asymmetricMasterSecret.getPrivateKey();
        byte[] secret = Curve.calculateAgreement(theirPublicKey.getKey(), ourPrivateKey);
        MasterCipher masterCipher = getMasterCipherForSecret(secret);
        return masterCipher.decryptBytes(parts[1]);
    } catch (InvalidKeyException e) {
        throw new InvalidMessageException(e);
    }
}
Also used : ECPrivateKey(org.whispersystems.libsignal.ecc.ECPrivateKey) InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) ECPublicKey(org.whispersystems.libsignal.ecc.ECPublicKey) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException)

Aggregations

ECPrivateKey (org.whispersystems.libsignal.ecc.ECPrivateKey)5 IOException (java.io.IOException)3 IdentityKey (org.whispersystems.libsignal.IdentityKey)3 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)3 IdentityKeyPair (org.whispersystems.libsignal.IdentityKeyPair)2 ECPublicKey (org.whispersystems.libsignal.ecc.ECPublicKey)2 NonNull (android.support.annotation.NonNull)1 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)1 ECKeyPair (org.whispersystems.libsignal.ecc.ECKeyPair)1