1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * if_alg: User-space algorithm interface 4 * 5 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au> 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> 14 #include <linux/scatterlist.h> 15 #include <linux/types.h> 16 #include <linux/atomic.h> 17 #include <net/sock.h> 18 19 #include <crypto/aead.h> 20 #include <crypto/skcipher.h> 21 22 #define ALG_MAX_PAGES 16 23 24 struct alg_sock { 25 /* struct sock must be the first member of struct alg_sock */ 26 struct sock sk; 27 28 struct sock *parent; 29 30 atomic_t refcnt; 31 atomic_t nokey_refcnt; 32 33 const struct af_alg_type *type; 34 void *private; 35 }; 36 37 struct af_alg_control { 38 struct af_alg_iv *iv; 39 int op; 40 unsigned int aead_assoclen; 41 }; 42 43 struct af_alg_type { 44 void *(*bind)(const char *name); 45 void (*release)(void *private); 46 int (*setkey)(void *private, const u8 *key, unsigned int keylen); 47 int (*setentropy)(void *private, sockptr_t entropy, unsigned int len); 48 int (*accept)(void *private, struct sock *sk); 49 int (*accept_nokey)(void *private, struct sock *sk); 50 int (*setauthsize)(void *private, unsigned int authsize); 51 52 struct proto_ops *ops; 53 struct proto_ops *ops_nokey; 54 struct module *owner; 55 char name[14]; 56 }; 57 58 struct af_alg_sgl { 59 struct sg_table sgt; 60 struct scatterlist sgl[ALG_MAX_PAGES + 1]; 61 bool need_unpin; 62 }; 63 64 /* TX SGL entry */ 65 struct af_alg_tsgl { 66 struct list_head list; 67 unsigned int cur; /* Last processed SG entry */ 68 struct scatterlist sg[]; /* Array of SGs forming the SGL */ 69 }; 70 71 #define MAX_SGL_ENTS ((4096 - sizeof(struct af_alg_tsgl)) / \ 72 sizeof(struct scatterlist) - 1) 73 74 /* RX SGL entry */ 75 struct af_alg_rsgl { 76 struct af_alg_sgl sgl; 77 struct list_head list; 78 size_t sg_num_bytes; /* Bytes of data in that SGL */ 79 }; 80 81 /** 82 * struct af_alg_async_req - definition of crypto request 83 * @sk: Socket the request is associated with 84 * @first_rsgl: First RX SG 85 * @last_rsgl: Pointer to last RX SG 86 * @rsgl_list: Track RX SGs 87 * @tsgl: Private, per request TX SGL of buffers to process 88 * @tsgl_entries: Number of entries in priv. TX SGL 89 * @outlen: Number of output bytes generated by crypto op 90 * @areqlen: Length of this data structure 91 * @cra_u: Cipher request 92 */ 93 struct af_alg_async_req { 94 struct sock *sk; 95 96 struct af_alg_rsgl first_rsgl; 97 struct af_alg_rsgl *last_rsgl; 98 struct list_head rsgl_list; 99 100 struct scatterlist *tsgl; 101 unsigned int tsgl_entries; 102 103 unsigned int outlen; 104 unsigned int areqlen; 105 106 union { 107 struct aead_request aead_req; 108 struct skcipher_request skcipher_req; 109 } cra_u; 110 111 /* req ctx trails this struct */ 112 }; 113 114 /** 115 * struct af_alg_ctx - definition of the crypto context 116 * 117 * The crypto context tracks the input data during the lifetime of an AF_ALG 118 * socket. 119 * 120 * @tsgl_list: Link to TX SGL 121 * @iv: IV for cipher operation 122 * @state: Existing state for continuing operation 123 * @aead_assoclen: Length of AAD for AEAD cipher operations 124 * @completion: Work queue for synchronous operation 125 * @used: TX bytes sent to kernel. This variable is used to 126 * ensure that user space cannot cause the kernel 127 * to allocate too much memory in sendmsg operation. 128 * @rcvused: Total RX bytes to be filled by kernel. This variable 129 * is used to ensure user space cannot cause the kernel 130 * to allocate too much memory in a recvmsg operation. 131 * @more: More data to be expected from user space? 132 * @merge: Shall new data from user space be merged into existing 133 * SG? 134 * @enc: Cryptographic operation to be performed when 135 * recvmsg is invoked. 136 * @write: True if we are in the middle of a write. 137 * @init: True if metadata has been sent. 138 * @len: Length of memory allocated for this data structure. 139 * @inflight: Non-zero when requests are in flight, for debugging only. 140 */ 141 struct af_alg_ctx { 142 struct list_head tsgl_list; 143 144 void *iv; 145 void *state; 146 size_t aead_assoclen; 147 148 struct crypto_wait wait; 149 150 size_t used; 151 atomic_t rcvused; 152 153 bool more:1, 154 merge:1, 155 enc:1, 156 write:1, 157 init:1; 158 159 unsigned int len; 160 161 unsigned int inflight; 162 }; 163 164 int af_alg_register_type(const struct af_alg_type *type); 165 int af_alg_unregister_type(const struct af_alg_type *type); 166 167 int af_alg_release(struct socket *sock); 168 void af_alg_release_parent(struct sock *sk); 169 int af_alg_accept(struct sock *sk, struct socket *newsock, 170 struct proto_accept_arg *arg); 171 172 void af_alg_free_sg(struct af_alg_sgl *sgl); 173 174 static inline struct alg_sock *alg_sk(struct sock *sk) 175 { 176 return (struct alg_sock *)sk; 177 } 178 179 /** 180 * Size of available buffer for sending data from user space to kernel. 181 * 182 * @sk socket of connection to user space 183 * @return number of bytes still available 184 */ 185 static inline int af_alg_sndbuf(struct sock *sk) 186 { 187 struct alg_sock *ask = alg_sk(sk); 188 struct af_alg_ctx *ctx = ask->private; 189 190 return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) - 191 ctx->used, 0); 192 } 193 194 /** 195 * Can the send buffer still be written to? 196 * 197 * @sk socket of connection to user space 198 * @return true => writable, false => not writable 199 */ 200 static inline bool af_alg_writable(struct sock *sk) 201 { 202 return PAGE_SIZE <= af_alg_sndbuf(sk); 203 } 204 205 /** 206 * Size of available buffer used by kernel for the RX user space operation. 207 * 208 * @sk socket of connection to user space 209 * @return number of bytes still available 210 */ 211 static inline int af_alg_rcvbuf(struct sock *sk) 212 { 213 struct alg_sock *ask = alg_sk(sk); 214 struct af_alg_ctx *ctx = ask->private; 215 216 return max_t(int, max_t(int, sk->sk_rcvbuf & PAGE_MASK, PAGE_SIZE) - 217 atomic_read(&ctx->rcvused), 0); 218 } 219 220 /** 221 * Can the RX buffer still be written to? 222 * 223 * @sk socket of connection to user space 224 * @return true => writable, false => not writable 225 */ 226 static inline bool af_alg_readable(struct sock *sk) 227 { 228 return PAGE_SIZE <= af_alg_rcvbuf(sk); 229 } 230 231 unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes); 232 void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst); 233 void af_alg_wmem_wakeup(struct sock *sk); 234 int af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min); 235 int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size, 236 unsigned int ivsize); 237 void af_alg_free_resources(struct af_alg_async_req *areq); 238 __poll_t af_alg_poll(struct file *file, struct socket *sock, 239 poll_table *wait); 240 struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk, 241 unsigned int areqlen); 242 int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags, 243 struct af_alg_async_req *areq, size_t maxsize, 244 size_t *outlen); 245 246 /* 247 * Mask used to disable unsupported algorithm implementations. 248 * 249 * This is the same as FSCRYPT_CRYPTOAPI_MASK in fs/crypto/fscrypt_private.h. 250 * In additions to the motivations there, this API is exposed to userspace 251 * that might not be fully trusted. 252 */ 253 #define AF_ALG_CRYPTOAPI_MASK \ 254 (CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY | \ 255 CRYPTO_ALG_KERN_DRIVER_ONLY) 256 257 258 #endif /* _CRYPTO_IF_ALG_H */ 259