1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * if_alg: User-space algorithm interface
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #ifndef _CRYPTO_IF_ALG_H
9*4882a593Smuzhiyun #define _CRYPTO_IF_ALG_H
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/compiler.h>
12*4882a593Smuzhiyun #include <linux/completion.h>
13*4882a593Smuzhiyun #include <linux/if_alg.h>
14*4882a593Smuzhiyun #include <linux/scatterlist.h>
15*4882a593Smuzhiyun #include <linux/types.h>
16*4882a593Smuzhiyun #include <linux/atomic.h>
17*4882a593Smuzhiyun #include <net/sock.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #include <crypto/aead.h>
20*4882a593Smuzhiyun #include <crypto/skcipher.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #define ALG_MAX_PAGES 16
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun struct crypto_async_request;
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun struct alg_sock {
27*4882a593Smuzhiyun /* struct sock must be the first member of struct alg_sock */
28*4882a593Smuzhiyun struct sock sk;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun struct sock *parent;
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun atomic_t refcnt;
33*4882a593Smuzhiyun atomic_t nokey_refcnt;
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun const struct af_alg_type *type;
36*4882a593Smuzhiyun void *private;
37*4882a593Smuzhiyun };
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun struct af_alg_control {
40*4882a593Smuzhiyun struct af_alg_iv *iv;
41*4882a593Smuzhiyun int op;
42*4882a593Smuzhiyun unsigned int aead_assoclen;
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun struct af_alg_type {
46*4882a593Smuzhiyun void *(*bind)(const char *name, u32 type, u32 mask);
47*4882a593Smuzhiyun void (*release)(void *private);
48*4882a593Smuzhiyun int (*setkey)(void *private, const u8 *key, unsigned int keylen);
49*4882a593Smuzhiyun int (*setentropy)(void *private, sockptr_t entropy, unsigned int len);
50*4882a593Smuzhiyun int (*accept)(void *private, struct sock *sk);
51*4882a593Smuzhiyun int (*accept_nokey)(void *private, struct sock *sk);
52*4882a593Smuzhiyun int (*setauthsize)(void *private, unsigned int authsize);
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun struct proto_ops *ops;
55*4882a593Smuzhiyun struct proto_ops *ops_nokey;
56*4882a593Smuzhiyun struct module *owner;
57*4882a593Smuzhiyun char name[14];
58*4882a593Smuzhiyun };
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun struct af_alg_sgl {
61*4882a593Smuzhiyun struct scatterlist sg[ALG_MAX_PAGES + 1];
62*4882a593Smuzhiyun struct page *pages[ALG_MAX_PAGES];
63*4882a593Smuzhiyun unsigned int npages;
64*4882a593Smuzhiyun };
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /* TX SGL entry */
67*4882a593Smuzhiyun struct af_alg_tsgl {
68*4882a593Smuzhiyun struct list_head list;
69*4882a593Smuzhiyun unsigned int cur; /* Last processed SG entry */
70*4882a593Smuzhiyun struct scatterlist sg[]; /* Array of SGs forming the SGL */
71*4882a593Smuzhiyun };
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun #define MAX_SGL_ENTS ((4096 - sizeof(struct af_alg_tsgl)) / \
74*4882a593Smuzhiyun sizeof(struct scatterlist) - 1)
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun /* RX SGL entry */
77*4882a593Smuzhiyun struct af_alg_rsgl {
78*4882a593Smuzhiyun struct af_alg_sgl sgl;
79*4882a593Smuzhiyun struct list_head list;
80*4882a593Smuzhiyun size_t sg_num_bytes; /* Bytes of data in that SGL */
81*4882a593Smuzhiyun };
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /**
84*4882a593Smuzhiyun * struct af_alg_async_req - definition of crypto request
85*4882a593Smuzhiyun * @iocb: IOCB for AIO operations
86*4882a593Smuzhiyun * @sk: Socket the request is associated with
87*4882a593Smuzhiyun * @first_rsgl: First RX SG
88*4882a593Smuzhiyun * @last_rsgl: Pointer to last RX SG
89*4882a593Smuzhiyun * @rsgl_list: Track RX SGs
90*4882a593Smuzhiyun * @tsgl: Private, per request TX SGL of buffers to process
91*4882a593Smuzhiyun * @tsgl_entries: Number of entries in priv. TX SGL
92*4882a593Smuzhiyun * @outlen: Number of output bytes generated by crypto op
93*4882a593Smuzhiyun * @areqlen: Length of this data structure
94*4882a593Smuzhiyun * @cra_u: Cipher request
95*4882a593Smuzhiyun */
96*4882a593Smuzhiyun struct af_alg_async_req {
97*4882a593Smuzhiyun struct kiocb *iocb;
98*4882a593Smuzhiyun struct sock *sk;
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun struct af_alg_rsgl first_rsgl;
101*4882a593Smuzhiyun struct af_alg_rsgl *last_rsgl;
102*4882a593Smuzhiyun struct list_head rsgl_list;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun struct scatterlist *tsgl;
105*4882a593Smuzhiyun unsigned int tsgl_entries;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun unsigned int outlen;
108*4882a593Smuzhiyun unsigned int areqlen;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun union {
111*4882a593Smuzhiyun struct aead_request aead_req;
112*4882a593Smuzhiyun struct skcipher_request skcipher_req;
113*4882a593Smuzhiyun } cra_u;
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun /* req ctx trails this struct */
116*4882a593Smuzhiyun };
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun /**
119*4882a593Smuzhiyun * struct af_alg_ctx - definition of the crypto context
120*4882a593Smuzhiyun *
121*4882a593Smuzhiyun * The crypto context tracks the input data during the lifetime of an AF_ALG
122*4882a593Smuzhiyun * socket.
123*4882a593Smuzhiyun *
124*4882a593Smuzhiyun * @tsgl_list: Link to TX SGL
125*4882a593Smuzhiyun * @iv: IV for cipher operation
126*4882a593Smuzhiyun * @aead_assoclen: Length of AAD for AEAD cipher operations
127*4882a593Smuzhiyun * @completion: Work queue for synchronous operation
128*4882a593Smuzhiyun * @used: TX bytes sent to kernel. This variable is used to
129*4882a593Smuzhiyun * ensure that user space cannot cause the kernel
130*4882a593Smuzhiyun * to allocate too much memory in sendmsg operation.
131*4882a593Smuzhiyun * @rcvused: Total RX bytes to be filled by kernel. This variable
132*4882a593Smuzhiyun * is used to ensure user space cannot cause the kernel
133*4882a593Smuzhiyun * to allocate too much memory in a recvmsg operation.
134*4882a593Smuzhiyun * @more: More data to be expected from user space?
135*4882a593Smuzhiyun * @merge: Shall new data from user space be merged into existing
136*4882a593Smuzhiyun * SG?
137*4882a593Smuzhiyun * @enc: Cryptographic operation to be performed when
138*4882a593Smuzhiyun * recvmsg is invoked.
139*4882a593Smuzhiyun * @init: True if metadata has been sent.
140*4882a593Smuzhiyun * @len: Length of memory allocated for this data structure.
141*4882a593Smuzhiyun */
142*4882a593Smuzhiyun struct af_alg_ctx {
143*4882a593Smuzhiyun struct list_head tsgl_list;
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun void *iv;
146*4882a593Smuzhiyun size_t aead_assoclen;
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun struct crypto_wait wait;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun size_t used;
151*4882a593Smuzhiyun atomic_t rcvused;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun bool more;
154*4882a593Smuzhiyun bool merge;
155*4882a593Smuzhiyun bool enc;
156*4882a593Smuzhiyun bool init;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun unsigned int len;
159*4882a593Smuzhiyun };
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun int af_alg_register_type(const struct af_alg_type *type);
162*4882a593Smuzhiyun int af_alg_unregister_type(const struct af_alg_type *type);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun int af_alg_release(struct socket *sock);
165*4882a593Smuzhiyun void af_alg_release_parent(struct sock *sk);
166*4882a593Smuzhiyun int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern);
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len);
169*4882a593Smuzhiyun void af_alg_free_sg(struct af_alg_sgl *sgl);
170*4882a593Smuzhiyun
alg_sk(struct sock * sk)171*4882a593Smuzhiyun static inline struct alg_sock *alg_sk(struct sock *sk)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun return (struct alg_sock *)sk;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun /**
177*4882a593Smuzhiyun * Size of available buffer for sending data from user space to kernel.
178*4882a593Smuzhiyun *
179*4882a593Smuzhiyun * @sk socket of connection to user space
180*4882a593Smuzhiyun * @return number of bytes still available
181*4882a593Smuzhiyun */
af_alg_sndbuf(struct sock * sk)182*4882a593Smuzhiyun static inline int af_alg_sndbuf(struct sock *sk)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun struct alg_sock *ask = alg_sk(sk);
185*4882a593Smuzhiyun struct af_alg_ctx *ctx = ask->private;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
188*4882a593Smuzhiyun ctx->used, 0);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun /**
192*4882a593Smuzhiyun * Can the send buffer still be written to?
193*4882a593Smuzhiyun *
194*4882a593Smuzhiyun * @sk socket of connection to user space
195*4882a593Smuzhiyun * @return true => writable, false => not writable
196*4882a593Smuzhiyun */
af_alg_writable(struct sock * sk)197*4882a593Smuzhiyun static inline bool af_alg_writable(struct sock *sk)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun return PAGE_SIZE <= af_alg_sndbuf(sk);
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun /**
203*4882a593Smuzhiyun * Size of available buffer used by kernel for the RX user space operation.
204*4882a593Smuzhiyun *
205*4882a593Smuzhiyun * @sk socket of connection to user space
206*4882a593Smuzhiyun * @return number of bytes still available
207*4882a593Smuzhiyun */
af_alg_rcvbuf(struct sock * sk)208*4882a593Smuzhiyun static inline int af_alg_rcvbuf(struct sock *sk)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun struct alg_sock *ask = alg_sk(sk);
211*4882a593Smuzhiyun struct af_alg_ctx *ctx = ask->private;
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun return max_t(int, max_t(int, sk->sk_rcvbuf & PAGE_MASK, PAGE_SIZE) -
214*4882a593Smuzhiyun atomic_read(&ctx->rcvused), 0);
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun /**
218*4882a593Smuzhiyun * Can the RX buffer still be written to?
219*4882a593Smuzhiyun *
220*4882a593Smuzhiyun * @sk socket of connection to user space
221*4882a593Smuzhiyun * @return true => writable, false => not writable
222*4882a593Smuzhiyun */
af_alg_readable(struct sock * sk)223*4882a593Smuzhiyun static inline bool af_alg_readable(struct sock *sk)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun return PAGE_SIZE <= af_alg_rcvbuf(sk);
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset);
229*4882a593Smuzhiyun void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
230*4882a593Smuzhiyun size_t dst_offset);
231*4882a593Smuzhiyun void af_alg_wmem_wakeup(struct sock *sk);
232*4882a593Smuzhiyun int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min);
233*4882a593Smuzhiyun int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
234*4882a593Smuzhiyun unsigned int ivsize);
235*4882a593Smuzhiyun ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
236*4882a593Smuzhiyun int offset, size_t size, int flags);
237*4882a593Smuzhiyun void af_alg_free_resources(struct af_alg_async_req *areq);
238*4882a593Smuzhiyun void af_alg_async_cb(struct crypto_async_request *_req, int err);
239*4882a593Smuzhiyun __poll_t af_alg_poll(struct file *file, struct socket *sock,
240*4882a593Smuzhiyun poll_table *wait);
241*4882a593Smuzhiyun struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
242*4882a593Smuzhiyun unsigned int areqlen);
243*4882a593Smuzhiyun int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
244*4882a593Smuzhiyun struct af_alg_async_req *areq, size_t maxsize,
245*4882a593Smuzhiyun size_t *outlen);
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun #endif /* _CRYPTO_IF_ALG_H */
248