1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * algif_rng: User-space interface for random number generators
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * This file provides the user-space API for random number generators.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Copyright (C) 2014, Stephan Mueller <smueller@chronox.de>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without
9*4882a593Smuzhiyun * modification, are permitted provided that the following conditions
10*4882a593Smuzhiyun * are met:
11*4882a593Smuzhiyun * 1. Redistributions of source code must retain the above copyright
12*4882a593Smuzhiyun * notice, and the entire permission notice in its entirety,
13*4882a593Smuzhiyun * including the disclaimer of warranties.
14*4882a593Smuzhiyun * 2. Redistributions in binary form must reproduce the above copyright
15*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer in the
16*4882a593Smuzhiyun * documentation and/or other materials provided with the distribution.
17*4882a593Smuzhiyun * 3. The name of the author may not be used to endorse or promote
18*4882a593Smuzhiyun * products derived from this software without specific prior
19*4882a593Smuzhiyun * written permission.
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * ALTERNATIVELY, this product may be distributed under the terms of
22*4882a593Smuzhiyun * the GNU General Public License, in which case the provisions of the GPL2
23*4882a593Smuzhiyun * are required INSTEAD OF the above restrictions. (This clause is
24*4882a593Smuzhiyun * necessary due to a potential bad interaction between the GPL and
25*4882a593Smuzhiyun * the restrictions contained in a BSD-style copyright.)
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
28*4882a593Smuzhiyun * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29*4882a593Smuzhiyun * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
30*4882a593Smuzhiyun * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
31*4882a593Smuzhiyun * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32*4882a593Smuzhiyun * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
33*4882a593Smuzhiyun * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
34*4882a593Smuzhiyun * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35*4882a593Smuzhiyun * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36*4882a593Smuzhiyun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37*4882a593Smuzhiyun * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
38*4882a593Smuzhiyun * DAMAGE.
39*4882a593Smuzhiyun */
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun #include <linux/capability.h>
42*4882a593Smuzhiyun #include <linux/module.h>
43*4882a593Smuzhiyun #include <crypto/rng.h>
44*4882a593Smuzhiyun #include <linux/random.h>
45*4882a593Smuzhiyun #include <crypto/if_alg.h>
46*4882a593Smuzhiyun #include <linux/net.h>
47*4882a593Smuzhiyun #include <net/sock.h>
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun MODULE_LICENSE("GPL");
50*4882a593Smuzhiyun MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
51*4882a593Smuzhiyun MODULE_DESCRIPTION("User-space interface for random number generators");
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun struct rng_ctx {
54*4882a593Smuzhiyun #define MAXSIZE 128
55*4882a593Smuzhiyun unsigned int len;
56*4882a593Smuzhiyun struct crypto_rng *drng;
57*4882a593Smuzhiyun u8 *addtl;
58*4882a593Smuzhiyun size_t addtl_len;
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun struct rng_parent_ctx {
62*4882a593Smuzhiyun struct crypto_rng *drng;
63*4882a593Smuzhiyun u8 *entropy;
64*4882a593Smuzhiyun };
65*4882a593Smuzhiyun
rng_reset_addtl(struct rng_ctx * ctx)66*4882a593Smuzhiyun static void rng_reset_addtl(struct rng_ctx *ctx)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun kfree_sensitive(ctx->addtl);
69*4882a593Smuzhiyun ctx->addtl = NULL;
70*4882a593Smuzhiyun ctx->addtl_len = 0;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
_rng_recvmsg(struct crypto_rng * drng,struct msghdr * msg,size_t len,u8 * addtl,size_t addtl_len)73*4882a593Smuzhiyun static int _rng_recvmsg(struct crypto_rng *drng, struct msghdr *msg, size_t len,
74*4882a593Smuzhiyun u8 *addtl, size_t addtl_len)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun int err = 0;
77*4882a593Smuzhiyun int genlen = 0;
78*4882a593Smuzhiyun u8 result[MAXSIZE];
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun if (len == 0)
81*4882a593Smuzhiyun return 0;
82*4882a593Smuzhiyun if (len > MAXSIZE)
83*4882a593Smuzhiyun len = MAXSIZE;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /*
86*4882a593Smuzhiyun * although not strictly needed, this is a precaution against coding
87*4882a593Smuzhiyun * errors
88*4882a593Smuzhiyun */
89*4882a593Smuzhiyun memset(result, 0, len);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /*
92*4882a593Smuzhiyun * The enforcement of a proper seeding of an RNG is done within an
93*4882a593Smuzhiyun * RNG implementation. Some RNGs (DRBG, krng) do not need specific
94*4882a593Smuzhiyun * seeding as they automatically seed. The X9.31 DRNG will return
95*4882a593Smuzhiyun * an error if it was not seeded properly.
96*4882a593Smuzhiyun */
97*4882a593Smuzhiyun genlen = crypto_rng_generate(drng, addtl, addtl_len, result, len);
98*4882a593Smuzhiyun if (genlen < 0)
99*4882a593Smuzhiyun return genlen;
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun err = memcpy_to_msg(msg, result, len);
102*4882a593Smuzhiyun memzero_explicit(result, len);
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun return err ? err : len;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun
rng_recvmsg(struct socket * sock,struct msghdr * msg,size_t len,int flags)107*4882a593Smuzhiyun static int rng_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
108*4882a593Smuzhiyun int flags)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun struct sock *sk = sock->sk;
111*4882a593Smuzhiyun struct alg_sock *ask = alg_sk(sk);
112*4882a593Smuzhiyun struct rng_ctx *ctx = ask->private;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun return _rng_recvmsg(ctx->drng, msg, len, NULL, 0);
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
rng_test_recvmsg(struct socket * sock,struct msghdr * msg,size_t len,int flags)117*4882a593Smuzhiyun static int rng_test_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
118*4882a593Smuzhiyun int flags)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun struct sock *sk = sock->sk;
121*4882a593Smuzhiyun struct alg_sock *ask = alg_sk(sk);
122*4882a593Smuzhiyun struct rng_ctx *ctx = ask->private;
123*4882a593Smuzhiyun int ret;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun lock_sock(sock->sk);
126*4882a593Smuzhiyun ret = _rng_recvmsg(ctx->drng, msg, len, ctx->addtl, ctx->addtl_len);
127*4882a593Smuzhiyun rng_reset_addtl(ctx);
128*4882a593Smuzhiyun release_sock(sock->sk);
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun return ret;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
rng_test_sendmsg(struct socket * sock,struct msghdr * msg,size_t len)133*4882a593Smuzhiyun static int rng_test_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun int err;
136*4882a593Smuzhiyun struct alg_sock *ask = alg_sk(sock->sk);
137*4882a593Smuzhiyun struct rng_ctx *ctx = ask->private;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun lock_sock(sock->sk);
140*4882a593Smuzhiyun if (len > MAXSIZE) {
141*4882a593Smuzhiyun err = -EMSGSIZE;
142*4882a593Smuzhiyun goto unlock;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun rng_reset_addtl(ctx);
146*4882a593Smuzhiyun ctx->addtl = kmalloc(len, GFP_KERNEL);
147*4882a593Smuzhiyun if (!ctx->addtl) {
148*4882a593Smuzhiyun err = -ENOMEM;
149*4882a593Smuzhiyun goto unlock;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun err = memcpy_from_msg(ctx->addtl, msg, len);
153*4882a593Smuzhiyun if (err) {
154*4882a593Smuzhiyun rng_reset_addtl(ctx);
155*4882a593Smuzhiyun goto unlock;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun ctx->addtl_len = len;
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun unlock:
160*4882a593Smuzhiyun release_sock(sock->sk);
161*4882a593Smuzhiyun return err ? err : len;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun static struct proto_ops algif_rng_ops = {
165*4882a593Smuzhiyun .family = PF_ALG,
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun .connect = sock_no_connect,
168*4882a593Smuzhiyun .socketpair = sock_no_socketpair,
169*4882a593Smuzhiyun .getname = sock_no_getname,
170*4882a593Smuzhiyun .ioctl = sock_no_ioctl,
171*4882a593Smuzhiyun .listen = sock_no_listen,
172*4882a593Smuzhiyun .shutdown = sock_no_shutdown,
173*4882a593Smuzhiyun .mmap = sock_no_mmap,
174*4882a593Smuzhiyun .bind = sock_no_bind,
175*4882a593Smuzhiyun .accept = sock_no_accept,
176*4882a593Smuzhiyun .sendmsg = sock_no_sendmsg,
177*4882a593Smuzhiyun .sendpage = sock_no_sendpage,
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun .release = af_alg_release,
180*4882a593Smuzhiyun .recvmsg = rng_recvmsg,
181*4882a593Smuzhiyun };
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun static struct proto_ops __maybe_unused algif_rng_test_ops = {
184*4882a593Smuzhiyun .family = PF_ALG,
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun .connect = sock_no_connect,
187*4882a593Smuzhiyun .socketpair = sock_no_socketpair,
188*4882a593Smuzhiyun .getname = sock_no_getname,
189*4882a593Smuzhiyun .ioctl = sock_no_ioctl,
190*4882a593Smuzhiyun .listen = sock_no_listen,
191*4882a593Smuzhiyun .shutdown = sock_no_shutdown,
192*4882a593Smuzhiyun .mmap = sock_no_mmap,
193*4882a593Smuzhiyun .bind = sock_no_bind,
194*4882a593Smuzhiyun .accept = sock_no_accept,
195*4882a593Smuzhiyun .sendpage = sock_no_sendpage,
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun .release = af_alg_release,
198*4882a593Smuzhiyun .recvmsg = rng_test_recvmsg,
199*4882a593Smuzhiyun .sendmsg = rng_test_sendmsg,
200*4882a593Smuzhiyun };
201*4882a593Smuzhiyun
rng_bind(const char * name,u32 type,u32 mask)202*4882a593Smuzhiyun static void *rng_bind(const char *name, u32 type, u32 mask)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun struct rng_parent_ctx *pctx;
205*4882a593Smuzhiyun struct crypto_rng *rng;
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun pctx = kzalloc(sizeof(*pctx), GFP_KERNEL);
208*4882a593Smuzhiyun if (!pctx)
209*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun rng = crypto_alloc_rng(name, type, mask);
212*4882a593Smuzhiyun if (IS_ERR(rng)) {
213*4882a593Smuzhiyun kfree(pctx);
214*4882a593Smuzhiyun return ERR_CAST(rng);
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun pctx->drng = rng;
218*4882a593Smuzhiyun return pctx;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun
rng_release(void * private)221*4882a593Smuzhiyun static void rng_release(void *private)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun struct rng_parent_ctx *pctx = private;
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun if (unlikely(!pctx))
226*4882a593Smuzhiyun return;
227*4882a593Smuzhiyun crypto_free_rng(pctx->drng);
228*4882a593Smuzhiyun kfree_sensitive(pctx->entropy);
229*4882a593Smuzhiyun kfree_sensitive(pctx);
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun
rng_sock_destruct(struct sock * sk)232*4882a593Smuzhiyun static void rng_sock_destruct(struct sock *sk)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun struct alg_sock *ask = alg_sk(sk);
235*4882a593Smuzhiyun struct rng_ctx *ctx = ask->private;
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun rng_reset_addtl(ctx);
238*4882a593Smuzhiyun sock_kfree_s(sk, ctx, ctx->len);
239*4882a593Smuzhiyun af_alg_release_parent(sk);
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun
rng_accept_parent(void * private,struct sock * sk)242*4882a593Smuzhiyun static int rng_accept_parent(void *private, struct sock *sk)
243*4882a593Smuzhiyun {
244*4882a593Smuzhiyun struct rng_ctx *ctx;
245*4882a593Smuzhiyun struct rng_parent_ctx *pctx = private;
246*4882a593Smuzhiyun struct alg_sock *ask = alg_sk(sk);
247*4882a593Smuzhiyun unsigned int len = sizeof(*ctx);
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun ctx = sock_kmalloc(sk, len, GFP_KERNEL);
250*4882a593Smuzhiyun if (!ctx)
251*4882a593Smuzhiyun return -ENOMEM;
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun ctx->len = len;
254*4882a593Smuzhiyun ctx->addtl = NULL;
255*4882a593Smuzhiyun ctx->addtl_len = 0;
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun /*
258*4882a593Smuzhiyun * No seeding done at that point -- if multiple accepts are
259*4882a593Smuzhiyun * done on one RNG instance, each resulting FD points to the same
260*4882a593Smuzhiyun * state of the RNG.
261*4882a593Smuzhiyun */
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun ctx->drng = pctx->drng;
264*4882a593Smuzhiyun ask->private = ctx;
265*4882a593Smuzhiyun sk->sk_destruct = rng_sock_destruct;
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun /*
268*4882a593Smuzhiyun * Non NULL pctx->entropy means that CAVP test has been initiated on
269*4882a593Smuzhiyun * this socket, replace proto_ops algif_rng_ops with algif_rng_test_ops.
270*4882a593Smuzhiyun */
271*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_CRYPTO_USER_API_RNG_CAVP) && pctx->entropy)
272*4882a593Smuzhiyun sk->sk_socket->ops = &algif_rng_test_ops;
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun return 0;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun
rng_setkey(void * private,const u8 * seed,unsigned int seedlen)277*4882a593Smuzhiyun static int rng_setkey(void *private, const u8 *seed, unsigned int seedlen)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun struct rng_parent_ctx *pctx = private;
280*4882a593Smuzhiyun /*
281*4882a593Smuzhiyun * Check whether seedlen is of sufficient size is done in RNG
282*4882a593Smuzhiyun * implementations.
283*4882a593Smuzhiyun */
284*4882a593Smuzhiyun return crypto_rng_reset(pctx->drng, seed, seedlen);
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
rng_setentropy(void * private,sockptr_t entropy,unsigned int len)287*4882a593Smuzhiyun static int __maybe_unused rng_setentropy(void *private, sockptr_t entropy,
288*4882a593Smuzhiyun unsigned int len)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun struct rng_parent_ctx *pctx = private;
291*4882a593Smuzhiyun u8 *kentropy = NULL;
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun if (!capable(CAP_SYS_ADMIN))
294*4882a593Smuzhiyun return -EACCES;
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun if (pctx->entropy)
297*4882a593Smuzhiyun return -EINVAL;
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun if (len > MAXSIZE)
300*4882a593Smuzhiyun return -EMSGSIZE;
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun if (len) {
303*4882a593Smuzhiyun kentropy = memdup_sockptr(entropy, len);
304*4882a593Smuzhiyun if (IS_ERR(kentropy))
305*4882a593Smuzhiyun return PTR_ERR(kentropy);
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun crypto_rng_alg(pctx->drng)->set_ent(pctx->drng, kentropy, len);
309*4882a593Smuzhiyun /*
310*4882a593Smuzhiyun * Since rng doesn't perform any memory management for the entropy
311*4882a593Smuzhiyun * buffer, save kentropy pointer to pctx now to free it after use.
312*4882a593Smuzhiyun */
313*4882a593Smuzhiyun pctx->entropy = kentropy;
314*4882a593Smuzhiyun return 0;
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun static const struct af_alg_type algif_type_rng = {
318*4882a593Smuzhiyun .bind = rng_bind,
319*4882a593Smuzhiyun .release = rng_release,
320*4882a593Smuzhiyun .accept = rng_accept_parent,
321*4882a593Smuzhiyun .setkey = rng_setkey,
322*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_USER_API_RNG_CAVP
323*4882a593Smuzhiyun .setentropy = rng_setentropy,
324*4882a593Smuzhiyun #endif
325*4882a593Smuzhiyun .ops = &algif_rng_ops,
326*4882a593Smuzhiyun .name = "rng",
327*4882a593Smuzhiyun .owner = THIS_MODULE
328*4882a593Smuzhiyun };
329*4882a593Smuzhiyun
rng_init(void)330*4882a593Smuzhiyun static int __init rng_init(void)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun return af_alg_register_type(&algif_type_rng);
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun
rng_exit(void)335*4882a593Smuzhiyun static void __exit rng_exit(void)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun int err = af_alg_unregister_type(&algif_type_rng);
338*4882a593Smuzhiyun BUG_ON(err);
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun module_init(rng_init);
342*4882a593Smuzhiyun module_exit(rng_exit);
343