gnunet_secretsharing_service.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @author Florian Dold
  19. *
  20. * @file
  21. * Verifiable additive secret sharing and cooperative decryption
  22. *
  23. * @defgroup secretsharing Secret Sharing service
  24. * Verifiable additive secret sharing and cooperative decryption.
  25. * @{
  26. */
  27. #ifndef GNUNET_SECRETSHARING_SERVICE_H
  28. #define GNUNET_SECRETSHARING_SERVICE_H
  29. #ifdef __cplusplus
  30. extern "C"
  31. {
  32. #if 0 /* keep Emacsens' auto-indent happy */
  33. }
  34. #endif
  35. #endif
  36. #include "platform.h"
  37. #include "gnunet_common.h"
  38. #include "gnunet_time_lib.h"
  39. #include "gnunet_configuration_lib.h"
  40. #include <gcrypt.h>
  41. /**
  42. * Number of bits for secretsharing elements.
  43. * Must be smaller than the Pallier key size used internally
  44. * by the secretsharing service.
  45. * When changing this value, other internal parameters must also
  46. * be adjusted.
  47. */
  48. #define GNUNET_SECRETSHARING_ELGAMAL_BITS 1024
  49. /**
  50. * The q-parameter for ElGamal encryption, a 1024-bit safe prime.
  51. */
  52. #define GNUNET_SECRETSHARING_ELGAMAL_P_HEX \
  53. "0x08a347d3d69e8b2dd7d1b12a08dfbccbebf4ca" \
  54. "6f4269a0814e158a34312964d946b3ef22882317" \
  55. "2bcf30fc08f772774cb404f9bc002a6f66b09a79" \
  56. "d810d67c4f8cb3bedc6060e3c8ef874b1b64df71" \
  57. "6c7d2b002da880e269438d5a776e6b5f253c8df5" \
  58. "6a16b1c7ce58def07c03db48238aadfc52a354a2" \
  59. "7ed285b0c1675cad3f3"
  60. /**
  61. * The q-parameter for ElGamal encryption,
  62. * a 1023-bit Sophie Germain prime, q = (p-1)/2
  63. */
  64. #define GNUNET_SECRETSHARING_ELGAMAL_Q_HEX \
  65. "0x0451a3e9eb4f4596ebe8d895046fde65f5fa65" \
  66. "37a134d040a70ac51a1894b26ca359f79144118b" \
  67. "95e7987e047bb93ba65a027cde001537b3584d3c" \
  68. "ec086b3e27c659df6e303071e477c3a58db26fb8" \
  69. "b63e958016d4407134a1c6ad3bb735af929e46fa" \
  70. "b50b58e3e72c6f783e01eda411c556fe2951aa51" \
  71. "3f6942d860b3ae569f9"
  72. /**
  73. * The g-parameter for ElGamal encryption,
  74. * a generator of the unique size q subgroup of Z_p^*
  75. */
  76. #define GNUNET_SECRETSHARING_ELGAMAL_G_HEX \
  77. "0x05c00c36d2e822950087ef09d8252994adc4e4" \
  78. "8fe3ec70269f035b46063aff0c99b633fd64df43" \
  79. "02442e1914c829a41505a275438871f365e91c12" \
  80. "3d5303ef9e90f4b8cb89bf86cc9b513e74a72634" \
  81. "9cfd9f953674fab5d511e1c078fc72d72b34086f" \
  82. "c82b4b951989eb85325cb203ff98df76bc366bba" \
  83. "1d7024c3650f60d0da"
  84. /**
  85. * Session that will eventually establish a shared secred between
  86. * the involved peers and allow encryption and cooperative decryption.
  87. */
  88. struct GNUNET_SECRETSHARING_Session;
  89. /**
  90. * Share of a secret shared with a group of peers.
  91. * Contains the secret share itself, the public key, the list of peers, and the
  92. * exponential commitments to the secret shares of the other peers.
  93. */
  94. struct GNUNET_SECRETSHARING_Share;
  95. /**
  96. * Handle to cancel a cooperative decryption operation.
  97. */
  98. struct GNUNET_SECRETSHARING_DecryptionHandle;
  99. /**
  100. * Public key of a group sharing a secret.
  101. */
  102. struct GNUNET_SECRETSHARING_PublicKey
  103. {
  104. uint32_t bits[GNUNET_SECRETSHARING_ELGAMAL_BITS / 8 / sizeof (uint32_t)];
  105. };
  106. /**
  107. * Encrypted field element.
  108. */
  109. struct GNUNET_SECRETSHARING_Ciphertext
  110. {
  111. uint32_t c1_bits[GNUNET_SECRETSHARING_ELGAMAL_BITS / 8 / sizeof (uint32_t)];
  112. uint32_t c2_bits[GNUNET_SECRETSHARING_ELGAMAL_BITS / 8 / sizeof (uint32_t)];
  113. };
  114. /**
  115. * Plain, unencrypted message that can be encrypted with
  116. * a group public key.
  117. * Note that we are not operating in GF(2^n), thus not every
  118. * bit pattern is a valid plain text.
  119. */
  120. struct GNUNET_SECRETSHARING_Plaintext
  121. {
  122. /**
  123. * Value of the message.
  124. */
  125. uint32_t bits[GNUNET_SECRETSHARING_ELGAMAL_BITS / 8 / sizeof (uint32_t)];
  126. };
  127. /**
  128. * Called once the secret has been established with all peers, or the deadline is due.
  129. *
  130. * Note that the number of peers can be smaller than 'k' (this threshold parameter), which
  131. * makes the threshold crypto system useless. However, in this case one can still determine which peers
  132. * were able to participate in the secret sharing successfully.
  133. *
  134. * If the secret sharing failed, num_ready_peers is 0 and my_share and public_key is NULL.
  135. *
  136. * After this callback has been called, the secretsharing session will be invalid.
  137. *
  138. * @param cls closure
  139. * @param my_share the share of this peer
  140. * @param public_key public key of the session
  141. * @param num_ready_peers number of peers in @a ready_peers
  142. * @param ready_peers peers that successfuly participated in establishing
  143. * the shared secret
  144. */
  145. typedef void (*GNUNET_SECRETSHARING_SecretReadyCallback) (void *cls,
  146. struct GNUNET_SECRETSHARING_Share *my_share,
  147. struct GNUNET_SECRETSHARING_PublicKey *public_key,
  148. unsigned int num_ready_peers,
  149. struct GNUNET_PeerIdentity *ready_peers);
  150. /**
  151. * Called when a decryption has succeeded.
  152. *
  153. * @param cls closure
  154. * @param data decrypted value
  155. * @param data_size number of bytes in @a data
  156. */
  157. typedef void (*GNUNET_SECRETSHARING_DecryptCallback) (void *cls,
  158. const struct GNUNET_SECRETSHARING_Plaintext *plaintext);
  159. /**
  160. * Create a session that will eventually establish a shared secret
  161. * with the other peers.
  162. *
  163. * @param cfg configuration to use
  164. * @param num_peers number of peers in @a peers
  165. * @param peers array of peers that we will share secrets with, can optionally contain the local peer
  166. * @param session_id unique session id
  167. * @param start When should all peers be available for sharing the secret?
  168. * Random number generation can take place before the start time.
  169. * @param deadline point in time where the session must be established; taken as hint
  170. * by underlying consensus sessions
  171. * @param threshold minimum number of peers that must cooperate to decrypt a value
  172. * @param cb called when the secret has been established
  173. * @param cls closure for @a cb
  174. */
  175. struct GNUNET_SECRETSHARING_Session *
  176. GNUNET_SECRETSHARING_create_session (const struct GNUNET_CONFIGURATION_Handle *cfg,
  177. unsigned int num_peers,
  178. const struct GNUNET_PeerIdentity *peers,
  179. const struct GNUNET_HashCode *session_id,
  180. struct GNUNET_TIME_Absolute start,
  181. struct GNUNET_TIME_Absolute deadline,
  182. unsigned int threshold,
  183. GNUNET_SECRETSHARING_SecretReadyCallback cb,
  184. void *cls);
  185. /**
  186. * Destroy a secret sharing session.
  187. * The secret ready callback will not be called.
  188. *
  189. * @param s session to destroy
  190. */
  191. void
  192. GNUNET_SECRETSHARING_session_destroy (struct GNUNET_SECRETSHARING_Session *s);
  193. /**
  194. * Encrypt a value. This operation is executed locally, no communication is
  195. * necessary.
  196. *
  197. * This is a helper function, encryption can be done soley with a session's public key
  198. * and the crypto system parameters.
  199. *
  200. * @param public_key public key to use for decryption
  201. * @param message message to encrypt
  202. * @param message_size number of bytes in @a message
  203. * @param result_ciphertext pointer to store the resulting ciphertext
  204. * @return #GNUNET_YES on succes, #GNUNET_SYSERR if the message is invalid (invalid range)
  205. */
  206. int
  207. GNUNET_SECRETSHARING_encrypt (const struct GNUNET_SECRETSHARING_PublicKey *public_key,
  208. const struct GNUNET_SECRETSHARING_Plaintext *plaintext,
  209. struct GNUNET_SECRETSHARING_Ciphertext *result_ciphertext);
  210. /**
  211. * Publish the given ciphertext for decryption. Once a sufficient (>=k) number of peers has
  212. * published the same value, it will be decrypted.
  213. *
  214. * When the operation is canceled, the decrypt_cb is not called anymore, but the calling
  215. * peer may already have irrevocably contributed his share for the decryption of the value.
  216. *
  217. * @param cfg configuration to use
  218. * @param share our secret share to use for decryption
  219. * @param ciphertext ciphertext to publish in order to decrypt it (if enough peers agree)
  220. * @param decrypt_cb callback called once the decryption succeeded
  221. * @param start By when should the cooperation for decryption start?
  222. * @param deadline By when should the decryption be finished?
  223. * @param decrypt_cb_cls closure for @a decrypt_cb
  224. * @return handle to cancel the operation
  225. */
  226. struct GNUNET_SECRETSHARING_DecryptionHandle *
  227. GNUNET_SECRETSHARING_decrypt (const struct GNUNET_CONFIGURATION_Handle *cfg,
  228. struct GNUNET_SECRETSHARING_Share *share,
  229. const struct GNUNET_SECRETSHARING_Ciphertext *ciphertext,
  230. struct GNUNET_TIME_Absolute start,
  231. struct GNUNET_TIME_Absolute deadline,
  232. GNUNET_SECRETSHARING_DecryptCallback decrypt_cb,
  233. void *decrypt_cb_cls);
  234. /**
  235. * Cancel a decryption.
  236. *
  237. * The decrypt_cb is not called anymore, but the calling
  238. * peer may already have irrevocably contributed his share for the decryption of the value.
  239. *
  240. * @param dh to cancel
  241. */
  242. void
  243. GNUNET_SECRETSHARING_decrypt_cancel (struct GNUNET_SECRETSHARING_DecryptionHandle *dh);
  244. /**
  245. * Read a share from its binary representation.
  246. *
  247. * @param data Binary representation of the share.
  248. * @param len Length of @a data.
  249. * @param[out] readlen Number of bytes read,
  250. * ignored if NULL.
  251. * @return The share, or NULL on error.
  252. */
  253. struct GNUNET_SECRETSHARING_Share *
  254. GNUNET_SECRETSHARING_share_read (const void *data, size_t len, size_t *readlen);
  255. /**
  256. * Convert a share to its binary representation.
  257. * Can be called with a NULL @a buf to get the size of the share.
  258. *
  259. * @param share Share to write.
  260. * @param buf Buffer to write to.
  261. * @param buflen Number of writable bytes in @a buf.
  262. * @param[out] writelen Pointer to store number of bytes written,
  263. * ignored if NULL.
  264. * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure.
  265. */
  266. int
  267. GNUNET_SECRETSHARING_share_write (const struct GNUNET_SECRETSHARING_Share *share,
  268. void *buf, size_t buflen, size_t *writelen);
  269. void
  270. GNUNET_SECRETSHARING_share_destroy (struct GNUNET_SECRETSHARING_Share *share);
  271. int
  272. GNUNET_SECRETSHARING_plaintext_generate (struct GNUNET_SECRETSHARING_Plaintext *plaintext,
  273. gcry_mpi_t exponent);
  274. int
  275. GNUNET_SECRETSHARING_plaintext_generate_i (struct GNUNET_SECRETSHARING_Plaintext *plaintext,
  276. int64_t exponent);
  277. #if 0 /* keep Emacsens' auto-indent happy */
  278. {
  279. #endif
  280. #ifdef __cplusplus
  281. }
  282. #endif
  283. #endif
  284. /** @} */ /* end of group */