Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * algif_aead: User-space interface for AEAD algorithms |
| 3 | * |
| 4 | * Copyright (C) 2014, Stephan Mueller <smueller@chronox.de> |
| 5 | * |
| 6 | * This file provides the user-space API for AEAD ciphers. |
| 7 | * |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the Free |
| 10 | * Software Foundation; either version 2 of the License, or (at your option) |
| 11 | * any later version. |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 12 | * |
| 13 | * The following concept of the memory management is used: |
| 14 | * |
| 15 | * The kernel maintains two SGLs, the TX SGL and the RX SGL. The TX SGL is |
| 16 | * filled by user space with the data submitted via sendpage/sendmsg. Filling |
| 17 | * up the TX SGL does not cause a crypto operation -- the data will only be |
| 18 | * tracked by the kernel. Upon receipt of one recvmsg call, the caller must |
| 19 | * provide a buffer which is tracked with the RX SGL. |
| 20 | * |
| 21 | * During the processing of the recvmsg operation, the cipher request is |
| 22 | * allocated and prepared. As part of the recvmsg operation, the processed |
| 23 | * TX buffers are extracted from the TX SGL into a separate SGL. |
| 24 | * |
| 25 | * After the completion of the crypto operation, the RX SGL and the cipher |
| 26 | * request is released. The extracted TX SGL parts are released together with |
| 27 | * the RX SGL release. |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 28 | */ |
| 29 | |
Tadeusz Struk | 83094e5e | 2016-03-11 11:50:33 -0800 | [diff] [blame] | 30 | #include <crypto/internal/aead.h> |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 31 | #include <crypto/scatterwalk.h> |
| 32 | #include <crypto/if_alg.h> |
Stephan Mueller | 72548b0 | 2017-07-30 14:32:58 +0200 | [diff] [blame] | 33 | #include <crypto/skcipher.h> |
| 34 | #include <crypto/null.h> |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 35 | #include <linux/init.h> |
| 36 | #include <linux/list.h> |
| 37 | #include <linux/kernel.h> |
| 38 | #include <linux/mm.h> |
| 39 | #include <linux/module.h> |
| 40 | #include <linux/net.h> |
| 41 | #include <net/sock.h> |
| 42 | |
Stephan Mueller | 2a2a251 | 2017-04-24 11:15:23 +0200 | [diff] [blame] | 43 | struct aead_tfm { |
| 44 | struct crypto_aead *aead; |
| 45 | bool has_key; |
Stephan Mueller | 72548b0 | 2017-07-30 14:32:58 +0200 | [diff] [blame] | 46 | struct crypto_skcipher *null_tfm; |
Stephan Mueller | 2a2a251 | 2017-04-24 11:15:23 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 49 | static inline bool aead_sufficient_data(struct sock *sk) |
| 50 | { |
| 51 | struct alg_sock *ask = alg_sk(sk); |
| 52 | struct sock *psk = ask->parent; |
| 53 | struct alg_sock *pask = alg_sk(psk); |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 54 | struct af_alg_ctx *ctx = ask->private; |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 55 | struct aead_tfm *aeadc = pask->private; |
| 56 | struct crypto_aead *tfm = aeadc->aead; |
| 57 | unsigned int as = crypto_aead_authsize(tfm); |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 58 | |
Stephan Mueller | 0c1e16c | 2016-12-05 15:26:19 +0100 | [diff] [blame] | 59 | /* |
| 60 | * The minimum amount of memory needed for an AEAD cipher is |
| 61 | * the AAD and in case of decryption the tag. |
| 62 | */ |
| 63 | return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as); |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Linus Torvalds | eccd02f | 2015-04-15 14:09:46 -0700 | [diff] [blame] | 66 | static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 67 | { |
| 68 | struct sock *sk = sock->sk; |
| 69 | struct alg_sock *ask = alg_sk(sk); |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 70 | struct sock *psk = ask->parent; |
| 71 | struct alg_sock *pask = alg_sk(psk); |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 72 | struct aead_tfm *aeadc = pask->private; |
| 73 | struct crypto_aead *tfm = aeadc->aead; |
| 74 | unsigned int ivsize = crypto_aead_ivsize(tfm); |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 75 | |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 76 | return af_alg_sendmsg(sock, msg, size, ivsize); |
Tadeusz Struk | 83094e5e | 2016-03-11 11:50:33 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Stephan Mueller | 72548b0 | 2017-07-30 14:32:58 +0200 | [diff] [blame] | 79 | static int crypto_aead_copy_sgl(struct crypto_skcipher *null_tfm, |
| 80 | struct scatterlist *src, |
| 81 | struct scatterlist *dst, unsigned int len) |
| 82 | { |
| 83 | SKCIPHER_REQUEST_ON_STACK(skreq, null_tfm); |
| 84 | |
| 85 | skcipher_request_set_tfm(skreq, null_tfm); |
| 86 | skcipher_request_set_callback(skreq, CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 87 | NULL, NULL); |
| 88 | skcipher_request_set_crypt(skreq, src, dst, len, NULL); |
| 89 | |
| 90 | return crypto_skcipher_encrypt(skreq); |
| 91 | } |
| 92 | |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 93 | static int _aead_recvmsg(struct socket *sock, struct msghdr *msg, |
| 94 | size_t ignored, int flags) |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 95 | { |
| 96 | struct sock *sk = sock->sk; |
| 97 | struct alg_sock *ask = alg_sk(sk); |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 98 | struct sock *psk = ask->parent; |
| 99 | struct alg_sock *pask = alg_sk(psk); |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 100 | struct af_alg_ctx *ctx = ask->private; |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 101 | struct aead_tfm *aeadc = pask->private; |
| 102 | struct crypto_aead *tfm = aeadc->aead; |
Stephan Mueller | 72548b0 | 2017-07-30 14:32:58 +0200 | [diff] [blame] | 103 | struct crypto_skcipher *null_tfm = aeadc->null_tfm; |
Stephan Mueller | 8e1fa89 | 2017-11-10 11:04:52 +0100 | [diff] [blame^] | 104 | unsigned int i, as = crypto_aead_authsize(tfm); |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 105 | struct af_alg_async_req *areq; |
Stephan Mueller | 8e1fa89 | 2017-11-10 11:04:52 +0100 | [diff] [blame^] | 106 | struct af_alg_tsgl *tsgl, *tmp; |
| 107 | struct scatterlist *rsgl_src, *tsgl_src = NULL; |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 108 | int err = 0; |
| 109 | size_t used = 0; /* [in] TX bufs to be en/decrypted */ |
| 110 | size_t outlen = 0; /* [out] RX bufs produced by kernel */ |
| 111 | size_t usedpages = 0; /* [in] RX bufs to be used from user */ |
| 112 | size_t processed = 0; /* [in] TX bufs to be consumed */ |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 113 | |
| 114 | /* |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 115 | * Data length provided by caller via sendmsg/sendpage that has not |
| 116 | * yet been processed. |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 117 | */ |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 118 | used = ctx->used; |
| 119 | |
| 120 | /* |
| 121 | * Make sure sufficient data is present -- note, the same check is |
| 122 | * is also present in sendmsg/sendpage. The checks in sendpage/sendmsg |
| 123 | * shall provide an information to the data sender that something is |
| 124 | * wrong, but they are irrelevant to maintain the kernel integrity. |
| 125 | * We need this check here too in case user space decides to not honor |
| 126 | * the error message in sendmsg/sendpage and still call recvmsg. This |
| 127 | * check here protects the kernel integrity. |
| 128 | */ |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 129 | if (!aead_sufficient_data(sk)) |
| 130 | return -EINVAL; |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 131 | |
Stephan Mueller | 0c1e16c | 2016-12-05 15:26:19 +0100 | [diff] [blame] | 132 | /* |
| 133 | * Calculate the minimum output buffer size holding the result of the |
| 134 | * cipher operation. When encrypting data, the receiving buffer is |
| 135 | * larger by the tag length compared to the input buffer as the |
| 136 | * encryption operation generates the tag. For decryption, the input |
| 137 | * buffer provides the tag which is consumed resulting in only the |
| 138 | * plaintext without a buffer for the tag returned to the caller. |
| 139 | */ |
| 140 | if (ctx->enc) |
| 141 | outlen = used + as; |
| 142 | else |
| 143 | outlen = used - as; |
Herbert Xu | 19fa775 | 2015-05-27 17:24:41 +0800 | [diff] [blame] | 144 | |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 145 | /* |
| 146 | * The cipher operation input data is reduced by the associated data |
| 147 | * length as this data is processed separately later on. |
| 148 | */ |
Stephan Mueller | 0c1e16c | 2016-12-05 15:26:19 +0100 | [diff] [blame] | 149 | used -= ctx->aead_assoclen; |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 150 | |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 151 | /* Allocate cipher request for current operation. */ |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 152 | areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) + |
| 153 | crypto_aead_reqsize(tfm)); |
| 154 | if (IS_ERR(areq)) |
| 155 | return PTR_ERR(areq); |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 156 | |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 157 | /* convert iovecs of output buffers into RX SGL */ |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 158 | err = af_alg_get_rsgl(sk, msg, flags, areq, outlen, &usedpages); |
| 159 | if (err) |
| 160 | goto free; |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 161 | |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 162 | /* |
| 163 | * Ensure output buffer is sufficiently large. If the caller provides |
| 164 | * less buffer space, only use the relative required input size. This |
| 165 | * allows AIO operation where the caller sent all data to be processed |
| 166 | * and the AIO operation performs the operation on the different chunks |
| 167 | * of the input data. |
| 168 | */ |
Stephan Mueller | 0c1e16c | 2016-12-05 15:26:19 +0100 | [diff] [blame] | 169 | if (usedpages < outlen) { |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 170 | size_t less = outlen - usedpages; |
| 171 | |
| 172 | if (used < less) { |
| 173 | err = -EINVAL; |
| 174 | goto free; |
| 175 | } |
| 176 | used -= less; |
| 177 | outlen -= less; |
Stephan Mueller | 0c1e16c | 2016-12-05 15:26:19 +0100 | [diff] [blame] | 178 | } |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 179 | |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 180 | processed = used + ctx->aead_assoclen; |
Stephan Mueller | 8e1fa89 | 2017-11-10 11:04:52 +0100 | [diff] [blame^] | 181 | list_for_each_entry_safe(tsgl, tmp, &ctx->tsgl_list, list) { |
| 182 | for (i = 0; i < tsgl->cur; i++) { |
| 183 | struct scatterlist *process_sg = tsgl->sg + i; |
| 184 | |
| 185 | if (!(process_sg->length) || !sg_page(process_sg)) |
| 186 | continue; |
| 187 | tsgl_src = process_sg; |
| 188 | break; |
| 189 | } |
| 190 | if (tsgl_src) |
| 191 | break; |
| 192 | } |
| 193 | if (processed && !tsgl_src) { |
| 194 | err = -EFAULT; |
| 195 | goto free; |
| 196 | } |
Stephan Mueller | 72548b0 | 2017-07-30 14:32:58 +0200 | [diff] [blame] | 197 | |
| 198 | /* |
| 199 | * Copy of AAD from source to destination |
| 200 | * |
| 201 | * The AAD is copied to the destination buffer without change. Even |
| 202 | * when user space uses an in-place cipher operation, the kernel |
| 203 | * will copy the data as it does not see whether such in-place operation |
| 204 | * is initiated. |
| 205 | * |
| 206 | * To ensure efficiency, the following implementation ensure that the |
| 207 | * ciphers are invoked to perform a crypto operation in-place. This |
| 208 | * is achieved by memory management specified as follows. |
| 209 | */ |
| 210 | |
| 211 | /* Use the RX SGL as source (and destination) for crypto op. */ |
Stephan Mueller | 8e1fa89 | 2017-11-10 11:04:52 +0100 | [diff] [blame^] | 212 | rsgl_src = areq->first_rsgl.sgl.sg; |
Stephan Mueller | 72548b0 | 2017-07-30 14:32:58 +0200 | [diff] [blame] | 213 | |
| 214 | if (ctx->enc) { |
| 215 | /* |
| 216 | * Encryption operation - The in-place cipher operation is |
| 217 | * achieved by the following operation: |
| 218 | * |
Stephan Mueller | 75d11e7 | 2017-08-09 16:20:00 +0200 | [diff] [blame] | 219 | * TX SGL: AAD || PT |
Stephan Mueller | 72548b0 | 2017-07-30 14:32:58 +0200 | [diff] [blame] | 220 | * | | |
| 221 | * | copy | |
| 222 | * v v |
Stephan Mueller | 75d11e7 | 2017-08-09 16:20:00 +0200 | [diff] [blame] | 223 | * RX SGL: AAD || PT || Tag |
Stephan Mueller | 72548b0 | 2017-07-30 14:32:58 +0200 | [diff] [blame] | 224 | */ |
Stephan Mueller | 8e1fa89 | 2017-11-10 11:04:52 +0100 | [diff] [blame^] | 225 | err = crypto_aead_copy_sgl(null_tfm, tsgl_src, |
Stephan Mueller | 72548b0 | 2017-07-30 14:32:58 +0200 | [diff] [blame] | 226 | areq->first_rsgl.sgl.sg, processed); |
| 227 | if (err) |
| 228 | goto free; |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 229 | af_alg_pull_tsgl(sk, processed, NULL, 0); |
Stephan Mueller | 72548b0 | 2017-07-30 14:32:58 +0200 | [diff] [blame] | 230 | } else { |
| 231 | /* |
| 232 | * Decryption operation - To achieve an in-place cipher |
| 233 | * operation, the following SGL structure is used: |
| 234 | * |
| 235 | * TX SGL: AAD || CT || Tag |
| 236 | * | | ^ |
| 237 | * | copy | | Create SGL link. |
| 238 | * v v | |
| 239 | * RX SGL: AAD || CT ----+ |
| 240 | */ |
| 241 | |
| 242 | /* Copy AAD || CT to RX SGL buffer for in-place operation. */ |
Stephan Mueller | 8e1fa89 | 2017-11-10 11:04:52 +0100 | [diff] [blame^] | 243 | err = crypto_aead_copy_sgl(null_tfm, tsgl_src, |
Stephan Mueller | 72548b0 | 2017-07-30 14:32:58 +0200 | [diff] [blame] | 244 | areq->first_rsgl.sgl.sg, outlen); |
| 245 | if (err) |
| 246 | goto free; |
| 247 | |
| 248 | /* Create TX SGL for tag and chain it to RX SGL. */ |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 249 | areq->tsgl_entries = af_alg_count_tsgl(sk, processed, |
| 250 | processed - as); |
Stephan Mueller | 72548b0 | 2017-07-30 14:32:58 +0200 | [diff] [blame] | 251 | if (!areq->tsgl_entries) |
| 252 | areq->tsgl_entries = 1; |
| 253 | areq->tsgl = sock_kmalloc(sk, sizeof(*areq->tsgl) * |
| 254 | areq->tsgl_entries, |
| 255 | GFP_KERNEL); |
| 256 | if (!areq->tsgl) { |
| 257 | err = -ENOMEM; |
| 258 | goto free; |
| 259 | } |
| 260 | sg_init_table(areq->tsgl, areq->tsgl_entries); |
| 261 | |
| 262 | /* Release TX SGL, except for tag data and reassign tag data. */ |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 263 | af_alg_pull_tsgl(sk, processed, areq->tsgl, processed - as); |
Stephan Mueller | 72548b0 | 2017-07-30 14:32:58 +0200 | [diff] [blame] | 264 | |
| 265 | /* chain the areq TX SGL holding the tag with RX SGL */ |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 266 | if (usedpages) { |
Stephan Mueller | 72548b0 | 2017-07-30 14:32:58 +0200 | [diff] [blame] | 267 | /* RX SGL present */ |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 268 | struct af_alg_sgl *sgl_prev = &areq->last_rsgl->sgl; |
Stephan Mueller | 72548b0 | 2017-07-30 14:32:58 +0200 | [diff] [blame] | 269 | |
| 270 | sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1); |
| 271 | sg_chain(sgl_prev->sg, sgl_prev->npages + 1, |
| 272 | areq->tsgl); |
| 273 | } else |
| 274 | /* no RX SGL present (e.g. authentication only) */ |
Stephan Mueller | 8e1fa89 | 2017-11-10 11:04:52 +0100 | [diff] [blame^] | 275 | rsgl_src = areq->tsgl; |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 276 | } |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 277 | |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 278 | /* Initialize the crypto operation */ |
Stephan Mueller | 8e1fa89 | 2017-11-10 11:04:52 +0100 | [diff] [blame^] | 279 | aead_request_set_crypt(&areq->cra_u.aead_req, rsgl_src, |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 280 | areq->first_rsgl.sgl.sg, used, ctx->iv); |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 281 | aead_request_set_ad(&areq->cra_u.aead_req, ctx->aead_assoclen); |
| 282 | aead_request_set_tfm(&areq->cra_u.aead_req, tfm); |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 283 | |
| 284 | if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) { |
| 285 | /* AIO operation */ |
| 286 | areq->iocb = msg->msg_iocb; |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 287 | aead_request_set_callback(&areq->cra_u.aead_req, |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 288 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 289 | af_alg_async_cb, areq); |
| 290 | err = ctx->enc ? crypto_aead_encrypt(&areq->cra_u.aead_req) : |
| 291 | crypto_aead_decrypt(&areq->cra_u.aead_req); |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 292 | } else { |
| 293 | /* Synchronous operation */ |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 294 | aead_request_set_callback(&areq->cra_u.aead_req, |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 295 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
Gilad Ben-Yossef | 2c3f8b1 | 2017-10-18 08:00:39 +0100 | [diff] [blame] | 296 | crypto_req_done, &ctx->wait); |
| 297 | err = crypto_wait_req(ctx->enc ? |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 298 | crypto_aead_encrypt(&areq->cra_u.aead_req) : |
| 299 | crypto_aead_decrypt(&areq->cra_u.aead_req), |
Gilad Ben-Yossef | 2c3f8b1 | 2017-10-18 08:00:39 +0100 | [diff] [blame] | 300 | &ctx->wait); |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 301 | } |
| 302 | |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 303 | /* AIO operation in progress */ |
| 304 | if (err == -EINPROGRESS) { |
| 305 | sock_hold(sk); |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 306 | |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 307 | /* Remember output size that will be generated. */ |
| 308 | areq->outlen = outlen; |
| 309 | |
| 310 | return -EIOCBQUEUED; |
Tadeusz Struk | 83094e5e | 2016-03-11 11:50:33 -0800 | [diff] [blame] | 311 | } |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 312 | |
| 313 | free: |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 314 | af_alg_free_areq_sgls(areq); |
| 315 | sock_kfree_s(sk, areq, areq->areqlen); |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 316 | |
| 317 | return err ? err : outlen; |
| 318 | } |
| 319 | |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 320 | static int aead_recvmsg(struct socket *sock, struct msghdr *msg, |
| 321 | size_t ignored, int flags) |
Tadeusz Struk | 83094e5e | 2016-03-11 11:50:33 -0800 | [diff] [blame] | 322 | { |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 323 | struct sock *sk = sock->sk; |
| 324 | int ret = 0; |
| 325 | |
| 326 | lock_sock(sk); |
| 327 | while (msg_data_left(msg)) { |
| 328 | int err = _aead_recvmsg(sock, msg, ignored, flags); |
| 329 | |
| 330 | /* |
| 331 | * This error covers -EIOCBQUEUED which implies that we can |
| 332 | * only handle one AIO request. If the caller wants to have |
| 333 | * multiple AIO requests in parallel, he must make multiple |
| 334 | * separate AIO calls. |
Stephan Mueller | 5703c82 | 2017-07-30 14:31:18 +0200 | [diff] [blame] | 335 | * |
| 336 | * Also return the error if no data has been processed so far. |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 337 | */ |
| 338 | if (err <= 0) { |
Stephan Mueller | 5703c82 | 2017-07-30 14:31:18 +0200 | [diff] [blame] | 339 | if (err == -EIOCBQUEUED || err == -EBADMSG || !ret) |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 340 | ret = err; |
| 341 | goto out; |
| 342 | } |
| 343 | |
| 344 | ret += err; |
| 345 | } |
| 346 | |
| 347 | out: |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 348 | af_alg_wmem_wakeup(sk); |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 349 | release_sock(sk); |
| 350 | return ret; |
Tadeusz Struk | 83094e5e | 2016-03-11 11:50:33 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 353 | static struct proto_ops algif_aead_ops = { |
| 354 | .family = PF_ALG, |
| 355 | |
| 356 | .connect = sock_no_connect, |
| 357 | .socketpair = sock_no_socketpair, |
| 358 | .getname = sock_no_getname, |
| 359 | .ioctl = sock_no_ioctl, |
| 360 | .listen = sock_no_listen, |
| 361 | .shutdown = sock_no_shutdown, |
| 362 | .getsockopt = sock_no_getsockopt, |
| 363 | .mmap = sock_no_mmap, |
| 364 | .bind = sock_no_bind, |
| 365 | .accept = sock_no_accept, |
| 366 | .setsockopt = sock_no_setsockopt, |
| 367 | |
| 368 | .release = af_alg_release, |
| 369 | .sendmsg = aead_sendmsg, |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 370 | .sendpage = af_alg_sendpage, |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 371 | .recvmsg = aead_recvmsg, |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 372 | .poll = af_alg_poll, |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 373 | }; |
| 374 | |
Stephan Mueller | 2a2a251 | 2017-04-24 11:15:23 +0200 | [diff] [blame] | 375 | static int aead_check_key(struct socket *sock) |
| 376 | { |
| 377 | int err = 0; |
| 378 | struct sock *psk; |
| 379 | struct alg_sock *pask; |
| 380 | struct aead_tfm *tfm; |
| 381 | struct sock *sk = sock->sk; |
| 382 | struct alg_sock *ask = alg_sk(sk); |
| 383 | |
| 384 | lock_sock(sk); |
| 385 | if (ask->refcnt) |
| 386 | goto unlock_child; |
| 387 | |
| 388 | psk = ask->parent; |
| 389 | pask = alg_sk(ask->parent); |
| 390 | tfm = pask->private; |
| 391 | |
| 392 | err = -ENOKEY; |
| 393 | lock_sock_nested(psk, SINGLE_DEPTH_NESTING); |
| 394 | if (!tfm->has_key) |
| 395 | goto unlock; |
| 396 | |
| 397 | if (!pask->refcnt++) |
| 398 | sock_hold(psk); |
| 399 | |
| 400 | ask->refcnt = 1; |
| 401 | sock_put(psk); |
| 402 | |
| 403 | err = 0; |
| 404 | |
| 405 | unlock: |
| 406 | release_sock(psk); |
| 407 | unlock_child: |
| 408 | release_sock(sk); |
| 409 | |
| 410 | return err; |
| 411 | } |
| 412 | |
| 413 | static int aead_sendmsg_nokey(struct socket *sock, struct msghdr *msg, |
| 414 | size_t size) |
| 415 | { |
| 416 | int err; |
| 417 | |
| 418 | err = aead_check_key(sock); |
| 419 | if (err) |
| 420 | return err; |
| 421 | |
| 422 | return aead_sendmsg(sock, msg, size); |
| 423 | } |
| 424 | |
| 425 | static ssize_t aead_sendpage_nokey(struct socket *sock, struct page *page, |
| 426 | int offset, size_t size, int flags) |
| 427 | { |
| 428 | int err; |
| 429 | |
| 430 | err = aead_check_key(sock); |
| 431 | if (err) |
| 432 | return err; |
| 433 | |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 434 | return af_alg_sendpage(sock, page, offset, size, flags); |
Stephan Mueller | 2a2a251 | 2017-04-24 11:15:23 +0200 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static int aead_recvmsg_nokey(struct socket *sock, struct msghdr *msg, |
| 438 | size_t ignored, int flags) |
| 439 | { |
| 440 | int err; |
| 441 | |
| 442 | err = aead_check_key(sock); |
| 443 | if (err) |
| 444 | return err; |
| 445 | |
| 446 | return aead_recvmsg(sock, msg, ignored, flags); |
| 447 | } |
| 448 | |
| 449 | static struct proto_ops algif_aead_ops_nokey = { |
| 450 | .family = PF_ALG, |
| 451 | |
| 452 | .connect = sock_no_connect, |
| 453 | .socketpair = sock_no_socketpair, |
| 454 | .getname = sock_no_getname, |
| 455 | .ioctl = sock_no_ioctl, |
| 456 | .listen = sock_no_listen, |
| 457 | .shutdown = sock_no_shutdown, |
| 458 | .getsockopt = sock_no_getsockopt, |
| 459 | .mmap = sock_no_mmap, |
| 460 | .bind = sock_no_bind, |
| 461 | .accept = sock_no_accept, |
| 462 | .setsockopt = sock_no_setsockopt, |
| 463 | |
| 464 | .release = af_alg_release, |
| 465 | .sendmsg = aead_sendmsg_nokey, |
| 466 | .sendpage = aead_sendpage_nokey, |
| 467 | .recvmsg = aead_recvmsg_nokey, |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 468 | .poll = af_alg_poll, |
Stephan Mueller | 2a2a251 | 2017-04-24 11:15:23 +0200 | [diff] [blame] | 469 | }; |
| 470 | |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 471 | static void *aead_bind(const char *name, u32 type, u32 mask) |
| 472 | { |
Stephan Mueller | 2a2a251 | 2017-04-24 11:15:23 +0200 | [diff] [blame] | 473 | struct aead_tfm *tfm; |
| 474 | struct crypto_aead *aead; |
Stephan Mueller | 72548b0 | 2017-07-30 14:32:58 +0200 | [diff] [blame] | 475 | struct crypto_skcipher *null_tfm; |
Stephan Mueller | 2a2a251 | 2017-04-24 11:15:23 +0200 | [diff] [blame] | 476 | |
| 477 | tfm = kzalloc(sizeof(*tfm), GFP_KERNEL); |
| 478 | if (!tfm) |
| 479 | return ERR_PTR(-ENOMEM); |
| 480 | |
| 481 | aead = crypto_alloc_aead(name, type, mask); |
| 482 | if (IS_ERR(aead)) { |
| 483 | kfree(tfm); |
| 484 | return ERR_CAST(aead); |
| 485 | } |
| 486 | |
Stephan Mueller | 72548b0 | 2017-07-30 14:32:58 +0200 | [diff] [blame] | 487 | null_tfm = crypto_get_default_null_skcipher2(); |
| 488 | if (IS_ERR(null_tfm)) { |
| 489 | crypto_free_aead(aead); |
| 490 | kfree(tfm); |
| 491 | return ERR_CAST(null_tfm); |
| 492 | } |
| 493 | |
Stephan Mueller | 2a2a251 | 2017-04-24 11:15:23 +0200 | [diff] [blame] | 494 | tfm->aead = aead; |
Stephan Mueller | 72548b0 | 2017-07-30 14:32:58 +0200 | [diff] [blame] | 495 | tfm->null_tfm = null_tfm; |
Stephan Mueller | 2a2a251 | 2017-04-24 11:15:23 +0200 | [diff] [blame] | 496 | |
| 497 | return tfm; |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static void aead_release(void *private) |
| 501 | { |
Stephan Mueller | 2a2a251 | 2017-04-24 11:15:23 +0200 | [diff] [blame] | 502 | struct aead_tfm *tfm = private; |
| 503 | |
| 504 | crypto_free_aead(tfm->aead); |
| 505 | kfree(tfm); |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static int aead_setauthsize(void *private, unsigned int authsize) |
| 509 | { |
Stephan Mueller | 2a2a251 | 2017-04-24 11:15:23 +0200 | [diff] [blame] | 510 | struct aead_tfm *tfm = private; |
| 511 | |
| 512 | return crypto_aead_setauthsize(tfm->aead, authsize); |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | static int aead_setkey(void *private, const u8 *key, unsigned int keylen) |
| 516 | { |
Stephan Mueller | 2a2a251 | 2017-04-24 11:15:23 +0200 | [diff] [blame] | 517 | struct aead_tfm *tfm = private; |
| 518 | int err; |
| 519 | |
| 520 | err = crypto_aead_setkey(tfm->aead, key, keylen); |
| 521 | tfm->has_key = !err; |
| 522 | |
| 523 | return err; |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | static void aead_sock_destruct(struct sock *sk) |
| 527 | { |
| 528 | struct alg_sock *ask = alg_sk(sk); |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 529 | struct af_alg_ctx *ctx = ask->private; |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 530 | struct sock *psk = ask->parent; |
| 531 | struct alg_sock *pask = alg_sk(psk); |
| 532 | struct aead_tfm *aeadc = pask->private; |
| 533 | struct crypto_aead *tfm = aeadc->aead; |
| 534 | unsigned int ivlen = crypto_aead_ivsize(tfm); |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 535 | |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 536 | af_alg_pull_tsgl(sk, ctx->used, NULL, 0); |
Stephan Mueller | 72548b0 | 2017-07-30 14:32:58 +0200 | [diff] [blame] | 537 | crypto_put_default_null_skcipher2(); |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 538 | sock_kzfree_s(sk, ctx->iv, ivlen); |
| 539 | sock_kfree_s(sk, ctx, ctx->len); |
| 540 | af_alg_release_parent(sk); |
| 541 | } |
| 542 | |
Stephan Mueller | 2a2a251 | 2017-04-24 11:15:23 +0200 | [diff] [blame] | 543 | static int aead_accept_parent_nokey(void *private, struct sock *sk) |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 544 | { |
Stephan Mueller | 2d97591 | 2017-08-02 07:56:19 +0200 | [diff] [blame] | 545 | struct af_alg_ctx *ctx; |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 546 | struct alg_sock *ask = alg_sk(sk); |
Stephan Mueller | 2a2a251 | 2017-04-24 11:15:23 +0200 | [diff] [blame] | 547 | struct aead_tfm *tfm = private; |
| 548 | struct crypto_aead *aead = tfm->aead; |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 549 | unsigned int len = sizeof(*ctx); |
Stephan Mueller | 2a2a251 | 2017-04-24 11:15:23 +0200 | [diff] [blame] | 550 | unsigned int ivlen = crypto_aead_ivsize(aead); |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 551 | |
| 552 | ctx = sock_kmalloc(sk, len, GFP_KERNEL); |
| 553 | if (!ctx) |
| 554 | return -ENOMEM; |
| 555 | memset(ctx, 0, len); |
| 556 | |
| 557 | ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL); |
| 558 | if (!ctx->iv) { |
| 559 | sock_kfree_s(sk, ctx, len); |
| 560 | return -ENOMEM; |
| 561 | } |
| 562 | memset(ctx->iv, 0, ivlen); |
| 563 | |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 564 | INIT_LIST_HEAD(&ctx->tsgl_list); |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 565 | ctx->len = len; |
| 566 | ctx->used = 0; |
Stephan Mueller | d887c52 | 2017-06-25 17:12:59 +0200 | [diff] [blame] | 567 | ctx->rcvused = 0; |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 568 | ctx->more = 0; |
| 569 | ctx->merge = 0; |
| 570 | ctx->enc = 0; |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 571 | ctx->aead_assoclen = 0; |
Gilad Ben-Yossef | 2c3f8b1 | 2017-10-18 08:00:39 +0100 | [diff] [blame] | 572 | crypto_init_wait(&ctx->wait); |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 573 | |
| 574 | ask->private = ctx; |
| 575 | |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 576 | sk->sk_destruct = aead_sock_destruct; |
| 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
Stephan Mueller | 2a2a251 | 2017-04-24 11:15:23 +0200 | [diff] [blame] | 581 | static int aead_accept_parent(void *private, struct sock *sk) |
| 582 | { |
| 583 | struct aead_tfm *tfm = private; |
| 584 | |
| 585 | if (!tfm->has_key) |
| 586 | return -ENOKEY; |
| 587 | |
| 588 | return aead_accept_parent_nokey(private, sk); |
| 589 | } |
| 590 | |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 591 | static const struct af_alg_type algif_type_aead = { |
| 592 | .bind = aead_bind, |
| 593 | .release = aead_release, |
| 594 | .setkey = aead_setkey, |
| 595 | .setauthsize = aead_setauthsize, |
| 596 | .accept = aead_accept_parent, |
Stephan Mueller | 2a2a251 | 2017-04-24 11:15:23 +0200 | [diff] [blame] | 597 | .accept_nokey = aead_accept_parent_nokey, |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 598 | .ops = &algif_aead_ops, |
Stephan Mueller | 2a2a251 | 2017-04-24 11:15:23 +0200 | [diff] [blame] | 599 | .ops_nokey = &algif_aead_ops_nokey, |
Stephan Mueller | 400c40c | 2015-02-28 20:50:00 +0100 | [diff] [blame] | 600 | .name = "aead", |
| 601 | .owner = THIS_MODULE |
| 602 | }; |
| 603 | |
| 604 | static int __init algif_aead_init(void) |
| 605 | { |
| 606 | return af_alg_register_type(&algif_type_aead); |
| 607 | } |
| 608 | |
| 609 | static void __exit algif_aead_exit(void) |
| 610 | { |
| 611 | int err = af_alg_unregister_type(&algif_type_aead); |
| 612 | BUG_ON(err); |
| 613 | } |
| 614 | |
| 615 | module_init(algif_aead_init); |
| 616 | module_exit(algif_aead_exit); |
| 617 | MODULE_LICENSE("GPL"); |
| 618 | MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>"); |
| 619 | MODULE_DESCRIPTION("AEAD kernel crypto API user space interface"); |