1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Asymmetric algorithms supported by virtio crypto device
3 *
4 * Authors: zhenwei pi <pizhenwei@bytedance.com>
5 * lei he <helei.sig11@bytedance.com>
6 *
7 * Copyright 2022 Bytedance CO., LTD.
8 */
9
10 #include <linux/mpi.h>
11 #include <linux/scatterlist.h>
12 #include <crypto/algapi.h>
13 #include <crypto/internal/akcipher.h>
14 #include <crypto/internal/rsa.h>
15 #include <linux/err.h>
16 #include <crypto/scatterwalk.h>
17 #include <linux/atomic.h>
18
19 #include <uapi/linux/virtio_crypto.h>
20 #include "virtio_crypto_common.h"
21
22 struct virtio_crypto_rsa_ctx {
23 MPI n;
24 };
25
26 struct virtio_crypto_akcipher_ctx {
27 struct crypto_engine_ctx enginectx;
28 struct virtio_crypto *vcrypto;
29 struct crypto_akcipher *tfm;
30 bool session_valid;
31 __u64 session_id;
32 union {
33 struct virtio_crypto_rsa_ctx rsa_ctx;
34 };
35 };
36
37 struct virtio_crypto_akcipher_request {
38 struct virtio_crypto_request base;
39 struct virtio_crypto_akcipher_ctx *akcipher_ctx;
40 struct akcipher_request *akcipher_req;
41 void *src_buf;
42 void *dst_buf;
43 uint32_t opcode;
44 };
45
46 struct virtio_crypto_akcipher_algo {
47 uint32_t algonum;
48 uint32_t service;
49 unsigned int active_devs;
50 struct akcipher_alg algo;
51 };
52
53 static DEFINE_MUTEX(algs_lock);
54
virtio_crypto_akcipher_finalize_req(struct virtio_crypto_akcipher_request * vc_akcipher_req,struct akcipher_request * req,int err)55 static void virtio_crypto_akcipher_finalize_req(
56 struct virtio_crypto_akcipher_request *vc_akcipher_req,
57 struct akcipher_request *req, int err)
58 {
59 virtcrypto_clear_request(&vc_akcipher_req->base);
60
61 crypto_finalize_akcipher_request(vc_akcipher_req->base.dataq->engine, req, err);
62 }
63
virtio_crypto_dataq_akcipher_callback(struct virtio_crypto_request * vc_req,int len)64 static void virtio_crypto_dataq_akcipher_callback(struct virtio_crypto_request *vc_req, int len)
65 {
66 struct virtio_crypto_akcipher_request *vc_akcipher_req =
67 container_of(vc_req, struct virtio_crypto_akcipher_request, base);
68 struct akcipher_request *akcipher_req;
69 int error;
70
71 switch (vc_req->status) {
72 case VIRTIO_CRYPTO_OK:
73 error = 0;
74 break;
75 case VIRTIO_CRYPTO_INVSESS:
76 case VIRTIO_CRYPTO_ERR:
77 error = -EINVAL;
78 break;
79 case VIRTIO_CRYPTO_BADMSG:
80 error = -EBADMSG;
81 break;
82
83 case VIRTIO_CRYPTO_KEY_REJECTED:
84 error = -EKEYREJECTED;
85 break;
86
87 default:
88 error = -EIO;
89 break;
90 }
91
92 akcipher_req = vc_akcipher_req->akcipher_req;
93 if (vc_akcipher_req->opcode != VIRTIO_CRYPTO_AKCIPHER_VERIFY)
94 sg_copy_from_buffer(akcipher_req->dst, sg_nents(akcipher_req->dst),
95 vc_akcipher_req->dst_buf, akcipher_req->dst_len);
96 virtio_crypto_akcipher_finalize_req(vc_akcipher_req, akcipher_req, error);
97 }
98
virtio_crypto_alg_akcipher_init_session(struct virtio_crypto_akcipher_ctx * ctx,struct virtio_crypto_ctrl_header * header,void * para,const uint8_t * key,unsigned int keylen)99 static int virtio_crypto_alg_akcipher_init_session(struct virtio_crypto_akcipher_ctx *ctx,
100 struct virtio_crypto_ctrl_header *header, void *para,
101 const uint8_t *key, unsigned int keylen)
102 {
103 struct scatterlist outhdr_sg, key_sg, inhdr_sg, *sgs[3];
104 struct virtio_crypto *vcrypto = ctx->vcrypto;
105 uint8_t *pkey;
106 int err;
107 unsigned int num_out = 0, num_in = 0;
108 struct virtio_crypto_op_ctrl_req *ctrl;
109 struct virtio_crypto_session_input *input;
110 struct virtio_crypto_ctrl_request *vc_ctrl_req;
111
112 pkey = kmemdup(key, keylen, GFP_ATOMIC);
113 if (!pkey)
114 return -ENOMEM;
115
116 vc_ctrl_req = kzalloc(sizeof(*vc_ctrl_req), GFP_KERNEL);
117 if (!vc_ctrl_req) {
118 err = -ENOMEM;
119 goto out;
120 }
121
122 ctrl = &vc_ctrl_req->ctrl;
123 memcpy(&ctrl->header, header, sizeof(ctrl->header));
124 memcpy(&ctrl->u, para, sizeof(ctrl->u));
125 input = &vc_ctrl_req->input;
126 input->status = cpu_to_le32(VIRTIO_CRYPTO_ERR);
127
128 sg_init_one(&outhdr_sg, ctrl, sizeof(*ctrl));
129 sgs[num_out++] = &outhdr_sg;
130
131 sg_init_one(&key_sg, pkey, keylen);
132 sgs[num_out++] = &key_sg;
133
134 sg_init_one(&inhdr_sg, input, sizeof(*input));
135 sgs[num_out + num_in++] = &inhdr_sg;
136
137 err = virtio_crypto_ctrl_vq_request(vcrypto, sgs, num_out, num_in, vc_ctrl_req);
138 if (err < 0)
139 goto out;
140
141 if (le32_to_cpu(input->status) != VIRTIO_CRYPTO_OK) {
142 pr_err("virtio_crypto: Create session failed status: %u\n",
143 le32_to_cpu(input->status));
144 err = -EINVAL;
145 goto out;
146 }
147
148 ctx->session_id = le64_to_cpu(input->session_id);
149 ctx->session_valid = true;
150 err = 0;
151
152 out:
153 kfree(vc_ctrl_req);
154 kfree_sensitive(pkey);
155
156 return err;
157 }
158
virtio_crypto_alg_akcipher_close_session(struct virtio_crypto_akcipher_ctx * ctx)159 static int virtio_crypto_alg_akcipher_close_session(struct virtio_crypto_akcipher_ctx *ctx)
160 {
161 struct scatterlist outhdr_sg, inhdr_sg, *sgs[2];
162 struct virtio_crypto_destroy_session_req *destroy_session;
163 struct virtio_crypto *vcrypto = ctx->vcrypto;
164 unsigned int num_out = 0, num_in = 0;
165 int err;
166 struct virtio_crypto_op_ctrl_req *ctrl;
167 struct virtio_crypto_inhdr *ctrl_status;
168 struct virtio_crypto_ctrl_request *vc_ctrl_req;
169
170 if (!ctx->session_valid)
171 return 0;
172
173 vc_ctrl_req = kzalloc(sizeof(*vc_ctrl_req), GFP_KERNEL);
174 if (!vc_ctrl_req)
175 return -ENOMEM;
176
177 ctrl_status = &vc_ctrl_req->ctrl_status;
178 ctrl_status->status = VIRTIO_CRYPTO_ERR;
179 ctrl = &vc_ctrl_req->ctrl;
180 ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION);
181 ctrl->header.queue_id = 0;
182
183 destroy_session = &ctrl->u.destroy_session;
184 destroy_session->session_id = cpu_to_le64(ctx->session_id);
185
186 sg_init_one(&outhdr_sg, ctrl, sizeof(*ctrl));
187 sgs[num_out++] = &outhdr_sg;
188
189 sg_init_one(&inhdr_sg, &ctrl_status->status, sizeof(ctrl_status->status));
190 sgs[num_out + num_in++] = &inhdr_sg;
191
192 err = virtio_crypto_ctrl_vq_request(vcrypto, sgs, num_out, num_in, vc_ctrl_req);
193 if (err < 0)
194 goto out;
195
196 if (ctrl_status->status != VIRTIO_CRYPTO_OK) {
197 pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n",
198 ctrl_status->status, destroy_session->session_id);
199 err = -EINVAL;
200 goto out;
201 }
202
203 err = 0;
204 ctx->session_valid = false;
205
206 out:
207 kfree(vc_ctrl_req);
208
209 return err;
210 }
211
__virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request * vc_akcipher_req,struct akcipher_request * req,struct data_queue * data_vq)212 static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request *vc_akcipher_req,
213 struct akcipher_request *req, struct data_queue *data_vq)
214 {
215 struct virtio_crypto_akcipher_ctx *ctx = vc_akcipher_req->akcipher_ctx;
216 struct virtio_crypto_request *vc_req = &vc_akcipher_req->base;
217 struct virtio_crypto *vcrypto = ctx->vcrypto;
218 struct virtio_crypto_op_data_req *req_data = vc_req->req_data;
219 struct scatterlist *sgs[4], outhdr_sg, inhdr_sg, srcdata_sg, dstdata_sg;
220 void *src_buf = NULL, *dst_buf = NULL;
221 unsigned int num_out = 0, num_in = 0;
222 int node = dev_to_node(&vcrypto->vdev->dev);
223 unsigned long flags;
224 int ret = -ENOMEM;
225 bool verify = vc_akcipher_req->opcode == VIRTIO_CRYPTO_AKCIPHER_VERIFY;
226 unsigned int src_len = verify ? req->src_len + req->dst_len : req->src_len;
227
228 /* out header */
229 sg_init_one(&outhdr_sg, req_data, sizeof(*req_data));
230 sgs[num_out++] = &outhdr_sg;
231
232 /* src data */
233 src_buf = kcalloc_node(src_len, 1, GFP_KERNEL, node);
234 if (!src_buf)
235 goto err;
236
237 if (verify) {
238 /* for verify operation, both src and dst data work as OUT direction */
239 sg_copy_to_buffer(req->src, sg_nents(req->src), src_buf, src_len);
240 sg_init_one(&srcdata_sg, src_buf, src_len);
241 sgs[num_out++] = &srcdata_sg;
242 } else {
243 sg_copy_to_buffer(req->src, sg_nents(req->src), src_buf, src_len);
244 sg_init_one(&srcdata_sg, src_buf, src_len);
245 sgs[num_out++] = &srcdata_sg;
246
247 /* dst data */
248 dst_buf = kcalloc_node(req->dst_len, 1, GFP_KERNEL, node);
249 if (!dst_buf)
250 goto err;
251
252 sg_init_one(&dstdata_sg, dst_buf, req->dst_len);
253 sgs[num_out + num_in++] = &dstdata_sg;
254 }
255
256 vc_akcipher_req->src_buf = src_buf;
257 vc_akcipher_req->dst_buf = dst_buf;
258
259 /* in header */
260 sg_init_one(&inhdr_sg, &vc_req->status, sizeof(vc_req->status));
261 sgs[num_out + num_in++] = &inhdr_sg;
262
263 spin_lock_irqsave(&data_vq->lock, flags);
264 ret = virtqueue_add_sgs(data_vq->vq, sgs, num_out, num_in, vc_req, GFP_ATOMIC);
265 virtqueue_kick(data_vq->vq);
266 spin_unlock_irqrestore(&data_vq->lock, flags);
267 if (ret)
268 goto err;
269
270 return 0;
271
272 err:
273 kfree(src_buf);
274 kfree(dst_buf);
275
276 return -ENOMEM;
277 }
278
virtio_crypto_rsa_do_req(struct crypto_engine * engine,void * vreq)279 static int virtio_crypto_rsa_do_req(struct crypto_engine *engine, void *vreq)
280 {
281 struct akcipher_request *req = container_of(vreq, struct akcipher_request, base);
282 struct virtio_crypto_akcipher_request *vc_akcipher_req = akcipher_request_ctx(req);
283 struct virtio_crypto_request *vc_req = &vc_akcipher_req->base;
284 struct virtio_crypto_akcipher_ctx *ctx = vc_akcipher_req->akcipher_ctx;
285 struct virtio_crypto *vcrypto = ctx->vcrypto;
286 struct data_queue *data_vq = vc_req->dataq;
287 struct virtio_crypto_op_header *header;
288 struct virtio_crypto_akcipher_data_req *akcipher_req;
289 int ret;
290
291 vc_req->sgs = NULL;
292 vc_req->req_data = kzalloc_node(sizeof(*vc_req->req_data),
293 GFP_KERNEL, dev_to_node(&vcrypto->vdev->dev));
294 if (!vc_req->req_data)
295 return -ENOMEM;
296
297 /* build request header */
298 header = &vc_req->req_data->header;
299 header->opcode = cpu_to_le32(vc_akcipher_req->opcode);
300 header->algo = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_RSA);
301 header->session_id = cpu_to_le64(ctx->session_id);
302
303 /* build request akcipher data */
304 akcipher_req = &vc_req->req_data->u.akcipher_req;
305 akcipher_req->para.src_data_len = cpu_to_le32(req->src_len);
306 akcipher_req->para.dst_data_len = cpu_to_le32(req->dst_len);
307
308 ret = __virtio_crypto_akcipher_do_req(vc_akcipher_req, req, data_vq);
309 if (ret < 0) {
310 kfree_sensitive(vc_req->req_data);
311 vc_req->req_data = NULL;
312 return ret;
313 }
314
315 return 0;
316 }
317
virtio_crypto_rsa_req(struct akcipher_request * req,uint32_t opcode)318 static int virtio_crypto_rsa_req(struct akcipher_request *req, uint32_t opcode)
319 {
320 struct crypto_akcipher *atfm = crypto_akcipher_reqtfm(req);
321 struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(atfm);
322 struct virtio_crypto_akcipher_request *vc_akcipher_req = akcipher_request_ctx(req);
323 struct virtio_crypto_request *vc_req = &vc_akcipher_req->base;
324 struct virtio_crypto *vcrypto = ctx->vcrypto;
325 /* Use the first data virtqueue as default */
326 struct data_queue *data_vq = &vcrypto->data_vq[0];
327
328 vc_req->dataq = data_vq;
329 vc_req->alg_cb = virtio_crypto_dataq_akcipher_callback;
330 vc_akcipher_req->akcipher_ctx = ctx;
331 vc_akcipher_req->akcipher_req = req;
332 vc_akcipher_req->opcode = opcode;
333
334 return crypto_transfer_akcipher_request_to_engine(data_vq->engine, req);
335 }
336
virtio_crypto_rsa_encrypt(struct akcipher_request * req)337 static int virtio_crypto_rsa_encrypt(struct akcipher_request *req)
338 {
339 return virtio_crypto_rsa_req(req, VIRTIO_CRYPTO_AKCIPHER_ENCRYPT);
340 }
341
virtio_crypto_rsa_decrypt(struct akcipher_request * req)342 static int virtio_crypto_rsa_decrypt(struct akcipher_request *req)
343 {
344 return virtio_crypto_rsa_req(req, VIRTIO_CRYPTO_AKCIPHER_DECRYPT);
345 }
346
virtio_crypto_rsa_sign(struct akcipher_request * req)347 static int virtio_crypto_rsa_sign(struct akcipher_request *req)
348 {
349 return virtio_crypto_rsa_req(req, VIRTIO_CRYPTO_AKCIPHER_SIGN);
350 }
351
virtio_crypto_rsa_verify(struct akcipher_request * req)352 static int virtio_crypto_rsa_verify(struct akcipher_request *req)
353 {
354 return virtio_crypto_rsa_req(req, VIRTIO_CRYPTO_AKCIPHER_VERIFY);
355 }
356
virtio_crypto_rsa_set_key(struct crypto_akcipher * tfm,const void * key,unsigned int keylen,bool private,int padding_algo,int hash_algo)357 static int virtio_crypto_rsa_set_key(struct crypto_akcipher *tfm,
358 const void *key,
359 unsigned int keylen,
360 bool private,
361 int padding_algo,
362 int hash_algo)
363 {
364 struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(tfm);
365 struct virtio_crypto_rsa_ctx *rsa_ctx = &ctx->rsa_ctx;
366 struct virtio_crypto *vcrypto;
367 struct virtio_crypto_ctrl_header header;
368 struct virtio_crypto_akcipher_session_para para;
369 struct rsa_key rsa_key = {0};
370 int node = virtio_crypto_get_current_node();
371 uint32_t keytype;
372 int ret;
373
374 /* mpi_free will test n, just free it. */
375 mpi_free(rsa_ctx->n);
376 rsa_ctx->n = NULL;
377
378 if (private) {
379 keytype = VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PRIVATE;
380 ret = rsa_parse_priv_key(&rsa_key, key, keylen);
381 } else {
382 keytype = VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PUBLIC;
383 ret = rsa_parse_pub_key(&rsa_key, key, keylen);
384 }
385
386 if (ret)
387 return ret;
388
389 rsa_ctx->n = mpi_read_raw_data(rsa_key.n, rsa_key.n_sz);
390 if (!rsa_ctx->n)
391 return -ENOMEM;
392
393 if (!ctx->vcrypto) {
394 vcrypto = virtcrypto_get_dev_node(node, VIRTIO_CRYPTO_SERVICE_AKCIPHER,
395 VIRTIO_CRYPTO_AKCIPHER_RSA);
396 if (!vcrypto) {
397 pr_err("virtio_crypto: Could not find a virtio device in the system or unsupported algo\n");
398 return -ENODEV;
399 }
400
401 ctx->vcrypto = vcrypto;
402 } else {
403 virtio_crypto_alg_akcipher_close_session(ctx);
404 }
405
406 /* set ctrl header */
407 header.opcode = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION);
408 header.algo = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_RSA);
409 header.queue_id = 0;
410
411 /* set RSA para */
412 para.algo = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_RSA);
413 para.keytype = cpu_to_le32(keytype);
414 para.keylen = cpu_to_le32(keylen);
415 para.u.rsa.padding_algo = cpu_to_le32(padding_algo);
416 para.u.rsa.hash_algo = cpu_to_le32(hash_algo);
417
418 return virtio_crypto_alg_akcipher_init_session(ctx, &header, ¶, key, keylen);
419 }
420
virtio_crypto_rsa_raw_set_priv_key(struct crypto_akcipher * tfm,const void * key,unsigned int keylen)421 static int virtio_crypto_rsa_raw_set_priv_key(struct crypto_akcipher *tfm,
422 const void *key,
423 unsigned int keylen)
424 {
425 return virtio_crypto_rsa_set_key(tfm, key, keylen, 1,
426 VIRTIO_CRYPTO_RSA_RAW_PADDING,
427 VIRTIO_CRYPTO_RSA_NO_HASH);
428 }
429
430
virtio_crypto_p1pad_rsa_sha1_set_priv_key(struct crypto_akcipher * tfm,const void * key,unsigned int keylen)431 static int virtio_crypto_p1pad_rsa_sha1_set_priv_key(struct crypto_akcipher *tfm,
432 const void *key,
433 unsigned int keylen)
434 {
435 return virtio_crypto_rsa_set_key(tfm, key, keylen, 1,
436 VIRTIO_CRYPTO_RSA_PKCS1_PADDING,
437 VIRTIO_CRYPTO_RSA_SHA1);
438 }
439
virtio_crypto_rsa_raw_set_pub_key(struct crypto_akcipher * tfm,const void * key,unsigned int keylen)440 static int virtio_crypto_rsa_raw_set_pub_key(struct crypto_akcipher *tfm,
441 const void *key,
442 unsigned int keylen)
443 {
444 return virtio_crypto_rsa_set_key(tfm, key, keylen, 0,
445 VIRTIO_CRYPTO_RSA_RAW_PADDING,
446 VIRTIO_CRYPTO_RSA_NO_HASH);
447 }
448
virtio_crypto_p1pad_rsa_sha1_set_pub_key(struct crypto_akcipher * tfm,const void * key,unsigned int keylen)449 static int virtio_crypto_p1pad_rsa_sha1_set_pub_key(struct crypto_akcipher *tfm,
450 const void *key,
451 unsigned int keylen)
452 {
453 return virtio_crypto_rsa_set_key(tfm, key, keylen, 0,
454 VIRTIO_CRYPTO_RSA_PKCS1_PADDING,
455 VIRTIO_CRYPTO_RSA_SHA1);
456 }
457
virtio_crypto_rsa_max_size(struct crypto_akcipher * tfm)458 static unsigned int virtio_crypto_rsa_max_size(struct crypto_akcipher *tfm)
459 {
460 struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(tfm);
461 struct virtio_crypto_rsa_ctx *rsa_ctx = &ctx->rsa_ctx;
462
463 return mpi_get_size(rsa_ctx->n);
464 }
465
virtio_crypto_rsa_init_tfm(struct crypto_akcipher * tfm)466 static int virtio_crypto_rsa_init_tfm(struct crypto_akcipher *tfm)
467 {
468 struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(tfm);
469
470 ctx->tfm = tfm;
471 ctx->enginectx.op.do_one_request = virtio_crypto_rsa_do_req;
472 ctx->enginectx.op.prepare_request = NULL;
473 ctx->enginectx.op.unprepare_request = NULL;
474
475 return 0;
476 }
477
virtio_crypto_rsa_exit_tfm(struct crypto_akcipher * tfm)478 static void virtio_crypto_rsa_exit_tfm(struct crypto_akcipher *tfm)
479 {
480 struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(tfm);
481 struct virtio_crypto_rsa_ctx *rsa_ctx = &ctx->rsa_ctx;
482
483 virtio_crypto_alg_akcipher_close_session(ctx);
484 virtcrypto_dev_put(ctx->vcrypto);
485 mpi_free(rsa_ctx->n);
486 rsa_ctx->n = NULL;
487 }
488
489 static struct virtio_crypto_akcipher_algo virtio_crypto_akcipher_algs[] = {
490 {
491 .algonum = VIRTIO_CRYPTO_AKCIPHER_RSA,
492 .service = VIRTIO_CRYPTO_SERVICE_AKCIPHER,
493 .algo = {
494 .encrypt = virtio_crypto_rsa_encrypt,
495 .decrypt = virtio_crypto_rsa_decrypt,
496 .set_pub_key = virtio_crypto_rsa_raw_set_pub_key,
497 .set_priv_key = virtio_crypto_rsa_raw_set_priv_key,
498 .max_size = virtio_crypto_rsa_max_size,
499 .init = virtio_crypto_rsa_init_tfm,
500 .exit = virtio_crypto_rsa_exit_tfm,
501 .reqsize = sizeof(struct virtio_crypto_akcipher_request),
502 .base = {
503 .cra_name = "rsa",
504 .cra_driver_name = "virtio-crypto-rsa",
505 .cra_priority = 150,
506 .cra_module = THIS_MODULE,
507 .cra_ctxsize = sizeof(struct virtio_crypto_akcipher_ctx),
508 },
509 },
510 },
511 {
512 .algonum = VIRTIO_CRYPTO_AKCIPHER_RSA,
513 .service = VIRTIO_CRYPTO_SERVICE_AKCIPHER,
514 .algo = {
515 .encrypt = virtio_crypto_rsa_encrypt,
516 .decrypt = virtio_crypto_rsa_decrypt,
517 .sign = virtio_crypto_rsa_sign,
518 .verify = virtio_crypto_rsa_verify,
519 .set_pub_key = virtio_crypto_p1pad_rsa_sha1_set_pub_key,
520 .set_priv_key = virtio_crypto_p1pad_rsa_sha1_set_priv_key,
521 .max_size = virtio_crypto_rsa_max_size,
522 .init = virtio_crypto_rsa_init_tfm,
523 .exit = virtio_crypto_rsa_exit_tfm,
524 .reqsize = sizeof(struct virtio_crypto_akcipher_request),
525 .base = {
526 .cra_name = "pkcs1pad(rsa,sha1)",
527 .cra_driver_name = "virtio-pkcs1-rsa-with-sha1",
528 .cra_priority = 150,
529 .cra_module = THIS_MODULE,
530 .cra_ctxsize = sizeof(struct virtio_crypto_akcipher_ctx),
531 },
532 },
533 },
534 };
535
virtio_crypto_akcipher_algs_register(struct virtio_crypto * vcrypto)536 int virtio_crypto_akcipher_algs_register(struct virtio_crypto *vcrypto)
537 {
538 int ret = 0;
539 int i = 0;
540
541 mutex_lock(&algs_lock);
542
543 for (i = 0; i < ARRAY_SIZE(virtio_crypto_akcipher_algs); i++) {
544 uint32_t service = virtio_crypto_akcipher_algs[i].service;
545 uint32_t algonum = virtio_crypto_akcipher_algs[i].algonum;
546
547 if (!virtcrypto_algo_is_supported(vcrypto, service, algonum))
548 continue;
549
550 if (virtio_crypto_akcipher_algs[i].active_devs == 0) {
551 ret = crypto_register_akcipher(&virtio_crypto_akcipher_algs[i].algo);
552 if (ret)
553 goto unlock;
554 }
555
556 virtio_crypto_akcipher_algs[i].active_devs++;
557 dev_info(&vcrypto->vdev->dev, "Registered akcipher algo %s\n",
558 virtio_crypto_akcipher_algs[i].algo.base.cra_name);
559 }
560
561 unlock:
562 mutex_unlock(&algs_lock);
563 return ret;
564 }
565
virtio_crypto_akcipher_algs_unregister(struct virtio_crypto * vcrypto)566 void virtio_crypto_akcipher_algs_unregister(struct virtio_crypto *vcrypto)
567 {
568 int i = 0;
569
570 mutex_lock(&algs_lock);
571
572 for (i = 0; i < ARRAY_SIZE(virtio_crypto_akcipher_algs); i++) {
573 uint32_t service = virtio_crypto_akcipher_algs[i].service;
574 uint32_t algonum = virtio_crypto_akcipher_algs[i].algonum;
575
576 if (virtio_crypto_akcipher_algs[i].active_devs == 0 ||
577 !virtcrypto_algo_is_supported(vcrypto, service, algonum))
578 continue;
579
580 if (virtio_crypto_akcipher_algs[i].active_devs == 1)
581 crypto_unregister_akcipher(&virtio_crypto_akcipher_algs[i].algo);
582
583 virtio_crypto_akcipher_algs[i].active_devs--;
584 }
585
586 mutex_unlock(&algs_lock);
587 }
588