emp-toolkit
garble_gate_halfgates.h
Go to the documentation of this file.
1 #ifndef LIBGARBLE_GARBLE_GATE_HALFGATES_H
2 #define LIBGARBLE_GARBLE_GATE_HALFGATES_H
3 
4 #include "garble.h"
5 #include "aes.h"
6 
7 #include <assert.h>
8 #include <string.h>
9 
10 static inline void
11 garble_gate_eval_halfgates(garble_gate_type_e type, block A, block B, block *out,
12  const block *table, uint64_t idx, const AES_KEY *key)
13 {
14  if (type == GARBLE_GATE_XOR) {
15  *out = garble_xor(A, B);
16  } else {
17  block HA, HB, W;
18  int sa, sb;
19  block tweak1, tweak2;
20 
21  sa = garble_lsb(A);
22  sb = garble_lsb(B);
23 
24  tweak1 = garble_make_block(2 * idx, (long) 0);
25  tweak2 = garble_make_block(2 * idx + 1, (long) 0);
26 
27  {
28  block keys[2];
29  block masks[2];
30 
31  keys[0] = garble_xor(garble_double(A), tweak1);
32  keys[1] = garble_xor(garble_double(B), tweak2);
33  masks[0] = keys[0];
34  masks[1] = keys[1];
35  AES_ecb_encrypt_blks(keys, 2, key);
36  HA = garble_xor(keys[0], masks[0]);
37  HB = garble_xor(keys[1], masks[1]);
38  }
39 
40  W = garble_xor(HA, HB);
41  if (sa)
42  W = garble_xor(W, table[0]);
43  if (sb) {
44  W = garble_xor(W, table[1]);
45  W = garble_xor(W, A);
46  }
47  *out = W;
48  }
49 }
50 
51 static inline void
52 garble_gate_garble_halfgates(garble_gate_type_e type, block A0, block A1, block B0,
53  block B1, block *out0, block *out1, block delta,
54  block *table, uint64_t idx, const AES_KEY *key)
55 {
56  if (type == GARBLE_GATE_XOR) {
57  *out0 = garble_xor(A0, B0);
58  *out1 = garble_xor(*out0, delta);
59  } else {
60  long pa = garble_lsb(A0);
61  long pb = garble_lsb(B0);
62  block tweak1, tweak2;
63  block HA0, HA1, HB0, HB1;
64  block tmp, W0;
65 
66  tweak1 = garble_make_block(2 * idx, (uint64_t) 0);
67  tweak2 = garble_make_block(2 * idx + 1, (uint64_t) 0);
68 
69  {
70  block masks[4], keys[4];
71 
72  keys[0] = garble_xor(garble_double(A0), tweak1);
73  keys[1] = garble_xor(garble_double(A1), tweak1);
74  keys[2] = garble_xor(garble_double(B0), tweak2);
75  keys[3] = garble_xor(garble_double(B1), tweak2);
76  memcpy(masks, keys, sizeof keys);
77  AES_ecb_encrypt_blks(keys, 4, key);
78  HA0 = garble_xor(keys[0], masks[0]);
79  HA1 = garble_xor(keys[1], masks[1]);
80  HB0 = garble_xor(keys[2], masks[2]);
81  HB1 = garble_xor(keys[3], masks[3]);
82  }
83  table[0] = garble_xor(HA0, HA1);
84  if (pb)
85  table[0] = garble_xor(table[0], delta);
86  W0 = HA0;
87  if (pa)
88  W0 = garble_xor(W0, table[0]);
89  tmp = garble_xor(HB0, HB1);
90  table[1] = garble_xor(tmp, A0);
91  W0 = garble_xor(W0, HB0);
92  if (pb)
93  W0 = garble_xor(W0, tmp);
94 
95  *out0 = W0;
96  *out1 = garble_xor(*out0, delta);
97  }
98 }
99 
100 #endif
#define garble_lsb(x)
Definition: block.h:15
#define garble_make_block(X, Y)
Definition: block.h:16
__m128i block
Definition: block.h:8
Definition: garble.h:30
Definition: aes.h:57
garble_gate_type_e
Definition: garble.h:25
#define garble_double(B)
Definition: block.h:17
#define garble_xor(x, y)
Definition: block.h:10