| 121 | } |
| 122 | |
| 123 | bool cryptomsg_decrypt_header(struct crypto_state *cs, const u8 hdr[18], |
| 124 | u16 *lenp) |
| 125 | { |
| 126 | unsigned char npub[crypto_aead_chacha20poly1305_ietf_NPUBBYTES]; |
| 127 | unsigned long long mlen; |
| 128 | be16 len; |
| 129 | |
| 130 | le64_nonce(npub, cs->rn++); |
| 131 | |
| 132 | /* BOLT #8: |
| 133 | * |
| 134 | * 2. Let the encrypted length prefix be known as `lc`. |
| 135 | * 3. Decrypt `lc` (using `ChaCha20-Poly1305`, `rn`, and `rk`), to |
| 136 | * obtain the size of the encrypted packet `l`. |
| 137 | * * A zero-length byte slice is to be passed as the AD |
| 138 | * (associated data). |
| 139 | * * The nonce `rn` MUST be incremented after this step. |
| 140 | */ |
| 141 | if (crypto_aead_chacha20poly1305_ietf_decrypt((unsigned char *)&len, |
| 142 | &mlen, NULL, |
| 143 | memcheck(hdr, 18), 18, |
| 144 | NULL, 0, |
| 145 | npub, cs->rk.data) != 0) { |
| 146 | /* FIXME: Report error! */ |
| 147 | return false; |
| 148 | } |
| 149 | assert(mlen == sizeof(len)); |
| 150 | *lenp = be16_to_cpu(len); |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | u8 *cryptomsg_encrypt_msg(const tal_t *ctx, |
| 155 | struct crypto_state *cs, |