123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- #include <linux/module.h>
- #include <linux/slab.h>
- #include <linux/module.h>
- #include <linux/fips.h>
- #include <linux/time.h>
- #include <linux/crypto.h>
- #include <crypto/internal/rng.h>
- struct rand_data;
- int jent_read_entropy(struct rand_data *ec, unsigned char *data,
- unsigned int len);
- int jent_entropy_init(void);
- struct rand_data *jent_entropy_collector_alloc(unsigned int osr,
- unsigned int flags);
- void jent_entropy_collector_free(struct rand_data *entropy_collector);
- __u64 jent_rol64(__u64 word, unsigned int shift)
- {
- return rol64(word, shift);
- }
- void *jent_zalloc(unsigned int len)
- {
- return kzalloc(len, GFP_KERNEL);
- }
- void jent_zfree(void *ptr)
- {
- kzfree(ptr);
- }
- int jent_fips_enabled(void)
- {
- return fips_enabled;
- }
- void jent_panic(char *s)
- {
- panic("%s", s);
- }
- void jent_memcpy(void *dest, const void *src, unsigned int n)
- {
- memcpy(dest, src, n);
- }
- void jent_get_nstime(__u64 *out)
- {
- __u64 tmp = 0;
- tmp = random_get_entropy();
-
- if (tmp == 0)
- tmp = ktime_get_ns();
- *out = tmp;
- }
- struct jitterentropy {
- spinlock_t jent_lock;
- struct rand_data *entropy_collector;
- };
- static int jent_kcapi_init(struct crypto_tfm *tfm)
- {
- struct jitterentropy *rng = crypto_tfm_ctx(tfm);
- int ret = 0;
- rng->entropy_collector = jent_entropy_collector_alloc(1, 0);
- if (!rng->entropy_collector)
- ret = -ENOMEM;
- spin_lock_init(&rng->jent_lock);
- return ret;
- }
- static void jent_kcapi_cleanup(struct crypto_tfm *tfm)
- {
- struct jitterentropy *rng = crypto_tfm_ctx(tfm);
- spin_lock(&rng->jent_lock);
- if (rng->entropy_collector)
- jent_entropy_collector_free(rng->entropy_collector);
- rng->entropy_collector = NULL;
- spin_unlock(&rng->jent_lock);
- }
- static int jent_kcapi_random(struct crypto_rng *tfm,
- const u8 *src, unsigned int slen,
- u8 *rdata, unsigned int dlen)
- {
- struct jitterentropy *rng = crypto_rng_ctx(tfm);
- int ret = 0;
- spin_lock(&rng->jent_lock);
- ret = jent_read_entropy(rng->entropy_collector, rdata, dlen);
- spin_unlock(&rng->jent_lock);
- return ret;
- }
- static int jent_kcapi_reset(struct crypto_rng *tfm,
- const u8 *seed, unsigned int slen)
- {
- return 0;
- }
- static struct rng_alg jent_alg = {
- .generate = jent_kcapi_random,
- .seed = jent_kcapi_reset,
- .seedsize = 0,
- .base = {
- .cra_name = "jitterentropy_rng",
- .cra_driver_name = "jitterentropy_rng",
- .cra_priority = 100,
- .cra_ctxsize = sizeof(struct jitterentropy),
- .cra_module = THIS_MODULE,
- .cra_init = jent_kcapi_init,
- .cra_exit = jent_kcapi_cleanup,
- }
- };
- static int __init jent_mod_init(void)
- {
- int ret = 0;
- ret = jent_entropy_init();
- if (ret) {
- pr_info("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
- return -EFAULT;
- }
- return crypto_register_rng(&jent_alg);
- }
- static void __exit jent_mod_exit(void)
- {
- crypto_unregister_rng(&jent_alg);
- }
- module_init(jent_mod_init);
- module_exit(jent_mod_exit);
- MODULE_LICENSE("Dual BSD/GPL");
- MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
- MODULE_DESCRIPTION("Non-physical True Random Number Generator based on CPU Jitter");
- MODULE_ALIAS_CRYPTO("jitterentropy_rng");
|