| 52 | } |
| 53 | |
| 54 | static u8 *enctlv_from_encmsg_raw(const tal_t *ctx, |
| 55 | const struct privkey *path_privkey, |
| 56 | const struct pubkey *node, |
| 57 | const u8 *raw_encmsg TAKES, |
| 58 | struct privkey *next_path_privkey, |
| 59 | struct pubkey *node_alias) |
| 60 | { |
| 61 | struct secret ss, rho; |
| 62 | u8 *ret; |
| 63 | int ok; |
| 64 | /* All-zero npub */ |
| 65 | static const unsigned char npub[crypto_aead_chacha20poly1305_ietf_NPUBBYTES]; |
| 66 | |
| 67 | /* BOLT #4: |
| 68 | * - $`ss_i = SHA256(e_i * N_i) = SHA256(k_i * E_i)`$ |
| 69 | * (ECDH shared secret known only by $`N_r`$ and $`N_i`$) |
| 70 | */ |
| 71 | if (secp256k1_ecdh(secp256k1_ctx, ss.data, |
| 72 | &node->pubkey, path_privkey->secret.data, |
| 73 | NULL, NULL) != 1) |
| 74 | return NULL; |
| 75 | SUPERVERBOSE("\t\"ss\": \"%s\",\n", |
| 76 | fmt_secret(tmpctx, &ss)); |
| 77 | |
| 78 | /* This calculates the node's alias, and next path_key */ |
| 79 | if (!blind_node(path_privkey, &ss, node, node_alias, next_path_privkey)) |
| 80 | return NULL; |
| 81 | |
| 82 | ret = tal_dup_talarr(ctx, u8, raw_encmsg); |
| 83 | |
| 84 | /* BOLT #4: |
| 85 | * - $`rho_i = HMAC256(\text{"rho"}, ss_i)`$ |
| 86 | * (key used to encrypt `encrypted_recipient_data` for $`N_i`$ by $`N_r`$) |
| 87 | */ |
| 88 | subkey_from_hmac("rho", &ss, &rho); |
| 89 | SUPERVERBOSE("\t\"rho\": \"%s\",\n", |
| 90 | fmt_secret(tmpctx, &rho)); |
| 91 | |
| 92 | /* BOLT #4: |
| 93 | * - MUST encrypt each `encrypted_data_tlv[i]` with ChaCha20-Poly1305 using |
| 94 | * the corresponding $`rho_i`$ key and an all-zero nonce to produce |
| 95 | * `encrypted_recipient_data[i]` |
| 96 | */ |
| 97 | /* Encrypt in place */ |
| 98 | towire_pad(&ret, crypto_aead_chacha20poly1305_ietf_ABYTES); |
| 99 | ok = crypto_aead_chacha20poly1305_ietf_encrypt(ret, NULL, |
| 100 | ret, |
| 101 | tal_bytelen(ret) |
| 102 | - crypto_aead_chacha20poly1305_ietf_ABYTES, |
| 103 | NULL, 0, |
| 104 | NULL, npub, |
| 105 | rho.data); |
| 106 | assert(ok == 0); |
| 107 | |
| 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | u8 *encrypt_tlv_encrypted_data(const tal_t *ctx, |