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