gnunet_secretsharing_service.h 12 KB

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