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 rsa_edesc *edesc;
124 ecode = caam_jr_strstatus(dev, err);
126 edesc = container_of(desc, struct rsa_edesc, hw_desc[0]);
128 rsa_pub_unmap(dev, edesc, req);
129 rsa_io_unmap(dev, edesc, req);
132 akcipher_request_complete(req, ecode);
135 static void rsa_priv_f1_done(struct device *dev, u32 *desc, u32 err,
138 struct akcipher_request *req = context;
139 struct rsa_edesc *edesc;
143 ecode = caam_jr_strstatus(dev, err);
145 edesc = container_of(desc, struct rsa_edesc, hw_desc[0]);
147 rsa_priv_f1_unmap(dev, edesc, req);
148 rsa_io_unmap(dev, edesc, req);
151 akcipher_request_complete(req, ecode);
154 static void rsa_priv_f2_done(struct device *dev, u32 *desc, u32 err,
157 struct akcipher_request *req = context;
158 struct rsa_edesc *edesc;
162 ecode = caam_jr_strstatus(dev, err);
164 edesc = container_of(desc, struct rsa_edesc, hw_desc[0]);
166 rsa_priv_f2_unmap(dev, edesc, req);
167 rsa_io_unmap(dev, edesc, req);
170 akcipher_request_complete(req, ecode);
173 static void rsa_priv_f3_done(struct device *dev, u32 *desc, u32 err,
176 struct akcipher_request *req = context;
177 struct rsa_edesc *edesc;
181 ecode = caam_jr_strstatus(dev, err);
183 edesc = container_of(desc, struct rsa_edesc, hw_desc[0]);
185 rsa_priv_f3_unmap(dev, edesc, req);
186 rsa_io_unmap(dev, edesc, req);
189 akcipher_request_complete(req, ecode);
193 * Count leading zeros, need it to strip, from a given scatterlist
195 * @sgl : scatterlist to count zeros from
196 * @nbytes: number of zeros, in bytes, to strip
197 * @flags : operation flags
199 static int caam_rsa_count_leading_zeros(struct scatterlist *sgl,
203 struct sg_mapping_iter miter;
206 unsigned int tbytes = nbytes;
209 ents = sg_nents_for_len(sgl, nbytes);
213 sg_miter_start(&miter, sgl, ents, SG_MITER_FROM_SG | flags);
218 /* do not strip more than given bytes */
219 while (len && !*buff && lzeros < nbytes) {
228 sg_miter_next(&miter);
236 miter.consumed = lzeros;
237 sg_miter_stop(&miter);
240 return tbytes - nbytes;
243 static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req,
246 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
247 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
248 struct device *dev = ctx->dev;
249 struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
250 struct caam_rsa_key *key = &ctx->key;
251 struct rsa_edesc *edesc;
252 gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
253 GFP_KERNEL : GFP_ATOMIC;
254 int sg_flags = (flags == GFP_ATOMIC) ? SG_MITER_ATOMIC : 0;
256 int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes;
257 int src_nents, dst_nents;
258 unsigned int diff_size = 0;
261 if (req->src_len > key->n_sz) {
263 * strip leading zeros and
264 * return the number of zeros to skip
266 lzeros = caam_rsa_count_leading_zeros(req->src, req->src_len -
267 key->n_sz, sg_flags);
269 return ERR_PTR(lzeros);
271 req_ctx->fixup_src = scatterwalk_ffwd(req_ctx->src, req->src,
273 req_ctx->fixup_src_len = req->src_len - lzeros;
276 * input src is less then n key modulus,
277 * so there will be zero padding
279 diff_size = key->n_sz - req->src_len;
280 req_ctx->fixup_src = req->src;
281 req_ctx->fixup_src_len = req->src_len;
284 src_nents = sg_nents_for_len(req_ctx->fixup_src,
285 req_ctx->fixup_src_len);
286 dst_nents = sg_nents_for_len(req->dst, req->dst_len);
288 if (!diff_size && src_nents == 1)
289 sec4_sg_len = 0; /* no need for an input hw s/g table */
291 sec4_sg_len = src_nents + !!diff_size;
292 sec4_sg_index = sec4_sg_len;
294 sec4_sg_len += pad_sg_nents(dst_nents);
296 sec4_sg_len = pad_sg_nents(sec4_sg_len);
298 sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry);
300 /* allocate space for base edesc, hw desc commands and link tables */
301 edesc = kzalloc(sizeof(*edesc) + desclen + sec4_sg_bytes,
304 return ERR_PTR(-ENOMEM);
306 sgc = dma_map_sg(dev, req_ctx->fixup_src, src_nents, DMA_TO_DEVICE);
307 if (unlikely(!sgc)) {
308 dev_err(dev, "unable to map source\n");
312 sgc = dma_map_sg(dev, req->dst, dst_nents, DMA_FROM_DEVICE);
313 if (unlikely(!sgc)) {
314 dev_err(dev, "unable to map destination\n");
318 edesc->sec4_sg = (void *)edesc + sizeof(*edesc) + desclen;
320 dma_to_sec4_sg_one(edesc->sec4_sg, ctx->padding_dma, diff_size,
324 sg_to_sec4_sg_last(req_ctx->fixup_src, req_ctx->fixup_src_len,
325 edesc->sec4_sg + !!diff_size, 0);
328 sg_to_sec4_sg_last(req->dst, req->dst_len,
329 edesc->sec4_sg + sec4_sg_index, 0);
331 /* Save nents for later use in Job Descriptor */
332 edesc->src_nents = src_nents;
333 edesc->dst_nents = dst_nents;
338 edesc->sec4_sg_dma = dma_map_single(dev, edesc->sec4_sg,
339 sec4_sg_bytes, DMA_TO_DEVICE);
340 if (dma_mapping_error(dev, edesc->sec4_sg_dma)) {
341 dev_err(dev, "unable to map S/G table\n");
345 edesc->sec4_sg_bytes = sec4_sg_bytes;
347 print_hex_dump_debug("caampkc sec4_sg@" __stringify(__LINE__) ": ",
348 DUMP_PREFIX_ADDRESS, 16, 4, edesc->sec4_sg,
349 edesc->sec4_sg_bytes, 1);
354 dma_unmap_sg(dev, req->dst, dst_nents, DMA_FROM_DEVICE);
356 dma_unmap_sg(dev, req_ctx->fixup_src, src_nents, DMA_TO_DEVICE);
359 return ERR_PTR(-ENOMEM);
362 static int set_rsa_pub_pdb(struct akcipher_request *req,
363 struct rsa_edesc *edesc)
365 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
366 struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
367 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
368 struct caam_rsa_key *key = &ctx->key;
369 struct device *dev = ctx->dev;
370 struct rsa_pub_pdb *pdb = &edesc->pdb.pub;
371 int sec4_sg_index = 0;
373 pdb->n_dma = dma_map_single(dev, key->n, key->n_sz, DMA_TO_DEVICE);
374 if (dma_mapping_error(dev, pdb->n_dma)) {
375 dev_err(dev, "Unable to map RSA modulus memory\n");
379 pdb->e_dma = dma_map_single(dev, key->e, key->e_sz, DMA_TO_DEVICE);
380 if (dma_mapping_error(dev, pdb->e_dma)) {
381 dev_err(dev, "Unable to map RSA public exponent memory\n");
382 dma_unmap_single(dev, pdb->n_dma, key->n_sz, DMA_TO_DEVICE);
386 if (edesc->src_nents > 1) {
387 pdb->sgf |= RSA_PDB_SGF_F;
388 pdb->f_dma = edesc->sec4_sg_dma;
389 sec4_sg_index += edesc->src_nents;
391 pdb->f_dma = sg_dma_address(req_ctx->fixup_src);
394 if (edesc->dst_nents > 1) {
395 pdb->sgf |= RSA_PDB_SGF_G;
396 pdb->g_dma = edesc->sec4_sg_dma +
397 sec4_sg_index * sizeof(struct sec4_sg_entry);
399 pdb->g_dma = sg_dma_address(req->dst);
402 pdb->sgf |= (key->e_sz << RSA_PDB_E_SHIFT) | key->n_sz;
403 pdb->f_len = req_ctx->fixup_src_len;
408 static int set_rsa_priv_f1_pdb(struct akcipher_request *req,
409 struct rsa_edesc *edesc)
411 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
412 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
413 struct caam_rsa_key *key = &ctx->key;
414 struct device *dev = ctx->dev;
415 struct rsa_priv_f1_pdb *pdb = &edesc->pdb.priv_f1;
416 int sec4_sg_index = 0;
418 pdb->n_dma = dma_map_single(dev, key->n, key->n_sz, DMA_TO_DEVICE);
419 if (dma_mapping_error(dev, pdb->n_dma)) {
420 dev_err(dev, "Unable to map modulus memory\n");
424 pdb->d_dma = dma_map_single(dev, key->d, key->d_sz, DMA_TO_DEVICE);
425 if (dma_mapping_error(dev, pdb->d_dma)) {
426 dev_err(dev, "Unable to map RSA private exponent memory\n");
427 dma_unmap_single(dev, pdb->n_dma, key->n_sz, DMA_TO_DEVICE);
431 if (edesc->src_nents > 1) {
432 pdb->sgf |= RSA_PRIV_PDB_SGF_G;
433 pdb->g_dma = edesc->sec4_sg_dma;
434 sec4_sg_index += edesc->src_nents;
436 struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
438 pdb->g_dma = sg_dma_address(req_ctx->fixup_src);
441 if (edesc->dst_nents > 1) {
442 pdb->sgf |= RSA_PRIV_PDB_SGF_F;
443 pdb->f_dma = edesc->sec4_sg_dma +
444 sec4_sg_index * sizeof(struct sec4_sg_entry);
446 pdb->f_dma = sg_dma_address(req->dst);
449 pdb->sgf |= (key->d_sz << RSA_PDB_D_SHIFT) | key->n_sz;
454 static int set_rsa_priv_f2_pdb(struct akcipher_request *req,
455 struct rsa_edesc *edesc)
457 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
458 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
459 struct caam_rsa_key *key = &ctx->key;
460 struct device *dev = ctx->dev;
461 struct rsa_priv_f2_pdb *pdb = &edesc->pdb.priv_f2;
462 int sec4_sg_index = 0;
463 size_t p_sz = key->p_sz;
464 size_t q_sz = key->q_sz;
466 pdb->d_dma = dma_map_single(dev, key->d, key->d_sz, DMA_TO_DEVICE);
467 if (dma_mapping_error(dev, pdb->d_dma)) {
468 dev_err(dev, "Unable to map RSA private exponent memory\n");
472 pdb->p_dma = dma_map_single(dev, key->p, p_sz, DMA_TO_DEVICE);
473 if (dma_mapping_error(dev, pdb->p_dma)) {
474 dev_err(dev, "Unable to map RSA prime factor p memory\n");
478 pdb->q_dma = dma_map_single(dev, key->q, q_sz, DMA_TO_DEVICE);
479 if (dma_mapping_error(dev, pdb->q_dma)) {
480 dev_err(dev, "Unable to map RSA prime factor q memory\n");
484 pdb->tmp1_dma = dma_map_single(dev, key->tmp1, p_sz, DMA_BIDIRECTIONAL);
485 if (dma_mapping_error(dev, pdb->tmp1_dma)) {
486 dev_err(dev, "Unable to map RSA tmp1 memory\n");
490 pdb->tmp2_dma = dma_map_single(dev, key->tmp2, q_sz, DMA_BIDIRECTIONAL);
491 if (dma_mapping_error(dev, pdb->tmp2_dma)) {
492 dev_err(dev, "Unable to map RSA tmp2 memory\n");
496 if (edesc->src_nents > 1) {
497 pdb->sgf |= RSA_PRIV_PDB_SGF_G;
498 pdb->g_dma = edesc->sec4_sg_dma;
499 sec4_sg_index += edesc->src_nents;
501 struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
503 pdb->g_dma = sg_dma_address(req_ctx->fixup_src);
506 if (edesc->dst_nents > 1) {
507 pdb->sgf |= RSA_PRIV_PDB_SGF_F;
508 pdb->f_dma = edesc->sec4_sg_dma +
509 sec4_sg_index * sizeof(struct sec4_sg_entry);
511 pdb->f_dma = sg_dma_address(req->dst);
514 pdb->sgf |= (key->d_sz << RSA_PDB_D_SHIFT) | key->n_sz;
515 pdb->p_q_len = (q_sz << RSA_PDB_Q_SHIFT) | p_sz;
520 dma_unmap_single(dev, pdb->tmp1_dma, p_sz, DMA_BIDIRECTIONAL);
522 dma_unmap_single(dev, pdb->q_dma, q_sz, DMA_TO_DEVICE);
524 dma_unmap_single(dev, pdb->p_dma, p_sz, DMA_TO_DEVICE);
526 dma_unmap_single(dev, pdb->d_dma, key->d_sz, DMA_TO_DEVICE);
531 static int set_rsa_priv_f3_pdb(struct akcipher_request *req,
532 struct rsa_edesc *edesc)
534 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
535 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
536 struct caam_rsa_key *key = &ctx->key;
537 struct device *dev = ctx->dev;
538 struct rsa_priv_f3_pdb *pdb = &edesc->pdb.priv_f3;
539 int sec4_sg_index = 0;
540 size_t p_sz = key->p_sz;
541 size_t q_sz = key->q_sz;
543 pdb->p_dma = dma_map_single(dev, key->p, p_sz, DMA_TO_DEVICE);
544 if (dma_mapping_error(dev, pdb->p_dma)) {
545 dev_err(dev, "Unable to map RSA prime factor p memory\n");
549 pdb->q_dma = dma_map_single(dev, key->q, q_sz, DMA_TO_DEVICE);
550 if (dma_mapping_error(dev, pdb->q_dma)) {
551 dev_err(dev, "Unable to map RSA prime factor q memory\n");
555 pdb->dp_dma = dma_map_single(dev, key->dp, p_sz, DMA_TO_DEVICE);
556 if (dma_mapping_error(dev, pdb->dp_dma)) {
557 dev_err(dev, "Unable to map RSA exponent dp memory\n");
561 pdb->dq_dma = dma_map_single(dev, key->dq, q_sz, DMA_TO_DEVICE);
562 if (dma_mapping_error(dev, pdb->dq_dma)) {
563 dev_err(dev, "Unable to map RSA exponent dq memory\n");
567 pdb->c_dma = dma_map_single(dev, key->qinv, p_sz, DMA_TO_DEVICE);
568 if (dma_mapping_error(dev, pdb->c_dma)) {
569 dev_err(dev, "Unable to map RSA CRT coefficient qinv memory\n");
573 pdb->tmp1_dma = dma_map_single(dev, key->tmp1, p_sz, DMA_BIDIRECTIONAL);
574 if (dma_mapping_error(dev, pdb->tmp1_dma)) {
575 dev_err(dev, "Unable to map RSA tmp1 memory\n");
579 pdb->tmp2_dma = dma_map_single(dev, key->tmp2, q_sz, DMA_BIDIRECTIONAL);
580 if (dma_mapping_error(dev, pdb->tmp2_dma)) {
581 dev_err(dev, "Unable to map RSA tmp2 memory\n");
585 if (edesc->src_nents > 1) {
586 pdb->sgf |= RSA_PRIV_PDB_SGF_G;
587 pdb->g_dma = edesc->sec4_sg_dma;
588 sec4_sg_index += edesc->src_nents;
590 struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
592 pdb->g_dma = sg_dma_address(req_ctx->fixup_src);
595 if (edesc->dst_nents > 1) {
596 pdb->sgf |= RSA_PRIV_PDB_SGF_F;
597 pdb->f_dma = edesc->sec4_sg_dma +
598 sec4_sg_index * sizeof(struct sec4_sg_entry);
600 pdb->f_dma = sg_dma_address(req->dst);
603 pdb->sgf |= key->n_sz;
604 pdb->p_q_len = (q_sz << RSA_PDB_Q_SHIFT) | p_sz;
609 dma_unmap_single(dev, pdb->tmp1_dma, p_sz, DMA_BIDIRECTIONAL);
611 dma_unmap_single(dev, pdb->c_dma, p_sz, DMA_TO_DEVICE);
613 dma_unmap_single(dev, pdb->dq_dma, q_sz, DMA_TO_DEVICE);
615 dma_unmap_single(dev, pdb->dp_dma, p_sz, DMA_TO_DEVICE);
617 dma_unmap_single(dev, pdb->q_dma, q_sz, DMA_TO_DEVICE);
619 dma_unmap_single(dev, pdb->p_dma, p_sz, DMA_TO_DEVICE);
624 static int caam_rsa_enc(struct akcipher_request *req)
626 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
627 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
628 struct caam_rsa_key *key = &ctx->key;
629 struct device *jrdev = ctx->dev;
630 struct rsa_edesc *edesc;
633 if (unlikely(!key->n || !key->e))
636 if (req->dst_len < key->n_sz) {
637 req->dst_len = key->n_sz;
638 dev_err(jrdev, "Output buffer length less than parameter n\n");
642 /* Allocate extended descriptor */
643 edesc = rsa_edesc_alloc(req, DESC_RSA_PUB_LEN);
645 return PTR_ERR(edesc);
647 /* Set RSA Encrypt Protocol Data Block */
648 ret = set_rsa_pub_pdb(req, edesc);
652 /* Initialize Job Descriptor */
653 init_rsa_pub_desc(edesc->hw_desc, &edesc->pdb.pub);
655 ret = caam_jr_enqueue(jrdev, edesc->hw_desc, rsa_pub_done, req);
659 rsa_pub_unmap(jrdev, edesc, req);
662 rsa_io_unmap(jrdev, edesc, req);
667 static int caam_rsa_dec_priv_f1(struct akcipher_request *req)
669 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
670 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
671 struct device *jrdev = ctx->dev;
672 struct rsa_edesc *edesc;
675 /* Allocate extended descriptor */
676 edesc = rsa_edesc_alloc(req, DESC_RSA_PRIV_F1_LEN);
678 return PTR_ERR(edesc);
680 /* Set RSA Decrypt Protocol Data Block - Private Key Form #1 */
681 ret = set_rsa_priv_f1_pdb(req, edesc);
685 /* Initialize Job Descriptor */
686 init_rsa_priv_f1_desc(edesc->hw_desc, &edesc->pdb.priv_f1);
688 ret = caam_jr_enqueue(jrdev, edesc->hw_desc, rsa_priv_f1_done, req);
692 rsa_priv_f1_unmap(jrdev, edesc, req);
695 rsa_io_unmap(jrdev, edesc, req);
700 static int caam_rsa_dec_priv_f2(struct akcipher_request *req)
702 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
703 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
704 struct device *jrdev = ctx->dev;
705 struct rsa_edesc *edesc;
708 /* Allocate extended descriptor */
709 edesc = rsa_edesc_alloc(req, DESC_RSA_PRIV_F2_LEN);
711 return PTR_ERR(edesc);
713 /* Set RSA Decrypt Protocol Data Block - Private Key Form #2 */
714 ret = set_rsa_priv_f2_pdb(req, edesc);
718 /* Initialize Job Descriptor */
719 init_rsa_priv_f2_desc(edesc->hw_desc, &edesc->pdb.priv_f2);
721 ret = caam_jr_enqueue(jrdev, edesc->hw_desc, rsa_priv_f2_done, req);
725 rsa_priv_f2_unmap(jrdev, edesc, req);
728 rsa_io_unmap(jrdev, edesc, req);
733 static int caam_rsa_dec_priv_f3(struct akcipher_request *req)
735 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
736 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
737 struct device *jrdev = ctx->dev;
738 struct rsa_edesc *edesc;
741 /* Allocate extended descriptor */
742 edesc = rsa_edesc_alloc(req, DESC_RSA_PRIV_F3_LEN);
744 return PTR_ERR(edesc);
746 /* Set RSA Decrypt Protocol Data Block - Private Key Form #3 */
747 ret = set_rsa_priv_f3_pdb(req, edesc);
751 /* Initialize Job Descriptor */
752 init_rsa_priv_f3_desc(edesc->hw_desc, &edesc->pdb.priv_f3);
754 ret = caam_jr_enqueue(jrdev, edesc->hw_desc, rsa_priv_f3_done, req);
758 rsa_priv_f3_unmap(jrdev, edesc, req);
761 rsa_io_unmap(jrdev, edesc, req);
766 static int caam_rsa_dec(struct akcipher_request *req)
768 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
769 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
770 struct caam_rsa_key *key = &ctx->key;
773 if (unlikely(!key->n || !key->d))
776 if (req->dst_len < key->n_sz) {
777 req->dst_len = key->n_sz;
778 dev_err(ctx->dev, "Output buffer length less than parameter n\n");
782 if (key->priv_form == FORM3)
783 ret = caam_rsa_dec_priv_f3(req);
784 else if (key->priv_form == FORM2)
785 ret = caam_rsa_dec_priv_f2(req);
787 ret = caam_rsa_dec_priv_f1(req);
792 static void caam_rsa_free_key(struct caam_rsa_key *key)
804 memset(key, 0, sizeof(*key));
807 static void caam_rsa_drop_leading_zeros(const u8 **ptr, size_t *nbytes)
809 while (!**ptr && *nbytes) {
816 * caam_read_rsa_crt - Used for reading dP, dQ, qInv CRT members.
817 * dP, dQ and qInv could decode to less than corresponding p, q length, as the
818 * BER-encoding requires that the minimum number of bytes be used to encode the
819 * integer. dP, dQ, qInv decoded values have to be zero-padded to appropriate
822 * @ptr : pointer to {dP, dQ, qInv} CRT member
823 * @nbytes: length in bytes of {dP, dQ, qInv} CRT member
824 * @dstlen: length in bytes of corresponding p or q prime factor
826 static u8 *caam_read_rsa_crt(const u8 *ptr, size_t nbytes, size_t dstlen)
830 caam_rsa_drop_leading_zeros(&ptr, &nbytes);
834 dst = kzalloc(dstlen, GFP_DMA | GFP_KERNEL);
838 memcpy(dst + (dstlen - nbytes), ptr, nbytes);
844 * caam_read_raw_data - Read a raw byte stream as a positive integer.
845 * The function skips buffer's leading zeros, copies the remained data
846 * to a buffer allocated in the GFP_DMA | GFP_KERNEL zone and returns
847 * the address of the new buffer.
849 * @buf : The data to read
850 * @nbytes: The amount of data to read
852 static inline u8 *caam_read_raw_data(const u8 *buf, size_t *nbytes)
855 caam_rsa_drop_leading_zeros(&buf, nbytes);
859 return kmemdup(buf, *nbytes, GFP_DMA | GFP_KERNEL);
862 static int caam_rsa_check_key_length(unsigned int len)
869 static int caam_rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
872 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
873 struct rsa_key raw_key = {NULL};
874 struct caam_rsa_key *rsa_key = &ctx->key;
877 /* Free the old RSA key if any */
878 caam_rsa_free_key(rsa_key);
880 ret = rsa_parse_pub_key(&raw_key, key, keylen);
884 /* Copy key in DMA zone */
885 rsa_key->e = kmemdup(raw_key.e, raw_key.e_sz, GFP_DMA | GFP_KERNEL);
890 * Skip leading zeros and copy the positive integer to a buffer
891 * allocated in the GFP_DMA | GFP_KERNEL zone. The decryption descriptor
892 * expects a positive integer for the RSA modulus and uses its length as
893 * decryption output length.
895 rsa_key->n = caam_read_raw_data(raw_key.n, &raw_key.n_sz);
899 if (caam_rsa_check_key_length(raw_key.n_sz << 3)) {
900 caam_rsa_free_key(rsa_key);
904 rsa_key->e_sz = raw_key.e_sz;
905 rsa_key->n_sz = raw_key.n_sz;
909 caam_rsa_free_key(rsa_key);
913 static void caam_rsa_set_priv_key_form(struct caam_rsa_ctx *ctx,
914 struct rsa_key *raw_key)
916 struct caam_rsa_key *rsa_key = &ctx->key;
917 size_t p_sz = raw_key->p_sz;
918 size_t q_sz = raw_key->q_sz;
920 rsa_key->p = caam_read_raw_data(raw_key->p, &p_sz);
923 rsa_key->p_sz = p_sz;
925 rsa_key->q = caam_read_raw_data(raw_key->q, &q_sz);
928 rsa_key->q_sz = q_sz;
930 rsa_key->tmp1 = kzalloc(raw_key->p_sz, GFP_DMA | GFP_KERNEL);
934 rsa_key->tmp2 = kzalloc(raw_key->q_sz, GFP_DMA | GFP_KERNEL);
938 rsa_key->priv_form = FORM2;
940 rsa_key->dp = caam_read_rsa_crt(raw_key->dp, raw_key->dp_sz, p_sz);
944 rsa_key->dq = caam_read_rsa_crt(raw_key->dq, raw_key->dq_sz, q_sz);
948 rsa_key->qinv = caam_read_rsa_crt(raw_key->qinv, raw_key->qinv_sz,
953 rsa_key->priv_form = FORM3;
962 kzfree(rsa_key->tmp2);
964 kzfree(rsa_key->tmp1);
971 static int caam_rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
974 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
975 struct rsa_key raw_key = {NULL};
976 struct caam_rsa_key *rsa_key = &ctx->key;
979 /* Free the old RSA key if any */
980 caam_rsa_free_key(rsa_key);
982 ret = rsa_parse_priv_key(&raw_key, key, keylen);
986 /* Copy key in DMA zone */
987 rsa_key->d = kmemdup(raw_key.d, raw_key.d_sz, GFP_DMA | GFP_KERNEL);
991 rsa_key->e = kmemdup(raw_key.e, raw_key.e_sz, GFP_DMA | GFP_KERNEL);
996 * Skip leading zeros and copy the positive integer to a buffer
997 * allocated in the GFP_DMA | GFP_KERNEL zone. The decryption descriptor
998 * expects a positive integer for the RSA modulus and uses its length as
999 * decryption output length.
1001 rsa_key->n = caam_read_raw_data(raw_key.n, &raw_key.n_sz);
1005 if (caam_rsa_check_key_length(raw_key.n_sz << 3)) {
1006 caam_rsa_free_key(rsa_key);
1010 rsa_key->d_sz = raw_key.d_sz;
1011 rsa_key->e_sz = raw_key.e_sz;
1012 rsa_key->n_sz = raw_key.n_sz;
1014 caam_rsa_set_priv_key_form(ctx, &raw_key);
1019 caam_rsa_free_key(rsa_key);
1023 static unsigned int caam_rsa_max_size(struct crypto_akcipher *tfm)
1025 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
1027 return ctx->key.n_sz;
1030 /* Per session pkc's driver context creation function */
1031 static int caam_rsa_init_tfm(struct crypto_akcipher *tfm)
1033 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
1035 ctx->dev = caam_jr_alloc();
1037 if (IS_ERR(ctx->dev)) {
1038 pr_err("Job Ring Device allocation for transform failed\n");
1039 return PTR_ERR(ctx->dev);
1042 ctx->padding_dma = dma_map_single(ctx->dev, zero_buffer,
1043 CAAM_RSA_MAX_INPUT_SIZE - 1,
1045 if (dma_mapping_error(ctx->dev, ctx->padding_dma)) {
1046 dev_err(ctx->dev, "unable to map padding\n");
1047 caam_jr_free(ctx->dev);
1054 /* Per session pkc's driver context cleanup function */
1055 static void caam_rsa_exit_tfm(struct crypto_akcipher *tfm)
1057 struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
1058 struct caam_rsa_key *key = &ctx->key;
1060 dma_unmap_single(ctx->dev, ctx->padding_dma, CAAM_RSA_MAX_INPUT_SIZE -
1062 caam_rsa_free_key(key);
1063 caam_jr_free(ctx->dev);
1066 static struct caam_akcipher_alg caam_rsa = {
1068 .encrypt = caam_rsa_enc,
1069 .decrypt = caam_rsa_dec,
1070 .set_pub_key = caam_rsa_set_pub_key,
1071 .set_priv_key = caam_rsa_set_priv_key,
1072 .max_size = caam_rsa_max_size,
1073 .init = caam_rsa_init_tfm,
1074 .exit = caam_rsa_exit_tfm,
1075 .reqsize = sizeof(struct caam_rsa_req_ctx),
1078 .cra_driver_name = "rsa-caam",
1079 .cra_priority = 3000,
1080 .cra_module = THIS_MODULE,
1081 .cra_ctxsize = sizeof(struct caam_rsa_ctx),
1086 /* Public Key Cryptography module initialization handler */
1087 int caam_pkc_init(struct device *ctrldev)
1089 struct caam_drv_private *priv = dev_get_drvdata(ctrldev);
1094 /* Determine public key hardware accelerator presence. */
1096 pk_inst = (rd_reg32(&priv->ctrl->perfmon.cha_num_ls) &
1097 CHA_ID_LS_PK_MASK) >> CHA_ID_LS_PK_SHIFT;
1099 pk_inst = rd_reg32(&priv->ctrl->vreg.pkha) & CHA_VER_NUM_MASK;
1101 /* Do not register algorithms if PKHA is not present. */
1105 /* allocate zero buffer, used for padding input */
1106 zero_buffer = kzalloc(CAAM_RSA_MAX_INPUT_SIZE - 1, GFP_DMA |
1111 err = crypto_register_akcipher(&caam_rsa.akcipher);
1115 dev_warn(ctrldev, "%s alg registration failed\n",
1116 caam_rsa.akcipher.base.cra_driver_name);
1119 caam_rsa.registered = true;
1120 dev_info(ctrldev, "caam pkc algorithms registered in /proc/crypto\n");
1126 void caam_pkc_exit(void)
1131 if (caam_rsa.registered)
1132 crypto_unregister_akcipher(&caam_rsa.akcipher);