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

Function cryptomsg_decrypt_body

common/cryptomsg.c:88–121  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

86}
87
88u8 *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
123bool cryptomsg_decrypt_header(struct crypto_state *cs, const u8 hdr[18],
124 u16 *lenp)

Callers 6

read_body_from_peer_doneFunction · 0.85
peer_init_receivedFunction · 0.85
test_decrypt_bodyFunction · 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 3

test_decrypt_bodyFunction · 0.68
mainFunction · 0.68