Generating IV from random numbers, not machine ID and local sequence number.
authorrtrimana <rtrimana@uci.edu>
Thu, 17 May 2018 15:22:35 +0000 (08:22 -0700)
committerrtrimana <rtrimana@uci.edu>
Thu, 17 May 2018 15:22:35 +0000 (08:22 -0700)
version2/src/C/CloudComm.cpp
version2/src/C/CloudComm.h

index 45b7dace5b84e055b5785384731dccd4def6ccf3..d9866166d2cbfd2b9a93fbb2c378ee3dbd1bcc9a 100644 (file)
@@ -27,6 +27,7 @@ CloudComm::CloudComm() :
        password(NULL),
        random(NULL),
        salt(NULL),
        password(NULL),
        random(NULL),
        salt(NULL),
+       ivArray(NULL),
        table(NULL),
        listeningPort(-1),
        doEnd(false),
        table(NULL),
        listeningPort(-1),
        doEnd(false),
@@ -52,6 +53,7 @@ CloudComm::CloudComm(Table *_table,  IoTString *_baseurl, IoTString *_password,
        password(_password->acquireRef()),
        random(new SecureRandom()),
        salt(NULL),
        password(_password->acquireRef()),
        random(new SecureRandom()),
        salt(NULL),
+       ivArray(NULL),
        table(_table),
        listeningPort(_listeningPort),
        doEnd(false),
        table(_table),
        listeningPort(_listeningPort),
        doEnd(false),
@@ -68,6 +70,8 @@ CloudComm::~CloudComm() {
        delete putslot;
        if (salt)
                delete salt;
        delete putslot;
        if (salt)
                delete salt;
+       if (ivArray)
+               delete ivArray;
        if (password)
                password->releaseRef();
        if (random)
        if (password)
                password->releaseRef();
        if (random)
@@ -116,12 +120,12 @@ void CloudComm::initCrypt() {
        if (password == NULL) {
                return;
        }
        if (password == NULL) {
                return;
        }
-       try {
+       try {   
                key = initKey();
                password->releaseRef();
                password = NULL;// drop password
                mac = new Mac();
                key = initKey();
                password->releaseRef();
                password = NULL;// drop password
                mac = new Mac();
-               mac->init(key);
+               mac->init(key);         
        } catch (Exception *e) {
                throw new Error("Failed To Initialize Ciphers");
        }
        } catch (Exception *e) {
                throw new Error("Failed To Initialize Ciphers");
        }
@@ -504,6 +508,14 @@ Array<char> *CloudComm::createIV(int64_t machineId, int64_t localSequenceNumber)
        return buffer->array();
 }
 
        return buffer->array();
 }
 
+void CloudComm::createIV() {
+       if (ivArray == NULL) {
+               ivArray = new Array<char>(CloudComm_IV_SIZE);
+               random->nextBytes(ivArray);
+               printf("Random bytes is generated: %d\n", ivArray->length());
+       }
+}
+
 Array<char> *AESEncrypt(Array<char> *ivBytes, AESKey *key, Array<char> *data) {
        Array<char> *output = new Array<char>(data->length());
        aes_encrypt_ctr((BYTE *)data->internalArray(), data->length(), (BYTE *) output->internalArray(), (WORD *)key->getKeySchedule(), key->getKey()->length() * 8, (BYTE *)ivBytes->internalArray());
 Array<char> *AESEncrypt(Array<char> *ivBytes, AESKey *key, Array<char> *data) {
        Array<char> *output = new Array<char>(data->length());
        aes_encrypt_ctr((BYTE *)data->internalArray(), data->length(), (BYTE *) output->internalArray(), (WORD *)key->getKeySchedule(), key->getKey()->length() * 8, (BYTE *)ivBytes->internalArray());
@@ -561,9 +573,11 @@ Array<Slot *> *CloudComm::putSlot(Slot *slot, int max) {
 
                int64_t sequencenumber = slot->getSequenceNumber();
                Array<char> *slotBytes = slot->encode(mac);
 
                int64_t sequencenumber = slot->getSequenceNumber();
                Array<char> *slotBytes = slot->encode(mac);
-               Array<char> * ivBytes = slot->getSlotCryptIV();
-               Array<char> *chars = encryptSlotAndPrependIV(slotBytes, ivBytes);
-               delete ivBytes;
+               //Array<char> *ivBytes = slot->getSlotCryptIV();
+               //Array<char> *chars = encryptSlotAndPrependIV(slotBytes, ivBytes);
+               //delete ivBytes;
+               createIV();
+               Array<char> *chars = encryptSlotAndPrependIV(slotBytes, ivArray);
                delete slotBytes;
                IoTString *url = buildRequest(true, sequencenumber, max);
                timer->startTime();
                delete slotBytes;
                IoTString *url = buildRequest(true, sequencenumber, max);
                timer->startTime();
@@ -623,33 +637,32 @@ Array<Slot *> *CloudComm::putSlot(Slot *slot, int max) {
  * sequencenumber or newer->
  */
 Array<Slot *> *CloudComm::getSlots(int64_t sequencenumber) {
  * sequencenumber or newer->
  */
 Array<Slot *> *CloudComm::getSlots(int64_t sequencenumber) {
+
        WebConnection wc = {-1, -1};
        try {
                if (salt == NULL) {
                        if (!getSalt()) {
                                throw new ServerException("getSlots failed", ServerException_TypeSalt);
        WebConnection wc = {-1, -1};
        try {
                if (salt == NULL) {
                        if (!getSalt()) {
                                throw new ServerException("getSlots failed", ServerException_TypeSalt);
-                       }
+                       }               
                        initCrypt();
                        initCrypt();
-               }
-
+               }               
                IoTString *url = buildRequest(false, sequencenumber, 0);
                timer->startTime();
                wc = openURL(url);
                delete url;
                closeURLReq(&wc);
                IoTString *url = buildRequest(false, sequencenumber, 0);
                timer->startTime();
                wc = openURL(url);
                delete url;
                closeURLReq(&wc);
-               timer->endTime();
+               timer->endTime();               
        } catch (SocketTimeoutException *e) {
                timer->endTime();
                throw new ServerException("getSlots failed", ServerException_TypeConnectTimeout);
        } catch (ServerException *e) {
                timer->endTime();
        } catch (SocketTimeoutException *e) {
                timer->endTime();
                throw new ServerException("getSlots failed", ServerException_TypeConnectTimeout);
        } catch (ServerException *e) {
                timer->endTime();
-
                throw e;
        } catch (Exception *e) {
                throw new Error("getSlots failed");
        }
 
                throw e;
        } catch (Exception *e) {
                throw new Error("getSlots failed");
        }
 
-       try {
+       try {   
                timer->startTime();
                int responsecode = getResponseCode(&wc);
                readHeaders(&wc);
                timer->startTime();
                int responsecode = getResponseCode(&wc);
                readHeaders(&wc);
@@ -661,7 +674,7 @@ Array<Slot *> *CloudComm::getSlots(int64_t sequencenumber) {
 
                delete resptype;
                Array<Slot *> *tmp = processSlots(&wc);
 
                delete resptype;
                Array<Slot *> *tmp = processSlots(&wc);
-               close(wc.fd);
+               close(wc.fd);           
                return tmp;
        } catch (SocketTimeoutException *e) {
                timer->endTime();
                return tmp;
        } catch (SocketTimeoutException *e) {
                timer->endTime();
@@ -707,8 +720,10 @@ Array<char> *CloudComm::sendLocalData(Array<char> *sendData, int64_t localSequen
                System_arraycopy(genmac, 0, totalData, sendData->length(), genmac->length());
 
                // Encrypt the data for sending
                System_arraycopy(genmac, 0, totalData, sendData->length(), genmac->length());
 
                // Encrypt the data for sending
-               Array<char> *iv = createIV(table->getMachineId(), table->getLocalSequenceNumber());
-               Array<char> *encryptedData = encryptSlotAndPrependIV(totalData, iv);
+               //Array<char> *iv = createIV(table->getMachineId(), table->getLocalSequenceNumber());
+               //Array<char> *encryptedData = encryptSlotAndPrependIV(totalData, iv);
+               createIV();
+               Array<char> *encryptedData = encryptSlotAndPrependIV(totalData, ivArray);
 
                // Open a TCP socket connection to a local device
                int socket = createSocket(host, port);
 
                // Open a TCP socket connection to a local device
                int socket = createSocket(host, port);
@@ -788,8 +803,10 @@ void CloudComm::localServerWorkerFunction() {
                        System_arraycopy(realmac, 0, totalData, sendData->length(), realmac->length());
 
                        // Encrypt the data for sending
                        System_arraycopy(realmac, 0, totalData, sendData->length(), realmac->length());
 
                        // Encrypt the data for sending
-                       Array<char> *iv = createIV(table->getMachineId(), table->getLocalSequenceNumber());
-                       Array<char> *encryptedData = encryptSlotAndPrependIV(totalData, iv);
+                       //Array<char> *iv = createIV(table->getMachineId(), table->getLocalSequenceNumber());
+                       //Array<char> *encryptedData = encryptSlotAndPrependIV(totalData, iv);
+                       createIV();
+                       Array<char> *encryptedData = encryptSlotAndPrependIV(totalData, ivArray);
 
                        timer->startTime();
                        // Send data to output (length of data, the data)
 
                        timer->startTime();
                        // Send data to output (length of data, the data)
index ce7cfa07023a3832252f2633cd1973b4b35e1541..e782d3090b2253a6a882e3c2ae7ae919f1fec5bb 100644 (file)
@@ -32,6 +32,7 @@ private:
        IoTString *password;
        SecureRandom *random;
        Array<char> *salt;
        IoTString *password;
        SecureRandom *random;
        Array<char> *salt;
+       Array<char> *ivArray;
        Table *table;
        int32_t listeningPort;
        pthread_t localServerThread;
        Table *table;
        int32_t listeningPort;
        pthread_t localServerThread;
@@ -57,6 +58,7 @@ private:
        void setSalt();
        bool getSalt();
        Array<char> *createIV(int64_t machineId, int64_t localSequenceNumber);
        void setSalt();
        bool getSalt();
        Array<char> *createIV(int64_t machineId, int64_t localSequenceNumber);
+       void createIV();
        Array<char> *encryptSlotAndPrependIV(Array<char> *rawData, Array<char> *ivBytes);
        Array<char> *stripIVAndDecryptSlot(Array<char> *rawData);
        Array<Slot *> *processSlots(WebConnection *wc);
        Array<char> *encryptSlotAndPrependIV(Array<char> *rawData, Array<char> *ivBytes);
        Array<char> *stripIVAndDecryptSlot(Array<char> *rawData);
        Array<Slot *> *processSlots(WebConnection *wc);