| 766 | unsigned dev_onion_reply_length = 256; |
| 767 | |
| 768 | struct onionreply *create_onionreply(const tal_t *ctx, |
| 769 | const struct secret *shared_secret, |
| 770 | const u8 *failure_msg) |
| 771 | { |
| 772 | size_t msglen = tal_count(failure_msg); |
| 773 | size_t padlen; |
| 774 | struct onionreply *reply = tal(ctx, struct onionreply); |
| 775 | u8 *payload = tal_arr(ctx, u8, 0); |
| 776 | struct secret key; |
| 777 | struct hmac hmac; |
| 778 | |
| 779 | /* BOLT #4: |
| 780 | * The _erring node_: |
| 781 | * - MUST set `pad` such that the `failure_len` plus `pad_len` |
| 782 | * is at least 256. |
| 783 | * - SHOULD set `pad` such that the `failure_len` plus `pad_len` is equal |
| 784 | * to 256. Deviating from this may cause older nodes to be unable to parse |
| 785 | * the return message. |
| 786 | */ |
| 787 | const u16 onion_reply_size = dev_onion_reply_length; |
| 788 | |
| 789 | /* We never do this currently, but could in future! */ |
| 790 | if (msglen > onion_reply_size) |
| 791 | padlen = 0; |
| 792 | else |
| 793 | padlen = onion_reply_size - msglen; |
| 794 | |
| 795 | /* BOLT #4: |
| 796 | * |
| 797 | * The node generating the error message builds a _return |
| 798 | * packet_ consisting of the following fields: |
| 799 | * |
| 800 | * 1. data: |
| 801 | * * [`32*byte`:`hmac`] |
| 802 | * * [`u16`:`failure_len`] |
| 803 | * * [`failure_len*byte`:`failuremsg`] |
| 804 | * * [`u16`:`pad_len`] |
| 805 | * * [`pad_len*byte`:`pad`] |
| 806 | */ |
| 807 | towire_u16(&payload, msglen); |
| 808 | towire(&payload, failure_msg, msglen); |
| 809 | towire_u16(&payload, padlen); |
| 810 | towire_pad(&payload, padlen); |
| 811 | |
| 812 | /* Two bytes for each length: failure_len and pad_len */ |
| 813 | assert(tal_count(payload) == onion_reply_size + 4); |
| 814 | |
| 815 | /* BOLT #4: |
| 816 | * |
| 817 | * Where `hmac` is an HMAC authenticating the remainder of the packet, |
| 818 | * with a key generated using the above process, with key type `um` |
| 819 | */ |
| 820 | subkey_from_hmac("um", shared_secret, &key); |
| 821 | |
| 822 | compute_hmac(&key, payload, tal_count(payload), NULL, 0, &hmac); |
| 823 | reply->contents = tal_arr(reply, u8, 0), |
| 824 | towire_hmac(&reply->contents, &hmac); |
| 825 | |