emp-toolkit
io_channel.h
Go to the documentation of this file.
1 #include "block.h"
2 #include "utils_ec.h"
3 #include "prg.h"
4 #ifndef IO_CHANNEL_H__
5 #define IO_CHANNEL_H__
6 
11 template<typename T>
12 class IOChannel { public:
13  PRG *prg = nullptr;
14  void send_data(const void * data, int nbyte) {
15  static_cast<T*>(this)->send_data_impl(data, nbyte);
16  }
17  void recv_data(void * data, int nbyte) {
18  static_cast<T*>(this)->recv_data_impl(data, nbyte);
19  }
20 
22  if(prg != nullptr)
23  delete prg;
24  }
25 
26  void set_key(const block* b) {
27  if(b == nullptr) {
28  delete prg;
29  prg = nullptr;
30  return;
31  }
32  if(prg != nullptr)
33  delete prg;
34  prg = new PRG(b);
35  }
36 
37  void send_data_enc(const void * data, int len) {
38  char * tmp = new char[len];
39  prg->random_data(tmp, len);
40  for(int i = 0; i < len; ++i)
41  tmp[i] ^= ((char *)data)[i];
42  send_data(tmp, len);
43  delete[] tmp;
44  }
45 
46  void send_block_enc(const block* data, int len) {
47  block * tmp = new block[len];
48  prg->random_block(tmp, len);
49  for(int i = 0; i < len; ++i)
50  tmp[i] = xorBlocks(data[i], tmp[i]);
51  send_block(tmp, len);
52  delete[] tmp;
53  }
54  void send_bn_enc(const bn_t * bn, size_t num) {
55  uint64_t buffer[4];
56  uint64_t buffer2[4];
57  uint32_t bn_size;
58  for(size_t i = 0; i < num; ++i) {
59  bn_size = bn_size_raw(bn[i]);
60  prg->random_data(buffer2, bn_size*sizeof(uint64_t));
61  bn_write_raw(buffer, bn_size, bn[i]);
62  for(size_t k = 0; k < bn_size; ++k)
63  buffer[k]^=buffer2[k];
64  send_data(&bn_size, sizeof(int));
65  send_data(buffer, bn_size*sizeof(uint64_t));
66  }
67  }
68 
69  void recv_data_enc(void * data, int len) {
70  recv_data(data, len);
71  if(prg == nullptr)return;
72  char * tmp = new char[len];
73  prg->random_data(tmp, len);
74  for(int i = 0; i < len; ++i)
75  ((char *)data)[i] ^= tmp[i];
76  delete[] tmp;
77  }
78  void recv_block_enc(block* data, int len) {
79  recv_block(data, len);
80  if(prg == nullptr)return;
81  block * tmp = new block[len];
82  prg->random_block(tmp, len);
83  for(int i = 0; i < len; ++i)
84  data[i] = xorBlocks(data[i], tmp[i]);
85  delete[] tmp;
86  }
87  void send_eb_enc(const eb_t * eb, size_t num) {
88  uint8_t buffer[EB_SIZE];
89  uint8_t buffer2[EB_SIZE];
90  for(size_t i = 0; i < num; ++i) {
91  uint32_t eb_size = eb_size_bin(eb[i], ECC_PACK);
92  send_data(&eb_size, sizeof(int));
93  prg->random_data(buffer2, eb_size);
94  eb_write_bin(buffer, eb_size, eb[i], ECC_PACK);
95  for(size_t k = 0; k < eb_size; ++k) {
96  buffer[k] = (char)(buffer[k]^buffer2[k]);
97  }
98  send_data(buffer, eb_size*sizeof(uint8_t));
99  }
100  }
101 
102  void recv_eb_enc(eb_t* eb, size_t num) {
103  uint8_t buffer[EB_SIZE];
104  uint8_t buffer2[EB_SIZE];
105  uint32_t eb_size;
106  for(size_t i = 0; i < num; ++i) {
107  recv_data(&eb_size, sizeof(int));
108  recv_data(buffer, eb_size*sizeof(uint8_t));
109  if(prg == nullptr)continue;
110  prg->random_data(buffer2, eb_size);
111  for(size_t k = 0; k < eb_size; ++k) {
112  buffer[k] = (char)(buffer[k]^buffer2[k]);
113  }
114  eb_read_bin(eb[i], buffer, eb_size);
115  }
116  }
117  void recv_bn_enc(bn_t* bn, size_t num) {
118  uint64_t buffer[4];
119  uint64_t buffer2[4];
120  uint32_t bn_size;
121  for(size_t i = 0; i < num; ++i) {
122  recv_data(&bn_size, sizeof(int));
123  recv_data(buffer, bn_size*sizeof(uint64_t));
124  if(prg == nullptr)continue;
125  prg->random_data(buffer2, sizeof(uint64_t)*bn_size);
126  for(size_t k = 0; k < bn_size; ++k)
127  buffer[k] ^=buffer2[k];
128  bn_read_raw(bn[i], buffer, bn_size);
129  }
130  }
131 
132  void send_block(const block* data, int nblock) {
133  send_data(data, nblock*sizeof(block));
134  }
135 
136  void recv_block(block* data, int nblock) {
137  recv_data(data, nblock*sizeof(block));
138  }
139 
140  void send_eb(const eb_t * eb, size_t num) {
141  uint8_t buffer[EB_SIZE];
142  for(size_t i = 0; i < num; ++i) {
143  int eb_size = eb_size_bin(eb[i], ECC_PACK);
144  eb_write_bin(buffer, eb_size, eb[i], ECC_PACK);
145  send_data(&eb_size, sizeof(int));
146  send_data(buffer, eb_size*sizeof(uint8_t));
147  }
148  }
149 
150  void recv_eb(eb_t* eb, size_t num) {
151  uint8_t buffer[EB_SIZE];
152  int eb_size;
153  for(size_t i = 0; i < num; ++i) {
154  recv_data(&eb_size, sizeof(int));
155  recv_data(buffer, eb_size*sizeof(uint8_t));
156  eb_read_bin(eb[i], buffer, eb_size);
157  }
158  }
159 
160  void send_bn(const bn_t * bn, size_t num) {
161  uint64_t buffer[4];
162  int bn_size;
163  for(size_t i = 0; i < num; ++i) {
164  bn_size = bn_size_raw(bn[i]);
165  bn_write_raw(buffer, bn_size, bn[i]);
166  send_data(&bn_size, sizeof(int));
167  send_data(buffer, bn_size*sizeof(uint64_t));
168  }
169  }
170 
171  void recv_bn(bn_t* bn, size_t num) {
172  uint64_t buffer[4];
173  int bn_size;
174  for(size_t i = 0; i < num; ++i) {
175  recv_data(&bn_size, sizeof(int));
176  recv_data(buffer, bn_size*sizeof(uint64_t));
177  bn_read_raw(bn[i], buffer, bn_size);
178  }
179  }
180 };
182 #endif// IO_CHANNEL_H__
void recv_eb_enc(eb_t *eb, size_t num)
Definition: io_channel.h:102
void send_bn(const bn_t *bn, size_t num)
Definition: io_channel.h:160
void send_data(const void *data, int nbyte)
Definition: io_channel.h:14
void send_bn_enc(const bn_t *bn, size_t num)
Definition: io_channel.h:54
void recv_data(void *data, int nbyte)
Definition: io_channel.h:17
__m128i block
Definition: block.h:8
void recv_bn_enc(bn_t *bn, size_t num)
Definition: io_channel.h:117
block xorBlocks(block x, block y)
Definition: block.h:35
#define ECC_PACK
Definition: utils_ec.h:9
void recv_data_enc(void *data, int len)
Definition: io_channel.h:69
void set_key(const block *b)
Definition: io_channel.h:26
void recv_block_enc(block *data, int len)
Definition: io_channel.h:78
void recv_eb(eb_t *eb, size_t num)
Definition: io_channel.h:150
void send_eb(const eb_t *eb, size_t num)
Definition: io_channel.h:140
void random_block(block *data, int nblocks=1)
Definition: prg.h:75
void send_data_enc(const void *data, int len)
Definition: io_channel.h:37
#define EB_SIZE
Definition: utils_ec.h:11
void send_block_enc(const block *data, int len)
Definition: io_channel.h:46
PRG * prg
Definition: io_channel.h:13
Definition: prg.h:16
void send_eb_enc(const eb_t *eb, size_t num)
Definition: io_channel.h:87
~IOChannel()
Definition: io_channel.h:21
void random_data(void *data, int nbytes)
Definition: prg.h:49
void recv_bn(bn_t *bn, size_t num)
Definition: io_channel.h:171
void send_block(const block *data, int nblock)
Definition: io_channel.h:132
void recv_block(block *data, int nblock)
Definition: io_channel.h:136
Definition: io_channel.h:12