2 * algif_rng: User-space interface for random number generators
4 * This file provides the user-space API for random number generators.
6 * Copyright (C) 2014, Stephan Mueller <smueller@chronox.de>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, and the entire permission notice in its entirety,
13 * including the disclaimer of warranties.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior
21 * ALTERNATIVELY, this product may be distributed under the terms of
22 * the GNU General Public License, in which case the provisions of the GPL2
23 * are required INSTEAD OF the above restrictions. (This clause is
24 * necessary due to a potential bad interaction between the GPL and
25 * the restrictions contained in a BSD-style copyright.)
27 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
28 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
30 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
31 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
33 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
34 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
41 #include <linux/capability.h>
42 #include <linux/module.h>
43 #include <crypto/rng.h>
44 #include <linux/random.h>
45 #include <crypto/if_alg.h>
46 #include <linux/net.h>
49 MODULE_LICENSE("GPL");
50 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
51 MODULE_DESCRIPTION("User-space interface for random number generators");
56 struct crypto_rng
*drng
;
61 struct rng_parent_ctx
{
62 struct crypto_rng
*drng
;
66 static void rng_reset_addtl(struct rng_ctx
*ctx
)
68 kfree_sensitive(ctx
->addtl
);
73 static int _rng_recvmsg(struct crypto_rng
*drng
, struct msghdr
*msg
, size_t len
,
74 u8
*addtl
, size_t addtl_len
)
86 * although not strictly needed, this is a precaution against coding
89 memset(result
, 0, len
);
92 * The enforcement of a proper seeding of an RNG is done within an
93 * RNG implementation. Some RNGs (DRBG, krng) do not need specific
94 * seeding as they automatically seed. The X9.31 DRNG will return
95 * an error if it was not seeded properly.
97 genlen
= crypto_rng_generate(drng
, addtl
, addtl_len
, result
, len
);
101 err
= memcpy_to_msg(msg
, result
, len
);
102 memzero_explicit(result
, len
);
104 return err
? err
: len
;
107 static int rng_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
,
110 struct sock
*sk
= sock
->sk
;
111 struct alg_sock
*ask
= alg_sk(sk
);
112 struct rng_ctx
*ctx
= ask
->private;
114 return _rng_recvmsg(ctx
->drng
, msg
, len
, NULL
, 0);
117 static int rng_test_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
,
120 struct sock
*sk
= sock
->sk
;
121 struct alg_sock
*ask
= alg_sk(sk
);
122 struct rng_ctx
*ctx
= ask
->private;
126 ret
= _rng_recvmsg(ctx
->drng
, msg
, len
, ctx
->addtl
, ctx
->addtl_len
);
127 rng_reset_addtl(ctx
);
128 release_sock(sock
->sk
);
133 static int rng_test_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
)
136 struct alg_sock
*ask
= alg_sk(sock
->sk
);
137 struct rng_ctx
*ctx
= ask
->private;
145 rng_reset_addtl(ctx
);
146 ctx
->addtl
= kmalloc(len
, GFP_KERNEL
);
152 err
= memcpy_from_msg(ctx
->addtl
, msg
, len
);
154 rng_reset_addtl(ctx
);
157 ctx
->addtl_len
= len
;
160 release_sock(sock
->sk
);
161 return err
? err
: len
;
164 static struct proto_ops algif_rng_ops
= {
167 .connect
= sock_no_connect
,
168 .socketpair
= sock_no_socketpair
,
169 .getname
= sock_no_getname
,
170 .ioctl
= sock_no_ioctl
,
171 .listen
= sock_no_listen
,
172 .shutdown
= sock_no_shutdown
,
173 .mmap
= sock_no_mmap
,
174 .bind
= sock_no_bind
,
175 .accept
= sock_no_accept
,
176 .sendmsg
= sock_no_sendmsg
,
178 .release
= af_alg_release
,
179 .recvmsg
= rng_recvmsg
,
182 static struct proto_ops __maybe_unused algif_rng_test_ops
= {
185 .connect
= sock_no_connect
,
186 .socketpair
= sock_no_socketpair
,
187 .getname
= sock_no_getname
,
188 .ioctl
= sock_no_ioctl
,
189 .listen
= sock_no_listen
,
190 .shutdown
= sock_no_shutdown
,
191 .mmap
= sock_no_mmap
,
192 .bind
= sock_no_bind
,
193 .accept
= sock_no_accept
,
195 .release
= af_alg_release
,
196 .recvmsg
= rng_test_recvmsg
,
197 .sendmsg
= rng_test_sendmsg
,
200 static void *rng_bind(const char *name
, u32 type
, u32 mask
)
202 struct rng_parent_ctx
*pctx
;
203 struct crypto_rng
*rng
;
205 pctx
= kzalloc(sizeof(*pctx
), GFP_KERNEL
);
207 return ERR_PTR(-ENOMEM
);
209 rng
= crypto_alloc_rng(name
, type
, mask
);
212 return ERR_CAST(rng
);
219 static void rng_release(void *private)
221 struct rng_parent_ctx
*pctx
= private;
225 crypto_free_rng(pctx
->drng
);
226 kfree_sensitive(pctx
->entropy
);
227 kfree_sensitive(pctx
);
230 static void rng_sock_destruct(struct sock
*sk
)
232 struct alg_sock
*ask
= alg_sk(sk
);
233 struct rng_ctx
*ctx
= ask
->private;
235 rng_reset_addtl(ctx
);
236 sock_kfree_s(sk
, ctx
, ctx
->len
);
237 af_alg_release_parent(sk
);
240 static int rng_accept_parent(void *private, struct sock
*sk
)
243 struct rng_parent_ctx
*pctx
= private;
244 struct alg_sock
*ask
= alg_sk(sk
);
245 unsigned int len
= sizeof(*ctx
);
247 ctx
= sock_kmalloc(sk
, len
, GFP_KERNEL
);
256 * No seeding done at that point -- if multiple accepts are
257 * done on one RNG instance, each resulting FD points to the same
261 ctx
->drng
= pctx
->drng
;
263 sk
->sk_destruct
= rng_sock_destruct
;
266 * Non NULL pctx->entropy means that CAVP test has been initiated on
267 * this socket, replace proto_ops algif_rng_ops with algif_rng_test_ops.
269 if (IS_ENABLED(CONFIG_CRYPTO_USER_API_RNG_CAVP
) && pctx
->entropy
)
270 sk
->sk_socket
->ops
= &algif_rng_test_ops
;
275 static int rng_setkey(void *private, const u8
*seed
, unsigned int seedlen
)
277 struct rng_parent_ctx
*pctx
= private;
279 * Check whether seedlen is of sufficient size is done in RNG
282 return crypto_rng_reset(pctx
->drng
, seed
, seedlen
);
285 static int __maybe_unused
rng_setentropy(void *private, sockptr_t entropy
,
288 struct rng_parent_ctx
*pctx
= private;
291 if (!capable(CAP_SYS_ADMIN
))
301 kentropy
= memdup_sockptr(entropy
, len
);
302 if (IS_ERR(kentropy
))
303 return PTR_ERR(kentropy
);
306 crypto_rng_alg(pctx
->drng
)->set_ent(pctx
->drng
, kentropy
, len
);
308 * Since rng doesn't perform any memory management for the entropy
309 * buffer, save kentropy pointer to pctx now to free it after use.
311 pctx
->entropy
= kentropy
;
315 static const struct af_alg_type algif_type_rng
= {
317 .release
= rng_release
,
318 .accept
= rng_accept_parent
,
319 .setkey
= rng_setkey
,
320 #ifdef CONFIG_CRYPTO_USER_API_RNG_CAVP
321 .setentropy
= rng_setentropy
,
323 .ops
= &algif_rng_ops
,
328 static int __init
rng_init(void)
330 return af_alg_register_type(&algif_type_rng
);
333 static void __exit
rng_exit(void)
335 int err
= af_alg_unregister_type(&algif_type_rng
);
339 module_init(rng_init
);
340 module_exit(rng_exit
);