| 1 | /* Kernel crypto API AF_ALG Random Number Generator API |
| 2 | * |
| 3 | * Copyright (C) 2016 - 2022, Stephan Mueller <[email protected]> |
| 4 | * |
| 5 | * License: see COPYING file in root directory |
| 6 | * |
| 7 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 8 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 9 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF |
| 10 | * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
| 11 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 12 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 13 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 14 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 15 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 16 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 17 | * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH |
| 18 | * DAMAGE. |
| 19 | */ |
| 20 | |
| 21 | #define _GNU_SOURCE |
| 22 | #include <unistd.h> |
| 23 | #include <linux/random.h> |
| 24 | #ifdef HAVE_GETRANDOM |
| 25 | #include <sys/random.h> |
| 26 | #endif |
| 27 | |
| 28 | #include "internal.h" |
| 29 | #include "kcapi.h" |
| 30 | |
| 31 | int kcapi_rng_init(struct kcapi_handle **handle, const char *ciphername, |
| 32 | uint32_t flags) |
| 33 | { |
| 34 | return _kcapi_handle_init(handle, "rng", ciphername, flags); |
| 35 | } |
| 36 | |
| 37 | void kcapi_rng_destroy(struct kcapi_handle *handle) |
| 38 | { |
| 39 | _kcapi_handle_destroy(handle); |
| 40 | } |
| 41 | |
| 42 | int kcapi_rng_seed(struct kcapi_handle *handle, uint8_t *seed, |
| 43 | uint32_t seedlen) |
| 44 | { |
| 45 | kcapi_dolog(KCAPI_LOG_VERBOSE, "Seed DRNG with %u bytes of seed", |
| 46 | seedlen); |
| 47 | return _kcapi_common_setkey(handle, seed, seedlen); |
| 48 | } |
| 49 | |
| 50 | int kcapi_rng_setentropy(struct kcapi_handle *handle, uint8_t *ent, |
| 51 | uint32_t entlen) |
| 52 | { |
| 53 | kcapi_dolog(KCAPI_LOG_VERBOSE, |
| 54 | "Set %u bytes of initial entropy of DRBG", entlen); |
| 55 | return _kcapi_common_setentropy(handle, ent, entlen); |
| 56 | } |
| 57 | |
| 58 | int kcapi_rng_setaddtl(struct kcapi_handle *handle, uint8_t *addtl, |
| 59 | uint32_t len) |
| 60 | { |
| 61 | ssize_t ret = 0; |
| 62 | |
| 63 | kcapi_dolog(KCAPI_LOG_VERBOSE, |
| 64 | "Set %u bytes of additional data for DRBG", len); |
| 65 | |
| 66 | if (len > INT_MAX) |
| 67 | return -EMSGSIZE; |
| 68 | |
| 69 | ret = _kcapi_common_accept(handle); |
| 70 | if (ret) |
| 71 | return (int)ret; |
| 72 | ret = send(*_kcapi_get_opfd(handle), addtl, len, MSG_MORE); |
| 73 | |
| 74 | if (ret < 0) |
| 75 | return (int)ret; |
| 76 | if ((uint32_t)ret < len) |
| 77 | return -EIO; |
| 78 | |
| 79 | handle->processed_sg += 1; |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | IMPL_SYMVER(rng_generate, "1.3.1") |
| 84 | ssize_t impl_rng_generate(struct kcapi_handle *handle, |
| 85 | uint8_t *buffer, size_t len) |
| 86 | { |
| 87 | ssize_t out = 0; |
| 88 | struct iovec iov; |
| 89 | |
| 90 | while (len) { |
| 91 | ssize_t r = 0; |
| 92 | |
| 93 | iov.iov_base = (void *)(uintptr_t)buffer; |
| 94 | iov.iov_len = len; |
| 95 | r = _kcapi_common_recv_data(handle, &iov, 1); |
| 96 | if (0 >= r) |
| 97 | return r; |
| 98 | len -= (size_t)r; |
| 99 | out += r; |
| 100 | |
| 101 | buffer += r; |
| 102 | } |
| 103 | |
| 104 | return out; |
| 105 | } |
| 106 | |
| 107 | ORIG_SYMVER(rng_generate, "0.12.0") |
| 108 | int32_t orig_rng_generate(struct kcapi_handle *handle, |
| 109 | uint8_t *buffer, uint32_t len) |
| 110 | { |
| 111 | return (int32_t)impl_rng_generate(handle, buffer, len); |
| 112 | } |
| 113 | |
| 114 | uint32_t kcapi_rng_seedsize(struct kcapi_handle *handle) |
| 115 | { |
| 116 | struct kcapi_handle_tfm *tfm = handle->tfm; |
| 117 | |
| 118 | return tfm->info.rng_seedsize; |
| 119 | } |
| 120 | |
| 121 | #if !defined(HAVE_GETRANDOM) && !defined(__NR_getrandom) |
| 122 | static int random_fd = -1; |
| 123 | static int open_random(void) |
| 124 | { |
| 125 | random_fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC); |
| 126 | if (0 > random_fd) |
| 127 | return random_fd; |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static void close_random(void) |
| 133 | { |
| 134 | close(random_fd); |
| 135 | } |
| 136 | #endif |
| 137 | |
| 138 | static int get_random(uint8_t *buf, size_t buflen) |
| 139 | { |
| 140 | ssize_t ret; |
| 141 | |
| 142 | if (buflen > INT_MAX) |
| 143 | return 1; |
| 144 | |
| 145 | #if (!defined(HAVE_GETRANDOM) && !defined(__NR_getrandom)) |
| 146 | ret = open_random(); |
| 147 | if (ret) |
| 148 | return ret; |
| 149 | #endif |
| 150 | |
| 151 | do { |
| 152 | #ifdef HAVE_GETRANDOM |
| 153 | ret = getrandom(buf, buflen, 0); |
| 154 | kcapi_dolog(KCAPI_LOG_DEBUG, |
| 155 | "Accessed getrandom system call for %zu bytes", |
| 156 | buflen); |
| 157 | #elif defined __NR_getrandom |
| 158 | ret = syscall(__NR_getrandom, buf, buflen, 0); |
| 159 | kcapi_dolog(KCAPI_LOG_DEBUG, |
| 160 | "Accessed getrandom system call for %zu bytes", |
| 161 | buflen); |
| 162 | #else |
| 163 | ret = read(random_fd, buf, buflen); |
| 164 | kcapi_dolog(KCAPI_LOG_DEBUG, |
| 165 | "Accessed /dev/urandom for %zu bytes", buflen); |
| 166 | #endif |
| 167 | if (0 < ret) { |
| 168 | buflen -= (size_t)ret; |
| 169 | buf += ret; |
| 170 | } |
| 171 | } while ((0 < ret || EINTR == errno || ERESTART == errno) |
| 172 | && buflen > 0); |
| 173 | |
| 174 | #if (!defined(HAVE_GETRANDOM) && !defined(__NR_getrandom)) |
| 175 | close_random(); |
| 176 | #endif |
| 177 | |
| 178 | if (buflen == 0) |
| 179 | return 0; |
| 180 | return 1; |
| 181 | } |
| 182 | |
| 183 | /* For efficiency reasons, this should be identical to algif_rng.c:MAXSIZE. */ |
| 184 | #define KCAPI_RNG_BUFSIZE 128 |
| 185 | /* Minimum seed is 256 bits. */ |
| 186 | #define KCAPI_RNG_MINSEEDSIZE 32 |
| 187 | #define KCAPI_APP_ALIGN 8 |
| 188 | #define __aligned(x) __attribute__((aligned(x))) |
| 189 | |
| 190 | IMPL_SYMVER(rng_get_bytes, "1.3.1") |
| 191 | ssize_t impl_rng_get_bytes(uint8_t *buffer, size_t outlen) |
| 192 | { |
| 193 | struct kcapi_handle *handle; |
| 194 | uint8_t buf[KCAPI_RNG_BUFSIZE] __aligned(KCAPI_APP_ALIGN); |
| 195 | uint8_t *seedbuf = buf; |
| 196 | uint32_t seedsize = 0; |
| 197 | size_t orig_outlen = outlen; |
| 198 | ssize_t ret = _kcapi_handle_init(&handle, "rng", "stdrng", 0); |
| 199 | if (ret) |
| 200 | return ret; |
| 201 | |
| 202 | seedsize = kcapi_rng_seedsize(handle); |
| 203 | if (seedsize) { |
| 204 | /* |
| 205 | * Only reseed, if there is a seedsize defined. For example, |
| 206 | * the DRBG has a seedsize of 0 because it seeds itself from |
| 207 | * known good noise sources. |
| 208 | */ |
| 209 | if (seedsize < KCAPI_RNG_MINSEEDSIZE) |
| 210 | seedsize = KCAPI_RNG_MINSEEDSIZE; |
| 211 | |
| 212 | /* |
| 213 | * Only allocate a new buffer if our buffer is |
| 214 | * insufficiently large. |
| 215 | */ |
| 216 | if (seedsize > KCAPI_RNG_BUFSIZE) { |
| 217 | seedbuf = calloc(1, seedsize); |
| 218 | if (!seedbuf) { |
| 219 | ret = -ENOMEM; |
| 220 | goto out; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | ret = get_random(seedbuf, seedsize); |
| 225 | if (ret) |
| 226 | goto out; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Invoke seeding even if seedsize is 0 -- this also triggers any |
| 231 | * internal seeding operation like in the DRBG. |
| 232 | */ |
| 233 | ret = kcapi_rng_seed(handle, seedbuf, seedsize); |
| 234 | if (ret) |
| 235 | goto out; |
| 236 | |
| 237 | while (outlen) { |
| 238 | size_t todo = (outlen < KCAPI_RNG_BUFSIZE) ? |
| 239 | outlen : KCAPI_RNG_BUFSIZE; |
| 240 | |
| 241 | ret = kcapi_rng_generate(handle, buffer, todo); |
| 242 | if (ret < 0) |
| 243 | goto out; |
| 244 | |
| 245 | if (ret == 0) { |
| 246 | ret = -EFAULT; |
| 247 | goto out; |
| 248 | } |
| 249 | outlen -= (size_t)ret; |
| 250 | buffer += ret; |
| 251 | } |
| 252 | |
| 253 | ret = (ssize_t)orig_outlen; |
| 254 | |
| 255 | out: |
| 256 | /* Free seedbuf if it was allocated. */ |
| 257 | if (seedbuf && (seedbuf != buf)) { |
| 258 | kcapi_memset_secure(seedbuf, 0, seedsize); |
| 259 | free(seedbuf); |
| 260 | } else |
| 261 | kcapi_memset_secure(buf, 0, sizeof(buf)); |
| 262 | _kcapi_handle_destroy(handle); |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | ORIG_SYMVER(rng_get_bytes, "1.0.0") |
| 267 | int32_t orig_rng_get_bytes(uint8_t *buffer, uint32_t outlen) |
| 268 | { |
| 269 | return (int32_t)impl_rng_get_bytes(buffer, outlen); |
| 270 | } |