dgk.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. \file dgk.h
  3. \author Daniel Demmler
  4. \copyright Copyright (C) 2019 ENCRYPTO Group, TU Darmstadt
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. ABY is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. \brief libdgk - v0.9
  16. A library implementing the DGK crypto system with full decryption
  17. Thanks to Marina Blanton for sharing her Miracl DGK implementation from
  18. M. Blanton and P. Gasti, "Secure and efficient protocols for iris and fingerprint identification" (ESORICS’11)
  19. with us. We used it as a template for this GMP version.
  20. The implementation structure was inspired by
  21. libpailler - A library implementing the Paillier crypto system. (http://hms.isi.jhu.edu/acsc/libpaillier/)
  22. */
  23. #ifndef _DGK_H_
  24. #define _DGK_H_
  25. #include <gmp.h>
  26. /*
  27. This represents a DGK public key.
  28. */
  29. struct dgk_pubkey_t {
  30. unsigned int bits; /* key bits e.g., 1024 */
  31. unsigned int lbits; /* share (message) length e.g., 32 */
  32. mpz_t n; /* public modulus n = pq */
  33. mpz_t u; /* u = 2^lbits (uses 2^(2lbits+2) internally) */
  34. mpz_t g; /* generator g */
  35. mpz_t h; /* generator h */
  36. };
  37. /*
  38. This represents a DGK private key; it needs to be used with a
  39. dgk_pubkey_t to be meaningful.
  40. */
  41. struct dgk_prvkey_t {
  42. mpz_t vp;
  43. mpz_t vq;
  44. mpz_t p;
  45. mpz_t q;
  46. mpz_t p_minusone;
  47. mpz_t q_minusone;
  48. mpz_t pinv;
  49. mpz_t qinv;
  50. };
  51. extern mpz_t* powtwo;
  52. extern mpz_t* gvpvqp;
  53. /**
  54. * create a DGK key pair. This will take some time, depending on the size (up to several minutes!)
  55. * modulusbits is the size of the modulus n, e.g. 1024 or 2048 bit
  56. * lbits is equal to the share length, e.g. 16 or 32 bit (We use 2*lbits+2 internally)
  57. * the parameter t is internally fixed to 160, as recommended in the paper
  58. * NOTE: this key generation is probabilistic and might create fauly keys!
  59. * Generated keys *MUST* be validated by running multiple trial encryption/decryptions and
  60. * checking the result for correctnes!
  61. * An example can be found in createKeys() in dgk.cpp:559
  62. */
  63. void dgk_keygen(unsigned int modulusbits, unsigned int lbits, dgk_pubkey_t** pub, dgk_prvkey_t** prv);
  64. /**
  65. * encrypt with public key only and double-base encryption - unfortunately not efficient due to different sized exponents, therefore deactivated
  66. */
  67. //void dgk_encrypt_db(mpz_t res, dgk_pubkey_t* pub, mpz_t pt);
  68. /**
  69. * encrypt with public key only, fixed-base encryption (must be initialized before first use!)
  70. */
  71. void dgk_encrypt_fb(mpz_t res, dgk_pubkey_t* pub, mpz_t pt);
  72. /**
  73. * encrypt with public key only, no further optimization (slower than fixed-base encryption)
  74. */
  75. void dgk_encrypt_plain(mpz_t res, dgk_pubkey_t* pub, mpz_t pt);
  76. /**
  77. * encrypt using CRT if we have the private key for efficiency
  78. */
  79. void dgk_encrypt_crt(mpz_t res, dgk_pubkey_t* pub, dgk_prvkey_t* prv, mpz_t pt);
  80. /**
  81. * use CRT and double base combined - unfortunately not efficient due to different sized exponents, therefore deactivated
  82. */
  83. // void dgk_encrypt_crt_db(mpz_t res, dgk_pubkey_t* pub, dgk_prvkey_t* prv, mpz_t pt);
  84. /**
  85. * DGK decryption
  86. */
  87. void dgk_decrypt(mpz_t res, dgk_pubkey_t* pub, dgk_prvkey_t* prv, mpz_t ct);
  88. /**
  89. * stores a generated key pair to disc
  90. */
  91. void dgk_storekey(unsigned int modulusbits, unsigned int lbits, dgk_pubkey_t* pub, dgk_prvkey_t* prv);
  92. /**
  93. * reads a previously stored key pair from disc
  94. */
  95. void dgk_readkey(unsigned int modulusbits, unsigned int lbits, dgk_pubkey_t** pub, dgk_prvkey_t** prv);
  96. /*
  97. These free the structures allocated and returned by various
  98. functions within library and should be used when the structures are
  99. no longer needed.
  100. */
  101. void dgk_freepubkey(dgk_pubkey_t* pub);
  102. void dgk_freeprvkey(dgk_prvkey_t* prv);
  103. /**
  104. * create the full public key struct type given only n, g and h (e.g. after key exchange)
  105. */
  106. void dgk_complete_pubkey(unsigned int modulusbits, unsigned int lbits, dgk_pubkey_t** pub, mpz_t n, mpz_t g, mpz_t h);
  107. /**
  108. * -------------------------------
  109. * the following are internal functions, that should not be called from the outside unless you really know what they do, hence commented out
  110. * -------------------------------
  111. */
  112. /**
  113. * create a batch of different keys and check that they are valid, overwrites existing keys (if any)
  114. */
  115. // void createKeys(){
  116. /**
  117. * test correct encrypt/decrypt
  118. */
  119. //void test_encdec()
  120. /**
  121. * test correct sharing
  122. */
  123. //void test_sharing(){
  124. #endif