ABY Framework  1.0
Arithmetic Bool Yao Framework
 All Classes Files Functions Variables Enumerations Enumerator Macros
xormasking.h
Go to the documentation of this file.
1 
19 #ifndef XORMASKING_H_
20 #define XORMASKING_H_
21 
22 #include "maskingfunction.h"
23 
24 class XORMasking: public MaskingFunction {
25 public:
26  XORMasking(uint32_t bitlength) {
27  init(bitlength);
28  }
29  ;
30  XORMasking(uint32_t bitlength, CBitVector& delta) {
31  m_vDelta = δ
32  init(bitlength);
33  }
34  ;
35  ~XORMasking() {
36  }
37  ;
38 
39  void init(uint32_t bitlength) {
40  m_nBitLength = bitlength;
41  }
42 
43  void Mask(uint32_t progress, uint32_t processedOTs, CBitVector* values, CBitVector* snd_buf, BYTE protocol) {
44  uint32_t nsndvals = 2;
45 
46  if (protocol == G_OT) {
47  snd_buf[0].XORBytes(values[0].GetArr() + ceil_divide(progress * m_nBitLength, 8), 0, ceil_divide(processedOTs * m_nBitLength, 8));
48  snd_buf[1].XORBytes(values[1].GetArr() + ceil_divide(progress * m_nBitLength, 8), 0, ceil_divide(processedOTs * m_nBitLength, 8));
49  } else if (protocol == C_OT) {
50  values[0].SetBytes(snd_buf[0].GetArr(), ceil_divide(progress * m_nBitLength, 8), ceil_divide(processedOTs * m_nBitLength, 8)); //.SetBits(hash_buf, i*m_nBitLength, m_nBitLength);
51  int bitPos = progress * m_nBitLength;
52  int length = processedOTs * m_nBitLength;
53  int bytePos = ceil_divide(bitPos, 8);
54 
55  values[1].SetBits(values[0].GetArr() + bytePos, bitPos, length);
56  values[1].XORBits(m_vDelta->GetArr() + bytePos, bitPos, length);
57  snd_buf[1].XORBits(values[1].GetArr() + bytePos, 0, length);
58  }
59  else if (protocol == R_OT) {
60  values[0].SetBytes(snd_buf[0].GetArr(), ceil_divide(progress * m_nBitLength, 8), ceil_divide(processedOTs * m_nBitLength, 8));
61  values[1].SetBytes(snd_buf[1].GetArr(), ceil_divide(progress * m_nBitLength, 8), ceil_divide(processedOTs * m_nBitLength, 8));
62  }
63  }
64  ;
65 
66  //output already has to contain the masks
67  void UnMask(uint32_t progress, uint32_t processedOTs, CBitVector& choices, CBitVector& output, CBitVector& rcv_buf, CBitVector& tmpmask, BYTE protocol) {
68  uint32_t bytelen = ceil_divide(m_nBitLength, 8);
69  uint32_t gprogress = progress * bytelen;
70  uint32_t lim = progress + processedOTs;
71 
72  if (protocol == G_OT) {
73  for (uint32_t u, i = progress, offset = processedOTs * bytelen, l = 0; i < lim; i++, gprogress += bytelen, l += bytelen) {
74  //TODO make this working for single bits
75  u = (uint32_t) choices.GetBitNoMask(i);
76  output.SetXOR(rcv_buf.GetArr() + (u * offset) + l, tmpmask.GetArr() + gprogress, gprogress, bytelen);
77  }
78 
79  } else if (protocol == C_OT)
80  {
81  int gprogress = progress * bytelen;
82  output.Copy(tmpmask.GetArr() + gprogress, gprogress, bytelen * processedOTs);
83  for (int i = progress, l = 0; i < lim; i++, l += bytelen, gprogress += bytelen) {
84  if (choices.GetBitNoMask(i)) {
85  //TODO make this working for single bits
86  output.XORBytes(rcv_buf.GetArr() + l, gprogress, bytelen);
87  }
88  }
89  } else if (protocol == R_OT) {
90  //The seed expansion has already been performed, so do nothing
91  }
92  }
93  ;
94 
95  void expandMask(CBitVector& out, BYTE* sbp, uint32_t offset, uint32_t processedOTs, uint32_t bitlength, crypto* crypt) {
96 
97  if (bitlength <= AES_KEY_BITS) {
98  for (uint32_t i = 0; i < processedOTs; i++, sbp += AES_KEY_BYTES) {
99  out.SetBits(sbp, (uint64_t) (offset + i) * bitlength, (uint64_t) bitlength);
100  }
101  } else {
102  BYTE m_bBuf[AES_BYTES];
103  BYTE ctr_buf[AES_BYTES] = { 0 };
104  uint32_t counter = *((uint32_t*) ctr_buf);
105  AES_KEY_CTX tkey;
106  for (uint32_t i = 0, rem; i < processedOTs; i++, sbp += AES_KEY_BYTES) {
107  crypt->init_aes_key(&tkey, sbp);
108  for (counter = 0; counter < bitlength / AES_BITS; counter++) {
109  crypt->encrypt(&tkey, m_bBuf, ctr_buf, AES_BYTES);
110  out.SetBits(m_bBuf, ((uint64_t) offset + i) * bitlength + (counter * AES_BITS), (uint64_t) AES_BITS);
111  }
112  //the final bits
113  if ((rem = bitlength - (counter * AES_BITS)) > 0) {
114  crypt->encrypt(&tkey, m_bBuf, ctr_buf, AES_BYTES);
115  out.SetBits(m_bBuf, ((uint64_t) offset + i) * bitlength + (counter * AES_BITS), (uint64_t) rem);
116  }
117  }
118  }
119  }
120 
121 private:
122  CBitVector* m_vDelta;
123  uint32_t m_nBitLength;
124 };
125 
126 #endif /* XORMASKING_H_ */
void SetXOR(BYTE *p, BYTE *q, int pos, int len)
Definition: cbitvector.cpp:337
BYTE GetBitNoMask(int idx)
Definition: cbitvector.h:467
void XORBytes(BYTE *p, int pos, int len)
Definition: cbitvector.cpp:269
Definition: crypto.h:58
void XORBits(BYTE *p, int pos, int len)
Definition: cbitvector.cpp:210
Definition: xormasking.h:24
void SetBytes(BYTE *p, int pos, int len)
Definition: cbitvector.cpp:302
void SetBits(BYTE *p, uint64_t pos, uint64_t len)
Definition: cbitvector.cpp:103
BYTE * GetArr()
Definition: cbitvector.h:777
Definition: maskingfunction.h:25
Masking Function implementation.
Definition: cbitvector.h:123
void Copy(CBitVector &vec)
Definition: cbitvector.h:369