eb5bfe87c4353a9892c61f3bef0bff2a8dafca3e
[oweals/gnunet.git] / src / util / crypto_paillier.c
1 /*
2      This file is part of GNUnet.
3      (C) 2014 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20
21 /**
22  * @file util/crypto_paillier.c
23  * @brief implementation of the paillier crypto system with libgcrypt
24  * @author Florian Dold
25  * @author Christian Fuchs
26  */
27 #include "platform.h"
28 #include <gcrypt.h>
29 #include "gnunet_util_lib.h"
30
31
32 /**
33  * Create a freshly generated paillier public key.
34  *
35  * @param[out] public_key Where to store the public key?
36  * @param[out] private_key Where to store the private key?
37  */
38 void
39 GNUNET_CRYPTO_paillier_create (struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
40                                struct GNUNET_CRYPTO_PaillierPrivateKey *private_key)
41 {
42   gcry_mpi_t p;
43   gcry_mpi_t q;
44
45   gcry_mpi_t phi;
46   gcry_mpi_t n;
47
48   GNUNET_assert (NULL != (phi = gcry_mpi_new (0)));
49   GNUNET_assert (NULL != (n = gcry_mpi_new (0)));
50
51   p = q = NULL;
52
53   // Generate two distinct primes.
54   // The probability that the loop body
55   // is executed more than once is very low.
56   do {
57     if (NULL != p)
58       gcry_mpi_release (p);
59     if (NULL != q)
60       gcry_mpi_release (q);
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   }
67   while (0 == gcry_mpi_cmp (p, q));
68   gcry_mpi_mul (n, p, q);
69   GNUNET_CRYPTO_mpi_print_unsigned (public_key, sizeof (struct GNUNET_CRYPTO_PaillierPublicKey), n);
70
71   // compute phi(n) = (p-1)(q-1)
72   gcry_mpi_sub_ui (p, p, 1);
73   gcry_mpi_sub_ui (q, q, 1);
74   gcry_mpi_mul (phi, p, q);
75
76   // lambda equals phi(n) in the simplified key generation
77   GNUNET_CRYPTO_mpi_print_unsigned (private_key->lambda, GNUNET_CRYPTO_PAILLIER_BITS / 8, phi);
78
79   // invert phi and abuse the phi mpi to store the result ...
80   GNUNET_assert (0 != gcry_mpi_invm (phi, phi, n));
81   GNUNET_CRYPTO_mpi_print_unsigned (private_key->mu, GNUNET_CRYPTO_PAILLIER_BITS / 8, phi);
82
83   gcry_mpi_release (p);
84   gcry_mpi_release (q);
85   gcry_mpi_release (phi);
86   gcry_mpi_release (n);
87 }
88
89
90 /**
91  * Encrypt a plaintext with a paillier public key.
92  *
93  * @param public_key Public key to use.
94  * @param m Plaintext to encrypt.
95  * @param desired_ops How many homomorphic ops the caller intends to use
96  * @param[out] ciphertext Encrytion of @a plaintext with @a public_key.
97  * @return guaranteed number of supported homomorphic operations >= 1, 
98  *         or desired_ops, in case that is lower,
99  *         or -1 if less than one homomorphic operation is possible
100  */
101 int
102 GNUNET_CRYPTO_paillier_encrypt (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
103                                 const gcry_mpi_t m,
104                                 int desired_ops,
105                                 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext)
106 {
107   int possible_opts;
108   gcry_mpi_t n_square;
109   gcry_mpi_t r;
110   gcry_mpi_t g;
111   gcry_mpi_t c;
112   gcry_mpi_t n;
113   gcry_mpi_t tmp1;
114   gcry_mpi_t tmp2;
115
116   // determine how many operations we could allow, if the other number
117   // has the same length. 
118   GNUNET_assert (NULL != (tmp1 = gcry_mpi_set_ui (NULL, 1)));
119   GNUNET_assert (NULL != (tmp2 = gcry_mpi_set_ui (NULL, 2)));
120   gcry_mpi_mul_2exp (tmp1, tmp1, GNUNET_CRYPTO_PAILLIER_BITS);
121   
122   // count number of possible operations
123   // this would be nicer with gcry_mpi_get_nbits, however it does not return 
124   // the BITLENGTH of the given MPI's value, but the bits required
125   // to represent the number as MPI.
126   for (possible_opts = -2; gcry_mpi_cmp (tmp1, m) > 0; possible_opts++) {
127     gcry_mpi_div (tmp1, NULL, tmp1, tmp2, 0);
128   }
129   gcry_mpi_release (tmp1);
130   gcry_mpi_release (tmp2);
131   
132   if (possible_opts < 1)
133     possible_opts = 0;
134   //soft-cap by caller
135   possible_opts = (desired_ops < possible_opts)? desired_ops : possible_opts;
136   
137   ciphertext->remaining_ops = htonl (possible_opts);
138
139   GNUNET_assert (0 != (n_square = gcry_mpi_new (0)));
140   GNUNET_assert (0 != (r = gcry_mpi_new (0)));
141   GNUNET_assert (0 != (g = gcry_mpi_new (0)));
142   GNUNET_assert (0 != (c = gcry_mpi_new (0)));
143
144   GNUNET_CRYPTO_mpi_scan_unsigned (&n, public_key, sizeof (struct GNUNET_CRYPTO_PaillierPublicKey));
145
146   gcry_mpi_mul (n_square, n, n);
147
148   // generate r < n
149   do {
150     gcry_mpi_randomize (r, GNUNET_CRYPTO_PAILLIER_BITS, GCRY_WEAK_RANDOM);
151   }
152   while (gcry_mpi_cmp (r, n) >= 0);
153
154   // c = (n+1)^m mod n^2
155   gcry_mpi_add_ui (c, n, 1);
156   gcry_mpi_powm (c, c, m, n_square);
157   // r <- r^n mod n^2
158   gcry_mpi_powm (r, r, n, n_square);
159   // c <- r*c mod n^2
160   gcry_mpi_mulm (c, r, c, n_square);
161
162   GNUNET_CRYPTO_mpi_print_unsigned (ciphertext->bits,
163                                     sizeof ciphertext->bits,
164                                     c);
165
166   gcry_mpi_release (n_square);
167   gcry_mpi_release (r);
168   gcry_mpi_release (c);
169
170   return possible_opts;
171 }
172
173
174 /**
175  * Decrypt a paillier ciphertext with a private key.
176  *
177  * @param private_key Private key to use for decryption.
178  * @param public_key Public key to use for encryption.
179  * @param ciphertext Ciphertext to decrypt.
180  * @param[out] m Decryption of @a ciphertext with @private_key.
181  */
182 void
183 GNUNET_CRYPTO_paillier_decrypt (const struct GNUNET_CRYPTO_PaillierPrivateKey *private_key,
184                                 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
185                                 const struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext,
186                                 gcry_mpi_t m)
187 {
188   gcry_mpi_t mu;
189   gcry_mpi_t lambda;
190   gcry_mpi_t n;
191   gcry_mpi_t n_square;
192   gcry_mpi_t c;
193
194   GNUNET_assert (0 != (n_square = gcry_mpi_new (0)));
195
196   GNUNET_CRYPTO_mpi_scan_unsigned (&lambda, private_key->lambda, sizeof private_key->lambda);
197   GNUNET_CRYPTO_mpi_scan_unsigned (&mu, private_key->mu, sizeof private_key->mu);
198   GNUNET_CRYPTO_mpi_scan_unsigned (&n, public_key, sizeof *public_key);
199   GNUNET_CRYPTO_mpi_scan_unsigned (&c, ciphertext->bits, sizeof ciphertext->bits);
200
201   gcry_mpi_mul (n_square, n, n);
202   // m = c^lambda mod n^2
203   gcry_mpi_powm (m, c, lambda, n_square);
204   // m = m - 1
205   gcry_mpi_sub_ui (m, m, 1);
206   // m <- m/n
207   gcry_mpi_div (m, NULL, m, n, 0);
208   gcry_mpi_mulm (m, m, mu, n);
209
210   gcry_mpi_release (mu);
211   gcry_mpi_release (lambda);
212   gcry_mpi_release (n);
213   gcry_mpi_release (n_square);
214   gcry_mpi_release (c);
215 }
216
217
218 /**
219  * Compute a ciphertext that represents the sum of the plaintext in @a x1 and @a x2
220  *
221  * Note that this operation can only be done a finite number of times
222  * before an overflow occurs.
223  *
224  * @param public_key Public key to use for encryption.
225  * @param c1 Paillier cipher text.
226  * @param c2 Paillier cipher text.
227  * @param[out] result Result of the homomorphic operation.
228  * @return #GNUNET_OK if the result could be computed,
229  *         #GNUNET_SYSERR if no more homomorphic operations are remaining.
230  */
231 int
232 GNUNET_CRYPTO_paillier_hom_add (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
233                                 const struct GNUNET_CRYPTO_PaillierCiphertext *c1,
234                                 const struct GNUNET_CRYPTO_PaillierCiphertext *c2,
235                                 struct GNUNET_CRYPTO_PaillierCiphertext *result)
236 {
237   gcry_mpi_t a;
238   gcry_mpi_t b;
239   gcry_mpi_t c;
240   gcry_mpi_t n_square;
241   int32_t o1;
242   int32_t o2;
243
244   o1 = ntohl (c1->remaining_ops);
245   o2 = ntohl (c2->remaining_ops);
246   if (0 >= o1 || 0 >= o2)
247     return GNUNET_SYSERR;
248
249   GNUNET_assert (0 != (c = gcry_mpi_new (0)));
250
251   GNUNET_CRYPTO_mpi_scan_unsigned (&a, c1->bits, sizeof c1->bits);
252   GNUNET_CRYPTO_mpi_scan_unsigned (&b, c1->bits, sizeof c2->bits);
253   GNUNET_CRYPTO_mpi_scan_unsigned (&n_square, public_key, sizeof *public_key);
254   gcry_mpi_mul (n_square, n_square, n_square);
255   gcry_mpi_mulm (c, a, b, n_square);
256
257   result->remaining_ops = htonl (((o2 > o1) ? o1 : o2) - 1);
258   GNUNET_CRYPTO_mpi_print_unsigned (result->bits,
259                                     sizeof result->bits,
260                                     c);
261   gcry_mpi_release (a);
262   gcry_mpi_release (b);
263   gcry_mpi_release (c);
264   gcry_mpi_release (n_square);
265   return ntohl (result->remaining_ops);
266 }
267
268
269 /**
270  * Get the number of remaining supported homomorphic operations. 
271  *
272  * @param c Paillier cipher text.
273  * @return the number of remaining homomorphic operations
274  */
275 int
276 GNUNET_CRYPTO_paillier_hom_get_remaining (const struct GNUNET_CRYPTO_PaillierCiphertext *c)
277 {
278   GNUNET_assert (NULL != c);
279   return ntohl (c->remaining_ops);
280 }
281
282 /* end of crypto_paillier.c */