2 This file is part of GNUnet.
3 (C) 2014 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/crypto_paillier.c
23 * @brief implementation of the paillier crypto system with libgcrypt
24 * @author Florian Dold
25 * @author Christian Fuchs
29 #include "gnunet_util_lib.h"
33 * Create a freshly generated paillier public key.
35 * @param[out] public_key Where to store the public key?
36 * @param[out] private_key Where to store the private key?
39 GNUNET_CRYPTO_paillier_create (struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
40 struct GNUNET_CRYPTO_PaillierPrivateKey *private_key)
48 GNUNET_assert (NULL != (phi = gcry_mpi_new (GNUNET_CRYPTO_PAILLIER_BITS)));
49 GNUNET_assert (NULL != (n = gcry_mpi_new (GNUNET_CRYPTO_PAILLIER_BITS)));
53 // Generate two distinct primes.
54 // The probability that the loop body
55 // is executed more than once is very low.
61 // generate rsa modulus
62 GNUNET_assert (0 == gcry_prime_generate (&p, GNUNET_CRYPTO_PAILLIER_BITS / 2, 0, NULL, NULL, NULL,
63 GCRY_WEAK_RANDOM, 0));
64 GNUNET_assert (0 == gcry_prime_generate (&q, GNUNET_CRYPTO_PAILLIER_BITS / 2, 0, NULL, NULL, NULL,
65 GCRY_WEAK_RANDOM, 0));
66 } while (0 == gcry_mpi_cmp (p, q));
67 gcry_mpi_mul (n, p, q);
68 GNUNET_CRYPTO_mpi_print_unsigned (public_key, sizeof (struct GNUNET_CRYPTO_PaillierPublicKey), n);
70 // compute phi(n) = (p-1)(q-1)
71 gcry_mpi_sub_ui (p, p, 1);
72 gcry_mpi_sub_ui (q, q, 1);
73 gcry_mpi_mul (phi, p, q);
75 // lambda equals phi(n) in the simplified key generation
76 GNUNET_CRYPTO_mpi_print_unsigned (private_key->lambda, GNUNET_CRYPTO_PAILLIER_BITS / 8, phi);
78 // invert phi and abuse the phi mpi to store the result ...
79 GNUNET_assert (0 != gcry_mpi_invm (phi, phi, n));
80 GNUNET_CRYPTO_mpi_print_unsigned (private_key->mu, GNUNET_CRYPTO_PAILLIER_BITS / 8, phi);
84 gcry_mpi_release (phi);
90 * Encrypt a plaintext with a paillier public key.
92 * @param public_key Public key to use.
93 * @param m Plaintext to encrypt.
94 * @param[out] ciphertext Encrytion of @a plaintext with @a public_key.
95 * @return guaranteed number of supported homomorphic operations >= 1, -1 for failure
98 GNUNET_CRYPTO_paillier_encrypt (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
100 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext)
111 // determine how many operations we could allow, if the other number
112 // has the same length.
113 GNUNET_assert (NULL != (tmp1 = gcry_mpi_set_ui(NULL, 1)));
114 GNUNET_assert (NULL != (tmp2 = gcry_mpi_set_ui(NULL, 2)));
115 gcry_mpi_mul_2exp(tmp1,tmp1,GNUNET_CRYPTO_PAILLIER_BITS);
116 for (possible_opts = 0; gcry_mpi_cmp(tmp1,m) > 0; possible_opts++){
117 gcry_mpi_div(tmp1, NULL, tmp1, tmp2 ,0);
119 gcry_mpi_release(tmp1);
120 gcry_mpi_release(tmp2);
121 if (0 >= possible_opts)
126 // reduce by one to guarantee the final homomorphic operation
127 ciphertext->remaining_ops = htonl(possible_opts);
129 GNUNET_assert (0 != (n_square = gcry_mpi_new (0)));
130 GNUNET_assert (0 != (r = gcry_mpi_new (0)));
131 GNUNET_assert (0 != (g = gcry_mpi_new (0)));
132 GNUNET_assert (0 != (c = gcry_mpi_new (0)));
134 GNUNET_CRYPTO_mpi_scan_unsigned (&n, public_key, sizeof (struct GNUNET_CRYPTO_PaillierPublicKey));
136 gcry_mpi_mul (n_square, n, n);
141 gcry_mpi_randomize (r, GNUNET_CRYPTO_PAILLIER_BITS, GCRY_WEAK_RANDOM);
143 while (gcry_mpi_cmp (r, n) >= 0);
145 // c = (n+1)^m mod n^2
146 gcry_mpi_add_ui (c, n, 1);
147 gcry_mpi_powm (c, c, m, n_square);
149 gcry_mpi_powm (r, r, n, n_square);
151 gcry_mpi_mulm (c, r, c, n_square);
153 GNUNET_CRYPTO_mpi_print_unsigned (ciphertext->bits,
154 sizeof ciphertext->bits,
157 gcry_mpi_release (n_square);
158 gcry_mpi_release (r);
159 gcry_mpi_release (c);
161 return possible_opts;
166 * Decrypt a paillier ciphertext with a private key.
168 * @param private_key Private key to use for decryption.
169 * @param public_key Public key to use for encryption.
170 * @param ciphertext Ciphertext to decrypt.
171 * @param[out] m Decryption of @a ciphertext with @private_key.
174 GNUNET_CRYPTO_paillier_decrypt (const struct GNUNET_CRYPTO_PaillierPrivateKey *private_key,
175 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
176 const struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext,
185 GNUNET_assert (0 != (n_square = gcry_mpi_new (0)));
187 GNUNET_CRYPTO_mpi_scan_unsigned (&lambda, private_key->lambda, sizeof private_key->lambda);
188 GNUNET_CRYPTO_mpi_scan_unsigned (&mu, private_key->mu, sizeof private_key->mu);
189 GNUNET_CRYPTO_mpi_scan_unsigned (&n, public_key, sizeof *public_key);
190 GNUNET_CRYPTO_mpi_scan_unsigned (&c, ciphertext, sizeof *ciphertext);
192 gcry_mpi_mul (n_square, n, n);
193 // m = c^lambda mod n^2
194 gcry_mpi_powm (m, c, lambda, n_square);
196 gcry_mpi_sub_ui (m, m, 1);
198 gcry_mpi_div (m, NULL, m, n, 0);
199 gcry_mpi_mulm (m, m, mu, n);
201 gcry_mpi_release (mu);
202 gcry_mpi_release (lambda);
203 gcry_mpi_release (n);
204 gcry_mpi_release (n_square);
205 gcry_mpi_release (c);
210 * Compute a ciphertext that represents the sum of the plaintext in @a x1 and @a x2
212 * Note that this operation can only be done a finite number of times
213 * before an overflow occurs.
215 * @param public_key Public key to use for encryption.
216 * @param c1 Paillier cipher text.
217 * @param c2 Paillier cipher text.
218 * @param[out] result Result of the homomorphic operation.
219 * @return #GNUNET_OK if the result could be computed,
220 * #GNUNET_SYSERR if no more homomorphic operations are remaining.
223 GNUNET_CRYPTO_paillier_hom_add (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
224 const struct GNUNET_CRYPTO_PaillierCiphertext *c1,
225 const struct GNUNET_CRYPTO_PaillierCiphertext *c2,
226 struct GNUNET_CRYPTO_PaillierCiphertext *result)
235 o1 = ntohl(c1->remaining_ops);
236 o2 = ntohl(c2->remaining_ops);
237 if (0 >= o1 || 0 >= o2)
238 return GNUNET_SYSERR;
240 GNUNET_assert (0 != (c = gcry_mpi_new (0)));
242 GNUNET_CRYPTO_mpi_scan_unsigned (&a, c1->bits, sizeof c1->bits);
243 GNUNET_CRYPTO_mpi_scan_unsigned (&b, c1->bits, sizeof c2->bits);
244 GNUNET_CRYPTO_mpi_scan_unsigned (&n_square, public_key, sizeof *public_key);
245 gcry_mpi_mul(n_square, n_square,n_square);
246 gcry_mpi_mulm(c,a,b,n_square);
248 result->remaining_ops = htonl(((o2 > o1) ? o1 : o2) - 1);
249 GNUNET_CRYPTO_mpi_print_unsigned (result->bits,
252 gcry_mpi_release (a);
253 gcry_mpi_release (b);
254 gcry_mpi_release (c);
255 gcry_mpi_release (n_square);
256 return ntohl(result->remaining_ops);
261 * Get the number of remaining supported homomorphic operations.
263 * @param c Paillier cipher text.
264 * @return the number of remaining homomorphic operations
267 GNUNET_CRYPTO_paillier_hom_get_remaining (const struct GNUNET_CRYPTO_PaillierCiphertext *c)
269 GNUNET_assert(NULL != c);
270 return ntohl(c->remaining_ops);
273 /* end of crypto_paillier.c */