Generating IV from random numbers, and not from machine ID and local sequence number.
[iotcloud.git] / version2 / src / java / iotcloud / CloudComm.java
index f12c2764e0a04520c94b6a094fd25c43266cf4f0..b5329f544cc683a5834665a8301d562d8296e520 100644 (file)
@@ -33,6 +33,7 @@ class CloudComm {
        private String password;
        private SecureRandom random;
        private byte salt[];
+       private byte iv[] = null;
        private Table table;
        private int listeningPort = -1;
        private Thread localServerThread = null;
@@ -247,6 +248,9 @@ class CloudComm {
                }
        }
 
+       /**
+        * Generate random numbers for IV from machine ID and local sequence number
+        */
        private byte[] createIV(long machineId, long localSequenceNumber) {
                ByteBuffer buffer = ByteBuffer.allocate(IV_SIZE);
                buffer.putLong(machineId);
@@ -255,6 +259,17 @@ class CloudComm {
                return buffer.array();
 
        }
+       
+       /**
+        * Generate random numbers for IV from random bits
+        */
+       private byte[] createIV() {
+               if (iv == null) {
+                       iv = new byte[IV_SIZE];
+                       random.nextBytes(iv);
+               }
+               return iv;
+       }
 
        private byte[] encryptSlotAndPrependIV(byte[] rawData, byte[] ivBytes) {
                try {
@@ -324,9 +339,7 @@ class CloudComm {
                        // byte[] bytes = new byte[slotBytes.length + IV_SIZE];
                        // System.arraycopy(iVBytes, 0, bytes, 0, iVBytes.length);
                        // System.arraycopy(slotBytes, 0, bytes, IV_SIZE, slotBytes.length);
-
-
-                       byte[] bytes = encryptSlotAndPrependIV(slotBytes, slot.getSlotCryptIV());
+                       byte[] bytes = encryptSlotAndPrependIV(slotBytes, createIV());
 
                        url = buildRequest(true, sequencenumber, max);
 
@@ -404,7 +417,6 @@ class CloudComm {
                                }
                                initCrypt();
                        }
-
                        url = buildRequest(false, sequencenumber, 0);
                        timer.startTime();
                        con = url.openConnection();
@@ -414,7 +426,6 @@ class CloudComm {
                        http.setReadTimeout(TIMEOUT_MILLIS);
 
 
-
                        http.connect();
                        timer.endTime();
 
@@ -424,7 +435,6 @@ class CloudComm {
                        throw new ServerException("getSlots failed", ServerException.TypeConnectTimeout);
                } catch (ServerException e) {
                        timer.endTime();
-
                        throw e;
                } catch (Exception e) {
                        // e.printStackTrace();
@@ -432,7 +442,6 @@ class CloudComm {
                }
 
                try {
-
                        timer.startTime();
                        InputStream is = http.getInputStream();
                        DataInputStream dis = new DataInputStream(is);
@@ -504,7 +513,7 @@ class CloudComm {
                        // Encrypt the data for sending
                        // byte[] encryptedData = encryptCipher.doFinal(totalData);
                        // byte[] encryptedData = encryptCipher.doFinal(totalData);
-                       byte[] iv = createIV(table.getMachineId(), table.getLocalSequenceNumber());
+                       byte[] iv = createIV();
                        byte[] encryptedData = encryptSlotAndPrependIV(totalData, iv);
 
                        // Open a TCP socket connection to a local device
@@ -612,7 +621,7 @@ class CloudComm {
 
                                // Encrypt the data for sending
                                // byte[] encryptedData = encryptCipher.doFinal(totalData);
-                               byte[] iv = createIV(table.getMachineId(), table.getLocalSequenceNumber());
+                               byte[] iv = createIV();
                                byte[] encryptedData = encryptSlotAndPrependIV(totalData, iv);