xref: /openbmc/linux/include/crypto/if_alg.h (revision 6aa7de05)
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 <net/sock.h>
22 
23 #include <crypto/aead.h>
24 #include <crypto/skcipher.h>
25 
26 #define ALG_MAX_PAGES			16
27 
28 struct crypto_async_request;
29 
30 struct alg_sock {
31 	/* struct sock must be the first member of struct alg_sock */
32 	struct sock sk;
33 
34 	struct sock *parent;
35 
36 	unsigned int refcnt;
37 	unsigned int nokey_refcnt;
38 
39 	const struct af_alg_type *type;
40 	void *private;
41 };
42 
43 struct af_alg_completion {
44 	struct completion completion;
45 	int err;
46 };
47 
48 struct af_alg_control {
49 	struct af_alg_iv *iv;
50 	int op;
51 	unsigned int aead_assoclen;
52 };
53 
54 struct 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);
59 	int (*accept_nokey)(void *private, struct sock *sk);
60 	int (*setauthsize)(void *private, unsigned int authsize);
61 
62 	struct proto_ops *ops;
63 	struct proto_ops *ops_nokey;
64 	struct module *owner;
65 	char name[14];
66 };
67 
68 struct af_alg_sgl {
69 	struct scatterlist sg[ALG_MAX_PAGES + 1];
70 	struct page *pages[ALG_MAX_PAGES];
71 	unsigned int npages;
72 };
73 
74 /* TX SGL entry */
75 struct 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 */
85 struct 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  */
104 struct 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  */
149 struct 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 
167 int af_alg_register_type(const struct af_alg_type *type);
168 int af_alg_unregister_type(const struct af_alg_type *type);
169 
170 int af_alg_release(struct socket *sock);
171 void af_alg_release_parent(struct sock *sk);
172 int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern);
173 
174 int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len);
175 void af_alg_free_sg(struct af_alg_sgl *sgl);
176 void af_alg_link_sg(struct af_alg_sgl *sgl_prev, struct af_alg_sgl *sgl_new);
177 
178 int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con);
179 
180 int af_alg_wait_for_completion(int err, struct af_alg_completion *completion);
181 void af_alg_complete(struct crypto_async_request *req, int err);
182 
183 static inline struct alg_sock *alg_sk(struct sock *sk)
184 {
185 	return (struct alg_sock *)sk;
186 }
187 
188 static inline void af_alg_init_completion(struct af_alg_completion *completion)
189 {
190 	init_completion(&completion->completion);
191 }
192 
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  */
199 static 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  */
214 static 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  */
225 static 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  */
240 static inline bool af_alg_readable(struct sock *sk)
241 {
242 	return PAGE_SIZE <= af_alg_rcvbuf(sk);
243 }
244 
245 int af_alg_alloc_tsgl(struct sock *sk);
246 unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset);
247 void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
248 		      size_t dst_offset);
249 void af_alg_free_areq_sgls(struct af_alg_async_req *areq);
250 int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags);
251 void af_alg_wmem_wakeup(struct sock *sk);
252 int af_alg_wait_for_data(struct sock *sk, unsigned flags);
253 void af_alg_data_wakeup(struct sock *sk);
254 int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
255 		   unsigned int ivsize);
256 ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
257 			int offset, size_t size, int flags);
258 void af_alg_async_cb(struct crypto_async_request *_req, int err);
259 unsigned int af_alg_poll(struct file *file, struct socket *sock,
260 			 poll_table *wait);
261 struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
262 					   unsigned int areqlen);
263 int 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 
267 #endif	/* _CRYPTO_IF_ALG_H */
268