-cleanup, FIXMEs
[oweals/gnunet.git] / src / scalarproduct / gnunet_scalarproduct.h
1 /*
2       This file is part of GNUnet.
3       (C) 2013 Christian Grothoff (and other contributing authors)
4
5       GNUnet is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published
7       by the Free Software Foundation; either version 3, or (at your
8       option) any later version.
9
10       GNUnet is distributed in the hope that it will be useful, but
11       WITHOUT ANY WARRANTY; without even the implied warranty of
12       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13       General Public License for more details.
14
15       You should have received a copy of the GNU General Public License
16       along with GNUnet; see the file COPYING.  If not, write to the
17       Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * @file scalarproduct/gnunet_scalarproduct.h
23  * @brief API to the scalarproduct service
24  * @author Christian M. Fuchs
25  */
26
27 #ifndef GNUNET_SCALARPRODUCT_H
28 #define GNUNET_SCALARPRODUCT_H
29
30 ///////////////////////////////////////////////////////////////////////////////
31 //                      Defines
32 ///////////////////////////////////////////////////////////////////////////////
33 #define DISABLE_CRYPTO
34
35 /**
36  * Length of the key used for encryption
37  */
38 #define KEYBITS 2048
39
40 /**
41  * When performing our crypto, we may add two encrypted values with each 
42  * a maximal length of GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH.
43  * thus we can receive a slightly longer element (+1 byte)
44  */
45 #define PAILLIER_ELEMENT_LENGTH (2*KEYBITS/8 +1)
46
47 #ifdef  __cplusplus
48 extern "C"
49 {
50 #endif
51
52 ///////////////////////////////////////////////////////////////////////////////
53 //                     Service Structure Definitions
54 ///////////////////////////////////////////////////////////////////////////////
55
56 /**
57  * Message type passed from requesting service Alice to responding service Bob
58  * to initiate a request and make bob participate in our protocol
59  */
60 struct GNUNET_SCALARPRODUCT_service_request {
61   /**
62    * GNUNET message header
63    */
64   struct GNUNET_MessageHeader header;
65
66   /**
67    * how many bytes the mask has
68    */
69   uint16_t mask_length GNUNET_PACKED;
70
71   /**
72    * the length of the publickey contained within this message
73    */
74   uint16_t pk_length GNUNET_PACKED;
75
76   /**
77    * the transaction/session key used to identify a session
78    */
79   struct GNUNET_HashCode key;
80
81   /**
82    * how many elements the vector in payload contains
83    */
84   uint16_t element_count GNUNET_PACKED;
85
86   /**
87    * how many elements are actually included after the mask was applied.
88    */
89   uint16_t used_element_count GNUNET_PACKED;
90
91   /**
92    * followed by mask | public_key | vector[used_element_count]
93    */
94 };
95
96 /**
97  * Message type passed from responding service Bob to responding service Alice
98  * to complete a request and allow Alice to compute the result
99  */
100 struct GNUNET_SCALARPRODUCT_service_response {
101   /**
102    * GNUNET message header
103    */
104   struct GNUNET_MessageHeader header;
105
106   /**
107    * how many elements the vector in payload contains
108    */
109   uint16_t element_count GNUNET_PACKED;
110
111   /**
112    * how many elements are actually included after the mask was applied.
113    */
114   uint16_t used_element_count GNUNET_PACKED;
115
116   /**
117    * the transaction/session key used to identify a session
118    */
119   struct GNUNET_HashCode key;
120
121   /**
122    * followed by s | s' | kp[] | kq[]
123    */
124 };
125
126 ///////////////////////////////////////////////////////////////////////////////
127 //                     Service Structure Definitions
128 ///////////////////////////////////////////////////////////////////////////////
129
130 /**
131  * state a session can be in
132  */
133 enum SessionState
134 {
135     WAITING_FOR_BOBS_CONNECT,
136     MESSAGE_FROM_RESPONDING_CLIENT_RECEIVED,
137     WAITING_FOR_RESPONSE_FROM_SERVICE,
138     REQUEST_FROM_SERVICE_RECEIVED,
139     FINALIZED
140 };
141
142 /**
143  * role a peer in a session can assume
144  */
145 enum PeerRole
146 {
147     ALICE,
148     BOB
149 };
150
151
152 /**
153  * A scalarproduct session which tracks:
154  * 
155  * a request form the client to our final response.
156  * or
157  * a request from a service to us(service).
158  */
159 struct ServiceSession
160 {
161     /**
162      * the role this peer has
163      */
164     enum PeerRole role;
165
166     /**
167      * session information is kept in a DLL
168      */
169     struct ServiceSession *next;
170
171     /**
172      * session information is kept in a DLL
173      */
174     struct ServiceSession *prev;
175
176     /**
177      * (hopefully) unique transaction ID
178      */
179     struct GNUNET_HashCode key;
180
181     /** 
182      * state of the session
183      */
184     enum SessionState state;
185
186     /**
187      * Alice or Bob's peerID
188      */
189     struct GNUNET_PeerIdentity peer;
190
191     /**
192      * the client this request is related to
193      */
194     struct GNUNET_SERVER_Client * client;
195
196     /**
197      * how many elements we were supplied with from the client
198      */
199     uint16_t element_count;
200
201     /**
202      * how many elements actually are used after applying the mask
203      */
204     uint16_t used_element_count;
205
206     /**
207      * how many bytes the mask is long. 
208      * just for convenience so we don't have to re-re-re calculate it each time
209      */
210     uint16_t mask_length;
211
212     /**
213      * all the vector elements we received
214      */
215     int32_t * vector;
216
217     /**
218      * mask of which elements to check
219      */
220     unsigned char * mask;
221
222     /**
223      * Public key of the remote service, only used by bob
224      */
225     gcry_sexp_t remote_pubkey;
226
227     /**
228      * E(ai)(Bob) or ai(Alice) after applying the mask
229      */
230     gcry_mpi_t * a;
231
232     /**
233      * The computed scalar 
234      */
235     gcry_mpi_t product;
236
237     /**
238      * My transmit handle for the current message to a alice/bob
239      */
240     struct GNUNET_MESH_TransmitHandle * service_transmit_handle;
241
242     /**
243      * My transmit handle for the current message to the client
244      */
245     struct GNUNET_SERVER_TransmitHandle * client_transmit_handle;
246
247     /**
248      * tunnel-handle associated with our mesh handle
249      */
250     struct GNUNET_MESH_Tunnel * tunnel;
251
252 };
253
254 /**
255  * We need to do a minimum of bookkeeping to maintain track of our transmit handles.
256  * each msg is associated with a session and handle. using this information we can determine which msg was sent.
257  */
258 struct MessageObject
259 {
260     /**
261      * The handle used to transmit with this request
262      */
263     void ** transmit_handle;
264
265     /**
266      * The message to send
267      */
268     struct GNUNET_MessageHeader * msg;
269 };
270
271 #ifdef  __cplusplus
272 }
273 #endif
274
275 #endif  /* GNUNET_SCALARPRODUCT_H */
276