Debian Codegraph @

lib/kcapi-utils.c

raw symbols SCIP
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"
22 #include "kcapi.h"
23
24 void kcapi_set_verbosity(enum kcapi_verbosity level)
25 {
26 kcapi_verbosity_level = level;
27 }
28
29 void kcapi_versionstring(char *buf, uint32_t buflen)
30 {
31 snprintf(buf, buflen, "libkcapi%s%d.%d.%.f",
32 (((uint32_t)KCAPI_PATCHLEVEL != KCAPI_PATCHLEVEL) ?
33 " pre-release " : " "),
34 KCAPI_MAJVERSION, KCAPI_MINVERSION, (double)KCAPI_PATCHLEVEL);
35 }
36
37 uint32_t kcapi_version(void)
38 {
39 uint32_t version = 0;
40
41 version = KCAPI_MAJVERSION * 1000000;
42 version += KCAPI_MINVERSION * 10000;
43 version += KCAPI_PATCHLEVEL * 100;
44
45 return version;
46 }
47
48 int kcapi_pad_iv(struct kcapi_handle *handle,
49 const uint8_t *iv, uint32_t ivlen,
50 uint8_t **newiv, uint32_t *newivlen)
51 {
52 uint8_t *niv = NULL;
53 struct kcapi_handle_tfm *tfm = handle->tfm;
54 uint32_t nivlen = tfm->info.ivsize;
55 uint32_t copylen = (ivlen > nivlen) ? nivlen : ivlen;
56 int ret = 0;
57
58 ret = posix_memalign((void *)&niv, 16, nivlen);
59 if (ret)
60 return -ret;
61 memcpy(niv, iv, copylen);
62 if (nivlen > copylen)
63 memset(niv + copylen, 0, nivlen - copylen);
64
65 *newiv = niv;
66 *newivlen = nivlen;
67
68 return 0;
69 }
70
71 int kcapi_set_maxsplicesize(struct kcapi_handle *handle, unsigned int size)
72 {
73 int ret;
74
75 if (!handle)
76 return -EINVAL;
77
78 ret = fcntl(handle->pipes[0], F_SETPIPE_SZ, size);
79 if (ret < 0)
80 goto err;
81
82 ret = fcntl(handle->pipes[1], F_SETPIPE_SZ, size);
83 if (ret < 0)
84 goto err;
85
86 handle->pipesize = (unsigned int)ret;
87
88 return 0;
89
90 err:
91 ret = -errno;
92 if (ret == -EBUSY) {
93 kcapi_dolog(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_dolog(KCAPI_LOG_WARN,
98 "AF_ALG: setting maximum splice pipe size to %u failed: %s",
99 size, strerror(ret));
100 }
101 return ret;
102 }
103
104 int kcapi_get_maxsplicesize(struct kcapi_handle *handle)
105 {
106 unsigned int pagesize = (unsigned int)sysconf(_SC_PAGESIZE);
107
108 if (!handle)
109 return -EINVAL;
110
111 /* Both pipe endpoints should have the same pipe size */
112 handle->pipesize = (unsigned int)fcntl(handle->pipes[0], F_GETPIPE_SZ);
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->pipesize > pagesize)
120 handle->pipesize -= pagesize;
121
122 /* TODO: what do we do for pipesize < pagesize? Can that even happen? */
123
124 return ((int)handle->pipesize);
125 }