Search in sources :

Example 11 with InvalidMacException

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

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

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