2 * AMD Cryptographic Coprocessor (CCP) driver
4 * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
7 * Author: Gary R Hook <gary.hook@amd.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/pci.h>
17 #include <linux/interrupt.h>
18 #include <crypto/scatterwalk.h>
19 #include <linux/ccp.h>
23 /* SHA initial context values */
24 static const __be32 ccp_sha1_init
[SHA1_DIGEST_SIZE
/ sizeof(__be32
)] = {
25 cpu_to_be32(SHA1_H0
), cpu_to_be32(SHA1_H1
),
26 cpu_to_be32(SHA1_H2
), cpu_to_be32(SHA1_H3
),
30 static const __be32 ccp_sha224_init
[SHA256_DIGEST_SIZE
/ sizeof(__be32
)] = {
31 cpu_to_be32(SHA224_H0
), cpu_to_be32(SHA224_H1
),
32 cpu_to_be32(SHA224_H2
), cpu_to_be32(SHA224_H3
),
33 cpu_to_be32(SHA224_H4
), cpu_to_be32(SHA224_H5
),
34 cpu_to_be32(SHA224_H6
), cpu_to_be32(SHA224_H7
),
37 static const __be32 ccp_sha256_init
[SHA256_DIGEST_SIZE
/ sizeof(__be32
)] = {
38 cpu_to_be32(SHA256_H0
), cpu_to_be32(SHA256_H1
),
39 cpu_to_be32(SHA256_H2
), cpu_to_be32(SHA256_H3
),
40 cpu_to_be32(SHA256_H4
), cpu_to_be32(SHA256_H5
),
41 cpu_to_be32(SHA256_H6
), cpu_to_be32(SHA256_H7
),
44 #define CCP_NEW_JOBID(ccp) ((ccp->vdata->version == CCP_VERSION(3, 0)) ? \
45 ccp_gen_jobid(ccp) : 0)
47 static u32
ccp_gen_jobid(struct ccp_device
*ccp
)
49 return atomic_inc_return(&ccp
->current_id
) & CCP_JOBID_MASK
;
52 static void ccp_sg_free(struct ccp_sg_workarea
*wa
)
55 dma_unmap_sg(wa
->dma_dev
, wa
->dma_sg
, wa
->nents
, wa
->dma_dir
);
60 static int ccp_init_sg_workarea(struct ccp_sg_workarea
*wa
, struct device
*dev
,
61 struct scatterlist
*sg
, u64 len
,
62 enum dma_data_direction dma_dir
)
64 memset(wa
, 0, sizeof(*wa
));
70 wa
->nents
= sg_nents_for_len(sg
, len
);
80 if (dma_dir
== DMA_NONE
)
85 wa
->dma_dir
= dma_dir
;
86 wa
->dma_count
= dma_map_sg(dev
, sg
, wa
->nents
, dma_dir
);
93 static void ccp_update_sg_workarea(struct ccp_sg_workarea
*wa
, unsigned int len
)
95 unsigned int nbytes
= min_t(u64
, len
, wa
->bytes_left
);
100 wa
->sg_used
+= nbytes
;
101 wa
->bytes_left
-= nbytes
;
102 if (wa
->sg_used
== wa
->sg
->length
) {
103 wa
->sg
= sg_next(wa
->sg
);
108 static void ccp_dm_free(struct ccp_dm_workarea
*wa
)
110 if (wa
->length
<= CCP_DMAPOOL_MAX_SIZE
) {
112 dma_pool_free(wa
->dma_pool
, wa
->address
,
116 dma_unmap_single(wa
->dev
, wa
->dma
.address
, wa
->length
,
125 static int ccp_init_dm_workarea(struct ccp_dm_workarea
*wa
,
126 struct ccp_cmd_queue
*cmd_q
,
128 enum dma_data_direction dir
)
130 memset(wa
, 0, sizeof(*wa
));
135 wa
->dev
= cmd_q
->ccp
->dev
;
138 if (len
<= CCP_DMAPOOL_MAX_SIZE
) {
139 wa
->dma_pool
= cmd_q
->dma_pool
;
141 wa
->address
= dma_pool_alloc(wa
->dma_pool
, GFP_KERNEL
,
146 wa
->dma
.length
= CCP_DMAPOOL_MAX_SIZE
;
148 memset(wa
->address
, 0, CCP_DMAPOOL_MAX_SIZE
);
150 wa
->address
= kzalloc(len
, GFP_KERNEL
);
154 wa
->dma
.address
= dma_map_single(wa
->dev
, wa
->address
, len
,
156 if (!wa
->dma
.address
)
159 wa
->dma
.length
= len
;
166 static void ccp_set_dm_area(struct ccp_dm_workarea
*wa
, unsigned int wa_offset
,
167 struct scatterlist
*sg
, unsigned int sg_offset
,
170 WARN_ON(!wa
->address
);
172 scatterwalk_map_and_copy(wa
->address
+ wa_offset
, sg
, sg_offset
, len
,
176 static void ccp_get_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 scatterwalk_map_and_copy(wa
->address
+ wa_offset
, sg
, sg_offset
, len
,
186 static int ccp_reverse_set_dm_area(struct ccp_dm_workarea
*wa
,
187 struct scatterlist
*sg
,
188 unsigned int len
, unsigned int se_len
,
191 unsigned int nbytes
, sg_offset
, dm_offset
, sb_len
, i
;
192 u8 buffer
[CCP_REVERSE_BUF_SIZE
];
194 if (WARN_ON(se_len
> sizeof(buffer
)))
201 sb_len
= min_t(unsigned int, nbytes
, se_len
);
204 scatterwalk_map_and_copy(buffer
, sg
, sg_offset
, sb_len
, 0);
205 for (i
= 0; i
< sb_len
; i
++)
206 wa
->address
[dm_offset
+ i
] = buffer
[sb_len
- i
- 1];
211 if ((sb_len
!= se_len
) && sign_extend
) {
212 /* Must sign-extend to nearest sign-extend length */
213 if (wa
->address
[dm_offset
- 1] & 0x80)
214 memset(wa
->address
+ dm_offset
, 0xff,
222 static void ccp_reverse_get_dm_area(struct ccp_dm_workarea
*wa
,
223 struct scatterlist
*sg
,
226 unsigned int nbytes
, sg_offset
, dm_offset
, sb_len
, i
;
227 u8 buffer
[CCP_REVERSE_BUF_SIZE
];
233 sb_len
= min_t(unsigned int, nbytes
, sizeof(buffer
));
236 for (i
= 0; i
< sb_len
; i
++)
237 buffer
[sb_len
- i
- 1] = wa
->address
[dm_offset
+ i
];
238 scatterwalk_map_and_copy(buffer
, sg
, sg_offset
, sb_len
, 1);
245 static void ccp_free_data(struct ccp_data
*data
, struct ccp_cmd_queue
*cmd_q
)
247 ccp_dm_free(&data
->dm_wa
);
248 ccp_sg_free(&data
->sg_wa
);
251 static int ccp_init_data(struct ccp_data
*data
, struct ccp_cmd_queue
*cmd_q
,
252 struct scatterlist
*sg
, u64 sg_len
,
254 enum dma_data_direction dir
)
258 memset(data
, 0, sizeof(*data
));
260 ret
= ccp_init_sg_workarea(&data
->sg_wa
, cmd_q
->ccp
->dev
, sg
, sg_len
,
265 ret
= ccp_init_dm_workarea(&data
->dm_wa
, cmd_q
, dm_len
, dir
);
272 ccp_free_data(data
, cmd_q
);
277 static unsigned int ccp_queue_buf(struct ccp_data
*data
, unsigned int from
)
279 struct ccp_sg_workarea
*sg_wa
= &data
->sg_wa
;
280 struct ccp_dm_workarea
*dm_wa
= &data
->dm_wa
;
281 unsigned int buf_count
, nbytes
;
283 /* Clear the buffer if setting it */
285 memset(dm_wa
->address
, 0, dm_wa
->length
);
290 /* Perform the copy operation
291 * nbytes will always be <= UINT_MAX because dm_wa->length is
294 nbytes
= min_t(u64
, sg_wa
->bytes_left
, dm_wa
->length
);
295 scatterwalk_map_and_copy(dm_wa
->address
, sg_wa
->sg
, sg_wa
->sg_used
,
298 /* Update the structures and generate the count */
300 while (sg_wa
->bytes_left
&& (buf_count
< dm_wa
->length
)) {
301 nbytes
= min(sg_wa
->sg
->length
- sg_wa
->sg_used
,
302 dm_wa
->length
- buf_count
);
303 nbytes
= min_t(u64
, sg_wa
->bytes_left
, nbytes
);
306 ccp_update_sg_workarea(sg_wa
, nbytes
);
312 static unsigned int ccp_fill_queue_buf(struct ccp_data
*data
)
314 return ccp_queue_buf(data
, 0);
317 static unsigned int ccp_empty_queue_buf(struct ccp_data
*data
)
319 return ccp_queue_buf(data
, 1);
322 static void ccp_prepare_data(struct ccp_data
*src
, struct ccp_data
*dst
,
323 struct ccp_op
*op
, unsigned int block_size
,
326 unsigned int sg_src_len
, sg_dst_len
, op_len
;
328 /* The CCP can only DMA from/to one address each per operation. This
329 * requires that we find the smallest DMA area between the source
330 * and destination. The resulting len values will always be <= UINT_MAX
331 * because the dma length is an unsigned int.
333 sg_src_len
= sg_dma_len(src
->sg_wa
.sg
) - src
->sg_wa
.sg_used
;
334 sg_src_len
= min_t(u64
, src
->sg_wa
.bytes_left
, sg_src_len
);
337 sg_dst_len
= sg_dma_len(dst
->sg_wa
.sg
) - dst
->sg_wa
.sg_used
;
338 sg_dst_len
= min_t(u64
, src
->sg_wa
.bytes_left
, sg_dst_len
);
339 op_len
= min(sg_src_len
, sg_dst_len
);
344 /* The data operation length will be at least block_size in length
345 * or the smaller of available sg room remaining for the source or
348 op_len
= max(op_len
, block_size
);
350 /* Unless we have to buffer data, there's no reason to wait */
353 if (sg_src_len
< block_size
) {
354 /* Not enough data in the sg element, so it
355 * needs to be buffered into a blocksize chunk
357 int cp_len
= ccp_fill_queue_buf(src
);
360 op
->src
.u
.dma
.address
= src
->dm_wa
.dma
.address
;
361 op
->src
.u
.dma
.offset
= 0;
362 op
->src
.u
.dma
.length
= (blocksize_op
) ? block_size
: cp_len
;
364 /* Enough data in the sg element, but we need to
365 * adjust for any previously copied data
367 op
->src
.u
.dma
.address
= sg_dma_address(src
->sg_wa
.sg
);
368 op
->src
.u
.dma
.offset
= src
->sg_wa
.sg_used
;
369 op
->src
.u
.dma
.length
= op_len
& ~(block_size
- 1);
371 ccp_update_sg_workarea(&src
->sg_wa
, op
->src
.u
.dma
.length
);
375 if (sg_dst_len
< block_size
) {
376 /* Not enough room in the sg element or we're on the
377 * last piece of data (when using padding), so the
378 * output needs to be buffered into a blocksize chunk
381 op
->dst
.u
.dma
.address
= dst
->dm_wa
.dma
.address
;
382 op
->dst
.u
.dma
.offset
= 0;
383 op
->dst
.u
.dma
.length
= op
->src
.u
.dma
.length
;
385 /* Enough room in the sg element, but we need to
386 * adjust for any previously used area
388 op
->dst
.u
.dma
.address
= sg_dma_address(dst
->sg_wa
.sg
);
389 op
->dst
.u
.dma
.offset
= dst
->sg_wa
.sg_used
;
390 op
->dst
.u
.dma
.length
= op
->src
.u
.dma
.length
;
395 static void ccp_process_data(struct ccp_data
*src
, struct ccp_data
*dst
,
401 if (op
->dst
.u
.dma
.address
== dst
->dm_wa
.dma
.address
)
402 ccp_empty_queue_buf(dst
);
404 ccp_update_sg_workarea(&dst
->sg_wa
,
405 op
->dst
.u
.dma
.length
);
409 static int ccp_copy_to_from_sb(struct ccp_cmd_queue
*cmd_q
,
410 struct ccp_dm_workarea
*wa
, u32 jobid
, u32 sb
,
411 u32 byte_swap
, bool from
)
415 memset(&op
, 0, sizeof(op
));
423 op
.src
.type
= CCP_MEMTYPE_SB
;
425 op
.dst
.type
= CCP_MEMTYPE_SYSTEM
;
426 op
.dst
.u
.dma
.address
= wa
->dma
.address
;
427 op
.dst
.u
.dma
.length
= wa
->length
;
429 op
.src
.type
= CCP_MEMTYPE_SYSTEM
;
430 op
.src
.u
.dma
.address
= wa
->dma
.address
;
431 op
.src
.u
.dma
.length
= wa
->length
;
432 op
.dst
.type
= CCP_MEMTYPE_SB
;
436 op
.u
.passthru
.byte_swap
= byte_swap
;
438 return cmd_q
->ccp
->vdata
->perform
->passthru(&op
);
441 static int ccp_copy_to_sb(struct ccp_cmd_queue
*cmd_q
,
442 struct ccp_dm_workarea
*wa
, u32 jobid
, u32 sb
,
445 return ccp_copy_to_from_sb(cmd_q
, wa
, jobid
, sb
, byte_swap
, false);
448 static int ccp_copy_from_sb(struct ccp_cmd_queue
*cmd_q
,
449 struct ccp_dm_workarea
*wa
, u32 jobid
, u32 sb
,
452 return ccp_copy_to_from_sb(cmd_q
, wa
, jobid
, sb
, byte_swap
, true);
455 static int ccp_run_aes_cmac_cmd(struct ccp_cmd_queue
*cmd_q
,
458 struct ccp_aes_engine
*aes
= &cmd
->u
.aes
;
459 struct ccp_dm_workarea key
, ctx
;
462 unsigned int dm_offset
;
465 if (!((aes
->key_len
== AES_KEYSIZE_128
) ||
466 (aes
->key_len
== AES_KEYSIZE_192
) ||
467 (aes
->key_len
== AES_KEYSIZE_256
)))
470 if (aes
->src_len
& (AES_BLOCK_SIZE
- 1))
473 if (aes
->iv_len
!= AES_BLOCK_SIZE
)
476 if (!aes
->key
|| !aes
->iv
|| !aes
->src
)
479 if (aes
->cmac_final
) {
480 if (aes
->cmac_key_len
!= AES_BLOCK_SIZE
)
487 BUILD_BUG_ON(CCP_AES_KEY_SB_COUNT
!= 1);
488 BUILD_BUG_ON(CCP_AES_CTX_SB_COUNT
!= 1);
491 memset(&op
, 0, sizeof(op
));
493 op
.jobid
= CCP_NEW_JOBID(cmd_q
->ccp
);
494 op
.sb_key
= cmd_q
->sb_key
;
495 op
.sb_ctx
= cmd_q
->sb_ctx
;
497 op
.u
.aes
.type
= aes
->type
;
498 op
.u
.aes
.mode
= aes
->mode
;
499 op
.u
.aes
.action
= aes
->action
;
501 /* All supported key sizes fit in a single (32-byte) SB entry
502 * and must be in little endian format. Use the 256-bit byte
503 * swap passthru option to convert from big endian to little
506 ret
= ccp_init_dm_workarea(&key
, cmd_q
,
507 CCP_AES_KEY_SB_COUNT
* CCP_SB_BYTES
,
512 dm_offset
= CCP_SB_BYTES
- aes
->key_len
;
513 ccp_set_dm_area(&key
, dm_offset
, aes
->key
, 0, aes
->key_len
);
514 ret
= ccp_copy_to_sb(cmd_q
, &key
, op
.jobid
, op
.sb_key
,
515 CCP_PASSTHRU_BYTESWAP_256BIT
);
517 cmd
->engine_error
= cmd_q
->cmd_error
;
521 /* The AES context fits in a single (32-byte) SB entry and
522 * must be in little endian format. Use the 256-bit byte swap
523 * passthru option to convert from big endian to little endian.
525 ret
= ccp_init_dm_workarea(&ctx
, cmd_q
,
526 CCP_AES_CTX_SB_COUNT
* CCP_SB_BYTES
,
531 dm_offset
= CCP_SB_BYTES
- AES_BLOCK_SIZE
;
532 ccp_set_dm_area(&ctx
, dm_offset
, aes
->iv
, 0, aes
->iv_len
);
533 ret
= ccp_copy_to_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
534 CCP_PASSTHRU_BYTESWAP_256BIT
);
536 cmd
->engine_error
= cmd_q
->cmd_error
;
540 /* Send data to the CCP AES engine */
541 ret
= ccp_init_data(&src
, cmd_q
, aes
->src
, aes
->src_len
,
542 AES_BLOCK_SIZE
, DMA_TO_DEVICE
);
546 while (src
.sg_wa
.bytes_left
) {
547 ccp_prepare_data(&src
, NULL
, &op
, AES_BLOCK_SIZE
, true);
548 if (aes
->cmac_final
&& !src
.sg_wa
.bytes_left
) {
551 /* Push the K1/K2 key to the CCP now */
552 ret
= ccp_copy_from_sb(cmd_q
, &ctx
, op
.jobid
,
554 CCP_PASSTHRU_BYTESWAP_256BIT
);
556 cmd
->engine_error
= cmd_q
->cmd_error
;
560 ccp_set_dm_area(&ctx
, 0, aes
->cmac_key
, 0,
562 ret
= ccp_copy_to_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
563 CCP_PASSTHRU_BYTESWAP_256BIT
);
565 cmd
->engine_error
= cmd_q
->cmd_error
;
570 ret
= cmd_q
->ccp
->vdata
->perform
->aes(&op
);
572 cmd
->engine_error
= cmd_q
->cmd_error
;
576 ccp_process_data(&src
, NULL
, &op
);
579 /* Retrieve the AES context - convert from LE to BE using
580 * 32-byte (256-bit) byteswapping
582 ret
= ccp_copy_from_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
583 CCP_PASSTHRU_BYTESWAP_256BIT
);
585 cmd
->engine_error
= cmd_q
->cmd_error
;
589 /* ...but we only need AES_BLOCK_SIZE bytes */
590 dm_offset
= CCP_SB_BYTES
- AES_BLOCK_SIZE
;
591 ccp_get_dm_area(&ctx
, dm_offset
, aes
->iv
, 0, aes
->iv_len
);
594 ccp_free_data(&src
, cmd_q
);
605 static int ccp_run_aes_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
607 struct ccp_aes_engine
*aes
= &cmd
->u
.aes
;
608 struct ccp_dm_workarea key
, ctx
;
609 struct ccp_data src
, dst
;
611 unsigned int dm_offset
;
612 bool in_place
= false;
615 if (aes
->mode
== CCP_AES_MODE_CMAC
)
616 return ccp_run_aes_cmac_cmd(cmd_q
, cmd
);
618 if (!((aes
->key_len
== AES_KEYSIZE_128
) ||
619 (aes
->key_len
== AES_KEYSIZE_192
) ||
620 (aes
->key_len
== AES_KEYSIZE_256
)))
623 if (((aes
->mode
== CCP_AES_MODE_ECB
) ||
624 (aes
->mode
== CCP_AES_MODE_CBC
) ||
625 (aes
->mode
== CCP_AES_MODE_CFB
)) &&
626 (aes
->src_len
& (AES_BLOCK_SIZE
- 1)))
629 if (!aes
->key
|| !aes
->src
|| !aes
->dst
)
632 if (aes
->mode
!= CCP_AES_MODE_ECB
) {
633 if (aes
->iv_len
!= AES_BLOCK_SIZE
)
640 BUILD_BUG_ON(CCP_AES_KEY_SB_COUNT
!= 1);
641 BUILD_BUG_ON(CCP_AES_CTX_SB_COUNT
!= 1);
644 memset(&op
, 0, sizeof(op
));
646 op
.jobid
= CCP_NEW_JOBID(cmd_q
->ccp
);
647 op
.sb_key
= cmd_q
->sb_key
;
648 op
.sb_ctx
= cmd_q
->sb_ctx
;
649 op
.init
= (aes
->mode
== CCP_AES_MODE_ECB
) ? 0 : 1;
650 op
.u
.aes
.type
= aes
->type
;
651 op
.u
.aes
.mode
= aes
->mode
;
652 op
.u
.aes
.action
= aes
->action
;
654 /* All supported key sizes fit in a single (32-byte) SB entry
655 * and must be in little endian format. Use the 256-bit byte
656 * swap passthru option to convert from big endian to little
659 ret
= ccp_init_dm_workarea(&key
, cmd_q
,
660 CCP_AES_KEY_SB_COUNT
* CCP_SB_BYTES
,
665 dm_offset
= CCP_SB_BYTES
- aes
->key_len
;
666 ccp_set_dm_area(&key
, dm_offset
, aes
->key
, 0, aes
->key_len
);
667 ret
= ccp_copy_to_sb(cmd_q
, &key
, op
.jobid
, op
.sb_key
,
668 CCP_PASSTHRU_BYTESWAP_256BIT
);
670 cmd
->engine_error
= cmd_q
->cmd_error
;
674 /* The AES context fits in a single (32-byte) SB entry and
675 * must be in little endian format. Use the 256-bit byte swap
676 * passthru option to convert from big endian to little endian.
678 ret
= ccp_init_dm_workarea(&ctx
, cmd_q
,
679 CCP_AES_CTX_SB_COUNT
* CCP_SB_BYTES
,
684 if (aes
->mode
!= CCP_AES_MODE_ECB
) {
685 /* Load the AES context - convert to LE */
686 dm_offset
= CCP_SB_BYTES
- AES_BLOCK_SIZE
;
687 ccp_set_dm_area(&ctx
, dm_offset
, aes
->iv
, 0, aes
->iv_len
);
688 ret
= ccp_copy_to_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
689 CCP_PASSTHRU_BYTESWAP_256BIT
);
691 cmd
->engine_error
= cmd_q
->cmd_error
;
696 /* Prepare the input and output data workareas. For in-place
697 * operations we need to set the dma direction to BIDIRECTIONAL
698 * and copy the src workarea to the dst workarea.
700 if (sg_virt(aes
->src
) == sg_virt(aes
->dst
))
703 ret
= ccp_init_data(&src
, cmd_q
, aes
->src
, aes
->src_len
,
705 in_place
? DMA_BIDIRECTIONAL
: DMA_TO_DEVICE
);
712 ret
= ccp_init_data(&dst
, cmd_q
, aes
->dst
, aes
->src_len
,
713 AES_BLOCK_SIZE
, DMA_FROM_DEVICE
);
718 /* Send data to the CCP AES engine */
719 while (src
.sg_wa
.bytes_left
) {
720 ccp_prepare_data(&src
, &dst
, &op
, AES_BLOCK_SIZE
, true);
721 if (!src
.sg_wa
.bytes_left
) {
724 /* Since we don't retrieve the AES context in ECB
725 * mode we have to wait for the operation to complete
726 * on the last piece of data
728 if (aes
->mode
== CCP_AES_MODE_ECB
)
732 ret
= cmd_q
->ccp
->vdata
->perform
->aes(&op
);
734 cmd
->engine_error
= cmd_q
->cmd_error
;
738 ccp_process_data(&src
, &dst
, &op
);
741 if (aes
->mode
!= CCP_AES_MODE_ECB
) {
742 /* Retrieve the AES context - convert from LE to BE using
743 * 32-byte (256-bit) byteswapping
745 ret
= ccp_copy_from_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
746 CCP_PASSTHRU_BYTESWAP_256BIT
);
748 cmd
->engine_error
= cmd_q
->cmd_error
;
752 /* ...but we only need AES_BLOCK_SIZE bytes */
753 dm_offset
= CCP_SB_BYTES
- AES_BLOCK_SIZE
;
754 ccp_get_dm_area(&ctx
, dm_offset
, aes
->iv
, 0, aes
->iv_len
);
759 ccp_free_data(&dst
, cmd_q
);
762 ccp_free_data(&src
, cmd_q
);
773 static int ccp_run_xts_aes_cmd(struct ccp_cmd_queue
*cmd_q
,
776 struct ccp_xts_aes_engine
*xts
= &cmd
->u
.xts
;
777 struct ccp_dm_workarea key
, ctx
;
778 struct ccp_data src
, dst
;
780 unsigned int unit_size
, dm_offset
;
781 bool in_place
= false;
784 switch (xts
->unit_size
) {
785 case CCP_XTS_AES_UNIT_SIZE_16
:
788 case CCP_XTS_AES_UNIT_SIZE_512
:
791 case CCP_XTS_AES_UNIT_SIZE_1024
:
794 case CCP_XTS_AES_UNIT_SIZE_2048
:
797 case CCP_XTS_AES_UNIT_SIZE_4096
:
805 if (xts
->key_len
!= AES_KEYSIZE_128
)
808 if (!xts
->final
&& (xts
->src_len
& (AES_BLOCK_SIZE
- 1)))
811 if (xts
->iv_len
!= AES_BLOCK_SIZE
)
814 if (!xts
->key
|| !xts
->iv
|| !xts
->src
|| !xts
->dst
)
817 BUILD_BUG_ON(CCP_XTS_AES_KEY_SB_COUNT
!= 1);
818 BUILD_BUG_ON(CCP_XTS_AES_CTX_SB_COUNT
!= 1);
821 memset(&op
, 0, sizeof(op
));
823 op
.jobid
= CCP_NEW_JOBID(cmd_q
->ccp
);
824 op
.sb_key
= cmd_q
->sb_key
;
825 op
.sb_ctx
= cmd_q
->sb_ctx
;
827 op
.u
.xts
.action
= xts
->action
;
828 op
.u
.xts
.unit_size
= xts
->unit_size
;
830 /* All supported key sizes fit in a single (32-byte) SB entry
831 * and must be in little endian format. Use the 256-bit byte
832 * swap passthru option to convert from big endian to little
835 ret
= ccp_init_dm_workarea(&key
, cmd_q
,
836 CCP_XTS_AES_KEY_SB_COUNT
* CCP_SB_BYTES
,
841 dm_offset
= CCP_SB_BYTES
- AES_KEYSIZE_128
;
842 ccp_set_dm_area(&key
, dm_offset
, xts
->key
, 0, xts
->key_len
);
843 ccp_set_dm_area(&key
, 0, xts
->key
, dm_offset
, xts
->key_len
);
844 ret
= ccp_copy_to_sb(cmd_q
, &key
, op
.jobid
, op
.sb_key
,
845 CCP_PASSTHRU_BYTESWAP_256BIT
);
847 cmd
->engine_error
= cmd_q
->cmd_error
;
851 /* The AES context fits in a single (32-byte) SB entry and
852 * for XTS is already in little endian format so no byte swapping
855 ret
= ccp_init_dm_workarea(&ctx
, cmd_q
,
856 CCP_XTS_AES_CTX_SB_COUNT
* CCP_SB_BYTES
,
861 ccp_set_dm_area(&ctx
, 0, xts
->iv
, 0, xts
->iv_len
);
862 ret
= ccp_copy_to_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
863 CCP_PASSTHRU_BYTESWAP_NOOP
);
865 cmd
->engine_error
= cmd_q
->cmd_error
;
869 /* Prepare the input and output data workareas. For in-place
870 * operations we need to set the dma direction to BIDIRECTIONAL
871 * and copy the src workarea to the dst workarea.
873 if (sg_virt(xts
->src
) == sg_virt(xts
->dst
))
876 ret
= ccp_init_data(&src
, cmd_q
, xts
->src
, xts
->src_len
,
878 in_place
? DMA_BIDIRECTIONAL
: DMA_TO_DEVICE
);
885 ret
= ccp_init_data(&dst
, cmd_q
, xts
->dst
, xts
->src_len
,
886 unit_size
, DMA_FROM_DEVICE
);
891 /* Send data to the CCP AES engine */
892 while (src
.sg_wa
.bytes_left
) {
893 ccp_prepare_data(&src
, &dst
, &op
, unit_size
, true);
894 if (!src
.sg_wa
.bytes_left
)
897 ret
= cmd_q
->ccp
->vdata
->perform
->xts_aes(&op
);
899 cmd
->engine_error
= cmd_q
->cmd_error
;
903 ccp_process_data(&src
, &dst
, &op
);
906 /* Retrieve the AES context - convert from LE to BE using
907 * 32-byte (256-bit) byteswapping
909 ret
= ccp_copy_from_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
910 CCP_PASSTHRU_BYTESWAP_256BIT
);
912 cmd
->engine_error
= cmd_q
->cmd_error
;
916 /* ...but we only need AES_BLOCK_SIZE bytes */
917 dm_offset
= CCP_SB_BYTES
- AES_BLOCK_SIZE
;
918 ccp_get_dm_area(&ctx
, dm_offset
, xts
->iv
, 0, xts
->iv_len
);
922 ccp_free_data(&dst
, cmd_q
);
925 ccp_free_data(&src
, cmd_q
);
936 static int ccp_run_sha_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
938 struct ccp_sha_engine
*sha
= &cmd
->u
.sha
;
939 struct ccp_dm_workarea ctx
;
942 unsigned int ioffset
, ooffset
;
943 unsigned int digest_size
;
952 if (sha
->ctx_len
< SHA1_DIGEST_SIZE
)
954 block_size
= SHA1_BLOCK_SIZE
;
956 case CCP_SHA_TYPE_224
:
957 if (sha
->ctx_len
< SHA224_DIGEST_SIZE
)
959 block_size
= SHA224_BLOCK_SIZE
;
961 case CCP_SHA_TYPE_256
:
962 if (sha
->ctx_len
< SHA256_DIGEST_SIZE
)
964 block_size
= SHA256_BLOCK_SIZE
;
973 if (!sha
->final
&& (sha
->src_len
& (block_size
- 1)))
976 /* The version 3 device can't handle zero-length input */
977 if (cmd_q
->ccp
->vdata
->version
== CCP_VERSION(3, 0)) {
980 unsigned int digest_len
;
983 /* Not final, just return */
987 /* CCP can't do a zero length sha operation so the
988 * caller must buffer the data.
993 /* The CCP cannot perform zero-length sha operations
994 * so the caller is required to buffer data for the
995 * final operation. However, a sha operation for a
996 * message with a total length of zero is valid so
997 * known values are required to supply the result.
1000 case CCP_SHA_TYPE_1
:
1001 sha_zero
= sha1_zero_message_hash
;
1002 digest_len
= SHA1_DIGEST_SIZE
;
1004 case CCP_SHA_TYPE_224
:
1005 sha_zero
= sha224_zero_message_hash
;
1006 digest_len
= SHA224_DIGEST_SIZE
;
1008 case CCP_SHA_TYPE_256
:
1009 sha_zero
= sha256_zero_message_hash
;
1010 digest_len
= SHA256_DIGEST_SIZE
;
1016 scatterwalk_map_and_copy((void *)sha_zero
, sha
->ctx
, 0,
1023 /* Set variables used throughout */
1024 switch (sha
->type
) {
1025 case CCP_SHA_TYPE_1
:
1026 digest_size
= SHA1_DIGEST_SIZE
;
1027 init
= (void *) ccp_sha1_init
;
1028 ctx_size
= SHA1_DIGEST_SIZE
;
1030 if (cmd_q
->ccp
->vdata
->version
!= CCP_VERSION(3, 0))
1031 ooffset
= ioffset
= CCP_SB_BYTES
- SHA1_DIGEST_SIZE
;
1033 ooffset
= ioffset
= 0;
1035 case CCP_SHA_TYPE_224
:
1036 digest_size
= SHA224_DIGEST_SIZE
;
1037 init
= (void *) ccp_sha224_init
;
1038 ctx_size
= SHA256_DIGEST_SIZE
;
1041 if (cmd_q
->ccp
->vdata
->version
!= CCP_VERSION(3, 0))
1042 ooffset
= CCP_SB_BYTES
- SHA224_DIGEST_SIZE
;
1046 case CCP_SHA_TYPE_256
:
1047 digest_size
= SHA256_DIGEST_SIZE
;
1048 init
= (void *) ccp_sha256_init
;
1049 ctx_size
= SHA256_DIGEST_SIZE
;
1051 ooffset
= ioffset
= 0;
1058 /* For zero-length plaintext the src pointer is ignored;
1059 * otherwise both parts must be valid
1061 if (sha
->src_len
&& !sha
->src
)
1064 memset(&op
, 0, sizeof(op
));
1066 op
.jobid
= CCP_NEW_JOBID(cmd_q
->ccp
);
1067 op
.sb_ctx
= cmd_q
->sb_ctx
; /* Pre-allocated */
1068 op
.u
.sha
.type
= sha
->type
;
1069 op
.u
.sha
.msg_bits
= sha
->msg_bits
;
1071 ret
= ccp_init_dm_workarea(&ctx
, cmd_q
, sb_count
* CCP_SB_BYTES
,
1076 switch (sha
->type
) {
1077 case CCP_SHA_TYPE_1
:
1078 case CCP_SHA_TYPE_224
:
1079 case CCP_SHA_TYPE_256
:
1080 memcpy(ctx
.address
+ ioffset
, init
, ctx_size
);
1087 /* Restore the context */
1088 ccp_set_dm_area(&ctx
, 0, sha
->ctx
, 0,
1089 sb_count
* CCP_SB_BYTES
);
1092 ret
= ccp_copy_to_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
1093 CCP_PASSTHRU_BYTESWAP_256BIT
);
1095 cmd
->engine_error
= cmd_q
->cmd_error
;
1100 /* Send data to the CCP SHA engine; block_size is set above */
1101 ret
= ccp_init_data(&src
, cmd_q
, sha
->src
, sha
->src_len
,
1102 block_size
, DMA_TO_DEVICE
);
1106 while (src
.sg_wa
.bytes_left
) {
1107 ccp_prepare_data(&src
, NULL
, &op
, block_size
, false);
1108 if (sha
->final
&& !src
.sg_wa
.bytes_left
)
1111 ret
= cmd_q
->ccp
->vdata
->perform
->sha(&op
);
1113 cmd
->engine_error
= cmd_q
->cmd_error
;
1117 ccp_process_data(&src
, NULL
, &op
);
1121 ret
= cmd_q
->ccp
->vdata
->perform
->sha(&op
);
1123 cmd
->engine_error
= cmd_q
->cmd_error
;
1128 /* Retrieve the SHA context - convert from LE to BE using
1129 * 32-byte (256-bit) byteswapping to BE
1131 ret
= ccp_copy_from_sb(cmd_q
, &ctx
, op
.jobid
, op
.sb_ctx
,
1132 CCP_PASSTHRU_BYTESWAP_256BIT
);
1134 cmd
->engine_error
= cmd_q
->cmd_error
;
1139 /* Finishing up, so get the digest */
1140 switch (sha
->type
) {
1141 case CCP_SHA_TYPE_1
:
1142 case CCP_SHA_TYPE_224
:
1143 case CCP_SHA_TYPE_256
:
1144 ccp_get_dm_area(&ctx
, ooffset
,
1153 /* Stash the context */
1154 ccp_get_dm_area(&ctx
, 0, sha
->ctx
, 0,
1155 sb_count
* CCP_SB_BYTES
);
1158 if (sha
->final
&& sha
->opad
) {
1159 /* HMAC operation, recursively perform final SHA */
1160 struct ccp_cmd hmac_cmd
;
1161 struct scatterlist sg
;
1164 if (sha
->opad_len
!= block_size
) {
1169 hmac_buf
= kmalloc(block_size
+ digest_size
, GFP_KERNEL
);
1174 sg_init_one(&sg
, hmac_buf
, block_size
+ digest_size
);
1176 scatterwalk_map_and_copy(hmac_buf
, sha
->opad
, 0, block_size
, 0);
1177 switch (sha
->type
) {
1178 case CCP_SHA_TYPE_1
:
1179 case CCP_SHA_TYPE_224
:
1180 case CCP_SHA_TYPE_256
:
1181 memcpy(hmac_buf
+ block_size
,
1182 ctx
.address
+ ooffset
,
1190 memset(&hmac_cmd
, 0, sizeof(hmac_cmd
));
1191 hmac_cmd
.engine
= CCP_ENGINE_SHA
;
1192 hmac_cmd
.u
.sha
.type
= sha
->type
;
1193 hmac_cmd
.u
.sha
.ctx
= sha
->ctx
;
1194 hmac_cmd
.u
.sha
.ctx_len
= sha
->ctx_len
;
1195 hmac_cmd
.u
.sha
.src
= &sg
;
1196 hmac_cmd
.u
.sha
.src_len
= block_size
+ digest_size
;
1197 hmac_cmd
.u
.sha
.opad
= NULL
;
1198 hmac_cmd
.u
.sha
.opad_len
= 0;
1199 hmac_cmd
.u
.sha
.first
= 1;
1200 hmac_cmd
.u
.sha
.final
= 1;
1201 hmac_cmd
.u
.sha
.msg_bits
= (block_size
+ digest_size
) << 3;
1203 ret
= ccp_run_sha_cmd(cmd_q
, &hmac_cmd
);
1205 cmd
->engine_error
= hmac_cmd
.engine_error
;
1212 ccp_free_data(&src
, cmd_q
);
1220 static int ccp_run_rsa_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
1222 struct ccp_rsa_engine
*rsa
= &cmd
->u
.rsa
;
1223 struct ccp_dm_workarea exp
, src
;
1224 struct ccp_data dst
;
1226 unsigned int sb_count
, i_len
, o_len
;
1229 if (rsa
->key_size
> CCP_RSA_MAX_WIDTH
)
1232 if (!rsa
->exp
|| !rsa
->mod
|| !rsa
->src
|| !rsa
->dst
)
1235 /* The RSA modulus must precede the message being acted upon, so
1236 * it must be copied to a DMA area where the message and the
1237 * modulus can be concatenated. Therefore the input buffer
1238 * length required is twice the output buffer length (which
1239 * must be a multiple of 256-bits).
1241 o_len
= ((rsa
->key_size
+ 255) / 256) * 32;
1244 sb_count
= o_len
/ CCP_SB_BYTES
;
1246 memset(&op
, 0, sizeof(op
));
1248 op
.jobid
= ccp_gen_jobid(cmd_q
->ccp
);
1249 op
.sb_key
= cmd_q
->ccp
->vdata
->perform
->sballoc(cmd_q
, sb_count
);
1254 /* The RSA exponent may span multiple (32-byte) SB entries and must
1255 * be in little endian format. Reverse copy each 32-byte chunk
1256 * of the exponent (En chunk to E0 chunk, E(n-1) chunk to E1 chunk)
1257 * and each byte within that chunk and do not perform any byte swap
1258 * operations on the passthru operation.
1260 ret
= ccp_init_dm_workarea(&exp
, cmd_q
, o_len
, DMA_TO_DEVICE
);
1264 ret
= ccp_reverse_set_dm_area(&exp
, rsa
->exp
, rsa
->exp_len
,
1265 CCP_SB_BYTES
, false);
1268 ret
= ccp_copy_to_sb(cmd_q
, &exp
, op
.jobid
, op
.sb_key
,
1269 CCP_PASSTHRU_BYTESWAP_NOOP
);
1271 cmd
->engine_error
= cmd_q
->cmd_error
;
1275 /* Concatenate the modulus and the message. Both the modulus and
1276 * the operands must be in little endian format. Since the input
1277 * is in big endian format it must be converted.
1279 ret
= ccp_init_dm_workarea(&src
, cmd_q
, i_len
, DMA_TO_DEVICE
);
1283 ret
= ccp_reverse_set_dm_area(&src
, rsa
->mod
, rsa
->mod_len
,
1284 CCP_SB_BYTES
, false);
1287 src
.address
+= o_len
; /* Adjust the address for the copy operation */
1288 ret
= ccp_reverse_set_dm_area(&src
, rsa
->src
, rsa
->src_len
,
1289 CCP_SB_BYTES
, false);
1292 src
.address
-= o_len
; /* Reset the address to original value */
1294 /* Prepare the output area for the operation */
1295 ret
= ccp_init_data(&dst
, cmd_q
, rsa
->dst
, rsa
->mod_len
,
1296 o_len
, DMA_FROM_DEVICE
);
1301 op
.src
.u
.dma
.address
= src
.dma
.address
;
1302 op
.src
.u
.dma
.offset
= 0;
1303 op
.src
.u
.dma
.length
= i_len
;
1304 op
.dst
.u
.dma
.address
= dst
.dm_wa
.dma
.address
;
1305 op
.dst
.u
.dma
.offset
= 0;
1306 op
.dst
.u
.dma
.length
= o_len
;
1308 op
.u
.rsa
.mod_size
= rsa
->key_size
;
1309 op
.u
.rsa
.input_len
= i_len
;
1311 ret
= cmd_q
->ccp
->vdata
->perform
->rsa(&op
);
1313 cmd
->engine_error
= cmd_q
->cmd_error
;
1317 ccp_reverse_get_dm_area(&dst
.dm_wa
, rsa
->dst
, rsa
->mod_len
);
1320 ccp_free_data(&dst
, cmd_q
);
1329 cmd_q
->ccp
->vdata
->perform
->sbfree(cmd_q
, op
.sb_key
, sb_count
);
1334 static int ccp_run_passthru_cmd(struct ccp_cmd_queue
*cmd_q
,
1335 struct ccp_cmd
*cmd
)
1337 struct ccp_passthru_engine
*pt
= &cmd
->u
.passthru
;
1338 struct ccp_dm_workarea mask
;
1339 struct ccp_data src
, dst
;
1341 bool in_place
= false;
1345 if (!pt
->final
&& (pt
->src_len
& (CCP_PASSTHRU_BLOCKSIZE
- 1)))
1348 if (!pt
->src
|| !pt
->dst
)
1351 if (pt
->bit_mod
!= CCP_PASSTHRU_BITWISE_NOOP
) {
1352 if (pt
->mask_len
!= CCP_PASSTHRU_MASKSIZE
)
1358 BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT
!= 1);
1360 memset(&op
, 0, sizeof(op
));
1362 op
.jobid
= CCP_NEW_JOBID(cmd_q
->ccp
);
1364 if (pt
->bit_mod
!= CCP_PASSTHRU_BITWISE_NOOP
) {
1366 op
.sb_key
= cmd_q
->sb_key
;
1368 ret
= ccp_init_dm_workarea(&mask
, cmd_q
,
1369 CCP_PASSTHRU_SB_COUNT
*
1375 ccp_set_dm_area(&mask
, 0, pt
->mask
, 0, pt
->mask_len
);
1376 ret
= ccp_copy_to_sb(cmd_q
, &mask
, op
.jobid
, op
.sb_key
,
1377 CCP_PASSTHRU_BYTESWAP_NOOP
);
1379 cmd
->engine_error
= cmd_q
->cmd_error
;
1384 /* Prepare the input and output data workareas. For in-place
1385 * operations we need to set the dma direction to BIDIRECTIONAL
1386 * and copy the src workarea to the dst workarea.
1388 if (sg_virt(pt
->src
) == sg_virt(pt
->dst
))
1391 ret
= ccp_init_data(&src
, cmd_q
, pt
->src
, pt
->src_len
,
1392 CCP_PASSTHRU_MASKSIZE
,
1393 in_place
? DMA_BIDIRECTIONAL
: DMA_TO_DEVICE
);
1400 ret
= ccp_init_data(&dst
, cmd_q
, pt
->dst
, pt
->src_len
,
1401 CCP_PASSTHRU_MASKSIZE
, DMA_FROM_DEVICE
);
1406 /* Send data to the CCP Passthru engine
1407 * Because the CCP engine works on a single source and destination
1408 * dma address at a time, each entry in the source scatterlist
1409 * (after the dma_map_sg call) must be less than or equal to the
1410 * (remaining) length in the destination scatterlist entry and the
1411 * length must be a multiple of CCP_PASSTHRU_BLOCKSIZE
1413 dst
.sg_wa
.sg_used
= 0;
1414 for (i
= 1; i
<= src
.sg_wa
.dma_count
; i
++) {
1415 if (!dst
.sg_wa
.sg
||
1416 (dst
.sg_wa
.sg
->length
< src
.sg_wa
.sg
->length
)) {
1421 if (i
== src
.sg_wa
.dma_count
) {
1426 op
.src
.type
= CCP_MEMTYPE_SYSTEM
;
1427 op
.src
.u
.dma
.address
= sg_dma_address(src
.sg_wa
.sg
);
1428 op
.src
.u
.dma
.offset
= 0;
1429 op
.src
.u
.dma
.length
= sg_dma_len(src
.sg_wa
.sg
);
1431 op
.dst
.type
= CCP_MEMTYPE_SYSTEM
;
1432 op
.dst
.u
.dma
.address
= sg_dma_address(dst
.sg_wa
.sg
);
1433 op
.dst
.u
.dma
.offset
= dst
.sg_wa
.sg_used
;
1434 op
.dst
.u
.dma
.length
= op
.src
.u
.dma
.length
;
1436 ret
= cmd_q
->ccp
->vdata
->perform
->passthru(&op
);
1438 cmd
->engine_error
= cmd_q
->cmd_error
;
1442 dst
.sg_wa
.sg_used
+= src
.sg_wa
.sg
->length
;
1443 if (dst
.sg_wa
.sg_used
== dst
.sg_wa
.sg
->length
) {
1444 dst
.sg_wa
.sg
= sg_next(dst
.sg_wa
.sg
);
1445 dst
.sg_wa
.sg_used
= 0;
1447 src
.sg_wa
.sg
= sg_next(src
.sg_wa
.sg
);
1452 ccp_free_data(&dst
, cmd_q
);
1455 ccp_free_data(&src
, cmd_q
);
1458 if (pt
->bit_mod
!= CCP_PASSTHRU_BITWISE_NOOP
)
1464 static int ccp_run_passthru_nomap_cmd(struct ccp_cmd_queue
*cmd_q
,
1465 struct ccp_cmd
*cmd
)
1467 struct ccp_passthru_nomap_engine
*pt
= &cmd
->u
.passthru_nomap
;
1468 struct ccp_dm_workarea mask
;
1472 if (!pt
->final
&& (pt
->src_len
& (CCP_PASSTHRU_BLOCKSIZE
- 1)))
1475 if (!pt
->src_dma
|| !pt
->dst_dma
)
1478 if (pt
->bit_mod
!= CCP_PASSTHRU_BITWISE_NOOP
) {
1479 if (pt
->mask_len
!= CCP_PASSTHRU_MASKSIZE
)
1485 BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT
!= 1);
1487 memset(&op
, 0, sizeof(op
));
1489 op
.jobid
= ccp_gen_jobid(cmd_q
->ccp
);
1491 if (pt
->bit_mod
!= CCP_PASSTHRU_BITWISE_NOOP
) {
1493 op
.sb_key
= cmd_q
->sb_key
;
1495 mask
.length
= pt
->mask_len
;
1496 mask
.dma
.address
= pt
->mask
;
1497 mask
.dma
.length
= pt
->mask_len
;
1499 ret
= ccp_copy_to_sb(cmd_q
, &mask
, op
.jobid
, op
.sb_key
,
1500 CCP_PASSTHRU_BYTESWAP_NOOP
);
1502 cmd
->engine_error
= cmd_q
->cmd_error
;
1507 /* Send data to the CCP Passthru engine */
1511 op
.src
.type
= CCP_MEMTYPE_SYSTEM
;
1512 op
.src
.u
.dma
.address
= pt
->src_dma
;
1513 op
.src
.u
.dma
.offset
= 0;
1514 op
.src
.u
.dma
.length
= pt
->src_len
;
1516 op
.dst
.type
= CCP_MEMTYPE_SYSTEM
;
1517 op
.dst
.u
.dma
.address
= pt
->dst_dma
;
1518 op
.dst
.u
.dma
.offset
= 0;
1519 op
.dst
.u
.dma
.length
= pt
->src_len
;
1521 ret
= cmd_q
->ccp
->vdata
->perform
->passthru(&op
);
1523 cmd
->engine_error
= cmd_q
->cmd_error
;
1528 static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
1530 struct ccp_ecc_engine
*ecc
= &cmd
->u
.ecc
;
1531 struct ccp_dm_workarea src
, dst
;
1536 if (!ecc
->u
.mm
.operand_1
||
1537 (ecc
->u
.mm
.operand_1_len
> CCP_ECC_MODULUS_BYTES
))
1540 if (ecc
->function
!= CCP_ECC_FUNCTION_MINV_384BIT
)
1541 if (!ecc
->u
.mm
.operand_2
||
1542 (ecc
->u
.mm
.operand_2_len
> CCP_ECC_MODULUS_BYTES
))
1545 if (!ecc
->u
.mm
.result
||
1546 (ecc
->u
.mm
.result_len
< CCP_ECC_MODULUS_BYTES
))
1549 memset(&op
, 0, sizeof(op
));
1551 op
.jobid
= CCP_NEW_JOBID(cmd_q
->ccp
);
1553 /* Concatenate the modulus and the operands. Both the modulus and
1554 * the operands must be in little endian format. Since the input
1555 * is in big endian format it must be converted and placed in a
1556 * fixed length buffer.
1558 ret
= ccp_init_dm_workarea(&src
, cmd_q
, CCP_ECC_SRC_BUF_SIZE
,
1563 /* Save the workarea address since it is updated in order to perform
1568 /* Copy the ECC modulus */
1569 ret
= ccp_reverse_set_dm_area(&src
, ecc
->mod
, ecc
->mod_len
,
1570 CCP_ECC_OPERAND_SIZE
, false);
1573 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1575 /* Copy the first operand */
1576 ret
= ccp_reverse_set_dm_area(&src
, ecc
->u
.mm
.operand_1
,
1577 ecc
->u
.mm
.operand_1_len
,
1578 CCP_ECC_OPERAND_SIZE
, false);
1581 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1583 if (ecc
->function
!= CCP_ECC_FUNCTION_MINV_384BIT
) {
1584 /* Copy the second operand */
1585 ret
= ccp_reverse_set_dm_area(&src
, ecc
->u
.mm
.operand_2
,
1586 ecc
->u
.mm
.operand_2_len
,
1587 CCP_ECC_OPERAND_SIZE
, false);
1590 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1593 /* Restore the workarea address */
1596 /* Prepare the output area for the operation */
1597 ret
= ccp_init_dm_workarea(&dst
, cmd_q
, CCP_ECC_DST_BUF_SIZE
,
1603 op
.src
.u
.dma
.address
= src
.dma
.address
;
1604 op
.src
.u
.dma
.offset
= 0;
1605 op
.src
.u
.dma
.length
= src
.length
;
1606 op
.dst
.u
.dma
.address
= dst
.dma
.address
;
1607 op
.dst
.u
.dma
.offset
= 0;
1608 op
.dst
.u
.dma
.length
= dst
.length
;
1610 op
.u
.ecc
.function
= cmd
->u
.ecc
.function
;
1612 ret
= cmd_q
->ccp
->vdata
->perform
->ecc(&op
);
1614 cmd
->engine_error
= cmd_q
->cmd_error
;
1618 ecc
->ecc_result
= le16_to_cpup(
1619 (const __le16
*)(dst
.address
+ CCP_ECC_RESULT_OFFSET
));
1620 if (!(ecc
->ecc_result
& CCP_ECC_RESULT_SUCCESS
)) {
1625 /* Save the ECC result */
1626 ccp_reverse_get_dm_area(&dst
, ecc
->u
.mm
.result
, CCP_ECC_MODULUS_BYTES
);
1637 static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
1639 struct ccp_ecc_engine
*ecc
= &cmd
->u
.ecc
;
1640 struct ccp_dm_workarea src
, dst
;
1645 if (!ecc
->u
.pm
.point_1
.x
||
1646 (ecc
->u
.pm
.point_1
.x_len
> CCP_ECC_MODULUS_BYTES
) ||
1647 !ecc
->u
.pm
.point_1
.y
||
1648 (ecc
->u
.pm
.point_1
.y_len
> CCP_ECC_MODULUS_BYTES
))
1651 if (ecc
->function
== CCP_ECC_FUNCTION_PADD_384BIT
) {
1652 if (!ecc
->u
.pm
.point_2
.x
||
1653 (ecc
->u
.pm
.point_2
.x_len
> CCP_ECC_MODULUS_BYTES
) ||
1654 !ecc
->u
.pm
.point_2
.y
||
1655 (ecc
->u
.pm
.point_2
.y_len
> CCP_ECC_MODULUS_BYTES
))
1658 if (!ecc
->u
.pm
.domain_a
||
1659 (ecc
->u
.pm
.domain_a_len
> CCP_ECC_MODULUS_BYTES
))
1662 if (ecc
->function
== CCP_ECC_FUNCTION_PMUL_384BIT
)
1663 if (!ecc
->u
.pm
.scalar
||
1664 (ecc
->u
.pm
.scalar_len
> CCP_ECC_MODULUS_BYTES
))
1668 if (!ecc
->u
.pm
.result
.x
||
1669 (ecc
->u
.pm
.result
.x_len
< CCP_ECC_MODULUS_BYTES
) ||
1670 !ecc
->u
.pm
.result
.y
||
1671 (ecc
->u
.pm
.result
.y_len
< CCP_ECC_MODULUS_BYTES
))
1674 memset(&op
, 0, sizeof(op
));
1676 op
.jobid
= CCP_NEW_JOBID(cmd_q
->ccp
);
1678 /* Concatenate the modulus and the operands. Both the modulus and
1679 * the operands must be in little endian format. Since the input
1680 * is in big endian format it must be converted and placed in a
1681 * fixed length buffer.
1683 ret
= ccp_init_dm_workarea(&src
, cmd_q
, CCP_ECC_SRC_BUF_SIZE
,
1688 /* Save the workarea address since it is updated in order to perform
1693 /* Copy the ECC modulus */
1694 ret
= ccp_reverse_set_dm_area(&src
, ecc
->mod
, ecc
->mod_len
,
1695 CCP_ECC_OPERAND_SIZE
, false);
1698 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1700 /* Copy the first point X and Y coordinate */
1701 ret
= ccp_reverse_set_dm_area(&src
, ecc
->u
.pm
.point_1
.x
,
1702 ecc
->u
.pm
.point_1
.x_len
,
1703 CCP_ECC_OPERAND_SIZE
, false);
1706 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1707 ret
= ccp_reverse_set_dm_area(&src
, ecc
->u
.pm
.point_1
.y
,
1708 ecc
->u
.pm
.point_1
.y_len
,
1709 CCP_ECC_OPERAND_SIZE
, false);
1712 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1714 /* Set the first point Z coordinate to 1 */
1715 *src
.address
= 0x01;
1716 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1718 if (ecc
->function
== CCP_ECC_FUNCTION_PADD_384BIT
) {
1719 /* Copy the second point X and Y coordinate */
1720 ret
= ccp_reverse_set_dm_area(&src
, ecc
->u
.pm
.point_2
.x
,
1721 ecc
->u
.pm
.point_2
.x_len
,
1722 CCP_ECC_OPERAND_SIZE
, false);
1725 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1726 ret
= ccp_reverse_set_dm_area(&src
, ecc
->u
.pm
.point_2
.y
,
1727 ecc
->u
.pm
.point_2
.y_len
,
1728 CCP_ECC_OPERAND_SIZE
, false);
1731 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1733 /* Set the second point Z coordinate to 1 */
1734 *src
.address
= 0x01;
1735 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1737 /* Copy the Domain "a" parameter */
1738 ret
= ccp_reverse_set_dm_area(&src
, ecc
->u
.pm
.domain_a
,
1739 ecc
->u
.pm
.domain_a_len
,
1740 CCP_ECC_OPERAND_SIZE
, false);
1743 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1745 if (ecc
->function
== CCP_ECC_FUNCTION_PMUL_384BIT
) {
1746 /* Copy the scalar value */
1747 ret
= ccp_reverse_set_dm_area(&src
, ecc
->u
.pm
.scalar
,
1748 ecc
->u
.pm
.scalar_len
,
1749 CCP_ECC_OPERAND_SIZE
,
1753 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1757 /* Restore the workarea address */
1760 /* Prepare the output area for the operation */
1761 ret
= ccp_init_dm_workarea(&dst
, cmd_q
, CCP_ECC_DST_BUF_SIZE
,
1767 op
.src
.u
.dma
.address
= src
.dma
.address
;
1768 op
.src
.u
.dma
.offset
= 0;
1769 op
.src
.u
.dma
.length
= src
.length
;
1770 op
.dst
.u
.dma
.address
= dst
.dma
.address
;
1771 op
.dst
.u
.dma
.offset
= 0;
1772 op
.dst
.u
.dma
.length
= dst
.length
;
1774 op
.u
.ecc
.function
= cmd
->u
.ecc
.function
;
1776 ret
= cmd_q
->ccp
->vdata
->perform
->ecc(&op
);
1778 cmd
->engine_error
= cmd_q
->cmd_error
;
1782 ecc
->ecc_result
= le16_to_cpup(
1783 (const __le16
*)(dst
.address
+ CCP_ECC_RESULT_OFFSET
));
1784 if (!(ecc
->ecc_result
& CCP_ECC_RESULT_SUCCESS
)) {
1789 /* Save the workarea address since it is updated as we walk through
1790 * to copy the point math result
1794 /* Save the ECC result X and Y coordinates */
1795 ccp_reverse_get_dm_area(&dst
, ecc
->u
.pm
.result
.x
,
1796 CCP_ECC_MODULUS_BYTES
);
1797 dst
.address
+= CCP_ECC_OUTPUT_SIZE
;
1798 ccp_reverse_get_dm_area(&dst
, ecc
->u
.pm
.result
.y
,
1799 CCP_ECC_MODULUS_BYTES
);
1800 dst
.address
+= CCP_ECC_OUTPUT_SIZE
;
1802 /* Restore the workarea address */
1814 static int ccp_run_ecc_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
1816 struct ccp_ecc_engine
*ecc
= &cmd
->u
.ecc
;
1818 ecc
->ecc_result
= 0;
1821 (ecc
->mod_len
> CCP_ECC_MODULUS_BYTES
))
1824 switch (ecc
->function
) {
1825 case CCP_ECC_FUNCTION_MMUL_384BIT
:
1826 case CCP_ECC_FUNCTION_MADD_384BIT
:
1827 case CCP_ECC_FUNCTION_MINV_384BIT
:
1828 return ccp_run_ecc_mm_cmd(cmd_q
, cmd
);
1830 case CCP_ECC_FUNCTION_PADD_384BIT
:
1831 case CCP_ECC_FUNCTION_PMUL_384BIT
:
1832 case CCP_ECC_FUNCTION_PDBL_384BIT
:
1833 return ccp_run_ecc_pm_cmd(cmd_q
, cmd
);
1840 int ccp_run_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
1844 cmd
->engine_error
= 0;
1845 cmd_q
->cmd_error
= 0;
1846 cmd_q
->int_rcvd
= 0;
1847 cmd_q
->free_slots
= cmd_q
->ccp
->vdata
->perform
->get_free_slots(cmd_q
);
1849 switch (cmd
->engine
) {
1850 case CCP_ENGINE_AES
:
1851 ret
= ccp_run_aes_cmd(cmd_q
, cmd
);
1853 case CCP_ENGINE_XTS_AES_128
:
1854 ret
= ccp_run_xts_aes_cmd(cmd_q
, cmd
);
1856 case CCP_ENGINE_SHA
:
1857 ret
= ccp_run_sha_cmd(cmd_q
, cmd
);
1859 case CCP_ENGINE_RSA
:
1860 ret
= ccp_run_rsa_cmd(cmd_q
, cmd
);
1862 case CCP_ENGINE_PASSTHRU
:
1863 if (cmd
->flags
& CCP_CMD_PASSTHRU_NO_DMA_MAP
)
1864 ret
= ccp_run_passthru_nomap_cmd(cmd_q
, cmd
);
1866 ret
= ccp_run_passthru_cmd(cmd_q
, cmd
);
1868 case CCP_ENGINE_ECC
:
1869 ret
= ccp_run_ecc_cmd(cmd_q
, cmd
);