Back to home page

LXR

 
 

    


0001 /*
0002  * algif_rng: User-space interface for random number generators
0003  *
0004  * This file provides the user-space API for random number generators.
0005  *
0006  * Copyright (C) 2014, Stephan Mueller <smueller@chronox.de>
0007  *
0008  * Redistribution and use in source and binary forms, with or without
0009  * modification, are permitted provided that the following conditions
0010  * are met:
0011  * 1. Redistributions of source code must retain the above copyright
0012  *    notice, and the entire permission notice in its entirety,
0013  *    including the disclaimer of warranties.
0014  * 2. Redistributions in binary form must reproduce the above copyright
0015  *    notice, this list of conditions and the following disclaimer in the
0016  *    documentation and/or other materials provided with the distribution.
0017  * 3. The name of the author may not be used to endorse or promote
0018  *    products derived from this software without specific prior
0019  *    written permission.
0020  *
0021  * ALTERNATIVELY, this product may be distributed under the terms of
0022  * the GNU General Public License, in which case the provisions of the GPL2
0023  * are required INSTEAD OF the above restrictions.  (This clause is
0024  * necessary due to a potential bad interaction between the GPL and
0025  * the restrictions contained in a BSD-style copyright.)
0026  *
0027  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
0028  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0029  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
0030  * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
0031  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0032  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
0033  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
0034  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0035  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0036  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
0037  * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
0038  * DAMAGE.
0039  */
0040 
0041 #include <linux/module.h>
0042 #include <crypto/rng.h>
0043 #include <linux/random.h>
0044 #include <crypto/if_alg.h>
0045 #include <linux/net.h>
0046 #include <net/sock.h>
0047 
0048 MODULE_LICENSE("GPL");
0049 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
0050 MODULE_DESCRIPTION("User-space interface for random number generators");
0051 
0052 struct rng_ctx {
0053 #define MAXSIZE 128
0054     unsigned int len;
0055     struct crypto_rng *drng;
0056 };
0057 
0058 static int rng_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
0059                int flags)
0060 {
0061     struct sock *sk = sock->sk;
0062     struct alg_sock *ask = alg_sk(sk);
0063     struct rng_ctx *ctx = ask->private;
0064     int err = -EFAULT;
0065     int genlen = 0;
0066     u8 result[MAXSIZE];
0067 
0068     if (len == 0)
0069         return 0;
0070     if (len > MAXSIZE)
0071         len = MAXSIZE;
0072 
0073     /*
0074      * although not strictly needed, this is a precaution against coding
0075      * errors
0076      */
0077     memset(result, 0, len);
0078 
0079     /*
0080      * The enforcement of a proper seeding of an RNG is done within an
0081      * RNG implementation. Some RNGs (DRBG, krng) do not need specific
0082      * seeding as they automatically seed. The X9.31 DRNG will return
0083      * an error if it was not seeded properly.
0084      */
0085     genlen = crypto_rng_get_bytes(ctx->drng, result, len);
0086     if (genlen < 0)
0087         return genlen;
0088 
0089     err = memcpy_to_msg(msg, result, len);
0090     memzero_explicit(result, len);
0091 
0092     return err ? err : len;
0093 }
0094 
0095 static struct proto_ops algif_rng_ops = {
0096     .family     =   PF_ALG,
0097 
0098     .connect    =   sock_no_connect,
0099     .socketpair =   sock_no_socketpair,
0100     .getname    =   sock_no_getname,
0101     .ioctl      =   sock_no_ioctl,
0102     .listen     =   sock_no_listen,
0103     .shutdown   =   sock_no_shutdown,
0104     .getsockopt =   sock_no_getsockopt,
0105     .mmap       =   sock_no_mmap,
0106     .bind       =   sock_no_bind,
0107     .accept     =   sock_no_accept,
0108     .setsockopt =   sock_no_setsockopt,
0109     .poll       =   sock_no_poll,
0110     .sendmsg    =   sock_no_sendmsg,
0111     .sendpage   =   sock_no_sendpage,
0112 
0113     .release    =   af_alg_release,
0114     .recvmsg    =   rng_recvmsg,
0115 };
0116 
0117 static void *rng_bind(const char *name, u32 type, u32 mask)
0118 {
0119     return crypto_alloc_rng(name, type, mask);
0120 }
0121 
0122 static void rng_release(void *private)
0123 {
0124     crypto_free_rng(private);
0125 }
0126 
0127 static void rng_sock_destruct(struct sock *sk)
0128 {
0129     struct alg_sock *ask = alg_sk(sk);
0130     struct rng_ctx *ctx = ask->private;
0131 
0132     sock_kfree_s(sk, ctx, ctx->len);
0133     af_alg_release_parent(sk);
0134 }
0135 
0136 static int rng_accept_parent(void *private, struct sock *sk)
0137 {
0138     struct rng_ctx *ctx;
0139     struct alg_sock *ask = alg_sk(sk);
0140     unsigned int len = sizeof(*ctx);
0141 
0142     ctx = sock_kmalloc(sk, len, GFP_KERNEL);
0143     if (!ctx)
0144         return -ENOMEM;
0145 
0146     ctx->len = len;
0147 
0148     /*
0149      * No seeding done at that point -- if multiple accepts are
0150      * done on one RNG instance, each resulting FD points to the same
0151      * state of the RNG.
0152      */
0153 
0154     ctx->drng = private;
0155     ask->private = ctx;
0156     sk->sk_destruct = rng_sock_destruct;
0157 
0158     return 0;
0159 }
0160 
0161 static int rng_setkey(void *private, const u8 *seed, unsigned int seedlen)
0162 {
0163     /*
0164      * Check whether seedlen is of sufficient size is done in RNG
0165      * implementations.
0166      */
0167     return crypto_rng_reset(private, seed, seedlen);
0168 }
0169 
0170 static const struct af_alg_type algif_type_rng = {
0171     .bind       =   rng_bind,
0172     .release    =   rng_release,
0173     .accept     =   rng_accept_parent,
0174     .setkey     =   rng_setkey,
0175     .ops        =   &algif_rng_ops,
0176     .name       =   "rng",
0177     .owner      =   THIS_MODULE
0178 };
0179 
0180 static int __init rng_init(void)
0181 {
0182     return af_alg_register_type(&algif_type_rng);
0183 }
0184 
0185 static void __exit rng_exit(void)
0186 {
0187     int err = af_alg_unregister_type(&algif_type_rng);
0188     BUG_ON(err);
0189 }
0190 
0191 module_init(rng_init);
0192 module_exit(rng_exit);