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