Search in sources :

Example 1 with ContentLengthInputStream

use of org.whispersystems.signalservice.internal.util.ContentLengthInputStream 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 ContentLengthInputStream

use of org.whispersystems.signalservice.internal.util.ContentLengthInputStream 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)

Aggregations

FileInputStream (java.io.FileInputStream)2 FilterInputStream (java.io.FilterInputStream)2 InputStream (java.io.InputStream)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Mac (javax.crypto.Mac)2 SecretKeySpec (javax.crypto.spec.SecretKeySpec)2 InvalidMacException (org.whispersystems.libsignal.InvalidMacException)2 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)2 ContentLengthInputStream (org.whispersystems.signalservice.internal.util.ContentLengthInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1