Browse Source

sphinx: Functions to enable RV mode and serialize compressed onions

We will later use these to generate RV compressed onions and to opt into the
rendezvous style generation.
travis-debug
Christian Decker 5 years ago
committed by Rusty Russell
parent
commit
59b6159e56
  1. 37
      common/sphinx.c
  2. 19
      common/sphinx.h
  3. 3
      common/test/run-sphinx.c

37
common/sphinx.c

@ -86,6 +86,19 @@ struct sphinx_path *sphinx_path_new_with_key(const tal_t *ctx,
return sp;
}
bool sphinx_path_set_rendezvous(struct sphinx_path *sp,
const struct node_id *rendezvous_id)
{
if (rendezvous_id == NULL) {
sp->rendezvous_id = tal_free(sp->rendezvous_id);
return true;
} else {
sp->rendezvous_id = tal_free(sp->rendezvous_id);
sp->rendezvous_id = tal(sp, struct pubkey);
return pubkey_from_node_id(sp->rendezvous_id, rendezvous_id);
}
}
static size_t sphinx_hop_size(const struct sphinx_hop *hop)
{
return tal_bytelen(hop->raw_payload) + HMAC_SIZE;
@ -143,6 +156,30 @@ u8 *serialize_onionpacket(
return dst;
}
u8 *serialize_compressed_onion(const tal_t *ctx,
const struct sphinx_path *sp,
const struct onionpacket *packet)
{
u8 *dst;
u8 der[PUBKEY_CMPR_LEN];
size_t payloads_size = sphinx_path_payloads_size(sp);
size_t max_prefill = ROUTING_INFO_SIZE - payloads_size;
size_t rv_onion_size = TOTAL_PACKET_SIZE - max_prefill;
int p = 0;
assert(sp->rendezvous_id != NULL);
dst = tal_arr(ctx, u8, rv_onion_size);
pubkey_to_der(der, &packet->ephemeralkey);
write_buffer(dst, &packet->version, 1, &p);
write_buffer(dst, der, sizeof(der), &p);
write_buffer(dst, packet->routinginfo, ROUTING_INFO_SIZE - max_prefill, &p);
write_buffer(dst, packet->mac, sizeof(packet->mac), &p);
return dst;
}
enum onion_type parse_onionpacket(const u8 *src,
const size_t srclen,
struct onionpacket *dest)

19
common/sphinx.h

@ -227,6 +227,25 @@ void sphinx_add_hop(struct sphinx_path *path, const struct pubkey *pubkey,
*/
size_t sphinx_path_payloads_size(const struct sphinx_path *path);
/**
* Compress a rendez-vous onion by removing the unused blinded middle
* part. This middle part can be regenerated by the node processing this
* onion.
*/
u8 *serialize_compressed_onion(const tal_t *ctx,
const struct sphinx_path *sp,
const struct onionpacket *packet);
/**
* Set the rendez-vous node_id and make the onion generated from the
* sphinx_path compressible. To unset pass in a NULL rendezvous_id.
*
* Returns false if there was an error converting from the node_id to a public
* key.
*/
bool sphinx_path_set_rendezvous(struct sphinx_path *sp,
const struct node_id *rendezvous_id);
#if DEVELOPER
/* Override to force us to reject valid onion packets */
extern bool dev_fail_process_onionpacket;

3
common/test/run-sphinx.c

@ -45,6 +45,9 @@ size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNN
/* Generated stub for bigsize_put */
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED)
{ fprintf(stderr, "bigsize_put called!\n"); abort(); }
/* Generated stub for pubkey_from_node_id */
bool pubkey_from_node_id(struct pubkey *key UNNEEDED, const struct node_id *id UNNEEDED)
{ fprintf(stderr, "pubkey_from_node_id called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */
extern secp256k1_context *secp256k1_ctx;

Loading…
Cancel
Save