crypto.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /**
  2. \file crypto.cpp
  3. \author michael.zohner@ec-spride.de
  4. \copyright ABY - A Framework for Efficient Mixed-protocol Secure Two-party Computation
  5. Copyright (C) 2019 ENCRYPTO Group, TU Darmstadt
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. ABY is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. \brief Implementation of crypto primitive class
  17. */
  18. #include "crypto.h"
  19. #include "../socket.h"
  20. #include <openssl/sha.h>
  21. #include <openssl/des.h>
  22. #include "ecc-pk-crypto.h"
  23. #include "gmp-pk-crypto.h"
  24. #include <cstring>
  25. #include <iostream>
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28. #include <utility>
  29. crypto::crypto(uint32_t symsecbits, uint8_t* seed) {
  30. init(symsecbits, seed);
  31. }
  32. crypto::crypto(uint32_t symsecbits) {
  33. uint8_t* seed = (uint8_t*) malloc(sizeof(uint8_t) * AES_BYTES);
  34. gen_secure_random(seed, AES_BYTES);
  35. init(symsecbits, seed);
  36. free(seed);
  37. }
  38. crypto::~crypto() {
  39. free_prf_state(&global_prf_state);
  40. free(aes_hash_in_buf);
  41. free(aes_hash_out_buf);
  42. free(sha_hash_buf);
  43. free(aes_hash_buf_y1);
  44. free(aes_hash_buf_y2);
  45. #ifdef OPENSSL_OPAQUE_EVP_CIPHER_CTX
  46. clean_aes_key(&aes_hash_key);
  47. clean_aes_key(&aes_enc_key);
  48. clean_aes_key(&aes_dec_key);
  49. #endif
  50. }
  51. void crypto::init(uint32_t symsecbits, uint8_t* seed) {
  52. secparam = get_sec_lvl(symsecbits);
  53. #ifdef OPENSSL_OPAQUE_EVP_CIPHER_CTX
  54. aes_hash_key = EVP_CIPHER_CTX_new();
  55. aes_enc_key = EVP_CIPHER_CTX_new();
  56. aes_dec_key = EVP_CIPHER_CTX_new();
  57. #endif
  58. init_prf_state(&global_prf_state, seed);
  59. aes_hash_in_buf = (uint8_t*) malloc(AES_BYTES);
  60. aes_hash_out_buf = (uint8_t*) malloc(AES_BYTES);
  61. aes_hash_buf_y1 = (uint8_t*) malloc(AES_BYTES);
  62. aes_hash_buf_y2 = (uint8_t*) malloc(AES_BYTES);
  63. if (secparam.symbits == ST.symbits) {
  64. hash_routine = &sha1_hash;
  65. sha_hash_buf = (uint8_t*) malloc(SHA1_OUT_BYTES);
  66. } else if (secparam.symbits == MT.symbits) {
  67. hash_routine = &sha256_hash;
  68. sha_hash_buf = (uint8_t*) malloc(SHA256_OUT_BYTES);
  69. } else if (secparam.symbits == LT.symbits) {
  70. hash_routine = &sha256_hash;
  71. sha_hash_buf = (uint8_t*) malloc(SHA256_OUT_BYTES);
  72. } else if (secparam.symbits == XLT.symbits) {
  73. hash_routine = &sha512_hash;
  74. sha_hash_buf = (uint8_t*) malloc(SHA512_OUT_BYTES);
  75. } else if (secparam.symbits == XXLT.symbits) {
  76. hash_routine = &sha512_hash;
  77. sha_hash_buf = (uint8_t*) malloc(SHA512_OUT_BYTES);
  78. } else {
  79. hash_routine = &sha256_hash;
  80. sha_hash_buf = (uint8_t*) malloc(SHA256_OUT_BYTES);
  81. }
  82. }
  83. pk_crypto* crypto::gen_field(field_type ftype) {
  84. uint8_t* pkseed = (uint8_t*) malloc(sizeof(uint8_t) * (secparam.symbits >> 3));
  85. gen_rnd(pkseed, secparam.symbits >> 3);
  86. pk_crypto* ret;
  87. if (ftype == P_FIELD)
  88. ret = new prime_field(secparam, pkseed);
  89. else
  90. ret = new ecc_field(secparam, pkseed);
  91. free(pkseed);
  92. return ret;
  93. }
  94. void gen_rnd_bytes(prf_state_ctx* prf_state, uint8_t* resbuf, uint32_t nbytes) {
  95. AES_KEY_CTX* aes_key;
  96. uint64_t* rndctr;
  97. uint8_t* tmpbuf;
  98. uint32_t i, size;
  99. int32_t dummy;
  100. aes_key = &(prf_state->aes_key);
  101. rndctr = prf_state->ctr;
  102. size = ceil_divide(nbytes, AES_BYTES);
  103. tmpbuf = (uint8_t*) malloc(sizeof(uint8_t) * size * AES_BYTES);
  104. //TODO it might be better to store the result directly in resbuf but this would require the invoking routine to pad it to a multiple of AES_BYTES
  105. for (i = 0; i < size; i++, rndctr[0]++) {
  106. #ifdef OPENSSL_OPAQUE_EVP_CIPHER_CTX
  107. EVP_EncryptUpdate(*aes_key, tmpbuf + i * AES_BYTES, &dummy, (uint8_t*) rndctr, AES_BYTES);
  108. #else
  109. EVP_EncryptUpdate(aes_key, tmpbuf + i * AES_BYTES, &dummy, (uint8_t*) rndctr, AES_BYTES);
  110. #endif
  111. }
  112. memcpy(resbuf, tmpbuf, nbytes);
  113. free(tmpbuf);
  114. }
  115. void crypto::gen_rnd(uint8_t* resbuf, uint32_t nbytes) {
  116. std::lock_guard<std::mutex> lock(global_prf_state_mutex);
  117. gen_rnd_bytes(&global_prf_state, resbuf, nbytes);
  118. }
  119. void crypto::gen_rnd_uniform(uint32_t* res, uint32_t mod) {
  120. //pad to multiple of 4 bytes for uint32_t length
  121. uint32_t nrndbytes = PadToMultiple(bits_in_bytes(secparam.symbits) + ceil_log2(mod), sizeof(uint32_t));
  122. uint64_t bitsint = (8*sizeof(uint32_t));
  123. uint32_t rnditers = ceil_divide(nrndbytes * 8, bitsint);
  124. uint32_t* rndbuf = (uint32_t*) malloc(nrndbytes);
  125. gen_rnd((uint8_t*) rndbuf, nrndbytes);
  126. uint64_t tmpval = 0, tmpmod = mod;
  127. for(uint32_t i = 0; i < rnditers; i++) {
  128. tmpval = (((uint64_t) (tmpval << bitsint)) | ((uint64_t)rndbuf[i]));
  129. tmpval %= tmpmod;
  130. }
  131. *res = (uint32_t) tmpval;
  132. free(rndbuf);
  133. }
  134. void crypto::gen_rnd_from_seed(uint8_t* resbuf, uint32_t resbytes, uint8_t* seed) {
  135. prf_state_ctx tmpstate;
  136. init_prf_state(&tmpstate, seed);
  137. gen_rnd_bytes(&tmpstate, resbuf, resbytes);
  138. free_prf_state(&tmpstate);
  139. }
  140. void crypto::encrypt(AES_KEY_CTX* enc_key, uint8_t* resbuf, uint8_t* inbuf, uint32_t ninbytes) {
  141. int32_t dummy;
  142. #ifdef OPENSSL_OPAQUE_EVP_CIPHER_CTX
  143. EVP_EncryptUpdate(*enc_key, resbuf, &dummy, inbuf, ninbytes);
  144. #else
  145. EVP_EncryptUpdate(enc_key, resbuf, &dummy, inbuf, ninbytes);
  146. #endif
  147. //EVP_EncryptFinal_ex(enc_key, resbuf, &dummy);
  148. }
  149. void crypto::decrypt(AES_KEY_CTX* dec_key, uint8_t* resbuf, uint8_t* inbuf, uint32_t ninbytes) {
  150. int32_t dummy;
  151. #ifdef OPENSSL_OPAQUE_EVP_CIPHER_CTX
  152. EVP_DecryptUpdate(*dec_key, resbuf, &dummy, inbuf, ninbytes);
  153. #else
  154. EVP_DecryptUpdate(dec_key, resbuf, &dummy, inbuf, ninbytes);
  155. #endif
  156. //EVP_DecryptFinal_ex(dec_key, resbuf, &dummy);
  157. }
  158. void crypto::encrypt(uint8_t* resbuf, uint8_t* inbuf, uint32_t ninbytes) {
  159. encrypt(&aes_enc_key, resbuf, inbuf, ninbytes);
  160. }
  161. void crypto::decrypt(uint8_t* resbuf, uint8_t* inbuf, uint32_t ninbytes) {
  162. decrypt(&aes_dec_key, resbuf, inbuf, ninbytes);
  163. }
  164. void crypto::seed_aes_hash(uint8_t* seed, bc_mode mode, const uint8_t* iv) {
  165. seed_aes_key(&aes_hash_key, seed, mode, iv);
  166. }
  167. void crypto::seed_aes_enc(uint8_t* seed, bc_mode mode, const uint8_t* iv) {
  168. seed_aes_key(&aes_enc_key, seed, mode, iv, true);
  169. seed_aes_key(&aes_dec_key, seed, mode, iv, false);
  170. }
  171. void crypto::init_aes_key(AES_KEY_CTX* aes_key, uint8_t* seed, bc_mode mode, const uint8_t* iv) {
  172. seed_aes_key(aes_key, seed, mode, iv);
  173. }
  174. void crypto::init_aes_key(AES_KEY_CTX* aes_key, uint32_t symbits, uint8_t* seed, bc_mode mode, const uint8_t* iv, bool encrypt) {
  175. seed_aes_key(aes_key, symbits, seed, mode, iv, encrypt);
  176. }
  177. void crypto::seed_aes_key(AES_KEY_CTX* aeskey, uint8_t* seed, bc_mode mode, const uint8_t* iv, bool encrypt) {
  178. seed_aes_key(aeskey, secparam.symbits, seed, mode, iv, encrypt);
  179. }
  180. void crypto::clean_aes_key(AES_KEY_CTX* aeskey) {
  181. #ifdef OPENSSL_OPAQUE_EVP_CIPHER_CTX
  182. EVP_CIPHER_CTX_free(*aeskey);
  183. #else
  184. EVP_CIPHER_CTX_cleanup(aeskey);
  185. #endif
  186. }
  187. void crypto::seed_aes_key(AES_KEY_CTX* aeskey, uint32_t symbits, uint8_t* seed, bc_mode mode, const uint8_t* iv, bool encrypt) {
  188. #ifdef OPENSSL_OPAQUE_EVP_CIPHER_CTX
  189. *aeskey = EVP_CIPHER_CTX_new();
  190. AES_KEY_CTX aes_key_tmp = *aeskey;
  191. #else
  192. EVP_CIPHER_CTX_init(aeskey);
  193. AES_KEY_CTX* aes_key_tmp = aeskey;
  194. #endif
  195. int (*initfct)(EVP_CIPHER_CTX*, const EVP_CIPHER*, ENGINE*, const unsigned char*, const unsigned char*);
  196. if (encrypt)
  197. initfct = EVP_EncryptInit_ex;
  198. else
  199. initfct = EVP_DecryptInit_ex;
  200. switch (mode) {
  201. case ECB:
  202. if (symbits <= 128) {
  203. initfct(aes_key_tmp, EVP_aes_128_ecb(), NULL, seed, iv);
  204. } else if(symbits == 192) {
  205. initfct(aes_key_tmp, EVP_aes_192_ecb(), NULL, seed, iv);
  206. } else {
  207. initfct(aes_key_tmp, EVP_aes_256_ecb(), NULL, seed, iv);
  208. }
  209. break;
  210. case CBC:
  211. if (symbits <= 128) {
  212. initfct(aes_key_tmp, EVP_aes_128_cbc(), NULL, seed, iv);
  213. } else if(symbits == 192) {
  214. initfct(aes_key_tmp, EVP_aes_192_cbc(), NULL, seed, iv);
  215. } else {
  216. initfct(aes_key_tmp, EVP_aes_256_cbc(), NULL, seed, iv);
  217. }
  218. break;
  219. default:
  220. if (symbits <= 128) {
  221. initfct(aes_key_tmp, EVP_aes_128_ecb(), NULL, seed, iv);
  222. } else if(symbits == 192) {
  223. initfct(aes_key_tmp, EVP_aes_192_ecb(), NULL, seed, iv);
  224. } else {
  225. initfct(aes_key_tmp, EVP_aes_256_ecb(), NULL, seed, iv);
  226. }
  227. break;
  228. }
  229. }
  230. void crypto::hash_ctr(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes, uint64_t ctr) {
  231. uint8_t* tmpbuf = (uint8_t*) malloc(ninbytes + sizeof(uint64_t));
  232. memcpy(tmpbuf, &ctr, sizeof(uint64_t));
  233. memcpy(tmpbuf + sizeof(uint64_t), inbuf, ninbytes);
  234. hash_routine(resbuf, noutbytes, tmpbuf, ninbytes+sizeof(uint64_t), sha_hash_buf);
  235. free(tmpbuf);
  236. }
  237. void crypto::hash(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes) {
  238. uint8_t* hash_buf = (uint8_t*) malloc(get_hash_bytes());
  239. hash_routine(resbuf, noutbytes, inbuf, ninbytes, hash_buf);
  240. free(hash_buf);
  241. }
  242. void crypto::hash_buf(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes, uint8_t* buf) {
  243. hash_routine(resbuf, noutbytes, inbuf, ninbytes, buf);
  244. }
  245. void crypto::hash_non_threadsafe(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes) {
  246. hash_routine(resbuf, noutbytes, inbuf, ninbytes, sha_hash_buf);
  247. }
  248. //A fixed-key hashing scheme that uses AES, should not be used for real hashing, hashes to AES_BYTES bytes
  249. //TODO not thread safe
  250. void crypto::fixed_key_aes_hash(AES_KEY_CTX* aes_key, uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes) {
  251. int32_t dummy;
  252. memset(aes_hash_in_buf, 0, AES_BYTES);
  253. memcpy(aes_hash_in_buf, inbuf, ninbytes);
  254. //two encryption iterations TODO: not secure since both blocks are treated independently, implement DM or MMO
  255. #ifdef OPENSSL_OPAQUE_EVP_CIPHER_CTX
  256. EVP_EncryptUpdate(*aes_key, aes_hash_out_buf, &dummy, aes_hash_in_buf, AES_BYTES);
  257. #else
  258. EVP_EncryptUpdate(aes_key, aes_hash_out_buf, &dummy, aes_hash_in_buf, AES_BYTES);
  259. #endif
  260. ((uint64_t*) aes_hash_out_buf)[0] ^= ((uint64_t*) aes_hash_in_buf)[0];
  261. ((uint64_t*) aes_hash_out_buf)[1] ^= ((uint64_t*) aes_hash_in_buf)[1];
  262. memcpy(resbuf, aes_hash_out_buf, noutbytes);
  263. }
  264. //Generate a random permutation of neles elements using Knuths algorithm
  265. void crypto::gen_rnd_perm(uint32_t* perm, uint32_t neles) {
  266. uint32_t* rndbuf = (uint32_t*) malloc(sizeof(uint32_t) * neles);
  267. uint32_t i, j;
  268. //TODO Generate random numbers (CAREFUL: NOT UNIFORM)
  269. gen_rnd((uint8_t*) rndbuf, sizeof(uint32_t) * neles);
  270. for (i = 0; i < neles; i++) {
  271. perm[i] = i;
  272. }
  273. for (i = 0; i < neles; i++) {
  274. j = rndbuf[i] % neles; //NOT UNIFORM
  275. std::swap(perm[i], perm[j]);
  276. }
  277. free(rndbuf);
  278. }
  279. uint32_t crypto::get_aes_key_bytes() {
  280. if (secparam.symbits == ST.symbits)
  281. return 16;
  282. else if (secparam.symbits == MT.symbits)
  283. return 16;
  284. else if (secparam.symbits == LT.symbits)
  285. return 16;
  286. else if (secparam.symbits == XLT.symbits)
  287. return 24;
  288. else if (secparam.symbits == XXLT.symbits)
  289. return 32;
  290. else
  291. return 64;
  292. }
  293. uint32_t crypto::get_hash_bytes() {
  294. if (secparam.symbits == ST.symbits)
  295. return 20;
  296. else if (secparam.symbits == MT.symbits)
  297. return 32;
  298. else if (secparam.symbits == LT.symbits)
  299. return 32;
  300. else if (secparam.symbits == XLT.symbits)
  301. return 64;
  302. else if (secparam.symbits == XXLT.symbits)
  303. return 64;
  304. else
  305. return 64;
  306. }
  307. //Generate a common seed, is only secure in the semi-honest model
  308. void crypto::gen_common_seed(prf_state_ctx* prf_state, CSocket& sock) {
  309. uint8_t *seed_buf, *seed_rcv_buf;
  310. uint32_t seed_bytes, i;
  311. seed_bytes = get_aes_key_bytes();
  312. seed_buf = (uint8_t*) malloc(seed_bytes);
  313. seed_rcv_buf = (uint8_t*) malloc(seed_bytes);
  314. //randomly generate and exchange seed bytes:
  315. gen_rnd(seed_buf, seed_bytes);
  316. sock.Send(seed_buf, seed_bytes);
  317. sock.Receive(seed_rcv_buf, seed_bytes);
  318. //xor both seeds
  319. for (i = 0; i < seed_bytes; i++) {
  320. seed_buf[i] ^= seed_rcv_buf[i];
  321. }
  322. init_prf_state(prf_state, seed_buf);
  323. free(seed_buf);
  324. free(seed_rcv_buf);
  325. }
  326. void crypto::init_prf_state(prf_state_ctx* prf_state, uint8_t* seed) {
  327. seed_aes_key(&(prf_state->aes_key), seed);
  328. prf_state->ctr = (uint64_t*) calloc(ceil_divide(secparam.symbits, 8 * sizeof(uint64_t)), sizeof(uint64_t));
  329. }
  330. void crypto::free_prf_state(prf_state_ctx* prf_state) {
  331. free(prf_state->ctr);
  332. clean_aes_key(&(prf_state->aes_key));
  333. }
  334. void des_encrypt(uint8_t* resbuf, uint8_t* inbuf, uint8_t* key, bool encrypt) {
  335. DES_cblock keyblock;
  336. DES_cblock msgblock;
  337. DES_cblock outblock;
  338. DES_key_schedule schedule;
  339. memcpy(msgblock, inbuf, 8);
  340. memcpy( keyblock, key,8);
  341. DES_set_key( &keyblock, &schedule );
  342. /* Encryption occurs here */
  343. DES_ecb_encrypt(&msgblock, &outblock, &schedule, (int) encrypt);
  344. memcpy(resbuf, outblock, 8);
  345. }
  346. void des3_encrypt(uint8_t* resbuf, uint8_t* inbuf, uint8_t* key, bool encrypt) {
  347. DES_cblock keyblock1, keyblock2, keyblock3;
  348. DES_cblock msgblock;
  349. DES_cblock outblock;
  350. DES_key_schedule schedule1, schedule2, schedule3;
  351. memcpy(msgblock, inbuf, 8);
  352. memcpy( keyblock1, key,8);
  353. memcpy( keyblock2, key+8,8);
  354. memcpy( keyblock2, key+16,8);
  355. DES_set_key( &keyblock1, &schedule1 );
  356. DES_set_key( &keyblock2, &schedule2 );
  357. DES_set_key( &keyblock3, &schedule3 );
  358. /* Encryption occurs here */
  359. DES_ecb3_encrypt(&msgblock, &outblock, &schedule1, &schedule2, &schedule2, (int) encrypt);
  360. memcpy(resbuf, outblock, 8);
  361. }
  362. void sha1_hash(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes, uint8_t* hash_buf) {
  363. SHA_CTX sha;
  364. SHA1_Init(&sha);
  365. SHA1_Update(&sha, inbuf, ninbytes);
  366. SHA1_Final(hash_buf, &sha);
  367. memcpy(resbuf, hash_buf, noutbytes);
  368. }
  369. void sha256_hash(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes, uint8_t* hash_buf) {
  370. SHA256_CTX sha;
  371. SHA256_Init(&sha);
  372. SHA256_Update(&sha, inbuf, ninbytes);
  373. SHA256_Final(hash_buf, &sha);
  374. memcpy(resbuf, hash_buf, noutbytes);
  375. }
  376. void sha512_hash(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes, uint8_t* hash_buf) {
  377. SHA512_CTX sha;
  378. SHA512_Init(&sha);
  379. SHA512_Update(&sha, inbuf, ninbytes);
  380. SHA512_Final(hash_buf, &sha);
  381. memcpy(resbuf, hash_buf, noutbytes);
  382. }
  383. //Read random bytes from /dev/urandom
  384. void gen_secure_random(uint8_t* dest, uint32_t nbytes) {
  385. int fd = open("/dev/urandom", O_RDONLY);
  386. if (fd < 0)
  387. {
  388. std::cerr << "Unable to open /dev/urandom, exiting" << std::endl;
  389. exit(0);
  390. }
  391. size_t bytectr = 0;
  392. while (bytectr < nbytes) {
  393. ssize_t result = read(fd, dest + bytectr, nbytes - bytectr);
  394. if (result < 0) {
  395. std::cerr << "Unable to read from /dev/urandom, exiting" << std::endl;
  396. exit(0);
  397. }
  398. bytectr += static_cast<size_t>(result);
  399. }
  400. if (close(fd) < 0)
  401. {
  402. std::cerr << "Unable to close /dev/urandom" << std::endl;
  403. }
  404. }
  405. seclvl get_sec_lvl(uint32_t symsecbits) {
  406. if (symsecbits == ST.symbits)
  407. return ST;
  408. else if (symsecbits == MT.symbits)
  409. return MT;
  410. else if (symsecbits == LT.symbits)
  411. return LT;
  412. else if (symsecbits == XLT.symbits)
  413. return XLT;
  414. else if (symsecbits == XXLT.symbits)
  415. return XXLT;
  416. else
  417. return LT;
  418. }