Adding helper functions for server part; cleaning up
[iotcloud.git] / doc / iotcloud.tex
1 \documentclass[11pt]{article}\r
2 \newcommand{\tuple}[1]{\ensuremath \langle #1 \rangle}\r
3 \usepackage{color}\r
4 \usepackage{amsthm}\r
5 \usepackage{amsmath}\r
6 \usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx\r
7 \newtheorem{theorem}{Theorem}\r
8 \newtheorem{defn}{Definition}\r
9 \newcommand{\note}[1]{{\color{red} \bf [[#1]]}}\r
10 \newcommand{\push}[1][1]{\hskip\dimexpr #1\algorithmicindent\relax}\r
11 \begin{document}\r
12 \section{Approach}\r
13 \r
14 \subsection{Keys}\r
15 \r
16 Each device has: user id + password\r
17 \r
18 Server login is:\r
19 hash1(user id), hash1(password)\r
20 \r
21 Symmetric Crypto keys is:\r
22 hash2(user id | password)\r
23 \r
24 Server has finite length queue of entries + max\_entry\_identifier +\r
25 server login key\r
26 \r
27 \subsection{Entry layout}\r
28 Each entry has:\r
29 \begin{enumerate}\r
30 \item Sequence identifier\r
31 \item Random IV (if needed by crypto algorithm)\r
32 \item Encrypted payload\r
33 \end{enumerate}\r
34 \r
35 Payload has:\r
36 \begin{enumerate}\r
37 \item Sequence identifier\r
38 \item Machine id (most probably something like a 64-bit random number \r
39 that is self-generated by client)\r
40 \item HMAC of previous slot\r
41 \item Data entries\r
42 \item HMAC of current slot\r
43 \end{enumerate}\r
44 \r
45 A data entry can be one of these:\r
46 \begin{enumerate}\r
47 \item All or part of a Key-value entry\r
48 \item Slot sequence entry: Machine id + last message identifier \r
49 \newline {The purpose of this is to keep the record of the last slot \r
50 from a certain client if a client's update has to expunge that other \r
51 client's last entry from the queue. This is kept in the slot until \r
52 the entry owner inserts a newer update into the queue.}\r
53 \item Queue state entry: Includes queue size \newline {The purpose \r
54 of this is for the client to tell if the server lies about the number \r
55 of slots in the queue, e.g. if there are 2 queue state entries in the queue, \r
56 e.g. 50 and 70, the client knows that when it sees 50, it should expect \r
57 at most 50 slots in the queue and after it sees 70, it should expect \r
58 50 slots before that queue state entry slot 50 and at most 70 slots. \r
59 The queue state entry slot 70 is counted as slot number 51 in the queue.}\r
60 \end{enumerate}\r
61 \r
62 \subsection{Live status}\r
63 \r
64 Live status of entries:\r
65 \begin{enumerate}\r
66 \item Key-Value Entry is dead if either (a) there is a newer key-value pair or (b) it is incomplete.\r
67         \r
68 \item Slot sequence number (of either a message version data\r
69 or user-level data) is dead if there is a newer slot from the same machine.\r
70 \r
71 \item Queue state entry is dead if there is a newer queue state entry.\r
72 {In the case of queue state entries 50 and 70, this means that queue state \r
73 entry 50 is dead and 70 is live. However, not until the number of slots reaches \r
74 70 that queue state entry 50 will be expunged from the queue.}\r
75 \end{enumerate}\r
76 \r
77 When data is at the end of the queue ready to expunge, if:\r
78 \begin{enumerate}\r
79 \item The key-value entry is not dead, it must be reinserted at the\r
80 beginning of the queue.\r
81 \r
82 \item If the slot sequence number is not dead, then a message sequence\r
83 entry must be inserted.\r
84 \r
85 \item If the queue state entry is not dead, it must be reinserted at the\r
86 beginning of the queue.\r
87 \end{enumerate}\r
88 \r
89 \r
90 \paragraph{Reads:}\r
91 Client sends a sequence number.  Server replies with either: all data\r
92 entries or all newer data entries.\r
93 \r
94 \paragraph{Writes:}\r
95 Client sends slot, server verifies that sequence number is valid,\r
96 checks entry hash, and replies with an accept message if all checks\r
97 pass.  On success, client updates its sequence number.  On failure,\r
98 server sends updates slots to client and client validates those slots.\r
99 \r
100 \paragraph{Local state on each client:}\r
101 A list of machines and the corresponding latest sequence numbers.\r
102 \r
103 \paragraph{Validation procedure on client:}\r
104 \begin{enumerate}\r
105 \item Decrypt each new slot in order.\r
106 \item For each slot:\r
107     (a) check its HMAC, and\r
108     (b) check that the previous entry HMAC field matches the previous\r
109     entry.\r
110 \item Check that the last message version for our machine matches our\r
111 last message sent.\r
112 \item For all other machines, check that the latest sequence number is\r
113 at least as large (never goes backwards).\r
114 \item That the queue has a current queue state entry.\r
115 \item That the number of entries received is consistent with the size\r
116 specified in the queue state entry.\r
117 \end{enumerate}\r
118 \r
119 Key-value entries can span multiple slots.  They aren't valid until\r
120 they are complete.\r
121 \r
122 \subsection{Resizing Queue}\r
123 Client can make a request to resize the queue. This is done as a write that combines:\r
124   (a) a slot with the message, and\r
125         (b) a request to the server\r
126 \r
127 \subsection{Server Algorithm}\r
128 $s \in SN$ is a sequence number\\\r
129 $sv \in SV$ is a slot's value\\\r
130 $slot_s = \tuple{s, sv} \in SL \subseteq SN \times SV$ \\\r
131 \r
132 \textbf{State} \\\r
133 \textit{SL = set of live slots on server} \\\r
134 \textit{max = maximum number of slots (input only for resize message)} \\\r
135 \textit{n = number of slots} \\ \\\r
136 \textbf{Helper Function} \\\r
137 $MaxSlot(SL')= \langle s, sv \rangle \mid \langle s, sv \rangle \r
138 \in SL' \wedge \forall \langle s', sv' \rangle \in SL', s \geq s'$ \\\r
139 $MinSlot(SL')= \langle s, sv \rangle \mid \langle s, sv \rangle \r
140 \in SL' \wedge \forall \langle s', sv' \rangle \in SL', s \leq s'$ \\\r
141 $SeqN(\langle s, sv \rangle)=s$ \\\r
142 $SlotVal(\langle s, sv \rangle)=sv$ \\\r
143 \r
144 \begin{algorithmic}[1]\r
145 \Function{GetSlot}{$s_g$}\r
146 \State \Return{$\{\tuple{s, sv} \in SL \mid s \geq s_g\}$}\r
147 \EndFunction\r
148 \end{algorithmic}\r
149 \r
150 \begin{algorithmic}[1]\r
151 \Function{PutSlot}{$s_p,sv_p,max'$}\r
152 \If{$(max' \neq \emptyset)$}\Comment{Resize}\r
153 \State $max \gets max'$\r
154 \EndIf\r
155 \State $\langle s_n,sv_n \rangle \gets MaxSlot(SL)$\Comment{Last sv}\r
156 \State $s_n \gets SeqN(\langle s_n,sv_n \rangle)$\r
157 \If{$(s_p = s_n + 1)$}\r
158         \If{$n = max$}\r
159         \State $\langle s_m,sv_m \rangle \gets MinSlot(SL)$\Comment{First sv}\r
160                 \State $SL \gets SL - \{\langle s_m,sv_m \rangle\}$\r
161         \Else \Comment{$n < max$}\r
162                 \State $n \gets n + 1$\r
163         \EndIf\r
164     \State $SL \gets SL \cup \{\langle s_p,sv_p \rangle\}$\r
165         \State \Return{$true$}\r
166 \Else\r
167         \State \Return{$(false,\{\langle s,sv \rangle \in SL \mid \r
168     s \geq s_p\})$}\r
169 \EndIf\r
170 \EndFunction\r
171 \end{algorithmic}\r
172 \r
173 \subsection{Client Algorithm}\r
174 \begin{algorithmic}[1]\r
175 \Function{CallClient}{$uid,pw,d,m,max,s,Data*,Result*$}\r
176 \textit{\r
177 \newline{// uid = user identification}\r
178 \newline{// pw = password}\r
179 \newline{// d = new data for write}\r
180 \newline{// m = client message (read/write/resize)}\r
181 \newline{// max = maximum number of slots (input only for resize message)}\r
182 \newline{// n = number of slots}\r
183 \newline{// s = sequence number for server request}\r
184 \newline{// t = sequence numbers of slots on server}\r
185 \newline{// mid = machine identification}\r
186 \newline{// seq = sequence number inside slot}\r
187 \newline{// newSlot = new slot}\r
188 \newline{// expSlot = expunged/expired slot}\r
189 \newline{// slotSeqE = slot sequence entry}\r
190 \newline{// M = list of all machines/devices with their respective latest s on client}\r
191 \newline{// Data = array of slots written/read (input only for write)}\r
192 \newline{// Result = array of decrypted and valid slots after a read}\r
193 \newline{// Slot = one data slot)}\r
194 \newline{// DSlot = one decrypted data slot)}\r
195 }\r
196 \State $SK = Hash(uid + pw)$\r
197 \If{$m = read$}\r
198         \State $Data \gets CallServer(m,max,s,Data)$\r
199         \If{$Data = \emptyset$}\r
200                 \State $ReportError(\emptyset,read)$\r
201         \Else\r
202                 \If{$\neg HasCurrentQueueStateEntry(Data)$}\r
203                         \State $ReportError(DSlot_i,read)$\r
204                 \EndIf\r
205                 \ForAll{$Slot_i \in Data$}\r
206                         \State $DSlot_i \gets Decrypt(SK,Slot_i)$\Comment{Check s and HMAC}\r
207                         \If{$\neg (ValidSeqN(DSlot_i) \land ValidHmac(DSlot_i) \land $\\\r
208                                 \push[1] $ValidPrevHmac(DSlot_i))$}\r
209                                 \State $ReportError(DSlot_i,read)$\r
210                         \Else\Comment{Check only live entries}\r
211                                 \If{$IsLiveSlotSequenceEntry(DSlot_i)$}\r
212                                         \State $lastS \gets LastSeqN(DSlot_i)$\r
213                                         \State $lastMid \gets LastMachineId(DSlot_i)$\r
214                                         \If{$lastS \neq LastSeqN(lastMid,M)$}\r
215                                                 \State $ReportError(DSlot_i,read)$\r
216                                         \EndIf\r
217                                 \ElsIf{$IsLiveKeyValueEntry(DSlot_i)$}\r
218                                         \State $mid \gets MachineId(DSlot_i)$\r
219                                         \State $seq \gets SeqN(DSlot_i)$\r
220                                         \If{$IsOwnMid(mid)$}\r
221                                                 \If{$IsLastS(mid,seq,Data) \land $\\\r
222                                                 \push[1] $(seq \neq LastSeqN(mid,M))$}\r
223                                                         \State $ReportError(DSlot_i,read)$\r
224                                                 \EndIf\r
225                                         \Else\Comment{Check s for other machines}\r
226                                                 \If{$IsLastS(mid,seq,Data) \land $\\\r
227                                                 \push[1] $(seq < LastSeqN(mid,M))$}\r
228                                                         \State $ReportError(DSlot_i,read)$\r
229                                                 \EndIf\r
230                                         \EndIf\Comment{Check queue state entry}\r
231                                 \ElsIf{$IsLiveQueueStateEntry(DSlot_i)$}\r
232                                         \If{$IsCurrentQueueState(DSlot_i)$}\r
233                                                 \If{$Length(Data) > QueueLength(DSlot_i)$}\r
234                                                         \State $ReportError(DSlot_i,read)$\r
235                                                 \EndIf\r
236                                         \EndIf\r
237                                 \Else\r
238                                         \State $ReportError(DSlot_i,read)$\r
239                                 \EndIf\r
240                         \EndIf\r
241                         \State $Result \gets Concat(Result, DSlot_i)$\r
242                 \EndFor\r
243         \EndIf\r
244 \r
245 \ElsIf{$m = write$}\r
246         \State $newSlot \gets CreateSlot(d)$\r
247         \State $Data[1] \gets Encrypt(SK,newSlot)$\r
248         \State $Data \gets CallServer(m,max,s,Data)$\r
249         \If{$Data = \emptyset$}\r
250                 \State $ReportError(\emptyset,write)$\r
251         \Else\Comment Check for valid return value from server\r
252                 \If{$\neg ValidOldLastEntry(Data[1])$}\r
253                         \State $ReportError(Data[1],write)$\r
254                 \Else\Comment{Check if we need slot sequence entry}\r
255                         \If{$Length(Data) = 2$}\r
256                                 \State $expSlot \gets Decrypt(SK,Data[2])$\r
257                                 \State $mid \gets MachineId(expSlot)$\r
258                                 \State $seq \gets SeqN(expSlot)$\r
259                                 \If{$seq = LastSeqN(mid,M)$}\Comment{Liveness check}\r
260                                         \State $slotSeqE \gets CreateSlotSeqE(mid,seq)$\r
261                                         \State $Data[1] \gets Encrypt(SK,slotSeqE)$\r
262                                         \State $Data \gets CallServer(m,max,s,Data)$\r
263                                 \EndIf\r
264                         \Else\r
265                                 \State $ReportError(Data,write)$\r
266                         \EndIf\r
267                 \EndIf\r
268         \EndIf\r
269 \r
270 \ElsIf{$m = resize$}\r
271         \State $Data \gets CallServer(m,max,s,Data)$\r
272         \If{$Data = \emptyset$}\r
273                 \State $ReportError(\emptyset,resize)$\r
274         \EndIf\r
275 \EndIf\r
276 \State \Return{$Result$}\r
277 \EndFunction\r
278 \end{algorithmic}\r
279 \r
280 \subsection{Formal Guarantees}\r
281 \r
282 \textit{To be completed ...}\r
283 \r
284 \begin{defn}[System Execution]\r
285 Let \\\r
286 \begin{center}\r
287 $Q = \{slot_{sn_1}, slot_{sn_2}, \dots, Slot_{sn_n}\}$, \\\r
288 $SN = \{sn_1, sn_2, \dots, sn_n\}$ \\\r
289 \end{center}\r
290 denote a queue $Q$ of $n$ slots, with each slot entry being denoted by a\r
291 valid sequence number $sn_i \in SN$. $Q$ represents a total order of all \r
292 slot updates from all corresponding machines.\r
293 %\textit{Formalize a system execution as a sequence of slot updates...\r
294 %There is a total order of all slot updates.}\r
295 \end{defn}\r
296 \r
297 \begin{defn}[Transitive Closure]\r
298 A transitive closure $\tau : \tau = \epsilon_{update(slot_i)} R $\r
299 $\epsilon_{receive(slot_i)}$ is a relation from slot update event\r
300 $\epsilon$ to a slot receive event $\epsilon$ for $slot_i$.\r
301 %Define transitive closure relation for slot updates...  There is an\r
302 %edge from a slot update to a slot receive event if the receive event\r
303 %reads from the update event.\r
304 \end{defn}\r
305 \r
306 \begin{defn}[Suborder]\r
307 Let \\\r
308 \begin{center}\r
309 $q = \{slot_{i}, slot_{i+1}, \dots, slot_{j}\}, \r
310 sn_1 \leq i \leq j \leq sn_n \Longrightarrow q \subseteq Q$\r
311 \end{center}\r
312 denote a suborder of the total order. Set q is a sequence of contiguous\r
313 slot updates that is a subset of a total order of all slot updates in Q.\r
314 %Define suborder of total order.  It is a sequence of contiguous slots\r
315 %updates (as observed by a given device).\r
316 \end{defn}\r
317 \r
318 \begin{defn}[Prefix of a suborder]\r
319 Given a suborder \\ \r
320 \begin{center}\r
321 $q' = \{slot_{i}, slot_{i+1}, \dots, slot_{j}, \dots\}, \r
322 sn_1 \leq i \leq j \leq sn_n \Longrightarrow \r
323 q' \subseteq Q$\r
324 \end{center}\r
325 with $slot_{j}$ as a slot update in $q'$, the prefix of $q'$ is a sequence \r
326 of all slot updates $\{slot_{i}, slot_{i+1}, \dots, slot_{j-1}\} \cup\r
327 slot_{j}$.\r
328 %Given a sub order $o=u_{i},u_{i+1},...,u_j, u_{j+i},..., u', ...$ and\r
329 %a slot update $u'$ in $o$, the prefix of $o$ is a sequence of all\r
330 %updates that occur before $u'$ and $u'$.\r
331 \end{defn}\r
332 \r
333 \begin{defn}[Consistency between a suborder and a total order]\r
334 A suborder $q'$ is consistent with a total order $T$ of $Q$, if all\r
335 updates in $q'$ appear in $T$ and they appear in the same order, as \r
336 the following:\r
337 \begin{center}\r
338 $q' = \{slot_{i}, slot_{i+1}, \dots, slot_{j}\},$\r
339 $T = \{slot_{sn_1}, \dots, slot_{i}, slot_{i+1}, \dots, slot_{j}, \dots,\r
340 slot_{sn_n}\},$ \\ $sn_1 \leq i \leq j \leq sn_n \Longrightarrow\r
341 q' \subseteq T$\r
342 \end{center}\r
343 %A suborder $o$ is consistent with a total order $t$, if all updates in $o$ %appear in $t$ and they appear in the same order.\r
344 \end{defn}\r
345 \r
346 \begin{defn}[Consistency between suborders]\r
347 Two suborders U and V are consistent if there exist a total order T \r
348 such that both U and V are suborders of T.\r
349 \begin{center}\r
350 $U = \{slot_{i}, slot_{i+1}, \dots, slot_{j}\},$ \\\r
351 $V = \{slot_{k}, slot_{k+1}, \dots, slot_{l}\},$ \\\r
352 $sn_1 \leq i \leq j \leq k \leq l \leq sn_n,$ \\\r
353 $U \subset T \land V \subset T$ \\\r
354 $T = \{slot_{sn_1}, \dots, slot_{i}, slot_{i+1}, \dots, slot_{j}, $\\\r
355 $\dots, slot_{k}, slot_{k+1}, \dots, slot_{l}, \dots, slot_{sn_n}\}$\r
356 \end{center}\r
357 %Define notion of consistency between suborders...  Two suborders U,V\r
358 %are consistent if there exist a total order T such that both U and V\r
359 %are suborders of T.\r
360 \end{defn}\r
361 \r
362 \begin{defn}[Error-free execution]\r
363 An error-free execution, for which the client does not flag any errors\r
364 is defined by the following criteria:\r
365 \begin{enumerate}\r
366 \item Item 1\r
367 \item Item 2\r
368 \end{enumerate}\r
369 %not flag any errors...\r
370 %Define error-free execution --- execution for which the client does\r
371 %not flag any errors...\r
372 \end{defn}\r
373 \r
374 \begin{theorem} Error-free execution of algorithm ensures that the suborder\r
375 for node n is consistent with the prefix suborder for all other nodes\r
376 that are in the transitive closure.\r
377 \end{theorem}\r
378 \begin{proof}\r
379 \textit{TODO}\r
380 \end{proof}\r
381 \r
382 \begin{defn}[State of Data Structure]\r
383 Define in terms of playing all updates sequentially onto local data\r
384 structure.\r
385 \end{defn}\r
386 \r
387 \begin{theorem}\r
388 Algorithm gives consistent view of data structure.\r
389 \end{theorem}\r
390 \begin{proof}\r
391 \textit{TODO}\r
392 \end{proof}\r
393 \r
394 \subsection{Future Work}\r
395 \paragraph{Support Messages}\r
396   A message is dead once receiving machine sends an entry with a newer\r
397   sequence identifier\r
398 \r
399 \paragraph{Persistent data structures}\r
400         Root object w/ fields\r
401         Other objects can be reachable from root\r
402         Each object has its own entries\r
403         Dead objects correspond to dead \r
404 \r
405 \paragraph{Multiple App Sharing}\r
406 \r
407 Idea is to separate subspace of entries...  Shared with other cloud...\r
408 \end{document}\r
409 \r