2 * AMD Cryptographic Coprocessor (CCP) driver
4 * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
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 <crypto/des.h>
20 #include <linux/ccp.h>
24 /* SHA initial context values */
25 static const __be32 ccp_sha1_init[SHA1_DIGEST_SIZE / sizeof(__be32)] = {
26 cpu_to_be32(SHA1_H0), cpu_to_be32(SHA1_H1),
27 cpu_to_be32(SHA1_H2), cpu_to_be32(SHA1_H3),
31 static const __be32 ccp_sha224_init[SHA256_DIGEST_SIZE / sizeof(__be32)] = {
32 cpu_to_be32(SHA224_H0), cpu_to_be32(SHA224_H1),
33 cpu_to_be32(SHA224_H2), cpu_to_be32(SHA224_H3),
34 cpu_to_be32(SHA224_H4), cpu_to_be32(SHA224_H5),
35 cpu_to_be32(SHA224_H6), cpu_to_be32(SHA224_H7),
38 static const __be32 ccp_sha256_init[SHA256_DIGEST_SIZE / sizeof(__be32)] = {
39 cpu_to_be32(SHA256_H0), cpu_to_be32(SHA256_H1),
40 cpu_to_be32(SHA256_H2), cpu_to_be32(SHA256_H3),
41 cpu_to_be32(SHA256_H4), cpu_to_be32(SHA256_H5),
42 cpu_to_be32(SHA256_H6), cpu_to_be32(SHA256_H7),
45 static const __be64 ccp_sha384_init[SHA512_DIGEST_SIZE / sizeof(__be64)] = {
46 cpu_to_be64(SHA384_H0), cpu_to_be64(SHA384_H1),
47 cpu_to_be64(SHA384_H2), cpu_to_be64(SHA384_H3),
48 cpu_to_be64(SHA384_H4), cpu_to_be64(SHA384_H5),
49 cpu_to_be64(SHA384_H6), cpu_to_be64(SHA384_H7),
52 static const __be64 ccp_sha512_init[SHA512_DIGEST_SIZE / sizeof(__be64)] = {
53 cpu_to_be64(SHA512_H0), cpu_to_be64(SHA512_H1),
54 cpu_to_be64(SHA512_H2), cpu_to_be64(SHA512_H3),
55 cpu_to_be64(SHA512_H4), cpu_to_be64(SHA512_H5),
56 cpu_to_be64(SHA512_H6), cpu_to_be64(SHA512_H7),
59 #define CCP_NEW_JOBID(ccp) ((ccp->vdata->version == CCP_VERSION(3, 0)) ? \
60 ccp_gen_jobid(ccp) : 0)
62 static u32 ccp_gen_jobid(struct ccp_device *ccp)
64 return atomic_inc_return(&ccp->current_id) & CCP_JOBID_MASK;
67 static void ccp_sg_free(struct ccp_sg_workarea *wa)
70 dma_unmap_sg(wa->dma_dev, wa->dma_sg, wa->nents, wa->dma_dir);
75 static int ccp_init_sg_workarea(struct ccp_sg_workarea *wa, struct device *dev,
76 struct scatterlist *sg, u64 len,
77 enum dma_data_direction dma_dir)
79 memset(wa, 0, sizeof(*wa));
85 wa->nents = sg_nents_for_len(sg, len);
95 if (dma_dir == DMA_NONE)
100 wa->dma_dir = dma_dir;
101 wa->dma_count = dma_map_sg(dev, sg, wa->nents, dma_dir);
108 static void ccp_update_sg_workarea(struct ccp_sg_workarea *wa, unsigned int len)
110 unsigned int nbytes = min_t(u64, len, wa->bytes_left);
115 wa->sg_used += nbytes;
116 wa->bytes_left -= nbytes;
117 if (wa->sg_used == wa->sg->length) {
118 wa->sg = sg_next(wa->sg);
123 static void ccp_dm_free(struct ccp_dm_workarea *wa)
125 if (wa->length <= CCP_DMAPOOL_MAX_SIZE) {
127 dma_pool_free(wa->dma_pool, wa->address,
131 dma_unmap_single(wa->dev, wa->dma.address, wa->length,
140 static int ccp_init_dm_workarea(struct ccp_dm_workarea *wa,
141 struct ccp_cmd_queue *cmd_q,
143 enum dma_data_direction dir)
145 memset(wa, 0, sizeof(*wa));
150 wa->dev = cmd_q->ccp->dev;
153 if (len <= CCP_DMAPOOL_MAX_SIZE) {
154 wa->dma_pool = cmd_q->dma_pool;
156 wa->address = dma_pool_alloc(wa->dma_pool, GFP_KERNEL,
161 wa->dma.length = CCP_DMAPOOL_MAX_SIZE;
163 memset(wa->address, 0, CCP_DMAPOOL_MAX_SIZE);
165 wa->address = kzalloc(len, GFP_KERNEL);
169 wa->dma.address = dma_map_single(wa->dev, wa->address, len,
171 if (dma_mapping_error(wa->dev, wa->dma.address))
174 wa->dma.length = len;
181 static void ccp_set_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
182 struct scatterlist *sg, unsigned int sg_offset,
185 WARN_ON(!wa->address);
187 scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
191 static void ccp_get_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
192 struct scatterlist *sg, unsigned int sg_offset,
195 WARN_ON(!wa->address);
197 scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
201 static int ccp_reverse_set_dm_area(struct ccp_dm_workarea *wa,
202 unsigned int wa_offset,
203 struct scatterlist *sg,
204 unsigned int sg_offset,
209 ccp_set_dm_area(wa, wa_offset, sg, sg_offset, len);
211 p = wa->address + wa_offset;
223 static void ccp_reverse_get_dm_area(struct ccp_dm_workarea *wa,
224 unsigned int wa_offset,
225 struct scatterlist *sg,
226 unsigned int sg_offset,
231 p = wa->address + wa_offset;
241 ccp_get_dm_area(wa, wa_offset, sg, sg_offset, len);
244 static void ccp_free_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q)
246 ccp_dm_free(&data->dm_wa);
247 ccp_sg_free(&data->sg_wa);
250 static int ccp_init_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q,
251 struct scatterlist *sg, u64 sg_len,
253 enum dma_data_direction dir)
257 memset(data, 0, sizeof(*data));
259 ret = ccp_init_sg_workarea(&data->sg_wa, cmd_q->ccp->dev, sg, sg_len,
264 ret = ccp_init_dm_workarea(&data->dm_wa, cmd_q, dm_len, dir);
271 ccp_free_data(data, cmd_q);
276 static unsigned int ccp_queue_buf(struct ccp_data *data, unsigned int from)
278 struct ccp_sg_workarea *sg_wa = &data->sg_wa;
279 struct ccp_dm_workarea *dm_wa = &data->dm_wa;
280 unsigned int buf_count, nbytes;
282 /* Clear the buffer if setting it */
284 memset(dm_wa->address, 0, dm_wa->length);
289 /* Perform the copy operation
290 * nbytes will always be <= UINT_MAX because dm_wa->length is
293 nbytes = min_t(u64, sg_wa->bytes_left, dm_wa->length);
294 scatterwalk_map_and_copy(dm_wa->address, sg_wa->sg, sg_wa->sg_used,
297 /* Update the structures and generate the count */
299 while (sg_wa->bytes_left && (buf_count < dm_wa->length)) {
300 nbytes = min(sg_wa->sg->length - sg_wa->sg_used,
301 dm_wa->length - buf_count);
302 nbytes = min_t(u64, sg_wa->bytes_left, nbytes);
305 ccp_update_sg_workarea(sg_wa, nbytes);
311 static unsigned int ccp_fill_queue_buf(struct ccp_data *data)
313 return ccp_queue_buf(data, 0);
316 static unsigned int ccp_empty_queue_buf(struct ccp_data *data)
318 return ccp_queue_buf(data, 1);
321 static void ccp_prepare_data(struct ccp_data *src, struct ccp_data *dst,
322 struct ccp_op *op, unsigned int block_size,
325 unsigned int sg_src_len, sg_dst_len, op_len;
327 /* The CCP can only DMA from/to one address each per operation. This
328 * requires that we find the smallest DMA area between the source
329 * and destination. The resulting len values will always be <= UINT_MAX
330 * because the dma length is an unsigned int.
332 sg_src_len = sg_dma_len(src->sg_wa.sg) - src->sg_wa.sg_used;
333 sg_src_len = min_t(u64, src->sg_wa.bytes_left, sg_src_len);
336 sg_dst_len = sg_dma_len(dst->sg_wa.sg) - dst->sg_wa.sg_used;
337 sg_dst_len = min_t(u64, src->sg_wa.bytes_left, sg_dst_len);
338 op_len = min(sg_src_len, sg_dst_len);
343 /* The data operation length will be at least block_size in length
344 * or the smaller of available sg room remaining for the source or
347 op_len = max(op_len, block_size);
349 /* Unless we have to buffer data, there's no reason to wait */
352 if (sg_src_len < block_size) {
353 /* Not enough data in the sg element, so it
354 * needs to be buffered into a blocksize chunk
356 int cp_len = ccp_fill_queue_buf(src);
359 op->src.u.dma.address = src->dm_wa.dma.address;
360 op->src.u.dma.offset = 0;
361 op->src.u.dma.length = (blocksize_op) ? block_size : cp_len;
363 /* Enough data in the sg element, but we need to
364 * adjust for any previously copied data
366 op->src.u.dma.address = sg_dma_address(src->sg_wa.sg);
367 op->src.u.dma.offset = src->sg_wa.sg_used;
368 op->src.u.dma.length = op_len & ~(block_size - 1);
370 ccp_update_sg_workarea(&src->sg_wa, op->src.u.dma.length);
374 if (sg_dst_len < block_size) {
375 /* Not enough room in the sg element or we're on the
376 * last piece of data (when using padding), so the
377 * output needs to be buffered into a blocksize chunk
380 op->dst.u.dma.address = dst->dm_wa.dma.address;
381 op->dst.u.dma.offset = 0;
382 op->dst.u.dma.length = op->src.u.dma.length;
384 /* Enough room in the sg element, but we need to
385 * adjust for any previously used area
387 op->dst.u.dma.address = sg_dma_address(dst->sg_wa.sg);
388 op->dst.u.dma.offset = dst->sg_wa.sg_used;
389 op->dst.u.dma.length = op->src.u.dma.length;
394 static void ccp_process_data(struct ccp_data *src, struct ccp_data *dst,
400 if (op->dst.u.dma.address == dst->dm_wa.dma.address)
401 ccp_empty_queue_buf(dst);
403 ccp_update_sg_workarea(&dst->sg_wa,
404 op->dst.u.dma.length);
408 static int ccp_copy_to_from_sb(struct ccp_cmd_queue *cmd_q,
409 struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
410 u32 byte_swap, bool from)
414 memset(&op, 0, sizeof(op));
422 op.src.type = CCP_MEMTYPE_SB;
424 op.dst.type = CCP_MEMTYPE_SYSTEM;
425 op.dst.u.dma.address = wa->dma.address;
426 op.dst.u.dma.length = wa->length;
428 op.src.type = CCP_MEMTYPE_SYSTEM;
429 op.src.u.dma.address = wa->dma.address;
430 op.src.u.dma.length = wa->length;
431 op.dst.type = CCP_MEMTYPE_SB;
435 op.u.passthru.byte_swap = byte_swap;
437 return cmd_q->ccp->vdata->perform->passthru(&op);
440 static int ccp_copy_to_sb(struct ccp_cmd_queue *cmd_q,
441 struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
444 return ccp_copy_to_from_sb(cmd_q, wa, jobid, sb, byte_swap, false);
447 static int ccp_copy_from_sb(struct ccp_cmd_queue *cmd_q,
448 struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
451 return ccp_copy_to_from_sb(cmd_q, wa, jobid, sb, byte_swap, true);
454 static int ccp_run_aes_cmac_cmd(struct ccp_cmd_queue *cmd_q,
457 struct ccp_aes_engine *aes = &cmd->u.aes;
458 struct ccp_dm_workarea key, ctx;
461 unsigned int dm_offset;
464 if (!((aes->key_len == AES_KEYSIZE_128) ||
465 (aes->key_len == AES_KEYSIZE_192) ||
466 (aes->key_len == AES_KEYSIZE_256)))
469 if (aes->src_len & (AES_BLOCK_SIZE - 1))
472 if (aes->iv_len != AES_BLOCK_SIZE)
475 if (!aes->key || !aes->iv || !aes->src)
478 if (aes->cmac_final) {
479 if (aes->cmac_key_len != AES_BLOCK_SIZE)
486 BUILD_BUG_ON(CCP_AES_KEY_SB_COUNT != 1);
487 BUILD_BUG_ON(CCP_AES_CTX_SB_COUNT != 1);
490 memset(&op, 0, sizeof(op));
492 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
493 op.sb_key = cmd_q->sb_key;
494 op.sb_ctx = cmd_q->sb_ctx;
496 op.u.aes.type = aes->type;
497 op.u.aes.mode = aes->mode;
498 op.u.aes.action = aes->action;
500 /* All supported key sizes fit in a single (32-byte) SB entry
501 * and must be in little endian format. Use the 256-bit byte
502 * swap passthru option to convert from big endian to little
505 ret = ccp_init_dm_workarea(&key, cmd_q,
506 CCP_AES_KEY_SB_COUNT * CCP_SB_BYTES,
511 dm_offset = CCP_SB_BYTES - aes->key_len;
512 ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
513 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
514 CCP_PASSTHRU_BYTESWAP_256BIT);
516 cmd->engine_error = cmd_q->cmd_error;
520 /* The AES context fits in a single (32-byte) SB entry and
521 * must be in little endian format. Use the 256-bit byte swap
522 * passthru option to convert from big endian to little endian.
524 ret = ccp_init_dm_workarea(&ctx, cmd_q,
525 CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
530 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
531 ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
532 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
533 CCP_PASSTHRU_BYTESWAP_256BIT);
535 cmd->engine_error = cmd_q->cmd_error;
539 /* Send data to the CCP AES engine */
540 ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
541 AES_BLOCK_SIZE, DMA_TO_DEVICE);
545 while (src.sg_wa.bytes_left) {
546 ccp_prepare_data(&src, NULL, &op, AES_BLOCK_SIZE, true);
547 if (aes->cmac_final && !src.sg_wa.bytes_left) {
550 /* Push the K1/K2 key to the CCP now */
551 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid,
553 CCP_PASSTHRU_BYTESWAP_256BIT);
555 cmd->engine_error = cmd_q->cmd_error;
559 ccp_set_dm_area(&ctx, 0, aes->cmac_key, 0,
561 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
562 CCP_PASSTHRU_BYTESWAP_256BIT);
564 cmd->engine_error = cmd_q->cmd_error;
569 ret = cmd_q->ccp->vdata->perform->aes(&op);
571 cmd->engine_error = cmd_q->cmd_error;
575 ccp_process_data(&src, NULL, &op);
578 /* Retrieve the AES context - convert from LE to BE using
579 * 32-byte (256-bit) byteswapping
581 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
582 CCP_PASSTHRU_BYTESWAP_256BIT);
584 cmd->engine_error = cmd_q->cmd_error;
588 /* ...but we only need AES_BLOCK_SIZE bytes */
589 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
590 ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
593 ccp_free_data(&src, cmd_q);
604 static int ccp_run_aes_gcm_cmd(struct ccp_cmd_queue *cmd_q,
607 struct ccp_aes_engine *aes = &cmd->u.aes;
608 struct ccp_dm_workarea key, ctx, final_wa, tag;
609 struct ccp_data src, dst;
613 unsigned long long *final;
614 unsigned int dm_offset;
616 bool in_place = true; /* Default value */
619 struct scatterlist *p_inp, sg_inp[2];
620 struct scatterlist *p_tag, sg_tag[2];
621 struct scatterlist *p_outp, sg_outp[2];
622 struct scatterlist *p_aad;
627 if (!((aes->key_len == AES_KEYSIZE_128) ||
628 (aes->key_len == AES_KEYSIZE_192) ||
629 (aes->key_len == AES_KEYSIZE_256)))
632 if (!aes->key) /* Gotta have a key SGL */
635 /* First, decompose the source buffer into AAD & PT,
636 * and the destination buffer into AAD, CT & tag, or
637 * the input into CT & tag.
638 * It is expected that the input and output SGs will
639 * be valid, even if the AAD and input lengths are 0.
642 p_inp = scatterwalk_ffwd(sg_inp, aes->src, aes->aad_len);
643 p_outp = scatterwalk_ffwd(sg_outp, aes->dst, aes->aad_len);
644 if (aes->action == CCP_AES_ACTION_ENCRYPT) {
646 p_tag = scatterwalk_ffwd(sg_tag, p_outp, ilen);
648 /* Input length for decryption includes tag */
649 ilen = aes->src_len - AES_BLOCK_SIZE;
650 p_tag = scatterwalk_ffwd(sg_tag, p_inp, ilen);
653 memset(&op, 0, sizeof(op));
655 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
656 op.sb_key = cmd_q->sb_key; /* Pre-allocated */
657 op.sb_ctx = cmd_q->sb_ctx; /* Pre-allocated */
659 op.u.aes.type = aes->type;
661 /* Copy the key to the LSB */
662 ret = ccp_init_dm_workarea(&key, cmd_q,
663 CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
668 dm_offset = CCP_SB_BYTES - aes->key_len;
669 ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
670 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
671 CCP_PASSTHRU_BYTESWAP_256BIT);
673 cmd->engine_error = cmd_q->cmd_error;
677 /* Copy the context (IV) to the LSB.
678 * There is an assumption here that the IV is 96 bits in length, plus
679 * a nonce of 32 bits. If no IV is present, use a zeroed buffer.
681 ret = ccp_init_dm_workarea(&ctx, cmd_q,
682 CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
687 dm_offset = CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES - aes->iv_len;
688 ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
690 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
691 CCP_PASSTHRU_BYTESWAP_256BIT);
693 cmd->engine_error = cmd_q->cmd_error;
698 if (aes->aad_len > 0) {
699 /* Step 1: Run a GHASH over the Additional Authenticated Data */
700 ret = ccp_init_data(&aad, cmd_q, p_aad, aes->aad_len,
706 op.u.aes.mode = CCP_AES_MODE_GHASH;
707 op.u.aes.action = CCP_AES_GHASHAAD;
709 while (aad.sg_wa.bytes_left) {
710 ccp_prepare_data(&aad, NULL, &op, AES_BLOCK_SIZE, true);
712 ret = cmd_q->ccp->vdata->perform->aes(&op);
714 cmd->engine_error = cmd_q->cmd_error;
718 ccp_process_data(&aad, NULL, &op);
723 op.u.aes.mode = CCP_AES_MODE_GCTR;
724 op.u.aes.action = aes->action;
727 /* Step 2: Run a GCTR over the plaintext */
728 in_place = (sg_virt(p_inp) == sg_virt(p_outp)) ? true : false;
730 ret = ccp_init_data(&src, cmd_q, p_inp, ilen,
732 in_place ? DMA_BIDIRECTIONAL
740 ret = ccp_init_data(&dst, cmd_q, p_outp, ilen,
741 AES_BLOCK_SIZE, DMA_FROM_DEVICE);
749 while (src.sg_wa.bytes_left) {
750 ccp_prepare_data(&src, &dst, &op, AES_BLOCK_SIZE, true);
751 if (!src.sg_wa.bytes_left) {
752 unsigned int nbytes = aes->src_len
757 op.u.aes.size = (nbytes * 8) - 1;
761 ret = cmd_q->ccp->vdata->perform->aes(&op);
763 cmd->engine_error = cmd_q->cmd_error;
767 ccp_process_data(&src, &dst, &op);
772 /* Step 3: Update the IV portion of the context with the original IV */
773 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
774 CCP_PASSTHRU_BYTESWAP_256BIT);
776 cmd->engine_error = cmd_q->cmd_error;
780 ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
782 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
783 CCP_PASSTHRU_BYTESWAP_256BIT);
785 cmd->engine_error = cmd_q->cmd_error;
789 /* Step 4: Concatenate the lengths of the AAD and source, and
790 * hash that 16 byte buffer.
792 ret = ccp_init_dm_workarea(&final_wa, cmd_q, AES_BLOCK_SIZE,
796 final = (unsigned long long *) final_wa.address;
797 final[0] = cpu_to_be64(aes->aad_len * 8);
798 final[1] = cpu_to_be64(ilen * 8);
800 op.u.aes.mode = CCP_AES_MODE_GHASH;
801 op.u.aes.action = CCP_AES_GHASHFINAL;
802 op.src.type = CCP_MEMTYPE_SYSTEM;
803 op.src.u.dma.address = final_wa.dma.address;
804 op.src.u.dma.length = AES_BLOCK_SIZE;
805 op.dst.type = CCP_MEMTYPE_SYSTEM;
806 op.dst.u.dma.address = final_wa.dma.address;
807 op.dst.u.dma.length = AES_BLOCK_SIZE;
810 ret = cmd_q->ccp->vdata->perform->aes(&op);
814 if (aes->action == CCP_AES_ACTION_ENCRYPT) {
815 /* Put the ciphered tag after the ciphertext. */
816 ccp_get_dm_area(&final_wa, 0, p_tag, 0, AES_BLOCK_SIZE);
818 /* Does this ciphered tag match the input? */
819 ret = ccp_init_dm_workarea(&tag, cmd_q, AES_BLOCK_SIZE,
823 ccp_set_dm_area(&tag, 0, p_tag, 0, AES_BLOCK_SIZE);
825 ret = memcmp(tag.address, final_wa.address, AES_BLOCK_SIZE);
830 ccp_dm_free(&final_wa);
833 if (aes->src_len && !in_place)
834 ccp_free_data(&dst, cmd_q);
838 ccp_free_data(&src, cmd_q);
842 ccp_free_data(&aad, cmd_q);
853 static int ccp_run_aes_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
855 struct ccp_aes_engine *aes = &cmd->u.aes;
856 struct ccp_dm_workarea key, ctx;
857 struct ccp_data src, dst;
859 unsigned int dm_offset;
860 bool in_place = false;
863 if (aes->mode == CCP_AES_MODE_CMAC)
864 return ccp_run_aes_cmac_cmd(cmd_q, cmd);
866 if (aes->mode == CCP_AES_MODE_GCM)
867 return ccp_run_aes_gcm_cmd(cmd_q, cmd);
869 if (!((aes->key_len == AES_KEYSIZE_128) ||
870 (aes->key_len == AES_KEYSIZE_192) ||
871 (aes->key_len == AES_KEYSIZE_256)))
874 if (((aes->mode == CCP_AES_MODE_ECB) ||
875 (aes->mode == CCP_AES_MODE_CBC) ||
876 (aes->mode == CCP_AES_MODE_CFB)) &&
877 (aes->src_len & (AES_BLOCK_SIZE - 1)))
880 if (!aes->key || !aes->src || !aes->dst)
883 if (aes->mode != CCP_AES_MODE_ECB) {
884 if (aes->iv_len != AES_BLOCK_SIZE)
891 BUILD_BUG_ON(CCP_AES_KEY_SB_COUNT != 1);
892 BUILD_BUG_ON(CCP_AES_CTX_SB_COUNT != 1);
895 memset(&op, 0, sizeof(op));
897 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
898 op.sb_key = cmd_q->sb_key;
899 op.sb_ctx = cmd_q->sb_ctx;
900 op.init = (aes->mode == CCP_AES_MODE_ECB) ? 0 : 1;
901 op.u.aes.type = aes->type;
902 op.u.aes.mode = aes->mode;
903 op.u.aes.action = aes->action;
905 /* All supported key sizes fit in a single (32-byte) SB entry
906 * and must be in little endian format. Use the 256-bit byte
907 * swap passthru option to convert from big endian to little
910 ret = ccp_init_dm_workarea(&key, cmd_q,
911 CCP_AES_KEY_SB_COUNT * CCP_SB_BYTES,
916 dm_offset = CCP_SB_BYTES - aes->key_len;
917 ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
918 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
919 CCP_PASSTHRU_BYTESWAP_256BIT);
921 cmd->engine_error = cmd_q->cmd_error;
925 /* The AES context fits in a single (32-byte) SB entry and
926 * must be in little endian format. Use the 256-bit byte swap
927 * passthru option to convert from big endian to little endian.
929 ret = ccp_init_dm_workarea(&ctx, cmd_q,
930 CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
935 if (aes->mode != CCP_AES_MODE_ECB) {
936 /* Load the AES context - convert to LE */
937 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
938 ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
939 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
940 CCP_PASSTHRU_BYTESWAP_256BIT);
942 cmd->engine_error = cmd_q->cmd_error;
947 case CCP_AES_MODE_CFB: /* CFB128 only */
948 case CCP_AES_MODE_CTR:
949 op.u.aes.size = AES_BLOCK_SIZE * BITS_PER_BYTE - 1;
955 /* Prepare the input and output data workareas. For in-place
956 * operations we need to set the dma direction to BIDIRECTIONAL
957 * and copy the src workarea to the dst workarea.
959 if (sg_virt(aes->src) == sg_virt(aes->dst))
962 ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
964 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
971 ret = ccp_init_data(&dst, cmd_q, aes->dst, aes->src_len,
972 AES_BLOCK_SIZE, DMA_FROM_DEVICE);
977 /* Send data to the CCP AES engine */
978 while (src.sg_wa.bytes_left) {
979 ccp_prepare_data(&src, &dst, &op, AES_BLOCK_SIZE, true);
980 if (!src.sg_wa.bytes_left) {
983 /* Since we don't retrieve the AES context in ECB
984 * mode we have to wait for the operation to complete
985 * on the last piece of data
987 if (aes->mode == CCP_AES_MODE_ECB)
991 ret = cmd_q->ccp->vdata->perform->aes(&op);
993 cmd->engine_error = cmd_q->cmd_error;
997 ccp_process_data(&src, &dst, &op);
1000 if (aes->mode != CCP_AES_MODE_ECB) {
1001 /* Retrieve the AES context - convert from LE to BE using
1002 * 32-byte (256-bit) byteswapping
1004 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1005 CCP_PASSTHRU_BYTESWAP_256BIT);
1007 cmd->engine_error = cmd_q->cmd_error;
1011 /* ...but we only need AES_BLOCK_SIZE bytes */
1012 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
1013 ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
1018 ccp_free_data(&dst, cmd_q);
1021 ccp_free_data(&src, cmd_q);
1032 static int ccp_run_xts_aes_cmd(struct ccp_cmd_queue *cmd_q,
1033 struct ccp_cmd *cmd)
1035 struct ccp_xts_aes_engine *xts = &cmd->u.xts;
1036 struct ccp_dm_workarea key, ctx;
1037 struct ccp_data src, dst;
1039 unsigned int unit_size, dm_offset;
1040 bool in_place = false;
1041 unsigned int sb_count;
1042 enum ccp_aes_type aestype;
1045 switch (xts->unit_size) {
1046 case CCP_XTS_AES_UNIT_SIZE_16:
1049 case CCP_XTS_AES_UNIT_SIZE_512:
1052 case CCP_XTS_AES_UNIT_SIZE_1024:
1055 case CCP_XTS_AES_UNIT_SIZE_2048:
1058 case CCP_XTS_AES_UNIT_SIZE_4096:
1066 if (xts->key_len == AES_KEYSIZE_128)
1067 aestype = CCP_AES_TYPE_128;
1068 else if (xts->key_len == AES_KEYSIZE_256)
1069 aestype = CCP_AES_TYPE_256;
1073 if (!xts->final && (xts->src_len & (AES_BLOCK_SIZE - 1)))
1076 if (xts->iv_len != AES_BLOCK_SIZE)
1079 if (!xts->key || !xts->iv || !xts->src || !xts->dst)
1082 BUILD_BUG_ON(CCP_XTS_AES_KEY_SB_COUNT != 1);
1083 BUILD_BUG_ON(CCP_XTS_AES_CTX_SB_COUNT != 1);
1086 memset(&op, 0, sizeof(op));
1088 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
1089 op.sb_key = cmd_q->sb_key;
1090 op.sb_ctx = cmd_q->sb_ctx;
1092 op.u.xts.type = aestype;
1093 op.u.xts.action = xts->action;
1094 op.u.xts.unit_size = xts->unit_size;
1096 /* A version 3 device only supports 128-bit keys, which fits into a
1097 * single SB entry. A version 5 device uses a 512-bit vector, so two
1100 if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0))
1101 sb_count = CCP_XTS_AES_KEY_SB_COUNT;
1103 sb_count = CCP5_XTS_AES_KEY_SB_COUNT;
1104 ret = ccp_init_dm_workarea(&key, cmd_q,
1105 sb_count * CCP_SB_BYTES,
1110 if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) {
1111 /* All supported key sizes must be in little endian format.
1112 * Use the 256-bit byte swap passthru option to convert from
1113 * big endian to little endian.
1115 dm_offset = CCP_SB_BYTES - AES_KEYSIZE_128;
1116 ccp_set_dm_area(&key, dm_offset, xts->key, 0, xts->key_len);
1117 ccp_set_dm_area(&key, 0, xts->key, xts->key_len, xts->key_len);
1119 /* Version 5 CCPs use a 512-bit space for the key: each portion
1120 * occupies 256 bits, or one entire slot, and is zero-padded.
1124 dm_offset = CCP_SB_BYTES;
1125 pad = dm_offset - xts->key_len;
1126 ccp_set_dm_area(&key, pad, xts->key, 0, xts->key_len);
1127 ccp_set_dm_area(&key, dm_offset + pad, xts->key, xts->key_len,
1130 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
1131 CCP_PASSTHRU_BYTESWAP_256BIT);
1133 cmd->engine_error = cmd_q->cmd_error;
1137 /* The AES context fits in a single (32-byte) SB entry and
1138 * for XTS is already in little endian format so no byte swapping
1141 ret = ccp_init_dm_workarea(&ctx, cmd_q,
1142 CCP_XTS_AES_CTX_SB_COUNT * CCP_SB_BYTES,
1147 ccp_set_dm_area(&ctx, 0, xts->iv, 0, xts->iv_len);
1148 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1149 CCP_PASSTHRU_BYTESWAP_NOOP);
1151 cmd->engine_error = cmd_q->cmd_error;
1155 /* Prepare the input and output data workareas. For in-place
1156 * operations we need to set the dma direction to BIDIRECTIONAL
1157 * and copy the src workarea to the dst workarea.
1159 if (sg_virt(xts->src) == sg_virt(xts->dst))
1162 ret = ccp_init_data(&src, cmd_q, xts->src, xts->src_len,
1164 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1171 ret = ccp_init_data(&dst, cmd_q, xts->dst, xts->src_len,
1172 unit_size, DMA_FROM_DEVICE);
1177 /* Send data to the CCP AES engine */
1178 while (src.sg_wa.bytes_left) {
1179 ccp_prepare_data(&src, &dst, &op, unit_size, true);
1180 if (!src.sg_wa.bytes_left)
1183 ret = cmd_q->ccp->vdata->perform->xts_aes(&op);
1185 cmd->engine_error = cmd_q->cmd_error;
1189 ccp_process_data(&src, &dst, &op);
1192 /* Retrieve the AES context - convert from LE to BE using
1193 * 32-byte (256-bit) byteswapping
1195 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1196 CCP_PASSTHRU_BYTESWAP_256BIT);
1198 cmd->engine_error = cmd_q->cmd_error;
1202 /* ...but we only need AES_BLOCK_SIZE bytes */
1203 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
1204 ccp_get_dm_area(&ctx, dm_offset, xts->iv, 0, xts->iv_len);
1208 ccp_free_data(&dst, cmd_q);
1211 ccp_free_data(&src, cmd_q);
1222 static int ccp_run_des3_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1224 struct ccp_des3_engine *des3 = &cmd->u.des3;
1226 struct ccp_dm_workarea key, ctx;
1227 struct ccp_data src, dst;
1229 unsigned int dm_offset;
1230 unsigned int len_singlekey;
1231 bool in_place = false;
1235 if (!cmd_q->ccp->vdata->perform->des3)
1238 if (des3->key_len != DES3_EDE_KEY_SIZE)
1241 if (((des3->mode == CCP_DES3_MODE_ECB) ||
1242 (des3->mode == CCP_DES3_MODE_CBC)) &&
1243 (des3->src_len & (DES3_EDE_BLOCK_SIZE - 1)))
1246 if (!des3->key || !des3->src || !des3->dst)
1249 if (des3->mode != CCP_DES3_MODE_ECB) {
1250 if (des3->iv_len != DES3_EDE_BLOCK_SIZE)
1258 /* Zero out all the fields of the command desc */
1259 memset(&op, 0, sizeof(op));
1261 /* Set up the Function field */
1263 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
1264 op.sb_key = cmd_q->sb_key;
1266 op.init = (des3->mode == CCP_DES3_MODE_ECB) ? 0 : 1;
1267 op.u.des3.type = des3->type;
1268 op.u.des3.mode = des3->mode;
1269 op.u.des3.action = des3->action;
1272 * All supported key sizes fit in a single (32-byte) KSB entry and
1273 * (like AES) must be in little endian format. Use the 256-bit byte
1274 * swap passthru option to convert from big endian to little endian.
1276 ret = ccp_init_dm_workarea(&key, cmd_q,
1277 CCP_DES3_KEY_SB_COUNT * CCP_SB_BYTES,
1283 * The contents of the key triplet are in the reverse order of what
1284 * is required by the engine. Copy the 3 pieces individually to put
1285 * them where they belong.
1287 dm_offset = CCP_SB_BYTES - des3->key_len; /* Basic offset */
1289 len_singlekey = des3->key_len / 3;
1290 ccp_set_dm_area(&key, dm_offset + 2 * len_singlekey,
1291 des3->key, 0, len_singlekey);
1292 ccp_set_dm_area(&key, dm_offset + len_singlekey,
1293 des3->key, len_singlekey, len_singlekey);
1294 ccp_set_dm_area(&key, dm_offset,
1295 des3->key, 2 * len_singlekey, len_singlekey);
1297 /* Copy the key to the SB */
1298 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
1299 CCP_PASSTHRU_BYTESWAP_256BIT);
1301 cmd->engine_error = cmd_q->cmd_error;
1306 * The DES3 context fits in a single (32-byte) KSB entry and
1307 * must be in little endian format. Use the 256-bit byte swap
1308 * passthru option to convert from big endian to little endian.
1310 if (des3->mode != CCP_DES3_MODE_ECB) {
1313 op.sb_ctx = cmd_q->sb_ctx;
1315 ret = ccp_init_dm_workarea(&ctx, cmd_q,
1316 CCP_DES3_CTX_SB_COUNT * CCP_SB_BYTES,
1321 /* Load the context into the LSB */
1322 dm_offset = CCP_SB_BYTES - des3->iv_len;
1323 ccp_set_dm_area(&ctx, dm_offset, des3->iv, 0, des3->iv_len);
1325 if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0))
1326 load_mode = CCP_PASSTHRU_BYTESWAP_NOOP;
1328 load_mode = CCP_PASSTHRU_BYTESWAP_256BIT;
1329 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1332 cmd->engine_error = cmd_q->cmd_error;
1338 * Prepare the input and output data workareas. For in-place
1339 * operations we need to set the dma direction to BIDIRECTIONAL
1340 * and copy the src workarea to the dst workarea.
1342 if (sg_virt(des3->src) == sg_virt(des3->dst))
1345 ret = ccp_init_data(&src, cmd_q, des3->src, des3->src_len,
1346 DES3_EDE_BLOCK_SIZE,
1347 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1354 ret = ccp_init_data(&dst, cmd_q, des3->dst, des3->src_len,
1355 DES3_EDE_BLOCK_SIZE, DMA_FROM_DEVICE);
1360 /* Send data to the CCP DES3 engine */
1361 while (src.sg_wa.bytes_left) {
1362 ccp_prepare_data(&src, &dst, &op, DES3_EDE_BLOCK_SIZE, true);
1363 if (!src.sg_wa.bytes_left) {
1366 /* Since we don't retrieve the context in ECB mode
1367 * we have to wait for the operation to complete
1368 * on the last piece of data
1373 ret = cmd_q->ccp->vdata->perform->des3(&op);
1375 cmd->engine_error = cmd_q->cmd_error;
1379 ccp_process_data(&src, &dst, &op);
1382 if (des3->mode != CCP_DES3_MODE_ECB) {
1383 /* Retrieve the context and make BE */
1384 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1385 CCP_PASSTHRU_BYTESWAP_256BIT);
1387 cmd->engine_error = cmd_q->cmd_error;
1391 /* ...but we only need the last DES3_EDE_BLOCK_SIZE bytes */
1392 if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0))
1393 dm_offset = CCP_SB_BYTES - des3->iv_len;
1396 ccp_get_dm_area(&ctx, dm_offset, des3->iv, 0,
1397 DES3_EDE_BLOCK_SIZE);
1401 ccp_free_data(&dst, cmd_q);
1404 ccp_free_data(&src, cmd_q);
1407 if (des3->mode != CCP_DES3_MODE_ECB)
1416 static int ccp_run_sha_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1418 struct ccp_sha_engine *sha = &cmd->u.sha;
1419 struct ccp_dm_workarea ctx;
1420 struct ccp_data src;
1422 unsigned int ioffset, ooffset;
1423 unsigned int digest_size;
1430 switch (sha->type) {
1431 case CCP_SHA_TYPE_1:
1432 if (sha->ctx_len < SHA1_DIGEST_SIZE)
1434 block_size = SHA1_BLOCK_SIZE;
1436 case CCP_SHA_TYPE_224:
1437 if (sha->ctx_len < SHA224_DIGEST_SIZE)
1439 block_size = SHA224_BLOCK_SIZE;
1441 case CCP_SHA_TYPE_256:
1442 if (sha->ctx_len < SHA256_DIGEST_SIZE)
1444 block_size = SHA256_BLOCK_SIZE;
1446 case CCP_SHA_TYPE_384:
1447 if (cmd_q->ccp->vdata->version < CCP_VERSION(4, 0)
1448 || sha->ctx_len < SHA384_DIGEST_SIZE)
1450 block_size = SHA384_BLOCK_SIZE;
1452 case CCP_SHA_TYPE_512:
1453 if (cmd_q->ccp->vdata->version < CCP_VERSION(4, 0)
1454 || sha->ctx_len < SHA512_DIGEST_SIZE)
1456 block_size = SHA512_BLOCK_SIZE;
1465 if (!sha->final && (sha->src_len & (block_size - 1)))
1468 /* The version 3 device can't handle zero-length input */
1469 if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) {
1471 if (!sha->src_len) {
1472 unsigned int digest_len;
1475 /* Not final, just return */
1479 /* CCP can't do a zero length sha operation so the
1480 * caller must buffer the data.
1485 /* The CCP cannot perform zero-length sha operations
1486 * so the caller is required to buffer data for the
1487 * final operation. However, a sha operation for a
1488 * message with a total length of zero is valid so
1489 * known values are required to supply the result.
1491 switch (sha->type) {
1492 case CCP_SHA_TYPE_1:
1493 sha_zero = sha1_zero_message_hash;
1494 digest_len = SHA1_DIGEST_SIZE;
1496 case CCP_SHA_TYPE_224:
1497 sha_zero = sha224_zero_message_hash;
1498 digest_len = SHA224_DIGEST_SIZE;
1500 case CCP_SHA_TYPE_256:
1501 sha_zero = sha256_zero_message_hash;
1502 digest_len = SHA256_DIGEST_SIZE;
1508 scatterwalk_map_and_copy((void *)sha_zero, sha->ctx, 0,
1515 /* Set variables used throughout */
1516 switch (sha->type) {
1517 case CCP_SHA_TYPE_1:
1518 digest_size = SHA1_DIGEST_SIZE;
1519 init = (void *) ccp_sha1_init;
1520 ctx_size = SHA1_DIGEST_SIZE;
1522 if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0))
1523 ooffset = ioffset = CCP_SB_BYTES - SHA1_DIGEST_SIZE;
1525 ooffset = ioffset = 0;
1527 case CCP_SHA_TYPE_224:
1528 digest_size = SHA224_DIGEST_SIZE;
1529 init = (void *) ccp_sha224_init;
1530 ctx_size = SHA256_DIGEST_SIZE;
1533 if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0))
1534 ooffset = CCP_SB_BYTES - SHA224_DIGEST_SIZE;
1538 case CCP_SHA_TYPE_256:
1539 digest_size = SHA256_DIGEST_SIZE;
1540 init = (void *) ccp_sha256_init;
1541 ctx_size = SHA256_DIGEST_SIZE;
1543 ooffset = ioffset = 0;
1545 case CCP_SHA_TYPE_384:
1546 digest_size = SHA384_DIGEST_SIZE;
1547 init = (void *) ccp_sha384_init;
1548 ctx_size = SHA512_DIGEST_SIZE;
1551 ooffset = 2 * CCP_SB_BYTES - SHA384_DIGEST_SIZE;
1553 case CCP_SHA_TYPE_512:
1554 digest_size = SHA512_DIGEST_SIZE;
1555 init = (void *) ccp_sha512_init;
1556 ctx_size = SHA512_DIGEST_SIZE;
1558 ooffset = ioffset = 0;
1565 /* For zero-length plaintext the src pointer is ignored;
1566 * otherwise both parts must be valid
1568 if (sha->src_len && !sha->src)
1571 memset(&op, 0, sizeof(op));
1573 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
1574 op.sb_ctx = cmd_q->sb_ctx; /* Pre-allocated */
1575 op.u.sha.type = sha->type;
1576 op.u.sha.msg_bits = sha->msg_bits;
1578 /* For SHA1/224/256 the context fits in a single (32-byte) SB entry;
1579 * SHA384/512 require 2 adjacent SB slots, with the right half in the
1580 * first slot, and the left half in the second. Each portion must then
1581 * be in little endian format: use the 256-bit byte swap option.
1583 ret = ccp_init_dm_workarea(&ctx, cmd_q, sb_count * CCP_SB_BYTES,
1588 switch (sha->type) {
1589 case CCP_SHA_TYPE_1:
1590 case CCP_SHA_TYPE_224:
1591 case CCP_SHA_TYPE_256:
1592 memcpy(ctx.address + ioffset, init, ctx_size);
1594 case CCP_SHA_TYPE_384:
1595 case CCP_SHA_TYPE_512:
1596 memcpy(ctx.address + ctx_size / 2, init,
1598 memcpy(ctx.address, init + ctx_size / 2,
1606 /* Restore the context */
1607 ccp_set_dm_area(&ctx, 0, sha->ctx, 0,
1608 sb_count * CCP_SB_BYTES);
1611 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1612 CCP_PASSTHRU_BYTESWAP_256BIT);
1614 cmd->engine_error = cmd_q->cmd_error;
1619 /* Send data to the CCP SHA engine; block_size is set above */
1620 ret = ccp_init_data(&src, cmd_q, sha->src, sha->src_len,
1621 block_size, DMA_TO_DEVICE);
1625 while (src.sg_wa.bytes_left) {
1626 ccp_prepare_data(&src, NULL, &op, block_size, false);
1627 if (sha->final && !src.sg_wa.bytes_left)
1630 ret = cmd_q->ccp->vdata->perform->sha(&op);
1632 cmd->engine_error = cmd_q->cmd_error;
1636 ccp_process_data(&src, NULL, &op);
1640 ret = cmd_q->ccp->vdata->perform->sha(&op);
1642 cmd->engine_error = cmd_q->cmd_error;
1647 /* Retrieve the SHA context - convert from LE to BE using
1648 * 32-byte (256-bit) byteswapping to BE
1650 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1651 CCP_PASSTHRU_BYTESWAP_256BIT);
1653 cmd->engine_error = cmd_q->cmd_error;
1658 /* Finishing up, so get the digest */
1659 switch (sha->type) {
1660 case CCP_SHA_TYPE_1:
1661 case CCP_SHA_TYPE_224:
1662 case CCP_SHA_TYPE_256:
1663 ccp_get_dm_area(&ctx, ooffset,
1667 case CCP_SHA_TYPE_384:
1668 case CCP_SHA_TYPE_512:
1669 ccp_get_dm_area(&ctx, 0,
1670 sha->ctx, LSB_ITEM_SIZE - ooffset,
1672 ccp_get_dm_area(&ctx, LSB_ITEM_SIZE + ooffset,
1674 LSB_ITEM_SIZE - ooffset);
1681 /* Stash the context */
1682 ccp_get_dm_area(&ctx, 0, sha->ctx, 0,
1683 sb_count * CCP_SB_BYTES);
1686 if (sha->final && sha->opad) {
1687 /* HMAC operation, recursively perform final SHA */
1688 struct ccp_cmd hmac_cmd;
1689 struct scatterlist sg;
1692 if (sha->opad_len != block_size) {
1697 hmac_buf = kmalloc(block_size + digest_size, GFP_KERNEL);
1702 sg_init_one(&sg, hmac_buf, block_size + digest_size);
1704 scatterwalk_map_and_copy(hmac_buf, sha->opad, 0, block_size, 0);
1705 switch (sha->type) {
1706 case CCP_SHA_TYPE_1:
1707 case CCP_SHA_TYPE_224:
1708 case CCP_SHA_TYPE_256:
1709 memcpy(hmac_buf + block_size,
1710 ctx.address + ooffset,
1713 case CCP_SHA_TYPE_384:
1714 case CCP_SHA_TYPE_512:
1715 memcpy(hmac_buf + block_size,
1716 ctx.address + LSB_ITEM_SIZE + ooffset,
1718 memcpy(hmac_buf + block_size +
1719 (LSB_ITEM_SIZE - ooffset),
1728 memset(&hmac_cmd, 0, sizeof(hmac_cmd));
1729 hmac_cmd.engine = CCP_ENGINE_SHA;
1730 hmac_cmd.u.sha.type = sha->type;
1731 hmac_cmd.u.sha.ctx = sha->ctx;
1732 hmac_cmd.u.sha.ctx_len = sha->ctx_len;
1733 hmac_cmd.u.sha.src = &sg;
1734 hmac_cmd.u.sha.src_len = block_size + digest_size;
1735 hmac_cmd.u.sha.opad = NULL;
1736 hmac_cmd.u.sha.opad_len = 0;
1737 hmac_cmd.u.sha.first = 1;
1738 hmac_cmd.u.sha.final = 1;
1739 hmac_cmd.u.sha.msg_bits = (block_size + digest_size) << 3;
1741 ret = ccp_run_sha_cmd(cmd_q, &hmac_cmd);
1743 cmd->engine_error = hmac_cmd.engine_error;
1750 ccp_free_data(&src, cmd_q);
1758 static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1760 struct ccp_rsa_engine *rsa = &cmd->u.rsa;
1761 struct ccp_dm_workarea exp, src, dst;
1763 unsigned int sb_count, i_len, o_len;
1766 /* Check against the maximum allowable size, in bits */
1767 if (rsa->key_size > cmd_q->ccp->vdata->rsamax)
1770 if (!rsa->exp || !rsa->mod || !rsa->src || !rsa->dst)
1773 memset(&op, 0, sizeof(op));
1775 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
1777 /* The RSA modulus must precede the message being acted upon, so
1778 * it must be copied to a DMA area where the message and the
1779 * modulus can be concatenated. Therefore the input buffer
1780 * length required is twice the output buffer length (which
1781 * must be a multiple of 256-bits). Compute o_len, i_len in bytes.
1782 * Buffer sizes must be a multiple of 32 bytes; rounding up may be
1785 o_len = 32 * ((rsa->key_size + 255) / 256);
1789 if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0)) {
1790 /* sb_count is the number of storage block slots required
1793 sb_count = o_len / CCP_SB_BYTES;
1794 op.sb_key = cmd_q->ccp->vdata->perform->sballoc(cmd_q,
1799 /* A version 5 device allows a modulus size that will not fit
1800 * in the LSB, so the command will transfer it from memory.
1801 * Set the sb key to the default, even though it's not used.
1803 op.sb_key = cmd_q->sb_key;
1806 /* The RSA exponent must be in little endian format. Reverse its
1809 ret = ccp_init_dm_workarea(&exp, cmd_q, o_len, DMA_TO_DEVICE);
1813 ret = ccp_reverse_set_dm_area(&exp, 0, rsa->exp, 0, rsa->exp_len);
1817 if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0)) {
1818 /* Copy the exponent to the local storage block, using
1819 * as many 32-byte blocks as were allocated above. It's
1820 * already little endian, so no further change is required.
1822 ret = ccp_copy_to_sb(cmd_q, &exp, op.jobid, op.sb_key,
1823 CCP_PASSTHRU_BYTESWAP_NOOP);
1825 cmd->engine_error = cmd_q->cmd_error;
1829 /* The exponent can be retrieved from memory via DMA. */
1830 op.exp.u.dma.address = exp.dma.address;
1831 op.exp.u.dma.offset = 0;
1834 /* Concatenate the modulus and the message. Both the modulus and
1835 * the operands must be in little endian format. Since the input
1836 * is in big endian format it must be converted.
1838 ret = ccp_init_dm_workarea(&src, cmd_q, i_len, DMA_TO_DEVICE);
1842 ret = ccp_reverse_set_dm_area(&src, 0, rsa->mod, 0, rsa->mod_len);
1845 ret = ccp_reverse_set_dm_area(&src, o_len, rsa->src, 0, rsa->src_len);
1849 /* Prepare the output area for the operation */
1850 ret = ccp_init_dm_workarea(&dst, cmd_q, o_len, DMA_FROM_DEVICE);
1855 op.src.u.dma.address = src.dma.address;
1856 op.src.u.dma.offset = 0;
1857 op.src.u.dma.length = i_len;
1858 op.dst.u.dma.address = dst.dma.address;
1859 op.dst.u.dma.offset = 0;
1860 op.dst.u.dma.length = o_len;
1862 op.u.rsa.mod_size = rsa->key_size;
1863 op.u.rsa.input_len = i_len;
1865 ret = cmd_q->ccp->vdata->perform->rsa(&op);
1867 cmd->engine_error = cmd_q->cmd_error;
1871 ccp_reverse_get_dm_area(&dst, 0, rsa->dst, 0, rsa->mod_len);
1884 cmd_q->ccp->vdata->perform->sbfree(cmd_q, op.sb_key, sb_count);
1889 static int ccp_run_passthru_cmd(struct ccp_cmd_queue *cmd_q,
1890 struct ccp_cmd *cmd)
1892 struct ccp_passthru_engine *pt = &cmd->u.passthru;
1893 struct ccp_dm_workarea mask;
1894 struct ccp_data src, dst;
1896 bool in_place = false;
1900 if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
1903 if (!pt->src || !pt->dst)
1906 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
1907 if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
1913 BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT != 1);
1915 memset(&op, 0, sizeof(op));
1917 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
1919 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
1921 op.sb_key = cmd_q->sb_key;
1923 ret = ccp_init_dm_workarea(&mask, cmd_q,
1924 CCP_PASSTHRU_SB_COUNT *
1930 ccp_set_dm_area(&mask, 0, pt->mask, 0, pt->mask_len);
1931 ret = ccp_copy_to_sb(cmd_q, &mask, op.jobid, op.sb_key,
1932 CCP_PASSTHRU_BYTESWAP_NOOP);
1934 cmd->engine_error = cmd_q->cmd_error;
1939 /* Prepare the input and output data workareas. For in-place
1940 * operations we need to set the dma direction to BIDIRECTIONAL
1941 * and copy the src workarea to the dst workarea.
1943 if (sg_virt(pt->src) == sg_virt(pt->dst))
1946 ret = ccp_init_data(&src, cmd_q, pt->src, pt->src_len,
1947 CCP_PASSTHRU_MASKSIZE,
1948 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1955 ret = ccp_init_data(&dst, cmd_q, pt->dst, pt->src_len,
1956 CCP_PASSTHRU_MASKSIZE, DMA_FROM_DEVICE);
1961 /* Send data to the CCP Passthru engine
1962 * Because the CCP engine works on a single source and destination
1963 * dma address at a time, each entry in the source scatterlist
1964 * (after the dma_map_sg call) must be less than or equal to the
1965 * (remaining) length in the destination scatterlist entry and the
1966 * length must be a multiple of CCP_PASSTHRU_BLOCKSIZE
1968 dst.sg_wa.sg_used = 0;
1969 for (i = 1; i <= src.sg_wa.dma_count; i++) {
1970 if (!dst.sg_wa.sg ||
1971 (dst.sg_wa.sg->length < src.sg_wa.sg->length)) {
1976 if (i == src.sg_wa.dma_count) {
1981 op.src.type = CCP_MEMTYPE_SYSTEM;
1982 op.src.u.dma.address = sg_dma_address(src.sg_wa.sg);
1983 op.src.u.dma.offset = 0;
1984 op.src.u.dma.length = sg_dma_len(src.sg_wa.sg);
1986 op.dst.type = CCP_MEMTYPE_SYSTEM;
1987 op.dst.u.dma.address = sg_dma_address(dst.sg_wa.sg);
1988 op.dst.u.dma.offset = dst.sg_wa.sg_used;
1989 op.dst.u.dma.length = op.src.u.dma.length;
1991 ret = cmd_q->ccp->vdata->perform->passthru(&op);
1993 cmd->engine_error = cmd_q->cmd_error;
1997 dst.sg_wa.sg_used += src.sg_wa.sg->length;
1998 if (dst.sg_wa.sg_used == dst.sg_wa.sg->length) {
1999 dst.sg_wa.sg = sg_next(dst.sg_wa.sg);
2000 dst.sg_wa.sg_used = 0;
2002 src.sg_wa.sg = sg_next(src.sg_wa.sg);
2007 ccp_free_data(&dst, cmd_q);
2010 ccp_free_data(&src, cmd_q);
2013 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP)
2019 static int ccp_run_passthru_nomap_cmd(struct ccp_cmd_queue *cmd_q,
2020 struct ccp_cmd *cmd)
2022 struct ccp_passthru_nomap_engine *pt = &cmd->u.passthru_nomap;
2023 struct ccp_dm_workarea mask;
2027 if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
2030 if (!pt->src_dma || !pt->dst_dma)
2033 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
2034 if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
2040 BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT != 1);
2042 memset(&op, 0, sizeof(op));
2044 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
2046 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
2048 op.sb_key = cmd_q->sb_key;
2050 mask.length = pt->mask_len;
2051 mask.dma.address = pt->mask;
2052 mask.dma.length = pt->mask_len;
2054 ret = ccp_copy_to_sb(cmd_q, &mask, op.jobid, op.sb_key,
2055 CCP_PASSTHRU_BYTESWAP_NOOP);
2057 cmd->engine_error = cmd_q->cmd_error;
2062 /* Send data to the CCP Passthru engine */
2066 op.src.type = CCP_MEMTYPE_SYSTEM;
2067 op.src.u.dma.address = pt->src_dma;
2068 op.src.u.dma.offset = 0;
2069 op.src.u.dma.length = pt->src_len;
2071 op.dst.type = CCP_MEMTYPE_SYSTEM;
2072 op.dst.u.dma.address = pt->dst_dma;
2073 op.dst.u.dma.offset = 0;
2074 op.dst.u.dma.length = pt->src_len;
2076 ret = cmd_q->ccp->vdata->perform->passthru(&op);
2078 cmd->engine_error = cmd_q->cmd_error;
2083 static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
2085 struct ccp_ecc_engine *ecc = &cmd->u.ecc;
2086 struct ccp_dm_workarea src, dst;
2091 if (!ecc->u.mm.operand_1 ||
2092 (ecc->u.mm.operand_1_len > CCP_ECC_MODULUS_BYTES))
2095 if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT)
2096 if (!ecc->u.mm.operand_2 ||
2097 (ecc->u.mm.operand_2_len > CCP_ECC_MODULUS_BYTES))
2100 if (!ecc->u.mm.result ||
2101 (ecc->u.mm.result_len < CCP_ECC_MODULUS_BYTES))
2104 memset(&op, 0, sizeof(op));
2106 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
2108 /* Concatenate the modulus and the operands. Both the modulus and
2109 * the operands must be in little endian format. Since the input
2110 * is in big endian format it must be converted and placed in a
2111 * fixed length buffer.
2113 ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
2118 /* Save the workarea address since it is updated in order to perform
2123 /* Copy the ECC modulus */
2124 ret = ccp_reverse_set_dm_area(&src, 0, ecc->mod, 0, ecc->mod_len);
2127 src.address += CCP_ECC_OPERAND_SIZE;
2129 /* Copy the first operand */
2130 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.mm.operand_1, 0,
2131 ecc->u.mm.operand_1_len);
2134 src.address += CCP_ECC_OPERAND_SIZE;
2136 if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT) {
2137 /* Copy the second operand */
2138 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.mm.operand_2, 0,
2139 ecc->u.mm.operand_2_len);
2142 src.address += CCP_ECC_OPERAND_SIZE;
2145 /* Restore the workarea address */
2148 /* Prepare the output area for the operation */
2149 ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
2155 op.src.u.dma.address = src.dma.address;
2156 op.src.u.dma.offset = 0;
2157 op.src.u.dma.length = src.length;
2158 op.dst.u.dma.address = dst.dma.address;
2159 op.dst.u.dma.offset = 0;
2160 op.dst.u.dma.length = dst.length;
2162 op.u.ecc.function = cmd->u.ecc.function;
2164 ret = cmd_q->ccp->vdata->perform->ecc(&op);
2166 cmd->engine_error = cmd_q->cmd_error;
2170 ecc->ecc_result = le16_to_cpup(
2171 (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
2172 if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
2177 /* Save the ECC result */
2178 ccp_reverse_get_dm_area(&dst, 0, ecc->u.mm.result, 0,
2179 CCP_ECC_MODULUS_BYTES);
2190 static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
2192 struct ccp_ecc_engine *ecc = &cmd->u.ecc;
2193 struct ccp_dm_workarea src, dst;
2198 if (!ecc->u.pm.point_1.x ||
2199 (ecc->u.pm.point_1.x_len > CCP_ECC_MODULUS_BYTES) ||
2200 !ecc->u.pm.point_1.y ||
2201 (ecc->u.pm.point_1.y_len > CCP_ECC_MODULUS_BYTES))
2204 if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
2205 if (!ecc->u.pm.point_2.x ||
2206 (ecc->u.pm.point_2.x_len > CCP_ECC_MODULUS_BYTES) ||
2207 !ecc->u.pm.point_2.y ||
2208 (ecc->u.pm.point_2.y_len > CCP_ECC_MODULUS_BYTES))
2211 if (!ecc->u.pm.domain_a ||
2212 (ecc->u.pm.domain_a_len > CCP_ECC_MODULUS_BYTES))
2215 if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT)
2216 if (!ecc->u.pm.scalar ||
2217 (ecc->u.pm.scalar_len > CCP_ECC_MODULUS_BYTES))
2221 if (!ecc->u.pm.result.x ||
2222 (ecc->u.pm.result.x_len < CCP_ECC_MODULUS_BYTES) ||
2223 !ecc->u.pm.result.y ||
2224 (ecc->u.pm.result.y_len < CCP_ECC_MODULUS_BYTES))
2227 memset(&op, 0, sizeof(op));
2229 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
2231 /* Concatenate the modulus and the operands. Both the modulus and
2232 * the operands must be in little endian format. Since the input
2233 * is in big endian format it must be converted and placed in a
2234 * fixed length buffer.
2236 ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
2241 /* Save the workarea address since it is updated in order to perform
2246 /* Copy the ECC modulus */
2247 ret = ccp_reverse_set_dm_area(&src, 0, ecc->mod, 0, ecc->mod_len);
2250 src.address += CCP_ECC_OPERAND_SIZE;
2252 /* Copy the first point X and Y coordinate */
2253 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_1.x, 0,
2254 ecc->u.pm.point_1.x_len);
2257 src.address += CCP_ECC_OPERAND_SIZE;
2258 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_1.y, 0,
2259 ecc->u.pm.point_1.y_len);
2262 src.address += CCP_ECC_OPERAND_SIZE;
2264 /* Set the first point Z coordinate to 1 */
2265 *src.address = 0x01;
2266 src.address += CCP_ECC_OPERAND_SIZE;
2268 if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
2269 /* Copy the second point X and Y coordinate */
2270 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_2.x, 0,
2271 ecc->u.pm.point_2.x_len);
2274 src.address += CCP_ECC_OPERAND_SIZE;
2275 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_2.y, 0,
2276 ecc->u.pm.point_2.y_len);
2279 src.address += CCP_ECC_OPERAND_SIZE;
2281 /* Set the second point Z coordinate to 1 */
2282 *src.address = 0x01;
2283 src.address += CCP_ECC_OPERAND_SIZE;
2285 /* Copy the Domain "a" parameter */
2286 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.domain_a, 0,
2287 ecc->u.pm.domain_a_len);
2290 src.address += CCP_ECC_OPERAND_SIZE;
2292 if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT) {
2293 /* Copy the scalar value */
2294 ret = ccp_reverse_set_dm_area(&src, 0,
2295 ecc->u.pm.scalar, 0,
2296 ecc->u.pm.scalar_len);
2299 src.address += CCP_ECC_OPERAND_SIZE;
2303 /* Restore the workarea address */
2306 /* Prepare the output area for the operation */
2307 ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
2313 op.src.u.dma.address = src.dma.address;
2314 op.src.u.dma.offset = 0;
2315 op.src.u.dma.length = src.length;
2316 op.dst.u.dma.address = dst.dma.address;
2317 op.dst.u.dma.offset = 0;
2318 op.dst.u.dma.length = dst.length;
2320 op.u.ecc.function = cmd->u.ecc.function;
2322 ret = cmd_q->ccp->vdata->perform->ecc(&op);
2324 cmd->engine_error = cmd_q->cmd_error;
2328 ecc->ecc_result = le16_to_cpup(
2329 (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
2330 if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
2335 /* Save the workarea address since it is updated as we walk through
2336 * to copy the point math result
2340 /* Save the ECC result X and Y coordinates */
2341 ccp_reverse_get_dm_area(&dst, 0, ecc->u.pm.result.x, 0,
2342 CCP_ECC_MODULUS_BYTES);
2343 dst.address += CCP_ECC_OUTPUT_SIZE;
2344 ccp_reverse_get_dm_area(&dst, 0, ecc->u.pm.result.y, 0,
2345 CCP_ECC_MODULUS_BYTES);
2346 dst.address += CCP_ECC_OUTPUT_SIZE;
2348 /* Restore the workarea address */
2360 static int ccp_run_ecc_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
2362 struct ccp_ecc_engine *ecc = &cmd->u.ecc;
2364 ecc->ecc_result = 0;
2367 (ecc->mod_len > CCP_ECC_MODULUS_BYTES))
2370 switch (ecc->function) {
2371 case CCP_ECC_FUNCTION_MMUL_384BIT:
2372 case CCP_ECC_FUNCTION_MADD_384BIT:
2373 case CCP_ECC_FUNCTION_MINV_384BIT:
2374 return ccp_run_ecc_mm_cmd(cmd_q, cmd);
2376 case CCP_ECC_FUNCTION_PADD_384BIT:
2377 case CCP_ECC_FUNCTION_PMUL_384BIT:
2378 case CCP_ECC_FUNCTION_PDBL_384BIT:
2379 return ccp_run_ecc_pm_cmd(cmd_q, cmd);
2386 int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
2390 cmd->engine_error = 0;
2391 cmd_q->cmd_error = 0;
2392 cmd_q->int_rcvd = 0;
2393 cmd_q->free_slots = cmd_q->ccp->vdata->perform->get_free_slots(cmd_q);
2395 switch (cmd->engine) {
2396 case CCP_ENGINE_AES:
2397 ret = ccp_run_aes_cmd(cmd_q, cmd);
2399 case CCP_ENGINE_XTS_AES_128:
2400 ret = ccp_run_xts_aes_cmd(cmd_q, cmd);
2402 case CCP_ENGINE_DES3:
2403 ret = ccp_run_des3_cmd(cmd_q, cmd);
2405 case CCP_ENGINE_SHA:
2406 ret = ccp_run_sha_cmd(cmd_q, cmd);
2408 case CCP_ENGINE_RSA:
2409 ret = ccp_run_rsa_cmd(cmd_q, cmd);
2411 case CCP_ENGINE_PASSTHRU:
2412 if (cmd->flags & CCP_CMD_PASSTHRU_NO_DMA_MAP)
2413 ret = ccp_run_passthru_nomap_cmd(cmd_q, cmd);
2415 ret = ccp_run_passthru_cmd(cmd_q, cmd);
2417 case CCP_ENGINE_ECC:
2418 ret = ccp_run_ecc_cmd(cmd_q, cmd);