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> 19 #include <linux/scatterlist.h> 20 #include <linux/types.h> 21 #include <net/sock.h> 22 23 #include <crypto/aead.h> 24 #include <crypto/skcipher.h> 25 26 #define ALG_MAX_PAGES 16 27 28 struct crypto_async_request; 29 30 struct alg_sock { 31 /* struct sock must be the first member of struct alg_sock */ 32 struct sock sk; 33 34 struct sock *parent; 35 36 unsigned int refcnt; 37 unsigned int nokey_refcnt; 38 39 const struct af_alg_type *type; 40 void *private; 41 }; 42 43 struct af_alg_control { 44 struct af_alg_iv *iv; 45 int op; 46 unsigned int aead_assoclen; 47 }; 48 49 struct 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); 54 int (*accept_nokey)(void *private, struct sock *sk); 55 int (*setauthsize)(void *private, unsigned int authsize); 56 57 struct proto_ops *ops; 58 struct proto_ops *ops_nokey; 59 struct module *owner; 60 char name[14]; 61 }; 62 63 struct af_alg_sgl { 64 struct scatterlist sg[ALG_MAX_PAGES + 1]; 65 struct page *pages[ALG_MAX_PAGES]; 66 unsigned int npages; 67 }; 68 69 /* TX SGL entry */ 70 struct 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 */ 80 struct 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 */ 99 struct 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 */ 144 struct af_alg_ctx { 145 struct list_head tsgl_list; 146 147 void *iv; 148 size_t aead_assoclen; 149 150 struct crypto_wait wait; 151 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 162 int af_alg_register_type(const struct af_alg_type *type); 163 int af_alg_unregister_type(const struct af_alg_type *type); 164 165 int af_alg_release(struct socket *sock); 166 void af_alg_release_parent(struct sock *sk); 167 int af_alg_accept(struct sock *sk, struct socket *newsock, bool kern); 168 169 int af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len); 170 void af_alg_free_sg(struct af_alg_sgl *sgl); 171 void af_alg_link_sg(struct af_alg_sgl *sgl_prev, struct af_alg_sgl *sgl_new); 172 173 int af_alg_cmsg_send(struct msghdr *msg, struct af_alg_control *con); 174 175 static inline struct alg_sock *alg_sk(struct sock *sk) 176 { 177 return (struct alg_sock *)sk; 178 } 179 180 /** 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 */ 186 static 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 */ 201 static 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 */ 212 static 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 */ 227 static inline bool af_alg_readable(struct sock *sk) 228 { 229 return PAGE_SIZE <= af_alg_rcvbuf(sk); 230 } 231 232 int af_alg_alloc_tsgl(struct sock *sk); 233 unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset); 234 void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst, 235 size_t dst_offset); 236 void af_alg_free_areq_sgls(struct af_alg_async_req *areq); 237 int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags); 238 void af_alg_wmem_wakeup(struct sock *sk); 239 int af_alg_wait_for_data(struct sock *sk, unsigned flags); 240 void af_alg_data_wakeup(struct sock *sk); 241 int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size, 242 unsigned int ivsize); 243 ssize_t af_alg_sendpage(struct socket *sock, struct page *page, 244 int offset, size_t size, int flags); 245 void af_alg_async_cb(struct crypto_async_request *_req, int err); 246 __poll_t af_alg_poll(struct file *file, struct socket *sock, 247 poll_table *wait); 248 struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk, 249 unsigned int areqlen); 250 int 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 254 #endif /* _CRYPTO_IF_ALG_H */ 255