emp-toolkit
prp.h
Go to the documentation of this file.
1 #include "block.h"
2 #include "config.h"
3 #include "garble/aes.h"
4 #include <stdio.h>
5 #ifndef PRP_H__
6 #define PRP_H__
7 
11 class PRP { public:
13  PRP(const char * seed = fix_key) {
14  aes = new AES_KEY;
15  aes_set_key(seed);
16  }
17  ~PRP() {
18  delete aes;
19  }
20  void aes_set_key(const char * key) {
21  __m128i v = _mm_load_si128((__m128i*)&key[0]);
22  aes_set_key(v);
23  }
24  void aes_set_key(const block& v) {
25  AES_set_encrypt_key(v, aes);
26  }
27 
28 
29  void permute_block(block *data, int nblocks) {
30  int i = 0;
31  for(; i < nblocks-AES_BATCH_SIZE; i+=AES_BATCH_SIZE) {
32  AES_ecb_encrypt_blks(data+i, AES_BATCH_SIZE, aes);
33  }
34  AES_ecb_encrypt_blks(data+i, (AES_BATCH_SIZE > nblocks-i) ? nblocks-i:AES_BATCH_SIZE, aes);
35  }
36 
37  void permute_data(uint8_t*data, int nbytes) {
38  permute_block((block *)data, nbytes/16);
39  if (nbytes % 16 != 0) {
40  uint8_t extra[16];
41  memset(extra, 0, 16);
42  memcpy(extra, (nbytes/16*16)+(char *) data, nbytes%16);
43  permute_block((block*)extra, 1);
44  memcpy((nbytes/16*16)+(char *) data, &extra, nbytes%16);
45  }
46  }
47 
48  block H(block in, uint64_t id) {
49  in = double_block(in);
50  __m128i k_128 = _mm_loadl_epi64( (__m128i const *) (&id));
51  in = xorBlocks(in, k_128);
52  block t = in;
53  permute_block(&t, 1);
54  in = xorBlocks(in, t);
55  return in;
56  }
57  template<int n>
58  void H(block out[n], block in[n], uint64_t id) {
59  block scratch[n];
60  for(int i = 0; i < n; ++i) {
61  out[i] = scratch[i] = xorBlocks(double_block(in[i]), _mm_loadl_epi64( (__m128i const *) (&id)));
62  ++id;
63  }
64  permute_block(scratch, n);
65  xorBlocks_arr(out, scratch, out, n);
66  }
67 
68  void Hn(block*out, block* in, uint64_t id, int length, block * scratch = nullptr) {
69  bool del = false;
70  if(scratch == nullptr) {
71  del = true;
72  scratch = new block[length];
73  }
74  for(int i = 0; i < length; ++i){
75  out[i] = scratch[i] = xorBlocks(double_block(in[i]), _mm_loadl_epi64( (__m128i const *) (&id)));
76  ++id;
77  }
78  permute_block(scratch, length);
79  xorBlocks_arr(out, scratch, out, length);
80  if(del) {
81  delete[] scratch;
82  scratch = nullptr;
83  }
84  }
85 };
87 #endif// PRP_H__
void permute_data(uint8_t *data, int nbytes)
Definition: prp.h:37
#define AES_BATCH_SIZE
Definition: config.h:4
__m128i block
Definition: block.h:8
block xorBlocks(block x, block y)
Definition: block.h:35
void aes_set_key(const block &v)
Definition: prp.h:24
void Hn(block *out, block *in, uint64_t id, int length, block *scratch=nullptr)
Definition: prp.h:68
Definition: aes.h:57
const char fix_key[]
Definition: block.h:130
block H(block in, uint64_t id)
Definition: prp.h:48
Definition: prp.h:11
AES_KEY * aes
Definition: prp.h:12
~PRP()
Definition: prp.h:17
void aes_set_key(const char *key)
Definition: prp.h:20
PRP(const char *seed=fix_key)
Definition: prp.h:13
void H(block out[n], block in[n], uint64_t id)
Definition: prp.h:58
void xorBlocks_arr(block *res, const block *x, const block *y, int nblocks)
Definition: block.h:37
void permute_block(block *data, int nblocks)
Definition: prp.h:29