algif_rng.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * algif_rng: User-space interface for random number generators
  3. *
  4. * This file provides the user-space API for random number generators.
  5. *
  6. * Copyright (C) 2014, Stephan Mueller <smueller@chronox.de>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  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
  19. * written permission.
  20. *
  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.)
  26. *
  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
  38. * DAMAGE.
  39. */
  40. #include <linux/module.h>
  41. #include <crypto/rng.h>
  42. #include <linux/random.h>
  43. #include <crypto/if_alg.h>
  44. #include <linux/net.h>
  45. #include <net/sock.h>
  46. MODULE_LICENSE("GPL");
  47. MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
  48. MODULE_DESCRIPTION("User-space interface for random number generators");
  49. struct rng_ctx {
  50. #define MAXSIZE 128
  51. unsigned int len;
  52. struct crypto_rng *drng;
  53. };
  54. static int rng_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  55. int flags)
  56. {
  57. struct sock *sk = sock->sk;
  58. struct alg_sock *ask = alg_sk(sk);
  59. struct rng_ctx *ctx = ask->private;
  60. int err = -EFAULT;
  61. int genlen = 0;
  62. u8 result[MAXSIZE];
  63. if (len == 0)
  64. return 0;
  65. if (len > MAXSIZE)
  66. len = MAXSIZE;
  67. /*
  68. * although not strictly needed, this is a precaution against coding
  69. * errors
  70. */
  71. memset(result, 0, len);
  72. /*
  73. * The enforcement of a proper seeding of an RNG is done within an
  74. * RNG implementation. Some RNGs (DRBG, krng) do not need specific
  75. * seeding as they automatically seed. The X9.31 DRNG will return
  76. * an error if it was not seeded properly.
  77. */
  78. genlen = crypto_rng_get_bytes(ctx->drng, result, len);
  79. if (genlen < 0)
  80. return genlen;
  81. err = memcpy_to_msg(msg, result, len);
  82. memzero_explicit(result, len);
  83. return err ? err : len;
  84. }
  85. static struct proto_ops algif_rng_ops = {
  86. .family = PF_ALG,
  87. .connect = sock_no_connect,
  88. .socketpair = sock_no_socketpair,
  89. .getname = sock_no_getname,
  90. .ioctl = sock_no_ioctl,
  91. .listen = sock_no_listen,
  92. .shutdown = sock_no_shutdown,
  93. .getsockopt = sock_no_getsockopt,
  94. .mmap = sock_no_mmap,
  95. .bind = sock_no_bind,
  96. .accept = sock_no_accept,
  97. .setsockopt = sock_no_setsockopt,
  98. .sendmsg = sock_no_sendmsg,
  99. .sendpage = sock_no_sendpage,
  100. .release = af_alg_release,
  101. .recvmsg = rng_recvmsg,
  102. };
  103. static void *rng_bind(const char *name, u32 type, u32 mask)
  104. {
  105. return crypto_alloc_rng(name, type, mask);
  106. }
  107. static void rng_release(void *private)
  108. {
  109. crypto_free_rng(private);
  110. }
  111. static void rng_sock_destruct(struct sock *sk)
  112. {
  113. struct alg_sock *ask = alg_sk(sk);
  114. struct rng_ctx *ctx = ask->private;
  115. sock_kfree_s(sk, ctx, ctx->len);
  116. af_alg_release_parent(sk);
  117. }
  118. static int rng_accept_parent(void *private, struct sock *sk)
  119. {
  120. struct rng_ctx *ctx;
  121. struct alg_sock *ask = alg_sk(sk);
  122. unsigned int len = sizeof(*ctx);
  123. ctx = sock_kmalloc(sk, len, GFP_KERNEL);
  124. if (!ctx)
  125. return -ENOMEM;
  126. ctx->len = len;
  127. /*
  128. * No seeding done at that point -- if multiple accepts are
  129. * done on one RNG instance, each resulting FD points to the same
  130. * state of the RNG.
  131. */
  132. ctx->drng = private;
  133. ask->private = ctx;
  134. sk->sk_destruct = rng_sock_destruct;
  135. return 0;
  136. }
  137. static int rng_setkey(void *private, const u8 *seed, unsigned int seedlen)
  138. {
  139. /*
  140. * Check whether seedlen is of sufficient size is done in RNG
  141. * implementations.
  142. */
  143. return crypto_rng_reset(private, seed, seedlen);
  144. }
  145. static const struct af_alg_type algif_type_rng = {
  146. .bind = rng_bind,
  147. .release = rng_release,
  148. .accept = rng_accept_parent,
  149. .setkey = rng_setkey,
  150. .ops = &algif_rng_ops,
  151. .name = "rng",
  152. .owner = THIS_MODULE
  153. };
  154. static int __init rng_init(void)
  155. {
  156. return af_alg_register_type(&algif_type_rng);
  157. }
  158. static void __exit rng_exit(void)
  159. {
  160. int err = af_alg_unregister_type(&algif_type_rng);
  161. BUG_ON(err);
  162. }
  163. module_init(rng_init);
  164. module_exit(rng_exit);