Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 2 | /* |
| 3 | * if_alg: User-space algorithm interface |
| 4 | * |
| 5 | * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au> |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef _CRYPTO_IF_ALG_H |
| 9 | #define _CRYPTO_IF_ALG_H |
| 10 | |
| 11 | #include <linux/compiler.h> |
| 12 | #include <linux/completion.h> |
| 13 | #include <linux/if_alg.h> |
Alexey Dobriyan | b7f080c | 2011-06-16 11:01:34 +0000 | [diff] [blame] | 14 | #include <linux/scatterlist.h> |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 15 | #include <linux/types.h> |
Jonathan Cameron | af955bf | 2017-12-19 10:27:24 +0000 | [diff] [blame] | 16 | #include <linux/atomic.h> |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 17 | #include <net/sock.h> |
| 18 | |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 19 | #include <crypto/aead.h> |
| 20 | #include <crypto/skcipher.h> |
| 21 | |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 22 | #define ALG_MAX_PAGES 16 |
| 23 | |
| 24 | struct crypto_async_request; |
| 25 | |
| 26 | struct alg_sock { |
| 27 | /* struct sock must be the first member of struct alg_sock */ |
| 28 | struct sock sk; |
| 29 | |
| 30 | struct sock *parent; |
| 31 | |
Herbert Xu | 34c86f4 | 2020-06-08 16:48:43 +1000 | [diff] [blame] | 32 | atomic_t refcnt; |
| 33 | atomic_t nokey_refcnt; |
Herbert Xu | c840ac6 | 2015-12-30 11:47:53 +0800 | [diff] [blame] | 34 | |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 35 | const struct af_alg_type *type; |
| 36 | void *private; |
| 37 | }; |
| 38 | |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 39 | struct af_alg_control { |
| 40 | struct af_alg_iv *iv; |
| 41 | int op; |
Stephan Mueller | af8e807 | 2014-12-03 20:55:42 +0100 | [diff] [blame] | 42 | unsigned int aead_assoclen; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | struct af_alg_type { |
| 46 | void *(*bind)(const char *name, u32 type, u32 mask); |
| 47 | void (*release)(void *private); |
| 48 | int (*setkey)(void *private, const u8 *key, unsigned int keylen); |
Elena Petrova | 77ebdab | 2020-09-18 16:42:16 +0100 | [diff] [blame] | 49 | int (*setentropy)(void *private, sockptr_t entropy, unsigned int len); |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 50 | int (*accept)(void *private, struct sock *sk); |
Herbert Xu | 3776658 | 2016-01-04 13:35:18 +0900 | [diff] [blame] | 51 | int (*accept_nokey)(void *private, struct sock *sk); |
Stephan Mueller | 25fb863 | 2014-12-07 23:21:42 +0100 | [diff] [blame] | 52 | int (*setauthsize)(void *private, unsigned int authsize); |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 53 | |
| 54 | struct proto_ops *ops; |
Herbert Xu | 3776658 | 2016-01-04 13:35:18 +0900 | [diff] [blame] | 55 | struct proto_ops *ops_nokey; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 56 | struct module *owner; |
| 57 | char name[14]; |
| 58 | }; |
| 59 | |
| 60 | struct af_alg_sgl { |
Tadeusz Struk | 66db373 | 2015-03-19 12:31:30 -0700 | [diff] [blame] | 61 | struct scatterlist sg[ALG_MAX_PAGES + 1]; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 62 | struct page *pages[ALG_MAX_PAGES]; |
Tadeusz Struk | 66db373 | 2015-03-19 12:31:30 -0700 | [diff] [blame] | 63 | unsigned int npages; |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 64 | }; |
| 65 | |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 66 | /* TX SGL entry */ |
| 67 | struct af_alg_tsgl { |
| 68 | struct list_head list; |
| 69 | unsigned int cur; /* Last processed SG entry */ |
Gustavo A. R. Silva | 5a8a076 | 2020-02-24 10:21:00 -0600 | [diff] [blame] | 70 | struct scatterlist sg[]; /* Array of SGs forming the SGL */ |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | #define MAX_SGL_ENTS ((4096 - sizeof(struct af_alg_tsgl)) / \ |
| 74 | sizeof(struct scatterlist) - 1) |
| 75 | |
| 76 | /* RX SGL entry */ |
| 77 | struct af_alg_rsgl { |
| 78 | struct af_alg_sgl sgl; |
| 79 | struct list_head list; |
| 80 | size_t sg_num_bytes; /* Bytes of data in that SGL */ |
| 81 | }; |
| 82 | |
| 83 | /** |
| 84 | * struct af_alg_async_req - definition of crypto request |
| 85 | * @iocb: IOCB for AIO operations |
| 86 | * @sk: Socket the request is associated with |
| 87 | * @first_rsgl: First RX SG |
| 88 | * @last_rsgl: Pointer to last RX SG |
| 89 | * @rsgl_list: Track RX SGs |
| 90 | * @tsgl: Private, per request TX SGL of buffers to process |
| 91 | * @tsgl_entries: Number of entries in priv. TX SGL |
| 92 | * @outlen: Number of output bytes generated by crypto op |
| 93 | * @areqlen: Length of this data structure |
| 94 | * @cra_u: Cipher request |
| 95 | */ |
| 96 | struct af_alg_async_req { |
| 97 | struct kiocb *iocb; |
| 98 | struct sock *sk; |
| 99 | |
| 100 | struct af_alg_rsgl first_rsgl; |
| 101 | struct af_alg_rsgl *last_rsgl; |
| 102 | struct list_head rsgl_list; |
| 103 | |
| 104 | struct scatterlist *tsgl; |
| 105 | unsigned int tsgl_entries; |
| 106 | |
| 107 | unsigned int outlen; |
| 108 | unsigned int areqlen; |
| 109 | |
| 110 | union { |
| 111 | struct aead_request aead_req; |
| 112 | struct skcipher_request skcipher_req; |
| 113 | } cra_u; |
| 114 | |
| 115 | /* req ctx trails this struct */ |
| 116 | }; |
| 117 | |
| 118 | /** |
| 119 | * struct af_alg_ctx - definition of the crypto context |
| 120 | * |
| 121 | * The crypto context tracks the input data during the lifetime of an AF_ALG |
| 122 | * socket. |
| 123 | * |
| 124 | * @tsgl_list: Link to TX SGL |
| 125 | * @iv: IV for cipher operation |
| 126 | * @aead_assoclen: Length of AAD for AEAD cipher operations |
| 127 | * @completion: Work queue for synchronous operation |
| 128 | * @used: TX bytes sent to kernel. This variable is used to |
| 129 | * ensure that user space cannot cause the kernel |
| 130 | * to allocate too much memory in sendmsg operation. |
| 131 | * @rcvused: Total RX bytes to be filled by kernel. This variable |
| 132 | * is used to ensure user space cannot cause the kernel |
| 133 | * to allocate too much memory in a recvmsg operation. |
| 134 | * @more: More data to be expected from user space? |
| 135 | * @merge: Shall new data from user space be merged into existing |
| 136 | * SG? |
| 137 | * @enc: Cryptographic operation to be performed when |
| 138 | * recvmsg is invoked. |
Herbert Xu | f3c802a | 2020-05-30 00:23:49 +1000 | [diff] [blame] | 139 | * @init: True if metadata has been sent. |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 140 | * @len: Length of memory allocated for this data structure. |
| 141 | */ |
| 142 | struct af_alg_ctx { |
| 143 | struct list_head tsgl_list; |
| 144 | |
| 145 | void *iv; |
| 146 | size_t aead_assoclen; |
| 147 | |
Gilad Ben-Yossef | 2c3f8b1 | 2017-10-18 08:00:39 +0100 | [diff] [blame] | 148 | struct crypto_wait wait; |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 149 | |
| 150 | size_t used; |
Jonathan Cameron | af955bf | 2017-12-19 10:27:24 +0000 | [diff] [blame] | 151 | atomic_t rcvused; |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 152 | |
| 153 | bool more; |
| 154 | bool merge; |
| 155 | bool enc; |
Herbert Xu | f3c802a | 2020-05-30 00:23:49 +1000 | [diff] [blame] | 156 | bool init; |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 157 | |
| 158 | unsigned int len; |
| 159 | }; |
| 160 | |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 161 | int af_alg_register_type(const struct af_alg_type *type); |
| 162 | int af_alg_unregister_type(const struct af_alg_type *type); |
| 163 | |
| 164 | int af_alg_release(struct socket *sock); |
Herbert Xu | c840ac6 | 2015-12-30 11:47:53 +0800 | [diff] [blame] | 165 | void af_alg_release_parent(struct sock *sk); |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 166 | int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern); |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 167 | |
Al Viro | 1d10eb2 | 2014-11-28 16:39:25 -0500 | [diff] [blame] | 168 | int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len); |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 169 | void af_alg_free_sg(struct af_alg_sgl *sgl); |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 170 | |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 171 | static inline struct alg_sock *alg_sk(struct sock *sk) |
| 172 | { |
| 173 | return (struct alg_sock *)sk; |
| 174 | } |
| 175 | |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 176 | /** |
| 177 | * Size of available buffer for sending data from user space to kernel. |
| 178 | * |
| 179 | * @sk socket of connection to user space |
| 180 | * @return number of bytes still available |
| 181 | */ |
| 182 | static inline int af_alg_sndbuf(struct sock *sk) |
| 183 | { |
| 184 | struct alg_sock *ask = alg_sk(sk); |
| 185 | struct af_alg_ctx *ctx = ask->private; |
| 186 | |
| 187 | return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) - |
| 188 | ctx->used, 0); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Can the send buffer still be written to? |
| 193 | * |
| 194 | * @sk socket of connection to user space |
| 195 | * @return true => writable, false => not writable |
| 196 | */ |
| 197 | static inline bool af_alg_writable(struct sock *sk) |
| 198 | { |
| 199 | return PAGE_SIZE <= af_alg_sndbuf(sk); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Size of available buffer used by kernel for the RX user space operation. |
| 204 | * |
| 205 | * @sk socket of connection to user space |
| 206 | * @return number of bytes still available |
| 207 | */ |
| 208 | static inline int af_alg_rcvbuf(struct sock *sk) |
| 209 | { |
| 210 | struct alg_sock *ask = alg_sk(sk); |
| 211 | struct af_alg_ctx *ctx = ask->private; |
| 212 | |
| 213 | return max_t(int, max_t(int, sk->sk_rcvbuf & PAGE_MASK, PAGE_SIZE) - |
Jonathan Cameron | af955bf | 2017-12-19 10:27:24 +0000 | [diff] [blame] | 214 | atomic_read(&ctx->rcvused), 0); |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Can the RX buffer still be written to? |
| 219 | * |
| 220 | * @sk socket of connection to user space |
| 221 | * @return true => writable, false => not writable |
| 222 | */ |
| 223 | static inline bool af_alg_readable(struct sock *sk) |
| 224 | { |
| 225 | return PAGE_SIZE <= af_alg_rcvbuf(sk); |
| 226 | } |
| 227 | |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 228 | unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset); |
| 229 | void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst, |
| 230 | size_t dst_offset); |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 231 | void af_alg_wmem_wakeup(struct sock *sk); |
Herbert Xu | f3c802a | 2020-05-30 00:23:49 +1000 | [diff] [blame] | 232 | int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min); |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 233 | int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size, |
| 234 | unsigned int ivsize); |
| 235 | ssize_t af_alg_sendpage(struct socket *sock, struct page *page, |
| 236 | int offset, size_t size, int flags); |
Stephan Mueller | 7d2c3f5 | 2017-11-10 13:20:55 +0100 | [diff] [blame] | 237 | void af_alg_free_resources(struct af_alg_async_req *areq); |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 238 | void af_alg_async_cb(struct crypto_async_request *_req, int err); |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 239 | __poll_t af_alg_poll(struct file *file, struct socket *sock, |
| 240 | poll_table *wait); |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 241 | struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk, |
| 242 | unsigned int areqlen); |
| 243 | int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags, |
| 244 | struct af_alg_async_req *areq, size_t maxsize, |
| 245 | size_t *outlen); |
| 246 | |
Herbert Xu | 03c8efc | 2010-10-19 21:12:39 +0800 | [diff] [blame] | 247 | #endif /* _CRYPTO_IF_ALG_H */ |