| 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_SOURCEcxx . . $ `lib/kcapi-rng.c:21:9`! |
| 22 | #include <unistd.h> |
| 23 | #include <linux/random.h> |
| 24 | #ifdef HAVE_GETRANDOMcxx . . $ `<built-in>:434:9`! |
| 25 | #include <sys/random.h> |
| 26 | #endif |
| 27 | |
| 28 | #include "internal.h"cxx . . $ `<file>/lib/internal.h`/ |
| 29 | #include "kcapi.h"cxx . . $ `<file>/lib/kcapi.h`/ |
| 30 | |
| 31 | int kcapi_rng_initcxx . . $ kcapi_rng_init(b54728dab28c239a).(struct kcapi_handlecxx . . $ kcapi_handle# **handle, const char *ciphername, |
| 32 | uint32_tcxx . . $ uint32_t# flags) |
| 33 | { |
| 34 | return _kcapi_handle_initcxx . . $ _kcapi_handle_init(db986b7fadb21840).(handle, "rng", ciphername, flags); |
| 35 | } |
| 36 | |
| 37 | void kcapi_rng_destroycxx . . $ kcapi_rng_destroy(aba29416d24fdd59).(struct kcapi_handlecxx . . $ kcapi_handle# *handle) |
| 38 | { |
| 39 | _kcapi_handle_destroycxx . . $ _kcapi_handle_destroy(aba29416d24fdd59).(handle); |
| 40 | } |
| 41 | |
| 42 | int kcapi_rng_seedcxx . . $ kcapi_rng_seed(1245f63d3d64d4aa).(struct kcapi_handlecxx . . $ kcapi_handle# *handle, uint8_tcxx . . $ uint8_t# *seed, |
| 43 | uint32_tcxx . . $ uint32_t# seedlen) |
| 44 | { |
| 45 | kcapi_dologcxx . . $ kcapi_dolog(5f46d8f16dd28dce).(KCAPI_LOG_VERBOSEcxx . . $ KCAPI_LOG_VERBOSE., "Seed DRNG with %u bytes of seed", |
| 46 | seedlen); |
| 47 | return _kcapi_common_setkeycxx . . $ _kcapi_common_setkey(4d0e261613a805ee).(handle, seed, seedlen); |
| 48 | } |
| 49 | |
| 50 | int kcapi_rng_setentropycxx . . $ kcapi_rng_setentropy(1245f63d3d64d4aa).(struct kcapi_handlecxx . . $ kcapi_handle# *handle, uint8_tcxx . . $ uint8_t# *ent, |
| 51 | uint32_tcxx . . $ uint32_t# entlen) |
| 52 | { |
| 53 | kcapi_dologcxx . . $ kcapi_dolog(5f46d8f16dd28dce).(KCAPI_LOG_VERBOSEcxx . . $ KCAPI_LOG_VERBOSE., |
| 54 | "Set %u bytes of initial entropy of DRBG", entlen); |
| 55 | return _kcapi_common_setentropycxx . . $ _kcapi_common_setentropy(4d0e261613a805ee).(handle, ent, entlen); |
| 56 | } |
| 57 | |
| 58 | int kcapi_rng_setaddtlcxx . . $ kcapi_rng_setaddtl(1245f63d3d64d4aa).(struct kcapi_handlecxx . . $ kcapi_handle# *handle, uint8_tcxx . . $ uint8_t# *addtl, |
| 59 | uint32_tcxx . . $ uint32_t# len) |
| 60 | { |
| 61 | ssize_tcxx . . $ ssize_t# ret = 0; |
| 62 | |
| 63 | kcapi_dologcxx . . $ kcapi_dolog(5f46d8f16dd28dce).(KCAPI_LOG_VERBOSEcxx . . $ KCAPI_LOG_VERBOSE., |
| 64 | "Set %u bytes of additional data for DRBG", len); |
| 65 | |
| 66 | if (len > INT_MAXcxx . . $ `<external>/67164224788392828/limits.h:120:9`!) |
| 67 | return -EMSGSIZEcxx . . $ `<external>/17922322188510340118/errno.h:74:9`!; |
| 68 | |
| 69 | ret = _kcapi_common_acceptcxx . . $ _kcapi_common_accept(cad780c06c088da8).(handle); |
| 70 | if (ret) |
| 71 | return (int)ret; |
| 72 | ret = sendcxx . . $ send(4e364047dcdcb5ca).(*_kcapi_get_opfdcxx . . $ _kcapi_get_opfd(f39930546eae134b).(handle), addtl, len, MSG_MOREcxx . . $ `<external>/8962547315054503424/socket.h:244:9`!); |
| 73 | |
| 74 | if (ret < 0) |
| 75 | return (int)ret; |
| 76 | if ((uint32_tcxx . . $ uint32_t#)ret < len) |
| 77 | return -EIOcxx . . $ `<external>/8987895939074593727/errno-base.h:9:9`!; |
| 78 | |
| 79 | handle->processed_sgcxx . . $ kcapi_handle#processed_sg. += 1; |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | IMPL_SYMVERcxx . . $ `lib/internal.h:363:10`!(rng_generate, "1.3.1") |
| 84 | ssize_tcxx . . $ ssize_t# impl_rng_generatecxx . . $ impl_rng_generate(e37e70272ed35378).(struct kcapi_handlecxx . . $ kcapi_handle# *handle, |
| 85 | uint8_tcxx . . $ uint8_t# *buffer, size_t len) |
| 86 | { |
| 87 | ssize_tcxx . . $ ssize_t# out = 0; |
| 88 | struct ioveccxx . . $ iovec# iov; |
| 89 | |
| 90 | while (len) { |
| 91 | ssize_tcxx . . $ ssize_t# r = 0; |
| 92 | |
| 93 | iov.iov_basecxx . . $ iovec#iov_base. = (void *)(uintptr_tcxx . . $ uintptr_t#)buffer; |
| 94 | iov.iov_lencxx . . $ iovec#iov_len. = len; |
| 95 | r = _kcapi_common_recv_datacxx . . $ _kcapi_common_recv_data(8a8f5b62e036cb66).(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_SYMVERcxx . . $ `lib/internal.h:366:10`!(rng_generate, "0.12.0") |
| 108 | int32_tcxx . . $ int32_t# orig_rng_generatecxx . . $ orig_rng_generate(1245f63d3d64d4aa).(struct kcapi_handlecxx . . $ kcapi_handle# *handle, |
| 109 | uint8_tcxx . . $ uint8_t# *buffer, uint32_tcxx . . $ uint32_t# len) |
| 110 | { |
| 111 | return (int32_tcxx . . $ int32_t#)impl_rng_generatecxx . . $ impl_rng_generate(e37e70272ed35378).(handle, buffer, len); |
| 112 | } |
| 113 | |
| 114 | uint32_tcxx . . $ uint32_t# kcapi_rng_seedsizecxx . . $ kcapi_rng_seedsize(994c7fcc3809be77).(struct kcapi_handlecxx . . $ kcapi_handle# *handle) |
| 115 | { |
| 116 | struct kcapi_handle_tfmcxx . . $ kcapi_handle_tfm# *tfm = handle->tfmcxx . . $ kcapi_handle#tfm.; |
| 117 | |
| 118 | return tfm->infocxx . . $ kcapi_handle_tfm#info..rng_seedsizecxx . . $ kcapi_cipher_info#rng_seedsize.; |
| 119 | } |
| 120 | |
| 121 | #if !defined(HAVE_GETRANDOMcxx . . $ `<built-in>:434:9`!) && !defined(__NR_getrandomcxx . . $ `<external>/1941951872834623984/unistd_64.h:322:9`!) |
| 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_randomcxx . . $ get_random(e44f9e08e4f6527d).(uint8_tcxx . . $ uint8_t# *buf, size_t buflen) |
| 139 | { |
| 140 | ssize_tcxx . . $ ssize_t# ret; |
| 141 | |
| 142 | if (buflen > INT_MAXcxx . . $ `<external>/67164224788392828/limits.h:120:9`!) |
| 143 | return 1; |
| 144 | |
| 145 | #if (!defined(HAVE_GETRANDOMcxx . . $ `<built-in>:434:9`!) && !defined(__NR_getrandomcxx . . $ `<external>/1941951872834623984/unistd_64.h:322:9`!)) |
| 146 | ret = open_random(); |
| 147 | if (ret) |
| 148 | return ret; |
| 149 | #endif |
| 150 | |
| 151 | do { |
| 152 | #ifdef HAVE_GETRANDOMcxx . . $ `<built-in>:434:9`! |
| 153 | ret = getrandomcxx . . $ getrandom(1f614d4b78d5005).(buf, buflen, 0); |
| 154 | kcapi_dologcxx . . $ kcapi_dolog(5f46d8f16dd28dce).(KCAPI_LOG_DEBUGcxx . . $ 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 || EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`! == errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! || ERESTARTcxx . . $ `<external>/17922322188510340118/errno.h:69:9`! == errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) |
| 172 | && buflen > 0); |
| 173 | |
| 174 | #if (!defined(HAVE_GETRANDOMcxx . . $ `<built-in>:434:9`!) && !defined(__NR_getrandomcxx . . $ `<external>/1941951872834623984/unistd_64.h:322:9`!)) |
| 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_BUFSIZEcxx . . $ `lib/kcapi-rng.c:184:9`! 128 |
| 185 | /* Minimum seed is 256 bits. */ |
| 186 | #define KCAPI_RNG_MINSEEDSIZEcxx . . $ `lib/kcapi-rng.c:186:9`! 32 |
| 187 | #define KCAPI_APP_ALIGNcxx . . $ `lib/kcapi-rng.c:187:9`! 8 |
| 188 | #define __alignedcxx . . $ `lib/kcapi-rng.c:188:9`!(x) __attribute__((aligned(x))) |
| 189 | |
| 190 | IMPL_SYMVERcxx . . $ `lib/internal.h:363:10`!(rng_get_bytes, "1.3.1") |
| 191 | ssize_tcxx . . $ ssize_t# impl_rng_get_bytescxx . . $ impl_rng_get_bytes(de74abb410f03b30).(uint8_tcxx . . $ uint8_t# *buffer, size_t outlen) |
| 192 | { |
| 193 | struct kcapi_handlecxx . . $ kcapi_handle# *handle; |
| 194 | uint8_tcxx . . $ uint8_t# buf[KCAPI_RNG_BUFSIZEcxx . . $ `lib/kcapi-rng.c:184:9`!] __alignedcxx . . $ `lib/kcapi-rng.c:188:9`!(KCAPI_APP_ALIGNcxx . . $ `lib/kcapi-rng.c:187:9`!); |
| 195 | uint8_tcxx . . $ uint8_t# *seedbuf = buf; |
| 196 | uint32_tcxx . . $ uint32_t# seedsize = 0; |
| 197 | size_t orig_outlen = outlen; |
| 198 | ssize_tcxx . . $ ssize_t# ret = _kcapi_handle_initcxx . . $ _kcapi_handle_init(db986b7fadb21840).(&handle, "rng", "stdrng", 0); |
| 199 | if (ret) |
| 200 | return ret; |
| 201 | |
| 202 | seedsize = kcapi_rng_seedsizecxx . . $ kcapi_rng_seedsize(994c7fcc3809be77).(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_MINSEEDSIZEcxx . . $ `lib/kcapi-rng.c:186:9`!) |
| 210 | seedsize = KCAPI_RNG_MINSEEDSIZEcxx . . $ `lib/kcapi-rng.c:186:9`!; |
| 211 | |
| 212 | /* |
| 213 | * Only allocate a new buffer if our buffer is |
| 214 | * insufficiently large. |
| 215 | */ |
| 216 | if (seedsize > KCAPI_RNG_BUFSIZEcxx . . $ `lib/kcapi-rng.c:184:9`!) { |
| 217 | seedbuf = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, seedsize); |
| 218 | if (!seedbuf) { |
| 219 | ret = -ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 220 | goto out; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | ret = get_randomcxx . . $ get_random(e44f9e08e4f6527d).(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_seedcxx . . $ kcapi_rng_seed(1245f63d3d64d4aa).(handle, seedbuf, seedsize); |
| 234 | if (ret) |
| 235 | goto out; |
| 236 | |
| 237 | while (outlen) { |
| 238 | size_t todo = (outlen < KCAPI_RNG_BUFSIZEcxx . . $ `lib/kcapi-rng.c:184:9`!) ? |
| 239 | outlen : KCAPI_RNG_BUFSIZEcxx . . $ `lib/kcapi-rng.c:184:9`!; |
| 240 | |
| 241 | ret = kcapi_rng_generatecxx . . $ kcapi_rng_generate(e37e70272ed35378).(handle, buffer, todo); |
| 242 | if (ret < 0) |
| 243 | goto out; |
| 244 | |
| 245 | if (ret == 0) { |
| 246 | ret = -EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!; |
| 247 | goto out; |
| 248 | } |
| 249 | outlen -= (size_t)ret; |
| 250 | buffer += ret; |
| 251 | } |
| 252 | |
| 253 | ret = (ssize_tcxx . . $ ssize_t#)orig_outlen; |
| 254 | |
| 255 | out: |
| 256 | /* Free seedbuf if it was allocated. */ |
| 257 | if (seedbuf && (seedbuf != buf)) { |
| 258 | kcapi_memset_securecxx . . $ kcapi_memset_secure(e37d57925061d5c9).(seedbuf, 0, seedsize); |
| 259 | freecxx . . $ free(5d22bdacc48458e8).(seedbuf); |
| 260 | } else |
| 261 | kcapi_memset_securecxx . . $ kcapi_memset_secure(e37d57925061d5c9).(buf, 0, sizeof(buf)); |
| 262 | _kcapi_handle_destroycxx . . $ _kcapi_handle_destroy(aba29416d24fdd59).(handle); |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | ORIG_SYMVERcxx . . $ `lib/internal.h:366:10`!(rng_get_bytes, "1.0.0") |
| 267 | int32_tcxx . . $ int32_t# orig_rng_get_bytescxx . . $ orig_rng_get_bytes(c271a33def0f2b11).(uint8_tcxx . . $ uint8_t# *buffer, uint32_tcxx . . $ uint32_t# outlen) |
| 268 | { |
| 269 | return (int32_tcxx . . $ int32_t#)impl_rng_get_bytescxx . . $ impl_rng_get_bytes(de74abb410f03b30).(buffer, outlen); |
| 270 | } |