lib/crypto/chacha20poly1305.c
Source file repositories/reference/linux-study-clean/lib/crypto/chacha20poly1305.c
File Facts
- System
- Linux kernel
- Corpus path
lib/crypto/chacha20poly1305.c- Extension
.c- Size
- 9885 bytes
- Lines
- 362
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
crypto/chacha.hcrypto/chacha20poly1305.hcrypto/poly1305.hcrypto/utils.hlinux/export.hlinux/init.hlinux/kernel.hlinux/mm.hlinux/module.hlinux/unaligned.h
Detected Declarations
function Copyrightfunction xchacha_initfunction __chacha20poly1305_encryptfunction chacha20poly1305_encryptfunction xchacha20poly1305_encryptfunction __chacha20poly1305_decryptfunction chacha20poly1305_decryptfunction xchacha20poly1305_decryptfunction chacha20poly1305_crypt_sg_inplacefunction chacha20poly1305_encrypt_sg_inplacefunction chacha20poly1305_decrypt_sg_inplaceexport chacha20poly1305_encryptexport xchacha20poly1305_encryptexport chacha20poly1305_decryptexport xchacha20poly1305_decryptexport chacha20poly1305_encrypt_sg_inplaceexport chacha20poly1305_decrypt_sg_inplace
Annotated Snippet
if (unlikely(partial)) {
size_t l = min(length, CHACHA_BLOCK_SIZE - partial);
crypto_xor(addr, b.chacha_stream + partial, l);
partial = (partial + l) & (CHACHA_BLOCK_SIZE - 1);
addr += l;
length -= l;
}
if (likely(length >= CHACHA_BLOCK_SIZE || length == sl)) {
size_t l = length;
if (unlikely(length < sl))
l &= ~(CHACHA_BLOCK_SIZE - 1);
chacha20_crypt(&chacha_state, addr, addr, l);
addr += l;
length -= l;
}
if (unlikely(length > 0)) {
chacha20_crypt(&chacha_state, b.chacha_stream, pad0,
CHACHA_BLOCK_SIZE);
crypto_xor(addr, b.chacha_stream, length);
partial = length;
}
if (encrypt)
poly1305_update(&poly1305_state, miter.addr,
min_t(size_t, sl, miter.length));
}
if (src_len & 0xf)
poly1305_update(&poly1305_state, pad0, 0x10 - (src_len & 0xf));
b.lens[0] = cpu_to_le64(ad_len);
b.lens[1] = cpu_to_le64(src_len);
poly1305_update(&poly1305_state, (u8 *)b.lens, sizeof(b.lens));
if (likely(sl <= -POLY1305_DIGEST_SIZE)) {
if (encrypt) {
poly1305_final(&poly1305_state,
miter.addr + miter.length + sl);
ret = true;
} else {
poly1305_final(&poly1305_state, b.mac[0]);
ret = !crypto_memneq(b.mac[0],
miter.addr + miter.length + sl,
POLY1305_DIGEST_SIZE);
}
}
sg_miter_stop(&miter);
if (unlikely(sl > -POLY1305_DIGEST_SIZE)) {
poly1305_final(&poly1305_state, b.mac[1]);
sg_copy_buffer(src, sg_nents(src), b.mac[encrypt],
sizeof(b.mac[1]), src_len, !encrypt);
ret = encrypt ||
!crypto_memneq(b.mac[0], b.mac[1], POLY1305_DIGEST_SIZE);
}
chacha_zeroize_state(&chacha_state);
memzero_explicit(&b, sizeof(b));
return ret;
}
bool chacha20poly1305_encrypt_sg_inplace(struct scatterlist *src, size_t src_len,
const u8 *ad, const size_t ad_len,
const u64 nonce,
const u8 key[at_least CHACHA20POLY1305_KEY_SIZE])
{
return chacha20poly1305_crypt_sg_inplace(src, src_len, ad, ad_len,
nonce, key, 1);
}
EXPORT_SYMBOL(chacha20poly1305_encrypt_sg_inplace);
bool chacha20poly1305_decrypt_sg_inplace(struct scatterlist *src, size_t src_len,
const u8 *ad, const size_t ad_len,
const u64 nonce,
const u8 key[at_least CHACHA20POLY1305_KEY_SIZE])
{
if (unlikely(src_len < POLY1305_DIGEST_SIZE))
return false;
return chacha20poly1305_crypt_sg_inplace(src,
src_len - POLY1305_DIGEST_SIZE,
ad, ad_len, nonce, key, 0);
}
Annotation
- Immediate include surface: `crypto/chacha.h`, `crypto/chacha20poly1305.h`, `crypto/poly1305.h`, `crypto/utils.h`, `linux/export.h`, `linux/init.h`, `linux/kernel.h`, `linux/mm.h`.
- Detected declarations: `function Copyright`, `function xchacha_init`, `function __chacha20poly1305_encrypt`, `function chacha20poly1305_encrypt`, `function xchacha20poly1305_encrypt`, `function __chacha20poly1305_decrypt`, `function chacha20poly1305_decrypt`, `function xchacha20poly1305_decrypt`, `function chacha20poly1305_crypt_sg_inplace`, `function chacha20poly1305_encrypt_sg_inplace`.
- Atlas domain: Kernel Services / lib.
- Implementation status: integration implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.