From bb413f35b364cd0314b41300057ecb887c61a2d8 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Thu, 17 May 2018 09:00:25 -0700 Subject: [PATCH] Generating IV from random numbers, and not from machine ID and local sequence number. --- version2/src/java/iotcloud/CloudComm.java | 27 +++++++++++++++-------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/version2/src/java/iotcloud/CloudComm.java b/version2/src/java/iotcloud/CloudComm.java index f12c276..b5329f5 100644 --- a/version2/src/java/iotcloud/CloudComm.java +++ b/version2/src/java/iotcloud/CloudComm.java @@ -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); -- 2.34.1