| 86 | } |
| 87 | |
| 88 | u8 *cryptomsg_decrypt_body(const tal_t *ctx, |
| 89 | struct crypto_state *cs, const u8 *in) |
| 90 | { |
| 91 | unsigned char npub[crypto_aead_chacha20poly1305_ietf_NPUBBYTES]; |
| 92 | unsigned long long mlen; |
| 93 | size_t inlen = tal_count(in); |
| 94 | u8 *decrypted; |
| 95 | |
| 96 | if (inlen < 16) |
| 97 | return NULL; |
| 98 | decrypted = tal_arr(ctx, u8, inlen - 16); |
| 99 | |
| 100 | le64_nonce(npub, cs->rn++); |
| 101 | |
| 102 | /* BOLT #8: |
| 103 | * |
| 104 | * 5. Decrypt `c` (using `ChaCha20-Poly1305`, `rn`, and `rk`), to |
| 105 | * obtain decrypted plaintext packet `p`. |
| 106 | * * The nonce `rn` MUST be incremented after this step. |
| 107 | */ |
| 108 | if (crypto_aead_chacha20poly1305_ietf_decrypt(decrypted, |
| 109 | &mlen, NULL, |
| 110 | memcheck(in, inlen), |
| 111 | inlen, |
| 112 | NULL, 0, |
| 113 | npub, cs->rk.data) != 0) { |
| 114 | /* FIXME: Report error! */ |
| 115 | return tal_free(decrypted); |
| 116 | } |
| 117 | assert(mlen == tal_count(decrypted)); |
| 118 | |
| 119 | maybe_rotate_key(&cs->rn, &cs->rk, &cs->r_ck); |
| 120 | return decrypted; |
| 121 | } |
| 122 | |
| 123 | bool cryptomsg_decrypt_header(struct crypto_state *cs, const u8 hdr[18], |
| 124 | u16 *lenp) |