Thomas Gleixner | 1ccea77 | 2019-05-19 15:51:43 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 2 | /* Algorithms supported by virtio crypto device |
| 3 | * |
| 4 | * Authors: Gonglei <arei.gonglei@huawei.com> |
| 5 | * |
| 6 | * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD. |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/scatterlist.h> |
| 10 | #include <crypto/algapi.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <crypto/scatterwalk.h> |
| 13 | #include <linux/atomic.h> |
| 14 | |
| 15 | #include <uapi/linux/virtio_crypto.h> |
| 16 | #include "virtio_crypto_common.h" |
| 17 | |
Zeng, Xin | d31e712 | 2017-06-23 11:31:19 -0400 | [diff] [blame] | 18 | |
| 19 | struct virtio_crypto_ablkcipher_ctx { |
Corentin LABBE | 67189375 | 2018-01-26 20:15:32 +0100 | [diff] [blame] | 20 | struct crypto_engine_ctx enginectx; |
Zeng, Xin | d31e712 | 2017-06-23 11:31:19 -0400 | [diff] [blame] | 21 | struct virtio_crypto *vcrypto; |
| 22 | struct crypto_tfm *tfm; |
| 23 | |
| 24 | struct virtio_crypto_sym_session_info enc_sess_info; |
| 25 | struct virtio_crypto_sym_session_info dec_sess_info; |
| 26 | }; |
| 27 | |
| 28 | struct virtio_crypto_sym_request { |
| 29 | struct virtio_crypto_request base; |
| 30 | |
| 31 | /* Cipher or aead */ |
| 32 | uint32_t type; |
| 33 | struct virtio_crypto_ablkcipher_ctx *ablkcipher_ctx; |
| 34 | struct ablkcipher_request *ablkcipher_req; |
| 35 | uint8_t *iv; |
| 36 | /* Encryption? */ |
| 37 | bool encrypt; |
| 38 | }; |
| 39 | |
Farhan Ali | d0d859b | 2018-06-19 11:41:34 -0400 | [diff] [blame] | 40 | struct virtio_crypto_algo { |
| 41 | uint32_t algonum; |
| 42 | uint32_t service; |
| 43 | unsigned int active_devs; |
| 44 | struct crypto_alg algo; |
| 45 | }; |
| 46 | |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 47 | /* |
| 48 | * The algs_lock protects the below global virtio_crypto_active_devs |
| 49 | * and crypto algorithms registion. |
| 50 | */ |
| 51 | static DEFINE_MUTEX(algs_lock); |
Zeng, Xin | d31e712 | 2017-06-23 11:31:19 -0400 | [diff] [blame] | 52 | static void virtio_crypto_ablkcipher_finalize_req( |
| 53 | struct virtio_crypto_sym_request *vc_sym_req, |
| 54 | struct ablkcipher_request *req, |
| 55 | int err); |
| 56 | |
| 57 | static void virtio_crypto_dataq_sym_callback |
| 58 | (struct virtio_crypto_request *vc_req, int len) |
| 59 | { |
| 60 | struct virtio_crypto_sym_request *vc_sym_req = |
| 61 | container_of(vc_req, struct virtio_crypto_sym_request, base); |
| 62 | struct ablkcipher_request *ablk_req; |
| 63 | int error; |
| 64 | |
| 65 | /* Finish the encrypt or decrypt process */ |
| 66 | if (vc_sym_req->type == VIRTIO_CRYPTO_SYM_OP_CIPHER) { |
| 67 | switch (vc_req->status) { |
| 68 | case VIRTIO_CRYPTO_OK: |
| 69 | error = 0; |
| 70 | break; |
| 71 | case VIRTIO_CRYPTO_INVSESS: |
| 72 | case VIRTIO_CRYPTO_ERR: |
| 73 | error = -EINVAL; |
| 74 | break; |
| 75 | case VIRTIO_CRYPTO_BADMSG: |
| 76 | error = -EBADMSG; |
| 77 | break; |
| 78 | default: |
| 79 | error = -EIO; |
| 80 | break; |
| 81 | } |
| 82 | ablk_req = vc_sym_req->ablkcipher_req; |
| 83 | virtio_crypto_ablkcipher_finalize_req(vc_sym_req, |
| 84 | ablk_req, error); |
| 85 | } |
| 86 | } |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 87 | |
| 88 | static u64 virtio_crypto_alg_sg_nents_length(struct scatterlist *sg) |
| 89 | { |
| 90 | u64 total = 0; |
| 91 | |
| 92 | for (total = 0; sg; sg = sg_next(sg)) |
| 93 | total += sg->length; |
| 94 | |
| 95 | return total; |
| 96 | } |
| 97 | |
| 98 | static int |
| 99 | virtio_crypto_alg_validate_key(int key_len, uint32_t *alg) |
| 100 | { |
| 101 | switch (key_len) { |
| 102 | case AES_KEYSIZE_128: |
| 103 | case AES_KEYSIZE_192: |
| 104 | case AES_KEYSIZE_256: |
| 105 | *alg = VIRTIO_CRYPTO_CIPHER_AES_CBC; |
| 106 | break; |
| 107 | default: |
| 108 | pr_err("virtio_crypto: Unsupported key length: %d\n", |
| 109 | key_len); |
| 110 | return -EINVAL; |
| 111 | } |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int virtio_crypto_alg_ablkcipher_init_session( |
| 116 | struct virtio_crypto_ablkcipher_ctx *ctx, |
| 117 | uint32_t alg, const uint8_t *key, |
| 118 | unsigned int keylen, |
| 119 | int encrypt) |
| 120 | { |
| 121 | struct scatterlist outhdr, key_sg, inhdr, *sgs[3]; |
| 122 | unsigned int tmp; |
| 123 | struct virtio_crypto *vcrypto = ctx->vcrypto; |
| 124 | int op = encrypt ? VIRTIO_CRYPTO_OP_ENCRYPT : VIRTIO_CRYPTO_OP_DECRYPT; |
| 125 | int err; |
| 126 | unsigned int num_out = 0, num_in = 0; |
| 127 | |
| 128 | /* |
| 129 | * Avoid to do DMA from the stack, switch to using |
| 130 | * dynamically-allocated for the key |
| 131 | */ |
| 132 | uint8_t *cipher_key = kmalloc(keylen, GFP_ATOMIC); |
| 133 | |
| 134 | if (!cipher_key) |
| 135 | return -ENOMEM; |
| 136 | |
| 137 | memcpy(cipher_key, key, keylen); |
| 138 | |
| 139 | spin_lock(&vcrypto->ctrl_lock); |
| 140 | /* Pad ctrl header */ |
| 141 | vcrypto->ctrl.header.opcode = |
| 142 | cpu_to_le32(VIRTIO_CRYPTO_CIPHER_CREATE_SESSION); |
| 143 | vcrypto->ctrl.header.algo = cpu_to_le32(alg); |
| 144 | /* Set the default dataqueue id to 0 */ |
| 145 | vcrypto->ctrl.header.queue_id = 0; |
| 146 | |
| 147 | vcrypto->input.status = cpu_to_le32(VIRTIO_CRYPTO_ERR); |
| 148 | /* Pad cipher's parameters */ |
| 149 | vcrypto->ctrl.u.sym_create_session.op_type = |
| 150 | cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER); |
| 151 | vcrypto->ctrl.u.sym_create_session.u.cipher.para.algo = |
| 152 | vcrypto->ctrl.header.algo; |
| 153 | vcrypto->ctrl.u.sym_create_session.u.cipher.para.keylen = |
| 154 | cpu_to_le32(keylen); |
| 155 | vcrypto->ctrl.u.sym_create_session.u.cipher.para.op = |
| 156 | cpu_to_le32(op); |
| 157 | |
| 158 | sg_init_one(&outhdr, &vcrypto->ctrl, sizeof(vcrypto->ctrl)); |
| 159 | sgs[num_out++] = &outhdr; |
| 160 | |
| 161 | /* Set key */ |
| 162 | sg_init_one(&key_sg, cipher_key, keylen); |
| 163 | sgs[num_out++] = &key_sg; |
| 164 | |
| 165 | /* Return status and session id back */ |
| 166 | sg_init_one(&inhdr, &vcrypto->input, sizeof(vcrypto->input)); |
| 167 | sgs[num_out + num_in++] = &inhdr; |
| 168 | |
| 169 | err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out, |
| 170 | num_in, vcrypto, GFP_ATOMIC); |
| 171 | if (err < 0) { |
| 172 | spin_unlock(&vcrypto->ctrl_lock); |
| 173 | kzfree(cipher_key); |
| 174 | return err; |
| 175 | } |
| 176 | virtqueue_kick(vcrypto->ctrl_vq); |
| 177 | |
| 178 | /* |
| 179 | * Trapping into the hypervisor, so the request should be |
| 180 | * handled immediately. |
| 181 | */ |
| 182 | while (!virtqueue_get_buf(vcrypto->ctrl_vq, &tmp) && |
| 183 | !virtqueue_is_broken(vcrypto->ctrl_vq)) |
| 184 | cpu_relax(); |
| 185 | |
| 186 | if (le32_to_cpu(vcrypto->input.status) != VIRTIO_CRYPTO_OK) { |
| 187 | spin_unlock(&vcrypto->ctrl_lock); |
| 188 | pr_err("virtio_crypto: Create session failed status: %u\n", |
| 189 | le32_to_cpu(vcrypto->input.status)); |
| 190 | kzfree(cipher_key); |
| 191 | return -EINVAL; |
| 192 | } |
| 193 | |
| 194 | if (encrypt) |
| 195 | ctx->enc_sess_info.session_id = |
| 196 | le64_to_cpu(vcrypto->input.session_id); |
| 197 | else |
| 198 | ctx->dec_sess_info.session_id = |
| 199 | le64_to_cpu(vcrypto->input.session_id); |
| 200 | |
| 201 | spin_unlock(&vcrypto->ctrl_lock); |
| 202 | |
| 203 | kzfree(cipher_key); |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static int virtio_crypto_alg_ablkcipher_close_session( |
| 208 | struct virtio_crypto_ablkcipher_ctx *ctx, |
| 209 | int encrypt) |
| 210 | { |
| 211 | struct scatterlist outhdr, status_sg, *sgs[2]; |
| 212 | unsigned int tmp; |
| 213 | struct virtio_crypto_destroy_session_req *destroy_session; |
| 214 | struct virtio_crypto *vcrypto = ctx->vcrypto; |
| 215 | int err; |
| 216 | unsigned int num_out = 0, num_in = 0; |
| 217 | |
| 218 | spin_lock(&vcrypto->ctrl_lock); |
| 219 | vcrypto->ctrl_status.status = VIRTIO_CRYPTO_ERR; |
| 220 | /* Pad ctrl header */ |
| 221 | vcrypto->ctrl.header.opcode = |
| 222 | cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION); |
| 223 | /* Set the default virtqueue id to 0 */ |
| 224 | vcrypto->ctrl.header.queue_id = 0; |
| 225 | |
| 226 | destroy_session = &vcrypto->ctrl.u.destroy_session; |
| 227 | |
| 228 | if (encrypt) |
| 229 | destroy_session->session_id = |
| 230 | cpu_to_le64(ctx->enc_sess_info.session_id); |
| 231 | else |
| 232 | destroy_session->session_id = |
| 233 | cpu_to_le64(ctx->dec_sess_info.session_id); |
| 234 | |
| 235 | sg_init_one(&outhdr, &vcrypto->ctrl, sizeof(vcrypto->ctrl)); |
| 236 | sgs[num_out++] = &outhdr; |
| 237 | |
| 238 | /* Return status and session id back */ |
| 239 | sg_init_one(&status_sg, &vcrypto->ctrl_status.status, |
| 240 | sizeof(vcrypto->ctrl_status.status)); |
| 241 | sgs[num_out + num_in++] = &status_sg; |
| 242 | |
| 243 | err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out, |
| 244 | num_in, vcrypto, GFP_ATOMIC); |
| 245 | if (err < 0) { |
| 246 | spin_unlock(&vcrypto->ctrl_lock); |
| 247 | return err; |
| 248 | } |
| 249 | virtqueue_kick(vcrypto->ctrl_vq); |
| 250 | |
| 251 | while (!virtqueue_get_buf(vcrypto->ctrl_vq, &tmp) && |
| 252 | !virtqueue_is_broken(vcrypto->ctrl_vq)) |
| 253 | cpu_relax(); |
| 254 | |
| 255 | if (vcrypto->ctrl_status.status != VIRTIO_CRYPTO_OK) { |
| 256 | spin_unlock(&vcrypto->ctrl_lock); |
| 257 | pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n", |
| 258 | vcrypto->ctrl_status.status, |
| 259 | destroy_session->session_id); |
| 260 | |
| 261 | return -EINVAL; |
| 262 | } |
| 263 | spin_unlock(&vcrypto->ctrl_lock); |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static int virtio_crypto_alg_ablkcipher_init_sessions( |
| 269 | struct virtio_crypto_ablkcipher_ctx *ctx, |
| 270 | const uint8_t *key, unsigned int keylen) |
| 271 | { |
| 272 | uint32_t alg; |
| 273 | int ret; |
| 274 | struct virtio_crypto *vcrypto = ctx->vcrypto; |
| 275 | |
| 276 | if (keylen > vcrypto->max_cipher_key_len) { |
| 277 | pr_err("virtio_crypto: the key is too long\n"); |
| 278 | goto bad_key; |
| 279 | } |
| 280 | |
| 281 | if (virtio_crypto_alg_validate_key(keylen, &alg)) |
| 282 | goto bad_key; |
| 283 | |
| 284 | /* Create encryption session */ |
| 285 | ret = virtio_crypto_alg_ablkcipher_init_session(ctx, |
| 286 | alg, key, keylen, 1); |
| 287 | if (ret) |
| 288 | return ret; |
| 289 | /* Create decryption session */ |
| 290 | ret = virtio_crypto_alg_ablkcipher_init_session(ctx, |
| 291 | alg, key, keylen, 0); |
| 292 | if (ret) { |
| 293 | virtio_crypto_alg_ablkcipher_close_session(ctx, 1); |
| 294 | return ret; |
| 295 | } |
| 296 | return 0; |
| 297 | |
| 298 | bad_key: |
| 299 | crypto_tfm_set_flags(ctx->tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 300 | return -EINVAL; |
| 301 | } |
| 302 | |
| 303 | /* Note: kernel crypto API realization */ |
| 304 | static int virtio_crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm, |
| 305 | const uint8_t *key, |
| 306 | unsigned int keylen) |
| 307 | { |
| 308 | struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(tfm); |
Farhan Ali | d0d859b | 2018-06-19 11:41:34 -0400 | [diff] [blame] | 309 | uint32_t alg; |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 310 | int ret; |
| 311 | |
Farhan Ali | d0d859b | 2018-06-19 11:41:34 -0400 | [diff] [blame] | 312 | ret = virtio_crypto_alg_validate_key(keylen, &alg); |
| 313 | if (ret) |
| 314 | return ret; |
| 315 | |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 316 | if (!ctx->vcrypto) { |
| 317 | /* New key */ |
| 318 | int node = virtio_crypto_get_current_node(); |
| 319 | struct virtio_crypto *vcrypto = |
Farhan Ali | d0d859b | 2018-06-19 11:41:34 -0400 | [diff] [blame] | 320 | virtcrypto_get_dev_node(node, |
| 321 | VIRTIO_CRYPTO_SERVICE_CIPHER, alg); |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 322 | if (!vcrypto) { |
Farhan Ali | d0d859b | 2018-06-19 11:41:34 -0400 | [diff] [blame] | 323 | pr_err("virtio_crypto: Could not find a virtio device in the system or unsupported algo\n"); |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 324 | return -ENODEV; |
| 325 | } |
| 326 | |
| 327 | ctx->vcrypto = vcrypto; |
| 328 | } else { |
| 329 | /* Rekeying, we should close the created sessions previously */ |
| 330 | virtio_crypto_alg_ablkcipher_close_session(ctx, 1); |
| 331 | virtio_crypto_alg_ablkcipher_close_session(ctx, 0); |
| 332 | } |
| 333 | |
| 334 | ret = virtio_crypto_alg_ablkcipher_init_sessions(ctx, key, keylen); |
| 335 | if (ret) { |
| 336 | virtcrypto_dev_put(ctx->vcrypto); |
| 337 | ctx->vcrypto = NULL; |
| 338 | |
| 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static int |
Zeng, Xin | d31e712 | 2017-06-23 11:31:19 -0400 | [diff] [blame] | 346 | __virtio_crypto_ablkcipher_do_req(struct virtio_crypto_sym_request *vc_sym_req, |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 347 | struct ablkcipher_request *req, |
Gonglei \(Arei\) | d79b5d0 | 2016-12-27 14:49:07 +0800 | [diff] [blame] | 348 | struct data_queue *data_vq) |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 349 | { |
| 350 | struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req); |
Zeng, Xin | d31e712 | 2017-06-23 11:31:19 -0400 | [diff] [blame] | 351 | struct virtio_crypto_ablkcipher_ctx *ctx = vc_sym_req->ablkcipher_ctx; |
| 352 | struct virtio_crypto_request *vc_req = &vc_sym_req->base; |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 353 | unsigned int ivsize = crypto_ablkcipher_ivsize(tfm); |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 354 | struct virtio_crypto *vcrypto = ctx->vcrypto; |
| 355 | struct virtio_crypto_op_data_req *req_data; |
| 356 | int src_nents, dst_nents; |
| 357 | int err; |
| 358 | unsigned long flags; |
| 359 | struct scatterlist outhdr, iv_sg, status_sg, **sgs; |
| 360 | int i; |
| 361 | u64 dst_len; |
| 362 | unsigned int num_out = 0, num_in = 0; |
| 363 | int sg_total; |
| 364 | uint8_t *iv; |
| 365 | |
| 366 | src_nents = sg_nents_for_len(req->src, req->nbytes); |
| 367 | dst_nents = sg_nents(req->dst); |
| 368 | |
| 369 | pr_debug("virtio_crypto: Number of sgs (src_nents: %d, dst_nents: %d)\n", |
| 370 | src_nents, dst_nents); |
| 371 | |
| 372 | /* Why 3? outhdr + iv + inhdr */ |
| 373 | sg_total = src_nents + dst_nents + 3; |
Jia-Ju Bai | f6adeef | 2018-07-23 16:43:46 +0800 | [diff] [blame] | 374 | sgs = kcalloc_node(sg_total, sizeof(*sgs), GFP_KERNEL, |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 375 | dev_to_node(&vcrypto->vdev->dev)); |
| 376 | if (!sgs) |
| 377 | return -ENOMEM; |
| 378 | |
Jia-Ju Bai | f6adeef | 2018-07-23 16:43:46 +0800 | [diff] [blame] | 379 | req_data = kzalloc_node(sizeof(*req_data), GFP_KERNEL, |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 380 | dev_to_node(&vcrypto->vdev->dev)); |
| 381 | if (!req_data) { |
| 382 | kfree(sgs); |
| 383 | return -ENOMEM; |
| 384 | } |
| 385 | |
| 386 | vc_req->req_data = req_data; |
Zeng, Xin | d31e712 | 2017-06-23 11:31:19 -0400 | [diff] [blame] | 387 | vc_sym_req->type = VIRTIO_CRYPTO_SYM_OP_CIPHER; |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 388 | /* Head of operation */ |
Zeng, Xin | d31e712 | 2017-06-23 11:31:19 -0400 | [diff] [blame] | 389 | if (vc_sym_req->encrypt) { |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 390 | req_data->header.session_id = |
| 391 | cpu_to_le64(ctx->enc_sess_info.session_id); |
| 392 | req_data->header.opcode = |
| 393 | cpu_to_le32(VIRTIO_CRYPTO_CIPHER_ENCRYPT); |
| 394 | } else { |
| 395 | req_data->header.session_id = |
| 396 | cpu_to_le64(ctx->dec_sess_info.session_id); |
Colin Ian King | 1bb64d8 | 2018-12-30 13:46:21 +0000 | [diff] [blame] | 397 | req_data->header.opcode = |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 398 | cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DECRYPT); |
| 399 | } |
| 400 | req_data->u.sym_req.op_type = cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER); |
| 401 | req_data->u.sym_req.u.cipher.para.iv_len = cpu_to_le32(ivsize); |
| 402 | req_data->u.sym_req.u.cipher.para.src_data_len = |
| 403 | cpu_to_le32(req->nbytes); |
| 404 | |
| 405 | dst_len = virtio_crypto_alg_sg_nents_length(req->dst); |
| 406 | if (unlikely(dst_len > U32_MAX)) { |
| 407 | pr_err("virtio_crypto: The dst_len is beyond U32_MAX\n"); |
| 408 | err = -EINVAL; |
| 409 | goto free; |
| 410 | } |
| 411 | |
| 412 | pr_debug("virtio_crypto: src_len: %u, dst_len: %llu\n", |
| 413 | req->nbytes, dst_len); |
| 414 | |
| 415 | if (unlikely(req->nbytes + dst_len + ivsize + |
| 416 | sizeof(vc_req->status) > vcrypto->max_size)) { |
| 417 | pr_err("virtio_crypto: The length is too big\n"); |
| 418 | err = -EINVAL; |
| 419 | goto free; |
| 420 | } |
| 421 | |
| 422 | req_data->u.sym_req.u.cipher.para.dst_data_len = |
| 423 | cpu_to_le32((uint32_t)dst_len); |
| 424 | |
| 425 | /* Outhdr */ |
| 426 | sg_init_one(&outhdr, req_data, sizeof(*req_data)); |
| 427 | sgs[num_out++] = &outhdr; |
| 428 | |
| 429 | /* IV */ |
| 430 | |
| 431 | /* |
| 432 | * Avoid to do DMA from the stack, switch to using |
| 433 | * dynamically-allocated for the IV |
| 434 | */ |
| 435 | iv = kzalloc_node(ivsize, GFP_ATOMIC, |
| 436 | dev_to_node(&vcrypto->vdev->dev)); |
| 437 | if (!iv) { |
| 438 | err = -ENOMEM; |
| 439 | goto free; |
| 440 | } |
| 441 | memcpy(iv, req->info, ivsize); |
| 442 | sg_init_one(&iv_sg, iv, ivsize); |
| 443 | sgs[num_out++] = &iv_sg; |
Zeng, Xin | d31e712 | 2017-06-23 11:31:19 -0400 | [diff] [blame] | 444 | vc_sym_req->iv = iv; |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 445 | |
| 446 | /* Source data */ |
| 447 | for (i = 0; i < src_nents; i++) |
| 448 | sgs[num_out++] = &req->src[i]; |
| 449 | |
| 450 | /* Destination data */ |
| 451 | for (i = 0; i < dst_nents; i++) |
| 452 | sgs[num_out + num_in++] = &req->dst[i]; |
| 453 | |
| 454 | /* Status */ |
| 455 | sg_init_one(&status_sg, &vc_req->status, sizeof(vc_req->status)); |
| 456 | sgs[num_out + num_in++] = &status_sg; |
| 457 | |
| 458 | vc_req->sgs = sgs; |
| 459 | |
| 460 | spin_lock_irqsave(&data_vq->lock, flags); |
| 461 | err = virtqueue_add_sgs(data_vq->vq, sgs, num_out, |
| 462 | num_in, vc_req, GFP_ATOMIC); |
| 463 | virtqueue_kick(data_vq->vq); |
| 464 | spin_unlock_irqrestore(&data_vq->lock, flags); |
| 465 | if (unlikely(err < 0)) |
| 466 | goto free_iv; |
| 467 | |
| 468 | return 0; |
| 469 | |
| 470 | free_iv: |
| 471 | kzfree(iv); |
| 472 | free: |
| 473 | kzfree(req_data); |
| 474 | kfree(sgs); |
| 475 | return err; |
| 476 | } |
| 477 | |
| 478 | static int virtio_crypto_ablkcipher_encrypt(struct ablkcipher_request *req) |
| 479 | { |
| 480 | struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req); |
| 481 | struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm); |
Zeng, Xin | d31e712 | 2017-06-23 11:31:19 -0400 | [diff] [blame] | 482 | struct virtio_crypto_sym_request *vc_sym_req = |
| 483 | ablkcipher_request_ctx(req); |
| 484 | struct virtio_crypto_request *vc_req = &vc_sym_req->base; |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 485 | struct virtio_crypto *vcrypto = ctx->vcrypto; |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 486 | /* Use the first data virtqueue as default */ |
| 487 | struct data_queue *data_vq = &vcrypto->data_vq[0]; |
| 488 | |
Gonglei \(Arei\) | d79b5d0 | 2016-12-27 14:49:07 +0800 | [diff] [blame] | 489 | vc_req->dataq = data_vq; |
Zeng, Xin | d31e712 | 2017-06-23 11:31:19 -0400 | [diff] [blame] | 490 | vc_req->alg_cb = virtio_crypto_dataq_sym_callback; |
| 491 | vc_sym_req->ablkcipher_ctx = ctx; |
| 492 | vc_sym_req->ablkcipher_req = req; |
| 493 | vc_sym_req->encrypt = true; |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 494 | |
Corentin LABBE | 67189375 | 2018-01-26 20:15:32 +0100 | [diff] [blame] | 495 | return crypto_transfer_ablkcipher_request_to_engine(data_vq->engine, req); |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | static int virtio_crypto_ablkcipher_decrypt(struct ablkcipher_request *req) |
| 499 | { |
| 500 | struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req); |
| 501 | struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm); |
Zeng, Xin | d31e712 | 2017-06-23 11:31:19 -0400 | [diff] [blame] | 502 | struct virtio_crypto_sym_request *vc_sym_req = |
| 503 | ablkcipher_request_ctx(req); |
| 504 | struct virtio_crypto_request *vc_req = &vc_sym_req->base; |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 505 | struct virtio_crypto *vcrypto = ctx->vcrypto; |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 506 | /* Use the first data virtqueue as default */ |
| 507 | struct data_queue *data_vq = &vcrypto->data_vq[0]; |
| 508 | |
Gonglei \(Arei\) | d79b5d0 | 2016-12-27 14:49:07 +0800 | [diff] [blame] | 509 | vc_req->dataq = data_vq; |
Zeng, Xin | d31e712 | 2017-06-23 11:31:19 -0400 | [diff] [blame] | 510 | vc_req->alg_cb = virtio_crypto_dataq_sym_callback; |
| 511 | vc_sym_req->ablkcipher_ctx = ctx; |
| 512 | vc_sym_req->ablkcipher_req = req; |
| 513 | vc_sym_req->encrypt = false; |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 514 | |
Corentin LABBE | 67189375 | 2018-01-26 20:15:32 +0100 | [diff] [blame] | 515 | return crypto_transfer_ablkcipher_request_to_engine(data_vq->engine, req); |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | static int virtio_crypto_ablkcipher_init(struct crypto_tfm *tfm) |
| 519 | { |
| 520 | struct virtio_crypto_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm); |
| 521 | |
Zeng, Xin | d31e712 | 2017-06-23 11:31:19 -0400 | [diff] [blame] | 522 | tfm->crt_ablkcipher.reqsize = sizeof(struct virtio_crypto_sym_request); |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 523 | ctx->tfm = tfm; |
| 524 | |
Corentin LABBE | 67189375 | 2018-01-26 20:15:32 +0100 | [diff] [blame] | 525 | ctx->enginectx.op.do_one_request = virtio_crypto_ablkcipher_crypt_req; |
| 526 | ctx->enginectx.op.prepare_request = NULL; |
| 527 | ctx->enginectx.op.unprepare_request = NULL; |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | static void virtio_crypto_ablkcipher_exit(struct crypto_tfm *tfm) |
| 532 | { |
| 533 | struct virtio_crypto_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm); |
| 534 | |
| 535 | if (!ctx->vcrypto) |
| 536 | return; |
| 537 | |
| 538 | virtio_crypto_alg_ablkcipher_close_session(ctx, 1); |
| 539 | virtio_crypto_alg_ablkcipher_close_session(ctx, 0); |
| 540 | virtcrypto_dev_put(ctx->vcrypto); |
| 541 | ctx->vcrypto = NULL; |
| 542 | } |
| 543 | |
Gonglei \(Arei\) | d79b5d0 | 2016-12-27 14:49:07 +0800 | [diff] [blame] | 544 | int virtio_crypto_ablkcipher_crypt_req( |
Corentin LABBE | 67189375 | 2018-01-26 20:15:32 +0100 | [diff] [blame] | 545 | struct crypto_engine *engine, void *vreq) |
Gonglei \(Arei\) | d79b5d0 | 2016-12-27 14:49:07 +0800 | [diff] [blame] | 546 | { |
Corentin LABBE | 67189375 | 2018-01-26 20:15:32 +0100 | [diff] [blame] | 547 | struct ablkcipher_request *req = container_of(vreq, struct ablkcipher_request, base); |
Zeng, Xin | d31e712 | 2017-06-23 11:31:19 -0400 | [diff] [blame] | 548 | struct virtio_crypto_sym_request *vc_sym_req = |
| 549 | ablkcipher_request_ctx(req); |
| 550 | struct virtio_crypto_request *vc_req = &vc_sym_req->base; |
Gonglei \(Arei\) | d79b5d0 | 2016-12-27 14:49:07 +0800 | [diff] [blame] | 551 | struct data_queue *data_vq = vc_req->dataq; |
| 552 | int ret; |
| 553 | |
Zeng, Xin | d31e712 | 2017-06-23 11:31:19 -0400 | [diff] [blame] | 554 | ret = __virtio_crypto_ablkcipher_do_req(vc_sym_req, req, data_vq); |
Gonglei \(Arei\) | d79b5d0 | 2016-12-27 14:49:07 +0800 | [diff] [blame] | 555 | if (ret < 0) |
| 556 | return ret; |
| 557 | |
| 558 | virtqueue_kick(data_vq->vq); |
| 559 | |
| 560 | return 0; |
| 561 | } |
| 562 | |
Zeng, Xin | d31e712 | 2017-06-23 11:31:19 -0400 | [diff] [blame] | 563 | static void virtio_crypto_ablkcipher_finalize_req( |
| 564 | struct virtio_crypto_sym_request *vc_sym_req, |
Gonglei \(Arei\) | d79b5d0 | 2016-12-27 14:49:07 +0800 | [diff] [blame] | 565 | struct ablkcipher_request *req, |
| 566 | int err) |
| 567 | { |
Corentin LABBE | 67189375 | 2018-01-26 20:15:32 +0100 | [diff] [blame] | 568 | crypto_finalize_ablkcipher_request(vc_sym_req->base.dataq->engine, |
| 569 | req, err); |
Zeng, Xin | d31e712 | 2017-06-23 11:31:19 -0400 | [diff] [blame] | 570 | kzfree(vc_sym_req->iv); |
| 571 | virtcrypto_clear_request(&vc_sym_req->base); |
Gonglei \(Arei\) | d79b5d0 | 2016-12-27 14:49:07 +0800 | [diff] [blame] | 572 | } |
| 573 | |
Farhan Ali | d0d859b | 2018-06-19 11:41:34 -0400 | [diff] [blame] | 574 | static struct virtio_crypto_algo virtio_crypto_algs[] = { { |
| 575 | .algonum = VIRTIO_CRYPTO_CIPHER_AES_CBC, |
| 576 | .service = VIRTIO_CRYPTO_SERVICE_CIPHER, |
| 577 | .algo = { |
| 578 | .cra_name = "cbc(aes)", |
| 579 | .cra_driver_name = "virtio_crypto_aes_cbc", |
| 580 | .cra_priority = 150, |
| 581 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 582 | .cra_blocksize = AES_BLOCK_SIZE, |
| 583 | .cra_ctxsize = sizeof(struct virtio_crypto_ablkcipher_ctx), |
| 584 | .cra_alignmask = 0, |
| 585 | .cra_module = THIS_MODULE, |
| 586 | .cra_type = &crypto_ablkcipher_type, |
| 587 | .cra_init = virtio_crypto_ablkcipher_init, |
| 588 | .cra_exit = virtio_crypto_ablkcipher_exit, |
| 589 | .cra_u = { |
| 590 | .ablkcipher = { |
| 591 | .setkey = virtio_crypto_ablkcipher_setkey, |
| 592 | .decrypt = virtio_crypto_ablkcipher_decrypt, |
| 593 | .encrypt = virtio_crypto_ablkcipher_encrypt, |
| 594 | .min_keysize = AES_MIN_KEY_SIZE, |
| 595 | .max_keysize = AES_MAX_KEY_SIZE, |
| 596 | .ivsize = AES_BLOCK_SIZE, |
| 597 | }, |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 598 | }, |
| 599 | }, |
| 600 | } }; |
| 601 | |
Farhan Ali | d0d859b | 2018-06-19 11:41:34 -0400 | [diff] [blame] | 602 | int virtio_crypto_algs_register(struct virtio_crypto *vcrypto) |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 603 | { |
| 604 | int ret = 0; |
Farhan Ali | d0d859b | 2018-06-19 11:41:34 -0400 | [diff] [blame] | 605 | int i = 0; |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 606 | |
| 607 | mutex_lock(&algs_lock); |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 608 | |
Farhan Ali | d0d859b | 2018-06-19 11:41:34 -0400 | [diff] [blame] | 609 | for (i = 0; i < ARRAY_SIZE(virtio_crypto_algs); i++) { |
| 610 | |
| 611 | uint32_t service = virtio_crypto_algs[i].service; |
| 612 | uint32_t algonum = virtio_crypto_algs[i].algonum; |
| 613 | |
| 614 | if (!virtcrypto_algo_is_supported(vcrypto, service, algonum)) |
| 615 | continue; |
| 616 | |
| 617 | if (virtio_crypto_algs[i].active_devs == 0) { |
| 618 | ret = crypto_register_alg(&virtio_crypto_algs[i].algo); |
| 619 | if (ret) |
| 620 | goto unlock; |
| 621 | } |
| 622 | |
| 623 | virtio_crypto_algs[i].active_devs++; |
| 624 | dev_info(&vcrypto->vdev->dev, "Registered algo %s\n", |
| 625 | virtio_crypto_algs[i].algo.cra_name); |
| 626 | } |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 627 | |
| 628 | unlock: |
| 629 | mutex_unlock(&algs_lock); |
| 630 | return ret; |
| 631 | } |
| 632 | |
Farhan Ali | d0d859b | 2018-06-19 11:41:34 -0400 | [diff] [blame] | 633 | void virtio_crypto_algs_unregister(struct virtio_crypto *vcrypto) |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 634 | { |
Farhan Ali | d0d859b | 2018-06-19 11:41:34 -0400 | [diff] [blame] | 635 | int i = 0; |
| 636 | |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 637 | mutex_lock(&algs_lock); |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 638 | |
Farhan Ali | d0d859b | 2018-06-19 11:41:34 -0400 | [diff] [blame] | 639 | for (i = 0; i < ARRAY_SIZE(virtio_crypto_algs); i++) { |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 640 | |
Farhan Ali | d0d859b | 2018-06-19 11:41:34 -0400 | [diff] [blame] | 641 | uint32_t service = virtio_crypto_algs[i].service; |
| 642 | uint32_t algonum = virtio_crypto_algs[i].algonum; |
| 643 | |
| 644 | if (virtio_crypto_algs[i].active_devs == 0 || |
| 645 | !virtcrypto_algo_is_supported(vcrypto, service, algonum)) |
| 646 | continue; |
| 647 | |
| 648 | if (virtio_crypto_algs[i].active_devs == 1) |
| 649 | crypto_unregister_alg(&virtio_crypto_algs[i].algo); |
| 650 | |
| 651 | virtio_crypto_algs[i].active_devs--; |
| 652 | } |
| 653 | |
Gonglei | dbaf062 | 2016-12-15 10:03:16 +0800 | [diff] [blame] | 654 | mutex_unlock(&algs_lock); |
| 655 | } |