19 #include <linux/module.h>
21 #include <linux/string.h>
25 #define DEFAULT_PRNG_KEY "0123456789abcdef"
26 #define DEFAULT_PRNG_KSZ 16
27 #define DEFAULT_BLK_SZ 16
28 #define DEFAULT_V_SEED "zaybxcwdveuftgsh"
34 #define PRNG_FIXED_SIZE 0x1
35 #define PRNG_NEED_RESET 0x2
60 static void hexdump(
char *note,
unsigned char *
buf,
unsigned int len)
70 #define dbgprint(format, args...) do {\
72 printk(format, ##args);\
75 static void xor_vectors(
unsigned char *
in1,
unsigned char *
in2,
76 unsigned char *
out,
unsigned int size)
80 for (i = 0; i <
size; i++)
81 out[i] = in1[i] ^ in2[i];
88 static int _get_more_prng_bytes(
struct prng_context *
ctx,
int cont_test)
105 for (i = 0; i < 3; i++) {
136 panic(
"cprng %p Failed repetition check!\n",
141 "ctx %p Failed repetition check!\n",
163 crypto_cipher_encrypt_one(ctx->
tfm, output, tmp);
176 dbgprint(
"Returning new block for context %p\n", ctx);
192 unsigned int byte_count = (
unsigned int)nbytes;
221 if (_get_more_prng_bytes(ctx, do_cont_test) < 0) {
248 if (_get_more_prng_bytes(ctx, do_cont_test) < 0) {
276 crypto_free_cipher(ctx->
tfm);
280 unsigned char *
key,
size_t klen,
281 unsigned char *
V,
unsigned char *
DT)
284 unsigned char *prng_key;
309 ret = crypto_cipher_setkey(ctx->
tfm, prng_key, klen);
312 crypto_cipher_get_flags(ctx->
tfm));
328 ctx->
tfm = crypto_alloc_cipher(
"aes", 0, 0);
329 if (IS_ERR(ctx->
tfm)) {
332 return PTR_ERR(ctx->
tfm);
347 static void cprng_exit(
struct crypto_tfm *tfm)
349 free_prng_context(crypto_tfm_ctx(tfm));
352 static int cprng_get_random(
struct crypto_rng *tfm,
u8 *rdata,
357 return get_prng_bytes(rdata, dlen, prng, 0);
366 static int cprng_reset(
struct crypto_rng *tfm,
u8 *seed,
unsigned int slen)
378 reset_prng_context(prng, key, DEFAULT_PRNG_KSZ, seed, dt);
385 #ifdef CONFIG_CRYPTO_FIPS
386 static int fips_cprng_get_random(
struct crypto_rng *tfm,
u8 *rdata,
391 return get_prng_bytes(rdata, dlen, prng, 1);
394 static int fips_cprng_reset(
struct crypto_rng *tfm,
u8 *seed,
unsigned int slen)
402 if (slen < DEFAULT_PRNG_KSZ + DEFAULT_BLK_SZ)
406 if (!
memcmp(seed, key, DEFAULT_PRNG_KSZ))
409 rc = cprng_reset(tfm, seed, slen);
415 rc = get_prng_bytes(rdata, DEFAULT_BLK_SZ, prng, 0);
424 .cra_name =
"stdrng",
425 .cra_driver_name =
"ansi_cprng",
431 .cra_init = cprng_init,
432 .cra_exit = cprng_exit,
435 .rng_make_random = cprng_get_random,
436 .rng_reset = cprng_reset,
440 #ifdef CONFIG_CRYPTO_FIPS
442 .cra_name =
"fips(ansi_cprng)",
443 .cra_driver_name =
"fips_ansi_cprng",
449 .cra_init = cprng_init,
450 .cra_exit = cprng_exit,
453 .rng_make_random = fips_cprng_get_random,
454 .rng_reset = fips_cprng_reset,
462 static int __init prng_mod_init(
void)
467 static void __exit prng_mod_fini(
void)