Debian Codegraph @

lib/internal.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2016 - 2022, Stephan Mueller <[email protected]>
3 *
4 * License: see COPYING file in root directory
5 *
6 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
7 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
9 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
10 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
12 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
13 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
14 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 */
19
20 #ifndef INTERNAL_H
21 #define INTERNAL_H
22
23 #define _GNU_SOURCE
24 #include <unistd.h>
25 #include <sys/syscall.h>
26 #include <linux/aio_abi.h>
27 #include <stdint.h>
28 #include <fcntl.h>
29 #include <sys/uio.h>
30 #include <errno.h>
31 #include <sys/socket.h>
32 #include <sys/types.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <limits.h>
36 #include <stdio.h>
37
38 #include <linux/if_alg.h>
39
40 #include "atomic.h"
41 #include "kcapi.h"
42
43 #ifdef __cplusplus
44 extern "C"
45 {
46 #endif
47
48 /* remove once in if_alg.h */
49 #ifndef ALG_SET_AEAD_ASSOCLEN
50 #define ALG_SET_AEAD_ASSOCLEN 4
51 #endif
52 #ifndef ALG_SET_AEAD_AUTHSIZE
53 #define ALG_SET_AEAD_AUTHSIZE 5
54 #endif
55 #ifndef ALG_SET_DRBG_ENTROPY
56 #define ALG_SET_DRBG_ENTROPY 6
57 #endif
58 #ifndef ALG_SET_PUBKEY
59 #define ALG_SET_PUBKEY 7
60 #endif
61 #ifndef ALG_SET_DH_PARAMETERS
62 #define ALG_SET_DH_PARAMETERS 8
63 #endif
64 #ifndef ALG_SET_ECDH_CURVE
65 #define ALG_SET_ECDH_CURVE 9
66 #endif
67
68
69 #ifndef ALG_OP_SIGN
70 #define ALG_OP_SIGN 2
71 #endif
72 #ifndef ALG_OP_VERIFY
73 #define ALG_OP_VERIFY 3
74 #endif
75
76 #ifndef ALG_OP_KEYGEN
77 #define ALG_OP_KEYGEN 4
78 #endif
79 #ifndef ALG_OP_SSGEN
80 #define ALG_OP_SSGEN 5
81 #endif
82
83 /* remove once in socket.h */
84 #ifndef AF_ALG
85 #define AF_ALG 38
86 #endif
87 #ifndef SOL_ALG
88 #define SOL_ALG 279
89 #endif
90
91 /* make sure that is equal to include/crypto/if_alg.h */
92 #ifndef ALG_MAX_PAGES
93 #define ALG_MAX_PAGES 16
94 #endif
95
96 /************************************************************
97 * Declarations for opaque data structures
98 ************************************************************/
99
100 /* Boolean variable */
101 enum { false, true };
102 typedef _Bool bool;
103
104 enum kcapi_cipher_type {
105 KCAPI_CIPHER_SKCIPHER,
106 KCAPI_CIPHER_AEAD,
107 KCAPI_CIPHER_KDF,
108 KCAPI_CIPHER_RNG,
109 KCAPI_CIPHER_MD,
110 KCAPI_CIPHER_KPP,
111 KCAPI_CIPHER_AKCIPHER,
112 };
113
114 /**
115 * Information obtained for different ciphers during handle init time
116 * using the NETLINK_CRYPTO interface.
117 * @blocksize block size of cipher (hash, symmetric, AEAD)
118 * @ivsize size of IV of cipher (symmetric, AEAD)
119 * @hash_digestsize size of message digest (hash)
120 * @blk_min_keysize minimum key size (symmetric)
121 * @blk_max_keysize maximum key size (symmetric)
122 * @aead_maxauthsize maximum authentication tag size (AEAD)
123 * @rng_seedsize seed size (RNG)
124 */
125 struct kcapi_cipher_info {
126 enum kcapi_cipher_type cipher_type;
127 /* generic */
128 uint32_t blocksize;
129 uint32_t ivsize;
130 /* hash */
131 uint32_t hash_digestsize;
132 /* blkcipher */
133 uint32_t blk_min_keysize;
134 uint32_t blk_max_keysize;
135 /* aead */
136 uint32_t aead_maxauthsize;
137 /* rng */
138 uint32_t rng_seedsize;
139 };
140
141 /**
142 * Common data required for symmetric and AEAD ciphers
143 * @iv: IV with length of kcapi_cipher_info->ivsize - input
144 */
145 struct kcapi_cipher_data {
146 const uint8_t *iv;
147 };
148
149 /**
150 * AEAD data
151 * @datalen: Length of plaintext / ciphertext data - input
152 * @data: Pointer to plaintext / ciphertext data - input / output (the length is
153 * calculated with: kcapi_skcipher_data->inlen -
154 * kcapi_aead_data->taglen - kcapi_aead_data->assoclen)
155 * @assoclen: Length of associated data - input
156 * @assoc: Pointer to associated data - input
157 * @taglen: Length of authentication tag - input
158 * @tag: Authentication tag - input for decryption, output for encryption
159 * @retlen: internal data -- number plaintext / ciphertext bytes returned by
160 * the read system call
161 */
162 struct kcapi_aead_data {
163 size_t datalen;
164 size_t assoclen;
165 size_t taglen;
166 uint8_t *data;
167 uint8_t *assoc;
168 uint8_t *tag;
169 };
170
171 /*
172 * This value sets the maximum number of concurrent AIO operations we support.
173 * This value can be changed as needed. However, note that the memory
174 * consumption of one cipher handle increases proportionally to this value. This
175 * means that during the _init API call processing, memory corresponding with
176 * this number must be allocated regardless whether it is used later on or not.
177 *
178 * If the caller supplies more IOVECs to be processed in parallel than this
179 * value, the libkcapi code below segments the the provided input data
180 * into IOVEC chunks of KCAPI_AIO_CONCURRENT size. Thus, the calling user
181 * will not see any difference when this value changes other than the
182 * performance impact during _init (the larger the value, the slower the
183 * _init processing) and later on during the cipher operations (the larger
184 * the value, the more parallel cipher operations are supported).
185 */
186 #define KCAPI_AIO_CONCURRENT 64
187
188 /**
189 * AIO related data structure to hold all information for AIO
190 * @skcipher_aio_disable: AIO support for symmetric ciphers not present
191 * @efd: event file descriptor
192 * @aio_ctx: AIO context to use for AIO syscalls
193 * @iocb_ret: return code of each iocb request - set to AIO_OUTSTANDING until
194 * a return code is obtained
195 * @cio: Active concurrent IOCBs
196 */
197 #define AIO_OUTSTANDING ((__s64)-(1<<13))
198 struct kcapi_aio {
199 bool disable;
200 int efd;
201 aio_context_t aio_ctx;
202 __s64 *iocb_ret;
203 struct iocb *cio;
204 struct iocb **ciopp;
205 };
206
207 struct kcapi_flags {
208 /*
209 * A flag to distinguish the new AEAD interface introduced with 4.9.0 to
210 * only require a tag if it is required as input or output.
211 *
212 * Also, kernels before 4.9.0 misbehave when no data is sent for hashing.
213 */
214 bool ge_v4_9;
215
216 /* AF_ALG interfaces changed to process more pages concurrently. */
217 uint32_t alg_max_pages;
218 };
219
220 struct kcapi_sys {
221 unsigned long kernel_maj, kernel_minor, kernel_patchlevel;
222 };
223
224 /**
225 * Kernel crypto API TFM representation - The entries are static after
226 * initialization.
227 * @tfmfd: Socket descriptor for AF_ALG
228 * @sysinfo: System information
229 * @info: properties of ciphers
230 * @refcnt: reference counter of data structure
231 */
232 struct kcapi_handle_tfm {
233 int tfmfd;
234 struct kcapi_sys sysinfo;
235 struct kcapi_cipher_info info;
236 atomic_t refcnt;
237 };
238
239 /**
240 * Cipher handle
241 * @tfm: kernel crypto API TFM
242 * @opfd: FD to open kernel crypto API TFM
243 * @pipes: vmplice/splice pipe pair
244 * @pagesize: Page size of the system
245 * @pipesize: Maximum amount of data that the pipe can handle
246 * @processed_sg: number of scatter/gather entries sent to the kernel
247 * @ciper: Common data for all ciphers
248 * @aead: AEAD cipher specific data
249 * @aio: AIO information
250 */
251 struct kcapi_handle {
252 struct kcapi_handle_tfm *tfm;
253 int pipes[2];
254 int opfd;
255 unsigned int pagesize;
256 unsigned int pipesize;
257 size_t processed_sg;
258 struct kcapi_cipher_data cipher;
259 struct kcapi_aead_data aead;
260 struct kcapi_aio aio;
261 struct kcapi_flags flags;
262 };
263
264 /************************************************************
265 * Declarations for internal functions
266 ************************************************************/
267
268 extern enum kcapi_verbosity kcapi_verbosity_level;
269 void kcapi_dolog(enum kcapi_verbosity severity, const char *fmt, ...);
270
271 static inline int *_kcapi_get_opfd(struct kcapi_handle *handle)
272 {
273 return &handle->opfd;
274 }
275
276 ssize_t _kcapi_common_send_meta(struct kcapi_handle *handle,
277 struct iovec *iov, size_t iovlen,
278 uint32_t enc, uint32_t flags);
279 ssize_t _kcapi_common_vmsplice_iov(struct kcapi_handle *handle,
280 struct iovec *iov, size_t iovlen,
281 uint32_t flags);
282 ssize_t _kcapi_common_send_data(struct kcapi_handle *handle,
283 struct iovec *iov, size_t iovlen,
284 uint32_t flags);
285 ssize_t _kcapi_common_recv_data(struct kcapi_handle *handle,
286 struct iovec *iov, size_t iovlen);
287 ssize_t _kcapi_common_read_data(struct kcapi_handle *handle,
288 uint8_t *out, size_t outlen);
289 int _kcapi_common_accept(struct kcapi_handle *handle);
290 int _kcapi_common_close(struct kcapi_handle *handle);
291 ssize_t _kcapi_common_vmsplice_chunk(struct kcapi_handle *handle,
292 const uint8_t *in, size_t inlen,
293 uint32_t flags);
294 int _kcapi_handle_init(struct kcapi_handle **caller, const char *type,
295 const char *ciphername, uint32_t flags);
296 void _kcapi_handle_destroy(struct kcapi_handle *handle);
297 int _kcapi_common_setkey(struct kcapi_handle *handle, const uint8_t *key,
298 uint32_t keylen);
299 int _kcapi_common_setentropy(struct kcapi_handle *handle, const uint8_t *ent,
300 uint32_t entlen);
301 ssize_t _kcapi_cipher_crypt(struct kcapi_handle *handle, const uint8_t *in,
302 size_t inlen, uint8_t *out, size_t outlen,
303 int access, unsigned int enc);
304 ssize_t _kcapi_cipher_crypt_chunk(struct kcapi_handle *handle,
305 const uint8_t *in, size_t inlen,
306 uint8_t *out, size_t outlen,
307 int access, unsigned int enc);
308 ssize_t _kcapi_cipher_crypt_aio(struct kcapi_handle *handle,
309 struct iovec *iniov, struct iovec *outiov,
310 size_t iovlen, int access, unsigned int enc);
311 int _kcapi_aio_send_iov(struct kcapi_handle *handle, struct iovec *iov,
312 size_t iovlen, int access, unsigned int enc);
313
314 int _kcapi_aio_read_iov(struct kcapi_handle *handle,
315 struct iovec *iov, size_t iovlen);
316 int32_t _kcapi_aio_read_all(struct kcapi_handle *handle, size_t toread,
317 struct timespec *timeout);
318
319 /************************************************************
320 * Declarations for system calls
321 ************************************************************/
322
323 static inline int io_setup(unsigned n, aio_context_t *ctx)
324 {
325 return (int)syscall(__NR_io_setup, n, ctx);
326 }
327
328 static inline int io_destroy(aio_context_t ctx)
329 {
330 return (int)syscall(__NR_io_destroy, ctx);
331 }
332
333 static inline int io_submit(aio_context_t ctx, long n, struct iocb **iocb)
334 {
335 return (int)syscall(__NR_io_submit, ctx, n, iocb);
336 }
337
338 static inline int io_getevents(__attribute__((unused)) aio_context_t ctx,
339 __attribute__((unused)) long min,
340 __attribute__((unused)) long max,
341 __attribute__((unused)) struct io_event *events,
342 __attribute__((unused)) struct timespec *timeout)
343 {
344 #ifdef __NR_io_getevents
345 return (int)syscall(__NR_io_getevents, ctx, min, max, events, timeout);
346 #else
347 return -ENOSYS;
348 #endif
349 }
350
351 /************************************************************
352 * Auxiliary macros
353 ************************************************************/
354
355 #if __GNUC__ >= 10
356 # define IMPL_SYMVER(name, version) \
357 __attribute__((__symver__("kcapi_" #name "@@LIBKCAPI_" version)))
358
359 # define ORIG_SYMVER(name, version) \
360 __attribute__((__symver__("kcapi_" #name "@LIBKCAPI_" version)))
361
362 #else
363 # define IMPL_SYMVER(name, version) \
364 __asm__(".symver impl_" #name ",kcapi_" #name "@@LIBKCAPI_" version);
365
366 # define ORIG_SYMVER(name, version) \
367 __asm__(".symver orig_" #name ",kcapi_" #name "@LIBKCAPI_" version);
368 #endif
369
370 #ifdef __cplusplus
371 }
372 #endif
373
374 #endif /* INTERNAL_H */