Bug 1350196 - Part 1: Generate IV ourselves. r=rnewman, r=nalexander, a=lizzard
authorGrigory Kruglov <gkruglov@mozilla.com>
Mon, 27 Mar 2017 18:40:02 -0700
changeset 395529 83289c6543562a83e7546d793e75332fa753ed6b
parent 395528 b8e229e6bed2cc43be188aaf6cd43de830e6929a
child 395530 437ac8a1ad2cdb94ba9b5573da06049855ea4a38
push id1468
push userasasaki@mozilla.com
push dateMon, 05 Jun 2017 19:31:07 +0000
treeherdermozilla-release@0641fc6ee9d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman, nalexander, lizzard
bugs1350196
milestone54.0a2
Bug 1350196 - Part 1: Generate IV ourselves. r=rnewman, r=nalexander, a=lizzard MozReview-Commit-ID: 5rR8BQRcJoR
mobile/android/services/src/main/java/org/mozilla/gecko/sync/crypto/CryptoInfo.java
--- a/mobile/android/services/src/main/java/org/mozilla/gecko/sync/crypto/CryptoInfo.java
+++ b/mobile/android/services/src/main/java/org/mozilla/gecko/sync/crypto/CryptoInfo.java
@@ -13,16 +13,17 @@ import javax.crypto.BadPaddingException;
 import javax.crypto.Cipher;
 import javax.crypto.IllegalBlockSizeException;
 import javax.crypto.Mac;
 import javax.crypto.NoSuchPaddingException;
 import javax.crypto.spec.IvParameterSpec;
 import javax.crypto.spec.SecretKeySpec;
 
 import org.mozilla.apache.commons.codec.binary.Base64;
+import org.mozilla.gecko.sync.Utils;
 
 /*
  * All info in these objects should be decoded (i.e. not BaseXX encoded).
  */
 public class CryptoInfo {
   private static final String TRANSFORMATION     = "AES/CBC/PKCS5Padding";
   private static final String KEY_ALGORITHM_SPEC = "AES";
 
@@ -151,19 +152,25 @@ public class CryptoInfo {
    */
   public void encrypt() throws CryptoException {
 
     Cipher cipher = CryptoInfo.getCipher(TRANSFORMATION);
     try {
       byte[] encryptionKey = getKeys().getEncryptionKey();
       SecretKeySpec spec = new SecretKeySpec(encryptionKey, KEY_ALGORITHM_SPEC);
 
-      // If no IV is provided, we allow the cipher to provide one.
+      // If no IV is provided, use a shared SecureRandom to generate one.
       if (getIV() == null || getIV().length == 0) {
-        cipher.init(Cipher.ENCRYPT_MODE, spec);
+        cipher.init(
+                Cipher.ENCRYPT_MODE,
+                spec,
+                new IvParameterSpec(
+                        Utils.generateRandomBytes(cipher.getBlockSize())
+                )
+        );
       } else {
         cipher.init(Cipher.ENCRYPT_MODE, spec, new IvParameterSpec(getIV()));
       }
     } catch (GeneralSecurityException ex) {
       throw new CryptoException(ex);
     }
 
     // Encrypt.