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

Function create_onionpacket

common/sphinx.c:504–592  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

502}
503
504struct onionpacket *create_onionpacket(
505 const tal_t *ctx,
506 struct sphinx_path *sp,
507 size_t fixed_size,
508 struct secret **path_secrets
509 )
510{
511 struct onionpacket *packet = talz(ctx, struct onionpacket);
512 int i, num_hops = tal_count(sp->hops);
513 size_t fillerSize = sphinx_path_payloads_size(sp) -
514 sphinx_hop_size(&sp->hops[num_hops - 1]);
515 u8 *filler;
516 struct keyset keys;
517 struct secret padkey;
518 struct hmac nexthmac;
519 struct hop_params *params;
520 struct secret *secrets = tal_arr(ctx, struct secret, num_hops);
521 size_t payloads_size = sphinx_path_payloads_size(sp);
522 size_t max_prefill = fixed_size - payloads_size;
523
524 if (sphinx_path_payloads_size(sp) > fixed_size) {
525 tal_free(packet);
526 tal_free(secrets);
527 return NULL;
528 }
529 packet->routinginfo = tal_arr(packet, u8, fixed_size);
530
531 if (sp->session_key == NULL) {
532 sp->session_key = tal(sp, struct secret);
533 randombytes_buf(sp->session_key, sizeof(struct secret));
534 }
535
536 params = generate_hop_params(ctx, sp->session_key->data, sp);
537 if (!params) {
538 tal_free(packet);
539 tal_free(secrets);
540 return NULL;
541 }
542 packet->version = 0;
543 memset(nexthmac.bytes, 0, sizeof(nexthmac.bytes));
544
545 /* BOLT #4:
546 *
547 * The packet is initialized with 1300 _random_ bytes derived from a
548 * CSPRNG
549 */
550 /* Note that this is just hop_payloads: the rest of the packet is
551 * overwritten below or above anyway. */
552 subkey_from_hmac("pad", sp->session_key, &padkey);
553 generate_cipher_stream(packet->routinginfo, &padkey, fixed_size);
554
555 filler = tal_arr(tmpctx, u8, fillerSize);
556 generate_header_padding(filler, tal_bytelen(filler), fixed_size, sp, params);
557
558 if (sp->rendezvous_id != NULL)
559 /* FIXME: Fuzz this or expose to the caller to hide encoded
560 * route length. */
561 sphinx_prefill(packet->routinginfo, sp, max_prefill, params,

Callers 7

send_paymentFunction · 0.85
json_createonionFunction · 0.85
json_sendonionmessageFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85
do_generateFunction · 0.85
runtestFunction · 0.85

Calls 13

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
generate_key_setFunction · 0.85
sphinx_write_frameFunction · 0.85
xor_cipher_streamFunction · 0.85
compute_packet_hmacFunction · 0.85

Tested by 2

mainFunction · 0.68
mainFunction · 0.68