xref: /linux/drivers/crypto/caam/caampkc.c (revision d53e44fe)
1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /*
3  * caam - Freescale FSL CAAM support for Public Key Cryptography
4  *
5  * Copyright 2016 Freescale Semiconductor, Inc.
6  * Copyright 2018-2019 NXP
7  *
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.
10  */
11 #include "compat.h"
12 #include "regs.h"
13 #include "intern.h"
14 #include "jr.h"
15 #include "error.h"
16 #include "desc_constr.h"
17 #include "sg_sw_sec4.h"
18 #include "caampkc.h"
19 
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 */
28 
29 /* buffer filled with zeros, used for padding */
30 static u8 *zero_buffer;
31 
32 /*
33  * variable used to avoid double free of resources in case
34  * algorithm registration was unsuccessful
35  */
36 static bool init_done;
37 
38 struct caam_akcipher_alg {
39 	struct akcipher_alg akcipher;
40 	bool registered;
41 };
42 
43 static void rsa_io_unmap(struct device *dev, struct rsa_edesc *edesc,
44 			 struct akcipher_request *req)
45 {
46 	struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
47 
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);
50 
51 	if (edesc->sec4_sg_bytes)
52 		dma_unmap_single(dev, edesc->sec4_sg_dma, edesc->sec4_sg_bytes,
53 				 DMA_TO_DEVICE);
54 }
55 
56 static void rsa_pub_unmap(struct device *dev, struct rsa_edesc *edesc,
57 			  struct akcipher_request *req)
58 {
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;
63 
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);
66 }
67 
68 static void rsa_priv_f1_unmap(struct device *dev, struct rsa_edesc *edesc,
69 			      struct akcipher_request *req)
70 {
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;
75 
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);
78 }
79 
80 static void rsa_priv_f2_unmap(struct device *dev, struct rsa_edesc *edesc,
81 			      struct akcipher_request *req)
82 {
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;
89 
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);
95 }
96 
97 static void rsa_priv_f3_unmap(struct device *dev, struct rsa_edesc *edesc,
98 			      struct akcipher_request *req)
99 {
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;
106 
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);
114 }
115 
116 /* RSA Job Completion handler */
117 static void rsa_pub_done(struct device *dev, u32 *desc, u32 err, void *context)
118 {
119 	struct akcipher_request *req = context;
120 	struct rsa_edesc *edesc;
121 	int ecode = 0;
122 
123 	if (err)
124 		ecode = caam_jr_strstatus(dev, err);
125 
126 	edesc = container_of(desc, struct rsa_edesc, hw_desc[0]);
127 
128 	rsa_pub_unmap(dev, edesc, req);
129 	rsa_io_unmap(dev, edesc, req);
130 	kfree(edesc);
131 
132 	akcipher_request_complete(req, ecode);
133 }
134 
135 static void rsa_priv_f_done(struct device *dev, u32 *desc, u32 err,
136 			    void *context)
137 {
138 	struct akcipher_request *req = context;
139 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
140 	struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
141 	struct caam_rsa_key *key = &ctx->key;
142 	struct rsa_edesc *edesc;
143 	int ecode = 0;
144 
145 	if (err)
146 		ecode = caam_jr_strstatus(dev, err);
147 
148 	edesc = container_of(desc, struct rsa_edesc, hw_desc[0]);
149 
150 	switch (key->priv_form) {
151 	case FORM1:
152 		rsa_priv_f1_unmap(dev, edesc, req);
153 		break;
154 	case FORM2:
155 		rsa_priv_f2_unmap(dev, edesc, req);
156 		break;
157 	case FORM3:
158 		rsa_priv_f3_unmap(dev, edesc, req);
159 	}
160 
161 	rsa_io_unmap(dev, edesc, req);
162 	kfree(edesc);
163 
164 	akcipher_request_complete(req, ecode);
165 }
166 
167 /**
168  * Count leading zeros, need it to strip, from a given scatterlist
169  *
170  * @sgl   : scatterlist to count zeros from
171  * @nbytes: number of zeros, in bytes, to strip
172  * @flags : operation flags
173  */
174 static int caam_rsa_count_leading_zeros(struct scatterlist *sgl,
175 					unsigned int nbytes,
176 					unsigned int flags)
177 {
178 	struct sg_mapping_iter miter;
179 	int lzeros, ents;
180 	unsigned int len;
181 	unsigned int tbytes = nbytes;
182 	const u8 *buff;
183 
184 	ents = sg_nents_for_len(sgl, nbytes);
185 	if (ents < 0)
186 		return ents;
187 
188 	sg_miter_start(&miter, sgl, ents, SG_MITER_FROM_SG | flags);
189 
190 	lzeros = 0;
191 	len = 0;
192 	while (nbytes > 0) {
193 		/* do not strip more than given bytes */
194 		while (len && !*buff && lzeros < nbytes) {
195 			lzeros++;
196 			len--;
197 			buff++;
198 		}
199 
200 		if (len && *buff)
201 			break;
202 
203 		sg_miter_next(&miter);
204 		buff = miter.addr;
205 		len = miter.length;
206 
207 		nbytes -= lzeros;
208 		lzeros = 0;
209 	}
210 
211 	miter.consumed = lzeros;
212 	sg_miter_stop(&miter);
213 	nbytes -= lzeros;
214 
215 	return tbytes - nbytes;
216 }
217 
218 static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req,
219 					 size_t desclen)
220 {
221 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
222 	struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
223 	struct device *dev = ctx->dev;
224 	struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
225 	struct caam_rsa_key *key = &ctx->key;
226 	struct rsa_edesc *edesc;
227 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
228 		       GFP_KERNEL : GFP_ATOMIC;
229 	int sg_flags = (flags == GFP_ATOMIC) ? SG_MITER_ATOMIC : 0;
230 	int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes;
231 	int src_nents, dst_nents;
232 	int mapped_src_nents, mapped_dst_nents;
233 	unsigned int diff_size = 0;
234 	int lzeros;
235 
236 	if (req->src_len > key->n_sz) {
237 		/*
238 		 * strip leading zeros and
239 		 * return the number of zeros to skip
240 		 */
241 		lzeros = caam_rsa_count_leading_zeros(req->src, req->src_len -
242 						      key->n_sz, sg_flags);
243 		if (lzeros < 0)
244 			return ERR_PTR(lzeros);
245 
246 		req_ctx->fixup_src = scatterwalk_ffwd(req_ctx->src, req->src,
247 						      lzeros);
248 		req_ctx->fixup_src_len = req->src_len - lzeros;
249 	} else {
250 		/*
251 		 * input src is less then n key modulus,
252 		 * so there will be zero padding
253 		 */
254 		diff_size = key->n_sz - req->src_len;
255 		req_ctx->fixup_src = req->src;
256 		req_ctx->fixup_src_len = req->src_len;
257 	}
258 
259 	src_nents = sg_nents_for_len(req_ctx->fixup_src,
260 				     req_ctx->fixup_src_len);
261 	dst_nents = sg_nents_for_len(req->dst, req->dst_len);
262 
263 	mapped_src_nents = dma_map_sg(dev, req_ctx->fixup_src, src_nents,
264 				      DMA_TO_DEVICE);
265 	if (unlikely(!mapped_src_nents)) {
266 		dev_err(dev, "unable to map source\n");
267 		return ERR_PTR(-ENOMEM);
268 	}
269 	mapped_dst_nents = dma_map_sg(dev, req->dst, dst_nents,
270 				      DMA_FROM_DEVICE);
271 	if (unlikely(!mapped_dst_nents)) {
272 		dev_err(dev, "unable to map destination\n");
273 		goto src_fail;
274 	}
275 
276 	if (!diff_size && mapped_src_nents == 1)
277 		sec4_sg_len = 0; /* no need for an input hw s/g table */
278 	else
279 		sec4_sg_len = mapped_src_nents + !!diff_size;
280 	sec4_sg_index = sec4_sg_len;
281 
282 	if (mapped_dst_nents > 1)
283 		sec4_sg_len += pad_sg_nents(mapped_dst_nents);
284 	else
285 		sec4_sg_len = pad_sg_nents(sec4_sg_len);
286 
287 	sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry);
288 
289 	/* allocate space for base edesc, hw desc commands and link tables */
290 	edesc = kzalloc(sizeof(*edesc) + desclen + sec4_sg_bytes,
291 			GFP_DMA | flags);
292 	if (!edesc)
293 		goto dst_fail;
294 
295 	edesc->sec4_sg = (void *)edesc + sizeof(*edesc) + desclen;
296 	if (diff_size)
297 		dma_to_sec4_sg_one(edesc->sec4_sg, ctx->padding_dma, diff_size,
298 				   0);
299 
300 	if (sec4_sg_index)
301 		sg_to_sec4_sg_last(req_ctx->fixup_src, req_ctx->fixup_src_len,
302 				   edesc->sec4_sg + !!diff_size, 0);
303 
304 	if (mapped_dst_nents > 1)
305 		sg_to_sec4_sg_last(req->dst, req->dst_len,
306 				   edesc->sec4_sg + sec4_sg_index, 0);
307 
308 	/* Save nents for later use in Job Descriptor */
309 	edesc->src_nents = src_nents;
310 	edesc->dst_nents = dst_nents;
311 
312 	if (!sec4_sg_bytes)
313 		return edesc;
314 
315 	edesc->mapped_src_nents = mapped_src_nents;
316 	edesc->mapped_dst_nents = mapped_dst_nents;
317 
318 	edesc->sec4_sg_dma = dma_map_single(dev, edesc->sec4_sg,
319 					    sec4_sg_bytes, DMA_TO_DEVICE);
320 	if (dma_mapping_error(dev, edesc->sec4_sg_dma)) {
321 		dev_err(dev, "unable to map S/G table\n");
322 		goto sec4_sg_fail;
323 	}
324 
325 	edesc->sec4_sg_bytes = sec4_sg_bytes;
326 
327 	print_hex_dump_debug("caampkc sec4_sg@" __stringify(__LINE__) ": ",
328 			     DUMP_PREFIX_ADDRESS, 16, 4, edesc->sec4_sg,
329 			     edesc->sec4_sg_bytes, 1);
330 
331 	return edesc;
332 
333 sec4_sg_fail:
334 	kfree(edesc);
335 dst_fail:
336 	dma_unmap_sg(dev, req->dst, dst_nents, DMA_FROM_DEVICE);
337 src_fail:
338 	dma_unmap_sg(dev, req_ctx->fixup_src, src_nents, DMA_TO_DEVICE);
339 	return ERR_PTR(-ENOMEM);
340 }
341 
342 static int set_rsa_pub_pdb(struct akcipher_request *req,
343 			   struct rsa_edesc *edesc)
344 {
345 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
346 	struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
347 	struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
348 	struct caam_rsa_key *key = &ctx->key;
349 	struct device *dev = ctx->dev;
350 	struct rsa_pub_pdb *pdb = &edesc->pdb.pub;
351 	int sec4_sg_index = 0;
352 
353 	pdb->n_dma = dma_map_single(dev, key->n, key->n_sz, DMA_TO_DEVICE);
354 	if (dma_mapping_error(dev, pdb->n_dma)) {
355 		dev_err(dev, "Unable to map RSA modulus memory\n");
356 		return -ENOMEM;
357 	}
358 
359 	pdb->e_dma = dma_map_single(dev, key->e, key->e_sz, DMA_TO_DEVICE);
360 	if (dma_mapping_error(dev, pdb->e_dma)) {
361 		dev_err(dev, "Unable to map RSA public exponent memory\n");
362 		dma_unmap_single(dev, pdb->n_dma, key->n_sz, DMA_TO_DEVICE);
363 		return -ENOMEM;
364 	}
365 
366 	if (edesc->mapped_src_nents > 1) {
367 		pdb->sgf |= RSA_PDB_SGF_F;
368 		pdb->f_dma = edesc->sec4_sg_dma;
369 		sec4_sg_index += edesc->mapped_src_nents;
370 	} else {
371 		pdb->f_dma = sg_dma_address(req_ctx->fixup_src);
372 	}
373 
374 	if (edesc->mapped_dst_nents > 1) {
375 		pdb->sgf |= RSA_PDB_SGF_G;
376 		pdb->g_dma = edesc->sec4_sg_dma +
377 			     sec4_sg_index * sizeof(struct sec4_sg_entry);
378 	} else {
379 		pdb->g_dma = sg_dma_address(req->dst);
380 	}
381 
382 	pdb->sgf |= (key->e_sz << RSA_PDB_E_SHIFT) | key->n_sz;
383 	pdb->f_len = req_ctx->fixup_src_len;
384 
385 	return 0;
386 }
387 
388 static int set_rsa_priv_f1_pdb(struct akcipher_request *req,
389 			       struct rsa_edesc *edesc)
390 {
391 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
392 	struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
393 	struct caam_rsa_key *key = &ctx->key;
394 	struct device *dev = ctx->dev;
395 	struct rsa_priv_f1_pdb *pdb = &edesc->pdb.priv_f1;
396 	int sec4_sg_index = 0;
397 
398 	pdb->n_dma = dma_map_single(dev, key->n, key->n_sz, DMA_TO_DEVICE);
399 	if (dma_mapping_error(dev, pdb->n_dma)) {
400 		dev_err(dev, "Unable to map modulus memory\n");
401 		return -ENOMEM;
402 	}
403 
404 	pdb->d_dma = dma_map_single(dev, key->d, key->d_sz, DMA_TO_DEVICE);
405 	if (dma_mapping_error(dev, pdb->d_dma)) {
406 		dev_err(dev, "Unable to map RSA private exponent memory\n");
407 		dma_unmap_single(dev, pdb->n_dma, key->n_sz, DMA_TO_DEVICE);
408 		return -ENOMEM;
409 	}
410 
411 	if (edesc->mapped_src_nents > 1) {
412 		pdb->sgf |= RSA_PRIV_PDB_SGF_G;
413 		pdb->g_dma = edesc->sec4_sg_dma;
414 		sec4_sg_index += edesc->mapped_src_nents;
415 
416 	} else {
417 		struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
418 
419 		pdb->g_dma = sg_dma_address(req_ctx->fixup_src);
420 	}
421 
422 	if (edesc->mapped_dst_nents > 1) {
423 		pdb->sgf |= RSA_PRIV_PDB_SGF_F;
424 		pdb->f_dma = edesc->sec4_sg_dma +
425 			     sec4_sg_index * sizeof(struct sec4_sg_entry);
426 	} else {
427 		pdb->f_dma = sg_dma_address(req->dst);
428 	}
429 
430 	pdb->sgf |= (key->d_sz << RSA_PDB_D_SHIFT) | key->n_sz;
431 
432 	return 0;
433 }
434 
435 static int set_rsa_priv_f2_pdb(struct akcipher_request *req,
436 			       struct rsa_edesc *edesc)
437 {
438 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
439 	struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
440 	struct caam_rsa_key *key = &ctx->key;
441 	struct device *dev = ctx->dev;
442 	struct rsa_priv_f2_pdb *pdb = &edesc->pdb.priv_f2;
443 	int sec4_sg_index = 0;
444 	size_t p_sz = key->p_sz;
445 	size_t q_sz = key->q_sz;
446 
447 	pdb->d_dma = dma_map_single(dev, key->d, key->d_sz, DMA_TO_DEVICE);
448 	if (dma_mapping_error(dev, pdb->d_dma)) {
449 		dev_err(dev, "Unable to map RSA private exponent memory\n");
450 		return -ENOMEM;
451 	}
452 
453 	pdb->p_dma = dma_map_single(dev, key->p, p_sz, DMA_TO_DEVICE);
454 	if (dma_mapping_error(dev, pdb->p_dma)) {
455 		dev_err(dev, "Unable to map RSA prime factor p memory\n");
456 		goto unmap_d;
457 	}
458 
459 	pdb->q_dma = dma_map_single(dev, key->q, q_sz, DMA_TO_DEVICE);
460 	if (dma_mapping_error(dev, pdb->q_dma)) {
461 		dev_err(dev, "Unable to map RSA prime factor q memory\n");
462 		goto unmap_p;
463 	}
464 
465 	pdb->tmp1_dma = dma_map_single(dev, key->tmp1, p_sz, DMA_BIDIRECTIONAL);
466 	if (dma_mapping_error(dev, pdb->tmp1_dma)) {
467 		dev_err(dev, "Unable to map RSA tmp1 memory\n");
468 		goto unmap_q;
469 	}
470 
471 	pdb->tmp2_dma = dma_map_single(dev, key->tmp2, q_sz, DMA_BIDIRECTIONAL);
472 	if (dma_mapping_error(dev, pdb->tmp2_dma)) {
473 		dev_err(dev, "Unable to map RSA tmp2 memory\n");
474 		goto unmap_tmp1;
475 	}
476 
477 	if (edesc->mapped_src_nents > 1) {
478 		pdb->sgf |= RSA_PRIV_PDB_SGF_G;
479 		pdb->g_dma = edesc->sec4_sg_dma;
480 		sec4_sg_index += edesc->mapped_src_nents;
481 	} else {
482 		struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
483 
484 		pdb->g_dma = sg_dma_address(req_ctx->fixup_src);
485 	}
486 
487 	if (edesc->mapped_dst_nents > 1) {
488 		pdb->sgf |= RSA_PRIV_PDB_SGF_F;
489 		pdb->f_dma = edesc->sec4_sg_dma +
490 			     sec4_sg_index * sizeof(struct sec4_sg_entry);
491 	} else {
492 		pdb->f_dma = sg_dma_address(req->dst);
493 	}
494 
495 	pdb->sgf |= (key->d_sz << RSA_PDB_D_SHIFT) | key->n_sz;
496 	pdb->p_q_len = (q_sz << RSA_PDB_Q_SHIFT) | p_sz;
497 
498 	return 0;
499 
500 unmap_tmp1:
501 	dma_unmap_single(dev, pdb->tmp1_dma, p_sz, DMA_BIDIRECTIONAL);
502 unmap_q:
503 	dma_unmap_single(dev, pdb->q_dma, q_sz, DMA_TO_DEVICE);
504 unmap_p:
505 	dma_unmap_single(dev, pdb->p_dma, p_sz, DMA_TO_DEVICE);
506 unmap_d:
507 	dma_unmap_single(dev, pdb->d_dma, key->d_sz, DMA_TO_DEVICE);
508 
509 	return -ENOMEM;
510 }
511 
512 static int set_rsa_priv_f3_pdb(struct akcipher_request *req,
513 			       struct rsa_edesc *edesc)
514 {
515 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
516 	struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
517 	struct caam_rsa_key *key = &ctx->key;
518 	struct device *dev = ctx->dev;
519 	struct rsa_priv_f3_pdb *pdb = &edesc->pdb.priv_f3;
520 	int sec4_sg_index = 0;
521 	size_t p_sz = key->p_sz;
522 	size_t q_sz = key->q_sz;
523 
524 	pdb->p_dma = dma_map_single(dev, key->p, p_sz, DMA_TO_DEVICE);
525 	if (dma_mapping_error(dev, pdb->p_dma)) {
526 		dev_err(dev, "Unable to map RSA prime factor p memory\n");
527 		return -ENOMEM;
528 	}
529 
530 	pdb->q_dma = dma_map_single(dev, key->q, q_sz, DMA_TO_DEVICE);
531 	if (dma_mapping_error(dev, pdb->q_dma)) {
532 		dev_err(dev, "Unable to map RSA prime factor q memory\n");
533 		goto unmap_p;
534 	}
535 
536 	pdb->dp_dma = dma_map_single(dev, key->dp, p_sz, DMA_TO_DEVICE);
537 	if (dma_mapping_error(dev, pdb->dp_dma)) {
538 		dev_err(dev, "Unable to map RSA exponent dp memory\n");
539 		goto unmap_q;
540 	}
541 
542 	pdb->dq_dma = dma_map_single(dev, key->dq, q_sz, DMA_TO_DEVICE);
543 	if (dma_mapping_error(dev, pdb->dq_dma)) {
544 		dev_err(dev, "Unable to map RSA exponent dq memory\n");
545 		goto unmap_dp;
546 	}
547 
548 	pdb->c_dma = dma_map_single(dev, key->qinv, p_sz, DMA_TO_DEVICE);
549 	if (dma_mapping_error(dev, pdb->c_dma)) {
550 		dev_err(dev, "Unable to map RSA CRT coefficient qinv memory\n");
551 		goto unmap_dq;
552 	}
553 
554 	pdb->tmp1_dma = dma_map_single(dev, key->tmp1, p_sz, DMA_BIDIRECTIONAL);
555 	if (dma_mapping_error(dev, pdb->tmp1_dma)) {
556 		dev_err(dev, "Unable to map RSA tmp1 memory\n");
557 		goto unmap_qinv;
558 	}
559 
560 	pdb->tmp2_dma = dma_map_single(dev, key->tmp2, q_sz, DMA_BIDIRECTIONAL);
561 	if (dma_mapping_error(dev, pdb->tmp2_dma)) {
562 		dev_err(dev, "Unable to map RSA tmp2 memory\n");
563 		goto unmap_tmp1;
564 	}
565 
566 	if (edesc->mapped_src_nents > 1) {
567 		pdb->sgf |= RSA_PRIV_PDB_SGF_G;
568 		pdb->g_dma = edesc->sec4_sg_dma;
569 		sec4_sg_index += edesc->mapped_src_nents;
570 	} else {
571 		struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
572 
573 		pdb->g_dma = sg_dma_address(req_ctx->fixup_src);
574 	}
575 
576 	if (edesc->mapped_dst_nents > 1) {
577 		pdb->sgf |= RSA_PRIV_PDB_SGF_F;
578 		pdb->f_dma = edesc->sec4_sg_dma +
579 			     sec4_sg_index * sizeof(struct sec4_sg_entry);
580 	} else {
581 		pdb->f_dma = sg_dma_address(req->dst);
582 	}
583 
584 	pdb->sgf |= key->n_sz;
585 	pdb->p_q_len = (q_sz << RSA_PDB_Q_SHIFT) | p_sz;
586 
587 	return 0;
588 
589 unmap_tmp1:
590 	dma_unmap_single(dev, pdb->tmp1_dma, p_sz, DMA_BIDIRECTIONAL);
591 unmap_qinv:
592 	dma_unmap_single(dev, pdb->c_dma, p_sz, DMA_TO_DEVICE);
593 unmap_dq:
594 	dma_unmap_single(dev, pdb->dq_dma, q_sz, DMA_TO_DEVICE);
595 unmap_dp:
596 	dma_unmap_single(dev, pdb->dp_dma, p_sz, DMA_TO_DEVICE);
597 unmap_q:
598 	dma_unmap_single(dev, pdb->q_dma, q_sz, DMA_TO_DEVICE);
599 unmap_p:
600 	dma_unmap_single(dev, pdb->p_dma, p_sz, DMA_TO_DEVICE);
601 
602 	return -ENOMEM;
603 }
604 
605 static int caam_rsa_enc(struct akcipher_request *req)
606 {
607 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
608 	struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
609 	struct caam_rsa_key *key = &ctx->key;
610 	struct device *jrdev = ctx->dev;
611 	struct rsa_edesc *edesc;
612 	int ret;
613 
614 	if (unlikely(!key->n || !key->e))
615 		return -EINVAL;
616 
617 	if (req->dst_len < key->n_sz) {
618 		req->dst_len = key->n_sz;
619 		dev_err(jrdev, "Output buffer length less than parameter n\n");
620 		return -EOVERFLOW;
621 	}
622 
623 	/* Allocate extended descriptor */
624 	edesc = rsa_edesc_alloc(req, DESC_RSA_PUB_LEN);
625 	if (IS_ERR(edesc))
626 		return PTR_ERR(edesc);
627 
628 	/* Set RSA Encrypt Protocol Data Block */
629 	ret = set_rsa_pub_pdb(req, edesc);
630 	if (ret)
631 		goto init_fail;
632 
633 	/* Initialize Job Descriptor */
634 	init_rsa_pub_desc(edesc->hw_desc, &edesc->pdb.pub);
635 
636 	ret = caam_jr_enqueue(jrdev, edesc->hw_desc, rsa_pub_done, req);
637 	if (!ret)
638 		return -EINPROGRESS;
639 
640 	rsa_pub_unmap(jrdev, edesc, req);
641 
642 init_fail:
643 	rsa_io_unmap(jrdev, edesc, req);
644 	kfree(edesc);
645 	return ret;
646 }
647 
648 static int caam_rsa_dec_priv_f1(struct akcipher_request *req)
649 {
650 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
651 	struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
652 	struct device *jrdev = ctx->dev;
653 	struct rsa_edesc *edesc;
654 	int ret;
655 
656 	/* Allocate extended descriptor */
657 	edesc = rsa_edesc_alloc(req, DESC_RSA_PRIV_F1_LEN);
658 	if (IS_ERR(edesc))
659 		return PTR_ERR(edesc);
660 
661 	/* Set RSA Decrypt Protocol Data Block - Private Key Form #1 */
662 	ret = set_rsa_priv_f1_pdb(req, edesc);
663 	if (ret)
664 		goto init_fail;
665 
666 	/* Initialize Job Descriptor */
667 	init_rsa_priv_f1_desc(edesc->hw_desc, &edesc->pdb.priv_f1);
668 
669 	ret = caam_jr_enqueue(jrdev, edesc->hw_desc, rsa_priv_f_done, req);
670 	if (!ret)
671 		return -EINPROGRESS;
672 
673 	rsa_priv_f1_unmap(jrdev, edesc, req);
674 
675 init_fail:
676 	rsa_io_unmap(jrdev, edesc, req);
677 	kfree(edesc);
678 	return ret;
679 }
680 
681 static int caam_rsa_dec_priv_f2(struct akcipher_request *req)
682 {
683 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
684 	struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
685 	struct device *jrdev = ctx->dev;
686 	struct rsa_edesc *edesc;
687 	int ret;
688 
689 	/* Allocate extended descriptor */
690 	edesc = rsa_edesc_alloc(req, DESC_RSA_PRIV_F2_LEN);
691 	if (IS_ERR(edesc))
692 		return PTR_ERR(edesc);
693 
694 	/* Set RSA Decrypt Protocol Data Block - Private Key Form #2 */
695 	ret = set_rsa_priv_f2_pdb(req, edesc);
696 	if (ret)
697 		goto init_fail;
698 
699 	/* Initialize Job Descriptor */
700 	init_rsa_priv_f2_desc(edesc->hw_desc, &edesc->pdb.priv_f2);
701 
702 	ret = caam_jr_enqueue(jrdev, edesc->hw_desc, rsa_priv_f_done, req);
703 	if (!ret)
704 		return -EINPROGRESS;
705 
706 	rsa_priv_f2_unmap(jrdev, edesc, req);
707 
708 init_fail:
709 	rsa_io_unmap(jrdev, edesc, req);
710 	kfree(edesc);
711 	return ret;
712 }
713 
714 static int caam_rsa_dec_priv_f3(struct akcipher_request *req)
715 {
716 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
717 	struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
718 	struct device *jrdev = ctx->dev;
719 	struct rsa_edesc *edesc;
720 	int ret;
721 
722 	/* Allocate extended descriptor */
723 	edesc = rsa_edesc_alloc(req, DESC_RSA_PRIV_F3_LEN);
724 	if (IS_ERR(edesc))
725 		return PTR_ERR(edesc);
726 
727 	/* Set RSA Decrypt Protocol Data Block - Private Key Form #3 */
728 	ret = set_rsa_priv_f3_pdb(req, edesc);
729 	if (ret)
730 		goto init_fail;
731 
732 	/* Initialize Job Descriptor */
733 	init_rsa_priv_f3_desc(edesc->hw_desc, &edesc->pdb.priv_f3);
734 
735 	ret = caam_jr_enqueue(jrdev, edesc->hw_desc, rsa_priv_f_done, req);
736 	if (!ret)
737 		return -EINPROGRESS;
738 
739 	rsa_priv_f3_unmap(jrdev, edesc, req);
740 
741 init_fail:
742 	rsa_io_unmap(jrdev, edesc, req);
743 	kfree(edesc);
744 	return ret;
745 }
746 
747 static int caam_rsa_dec(struct akcipher_request *req)
748 {
749 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
750 	struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
751 	struct caam_rsa_key *key = &ctx->key;
752 	int ret;
753 
754 	if (unlikely(!key->n || !key->d))
755 		return -EINVAL;
756 
757 	if (req->dst_len < key->n_sz) {
758 		req->dst_len = key->n_sz;
759 		dev_err(ctx->dev, "Output buffer length less than parameter n\n");
760 		return -EOVERFLOW;
761 	}
762 
763 	if (key->priv_form == FORM3)
764 		ret = caam_rsa_dec_priv_f3(req);
765 	else if (key->priv_form == FORM2)
766 		ret = caam_rsa_dec_priv_f2(req);
767 	else
768 		ret = caam_rsa_dec_priv_f1(req);
769 
770 	return ret;
771 }
772 
773 static void caam_rsa_free_key(struct caam_rsa_key *key)
774 {
775 	kzfree(key->d);
776 	kzfree(key->p);
777 	kzfree(key->q);
778 	kzfree(key->dp);
779 	kzfree(key->dq);
780 	kzfree(key->qinv);
781 	kzfree(key->tmp1);
782 	kzfree(key->tmp2);
783 	kfree(key->e);
784 	kfree(key->n);
785 	memset(key, 0, sizeof(*key));
786 }
787 
788 static void caam_rsa_drop_leading_zeros(const u8 **ptr, size_t *nbytes)
789 {
790 	while (!**ptr && *nbytes) {
791 		(*ptr)++;
792 		(*nbytes)--;
793 	}
794 }
795 
796 /**
797  * caam_read_rsa_crt - Used for reading dP, dQ, qInv CRT members.
798  * dP, dQ and qInv could decode to less than corresponding p, q length, as the
799  * BER-encoding requires that the minimum number of bytes be used to encode the
800  * integer. dP, dQ, qInv decoded values have to be zero-padded to appropriate
801  * length.
802  *
803  * @ptr   : pointer to {dP, dQ, qInv} CRT member
804  * @nbytes: length in bytes of {dP, dQ, qInv} CRT member
805  * @dstlen: length in bytes of corresponding p or q prime factor
806  */
807 static u8 *caam_read_rsa_crt(const u8 *ptr, size_t nbytes, size_t dstlen)
808 {
809 	u8 *dst;
810 
811 	caam_rsa_drop_leading_zeros(&ptr, &nbytes);
812 	if (!nbytes)
813 		return NULL;
814 
815 	dst = kzalloc(dstlen, GFP_DMA | GFP_KERNEL);
816 	if (!dst)
817 		return NULL;
818 
819 	memcpy(dst + (dstlen - nbytes), ptr, nbytes);
820 
821 	return dst;
822 }
823 
824 /**
825  * caam_read_raw_data - Read a raw byte stream as a positive integer.
826  * The function skips buffer's leading zeros, copies the remained data
827  * to a buffer allocated in the GFP_DMA | GFP_KERNEL zone and returns
828  * the address of the new buffer.
829  *
830  * @buf   : The data to read
831  * @nbytes: The amount of data to read
832  */
833 static inline u8 *caam_read_raw_data(const u8 *buf, size_t *nbytes)
834 {
835 
836 	caam_rsa_drop_leading_zeros(&buf, nbytes);
837 	if (!*nbytes)
838 		return NULL;
839 
840 	return kmemdup(buf, *nbytes, GFP_DMA | GFP_KERNEL);
841 }
842 
843 static int caam_rsa_check_key_length(unsigned int len)
844 {
845 	if (len > 4096)
846 		return -EINVAL;
847 	return 0;
848 }
849 
850 static int caam_rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
851 				unsigned int keylen)
852 {
853 	struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
854 	struct rsa_key raw_key = {NULL};
855 	struct caam_rsa_key *rsa_key = &ctx->key;
856 	int ret;
857 
858 	/* Free the old RSA key if any */
859 	caam_rsa_free_key(rsa_key);
860 
861 	ret = rsa_parse_pub_key(&raw_key, key, keylen);
862 	if (ret)
863 		return ret;
864 
865 	/* Copy key in DMA zone */
866 	rsa_key->e = kmemdup(raw_key.e, raw_key.e_sz, GFP_DMA | GFP_KERNEL);
867 	if (!rsa_key->e)
868 		goto err;
869 
870 	/*
871 	 * Skip leading zeros and copy the positive integer to a buffer
872 	 * allocated in the GFP_DMA | GFP_KERNEL zone. The decryption descriptor
873 	 * expects a positive integer for the RSA modulus and uses its length as
874 	 * decryption output length.
875 	 */
876 	rsa_key->n = caam_read_raw_data(raw_key.n, &raw_key.n_sz);
877 	if (!rsa_key->n)
878 		goto err;
879 
880 	if (caam_rsa_check_key_length(raw_key.n_sz << 3)) {
881 		caam_rsa_free_key(rsa_key);
882 		return -EINVAL;
883 	}
884 
885 	rsa_key->e_sz = raw_key.e_sz;
886 	rsa_key->n_sz = raw_key.n_sz;
887 
888 	return 0;
889 err:
890 	caam_rsa_free_key(rsa_key);
891 	return -ENOMEM;
892 }
893 
894 static void caam_rsa_set_priv_key_form(struct caam_rsa_ctx *ctx,
895 				       struct rsa_key *raw_key)
896 {
897 	struct caam_rsa_key *rsa_key = &ctx->key;
898 	size_t p_sz = raw_key->p_sz;
899 	size_t q_sz = raw_key->q_sz;
900 
901 	rsa_key->p = caam_read_raw_data(raw_key->p, &p_sz);
902 	if (!rsa_key->p)
903 		return;
904 	rsa_key->p_sz = p_sz;
905 
906 	rsa_key->q = caam_read_raw_data(raw_key->q, &q_sz);
907 	if (!rsa_key->q)
908 		goto free_p;
909 	rsa_key->q_sz = q_sz;
910 
911 	rsa_key->tmp1 = kzalloc(raw_key->p_sz, GFP_DMA | GFP_KERNEL);
912 	if (!rsa_key->tmp1)
913 		goto free_q;
914 
915 	rsa_key->tmp2 = kzalloc(raw_key->q_sz, GFP_DMA | GFP_KERNEL);
916 	if (!rsa_key->tmp2)
917 		goto free_tmp1;
918 
919 	rsa_key->priv_form = FORM2;
920 
921 	rsa_key->dp = caam_read_rsa_crt(raw_key->dp, raw_key->dp_sz, p_sz);
922 	if (!rsa_key->dp)
923 		goto free_tmp2;
924 
925 	rsa_key->dq = caam_read_rsa_crt(raw_key->dq, raw_key->dq_sz, q_sz);
926 	if (!rsa_key->dq)
927 		goto free_dp;
928 
929 	rsa_key->qinv = caam_read_rsa_crt(raw_key->qinv, raw_key->qinv_sz,
930 					  q_sz);
931 	if (!rsa_key->qinv)
932 		goto free_dq;
933 
934 	rsa_key->priv_form = FORM3;
935 
936 	return;
937 
938 free_dq:
939 	kzfree(rsa_key->dq);
940 free_dp:
941 	kzfree(rsa_key->dp);
942 free_tmp2:
943 	kzfree(rsa_key->tmp2);
944 free_tmp1:
945 	kzfree(rsa_key->tmp1);
946 free_q:
947 	kzfree(rsa_key->q);
948 free_p:
949 	kzfree(rsa_key->p);
950 }
951 
952 static int caam_rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
953 				 unsigned int keylen)
954 {
955 	struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
956 	struct rsa_key raw_key = {NULL};
957 	struct caam_rsa_key *rsa_key = &ctx->key;
958 	int ret;
959 
960 	/* Free the old RSA key if any */
961 	caam_rsa_free_key(rsa_key);
962 
963 	ret = rsa_parse_priv_key(&raw_key, key, keylen);
964 	if (ret)
965 		return ret;
966 
967 	/* Copy key in DMA zone */
968 	rsa_key->d = kmemdup(raw_key.d, raw_key.d_sz, GFP_DMA | GFP_KERNEL);
969 	if (!rsa_key->d)
970 		goto err;
971 
972 	rsa_key->e = kmemdup(raw_key.e, raw_key.e_sz, GFP_DMA | GFP_KERNEL);
973 	if (!rsa_key->e)
974 		goto err;
975 
976 	/*
977 	 * Skip leading zeros and copy the positive integer to a buffer
978 	 * allocated in the GFP_DMA | GFP_KERNEL zone. The decryption descriptor
979 	 * expects a positive integer for the RSA modulus and uses its length as
980 	 * decryption output length.
981 	 */
982 	rsa_key->n = caam_read_raw_data(raw_key.n, &raw_key.n_sz);
983 	if (!rsa_key->n)
984 		goto err;
985 
986 	if (caam_rsa_check_key_length(raw_key.n_sz << 3)) {
987 		caam_rsa_free_key(rsa_key);
988 		return -EINVAL;
989 	}
990 
991 	rsa_key->d_sz = raw_key.d_sz;
992 	rsa_key->e_sz = raw_key.e_sz;
993 	rsa_key->n_sz = raw_key.n_sz;
994 
995 	caam_rsa_set_priv_key_form(ctx, &raw_key);
996 
997 	return 0;
998 
999 err:
1000 	caam_rsa_free_key(rsa_key);
1001 	return -ENOMEM;
1002 }
1003 
1004 static unsigned int caam_rsa_max_size(struct crypto_akcipher *tfm)
1005 {
1006 	struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
1007 
1008 	return ctx->key.n_sz;
1009 }
1010 
1011 /* Per session pkc's driver context creation function */
1012 static int caam_rsa_init_tfm(struct crypto_akcipher *tfm)
1013 {
1014 	struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
1015 
1016 	ctx->dev = caam_jr_alloc();
1017 
1018 	if (IS_ERR(ctx->dev)) {
1019 		pr_err("Job Ring Device allocation for transform failed\n");
1020 		return PTR_ERR(ctx->dev);
1021 	}
1022 
1023 	ctx->padding_dma = dma_map_single(ctx->dev, zero_buffer,
1024 					  CAAM_RSA_MAX_INPUT_SIZE - 1,
1025 					  DMA_TO_DEVICE);
1026 	if (dma_mapping_error(ctx->dev, ctx->padding_dma)) {
1027 		dev_err(ctx->dev, "unable to map padding\n");
1028 		caam_jr_free(ctx->dev);
1029 		return -ENOMEM;
1030 	}
1031 
1032 	return 0;
1033 }
1034 
1035 /* Per session pkc's driver context cleanup function */
1036 static void caam_rsa_exit_tfm(struct crypto_akcipher *tfm)
1037 {
1038 	struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
1039 	struct caam_rsa_key *key = &ctx->key;
1040 
1041 	dma_unmap_single(ctx->dev, ctx->padding_dma, CAAM_RSA_MAX_INPUT_SIZE -
1042 			 1, DMA_TO_DEVICE);
1043 	caam_rsa_free_key(key);
1044 	caam_jr_free(ctx->dev);
1045 }
1046 
1047 static struct caam_akcipher_alg caam_rsa = {
1048 	.akcipher = {
1049 		.encrypt = caam_rsa_enc,
1050 		.decrypt = caam_rsa_dec,
1051 		.set_pub_key = caam_rsa_set_pub_key,
1052 		.set_priv_key = caam_rsa_set_priv_key,
1053 		.max_size = caam_rsa_max_size,
1054 		.init = caam_rsa_init_tfm,
1055 		.exit = caam_rsa_exit_tfm,
1056 		.reqsize = sizeof(struct caam_rsa_req_ctx),
1057 		.base = {
1058 			.cra_name = "rsa",
1059 			.cra_driver_name = "rsa-caam",
1060 			.cra_priority = 3000,
1061 			.cra_module = THIS_MODULE,
1062 			.cra_ctxsize = sizeof(struct caam_rsa_ctx),
1063 		},
1064 	}
1065 };
1066 
1067 /* Public Key Cryptography module initialization handler */
1068 int caam_pkc_init(struct device *ctrldev)
1069 {
1070 	struct caam_drv_private *priv = dev_get_drvdata(ctrldev);
1071 	u32 pk_inst;
1072 	int err;
1073 	init_done = false;
1074 
1075 	/* Determine public key hardware accelerator presence. */
1076 	if (priv->era < 10)
1077 		pk_inst = (rd_reg32(&priv->ctrl->perfmon.cha_num_ls) &
1078 			   CHA_ID_LS_PK_MASK) >> CHA_ID_LS_PK_SHIFT;
1079 	else
1080 		pk_inst = rd_reg32(&priv->ctrl->vreg.pkha) & CHA_VER_NUM_MASK;
1081 
1082 	/* Do not register algorithms if PKHA is not present. */
1083 	if (!pk_inst)
1084 		return 0;
1085 
1086 	/* allocate zero buffer, used for padding input */
1087 	zero_buffer = kzalloc(CAAM_RSA_MAX_INPUT_SIZE - 1, GFP_DMA |
1088 			      GFP_KERNEL);
1089 	if (!zero_buffer)
1090 		return -ENOMEM;
1091 
1092 	err = crypto_register_akcipher(&caam_rsa.akcipher);
1093 
1094 	if (err) {
1095 		kfree(zero_buffer);
1096 		dev_warn(ctrldev, "%s alg registration failed\n",
1097 			 caam_rsa.akcipher.base.cra_driver_name);
1098 	} else {
1099 		init_done = true;
1100 		caam_rsa.registered = true;
1101 		dev_info(ctrldev, "caam pkc algorithms registered in /proc/crypto\n");
1102 	}
1103 
1104 	return err;
1105 }
1106 
1107 void caam_pkc_exit(void)
1108 {
1109 	if (!init_done)
1110 		return;
1111 
1112 	if (caam_rsa.registered)
1113 		crypto_unregister_akcipher(&caam_rsa.akcipher);
1114 
1115 	kfree(zero_buffer);
1116 }
1117