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