1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * sun4i-ss-cipher.c - hardware cryptographic accelerator for Allwinner A20 SoC
5 * Copyright (C) 2013-2015 Corentin LABBE <clabbe.montjoie@gmail.com>
7 * This file add support for AES cipher with 128,192,256 bits
8 * keysize in CBC and ECB mode.
9 * Add support also for DES and 3DES in CBC and ECB mode.
11 * You could find the datasheet in Documentation/arm/sunxi.rst
15 static int noinline_for_stack
sun4i_ss_opti_poll(struct skcipher_request
*areq
)
17 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(areq
);
18 struct sun4i_tfm_ctx
*op
= crypto_skcipher_ctx(tfm
);
19 struct sun4i_ss_ctx
*ss
= op
->ss
;
20 unsigned int ivsize
= crypto_skcipher_ivsize(tfm
);
21 struct sun4i_cipher_req_ctx
*ctx
= skcipher_request_ctx(areq
);
23 /* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */
24 u32 rx_cnt
= SS_RX_DEFAULT
;
30 unsigned int ileft
= areq
->cryptlen
;
31 unsigned int oleft
= areq
->cryptlen
;
33 struct sg_mapping_iter mi
, mo
;
34 unsigned int oi
, oo
; /* offset for in and out */
40 if (!areq
->src
|| !areq
->dst
) {
41 dev_err_ratelimited(ss
->dev
, "ERROR: Some SGs are NULL\n");
45 spin_lock_irqsave(&ss
->slock
, flags
);
47 for (i
= 0; i
< op
->keylen
; i
+= 4)
48 writel(*(op
->key
+ i
/ 4), ss
->base
+ SS_KEY0
+ i
);
51 for (i
= 0; i
< 4 && i
< ivsize
/ 4; i
++) {
52 v
= *(u32
*)(areq
->iv
+ i
* 4);
53 writel(v
, ss
->base
+ SS_IV0
+ i
* 4);
56 writel(mode
, ss
->base
+ SS_CTL
);
58 sg_miter_start(&mi
, areq
->src
, sg_nents(areq
->src
),
59 SG_MITER_FROM_SG
| SG_MITER_ATOMIC
);
60 sg_miter_start(&mo
, areq
->dst
, sg_nents(areq
->dst
),
61 SG_MITER_TO_SG
| SG_MITER_ATOMIC
);
64 if (!mi
.addr
|| !mo
.addr
) {
65 dev_err_ratelimited(ss
->dev
, "ERROR: sg_miter return null\n");
70 ileft
= areq
->cryptlen
/ 4;
71 oleft
= areq
->cryptlen
/ 4;
75 todo
= min(rx_cnt
, ileft
);
76 todo
= min_t(size_t, todo
, (mi
.length
- oi
) / 4);
79 writesl(ss
->base
+ SS_RXFIFO
, mi
.addr
+ oi
, todo
);
82 if (oi
== mi
.length
) {
87 spaces
= readl(ss
->base
+ SS_FCSR
);
88 rx_cnt
= SS_RXFIFO_SPACES(spaces
);
89 tx_cnt
= SS_TXFIFO_SPACES(spaces
);
91 todo
= min(tx_cnt
, oleft
);
92 todo
= min_t(size_t, todo
, (mo
.length
- oo
) / 4);
95 readsl(ss
->base
+ SS_TXFIFO
, mo
.addr
+ oo
, todo
);
98 if (oo
== mo
.length
) {
105 for (i
= 0; i
< 4 && i
< ivsize
/ 4; i
++) {
106 v
= readl(ss
->base
+ SS_IV0
+ i
* 4);
107 *(u32
*)(areq
->iv
+ i
* 4) = v
;
114 writel(0, ss
->base
+ SS_CTL
);
115 spin_unlock_irqrestore(&ss
->slock
, flags
);
120 static int noinline_for_stack
sun4i_ss_cipher_poll_fallback(struct skcipher_request
*areq
)
122 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(areq
);
123 struct sun4i_tfm_ctx
*op
= crypto_skcipher_ctx(tfm
);
124 struct sun4i_cipher_req_ctx
*ctx
= skcipher_request_ctx(areq
);
127 skcipher_request_set_tfm(&ctx
->fallback_req
, op
->fallback_tfm
);
128 skcipher_request_set_callback(&ctx
->fallback_req
, areq
->base
.flags
,
129 areq
->base
.complete
, areq
->base
.data
);
130 skcipher_request_set_crypt(&ctx
->fallback_req
, areq
->src
, areq
->dst
,
131 areq
->cryptlen
, areq
->iv
);
132 if (ctx
->mode
& SS_DECRYPTION
)
133 err
= crypto_skcipher_decrypt(&ctx
->fallback_req
);
135 err
= crypto_skcipher_encrypt(&ctx
->fallback_req
);
140 /* Generic function that support SG with size not multiple of 4 */
141 static int sun4i_ss_cipher_poll(struct skcipher_request
*areq
)
143 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(areq
);
144 struct sun4i_tfm_ctx
*op
= crypto_skcipher_ctx(tfm
);
145 struct sun4i_ss_ctx
*ss
= op
->ss
;
147 struct scatterlist
*in_sg
= areq
->src
;
148 struct scatterlist
*out_sg
= areq
->dst
;
149 unsigned int ivsize
= crypto_skcipher_ivsize(tfm
);
150 struct sun4i_cipher_req_ctx
*ctx
= skcipher_request_ctx(areq
);
151 struct skcipher_alg
*alg
= crypto_skcipher_alg(tfm
);
152 struct sun4i_ss_alg_template
*algt
;
153 u32 mode
= ctx
->mode
;
154 /* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */
155 u32 rx_cnt
= SS_RX_DEFAULT
;
161 unsigned int ileft
= areq
->cryptlen
;
162 unsigned int oleft
= areq
->cryptlen
;
164 struct sg_mapping_iter mi
, mo
;
165 unsigned int oi
, oo
; /* offset for in and out */
166 unsigned int ob
= 0; /* offset in buf */
167 unsigned int obo
= 0; /* offset in bufo*/
168 unsigned int obl
= 0; /* length of data in bufo */
175 if (!areq
->src
|| !areq
->dst
) {
176 dev_err_ratelimited(ss
->dev
, "ERROR: Some SGs are NULL\n");
180 algt
= container_of(alg
, struct sun4i_ss_alg_template
, alg
.crypto
);
181 if (areq
->cryptlen
% algt
->alg
.crypto
.base
.cra_blocksize
)
182 need_fallback
= true;
185 * if we have only SGs with size multiple of 4,
186 * we can use the SS optimized function
188 while (in_sg
&& no_chunk
== 1) {
189 if (in_sg
->length
% 4)
191 in_sg
= sg_next(in_sg
);
193 while (out_sg
&& no_chunk
== 1) {
194 if (out_sg
->length
% 4)
196 out_sg
= sg_next(out_sg
);
199 if (no_chunk
== 1 && !need_fallback
)
200 return sun4i_ss_opti_poll(areq
);
203 return sun4i_ss_cipher_poll_fallback(areq
);
205 spin_lock_irqsave(&ss
->slock
, flags
);
207 for (i
= 0; i
< op
->keylen
; i
+= 4)
208 writel(*(op
->key
+ i
/ 4), ss
->base
+ SS_KEY0
+ i
);
211 for (i
= 0; i
< 4 && i
< ivsize
/ 4; i
++) {
212 v
= *(u32
*)(areq
->iv
+ i
* 4);
213 writel(v
, ss
->base
+ SS_IV0
+ i
* 4);
216 writel(mode
, ss
->base
+ SS_CTL
);
218 sg_miter_start(&mi
, areq
->src
, sg_nents(areq
->src
),
219 SG_MITER_FROM_SG
| SG_MITER_ATOMIC
);
220 sg_miter_start(&mo
, areq
->dst
, sg_nents(areq
->dst
),
221 SG_MITER_TO_SG
| SG_MITER_ATOMIC
);
224 if (!mi
.addr
|| !mo
.addr
) {
225 dev_err_ratelimited(ss
->dev
, "ERROR: sg_miter return null\n");
229 ileft
= areq
->cryptlen
;
230 oleft
= areq
->cryptlen
;
236 char buf
[4 * SS_RX_MAX
];/* buffer for linearize SG src */
239 * todo is the number of consecutive 4byte word that we
240 * can read from current SG
242 todo
= min(rx_cnt
, ileft
/ 4);
243 todo
= min_t(size_t, todo
, (mi
.length
- oi
) / 4);
245 writesl(ss
->base
+ SS_RXFIFO
, mi
.addr
+ oi
,
251 * not enough consecutive bytes, so we need to
252 * linearize in buf. todo is in bytes
253 * After that copy, if we have a multiple of 4
254 * we need to be able to write all buf in one
255 * pass, so it is why we min() with rx_cnt
257 todo
= min(rx_cnt
* 4 - ob
, ileft
);
258 todo
= min_t(size_t, todo
, mi
.length
- oi
);
259 memcpy(buf
+ ob
, mi
.addr
+ oi
, todo
);
264 writesl(ss
->base
+ SS_RXFIFO
, buf
,
269 if (oi
== mi
.length
) {
275 spaces
= readl(ss
->base
+ SS_FCSR
);
276 rx_cnt
= SS_RXFIFO_SPACES(spaces
);
277 tx_cnt
= SS_TXFIFO_SPACES(spaces
);
279 "%x %u/%zu %u/%u cnt=%u %u/%zu %u/%u cnt=%u %u\n",
281 oi
, mi
.length
, ileft
, areq
->cryptlen
, rx_cnt
,
282 oo
, mo
.length
, oleft
, areq
->cryptlen
, tx_cnt
, ob
);
286 /* todo in 4bytes word */
287 todo
= min(tx_cnt
, oleft
/ 4);
288 todo
= min_t(size_t, todo
, (mo
.length
- oo
) / 4);
290 readsl(ss
->base
+ SS_TXFIFO
, mo
.addr
+ oo
, todo
);
293 if (oo
== mo
.length
) {
298 char bufo
[4 * SS_TX_MAX
]; /* buffer for linearize SG dst */
301 * read obl bytes in bufo, we read at maximum for
302 * emptying the device
304 readsl(ss
->base
+ SS_TXFIFO
, bufo
, tx_cnt
);
309 * how many bytes we can copy ?
310 * no more than remaining SG size
311 * no more than remaining buffer
312 * no need to test against oleft
315 mo
.length
- oo
, obl
- obo
);
316 memcpy(mo
.addr
+ oo
, bufo
+ obo
, todo
);
320 if (oo
== mo
.length
) {
325 /* bufo must be fully used here */
329 for (i
= 0; i
< 4 && i
< ivsize
/ 4; i
++) {
330 v
= readl(ss
->base
+ SS_IV0
+ i
* 4);
331 *(u32
*)(areq
->iv
+ i
* 4) = v
;
338 writel(0, ss
->base
+ SS_CTL
);
339 spin_unlock_irqrestore(&ss
->slock
, flags
);
345 int sun4i_ss_cbc_aes_encrypt(struct skcipher_request
*areq
)
347 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(areq
);
348 struct sun4i_tfm_ctx
*op
= crypto_skcipher_ctx(tfm
);
349 struct sun4i_cipher_req_ctx
*rctx
= skcipher_request_ctx(areq
);
351 rctx
->mode
= SS_OP_AES
| SS_CBC
| SS_ENABLED
| SS_ENCRYPTION
|
353 return sun4i_ss_cipher_poll(areq
);
356 int sun4i_ss_cbc_aes_decrypt(struct skcipher_request
*areq
)
358 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(areq
);
359 struct sun4i_tfm_ctx
*op
= crypto_skcipher_ctx(tfm
);
360 struct sun4i_cipher_req_ctx
*rctx
= skcipher_request_ctx(areq
);
362 rctx
->mode
= SS_OP_AES
| SS_CBC
| SS_ENABLED
| SS_DECRYPTION
|
364 return sun4i_ss_cipher_poll(areq
);
368 int sun4i_ss_ecb_aes_encrypt(struct skcipher_request
*areq
)
370 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(areq
);
371 struct sun4i_tfm_ctx
*op
= crypto_skcipher_ctx(tfm
);
372 struct sun4i_cipher_req_ctx
*rctx
= skcipher_request_ctx(areq
);
374 rctx
->mode
= SS_OP_AES
| SS_ECB
| SS_ENABLED
| SS_ENCRYPTION
|
376 return sun4i_ss_cipher_poll(areq
);
379 int sun4i_ss_ecb_aes_decrypt(struct skcipher_request
*areq
)
381 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(areq
);
382 struct sun4i_tfm_ctx
*op
= crypto_skcipher_ctx(tfm
);
383 struct sun4i_cipher_req_ctx
*rctx
= skcipher_request_ctx(areq
);
385 rctx
->mode
= SS_OP_AES
| SS_ECB
| SS_ENABLED
| SS_DECRYPTION
|
387 return sun4i_ss_cipher_poll(areq
);
391 int sun4i_ss_cbc_des_encrypt(struct skcipher_request
*areq
)
393 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(areq
);
394 struct sun4i_tfm_ctx
*op
= crypto_skcipher_ctx(tfm
);
395 struct sun4i_cipher_req_ctx
*rctx
= skcipher_request_ctx(areq
);
397 rctx
->mode
= SS_OP_DES
| SS_CBC
| SS_ENABLED
| SS_ENCRYPTION
|
399 return sun4i_ss_cipher_poll(areq
);
402 int sun4i_ss_cbc_des_decrypt(struct skcipher_request
*areq
)
404 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(areq
);
405 struct sun4i_tfm_ctx
*op
= crypto_skcipher_ctx(tfm
);
406 struct sun4i_cipher_req_ctx
*rctx
= skcipher_request_ctx(areq
);
408 rctx
->mode
= SS_OP_DES
| SS_CBC
| SS_ENABLED
| SS_DECRYPTION
|
410 return sun4i_ss_cipher_poll(areq
);
414 int sun4i_ss_ecb_des_encrypt(struct skcipher_request
*areq
)
416 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(areq
);
417 struct sun4i_tfm_ctx
*op
= crypto_skcipher_ctx(tfm
);
418 struct sun4i_cipher_req_ctx
*rctx
= skcipher_request_ctx(areq
);
420 rctx
->mode
= SS_OP_DES
| SS_ECB
| SS_ENABLED
| SS_ENCRYPTION
|
422 return sun4i_ss_cipher_poll(areq
);
425 int sun4i_ss_ecb_des_decrypt(struct skcipher_request
*areq
)
427 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(areq
);
428 struct sun4i_tfm_ctx
*op
= crypto_skcipher_ctx(tfm
);
429 struct sun4i_cipher_req_ctx
*rctx
= skcipher_request_ctx(areq
);
431 rctx
->mode
= SS_OP_DES
| SS_ECB
| SS_ENABLED
| SS_DECRYPTION
|
433 return sun4i_ss_cipher_poll(areq
);
437 int sun4i_ss_cbc_des3_encrypt(struct skcipher_request
*areq
)
439 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(areq
);
440 struct sun4i_tfm_ctx
*op
= crypto_skcipher_ctx(tfm
);
441 struct sun4i_cipher_req_ctx
*rctx
= skcipher_request_ctx(areq
);
443 rctx
->mode
= SS_OP_3DES
| SS_CBC
| SS_ENABLED
| SS_ENCRYPTION
|
445 return sun4i_ss_cipher_poll(areq
);
448 int sun4i_ss_cbc_des3_decrypt(struct skcipher_request
*areq
)
450 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(areq
);
451 struct sun4i_tfm_ctx
*op
= crypto_skcipher_ctx(tfm
);
452 struct sun4i_cipher_req_ctx
*rctx
= skcipher_request_ctx(areq
);
454 rctx
->mode
= SS_OP_3DES
| SS_CBC
| SS_ENABLED
| SS_DECRYPTION
|
456 return sun4i_ss_cipher_poll(areq
);
460 int sun4i_ss_ecb_des3_encrypt(struct skcipher_request
*areq
)
462 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(areq
);
463 struct sun4i_tfm_ctx
*op
= crypto_skcipher_ctx(tfm
);
464 struct sun4i_cipher_req_ctx
*rctx
= skcipher_request_ctx(areq
);
466 rctx
->mode
= SS_OP_3DES
| SS_ECB
| SS_ENABLED
| SS_ENCRYPTION
|
468 return sun4i_ss_cipher_poll(areq
);
471 int sun4i_ss_ecb_des3_decrypt(struct skcipher_request
*areq
)
473 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(areq
);
474 struct sun4i_tfm_ctx
*op
= crypto_skcipher_ctx(tfm
);
475 struct sun4i_cipher_req_ctx
*rctx
= skcipher_request_ctx(areq
);
477 rctx
->mode
= SS_OP_3DES
| SS_ECB
| SS_ENABLED
| SS_DECRYPTION
|
479 return sun4i_ss_cipher_poll(areq
);
482 int sun4i_ss_cipher_init(struct crypto_tfm
*tfm
)
484 struct sun4i_tfm_ctx
*op
= crypto_tfm_ctx(tfm
);
485 struct sun4i_ss_alg_template
*algt
;
486 const char *name
= crypto_tfm_alg_name(tfm
);
489 memset(op
, 0, sizeof(struct sun4i_tfm_ctx
));
491 algt
= container_of(tfm
->__crt_alg
, struct sun4i_ss_alg_template
,
495 op
->fallback_tfm
= crypto_alloc_skcipher(name
, 0, CRYPTO_ALG_NEED_FALLBACK
);
496 if (IS_ERR(op
->fallback_tfm
)) {
497 dev_err(op
->ss
->dev
, "ERROR: Cannot allocate fallback for %s %ld\n",
498 name
, PTR_ERR(op
->fallback_tfm
));
499 return PTR_ERR(op
->fallback_tfm
);
502 crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm
),
503 sizeof(struct sun4i_cipher_req_ctx
) +
504 crypto_skcipher_reqsize(op
->fallback_tfm
));
507 err
= pm_runtime_get_sync(op
->ss
->dev
);
513 crypto_free_skcipher(op
->fallback_tfm
);
517 void sun4i_ss_cipher_exit(struct crypto_tfm
*tfm
)
519 struct sun4i_tfm_ctx
*op
= crypto_tfm_ctx(tfm
);
521 crypto_free_skcipher(op
->fallback_tfm
);
522 pm_runtime_put(op
->ss
->dev
);
525 /* check and set the AES key, prepare the mode to be used */
526 int sun4i_ss_aes_setkey(struct crypto_skcipher
*tfm
, const u8
*key
,
529 struct sun4i_tfm_ctx
*op
= crypto_skcipher_ctx(tfm
);
530 struct sun4i_ss_ctx
*ss
= op
->ss
;
534 op
->keymode
= SS_AES_128BITS
;
537 op
->keymode
= SS_AES_192BITS
;
540 op
->keymode
= SS_AES_256BITS
;
543 dev_dbg(ss
->dev
, "ERROR: Invalid keylen %u\n", keylen
);
547 memcpy(op
->key
, key
, keylen
);
549 crypto_skcipher_clear_flags(op
->fallback_tfm
, CRYPTO_TFM_REQ_MASK
);
550 crypto_skcipher_set_flags(op
->fallback_tfm
, tfm
->base
.crt_flags
& CRYPTO_TFM_REQ_MASK
);
552 return crypto_skcipher_setkey(op
->fallback_tfm
, key
, keylen
);
555 /* check and set the DES key, prepare the mode to be used */
556 int sun4i_ss_des_setkey(struct crypto_skcipher
*tfm
, const u8
*key
,
559 struct sun4i_tfm_ctx
*op
= crypto_skcipher_ctx(tfm
);
562 err
= verify_skcipher_des_key(tfm
, key
);
567 memcpy(op
->key
, key
, keylen
);
569 crypto_skcipher_clear_flags(op
->fallback_tfm
, CRYPTO_TFM_REQ_MASK
);
570 crypto_skcipher_set_flags(op
->fallback_tfm
, tfm
->base
.crt_flags
& CRYPTO_TFM_REQ_MASK
);
572 return crypto_skcipher_setkey(op
->fallback_tfm
, key
, keylen
);
575 /* check and set the 3DES key, prepare the mode to be used */
576 int sun4i_ss_des3_setkey(struct crypto_skcipher
*tfm
, const u8
*key
,
579 struct sun4i_tfm_ctx
*op
= crypto_skcipher_ctx(tfm
);
582 err
= verify_skcipher_des3_key(tfm
, key
);
587 memcpy(op
->key
, key
, keylen
);
589 crypto_skcipher_clear_flags(op
->fallback_tfm
, CRYPTO_TFM_REQ_MASK
);
590 crypto_skcipher_set_flags(op
->fallback_tfm
, tfm
->base
.crt_flags
& CRYPTO_TFM_REQ_MASK
);
592 return crypto_skcipher_setkey(op
->fallback_tfm
, key
, keylen
);