From d4188c4a1c322d3fbf0b819ee16791ee08537d00 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Sun, 1 Dec 2013 18:00:12 -0800 Subject: [PATCH] SHA256 attachment MACs. --- .../textsecure/crypto/AttachmentCipher.java | 8 +++---- .../crypto/AttachmentCipherInputStream.java | 22 +++++++++---------- .../securesms/service/PushDownloader.java | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/library/src/org/whispersystems/textsecure/crypto/AttachmentCipher.java b/library/src/org/whispersystems/textsecure/crypto/AttachmentCipher.java index 4496f8653a..34edf0cb89 100644 --- a/library/src/org/whispersystems/textsecure/crypto/AttachmentCipher.java +++ b/library/src/org/whispersystems/textsecure/crypto/AttachmentCipher.java @@ -41,7 +41,7 @@ import java.util.Arrays; public class AttachmentCipher { static final int CIPHER_KEY_SIZE = 32; - static final int MAC_KEY_SIZE = 20; + static final int MAC_KEY_SIZE = 32; private final SecretKeySpec cipherKey; private final SecretKeySpec macKey; @@ -58,7 +58,7 @@ public class AttachmentCipher { public AttachmentCipher(byte[] combinedKeyMaterial) { byte[][] parts = Util.split(combinedKeyMaterial, CIPHER_KEY_SIZE, MAC_KEY_SIZE); this.cipherKey = new SecretKeySpec(parts[0], "AES"); - this.macKey = new SecretKeySpec(parts[1], "HmacSHA1"); + this.macKey = new SecretKeySpec(parts[1], "HmacSHA256"); this.cipher = initializeCipher(); this.mac = initializeMac(); } @@ -123,7 +123,7 @@ public class AttachmentCipher { private Mac initializeMac() { try { - Mac mac = Mac.getInstance("HmacSHA1"); + Mac mac = Mac.getInstance("HmacSHA256"); return mac; } catch (NoSuchAlgorithmException e) { throw new AssertionError(e); @@ -150,7 +150,7 @@ public class AttachmentCipher { private SecretKeySpec initializeRandomMacKey() { byte[] key = new byte[MAC_KEY_SIZE]; Util.getSecureRandom().nextBytes(key); - return new SecretKeySpec(key, "HmacSHA1"); + return new SecretKeySpec(key, "HmacSHA256"); } } diff --git a/library/src/org/whispersystems/textsecure/crypto/AttachmentCipherInputStream.java b/library/src/org/whispersystems/textsecure/crypto/AttachmentCipherInputStream.java index 753e4c80fd..7cea6e9cc9 100644 --- a/library/src/org/whispersystems/textsecure/crypto/AttachmentCipherInputStream.java +++ b/library/src/org/whispersystems/textsecure/crypto/AttachmentCipherInputStream.java @@ -18,17 +18,8 @@ package org.whispersystems.textsecure.crypto; import android.util.Log; -import org.whispersystems.textsecure.util.Hex; import org.whispersystems.textsecure.util.Util; -import javax.crypto.BadPaddingException; -import javax.crypto.Cipher; -import javax.crypto.IllegalBlockSizeException; -import javax.crypto.Mac; -import javax.crypto.NoSuchPaddingException; -import javax.crypto.ShortBufferException; -import javax.crypto.spec.IvParameterSpec; -import javax.crypto.spec.SecretKeySpec; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -38,6 +29,15 @@ import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.Arrays; +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.Mac; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.ShortBufferException; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; + /** * Class for streaming an encrypted push attachment off disk. * @@ -63,8 +63,8 @@ public class AttachmentCipherInputStream extends FileInputStream { AttachmentCipher.CIPHER_KEY_SIZE, AttachmentCipher.MAC_KEY_SIZE); - Mac mac = Mac.getInstance("HmacSHA1"); - mac.init(new SecretKeySpec(parts[1], "HmacSHA1")); + 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!"); diff --git a/src/org/thoughtcrime/securesms/service/PushDownloader.java b/src/org/thoughtcrime/securesms/service/PushDownloader.java index 7ae620b772..1aa49878da 100644 --- a/src/org/thoughtcrime/securesms/service/PushDownloader.java +++ b/src/org/thoughtcrime/securesms/service/PushDownloader.java @@ -37,7 +37,7 @@ public class PushDownloader { } public void process(MasterSecret masterSecret, Intent intent) { - if (!intent.getAction().equals(SendReceiveService.DOWNLOAD_PUSH_ACTION)) + if (!SendReceiveService.DOWNLOAD_PUSH_ACTION.equals(intent.getAction())) return; long messageId = intent.getLongExtra("message_id", -1);