Fixes
[iotcloud.git] / version2 / src / java / iotcloud / CloudComm.java
index d0a514d30cd5a3f12ce3c13efcf69b6e4873ebe1..c08d4b2e9db766fc49dae177a270c8ea0f4fbed1 100644 (file)
@@ -8,6 +8,9 @@ import javax.crypto.spec.*;
 import java.security.SecureRandom;
 import java.nio.ByteBuffer;
 
 import java.security.SecureRandom;
 import java.nio.ByteBuffer;
 
+
+import java.util.*;
+
 /**
  * This class provides a communication API to the webserver.  It also
  * validates the HMACs on the slots and handles encryption.
 /**
  * This class provides a communication API to the webserver.  It also
  * validates the HMACs on the slots and handles encryption.
@@ -18,7 +21,7 @@ import java.nio.ByteBuffer;
 
 class CloudComm {
        private static final int SALT_SIZE = 8;
 
 class CloudComm {
        private static final int SALT_SIZE = 8;
-       private static final int TIMEOUT_MILLIS = 2000; // 100
+       private static final int TIMEOUT_MILLIS = 5000; // 100
        public static final int IV_SIZE = 16;
 
        /** Sets the size for the HMAC. */
        public static final int IV_SIZE = 16;
 
        /** Sets the size for the HMAC. */
@@ -247,7 +250,8 @@ class CloudComm {
        private byte[] createIV(long machineId, long localSequenceNumber) {
                ByteBuffer buffer = ByteBuffer.allocate(IV_SIZE);
                buffer.putLong(machineId);
        private byte[] createIV(long machineId, long localSequenceNumber) {
                ByteBuffer buffer = ByteBuffer.allocate(IV_SIZE);
                buffer.putLong(machineId);
-               buffer.putLong(localSequenceNumber);
+               long localSequenceNumberShifted = localSequenceNumber << 16;
+               buffer.putLong(localSequenceNumberShifted);
                return buffer.array();
 
        }
                return buffer.array();
 
        }
@@ -255,7 +259,7 @@ class CloudComm {
        private byte[] encryptSlotAndPrependIV(byte[] rawData, byte[] ivBytes) {
                try {
                        IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        private byte[] encryptSlotAndPrependIV(byte[] rawData, byte[] ivBytes) {
                try {
                        IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
-                       Cipher cipher = Cipher.getInstance("AES/CTR/PKCS5Padding");
+                       Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
                        cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
 
                        byte[] encryptedBytes = cipher.doFinal(rawData);
                        cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
 
                        byte[] encryptedBytes = cipher.doFinal(rawData);
@@ -282,9 +286,8 @@ class CloudComm {
 
                        IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
 
 
                        IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
 
-                       Cipher cipher = Cipher.getInstance("AES/CTR/PKCS5Padding");
+                       Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
                        cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
                        cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
-
                        return cipher.doFinal(encryptedBytes);
 
                } catch (Exception e) {
                        return cipher.doFinal(encryptedBytes);
 
                } catch (Exception e) {