1 // SPDX-License-Identifier: GPL-2.0-only
3 * AMD Cryptographic Coprocessor (CCP) driver
5 * Copyright (C) 2013-2019 Advanced Micro Devices, Inc.
7 * Author: Tom Lendacky <thomas.lendacky@amd.com>
8 * Author: Gary R Hook <gary.hook@amd.com>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/interrupt.h>
14 #include <crypto/scatterwalk.h>
15 #include <crypto/des.h>
16 #include <linux/ccp.h>
20 /* SHA initial context values */
21 static const __be32 ccp_sha1_init
[SHA1_DIGEST_SIZE
/ sizeof(__be32
)] = {
22 cpu_to_be32(SHA1_H0
), cpu_to_be32(SHA1_H1
),
23 cpu_to_be32(SHA1_H2
), cpu_to_be32(SHA1_H3
),
27 static const __be32 ccp_sha224_init
[SHA256_DIGEST_SIZE
/ sizeof(__be32
)] = {
28 cpu_to_be32(SHA224_H0
), cpu_to_be32(SHA224_H1
),
29 cpu_to_be32(SHA224_H2
), cpu_to_be32(SHA224_H3
),
30 cpu_to_be32(SHA224_H4
), cpu_to_be32(SHA224_H5
),
31 cpu_to_be32(SHA224_H6
), cpu_to_be32(SHA224_H7
),
34 static const __be32 ccp_sha256_init
[SHA256_DIGEST_SIZE
/ sizeof(__be32
)] = {
35 cpu_to_be32(SHA256_H0
), cpu_to_be32(SHA256_H1
),
36 cpu_to_be32(SHA256_H2
), cpu_to_be32(SHA256_H3
),
37 cpu_to_be32(SHA256_H4
), cpu_to_be32(SHA256_H5
),
38 cpu_to_be32(SHA256_H6
), cpu_to_be32(SHA256_H7
),
41 static const __be64 ccp_sha384_init
[SHA512_DIGEST_SIZE
/ sizeof(__be64
)] = {
42 cpu_to_be64(SHA384_H0
), cpu_to_be64(SHA384_H1
),
43 cpu_to_be64(SHA384_H2
), cpu_to_be64(SHA384_H3
),
44 cpu_to_be64(SHA384_H4
), cpu_to_be64(SHA384_H5
),
45 cpu_to_be64(SHA384_H6
), cpu_to_be64(SHA384_H7
),
48 static const __be64 ccp_sha512_init
[SHA512_DIGEST_SIZE
/ sizeof(__be64
)] = {
49 cpu_to_be64(SHA512_H0
), cpu_to_be64(SHA512_H1
),
50 cpu_to_be64(SHA512_H2
), cpu_to_be64(SHA512_H3
),
51 cpu_to_be64(SHA512_H4
), cpu_to_be64(SHA512_H5
),
52 cpu_to_be64(SHA512_H6
), cpu_to_be64(SHA512_H7
),
55 #define CCP_NEW_JOBID(ccp) ((ccp->vdata->version == CCP_VERSION(3, 0)) ? \
56 ccp_gen_jobid(ccp) : 0)
58 static u32
ccp_gen_jobid(struct ccp_device
*ccp
)
60 return atomic_inc_return(&ccp
->current_id
) & CCP_JOBID_MASK
;
63 static void ccp_sg_free(struct ccp_sg_workarea
*wa
)
66 dma_unmap_sg(wa
->dma_dev
, wa
->dma_sg
, wa
->nents
, wa
->dma_dir
);
71 static int ccp_init_sg_workarea(struct ccp_sg_workarea
*wa
, struct device
*dev
,
72 struct scatterlist
*sg
, u64 len
,
73 enum dma_data_direction dma_dir
)
75 memset(wa
, 0, sizeof(*wa
));
81 wa
->nents
= sg_nents_for_len(sg
, len
);
91 if (dma_dir
== DMA_NONE
)
96 wa
->dma_dir
= dma_dir
;
97 wa
->dma_count
= dma_map_sg(dev
, sg
, wa
->nents
, dma_dir
);
104 static void ccp_update_sg_workarea(struct ccp_sg_workarea
*wa
, unsigned int len
)
106 unsigned int nbytes
= min_t(u64
, len
, wa
->bytes_left
);
111 wa
->sg_used
+= nbytes
;
112 wa
->bytes_left
-= nbytes
;
113 if (wa
->sg_used
== wa
->sg
->length
) {
114 wa
->sg
= sg_next(wa
->sg
);
119 static void ccp_dm_free(struct ccp_dm_workarea
*wa
)
121 if (wa
->length
<= CCP_DMAPOOL_MAX_SIZE
) {
123 dma_pool_free(wa
->dma_pool
, wa
->address
,
127 dma_unmap_single(wa
->dev
, wa
->dma
.address
, wa
->length
,
136 static int ccp_init_dm_workarea(struct ccp_dm_workarea
*wa
,
137 struct ccp_cmd_queue
*cmd_q
,
139 enum dma_data_direction dir
)
141 memset(wa
, 0, sizeof(*wa
));
146 wa
->dev
= cmd_q
->ccp
->dev
;
149 if (len
<= CCP_DMAPOOL_MAX_SIZE
) {
150 wa
->dma_pool
= cmd_q
->dma_pool
;
152 wa
->address
= dma_pool_zalloc(wa
->dma_pool
, GFP_KERNEL
,
157 wa
->dma
.length
= CCP_DMAPOOL_MAX_SIZE
;
160 wa
->address
= kzalloc(len
, GFP_KERNEL
);
164 wa
->dma
.address
= dma_map_single(wa
->dev
, wa
->address
, len
,
166 if (dma_mapping_error(wa
->dev
, wa
->dma
.address
))
169 wa
->dma
.length
= len
;
176 static int ccp_set_dm_area(struct ccp_dm_workarea
*wa
, unsigned int wa_offset
,
177 struct scatterlist
*sg
, unsigned int sg_offset
,
180 WARN_ON(!wa
->address
);
182 if (len
> (wa
->length
- wa_offset
))
185 scatterwalk_map_and_copy(wa
->address
+ wa_offset
, sg
, sg_offset
, len
,
190 static void ccp_get_dm_area(struct ccp_dm_workarea
*wa
, unsigned int wa_offset
,
191 struct scatterlist
*sg
, unsigned int sg_offset
,
194 WARN_ON(!wa
->address
);
196 scatterwalk_map_and_copy(wa
->address
+ wa_offset
, sg
, sg_offset
, len
,
200 static int ccp_reverse_set_dm_area(struct ccp_dm_workarea
*wa
,
201 unsigned int wa_offset
,
202 struct scatterlist
*sg
,
203 unsigned int sg_offset
,
209 rc
= ccp_set_dm_area(wa
, wa_offset
, sg
, sg_offset
, len
);
213 p
= wa
->address
+ wa_offset
;
225 static void ccp_reverse_get_dm_area(struct ccp_dm_workarea
*wa
,
226 unsigned int wa_offset
,
227 struct scatterlist
*sg
,
228 unsigned int sg_offset
,
233 p
= wa
->address
+ wa_offset
;
243 ccp_get_dm_area(wa
, wa_offset
, sg
, sg_offset
, len
);
246 static void ccp_free_data(struct ccp_data
*data
, struct ccp_cmd_queue
*cmd_q
)
248 ccp_dm_free(&data
->dm_wa
);
249 ccp_sg_free(&data
->sg_wa
);
252 static int ccp_init_data(struct ccp_data
*data
, struct ccp_cmd_queue
*cmd_q
,
253 struct scatterlist
*sg
, u64 sg_len
,
255 enum dma_data_direction dir
)
259 memset(data
, 0, sizeof(*data
));
261 ret
= ccp_init_sg_workarea(&data
->sg_wa
, cmd_q
->ccp
->dev
, sg
, sg_len
,
266 ret
= ccp_init_dm_workarea(&data
->dm_wa
, cmd_q
, dm_len
, dir
);
273 ccp_free_data(data
, cmd_q
);
278 static unsigned int ccp_queue_buf(struct ccp_data
*data
, unsigned int from
)
280 struct ccp_sg_workarea
*sg_wa
= &data
->sg_wa
;
281 struct ccp_dm_workarea
*dm_wa
= &data
->dm_wa
;
282 unsigned int buf_count
, nbytes
;
284 /* Clear the buffer if setting it */
286 memset(dm_wa
->address
, 0, dm_wa
->length
);
291 /* Perform the copy operation
292 * nbytes will always be <= UINT_MAX because dm_wa->length is
295 nbytes
= min_t(u64
, sg_wa
->bytes_left
, dm_wa
->length
);
296 scatterwalk_map_and_copy(dm_wa
->address
, sg_wa
->sg
, sg_wa
->sg_used
,
299 /* Update the structures and generate the count */
301 while (sg_wa
->bytes_left
&& (buf_count
< dm_wa
->length
)) {
302 nbytes
= min(sg_wa
->sg
->length
- sg_wa
->sg_used
,
303 dm_wa
->length
- buf_count
);
304 nbytes
= min_t(u64
, sg_wa
->bytes_left
, nbytes
);
307 ccp_update_sg_workarea(sg_wa
, nbytes
);
313 static unsigned int ccp_fill_queue_buf(struct ccp_data
*data
)
315 return ccp_queue_buf(data
, 0);
318 static unsigned int ccp_empty_queue_buf(struct ccp_data
*data
)
320 return ccp_queue_buf(data
, 1);
323 static void ccp_prepare_data(struct ccp_data
*src
, struct ccp_data
*dst
,
324 struct ccp_op
*op
, unsigned int block_size
,
327 unsigned int sg_src_len
, sg_dst_len
, op_len
;
329 /* The CCP can only DMA from/to one address each per operation. This
330 * requires that we find the smallest DMA area between the source
331 * and destination. The resulting len values will always be <= UINT_MAX
332 * because the dma length is an unsigned int.
334 sg_src_len
= sg_dma_len(src
->sg_wa
.sg
) - src
->sg_wa
.sg_used
;
335 sg_src_len
= min_t(u64
, src
->sg_wa
.bytes_left
, sg_src_len
);
338 sg_dst_len
= sg_dma_len(dst
->sg_wa
.sg
) - dst
->sg_wa
.sg_used
;
339 sg_dst_len
= min_t(u64
, src
->sg_wa
.bytes_left
, sg_dst_len
);
340 op_len
= min(sg_src_len
, sg_dst_len
);
345 /* The data operation length will be at least block_size in length
346 * or the smaller of available sg room remaining for the source or
349 op_len
= max(op_len
, block_size
);
351 /* Unless we have to buffer data, there's no reason to wait */
354 if (sg_src_len
< block_size
) {
355 /* Not enough data in the sg element, so it
356 * needs to be buffered into a blocksize chunk
358 int cp_len
= ccp_fill_queue_buf(src
);
361 op
->src
.u
.dma
.address
= src
->dm_wa
.dma
.address
;
362 op
->src
.u
.dma
.offset
= 0;
363 op
->src
.u
.dma
.length
= (blocksize_op
) ? block_size
: cp_len
;
365 /* Enough data in the sg element, but we need to
366 * adjust for any previously copied data
368 op
->src
.u
.dma
.address
= sg_dma_address(src
->sg_wa
.sg
);
369 op
->src
.u
.dma
.offset
= src
->sg_wa
.sg_used
;
370 op
->src
.u
.dma
.length
= op_len
& ~(block_size
- 1);
372 ccp_update_sg_workarea(&src
->sg_wa
, op
->src
.u
.dma
.length
);
376 if (sg_dst_len
< block_size
) {
377 /* Not enough room in the sg element or we're on the
378 * last piece of data (when using padding), so the
379 * output needs to be buffered into a blocksize chunk
382 op
->dst
.u
.dma
.address
= dst
->dm_wa
.dma
.address
;
383 op
->dst
.u
.dma
.offset
= 0;
384 op
->dst
.u
.dma
.length
= op
->src
.u
.dma
.length
;
386 /* Enough room in the sg element, but we need to
387 * adjust for any previously used area
389 op
->dst
.u
.dma
.address
= sg_dma_address(dst
->sg_wa
.sg
);
390 op
->dst
.u
.dma
.offset
= dst
->sg_wa
.sg_used
;
391 op
->dst
.u
.dma
.length
= op
->src
.u
.dma
.length
;
396 static void ccp_process_data(struct ccp_data
*src
, struct ccp_data
*dst
,
402 if (op
->dst
.u
.dma
.address
== dst
->dm_wa
.dma
.address
)
403 ccp_empty_queue_buf(dst
);
405 ccp_update_sg_workarea(&dst
->sg_wa
,
406 op
->dst
.u
.dma
.length
);
410 static int ccp_copy_to_from_sb(struct ccp_cmd_queue
*cmd_q
,
411 struct ccp_dm_workarea
*wa
, u32 jobid
, u32 sb
,
412 u32 byte_swap
, bool from
)
416 memset(&op
, 0, sizeof(op
));
424 op
.src
.type
= CCP_MEMTYPE_SB
;
426 op
.dst
.type
= CCP_MEMTYPE_SYSTEM
;
427 op
.dst
.u
.dma
.address
= wa
->dma
.address
;
428 op
.dst
.u
.dma
.length
= wa
->length
;
430 op
.src
.type
= CCP_MEMTYPE_SYSTEM
;
431 op
.src
.u
.dma
.address
= wa
->dma
.address
;
432 op
.src
.u
.dma
.length
= wa
->length
;
433 op
.dst
.type
= CCP_MEMTYPE_SB
;
437 op
.u
.passthru
.byte_swap
= byte_swap
;
439 return cmd_q
->ccp
->vdata
->perform
->passthru(&op
);
442 static int ccp_copy_to_sb(struct ccp_cmd_queue
*cmd_q
,
443 struct ccp_dm_workarea
*wa
, u32 jobid
, u32 sb
,
446 return ccp_copy_to_from_sb(cmd_q
, wa
, jobid
, sb
, byte_swap
, false);
449 static int ccp_copy_from_sb(struct ccp_cmd_queue
*cmd_q
,
450 struct ccp_dm_workarea
*wa
, u32 jobid
, u32 sb
,
453 return ccp_copy_to_from_sb(cmd_q
, wa
, jobid
, sb
, byte_swap
, true);
456 static noinline_for_stack
int
457 ccp_run_aes_cmac_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
459 struct ccp_aes_engine
*aes
= &cmd
->u
.aes
;
460 struct ccp_dm_workarea key
, ctx
;
463 unsigned int dm_offset
;
466 if (!((aes
->key_len
== AES_KEYSIZE_128
) ||
467 (aes
->key_len
== AES_KEYSIZE_192
) ||
468 (aes
->key_len
== AES_KEYSIZE_256
)))
471 if (aes
->src_len
& (AES_BLOCK_SIZE
- 1))
474 if (aes
->iv_len
!= AES_BLOCK_SIZE
)
477 if (!aes
->key
|| !aes
->iv
|| !aes
->src
)
480 if (aes
->cmac_final
) {
481 if (aes
->cmac_key_len
!= AES_BLOCK_SIZE
)
488 BUILD_BUG_ON(CCP_AES_KEY_SB_COUNT
!= 1);
489 BUILD_BUG_ON(CCP_AES_CTX_SB_COUNT
!= 1);
492 memset(&op
, 0, sizeof(op
));
494 op
.jobid
= CCP_NEW_JOBID(cmd_q
->ccp
);
495 op
.sb_key
= cmd_q
->sb_key
;
496 op
.sb_ctx
= cmd_q
->sb_ctx
;
498 op
.u
.aes
.type
= aes
->type
;
499 op
.u
.aes
.mode
= aes
->mode
;
500 op
.u
.aes
.action
= aes
->action
;
502 /* All supported key sizes fit in a single (32-byte) SB entry
503 * and must be in little endian format. Use the 256-bit byte
504 * swap passthru option to convert from big endian to little
507 ret
= ccp_init_dm_workarea(&key
, cmd_q
,
508 CCP_AES_KEY_SB_COUNT
* CCP_SB_BYTES
,
513 dm_offset
= CCP_SB_BYTES
- aes
->key_len
;
514 ret
= ccp_set_dm_area(&key
, dm_offset
, aes
->key
, 0, aes
->key_len
);
517 ret
= ccp_copy_to_sb(cmd_q
, &key
, op
.jobid
, op
.sb_key
,
518 CCP_PASSTHRU_BYTESWAP_256BIT
);
520 cmd
->engine_error
= cmd_q
->cmd_error
;
524 /* The AES context fits in a single (32-byte) SB entry and
525 * must be in little endian format. Use the 256-bit byte swap
526 * passthru option to convert from big endian to little endian.
528 ret
= ccp_init_dm_workarea(&ctx
, cmd_q
,
529 CCP_AES_CTX_SB_COUNT
* CCP_SB_BYTES
,
534 dm_offset
= CCP_SB_BYTES
- AES_BLOCK_SIZE
;
535 ret
= ccp_set_dm_area(&ctx
, dm_offset
, aes
->iv
, 0, aes
->iv_len
);
538 ret
= ccp_copy_to_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
539 CCP_PASSTHRU_BYTESWAP_256BIT
);
541 cmd
->engine_error
= cmd_q
->cmd_error
;
545 /* Send data to the CCP AES engine */
546 ret
= ccp_init_data(&src
, cmd_q
, aes
->src
, aes
->src_len
,
547 AES_BLOCK_SIZE
, DMA_TO_DEVICE
);
551 while (src
.sg_wa
.bytes_left
) {
552 ccp_prepare_data(&src
, NULL
, &op
, AES_BLOCK_SIZE
, true);
553 if (aes
->cmac_final
&& !src
.sg_wa
.bytes_left
) {
556 /* Push the K1/K2 key to the CCP now */
557 ret
= ccp_copy_from_sb(cmd_q
, &ctx
, op
.jobid
,
559 CCP_PASSTHRU_BYTESWAP_256BIT
);
561 cmd
->engine_error
= cmd_q
->cmd_error
;
565 ret
= ccp_set_dm_area(&ctx
, 0, aes
->cmac_key
, 0,
569 ret
= ccp_copy_to_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
570 CCP_PASSTHRU_BYTESWAP_256BIT
);
572 cmd
->engine_error
= cmd_q
->cmd_error
;
577 ret
= cmd_q
->ccp
->vdata
->perform
->aes(&op
);
579 cmd
->engine_error
= cmd_q
->cmd_error
;
583 ccp_process_data(&src
, NULL
, &op
);
586 /* Retrieve the AES context - convert from LE to BE using
587 * 32-byte (256-bit) byteswapping
589 ret
= ccp_copy_from_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
590 CCP_PASSTHRU_BYTESWAP_256BIT
);
592 cmd
->engine_error
= cmd_q
->cmd_error
;
596 /* ...but we only need AES_BLOCK_SIZE bytes */
597 dm_offset
= CCP_SB_BYTES
- AES_BLOCK_SIZE
;
598 ccp_get_dm_area(&ctx
, dm_offset
, aes
->iv
, 0, aes
->iv_len
);
601 ccp_free_data(&src
, cmd_q
);
612 static noinline_for_stack
int
613 ccp_run_aes_gcm_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
615 struct ccp_aes_engine
*aes
= &cmd
->u
.aes
;
616 struct ccp_dm_workarea key
, ctx
, final_wa
, tag
;
617 struct ccp_data src
, dst
;
621 unsigned long long *final
;
622 unsigned int dm_offset
;
623 unsigned int authsize
;
626 bool in_place
= true; /* Default value */
629 struct scatterlist
*p_inp
, sg_inp
[2];
630 struct scatterlist
*p_tag
, sg_tag
[2];
631 struct scatterlist
*p_outp
, sg_outp
[2];
632 struct scatterlist
*p_aad
;
637 if (!((aes
->key_len
== AES_KEYSIZE_128
) ||
638 (aes
->key_len
== AES_KEYSIZE_192
) ||
639 (aes
->key_len
== AES_KEYSIZE_256
)))
642 if (!aes
->key
) /* Gotta have a key SGL */
645 /* Zero defaults to 16 bytes, the maximum size */
646 authsize
= aes
->authsize
? aes
->authsize
: AES_BLOCK_SIZE
;
660 /* First, decompose the source buffer into AAD & PT,
661 * and the destination buffer into AAD, CT & tag, or
662 * the input into CT & tag.
663 * It is expected that the input and output SGs will
664 * be valid, even if the AAD and input lengths are 0.
667 p_inp
= scatterwalk_ffwd(sg_inp
, aes
->src
, aes
->aad_len
);
668 p_outp
= scatterwalk_ffwd(sg_outp
, aes
->dst
, aes
->aad_len
);
669 if (aes
->action
== CCP_AES_ACTION_ENCRYPT
) {
671 p_tag
= scatterwalk_ffwd(sg_tag
, p_outp
, ilen
);
673 /* Input length for decryption includes tag */
674 ilen
= aes
->src_len
- authsize
;
675 p_tag
= scatterwalk_ffwd(sg_tag
, p_inp
, ilen
);
678 jobid
= CCP_NEW_JOBID(cmd_q
->ccp
);
680 memset(&op
, 0, sizeof(op
));
683 op
.sb_key
= cmd_q
->sb_key
; /* Pre-allocated */
684 op
.sb_ctx
= cmd_q
->sb_ctx
; /* Pre-allocated */
686 op
.u
.aes
.type
= aes
->type
;
688 /* Copy the key to the LSB */
689 ret
= ccp_init_dm_workarea(&key
, cmd_q
,
690 CCP_AES_CTX_SB_COUNT
* CCP_SB_BYTES
,
695 dm_offset
= CCP_SB_BYTES
- aes
->key_len
;
696 ret
= ccp_set_dm_area(&key
, dm_offset
, aes
->key
, 0, aes
->key_len
);
699 ret
= ccp_copy_to_sb(cmd_q
, &key
, op
.jobid
, op
.sb_key
,
700 CCP_PASSTHRU_BYTESWAP_256BIT
);
702 cmd
->engine_error
= cmd_q
->cmd_error
;
706 /* Copy the context (IV) to the LSB.
707 * There is an assumption here that the IV is 96 bits in length, plus
708 * a nonce of 32 bits. If no IV is present, use a zeroed buffer.
710 ret
= ccp_init_dm_workarea(&ctx
, cmd_q
,
711 CCP_AES_CTX_SB_COUNT
* CCP_SB_BYTES
,
716 dm_offset
= CCP_AES_CTX_SB_COUNT
* CCP_SB_BYTES
- aes
->iv_len
;
717 ret
= ccp_set_dm_area(&ctx
, dm_offset
, aes
->iv
, 0, aes
->iv_len
);
721 ret
= ccp_copy_to_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
722 CCP_PASSTHRU_BYTESWAP_256BIT
);
724 cmd
->engine_error
= cmd_q
->cmd_error
;
729 if (aes
->aad_len
> 0) {
730 /* Step 1: Run a GHASH over the Additional Authenticated Data */
731 ret
= ccp_init_data(&aad
, cmd_q
, p_aad
, aes
->aad_len
,
737 op
.u
.aes
.mode
= CCP_AES_MODE_GHASH
;
738 op
.u
.aes
.action
= CCP_AES_GHASHAAD
;
740 while (aad
.sg_wa
.bytes_left
) {
741 ccp_prepare_data(&aad
, NULL
, &op
, AES_BLOCK_SIZE
, true);
743 ret
= cmd_q
->ccp
->vdata
->perform
->aes(&op
);
745 cmd
->engine_error
= cmd_q
->cmd_error
;
749 ccp_process_data(&aad
, NULL
, &op
);
754 op
.u
.aes
.mode
= CCP_AES_MODE_GCTR
;
755 op
.u
.aes
.action
= aes
->action
;
758 /* Step 2: Run a GCTR over the plaintext */
759 in_place
= (sg_virt(p_inp
) == sg_virt(p_outp
)) ? true : false;
761 ret
= ccp_init_data(&src
, cmd_q
, p_inp
, ilen
,
763 in_place
? DMA_BIDIRECTIONAL
771 ret
= ccp_init_data(&dst
, cmd_q
, p_outp
, ilen
,
772 AES_BLOCK_SIZE
, DMA_FROM_DEVICE
);
780 while (src
.sg_wa
.bytes_left
) {
781 ccp_prepare_data(&src
, &dst
, &op
, AES_BLOCK_SIZE
, true);
782 if (!src
.sg_wa
.bytes_left
) {
783 unsigned int nbytes
= ilen
% AES_BLOCK_SIZE
;
787 op
.u
.aes
.size
= (nbytes
* 8) - 1;
791 ret
= cmd_q
->ccp
->vdata
->perform
->aes(&op
);
793 cmd
->engine_error
= cmd_q
->cmd_error
;
797 ccp_process_data(&src
, &dst
, &op
);
802 /* Step 3: Update the IV portion of the context with the original IV */
803 ret
= ccp_copy_from_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
804 CCP_PASSTHRU_BYTESWAP_256BIT
);
806 cmd
->engine_error
= cmd_q
->cmd_error
;
810 ret
= ccp_set_dm_area(&ctx
, dm_offset
, aes
->iv
, 0, aes
->iv_len
);
814 ret
= ccp_copy_to_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
815 CCP_PASSTHRU_BYTESWAP_256BIT
);
817 cmd
->engine_error
= cmd_q
->cmd_error
;
821 /* Step 4: Concatenate the lengths of the AAD and source, and
822 * hash that 16 byte buffer.
824 ret
= ccp_init_dm_workarea(&final_wa
, cmd_q
, AES_BLOCK_SIZE
,
828 final
= (unsigned long long *) final_wa
.address
;
829 final
[0] = cpu_to_be64(aes
->aad_len
* 8);
830 final
[1] = cpu_to_be64(ilen
* 8);
832 memset(&op
, 0, sizeof(op
));
835 op
.sb_key
= cmd_q
->sb_key
; /* Pre-allocated */
836 op
.sb_ctx
= cmd_q
->sb_ctx
; /* Pre-allocated */
838 op
.u
.aes
.type
= aes
->type
;
839 op
.u
.aes
.mode
= CCP_AES_MODE_GHASH
;
840 op
.u
.aes
.action
= CCP_AES_GHASHFINAL
;
841 op
.src
.type
= CCP_MEMTYPE_SYSTEM
;
842 op
.src
.u
.dma
.address
= final_wa
.dma
.address
;
843 op
.src
.u
.dma
.length
= AES_BLOCK_SIZE
;
844 op
.dst
.type
= CCP_MEMTYPE_SYSTEM
;
845 op
.dst
.u
.dma
.address
= final_wa
.dma
.address
;
846 op
.dst
.u
.dma
.length
= AES_BLOCK_SIZE
;
849 ret
= cmd_q
->ccp
->vdata
->perform
->aes(&op
);
853 if (aes
->action
== CCP_AES_ACTION_ENCRYPT
) {
854 /* Put the ciphered tag after the ciphertext. */
855 ccp_get_dm_area(&final_wa
, 0, p_tag
, 0, authsize
);
857 /* Does this ciphered tag match the input? */
858 ret
= ccp_init_dm_workarea(&tag
, cmd_q
, authsize
,
862 ret
= ccp_set_dm_area(&tag
, 0, p_tag
, 0, authsize
);
866 ret
= crypto_memneq(tag
.address
, final_wa
.address
,
867 authsize
) ? -EBADMSG
: 0;
872 ccp_dm_free(&final_wa
);
875 if (ilen
> 0 && !in_place
)
876 ccp_free_data(&dst
, cmd_q
);
880 ccp_free_data(&src
, cmd_q
);
884 ccp_free_data(&aad
, cmd_q
);
895 static noinline_for_stack
int
896 ccp_run_aes_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
898 struct ccp_aes_engine
*aes
= &cmd
->u
.aes
;
899 struct ccp_dm_workarea key
, ctx
;
900 struct ccp_data src
, dst
;
902 unsigned int dm_offset
;
903 bool in_place
= false;
906 if (!((aes
->key_len
== AES_KEYSIZE_128
) ||
907 (aes
->key_len
== AES_KEYSIZE_192
) ||
908 (aes
->key_len
== AES_KEYSIZE_256
)))
911 if (((aes
->mode
== CCP_AES_MODE_ECB
) ||
912 (aes
->mode
== CCP_AES_MODE_CBC
)) &&
913 (aes
->src_len
& (AES_BLOCK_SIZE
- 1)))
916 if (!aes
->key
|| !aes
->src
|| !aes
->dst
)
919 if (aes
->mode
!= CCP_AES_MODE_ECB
) {
920 if (aes
->iv_len
!= AES_BLOCK_SIZE
)
927 BUILD_BUG_ON(CCP_AES_KEY_SB_COUNT
!= 1);
928 BUILD_BUG_ON(CCP_AES_CTX_SB_COUNT
!= 1);
931 memset(&op
, 0, sizeof(op
));
933 op
.jobid
= CCP_NEW_JOBID(cmd_q
->ccp
);
934 op
.sb_key
= cmd_q
->sb_key
;
935 op
.sb_ctx
= cmd_q
->sb_ctx
;
936 op
.init
= (aes
->mode
== CCP_AES_MODE_ECB
) ? 0 : 1;
937 op
.u
.aes
.type
= aes
->type
;
938 op
.u
.aes
.mode
= aes
->mode
;
939 op
.u
.aes
.action
= aes
->action
;
941 /* All supported key sizes fit in a single (32-byte) SB entry
942 * and must be in little endian format. Use the 256-bit byte
943 * swap passthru option to convert from big endian to little
946 ret
= ccp_init_dm_workarea(&key
, cmd_q
,
947 CCP_AES_KEY_SB_COUNT
* CCP_SB_BYTES
,
952 dm_offset
= CCP_SB_BYTES
- aes
->key_len
;
953 ret
= ccp_set_dm_area(&key
, dm_offset
, aes
->key
, 0, aes
->key_len
);
956 ret
= ccp_copy_to_sb(cmd_q
, &key
, op
.jobid
, op
.sb_key
,
957 CCP_PASSTHRU_BYTESWAP_256BIT
);
959 cmd
->engine_error
= cmd_q
->cmd_error
;
963 /* The AES context fits in a single (32-byte) SB entry and
964 * must be in little endian format. Use the 256-bit byte swap
965 * passthru option to convert from big endian to little endian.
967 ret
= ccp_init_dm_workarea(&ctx
, cmd_q
,
968 CCP_AES_CTX_SB_COUNT
* CCP_SB_BYTES
,
973 if (aes
->mode
!= CCP_AES_MODE_ECB
) {
974 /* Load the AES context - convert to LE */
975 dm_offset
= CCP_SB_BYTES
- AES_BLOCK_SIZE
;
976 ret
= ccp_set_dm_area(&ctx
, dm_offset
, aes
->iv
, 0, aes
->iv_len
);
979 ret
= ccp_copy_to_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
980 CCP_PASSTHRU_BYTESWAP_256BIT
);
982 cmd
->engine_error
= cmd_q
->cmd_error
;
987 case CCP_AES_MODE_CFB
: /* CFB128 only */
988 case CCP_AES_MODE_CTR
:
989 op
.u
.aes
.size
= AES_BLOCK_SIZE
* BITS_PER_BYTE
- 1;
995 /* Prepare the input and output data workareas. For in-place
996 * operations we need to set the dma direction to BIDIRECTIONAL
997 * and copy the src workarea to the dst workarea.
999 if (sg_virt(aes
->src
) == sg_virt(aes
->dst
))
1002 ret
= ccp_init_data(&src
, cmd_q
, aes
->src
, aes
->src_len
,
1004 in_place
? DMA_BIDIRECTIONAL
: DMA_TO_DEVICE
);
1011 ret
= ccp_init_data(&dst
, cmd_q
, aes
->dst
, aes
->src_len
,
1012 AES_BLOCK_SIZE
, DMA_FROM_DEVICE
);
1017 /* Send data to the CCP AES engine */
1018 while (src
.sg_wa
.bytes_left
) {
1019 ccp_prepare_data(&src
, &dst
, &op
, AES_BLOCK_SIZE
, true);
1020 if (!src
.sg_wa
.bytes_left
) {
1023 /* Since we don't retrieve the AES context in ECB
1024 * mode we have to wait for the operation to complete
1025 * on the last piece of data
1027 if (aes
->mode
== CCP_AES_MODE_ECB
)
1031 ret
= cmd_q
->ccp
->vdata
->perform
->aes(&op
);
1033 cmd
->engine_error
= cmd_q
->cmd_error
;
1037 ccp_process_data(&src
, &dst
, &op
);
1040 if (aes
->mode
!= CCP_AES_MODE_ECB
) {
1041 /* Retrieve the AES context - convert from LE to BE using
1042 * 32-byte (256-bit) byteswapping
1044 ret
= ccp_copy_from_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
1045 CCP_PASSTHRU_BYTESWAP_256BIT
);
1047 cmd
->engine_error
= cmd_q
->cmd_error
;
1051 /* ...but we only need AES_BLOCK_SIZE bytes */
1052 dm_offset
= CCP_SB_BYTES
- AES_BLOCK_SIZE
;
1053 ccp_get_dm_area(&ctx
, dm_offset
, aes
->iv
, 0, aes
->iv_len
);
1058 ccp_free_data(&dst
, cmd_q
);
1061 ccp_free_data(&src
, cmd_q
);
1072 static noinline_for_stack
int
1073 ccp_run_xts_aes_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
1075 struct ccp_xts_aes_engine
*xts
= &cmd
->u
.xts
;
1076 struct ccp_dm_workarea key
, ctx
;
1077 struct ccp_data src
, dst
;
1079 unsigned int unit_size
, dm_offset
;
1080 bool in_place
= false;
1081 unsigned int sb_count
;
1082 enum ccp_aes_type aestype
;
1085 switch (xts
->unit_size
) {
1086 case CCP_XTS_AES_UNIT_SIZE_16
:
1089 case CCP_XTS_AES_UNIT_SIZE_512
:
1092 case CCP_XTS_AES_UNIT_SIZE_1024
:
1095 case CCP_XTS_AES_UNIT_SIZE_2048
:
1098 case CCP_XTS_AES_UNIT_SIZE_4096
:
1106 if (xts
->key_len
== AES_KEYSIZE_128
)
1107 aestype
= CCP_AES_TYPE_128
;
1108 else if (xts
->key_len
== AES_KEYSIZE_256
)
1109 aestype
= CCP_AES_TYPE_256
;
1113 if (!xts
->final
&& (xts
->src_len
& (AES_BLOCK_SIZE
- 1)))
1116 if (xts
->iv_len
!= AES_BLOCK_SIZE
)
1119 if (!xts
->key
|| !xts
->iv
|| !xts
->src
|| !xts
->dst
)
1122 BUILD_BUG_ON(CCP_XTS_AES_KEY_SB_COUNT
!= 1);
1123 BUILD_BUG_ON(CCP_XTS_AES_CTX_SB_COUNT
!= 1);
1126 memset(&op
, 0, sizeof(op
));
1128 op
.jobid
= CCP_NEW_JOBID(cmd_q
->ccp
);
1129 op
.sb_key
= cmd_q
->sb_key
;
1130 op
.sb_ctx
= cmd_q
->sb_ctx
;
1132 op
.u
.xts
.type
= aestype
;
1133 op
.u
.xts
.action
= xts
->action
;
1134 op
.u
.xts
.unit_size
= xts
->unit_size
;
1136 /* A version 3 device only supports 128-bit keys, which fits into a
1137 * single SB entry. A version 5 device uses a 512-bit vector, so two
1140 if (cmd_q
->ccp
->vdata
->version
== CCP_VERSION(3, 0))
1141 sb_count
= CCP_XTS_AES_KEY_SB_COUNT
;
1143 sb_count
= CCP5_XTS_AES_KEY_SB_COUNT
;
1144 ret
= ccp_init_dm_workarea(&key
, cmd_q
,
1145 sb_count
* CCP_SB_BYTES
,
1150 if (cmd_q
->ccp
->vdata
->version
== CCP_VERSION(3, 0)) {
1151 /* All supported key sizes must be in little endian format.
1152 * Use the 256-bit byte swap passthru option to convert from
1153 * big endian to little endian.
1155 dm_offset
= CCP_SB_BYTES
- AES_KEYSIZE_128
;
1156 ret
= ccp_set_dm_area(&key
, dm_offset
, xts
->key
, 0, xts
->key_len
);
1159 ret
= ccp_set_dm_area(&key
, 0, xts
->key
, xts
->key_len
, xts
->key_len
);
1163 /* Version 5 CCPs use a 512-bit space for the key: each portion
1164 * occupies 256 bits, or one entire slot, and is zero-padded.
1168 dm_offset
= CCP_SB_BYTES
;
1169 pad
= dm_offset
- xts
->key_len
;
1170 ret
= ccp_set_dm_area(&key
, pad
, xts
->key
, 0, xts
->key_len
);
1173 ret
= ccp_set_dm_area(&key
, dm_offset
+ pad
, xts
->key
,
1174 xts
->key_len
, xts
->key_len
);
1178 ret
= ccp_copy_to_sb(cmd_q
, &key
, op
.jobid
, op
.sb_key
,
1179 CCP_PASSTHRU_BYTESWAP_256BIT
);
1181 cmd
->engine_error
= cmd_q
->cmd_error
;
1185 /* The AES context fits in a single (32-byte) SB entry and
1186 * for XTS is already in little endian format so no byte swapping
1189 ret
= ccp_init_dm_workarea(&ctx
, cmd_q
,
1190 CCP_XTS_AES_CTX_SB_COUNT
* CCP_SB_BYTES
,
1195 ret
= ccp_set_dm_area(&ctx
, 0, xts
->iv
, 0, xts
->iv_len
);
1198 ret
= ccp_copy_to_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
1199 CCP_PASSTHRU_BYTESWAP_NOOP
);
1201 cmd
->engine_error
= cmd_q
->cmd_error
;
1205 /* Prepare the input and output data workareas. For in-place
1206 * operations we need to set the dma direction to BIDIRECTIONAL
1207 * and copy the src workarea to the dst workarea.
1209 if (sg_virt(xts
->src
) == sg_virt(xts
->dst
))
1212 ret
= ccp_init_data(&src
, cmd_q
, xts
->src
, xts
->src_len
,
1214 in_place
? DMA_BIDIRECTIONAL
: DMA_TO_DEVICE
);
1221 ret
= ccp_init_data(&dst
, cmd_q
, xts
->dst
, xts
->src_len
,
1222 unit_size
, DMA_FROM_DEVICE
);
1227 /* Send data to the CCP AES engine */
1228 while (src
.sg_wa
.bytes_left
) {
1229 ccp_prepare_data(&src
, &dst
, &op
, unit_size
, true);
1230 if (!src
.sg_wa
.bytes_left
)
1233 ret
= cmd_q
->ccp
->vdata
->perform
->xts_aes(&op
);
1235 cmd
->engine_error
= cmd_q
->cmd_error
;
1239 ccp_process_data(&src
, &dst
, &op
);
1242 /* Retrieve the AES context - convert from LE to BE using
1243 * 32-byte (256-bit) byteswapping
1245 ret
= ccp_copy_from_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
1246 CCP_PASSTHRU_BYTESWAP_256BIT
);
1248 cmd
->engine_error
= cmd_q
->cmd_error
;
1252 /* ...but we only need AES_BLOCK_SIZE bytes */
1253 dm_offset
= CCP_SB_BYTES
- AES_BLOCK_SIZE
;
1254 ccp_get_dm_area(&ctx
, dm_offset
, xts
->iv
, 0, xts
->iv_len
);
1258 ccp_free_data(&dst
, cmd_q
);
1261 ccp_free_data(&src
, cmd_q
);
1272 static noinline_for_stack
int
1273 ccp_run_des3_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
1275 struct ccp_des3_engine
*des3
= &cmd
->u
.des3
;
1277 struct ccp_dm_workarea key
, ctx
;
1278 struct ccp_data src
, dst
;
1280 unsigned int dm_offset
;
1281 unsigned int len_singlekey
;
1282 bool in_place
= false;
1286 if (cmd_q
->ccp
->vdata
->version
< CCP_VERSION(5, 0))
1289 if (!cmd_q
->ccp
->vdata
->perform
->des3
)
1292 if (des3
->key_len
!= DES3_EDE_KEY_SIZE
)
1295 if (((des3
->mode
== CCP_DES3_MODE_ECB
) ||
1296 (des3
->mode
== CCP_DES3_MODE_CBC
)) &&
1297 (des3
->src_len
& (DES3_EDE_BLOCK_SIZE
- 1)))
1300 if (!des3
->key
|| !des3
->src
|| !des3
->dst
)
1303 if (des3
->mode
!= CCP_DES3_MODE_ECB
) {
1304 if (des3
->iv_len
!= DES3_EDE_BLOCK_SIZE
)
1312 /* Zero out all the fields of the command desc */
1313 memset(&op
, 0, sizeof(op
));
1315 /* Set up the Function field */
1317 op
.jobid
= CCP_NEW_JOBID(cmd_q
->ccp
);
1318 op
.sb_key
= cmd_q
->sb_key
;
1320 op
.init
= (des3
->mode
== CCP_DES3_MODE_ECB
) ? 0 : 1;
1321 op
.u
.des3
.type
= des3
->type
;
1322 op
.u
.des3
.mode
= des3
->mode
;
1323 op
.u
.des3
.action
= des3
->action
;
1326 * All supported key sizes fit in a single (32-byte) KSB entry and
1327 * (like AES) must be in little endian format. Use the 256-bit byte
1328 * swap passthru option to convert from big endian to little endian.
1330 ret
= ccp_init_dm_workarea(&key
, cmd_q
,
1331 CCP_DES3_KEY_SB_COUNT
* CCP_SB_BYTES
,
1337 * The contents of the key triplet are in the reverse order of what
1338 * is required by the engine. Copy the 3 pieces individually to put
1339 * them where they belong.
1341 dm_offset
= CCP_SB_BYTES
- des3
->key_len
; /* Basic offset */
1343 len_singlekey
= des3
->key_len
/ 3;
1344 ret
= ccp_set_dm_area(&key
, dm_offset
+ 2 * len_singlekey
,
1345 des3
->key
, 0, len_singlekey
);
1348 ret
= ccp_set_dm_area(&key
, dm_offset
+ len_singlekey
,
1349 des3
->key
, len_singlekey
, len_singlekey
);
1352 ret
= ccp_set_dm_area(&key
, dm_offset
,
1353 des3
->key
, 2 * len_singlekey
, len_singlekey
);
1357 /* Copy the key to the SB */
1358 ret
= ccp_copy_to_sb(cmd_q
, &key
, op
.jobid
, op
.sb_key
,
1359 CCP_PASSTHRU_BYTESWAP_256BIT
);
1361 cmd
->engine_error
= cmd_q
->cmd_error
;
1366 * The DES3 context fits in a single (32-byte) KSB entry and
1367 * must be in little endian format. Use the 256-bit byte swap
1368 * passthru option to convert from big endian to little endian.
1370 if (des3
->mode
!= CCP_DES3_MODE_ECB
) {
1371 op
.sb_ctx
= cmd_q
->sb_ctx
;
1373 ret
= ccp_init_dm_workarea(&ctx
, cmd_q
,
1374 CCP_DES3_CTX_SB_COUNT
* CCP_SB_BYTES
,
1379 /* Load the context into the LSB */
1380 dm_offset
= CCP_SB_BYTES
- des3
->iv_len
;
1381 ret
= ccp_set_dm_area(&ctx
, dm_offset
, des3
->iv
, 0,
1386 ret
= ccp_copy_to_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
1387 CCP_PASSTHRU_BYTESWAP_256BIT
);
1389 cmd
->engine_error
= cmd_q
->cmd_error
;
1395 * Prepare the input and output data workareas. For in-place
1396 * operations we need to set the dma direction to BIDIRECTIONAL
1397 * and copy the src workarea to the dst workarea.
1399 if (sg_virt(des3
->src
) == sg_virt(des3
->dst
))
1402 ret
= ccp_init_data(&src
, cmd_q
, des3
->src
, des3
->src_len
,
1403 DES3_EDE_BLOCK_SIZE
,
1404 in_place
? DMA_BIDIRECTIONAL
: DMA_TO_DEVICE
);
1411 ret
= ccp_init_data(&dst
, cmd_q
, des3
->dst
, des3
->src_len
,
1412 DES3_EDE_BLOCK_SIZE
, DMA_FROM_DEVICE
);
1417 /* Send data to the CCP DES3 engine */
1418 while (src
.sg_wa
.bytes_left
) {
1419 ccp_prepare_data(&src
, &dst
, &op
, DES3_EDE_BLOCK_SIZE
, true);
1420 if (!src
.sg_wa
.bytes_left
) {
1423 /* Since we don't retrieve the context in ECB mode
1424 * we have to wait for the operation to complete
1425 * on the last piece of data
1430 ret
= cmd_q
->ccp
->vdata
->perform
->des3(&op
);
1432 cmd
->engine_error
= cmd_q
->cmd_error
;
1436 ccp_process_data(&src
, &dst
, &op
);
1439 if (des3
->mode
!= CCP_DES3_MODE_ECB
) {
1440 /* Retrieve the context and make BE */
1441 ret
= ccp_copy_from_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
1442 CCP_PASSTHRU_BYTESWAP_256BIT
);
1444 cmd
->engine_error
= cmd_q
->cmd_error
;
1448 /* ...but we only need the last DES3_EDE_BLOCK_SIZE bytes */
1449 ccp_get_dm_area(&ctx
, dm_offset
, des3
->iv
, 0,
1450 DES3_EDE_BLOCK_SIZE
);
1454 ccp_free_data(&dst
, cmd_q
);
1457 ccp_free_data(&src
, cmd_q
);
1460 if (des3
->mode
!= CCP_DES3_MODE_ECB
)
1469 static noinline_for_stack
int
1470 ccp_run_sha_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
1472 struct ccp_sha_engine
*sha
= &cmd
->u
.sha
;
1473 struct ccp_dm_workarea ctx
;
1474 struct ccp_data src
;
1476 unsigned int ioffset
, ooffset
;
1477 unsigned int digest_size
;
1484 switch (sha
->type
) {
1485 case CCP_SHA_TYPE_1
:
1486 if (sha
->ctx_len
< SHA1_DIGEST_SIZE
)
1488 block_size
= SHA1_BLOCK_SIZE
;
1490 case CCP_SHA_TYPE_224
:
1491 if (sha
->ctx_len
< SHA224_DIGEST_SIZE
)
1493 block_size
= SHA224_BLOCK_SIZE
;
1495 case CCP_SHA_TYPE_256
:
1496 if (sha
->ctx_len
< SHA256_DIGEST_SIZE
)
1498 block_size
= SHA256_BLOCK_SIZE
;
1500 case CCP_SHA_TYPE_384
:
1501 if (cmd_q
->ccp
->vdata
->version
< CCP_VERSION(4, 0)
1502 || sha
->ctx_len
< SHA384_DIGEST_SIZE
)
1504 block_size
= SHA384_BLOCK_SIZE
;
1506 case CCP_SHA_TYPE_512
:
1507 if (cmd_q
->ccp
->vdata
->version
< CCP_VERSION(4, 0)
1508 || sha
->ctx_len
< SHA512_DIGEST_SIZE
)
1510 block_size
= SHA512_BLOCK_SIZE
;
1519 if (!sha
->final
&& (sha
->src_len
& (block_size
- 1)))
1522 /* The version 3 device can't handle zero-length input */
1523 if (cmd_q
->ccp
->vdata
->version
== CCP_VERSION(3, 0)) {
1525 if (!sha
->src_len
) {
1526 unsigned int digest_len
;
1529 /* Not final, just return */
1533 /* CCP can't do a zero length sha operation so the
1534 * caller must buffer the data.
1539 /* The CCP cannot perform zero-length sha operations
1540 * so the caller is required to buffer data for the
1541 * final operation. However, a sha operation for a
1542 * message with a total length of zero is valid so
1543 * known values are required to supply the result.
1545 switch (sha
->type
) {
1546 case CCP_SHA_TYPE_1
:
1547 sha_zero
= sha1_zero_message_hash
;
1548 digest_len
= SHA1_DIGEST_SIZE
;
1550 case CCP_SHA_TYPE_224
:
1551 sha_zero
= sha224_zero_message_hash
;
1552 digest_len
= SHA224_DIGEST_SIZE
;
1554 case CCP_SHA_TYPE_256
:
1555 sha_zero
= sha256_zero_message_hash
;
1556 digest_len
= SHA256_DIGEST_SIZE
;
1562 scatterwalk_map_and_copy((void *)sha_zero
, sha
->ctx
, 0,
1569 /* Set variables used throughout */
1570 switch (sha
->type
) {
1571 case CCP_SHA_TYPE_1
:
1572 digest_size
= SHA1_DIGEST_SIZE
;
1573 init
= (void *) ccp_sha1_init
;
1574 ctx_size
= SHA1_DIGEST_SIZE
;
1576 if (cmd_q
->ccp
->vdata
->version
!= CCP_VERSION(3, 0))
1577 ooffset
= ioffset
= CCP_SB_BYTES
- SHA1_DIGEST_SIZE
;
1579 ooffset
= ioffset
= 0;
1581 case CCP_SHA_TYPE_224
:
1582 digest_size
= SHA224_DIGEST_SIZE
;
1583 init
= (void *) ccp_sha224_init
;
1584 ctx_size
= SHA256_DIGEST_SIZE
;
1587 if (cmd_q
->ccp
->vdata
->version
!= CCP_VERSION(3, 0))
1588 ooffset
= CCP_SB_BYTES
- SHA224_DIGEST_SIZE
;
1592 case CCP_SHA_TYPE_256
:
1593 digest_size
= SHA256_DIGEST_SIZE
;
1594 init
= (void *) ccp_sha256_init
;
1595 ctx_size
= SHA256_DIGEST_SIZE
;
1597 ooffset
= ioffset
= 0;
1599 case CCP_SHA_TYPE_384
:
1600 digest_size
= SHA384_DIGEST_SIZE
;
1601 init
= (void *) ccp_sha384_init
;
1602 ctx_size
= SHA512_DIGEST_SIZE
;
1605 ooffset
= 2 * CCP_SB_BYTES
- SHA384_DIGEST_SIZE
;
1607 case CCP_SHA_TYPE_512
:
1608 digest_size
= SHA512_DIGEST_SIZE
;
1609 init
= (void *) ccp_sha512_init
;
1610 ctx_size
= SHA512_DIGEST_SIZE
;
1612 ooffset
= ioffset
= 0;
1619 /* For zero-length plaintext the src pointer is ignored;
1620 * otherwise both parts must be valid
1622 if (sha
->src_len
&& !sha
->src
)
1625 memset(&op
, 0, sizeof(op
));
1627 op
.jobid
= CCP_NEW_JOBID(cmd_q
->ccp
);
1628 op
.sb_ctx
= cmd_q
->sb_ctx
; /* Pre-allocated */
1629 op
.u
.sha
.type
= sha
->type
;
1630 op
.u
.sha
.msg_bits
= sha
->msg_bits
;
1632 /* For SHA1/224/256 the context fits in a single (32-byte) SB entry;
1633 * SHA384/512 require 2 adjacent SB slots, with the right half in the
1634 * first slot, and the left half in the second. Each portion must then
1635 * be in little endian format: use the 256-bit byte swap option.
1637 ret
= ccp_init_dm_workarea(&ctx
, cmd_q
, sb_count
* CCP_SB_BYTES
,
1642 switch (sha
->type
) {
1643 case CCP_SHA_TYPE_1
:
1644 case CCP_SHA_TYPE_224
:
1645 case CCP_SHA_TYPE_256
:
1646 memcpy(ctx
.address
+ ioffset
, init
, ctx_size
);
1648 case CCP_SHA_TYPE_384
:
1649 case CCP_SHA_TYPE_512
:
1650 memcpy(ctx
.address
+ ctx_size
/ 2, init
,
1652 memcpy(ctx
.address
, init
+ ctx_size
/ 2,
1660 /* Restore the context */
1661 ret
= ccp_set_dm_area(&ctx
, 0, sha
->ctx
, 0,
1662 sb_count
* CCP_SB_BYTES
);
1667 ret
= ccp_copy_to_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
1668 CCP_PASSTHRU_BYTESWAP_256BIT
);
1670 cmd
->engine_error
= cmd_q
->cmd_error
;
1675 /* Send data to the CCP SHA engine; block_size is set above */
1676 ret
= ccp_init_data(&src
, cmd_q
, sha
->src
, sha
->src_len
,
1677 block_size
, DMA_TO_DEVICE
);
1681 while (src
.sg_wa
.bytes_left
) {
1682 ccp_prepare_data(&src
, NULL
, &op
, block_size
, false);
1683 if (sha
->final
&& !src
.sg_wa
.bytes_left
)
1686 ret
= cmd_q
->ccp
->vdata
->perform
->sha(&op
);
1688 cmd
->engine_error
= cmd_q
->cmd_error
;
1692 ccp_process_data(&src
, NULL
, &op
);
1696 ret
= cmd_q
->ccp
->vdata
->perform
->sha(&op
);
1698 cmd
->engine_error
= cmd_q
->cmd_error
;
1703 /* Retrieve the SHA context - convert from LE to BE using
1704 * 32-byte (256-bit) byteswapping to BE
1706 ret
= ccp_copy_from_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
1707 CCP_PASSTHRU_BYTESWAP_256BIT
);
1709 cmd
->engine_error
= cmd_q
->cmd_error
;
1714 /* Finishing up, so get the digest */
1715 switch (sha
->type
) {
1716 case CCP_SHA_TYPE_1
:
1717 case CCP_SHA_TYPE_224
:
1718 case CCP_SHA_TYPE_256
:
1719 ccp_get_dm_area(&ctx
, ooffset
,
1723 case CCP_SHA_TYPE_384
:
1724 case CCP_SHA_TYPE_512
:
1725 ccp_get_dm_area(&ctx
, 0,
1726 sha
->ctx
, LSB_ITEM_SIZE
- ooffset
,
1728 ccp_get_dm_area(&ctx
, LSB_ITEM_SIZE
+ ooffset
,
1730 LSB_ITEM_SIZE
- ooffset
);
1737 /* Stash the context */
1738 ccp_get_dm_area(&ctx
, 0, sha
->ctx
, 0,
1739 sb_count
* CCP_SB_BYTES
);
1742 if (sha
->final
&& sha
->opad
) {
1743 /* HMAC operation, recursively perform final SHA */
1744 struct ccp_cmd hmac_cmd
;
1745 struct scatterlist sg
;
1748 if (sha
->opad_len
!= block_size
) {
1753 hmac_buf
= kmalloc(block_size
+ digest_size
, GFP_KERNEL
);
1758 sg_init_one(&sg
, hmac_buf
, block_size
+ digest_size
);
1760 scatterwalk_map_and_copy(hmac_buf
, sha
->opad
, 0, block_size
, 0);
1761 switch (sha
->type
) {
1762 case CCP_SHA_TYPE_1
:
1763 case CCP_SHA_TYPE_224
:
1764 case CCP_SHA_TYPE_256
:
1765 memcpy(hmac_buf
+ block_size
,
1766 ctx
.address
+ ooffset
,
1769 case CCP_SHA_TYPE_384
:
1770 case CCP_SHA_TYPE_512
:
1771 memcpy(hmac_buf
+ block_size
,
1772 ctx
.address
+ LSB_ITEM_SIZE
+ ooffset
,
1774 memcpy(hmac_buf
+ block_size
+
1775 (LSB_ITEM_SIZE
- ooffset
),
1785 memset(&hmac_cmd
, 0, sizeof(hmac_cmd
));
1786 hmac_cmd
.engine
= CCP_ENGINE_SHA
;
1787 hmac_cmd
.u
.sha
.type
= sha
->type
;
1788 hmac_cmd
.u
.sha
.ctx
= sha
->ctx
;
1789 hmac_cmd
.u
.sha
.ctx_len
= sha
->ctx_len
;
1790 hmac_cmd
.u
.sha
.src
= &sg
;
1791 hmac_cmd
.u
.sha
.src_len
= block_size
+ digest_size
;
1792 hmac_cmd
.u
.sha
.opad
= NULL
;
1793 hmac_cmd
.u
.sha
.opad_len
= 0;
1794 hmac_cmd
.u
.sha
.first
= 1;
1795 hmac_cmd
.u
.sha
.final
= 1;
1796 hmac_cmd
.u
.sha
.msg_bits
= (block_size
+ digest_size
) << 3;
1798 ret
= ccp_run_sha_cmd(cmd_q
, &hmac_cmd
);
1800 cmd
->engine_error
= hmac_cmd
.engine_error
;
1807 ccp_free_data(&src
, cmd_q
);
1815 static noinline_for_stack
int
1816 ccp_run_rsa_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
1818 struct ccp_rsa_engine
*rsa
= &cmd
->u
.rsa
;
1819 struct ccp_dm_workarea exp
, src
, dst
;
1821 unsigned int sb_count
, i_len
, o_len
;
1824 /* Check against the maximum allowable size, in bits */
1825 if (rsa
->key_size
> cmd_q
->ccp
->vdata
->rsamax
)
1828 if (!rsa
->exp
|| !rsa
->mod
|| !rsa
->src
|| !rsa
->dst
)
1831 memset(&op
, 0, sizeof(op
));
1833 op
.jobid
= CCP_NEW_JOBID(cmd_q
->ccp
);
1835 /* The RSA modulus must precede the message being acted upon, so
1836 * it must be copied to a DMA area where the message and the
1837 * modulus can be concatenated. Therefore the input buffer
1838 * length required is twice the output buffer length (which
1839 * must be a multiple of 256-bits). Compute o_len, i_len in bytes.
1840 * Buffer sizes must be a multiple of 32 bytes; rounding up may be
1843 o_len
= 32 * ((rsa
->key_size
+ 255) / 256);
1847 if (cmd_q
->ccp
->vdata
->version
< CCP_VERSION(5, 0)) {
1848 /* sb_count is the number of storage block slots required
1851 sb_count
= o_len
/ CCP_SB_BYTES
;
1852 op
.sb_key
= cmd_q
->ccp
->vdata
->perform
->sballoc(cmd_q
,
1857 /* A version 5 device allows a modulus size that will not fit
1858 * in the LSB, so the command will transfer it from memory.
1859 * Set the sb key to the default, even though it's not used.
1861 op
.sb_key
= cmd_q
->sb_key
;
1864 /* The RSA exponent must be in little endian format. Reverse its
1867 ret
= ccp_init_dm_workarea(&exp
, cmd_q
, o_len
, DMA_TO_DEVICE
);
1871 ret
= ccp_reverse_set_dm_area(&exp
, 0, rsa
->exp
, 0, rsa
->exp_len
);
1875 if (cmd_q
->ccp
->vdata
->version
< CCP_VERSION(5, 0)) {
1876 /* Copy the exponent to the local storage block, using
1877 * as many 32-byte blocks as were allocated above. It's
1878 * already little endian, so no further change is required.
1880 ret
= ccp_copy_to_sb(cmd_q
, &exp
, op
.jobid
, op
.sb_key
,
1881 CCP_PASSTHRU_BYTESWAP_NOOP
);
1883 cmd
->engine_error
= cmd_q
->cmd_error
;
1887 /* The exponent can be retrieved from memory via DMA. */
1888 op
.exp
.u
.dma
.address
= exp
.dma
.address
;
1889 op
.exp
.u
.dma
.offset
= 0;
1892 /* Concatenate the modulus and the message. Both the modulus and
1893 * the operands must be in little endian format. Since the input
1894 * is in big endian format it must be converted.
1896 ret
= ccp_init_dm_workarea(&src
, cmd_q
, i_len
, DMA_TO_DEVICE
);
1900 ret
= ccp_reverse_set_dm_area(&src
, 0, rsa
->mod
, 0, rsa
->mod_len
);
1903 ret
= ccp_reverse_set_dm_area(&src
, o_len
, rsa
->src
, 0, rsa
->src_len
);
1907 /* Prepare the output area for the operation */
1908 ret
= ccp_init_dm_workarea(&dst
, cmd_q
, o_len
, DMA_FROM_DEVICE
);
1913 op
.src
.u
.dma
.address
= src
.dma
.address
;
1914 op
.src
.u
.dma
.offset
= 0;
1915 op
.src
.u
.dma
.length
= i_len
;
1916 op
.dst
.u
.dma
.address
= dst
.dma
.address
;
1917 op
.dst
.u
.dma
.offset
= 0;
1918 op
.dst
.u
.dma
.length
= o_len
;
1920 op
.u
.rsa
.mod_size
= rsa
->key_size
;
1921 op
.u
.rsa
.input_len
= i_len
;
1923 ret
= cmd_q
->ccp
->vdata
->perform
->rsa(&op
);
1925 cmd
->engine_error
= cmd_q
->cmd_error
;
1929 ccp_reverse_get_dm_area(&dst
, 0, rsa
->dst
, 0, rsa
->mod_len
);
1942 cmd_q
->ccp
->vdata
->perform
->sbfree(cmd_q
, op
.sb_key
, sb_count
);
1947 static noinline_for_stack
int
1948 ccp_run_passthru_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
1950 struct ccp_passthru_engine
*pt
= &cmd
->u
.passthru
;
1951 struct ccp_dm_workarea mask
;
1952 struct ccp_data src
, dst
;
1954 bool in_place
= false;
1958 if (!pt
->final
&& (pt
->src_len
& (CCP_PASSTHRU_BLOCKSIZE
- 1)))
1961 if (!pt
->src
|| !pt
->dst
)
1964 if (pt
->bit_mod
!= CCP_PASSTHRU_BITWISE_NOOP
) {
1965 if (pt
->mask_len
!= CCP_PASSTHRU_MASKSIZE
)
1971 BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT
!= 1);
1973 memset(&op
, 0, sizeof(op
));
1975 op
.jobid
= CCP_NEW_JOBID(cmd_q
->ccp
);
1977 if (pt
->bit_mod
!= CCP_PASSTHRU_BITWISE_NOOP
) {
1979 op
.sb_key
= cmd_q
->sb_key
;
1981 ret
= ccp_init_dm_workarea(&mask
, cmd_q
,
1982 CCP_PASSTHRU_SB_COUNT
*
1988 ret
= ccp_set_dm_area(&mask
, 0, pt
->mask
, 0, pt
->mask_len
);
1991 ret
= ccp_copy_to_sb(cmd_q
, &mask
, op
.jobid
, op
.sb_key
,
1992 CCP_PASSTHRU_BYTESWAP_NOOP
);
1994 cmd
->engine_error
= cmd_q
->cmd_error
;
1999 /* Prepare the input and output data workareas. For in-place
2000 * operations we need to set the dma direction to BIDIRECTIONAL
2001 * and copy the src workarea to the dst workarea.
2003 if (sg_virt(pt
->src
) == sg_virt(pt
->dst
))
2006 ret
= ccp_init_data(&src
, cmd_q
, pt
->src
, pt
->src_len
,
2007 CCP_PASSTHRU_MASKSIZE
,
2008 in_place
? DMA_BIDIRECTIONAL
: DMA_TO_DEVICE
);
2015 ret
= ccp_init_data(&dst
, cmd_q
, pt
->dst
, pt
->src_len
,
2016 CCP_PASSTHRU_MASKSIZE
, DMA_FROM_DEVICE
);
2021 /* Send data to the CCP Passthru engine
2022 * Because the CCP engine works on a single source and destination
2023 * dma address at a time, each entry in the source scatterlist
2024 * (after the dma_map_sg call) must be less than or equal to the
2025 * (remaining) length in the destination scatterlist entry and the
2026 * length must be a multiple of CCP_PASSTHRU_BLOCKSIZE
2028 dst
.sg_wa
.sg_used
= 0;
2029 for (i
= 1; i
<= src
.sg_wa
.dma_count
; i
++) {
2030 if (!dst
.sg_wa
.sg
||
2031 (dst
.sg_wa
.sg
->length
< src
.sg_wa
.sg
->length
)) {
2036 if (i
== src
.sg_wa
.dma_count
) {
2041 op
.src
.type
= CCP_MEMTYPE_SYSTEM
;
2042 op
.src
.u
.dma
.address
= sg_dma_address(src
.sg_wa
.sg
);
2043 op
.src
.u
.dma
.offset
= 0;
2044 op
.src
.u
.dma
.length
= sg_dma_len(src
.sg_wa
.sg
);
2046 op
.dst
.type
= CCP_MEMTYPE_SYSTEM
;
2047 op
.dst
.u
.dma
.address
= sg_dma_address(dst
.sg_wa
.sg
);
2048 op
.dst
.u
.dma
.offset
= dst
.sg_wa
.sg_used
;
2049 op
.dst
.u
.dma
.length
= op
.src
.u
.dma
.length
;
2051 ret
= cmd_q
->ccp
->vdata
->perform
->passthru(&op
);
2053 cmd
->engine_error
= cmd_q
->cmd_error
;
2057 dst
.sg_wa
.sg_used
+= src
.sg_wa
.sg
->length
;
2058 if (dst
.sg_wa
.sg_used
== dst
.sg_wa
.sg
->length
) {
2059 dst
.sg_wa
.sg
= sg_next(dst
.sg_wa
.sg
);
2060 dst
.sg_wa
.sg_used
= 0;
2062 src
.sg_wa
.sg
= sg_next(src
.sg_wa
.sg
);
2067 ccp_free_data(&dst
, cmd_q
);
2070 ccp_free_data(&src
, cmd_q
);
2073 if (pt
->bit_mod
!= CCP_PASSTHRU_BITWISE_NOOP
)
2079 static noinline_for_stack
int
2080 ccp_run_passthru_nomap_cmd(struct ccp_cmd_queue
*cmd_q
,
2081 struct ccp_cmd
*cmd
)
2083 struct ccp_passthru_nomap_engine
*pt
= &cmd
->u
.passthru_nomap
;
2084 struct ccp_dm_workarea mask
;
2088 if (!pt
->final
&& (pt
->src_len
& (CCP_PASSTHRU_BLOCKSIZE
- 1)))
2091 if (!pt
->src_dma
|| !pt
->dst_dma
)
2094 if (pt
->bit_mod
!= CCP_PASSTHRU_BITWISE_NOOP
) {
2095 if (pt
->mask_len
!= CCP_PASSTHRU_MASKSIZE
)
2101 BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT
!= 1);
2103 memset(&op
, 0, sizeof(op
));
2105 op
.jobid
= CCP_NEW_JOBID(cmd_q
->ccp
);
2107 if (pt
->bit_mod
!= CCP_PASSTHRU_BITWISE_NOOP
) {
2109 op
.sb_key
= cmd_q
->sb_key
;
2111 mask
.length
= pt
->mask_len
;
2112 mask
.dma
.address
= pt
->mask
;
2113 mask
.dma
.length
= pt
->mask_len
;
2115 ret
= ccp_copy_to_sb(cmd_q
, &mask
, op
.jobid
, op
.sb_key
,
2116 CCP_PASSTHRU_BYTESWAP_NOOP
);
2118 cmd
->engine_error
= cmd_q
->cmd_error
;
2123 /* Send data to the CCP Passthru engine */
2127 op
.src
.type
= CCP_MEMTYPE_SYSTEM
;
2128 op
.src
.u
.dma
.address
= pt
->src_dma
;
2129 op
.src
.u
.dma
.offset
= 0;
2130 op
.src
.u
.dma
.length
= pt
->src_len
;
2132 op
.dst
.type
= CCP_MEMTYPE_SYSTEM
;
2133 op
.dst
.u
.dma
.address
= pt
->dst_dma
;
2134 op
.dst
.u
.dma
.offset
= 0;
2135 op
.dst
.u
.dma
.length
= pt
->src_len
;
2137 ret
= cmd_q
->ccp
->vdata
->perform
->passthru(&op
);
2139 cmd
->engine_error
= cmd_q
->cmd_error
;
2144 static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
2146 struct ccp_ecc_engine
*ecc
= &cmd
->u
.ecc
;
2147 struct ccp_dm_workarea src
, dst
;
2152 if (!ecc
->u
.mm
.operand_1
||
2153 (ecc
->u
.mm
.operand_1_len
> CCP_ECC_MODULUS_BYTES
))
2156 if (ecc
->function
!= CCP_ECC_FUNCTION_MINV_384BIT
)
2157 if (!ecc
->u
.mm
.operand_2
||
2158 (ecc
->u
.mm
.operand_2_len
> CCP_ECC_MODULUS_BYTES
))
2161 if (!ecc
->u
.mm
.result
||
2162 (ecc
->u
.mm
.result_len
< CCP_ECC_MODULUS_BYTES
))
2165 memset(&op
, 0, sizeof(op
));
2167 op
.jobid
= CCP_NEW_JOBID(cmd_q
->ccp
);
2169 /* Concatenate the modulus and the operands. Both the modulus and
2170 * the operands must be in little endian format. Since the input
2171 * is in big endian format it must be converted and placed in a
2172 * fixed length buffer.
2174 ret
= ccp_init_dm_workarea(&src
, cmd_q
, CCP_ECC_SRC_BUF_SIZE
,
2179 /* Save the workarea address since it is updated in order to perform
2184 /* Copy the ECC modulus */
2185 ret
= ccp_reverse_set_dm_area(&src
, 0, ecc
->mod
, 0, ecc
->mod_len
);
2188 src
.address
+= CCP_ECC_OPERAND_SIZE
;
2190 /* Copy the first operand */
2191 ret
= ccp_reverse_set_dm_area(&src
, 0, ecc
->u
.mm
.operand_1
, 0,
2192 ecc
->u
.mm
.operand_1_len
);
2195 src
.address
+= CCP_ECC_OPERAND_SIZE
;
2197 if (ecc
->function
!= CCP_ECC_FUNCTION_MINV_384BIT
) {
2198 /* Copy the second operand */
2199 ret
= ccp_reverse_set_dm_area(&src
, 0, ecc
->u
.mm
.operand_2
, 0,
2200 ecc
->u
.mm
.operand_2_len
);
2203 src
.address
+= CCP_ECC_OPERAND_SIZE
;
2206 /* Restore the workarea address */
2209 /* Prepare the output area for the operation */
2210 ret
= ccp_init_dm_workarea(&dst
, cmd_q
, CCP_ECC_DST_BUF_SIZE
,
2216 op
.src
.u
.dma
.address
= src
.dma
.address
;
2217 op
.src
.u
.dma
.offset
= 0;
2218 op
.src
.u
.dma
.length
= src
.length
;
2219 op
.dst
.u
.dma
.address
= dst
.dma
.address
;
2220 op
.dst
.u
.dma
.offset
= 0;
2221 op
.dst
.u
.dma
.length
= dst
.length
;
2223 op
.u
.ecc
.function
= cmd
->u
.ecc
.function
;
2225 ret
= cmd_q
->ccp
->vdata
->perform
->ecc(&op
);
2227 cmd
->engine_error
= cmd_q
->cmd_error
;
2231 ecc
->ecc_result
= le16_to_cpup(
2232 (const __le16
*)(dst
.address
+ CCP_ECC_RESULT_OFFSET
));
2233 if (!(ecc
->ecc_result
& CCP_ECC_RESULT_SUCCESS
)) {
2238 /* Save the ECC result */
2239 ccp_reverse_get_dm_area(&dst
, 0, ecc
->u
.mm
.result
, 0,
2240 CCP_ECC_MODULUS_BYTES
);
2251 static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
2253 struct ccp_ecc_engine
*ecc
= &cmd
->u
.ecc
;
2254 struct ccp_dm_workarea src
, dst
;
2259 if (!ecc
->u
.pm
.point_1
.x
||
2260 (ecc
->u
.pm
.point_1
.x_len
> CCP_ECC_MODULUS_BYTES
) ||
2261 !ecc
->u
.pm
.point_1
.y
||
2262 (ecc
->u
.pm
.point_1
.y_len
> CCP_ECC_MODULUS_BYTES
))
2265 if (ecc
->function
== CCP_ECC_FUNCTION_PADD_384BIT
) {
2266 if (!ecc
->u
.pm
.point_2
.x
||
2267 (ecc
->u
.pm
.point_2
.x_len
> CCP_ECC_MODULUS_BYTES
) ||
2268 !ecc
->u
.pm
.point_2
.y
||
2269 (ecc
->u
.pm
.point_2
.y_len
> CCP_ECC_MODULUS_BYTES
))
2272 if (!ecc
->u
.pm
.domain_a
||
2273 (ecc
->u
.pm
.domain_a_len
> CCP_ECC_MODULUS_BYTES
))
2276 if (ecc
->function
== CCP_ECC_FUNCTION_PMUL_384BIT
)
2277 if (!ecc
->u
.pm
.scalar
||
2278 (ecc
->u
.pm
.scalar_len
> CCP_ECC_MODULUS_BYTES
))
2282 if (!ecc
->u
.pm
.result
.x
||
2283 (ecc
->u
.pm
.result
.x_len
< CCP_ECC_MODULUS_BYTES
) ||
2284 !ecc
->u
.pm
.result
.y
||
2285 (ecc
->u
.pm
.result
.y_len
< CCP_ECC_MODULUS_BYTES
))
2288 memset(&op
, 0, sizeof(op
));
2290 op
.jobid
= CCP_NEW_JOBID(cmd_q
->ccp
);
2292 /* Concatenate the modulus and the operands. Both the modulus and
2293 * the operands must be in little endian format. Since the input
2294 * is in big endian format it must be converted and placed in a
2295 * fixed length buffer.
2297 ret
= ccp_init_dm_workarea(&src
, cmd_q
, CCP_ECC_SRC_BUF_SIZE
,
2302 /* Save the workarea address since it is updated in order to perform
2307 /* Copy the ECC modulus */
2308 ret
= ccp_reverse_set_dm_area(&src
, 0, ecc
->mod
, 0, ecc
->mod_len
);
2311 src
.address
+= CCP_ECC_OPERAND_SIZE
;
2313 /* Copy the first point X and Y coordinate */
2314 ret
= ccp_reverse_set_dm_area(&src
, 0, ecc
->u
.pm
.point_1
.x
, 0,
2315 ecc
->u
.pm
.point_1
.x_len
);
2318 src
.address
+= CCP_ECC_OPERAND_SIZE
;
2319 ret
= ccp_reverse_set_dm_area(&src
, 0, ecc
->u
.pm
.point_1
.y
, 0,
2320 ecc
->u
.pm
.point_1
.y_len
);
2323 src
.address
+= CCP_ECC_OPERAND_SIZE
;
2325 /* Set the first point Z coordinate to 1 */
2326 *src
.address
= 0x01;
2327 src
.address
+= CCP_ECC_OPERAND_SIZE
;
2329 if (ecc
->function
== CCP_ECC_FUNCTION_PADD_384BIT
) {
2330 /* Copy the second point X and Y coordinate */
2331 ret
= ccp_reverse_set_dm_area(&src
, 0, ecc
->u
.pm
.point_2
.x
, 0,
2332 ecc
->u
.pm
.point_2
.x_len
);
2335 src
.address
+= CCP_ECC_OPERAND_SIZE
;
2336 ret
= ccp_reverse_set_dm_area(&src
, 0, ecc
->u
.pm
.point_2
.y
, 0,
2337 ecc
->u
.pm
.point_2
.y_len
);
2340 src
.address
+= CCP_ECC_OPERAND_SIZE
;
2342 /* Set the second point Z coordinate to 1 */
2343 *src
.address
= 0x01;
2344 src
.address
+= CCP_ECC_OPERAND_SIZE
;
2346 /* Copy the Domain "a" parameter */
2347 ret
= ccp_reverse_set_dm_area(&src
, 0, ecc
->u
.pm
.domain_a
, 0,
2348 ecc
->u
.pm
.domain_a_len
);
2351 src
.address
+= CCP_ECC_OPERAND_SIZE
;
2353 if (ecc
->function
== CCP_ECC_FUNCTION_PMUL_384BIT
) {
2354 /* Copy the scalar value */
2355 ret
= ccp_reverse_set_dm_area(&src
, 0,
2356 ecc
->u
.pm
.scalar
, 0,
2357 ecc
->u
.pm
.scalar_len
);
2360 src
.address
+= CCP_ECC_OPERAND_SIZE
;
2364 /* Restore the workarea address */
2367 /* Prepare the output area for the operation */
2368 ret
= ccp_init_dm_workarea(&dst
, cmd_q
, CCP_ECC_DST_BUF_SIZE
,
2374 op
.src
.u
.dma
.address
= src
.dma
.address
;
2375 op
.src
.u
.dma
.offset
= 0;
2376 op
.src
.u
.dma
.length
= src
.length
;
2377 op
.dst
.u
.dma
.address
= dst
.dma
.address
;
2378 op
.dst
.u
.dma
.offset
= 0;
2379 op
.dst
.u
.dma
.length
= dst
.length
;
2381 op
.u
.ecc
.function
= cmd
->u
.ecc
.function
;
2383 ret
= cmd_q
->ccp
->vdata
->perform
->ecc(&op
);
2385 cmd
->engine_error
= cmd_q
->cmd_error
;
2389 ecc
->ecc_result
= le16_to_cpup(
2390 (const __le16
*)(dst
.address
+ CCP_ECC_RESULT_OFFSET
));
2391 if (!(ecc
->ecc_result
& CCP_ECC_RESULT_SUCCESS
)) {
2396 /* Save the workarea address since it is updated as we walk through
2397 * to copy the point math result
2401 /* Save the ECC result X and Y coordinates */
2402 ccp_reverse_get_dm_area(&dst
, 0, ecc
->u
.pm
.result
.x
, 0,
2403 CCP_ECC_MODULUS_BYTES
);
2404 dst
.address
+= CCP_ECC_OUTPUT_SIZE
;
2405 ccp_reverse_get_dm_area(&dst
, 0, ecc
->u
.pm
.result
.y
, 0,
2406 CCP_ECC_MODULUS_BYTES
);
2407 dst
.address
+= CCP_ECC_OUTPUT_SIZE
;
2409 /* Restore the workarea address */
2421 static noinline_for_stack
int
2422 ccp_run_ecc_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
2424 struct ccp_ecc_engine
*ecc
= &cmd
->u
.ecc
;
2426 ecc
->ecc_result
= 0;
2429 (ecc
->mod_len
> CCP_ECC_MODULUS_BYTES
))
2432 switch (ecc
->function
) {
2433 case CCP_ECC_FUNCTION_MMUL_384BIT
:
2434 case CCP_ECC_FUNCTION_MADD_384BIT
:
2435 case CCP_ECC_FUNCTION_MINV_384BIT
:
2436 return ccp_run_ecc_mm_cmd(cmd_q
, cmd
);
2438 case CCP_ECC_FUNCTION_PADD_384BIT
:
2439 case CCP_ECC_FUNCTION_PMUL_384BIT
:
2440 case CCP_ECC_FUNCTION_PDBL_384BIT
:
2441 return ccp_run_ecc_pm_cmd(cmd_q
, cmd
);
2448 int ccp_run_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
2452 cmd
->engine_error
= 0;
2453 cmd_q
->cmd_error
= 0;
2454 cmd_q
->int_rcvd
= 0;
2455 cmd_q
->free_slots
= cmd_q
->ccp
->vdata
->perform
->get_free_slots(cmd_q
);
2457 switch (cmd
->engine
) {
2458 case CCP_ENGINE_AES
:
2459 switch (cmd
->u
.aes
.mode
) {
2460 case CCP_AES_MODE_CMAC
:
2461 ret
= ccp_run_aes_cmac_cmd(cmd_q
, cmd
);
2463 case CCP_AES_MODE_GCM
:
2464 ret
= ccp_run_aes_gcm_cmd(cmd_q
, cmd
);
2467 ret
= ccp_run_aes_cmd(cmd_q
, cmd
);
2471 case CCP_ENGINE_XTS_AES_128
:
2472 ret
= ccp_run_xts_aes_cmd(cmd_q
, cmd
);
2474 case CCP_ENGINE_DES3
:
2475 ret
= ccp_run_des3_cmd(cmd_q
, cmd
);
2477 case CCP_ENGINE_SHA
:
2478 ret
= ccp_run_sha_cmd(cmd_q
, cmd
);
2480 case CCP_ENGINE_RSA
:
2481 ret
= ccp_run_rsa_cmd(cmd_q
, cmd
);
2483 case CCP_ENGINE_PASSTHRU
:
2484 if (cmd
->flags
& CCP_CMD_PASSTHRU_NO_DMA_MAP
)
2485 ret
= ccp_run_passthru_nomap_cmd(cmd_q
, cmd
);
2487 ret
= ccp_run_passthru_cmd(cmd_q
, cmd
);
2489 case CCP_ENGINE_ECC
:
2490 ret
= ccp_run_ecc_cmd(cmd_q
, cmd
);