crypto.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. \file crypto.h
  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 Crypto primitive class
  17. */
  18. #ifndef CRYPTO_H_
  19. #define CRYPTO_H_
  20. #include <openssl/evp.h>
  21. #include "../constants.h"
  22. #include <mutex>
  23. // forward declarations
  24. class pk_crypto;
  25. class CSocket;
  26. const uint8_t ZERO_IV[AES_BYTES] = { 0 };
  27. const uint8_t const_seed[2][16] = {{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF },
  28. {0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00 } };
  29. enum bc_mode {
  30. ECB, CBC
  31. };
  32. //Check for the OpenSSL version number, since the EVP_CIPHER_CTX has become opaque from >= 1.1.0
  33. #if OPENSSL_VERSION_NUMBER >= 0x10100000L
  34. #define OPENSSL_OPAQUE_EVP_CIPHER_CTX
  35. #endif
  36. #ifdef OPENSSL_OPAQUE_EVP_CIPHER_CTX
  37. typedef EVP_CIPHER_CTX* AES_KEY_CTX;
  38. #else
  39. typedef EVP_CIPHER_CTX AES_KEY_CTX;
  40. #endif
  41. /* Predefined security levels,
  42. * ST (SHORTTERM) = 1024/160/163 bit public key, 80 bit private key
  43. * MT (MEDIUMTERM) = 2048/192/233 bit public key, 112 bit private key
  44. * LT (LONGTERM) = 3072/256/283 bit public key, 128 bit private key
  45. * XLT (EXTRA LONGTERM) = 7680/384/409 bit public key, 192 bit private key
  46. * XXLT (EXTRA EXTRA LONGTERM) = 15360/512/571 bit public key, 256 bit private key
  47. */
  48. struct prf_state_ctx {
  49. AES_KEY_CTX aes_key;
  50. uint64_t* ctr;
  51. };
  52. //TODO: not thread-safe when multiple threads generate random data using the same seed
  53. class crypto {
  54. public:
  55. crypto(uint32_t symsecbits, uint8_t* seed);
  56. crypto(uint32_t symsecbits);
  57. ~crypto();
  58. //Randomness generation routines
  59. void gen_rnd(uint8_t* resbuf, uint32_t numbytes);
  60. void gen_rnd_from_seed(uint8_t* resbuf, uint32_t resbytes, uint8_t* seed);
  61. //void gen_rnd(prf_state_ctx* prf_state, uint8_t* resbuf, uint32_t nbytes);
  62. void gen_rnd_uniform(uint32_t* res, uint32_t mod);
  63. void gen_rnd_perm(uint32_t* perm, uint32_t neles);
  64. //Encryption routines
  65. void encrypt(uint8_t* resbuf, uint8_t* inbuf, uint32_t ninbytes);
  66. void decrypt(uint8_t* resbuf, uint8_t* inbuf, uint32_t ninbytes);
  67. //Hash routines
  68. void hash(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes);
  69. void hash_buf(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes, uint8_t* buf);
  70. void hash_non_threadsafe(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes);
  71. void hash_ctr(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes, uint64_t ctr);
  72. void fixed_key_aes_hash(AES_KEY_CTX* aes_key, uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes);
  73. void fixed_key_aes_hash_ctr(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes);
  74. //Key seed routines
  75. void seed_aes_hash(uint8_t* seed, bc_mode mode = ECB, const uint8_t* iv = ZERO_IV);
  76. void seed_aes_enc(uint8_t* seed, bc_mode mode = ECB, const uint8_t* iv = ZERO_IV);
  77. //External encryption routines
  78. void init_aes_key(AES_KEY_CTX* aes_key, uint8_t* seed, bc_mode mode = ECB, const uint8_t* iv = ZERO_IV);
  79. 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, bool encrypt = true);
  80. void clean_aes_key(AES_KEY_CTX* aeskey);
  81. uint32_t get_aes_key_bytes();
  82. void encrypt(AES_KEY_CTX* enc_key, uint8_t* resbuf, uint8_t* inbuf, uint32_t ninbytes);
  83. void decrypt(AES_KEY_CTX* dec_key, uint8_t* resbuf, uint8_t* inbuf, uint32_t ninbytes);
  84. pk_crypto* gen_field(field_type ftype);
  85. seclvl get_seclvl() {
  86. return secparam;
  87. }
  88. ;
  89. uint32_t get_hash_bytes();
  90. void gen_common_seed(prf_state_ctx* aes_key, CSocket& sock);
  91. void init_prf_state(prf_state_ctx* prf_state, uint8_t* seed);
  92. void free_prf_state(prf_state_ctx* prf_state);
  93. private:
  94. void seed_aes_key(AES_KEY_CTX* aeskey, uint8_t* seed, bc_mode mode = ECB, const uint8_t* iv = ZERO_IV, bool encrypt = true);
  95. 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);
  96. void init(uint32_t symsecbits, uint8_t* seed);
  97. AES_KEY_CTX aes_hash_key;
  98. AES_KEY_CTX aes_enc_key;
  99. AES_KEY_CTX aes_dec_key;
  100. prf_state_ctx global_prf_state;
  101. std::mutex global_prf_state_mutex;
  102. seclvl secparam;
  103. uint8_t* aes_hash_in_buf;
  104. uint8_t* aes_hash_out_buf;
  105. uint8_t* aes_hash_buf_y1;
  106. uint8_t* aes_hash_buf_y2;
  107. uint8_t* sha_hash_buf;
  108. void (*hash_routine)(uint8_t*, uint32_t, uint8_t*, uint32_t, uint8_t*);
  109. };
  110. //Some functions that should be useable without the class
  111. void des_encrypt(uint8_t* resbuf, uint8_t* inbuf, uint8_t* key, bool encrypt);
  112. void des3_encrypt(uint8_t* resbuf, uint8_t* inbuf, uint8_t* key, bool encrypt);
  113. void sha1_hash(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes, uint8_t* hash_buf);
  114. void sha256_hash(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes, uint8_t* hash_buf);
  115. void sha512_hash(uint8_t* resbuf, uint32_t noutbytes, uint8_t* inbuf, uint32_t ninbytes, uint8_t* hash_buf);
  116. void gen_secure_random(uint8_t* dest, uint32_t nbytes);
  117. void gen_rnd_bytes(prf_state_ctx* prf_state, uint8_t* resbuf, uint32_t nbytes);
  118. seclvl get_sec_lvl(uint32_t symsecbits); //TODO pick a more elegant name (see crypto->get_seclvl())
  119. #endif /* CRYPTO_H_ */