MCPcopy Create free account
hub / github.com/ElementsProject/lightning / cryptomsg_decrypt_body

Function cryptomsg_decrypt_body

common/cryptomsg.c:86–119  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 4

read_body_from_peer_doneFunction · 0.85
peer_init_receivedFunction · 0.85
mainFunction · 0.85
sync_crypto_readFunction · 0.85

Calls 3

tal_freeFunction · 0.85
maybe_rotate_keyFunction · 0.85
le64_nonceFunction · 0.70

Tested by 1

mainFunction · 0.68