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