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