From 31ae9403c2ed05be4ab25d0e2804bbfb99f43f2e Mon Sep 17 00:00:00 2001 From: bdemsky Date: Thu, 29 Mar 2018 11:27:55 -0700 Subject: [PATCH] edits --- version2/src/C/CloudComm.cpp | 103 +++---------------------------- version2/src/C/SecureRandom.cpp | 8 ++- version2/src/C/Test.ino | 29 +++------ version2/src/C/TimingSingleton.h | 8 +-- version2/src/C/Transaction.cpp | 6 +- version2/src/C/common.h | 6 +- version2/src/C/hashtable.h | 1 - version2/src/C/pbkdf2-sha256.cpp | 32 +++++----- 8 files changed, 47 insertions(+), 146 deletions(-) diff --git a/version2/src/C/CloudComm.cpp b/version2/src/C/CloudComm.cpp index 3e649c3..9ecbd97 100755 --- a/version2/src/C/CloudComm.cpp +++ b/version2/src/C/CloudComm.cpp @@ -36,12 +36,6 @@ CloudComm::CloudComm() : { } -void *threadWrapper(void *cloud) { - CloudComm *c = (CloudComm *) cloud; - c->localServerWorkerFunction(); - return NULL; -} - /** * Constructor for actual use. Takes in the url and password. */ @@ -143,7 +137,7 @@ void loopWrite(TCPClient * client, char *array, int bytestowrite) { byteswritten += bytes; bytestowrite -= bytes; } else { - printf("Error in write\n"); + //printf("Error in write\n"); exit(-1); } } @@ -157,7 +151,7 @@ void loopRead(TCPClient * client, char *array, int bytestoread) { bytesread += bytes; bytestoread -= bytes; } else { - printf("Error in read\n"); + //printf("Error in read\n"); exit(-1); } } @@ -165,7 +159,7 @@ void loopRead(TCPClient * client, char *array, int bytestoread) { WebConnection openURL(IoTString *url) { if (url->length() < 7 || memcmp(url->internalBytes()->internalArray(), "http://", 7)) { - printf("BOGUS URL\n"); + //printf("BOGUS URL\n"); exit(-1); } int i = 7; @@ -174,14 +168,14 @@ WebConnection openURL(IoTString *url) { break; if ( i == url->length()) { - printf("ERROR in openURL\n"); + //printf("ERROR in openURL\n"); exit(-1); } char *host = (char *) malloc(i - 6); memcpy(host, &url->internalBytes()->internalArray()[7], i - 7); host[i - 7] = 0; - printf("%s\n", host); + //printf("%s\n", host); char *message = (char *)malloc(sizeof("POST HTTP/1.1\r\n") + sizeof("Host: \r\n") + 2 * url->length()); @@ -215,12 +209,7 @@ TCPClient createSocket(IoTString *name, int port) { char *host = (char *) malloc(name->length() + 1); memcpy(host, name->internalBytes()->internalArray(), name->length()); host[name->length()] = 0; - printf("%s\n", host); - /* How big is the message? */ - - /* create the socket */ - int sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (sockfd < 0) {printf("ERROR opening socket\n"); exit(-1);} + //printf("%s\n", host); /* lookup the ip address */ TCPClient client; @@ -232,15 +221,6 @@ TCPClient createSocket(IoTString *name, int port) { return client; } -TCPServer createSocket(int port) { - int fd; - TCPServer server = TCPServer(port); - server.begin(); - - return server; -} - - void writeSocketData(TCPClient * fd, Array *data) { loopWrite(fd, data->internalArray(), data->length()); } @@ -299,7 +279,7 @@ void readLine(WebConnection *wc, char *response, int numBytes) { if (bytes <= 0) break; if (offset == (numBytes - 1)) { - printf("Response too long"); + //printf("Response too long"); exit(-1); } response[offset++] = newchar; @@ -314,7 +294,7 @@ int getResponseCode(WebConnection *wc) { readLine(wc, response, sizeof(response)); int ver1 = 0, ver2 = 0, respcode = 0; sscanf(response, "HTTP/%d.%d %d", &ver1, &ver2, &respcode); - printf("Response code %d\n", respcode); + //printf("Response code %d\n", respcode); return respcode; } @@ -642,7 +622,7 @@ Array *CloudComm::sendLocalData(Array *sendData, int64_t localSequen if (salt == NULL) return NULL; // try { - printf("Passing Locally\n"); + //printf("Passing Locally\n"); mac->update(sendData, 0, sendData->length()); Array *genmac = mac->doFinal(); Array *totalData = new Array(sendData->length() + genmac->length()); @@ -689,71 +669,6 @@ Array *CloudComm::sendLocalData(Array *sendData, int64_t localSequen return NULL; } -void CloudComm::localServerWorkerFunction() { - /* int inputSocket = -1; - - try { - // Local server socket - inputSocket = createSocket(listeningPort); - } catch (Exception *e) { - throw new Error("Local server setup failure..."); - } - - while (!doEnd) { - try { - // Accept incoming socket - int socket = acceptSocket(inputSocket); - - // Get the encrypted data from the server - int dataSize = readSocketInt(socket); - Array *readData = new Array(dataSize); - readSocketData(socket, readData); - timer->endTime(); - - // Decrypt the data - readData = stripIVAndDecryptSlot(readData); - mac->update(readData, 0, readData->length() - CloudComm_HMAC_SIZE); - Array *genmac = mac->doFinal(); - Array *recmac = new Array(CloudComm_HMAC_SIZE); - System_arraycopy(readData, readData->length() - recmac->length(), recmac, 0, recmac->length()); - - if (!recmac->equals(genmac)) - // throw new Error("Local Error: Invalid HMAC! Potential Attack!"); - error("Local Error: Invalid HMAC! Potential Attack!\n"); - - Array *returnData = new Array(readData->length() - recmac->length()); - System_arraycopy(readData, 0, returnData, 0, returnData->length()); - - // Process the data - Array *sendData = table->acceptDataFromLocal(returnData); - mac->update(sendData, 0, sendData->length()); - Array *realmac = mac->doFinal(); - Array *totalData = new Array(sendData->length() + realmac->length()); - System_arraycopy(sendData, 0, totalData, 0, sendData->length()); - System_arraycopy(realmac, 0, totalData, sendData->length(), realmac->length()); - - // Encrypt the data for sending - Array *iv = createIV(table->getMachineId(), table->getLocalSequenceNumber()); - Array *encryptedData = encryptSlotAndPrependIV(totalData, iv); - - timer->startTime(); - // Send data to output (length of data, the data) - writeSocketInt(socket, encryptedData->length()); - writeSocketData(socket, encryptedData); - close(socket); - } catch (Exception *e) { - } - } - - if (inputSocket != -1) { - try { - close(inputSocket); - } catch (Exception *e) { - throw new Error("Local server close failure..."); - } - }*/ -} - void CloudComm::closeCloud() { doEnd = true; diff --git a/version2/src/C/SecureRandom.cpp b/version2/src/C/SecureRandom.cpp index 9336b33..6cf331b 100755 --- a/version2/src/C/SecureRandom.cpp +++ b/version2/src/C/SecureRandom.cpp @@ -1,14 +1,18 @@ #include "SecureRandom.h" #include +#include "application.h" //#include SecureRandom::SecureRandom() { } void SecureRandom::nextBytes(Array *array) { - arc4random_buf(array->internalArray(), array->length()); + uint len = array->length(); + for(uint i=0; iinternalArray()[i] = (char) random(256); + } } int32_t SecureRandom::nextInt(int32_t val) { - return arc4random_uniform(val); + return random(val); } diff --git a/version2/src/C/Test.ino b/version2/src/C/Test.ino index 4378081..1d31229 100644 --- a/version2/src/C/Test.ino +++ b/version2/src/C/Test.ino @@ -20,18 +20,14 @@ void setup() { IoTString * password = new IoTString("reallysecret"); t1 = new Table(baseurl, password, 321, -1); t1->initTable(); - printf("T1 Ready\n"); t2 = new Table(baseurl, password, 351, -1); t2->update(); - printf("T2 Ready\n"); delete baseurl; delete password; // Make the Keys - printf("Setting up keys\n"); for (int i = 0; i < NUMBER_OF_TESTS; i++) { - printf("%d\n",i); char buffer[80]; sprintf(buffer, "a%d", i); IoTString *ia = new IoTString(buffer); @@ -56,9 +52,7 @@ void setup() { void loop() { // Do Updates for the keys - printf("Setting Key-Values...\n"); for (int i = 0; i < NUMBER_OF_TESTS; i++) { - printf("%d\n", i); char buffer[80]; sprintf(buffer, "a%d", i); IoTString * iKeyA = new IoTString(buffer); @@ -97,11 +91,9 @@ void loop() { delete iKeyD; delete iValueD; } - printf("Updating Clients...\n"); t1->update(); t2->update(); - printf("Checking Key-Values...\n"); for (int i = 0; i < NUMBER_OF_TESTS; i++) { char buffer[80]; sprintf(buffer, "a%d", i); @@ -131,42 +123,42 @@ void loop() { IoTString *testValD2 = t2->getCommitted(iKeyD); if ((testValA1 == NULL) || (testValA1->equals(iValueA) == false)) { - printf("Key-Value t1 incorrect: keyA\n"); + // printf("Key-Value t1 incorrect: keyA\n"); foundError = true; } if ((testValB1 == NULL) || (testValB1->equals(iValueB) == false)) { - printf("Key-Value t1 incorrect: keyB\n"); + // printf("Key-Value t1 incorrect: keyB\n"); foundError = true; } if ((testValC1 == NULL) || (testValC1->equals(iValueC) == false)) { - printf("Key-Value t1 incorrect: keyC\n"); + // printf("Key-Value t1 incorrect: keyC\n"); foundError = true; } if ((testValD1 == NULL) || (testValD1->equals(iValueD) == false)) { - printf("Key-Value t1 incorrect: keyD\n"); + // printf("Key-Value t1 incorrect: keyD\n"); foundError = true; } if ((testValA2 == NULL) || (testValA2->equals(iValueA) == false)) { - printf("Key-Value t2 incorrect: keyA testValA2\n"); + // printf("Key-Value t2 incorrect: keyA testValA2\n"); foundError = true; } if ((testValB2 == NULL) || (testValB2->equals(iValueB) == false)) { - printf("Key-Value t2 incorrect: keyB testValB2\n"); +// printf("Key-Value t2 incorrect: keyB testValB2\n"); foundError = true; } if ((testValC2 == NULL) || (testValC2->equals(iValueC) == false)) { - printf("Key-Value t2 incorrect: keyC testValC2\n"); + // printf("Key-Value t2 incorrect: keyC testValC2\n"); foundError = true; } if ((testValD2 == NULL) || (testValD2->equals(iValueD) == false)) { - printf("Key-Value t2 incorrect: keyD testValD2\n"); +// printf("Key-Value t2 incorrect: keyD testValD2\n"); foundError = true; } delete iKeyA; delete iValueA; @@ -183,15 +175,14 @@ void loop() { TransactionStatus * status = transStatusList->get(i); if (status->getStatus() != TransactionStatus_StatusCommitted) { foundError = true; - printf("Status error\n"); +// printf("Status error\n"); } delete status; } if (foundError) { - printf("Found Errors...\n"); +// printf("Found Errors...\n"); } else { - printf("No Errors Found...\n"); } transStatusList->clear(); diff --git a/version2/src/C/TimingSingleton.h b/version2/src/C/TimingSingleton.h index c9189c2..945f90a 100755 --- a/version2/src/C/TimingSingleton.h +++ b/version2/src/C/TimingSingleton.h @@ -15,8 +15,7 @@ public: int64_t nanoTime() { int64_t time; struct timeval tv; - gettimeofday(&tv, NULL); - return tv.tv_sec * 1000000000 + tv.tv_usec * 1000; + return 0; } void startTime() { @@ -32,8 +31,5 @@ public: } }; -TimingSingleton t_singleton; -TimingSingleton *TimingSingleton_getInstance() { - return &t_singleton; -} +TimingSingleton * TimingSingleton_getInstance(); #endif diff --git a/version2/src/C/Transaction.cpp b/version2/src/C/Transaction.cpp index e11bbc8..ed31b77 100755 --- a/version2/src/C/Transaction.cpp +++ b/version2/src/C/Transaction.cpp @@ -58,7 +58,7 @@ Transaction::~Transaction() { void Transaction::addPartEncode(TransactionPart *newPart) { newPart->acquireRef(); - printf("Add part %d\n", newPart->getPartNumber()); + //printf("Add part %d\n", newPart->getPartNumber()); TransactionPart *old = parts->setExpand(newPart->getPartNumber(), newPart); if (old == NULL) { partCount++; @@ -327,9 +327,9 @@ bool Transaction::evaluateGuard(HashtablegetKey()->internalBytes()->internalArray(), kv->getValue()->internalBytes()->internalArray()); + //printf("%s %s\n", kvGuard->getKey()->internalBytes()->internalArray(), kv->getValue()->internalBytes()->internalArray()); } else { - printf("%s null\n", kvGuard->getValue()->internalBytes()->internalArray()); + //printf("%s null\n", kvGuard->getValue()->internalBytes()->internalArray()); } delete kvit; return false; diff --git a/version2/src/C/common.h b/version2/src/C/common.h index 134c13b..d6aff0a 100755 --- a/version2/src/C/common.h +++ b/version2/src/C/common.h @@ -4,13 +4,10 @@ //typedef uint32_t uint; #define CMEMALLOC ; -#define model_print printf #define ASSERT(expr) \ do { \ if (!(expr)) { \ - fprintf(stderr, "Error: assertion failed in %s at line %d\n", __FILE__, __LINE__); \ - /* print_trace(); // Trace printing may cause dynamic memory allocation */ \ - exit(EXIT_FAILURE); \ + exit(EXIT_FAILURE); \ } \ } while (0) @@ -20,7 +17,6 @@ #define myerror(msg) \ do { \ - printf(msg); \ exit(-1); \ } while(0) diff --git a/version2/src/C/hashtable.h b/version2/src/C/hashtable.h index 868060c..e828746 100755 --- a/version2/src/C/hashtable.h +++ b/version2/src/C/hashtable.h @@ -412,7 +412,6 @@ public: unsigned int oldcapacity = capacity; if ((newtable = (struct Hashlistnode<_Key, _Val> *)ourcalloc(newsize, sizeof(struct Hashlistnode<_Key, _Val>))) == NULL) { - model_print("calloc error %s %d\n", __FILE__, __LINE__); exit(EXIT_FAILURE); } diff --git a/version2/src/C/pbkdf2-sha256.cpp b/version2/src/C/pbkdf2-sha256.cpp index 2a78fa0..0115640 100755 --- a/version2/src/C/pbkdf2-sha256.cpp +++ b/version2/src/C/pbkdf2-sha256.cpp @@ -649,7 +649,6 @@ typedef struct { int do_test(testvector *tv) { - printf("Started %s\n", tv->t); fflush(stdout); char *key = malloc(tv->dkLen); if (key == 0) { @@ -684,7 +683,8 @@ int main() k = i < 3; if (verbose != 0) - printf(" SHA-%d test #%d: ", 256 - k * 32, j + 1); + ; + // printf(" SHA-%d test #%d: ", 256 - k * 32, j + 1); sha2_starts(&ctx, k); @@ -701,25 +701,25 @@ int main() if (memcmp(sha2sum, sha2_test_sum[i], 32 - k * 4) != 0) { if (verbose != 0) - printf("failed\n"); + ; + //printf("failed\n"); return (1); } if (verbose != 0) - printf("passed\n"); + ;//printf("passed\n"); } if (verbose != 0) - printf("\n"); + ;//printf("\n"); for (i = 0; i < 14; i++) { j = i % 7; k = i < 7; if (verbose != 0) - printf(" HMAC-SHA-%d test #%d: ", 256 - k * 32, - j + 1); + ;//printf(" HMAC-SHA-%d test #%d: ", 256 - k * 32, j + 1); if (j == 5 || j == 6) { memset(buf, '\xAA', buflen = 131); @@ -737,17 +737,17 @@ int main() if (memcmp(sha2sum, sha2_hmac_test_sum[i], buflen) != 0) { if (verbose != 0) - printf("failed\n"); + ;//printf("failed\n"); return (1); } if (verbose != 0) - printf("passed\n"); + ;//printf("passed\n"); } if (verbose != 0) - printf("\n"); + ;//printf("\n"); testvector *tv = 0; int res = 0; @@ -764,7 +764,7 @@ int main() tv = &t1; res = do_test(tv); if (res != 0) { - printf("%s failed\n", tv->t); + ;//printf("%s failed\n", tv->t); return res; } @@ -781,7 +781,7 @@ int main() tv = &t2; res = do_test(tv); if (res != 0) { - printf("%s failed\n", tv->t); + ;//printf("%s failed\n", tv->t); return res; } @@ -798,7 +798,7 @@ int main() tv = &t3; res = do_test(tv); if (res != 0) { - printf("%s failed\n", tv->t); + ;//printf("%s failed\n", tv->t); return res; } @@ -815,7 +815,7 @@ int main() tv = &t4; // res = do_test(tv); if (res != 0) { - printf("%s failed\n", tv->t); + ;//printf("%s failed\n", tv->t); return res; } @@ -834,7 +834,7 @@ int main() tv = &t5; res = do_test(tv); if (res != 0) { - printf("%s failed\n", tv->t); + ;//printf("%s failed\n", tv->t); return res; } @@ -849,7 +849,7 @@ int main() tv = &t6; res = do_test(tv); if (res != 0) { - printf("%s failed\n", tv->t); + ;//printf("%s failed\n", tv->t); return res; } -- 2.34.1