emp-toolkit
garble_gate_privacy_free.h
Go to the documentation of this file.
1 #ifndef LIBGARBLE_GARBLE_GATE_PRIVACY_FREE_H
2 #define LIBGARBLE_GARBLE_GATE_PRIVACY_FREE_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_privacy_free(garble_gate_type_e type, block A, block B,
12  block *out, const block *table, uint64_t idx,
13  const AES_KEY *key)
14 {
15  if (type == GARBLE_GATE_XOR) {
16  *out = garble_xor(A, B);
17  } else {
18  block HA, W;
19  bool sa;
20  block tweak;
21 
22  sa = garble_lsb(A);
23 
24  tweak = garble_make_block(2 * idx, (uint64_t) 0);
25 
26  {
27  block tmp, mask;
28 
29  tmp = garble_xor(garble_double(A), tweak);
30  mask = tmp;
31  AES_ecb_encrypt_blks(&tmp, 1, key);
32  HA = garble_xor(tmp, mask);
33  }
34  if (sa) {
35  *((char *) &HA) |= 0x01;
36  W = garble_xor(HA, table[0]);
37  W = garble_xor(W, B);
38  } else {
39  *((char *) &HA) &= 0xfe;
40  W = HA;
41  }
42  *out = W;
43  }
44 }
45 
46 
47 static inline void
48 garble_gate_garble_privacy_free(garble_gate_type_e type, block A0, block A1,
49  block B0, block B1, block *out0, block *out1,
50  block delta, block *table, uint64_t idx,
51  const AES_KEY *key)
52 {
53 #ifdef DEBUG
54  if ((*((char *) &A0) & 0x01) == 1
55  || (*((char *) &B0) & 0x01) == 1
56  || (*((char *) &A1) & 0x01) == 0
57  || (*((char *) &B1) & 0x01) == 0) {
58  assert(false && "invalid lsb in block");
59  }
60 #endif
61 
62  if (type == GARBLE_GATE_XOR) {
63  *out0 = garble_xor(A0, B0);
64  *out1 = garble_xor(*out0, delta);
65  } else {
66  block tweak, tmp;
67  block HA0, HA1;
68 
69  tweak = garble_make_block(2 * idx, (long) 0);
70  {
71  block masks[2], keys[2];
72 
73  keys[0] = garble_xor(garble_double(A0), tweak);
74  keys[1] = garble_xor(garble_double(A1), tweak);
75  memcpy(masks, keys, sizeof keys);
76  AES_ecb_encrypt_blks(keys, 2, key);
77  HA0 = garble_xor(keys[0], masks[0]);
78  HA1 = garble_xor(keys[1], masks[1]);
79  }
80  *((char *) &HA0) &= 0xfe;
81  *((char *) &HA1) |= 0x01;
82  tmp = garble_xor(HA0, HA1);
83  table[0] = garble_xor(tmp, B0);
84  *out0 = HA0;
85  *out1 = garble_xor(HA0, delta);
86  }
87 }
88 
89 #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