| 1 | /* libkcapi Utilities 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 | #include "internal.h"cxx . . $ `<file>/lib/internal.h`/ |
| 22 | #include "kcapi.h"cxx . . $ `<file>/lib/kcapi.h`/ |
| 23 | |
| 24 | void kcapi_set_verbositycxx . . $ kcapi_set_verbosity(dcea45016c0305c8).(enum kcapi_verbositycxx . . $ kcapi_verbosity# level) |
| 25 | { |
| 26 | kcapi_verbosity_levelcxx . . $ kcapi_verbosity_level. = level; |
| 27 | } |
| 28 | |
| 29 | void kcapi_versionstringcxx . . $ kcapi_versionstring(2ae3250752bf8d86).(char *buf, uint32_tcxx . . $ uint32_t# buflen) |
| 30 | { |
| 31 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, buflen, "libkcapi%s%d.%d.%.f", |
| 32 | (((uint32_tcxx . . $ uint32_t#)KCAPI_PATCHLEVELcxx . . $ `<built-in>:419:9`! != KCAPI_PATCHLEVELcxx . . $ `<built-in>:419:9`!) ? |
| 33 | " pre-release " : " "), |
| 34 | KCAPI_MAJVERSIONcxx . . $ `<built-in>:417:9`!, KCAPI_MINVERSIONcxx . . $ `<built-in>:418:9`!, (double)KCAPI_PATCHLEVELcxx . . $ `<built-in>:419:9`!); |
| 35 | } |
| 36 | |
| 37 | uint32_tcxx . . $ uint32_t# kcapi_versioncxx . . $ kcapi_version(db52760e79bf9054).(void) |
| 38 | { |
| 39 | uint32_tcxx . . $ uint32_t# version = 0; |
| 40 | |
| 41 | version = KCAPI_MAJVERSIONcxx . . $ `<built-in>:417:9`! * 1000000; |
| 42 | version += KCAPI_MINVERSIONcxx . . $ `<built-in>:418:9`! * 10000; |
| 43 | version += KCAPI_PATCHLEVELcxx . . $ `<built-in>:419:9`! * 100; |
| 44 | |
| 45 | return version; |
| 46 | } |
| 47 | |
| 48 | int kcapi_pad_ivcxx . . $ kcapi_pad_iv(cc7eda497594f08e).(struct kcapi_handlecxx . . $ kcapi_handle# *handle, |
| 49 | const uint8_tcxx . . $ uint8_t# *iv, uint32_tcxx . . $ uint32_t# ivlen, |
| 50 | uint8_tcxx . . $ uint8_t# **newiv, uint32_tcxx . . $ uint32_t# *newivlen) |
| 51 | { |
| 52 | uint8_tcxx . . $ uint8_t# *niv = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 53 | struct kcapi_handle_tfmcxx . . $ kcapi_handle_tfm# *tfm = handle->tfmcxx . . $ kcapi_handle#tfm.; |
| 54 | uint32_tcxx . . $ uint32_t# nivlen = tfm->infocxx . . $ kcapi_handle_tfm#info..ivsizecxx . . $ kcapi_cipher_info#ivsize.; |
| 55 | uint32_tcxx . . $ uint32_t# copylen = (ivlen > nivlen) ? nivlen : ivlen; |
| 56 | int ret = 0; |
| 57 | |
| 58 | ret = posix_memaligncxx . . $ posix_memalign(e974a8a87fa206f3).((void *)&niv, 16, nivlen); |
| 59 | if (ret) |
| 60 | return -ret; |
| 61 | memcpycxx . . $ memcpy(cc817cb175f282d).(niv, iv, copylen); |
| 62 | if (nivlen > copylen) |
| 63 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(niv + copylen, 0, nivlen - copylen); |
| 64 | |
| 65 | *newiv = niv; |
| 66 | *newivlen = nivlen; |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int kcapi_set_maxsplicesizecxx . . $ kcapi_set_maxsplicesize(8327ae3352751b56).(struct kcapi_handlecxx . . $ kcapi_handle# *handle, unsigned int size) |
| 72 | { |
| 73 | int ret; |
| 74 | |
| 75 | if (!handle) |
| 76 | return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 77 | |
| 78 | ret = fcntlcxx . . $ fcntl(cac4efb70f20a0fb).(handle->pipescxx . . $ kcapi_handle#pipes.[0], F_SETPIPE_SZcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:203:10`!, size); |
| 79 | if (ret < 0) |
| 80 | goto err; |
| 81 | |
| 82 | ret = fcntlcxx . . $ fcntl(cac4efb70f20a0fb).(handle->pipescxx . . $ kcapi_handle#pipes.[1], F_SETPIPE_SZcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:203:10`!, size); |
| 83 | if (ret < 0) |
| 84 | goto err; |
| 85 | |
| 86 | handle->pipesizecxx . . $ kcapi_handle#pipesize. = (unsigned int)ret; |
| 87 | |
| 88 | return 0; |
| 89 | |
| 90 | err: |
| 91 | ret = -errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!; |
| 92 | if (ret == -EBUSYcxx . . $ `<external>/8987895939074593727/errno-base.h:20:9`!) { |
| 93 | kcapi_dologcxx . . $ kcapi_dolog(5f46d8f16dd28dce).(KCAPI_LOG_WARNcxx . . $ KCAPI_LOG_WARN., |
| 94 | "AF_ALG: setting maximum splice pipe size to %u failed - it would exceed maximum quota", |
| 95 | size); |
| 96 | } else { |
| 97 | kcapi_dologcxx . . $ kcapi_dolog(5f46d8f16dd28dce).(KCAPI_LOG_WARNcxx . . $ KCAPI_LOG_WARN., |
| 98 | "AF_ALG: setting maximum splice pipe size to %u failed: %s", |
| 99 | size, strerrorcxx . . $ strerror(ff224f080444ee45).(ret)); |
| 100 | } |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | int kcapi_get_maxsplicesizecxx . . $ kcapi_get_maxsplicesize(cad780c06c088da8).(struct kcapi_handlecxx . . $ kcapi_handle# *handle) |
| 105 | { |
| 106 | unsigned int pagesize = (unsigned int)sysconfcxx . . $ sysconf(dc1f09c09fbff20f).(_SC_PAGESIZEcxx . . $ `<external>/6393649343499073842/confname.h:134:9`!); |
| 107 | |
| 108 | if (!handle) |
| 109 | return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 110 | |
| 111 | /* Both pipe endpoints should have the same pipe size */ |
| 112 | handle->pipesizecxx . . $ kcapi_handle#pipesize. = (unsigned int)fcntlcxx . . $ fcntl(cac4efb70f20a0fb).(handle->pipescxx . . $ kcapi_handle#pipes.[0], F_GETPIPE_SZcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:204:10`!); |
| 113 | |
| 114 | /* |
| 115 | * For vmsplice to allow the maximum number of 16 pages, we need to |
| 116 | * increase the pipe buffer by one more page - it seems the kernel |
| 117 | * uses some parts of the pipe for some house-keeping?! |
| 118 | */ |
| 119 | if (handle->pipesizecxx . . $ kcapi_handle#pipesize. > pagesize) |
| 120 | handle->pipesizecxx . . $ kcapi_handle#pipesize. -= pagesize; |
| 121 | |
| 122 | /* TODO: what do we do for pipesize < pagesize? Can that even happen? */ |
| 123 | |
| 124 | return ((int)handle->pipesizecxx . . $ kcapi_handle#pipesize.); |
| 125 | } |