1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * This software was developed by Pawel Jakub Dawidek under sponsorship from
8  * the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/nv.h>
36 
37 #include <assert.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 #include <libcasper.h>
44 #include <libcasper_service.h>
45 
46 #include "cap_random.h"
47 
48 #define	MAXSIZE	(1024 * 1024)
49 
50 int
cap_random_buf(cap_channel_t * chan,void * buf,size_t nbytes)51 cap_random_buf(cap_channel_t *chan, void *buf, size_t nbytes)
52 {
53 	nvlist_t *nvl;
54 	const void *randbuf;
55 	uint8_t *ptr;
56 	size_t left, randbufsize;
57 
58 	left = nbytes;
59 	ptr = buf;
60 
61 	while (left > 0) {
62 		nvl = nvlist_create(0);
63 		nvlist_add_string(nvl, "cmd", "generate");
64 		nvlist_add_number(nvl, "size",
65 		    (uint64_t)(left > MAXSIZE ? MAXSIZE : left));
66 		nvl = cap_xfer_nvlist(chan, nvl);
67 		if (nvl == NULL)
68 			return (-1);
69 		if (nvlist_get_number(nvl, "error") != 0) {
70 			errno = (int)nvlist_get_number(nvl, "error");
71 			nvlist_destroy(nvl);
72 			return (-1);
73 		}
74 
75 		randbuf = nvlist_get_binary(nvl, "data", &randbufsize);
76 		memcpy(ptr, randbuf, randbufsize);
77 
78 		nvlist_destroy(nvl);
79 
80 		ptr += randbufsize;
81 		assert(left >= randbufsize);
82 		left -= randbufsize;
83 	}
84 
85 	return (0);
86 }
87 
88 /*
89  * Service functions.
90  */
91 
92 static int
random_command(const char * cmd,const nvlist_t * limits __unused,nvlist_t * nvlin,nvlist_t * nvlout)93 random_command(const char *cmd, const nvlist_t *limits __unused,
94     nvlist_t *nvlin, nvlist_t *nvlout)
95 {
96 	void *data;
97 	size_t size;
98 
99 	if (strcmp(cmd, "generate") != 0)
100 		return (EINVAL);
101 	if (!nvlist_exists_number(nvlin, "size"))
102 		return (EINVAL);
103 
104 	size = (size_t)nvlist_get_number(nvlin, "size");
105 	if (size == 0 || size > MAXSIZE)
106 		return (EINVAL);
107 
108 	data = malloc(size);
109 	if (data == NULL)
110 		return (ENOMEM);
111 
112 	arc4random_buf(data, size);
113 
114 	nvlist_move_binary(nvlout, "data", data, size);
115 
116 	return (0);
117 }
118 
119 CREATE_SERVICE("system.random", NULL, random_command, 0);
120