mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-03-04 08:18:44 +00:00
committed by
Moxie Marlinspike
parent
b7badec752
commit
0b62cf372b
@@ -44,24 +44,24 @@ import android.util.Log;
|
||||
*/
|
||||
|
||||
public class DecryptingPartInputStream extends FileInputStream {
|
||||
|
||||
|
||||
private static final int IV_LENGTH = 16;
|
||||
private static final int MAC_LENGTH = 20;
|
||||
|
||||
|
||||
private Cipher cipher;
|
||||
private Mac mac;
|
||||
|
||||
|
||||
private boolean done;
|
||||
private long totalDataSize;
|
||||
private long totalRead;
|
||||
private byte[] overflowBuffer;
|
||||
|
||||
|
||||
public DecryptingPartInputStream(File file, MasterSecret masterSecret) throws FileNotFoundException {
|
||||
super(file);
|
||||
try {
|
||||
if (file.length() <= IV_LENGTH + MAC_LENGTH)
|
||||
throw new FileNotFoundException("Part shorter than crypto overhead!");
|
||||
|
||||
|
||||
done = false;
|
||||
mac = initializeMac(masterSecret.getMacKey());
|
||||
cipher = initializeCipher(masterSecret.getEncryptionKey());
|
||||
@@ -81,12 +81,12 @@ public class DecryptingPartInputStream extends FileInputStream {
|
||||
throw new FileNotFoundException("IOException while reading IV!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int read(byte[] buffer) throws IOException {
|
||||
return read(buffer, 0, buffer.length);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int read(byte[] buffer, int offset, int length) throws IOException {
|
||||
if (totalRead != totalDataSize)
|
||||
@@ -96,11 +96,11 @@ public class DecryptingPartInputStream extends FileInputStream {
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
private int readFinal(byte[] buffer, int offset, int length) throws IOException {
|
||||
try {
|
||||
try {
|
||||
int flourish = cipher.doFinal(buffer, offset);
|
||||
// mac.update(buffer, offset, flourish);
|
||||
//mac.update(buffer, offset, flourish);
|
||||
|
||||
byte[] ourMac = mac.doFinal();
|
||||
byte[] theirMac = new byte[mac.getMacLength()];
|
||||
@@ -108,7 +108,7 @@ public class DecryptingPartInputStream extends FileInputStream {
|
||||
|
||||
if (!Arrays.equals(ourMac, theirMac))
|
||||
throw new IOException("MAC doesn't match! Potential tampering?");
|
||||
|
||||
|
||||
done = true;
|
||||
return flourish;
|
||||
} catch (IllegalBlockSizeException e) {
|
||||
@@ -122,7 +122,7 @@ public class DecryptingPartInputStream extends FileInputStream {
|
||||
throw new IOException("Bad padding exception!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private int readIncremental(byte[] buffer, int offset, int length) throws IOException {
|
||||
int readLength = 0;
|
||||
if (null != overflowBuffer) {
|
||||
@@ -145,11 +145,11 @@ public class DecryptingPartInputStream extends FileInputStream {
|
||||
|
||||
if (length + totalRead > totalDataSize)
|
||||
length = (int)(totalDataSize - totalRead);
|
||||
|
||||
|
||||
byte[] internalBuffer = new byte[length];
|
||||
int read = super.read(internalBuffer, 0, internalBuffer.length <= cipher.getBlockSize() ? internalBuffer.length : internalBuffer.length - cipher.getBlockSize());
|
||||
totalRead += read;
|
||||
|
||||
|
||||
try {
|
||||
mac.update(internalBuffer, 0, read);
|
||||
|
||||
@@ -175,41 +175,41 @@ public class DecryptingPartInputStream extends FileInputStream {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Mac initializeMac(SecretKeySpec key) throws NoSuchAlgorithmException, InvalidKeyException {
|
||||
Mac hmac = Mac.getInstance("HmacSHA1");
|
||||
hmac.init(key);
|
||||
|
||||
return hmac;
|
||||
}
|
||||
|
||||
|
||||
private Cipher initializeCipher(SecretKeySpec key)
|
||||
throws InvalidKeyException, InvalidAlgorithmParameterException,
|
||||
NoSuchAlgorithmException, NoSuchPaddingException, IOException
|
||||
NoSuchAlgorithmException, NoSuchPaddingException, IOException
|
||||
{
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
IvParameterSpec iv = readIv(cipher.getBlockSize());
|
||||
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
||||
|
||||
|
||||
return cipher;
|
||||
}
|
||||
|
||||
|
||||
private IvParameterSpec readIv(int size) throws IOException {
|
||||
byte[] iv = new byte[size];
|
||||
readFully(iv);
|
||||
|
||||
|
||||
mac.update(iv);
|
||||
return new IvParameterSpec(iv);
|
||||
}
|
||||
|
||||
|
||||
private void readFully(byte[] buffer) throws IOException {
|
||||
int offset = 0;
|
||||
|
||||
|
||||
for (;;) {
|
||||
int read = super.read(buffer, offset, buffer.length-offset);
|
||||
|
||||
|
||||
if (read + offset < buffer.length) offset += read;
|
||||
else return;
|
||||
}
|
||||
else return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user