diff options
author | Christian Fuchs <christian.fuchs@cfuchs.net> | 2014-01-29 09:32:59 +0000 |
---|---|---|
committer | Christian Fuchs <christian.fuchs@cfuchs.net> | 2014-01-29 09:32:59 +0000 |
commit | ecaa90d24cde249ab9c739713bfc37f6b108b7e7 (patch) | |
tree | 42673b368e738d2080933af2e50f466d5bbdba39 /src/util/crypto_paillier.c | |
parent | 5523a1f4cf8cbd6ff6e5ab2eb8628a905d25da20 (diff) | |
download | gnunet-ecaa90d24cde249ab9c739713bfc37f6b108b7e7.tar.gz gnunet-ecaa90d24cde249ab9c739713bfc37f6b108b7e7.zip |
- omitted test for crypto_paillier for now...
Diffstat (limited to 'src/util/crypto_paillier.c')
-rw-r--r-- | src/util/crypto_paillier.c | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/src/util/crypto_paillier.c b/src/util/crypto_paillier.c index 37627498d..e83bcb4bb 100644 --- a/src/util/crypto_paillier.c +++ b/src/util/crypto_paillier.c | |||
@@ -99,21 +99,32 @@ GNUNET_CRYPTO_paillier_encrypt (const struct GNUNET_CRYPTO_PaillierPublicKey *pu | |||
99 | const gcry_mpi_t m, | 99 | const gcry_mpi_t m, |
100 | struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext) | 100 | struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext) |
101 | { | 101 | { |
102 | int length; | 102 | int possible_opts; |
103 | gcry_mpi_t n_square; | 103 | gcry_mpi_t n_square; |
104 | gcry_mpi_t r; | 104 | gcry_mpi_t r; |
105 | gcry_mpi_t g; | 105 | gcry_mpi_t g; |
106 | gcry_mpi_t c; | 106 | gcry_mpi_t c; |
107 | gcry_mpi_t n; | 107 | gcry_mpi_t n; |
108 | gcry_mpi_t tmp1; | ||
109 | gcry_mpi_t tmp2; | ||
108 | 110 | ||
109 | // determine how many operations we could allow, if the other number | 111 | // determine how many operations we could allow, if the other number |
110 | // has the same length. | 112 | // has the same length. |
111 | length = gcry_mpi_get_nbits(m); | 113 | GNUNET_assert (NULL != (tmp1 = gcry_mpi_set_ui(NULL, 1))); |
112 | if (GNUNET_CRYPTO_PAILLIER_BITS <= length) | 114 | GNUNET_assert (NULL != (tmp2 = gcry_mpi_set_ui(NULL, 2))); |
113 | //paillier with 0 ops makes no sense, better use RSA and co. | 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); | ||
118 | } | ||
119 | gcry_mpi_release(tmp1); | ||
120 | gcry_mpi_release(tmp2); | ||
121 | if (0 >= possible_opts) | ||
122 | { | ||
114 | return -1; | 123 | return -1; |
124 | } | ||
115 | else | 125 | else |
116 | ciphertext->remaining_ops = htonl(GNUNET_CRYPTO_PAILLIER_BITS - length); | 126 | // reduce by one to guarantee the final homomorphic operation |
127 | ciphertext->remaining_ops = htonl(possible_opts); | ||
117 | 128 | ||
118 | GNUNET_assert (0 != (n_square = gcry_mpi_new (0))); | 129 | GNUNET_assert (0 != (n_square = gcry_mpi_new (0))); |
119 | GNUNET_assert (0 != (r = gcry_mpi_new (0))); | 130 | GNUNET_assert (0 != (r = gcry_mpi_new (0))); |
@@ -147,7 +158,7 @@ GNUNET_CRYPTO_paillier_encrypt (const struct GNUNET_CRYPTO_PaillierPublicKey *pu | |||
147 | gcry_mpi_release (r); | 158 | gcry_mpi_release (r); |
148 | gcry_mpi_release (c); | 159 | gcry_mpi_release (c); |
149 | 160 | ||
150 | return GNUNET_CRYPTO_PAILLIER_BITS-length; | 161 | return possible_opts; |
151 | } | 162 | } |
152 | 163 | ||
153 | 164 | ||
@@ -218,8 +229,12 @@ GNUNET_CRYPTO_paillier_hom_add (const struct GNUNET_CRYPTO_PaillierPublicKey *pu | |||
218 | gcry_mpi_t b; | 229 | gcry_mpi_t b; |
219 | gcry_mpi_t c; | 230 | gcry_mpi_t c; |
220 | gcry_mpi_t n_square; | 231 | gcry_mpi_t n_square; |
232 | int32_t o1; | ||
233 | int32_t o2; | ||
221 | 234 | ||
222 | if (0 == c1->remaining_ops || 0 == c2->remaining_ops) | 235 | o1 = ntohl(c1->remaining_ops); |
236 | o2 = ntohl(c2->remaining_ops); | ||
237 | if (0 >= o1 || 0 >= o2) | ||
223 | return GNUNET_SYSERR; | 238 | return GNUNET_SYSERR; |
224 | 239 | ||
225 | GNUNET_assert (0 != (c = gcry_mpi_new (0))); | 240 | GNUNET_assert (0 != (c = gcry_mpi_new (0))); |
@@ -230,7 +245,7 @@ GNUNET_CRYPTO_paillier_hom_add (const struct GNUNET_CRYPTO_PaillierPublicKey *pu | |||
230 | gcry_mpi_mul(n_square, n_square,n_square); | 245 | gcry_mpi_mul(n_square, n_square,n_square); |
231 | gcry_mpi_mulm(c,a,b,n_square); | 246 | gcry_mpi_mulm(c,a,b,n_square); |
232 | 247 | ||
233 | result->remaining_ops = ((c1->remaining_ops > c2->remaining_ops) ? c2->remaining_ops : c1->remaining_ops) - 1; | 248 | result->remaining_ops = htonl(((o2 > o1) ? o1 : o2) - 1); |
234 | GNUNET_CRYPTO_mpi_print_unsigned (result->bits, | 249 | GNUNET_CRYPTO_mpi_print_unsigned (result->bits, |
235 | sizeof result->bits, | 250 | sizeof result->bits, |
236 | c); | 251 | c); |
@@ -238,7 +253,7 @@ GNUNET_CRYPTO_paillier_hom_add (const struct GNUNET_CRYPTO_PaillierPublicKey *pu | |||
238 | gcry_mpi_release (b); | 253 | gcry_mpi_release (b); |
239 | gcry_mpi_release (c); | 254 | gcry_mpi_release (c); |
240 | gcry_mpi_release (n_square); | 255 | gcry_mpi_release (n_square); |
241 | return GNUNET_OK; | 256 | return ntohl(result->remaining_ops); |
242 | } | 257 | } |
243 | 258 | ||
244 | 259 | ||