1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale FSL CAAM support for crypto API over QI backend.
6 * Copyright 2013-2016 Freescale Semiconductor, Inc.
7 * Copyright 2016-2019 NXP
14 #include "desc_constr.h"
20 #include "caamalg_desc.h"
21 #include <crypto/xts.h>
22 #include <linux/unaligned.h>
23 #include <linux/device.h>
24 #include <linux/err.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/kernel.h>
27 #include <linux/string.h>
32 #define CAAM_CRA_PRIORITY 2000
33 /* max key is sum of AES_MAX_KEY_SIZE, max split key size */
34 #define CAAM_MAX_KEY_SIZE (AES_MAX_KEY_SIZE + \
35 SHA512_DIGEST_SIZE * 2)
37 #define DESC_MAX_USED_BYTES (DESC_QI_AEAD_GIVENC_LEN + \
39 #define DESC_MAX_USED_LEN (DESC_MAX_USED_BYTES / CAAM_CMD_SZ)
41 struct caam_alg_entry
{
49 struct caam_aead_alg
{
51 struct caam_alg_entry caam
;
55 struct caam_skcipher_alg
{
56 struct skcipher_alg skcipher
;
57 struct caam_alg_entry caam
;
66 u32 sh_desc_enc
[DESC_MAX_USED_LEN
];
67 u32 sh_desc_dec
[DESC_MAX_USED_LEN
];
68 u8 key
[CAAM_MAX_KEY_SIZE
];
70 enum dma_data_direction dir
;
73 unsigned int authsize
;
75 spinlock_t lock
; /* Protects multiple init of driver context */
76 struct caam_drv_ctx
*drv_ctx
[NUM_OP
];
77 bool xts_key_fallback
;
78 struct crypto_skcipher
*fallback
;
81 struct caam_skcipher_req_ctx
{
82 struct skcipher_request fallback_req
;
85 static int aead_set_sh_desc(struct crypto_aead
*aead
)
87 struct caam_aead_alg
*alg
= container_of(crypto_aead_alg(aead
),
89 struct caam_ctx
*ctx
= crypto_aead_ctx_dma(aead
);
90 unsigned int ivsize
= crypto_aead_ivsize(aead
);
93 unsigned int data_len
[2];
95 const bool ctr_mode
= ((ctx
->cdata
.algtype
& OP_ALG_AAI_MASK
) ==
96 OP_ALG_AAI_CTR_MOD128
);
97 const bool is_rfc3686
= alg
->caam
.rfc3686
;
98 struct caam_drv_private
*ctrlpriv
= dev_get_drvdata(ctx
->jrdev
->parent
);
100 if (!ctx
->cdata
.keylen
|| !ctx
->authsize
)
104 * AES-CTR needs to load IV in CONTEXT1 reg
105 * at an offset of 128bits (16bytes)
106 * CONTEXT1[255:128] = IV
113 * CONTEXT1[255:128] = {NONCE, IV, COUNTER}
116 ctx1_iv_off
= 16 + CTR_RFC3686_NONCE_SIZE
;
117 nonce
= (u32
*)((void *)ctx
->key
+ ctx
->adata
.keylen_pad
+
118 ctx
->cdata
.keylen
- CTR_RFC3686_NONCE_SIZE
);
122 * In case |user key| > |derived key|, using DKP<imm,imm> would result
123 * in invalid opcodes (last bytes of user key) in the resulting
124 * descriptor. Use DKP<ptr,imm> instead => both virtual and dma key
125 * addresses are needed.
127 ctx
->adata
.key_virt
= ctx
->key
;
128 ctx
->adata
.key_dma
= ctx
->key_dma
;
130 ctx
->cdata
.key_virt
= ctx
->key
+ ctx
->adata
.keylen_pad
;
131 ctx
->cdata
.key_dma
= ctx
->key_dma
+ ctx
->adata
.keylen_pad
;
133 data_len
[0] = ctx
->adata
.keylen_pad
;
134 data_len
[1] = ctx
->cdata
.keylen
;
139 /* aead_encrypt shared descriptor */
140 if (desc_inline_query(DESC_QI_AEAD_ENC_LEN
+
141 (is_rfc3686
? DESC_AEAD_CTR_RFC3686_LEN
: 0),
142 DESC_JOB_IO_LEN
, data_len
, &inl_mask
,
143 ARRAY_SIZE(data_len
)) < 0)
146 ctx
->adata
.key_inline
= !!(inl_mask
& 1);
147 ctx
->cdata
.key_inline
= !!(inl_mask
& 2);
149 cnstr_shdsc_aead_encap(ctx
->sh_desc_enc
, &ctx
->cdata
, &ctx
->adata
,
150 ivsize
, ctx
->authsize
, is_rfc3686
, nonce
,
151 ctx1_iv_off
, true, ctrlpriv
->era
);
154 /* aead_decrypt shared descriptor */
155 if (desc_inline_query(DESC_QI_AEAD_DEC_LEN
+
156 (is_rfc3686
? DESC_AEAD_CTR_RFC3686_LEN
: 0),
157 DESC_JOB_IO_LEN
, data_len
, &inl_mask
,
158 ARRAY_SIZE(data_len
)) < 0)
161 ctx
->adata
.key_inline
= !!(inl_mask
& 1);
162 ctx
->cdata
.key_inline
= !!(inl_mask
& 2);
164 cnstr_shdsc_aead_decap(ctx
->sh_desc_dec
, &ctx
->cdata
, &ctx
->adata
,
165 ivsize
, ctx
->authsize
, alg
->caam
.geniv
,
166 is_rfc3686
, nonce
, ctx1_iv_off
, true,
169 if (!alg
->caam
.geniv
)
172 /* aead_givencrypt shared descriptor */
173 if (desc_inline_query(DESC_QI_AEAD_GIVENC_LEN
+
174 (is_rfc3686
? DESC_AEAD_CTR_RFC3686_LEN
: 0),
175 DESC_JOB_IO_LEN
, data_len
, &inl_mask
,
176 ARRAY_SIZE(data_len
)) < 0)
179 ctx
->adata
.key_inline
= !!(inl_mask
& 1);
180 ctx
->cdata
.key_inline
= !!(inl_mask
& 2);
182 cnstr_shdsc_aead_givencap(ctx
->sh_desc_enc
, &ctx
->cdata
, &ctx
->adata
,
183 ivsize
, ctx
->authsize
, is_rfc3686
, nonce
,
184 ctx1_iv_off
, true, ctrlpriv
->era
);
190 static int aead_setauthsize(struct crypto_aead
*authenc
, unsigned int authsize
)
192 struct caam_ctx
*ctx
= crypto_aead_ctx_dma(authenc
);
194 ctx
->authsize
= authsize
;
195 aead_set_sh_desc(authenc
);
200 static int aead_setkey(struct crypto_aead
*aead
, const u8
*key
,
203 struct caam_ctx
*ctx
= crypto_aead_ctx_dma(aead
);
204 struct device
*jrdev
= ctx
->jrdev
;
205 struct caam_drv_private
*ctrlpriv
= dev_get_drvdata(jrdev
->parent
);
206 struct crypto_authenc_keys keys
;
209 if (crypto_authenc_extractkeys(&keys
, key
, keylen
) != 0)
212 dev_dbg(jrdev
, "keylen %d enckeylen %d authkeylen %d\n",
213 keys
.authkeylen
+ keys
.enckeylen
, keys
.enckeylen
,
215 print_hex_dump_debug("key in @" __stringify(__LINE__
)": ",
216 DUMP_PREFIX_ADDRESS
, 16, 4, key
, keylen
, 1);
219 * If DKP is supported, use it in the shared descriptor to generate
222 if (ctrlpriv
->era
>= 6) {
223 ctx
->adata
.keylen
= keys
.authkeylen
;
224 ctx
->adata
.keylen_pad
= split_key_len(ctx
->adata
.algtype
&
227 if (ctx
->adata
.keylen_pad
+ keys
.enckeylen
> CAAM_MAX_KEY_SIZE
)
230 memcpy(ctx
->key
, keys
.authkey
, keys
.authkeylen
);
231 memcpy(ctx
->key
+ ctx
->adata
.keylen_pad
, keys
.enckey
,
233 dma_sync_single_for_device(jrdev
->parent
, ctx
->key_dma
,
234 ctx
->adata
.keylen_pad
+
235 keys
.enckeylen
, ctx
->dir
);
239 ret
= gen_split_key(jrdev
, ctx
->key
, &ctx
->adata
, keys
.authkey
,
240 keys
.authkeylen
, CAAM_MAX_KEY_SIZE
-
245 /* postpend encryption key to auth split key */
246 memcpy(ctx
->key
+ ctx
->adata
.keylen_pad
, keys
.enckey
, keys
.enckeylen
);
247 dma_sync_single_for_device(jrdev
->parent
, ctx
->key_dma
,
248 ctx
->adata
.keylen_pad
+ keys
.enckeylen
,
251 print_hex_dump_debug("ctx.key@" __stringify(__LINE__
)": ",
252 DUMP_PREFIX_ADDRESS
, 16, 4, ctx
->key
,
253 ctx
->adata
.keylen_pad
+ keys
.enckeylen
, 1);
256 ctx
->cdata
.keylen
= keys
.enckeylen
;
258 ret
= aead_set_sh_desc(aead
);
262 /* Now update the driver contexts with the new shared descriptor */
263 if (ctx
->drv_ctx
[ENCRYPT
]) {
264 ret
= caam_drv_ctx_update(ctx
->drv_ctx
[ENCRYPT
],
267 dev_err(jrdev
, "driver enc context update failed\n");
272 if (ctx
->drv_ctx
[DECRYPT
]) {
273 ret
= caam_drv_ctx_update(ctx
->drv_ctx
[DECRYPT
],
276 dev_err(jrdev
, "driver dec context update failed\n");
281 memzero_explicit(&keys
, sizeof(keys
));
284 memzero_explicit(&keys
, sizeof(keys
));
288 static int des3_aead_setkey(struct crypto_aead
*aead
, const u8
*key
,
291 struct crypto_authenc_keys keys
;
294 err
= crypto_authenc_extractkeys(&keys
, key
, keylen
);
298 err
= verify_aead_des3_key(aead
, keys
.enckey
, keys
.enckeylen
) ?:
299 aead_setkey(aead
, key
, keylen
);
301 memzero_explicit(&keys
, sizeof(keys
));
305 static int gcm_set_sh_desc(struct crypto_aead
*aead
)
307 struct caam_ctx
*ctx
= crypto_aead_ctx_dma(aead
);
308 unsigned int ivsize
= crypto_aead_ivsize(aead
);
309 int rem_bytes
= CAAM_DESC_BYTES_MAX
- DESC_JOB_IO_LEN
-
312 if (!ctx
->cdata
.keylen
|| !ctx
->authsize
)
316 * Job Descriptor and Shared Descriptor
317 * must fit into the 64-word Descriptor h/w Buffer
319 if (rem_bytes
>= DESC_QI_GCM_ENC_LEN
) {
320 ctx
->cdata
.key_inline
= true;
321 ctx
->cdata
.key_virt
= ctx
->key
;
323 ctx
->cdata
.key_inline
= false;
324 ctx
->cdata
.key_dma
= ctx
->key_dma
;
327 cnstr_shdsc_gcm_encap(ctx
->sh_desc_enc
, &ctx
->cdata
, ivsize
,
328 ctx
->authsize
, true);
331 * Job Descriptor and Shared Descriptor
332 * must fit into the 64-word Descriptor h/w Buffer
334 if (rem_bytes
>= DESC_QI_GCM_DEC_LEN
) {
335 ctx
->cdata
.key_inline
= true;
336 ctx
->cdata
.key_virt
= ctx
->key
;
338 ctx
->cdata
.key_inline
= false;
339 ctx
->cdata
.key_dma
= ctx
->key_dma
;
342 cnstr_shdsc_gcm_decap(ctx
->sh_desc_dec
, &ctx
->cdata
, ivsize
,
343 ctx
->authsize
, true);
348 static int gcm_setauthsize(struct crypto_aead
*authenc
, unsigned int authsize
)
350 struct caam_ctx
*ctx
= crypto_aead_ctx_dma(authenc
);
353 err
= crypto_gcm_check_authsize(authsize
);
357 ctx
->authsize
= authsize
;
358 gcm_set_sh_desc(authenc
);
363 static int gcm_setkey(struct crypto_aead
*aead
,
364 const u8
*key
, unsigned int keylen
)
366 struct caam_ctx
*ctx
= crypto_aead_ctx_dma(aead
);
367 struct device
*jrdev
= ctx
->jrdev
;
370 ret
= aes_check_keylen(keylen
);
374 print_hex_dump_debug("key in @" __stringify(__LINE__
)": ",
375 DUMP_PREFIX_ADDRESS
, 16, 4, key
, keylen
, 1);
377 memcpy(ctx
->key
, key
, keylen
);
378 dma_sync_single_for_device(jrdev
->parent
, ctx
->key_dma
, keylen
,
380 ctx
->cdata
.keylen
= keylen
;
382 ret
= gcm_set_sh_desc(aead
);
386 /* Now update the driver contexts with the new shared descriptor */
387 if (ctx
->drv_ctx
[ENCRYPT
]) {
388 ret
= caam_drv_ctx_update(ctx
->drv_ctx
[ENCRYPT
],
391 dev_err(jrdev
, "driver enc context update failed\n");
396 if (ctx
->drv_ctx
[DECRYPT
]) {
397 ret
= caam_drv_ctx_update(ctx
->drv_ctx
[DECRYPT
],
400 dev_err(jrdev
, "driver dec context update failed\n");
408 static int rfc4106_set_sh_desc(struct crypto_aead
*aead
)
410 struct caam_ctx
*ctx
= crypto_aead_ctx_dma(aead
);
411 unsigned int ivsize
= crypto_aead_ivsize(aead
);
412 int rem_bytes
= CAAM_DESC_BYTES_MAX
- DESC_JOB_IO_LEN
-
415 if (!ctx
->cdata
.keylen
|| !ctx
->authsize
)
418 ctx
->cdata
.key_virt
= ctx
->key
;
421 * Job Descriptor and Shared Descriptor
422 * must fit into the 64-word Descriptor h/w Buffer
424 if (rem_bytes
>= DESC_QI_RFC4106_ENC_LEN
) {
425 ctx
->cdata
.key_inline
= true;
427 ctx
->cdata
.key_inline
= false;
428 ctx
->cdata
.key_dma
= ctx
->key_dma
;
431 cnstr_shdsc_rfc4106_encap(ctx
->sh_desc_enc
, &ctx
->cdata
, ivsize
,
432 ctx
->authsize
, true);
435 * Job Descriptor and Shared Descriptor
436 * must fit into the 64-word Descriptor h/w Buffer
438 if (rem_bytes
>= DESC_QI_RFC4106_DEC_LEN
) {
439 ctx
->cdata
.key_inline
= true;
441 ctx
->cdata
.key_inline
= false;
442 ctx
->cdata
.key_dma
= ctx
->key_dma
;
445 cnstr_shdsc_rfc4106_decap(ctx
->sh_desc_dec
, &ctx
->cdata
, ivsize
,
446 ctx
->authsize
, true);
451 static int rfc4106_setauthsize(struct crypto_aead
*authenc
,
452 unsigned int authsize
)
454 struct caam_ctx
*ctx
= crypto_aead_ctx_dma(authenc
);
457 err
= crypto_rfc4106_check_authsize(authsize
);
461 ctx
->authsize
= authsize
;
462 rfc4106_set_sh_desc(authenc
);
467 static int rfc4106_setkey(struct crypto_aead
*aead
,
468 const u8
*key
, unsigned int keylen
)
470 struct caam_ctx
*ctx
= crypto_aead_ctx_dma(aead
);
471 struct device
*jrdev
= ctx
->jrdev
;
474 ret
= aes_check_keylen(keylen
- 4);
478 print_hex_dump_debug("key in @" __stringify(__LINE__
)": ",
479 DUMP_PREFIX_ADDRESS
, 16, 4, key
, keylen
, 1);
481 memcpy(ctx
->key
, key
, keylen
);
483 * The last four bytes of the key material are used as the salt value
484 * in the nonce. Update the AES key length.
486 ctx
->cdata
.keylen
= keylen
- 4;
487 dma_sync_single_for_device(jrdev
->parent
, ctx
->key_dma
,
488 ctx
->cdata
.keylen
, ctx
->dir
);
490 ret
= rfc4106_set_sh_desc(aead
);
494 /* Now update the driver contexts with the new shared descriptor */
495 if (ctx
->drv_ctx
[ENCRYPT
]) {
496 ret
= caam_drv_ctx_update(ctx
->drv_ctx
[ENCRYPT
],
499 dev_err(jrdev
, "driver enc context update failed\n");
504 if (ctx
->drv_ctx
[DECRYPT
]) {
505 ret
= caam_drv_ctx_update(ctx
->drv_ctx
[DECRYPT
],
508 dev_err(jrdev
, "driver dec context update failed\n");
516 static int rfc4543_set_sh_desc(struct crypto_aead
*aead
)
518 struct caam_ctx
*ctx
= crypto_aead_ctx_dma(aead
);
519 unsigned int ivsize
= crypto_aead_ivsize(aead
);
520 int rem_bytes
= CAAM_DESC_BYTES_MAX
- DESC_JOB_IO_LEN
-
523 if (!ctx
->cdata
.keylen
|| !ctx
->authsize
)
526 ctx
->cdata
.key_virt
= ctx
->key
;
529 * Job Descriptor and Shared Descriptor
530 * must fit into the 64-word Descriptor h/w Buffer
532 if (rem_bytes
>= DESC_QI_RFC4543_ENC_LEN
) {
533 ctx
->cdata
.key_inline
= true;
535 ctx
->cdata
.key_inline
= false;
536 ctx
->cdata
.key_dma
= ctx
->key_dma
;
539 cnstr_shdsc_rfc4543_encap(ctx
->sh_desc_enc
, &ctx
->cdata
, ivsize
,
540 ctx
->authsize
, true);
543 * Job Descriptor and Shared Descriptor
544 * must fit into the 64-word Descriptor h/w Buffer
546 if (rem_bytes
>= DESC_QI_RFC4543_DEC_LEN
) {
547 ctx
->cdata
.key_inline
= true;
549 ctx
->cdata
.key_inline
= false;
550 ctx
->cdata
.key_dma
= ctx
->key_dma
;
553 cnstr_shdsc_rfc4543_decap(ctx
->sh_desc_dec
, &ctx
->cdata
, ivsize
,
554 ctx
->authsize
, true);
559 static int rfc4543_setauthsize(struct crypto_aead
*authenc
,
560 unsigned int authsize
)
562 struct caam_ctx
*ctx
= crypto_aead_ctx_dma(authenc
);
567 ctx
->authsize
= authsize
;
568 rfc4543_set_sh_desc(authenc
);
573 static int rfc4543_setkey(struct crypto_aead
*aead
,
574 const u8
*key
, unsigned int keylen
)
576 struct caam_ctx
*ctx
= crypto_aead_ctx_dma(aead
);
577 struct device
*jrdev
= ctx
->jrdev
;
580 ret
= aes_check_keylen(keylen
- 4);
584 print_hex_dump_debug("key in @" __stringify(__LINE__
)": ",
585 DUMP_PREFIX_ADDRESS
, 16, 4, key
, keylen
, 1);
587 memcpy(ctx
->key
, key
, keylen
);
589 * The last four bytes of the key material are used as the salt value
590 * in the nonce. Update the AES key length.
592 ctx
->cdata
.keylen
= keylen
- 4;
593 dma_sync_single_for_device(jrdev
->parent
, ctx
->key_dma
,
594 ctx
->cdata
.keylen
, ctx
->dir
);
596 ret
= rfc4543_set_sh_desc(aead
);
600 /* Now update the driver contexts with the new shared descriptor */
601 if (ctx
->drv_ctx
[ENCRYPT
]) {
602 ret
= caam_drv_ctx_update(ctx
->drv_ctx
[ENCRYPT
],
605 dev_err(jrdev
, "driver enc context update failed\n");
610 if (ctx
->drv_ctx
[DECRYPT
]) {
611 ret
= caam_drv_ctx_update(ctx
->drv_ctx
[DECRYPT
],
614 dev_err(jrdev
, "driver dec context update failed\n");
622 static int skcipher_setkey(struct crypto_skcipher
*skcipher
, const u8
*key
,
623 unsigned int keylen
, const u32 ctx1_iv_off
)
625 struct caam_ctx
*ctx
= crypto_skcipher_ctx_dma(skcipher
);
626 struct caam_skcipher_alg
*alg
=
627 container_of(crypto_skcipher_alg(skcipher
), typeof(*alg
),
629 struct device
*jrdev
= ctx
->jrdev
;
630 unsigned int ivsize
= crypto_skcipher_ivsize(skcipher
);
631 const bool is_rfc3686
= alg
->caam
.rfc3686
;
634 print_hex_dump_debug("key in @" __stringify(__LINE__
)": ",
635 DUMP_PREFIX_ADDRESS
, 16, 4, key
, keylen
, 1);
637 ctx
->cdata
.keylen
= keylen
;
638 ctx
->cdata
.key_virt
= key
;
639 ctx
->cdata
.key_inline
= true;
641 /* skcipher encrypt, decrypt shared descriptors */
642 cnstr_shdsc_skcipher_encap(ctx
->sh_desc_enc
, &ctx
->cdata
, ivsize
,
643 is_rfc3686
, ctx1_iv_off
);
644 cnstr_shdsc_skcipher_decap(ctx
->sh_desc_dec
, &ctx
->cdata
, ivsize
,
645 is_rfc3686
, ctx1_iv_off
);
647 /* Now update the driver contexts with the new shared descriptor */
648 if (ctx
->drv_ctx
[ENCRYPT
]) {
649 ret
= caam_drv_ctx_update(ctx
->drv_ctx
[ENCRYPT
],
652 dev_err(jrdev
, "driver enc context update failed\n");
657 if (ctx
->drv_ctx
[DECRYPT
]) {
658 ret
= caam_drv_ctx_update(ctx
->drv_ctx
[DECRYPT
],
661 dev_err(jrdev
, "driver dec context update failed\n");
669 static int aes_skcipher_setkey(struct crypto_skcipher
*skcipher
,
670 const u8
*key
, unsigned int keylen
)
674 err
= aes_check_keylen(keylen
);
678 return skcipher_setkey(skcipher
, key
, keylen
, 0);
681 static int rfc3686_skcipher_setkey(struct crypto_skcipher
*skcipher
,
682 const u8
*key
, unsigned int keylen
)
689 * | CONTEXT1[255:128] = {NONCE, IV, COUNTER}
690 * | *key = {KEY, NONCE}
692 ctx1_iv_off
= 16 + CTR_RFC3686_NONCE_SIZE
;
693 keylen
-= CTR_RFC3686_NONCE_SIZE
;
695 err
= aes_check_keylen(keylen
);
699 return skcipher_setkey(skcipher
, key
, keylen
, ctx1_iv_off
);
702 static int ctr_skcipher_setkey(struct crypto_skcipher
*skcipher
,
703 const u8
*key
, unsigned int keylen
)
709 * AES-CTR needs to load IV in CONTEXT1 reg
710 * at an offset of 128bits (16bytes)
711 * CONTEXT1[255:128] = IV
715 err
= aes_check_keylen(keylen
);
719 return skcipher_setkey(skcipher
, key
, keylen
, ctx1_iv_off
);
722 static int des3_skcipher_setkey(struct crypto_skcipher
*skcipher
,
723 const u8
*key
, unsigned int keylen
)
725 return verify_skcipher_des3_key(skcipher
, key
) ?:
726 skcipher_setkey(skcipher
, key
, keylen
, 0);
729 static int des_skcipher_setkey(struct crypto_skcipher
*skcipher
,
730 const u8
*key
, unsigned int keylen
)
732 return verify_skcipher_des_key(skcipher
, key
) ?:
733 skcipher_setkey(skcipher
, key
, keylen
, 0);
736 static int xts_skcipher_setkey(struct crypto_skcipher
*skcipher
, const u8
*key
,
739 struct caam_ctx
*ctx
= crypto_skcipher_ctx_dma(skcipher
);
740 struct device
*jrdev
= ctx
->jrdev
;
741 struct caam_drv_private
*ctrlpriv
= dev_get_drvdata(jrdev
->parent
);
745 err
= xts_verify_key(skcipher
, key
, keylen
);
747 dev_dbg(jrdev
, "key size mismatch\n");
751 if (keylen
!= 2 * AES_KEYSIZE_128
&& keylen
!= 2 * AES_KEYSIZE_256
)
752 ctx
->xts_key_fallback
= true;
754 if (ctrlpriv
->era
<= 8 || ctx
->xts_key_fallback
) {
755 err
= crypto_skcipher_setkey(ctx
->fallback
, key
, keylen
);
760 ctx
->cdata
.keylen
= keylen
;
761 ctx
->cdata
.key_virt
= key
;
762 ctx
->cdata
.key_inline
= true;
764 /* xts skcipher encrypt, decrypt shared descriptors */
765 cnstr_shdsc_xts_skcipher_encap(ctx
->sh_desc_enc
, &ctx
->cdata
);
766 cnstr_shdsc_xts_skcipher_decap(ctx
->sh_desc_dec
, &ctx
->cdata
);
768 /* Now update the driver contexts with the new shared descriptor */
769 if (ctx
->drv_ctx
[ENCRYPT
]) {
770 ret
= caam_drv_ctx_update(ctx
->drv_ctx
[ENCRYPT
],
773 dev_err(jrdev
, "driver enc context update failed\n");
778 if (ctx
->drv_ctx
[DECRYPT
]) {
779 ret
= caam_drv_ctx_update(ctx
->drv_ctx
[DECRYPT
],
782 dev_err(jrdev
, "driver dec context update failed\n");
791 * aead_edesc - s/w-extended aead descriptor
792 * @src_nents: number of segments in input scatterlist
793 * @dst_nents: number of segments in output scatterlist
794 * @iv_dma: dma address of iv for checking continuity and link table
795 * @qm_sg_bytes: length of dma mapped h/w link table
796 * @qm_sg_dma: bus physical mapped address of h/w link table
797 * @assoclen: associated data length, in CAAM endianness
798 * @assoclen_dma: bus physical mapped address of req->assoclen
799 * @drv_req: driver-specific request structure
800 * @sgt: the h/w link table, followed by IV
807 dma_addr_t qm_sg_dma
;
808 unsigned int assoclen
;
809 dma_addr_t assoclen_dma
;
810 struct caam_drv_req drv_req
;
811 struct qm_sg_entry sgt
[];
815 * skcipher_edesc - s/w-extended skcipher descriptor
816 * @src_nents: number of segments in input scatterlist
817 * @dst_nents: number of segments in output scatterlist
818 * @iv_dma: dma address of iv for checking continuity and link table
819 * @qm_sg_bytes: length of dma mapped h/w link table
820 * @qm_sg_dma: bus physical mapped address of h/w link table
821 * @drv_req: driver-specific request structure
822 * @sgt: the h/w link table, followed by IV
824 struct skcipher_edesc
{
829 dma_addr_t qm_sg_dma
;
830 struct caam_drv_req drv_req
;
831 struct qm_sg_entry sgt
[];
834 static struct caam_drv_ctx
*get_drv_ctx(struct caam_ctx
*ctx
,
838 * This function is called on the fast path with values of 'type'
839 * known at compile time. Invalid arguments are not expected and
840 * thus no checks are made.
842 struct caam_drv_ctx
*drv_ctx
= ctx
->drv_ctx
[type
];
845 if (unlikely(!drv_ctx
)) {
846 spin_lock(&ctx
->lock
);
848 /* Read again to check if some other core init drv_ctx */
849 drv_ctx
= ctx
->drv_ctx
[type
];
854 desc
= ctx
->sh_desc_enc
;
855 else /* (type == DECRYPT) */
856 desc
= ctx
->sh_desc_dec
;
858 cpu
= smp_processor_id();
859 drv_ctx
= caam_drv_ctx_init(ctx
->qidev
, &cpu
, desc
);
860 if (!IS_ERR(drv_ctx
))
861 drv_ctx
->op_type
= type
;
863 ctx
->drv_ctx
[type
] = drv_ctx
;
866 spin_unlock(&ctx
->lock
);
872 static void caam_unmap(struct device
*dev
, struct scatterlist
*src
,
873 struct scatterlist
*dst
, int src_nents
,
874 int dst_nents
, dma_addr_t iv_dma
, int ivsize
,
875 enum dma_data_direction iv_dir
, dma_addr_t qm_sg_dma
,
880 dma_unmap_sg(dev
, src
, src_nents
, DMA_TO_DEVICE
);
882 dma_unmap_sg(dev
, dst
, dst_nents
, DMA_FROM_DEVICE
);
884 dma_unmap_sg(dev
, src
, src_nents
, DMA_BIDIRECTIONAL
);
888 dma_unmap_single(dev
, iv_dma
, ivsize
, iv_dir
);
890 dma_unmap_single(dev
, qm_sg_dma
, qm_sg_bytes
, DMA_TO_DEVICE
);
893 static void aead_unmap(struct device
*dev
,
894 struct aead_edesc
*edesc
,
895 struct aead_request
*req
)
897 struct crypto_aead
*aead
= crypto_aead_reqtfm(req
);
898 int ivsize
= crypto_aead_ivsize(aead
);
900 caam_unmap(dev
, req
->src
, req
->dst
, edesc
->src_nents
, edesc
->dst_nents
,
901 edesc
->iv_dma
, ivsize
, DMA_TO_DEVICE
, edesc
->qm_sg_dma
,
903 dma_unmap_single(dev
, edesc
->assoclen_dma
, 4, DMA_TO_DEVICE
);
906 static void skcipher_unmap(struct device
*dev
, struct skcipher_edesc
*edesc
,
907 struct skcipher_request
*req
)
909 struct crypto_skcipher
*skcipher
= crypto_skcipher_reqtfm(req
);
910 int ivsize
= crypto_skcipher_ivsize(skcipher
);
912 caam_unmap(dev
, req
->src
, req
->dst
, edesc
->src_nents
, edesc
->dst_nents
,
913 edesc
->iv_dma
, ivsize
, DMA_BIDIRECTIONAL
, edesc
->qm_sg_dma
,
917 static void aead_done(struct caam_drv_req
*drv_req
, u32 status
)
919 struct device
*qidev
;
920 struct aead_edesc
*edesc
;
921 struct aead_request
*aead_req
= drv_req
->app_ctx
;
922 struct crypto_aead
*aead
= crypto_aead_reqtfm(aead_req
);
923 struct caam_ctx
*caam_ctx
= crypto_aead_ctx_dma(aead
);
926 qidev
= caam_ctx
->qidev
;
928 if (unlikely(status
))
929 ecode
= caam_jr_strstatus(qidev
, status
);
931 edesc
= container_of(drv_req
, typeof(*edesc
), drv_req
);
932 aead_unmap(qidev
, edesc
, aead_req
);
934 aead_request_complete(aead_req
, ecode
);
935 qi_cache_free(edesc
);
939 * allocate and map the aead extended descriptor
941 static struct aead_edesc
*aead_edesc_alloc(struct aead_request
*req
,
944 struct crypto_aead
*aead
= crypto_aead_reqtfm(req
);
945 struct caam_ctx
*ctx
= crypto_aead_ctx_dma(aead
);
946 struct caam_aead_alg
*alg
= container_of(crypto_aead_alg(aead
),
948 struct device
*qidev
= ctx
->qidev
;
949 gfp_t flags
= (req
->base
.flags
& CRYPTO_TFM_REQ_MAY_SLEEP
) ?
950 GFP_KERNEL
: GFP_ATOMIC
;
951 int src_nents
, mapped_src_nents
, dst_nents
= 0, mapped_dst_nents
= 0;
952 int src_len
, dst_len
= 0;
953 struct aead_edesc
*edesc
;
954 dma_addr_t qm_sg_dma
, iv_dma
= 0;
956 unsigned int authsize
= ctx
->authsize
;
957 int qm_sg_index
= 0, qm_sg_ents
= 0, qm_sg_bytes
;
959 struct qm_sg_entry
*sg_table
, *fd_sgt
;
960 struct caam_drv_ctx
*drv_ctx
;
962 drv_ctx
= get_drv_ctx(ctx
, encrypt
? ENCRYPT
: DECRYPT
);
964 return ERR_CAST(drv_ctx
);
966 /* allocate space for base edesc and hw desc commands, link tables */
967 edesc
= qi_cache_alloc(flags
);
968 if (unlikely(!edesc
)) {
969 dev_err(qidev
, "could not allocate extended descriptor\n");
970 return ERR_PTR(-ENOMEM
);
973 if (likely(req
->src
== req
->dst
)) {
974 src_len
= req
->assoclen
+ req
->cryptlen
+
975 (encrypt
? authsize
: 0);
977 src_nents
= sg_nents_for_len(req
->src
, src_len
);
978 if (unlikely(src_nents
< 0)) {
979 dev_err(qidev
, "Insufficient bytes (%d) in src S/G\n",
981 qi_cache_free(edesc
);
982 return ERR_PTR(src_nents
);
985 mapped_src_nents
= dma_map_sg(qidev
, req
->src
, src_nents
,
987 if (unlikely(!mapped_src_nents
)) {
988 dev_err(qidev
, "unable to map source\n");
989 qi_cache_free(edesc
);
990 return ERR_PTR(-ENOMEM
);
993 src_len
= req
->assoclen
+ req
->cryptlen
;
994 dst_len
= src_len
+ (encrypt
? authsize
: (-authsize
));
996 src_nents
= sg_nents_for_len(req
->src
, src_len
);
997 if (unlikely(src_nents
< 0)) {
998 dev_err(qidev
, "Insufficient bytes (%d) in src S/G\n",
1000 qi_cache_free(edesc
);
1001 return ERR_PTR(src_nents
);
1004 dst_nents
= sg_nents_for_len(req
->dst
, dst_len
);
1005 if (unlikely(dst_nents
< 0)) {
1006 dev_err(qidev
, "Insufficient bytes (%d) in dst S/G\n",
1008 qi_cache_free(edesc
);
1009 return ERR_PTR(dst_nents
);
1013 mapped_src_nents
= dma_map_sg(qidev
, req
->src
,
1014 src_nents
, DMA_TO_DEVICE
);
1015 if (unlikely(!mapped_src_nents
)) {
1016 dev_err(qidev
, "unable to map source\n");
1017 qi_cache_free(edesc
);
1018 return ERR_PTR(-ENOMEM
);
1021 mapped_src_nents
= 0;
1025 mapped_dst_nents
= dma_map_sg(qidev
, req
->dst
,
1028 if (unlikely(!mapped_dst_nents
)) {
1029 dev_err(qidev
, "unable to map destination\n");
1030 dma_unmap_sg(qidev
, req
->src
, src_nents
,
1032 qi_cache_free(edesc
);
1033 return ERR_PTR(-ENOMEM
);
1036 mapped_dst_nents
= 0;
1040 if ((alg
->caam
.rfc3686
&& encrypt
) || !alg
->caam
.geniv
)
1041 ivsize
= crypto_aead_ivsize(aead
);
1044 * Create S/G table: req->assoclen, [IV,] req->src [, req->dst].
1045 * Input is not contiguous.
1046 * HW reads 4 S/G entries at a time; make sure the reads don't go beyond
1047 * the end of the table by allocating more S/G entries. Logic:
1048 * if (src != dst && output S/G)
1049 * pad output S/G, if needed
1050 * else if (src == dst && S/G)
1051 * overlapping S/Gs; pad one of them
1052 * else if (input S/G) ...
1053 * pad input S/G, if needed
1055 qm_sg_ents
= 1 + !!ivsize
+ mapped_src_nents
;
1056 if (mapped_dst_nents
> 1)
1057 qm_sg_ents
+= pad_sg_nents(mapped_dst_nents
);
1058 else if ((req
->src
== req
->dst
) && (mapped_src_nents
> 1))
1059 qm_sg_ents
= max(pad_sg_nents(qm_sg_ents
),
1060 1 + !!ivsize
+ pad_sg_nents(mapped_src_nents
));
1062 qm_sg_ents
= pad_sg_nents(qm_sg_ents
);
1064 sg_table
= &edesc
->sgt
[0];
1065 qm_sg_bytes
= qm_sg_ents
* sizeof(*sg_table
);
1066 if (unlikely(offsetof(struct aead_edesc
, sgt
) + qm_sg_bytes
+ ivsize
>
1067 CAAM_QI_MEMCACHE_SIZE
)) {
1068 dev_err(qidev
, "No space for %d S/G entries and/or %dB IV\n",
1069 qm_sg_ents
, ivsize
);
1070 caam_unmap(qidev
, req
->src
, req
->dst
, src_nents
, dst_nents
, 0,
1072 qi_cache_free(edesc
);
1073 return ERR_PTR(-ENOMEM
);
1077 u8
*iv
= (u8
*)(sg_table
+ qm_sg_ents
);
1079 /* Make sure IV is located in a DMAable area */
1080 memcpy(iv
, req
->iv
, ivsize
);
1082 iv_dma
= dma_map_single(qidev
, iv
, ivsize
, DMA_TO_DEVICE
);
1083 if (dma_mapping_error(qidev
, iv_dma
)) {
1084 dev_err(qidev
, "unable to map IV\n");
1085 caam_unmap(qidev
, req
->src
, req
->dst
, src_nents
,
1086 dst_nents
, 0, 0, DMA_NONE
, 0, 0);
1087 qi_cache_free(edesc
);
1088 return ERR_PTR(-ENOMEM
);
1092 edesc
->src_nents
= src_nents
;
1093 edesc
->dst_nents
= dst_nents
;
1094 edesc
->iv_dma
= iv_dma
;
1095 edesc
->drv_req
.app_ctx
= req
;
1096 edesc
->drv_req
.cbk
= aead_done
;
1097 edesc
->drv_req
.drv_ctx
= drv_ctx
;
1099 edesc
->assoclen
= cpu_to_caam32(req
->assoclen
);
1100 edesc
->assoclen_dma
= dma_map_single(qidev
, &edesc
->assoclen
, 4,
1102 if (dma_mapping_error(qidev
, edesc
->assoclen_dma
)) {
1103 dev_err(qidev
, "unable to map assoclen\n");
1104 caam_unmap(qidev
, req
->src
, req
->dst
, src_nents
, dst_nents
,
1105 iv_dma
, ivsize
, DMA_TO_DEVICE
, 0, 0);
1106 qi_cache_free(edesc
);
1107 return ERR_PTR(-ENOMEM
);
1110 dma_to_qm_sg_one(sg_table
, edesc
->assoclen_dma
, 4, 0);
1113 dma_to_qm_sg_one(sg_table
+ qm_sg_index
, iv_dma
, ivsize
, 0);
1116 sg_to_qm_sg_last(req
->src
, src_len
, sg_table
+ qm_sg_index
, 0);
1117 qm_sg_index
+= mapped_src_nents
;
1119 if (mapped_dst_nents
> 1)
1120 sg_to_qm_sg_last(req
->dst
, dst_len
, sg_table
+ qm_sg_index
, 0);
1122 qm_sg_dma
= dma_map_single(qidev
, sg_table
, qm_sg_bytes
, DMA_TO_DEVICE
);
1123 if (dma_mapping_error(qidev
, qm_sg_dma
)) {
1124 dev_err(qidev
, "unable to map S/G table\n");
1125 dma_unmap_single(qidev
, edesc
->assoclen_dma
, 4, DMA_TO_DEVICE
);
1126 caam_unmap(qidev
, req
->src
, req
->dst
, src_nents
, dst_nents
,
1127 iv_dma
, ivsize
, DMA_TO_DEVICE
, 0, 0);
1128 qi_cache_free(edesc
);
1129 return ERR_PTR(-ENOMEM
);
1132 edesc
->qm_sg_dma
= qm_sg_dma
;
1133 edesc
->qm_sg_bytes
= qm_sg_bytes
;
1135 out_len
= req
->assoclen
+ req
->cryptlen
+
1136 (encrypt
? ctx
->authsize
: (-ctx
->authsize
));
1137 in_len
= 4 + ivsize
+ req
->assoclen
+ req
->cryptlen
;
1139 fd_sgt
= &edesc
->drv_req
.fd_sgt
[0];
1140 dma_to_qm_sg_one_last_ext(&fd_sgt
[1], qm_sg_dma
, in_len
, 0);
1142 if (req
->dst
== req
->src
) {
1143 if (mapped_src_nents
== 1)
1144 dma_to_qm_sg_one(&fd_sgt
[0], sg_dma_address(req
->src
),
1147 dma_to_qm_sg_one_ext(&fd_sgt
[0], qm_sg_dma
+
1148 (1 + !!ivsize
) * sizeof(*sg_table
),
1150 } else if (mapped_dst_nents
<= 1) {
1151 dma_to_qm_sg_one(&fd_sgt
[0], sg_dma_address(req
->dst
), out_len
,
1154 dma_to_qm_sg_one_ext(&fd_sgt
[0], qm_sg_dma
+ sizeof(*sg_table
) *
1155 qm_sg_index
, out_len
, 0);
1161 static inline int aead_crypt(struct aead_request
*req
, bool encrypt
)
1163 struct aead_edesc
*edesc
;
1164 struct crypto_aead
*aead
= crypto_aead_reqtfm(req
);
1165 struct caam_ctx
*ctx
= crypto_aead_ctx_dma(aead
);
1168 if (unlikely(caam_congested
))
1171 /* allocate extended descriptor */
1172 edesc
= aead_edesc_alloc(req
, encrypt
);
1174 return PTR_ERR(edesc
);
1176 /* Create and submit job descriptor */
1177 ret
= caam_qi_enqueue(ctx
->qidev
, &edesc
->drv_req
);
1181 aead_unmap(ctx
->qidev
, edesc
, req
);
1182 qi_cache_free(edesc
);
1188 static int aead_encrypt(struct aead_request
*req
)
1190 return aead_crypt(req
, true);
1193 static int aead_decrypt(struct aead_request
*req
)
1195 return aead_crypt(req
, false);
1198 static int ipsec_gcm_encrypt(struct aead_request
*req
)
1200 return crypto_ipsec_check_assoclen(req
->assoclen
) ? : aead_crypt(req
,
1204 static int ipsec_gcm_decrypt(struct aead_request
*req
)
1206 return crypto_ipsec_check_assoclen(req
->assoclen
) ? : aead_crypt(req
,
1210 static inline u8
*skcipher_edesc_iv(struct skcipher_edesc
*edesc
)
1212 return PTR_ALIGN((u8
*)&edesc
->sgt
[0] + edesc
->qm_sg_bytes
,
1213 dma_get_cache_alignment());
1216 static void skcipher_done(struct caam_drv_req
*drv_req
, u32 status
)
1218 struct skcipher_edesc
*edesc
;
1219 struct skcipher_request
*req
= drv_req
->app_ctx
;
1220 struct crypto_skcipher
*skcipher
= crypto_skcipher_reqtfm(req
);
1221 struct caam_ctx
*caam_ctx
= crypto_skcipher_ctx_dma(skcipher
);
1222 struct device
*qidev
= caam_ctx
->qidev
;
1223 int ivsize
= crypto_skcipher_ivsize(skcipher
);
1226 dev_dbg(qidev
, "%s %d: status 0x%x\n", __func__
, __LINE__
, status
);
1228 edesc
= container_of(drv_req
, typeof(*edesc
), drv_req
);
1231 ecode
= caam_jr_strstatus(qidev
, status
);
1233 print_hex_dump_debug("dstiv @" __stringify(__LINE__
)": ",
1234 DUMP_PREFIX_ADDRESS
, 16, 4, req
->iv
,
1235 edesc
->src_nents
> 1 ? 100 : ivsize
, 1);
1236 caam_dump_sg("dst @" __stringify(__LINE__
)": ",
1237 DUMP_PREFIX_ADDRESS
, 16, 4, req
->dst
,
1238 edesc
->dst_nents
> 1 ? 100 : req
->cryptlen
, 1);
1240 skcipher_unmap(qidev
, edesc
, req
);
1243 * The crypto API expects us to set the IV (req->iv) to the last
1244 * ciphertext block (CBC mode) or last counter (CTR mode).
1245 * This is used e.g. by the CTS mode.
1248 memcpy(req
->iv
, skcipher_edesc_iv(edesc
), ivsize
);
1250 qi_cache_free(edesc
);
1251 skcipher_request_complete(req
, ecode
);
1254 static struct skcipher_edesc
*skcipher_edesc_alloc(struct skcipher_request
*req
,
1257 struct crypto_skcipher
*skcipher
= crypto_skcipher_reqtfm(req
);
1258 struct caam_ctx
*ctx
= crypto_skcipher_ctx_dma(skcipher
);
1259 struct device
*qidev
= ctx
->qidev
;
1260 gfp_t flags
= (req
->base
.flags
& CRYPTO_TFM_REQ_MAY_SLEEP
) ?
1261 GFP_KERNEL
: GFP_ATOMIC
;
1262 int src_nents
, mapped_src_nents
, dst_nents
= 0, mapped_dst_nents
= 0;
1263 struct skcipher_edesc
*edesc
;
1266 int ivsize
= crypto_skcipher_ivsize(skcipher
);
1267 int dst_sg_idx
, qm_sg_ents
, qm_sg_bytes
;
1268 struct qm_sg_entry
*sg_table
, *fd_sgt
;
1269 struct caam_drv_ctx
*drv_ctx
;
1272 drv_ctx
= get_drv_ctx(ctx
, encrypt
? ENCRYPT
: DECRYPT
);
1273 if (IS_ERR(drv_ctx
))
1274 return ERR_CAST(drv_ctx
);
1276 src_nents
= sg_nents_for_len(req
->src
, req
->cryptlen
);
1277 if (unlikely(src_nents
< 0)) {
1278 dev_err(qidev
, "Insufficient bytes (%d) in src S/G\n",
1280 return ERR_PTR(src_nents
);
1283 if (unlikely(req
->src
!= req
->dst
)) {
1284 dst_nents
= sg_nents_for_len(req
->dst
, req
->cryptlen
);
1285 if (unlikely(dst_nents
< 0)) {
1286 dev_err(qidev
, "Insufficient bytes (%d) in dst S/G\n",
1288 return ERR_PTR(dst_nents
);
1291 mapped_src_nents
= dma_map_sg(qidev
, req
->src
, src_nents
,
1293 if (unlikely(!mapped_src_nents
)) {
1294 dev_err(qidev
, "unable to map source\n");
1295 return ERR_PTR(-ENOMEM
);
1298 mapped_dst_nents
= dma_map_sg(qidev
, req
->dst
, dst_nents
,
1300 if (unlikely(!mapped_dst_nents
)) {
1301 dev_err(qidev
, "unable to map destination\n");
1302 dma_unmap_sg(qidev
, req
->src
, src_nents
, DMA_TO_DEVICE
);
1303 return ERR_PTR(-ENOMEM
);
1306 mapped_src_nents
= dma_map_sg(qidev
, req
->src
, src_nents
,
1308 if (unlikely(!mapped_src_nents
)) {
1309 dev_err(qidev
, "unable to map source\n");
1310 return ERR_PTR(-ENOMEM
);
1314 qm_sg_ents
= 1 + mapped_src_nents
;
1315 dst_sg_idx
= qm_sg_ents
;
1318 * Input, output HW S/G tables: [IV, src][dst, IV]
1319 * IV entries point to the same buffer
1320 * If src == dst, S/G entries are reused (S/G tables overlap)
1322 * HW reads 4 S/G entries at a time; make sure the reads don't go beyond
1323 * the end of the table by allocating more S/G entries.
1325 if (req
->src
!= req
->dst
)
1326 qm_sg_ents
+= pad_sg_nents(mapped_dst_nents
+ 1);
1328 qm_sg_ents
= 1 + pad_sg_nents(qm_sg_ents
);
1330 qm_sg_bytes
= qm_sg_ents
* sizeof(struct qm_sg_entry
);
1332 len
= offsetof(struct skcipher_edesc
, sgt
) + qm_sg_bytes
;
1333 len
= ALIGN(len
, dma_get_cache_alignment());
1336 if (unlikely(len
> CAAM_QI_MEMCACHE_SIZE
)) {
1337 dev_err(qidev
, "No space for %d S/G entries and/or %dB IV\n",
1338 qm_sg_ents
, ivsize
);
1339 caam_unmap(qidev
, req
->src
, req
->dst
, src_nents
, dst_nents
, 0,
1341 return ERR_PTR(-ENOMEM
);
1344 /* allocate space for base edesc, link tables and IV */
1345 edesc
= qi_cache_alloc(flags
);
1346 if (unlikely(!edesc
)) {
1347 dev_err(qidev
, "could not allocate extended descriptor\n");
1348 caam_unmap(qidev
, req
->src
, req
->dst
, src_nents
, dst_nents
, 0,
1350 return ERR_PTR(-ENOMEM
);
1353 edesc
->src_nents
= src_nents
;
1354 edesc
->dst_nents
= dst_nents
;
1355 edesc
->qm_sg_bytes
= qm_sg_bytes
;
1356 edesc
->drv_req
.app_ctx
= req
;
1357 edesc
->drv_req
.cbk
= skcipher_done
;
1358 edesc
->drv_req
.drv_ctx
= drv_ctx
;
1360 /* Make sure IV is located in a DMAable area */
1361 sg_table
= &edesc
->sgt
[0];
1362 iv
= skcipher_edesc_iv(edesc
);
1363 memcpy(iv
, req
->iv
, ivsize
);
1365 iv_dma
= dma_map_single(qidev
, iv
, ivsize
, DMA_BIDIRECTIONAL
);
1366 if (dma_mapping_error(qidev
, iv_dma
)) {
1367 dev_err(qidev
, "unable to map IV\n");
1368 caam_unmap(qidev
, req
->src
, req
->dst
, src_nents
, dst_nents
, 0,
1370 qi_cache_free(edesc
);
1371 return ERR_PTR(-ENOMEM
);
1374 edesc
->iv_dma
= iv_dma
;
1376 dma_to_qm_sg_one(sg_table
, iv_dma
, ivsize
, 0);
1377 sg_to_qm_sg(req
->src
, req
->cryptlen
, sg_table
+ 1, 0);
1379 if (req
->src
!= req
->dst
)
1380 sg_to_qm_sg(req
->dst
, req
->cryptlen
, sg_table
+ dst_sg_idx
, 0);
1382 dma_to_qm_sg_one(sg_table
+ dst_sg_idx
+ mapped_dst_nents
, iv_dma
,
1385 edesc
->qm_sg_dma
= dma_map_single(qidev
, sg_table
, edesc
->qm_sg_bytes
,
1387 if (dma_mapping_error(qidev
, edesc
->qm_sg_dma
)) {
1388 dev_err(qidev
, "unable to map S/G table\n");
1389 caam_unmap(qidev
, req
->src
, req
->dst
, src_nents
, dst_nents
,
1390 iv_dma
, ivsize
, DMA_BIDIRECTIONAL
, 0, 0);
1391 qi_cache_free(edesc
);
1392 return ERR_PTR(-ENOMEM
);
1395 fd_sgt
= &edesc
->drv_req
.fd_sgt
[0];
1397 dma_to_qm_sg_one_last_ext(&fd_sgt
[1], edesc
->qm_sg_dma
,
1398 ivsize
+ req
->cryptlen
, 0);
1400 if (req
->src
== req
->dst
)
1401 dma_to_qm_sg_one_ext(&fd_sgt
[0], edesc
->qm_sg_dma
+
1402 sizeof(*sg_table
), req
->cryptlen
+ ivsize
,
1405 dma_to_qm_sg_one_ext(&fd_sgt
[0], edesc
->qm_sg_dma
+ dst_sg_idx
*
1406 sizeof(*sg_table
), req
->cryptlen
+ ivsize
,
1412 static inline bool xts_skcipher_ivsize(struct skcipher_request
*req
)
1414 struct crypto_skcipher
*skcipher
= crypto_skcipher_reqtfm(req
);
1415 unsigned int ivsize
= crypto_skcipher_ivsize(skcipher
);
1417 return !!get_unaligned((u64
*)(req
->iv
+ (ivsize
/ 2)));
1420 static inline int skcipher_crypt(struct skcipher_request
*req
, bool encrypt
)
1422 struct skcipher_edesc
*edesc
;
1423 struct crypto_skcipher
*skcipher
= crypto_skcipher_reqtfm(req
);
1424 struct caam_ctx
*ctx
= crypto_skcipher_ctx_dma(skcipher
);
1425 struct caam_drv_private
*ctrlpriv
= dev_get_drvdata(ctx
->jrdev
->parent
);
1429 * XTS is expected to return an error even for input length = 0
1430 * Note that the case input length < block size will be caught during
1431 * HW offloading and return an error.
1433 if (!req
->cryptlen
&& !ctx
->fallback
)
1436 if (ctx
->fallback
&& ((ctrlpriv
->era
<= 8 && xts_skcipher_ivsize(req
)) ||
1437 ctx
->xts_key_fallback
)) {
1438 struct caam_skcipher_req_ctx
*rctx
= skcipher_request_ctx(req
);
1440 skcipher_request_set_tfm(&rctx
->fallback_req
, ctx
->fallback
);
1441 skcipher_request_set_callback(&rctx
->fallback_req
,
1445 skcipher_request_set_crypt(&rctx
->fallback_req
, req
->src
,
1446 req
->dst
, req
->cryptlen
, req
->iv
);
1448 return encrypt
? crypto_skcipher_encrypt(&rctx
->fallback_req
) :
1449 crypto_skcipher_decrypt(&rctx
->fallback_req
);
1452 if (unlikely(caam_congested
))
1455 /* allocate extended descriptor */
1456 edesc
= skcipher_edesc_alloc(req
, encrypt
);
1458 return PTR_ERR(edesc
);
1460 ret
= caam_qi_enqueue(ctx
->qidev
, &edesc
->drv_req
);
1464 skcipher_unmap(ctx
->qidev
, edesc
, req
);
1465 qi_cache_free(edesc
);
1471 static int skcipher_encrypt(struct skcipher_request
*req
)
1473 return skcipher_crypt(req
, true);
1476 static int skcipher_decrypt(struct skcipher_request
*req
)
1478 return skcipher_crypt(req
, false);
1481 static struct caam_skcipher_alg driver_algs
[] = {
1485 .cra_name
= "cbc(aes)",
1486 .cra_driver_name
= "cbc-aes-caam-qi",
1487 .cra_blocksize
= AES_BLOCK_SIZE
,
1489 .setkey
= aes_skcipher_setkey
,
1490 .encrypt
= skcipher_encrypt
,
1491 .decrypt
= skcipher_decrypt
,
1492 .min_keysize
= AES_MIN_KEY_SIZE
,
1493 .max_keysize
= AES_MAX_KEY_SIZE
,
1494 .ivsize
= AES_BLOCK_SIZE
,
1496 .caam
.class1_alg_type
= OP_ALG_ALGSEL_AES
| OP_ALG_AAI_CBC
,
1501 .cra_name
= "cbc(des3_ede)",
1502 .cra_driver_name
= "cbc-3des-caam-qi",
1503 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
1505 .setkey
= des3_skcipher_setkey
,
1506 .encrypt
= skcipher_encrypt
,
1507 .decrypt
= skcipher_decrypt
,
1508 .min_keysize
= DES3_EDE_KEY_SIZE
,
1509 .max_keysize
= DES3_EDE_KEY_SIZE
,
1510 .ivsize
= DES3_EDE_BLOCK_SIZE
,
1512 .caam
.class1_alg_type
= OP_ALG_ALGSEL_3DES
| OP_ALG_AAI_CBC
,
1517 .cra_name
= "cbc(des)",
1518 .cra_driver_name
= "cbc-des-caam-qi",
1519 .cra_blocksize
= DES_BLOCK_SIZE
,
1521 .setkey
= des_skcipher_setkey
,
1522 .encrypt
= skcipher_encrypt
,
1523 .decrypt
= skcipher_decrypt
,
1524 .min_keysize
= DES_KEY_SIZE
,
1525 .max_keysize
= DES_KEY_SIZE
,
1526 .ivsize
= DES_BLOCK_SIZE
,
1528 .caam
.class1_alg_type
= OP_ALG_ALGSEL_DES
| OP_ALG_AAI_CBC
,
1533 .cra_name
= "ctr(aes)",
1534 .cra_driver_name
= "ctr-aes-caam-qi",
1537 .setkey
= ctr_skcipher_setkey
,
1538 .encrypt
= skcipher_encrypt
,
1539 .decrypt
= skcipher_decrypt
,
1540 .min_keysize
= AES_MIN_KEY_SIZE
,
1541 .max_keysize
= AES_MAX_KEY_SIZE
,
1542 .ivsize
= AES_BLOCK_SIZE
,
1543 .chunksize
= AES_BLOCK_SIZE
,
1545 .caam
.class1_alg_type
= OP_ALG_ALGSEL_AES
|
1546 OP_ALG_AAI_CTR_MOD128
,
1551 .cra_name
= "rfc3686(ctr(aes))",
1552 .cra_driver_name
= "rfc3686-ctr-aes-caam-qi",
1555 .setkey
= rfc3686_skcipher_setkey
,
1556 .encrypt
= skcipher_encrypt
,
1557 .decrypt
= skcipher_decrypt
,
1558 .min_keysize
= AES_MIN_KEY_SIZE
+
1559 CTR_RFC3686_NONCE_SIZE
,
1560 .max_keysize
= AES_MAX_KEY_SIZE
+
1561 CTR_RFC3686_NONCE_SIZE
,
1562 .ivsize
= CTR_RFC3686_IV_SIZE
,
1563 .chunksize
= AES_BLOCK_SIZE
,
1566 .class1_alg_type
= OP_ALG_ALGSEL_AES
|
1567 OP_ALG_AAI_CTR_MOD128
,
1574 .cra_name
= "xts(aes)",
1575 .cra_driver_name
= "xts-aes-caam-qi",
1576 .cra_flags
= CRYPTO_ALG_NEED_FALLBACK
,
1577 .cra_blocksize
= AES_BLOCK_SIZE
,
1579 .setkey
= xts_skcipher_setkey
,
1580 .encrypt
= skcipher_encrypt
,
1581 .decrypt
= skcipher_decrypt
,
1582 .min_keysize
= 2 * AES_MIN_KEY_SIZE
,
1583 .max_keysize
= 2 * AES_MAX_KEY_SIZE
,
1584 .ivsize
= AES_BLOCK_SIZE
,
1586 .caam
.class1_alg_type
= OP_ALG_ALGSEL_AES
| OP_ALG_AAI_XTS
,
1590 static struct caam_aead_alg driver_aeads
[] = {
1594 .cra_name
= "rfc4106(gcm(aes))",
1595 .cra_driver_name
= "rfc4106-gcm-aes-caam-qi",
1598 .setkey
= rfc4106_setkey
,
1599 .setauthsize
= rfc4106_setauthsize
,
1600 .encrypt
= ipsec_gcm_encrypt
,
1601 .decrypt
= ipsec_gcm_decrypt
,
1603 .maxauthsize
= AES_BLOCK_SIZE
,
1606 .class1_alg_type
= OP_ALG_ALGSEL_AES
| OP_ALG_AAI_GCM
,
1613 .cra_name
= "rfc4543(gcm(aes))",
1614 .cra_driver_name
= "rfc4543-gcm-aes-caam-qi",
1617 .setkey
= rfc4543_setkey
,
1618 .setauthsize
= rfc4543_setauthsize
,
1619 .encrypt
= ipsec_gcm_encrypt
,
1620 .decrypt
= ipsec_gcm_decrypt
,
1622 .maxauthsize
= AES_BLOCK_SIZE
,
1625 .class1_alg_type
= OP_ALG_ALGSEL_AES
| OP_ALG_AAI_GCM
,
1629 /* Galois Counter Mode */
1633 .cra_name
= "gcm(aes)",
1634 .cra_driver_name
= "gcm-aes-caam-qi",
1637 .setkey
= gcm_setkey
,
1638 .setauthsize
= gcm_setauthsize
,
1639 .encrypt
= aead_encrypt
,
1640 .decrypt
= aead_decrypt
,
1642 .maxauthsize
= AES_BLOCK_SIZE
,
1645 .class1_alg_type
= OP_ALG_ALGSEL_AES
| OP_ALG_AAI_GCM
,
1649 /* single-pass ipsec_esp descriptor */
1653 .cra_name
= "authenc(hmac(md5),cbc(aes))",
1654 .cra_driver_name
= "authenc-hmac-md5-"
1656 .cra_blocksize
= AES_BLOCK_SIZE
,
1658 .setkey
= aead_setkey
,
1659 .setauthsize
= aead_setauthsize
,
1660 .encrypt
= aead_encrypt
,
1661 .decrypt
= aead_decrypt
,
1662 .ivsize
= AES_BLOCK_SIZE
,
1663 .maxauthsize
= MD5_DIGEST_SIZE
,
1666 .class1_alg_type
= OP_ALG_ALGSEL_AES
| OP_ALG_AAI_CBC
,
1667 .class2_alg_type
= OP_ALG_ALGSEL_MD5
|
1668 OP_ALG_AAI_HMAC_PRECOMP
,
1674 .cra_name
= "echainiv(authenc(hmac(md5),"
1676 .cra_driver_name
= "echainiv-authenc-hmac-md5-"
1678 .cra_blocksize
= AES_BLOCK_SIZE
,
1680 .setkey
= aead_setkey
,
1681 .setauthsize
= aead_setauthsize
,
1682 .encrypt
= aead_encrypt
,
1683 .decrypt
= aead_decrypt
,
1684 .ivsize
= AES_BLOCK_SIZE
,
1685 .maxauthsize
= MD5_DIGEST_SIZE
,
1688 .class1_alg_type
= OP_ALG_ALGSEL_AES
| OP_ALG_AAI_CBC
,
1689 .class2_alg_type
= OP_ALG_ALGSEL_MD5
|
1690 OP_ALG_AAI_HMAC_PRECOMP
,
1697 .cra_name
= "authenc(hmac(sha1),cbc(aes))",
1698 .cra_driver_name
= "authenc-hmac-sha1-"
1700 .cra_blocksize
= AES_BLOCK_SIZE
,
1702 .setkey
= aead_setkey
,
1703 .setauthsize
= aead_setauthsize
,
1704 .encrypt
= aead_encrypt
,
1705 .decrypt
= aead_decrypt
,
1706 .ivsize
= AES_BLOCK_SIZE
,
1707 .maxauthsize
= SHA1_DIGEST_SIZE
,
1710 .class1_alg_type
= OP_ALG_ALGSEL_AES
| OP_ALG_AAI_CBC
,
1711 .class2_alg_type
= OP_ALG_ALGSEL_SHA1
|
1712 OP_ALG_AAI_HMAC_PRECOMP
,
1718 .cra_name
= "echainiv(authenc(hmac(sha1),"
1720 .cra_driver_name
= "echainiv-authenc-"
1721 "hmac-sha1-cbc-aes-caam-qi",
1722 .cra_blocksize
= AES_BLOCK_SIZE
,
1724 .setkey
= aead_setkey
,
1725 .setauthsize
= aead_setauthsize
,
1726 .encrypt
= aead_encrypt
,
1727 .decrypt
= aead_decrypt
,
1728 .ivsize
= AES_BLOCK_SIZE
,
1729 .maxauthsize
= SHA1_DIGEST_SIZE
,
1732 .class1_alg_type
= OP_ALG_ALGSEL_AES
| OP_ALG_AAI_CBC
,
1733 .class2_alg_type
= OP_ALG_ALGSEL_SHA1
|
1734 OP_ALG_AAI_HMAC_PRECOMP
,
1741 .cra_name
= "authenc(hmac(sha224),cbc(aes))",
1742 .cra_driver_name
= "authenc-hmac-sha224-"
1744 .cra_blocksize
= AES_BLOCK_SIZE
,
1746 .setkey
= aead_setkey
,
1747 .setauthsize
= aead_setauthsize
,
1748 .encrypt
= aead_encrypt
,
1749 .decrypt
= aead_decrypt
,
1750 .ivsize
= AES_BLOCK_SIZE
,
1751 .maxauthsize
= SHA224_DIGEST_SIZE
,
1754 .class1_alg_type
= OP_ALG_ALGSEL_AES
| OP_ALG_AAI_CBC
,
1755 .class2_alg_type
= OP_ALG_ALGSEL_SHA224
|
1756 OP_ALG_AAI_HMAC_PRECOMP
,
1762 .cra_name
= "echainiv(authenc(hmac(sha224),"
1764 .cra_driver_name
= "echainiv-authenc-"
1765 "hmac-sha224-cbc-aes-caam-qi",
1766 .cra_blocksize
= AES_BLOCK_SIZE
,
1768 .setkey
= aead_setkey
,
1769 .setauthsize
= aead_setauthsize
,
1770 .encrypt
= aead_encrypt
,
1771 .decrypt
= aead_decrypt
,
1772 .ivsize
= AES_BLOCK_SIZE
,
1773 .maxauthsize
= SHA224_DIGEST_SIZE
,
1776 .class1_alg_type
= OP_ALG_ALGSEL_AES
| OP_ALG_AAI_CBC
,
1777 .class2_alg_type
= OP_ALG_ALGSEL_SHA224
|
1778 OP_ALG_AAI_HMAC_PRECOMP
,
1785 .cra_name
= "authenc(hmac(sha256),cbc(aes))",
1786 .cra_driver_name
= "authenc-hmac-sha256-"
1788 .cra_blocksize
= AES_BLOCK_SIZE
,
1790 .setkey
= aead_setkey
,
1791 .setauthsize
= aead_setauthsize
,
1792 .encrypt
= aead_encrypt
,
1793 .decrypt
= aead_decrypt
,
1794 .ivsize
= AES_BLOCK_SIZE
,
1795 .maxauthsize
= SHA256_DIGEST_SIZE
,
1798 .class1_alg_type
= OP_ALG_ALGSEL_AES
| OP_ALG_AAI_CBC
,
1799 .class2_alg_type
= OP_ALG_ALGSEL_SHA256
|
1800 OP_ALG_AAI_HMAC_PRECOMP
,
1806 .cra_name
= "echainiv(authenc(hmac(sha256),"
1808 .cra_driver_name
= "echainiv-authenc-"
1809 "hmac-sha256-cbc-aes-"
1811 .cra_blocksize
= AES_BLOCK_SIZE
,
1813 .setkey
= aead_setkey
,
1814 .setauthsize
= aead_setauthsize
,
1815 .encrypt
= aead_encrypt
,
1816 .decrypt
= aead_decrypt
,
1817 .ivsize
= AES_BLOCK_SIZE
,
1818 .maxauthsize
= SHA256_DIGEST_SIZE
,
1821 .class1_alg_type
= OP_ALG_ALGSEL_AES
| OP_ALG_AAI_CBC
,
1822 .class2_alg_type
= OP_ALG_ALGSEL_SHA256
|
1823 OP_ALG_AAI_HMAC_PRECOMP
,
1830 .cra_name
= "authenc(hmac(sha384),cbc(aes))",
1831 .cra_driver_name
= "authenc-hmac-sha384-"
1833 .cra_blocksize
= AES_BLOCK_SIZE
,
1835 .setkey
= aead_setkey
,
1836 .setauthsize
= aead_setauthsize
,
1837 .encrypt
= aead_encrypt
,
1838 .decrypt
= aead_decrypt
,
1839 .ivsize
= AES_BLOCK_SIZE
,
1840 .maxauthsize
= SHA384_DIGEST_SIZE
,
1843 .class1_alg_type
= OP_ALG_ALGSEL_AES
| OP_ALG_AAI_CBC
,
1844 .class2_alg_type
= OP_ALG_ALGSEL_SHA384
|
1845 OP_ALG_AAI_HMAC_PRECOMP
,
1851 .cra_name
= "echainiv(authenc(hmac(sha384),"
1853 .cra_driver_name
= "echainiv-authenc-"
1854 "hmac-sha384-cbc-aes-"
1856 .cra_blocksize
= AES_BLOCK_SIZE
,
1858 .setkey
= aead_setkey
,
1859 .setauthsize
= aead_setauthsize
,
1860 .encrypt
= aead_encrypt
,
1861 .decrypt
= aead_decrypt
,
1862 .ivsize
= AES_BLOCK_SIZE
,
1863 .maxauthsize
= SHA384_DIGEST_SIZE
,
1866 .class1_alg_type
= OP_ALG_ALGSEL_AES
| OP_ALG_AAI_CBC
,
1867 .class2_alg_type
= OP_ALG_ALGSEL_SHA384
|
1868 OP_ALG_AAI_HMAC_PRECOMP
,
1875 .cra_name
= "authenc(hmac(sha512),cbc(aes))",
1876 .cra_driver_name
= "authenc-hmac-sha512-"
1878 .cra_blocksize
= AES_BLOCK_SIZE
,
1880 .setkey
= aead_setkey
,
1881 .setauthsize
= aead_setauthsize
,
1882 .encrypt
= aead_encrypt
,
1883 .decrypt
= aead_decrypt
,
1884 .ivsize
= AES_BLOCK_SIZE
,
1885 .maxauthsize
= SHA512_DIGEST_SIZE
,
1888 .class1_alg_type
= OP_ALG_ALGSEL_AES
| OP_ALG_AAI_CBC
,
1889 .class2_alg_type
= OP_ALG_ALGSEL_SHA512
|
1890 OP_ALG_AAI_HMAC_PRECOMP
,
1896 .cra_name
= "echainiv(authenc(hmac(sha512),"
1898 .cra_driver_name
= "echainiv-authenc-"
1899 "hmac-sha512-cbc-aes-"
1901 .cra_blocksize
= AES_BLOCK_SIZE
,
1903 .setkey
= aead_setkey
,
1904 .setauthsize
= aead_setauthsize
,
1905 .encrypt
= aead_encrypt
,
1906 .decrypt
= aead_decrypt
,
1907 .ivsize
= AES_BLOCK_SIZE
,
1908 .maxauthsize
= SHA512_DIGEST_SIZE
,
1911 .class1_alg_type
= OP_ALG_ALGSEL_AES
| OP_ALG_AAI_CBC
,
1912 .class2_alg_type
= OP_ALG_ALGSEL_SHA512
|
1913 OP_ALG_AAI_HMAC_PRECOMP
,
1920 .cra_name
= "authenc(hmac(md5),cbc(des3_ede))",
1921 .cra_driver_name
= "authenc-hmac-md5-"
1922 "cbc-des3_ede-caam-qi",
1923 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
1925 .setkey
= des3_aead_setkey
,
1926 .setauthsize
= aead_setauthsize
,
1927 .encrypt
= aead_encrypt
,
1928 .decrypt
= aead_decrypt
,
1929 .ivsize
= DES3_EDE_BLOCK_SIZE
,
1930 .maxauthsize
= MD5_DIGEST_SIZE
,
1933 .class1_alg_type
= OP_ALG_ALGSEL_3DES
| OP_ALG_AAI_CBC
,
1934 .class2_alg_type
= OP_ALG_ALGSEL_MD5
|
1935 OP_ALG_AAI_HMAC_PRECOMP
,
1941 .cra_name
= "echainiv(authenc(hmac(md5),"
1943 .cra_driver_name
= "echainiv-authenc-hmac-md5-"
1944 "cbc-des3_ede-caam-qi",
1945 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
1947 .setkey
= des3_aead_setkey
,
1948 .setauthsize
= aead_setauthsize
,
1949 .encrypt
= aead_encrypt
,
1950 .decrypt
= aead_decrypt
,
1951 .ivsize
= DES3_EDE_BLOCK_SIZE
,
1952 .maxauthsize
= MD5_DIGEST_SIZE
,
1955 .class1_alg_type
= OP_ALG_ALGSEL_3DES
| OP_ALG_AAI_CBC
,
1956 .class2_alg_type
= OP_ALG_ALGSEL_MD5
|
1957 OP_ALG_AAI_HMAC_PRECOMP
,
1964 .cra_name
= "authenc(hmac(sha1),"
1966 .cra_driver_name
= "authenc-hmac-sha1-"
1967 "cbc-des3_ede-caam-qi",
1968 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
1970 .setkey
= des3_aead_setkey
,
1971 .setauthsize
= aead_setauthsize
,
1972 .encrypt
= aead_encrypt
,
1973 .decrypt
= aead_decrypt
,
1974 .ivsize
= DES3_EDE_BLOCK_SIZE
,
1975 .maxauthsize
= SHA1_DIGEST_SIZE
,
1978 .class1_alg_type
= OP_ALG_ALGSEL_3DES
| OP_ALG_AAI_CBC
,
1979 .class2_alg_type
= OP_ALG_ALGSEL_SHA1
|
1980 OP_ALG_AAI_HMAC_PRECOMP
,
1986 .cra_name
= "echainiv(authenc(hmac(sha1),"
1988 .cra_driver_name
= "echainiv-authenc-"
1990 "cbc-des3_ede-caam-qi",
1991 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
1993 .setkey
= des3_aead_setkey
,
1994 .setauthsize
= aead_setauthsize
,
1995 .encrypt
= aead_encrypt
,
1996 .decrypt
= aead_decrypt
,
1997 .ivsize
= DES3_EDE_BLOCK_SIZE
,
1998 .maxauthsize
= SHA1_DIGEST_SIZE
,
2001 .class1_alg_type
= OP_ALG_ALGSEL_3DES
| OP_ALG_AAI_CBC
,
2002 .class2_alg_type
= OP_ALG_ALGSEL_SHA1
|
2003 OP_ALG_AAI_HMAC_PRECOMP
,
2010 .cra_name
= "authenc(hmac(sha224),"
2012 .cra_driver_name
= "authenc-hmac-sha224-"
2013 "cbc-des3_ede-caam-qi",
2014 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2016 .setkey
= des3_aead_setkey
,
2017 .setauthsize
= aead_setauthsize
,
2018 .encrypt
= aead_encrypt
,
2019 .decrypt
= aead_decrypt
,
2020 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2021 .maxauthsize
= SHA224_DIGEST_SIZE
,
2024 .class1_alg_type
= OP_ALG_ALGSEL_3DES
| OP_ALG_AAI_CBC
,
2025 .class2_alg_type
= OP_ALG_ALGSEL_SHA224
|
2026 OP_ALG_AAI_HMAC_PRECOMP
,
2032 .cra_name
= "echainiv(authenc(hmac(sha224),"
2034 .cra_driver_name
= "echainiv-authenc-"
2036 "cbc-des3_ede-caam-qi",
2037 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2039 .setkey
= des3_aead_setkey
,
2040 .setauthsize
= aead_setauthsize
,
2041 .encrypt
= aead_encrypt
,
2042 .decrypt
= aead_decrypt
,
2043 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2044 .maxauthsize
= SHA224_DIGEST_SIZE
,
2047 .class1_alg_type
= OP_ALG_ALGSEL_3DES
| OP_ALG_AAI_CBC
,
2048 .class2_alg_type
= OP_ALG_ALGSEL_SHA224
|
2049 OP_ALG_AAI_HMAC_PRECOMP
,
2056 .cra_name
= "authenc(hmac(sha256),"
2058 .cra_driver_name
= "authenc-hmac-sha256-"
2059 "cbc-des3_ede-caam-qi",
2060 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2062 .setkey
= des3_aead_setkey
,
2063 .setauthsize
= aead_setauthsize
,
2064 .encrypt
= aead_encrypt
,
2065 .decrypt
= aead_decrypt
,
2066 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2067 .maxauthsize
= SHA256_DIGEST_SIZE
,
2070 .class1_alg_type
= OP_ALG_ALGSEL_3DES
| OP_ALG_AAI_CBC
,
2071 .class2_alg_type
= OP_ALG_ALGSEL_SHA256
|
2072 OP_ALG_AAI_HMAC_PRECOMP
,
2078 .cra_name
= "echainiv(authenc(hmac(sha256),"
2080 .cra_driver_name
= "echainiv-authenc-"
2082 "cbc-des3_ede-caam-qi",
2083 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2085 .setkey
= des3_aead_setkey
,
2086 .setauthsize
= aead_setauthsize
,
2087 .encrypt
= aead_encrypt
,
2088 .decrypt
= aead_decrypt
,
2089 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2090 .maxauthsize
= SHA256_DIGEST_SIZE
,
2093 .class1_alg_type
= OP_ALG_ALGSEL_3DES
| OP_ALG_AAI_CBC
,
2094 .class2_alg_type
= OP_ALG_ALGSEL_SHA256
|
2095 OP_ALG_AAI_HMAC_PRECOMP
,
2102 .cra_name
= "authenc(hmac(sha384),"
2104 .cra_driver_name
= "authenc-hmac-sha384-"
2105 "cbc-des3_ede-caam-qi",
2106 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2108 .setkey
= des3_aead_setkey
,
2109 .setauthsize
= aead_setauthsize
,
2110 .encrypt
= aead_encrypt
,
2111 .decrypt
= aead_decrypt
,
2112 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2113 .maxauthsize
= SHA384_DIGEST_SIZE
,
2116 .class1_alg_type
= OP_ALG_ALGSEL_3DES
| OP_ALG_AAI_CBC
,
2117 .class2_alg_type
= OP_ALG_ALGSEL_SHA384
|
2118 OP_ALG_AAI_HMAC_PRECOMP
,
2124 .cra_name
= "echainiv(authenc(hmac(sha384),"
2126 .cra_driver_name
= "echainiv-authenc-"
2128 "cbc-des3_ede-caam-qi",
2129 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2131 .setkey
= des3_aead_setkey
,
2132 .setauthsize
= aead_setauthsize
,
2133 .encrypt
= aead_encrypt
,
2134 .decrypt
= aead_decrypt
,
2135 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2136 .maxauthsize
= SHA384_DIGEST_SIZE
,
2139 .class1_alg_type
= OP_ALG_ALGSEL_3DES
| OP_ALG_AAI_CBC
,
2140 .class2_alg_type
= OP_ALG_ALGSEL_SHA384
|
2141 OP_ALG_AAI_HMAC_PRECOMP
,
2148 .cra_name
= "authenc(hmac(sha512),"
2150 .cra_driver_name
= "authenc-hmac-sha512-"
2151 "cbc-des3_ede-caam-qi",
2152 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2154 .setkey
= des3_aead_setkey
,
2155 .setauthsize
= aead_setauthsize
,
2156 .encrypt
= aead_encrypt
,
2157 .decrypt
= aead_decrypt
,
2158 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2159 .maxauthsize
= SHA512_DIGEST_SIZE
,
2162 .class1_alg_type
= OP_ALG_ALGSEL_3DES
| OP_ALG_AAI_CBC
,
2163 .class2_alg_type
= OP_ALG_ALGSEL_SHA512
|
2164 OP_ALG_AAI_HMAC_PRECOMP
,
2170 .cra_name
= "echainiv(authenc(hmac(sha512),"
2172 .cra_driver_name
= "echainiv-authenc-"
2174 "cbc-des3_ede-caam-qi",
2175 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2177 .setkey
= des3_aead_setkey
,
2178 .setauthsize
= aead_setauthsize
,
2179 .encrypt
= aead_encrypt
,
2180 .decrypt
= aead_decrypt
,
2181 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2182 .maxauthsize
= SHA512_DIGEST_SIZE
,
2185 .class1_alg_type
= OP_ALG_ALGSEL_3DES
| OP_ALG_AAI_CBC
,
2186 .class2_alg_type
= OP_ALG_ALGSEL_SHA512
|
2187 OP_ALG_AAI_HMAC_PRECOMP
,
2194 .cra_name
= "authenc(hmac(md5),cbc(des))",
2195 .cra_driver_name
= "authenc-hmac-md5-"
2197 .cra_blocksize
= DES_BLOCK_SIZE
,
2199 .setkey
= aead_setkey
,
2200 .setauthsize
= aead_setauthsize
,
2201 .encrypt
= aead_encrypt
,
2202 .decrypt
= aead_decrypt
,
2203 .ivsize
= DES_BLOCK_SIZE
,
2204 .maxauthsize
= MD5_DIGEST_SIZE
,
2207 .class1_alg_type
= OP_ALG_ALGSEL_DES
| OP_ALG_AAI_CBC
,
2208 .class2_alg_type
= OP_ALG_ALGSEL_MD5
|
2209 OP_ALG_AAI_HMAC_PRECOMP
,
2215 .cra_name
= "echainiv(authenc(hmac(md5),"
2217 .cra_driver_name
= "echainiv-authenc-hmac-md5-"
2219 .cra_blocksize
= DES_BLOCK_SIZE
,
2221 .setkey
= aead_setkey
,
2222 .setauthsize
= aead_setauthsize
,
2223 .encrypt
= aead_encrypt
,
2224 .decrypt
= aead_decrypt
,
2225 .ivsize
= DES_BLOCK_SIZE
,
2226 .maxauthsize
= MD5_DIGEST_SIZE
,
2229 .class1_alg_type
= OP_ALG_ALGSEL_DES
| OP_ALG_AAI_CBC
,
2230 .class2_alg_type
= OP_ALG_ALGSEL_MD5
|
2231 OP_ALG_AAI_HMAC_PRECOMP
,
2238 .cra_name
= "authenc(hmac(sha1),cbc(des))",
2239 .cra_driver_name
= "authenc-hmac-sha1-"
2241 .cra_blocksize
= DES_BLOCK_SIZE
,
2243 .setkey
= aead_setkey
,
2244 .setauthsize
= aead_setauthsize
,
2245 .encrypt
= aead_encrypt
,
2246 .decrypt
= aead_decrypt
,
2247 .ivsize
= DES_BLOCK_SIZE
,
2248 .maxauthsize
= SHA1_DIGEST_SIZE
,
2251 .class1_alg_type
= OP_ALG_ALGSEL_DES
| OP_ALG_AAI_CBC
,
2252 .class2_alg_type
= OP_ALG_ALGSEL_SHA1
|
2253 OP_ALG_AAI_HMAC_PRECOMP
,
2259 .cra_name
= "echainiv(authenc(hmac(sha1),"
2261 .cra_driver_name
= "echainiv-authenc-"
2262 "hmac-sha1-cbc-des-caam-qi",
2263 .cra_blocksize
= DES_BLOCK_SIZE
,
2265 .setkey
= aead_setkey
,
2266 .setauthsize
= aead_setauthsize
,
2267 .encrypt
= aead_encrypt
,
2268 .decrypt
= aead_decrypt
,
2269 .ivsize
= DES_BLOCK_SIZE
,
2270 .maxauthsize
= SHA1_DIGEST_SIZE
,
2273 .class1_alg_type
= OP_ALG_ALGSEL_DES
| OP_ALG_AAI_CBC
,
2274 .class2_alg_type
= OP_ALG_ALGSEL_SHA1
|
2275 OP_ALG_AAI_HMAC_PRECOMP
,
2282 .cra_name
= "authenc(hmac(sha224),cbc(des))",
2283 .cra_driver_name
= "authenc-hmac-sha224-"
2285 .cra_blocksize
= DES_BLOCK_SIZE
,
2287 .setkey
= aead_setkey
,
2288 .setauthsize
= aead_setauthsize
,
2289 .encrypt
= aead_encrypt
,
2290 .decrypt
= aead_decrypt
,
2291 .ivsize
= DES_BLOCK_SIZE
,
2292 .maxauthsize
= SHA224_DIGEST_SIZE
,
2295 .class1_alg_type
= OP_ALG_ALGSEL_DES
| OP_ALG_AAI_CBC
,
2296 .class2_alg_type
= OP_ALG_ALGSEL_SHA224
|
2297 OP_ALG_AAI_HMAC_PRECOMP
,
2303 .cra_name
= "echainiv(authenc(hmac(sha224),"
2305 .cra_driver_name
= "echainiv-authenc-"
2306 "hmac-sha224-cbc-des-"
2308 .cra_blocksize
= DES_BLOCK_SIZE
,
2310 .setkey
= aead_setkey
,
2311 .setauthsize
= aead_setauthsize
,
2312 .encrypt
= aead_encrypt
,
2313 .decrypt
= aead_decrypt
,
2314 .ivsize
= DES_BLOCK_SIZE
,
2315 .maxauthsize
= SHA224_DIGEST_SIZE
,
2318 .class1_alg_type
= OP_ALG_ALGSEL_DES
| OP_ALG_AAI_CBC
,
2319 .class2_alg_type
= OP_ALG_ALGSEL_SHA224
|
2320 OP_ALG_AAI_HMAC_PRECOMP
,
2327 .cra_name
= "authenc(hmac(sha256),cbc(des))",
2328 .cra_driver_name
= "authenc-hmac-sha256-"
2330 .cra_blocksize
= DES_BLOCK_SIZE
,
2332 .setkey
= aead_setkey
,
2333 .setauthsize
= aead_setauthsize
,
2334 .encrypt
= aead_encrypt
,
2335 .decrypt
= aead_decrypt
,
2336 .ivsize
= DES_BLOCK_SIZE
,
2337 .maxauthsize
= SHA256_DIGEST_SIZE
,
2340 .class1_alg_type
= OP_ALG_ALGSEL_DES
| OP_ALG_AAI_CBC
,
2341 .class2_alg_type
= OP_ALG_ALGSEL_SHA256
|
2342 OP_ALG_AAI_HMAC_PRECOMP
,
2348 .cra_name
= "echainiv(authenc(hmac(sha256),"
2350 .cra_driver_name
= "echainiv-authenc-"
2351 "hmac-sha256-cbc-des-"
2353 .cra_blocksize
= DES_BLOCK_SIZE
,
2355 .setkey
= aead_setkey
,
2356 .setauthsize
= aead_setauthsize
,
2357 .encrypt
= aead_encrypt
,
2358 .decrypt
= aead_decrypt
,
2359 .ivsize
= DES_BLOCK_SIZE
,
2360 .maxauthsize
= SHA256_DIGEST_SIZE
,
2363 .class1_alg_type
= OP_ALG_ALGSEL_DES
| OP_ALG_AAI_CBC
,
2364 .class2_alg_type
= OP_ALG_ALGSEL_SHA256
|
2365 OP_ALG_AAI_HMAC_PRECOMP
,
2372 .cra_name
= "authenc(hmac(sha384),cbc(des))",
2373 .cra_driver_name
= "authenc-hmac-sha384-"
2375 .cra_blocksize
= DES_BLOCK_SIZE
,
2377 .setkey
= aead_setkey
,
2378 .setauthsize
= aead_setauthsize
,
2379 .encrypt
= aead_encrypt
,
2380 .decrypt
= aead_decrypt
,
2381 .ivsize
= DES_BLOCK_SIZE
,
2382 .maxauthsize
= SHA384_DIGEST_SIZE
,
2385 .class1_alg_type
= OP_ALG_ALGSEL_DES
| OP_ALG_AAI_CBC
,
2386 .class2_alg_type
= OP_ALG_ALGSEL_SHA384
|
2387 OP_ALG_AAI_HMAC_PRECOMP
,
2393 .cra_name
= "echainiv(authenc(hmac(sha384),"
2395 .cra_driver_name
= "echainiv-authenc-"
2396 "hmac-sha384-cbc-des-"
2398 .cra_blocksize
= DES_BLOCK_SIZE
,
2400 .setkey
= aead_setkey
,
2401 .setauthsize
= aead_setauthsize
,
2402 .encrypt
= aead_encrypt
,
2403 .decrypt
= aead_decrypt
,
2404 .ivsize
= DES_BLOCK_SIZE
,
2405 .maxauthsize
= SHA384_DIGEST_SIZE
,
2408 .class1_alg_type
= OP_ALG_ALGSEL_DES
| OP_ALG_AAI_CBC
,
2409 .class2_alg_type
= OP_ALG_ALGSEL_SHA384
|
2410 OP_ALG_AAI_HMAC_PRECOMP
,
2417 .cra_name
= "authenc(hmac(sha512),cbc(des))",
2418 .cra_driver_name
= "authenc-hmac-sha512-"
2420 .cra_blocksize
= DES_BLOCK_SIZE
,
2422 .setkey
= aead_setkey
,
2423 .setauthsize
= aead_setauthsize
,
2424 .encrypt
= aead_encrypt
,
2425 .decrypt
= aead_decrypt
,
2426 .ivsize
= DES_BLOCK_SIZE
,
2427 .maxauthsize
= SHA512_DIGEST_SIZE
,
2430 .class1_alg_type
= OP_ALG_ALGSEL_DES
| OP_ALG_AAI_CBC
,
2431 .class2_alg_type
= OP_ALG_ALGSEL_SHA512
|
2432 OP_ALG_AAI_HMAC_PRECOMP
,
2438 .cra_name
= "echainiv(authenc(hmac(sha512),"
2440 .cra_driver_name
= "echainiv-authenc-"
2441 "hmac-sha512-cbc-des-"
2443 .cra_blocksize
= DES_BLOCK_SIZE
,
2445 .setkey
= aead_setkey
,
2446 .setauthsize
= aead_setauthsize
,
2447 .encrypt
= aead_encrypt
,
2448 .decrypt
= aead_decrypt
,
2449 .ivsize
= DES_BLOCK_SIZE
,
2450 .maxauthsize
= SHA512_DIGEST_SIZE
,
2453 .class1_alg_type
= OP_ALG_ALGSEL_DES
| OP_ALG_AAI_CBC
,
2454 .class2_alg_type
= OP_ALG_ALGSEL_SHA512
|
2455 OP_ALG_AAI_HMAC_PRECOMP
,
2461 static int caam_init_common(struct caam_ctx
*ctx
, struct caam_alg_entry
*caam
,
2464 struct caam_drv_private
*priv
;
2468 * distribute tfms across job rings to ensure in-order
2469 * crypto request processing per tfm
2471 ctx
->jrdev
= caam_jr_alloc();
2472 if (IS_ERR(ctx
->jrdev
)) {
2473 pr_err("Job Ring Device allocation for transform failed\n");
2474 return PTR_ERR(ctx
->jrdev
);
2477 dev
= ctx
->jrdev
->parent
;
2478 priv
= dev_get_drvdata(dev
);
2479 if (priv
->era
>= 6 && uses_dkp
)
2480 ctx
->dir
= DMA_BIDIRECTIONAL
;
2482 ctx
->dir
= DMA_TO_DEVICE
;
2484 ctx
->key_dma
= dma_map_single(dev
, ctx
->key
, sizeof(ctx
->key
),
2486 if (dma_mapping_error(dev
, ctx
->key_dma
)) {
2487 dev_err(dev
, "unable to map key\n");
2488 caam_jr_free(ctx
->jrdev
);
2492 /* copy descriptor header template value */
2493 ctx
->cdata
.algtype
= OP_TYPE_CLASS1_ALG
| caam
->class1_alg_type
;
2494 ctx
->adata
.algtype
= OP_TYPE_CLASS2_ALG
| caam
->class2_alg_type
;
2498 spin_lock_init(&ctx
->lock
);
2499 ctx
->drv_ctx
[ENCRYPT
] = NULL
;
2500 ctx
->drv_ctx
[DECRYPT
] = NULL
;
2505 static int caam_cra_init(struct crypto_skcipher
*tfm
)
2507 struct skcipher_alg
*alg
= crypto_skcipher_alg(tfm
);
2508 struct caam_skcipher_alg
*caam_alg
=
2509 container_of(alg
, typeof(*caam_alg
), skcipher
);
2510 struct caam_ctx
*ctx
= crypto_skcipher_ctx_dma(tfm
);
2511 u32 alg_aai
= caam_alg
->caam
.class1_alg_type
& OP_ALG_AAI_MASK
;
2514 if (alg_aai
== OP_ALG_AAI_XTS
) {
2515 const char *tfm_name
= crypto_tfm_alg_name(&tfm
->base
);
2516 struct crypto_skcipher
*fallback
;
2518 fallback
= crypto_alloc_skcipher(tfm_name
, 0,
2519 CRYPTO_ALG_NEED_FALLBACK
);
2520 if (IS_ERR(fallback
)) {
2521 pr_err("Failed to allocate %s fallback: %ld\n",
2522 tfm_name
, PTR_ERR(fallback
));
2523 return PTR_ERR(fallback
);
2526 ctx
->fallback
= fallback
;
2527 crypto_skcipher_set_reqsize(tfm
, sizeof(struct caam_skcipher_req_ctx
) +
2528 crypto_skcipher_reqsize(fallback
));
2531 ret
= caam_init_common(ctx
, &caam_alg
->caam
, false);
2532 if (ret
&& ctx
->fallback
)
2533 crypto_free_skcipher(ctx
->fallback
);
2538 static int caam_aead_init(struct crypto_aead
*tfm
)
2540 struct aead_alg
*alg
= crypto_aead_alg(tfm
);
2541 struct caam_aead_alg
*caam_alg
= container_of(alg
, typeof(*caam_alg
),
2543 struct caam_ctx
*ctx
= crypto_aead_ctx_dma(tfm
);
2545 return caam_init_common(ctx
, &caam_alg
->caam
, !caam_alg
->caam
.nodkp
);
2548 static void caam_exit_common(struct caam_ctx
*ctx
)
2550 caam_drv_ctx_rel(ctx
->drv_ctx
[ENCRYPT
]);
2551 caam_drv_ctx_rel(ctx
->drv_ctx
[DECRYPT
]);
2553 dma_unmap_single(ctx
->jrdev
->parent
, ctx
->key_dma
, sizeof(ctx
->key
),
2556 caam_jr_free(ctx
->jrdev
);
2559 static void caam_cra_exit(struct crypto_skcipher
*tfm
)
2561 struct caam_ctx
*ctx
= crypto_skcipher_ctx_dma(tfm
);
2564 crypto_free_skcipher(ctx
->fallback
);
2565 caam_exit_common(ctx
);
2568 static void caam_aead_exit(struct crypto_aead
*tfm
)
2570 caam_exit_common(crypto_aead_ctx_dma(tfm
));
2573 void caam_qi_algapi_exit(void)
2577 for (i
= 0; i
< ARRAY_SIZE(driver_aeads
); i
++) {
2578 struct caam_aead_alg
*t_alg
= driver_aeads
+ i
;
2580 if (t_alg
->registered
)
2581 crypto_unregister_aead(&t_alg
->aead
);
2584 for (i
= 0; i
< ARRAY_SIZE(driver_algs
); i
++) {
2585 struct caam_skcipher_alg
*t_alg
= driver_algs
+ i
;
2587 if (t_alg
->registered
)
2588 crypto_unregister_skcipher(&t_alg
->skcipher
);
2592 static void caam_skcipher_alg_init(struct caam_skcipher_alg
*t_alg
)
2594 struct skcipher_alg
*alg
= &t_alg
->skcipher
;
2596 alg
->base
.cra_module
= THIS_MODULE
;
2597 alg
->base
.cra_priority
= CAAM_CRA_PRIORITY
;
2598 alg
->base
.cra_ctxsize
= sizeof(struct caam_ctx
) + crypto_dma_padding();
2599 alg
->base
.cra_flags
|= (CRYPTO_ALG_ASYNC
| CRYPTO_ALG_ALLOCATES_MEMORY
|
2600 CRYPTO_ALG_KERN_DRIVER_ONLY
);
2602 alg
->init
= caam_cra_init
;
2603 alg
->exit
= caam_cra_exit
;
2606 static void caam_aead_alg_init(struct caam_aead_alg
*t_alg
)
2608 struct aead_alg
*alg
= &t_alg
->aead
;
2610 alg
->base
.cra_module
= THIS_MODULE
;
2611 alg
->base
.cra_priority
= CAAM_CRA_PRIORITY
;
2612 alg
->base
.cra_ctxsize
= sizeof(struct caam_ctx
) + crypto_dma_padding();
2613 alg
->base
.cra_flags
= CRYPTO_ALG_ASYNC
| CRYPTO_ALG_ALLOCATES_MEMORY
|
2614 CRYPTO_ALG_KERN_DRIVER_ONLY
;
2616 alg
->init
= caam_aead_init
;
2617 alg
->exit
= caam_aead_exit
;
2620 int caam_qi_algapi_init(struct device
*ctrldev
)
2622 struct caam_drv_private
*priv
= dev_get_drvdata(ctrldev
);
2624 u32 aes_vid
, aes_inst
, des_inst
, md_vid
, md_inst
;
2625 unsigned int md_limit
= SHA512_DIGEST_SIZE
;
2626 bool registered
= false;
2628 /* Make sure this runs only on (DPAA 1.x) QI */
2629 if (!priv
->qi_present
|| caam_dpaa2
)
2633 * Register crypto algorithms the device supports.
2634 * First, detect presence and attributes of DES, AES, and MD blocks.
2636 if (priv
->era
< 10) {
2637 u32 cha_vid
, cha_inst
;
2639 cha_vid
= rd_reg32(&priv
->ctrl
->perfmon
.cha_id_ls
);
2640 aes_vid
= cha_vid
& CHA_ID_LS_AES_MASK
;
2641 md_vid
= (cha_vid
& CHA_ID_LS_MD_MASK
) >> CHA_ID_LS_MD_SHIFT
;
2643 cha_inst
= rd_reg32(&priv
->ctrl
->perfmon
.cha_num_ls
);
2644 des_inst
= (cha_inst
& CHA_ID_LS_DES_MASK
) >>
2645 CHA_ID_LS_DES_SHIFT
;
2646 aes_inst
= cha_inst
& CHA_ID_LS_AES_MASK
;
2647 md_inst
= (cha_inst
& CHA_ID_LS_MD_MASK
) >> CHA_ID_LS_MD_SHIFT
;
2651 aesa
= rd_reg32(&priv
->ctrl
->vreg
.aesa
);
2652 mdha
= rd_reg32(&priv
->ctrl
->vreg
.mdha
);
2654 aes_vid
= (aesa
& CHA_VER_VID_MASK
) >> CHA_VER_VID_SHIFT
;
2655 md_vid
= (mdha
& CHA_VER_VID_MASK
) >> CHA_VER_VID_SHIFT
;
2657 des_inst
= rd_reg32(&priv
->ctrl
->vreg
.desa
) & CHA_VER_NUM_MASK
;
2658 aes_inst
= aesa
& CHA_VER_NUM_MASK
;
2659 md_inst
= mdha
& CHA_VER_NUM_MASK
;
2662 /* If MD is present, limit digest size based on LP256 */
2663 if (md_inst
&& md_vid
== CHA_VER_VID_MD_LP256
)
2664 md_limit
= SHA256_DIGEST_SIZE
;
2666 for (i
= 0; i
< ARRAY_SIZE(driver_algs
); i
++) {
2667 struct caam_skcipher_alg
*t_alg
= driver_algs
+ i
;
2668 u32 alg_sel
= t_alg
->caam
.class1_alg_type
& OP_ALG_ALGSEL_MASK
;
2670 /* Skip DES algorithms if not supported by device */
2672 ((alg_sel
== OP_ALG_ALGSEL_3DES
) ||
2673 (alg_sel
== OP_ALG_ALGSEL_DES
)))
2676 /* Skip AES algorithms if not supported by device */
2677 if (!aes_inst
&& (alg_sel
== OP_ALG_ALGSEL_AES
))
2680 caam_skcipher_alg_init(t_alg
);
2682 err
= crypto_register_skcipher(&t_alg
->skcipher
);
2684 dev_warn(ctrldev
, "%s alg registration failed\n",
2685 t_alg
->skcipher
.base
.cra_driver_name
);
2689 t_alg
->registered
= true;
2693 for (i
= 0; i
< ARRAY_SIZE(driver_aeads
); i
++) {
2694 struct caam_aead_alg
*t_alg
= driver_aeads
+ i
;
2695 u32 c1_alg_sel
= t_alg
->caam
.class1_alg_type
&
2697 u32 c2_alg_sel
= t_alg
->caam
.class2_alg_type
&
2699 u32 alg_aai
= t_alg
->caam
.class1_alg_type
& OP_ALG_AAI_MASK
;
2701 /* Skip DES algorithms if not supported by device */
2703 ((c1_alg_sel
== OP_ALG_ALGSEL_3DES
) ||
2704 (c1_alg_sel
== OP_ALG_ALGSEL_DES
)))
2707 /* Skip AES algorithms if not supported by device */
2708 if (!aes_inst
&& (c1_alg_sel
== OP_ALG_ALGSEL_AES
))
2712 * Check support for AES algorithms not available
2715 if (aes_vid
== CHA_VER_VID_AES_LP
&& alg_aai
== OP_ALG_AAI_GCM
)
2719 * Skip algorithms requiring message digests
2720 * if MD or MD size is not supported by device.
2723 (!md_inst
|| (t_alg
->aead
.maxauthsize
> md_limit
)))
2726 caam_aead_alg_init(t_alg
);
2728 err
= crypto_register_aead(&t_alg
->aead
);
2730 pr_warn("%s alg registration failed\n",
2731 t_alg
->aead
.base
.cra_driver_name
);
2735 t_alg
->registered
= true;
2740 dev_info(ctrldev
, "algorithms registered in /proc/crypto\n");