sphinx: Add functions to decompress

Also implements a way to decompress an onion using the devtools/onion tool

Changelog-Added: devtools: The `onion` tool can now generate, compress and decompress onions for rendez-vous routing
This commit is contained in:
Christian Decker
2020-02-28 17:55:43 +01:00
committed by Rusty Russell
parent 4724d55e80
commit 49a3321d7e
3 changed files with 64 additions and 0 deletions

View File

@@ -785,3 +785,27 @@ u8 *unwrap_onionreply(const tal_t *ctx,
return tal_free(final);
return final;
}
u8 *sphinx_decompress(const tal_t *ctx, const u8 *compressed,
struct secret *shared_secret)
{
size_t compressedlen = tal_bytelen(compressed);
size_t prefill_size = TOTAL_PACKET_SIZE - compressedlen;
u8 *dst;
int p = 0;
assert(prefill_size >= 0);
assert(compressedlen >= VERSION_SIZE + PUBKEY_SIZE + HMAC_SIZE);
dst = tal_arrz(ctx, u8, TOTAL_PACKET_SIZE);
write_buffer(
dst, compressed,
VERSION_SIZE + PUBKEY_SIZE + ROUTING_INFO_SIZE - prefill_size, &p);
/* We can just XOR here since we initialized the array with zeros. */
sphinx_prefill_stream_xor(dst + p, prefill_size, shared_secret);
p += prefill_size;
write_buffer(dst, compressed + compressedlen - HMAC_SIZE, HMAC_SIZE,
&p);
return dst;
}