blob: 75ec9c662268bc6d0fcd1226116b4faa7469c0e5 [file] [log] [blame]
Herbert Xu03c8efc2010-10-19 21:12:39 +08001/*
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>
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000019#include <linux/scatterlist.h>
Herbert Xu03c8efc2010-10-19 21:12:39 +080020#include <linux/types.h>
21#include <net/sock.h>
22
Stephan Mueller2d975912017-08-02 07:56:19 +020023#include <crypto/aead.h>
24#include <crypto/skcipher.h>
25
Herbert Xu03c8efc2010-10-19 21:12:39 +080026#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
Herbert Xuc840ac62015-12-30 11:47:53 +080036 unsigned int refcnt;
Herbert Xu6a935172016-01-13 14:59:03 +080037 unsigned int nokey_refcnt;
Herbert Xuc840ac62015-12-30 11:47:53 +080038
Herbert Xu03c8efc2010-10-19 21:12:39 +080039 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;
Stephan Muelleraf8e8072014-12-03 20:55:42 +010051 unsigned int aead_assoclen;
Herbert Xu03c8efc2010-10-19 21:12:39 +080052};
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);
Herbert Xu37766582016-01-04 13:35:18 +090059 int (*accept_nokey)(void *private, struct sock *sk);
Stephan Mueller25fb8632014-12-07 23:21:42 +010060 int (*setauthsize)(void *private, unsigned int authsize);
Herbert Xu03c8efc2010-10-19 21:12:39 +080061
62 struct proto_ops *ops;
Herbert Xu37766582016-01-04 13:35:18 +090063 struct proto_ops *ops_nokey;
Herbert Xu03c8efc2010-10-19 21:12:39 +080064 struct module *owner;
65 char name[14];
66};
67
68struct af_alg_sgl {
Tadeusz Struk66db3732015-03-19 12:31:30 -070069 struct scatterlist sg[ALG_MAX_PAGES + 1];
Herbert Xu03c8efc2010-10-19 21:12:39 +080070 struct page *pages[ALG_MAX_PAGES];
Tadeusz Struk66db3732015-03-19 12:31:30 -070071 unsigned int npages;
Herbert Xu03c8efc2010-10-19 21:12:39 +080072};
73
Stephan Mueller2d975912017-08-02 07:56:19 +020074/* 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
Herbert Xu03c8efc2010-10-19 21:12:39 +0800167int 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);
Herbert Xuc840ac62015-12-30 11:47:53 +0800171void af_alg_release_parent(struct sock *sk);
David Howellscdfbabf2017-03-09 08:09:05 +0000172int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern);
Herbert Xu03c8efc2010-10-19 21:12:39 +0800173
Al Viro1d10eb22014-11-28 16:39:25 -0500174int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len);
Herbert Xu03c8efc2010-10-19 21:12:39 +0800175void af_alg_free_sg(struct af_alg_sgl *sgl);
Tadeusz Struk66db3732015-03-19 12:31:30 -0700176void af_alg_link_sg(struct af_alg_sgl *sgl_prev, struct af_alg_sgl *sgl_new);
Herbert Xu03c8efc2010-10-19 21:12:39 +0800177
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
Herbert Xu03c8efc2010-10-19 21:12:39 +0800188static inline void af_alg_init_completion(struct af_alg_completion *completion)
189{
190 init_completion(&completion->completion);
191}
192
Stephan Mueller2d975912017-08-02 07:56:19 +0200193/**
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
Herbert Xu03c8efc2010-10-19 21:12:39 +0800267#endif /* _CRYPTO_IF_ALG_H */