diff options
author | Christian Fuchs <christian.fuchs@cfuchs.net> | 2013-10-08 17:06:06 +0000 |
---|---|---|
committer | Christian Fuchs <christian.fuchs@cfuchs.net> | 2013-10-08 17:06:06 +0000 |
commit | ad501d7d03b2c998ad703abc8dc8f02b3acec2ac (patch) | |
tree | 95e1d63980fafe4bf547d12a7f9a20fd739552e5 /src/scalarproduct | |
parent | 045bc5be4f794b863b334a41c126c222c5af6c3c (diff) | |
download | gnunet-ad501d7d03b2c998ad703abc8dc8f02b3acec2ac.tar.gz gnunet-ad501d7d03b2c998ad703abc8dc8f02b3acec2ac.zip |
onwards towards victory... more multipart receiving...
Diffstat (limited to 'src/scalarproduct')
-rw-r--r-- | src/scalarproduct/gnunet-service-scalarproduct.c | 154 |
1 files changed, 117 insertions, 37 deletions
diff --git a/src/scalarproduct/gnunet-service-scalarproduct.c b/src/scalarproduct/gnunet-service-scalarproduct.c index 3d4a37754..6462a1407 100644 --- a/src/scalarproduct/gnunet-service-scalarproduct.c +++ b/src/scalarproduct/gnunet-service-scalarproduct.c | |||
@@ -168,6 +168,16 @@ struct ServiceSession | |||
168 | * Bob's permutation q of R | 168 | * Bob's permutation q of R |
169 | */ | 169 | */ |
170 | gcry_mpi_t * r_prime; | 170 | gcry_mpi_t * r_prime; |
171 | |||
172 | /** | ||
173 | * Bob's s | ||
174 | */ | ||
175 | gcry_mpi_t s; | ||
176 | |||
177 | /** | ||
178 | * Bob's s' | ||
179 | */ | ||
180 | gcry_mpi_t s_prime; | ||
171 | 181 | ||
172 | /** | 182 | /** |
173 | * Bobs matching response session from the client | 183 | * Bobs matching response session from the client |
@@ -1709,8 +1719,7 @@ tunnel_destruction_handler (void *cls, | |||
1709 | * @return product as MPI, never NULL | 1719 | * @return product as MPI, never NULL |
1710 | */ | 1720 | */ |
1711 | static gcry_mpi_t | 1721 | static gcry_mpi_t |
1712 | compute_scalar_product (struct ServiceSession * session, | 1722 | compute_scalar_product (struct ServiceSession * session) |
1713 | gcry_mpi_t * r, gcry_mpi_t * r_prime, gcry_mpi_t s, gcry_mpi_t s_prime) | ||
1714 | { | 1723 | { |
1715 | uint32_t count; | 1724 | uint32_t count; |
1716 | gcry_mpi_t t; | 1725 | gcry_mpi_t t; |
@@ -1727,12 +1736,12 @@ compute_scalar_product (struct ServiceSession * session, | |||
1727 | // from the E(a_pi)(+)E(-b_pi-r_pi) and E(a_qi)(+)E(-r_qi) twice each, | 1736 | // from the E(a_pi)(+)E(-b_pi-r_pi) and E(a_qi)(+)E(-r_qi) twice each, |
1728 | // the result is E((S + a_pi) + (S -b_pi-r_pi)) and E(S + a_qi + S - r_qi) | 1737 | // the result is E((S + a_pi) + (S -b_pi-r_pi)) and E(S + a_qi + S - r_qi) |
1729 | for (i = 0; i < count; i++) { | 1738 | for (i = 0; i < count; i++) { |
1730 | decrypt_element (r[i], r[i], my_mu, my_lambda, my_n, my_nsquare); | 1739 | decrypt_element (session->r[i], session->r[i], my_mu, my_lambda, my_n, my_nsquare); |
1731 | gcry_mpi_sub (r[i], r[i], my_offset); | 1740 | gcry_mpi_sub (session->r[i], session->r[i], my_offset); |
1732 | gcry_mpi_sub (r[i], r[i], my_offset); | 1741 | gcry_mpi_sub (session->r[i], session->r[i], my_offset); |
1733 | decrypt_element (r_prime[i], r_prime[i], my_mu, my_lambda, my_n, my_nsquare); | 1742 | decrypt_element (session->r_prime[i], session->r_prime[i], my_mu, my_lambda, my_n, my_nsquare); |
1734 | gcry_mpi_sub (r_prime[i], r_prime[i], my_offset); | 1743 | gcry_mpi_sub (session->r_prime[i], session->r_prime[i], my_offset); |
1735 | gcry_mpi_sub (r_prime[i], r_prime[i], my_offset); | 1744 | gcry_mpi_sub (session->r_prime[i], session->r_prime[i], my_offset); |
1736 | } | 1745 | } |
1737 | 1746 | ||
1738 | // calculate t = sum(ai) | 1747 | // calculate t = sum(ai) |
@@ -1740,28 +1749,28 @@ compute_scalar_product (struct ServiceSession * session, | |||
1740 | 1749 | ||
1741 | // calculate U | 1750 | // calculate U |
1742 | u = gcry_mpi_new (0); | 1751 | u = gcry_mpi_new (0); |
1743 | tmp = compute_square_sum (r, count); | 1752 | tmp = compute_square_sum (session->r, count); |
1744 | gcry_mpi_sub (u, u, tmp); | 1753 | gcry_mpi_sub (u, u, tmp); |
1745 | gcry_mpi_release (tmp); | 1754 | gcry_mpi_release (tmp); |
1746 | 1755 | ||
1747 | //calculate U' | 1756 | //calculate U' |
1748 | utick = gcry_mpi_new (0); | 1757 | utick = gcry_mpi_new (0); |
1749 | tmp = compute_square_sum (r_prime, count); | 1758 | tmp = compute_square_sum (session->r_prime, count); |
1750 | gcry_mpi_sub (utick, utick, tmp); | 1759 | gcry_mpi_sub (utick, utick, tmp); |
1751 | 1760 | ||
1752 | GNUNET_assert (p = gcry_mpi_new (0)); | 1761 | GNUNET_assert (p = gcry_mpi_new (0)); |
1753 | GNUNET_assert (ptick = gcry_mpi_new (0)); | 1762 | GNUNET_assert (ptick = gcry_mpi_new (0)); |
1754 | 1763 | ||
1755 | // compute P | 1764 | // compute P |
1756 | decrypt_element (s, s, my_mu, my_lambda, my_n, my_nsquare); | 1765 | decrypt_element (session->s, session->s, my_mu, my_lambda, my_n, my_nsquare); |
1757 | decrypt_element (s_prime, s_prime, my_mu, my_lambda, my_n, my_nsquare); | 1766 | decrypt_element (session->s_prime, session->s_prime, my_mu, my_lambda, my_n, my_nsquare); |
1758 | 1767 | ||
1759 | // compute P | 1768 | // compute P |
1760 | gcry_mpi_add (p, s, t); | 1769 | gcry_mpi_add (p, session->s, t); |
1761 | gcry_mpi_add (p, p, u); | 1770 | gcry_mpi_add (p, p, u); |
1762 | 1771 | ||
1763 | // compute P' | 1772 | // compute P' |
1764 | gcry_mpi_add (ptick, s_prime, t); | 1773 | gcry_mpi_add (ptick, session->s_prime, t); |
1765 | gcry_mpi_add (ptick, ptick, utick); | 1774 | gcry_mpi_add (ptick, ptick, utick); |
1766 | 1775 | ||
1767 | gcry_mpi_release (t); | 1776 | gcry_mpi_release (t); |
@@ -2166,6 +2175,82 @@ handle_service_response_multipart (void *cls, | |||
2166 | void **tunnel_ctx, | 2175 | void **tunnel_ctx, |
2167 | const struct GNUNET_MessageHeader * message) | 2176 | const struct GNUNET_MessageHeader * message) |
2168 | { | 2177 | { |
2178 | struct ServiceSession * session; | ||
2179 | const struct GNUNET_SCALARPRODUCT_multipart_message * msg = (const struct GNUNET_SCALARPRODUCT_multipart_message *) message; | ||
2180 | unsigned char * current; | ||
2181 | size_t read; | ||
2182 | size_t i; | ||
2183 | uint32_t contained_element_count; | ||
2184 | size_t msg_size; | ||
2185 | int rc; | ||
2186 | |||
2187 | GNUNET_assert (NULL != message); | ||
2188 | // are we in the correct state? | ||
2189 | session = (struct ServiceSession *) * tunnel_ctx; | ||
2190 | if (ALICE != session->role) { | ||
2191 | goto except; | ||
2192 | } | ||
2193 | if (WAITING_FOR_MULTIPART_TRANSMISSION != session->state) { | ||
2194 | goto except; | ||
2195 | } | ||
2196 | // shorter than minimum? | ||
2197 | if (ntohs (msg->header.size) <= sizeof (struct GNUNET_SCALARPRODUCT_multipart_message)) { | ||
2198 | goto except; | ||
2199 | } | ||
2200 | contained_element_count = ntohl (msg->multipart_element_count); | ||
2201 | msg_size = sizeof (struct GNUNET_SCALARPRODUCT_multipart_message) | ||
2202 | + 2 * contained_element_count * PAILLIER_ELEMENT_LENGTH; | ||
2203 | //sanity check: is the message as long as the message_count fields suggests? | ||
2204 | if ((ntohs (msg->header.size) != msg_size) || (session->used_element_count < contained_element_count)) { | ||
2205 | goto except; | ||
2206 | } | ||
2207 | //convert s | ||
2208 | current = (unsigned char *) &msg[1]; | ||
2209 | // Convert each k[][perm] to its MPI_value | ||
2210 | for (i = 0; i < contained_element_count; i++) { | ||
2211 | if (0 != (rc = gcry_mpi_scan (&session->r[i], GCRYMPI_FMT_USG, current, | ||
2212 | PAILLIER_ELEMENT_LENGTH, &read))) { | ||
2213 | LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc); | ||
2214 | GNUNET_break_op (0); | ||
2215 | goto except; | ||
2216 | } | ||
2217 | current += PAILLIER_ELEMENT_LENGTH; | ||
2218 | if (0 != (rc = gcry_mpi_scan (&session->r_prime[i], GCRYMPI_FMT_USG, current, | ||
2219 | PAILLIER_ELEMENT_LENGTH, &read))) { | ||
2220 | LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc); | ||
2221 | GNUNET_break_op (0); | ||
2222 | goto except; | ||
2223 | } | ||
2224 | current += PAILLIER_ELEMENT_LENGTH; | ||
2225 | } | ||
2226 | session->transferred_element_count += contained_element_count; | ||
2227 | if (session->transferred_element_count == session->used_element_count){ | ||
2228 | session->state = SERVICE_RESPONSE_RECEIVED; | ||
2229 | session->product = compute_scalar_product (session); | ||
2230 | return GNUNET_SYSERR; // terminate the tunnel right away, we are done here! | ||
2231 | } | ||
2232 | return GNUNET_OK; | ||
2233 | except: | ||
2234 | GNUNET_break_op (0); | ||
2235 | if (session->s) | ||
2236 | gcry_mpi_release (session->s); | ||
2237 | if (session->s_prime) | ||
2238 | gcry_mpi_release (session->s_prime); | ||
2239 | for (i = 0; session->r && i < session->transferred_element_count; i++) | ||
2240 | if (session->r[i]) gcry_mpi_release (session->r[i]); | ||
2241 | for (i = 0; session->r_prime && i < session->transferred_element_count; i++) | ||
2242 | if (session->r_prime[i]) gcry_mpi_release (session->r_prime[i]); | ||
2243 | GNUNET_free_non_null (session->r); | ||
2244 | GNUNET_free_non_null (session->r_prime); | ||
2245 | |||
2246 | session->tunnel = NULL; | ||
2247 | // send message with product to client | ||
2248 | session->client_notification_task = | ||
2249 | GNUNET_SCHEDULER_add_now (&prepare_client_response, | ||
2250 | session); | ||
2251 | // the tunnel has done its job, terminate our connection and the tunnel | ||
2252 | // the peer will be notified that the tunnel was destroyed via tunnel_destruction_handler | ||
2253 | // just close the connection, as recommended by Christian | ||
2169 | return GNUNET_SYSERR; | 2254 | return GNUNET_SYSERR; |
2170 | } | 2255 | } |
2171 | 2256 | ||
@@ -2190,14 +2275,10 @@ handle_service_response (void *cls, | |||
2190 | struct ServiceSession * session; | 2275 | struct ServiceSession * session; |
2191 | const struct GNUNET_SCALARPRODUCT_service_response * msg = (const struct GNUNET_SCALARPRODUCT_service_response *) message; | 2276 | const struct GNUNET_SCALARPRODUCT_service_response * msg = (const struct GNUNET_SCALARPRODUCT_service_response *) message; |
2192 | unsigned char * current; | 2277 | unsigned char * current; |
2193 | gcry_mpi_t s = NULL; | ||
2194 | gcry_mpi_t s_prime = NULL; | ||
2195 | size_t read; | 2278 | size_t read; |
2196 | size_t i; | 2279 | size_t i; |
2197 | uint32_t contained_element_count; | 2280 | uint32_t contained_element_count; |
2198 | size_t msg_size; | 2281 | size_t msg_size; |
2199 | gcry_mpi_t * r = NULL; | ||
2200 | gcry_mpi_t * r_prime = NULL; | ||
2201 | int rc; | 2282 | int rc; |
2202 | 2283 | ||
2203 | GNUNET_assert (NULL != message); | 2284 | GNUNET_assert (NULL != message); |
@@ -2207,7 +2288,6 @@ handle_service_response (void *cls, | |||
2207 | return GNUNET_SYSERR; | 2288 | return GNUNET_SYSERR; |
2208 | } | 2289 | } |
2209 | 2290 | ||
2210 | session->product = NULL; | ||
2211 | session->state = WAITING_FOR_MULTIPART_TRANSMISSION; | 2291 | session->state = WAITING_FOR_MULTIPART_TRANSMISSION; |
2212 | 2292 | ||
2213 | //we need at least a peer and one message id to compare | 2293 | //we need at least a peer and one message id to compare |
@@ -2227,7 +2307,7 @@ handle_service_response (void *cls, | |||
2227 | session->transferred_element_count = contained_element_count; | 2307 | session->transferred_element_count = contained_element_count; |
2228 | //convert s | 2308 | //convert s |
2229 | current = (unsigned char *) &msg[1]; | 2309 | current = (unsigned char *) &msg[1]; |
2230 | if (0 != (rc = gcry_mpi_scan (&s, GCRYMPI_FMT_USG, current, | 2310 | if (0 != (rc = gcry_mpi_scan (&session->s, GCRYMPI_FMT_USG, current, |
2231 | PAILLIER_ELEMENT_LENGTH, &read))) { | 2311 | PAILLIER_ELEMENT_LENGTH, &read))) { |
2232 | LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc); | 2312 | LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc); |
2233 | GNUNET_break_op (0); | 2313 | GNUNET_break_op (0); |
@@ -2235,25 +2315,25 @@ handle_service_response (void *cls, | |||
2235 | } | 2315 | } |
2236 | current += PAILLIER_ELEMENT_LENGTH; | 2316 | current += PAILLIER_ELEMENT_LENGTH; |
2237 | //convert stick | 2317 | //convert stick |
2238 | if (0 != (rc = gcry_mpi_scan (&s_prime, GCRYMPI_FMT_USG, current, | 2318 | if (0 != (rc = gcry_mpi_scan (&session->s_prime, GCRYMPI_FMT_USG, current, |
2239 | PAILLIER_ELEMENT_LENGTH, &read))) { | 2319 | PAILLIER_ELEMENT_LENGTH, &read))) { |
2240 | LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc); | 2320 | LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc); |
2241 | GNUNET_break_op (0); | 2321 | GNUNET_break_op (0); |
2242 | goto invalid_msg; | 2322 | goto invalid_msg; |
2243 | } | 2323 | } |
2244 | current += PAILLIER_ELEMENT_LENGTH; | 2324 | current += PAILLIER_ELEMENT_LENGTH; |
2245 | r = GNUNET_malloc (sizeof (gcry_mpi_t) * count); | 2325 | session->r = GNUNET_malloc (sizeof (gcry_mpi_t) * count); |
2246 | r_prime = GNUNET_malloc (sizeof (gcry_mpi_t) * count); | 2326 | session->r_prime = GNUNET_malloc (sizeof (gcry_mpi_t) * count); |
2247 | // Convert each k[][perm] to its MPI_value | 2327 | // Convert each k[][perm] to its MPI_value |
2248 | for (i = 0; i < contained_element_count; i++) { | 2328 | for (i = 0; i < contained_element_count; i++) { |
2249 | if (0 != (rc = gcry_mpi_scan (&r[i], GCRYMPI_FMT_USG, current, | 2329 | if (0 != (rc = gcry_mpi_scan (&session->r[i], GCRYMPI_FMT_USG, current, |
2250 | PAILLIER_ELEMENT_LENGTH, &read))) { | 2330 | PAILLIER_ELEMENT_LENGTH, &read))) { |
2251 | LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc); | 2331 | LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc); |
2252 | GNUNET_break_op (0); | 2332 | GNUNET_break_op (0); |
2253 | goto invalid_msg; | 2333 | goto invalid_msg; |
2254 | } | 2334 | } |
2255 | current += PAILLIER_ELEMENT_LENGTH; | 2335 | current += PAILLIER_ELEMENT_LENGTH; |
2256 | if (0 != (rc = gcry_mpi_scan (&r_prime[i], GCRYMPI_FMT_USG, current, | 2336 | if (0 != (rc = gcry_mpi_scan (&session->r_prime[i], GCRYMPI_FMT_USG, current, |
2257 | PAILLIER_ELEMENT_LENGTH, &read))) { | 2337 | PAILLIER_ELEMENT_LENGTH, &read))) { |
2258 | LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc); | 2338 | LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc); |
2259 | GNUNET_break_op (0); | 2339 | GNUNET_break_op (0); |
@@ -2263,21 +2343,21 @@ handle_service_response (void *cls, | |||
2263 | } | 2343 | } |
2264 | if (session->transferred_element_count == session->used_element_count){ | 2344 | if (session->transferred_element_count == session->used_element_count){ |
2265 | session->state = SERVICE_RESPONSE_RECEIVED; | 2345 | session->state = SERVICE_RESPONSE_RECEIVED; |
2266 | session->product = compute_scalar_product (session, r, r_prime, s, s_prime); | 2346 | session->product = compute_scalar_product (session); |
2267 | return GNUNET_SYSERR; | 2347 | return GNUNET_SYSERR; // terminate the tunnel right away, we are done here! |
2268 | } | 2348 | } |
2269 | return GNUNET_OK; | 2349 | return GNUNET_OK; |
2270 | invalid_msg: | 2350 | invalid_msg: |
2271 | if (s) | 2351 | if (session->s) |
2272 | gcry_mpi_release (s); | 2352 | gcry_mpi_release (session->s); |
2273 | if (s_prime) | 2353 | if (session->s_prime) |
2274 | gcry_mpi_release (s_prime); | 2354 | gcry_mpi_release (session->s_prime); |
2275 | for (i = 0; r && i < contained_element_count; i++) | 2355 | for (i = 0; session->r && i < contained_element_count; i++) |
2276 | if (r[i]) gcry_mpi_release (r[i]); | 2356 | if (session->r[i]) gcry_mpi_release (session->r[i]); |
2277 | for (i = 0; r_prime && i < contained_element_count; i++) | 2357 | for (i = 0; session->r_prime && i < contained_element_count; i++) |
2278 | if (r_prime[i]) gcry_mpi_release (r_prime[i]); | 2358 | if (session->r_prime[i]) gcry_mpi_release (session->r_prime[i]); |
2279 | GNUNET_free_non_null (r); | 2359 | GNUNET_free_non_null (session->r); |
2280 | GNUNET_free_non_null (r_prime); | 2360 | GNUNET_free_non_null (session->r_prime); |
2281 | 2361 | ||
2282 | session->tunnel = NULL; | 2362 | session->tunnel = NULL; |
2283 | // send message with product to client | 2363 | // send message with product to client |