blob: 6abf0a3604dc391c3218a063473c737be662aa8a [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
Herbert Xu03c8efc2010-10-19 21:12:39 +080043struct af_alg_control {
44 struct af_alg_iv *iv;
45 int op;
Stephan Muelleraf8e8072014-12-03 20:55:42 +010046 unsigned int aead_assoclen;
Herbert Xu03c8efc2010-10-19 21:12:39 +080047};
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);
Herbert Xu37766582016-01-04 13:35:18 +090054 int (*accept_nokey)(void *private, struct sock *sk);
Stephan Mueller25fb8632014-12-07 23:21:42 +010055 int (*setauthsize)(void *private, unsigned int authsize);
Herbert Xu03c8efc2010-10-19 21:12:39 +080056
57 struct proto_ops *ops;
Herbert Xu37766582016-01-04 13:35:18 +090058 struct proto_ops *ops_nokey;
Herbert Xu03c8efc2010-10-19 21:12:39 +080059 struct module *owner;
60 char name[14];
61};
62
63struct af_alg_sgl {
Tadeusz Struk66db3732015-03-19 12:31:30 -070064 struct scatterlist sg[ALG_MAX_PAGES + 1];
Herbert Xu03c8efc2010-10-19 21:12:39 +080065 struct page *pages[ALG_MAX_PAGES];
Tadeusz Struk66db3732015-03-19 12:31:30 -070066 unsigned int npages;
Herbert Xu03c8efc2010-10-19 21:12:39 +080067};
68
Stephan Mueller2d975912017-08-02 07:56:19 +020069/* 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
Gilad Ben-Yossef2c3f8b12017-10-18 08:00:39 +0100150 struct crypto_wait wait;
Stephan Mueller2d975912017-08-02 07:56:19 +0200151
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
Herbert Xu03c8efc2010-10-19 21:12:39 +0800162int 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);
Herbert Xuc840ac62015-12-30 11:47:53 +0800166void af_alg_release_parent(struct sock *sk);
David Howellscdfbabf2017-03-09 08:09:05 +0000167int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern);
Herbert Xu03c8efc2010-10-19 21:12:39 +0800168
Al Viro1d10eb22014-11-28 16:39:25 -0500169int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len);
Herbert Xu03c8efc2010-10-19 21:12:39 +0800170void af_alg_free_sg(struct af_alg_sgl *sgl);
Tadeusz Struk66db3732015-03-19 12:31:30 -0700171void af_alg_link_sg(struct af_alg_sgl *sgl_prev, struct af_alg_sgl *sgl_new);
Herbert Xu03c8efc2010-10-19 21:12:39 +0800172
173int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con);
174
Herbert Xu03c8efc2010-10-19 21:12:39 +0800175static inline struct alg_sock *alg_sk(struct sock *sk)
176{
177 return (struct alg_sock *)sk;
178}
179
Stephan Mueller2d975912017-08-02 07:56:19 +0200180/**
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
Herbert Xu03c8efc2010-10-19 21:12:39 +0800254#endif /* _CRYPTO_IF_ALG_H */