BOLT #8: * * `encryptWithAD(k, n, ad, plaintext)`: outputs `encrypt(k, n, ad, * plaintext)` * * Where `encrypt` is an evaluation of `ChaCha20-Poly1305` (IETF * variant) with the passed arguments, with nonce `n` */
| 270 | * variant) with the passed arguments, with nonce `n` |
| 271 | */ |
| 272 | static void encrypt_ad(const struct secret *k, u64 nonce, |
| 273 | const void *additional_data, size_t additional_data_len, |
| 274 | const void *plaintext, size_t plaintext_len, |
| 275 | void *output, size_t outputlen) |
| 276 | { |
| 277 | unsigned char npub[crypto_aead_chacha20poly1305_ietf_NPUBBYTES]; |
| 278 | unsigned long long clen; |
| 279 | int ret; |
| 280 | |
| 281 | assert(outputlen == plaintext_len + crypto_aead_chacha20poly1305_ietf_ABYTES); |
| 282 | le64_nonce(npub, nonce); |
| 283 | BUILD_ASSERT(sizeof(*k) == crypto_aead_chacha20poly1305_ietf_KEYBYTES); |
| 284 | SUPERVERBOSE("# encryptWithAD(0x%s, 0x%s, 0x%s, %s%s)", |
| 285 | tal_hexstr(tmpctx, k, sizeof(*k)), |
| 286 | tal_hexstr(tmpctx, npub, sizeof(npub)), |
| 287 | tal_hexstr(tmpctx, additional_data, additional_data_len), |
| 288 | plaintext_len ? "0x" : "<empty>", |
| 289 | tal_hexstr(tmpctx, plaintext, plaintext_len)); |
| 290 | |
| 291 | ret = crypto_aead_chacha20poly1305_ietf_encrypt(output, &clen, |
| 292 | memcheck(plaintext, plaintext_len), |
| 293 | plaintext_len, |
| 294 | additional_data, additional_data_len, |
| 295 | NULL, npub, k->data); |
| 296 | assert(ret == 0); |
| 297 | assert(clen == plaintext_len + crypto_aead_chacha20poly1305_ietf_ABYTES); |
| 298 | } |
| 299 | |
| 300 | /* BOLT #8: |
| 301 | * * `decryptWithAD(k, n, ad, ciphertext)`: outputs `decrypt(k, n, ad, |
no test coverage detected