Search in sources :

Example 1 with ECPublicKey

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

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

the class AsymmetricMasterCipher method encryptBytes.

public byte[] encryptBytes(byte[] body) {
    try {
        ECPublicKey theirPublic = asymmetricMasterSecret.getDjbPublicKey();
        ECKeyPair ourKeyPair = Curve.generateKeyPair();
        byte[] secret = Curve.calculateAgreement(theirPublic, ourKeyPair.getPrivateKey());
        MasterCipher masterCipher = getMasterCipherForSecret(secret);
        byte[] encryptedBodyBytes = masterCipher.encryptBytes(body);
        PublicKey ourPublicKey = new PublicKey(31337, ourKeyPair.getPublicKey());
        byte[] publicKeyBytes = ourPublicKey.serialize();
        return Util.combine(publicKeyBytes, encryptedBodyBytes);
    } catch (InvalidKeyException e) {
        throw new AssertionError(e);
    }
}
Also used : ECPublicKey(org.whispersystems.libsignal.ecc.ECPublicKey) ECPublicKey(org.whispersystems.libsignal.ecc.ECPublicKey) ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException)

Aggregations

InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)2 ECPublicKey (org.whispersystems.libsignal.ecc.ECPublicKey)2 IOException (java.io.IOException)1 ECKeyPair (org.whispersystems.libsignal.ecc.ECKeyPair)1 ECPrivateKey (org.whispersystems.libsignal.ecc.ECPrivateKey)1