Generating IV from random numbers, not machine ID and local sequence number.
[iotcloud.git] / version2 / src / C / CloudComm.h
1 #ifndef CLOUDCOMM_H
2 #define CLOUDCOMM_H
3
4 #include "common.h"
5 #include <pthread.h>
6 /**
7  * This class provides a communication API to the webserver.  It also
8  * validates the HMACs on the slots and handles encryption.
9  * @author Brian Demsky <bdemsky@uci.edu>
10  * @version 1.0
11  */
12
13 #define CloudComm_SALT_SIZE 8
14 #define CloudComm_TIMEOUT_MILLIS 5000
15 ;       // 100
16 #define CloudComm_IV_SIZE 16
17 /** Sets the size for the HMAC. */
18 #define CloudComm_HMAC_SIZE 32
19 #define HttpURLConnection_HTTP_OK 200
20
21 typedef struct {
22         int fd;
23         int numBytes;
24 } WebConnection;
25
26
27 class CloudComm {
28 private:
29         IoTString *baseurl;
30         AESKey *key;
31         Mac *mac;
32         IoTString *password;
33         SecureRandom *random;
34         Array<char> *salt;
35         Array<char> *ivArray;
36         Table *table;
37         int32_t listeningPort;
38         pthread_t localServerThread;
39         bool doEnd;
40         TimingSingleton *timer;
41         Array<char> *getslot;
42         Array<char> *putslot;
43
44         /**
45          * Generates Key from password.
46          */
47         AESKey *initKey();
48
49         /**
50          * Inits the HMAC generator.
51          */
52         void initCrypt();
53
54         /*
55          * Builds the URL for the given request.
56          */
57         IoTString *buildRequest(bool isput, int64_t sequencenumber, int64_t maxentries);
58         void setSalt();
59         bool getSalt();
60         Array<char> *createIV(int64_t machineId, int64_t localSequenceNumber);
61         void createIV();
62         Array<char> *encryptSlotAndPrependIV(Array<char> *rawData, Array<char> *ivBytes);
63         Array<char> *stripIVAndDecryptSlot(Array<char> *rawData);
64         Array<Slot *> *processSlots(WebConnection *wc);
65
66
67 public:
68         /**
69          * Empty Constructor needed for child class.
70          */
71         CloudComm();
72
73         /**
74          * Constructor for actual use. Takes in the url and password.
75          */
76         CloudComm(Table *_table,  IoTString *_baseurl, IoTString *_password, int _listeningPort);
77         ~CloudComm();
78
79         /**
80          * Inits all the security stuff
81          */
82         void initSecurity();
83
84         /*
85          * API for putting a slot into the queue.  Returns NULL on success.
86          * On failure, the server will send slots with newer sequence
87          * numbers.
88          */
89         Array<Slot *> *putSlot(Slot *slot, int max);
90
91         /**
92          * Request the server to send all slots with the given
93          * sequencenumber or newer.
94          */
95         Array<Slot *> *getSlots(int64_t sequencenumber);
96
97
98         /**
99          * Method that actually handles building Slot objects from the
100          * server response.  Shared by both putSlot and getSlots.
101          */
102
103         Array<char> *sendLocalData(Array<char> *sendData, int64_t localSequenceNumber, IoTString *host, int port);
104         void closeCloud();
105         void localServerWorkerFunction();
106 };
107 #endif