1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * if_alg: User-space algorithm interface
5 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
8 #ifndef _CRYPTO_IF_ALG_H
9 #define _CRYPTO_IF_ALG_H
11 #include <linux/compiler.h>
12 #include <linux/completion.h>
13 #include <linux/if_alg.h>
14 #include <linux/scatterlist.h>
15 #include <linux/types.h>
16 #include <linux/atomic.h>
19 #include <crypto/aead.h>
20 #include <crypto/skcipher.h>
22 #define ALG_MAX_PAGES 16
25 /* struct sock must be the first member of struct alg_sock */
31 atomic_t nokey_refcnt;
33 const struct af_alg_type *type;
37 struct af_alg_control {
40 unsigned int aead_assoclen;
44 void *(*bind)(const char *name, u32 type, u32 mask);
45 void (*release)(void *private);
46 int (*setkey)(void *private, const u8 *key, unsigned int keylen);
47 int (*setentropy)(void *private, sockptr_t entropy, unsigned int len);
48 int (*accept)(void *private, struct sock *sk);
49 int (*accept_nokey)(void *private, struct sock *sk);
50 int (*setauthsize)(void *private, unsigned int authsize);
52 struct proto_ops *ops;
53 struct proto_ops *ops_nokey;
60 struct scatterlist sgl[ALG_MAX_PAGES + 1];
66 struct list_head list;
67 unsigned int cur; /* Last processed SG entry */
68 struct scatterlist sg[]; /* Array of SGs forming the SGL */
71 #define MAX_SGL_ENTS ((4096 - sizeof(struct af_alg_tsgl)) / \
72 sizeof(struct scatterlist) - 1)
76 struct af_alg_sgl sgl;
77 struct list_head list;
78 size_t sg_num_bytes; /* Bytes of data in that SGL */
82 * struct af_alg_async_req - definition of crypto request
83 * @iocb: IOCB for AIO operations
84 * @sk: Socket the request is associated with
85 * @first_rsgl: First RX SG
86 * @last_rsgl: Pointer to last RX SG
87 * @rsgl_list: Track RX SGs
88 * @tsgl: Private, per request TX SGL of buffers to process
89 * @tsgl_entries: Number of entries in priv. TX SGL
90 * @outlen: Number of output bytes generated by crypto op
91 * @areqlen: Length of this data structure
92 * @cra_u: Cipher request
94 struct af_alg_async_req {
98 struct af_alg_rsgl first_rsgl;
99 struct af_alg_rsgl *last_rsgl;
100 struct list_head rsgl_list;
102 struct scatterlist *tsgl;
103 unsigned int tsgl_entries;
106 unsigned int areqlen;
109 struct aead_request aead_req;
110 struct skcipher_request skcipher_req;
113 /* req ctx trails this struct */
117 * struct af_alg_ctx - definition of the crypto context
119 * The crypto context tracks the input data during the lifetime of an AF_ALG
122 * @tsgl_list: Link to TX SGL
123 * @iv: IV for cipher operation
124 * @aead_assoclen: Length of AAD for AEAD cipher operations
125 * @completion: Work queue for synchronous operation
126 * @used: TX bytes sent to kernel. This variable is used to
127 * ensure that user space cannot cause the kernel
128 * to allocate too much memory in sendmsg operation.
129 * @rcvused: Total RX bytes to be filled by kernel. This variable
130 * is used to ensure user space cannot cause the kernel
131 * to allocate too much memory in a recvmsg operation.
132 * @more: More data to be expected from user space?
133 * @merge: Shall new data from user space be merged into existing
135 * @enc: Cryptographic operation to be performed when
136 * recvmsg is invoked.
137 * @init: True if metadata has been sent.
138 * @len: Length of memory allocated for this data structure.
139 * @inflight: Non-zero when AIO requests are in flight.
142 struct list_head tsgl_list;
145 size_t aead_assoclen;
147 struct crypto_wait wait;
159 unsigned int inflight;
162 int af_alg_register_type(const struct af_alg_type *type);
163 int af_alg_unregister_type(const struct af_alg_type *type);
165 int af_alg_release(struct socket *sock);
166 void af_alg_release_parent(struct sock *sk);
167 int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern);
169 void af_alg_free_sg(struct af_alg_sgl *sgl);
171 static inline struct alg_sock *alg_sk(struct sock *sk)
173 return (struct alg_sock *)sk;
177 * Size of available buffer for sending data from user space to kernel.
179 * @sk socket of connection to user space
180 * @return number of bytes still available
182 static inline int af_alg_sndbuf(struct sock *sk)
184 struct alg_sock *ask = alg_sk(sk);
185 struct af_alg_ctx *ctx = ask->private;
187 return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
192 * Can the send buffer still be written to?
194 * @sk socket of connection to user space
195 * @return true => writable, false => not writable
197 static inline bool af_alg_writable(struct sock *sk)
199 return PAGE_SIZE <= af_alg_sndbuf(sk);
203 * Size of available buffer used by kernel for the RX user space operation.
205 * @sk socket of connection to user space
206 * @return number of bytes still available
208 static inline int af_alg_rcvbuf(struct sock *sk)
210 struct alg_sock *ask = alg_sk(sk);
211 struct af_alg_ctx *ctx = ask->private;
213 return max_t(int, max_t(int, sk->sk_rcvbuf & PAGE_MASK, PAGE_SIZE) -
214 atomic_read(&ctx->rcvused), 0);
218 * Can the RX buffer still be written to?
220 * @sk socket of connection to user space
221 * @return true => writable, false => not writable
223 static inline bool af_alg_readable(struct sock *sk)
225 return PAGE_SIZE <= af_alg_rcvbuf(sk);
228 unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset);
229 void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
231 void af_alg_wmem_wakeup(struct sock *sk);
232 int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min);
233 int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
234 unsigned int ivsize);
235 void af_alg_free_resources(struct af_alg_async_req *areq);
236 void af_alg_async_cb(void *data, int err);
237 __poll_t af_alg_poll(struct file *file, struct socket *sock,
239 struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
240 unsigned int areqlen);
241 int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
242 struct af_alg_async_req *areq, size_t maxsize,
245 #endif /* _CRYPTO_IF_ALG_H */