crypto/rng.c
Source file repositories/reference/linux-study-clean/crypto/rng.c
File Facts
- System
- Linux kernel
- Corpus path
crypto/rng.c- Extension
.c- Size
- 5064 bytes
- Lines
- 246
- Domain
- Kernel Services
- Bucket
- crypto
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
crypto/internal/rng.hlinux/atomic.hlinux/cryptouser.hlinux/err.hlinux/kernel.hlinux/module.hlinux/mutex.hlinux/random.hlinux/seq_file.hlinux/slab.hlinux/string.hnet/netlink.hinternal.h
Detected Declarations
function crypto_rng_resetfunction crypto_rng_init_tfmfunction seedsizefunction crypto_rng_reportfunction crypto_rng_showfunction crypto_get_default_rngfunction crypto_put_default_rngfunction __crypto_stdrng_get_bytesfunction crypto_del_default_rngfunction rng_default_set_entfunction crypto_unregister_rngfunction crypto_register_rngsfunction crypto_unregister_rngsfunction rng_exitexport crypto_rng_resetexport crypto_alloc_rngexport __crypto_stdrng_get_bytesexport crypto_del_default_rngexport crypto_register_rngexport crypto_unregister_rngexport crypto_register_rngsexport crypto_unregister_rngs
Annotated Snippet
if (err) {
crypto_free_rng(rng);
goto unlock;
}
crypto_default_rng = rng;
}
crypto_default_rng_refcnt++;
err = 0;
unlock:
mutex_unlock(&crypto_default_rng_lock);
return err;
}
static void crypto_put_default_rng(void)
{
mutex_lock(&crypto_default_rng_lock);
crypto_default_rng_refcnt--;
mutex_unlock(&crypto_default_rng_lock);
}
int __crypto_stdrng_get_bytes(void *buf, unsigned int len)
{
int err;
err = crypto_get_default_rng();
if (err)
return err;
err = crypto_rng_get_bytes(crypto_default_rng, buf, len);
crypto_put_default_rng();
return err;
}
EXPORT_SYMBOL_GPL(__crypto_stdrng_get_bytes);
#if defined(CONFIG_CRYPTO_RNG) || defined(CONFIG_CRYPTO_RNG_MODULE)
int crypto_del_default_rng(void)
{
int err = -EBUSY;
mutex_lock(&crypto_default_rng_lock);
if (crypto_default_rng_refcnt)
goto out;
crypto_free_rng(crypto_default_rng);
crypto_default_rng = NULL;
err = 0;
out:
mutex_unlock(&crypto_default_rng_lock);
return err;
}
EXPORT_SYMBOL_GPL(crypto_del_default_rng);
#endif
static void rng_default_set_ent(struct crypto_rng *tfm, const u8 *data,
unsigned int len)
{
}
int crypto_register_rng(struct rng_alg *alg)
{
struct crypto_alg *base = &alg->base;
if (alg->seedsize > PAGE_SIZE / 8)
return -EINVAL;
base->cra_type = &crypto_rng_type;
base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
base->cra_flags |= CRYPTO_ALG_TYPE_RNG;
if (!alg->set_ent)
alg->set_ent = rng_default_set_ent;
return crypto_register_alg(base);
}
EXPORT_SYMBOL_GPL(crypto_register_rng);
void crypto_unregister_rng(struct rng_alg *alg)
{
crypto_unregister_alg(&alg->base);
}
EXPORT_SYMBOL_GPL(crypto_unregister_rng);
int crypto_register_rngs(struct rng_alg *algs, int count)
Annotation
- Immediate include surface: `crypto/internal/rng.h`, `linux/atomic.h`, `linux/cryptouser.h`, `linux/err.h`, `linux/kernel.h`, `linux/module.h`, `linux/mutex.h`, `linux/random.h`.
- Detected declarations: `function crypto_rng_reset`, `function crypto_rng_init_tfm`, `function seedsize`, `function crypto_rng_report`, `function crypto_rng_show`, `function crypto_get_default_rng`, `function crypto_put_default_rng`, `function __crypto_stdrng_get_bytes`, `function crypto_del_default_rng`, `function rng_default_set_ent`.
- Atlas domain: Kernel Services / crypto.
- Implementation status: integration implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.