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

Function process_onion

common/sphinx.c:646–745  ·  view source on GitHub ↗

* Given an onionpacket msg extract the information for the current * node and unwrap the remainder so that the node can forward it. */

Source from the content-addressed store, hash-verified

644 * node and unwrap the remainder so that the node can forward it.
645 */
646static struct route_step *process_onion(const tal_t *ctx,
647 const struct onionpacket *msg,
648 const struct secret *shared_secret,
649 const u8 *assocdata,
650 const size_t assocdatalen,
651 bool is_onion_message)
652{
653 struct route_step *step = talz(ctx, struct route_step);
654 struct hmac hmac;
655 struct secret mu, rho;
656 u8 blind[BLINDING_FACTOR_SIZE];
657 u8 *paddedheader;
658 size_t payload_size;
659 bigsize_t shift_size;
660 const u8 *cursor;
661 size_t max;
662
663 step->next = talz(step, struct onionpacket);
664 step->next->version = msg->version;
665
666 /* BOLT #4:
667 * - Derive `mu` as $`HMAC256(\text{"mu"}, ss)`$
668 * (see [Key Generation](#key-generation)).
669 * - Derive the HMAC as $`HMAC256(mu, hop\_payloads || associated\_data)`$.
670 */
671 subkey_from_hmac("mu", shared_secret, &mu);
672 compute_packet_hmac(msg, assocdata, assocdatalen, &mu, &hmac);
673
674 if (!hmac_eq(&msg->hmac, &hmac) || dev_fail_process_onionpacket) {
675 /* Computed MAC does not match expected MAC, the message was modified. */
676 return tal_free(step);
677 }
678
679 /* BOLT #4:
680 * - Derive `rho` as $`HMAC256(\text{"rho"}, ss)`$
681 * (see [Key Generation](#key-generation)).
682 * - Derive `bytestream` of twice the length of `hop_payloads` using `rho`
683 * (see [Pseudo Random Byte Stream](pseudo-random-byte-stream)).
684 * - Set `unwrapped_payloads` to the XOR of `hop_payloads` and `bytestream`.
685 */
686 subkey_from_hmac("rho", shared_secret, &rho);
687
688 //FIXME:store seen secrets to avoid replay attacks
689 paddedheader = tal_arrz(step, u8, tal_bytelen(msg->routinginfo)*2);
690 memcpy(paddedheader, msg->routinginfo, tal_bytelen(msg->routinginfo));
691 xor_cipher_stream(paddedheader, &rho, tal_bytelen(paddedheader));
692
693 compute_blinding_factor(&msg->ephemeralkey, shared_secret, blind);
694 if (!blind_group_element(&step->next->ephemeralkey, &msg->ephemeralkey, blind))
695 return tal_free(step);
696
697 /* Now, try to pull data out. */
698 cursor = paddedheader;
699 max = tal_bytelen(msg->routinginfo);
700
701 /* Any of these could fail, falling thru with cursor == NULL */
702 payload_size = fromwire_bigsize(&cursor, &max);
703

Callers 2

process_onionpacketFunction · 0.85
process_onionmsg_packetFunction · 0.85

Calls 11

compute_packet_hmacFunction · 0.85
tal_freeFunction · 0.85
tal_bytelenFunction · 0.85
xor_cipher_streamFunction · 0.85
compute_blinding_factorFunction · 0.85
memeqzeroFunction · 0.85
subkey_from_hmacFunction · 0.70
blind_group_elementFunction · 0.70
fromwire_bigsizeFunction · 0.70
fromwire_hmacFunction · 0.70
fromwire_padFunction · 0.50

Tested by

no test coverage detected