Print this page
9971 Make getrandom(2) a public interface
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/syscall/getrandom.c
+++ new/usr/src/uts/common/syscall/getrandom.c
1 1 /*
2 2 * This file and its contents are supplied under the terms of the
↓ open down ↓ |
2 lines elided |
↑ open up ↑ |
3 3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 4 * You may only use this file in accordance with the terms of version
5 5 * 1.0 of the CDDL.
6 6 *
7 7 * A full copy of the text of the CDDL should have accompanied this
8 8 * source. A copy of the CDDL is also available via the Internet at
9 9 * http://www.illumos.org/license/CDDL.
10 10 */
11 11
12 12 /*
13 - * Copyright (c) 2015, Joyent, Inc.
13 + * Copyright (c) 2018, Joyent, Inc.
14 14 */
15 15
16 16 /*
17 17 * getrandom system call implementation
18 18 */
19 19
20 20 #include <sys/types.h>
21 21 #include <sys/errno.h>
22 22 #include <sys/systm.h>
23 23 #include <sys/random.h>
24 24 #include <sys/ddi.h>
25 25 #include <sys/sunddi.h>
26 26 #include <sys/sysmacros.h>
27 27
28 28 #include <sys/random.h>
↓ open down ↓ |
5 lines elided |
↑ open up ↑ |
29 29
30 30 /*
31 31 * Impose a maximum upper bound on the number of bytes that we'll read in one
32 32 * go, ala a read of /dev/random. For /dev/urandom, we clamp it based on our
33 33 * return value, because the system call returns an int, we can't handle more
34 34 * than INT_MAX.
35 35 */
36 36 #define MAXRANDBYTES 1024
37 37 #define MAXURANDBYTES INT_MAX
38 38
39 -int
40 -getrandom(void *bufp, size_t buflen, int flags)
39 +ssize_t
40 +getrandom(void *bufp, size_t buflen, unsigned int flags)
41 41 {
42 42 int out = 0;
43 43 uint8_t rbytes[128];
44 44 uint8_t *buf = bufp;
45 45
46 46 if (flags & ~(GRND_NONBLOCK | GRND_RANDOM))
47 47 return (set_errno(EINVAL));
48 48
49 49 if ((flags & GRND_RANDOM) && buflen > MAXRANDBYTES) {
50 50 buflen = MAXRANDBYTES;
51 51 } else if (buflen > MAXURANDBYTES) {
52 52 buflen = MAXURANDBYTES;
53 53 }
54 54
55 55 while (out < buflen) {
56 56 int err;
57 57 size_t len = MIN(sizeof (rbytes), buflen - out);
58 58
59 59 if (flags & GRND_RANDOM) {
60 60 if (flags & GRND_NONBLOCK)
61 61 err = random_get_bytes(rbytes, len);
62 62 else
63 63 err = random_get_blocking_bytes(rbytes, len);
64 64 } else {
65 65 err = random_get_pseudo_bytes(rbytes, len);
66 66 }
67 67
68 68 if (err == 0) {
69 69 if (ddi_copyout(rbytes, buf + out, len, 0) != 0)
70 70 return (set_errno(EFAULT));
71 71 out += len;
72 72 } else if (err == EAGAIN && out > 0) {
73 73 break;
74 74 } else {
75 75 return (set_errno(err));
76 76 }
77 77 }
78 78
79 79 return (out);
80 80 }
↓ open down ↓ |
30 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX