MCPcopy Create free account
hub / github.com/ElementsProject/lightning / create_onionpacket

Function create_onionpacket

common/sphinx.c:526–638  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

524}
525
526struct onionpacket *create_onionpacket(
527 const tal_t *ctx,
528 struct sphinx_path *sp,
529 size_t fixed_size,
530 struct secret **path_secrets
531 )
532{
533 struct onionpacket *packet = talz(ctx, struct onionpacket);
534 int i, num_hops = tal_count(sp->hops);
535 size_t fillerSize = sphinx_path_payloads_size(sp) -
536 sphinx_hop_size(&sp->hops[num_hops - 1]);
537 u8 *filler;
538 struct secret padkey;
539 struct hmac nexthmac;
540 struct hop_params *params;
541 struct secret *secrets = tal_arr(ctx, struct secret, num_hops);
542 size_t payloads_size = sphinx_path_payloads_size(sp);
543 size_t max_prefill = fixed_size - payloads_size;
544
545 if (sphinx_path_payloads_size(sp) > fixed_size) {
546 tal_free(packet);
547 tal_free(secrets);
548 return NULL;
549 }
550 packet->routinginfo = tal_arr(packet, u8, fixed_size);
551
552 if (sp->session_key == NULL) {
553 sp->session_key = tal(sp, struct secret);
554 randbytes(sp->session_key, sizeof(struct secret));
555 }
556
557 params = generate_hop_params(ctx, sp->session_key->data, sp);
558 if (!params) {
559 tal_free(packet);
560 tal_free(secrets);
561 return NULL;
562 }
563 packet->version = 0;
564 memset(nexthmac.bytes, 0, sizeof(nexthmac.bytes));
565
566 /* BOLT #4:
567 *
568 * The packet is initialized with 1300 _random_ bytes derived from a
569 * CSPRNG
570 */
571 /* Note that this is just hop_payloads: the rest of the packet is
572 * overwritten below or above anyway. */
573 subkey_from_hmac("pad", sp->session_key, &padkey);
574 generate_cipher_stream(packet->routinginfo, &padkey, fixed_size);
575
576 filler = tal_arr(tmpctx, u8, fillerSize);
577 generate_header_padding(filler, tal_bytelen(filler), fixed_size, sp, params);
578
579 if (sp->rendezvous_id != NULL)
580 /* FIXME: Fuzz this or expose to the caller to hide encoded
581 * route length. */
582 sphinx_prefill(packet->routinginfo, sp, max_prefill, params,
583 fixed_size);

Callers 11

send_paymentFunction · 0.85
json_createonionFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85
inject_onionmessage_Function · 0.85
create_onionFunction · 0.85
create_onionFunction · 0.85
do_generateFunction · 0.85
runtestFunction · 0.85

Calls 12

sphinx_hop_sizeFunction · 0.85
tal_freeFunction · 0.85
generate_hop_paramsFunction · 0.85
generate_cipher_streamFunction · 0.85
generate_header_paddingFunction · 0.85
tal_bytelenFunction · 0.85
sphinx_prefillFunction · 0.85
sphinx_write_frameFunction · 0.85
xor_cipher_streamFunction · 0.85
compute_packet_hmacFunction · 0.85
subkey_from_hmacFunction · 0.70

Tested by 4

mainFunction · 0.68
mainFunction · 0.68
mainFunction · 0.68
mainFunction · 0.68