| 849 | } |
| 850 | |
| 851 | u8 *unwrap_onionreply(const tal_t *ctx, |
| 852 | const struct secret *shared_secrets, |
| 853 | const int numhops, |
| 854 | const struct onionreply *reply, |
| 855 | int *origin_index) |
| 856 | { |
| 857 | struct onionreply *r; |
| 858 | const u8 *cursor; |
| 859 | size_t max; |
| 860 | u8 *ret; |
| 861 | |
| 862 | r = new_onionreply(tmpctx, reply->contents); |
| 863 | *origin_index = -1; |
| 864 | ret = NULL; |
| 865 | |
| 866 | /* BOLT #4: |
| 867 | * The _origin node_: |
| 868 | * - once the return message has been decrypted: |
| 869 | * - SHOULD store a copy of the message. |
| 870 | * - SHOULD continue decrypting, until the loop has been repeated 27 times |
| 871 | * (maximum route length of tlv payload type). |
| 872 | * - SHOULD use constant `ammag` and `um` keys to obfuscate the route length. |
| 873 | *... |
| 874 | * ### Rationale |
| 875 | * |
| 876 | * The requirements for the _origin node_ should help hide the payment sender. |
| 877 | * By continuing decrypting 27 times (dummy decryption cycles after the error is found) |
| 878 | * the erroring node cannot learn its relative position in the route by performing |
| 879 | * a timing analysis if the sender were to retry the same route multiple times. |
| 880 | */ |
| 881 | for (size_t i = 0; i < 27; i++) { |
| 882 | struct secret ss, key; |
| 883 | struct hmac hmac, expected_hmac; |
| 884 | |
| 885 | if (i < numhops) |
| 886 | ss = shared_secrets[i]; |
| 887 | else |
| 888 | memset(&ss, 0x7, sizeof(ss)); |
| 889 | |
| 890 | /* Since the encryption is just XORing with the cipher |
| 891 | * stream encryption is identical to decryption */ |
| 892 | r = wrap_onionreply(tmpctx, &ss, r); |
| 893 | |
| 894 | /* Check if the HMAC matches, this means that this is |
| 895 | * the origin */ |
| 896 | subkey_from_hmac("um", &ss, &key); |
| 897 | |
| 898 | cursor = r->contents; |
| 899 | max = tal_count(r->contents); |
| 900 | |
| 901 | fromwire_hmac(&cursor, &max, &hmac); |
| 902 | /* Too short. */ |
| 903 | if (!cursor) |
| 904 | return NULL; |
| 905 | |
| 906 | compute_hmac(&key, cursor, max, NULL, 0, &expected_hmac); |
| 907 | if (hmac_eq(&hmac, &expected_hmac)) { |
| 908 | u16 msglen; |