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

index 989803918191ab5cddcfa4f86bec584e2977bb3f..9af2bf69bf89189bdf2dbc7c2d14af5898c0f42f 100755 (executable)
@@ -27,6 +27,7 @@ CloudComm::CloudComm() :
        password(NULL),
        random(NULL),
        salt(NULL),
+       ivArray(NULL),
        table(NULL),
        listeningPort(-1),
        doEnd(false),
@@ -46,6 +47,7 @@ CloudComm::CloudComm(Table *_table,  IoTString *_baseurl, IoTString *_password,
        password(_password->acquireRef()),
        random(new SecureRandom()),
        salt(NULL),
+       ivArray(NULL),
        table(_table),
        listeningPort(_listeningPort),
        doEnd(false),
@@ -62,6 +64,8 @@ CloudComm::~CloudComm() {
        delete putslot;
        if (salt)
                delete salt;
+       if (ivArray)
+               delete ivArray;
        if (password)
                password->releaseRef();
        if (random)
@@ -447,6 +451,13 @@ 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> *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());
@@ -505,9 +516,11 @@ 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;
+               //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();
@@ -653,8 +666,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
-               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
                TCPClient socket = createSocket(host, port);
index 35d5fcce68ad200eba75fd8d6427ce73be13025a..218fb4e25e20319109da2f04acc280e83a3d4444 100755 (executable)
@@ -34,6 +34,7 @@ private:
        IoTString *password;
        SecureRandom *random;
        Array<char> *salt;
+       Array<char> *ivArray;
        Table *table;
        int32_t listeningPort;
        //      pthread_t localServerThread;
@@ -59,6 +60,7 @@ private:
        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);