From 59b6159e562eecf59209631e6f51942efffa0e9e Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Fri, 28 Feb 2020 14:56:59 +0100 Subject: [PATCH] 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. --- common/sphinx.c | 37 +++++++++++++++++++++++++++++++++++++ common/sphinx.h | 19 +++++++++++++++++++ common/test/run-sphinx.c | 3 +++ 3 files changed, 59 insertions(+) diff --git a/common/sphinx.c b/common/sphinx.c index 9155dd247..972d84279 100644 --- a/common/sphinx.c +++ b/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) diff --git a/common/sphinx.h b/common/sphinx.h index 045167d12..28fd588e1 100644 --- a/common/sphinx.h +++ b/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; diff --git a/common/test/run-sphinx.c b/common/test/run-sphinx.c index 649908818..66659e2b1 100644 --- a/common/test/run-sphinx.c +++ b/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;