6 * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
15 #include <linux/atomic.h>
16 #include <crypto/internal/rng.h>
17 #include <linux/err.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/random.h>
21 #include <linux/seq_file.h>
22 #include <linux/slab.h>
23 #include <linux/string.h>
24 #include <linux/cryptouser.h>
25 #include <net/netlink.h>
29 static DEFINE_MUTEX(crypto_default_rng_lock
);
30 struct crypto_rng
*crypto_default_rng
;
31 EXPORT_SYMBOL_GPL(crypto_default_rng
);
32 static int crypto_default_rng_refcnt
;
34 static inline struct crypto_rng
*__crypto_rng_cast(struct crypto_tfm
*tfm
)
36 return container_of(tfm
, struct crypto_rng
, base
);
39 static int generate(struct crypto_rng
*tfm
, const u8
*src
, unsigned int slen
,
40 u8
*dst
, unsigned int dlen
)
42 return crypto_rng_alg(tfm
)->rng_make_random(tfm
, dst
, dlen
);
45 static int rngapi_reset(struct crypto_rng
*tfm
, u8
*seed
, unsigned int slen
)
51 buf
= kmalloc(slen
, GFP_KERNEL
);
55 get_random_bytes(buf
, slen
);
59 err
= crypto_rng_alg(tfm
)->rng_reset(tfm
, seed
, slen
);
65 static int crypto_rng_init_tfm(struct crypto_tfm
*tfm
)
67 struct crypto_rng
*rng
= __crypto_rng_cast(tfm
);
69 rng
->generate
= generate
;
70 rng
->seed
= rngapi_reset
;
76 static int crypto_rng_report(struct sk_buff
*skb
, struct crypto_alg
*alg
)
78 struct crypto_report_rng rrng
;
80 strncpy(rrng
.type
, "rng", sizeof(rrng
.type
));
82 rrng
.seedsize
= alg
->cra_rng
.seedsize
;
84 if (nla_put(skb
, CRYPTOCFGA_REPORT_RNG
,
85 sizeof(struct crypto_report_rng
), &rrng
))
93 static int crypto_rng_report(struct sk_buff
*skb
, struct crypto_alg
*alg
)
99 static void crypto_rng_show(struct seq_file
*m
, struct crypto_alg
*alg
)
100 __attribute__ ((unused
));
101 static void crypto_rng_show(struct seq_file
*m
, struct crypto_alg
*alg
)
103 seq_printf(m
, "type : rng\n");
104 seq_printf(m
, "seedsize : %u\n", alg
->cra_rng
.seedsize
);
107 const struct crypto_type crypto_rng_type
= {
108 .extsize
= crypto_alg_extsize
,
109 .init_tfm
= crypto_rng_init_tfm
,
110 #ifdef CONFIG_PROC_FS
111 .show
= crypto_rng_show
,
113 .report
= crypto_rng_report
,
114 .maskclear
= ~CRYPTO_ALG_TYPE_MASK
,
115 .maskset
= CRYPTO_ALG_TYPE_MASK
,
116 .type
= CRYPTO_ALG_TYPE_RNG
,
117 .tfmsize
= offsetof(struct crypto_rng
, base
),
119 EXPORT_SYMBOL_GPL(crypto_rng_type
);
121 struct crypto_rng
*crypto_alloc_rng(const char *alg_name
, u32 type
, u32 mask
)
123 return crypto_alloc_tfm(alg_name
, &crypto_rng_type
, type
, mask
);
125 EXPORT_SYMBOL_GPL(crypto_alloc_rng
);
127 int crypto_get_default_rng(void)
129 struct crypto_rng
*rng
;
132 mutex_lock(&crypto_default_rng_lock
);
133 if (!crypto_default_rng
) {
134 rng
= crypto_alloc_rng("stdrng", 0, 0);
139 err
= crypto_rng_reset(rng
, NULL
, crypto_rng_seedsize(rng
));
141 crypto_free_rng(rng
);
145 crypto_default_rng
= rng
;
148 crypto_default_rng_refcnt
++;
152 mutex_unlock(&crypto_default_rng_lock
);
156 EXPORT_SYMBOL_GPL(crypto_get_default_rng
);
158 void crypto_put_default_rng(void)
160 mutex_lock(&crypto_default_rng_lock
);
161 if (!--crypto_default_rng_refcnt
) {
162 crypto_free_rng(crypto_default_rng
);
163 crypto_default_rng
= NULL
;
165 mutex_unlock(&crypto_default_rng_lock
);
167 EXPORT_SYMBOL_GPL(crypto_put_default_rng
);
169 MODULE_LICENSE("GPL");
170 MODULE_DESCRIPTION("Random Number Generator");