From d6fe7328e143215d77e672e5f13f6756c00512e9 Mon Sep 17 00:00:00 2001
From: rtrimana <rtrimana@uci.edu>
Date: Thu, 17 May 2018 17:00:53 -0700
Subject: [PATCH] Generating IV for every slot.

---
 version2/src/C/CloudComm.cpp | 28 +++++++++++-----------------
 version2/src/C/CloudComm.h   |  3 +--
 2 files changed, 12 insertions(+), 19 deletions(-)

diff --git a/version2/src/C/CloudComm.cpp b/version2/src/C/CloudComm.cpp
index 9af2bf6..a729ac2 100755
--- a/version2/src/C/CloudComm.cpp
+++ b/version2/src/C/CloudComm.cpp
@@ -27,7 +27,6 @@ CloudComm::CloudComm() :
 	password(NULL),
 	random(NULL),
 	salt(NULL),
-	ivArray(NULL),
 	table(NULL),
 	listeningPort(-1),
 	doEnd(false),
@@ -47,7 +46,6 @@ CloudComm::CloudComm(Table *_table,  IoTString *_baseurl, IoTString *_password,
 	password(_password->acquireRef()),
 	random(new SecureRandom()),
 	salt(NULL),
-	ivArray(NULL),
 	table(_table),
 	listeningPort(_listeningPort),
 	doEnd(false),
@@ -64,8 +62,6 @@ CloudComm::~CloudComm() {
 	delete putslot;
 	if (salt)
 		delete salt;
-	if (ivArray)
-		delete ivArray;
 	if (password)
 		password->releaseRef();
 	if (random)
@@ -451,11 +447,10 @@ Array<char> *CloudComm::createIV(int64_t machineId, int64_t localSequenceNumber)
 	return buffer->array();
 }
 
-void CloudComm::createIV() {
-	if (ivArray == NULL) {
-		ivArray = new Array<char>(CloudComm_IV_SIZE);
-		random->nextBytes(ivArray);
-	}
+Array<char> *CloudComm::createIV() {
+	Array<char> *ivArray = new Array<char>(CloudComm_IV_SIZE);
+	random->nextBytes(ivArray);
+	return ivArray;
 }
 
 Array<char> *AESEncrypt(Array<char> *ivBytes, AESKey *key, Array<char> *data) {
@@ -516,11 +511,10 @@ Array<Slot *> *CloudComm::putSlot(Slot *slot, int max) {
 
 		int64_t sequencenumber = slot->getSequenceNumber();
 		Array<char> *slotBytes = slot->encode(mac);
-		//Array<char> * ivBytes = slot->getSlotCryptIV();
-		//Array<char> *chars = encryptSlotAndPrependIV(slotBytes, ivBytes);
-		//delete ivBytes;
-		createIV();
-		Array<char> *chars = encryptSlotAndPrependIV(slotBytes, ivArray);
+		//Array<char> *ivBytes = slot->getSlotCryptIV();
+		Array<char> *ivBytes = createIV();
+		Array<char> *chars = encryptSlotAndPrependIV(slotBytes, ivBytes);
+		delete ivBytes;
 		delete slotBytes;
 		IoTString *url = buildRequest(true, sequencenumber, max);
 		timer->startTime();
@@ -667,9 +661,9 @@ Array<char> *CloudComm::sendLocalData(Array<char> *sendData, int64_t localSequen
 
 		// Encrypt the data for sending
 		//Array<char> *iv = createIV(table->getMachineId(), table->getLocalSequenceNumber());
-		//Array<char> *encryptedData = encryptSlotAndPrependIV(totalData, iv);
-		createIV();
-		Array<char> *encryptedData = encryptSlotAndPrependIV(totalData, ivArray);
+		Array<char> *iv = createIV();
+		Array<char> *encryptedData = encryptSlotAndPrependIV(totalData, iv);
+		delete iv;
 
 		// Open a TCP socket connection to a local device
 		TCPClient socket = createSocket(host, port);
diff --git a/version2/src/C/CloudComm.h b/version2/src/C/CloudComm.h
index 218fb4e..1d81c2b 100755
--- a/version2/src/C/CloudComm.h
+++ b/version2/src/C/CloudComm.h
@@ -34,7 +34,6 @@ private:
 	IoTString *password;
 	SecureRandom *random;
 	Array<char> *salt;
-	Array<char> *ivArray;
 	Table *table;
 	int32_t listeningPort;
 	//	pthread_t localServerThread;
@@ -60,7 +59,7 @@ private:
 	void setSalt();
 	bool getSalt();
 	Array<char> *createIV(int64_t machineId, int64_t localSequenceNumber);
-	void createIV();
+	Array<char> *createIV();
 	Array<char> *encryptSlotAndPrependIV(Array<char> *rawData, Array<char> *ivBytes);
 	Array<char> *stripIVAndDecryptSlot(Array<char> *rawData);
 	Array<Slot *> *processSlots(WebConnection *wc);
-- 
2.34.1