1.\" Copyright (c) 2018 Mariusz Zaborski <[email protected]>
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
25.\" $FreeBSD$
26.\"
27.Dd February 26, 2018
28.Dt CAP_RANDOM 3
29.Os
30.Sh NAME
31.Nm cap_random_buf
32.Nd "library for getting entropy in capability mode"
33.Sh LIBRARY
34.Lb libcap_random
35.Sh SYNOPSIS
36.In sys/nv.h
37.In libcasper.h
38.In casper/cap_random.h
39.Ft "int"
40.Fn cap_random_buf "cap_channel_t *chan" "void *buf" "size_t nbytes"
41.Sh DESCRIPTION
42The function
43.Fn cap_random_buf
44is equivalent to
45.Xr arc4random_buf 3
46except that the connection to the
47.Nm system.random
48service needs to be provided.
49.Sh EXAMPLES
50The following example first opens a capability to casper and then uses this
51capability to create the
52.Nm system.random
53casper service to obtain entropy.
54.Bd -literal
55cap_channel_t *capcas, *caprandom;
56unsigned char buf[16];
57int i;
58
59/* Open capability to Casper. */
60capcas = cap_init();
61if (capcas == NULL)
62	err(1, "Unable to contact Casper");
63
64/* Enter capability mode sandbox. */
65if (cap_enter() < 0 && errno != ENOSYS)
66	err(1, "Unable to enter capability mode");
67
68/* Use Casper capability to create capability to the system.random service. */
69caprandom = cap_service_open(capcas, "system.random");
70if (caprandom == NULL)
71	err(1, "Unable to open system.random service");
72
73/* Close Casper capability, we don't need it anymore. */
74cap_close(capcas);
75
76/* Obtain entropy. */
77if (cap_random_buf(caprandom, buf, sizeof(buf)) < 0)
78	err(1, "Unable to obtain entropy");
79
80for (i = 0; i < sizeof(buf); i++)
81	printf("%.2x ", buf[i]);
82printf("\\n");
83.Ed
84.Sh SEE ALSO
85.Xr cap_enter 2 ,
86.Xr arc4random_buf 3 ,
87.Xr err 3 ,
88.Xr capsicum 4 ,
89.Xr nv 9
90.Sh AUTHORS
91The
92.Nm cap_random
93service was implemented by
94.An Pawel Jakub Dawidek Aq Mt [email protected]
95under sponsorship from the FreeBSD Foundation.
96.Pp
97This manual page was written by
98.An Mariusz Zaborski Aq Mt [email protected] .
99