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