- don't try to rekey loopback tunnels (#3254)
[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 (GNUNET_CRYPTO_PAILLIER_BITS)));
49   GNUNET_assert (NULL != (n = gcry_mpi_new (GNUNET_CRYPTO_PAILLIER_BITS)));
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   } 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);
69
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);
74
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);
77
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);
81
82   gcry_mpi_release (p);
83   gcry_mpi_release (q);
84   gcry_mpi_release (phi);
85   gcry_mpi_release (n);
86 }
87
88
89 /**
90  * Encrypt a plaintext with a paillier public key.
91  *
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
96  */
97 int
98 GNUNET_CRYPTO_paillier_encrypt (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
99                                 const gcry_mpi_t m,
100                                 struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext)
101 {
102   unsigned int length;
103   gcry_mpi_t n_square;
104   gcry_mpi_t r;
105   gcry_mpi_t g;
106   gcry_mpi_t c;
107   gcry_mpi_t n;
108   
109   // determine how many operations we could allow, if the other number
110   // has the same length. 
111   length = gcry_mpi_get_nbits(m);
112   if (length >= GNUNET_CRYPTO_PAILLIER_BITS)
113     return -1;
114   else
115     ciphertext->remaining_ops = ntohl(pow(2,(GNUNET_CRYPTO_PAILLIER_BITS-length-1)));
116   
117   GNUNET_assert (0 != (n_square = gcry_mpi_new (0)));
118   GNUNET_assert (0 != (r = gcry_mpi_new (0)));
119   GNUNET_assert (0 != (g = gcry_mpi_new (0)));
120   GNUNET_assert (0 != (c = gcry_mpi_new (0)));
121
122   GNUNET_CRYPTO_mpi_scan_unsigned (&n, public_key, sizeof (struct GNUNET_CRYPTO_PaillierPublicKey));
123
124   gcry_mpi_mul (n_square, n, n);
125
126   // generate r < n
127   do
128   {
129     gcry_mpi_randomize (r, GNUNET_CRYPTO_PAILLIER_BITS, GCRY_WEAK_RANDOM);
130   }
131   while (gcry_mpi_cmp (r, n) >= 0);
132
133   // c = (n+1)^m mod n^2
134   gcry_mpi_add_ui (c, n, 1);
135   gcry_mpi_powm (c, c, m, n_square);
136   // r <- r^n mod n^2
137   gcry_mpi_powm (r, r, n, n_square);
138   // c <- r*c mod n^2
139   gcry_mpi_mulm (c, r, c, n_square);
140
141   GNUNET_CRYPTO_mpi_print_unsigned (ciphertext->bits, 
142                                     sizeof ciphertext->bits, 
143                                     c);
144
145   gcry_mpi_release (n_square);
146   gcry_mpi_release (r);
147   gcry_mpi_release (c);
148   
149   return pow(2,(GNUNET_CRYPTO_PAILLIER_BITS-length-1));
150 }
151
152
153 /**
154  * Decrypt a paillier ciphertext with a private key.
155  *
156  * @param private_key Private key to use for decryption.
157  * @param public_key Public key to use for encryption.
158  * @param ciphertext Ciphertext to decrypt.
159  * @param[out] m Decryption of @a ciphertext with @private_key.
160  */
161 void
162 GNUNET_CRYPTO_paillier_decrypt (const struct GNUNET_CRYPTO_PaillierPrivateKey *private_key,
163                                 const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
164                                 const struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext,
165                                 gcry_mpi_t m)
166 {
167   gcry_mpi_t mu;
168   gcry_mpi_t lambda;
169   gcry_mpi_t n;
170   gcry_mpi_t n_square;
171   gcry_mpi_t c;
172
173   GNUNET_assert (0 != (n_square = gcry_mpi_new (0)));
174
175   GNUNET_CRYPTO_mpi_scan_unsigned (&lambda, private_key->lambda, sizeof private_key->lambda);
176   GNUNET_CRYPTO_mpi_scan_unsigned (&mu, private_key->mu, sizeof private_key->mu);
177   GNUNET_CRYPTO_mpi_scan_unsigned (&n, public_key, sizeof *public_key);
178   GNUNET_CRYPTO_mpi_scan_unsigned (&c, ciphertext, sizeof *ciphertext);
179
180   gcry_mpi_mul (n_square, n, n);
181   // m = c^lambda mod n^2
182   gcry_mpi_powm (m, c, lambda, n_square);
183   // m = m - 1
184   gcry_mpi_sub_ui (m, m, 1);
185   // m <- m/n
186   gcry_mpi_div (m, NULL, m, n, 0);
187   gcry_mpi_mulm (m, m, mu, n);
188
189   gcry_mpi_release (mu);
190   gcry_mpi_release (lambda);
191   gcry_mpi_release (n);
192   gcry_mpi_release (n_square);
193   gcry_mpi_release (c);
194 }
195
196
197 /**
198  * Compute a ciphertext that represents the sum of the plaintext in @a x1 and @a x2
199  *
200  * Note that this operation can only be done a finite number of times
201  * before an overflow occurs.
202  *
203  * @param public_key Public key to use for encryption.
204  * @param c1 Paillier cipher text.
205  * @param c2 Paillier cipher text.
206  * @param[out] result Result of the homomorphic operation.
207  * @return #GNUNET_OK if the result could be computed,
208  *         #GNUNET_SYSERR if no more homomorphic operations are remaining.
209  */
210 int
211 GNUNET_CRYPTO_paillier_hom_add (const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
212                                 const struct GNUNET_CRYPTO_PaillierCiphertext *c1,
213                                 const struct GNUNET_CRYPTO_PaillierCiphertext *c2,
214                                 struct GNUNET_CRYPTO_PaillierCiphertext *result)
215 {
216   gcry_mpi_t a;
217   gcry_mpi_t b;
218   gcry_mpi_t c;
219   gcry_mpi_t n_square;
220   
221   if (0 == c1->remaining_ops || 0 == c2->remaining_ops)
222     return GNUNET_SYSERR;
223   
224   GNUNET_assert (0 != (c = gcry_mpi_new (0)));
225   
226   GNUNET_CRYPTO_mpi_scan_unsigned (&a, c1->bits, sizeof c1->bits);
227   GNUNET_CRYPTO_mpi_scan_unsigned (&b, c1->bits, sizeof c2->bits);
228   GNUNET_CRYPTO_mpi_scan_unsigned (&n_square, public_key, sizeof *public_key);
229   gcry_mpi_mul(n_square, n_square,n_square);
230   gcry_mpi_mulm(c,a,b,n_square);
231   
232   result->remaining_ops = (c1->remaining_ops > c2->remaining_ops) ? c2->remaining_ops : c1->remaining_ops;
233   GNUNET_CRYPTO_mpi_print_unsigned (result->bits, 
234                                     sizeof result->bits, 
235                                     c);
236   gcry_mpi_release (a);
237   gcry_mpi_release (b);
238   gcry_mpi_release (c);
239   gcry_mpi_release (n_square);
240   return GNUNET_OK;
241 }
242
243
244 /* end of crypto_paillier.c */