Linux 4.14.215
[linux/fpc-iii.git] / include / crypto / if_alg.h
blob2ea6a95ca825bcece2198b0ac34bb45ad9fba6cc
1 /*
2 * if_alg: User-space algorithm interface
4 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
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.
13 #ifndef _CRYPTO_IF_ALG_H
14 #define _CRYPTO_IF_ALG_H
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>
24 #include <crypto/aead.h>
25 #include <crypto/skcipher.h>
27 #define ALG_MAX_PAGES 16
29 struct crypto_async_request;
31 struct alg_sock {
32 /* struct sock must be the first member of struct alg_sock */
33 struct sock sk;
35 struct sock *parent;
37 atomic_t refcnt;
38 atomic_t nokey_refcnt;
40 const struct af_alg_type *type;
41 void *private;
44 struct af_alg_completion {
45 struct completion completion;
46 int err;
49 struct af_alg_control {
50 struct af_alg_iv *iv;
51 int op;
52 unsigned int aead_assoclen;
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);
63 struct proto_ops *ops;
64 struct proto_ops *ops_nokey;
65 struct module *owner;
66 char name[14];
69 struct af_alg_sgl {
70 struct scatterlist sg[ALG_MAX_PAGES + 1];
71 struct page *pages[ALG_MAX_PAGES];
72 unsigned int npages;
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 */
82 #define MAX_SGL_ENTS ((4096 - sizeof(struct af_alg_tsgl)) / \
83 sizeof(struct scatterlist) - 1)
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 */
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
105 struct af_alg_async_req {
106 struct kiocb *iocb;
107 struct sock *sk;
109 struct af_alg_rsgl first_rsgl;
110 struct af_alg_rsgl *last_rsgl;
111 struct list_head rsgl_list;
113 struct scatterlist *tsgl;
114 unsigned int tsgl_entries;
116 unsigned int outlen;
117 unsigned int areqlen;
119 union {
120 struct aead_request aead_req;
121 struct skcipher_request skcipher_req;
122 } cra_u;
124 /* req ctx trails this struct */
128 * struct af_alg_ctx - definition of the crypto context
130 * The crypto context tracks the input data during the lifetime of an AF_ALG
131 * socket.
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.
150 struct af_alg_ctx {
151 struct list_head tsgl_list;
153 void *iv;
154 size_t aead_assoclen;
156 struct af_alg_completion completion;
158 size_t used;
159 atomic_t rcvused;
161 bool more;
162 bool merge;
163 bool enc;
165 unsigned int len;
168 int af_alg_register_type(const struct af_alg_type *type);
169 int af_alg_unregister_type(const struct af_alg_type *type);
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);
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);
179 int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con);
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);
184 static inline struct alg_sock *alg_sk(struct sock *sk)
186 return (struct alg_sock *)sk;
189 static inline void af_alg_init_completion(struct af_alg_completion *completion)
191 init_completion(&completion->completion);
195 * Size of available buffer for sending data from user space to kernel.
197 * @sk socket of connection to user space
198 * @return number of bytes still available
200 static inline int af_alg_sndbuf(struct sock *sk)
202 struct alg_sock *ask = alg_sk(sk);
203 struct af_alg_ctx *ctx = ask->private;
205 return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
206 ctx->used, 0);
210 * Can the send buffer still be written to?
212 * @sk socket of connection to user space
213 * @return true => writable, false => not writable
215 static inline bool af_alg_writable(struct sock *sk)
217 return PAGE_SIZE <= af_alg_sndbuf(sk);
221 * Size of available buffer used by kernel for the RX user space operation.
223 * @sk socket of connection to user space
224 * @return number of bytes still available
226 static inline int af_alg_rcvbuf(struct sock *sk)
228 struct alg_sock *ask = alg_sk(sk);
229 struct af_alg_ctx *ctx = ask->private;
231 return max_t(int, max_t(int, sk->sk_rcvbuf & PAGE_MASK, PAGE_SIZE) -
232 atomic_read(&ctx->rcvused), 0);
236 * Can the RX buffer still be written to?
238 * @sk socket of connection to user space
239 * @return true => writable, false => not writable
241 static inline bool af_alg_readable(struct sock *sk)
243 return PAGE_SIZE <= af_alg_rcvbuf(sk);
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);
269 #endif /* _CRYPTO_IF_ALG_H */