1 // SPDX-License-Identifier: GPL-2.0
3 * sun8i-ss-cipher.c - hardware cryptographic offloader for
4 * Allwinner A80/A83T SoC
6 * Copyright (C) 2016-2019 Corentin LABBE <clabbe.montjoie@gmail.com>
8 * This file add support for AES cipher with 128,192,256 bits keysize in
11 * You could find a link for the datasheet in Documentation/arm/sunxi.rst
14 #include <linux/bottom_half.h>
15 #include <linux/crypto.h>
16 #include <linux/dma-mapping.h>
18 #include <linux/pm_runtime.h>
19 #include <crypto/scatterwalk.h>
20 #include <crypto/internal/skcipher.h>
23 static bool sun8i_ss_need_fallback(struct skcipher_request *areq)
25 struct scatterlist *in_sg = areq->src;
26 struct scatterlist *out_sg = areq->dst;
27 struct scatterlist *sg;
29 if (areq->cryptlen == 0 || areq->cryptlen % 16)
32 if (sg_nents(areq->src) > 8 || sg_nents(areq->dst) > 8)
37 if ((sg->length % 16) != 0)
39 if ((sg_dma_len(sg) % 16) != 0)
41 if (!IS_ALIGNED(sg->offset, 16))
47 if ((sg->length % 16) != 0)
49 if ((sg_dma_len(sg) % 16) != 0)
51 if (!IS_ALIGNED(sg->offset, 16))
56 /* SS need same numbers of SG (with same length) for source and destination */
59 while (in_sg && out_sg) {
60 if (in_sg->length != out_sg->length)
62 in_sg = sg_next(in_sg);
63 out_sg = sg_next(out_sg);
70 static int sun8i_ss_cipher_fallback(struct skcipher_request *areq)
72 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
73 struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
74 struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
77 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
78 struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
79 struct sun8i_ss_alg_template *algt;
81 algt = container_of(alg, struct sun8i_ss_alg_template, alg.skcipher);
84 skcipher_request_set_tfm(&rctx->fallback_req, op->fallback_tfm);
85 skcipher_request_set_callback(&rctx->fallback_req, areq->base.flags,
86 areq->base.complete, areq->base.data);
87 skcipher_request_set_crypt(&rctx->fallback_req, areq->src, areq->dst,
88 areq->cryptlen, areq->iv);
89 if (rctx->op_dir & SS_DECRYPTION)
90 err = crypto_skcipher_decrypt(&rctx->fallback_req);
92 err = crypto_skcipher_encrypt(&rctx->fallback_req);
96 static int sun8i_ss_cipher(struct skcipher_request *areq)
98 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
99 struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
100 struct sun8i_ss_dev *ss = op->ss;
101 struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
102 struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
103 struct sun8i_ss_alg_template *algt;
104 struct scatterlist *sg;
105 unsigned int todo, len, offset, ivsize;
106 void *backup_iv = NULL;
112 algt = container_of(alg, struct sun8i_ss_alg_template, alg.skcipher);
114 dev_dbg(ss->dev, "%s %s %u %x IV(%p %u) key=%u\n", __func__,
115 crypto_tfm_alg_name(areq->base.tfm),
117 rctx->op_dir, areq->iv, crypto_skcipher_ivsize(tfm),
120 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
124 rctx->op_mode = ss->variant->op_mode[algt->ss_blockmode];
125 rctx->method = ss->variant->alg_cipher[algt->ss_algo_id];
126 rctx->keylen = op->keylen;
128 rctx->p_key = dma_map_single(ss->dev, op->key, op->keylen, DMA_TO_DEVICE);
129 if (dma_mapping_error(ss->dev, rctx->p_key)) {
130 dev_err(ss->dev, "Cannot DMA MAP KEY\n");
135 ivsize = crypto_skcipher_ivsize(tfm);
136 if (areq->iv && crypto_skcipher_ivsize(tfm) > 0) {
137 rctx->ivlen = ivsize;
138 rctx->biv = kzalloc(ivsize, GFP_KERNEL | GFP_DMA);
143 if (rctx->op_dir & SS_DECRYPTION) {
144 backup_iv = kzalloc(ivsize, GFP_KERNEL);
149 offset = areq->cryptlen - ivsize;
150 scatterwalk_map_and_copy(backup_iv, areq->src, offset,
153 memcpy(rctx->biv, areq->iv, ivsize);
154 rctx->p_iv = dma_map_single(ss->dev, rctx->biv, rctx->ivlen,
156 if (dma_mapping_error(ss->dev, rctx->p_iv)) {
157 dev_err(ss->dev, "Cannot DMA MAP IV\n");
162 if (areq->src == areq->dst) {
163 nr_sgs = dma_map_sg(ss->dev, areq->src, sg_nents(areq->src),
165 if (nr_sgs <= 0 || nr_sgs > 8) {
166 dev_err(ss->dev, "Invalid sg number %d\n", nr_sgs);
172 nr_sgs = dma_map_sg(ss->dev, areq->src, sg_nents(areq->src),
174 if (nr_sgs <= 0 || nr_sgs > 8) {
175 dev_err(ss->dev, "Invalid sg number %d\n", nr_sgs);
179 nr_sgd = dma_map_sg(ss->dev, areq->dst, sg_nents(areq->dst),
181 if (nr_sgd <= 0 || nr_sgd > 8) {
182 dev_err(ss->dev, "Invalid sg number %d\n", nr_sgd);
188 len = areq->cryptlen;
191 while (i < nr_sgs && sg && len) {
192 if (sg_dma_len(sg) == 0)
194 rctx->t_src[i].addr = sg_dma_address(sg);
195 todo = min(len, sg_dma_len(sg));
196 rctx->t_src[i].len = todo / 4;
197 dev_dbg(ss->dev, "%s total=%u SGS(%d %u off=%d) todo=%u\n", __func__,
198 areq->cryptlen, i, rctx->t_src[i].len, sg->offset, todo);
205 dev_err(ss->dev, "remaining len %d\n", len);
210 len = areq->cryptlen;
213 while (i < nr_sgd && sg && len) {
214 if (sg_dma_len(sg) == 0)
216 rctx->t_dst[i].addr = sg_dma_address(sg);
217 todo = min(len, sg_dma_len(sg));
218 rctx->t_dst[i].len = todo / 4;
219 dev_dbg(ss->dev, "%s total=%u SGD(%d %u off=%d) todo=%u\n", __func__,
220 areq->cryptlen, i, rctx->t_dst[i].len, sg->offset, todo);
227 dev_err(ss->dev, "remaining len %d\n", len);
232 err = sun8i_ss_run_task(ss, rctx, crypto_tfm_alg_name(areq->base.tfm));
235 if (areq->src == areq->dst) {
236 dma_unmap_sg(ss->dev, areq->src, sg_nents(areq->src),
239 dma_unmap_sg(ss->dev, areq->src, sg_nents(areq->src),
241 dma_unmap_sg(ss->dev, areq->dst, sg_nents(areq->dst),
247 dma_unmap_single(ss->dev, rctx->p_iv, rctx->ivlen,
250 if (areq->iv && ivsize > 0) {
252 offset = areq->cryptlen - ivsize;
253 if (rctx->op_dir & SS_DECRYPTION) {
254 memcpy(areq->iv, backup_iv, ivsize);
255 kfree_sensitive(backup_iv);
257 scatterwalk_map_and_copy(areq->iv, areq->dst, offset,
265 dma_unmap_single(ss->dev, rctx->p_key, op->keylen, DMA_TO_DEVICE);
272 static int sun8i_ss_handle_cipher_request(struct crypto_engine *engine, void *areq)
275 struct skcipher_request *breq = container_of(areq, struct skcipher_request, base);
277 err = sun8i_ss_cipher(breq);
279 crypto_finalize_skcipher_request(engine, breq, err);
285 int sun8i_ss_skdecrypt(struct skcipher_request *areq)
287 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
288 struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
289 struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
290 struct crypto_engine *engine;
293 memset(rctx, 0, sizeof(struct sun8i_cipher_req_ctx));
294 rctx->op_dir = SS_DECRYPTION;
296 if (sun8i_ss_need_fallback(areq))
297 return sun8i_ss_cipher_fallback(areq);
299 e = sun8i_ss_get_engine_number(op->ss);
300 engine = op->ss->flows[e].engine;
303 return crypto_transfer_skcipher_request_to_engine(engine, areq);
306 int sun8i_ss_skencrypt(struct skcipher_request *areq)
308 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
309 struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
310 struct sun8i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
311 struct crypto_engine *engine;
314 memset(rctx, 0, sizeof(struct sun8i_cipher_req_ctx));
315 rctx->op_dir = SS_ENCRYPTION;
317 if (sun8i_ss_need_fallback(areq))
318 return sun8i_ss_cipher_fallback(areq);
320 e = sun8i_ss_get_engine_number(op->ss);
321 engine = op->ss->flows[e].engine;
324 return crypto_transfer_skcipher_request_to_engine(engine, areq);
327 int sun8i_ss_cipher_init(struct crypto_tfm *tfm)
329 struct sun8i_cipher_tfm_ctx *op = crypto_tfm_ctx(tfm);
330 struct sun8i_ss_alg_template *algt;
331 const char *name = crypto_tfm_alg_name(tfm);
332 struct crypto_skcipher *sktfm = __crypto_skcipher_cast(tfm);
333 struct skcipher_alg *alg = crypto_skcipher_alg(sktfm);
336 memset(op, 0, sizeof(struct sun8i_cipher_tfm_ctx));
338 algt = container_of(alg, struct sun8i_ss_alg_template, alg.skcipher);
341 op->fallback_tfm = crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
342 if (IS_ERR(op->fallback_tfm)) {
343 dev_err(op->ss->dev, "ERROR: Cannot allocate fallback for %s %ld\n",
344 name, PTR_ERR(op->fallback_tfm));
345 return PTR_ERR(op->fallback_tfm);
348 sktfm->reqsize = sizeof(struct sun8i_cipher_req_ctx) +
349 crypto_skcipher_reqsize(op->fallback_tfm);
352 dev_info(op->ss->dev, "Fallback for %s is %s\n",
353 crypto_tfm_alg_driver_name(&sktfm->base),
354 crypto_tfm_alg_driver_name(crypto_skcipher_tfm(op->fallback_tfm)));
356 op->enginectx.op.do_one_request = sun8i_ss_handle_cipher_request;
357 op->enginectx.op.prepare_request = NULL;
358 op->enginectx.op.unprepare_request = NULL;
360 err = pm_runtime_resume_and_get(op->ss->dev);
362 dev_err(op->ss->dev, "pm error %d\n", err);
368 crypto_free_skcipher(op->fallback_tfm);
372 void sun8i_ss_cipher_exit(struct crypto_tfm *tfm)
374 struct sun8i_cipher_tfm_ctx *op = crypto_tfm_ctx(tfm);
376 kfree_sensitive(op->key);
377 crypto_free_skcipher(op->fallback_tfm);
378 pm_runtime_put_sync(op->ss->dev);
381 int sun8i_ss_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
384 struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
385 struct sun8i_ss_dev *ss = op->ss;
395 dev_dbg(ss->dev, "ERROR: Invalid keylen %u\n", keylen);
398 kfree_sensitive(op->key);
400 op->key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA);
404 crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
405 crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
407 return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);
410 int sun8i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
413 struct sun8i_cipher_tfm_ctx *op = crypto_skcipher_ctx(tfm);
414 struct sun8i_ss_dev *ss = op->ss;
416 if (unlikely(keylen != 3 * DES_KEY_SIZE)) {
417 dev_dbg(ss->dev, "Invalid keylen %u\n", keylen);
421 kfree_sensitive(op->key);
423 op->key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA);
427 crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
428 crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
430 return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);