ABY Framework  1.0
Arithmetic Bool Yao Framework
 All Classes Files Functions Variables Enumerations Enumerator Macros
ot-extension.h
Go to the documentation of this file.
1 
19 #ifndef __OT_EXTENSION_H_
20 #define __OT_EXTENSION_H_
21 
22 #include "../util/typedefs.h"
23 #include "../util/socket.h"
24 #include "../util/thread.h"
25 #include "../util/cbitvector.h"
26 #include "../util/crypto/crypto.h"
27 #include "maskingfunction.h"
28 
29 //#define DEBUG
30 #define FIXED_KEY_AES_HASHING
31 //#define AES_OWF
32 //#define DEBUG_MALICIOUS
33 //#define VERIFY_OT
34 //#define OT_HASH_DEBUG
35 //#define OTTiming
36 //#define HIGH_SPEED_ROT_LT
37 
38 const BYTE G_OT = 0x01;
39 const BYTE C_OT = 0x02;
40 const BYTE R_OT = 0x03;
41 
42 #define NUMOTBLOCKS 4096
43 
44 typedef struct OTBlock_t {
45  int blockid;
46  int processedOTs;
47  BYTE* snd_buf;
48  OTBlock_t* next;
49 } OTBlock;
50 
51 #ifdef FIXED_KEY_AES_HASHING
52 static const uint8_t fixed_key_aes_seed[32] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55,
53  0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
54 #endif
55 
56 static void InitAESKey(AES_KEY_CTX* ctx, BYTE* keybytes, uint32_t numkeys, crypto* crypt) {
57  BYTE* pBufIdx = keybytes;
58  uint32_t aes_key_bytes = crypt->get_aes_key_bytes();
59  for (uint32_t i = 0; i < numkeys; i++) {
60  crypt->init_aes_key(ctx + i, pBufIdx);
61  pBufIdx += aes_key_bytes;
62  }
63 }
64 
65 #define OWF_BYTES AES_BYTES
66 
67 class OTExtSnd {
68  /*
69  * OT sender part
70  * Input:
71  * ret: returns the resulting bit representations. Has to initialized to a byte size of: nOTs * nSndVals * state.field_size
72  *
73  * CBitVector* values: holds the values to be transferred. If C_OT is enabled, the first dimension holds the value while the delta is written into the second dimension
74  * Output: was the execution successful?
75  */
76 public:
77  OTExtSnd(uint32_t nSndVals, uint32_t nOTs, uint32_t bitlength, crypto* crypt, CSocket* sock, CBitVector& U, BYTE* keybytes, CBitVector& x0, CBitVector& x1, BYTE type,
78  int nbaseOTs = -1, int nchecks = -1, int nbaseseeds = -1) {
79  Init(nSndVals, crypt, sock, U, keybytes, nbaseOTs, nchecks, nbaseseeds);
80  m_nOTs = nOTs;
81  m_vValues[0] = x0;
82  m_vValues[1] = x1;
83  m_nBitLength = bitlength;
84  m_bProtocol = type;
85  }
86  ;
87 
88  OTExtSnd(uint32_t nSndVals, crypto* crypt, CSocket* sock, CBitVector& U, BYTE* keybytes, int nbaseOTs = -1, int nchecks = -1, int nbaseseeds = -1) {
89  Init(nSndVals, crypt, sock, U, keybytes, nbaseOTs, nchecks, nbaseseeds);
90  }
91  ;
92 
93  void Init(uint32_t nSndVals, crypto* crypt, CSocket* sock, CBitVector& U, BYTE* keybytes, int nbaseOTs, int nchecks, int nbaseseeds) {
94  m_nSndVals = nSndVals;
95  m_vSockets = sock;
96  m_nCounter = 0;
97  m_cCrypt = crypt;
98  m_nSymSecParam = m_cCrypt->get_seclvl().symbits;
99  m_nBaseOTs = m_nSymSecParam;
100 
101  if (nbaseOTs != -1)
102  m_nBaseOTs = nbaseOTs;
103 
104  int keyseeds = m_nBaseOTs;
105  if (nbaseseeds != -1)
106  keyseeds = nbaseseeds;
107 
108  m_vU.Create(keyseeds);
109  m_vU.Copy(U.GetArr(), 0, ceil_divide(keyseeds, 8));
110  for (int i = keyseeds; i < PadToMultiple(keyseeds, 8); i++)
111  m_vU.SetBit(i, 0);
112 
113  m_vValues = (CBitVector*) malloc(sizeof(CBitVector) * nSndVals);
114  m_vKeySeeds = (AES_KEY_CTX*) malloc(sizeof(AES_KEY_CTX) * keyseeds);
115  m_lSendLock = new CLock;
116 
117  InitAESKey(m_vKeySeeds, keybytes, keyseeds, m_cCrypt);
118 
119 #ifdef FIXED_KEY_AES_HASHING
120  m_kCRFKey = (AES_KEY_CTX*) malloc(sizeof(AES_KEY_CTX));
121  m_cCrypt->init_aes_key(m_kCRFKey, (uint8_t*) fixed_key_aes_seed);
122 #endif
123  }
124  ;
125 
126  ~OTExtSnd() {
127  //free(m_vKeySeeds);
128  }
129  ;
130  BOOL send(uint32_t numOTs, uint32_t bitlength, CBitVector& s0, CBitVector& s1, BYTE type, uint32_t numThreads, MaskingFunction* maskfct);
131  BOOL send(uint32_t numThreads);
132 
133  BOOL OTSenderRoutine(uint32_t id, uint32_t myNumOTs);
134 
135  void BuildQMatrix(CBitVector& T, CBitVector& RcvBuf, uint32_t blocksize, BYTE* ctr);
136  void ProcessAndEnqueue(CBitVector* snd_buf, uint32_t id, uint32_t progress, uint32_t processedOTs);
137  void SendBlocks(uint32_t numThreads);
138  void MaskInputs(CBitVector& Q, CBitVector* seedbuf, CBitVector* snd_buf, uint32_t ctr, uint32_t processedOTs);
139  BOOL verifyOT(uint32_t myNumOTs);
140 
141 protected:
142  BYTE m_bProtocol;
143  uint32_t m_nSndVals;
144  uint32_t m_nOTs;
145  uint32_t m_nBitLength;
146  uint32_t m_nCounter;
147  uint32_t m_nBlocks;
148  uint32_t m_nSymSecParam;
149  uint32_t m_nBaseOTs;
150 
151  crypto* m_cCrypt;
152 
153  CSocket* m_vSockets;
154  CBitVector m_vU;
155  CBitVector* m_vValues;
156  MaskingFunction* m_fMaskFct;
157  AES_KEY_CTX* m_vKeySeeds;
158  OTBlock* m_sBlockHead;
159  OTBlock* m_sBlockTail;
160  CLock* m_lSendLock;
161  BYTE* m_vSeed;
162 
163 #ifdef FIXED_KEY_AES_HASHING
164  AES_KEY_CTX* m_kCRFKey;
165 #endif
166 
167  class OTSenderThread: public CThread {
168  public:
169  OTSenderThread(uint32_t id, uint32_t nOTs, OTExtSnd* ext) {
170  senderID = id;
171  numOTs = nOTs;
172  callback = ext;
173  success = false;
174  }
175  ;
176  ~OTSenderThread() {
177  }
178  ;
179  void ThreadMain() {
180  success = callback->OTSenderRoutine(senderID, numOTs);
181  }
182  ;
183  private:
184  uint32_t senderID;
185  uint32_t numOTs;
186  OTExtSnd* callback;
187  BOOL success;
188  };
189 
190 };
191 
192 class OTExtRec {
193  /*
194  * OT receiver part
195  * Input:
196  * nSndVals: perform a 1-out-of-nSndVals OT
197  * nOTs: the number of OTs that shall be performed
198  * choices: a vector containing nBaseOTs choices in the domain 0-(SndVals-1)
199  * ret: returns the resulting bit representations, Has to initialized to a byte size of: nOTs * state.field_size
200  *
201  * Output: was the execution successful?
202  */
203 public:
204  OTExtRec(uint32_t nSndVals, uint32_t nOTs, uint32_t bitlength, crypto* crypt, CSocket* sock, BYTE* keybytes, CBitVector& choices, CBitVector& ret, BYTE protocol,
205  int nbaseOTs = -1, int nbaseseeds = -1) {
206  Init(nSndVals, crypt, sock, keybytes, nbaseOTs, nbaseseeds);
207  m_nOTs = nOTs;
208  m_nChoices = choices;
209  m_nRet = ret;
210  m_nBitLength = bitlength;
211  m_eOTFlav = protocol;
212  }
213  ;
214  OTExtRec(uint32_t nSndVals, crypto* crypt, CSocket* sock, BYTE* keybytes, int nbaseOTs = -1, int nbaseseeds = -1) {
215  Init(nSndVals, crypt, sock, keybytes, nbaseOTs, nbaseseeds);
216  }
217  ;
218 
219  void Init(uint32_t nSndVals, crypto* crypt, CSocket* sock, BYTE* keybytes, int nbaseOTs, int nbaseseeds) {
220  m_nSndVals = nSndVals;
221  m_vSockets = sock;
222  m_cCrypt = crypt;
223  m_nSymSecParam = m_cCrypt->get_seclvl().symbits;
224  m_nBaseOTs = m_nSymSecParam;
225  if (nbaseOTs != -1)
226  m_nBaseOTs = nbaseOTs;
227  int keyseeds = m_nBaseOTs;
228  if (nbaseseeds != -1)
229  keyseeds = nbaseseeds;
230 
231  m_nCounter = 0;
232  m_vKeySeedMtx = (AES_KEY_CTX*) malloc(sizeof(AES_KEY_CTX) * keyseeds * nSndVals);
233  InitAESKey(m_vKeySeedMtx, keybytes, keyseeds * nSndVals, m_cCrypt);
234 
235 #ifdef FIXED_KEY_AES_HASHING
236  m_kCRFKey = (AES_KEY_CTX*) malloc(sizeof(AES_KEY_CTX));
237  m_cCrypt->init_aes_key(m_kCRFKey, (uint8_t*) fixed_key_aes_seed);
238 #endif
239  }
240 
241  ~OTExtRec() {
242  //free(m_vKeySeedMtx);
243  }
244  ;
245 
246  BOOL receive(uint32_t numOTs, uint32_t bitlength, CBitVector& choices, CBitVector& ret, BYTE type, uint32_t numThreads, MaskingFunction* maskfct);
247 
248  BOOL receive(uint32_t numThreads);
249  BOOL OTReceiverRoutine(uint32_t id, uint32_t myNumOTs);
250  void ReceiveAndProcess(uint32_t numThreads);
251  void BuildMatrices(CBitVector& T, CBitVector& SndBuf, uint32_t numblocks, uint32_t ctr, BYTE* ctr_buf);
252  void HashValues(CBitVector& T, CBitVector& seedbuf, uint32_t ctr, uint32_t lim);
253  BOOL verifyOT(uint32_t myNumOTs);
254 
255 protected:
256  BYTE m_eOTFlav;
257  uint32_t m_nSndVals;
258  uint32_t m_nOTs;
259  uint32_t m_nBitLength;
260  uint32_t m_nCounter;
261  uint32_t m_nSymSecParam;
262  uint32_t m_nBaseOTs;
263 
264  crypto* m_cCrypt;
265 
266  CSocket* m_vSockets;
267  CBitVector m_nChoices;
268  CBitVector m_nRet;
269  CBitVector m_vTempOTMasks;
270  MaskingFunction* m_fMaskFct;
271  AES_KEY_CTX* m_vKeySeedMtx;
272 
273 #ifdef FIXED_KEY_AES_HASHING
274  AES_KEY_CTX* m_kCRFKey;
275 #endif
276 
277  class OTReceiverThread: public CThread {
278  public:
279  OTReceiverThread(uint32_t id, uint32_t nOTs, OTExtRec* ext) {
280  receiverID = id;
281  numOTs = nOTs;
282  callback = ext;
283  success = false;
284  }
285  ;
286  ~OTReceiverThread() {
287  }
288  ;
289  void ThreadMain() {
290  success = callback->OTReceiverRoutine(receiverID, numOTs);
291  }
292  ;
293  private:
294  uint32_t receiverID;
295  uint32_t numOTs;
296  OTExtRec* callback;
297  BOOL success;
298  };
299 
300 };
301 
302 #ifdef FIXED_KEY_AES_HASHING
303 inline void FixedKeyHashing(AES_KEY_CTX* aeskey, BYTE* outbuf, BYTE* inbuf, BYTE* tmpbuf, uint64_t id, uint32_t bytessecparam, crypto* crypt) {
304 #ifdef HIGH_SPEED_ROT_LT
305  ((uint64_t*) tmpbuf)[0] = id ^ ((uint64_t*) inbuf)[0];
306  ((uint64_t*) tmpbuf)[1] = ((uint64_t*) inbuf)[1];
307 #else
308  memset(tmpbuf, 0, AES_BYTES);
309  memcpy(tmpbuf, (BYTE*) (&id), sizeof(int));
310 
311  for (int i = 0; i < bytessecparam; i++) {
312  tmpbuf[i] = tmpbuf[i] ^ inbuf[i];
313  }
314 #endif
315 
316  crypt->encrypt(aeskey, outbuf, tmpbuf, AES_BYTES);
317 
318 #ifdef HIGH_SPEED_ROT_LT
319  ((uint64_t*) outbuf)[0] ^= ((uint64_t*) inbuf)[0];
320  ((uint64_t*) outbuf)[1] ^= ((uint64_t*) inbuf)[1];
321 #else
322  for (int i = 0; i < bytessecparam; i++) {
323  outbuf[i] = outbuf[i] ^ inbuf[i];
324  }
325 #endif
326 }
327 #endif
328 
329 #endif /* __OT_EXTENSION_H_ */
void Create(uint64_t bits)
Definition: cbitvector.cpp:40
Definition: ot-extension.h:167
Definition: crypto.h:58
Definition: socket.h:24
Definition: ot-extension.h:192
Definition: thread.h:125
Definition: ot-extension.h:67
void SetBit(int idx, BYTE b)
Definition: cbitvector.h:431
BYTE * GetArr()
Definition: cbitvector.h:777
Definition: ot-extension.h:44
Definition: maskingfunction.h:25
Definition: thread.h:170
Masking Function implementation.
Definition: cbitvector.h:123
void Copy(CBitVector &vec)
Definition: cbitvector.h:369
Definition: ot-extension.h:277