Search in sources :

Example 1 with InvalidMacException

use of org.whispersystems.libsignal.InvalidMacException in project libsignal-service-java by signalapp.

the class AttachmentCipherInputStream method createFor.

public static InputStream createFor(File file, long plaintextLength, byte[] combinedKeyMaterial, byte[] digest) throws InvalidMessageException, IOException {
    try {
        byte[][] parts = Util.split(combinedKeyMaterial, CIPHER_KEY_SIZE, MAC_KEY_SIZE);
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(parts[1], "HmacSHA256"));
        if (file.length() <= BLOCK_SIZE + mac.getMacLength()) {
            throw new InvalidMessageException("Message shorter than crypto overhead!");
        }
        verifyMac(file, mac, digest);
        InputStream inputStream = new AttachmentCipherInputStream(new FileInputStream(file), parts[0], file.length() - BLOCK_SIZE - mac.getMacLength());
        if (plaintextLength != 0) {
            inputStream = new ContentLengthInputStream(inputStream, plaintextLength);
        }
        return inputStream;
    } catch (NoSuchAlgorithmException | InvalidKeyException e) {
        throw new AssertionError(e);
    } catch (InvalidMacException e) {
        throw new InvalidMessageException(e);
    }
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) ContentLengthInputStream(org.whispersystems.signalservice.internal.util.ContentLengthInputStream) ContentLengthInputStream(org.whispersystems.signalservice.internal.util.ContentLengthInputStream) FileInputStream(java.io.FileInputStream) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac) FileInputStream(java.io.FileInputStream) SecretKeySpec(javax.crypto.spec.SecretKeySpec) InvalidMacException(org.whispersystems.libsignal.InvalidMacException)

Example 2 with InvalidMacException

use of org.whispersystems.libsignal.InvalidMacException in project libsignal-service-java by signalapp.

the class AttachmentCipherInputStream method verifyMac.

private static void verifyMac(File file, Mac mac, byte[] theirDigest) throws FileNotFoundException, InvalidMacException {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA256");
        FileInputStream fin = new FileInputStream(file);
        int remainingData = Util.toIntExact(file.length()) - mac.getMacLength();
        byte[] buffer = new byte[4096];
        while (remainingData > 0) {
            int read = fin.read(buffer, 0, Math.min(buffer.length, remainingData));
            mac.update(buffer, 0, read);
            digest.update(buffer, 0, read);
            remainingData -= read;
        }
        byte[] ourMac = mac.doFinal();
        byte[] theirMac = new byte[mac.getMacLength()];
        Util.readFully(fin, theirMac);
        if (!MessageDigest.isEqual(ourMac, theirMac)) {
            throw new InvalidMacException("MAC doesn't match!");
        }
        byte[] ourDigest = digest.digest(theirMac);
        if (!MessageDigest.isEqual(ourDigest, theirDigest)) {
            throw new InvalidMacException("Digest doesn't match!");
        }
    } catch (IOException | ArithmeticException e1) {
        throw new InvalidMacException(e1);
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    }
}
Also used : InvalidMacException(org.whispersystems.libsignal.InvalidMacException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) FileInputStream(java.io.FileInputStream)

Example 3 with InvalidMacException

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

the class AttachmentCipherInputStream method createForAttachment.

public static InputStream createForAttachment(File file, long plaintextLength, byte[] combinedKeyMaterial, byte[] digest) throws InvalidMessageException, IOException {
    try {
        byte[][] parts = Util.split(combinedKeyMaterial, CIPHER_KEY_SIZE, MAC_KEY_SIZE);
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(parts[1], "HmacSHA256"));
        if (file.length() <= BLOCK_SIZE + mac.getMacLength()) {
            throw new InvalidMessageException("Message shorter than crypto overhead!");
        }
        if (digest == null) {
            throw new InvalidMacException("Missing digest!");
        }
        try (FileInputStream fin = new FileInputStream(file)) {
            verifyMac(fin, file.length(), mac, digest);
        }
        InputStream inputStream = new AttachmentCipherInputStream(new FileInputStream(file), parts[0], file.length() - BLOCK_SIZE - mac.getMacLength());
        if (plaintextLength != 0) {
            inputStream = new ContentLengthInputStream(inputStream, plaintextLength);
        }
        return inputStream;
    } catch (NoSuchAlgorithmException | InvalidKeyException e) {
        throw new AssertionError(e);
    } catch (InvalidMacException e) {
        throw new InvalidMessageException(e);
    }
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) ContentLengthInputStream(org.whispersystems.signalservice.internal.util.ContentLengthInputStream) ContentLengthInputStream(org.whispersystems.signalservice.internal.util.ContentLengthInputStream) FileInputStream(java.io.FileInputStream) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac) FileInputStream(java.io.FileInputStream) SecretKeySpec(javax.crypto.spec.SecretKeySpec) InvalidMacException(org.whispersystems.libsignal.InvalidMacException)

Example 4 with InvalidMacException

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

the class AttachmentCipherInputStream method createForStickerData.

public static InputStream createForStickerData(byte[] data, byte[] packKey) throws InvalidMessageException, IOException {
    try {
        byte[] combinedKeyMaterial = new HKDFv3().deriveSecrets(packKey, "Sticker Pack".getBytes(), 64);
        byte[][] parts = Util.split(combinedKeyMaterial, CIPHER_KEY_SIZE, MAC_KEY_SIZE);
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(parts[1], "HmacSHA256"));
        if (data.length <= BLOCK_SIZE + mac.getMacLength()) {
            throw new InvalidMessageException("Message shorter than crypto overhead!");
        }
        try (InputStream inputStream = new ByteArrayInputStream(data)) {
            verifyMac(inputStream, data.length, mac, null);
        }
        return new AttachmentCipherInputStream(new ByteArrayInputStream(data), parts[0], data.length - BLOCK_SIZE - mac.getMacLength());
    } catch (NoSuchAlgorithmException | InvalidKeyException e) {
        throw new AssertionError(e);
    } catch (InvalidMacException e) {
        throw new InvalidMessageException(e);
    }
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) ContentLengthInputStream(org.whispersystems.signalservice.internal.util.ContentLengthInputStream) FileInputStream(java.io.FileInputStream) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac) ByteArrayInputStream(java.io.ByteArrayInputStream) SecretKeySpec(javax.crypto.spec.SecretKeySpec) InvalidMacException(org.whispersystems.libsignal.InvalidMacException) HKDFv3(org.whispersystems.libsignal.kdf.HKDFv3)

Example 5 with InvalidMacException

use of org.whispersystems.libsignal.InvalidMacException in project libsignal-service-java by signalapp.

the class AttachmentCipherInputStream method verifyMac.

private static void verifyMac(InputStream inputStream, long length, Mac mac, byte[] theirDigest) throws InvalidMacException {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA256");
        int remainingData = Util.toIntExact(length) - mac.getMacLength();
        byte[] buffer = new byte[4096];
        while (remainingData > 0) {
            int read = inputStream.read(buffer, 0, Math.min(buffer.length, remainingData));
            mac.update(buffer, 0, read);
            digest.update(buffer, 0, read);
            remainingData -= read;
        }
        byte[] ourMac = mac.doFinal();
        byte[] theirMac = new byte[mac.getMacLength()];
        Util.readFully(inputStream, theirMac);
        if (!MessageDigest.isEqual(ourMac, theirMac)) {
            throw new InvalidMacException("MAC doesn't match!");
        }
        byte[] ourDigest = digest.digest(theirMac);
        if (theirDigest != null && !MessageDigest.isEqual(ourDigest, theirDigest)) {
            throw new InvalidMacException("Digest doesn't match!");
        }
    } catch (IOException | ArithmeticException e1) {
        throw new InvalidMacException(e1);
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    }
}
Also used : InvalidMacException(org.whispersystems.libsignal.InvalidMacException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)11 InvalidMacException (org.whispersystems.libsignal.InvalidMacException)11 FileInputStream (java.io.FileInputStream)8 FilterInputStream (java.io.FilterInputStream)7 InputStream (java.io.InputStream)7 InvalidKeyException (java.security.InvalidKeyException)7 Mac (javax.crypto.Mac)7 SecretKeySpec (javax.crypto.spec.SecretKeySpec)7 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)7 ContentLengthInputStream (org.whispersystems.signalservice.internal.util.ContentLengthInputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 IOException (java.io.IOException)4 MessageDigest (java.security.MessageDigest)4 HKDFv3 (org.whispersystems.libsignal.kdf.HKDFv3)3