1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
3 * caam - Freescale FSL CAAM support for Public Key Cryptography
5 * Copyright 2016 Freescale Semiconductor, Inc.
6 * Copyright 2018-2019 NXP
8 * There is no Shared Descriptor for PKC so that the Job Descriptor must carry
9 * all the desired key parameters, input and output pointers.
16 #include "desc_constr.h"
17 #include "sg_sw_sec4.h"
20 #define DESC_RSA_PUB_LEN (2 * CAAM_CMD_SZ + SIZEOF_RSA_PUB_PDB)
21 #define DESC_RSA_PRIV_F1_LEN (2 * CAAM_CMD_SZ + \
22 SIZEOF_RSA_PRIV_F1_PDB)
23 #define DESC_RSA_PRIV_F2_LEN (2 * CAAM_CMD_SZ + \
24 SIZEOF_RSA_PRIV_F2_PDB)
25 #define DESC_RSA_PRIV_F3_LEN (2 * CAAM_CMD_SZ + \
26 SIZEOF_RSA_PRIV_F3_PDB)
27 #define CAAM_RSA_MAX_INPUT_SIZE 512 /* for a 4096-bit modulus */
29 /* buffer filled with zeros, used for padding */
30 static u8 *zero_buffer;
33 * variable used to avoid double free of resources in case
34 * algorithm registration was unsuccessful
36 static bool init_done;
38 struct caam_akcipher_alg {
39 struct akcipher_alg akcipher;
43 static void rsa_io_unmap(struct device *dev, struct rsa_edesc *edesc,
44 struct akcipher_request *req)
46 struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
48 dma_unmap_sg(dev, req->dst, edesc->dst_nents, DMA_FROM_DEVICE);
49 dma_unmap_sg(dev, req_ctx->fixup_src, edesc->src_nents, DMA_TO_DEVICE);
51 if (edesc->sec4_sg_bytes)
52 dma_unmap_single(dev, edesc->sec4_sg_dma, edesc->sec4_sg_bytes,
56 static void rsa_pub_unmap(struct device *dev, struct rsa_edesc *edesc,
57 struct akcipher_request *req)
59 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
60 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
61 struct caam_rsa_key *key = &ctx->key;
62 struct rsa_pub_pdb *pdb = &edesc->pdb.pub;
64 dma_unmap_single(dev, pdb->n_dma, key->n_sz, DMA_TO_DEVICE);
65 dma_unmap_single(dev, pdb->e_dma, key->e_sz, DMA_TO_DEVICE);
68 static void rsa_priv_f1_unmap(struct device *dev, struct rsa_edesc *edesc,
69 struct akcipher_request *req)
71 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
72 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
73 struct caam_rsa_key *key = &ctx->key;
74 struct rsa_priv_f1_pdb *pdb = &edesc->pdb.priv_f1;
76 dma_unmap_single(dev, pdb->n_dma, key->n_sz, DMA_TO_DEVICE);
77 dma_unmap_single(dev, pdb->d_dma, key->d_sz, DMA_TO_DEVICE);
80 static void rsa_priv_f2_unmap(struct device *dev, struct rsa_edesc *edesc,
81 struct akcipher_request *req)
83 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
84 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
85 struct caam_rsa_key *key = &ctx->key;
86 struct rsa_priv_f2_pdb *pdb = &edesc->pdb.priv_f2;
87 size_t p_sz = key->p_sz;
88 size_t q_sz = key->q_sz;
90 dma_unmap_single(dev, pdb->d_dma, key->d_sz, DMA_TO_DEVICE);
91 dma_unmap_single(dev, pdb->p_dma, p_sz, DMA_TO_DEVICE);
92 dma_unmap_single(dev, pdb->q_dma, q_sz, DMA_TO_DEVICE);
93 dma_unmap_single(dev, pdb->tmp1_dma, p_sz, DMA_BIDIRECTIONAL);
94 dma_unmap_single(dev, pdb->tmp2_dma, q_sz, DMA_BIDIRECTIONAL);
97 static void rsa_priv_f3_unmap(struct device *dev, struct rsa_edesc *edesc,
98 struct akcipher_request *req)
100 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
101 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
102 struct caam_rsa_key *key = &ctx->key;
103 struct rsa_priv_f3_pdb *pdb = &edesc->pdb.priv_f3;
104 size_t p_sz = key->p_sz;
105 size_t q_sz = key->q_sz;
107 dma_unmap_single(dev, pdb->p_dma, p_sz, DMA_TO_DEVICE);
108 dma_unmap_single(dev, pdb->q_dma, q_sz, DMA_TO_DEVICE);
109 dma_unmap_single(dev, pdb->dp_dma, p_sz, DMA_TO_DEVICE);
110 dma_unmap_single(dev, pdb->dq_dma, q_sz, DMA_TO_DEVICE);
111 dma_unmap_single(dev, pdb->c_dma, p_sz, DMA_TO_DEVICE);
112 dma_unmap_single(dev, pdb->tmp1_dma, p_sz, DMA_BIDIRECTIONAL);
113 dma_unmap_single(dev, pdb->tmp2_dma, q_sz, DMA_BIDIRECTIONAL);
116 /* RSA Job Completion handler */
117 static void rsa_pub_done(struct device *dev, u32 *desc, u32 err, void *context)
119 struct akcipher_request *req = context;
120 struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
121 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
122 struct rsa_edesc *edesc;
127 ecode = caam_jr_strstatus(dev, err);
129 edesc = req_ctx->edesc;
130 has_bklog = edesc->bklog;
132 rsa_pub_unmap(dev, edesc, req);
133 rsa_io_unmap(dev, edesc, req);
137 * If no backlog flag, the completion of the request is done
138 * by CAAM, not crypto engine.
141 akcipher_request_complete(req, ecode);
143 crypto_finalize_akcipher_request(jrp->engine, req, ecode);
146 static void rsa_priv_f_done(struct device *dev, u32 *desc, u32 err,
149 struct akcipher_request *req = context;
150 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
151 struct caam_drv_private_jr *jrp = dev_get_drvdata(dev);
152 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
153 struct caam_rsa_key *key = &ctx->key;
154 struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
155 struct rsa_edesc *edesc;
160 ecode = caam_jr_strstatus(dev, err);
162 edesc = req_ctx->edesc;
163 has_bklog = edesc->bklog;
165 switch (key->priv_form) {
167 rsa_priv_f1_unmap(dev, edesc, req);
170 rsa_priv_f2_unmap(dev, edesc, req);
173 rsa_priv_f3_unmap(dev, edesc, req);
176 rsa_io_unmap(dev, edesc, req);
180 * If no backlog flag, the completion of the request is done
181 * by CAAM, not crypto engine.
184 akcipher_request_complete(req, ecode);
186 crypto_finalize_akcipher_request(jrp->engine, req, ecode);
190 * Count leading zeros, need it to strip, from a given scatterlist
192 * @sgl : scatterlist to count zeros from
193 * @nbytes: number of zeros, in bytes, to strip
194 * @flags : operation flags
196 static int caam_rsa_count_leading_zeros(struct scatterlist *sgl,
200 struct sg_mapping_iter miter;
203 unsigned int tbytes = nbytes;
206 ents = sg_nents_for_len(sgl, nbytes);
210 sg_miter_start(&miter, sgl, ents, SG_MITER_FROM_SG | flags);
215 /* do not strip more than given bytes */
216 while (len && !*buff && lzeros < nbytes) {
225 if (!sg_miter_next(&miter))
235 miter.consumed = lzeros;
236 sg_miter_stop(&miter);
239 return tbytes - nbytes;
242 static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req,
245 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
246 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
247 struct device *dev = ctx->dev;
248 struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
249 struct caam_rsa_key *key = &ctx->key;
250 struct rsa_edesc *edesc;
251 gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
252 GFP_KERNEL : GFP_ATOMIC;
253 int sg_flags = (flags == GFP_ATOMIC) ? SG_MITER_ATOMIC : 0;
254 int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes;
255 int src_nents, dst_nents;
256 int mapped_src_nents, mapped_dst_nents;
257 unsigned int diff_size = 0;
260 if (req->src_len > key->n_sz) {
262 * strip leading zeros and
263 * return the number of zeros to skip
265 lzeros = caam_rsa_count_leading_zeros(req->src, req->src_len -
266 key->n_sz, sg_flags);
268 return ERR_PTR(lzeros);
270 req_ctx->fixup_src = scatterwalk_ffwd(req_ctx->src, req->src,
272 req_ctx->fixup_src_len = req->src_len - lzeros;
275 * input src is less then n key modulus,
276 * so there will be zero padding
278 diff_size = key->n_sz - req->src_len;
279 req_ctx->fixup_src = req->src;
280 req_ctx->fixup_src_len = req->src_len;
283 src_nents = sg_nents_for_len(req_ctx->fixup_src,
284 req_ctx->fixup_src_len);
285 dst_nents = sg_nents_for_len(req->dst, req->dst_len);
287 mapped_src_nents = dma_map_sg(dev, req_ctx->fixup_src, src_nents,
289 if (unlikely(!mapped_src_nents)) {
290 dev_err(dev, "unable to map source\n");
291 return ERR_PTR(-ENOMEM);
293 mapped_dst_nents = dma_map_sg(dev, req->dst, dst_nents,
295 if (unlikely(!mapped_dst_nents)) {
296 dev_err(dev, "unable to map destination\n");
300 if (!diff_size && mapped_src_nents == 1)
301 sec4_sg_len = 0; /* no need for an input hw s/g table */
303 sec4_sg_len = mapped_src_nents + !!diff_size;
304 sec4_sg_index = sec4_sg_len;
306 if (mapped_dst_nents > 1)
307 sec4_sg_len += pad_sg_nents(mapped_dst_nents);
309 sec4_sg_len = pad_sg_nents(sec4_sg_len);
311 sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry);
313 /* allocate space for base edesc, hw desc commands and link tables */
314 edesc = kzalloc(sizeof(*edesc) + desclen + sec4_sg_bytes,
319 edesc->sec4_sg = (void *)edesc + sizeof(*edesc) + desclen;
321 dma_to_sec4_sg_one(edesc->sec4_sg, ctx->padding_dma, diff_size,
325 sg_to_sec4_sg_last(req_ctx->fixup_src, req_ctx->fixup_src_len,
326 edesc->sec4_sg + !!diff_size, 0);
328 if (mapped_dst_nents > 1)
329 sg_to_sec4_sg_last(req->dst, req->dst_len,
330 edesc->sec4_sg + sec4_sg_index, 0);
332 /* Save nents for later use in Job Descriptor */
333 edesc->src_nents = src_nents;
334 edesc->dst_nents = dst_nents;
336 req_ctx->edesc = edesc;
341 edesc->mapped_src_nents = mapped_src_nents;
342 edesc->mapped_dst_nents = mapped_dst_nents;
344 edesc->sec4_sg_dma = dma_map_single(dev, edesc->sec4_sg,
345 sec4_sg_bytes, DMA_TO_DEVICE);
346 if (dma_mapping_error(dev, edesc->sec4_sg_dma)) {
347 dev_err(dev, "unable to map S/G table\n");
351 edesc->sec4_sg_bytes = sec4_sg_bytes;
353 print_hex_dump_debug("caampkc sec4_sg@" __stringify(__LINE__) ": ",
354 DUMP_PREFIX_ADDRESS, 16, 4, edesc->sec4_sg,
355 edesc->sec4_sg_bytes, 1);
362 dma_unmap_sg(dev, req->dst, dst_nents, DMA_FROM_DEVICE);
364 dma_unmap_sg(dev, req_ctx->fixup_src, src_nents, DMA_TO_DEVICE);
365 return ERR_PTR(-ENOMEM);
368 static int akcipher_do_one_req(struct crypto_engine *engine, void *areq)
370 struct akcipher_request *req = container_of(areq,
371 struct akcipher_request,
373 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
374 struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
375 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
376 struct device *jrdev = ctx->dev;
377 u32 *desc = req_ctx->edesc->hw_desc;
380 req_ctx->edesc->bklog = true;
382 ret = caam_jr_enqueue(jrdev, desc, req_ctx->akcipher_op_done, req);
384 if (ret != -EINPROGRESS) {
385 rsa_pub_unmap(jrdev, req_ctx->edesc, req);
386 rsa_io_unmap(jrdev, req_ctx->edesc, req);
387 kfree(req_ctx->edesc);
395 static int set_rsa_pub_pdb(struct akcipher_request *req,
396 struct rsa_edesc *edesc)
398 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
399 struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
400 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
401 struct caam_rsa_key *key = &ctx->key;
402 struct device *dev = ctx->dev;
403 struct rsa_pub_pdb *pdb = &edesc->pdb.pub;
404 int sec4_sg_index = 0;
406 pdb->n_dma = dma_map_single(dev, key->n, key->n_sz, DMA_TO_DEVICE);
407 if (dma_mapping_error(dev, pdb->n_dma)) {
408 dev_err(dev, "Unable to map RSA modulus memory\n");
412 pdb->e_dma = dma_map_single(dev, key->e, key->e_sz, DMA_TO_DEVICE);
413 if (dma_mapping_error(dev, pdb->e_dma)) {
414 dev_err(dev, "Unable to map RSA public exponent memory\n");
415 dma_unmap_single(dev, pdb->n_dma, key->n_sz, DMA_TO_DEVICE);
419 if (edesc->mapped_src_nents > 1) {
420 pdb->sgf |= RSA_PDB_SGF_F;
421 pdb->f_dma = edesc->sec4_sg_dma;
422 sec4_sg_index += edesc->mapped_src_nents;
424 pdb->f_dma = sg_dma_address(req_ctx->fixup_src);
427 if (edesc->mapped_dst_nents > 1) {
428 pdb->sgf |= RSA_PDB_SGF_G;
429 pdb->g_dma = edesc->sec4_sg_dma +
430 sec4_sg_index * sizeof(struct sec4_sg_entry);
432 pdb->g_dma = sg_dma_address(req->dst);
435 pdb->sgf |= (key->e_sz << RSA_PDB_E_SHIFT) | key->n_sz;
436 pdb->f_len = req_ctx->fixup_src_len;
441 static int set_rsa_priv_f1_pdb(struct akcipher_request *req,
442 struct rsa_edesc *edesc)
444 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
445 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
446 struct caam_rsa_key *key = &ctx->key;
447 struct device *dev = ctx->dev;
448 struct rsa_priv_f1_pdb *pdb = &edesc->pdb.priv_f1;
449 int sec4_sg_index = 0;
451 pdb->n_dma = dma_map_single(dev, key->n, key->n_sz, DMA_TO_DEVICE);
452 if (dma_mapping_error(dev, pdb->n_dma)) {
453 dev_err(dev, "Unable to map modulus memory\n");
457 pdb->d_dma = dma_map_single(dev, key->d, key->d_sz, DMA_TO_DEVICE);
458 if (dma_mapping_error(dev, pdb->d_dma)) {
459 dev_err(dev, "Unable to map RSA private exponent memory\n");
460 dma_unmap_single(dev, pdb->n_dma, key->n_sz, DMA_TO_DEVICE);
464 if (edesc->mapped_src_nents > 1) {
465 pdb->sgf |= RSA_PRIV_PDB_SGF_G;
466 pdb->g_dma = edesc->sec4_sg_dma;
467 sec4_sg_index += edesc->mapped_src_nents;
470 struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
472 pdb->g_dma = sg_dma_address(req_ctx->fixup_src);
475 if (edesc->mapped_dst_nents > 1) {
476 pdb->sgf |= RSA_PRIV_PDB_SGF_F;
477 pdb->f_dma = edesc->sec4_sg_dma +
478 sec4_sg_index * sizeof(struct sec4_sg_entry);
480 pdb->f_dma = sg_dma_address(req->dst);
483 pdb->sgf |= (key->d_sz << RSA_PDB_D_SHIFT) | key->n_sz;
488 static int set_rsa_priv_f2_pdb(struct akcipher_request *req,
489 struct rsa_edesc *edesc)
491 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
492 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
493 struct caam_rsa_key *key = &ctx->key;
494 struct device *dev = ctx->dev;
495 struct rsa_priv_f2_pdb *pdb = &edesc->pdb.priv_f2;
496 int sec4_sg_index = 0;
497 size_t p_sz = key->p_sz;
498 size_t q_sz = key->q_sz;
500 pdb->d_dma = dma_map_single(dev, key->d, key->d_sz, DMA_TO_DEVICE);
501 if (dma_mapping_error(dev, pdb->d_dma)) {
502 dev_err(dev, "Unable to map RSA private exponent memory\n");
506 pdb->p_dma = dma_map_single(dev, key->p, p_sz, DMA_TO_DEVICE);
507 if (dma_mapping_error(dev, pdb->p_dma)) {
508 dev_err(dev, "Unable to map RSA prime factor p memory\n");
512 pdb->q_dma = dma_map_single(dev, key->q, q_sz, DMA_TO_DEVICE);
513 if (dma_mapping_error(dev, pdb->q_dma)) {
514 dev_err(dev, "Unable to map RSA prime factor q memory\n");
518 pdb->tmp1_dma = dma_map_single(dev, key->tmp1, p_sz, DMA_BIDIRECTIONAL);
519 if (dma_mapping_error(dev, pdb->tmp1_dma)) {
520 dev_err(dev, "Unable to map RSA tmp1 memory\n");
524 pdb->tmp2_dma = dma_map_single(dev, key->tmp2, q_sz, DMA_BIDIRECTIONAL);
525 if (dma_mapping_error(dev, pdb->tmp2_dma)) {
526 dev_err(dev, "Unable to map RSA tmp2 memory\n");
530 if (edesc->mapped_src_nents > 1) {
531 pdb->sgf |= RSA_PRIV_PDB_SGF_G;
532 pdb->g_dma = edesc->sec4_sg_dma;
533 sec4_sg_index += edesc->mapped_src_nents;
535 struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
537 pdb->g_dma = sg_dma_address(req_ctx->fixup_src);
540 if (edesc->mapped_dst_nents > 1) {
541 pdb->sgf |= RSA_PRIV_PDB_SGF_F;
542 pdb->f_dma = edesc->sec4_sg_dma +
543 sec4_sg_index * sizeof(struct sec4_sg_entry);
545 pdb->f_dma = sg_dma_address(req->dst);
548 pdb->sgf |= (key->d_sz << RSA_PDB_D_SHIFT) | key->n_sz;
549 pdb->p_q_len = (q_sz << RSA_PDB_Q_SHIFT) | p_sz;
554 dma_unmap_single(dev, pdb->tmp1_dma, p_sz, DMA_BIDIRECTIONAL);
556 dma_unmap_single(dev, pdb->q_dma, q_sz, DMA_TO_DEVICE);
558 dma_unmap_single(dev, pdb->p_dma, p_sz, DMA_TO_DEVICE);
560 dma_unmap_single(dev, pdb->d_dma, key->d_sz, DMA_TO_DEVICE);
565 static int set_rsa_priv_f3_pdb(struct akcipher_request *req,
566 struct rsa_edesc *edesc)
568 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
569 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
570 struct caam_rsa_key *key = &ctx->key;
571 struct device *dev = ctx->dev;
572 struct rsa_priv_f3_pdb *pdb = &edesc->pdb.priv_f3;
573 int sec4_sg_index = 0;
574 size_t p_sz = key->p_sz;
575 size_t q_sz = key->q_sz;
577 pdb->p_dma = dma_map_single(dev, key->p, p_sz, DMA_TO_DEVICE);
578 if (dma_mapping_error(dev, pdb->p_dma)) {
579 dev_err(dev, "Unable to map RSA prime factor p memory\n");
583 pdb->q_dma = dma_map_single(dev, key->q, q_sz, DMA_TO_DEVICE);
584 if (dma_mapping_error(dev, pdb->q_dma)) {
585 dev_err(dev, "Unable to map RSA prime factor q memory\n");
589 pdb->dp_dma = dma_map_single(dev, key->dp, p_sz, DMA_TO_DEVICE);
590 if (dma_mapping_error(dev, pdb->dp_dma)) {
591 dev_err(dev, "Unable to map RSA exponent dp memory\n");
595 pdb->dq_dma = dma_map_single(dev, key->dq, q_sz, DMA_TO_DEVICE);
596 if (dma_mapping_error(dev, pdb->dq_dma)) {
597 dev_err(dev, "Unable to map RSA exponent dq memory\n");
601 pdb->c_dma = dma_map_single(dev, key->qinv, p_sz, DMA_TO_DEVICE);
602 if (dma_mapping_error(dev, pdb->c_dma)) {
603 dev_err(dev, "Unable to map RSA CRT coefficient qinv memory\n");
607 pdb->tmp1_dma = dma_map_single(dev, key->tmp1, p_sz, DMA_BIDIRECTIONAL);
608 if (dma_mapping_error(dev, pdb->tmp1_dma)) {
609 dev_err(dev, "Unable to map RSA tmp1 memory\n");
613 pdb->tmp2_dma = dma_map_single(dev, key->tmp2, q_sz, DMA_BIDIRECTIONAL);
614 if (dma_mapping_error(dev, pdb->tmp2_dma)) {
615 dev_err(dev, "Unable to map RSA tmp2 memory\n");
619 if (edesc->mapped_src_nents > 1) {
620 pdb->sgf |= RSA_PRIV_PDB_SGF_G;
621 pdb->g_dma = edesc->sec4_sg_dma;
622 sec4_sg_index += edesc->mapped_src_nents;
624 struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
626 pdb->g_dma = sg_dma_address(req_ctx->fixup_src);
629 if (edesc->mapped_dst_nents > 1) {
630 pdb->sgf |= RSA_PRIV_PDB_SGF_F;
631 pdb->f_dma = edesc->sec4_sg_dma +
632 sec4_sg_index * sizeof(struct sec4_sg_entry);
634 pdb->f_dma = sg_dma_address(req->dst);
637 pdb->sgf |= key->n_sz;
638 pdb->p_q_len = (q_sz << RSA_PDB_Q_SHIFT) | p_sz;
643 dma_unmap_single(dev, pdb->tmp1_dma, p_sz, DMA_BIDIRECTIONAL);
645 dma_unmap_single(dev, pdb->c_dma, p_sz, DMA_TO_DEVICE);
647 dma_unmap_single(dev, pdb->dq_dma, q_sz, DMA_TO_DEVICE);
649 dma_unmap_single(dev, pdb->dp_dma, p_sz, DMA_TO_DEVICE);
651 dma_unmap_single(dev, pdb->q_dma, q_sz, DMA_TO_DEVICE);
653 dma_unmap_single(dev, pdb->p_dma, p_sz, DMA_TO_DEVICE);
658 static int akcipher_enqueue_req(struct device *jrdev,
659 void (*cbk)(struct device *jrdev, u32 *desc,
660 u32 err, void *context),
661 struct akcipher_request *req)
663 struct caam_drv_private_jr *jrpriv = dev_get_drvdata(jrdev);
664 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
665 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
666 struct caam_rsa_key *key = &ctx->key;
667 struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
668 struct rsa_edesc *edesc = req_ctx->edesc;
669 u32 *desc = edesc->hw_desc;
672 req_ctx->akcipher_op_done = cbk;
674 * Only the backlog request are sent to crypto-engine since the others
675 * can be handled by CAAM, if free, especially since JR has up to 1024
676 * entries (more than the 10 entries from crypto-engine).
678 if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)
679 ret = crypto_transfer_akcipher_request_to_engine(jrpriv->engine,
682 ret = caam_jr_enqueue(jrdev, desc, cbk, req);
684 if ((ret != -EINPROGRESS) && (ret != -EBUSY)) {
685 switch (key->priv_form) {
687 rsa_priv_f1_unmap(jrdev, edesc, req);
690 rsa_priv_f2_unmap(jrdev, edesc, req);
693 rsa_priv_f3_unmap(jrdev, edesc, req);
696 rsa_pub_unmap(jrdev, edesc, req);
698 rsa_io_unmap(jrdev, edesc, req);
705 static int caam_rsa_enc(struct akcipher_request *req)
707 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
708 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
709 struct caam_rsa_key *key = &ctx->key;
710 struct device *jrdev = ctx->dev;
711 struct rsa_edesc *edesc;
714 if (unlikely(!key->n || !key->e))
717 if (req->dst_len < key->n_sz) {
718 req->dst_len = key->n_sz;
719 dev_err(jrdev, "Output buffer length less than parameter n\n");
723 /* Allocate extended descriptor */
724 edesc = rsa_edesc_alloc(req, DESC_RSA_PUB_LEN);
726 return PTR_ERR(edesc);
728 /* Set RSA Encrypt Protocol Data Block */
729 ret = set_rsa_pub_pdb(req, edesc);
733 /* Initialize Job Descriptor */
734 init_rsa_pub_desc(edesc->hw_desc, &edesc->pdb.pub);
736 return akcipher_enqueue_req(jrdev, rsa_pub_done, req);
739 rsa_io_unmap(jrdev, edesc, req);
744 static int caam_rsa_dec_priv_f1(struct akcipher_request *req)
746 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
747 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
748 struct device *jrdev = ctx->dev;
749 struct rsa_edesc *edesc;
752 /* Allocate extended descriptor */
753 edesc = rsa_edesc_alloc(req, DESC_RSA_PRIV_F1_LEN);
755 return PTR_ERR(edesc);
757 /* Set RSA Decrypt Protocol Data Block - Private Key Form #1 */
758 ret = set_rsa_priv_f1_pdb(req, edesc);
762 /* Initialize Job Descriptor */
763 init_rsa_priv_f1_desc(edesc->hw_desc, &edesc->pdb.priv_f1);
765 return akcipher_enqueue_req(jrdev, rsa_priv_f_done, req);
768 rsa_io_unmap(jrdev, edesc, req);
773 static int caam_rsa_dec_priv_f2(struct akcipher_request *req)
775 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
776 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
777 struct device *jrdev = ctx->dev;
778 struct rsa_edesc *edesc;
781 /* Allocate extended descriptor */
782 edesc = rsa_edesc_alloc(req, DESC_RSA_PRIV_F2_LEN);
784 return PTR_ERR(edesc);
786 /* Set RSA Decrypt Protocol Data Block - Private Key Form #2 */
787 ret = set_rsa_priv_f2_pdb(req, edesc);
791 /* Initialize Job Descriptor */
792 init_rsa_priv_f2_desc(edesc->hw_desc, &edesc->pdb.priv_f2);
794 return akcipher_enqueue_req(jrdev, rsa_priv_f_done, req);
797 rsa_io_unmap(jrdev, edesc, req);
802 static int caam_rsa_dec_priv_f3(struct akcipher_request *req)
804 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
805 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
806 struct device *jrdev = ctx->dev;
807 struct rsa_edesc *edesc;
810 /* Allocate extended descriptor */
811 edesc = rsa_edesc_alloc(req, DESC_RSA_PRIV_F3_LEN);
813 return PTR_ERR(edesc);
815 /* Set RSA Decrypt Protocol Data Block - Private Key Form #3 */
816 ret = set_rsa_priv_f3_pdb(req, edesc);
820 /* Initialize Job Descriptor */
821 init_rsa_priv_f3_desc(edesc->hw_desc, &edesc->pdb.priv_f3);
823 return akcipher_enqueue_req(jrdev, rsa_priv_f_done, req);
826 rsa_io_unmap(jrdev, edesc, req);
831 static int caam_rsa_dec(struct akcipher_request *req)
833 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
834 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
835 struct caam_rsa_key *key = &ctx->key;
838 if (unlikely(!key->n || !key->d))
841 if (req->dst_len < key->n_sz) {
842 req->dst_len = key->n_sz;
843 dev_err(ctx->dev, "Output buffer length less than parameter n\n");
847 if (key->priv_form == FORM3)
848 ret = caam_rsa_dec_priv_f3(req);
849 else if (key->priv_form == FORM2)
850 ret = caam_rsa_dec_priv_f2(req);
852 ret = caam_rsa_dec_priv_f1(req);
857 static void caam_rsa_free_key(struct caam_rsa_key *key)
859 kfree_sensitive(key->d);
860 kfree_sensitive(key->p);
861 kfree_sensitive(key->q);
862 kfree_sensitive(key->dp);
863 kfree_sensitive(key->dq);
864 kfree_sensitive(key->qinv);
865 kfree_sensitive(key->tmp1);
866 kfree_sensitive(key->tmp2);
869 memset(key, 0, sizeof(*key));
872 static void caam_rsa_drop_leading_zeros(const u8 **ptr, size_t *nbytes)
874 while (!**ptr && *nbytes) {
881 * caam_read_rsa_crt - Used for reading dP, dQ, qInv CRT members.
882 * dP, dQ and qInv could decode to less than corresponding p, q length, as the
883 * BER-encoding requires that the minimum number of bytes be used to encode the
884 * integer. dP, dQ, qInv decoded values have to be zero-padded to appropriate
887 * @ptr : pointer to {dP, dQ, qInv} CRT member
888 * @nbytes: length in bytes of {dP, dQ, qInv} CRT member
889 * @dstlen: length in bytes of corresponding p or q prime factor
891 static u8 *caam_read_rsa_crt(const u8 *ptr, size_t nbytes, size_t dstlen)
895 caam_rsa_drop_leading_zeros(&ptr, &nbytes);
899 dst = kzalloc(dstlen, GFP_DMA | GFP_KERNEL);
903 memcpy(dst + (dstlen - nbytes), ptr, nbytes);
909 * caam_read_raw_data - Read a raw byte stream as a positive integer.
910 * The function skips buffer's leading zeros, copies the remained data
911 * to a buffer allocated in the GFP_DMA | GFP_KERNEL zone and returns
912 * the address of the new buffer.
914 * @buf : The data to read
915 * @nbytes: The amount of data to read
917 static inline u8 *caam_read_raw_data(const u8 *buf, size_t *nbytes)
920 caam_rsa_drop_leading_zeros(&buf, nbytes);
924 return kmemdup(buf, *nbytes, GFP_DMA | GFP_KERNEL);
927 static int caam_rsa_check_key_length(unsigned int len)
934 static int caam_rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
937 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
938 struct rsa_key raw_key = {NULL};
939 struct caam_rsa_key *rsa_key = &ctx->key;
942 /* Free the old RSA key if any */
943 caam_rsa_free_key(rsa_key);
945 ret = rsa_parse_pub_key(&raw_key, key, keylen);
949 /* Copy key in DMA zone */
950 rsa_key->e = kmemdup(raw_key.e, raw_key.e_sz, GFP_DMA | GFP_KERNEL);
955 * Skip leading zeros and copy the positive integer to a buffer
956 * allocated in the GFP_DMA | GFP_KERNEL zone. The decryption descriptor
957 * expects a positive integer for the RSA modulus and uses its length as
958 * decryption output length.
960 rsa_key->n = caam_read_raw_data(raw_key.n, &raw_key.n_sz);
964 if (caam_rsa_check_key_length(raw_key.n_sz << 3)) {
965 caam_rsa_free_key(rsa_key);
969 rsa_key->e_sz = raw_key.e_sz;
970 rsa_key->n_sz = raw_key.n_sz;
974 caam_rsa_free_key(rsa_key);
978 static void caam_rsa_set_priv_key_form(struct caam_rsa_ctx *ctx,
979 struct rsa_key *raw_key)
981 struct caam_rsa_key *rsa_key = &ctx->key;
982 size_t p_sz = raw_key->p_sz;
983 size_t q_sz = raw_key->q_sz;
985 rsa_key->p = caam_read_raw_data(raw_key->p, &p_sz);
988 rsa_key->p_sz = p_sz;
990 rsa_key->q = caam_read_raw_data(raw_key->q, &q_sz);
993 rsa_key->q_sz = q_sz;
995 rsa_key->tmp1 = kzalloc(raw_key->p_sz, GFP_DMA | GFP_KERNEL);
999 rsa_key->tmp2 = kzalloc(raw_key->q_sz, GFP_DMA | GFP_KERNEL);
1003 rsa_key->priv_form = FORM2;
1005 rsa_key->dp = caam_read_rsa_crt(raw_key->dp, raw_key->dp_sz, p_sz);
1009 rsa_key->dq = caam_read_rsa_crt(raw_key->dq, raw_key->dq_sz, q_sz);
1013 rsa_key->qinv = caam_read_rsa_crt(raw_key->qinv, raw_key->qinv_sz,
1018 rsa_key->priv_form = FORM3;
1023 kfree_sensitive(rsa_key->dq);
1025 kfree_sensitive(rsa_key->dp);
1027 kfree_sensitive(rsa_key->tmp2);
1029 kfree_sensitive(rsa_key->tmp1);
1031 kfree_sensitive(rsa_key->q);
1033 kfree_sensitive(rsa_key->p);
1036 static int caam_rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
1037 unsigned int keylen)
1039 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
1040 struct rsa_key raw_key = {NULL};
1041 struct caam_rsa_key *rsa_key = &ctx->key;
1044 /* Free the old RSA key if any */
1045 caam_rsa_free_key(rsa_key);
1047 ret = rsa_parse_priv_key(&raw_key, key, keylen);
1051 /* Copy key in DMA zone */
1052 rsa_key->d = kmemdup(raw_key.d, raw_key.d_sz, GFP_DMA | GFP_KERNEL);
1056 rsa_key->e = kmemdup(raw_key.e, raw_key.e_sz, GFP_DMA | GFP_KERNEL);
1061 * Skip leading zeros and copy the positive integer to a buffer
1062 * allocated in the GFP_DMA | GFP_KERNEL zone. The decryption descriptor
1063 * expects a positive integer for the RSA modulus and uses its length as
1064 * decryption output length.
1066 rsa_key->n = caam_read_raw_data(raw_key.n, &raw_key.n_sz);
1070 if (caam_rsa_check_key_length(raw_key.n_sz << 3)) {
1071 caam_rsa_free_key(rsa_key);
1075 rsa_key->d_sz = raw_key.d_sz;
1076 rsa_key->e_sz = raw_key.e_sz;
1077 rsa_key->n_sz = raw_key.n_sz;
1079 caam_rsa_set_priv_key_form(ctx, &raw_key);
1084 caam_rsa_free_key(rsa_key);
1088 static unsigned int caam_rsa_max_size(struct crypto_akcipher *tfm)
1090 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
1092 return ctx->key.n_sz;
1095 /* Per session pkc's driver context creation function */
1096 static int caam_rsa_init_tfm(struct crypto_akcipher *tfm)
1098 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
1100 ctx->dev = caam_jr_alloc();
1102 if (IS_ERR(ctx->dev)) {
1103 pr_err("Job Ring Device allocation for transform failed\n");
1104 return PTR_ERR(ctx->dev);
1107 ctx->padding_dma = dma_map_single(ctx->dev, zero_buffer,
1108 CAAM_RSA_MAX_INPUT_SIZE - 1,
1110 if (dma_mapping_error(ctx->dev, ctx->padding_dma)) {
1111 dev_err(ctx->dev, "unable to map padding\n");
1112 caam_jr_free(ctx->dev);
1116 ctx->enginectx.op.do_one_request = akcipher_do_one_req;
1121 /* Per session pkc's driver context cleanup function */
1122 static void caam_rsa_exit_tfm(struct crypto_akcipher *tfm)
1124 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
1125 struct caam_rsa_key *key = &ctx->key;
1127 dma_unmap_single(ctx->dev, ctx->padding_dma, CAAM_RSA_MAX_INPUT_SIZE -
1129 caam_rsa_free_key(key);
1130 caam_jr_free(ctx->dev);
1133 static struct caam_akcipher_alg caam_rsa = {
1135 .encrypt = caam_rsa_enc,
1136 .decrypt = caam_rsa_dec,
1137 .set_pub_key = caam_rsa_set_pub_key,
1138 .set_priv_key = caam_rsa_set_priv_key,
1139 .max_size = caam_rsa_max_size,
1140 .init = caam_rsa_init_tfm,
1141 .exit = caam_rsa_exit_tfm,
1142 .reqsize = sizeof(struct caam_rsa_req_ctx),
1145 .cra_driver_name = "rsa-caam",
1146 .cra_priority = 3000,
1147 .cra_module = THIS_MODULE,
1148 .cra_ctxsize = sizeof(struct caam_rsa_ctx),
1153 /* Public Key Cryptography module initialization handler */
1154 int caam_pkc_init(struct device *ctrldev)
1156 struct caam_drv_private *priv = dev_get_drvdata(ctrldev);
1161 /* Determine public key hardware accelerator presence. */
1162 if (priv->era < 10) {
1163 pk_inst = (rd_reg32(&priv->ctrl->perfmon.cha_num_ls) &
1164 CHA_ID_LS_PK_MASK) >> CHA_ID_LS_PK_SHIFT;
1166 pkha = rd_reg32(&priv->ctrl->vreg.pkha);
1167 pk_inst = pkha & CHA_VER_NUM_MASK;
1170 * Newer CAAMs support partially disabled functionality. If this is the
1171 * case, the number is non-zero, but this bit is set to indicate that
1172 * no encryption or decryption is supported. Only signing and verifying
1175 if (pkha & CHA_VER_MISC_PKHA_NO_CRYPT)
1179 /* Do not register algorithms if PKHA is not present. */
1183 /* allocate zero buffer, used for padding input */
1184 zero_buffer = kzalloc(CAAM_RSA_MAX_INPUT_SIZE - 1, GFP_DMA |
1189 err = crypto_register_akcipher(&caam_rsa.akcipher);
1193 dev_warn(ctrldev, "%s alg registration failed\n",
1194 caam_rsa.akcipher.base.cra_driver_name);
1197 caam_rsa.registered = true;
1198 dev_info(ctrldev, "caam pkc algorithms registered in /proc/crypto\n");
1204 void caam_pkc_exit(void)
1209 if (caam_rsa.registered)
1210 crypto_unregister_akcipher(&caam_rsa.akcipher);