ABY Framework  1.0
Arithmetic Bool Yao Framework
 All Classes Files Functions Variables Enumerations Enumerator Macros
crypto.h
Go to the documentation of this file.
1 
19 #ifndef CRYPTO_H_
20 #define CRYPTO_H_
21 
22 #include <openssl/evp.h>
23 #include <openssl/sha.h>
24 #include <fstream>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 
28 #include "../typedefs.h"
29 #include "../constants.h"
30 #include "gmp-pk-crypto.h"
31 #include "ecc-pk-crypto.h"
32 #include "../socket.h"
33 
34 const uint8_t ZERO_IV[AES_BYTES] = { 0 };
35 
36 const uint8_t const_seed[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
37 
38 enum bc_mode {
39  ECB, CBC
40 };
41 
42 typedef EVP_CIPHER_CTX AES_KEY_CTX;
43 
44 /* Predefined security levels,
45  * ST (SHORTTERM) = 1024/160/163 bit public key, 80 bit private key
46  * MT (MEDIUMTERM) = 2048/192/233 bit public key, 112 bit private key
47  * LT (LONGTERM) = 3072/256/283 bit public key, 128 bit private key
48  * XLT (EXTRA LONGTERM) = 7680/384/409 bit public key, 192 bit private key
49  * XXLT (EXTRA EXTRA LONGTERM) = 15360/512/571 bit public key, 256 bit private key
50  */
51 
52 struct prf_state_ctx {
53  AES_KEY_CTX aes_key;
54  uint64_t* ctr;
55 };
56 
57 //TODO: not thread-safe when multiple threads generate random data using the same seed
58 class crypto {
59 
60 public:
61 
62  crypto(uint32_t symsecbits, uint8_t* seed);
63  crypto(uint32_t symsecbits);
64  ~crypto();
65 
66  //Randomness generation routines
67  void gen_rnd(uint8_t* resbuf, uint32_t numbytes);
68  //void gen_rnd(prf_state_ctx* prf_state, uint8_t* resbuf, uint32_t nbytes);
69  void gen_rnd_uniform(uint8_t* resbuf, uint64_t mod);
70  void gen_rnd_perm(uint32_t* perm, uint32_t neles);
71 
72  //Encryption routines
73  void encrypt(uint8_t* resbuf, uint8_t* inbuf, uint32_t ninbytes);
74  void decrypt(uint8_t* resbuf, uint8_t* inbuf, uint32_t ninbytes);
75 
76  //Hash routines
77  void hash(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes);
78  void hash_ctr(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes, uint32_t ctr);
79  void fixed_key_aes_hash(AES_KEY_CTX* aes_key, uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes);
80  void fixed_key_aes_hash_ctr(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes);
81 
82  //Key seed routines
83  void seed_aes_hash(uint8_t* seed, bc_mode mode = ECB, const uint8_t* iv = ZERO_IV);
84  void seed_aes_enc(uint8_t* seed, bc_mode mode = ECB, const uint8_t* iv = ZERO_IV);
85 
86  //External encryption routines
87  void init_aes_key(AES_KEY_CTX* aes_key, uint8_t* seed, bc_mode mode = ECB, const uint8_t* iv = ZERO_IV);
88  void init_aes_key(AES_KEY_CTX* aes_key, uint32_t symbits, uint8_t* seed, bc_mode mode = ECB, const uint8_t* iv = ZERO_IV);
89  uint32_t get_aes_key_bytes();
90  void encrypt(AES_KEY_CTX* enc_key, uint8_t* resbuf, uint8_t* inbuf, uint32_t ninbytes);
91  void decrypt(AES_KEY_CTX* dec_key, uint8_t* resbuf, uint8_t* inbuf, uint32_t ninbytes);
92 
93  pk_crypto* gen_field(field_type ftype);
94 
95  seclvl get_seclvl() {
96  return secparam;
97  }
98  ;
99  uint32_t get_hash_bytes();
100 
101  void gen_common_seed(prf_state_ctx* aes_key, CSocket& sock);
102  void init_prf_state(prf_state_ctx* prf_state, uint8_t* seed);
103  void free_prf_state(prf_state_ctx* prf_state);
104 private:
105  void seed_aes_key(AES_KEY_CTX* aeskey, uint8_t* seed, bc_mode mode = ECB, const uint8_t* iv = ZERO_IV, bool encrypt = true);
106  void seed_aes_key(AES_KEY_CTX* aeskey, uint32_t symseclvl, uint8_t* seed, bc_mode mode = ECB, const uint8_t* iv = ZERO_IV, bool encrypt = true);
107  void init(uint32_t symsecbits, uint8_t* seed);
108 
109 
110  AES_KEY_CTX aes_hash_key;
111  AES_KEY_CTX aes_enc_key;
112  AES_KEY_CTX aes_dec_key;
113  prf_state_ctx global_prf_state;
114 
115  seclvl secparam;
116  uint8_t* aes_hash_in_buf;
117  uint8_t* aes_hash_out_buf;
118  uint8_t* aes_hash_buf_y1;
119  uint8_t* aes_hash_buf_y2;
120 
121  uint8_t* sha_hash_buf;
122 
123  void (*hash_routine)(uint8_t*, uint32_t, uint8_t*, uint32_t, uint8_t*);
124 };
125 
126 //Some functions that should be useable without the class
127 void sha1_hash(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes, uint8_t* hash_buf);
128 void sha256_hash(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes, uint8_t* hash_buf);
129 void sha512_hash(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes, uint8_t* hash_buf);
130 void gen_secure_random(uint8_t* dest, uint32_t nbytes);
131 void gen_rnd_bytes(prf_state_ctx* prf_state, uint8_t* resbuf, uint32_t nbytes);
132 
133 seclvl get_sec_lvl(uint32_t symsecbits); //TODO pick a more elegant name (see crypto->get_seclvl())
134 
135 static const uint32_t m_nCodeWordBits = 256;
136 static const uint32_t m_nCodeWordBytes = m_nCodeWordBits / 8;
137 
138 #endif /* CRYPTO_H_ */
Definition: pk-crypto.h:30
Class with finite-field-cryptography operations (using the GMP library)
Class with ECC operations.
Definition: crypto.h:58
Definition: socket.h:24
Definition: crypto.h:52
Definition: typedefs.h:79