edits
[iotcloud.git] / version2 / src / C / CloudComm.cc
index 4e4000fae9ee310be4836cbb3b83a021ef902005..3b5fb970199962959a18895dc03824a3b923ebf4 100644 (file)
@@ -9,6 +9,7 @@
 #include "Slot.h"
 #include "Crypto.h"
 #include "ByteBuffer.h"
+#include "aes.h"
 #include <sys/socket.h>
 #include <unistd.h>
 
@@ -32,7 +33,7 @@ CloudComm::CloudComm() :
 {
 }
 
-void * threadWrapper(void * cloud) {
+void *threadWrapper(void *cloud) {
        CloudComm *c = (CloudComm *) cloud;
        c->localServerWorkerFunction();
        return NULL;
@@ -63,10 +64,10 @@ CloudComm::CloudComm(Table *_table,  IoTString *_baseurl, IoTString *_password,
  */
 AESKey *CloudComm::initKey() {
        try {
-               AESKey * key = new AESKey(password->internalBytes(),
-                                                                                                                       salt,
-                                                                                                                       65536,
-                                                                                                                       128);
+               AESKey *key = new AESKey(password->internalBytes(),
+                                                                                                                salt,
+                                                                                                                65536,
+                                                                                                                128);
                return key;
        } catch (Exception *e) {
                throw new Error("Failed generating key.");
@@ -97,7 +98,7 @@ void CloudComm::initCrypt() {
        try {
                key = initKey();
                password = NULL;// drop password
-               mac = Mac_getInstance("HmacSHA256");
+               mac = new Mac();
                mac->init(key);
        } catch (Exception *e) {
                throw new Error("Failed To Initialize Ciphers");
@@ -146,7 +147,7 @@ void readSocketData(int fd, Array<char> *data) {}
 void writeURLData(int fd, Array<char> *data) {
 }
 
-void readURLData(int fd, Array<char> * output) {
+void readURLData(int fd, Array<char> *output) {
 }
 
 int readURLInt(int fd) {
@@ -195,7 +196,7 @@ void CloudComm::setSalt() {
 bool CloudComm::getSalt() {
        int fd = -1;
        IoTString *urlstr = NULL;
-       
+
        try {
                char *buffer = (char *) malloc(baseurl->length() + 100);
                memcpy(buffer, baseurl->internalBytes(), baseurl->length());
@@ -245,12 +246,16 @@ Array<char> *CloudComm::createIV(int64_t machineId, int64_t localSequenceNumber)
        return buffer->array();
 }
 
-Array<char> *AESEncrypt(Array<char> * ivBytes, AESKey * key, Array<char> * data) {
-       return NULL;
+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->getKey()->internalArray(), key->getKey()->length()/(sizeof(WORD)/sizeof(BYTE)), (BYTE *)ivBytes->internalArray());
+       return output;
 }
 
-Array<char> *AESDecrypt(Array<char> * ivBytes, AESKey * key, Array<char> * data) {
-       return NULL;
+Array<char> *AESDecrypt(Array<char> *ivBytes, AESKey *key, Array<char> *data) {
+       Array<char> * output=new Array<char>(data->length());
+       aes_decrypt_ctr((BYTE *)data->internalArray(), data->length(), (BYTE *)output->internalArray(), (WORD *)key->getKey()->internalArray(), key->getKey()->length()/(sizeof(WORD)/sizeof(BYTE)), (BYTE *)ivBytes->internalArray());
+       return output;
 }
 
 Array<char> *CloudComm::encryptSlotAndPrependIV(Array<char> *rawData, Array<char> *ivBytes) {
@@ -397,7 +402,7 @@ Array<Slot *> *CloudComm::processSlots(int fd) {
        return slots;
 }
 
-Array<char> *CloudComm::sendLocalData(Array<char> *sendData, int64_t localSequenceNumber, IoTString * host, int port) {
+Array<char> *CloudComm::sendLocalData(Array<char> *sendData, int64_t localSequenceNumber, IoTString *host, int port) {
        if (salt == NULL)
                return NULL;
        try {