djn.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. \file djn.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
  16. libdjn - v0.9
  17. A library implementing the Damgaard Jurik Nielsen cryptosystem with s=1 (~Paillier).
  18. based on:
  19. libpaillier - A library implementing the Paillier cryptosystem.
  20. (http://hms.isi.jhu.edu/acsc/libpaillier/)
  21. */
  22. #ifndef _DJN_H_
  23. #define _DJN_H_
  24. #include <gmp.h>
  25. /*
  26. On memory handling:
  27. At no point is any special effort made to securely "shred" sensitive
  28. memory or prevent it from being paged out to disk. This means that
  29. it is important that functions dealing with private keys and
  30. plaintexts (e.g., djn_keygen and djn_enc) only be run on
  31. trusted machines. The resulting ciphertexts and public keys,
  32. however, may of course be handled in an untrusted manner.
  33. */
  34. /******
  35. TYPES
  36. *******/
  37. /*
  38. This represents a public key, which is the modulus n plus a generator h.
  39. */
  40. struct djn_pubkey_t {
  41. int bits; /* e.g., 1024 */
  42. int rbits; /* e.g., 512 */
  43. mpz_t n; /* public modulus n = p q */
  44. mpz_t n_squared; /* cached to avoid recomputing */
  45. mpz_t h; /* generator h = -x^2 mod n */
  46. mpz_t h_s; /* h_s = h^n mod n^2 */
  47. };
  48. /*
  49. This represents a Paillier private key; it needs to be used with a
  50. djn_pubkey_t to be meaningful. It includes the Carmichael
  51. function (lambda) of the modulus. The other value is kept for
  52. efficiency and should be considered private.
  53. */
  54. struct djn_prvkey_t {
  55. mpz_t lambda; /* lambda(n), i.e., lcm(p-1,q-1) */
  56. mpz_t lambda_inverse; /* inverse of lambda (mod n)*/
  57. mpz_t p; /* cached to avoid recomputing */
  58. mpz_t q; /* cached to avoid recomputing */
  59. mpz_t q_inverse; /* inverse of q (mod p) */
  60. mpz_t q_squared_inverse; /* inverse of q^2 (mod p^2) */
  61. mpz_t p_minusone; /* cached to avoid recomputing */
  62. mpz_t q_minusone; /* cached to avoid recomputing */
  63. mpz_t p_squared; /* cached to avoid recomputing */
  64. mpz_t q_squared; /* cached to avoid recomputing */
  65. mpz_t ordpsq; /* p^2-p */
  66. mpz_t ordqsq; /* q^2-q */
  67. };
  68. /*
  69. This is the type of the callback functions used to obtain the
  70. randomness needed by the probabilistic algorithms. The functions
  71. djn_get_rand_devrandom and djn_get_rand_devurandom
  72. (documented later) may be passed to any library function requiring a
  73. djn_get_rand_t, or you may implement your own. If you implement
  74. your own such function, it should fill in "len" random bytes in the
  75. array "buf".
  76. */
  77. typedef void (*djn_get_rand_t)(void* buf, int len);
  78. /*****************
  79. BASIC OPERATIONS
  80. *****************/
  81. /*
  82. Generate a keypair of length modulusbits using randomness from the
  83. provided get_rand function. Space will be allocated for each of the
  84. keys, and the given pointers will be set to point to the new
  85. djn_pubkey_t and djn_prvkey_t structures. The functions
  86. djn_get_rand_devrandom and djn_get_rand_devurandom may be
  87. passed as the final argument.
  88. */
  89. void djn_keygen(unsigned int modulusbits, djn_pubkey_t** pub, djn_prvkey_t** prv);
  90. /*
  91. Encrypt the given plaintext with the given public key using
  92. randomness from get_rand for blinding. If res is not null, its
  93. contents will be overwritten with the result. Otherwise, a new
  94. djn_ciphertext_t will be allocated and returned.
  95. */
  96. void djn_encrypt(mpz_t res, djn_pubkey_t* pub, mpz_t pt);
  97. /*
  98. Encrypt the given plaintext with the given public key using
  99. randomness from get_rand for blinding. If res is not null, its
  100. contents will be overwritten with the result. Otherwise, a new
  101. djn_ciphertext_t will be allocated and returned.
  102. */
  103. void djn_encrypt_crt(mpz_t res, djn_pubkey_t* pub, djn_prvkey_t* prv, mpz_t pt);
  104. /**
  105. * fixed base encryption. Requires pre-computed fixed base table.
  106. */
  107. void djn_encrypt_fb(mpz_t res, djn_pubkey_t* pub, mpz_t plaintext);
  108. /*
  109. Decrypt the given ciphertext with the given key pair. If res is not
  110. null, its contents will be overwritten with the result. Otherwise, a
  111. new djn_plaintext_t will be allocated and returned.
  112. */
  113. void djn_decrypt(mpz_t res, djn_pubkey_t* pub, djn_prvkey_t* prv, mpz_t ct);
  114. /**********************
  115. KEY IMPORT AND EXPORT
  116. **********************/
  117. /*
  118. Import or export public and private keys from or to hexadecimal,
  119. ASCII strings, which are suitable for I/O. Note that the
  120. corresponding public key is necessary to initialize a private key
  121. from a hex string. In all cases, the returned value is allocated for
  122. the caller and the values passed are unchanged.
  123. */
  124. char* djn_pubkey_to_hex(djn_pubkey_t* pub);
  125. char* djn_prvkey_to_hex(djn_prvkey_t* prv);
  126. djn_pubkey_t* djn_pubkey_from_hex(char* str);
  127. djn_prvkey_t* djn_prvkey_from_hex(char* str, djn_pubkey_t* pub);
  128. /********
  129. CLEANUP
  130. ********/
  131. /*
  132. These free the structures allocated and returned by various
  133. functions within library and should be used when the structures are
  134. no longer needed.
  135. */
  136. void djn_freepubkey(djn_pubkey_t* pub);
  137. void djn_freeprvkey(djn_prvkey_t* prv);
  138. /***********
  139. MISC STUFF
  140. ***********/
  141. /*
  142. Just a utility used internally when we need round a number of bits
  143. up the number of bytes necessary to hold them.
  144. */
  145. #define PAILLIER_BITS_TO_BYTES(n) ((n) % 8 ? (n) / 8 + 1 : (n) / 8)
  146. void djn_pow_mod_n_crt(mpz_t res, const mpz_t b, const mpz_t e, const djn_pubkey_t* pub, const djn_prvkey_t* prv);
  147. void djn_pow_mod_n_squared_crt(mpz_t res, const mpz_t b, const mpz_t e, const djn_pubkey_t* pub, const djn_prvkey_t* prv);
  148. /**
  149. * create full public key given only n and h (e.g., after a key exchange)
  150. */
  151. void djn_complete_pubkey(unsigned int modulusbits, djn_pubkey_t** pub, mpz_t n, mpz_t h);
  152. #endif