Loading...
1/*
2 * AMD Cryptographic Coprocessor (CCP) driver
3 *
4 * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
5 *
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/pci.h>
16#include <linux/interrupt.h>
17#include <crypto/scatterwalk.h>
18#include <linux/ccp.h>
19
20#include "ccp-dev.h"
21
22/* SHA initial context values */
23static const __be32 ccp_sha1_init[CCP_SHA_CTXSIZE / sizeof(__be32)] = {
24 cpu_to_be32(SHA1_H0), cpu_to_be32(SHA1_H1),
25 cpu_to_be32(SHA1_H2), cpu_to_be32(SHA1_H3),
26 cpu_to_be32(SHA1_H4), 0, 0, 0,
27};
28
29static const __be32 ccp_sha224_init[CCP_SHA_CTXSIZE / sizeof(__be32)] = {
30 cpu_to_be32(SHA224_H0), cpu_to_be32(SHA224_H1),
31 cpu_to_be32(SHA224_H2), cpu_to_be32(SHA224_H3),
32 cpu_to_be32(SHA224_H4), cpu_to_be32(SHA224_H5),
33 cpu_to_be32(SHA224_H6), cpu_to_be32(SHA224_H7),
34};
35
36static const __be32 ccp_sha256_init[CCP_SHA_CTXSIZE / sizeof(__be32)] = {
37 cpu_to_be32(SHA256_H0), cpu_to_be32(SHA256_H1),
38 cpu_to_be32(SHA256_H2), cpu_to_be32(SHA256_H3),
39 cpu_to_be32(SHA256_H4), cpu_to_be32(SHA256_H5),
40 cpu_to_be32(SHA256_H6), cpu_to_be32(SHA256_H7),
41};
42
43static u32 ccp_alloc_ksb(struct ccp_device *ccp, unsigned int count)
44{
45 int start;
46
47 for (;;) {
48 mutex_lock(&ccp->ksb_mutex);
49
50 start = (u32)bitmap_find_next_zero_area(ccp->ksb,
51 ccp->ksb_count,
52 ccp->ksb_start,
53 count, 0);
54 if (start <= ccp->ksb_count) {
55 bitmap_set(ccp->ksb, start, count);
56
57 mutex_unlock(&ccp->ksb_mutex);
58 break;
59 }
60
61 ccp->ksb_avail = 0;
62
63 mutex_unlock(&ccp->ksb_mutex);
64
65 /* Wait for KSB entries to become available */
66 if (wait_event_interruptible(ccp->ksb_queue, ccp->ksb_avail))
67 return 0;
68 }
69
70 return KSB_START + start;
71}
72
73static void ccp_free_ksb(struct ccp_device *ccp, unsigned int start,
74 unsigned int count)
75{
76 if (!start)
77 return;
78
79 mutex_lock(&ccp->ksb_mutex);
80
81 bitmap_clear(ccp->ksb, start - KSB_START, count);
82
83 ccp->ksb_avail = 1;
84
85 mutex_unlock(&ccp->ksb_mutex);
86
87 wake_up_interruptible_all(&ccp->ksb_queue);
88}
89
90static u32 ccp_gen_jobid(struct ccp_device *ccp)
91{
92 return atomic_inc_return(&ccp->current_id) & CCP_JOBID_MASK;
93}
94
95static void ccp_sg_free(struct ccp_sg_workarea *wa)
96{
97 if (wa->dma_count)
98 dma_unmap_sg(wa->dma_dev, wa->dma_sg, wa->nents, wa->dma_dir);
99
100 wa->dma_count = 0;
101}
102
103static int ccp_init_sg_workarea(struct ccp_sg_workarea *wa, struct device *dev,
104 struct scatterlist *sg, u64 len,
105 enum dma_data_direction dma_dir)
106{
107 memset(wa, 0, sizeof(*wa));
108
109 wa->sg = sg;
110 if (!sg)
111 return 0;
112
113 wa->nents = sg_nents_for_len(sg, len);
114 if (wa->nents < 0)
115 return wa->nents;
116
117 wa->bytes_left = len;
118 wa->sg_used = 0;
119
120 if (len == 0)
121 return 0;
122
123 if (dma_dir == DMA_NONE)
124 return 0;
125
126 wa->dma_sg = sg;
127 wa->dma_dev = dev;
128 wa->dma_dir = dma_dir;
129 wa->dma_count = dma_map_sg(dev, sg, wa->nents, dma_dir);
130 if (!wa->dma_count)
131 return -ENOMEM;
132
133 return 0;
134}
135
136static void ccp_update_sg_workarea(struct ccp_sg_workarea *wa, unsigned int len)
137{
138 unsigned int nbytes = min_t(u64, len, wa->bytes_left);
139
140 if (!wa->sg)
141 return;
142
143 wa->sg_used += nbytes;
144 wa->bytes_left -= nbytes;
145 if (wa->sg_used == wa->sg->length) {
146 wa->sg = sg_next(wa->sg);
147 wa->sg_used = 0;
148 }
149}
150
151static void ccp_dm_free(struct ccp_dm_workarea *wa)
152{
153 if (wa->length <= CCP_DMAPOOL_MAX_SIZE) {
154 if (wa->address)
155 dma_pool_free(wa->dma_pool, wa->address,
156 wa->dma.address);
157 } else {
158 if (wa->dma.address)
159 dma_unmap_single(wa->dev, wa->dma.address, wa->length,
160 wa->dma.dir);
161 kfree(wa->address);
162 }
163
164 wa->address = NULL;
165 wa->dma.address = 0;
166}
167
168static int ccp_init_dm_workarea(struct ccp_dm_workarea *wa,
169 struct ccp_cmd_queue *cmd_q,
170 unsigned int len,
171 enum dma_data_direction dir)
172{
173 memset(wa, 0, sizeof(*wa));
174
175 if (!len)
176 return 0;
177
178 wa->dev = cmd_q->ccp->dev;
179 wa->length = len;
180
181 if (len <= CCP_DMAPOOL_MAX_SIZE) {
182 wa->dma_pool = cmd_q->dma_pool;
183
184 wa->address = dma_pool_alloc(wa->dma_pool, GFP_KERNEL,
185 &wa->dma.address);
186 if (!wa->address)
187 return -ENOMEM;
188
189 wa->dma.length = CCP_DMAPOOL_MAX_SIZE;
190
191 memset(wa->address, 0, CCP_DMAPOOL_MAX_SIZE);
192 } else {
193 wa->address = kzalloc(len, GFP_KERNEL);
194 if (!wa->address)
195 return -ENOMEM;
196
197 wa->dma.address = dma_map_single(wa->dev, wa->address, len,
198 dir);
199 if (!wa->dma.address)
200 return -ENOMEM;
201
202 wa->dma.length = len;
203 }
204 wa->dma.dir = dir;
205
206 return 0;
207}
208
209static void ccp_set_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
210 struct scatterlist *sg, unsigned int sg_offset,
211 unsigned int len)
212{
213 WARN_ON(!wa->address);
214
215 scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
216 0);
217}
218
219static void ccp_get_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
220 struct scatterlist *sg, unsigned int sg_offset,
221 unsigned int len)
222{
223 WARN_ON(!wa->address);
224
225 scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
226 1);
227}
228
229static int ccp_reverse_set_dm_area(struct ccp_dm_workarea *wa,
230 struct scatterlist *sg,
231 unsigned int len, unsigned int se_len,
232 bool sign_extend)
233{
234 unsigned int nbytes, sg_offset, dm_offset, ksb_len, i;
235 u8 buffer[CCP_REVERSE_BUF_SIZE];
236
237 if (WARN_ON(se_len > sizeof(buffer)))
238 return -EINVAL;
239
240 sg_offset = len;
241 dm_offset = 0;
242 nbytes = len;
243 while (nbytes) {
244 ksb_len = min_t(unsigned int, nbytes, se_len);
245 sg_offset -= ksb_len;
246
247 scatterwalk_map_and_copy(buffer, sg, sg_offset, ksb_len, 0);
248 for (i = 0; i < ksb_len; i++)
249 wa->address[dm_offset + i] = buffer[ksb_len - i - 1];
250
251 dm_offset += ksb_len;
252 nbytes -= ksb_len;
253
254 if ((ksb_len != se_len) && sign_extend) {
255 /* Must sign-extend to nearest sign-extend length */
256 if (wa->address[dm_offset - 1] & 0x80)
257 memset(wa->address + dm_offset, 0xff,
258 se_len - ksb_len);
259 }
260 }
261
262 return 0;
263}
264
265static void ccp_reverse_get_dm_area(struct ccp_dm_workarea *wa,
266 struct scatterlist *sg,
267 unsigned int len)
268{
269 unsigned int nbytes, sg_offset, dm_offset, ksb_len, i;
270 u8 buffer[CCP_REVERSE_BUF_SIZE];
271
272 sg_offset = 0;
273 dm_offset = len;
274 nbytes = len;
275 while (nbytes) {
276 ksb_len = min_t(unsigned int, nbytes, sizeof(buffer));
277 dm_offset -= ksb_len;
278
279 for (i = 0; i < ksb_len; i++)
280 buffer[ksb_len - i - 1] = wa->address[dm_offset + i];
281 scatterwalk_map_and_copy(buffer, sg, sg_offset, ksb_len, 1);
282
283 sg_offset += ksb_len;
284 nbytes -= ksb_len;
285 }
286}
287
288static void ccp_free_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q)
289{
290 ccp_dm_free(&data->dm_wa);
291 ccp_sg_free(&data->sg_wa);
292}
293
294static int ccp_init_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q,
295 struct scatterlist *sg, u64 sg_len,
296 unsigned int dm_len,
297 enum dma_data_direction dir)
298{
299 int ret;
300
301 memset(data, 0, sizeof(*data));
302
303 ret = ccp_init_sg_workarea(&data->sg_wa, cmd_q->ccp->dev, sg, sg_len,
304 dir);
305 if (ret)
306 goto e_err;
307
308 ret = ccp_init_dm_workarea(&data->dm_wa, cmd_q, dm_len, dir);
309 if (ret)
310 goto e_err;
311
312 return 0;
313
314e_err:
315 ccp_free_data(data, cmd_q);
316
317 return ret;
318}
319
320static unsigned int ccp_queue_buf(struct ccp_data *data, unsigned int from)
321{
322 struct ccp_sg_workarea *sg_wa = &data->sg_wa;
323 struct ccp_dm_workarea *dm_wa = &data->dm_wa;
324 unsigned int buf_count, nbytes;
325
326 /* Clear the buffer if setting it */
327 if (!from)
328 memset(dm_wa->address, 0, dm_wa->length);
329
330 if (!sg_wa->sg)
331 return 0;
332
333 /* Perform the copy operation
334 * nbytes will always be <= UINT_MAX because dm_wa->length is
335 * an unsigned int
336 */
337 nbytes = min_t(u64, sg_wa->bytes_left, dm_wa->length);
338 scatterwalk_map_and_copy(dm_wa->address, sg_wa->sg, sg_wa->sg_used,
339 nbytes, from);
340
341 /* Update the structures and generate the count */
342 buf_count = 0;
343 while (sg_wa->bytes_left && (buf_count < dm_wa->length)) {
344 nbytes = min(sg_wa->sg->length - sg_wa->sg_used,
345 dm_wa->length - buf_count);
346 nbytes = min_t(u64, sg_wa->bytes_left, nbytes);
347
348 buf_count += nbytes;
349 ccp_update_sg_workarea(sg_wa, nbytes);
350 }
351
352 return buf_count;
353}
354
355static unsigned int ccp_fill_queue_buf(struct ccp_data *data)
356{
357 return ccp_queue_buf(data, 0);
358}
359
360static unsigned int ccp_empty_queue_buf(struct ccp_data *data)
361{
362 return ccp_queue_buf(data, 1);
363}
364
365static void ccp_prepare_data(struct ccp_data *src, struct ccp_data *dst,
366 struct ccp_op *op, unsigned int block_size,
367 bool blocksize_op)
368{
369 unsigned int sg_src_len, sg_dst_len, op_len;
370
371 /* The CCP can only DMA from/to one address each per operation. This
372 * requires that we find the smallest DMA area between the source
373 * and destination. The resulting len values will always be <= UINT_MAX
374 * because the dma length is an unsigned int.
375 */
376 sg_src_len = sg_dma_len(src->sg_wa.sg) - src->sg_wa.sg_used;
377 sg_src_len = min_t(u64, src->sg_wa.bytes_left, sg_src_len);
378
379 if (dst) {
380 sg_dst_len = sg_dma_len(dst->sg_wa.sg) - dst->sg_wa.sg_used;
381 sg_dst_len = min_t(u64, src->sg_wa.bytes_left, sg_dst_len);
382 op_len = min(sg_src_len, sg_dst_len);
383 } else {
384 op_len = sg_src_len;
385 }
386
387 /* The data operation length will be at least block_size in length
388 * or the smaller of available sg room remaining for the source or
389 * the destination
390 */
391 op_len = max(op_len, block_size);
392
393 /* Unless we have to buffer data, there's no reason to wait */
394 op->soc = 0;
395
396 if (sg_src_len < block_size) {
397 /* Not enough data in the sg element, so it
398 * needs to be buffered into a blocksize chunk
399 */
400 int cp_len = ccp_fill_queue_buf(src);
401
402 op->soc = 1;
403 op->src.u.dma.address = src->dm_wa.dma.address;
404 op->src.u.dma.offset = 0;
405 op->src.u.dma.length = (blocksize_op) ? block_size : cp_len;
406 } else {
407 /* Enough data in the sg element, but we need to
408 * adjust for any previously copied data
409 */
410 op->src.u.dma.address = sg_dma_address(src->sg_wa.sg);
411 op->src.u.dma.offset = src->sg_wa.sg_used;
412 op->src.u.dma.length = op_len & ~(block_size - 1);
413
414 ccp_update_sg_workarea(&src->sg_wa, op->src.u.dma.length);
415 }
416
417 if (dst) {
418 if (sg_dst_len < block_size) {
419 /* Not enough room in the sg element or we're on the
420 * last piece of data (when using padding), so the
421 * output needs to be buffered into a blocksize chunk
422 */
423 op->soc = 1;
424 op->dst.u.dma.address = dst->dm_wa.dma.address;
425 op->dst.u.dma.offset = 0;
426 op->dst.u.dma.length = op->src.u.dma.length;
427 } else {
428 /* Enough room in the sg element, but we need to
429 * adjust for any previously used area
430 */
431 op->dst.u.dma.address = sg_dma_address(dst->sg_wa.sg);
432 op->dst.u.dma.offset = dst->sg_wa.sg_used;
433 op->dst.u.dma.length = op->src.u.dma.length;
434 }
435 }
436}
437
438static void ccp_process_data(struct ccp_data *src, struct ccp_data *dst,
439 struct ccp_op *op)
440{
441 op->init = 0;
442
443 if (dst) {
444 if (op->dst.u.dma.address == dst->dm_wa.dma.address)
445 ccp_empty_queue_buf(dst);
446 else
447 ccp_update_sg_workarea(&dst->sg_wa,
448 op->dst.u.dma.length);
449 }
450}
451
452static int ccp_copy_to_from_ksb(struct ccp_cmd_queue *cmd_q,
453 struct ccp_dm_workarea *wa, u32 jobid, u32 ksb,
454 u32 byte_swap, bool from)
455{
456 struct ccp_op op;
457
458 memset(&op, 0, sizeof(op));
459
460 op.cmd_q = cmd_q;
461 op.jobid = jobid;
462 op.eom = 1;
463
464 if (from) {
465 op.soc = 1;
466 op.src.type = CCP_MEMTYPE_KSB;
467 op.src.u.ksb = ksb;
468 op.dst.type = CCP_MEMTYPE_SYSTEM;
469 op.dst.u.dma.address = wa->dma.address;
470 op.dst.u.dma.length = wa->length;
471 } else {
472 op.src.type = CCP_MEMTYPE_SYSTEM;
473 op.src.u.dma.address = wa->dma.address;
474 op.src.u.dma.length = wa->length;
475 op.dst.type = CCP_MEMTYPE_KSB;
476 op.dst.u.ksb = ksb;
477 }
478
479 op.u.passthru.byte_swap = byte_swap;
480
481 return cmd_q->ccp->vdata->perform->perform_passthru(&op);
482}
483
484static int ccp_copy_to_ksb(struct ccp_cmd_queue *cmd_q,
485 struct ccp_dm_workarea *wa, u32 jobid, u32 ksb,
486 u32 byte_swap)
487{
488 return ccp_copy_to_from_ksb(cmd_q, wa, jobid, ksb, byte_swap, false);
489}
490
491static int ccp_copy_from_ksb(struct ccp_cmd_queue *cmd_q,
492 struct ccp_dm_workarea *wa, u32 jobid, u32 ksb,
493 u32 byte_swap)
494{
495 return ccp_copy_to_from_ksb(cmd_q, wa, jobid, ksb, byte_swap, true);
496}
497
498static int ccp_run_aes_cmac_cmd(struct ccp_cmd_queue *cmd_q,
499 struct ccp_cmd *cmd)
500{
501 struct ccp_aes_engine *aes = &cmd->u.aes;
502 struct ccp_dm_workarea key, ctx;
503 struct ccp_data src;
504 struct ccp_op op;
505 unsigned int dm_offset;
506 int ret;
507
508 if (!((aes->key_len == AES_KEYSIZE_128) ||
509 (aes->key_len == AES_KEYSIZE_192) ||
510 (aes->key_len == AES_KEYSIZE_256)))
511 return -EINVAL;
512
513 if (aes->src_len & (AES_BLOCK_SIZE - 1))
514 return -EINVAL;
515
516 if (aes->iv_len != AES_BLOCK_SIZE)
517 return -EINVAL;
518
519 if (!aes->key || !aes->iv || !aes->src)
520 return -EINVAL;
521
522 if (aes->cmac_final) {
523 if (aes->cmac_key_len != AES_BLOCK_SIZE)
524 return -EINVAL;
525
526 if (!aes->cmac_key)
527 return -EINVAL;
528 }
529
530 BUILD_BUG_ON(CCP_AES_KEY_KSB_COUNT != 1);
531 BUILD_BUG_ON(CCP_AES_CTX_KSB_COUNT != 1);
532
533 ret = -EIO;
534 memset(&op, 0, sizeof(op));
535 op.cmd_q = cmd_q;
536 op.jobid = ccp_gen_jobid(cmd_q->ccp);
537 op.ksb_key = cmd_q->ksb_key;
538 op.ksb_ctx = cmd_q->ksb_ctx;
539 op.init = 1;
540 op.u.aes.type = aes->type;
541 op.u.aes.mode = aes->mode;
542 op.u.aes.action = aes->action;
543
544 /* All supported key sizes fit in a single (32-byte) KSB entry
545 * and must be in little endian format. Use the 256-bit byte
546 * swap passthru option to convert from big endian to little
547 * endian.
548 */
549 ret = ccp_init_dm_workarea(&key, cmd_q,
550 CCP_AES_KEY_KSB_COUNT * CCP_KSB_BYTES,
551 DMA_TO_DEVICE);
552 if (ret)
553 return ret;
554
555 dm_offset = CCP_KSB_BYTES - aes->key_len;
556 ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
557 ret = ccp_copy_to_ksb(cmd_q, &key, op.jobid, op.ksb_key,
558 CCP_PASSTHRU_BYTESWAP_256BIT);
559 if (ret) {
560 cmd->engine_error = cmd_q->cmd_error;
561 goto e_key;
562 }
563
564 /* The AES context fits in a single (32-byte) KSB entry and
565 * must be in little endian format. Use the 256-bit byte swap
566 * passthru option to convert from big endian to little endian.
567 */
568 ret = ccp_init_dm_workarea(&ctx, cmd_q,
569 CCP_AES_CTX_KSB_COUNT * CCP_KSB_BYTES,
570 DMA_BIDIRECTIONAL);
571 if (ret)
572 goto e_key;
573
574 dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
575 ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
576 ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
577 CCP_PASSTHRU_BYTESWAP_256BIT);
578 if (ret) {
579 cmd->engine_error = cmd_q->cmd_error;
580 goto e_ctx;
581 }
582
583 /* Send data to the CCP AES engine */
584 ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
585 AES_BLOCK_SIZE, DMA_TO_DEVICE);
586 if (ret)
587 goto e_ctx;
588
589 while (src.sg_wa.bytes_left) {
590 ccp_prepare_data(&src, NULL, &op, AES_BLOCK_SIZE, true);
591 if (aes->cmac_final && !src.sg_wa.bytes_left) {
592 op.eom = 1;
593
594 /* Push the K1/K2 key to the CCP now */
595 ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid,
596 op.ksb_ctx,
597 CCP_PASSTHRU_BYTESWAP_256BIT);
598 if (ret) {
599 cmd->engine_error = cmd_q->cmd_error;
600 goto e_src;
601 }
602
603 ccp_set_dm_area(&ctx, 0, aes->cmac_key, 0,
604 aes->cmac_key_len);
605 ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
606 CCP_PASSTHRU_BYTESWAP_256BIT);
607 if (ret) {
608 cmd->engine_error = cmd_q->cmd_error;
609 goto e_src;
610 }
611 }
612
613 ret = cmd_q->ccp->vdata->perform->perform_aes(&op);
614 if (ret) {
615 cmd->engine_error = cmd_q->cmd_error;
616 goto e_src;
617 }
618
619 ccp_process_data(&src, NULL, &op);
620 }
621
622 /* Retrieve the AES context - convert from LE to BE using
623 * 32-byte (256-bit) byteswapping
624 */
625 ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
626 CCP_PASSTHRU_BYTESWAP_256BIT);
627 if (ret) {
628 cmd->engine_error = cmd_q->cmd_error;
629 goto e_src;
630 }
631
632 /* ...but we only need AES_BLOCK_SIZE bytes */
633 dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
634 ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
635
636e_src:
637 ccp_free_data(&src, cmd_q);
638
639e_ctx:
640 ccp_dm_free(&ctx);
641
642e_key:
643 ccp_dm_free(&key);
644
645 return ret;
646}
647
648static int ccp_run_aes_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
649{
650 struct ccp_aes_engine *aes = &cmd->u.aes;
651 struct ccp_dm_workarea key, ctx;
652 struct ccp_data src, dst;
653 struct ccp_op op;
654 unsigned int dm_offset;
655 bool in_place = false;
656 int ret;
657
658 if (aes->mode == CCP_AES_MODE_CMAC)
659 return ccp_run_aes_cmac_cmd(cmd_q, cmd);
660
661 if (!((aes->key_len == AES_KEYSIZE_128) ||
662 (aes->key_len == AES_KEYSIZE_192) ||
663 (aes->key_len == AES_KEYSIZE_256)))
664 return -EINVAL;
665
666 if (((aes->mode == CCP_AES_MODE_ECB) ||
667 (aes->mode == CCP_AES_MODE_CBC) ||
668 (aes->mode == CCP_AES_MODE_CFB)) &&
669 (aes->src_len & (AES_BLOCK_SIZE - 1)))
670 return -EINVAL;
671
672 if (!aes->key || !aes->src || !aes->dst)
673 return -EINVAL;
674
675 if (aes->mode != CCP_AES_MODE_ECB) {
676 if (aes->iv_len != AES_BLOCK_SIZE)
677 return -EINVAL;
678
679 if (!aes->iv)
680 return -EINVAL;
681 }
682
683 BUILD_BUG_ON(CCP_AES_KEY_KSB_COUNT != 1);
684 BUILD_BUG_ON(CCP_AES_CTX_KSB_COUNT != 1);
685
686 ret = -EIO;
687 memset(&op, 0, sizeof(op));
688 op.cmd_q = cmd_q;
689 op.jobid = ccp_gen_jobid(cmd_q->ccp);
690 op.ksb_key = cmd_q->ksb_key;
691 op.ksb_ctx = cmd_q->ksb_ctx;
692 op.init = (aes->mode == CCP_AES_MODE_ECB) ? 0 : 1;
693 op.u.aes.type = aes->type;
694 op.u.aes.mode = aes->mode;
695 op.u.aes.action = aes->action;
696
697 /* All supported key sizes fit in a single (32-byte) KSB entry
698 * and must be in little endian format. Use the 256-bit byte
699 * swap passthru option to convert from big endian to little
700 * endian.
701 */
702 ret = ccp_init_dm_workarea(&key, cmd_q,
703 CCP_AES_KEY_KSB_COUNT * CCP_KSB_BYTES,
704 DMA_TO_DEVICE);
705 if (ret)
706 return ret;
707
708 dm_offset = CCP_KSB_BYTES - aes->key_len;
709 ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
710 ret = ccp_copy_to_ksb(cmd_q, &key, op.jobid, op.ksb_key,
711 CCP_PASSTHRU_BYTESWAP_256BIT);
712 if (ret) {
713 cmd->engine_error = cmd_q->cmd_error;
714 goto e_key;
715 }
716
717 /* The AES context fits in a single (32-byte) KSB entry and
718 * must be in little endian format. Use the 256-bit byte swap
719 * passthru option to convert from big endian to little endian.
720 */
721 ret = ccp_init_dm_workarea(&ctx, cmd_q,
722 CCP_AES_CTX_KSB_COUNT * CCP_KSB_BYTES,
723 DMA_BIDIRECTIONAL);
724 if (ret)
725 goto e_key;
726
727 if (aes->mode != CCP_AES_MODE_ECB) {
728 /* Load the AES context - conver to LE */
729 dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
730 ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
731 ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
732 CCP_PASSTHRU_BYTESWAP_256BIT);
733 if (ret) {
734 cmd->engine_error = cmd_q->cmd_error;
735 goto e_ctx;
736 }
737 }
738
739 /* Prepare the input and output data workareas. For in-place
740 * operations we need to set the dma direction to BIDIRECTIONAL
741 * and copy the src workarea to the dst workarea.
742 */
743 if (sg_virt(aes->src) == sg_virt(aes->dst))
744 in_place = true;
745
746 ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
747 AES_BLOCK_SIZE,
748 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
749 if (ret)
750 goto e_ctx;
751
752 if (in_place) {
753 dst = src;
754 } else {
755 ret = ccp_init_data(&dst, cmd_q, aes->dst, aes->src_len,
756 AES_BLOCK_SIZE, DMA_FROM_DEVICE);
757 if (ret)
758 goto e_src;
759 }
760
761 /* Send data to the CCP AES engine */
762 while (src.sg_wa.bytes_left) {
763 ccp_prepare_data(&src, &dst, &op, AES_BLOCK_SIZE, true);
764 if (!src.sg_wa.bytes_left) {
765 op.eom = 1;
766
767 /* Since we don't retrieve the AES context in ECB
768 * mode we have to wait for the operation to complete
769 * on the last piece of data
770 */
771 if (aes->mode == CCP_AES_MODE_ECB)
772 op.soc = 1;
773 }
774
775 ret = cmd_q->ccp->vdata->perform->perform_aes(&op);
776 if (ret) {
777 cmd->engine_error = cmd_q->cmd_error;
778 goto e_dst;
779 }
780
781 ccp_process_data(&src, &dst, &op);
782 }
783
784 if (aes->mode != CCP_AES_MODE_ECB) {
785 /* Retrieve the AES context - convert from LE to BE using
786 * 32-byte (256-bit) byteswapping
787 */
788 ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
789 CCP_PASSTHRU_BYTESWAP_256BIT);
790 if (ret) {
791 cmd->engine_error = cmd_q->cmd_error;
792 goto e_dst;
793 }
794
795 /* ...but we only need AES_BLOCK_SIZE bytes */
796 dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
797 ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
798 }
799
800e_dst:
801 if (!in_place)
802 ccp_free_data(&dst, cmd_q);
803
804e_src:
805 ccp_free_data(&src, cmd_q);
806
807e_ctx:
808 ccp_dm_free(&ctx);
809
810e_key:
811 ccp_dm_free(&key);
812
813 return ret;
814}
815
816static int ccp_run_xts_aes_cmd(struct ccp_cmd_queue *cmd_q,
817 struct ccp_cmd *cmd)
818{
819 struct ccp_xts_aes_engine *xts = &cmd->u.xts;
820 struct ccp_dm_workarea key, ctx;
821 struct ccp_data src, dst;
822 struct ccp_op op;
823 unsigned int unit_size, dm_offset;
824 bool in_place = false;
825 int ret;
826
827 switch (xts->unit_size) {
828 case CCP_XTS_AES_UNIT_SIZE_16:
829 unit_size = 16;
830 break;
831 case CCP_XTS_AES_UNIT_SIZE_512:
832 unit_size = 512;
833 break;
834 case CCP_XTS_AES_UNIT_SIZE_1024:
835 unit_size = 1024;
836 break;
837 case CCP_XTS_AES_UNIT_SIZE_2048:
838 unit_size = 2048;
839 break;
840 case CCP_XTS_AES_UNIT_SIZE_4096:
841 unit_size = 4096;
842 break;
843
844 default:
845 return -EINVAL;
846 }
847
848 if (xts->key_len != AES_KEYSIZE_128)
849 return -EINVAL;
850
851 if (!xts->final && (xts->src_len & (AES_BLOCK_SIZE - 1)))
852 return -EINVAL;
853
854 if (xts->iv_len != AES_BLOCK_SIZE)
855 return -EINVAL;
856
857 if (!xts->key || !xts->iv || !xts->src || !xts->dst)
858 return -EINVAL;
859
860 BUILD_BUG_ON(CCP_XTS_AES_KEY_KSB_COUNT != 1);
861 BUILD_BUG_ON(CCP_XTS_AES_CTX_KSB_COUNT != 1);
862
863 ret = -EIO;
864 memset(&op, 0, sizeof(op));
865 op.cmd_q = cmd_q;
866 op.jobid = ccp_gen_jobid(cmd_q->ccp);
867 op.ksb_key = cmd_q->ksb_key;
868 op.ksb_ctx = cmd_q->ksb_ctx;
869 op.init = 1;
870 op.u.xts.action = xts->action;
871 op.u.xts.unit_size = xts->unit_size;
872
873 /* All supported key sizes fit in a single (32-byte) KSB entry
874 * and must be in little endian format. Use the 256-bit byte
875 * swap passthru option to convert from big endian to little
876 * endian.
877 */
878 ret = ccp_init_dm_workarea(&key, cmd_q,
879 CCP_XTS_AES_KEY_KSB_COUNT * CCP_KSB_BYTES,
880 DMA_TO_DEVICE);
881 if (ret)
882 return ret;
883
884 dm_offset = CCP_KSB_BYTES - AES_KEYSIZE_128;
885 ccp_set_dm_area(&key, dm_offset, xts->key, 0, xts->key_len);
886 ccp_set_dm_area(&key, 0, xts->key, dm_offset, xts->key_len);
887 ret = ccp_copy_to_ksb(cmd_q, &key, op.jobid, op.ksb_key,
888 CCP_PASSTHRU_BYTESWAP_256BIT);
889 if (ret) {
890 cmd->engine_error = cmd_q->cmd_error;
891 goto e_key;
892 }
893
894 /* The AES context fits in a single (32-byte) KSB entry and
895 * for XTS is already in little endian format so no byte swapping
896 * is needed.
897 */
898 ret = ccp_init_dm_workarea(&ctx, cmd_q,
899 CCP_XTS_AES_CTX_KSB_COUNT * CCP_KSB_BYTES,
900 DMA_BIDIRECTIONAL);
901 if (ret)
902 goto e_key;
903
904 ccp_set_dm_area(&ctx, 0, xts->iv, 0, xts->iv_len);
905 ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
906 CCP_PASSTHRU_BYTESWAP_NOOP);
907 if (ret) {
908 cmd->engine_error = cmd_q->cmd_error;
909 goto e_ctx;
910 }
911
912 /* Prepare the input and output data workareas. For in-place
913 * operations we need to set the dma direction to BIDIRECTIONAL
914 * and copy the src workarea to the dst workarea.
915 */
916 if (sg_virt(xts->src) == sg_virt(xts->dst))
917 in_place = true;
918
919 ret = ccp_init_data(&src, cmd_q, xts->src, xts->src_len,
920 unit_size,
921 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
922 if (ret)
923 goto e_ctx;
924
925 if (in_place) {
926 dst = src;
927 } else {
928 ret = ccp_init_data(&dst, cmd_q, xts->dst, xts->src_len,
929 unit_size, DMA_FROM_DEVICE);
930 if (ret)
931 goto e_src;
932 }
933
934 /* Send data to the CCP AES engine */
935 while (src.sg_wa.bytes_left) {
936 ccp_prepare_data(&src, &dst, &op, unit_size, true);
937 if (!src.sg_wa.bytes_left)
938 op.eom = 1;
939
940 ret = cmd_q->ccp->vdata->perform->perform_xts_aes(&op);
941 if (ret) {
942 cmd->engine_error = cmd_q->cmd_error;
943 goto e_dst;
944 }
945
946 ccp_process_data(&src, &dst, &op);
947 }
948
949 /* Retrieve the AES context - convert from LE to BE using
950 * 32-byte (256-bit) byteswapping
951 */
952 ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
953 CCP_PASSTHRU_BYTESWAP_256BIT);
954 if (ret) {
955 cmd->engine_error = cmd_q->cmd_error;
956 goto e_dst;
957 }
958
959 /* ...but we only need AES_BLOCK_SIZE bytes */
960 dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
961 ccp_get_dm_area(&ctx, dm_offset, xts->iv, 0, xts->iv_len);
962
963e_dst:
964 if (!in_place)
965 ccp_free_data(&dst, cmd_q);
966
967e_src:
968 ccp_free_data(&src, cmd_q);
969
970e_ctx:
971 ccp_dm_free(&ctx);
972
973e_key:
974 ccp_dm_free(&key);
975
976 return ret;
977}
978
979static int ccp_run_sha_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
980{
981 struct ccp_sha_engine *sha = &cmd->u.sha;
982 struct ccp_dm_workarea ctx;
983 struct ccp_data src;
984 struct ccp_op op;
985 int ret;
986
987 if (sha->ctx_len != CCP_SHA_CTXSIZE)
988 return -EINVAL;
989
990 if (!sha->ctx)
991 return -EINVAL;
992
993 if (!sha->final && (sha->src_len & (CCP_SHA_BLOCKSIZE - 1)))
994 return -EINVAL;
995
996 if (!sha->src_len) {
997 const u8 *sha_zero;
998
999 /* Not final, just return */
1000 if (!sha->final)
1001 return 0;
1002
1003 /* CCP can't do a zero length sha operation so the caller
1004 * must buffer the data.
1005 */
1006 if (sha->msg_bits)
1007 return -EINVAL;
1008
1009 /* The CCP cannot perform zero-length sha operations so the
1010 * caller is required to buffer data for the final operation.
1011 * However, a sha operation for a message with a total length
1012 * of zero is valid so known values are required to supply
1013 * the result.
1014 */
1015 switch (sha->type) {
1016 case CCP_SHA_TYPE_1:
1017 sha_zero = sha1_zero_message_hash;
1018 break;
1019 case CCP_SHA_TYPE_224:
1020 sha_zero = sha224_zero_message_hash;
1021 break;
1022 case CCP_SHA_TYPE_256:
1023 sha_zero = sha256_zero_message_hash;
1024 break;
1025 default:
1026 return -EINVAL;
1027 }
1028
1029 scatterwalk_map_and_copy((void *)sha_zero, sha->ctx, 0,
1030 sha->ctx_len, 1);
1031
1032 return 0;
1033 }
1034
1035 if (!sha->src)
1036 return -EINVAL;
1037
1038 BUILD_BUG_ON(CCP_SHA_KSB_COUNT != 1);
1039
1040 memset(&op, 0, sizeof(op));
1041 op.cmd_q = cmd_q;
1042 op.jobid = ccp_gen_jobid(cmd_q->ccp);
1043 op.ksb_ctx = cmd_q->ksb_ctx;
1044 op.u.sha.type = sha->type;
1045 op.u.sha.msg_bits = sha->msg_bits;
1046
1047 /* The SHA context fits in a single (32-byte) KSB entry and
1048 * must be in little endian format. Use the 256-bit byte swap
1049 * passthru option to convert from big endian to little endian.
1050 */
1051 ret = ccp_init_dm_workarea(&ctx, cmd_q,
1052 CCP_SHA_KSB_COUNT * CCP_KSB_BYTES,
1053 DMA_BIDIRECTIONAL);
1054 if (ret)
1055 return ret;
1056
1057 if (sha->first) {
1058 const __be32 *init;
1059
1060 switch (sha->type) {
1061 case CCP_SHA_TYPE_1:
1062 init = ccp_sha1_init;
1063 break;
1064 case CCP_SHA_TYPE_224:
1065 init = ccp_sha224_init;
1066 break;
1067 case CCP_SHA_TYPE_256:
1068 init = ccp_sha256_init;
1069 break;
1070 default:
1071 ret = -EINVAL;
1072 goto e_ctx;
1073 }
1074 memcpy(ctx.address, init, CCP_SHA_CTXSIZE);
1075 } else {
1076 ccp_set_dm_area(&ctx, 0, sha->ctx, 0, sha->ctx_len);
1077 }
1078
1079 ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
1080 CCP_PASSTHRU_BYTESWAP_256BIT);
1081 if (ret) {
1082 cmd->engine_error = cmd_q->cmd_error;
1083 goto e_ctx;
1084 }
1085
1086 /* Send data to the CCP SHA engine */
1087 ret = ccp_init_data(&src, cmd_q, sha->src, sha->src_len,
1088 CCP_SHA_BLOCKSIZE, DMA_TO_DEVICE);
1089 if (ret)
1090 goto e_ctx;
1091
1092 while (src.sg_wa.bytes_left) {
1093 ccp_prepare_data(&src, NULL, &op, CCP_SHA_BLOCKSIZE, false);
1094 if (sha->final && !src.sg_wa.bytes_left)
1095 op.eom = 1;
1096
1097 ret = cmd_q->ccp->vdata->perform->perform_sha(&op);
1098 if (ret) {
1099 cmd->engine_error = cmd_q->cmd_error;
1100 goto e_data;
1101 }
1102
1103 ccp_process_data(&src, NULL, &op);
1104 }
1105
1106 /* Retrieve the SHA context - convert from LE to BE using
1107 * 32-byte (256-bit) byteswapping to BE
1108 */
1109 ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
1110 CCP_PASSTHRU_BYTESWAP_256BIT);
1111 if (ret) {
1112 cmd->engine_error = cmd_q->cmd_error;
1113 goto e_data;
1114 }
1115
1116 ccp_get_dm_area(&ctx, 0, sha->ctx, 0, sha->ctx_len);
1117
1118 if (sha->final && sha->opad) {
1119 /* HMAC operation, recursively perform final SHA */
1120 struct ccp_cmd hmac_cmd;
1121 struct scatterlist sg;
1122 u64 block_size, digest_size;
1123 u8 *hmac_buf;
1124
1125 switch (sha->type) {
1126 case CCP_SHA_TYPE_1:
1127 block_size = SHA1_BLOCK_SIZE;
1128 digest_size = SHA1_DIGEST_SIZE;
1129 break;
1130 case CCP_SHA_TYPE_224:
1131 block_size = SHA224_BLOCK_SIZE;
1132 digest_size = SHA224_DIGEST_SIZE;
1133 break;
1134 case CCP_SHA_TYPE_256:
1135 block_size = SHA256_BLOCK_SIZE;
1136 digest_size = SHA256_DIGEST_SIZE;
1137 break;
1138 default:
1139 ret = -EINVAL;
1140 goto e_data;
1141 }
1142
1143 if (sha->opad_len != block_size) {
1144 ret = -EINVAL;
1145 goto e_data;
1146 }
1147
1148 hmac_buf = kmalloc(block_size + digest_size, GFP_KERNEL);
1149 if (!hmac_buf) {
1150 ret = -ENOMEM;
1151 goto e_data;
1152 }
1153 sg_init_one(&sg, hmac_buf, block_size + digest_size);
1154
1155 scatterwalk_map_and_copy(hmac_buf, sha->opad, 0, block_size, 0);
1156 memcpy(hmac_buf + block_size, ctx.address, digest_size);
1157
1158 memset(&hmac_cmd, 0, sizeof(hmac_cmd));
1159 hmac_cmd.engine = CCP_ENGINE_SHA;
1160 hmac_cmd.u.sha.type = sha->type;
1161 hmac_cmd.u.sha.ctx = sha->ctx;
1162 hmac_cmd.u.sha.ctx_len = sha->ctx_len;
1163 hmac_cmd.u.sha.src = &sg;
1164 hmac_cmd.u.sha.src_len = block_size + digest_size;
1165 hmac_cmd.u.sha.opad = NULL;
1166 hmac_cmd.u.sha.opad_len = 0;
1167 hmac_cmd.u.sha.first = 1;
1168 hmac_cmd.u.sha.final = 1;
1169 hmac_cmd.u.sha.msg_bits = (block_size + digest_size) << 3;
1170
1171 ret = ccp_run_sha_cmd(cmd_q, &hmac_cmd);
1172 if (ret)
1173 cmd->engine_error = hmac_cmd.engine_error;
1174
1175 kfree(hmac_buf);
1176 }
1177
1178e_data:
1179 ccp_free_data(&src, cmd_q);
1180
1181e_ctx:
1182 ccp_dm_free(&ctx);
1183
1184 return ret;
1185}
1186
1187static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1188{
1189 struct ccp_rsa_engine *rsa = &cmd->u.rsa;
1190 struct ccp_dm_workarea exp, src;
1191 struct ccp_data dst;
1192 struct ccp_op op;
1193 unsigned int ksb_count, i_len, o_len;
1194 int ret;
1195
1196 if (rsa->key_size > CCP_RSA_MAX_WIDTH)
1197 return -EINVAL;
1198
1199 if (!rsa->exp || !rsa->mod || !rsa->src || !rsa->dst)
1200 return -EINVAL;
1201
1202 /* The RSA modulus must precede the message being acted upon, so
1203 * it must be copied to a DMA area where the message and the
1204 * modulus can be concatenated. Therefore the input buffer
1205 * length required is twice the output buffer length (which
1206 * must be a multiple of 256-bits).
1207 */
1208 o_len = ((rsa->key_size + 255) / 256) * 32;
1209 i_len = o_len * 2;
1210
1211 ksb_count = o_len / CCP_KSB_BYTES;
1212
1213 memset(&op, 0, sizeof(op));
1214 op.cmd_q = cmd_q;
1215 op.jobid = ccp_gen_jobid(cmd_q->ccp);
1216 op.ksb_key = ccp_alloc_ksb(cmd_q->ccp, ksb_count);
1217 if (!op.ksb_key)
1218 return -EIO;
1219
1220 /* The RSA exponent may span multiple (32-byte) KSB entries and must
1221 * be in little endian format. Reverse copy each 32-byte chunk
1222 * of the exponent (En chunk to E0 chunk, E(n-1) chunk to E1 chunk)
1223 * and each byte within that chunk and do not perform any byte swap
1224 * operations on the passthru operation.
1225 */
1226 ret = ccp_init_dm_workarea(&exp, cmd_q, o_len, DMA_TO_DEVICE);
1227 if (ret)
1228 goto e_ksb;
1229
1230 ret = ccp_reverse_set_dm_area(&exp, rsa->exp, rsa->exp_len,
1231 CCP_KSB_BYTES, false);
1232 if (ret)
1233 goto e_exp;
1234 ret = ccp_copy_to_ksb(cmd_q, &exp, op.jobid, op.ksb_key,
1235 CCP_PASSTHRU_BYTESWAP_NOOP);
1236 if (ret) {
1237 cmd->engine_error = cmd_q->cmd_error;
1238 goto e_exp;
1239 }
1240
1241 /* Concatenate the modulus and the message. Both the modulus and
1242 * the operands must be in little endian format. Since the input
1243 * is in big endian format it must be converted.
1244 */
1245 ret = ccp_init_dm_workarea(&src, cmd_q, i_len, DMA_TO_DEVICE);
1246 if (ret)
1247 goto e_exp;
1248
1249 ret = ccp_reverse_set_dm_area(&src, rsa->mod, rsa->mod_len,
1250 CCP_KSB_BYTES, false);
1251 if (ret)
1252 goto e_src;
1253 src.address += o_len; /* Adjust the address for the copy operation */
1254 ret = ccp_reverse_set_dm_area(&src, rsa->src, rsa->src_len,
1255 CCP_KSB_BYTES, false);
1256 if (ret)
1257 goto e_src;
1258 src.address -= o_len; /* Reset the address to original value */
1259
1260 /* Prepare the output area for the operation */
1261 ret = ccp_init_data(&dst, cmd_q, rsa->dst, rsa->mod_len,
1262 o_len, DMA_FROM_DEVICE);
1263 if (ret)
1264 goto e_src;
1265
1266 op.soc = 1;
1267 op.src.u.dma.address = src.dma.address;
1268 op.src.u.dma.offset = 0;
1269 op.src.u.dma.length = i_len;
1270 op.dst.u.dma.address = dst.dm_wa.dma.address;
1271 op.dst.u.dma.offset = 0;
1272 op.dst.u.dma.length = o_len;
1273
1274 op.u.rsa.mod_size = rsa->key_size;
1275 op.u.rsa.input_len = i_len;
1276
1277 ret = cmd_q->ccp->vdata->perform->perform_rsa(&op);
1278 if (ret) {
1279 cmd->engine_error = cmd_q->cmd_error;
1280 goto e_dst;
1281 }
1282
1283 ccp_reverse_get_dm_area(&dst.dm_wa, rsa->dst, rsa->mod_len);
1284
1285e_dst:
1286 ccp_free_data(&dst, cmd_q);
1287
1288e_src:
1289 ccp_dm_free(&src);
1290
1291e_exp:
1292 ccp_dm_free(&exp);
1293
1294e_ksb:
1295 ccp_free_ksb(cmd_q->ccp, op.ksb_key, ksb_count);
1296
1297 return ret;
1298}
1299
1300static int ccp_run_passthru_cmd(struct ccp_cmd_queue *cmd_q,
1301 struct ccp_cmd *cmd)
1302{
1303 struct ccp_passthru_engine *pt = &cmd->u.passthru;
1304 struct ccp_dm_workarea mask;
1305 struct ccp_data src, dst;
1306 struct ccp_op op;
1307 bool in_place = false;
1308 unsigned int i;
1309 int ret;
1310
1311 if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
1312 return -EINVAL;
1313
1314 if (!pt->src || !pt->dst)
1315 return -EINVAL;
1316
1317 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
1318 if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
1319 return -EINVAL;
1320 if (!pt->mask)
1321 return -EINVAL;
1322 }
1323
1324 BUILD_BUG_ON(CCP_PASSTHRU_KSB_COUNT != 1);
1325
1326 memset(&op, 0, sizeof(op));
1327 op.cmd_q = cmd_q;
1328 op.jobid = ccp_gen_jobid(cmd_q->ccp);
1329
1330 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
1331 /* Load the mask */
1332 op.ksb_key = cmd_q->ksb_key;
1333
1334 ret = ccp_init_dm_workarea(&mask, cmd_q,
1335 CCP_PASSTHRU_KSB_COUNT *
1336 CCP_KSB_BYTES,
1337 DMA_TO_DEVICE);
1338 if (ret)
1339 return ret;
1340
1341 ccp_set_dm_area(&mask, 0, pt->mask, 0, pt->mask_len);
1342 ret = ccp_copy_to_ksb(cmd_q, &mask, op.jobid, op.ksb_key,
1343 CCP_PASSTHRU_BYTESWAP_NOOP);
1344 if (ret) {
1345 cmd->engine_error = cmd_q->cmd_error;
1346 goto e_mask;
1347 }
1348 }
1349
1350 /* Prepare the input and output data workareas. For in-place
1351 * operations we need to set the dma direction to BIDIRECTIONAL
1352 * and copy the src workarea to the dst workarea.
1353 */
1354 if (sg_virt(pt->src) == sg_virt(pt->dst))
1355 in_place = true;
1356
1357 ret = ccp_init_data(&src, cmd_q, pt->src, pt->src_len,
1358 CCP_PASSTHRU_MASKSIZE,
1359 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1360 if (ret)
1361 goto e_mask;
1362
1363 if (in_place) {
1364 dst = src;
1365 } else {
1366 ret = ccp_init_data(&dst, cmd_q, pt->dst, pt->src_len,
1367 CCP_PASSTHRU_MASKSIZE, DMA_FROM_DEVICE);
1368 if (ret)
1369 goto e_src;
1370 }
1371
1372 /* Send data to the CCP Passthru engine
1373 * Because the CCP engine works on a single source and destination
1374 * dma address at a time, each entry in the source scatterlist
1375 * (after the dma_map_sg call) must be less than or equal to the
1376 * (remaining) length in the destination scatterlist entry and the
1377 * length must be a multiple of CCP_PASSTHRU_BLOCKSIZE
1378 */
1379 dst.sg_wa.sg_used = 0;
1380 for (i = 1; i <= src.sg_wa.dma_count; i++) {
1381 if (!dst.sg_wa.sg ||
1382 (dst.sg_wa.sg->length < src.sg_wa.sg->length)) {
1383 ret = -EINVAL;
1384 goto e_dst;
1385 }
1386
1387 if (i == src.sg_wa.dma_count) {
1388 op.eom = 1;
1389 op.soc = 1;
1390 }
1391
1392 op.src.type = CCP_MEMTYPE_SYSTEM;
1393 op.src.u.dma.address = sg_dma_address(src.sg_wa.sg);
1394 op.src.u.dma.offset = 0;
1395 op.src.u.dma.length = sg_dma_len(src.sg_wa.sg);
1396
1397 op.dst.type = CCP_MEMTYPE_SYSTEM;
1398 op.dst.u.dma.address = sg_dma_address(dst.sg_wa.sg);
1399 op.dst.u.dma.offset = dst.sg_wa.sg_used;
1400 op.dst.u.dma.length = op.src.u.dma.length;
1401
1402 ret = cmd_q->ccp->vdata->perform->perform_passthru(&op);
1403 if (ret) {
1404 cmd->engine_error = cmd_q->cmd_error;
1405 goto e_dst;
1406 }
1407
1408 dst.sg_wa.sg_used += src.sg_wa.sg->length;
1409 if (dst.sg_wa.sg_used == dst.sg_wa.sg->length) {
1410 dst.sg_wa.sg = sg_next(dst.sg_wa.sg);
1411 dst.sg_wa.sg_used = 0;
1412 }
1413 src.sg_wa.sg = sg_next(src.sg_wa.sg);
1414 }
1415
1416e_dst:
1417 if (!in_place)
1418 ccp_free_data(&dst, cmd_q);
1419
1420e_src:
1421 ccp_free_data(&src, cmd_q);
1422
1423e_mask:
1424 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP)
1425 ccp_dm_free(&mask);
1426
1427 return ret;
1428}
1429
1430static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1431{
1432 struct ccp_ecc_engine *ecc = &cmd->u.ecc;
1433 struct ccp_dm_workarea src, dst;
1434 struct ccp_op op;
1435 int ret;
1436 u8 *save;
1437
1438 if (!ecc->u.mm.operand_1 ||
1439 (ecc->u.mm.operand_1_len > CCP_ECC_MODULUS_BYTES))
1440 return -EINVAL;
1441
1442 if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT)
1443 if (!ecc->u.mm.operand_2 ||
1444 (ecc->u.mm.operand_2_len > CCP_ECC_MODULUS_BYTES))
1445 return -EINVAL;
1446
1447 if (!ecc->u.mm.result ||
1448 (ecc->u.mm.result_len < CCP_ECC_MODULUS_BYTES))
1449 return -EINVAL;
1450
1451 memset(&op, 0, sizeof(op));
1452 op.cmd_q = cmd_q;
1453 op.jobid = ccp_gen_jobid(cmd_q->ccp);
1454
1455 /* Concatenate the modulus and the operands. Both the modulus and
1456 * the operands must be in little endian format. Since the input
1457 * is in big endian format it must be converted and placed in a
1458 * fixed length buffer.
1459 */
1460 ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
1461 DMA_TO_DEVICE);
1462 if (ret)
1463 return ret;
1464
1465 /* Save the workarea address since it is updated in order to perform
1466 * the concatenation
1467 */
1468 save = src.address;
1469
1470 /* Copy the ECC modulus */
1471 ret = ccp_reverse_set_dm_area(&src, ecc->mod, ecc->mod_len,
1472 CCP_ECC_OPERAND_SIZE, false);
1473 if (ret)
1474 goto e_src;
1475 src.address += CCP_ECC_OPERAND_SIZE;
1476
1477 /* Copy the first operand */
1478 ret = ccp_reverse_set_dm_area(&src, ecc->u.mm.operand_1,
1479 ecc->u.mm.operand_1_len,
1480 CCP_ECC_OPERAND_SIZE, false);
1481 if (ret)
1482 goto e_src;
1483 src.address += CCP_ECC_OPERAND_SIZE;
1484
1485 if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT) {
1486 /* Copy the second operand */
1487 ret = ccp_reverse_set_dm_area(&src, ecc->u.mm.operand_2,
1488 ecc->u.mm.operand_2_len,
1489 CCP_ECC_OPERAND_SIZE, false);
1490 if (ret)
1491 goto e_src;
1492 src.address += CCP_ECC_OPERAND_SIZE;
1493 }
1494
1495 /* Restore the workarea address */
1496 src.address = save;
1497
1498 /* Prepare the output area for the operation */
1499 ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
1500 DMA_FROM_DEVICE);
1501 if (ret)
1502 goto e_src;
1503
1504 op.soc = 1;
1505 op.src.u.dma.address = src.dma.address;
1506 op.src.u.dma.offset = 0;
1507 op.src.u.dma.length = src.length;
1508 op.dst.u.dma.address = dst.dma.address;
1509 op.dst.u.dma.offset = 0;
1510 op.dst.u.dma.length = dst.length;
1511
1512 op.u.ecc.function = cmd->u.ecc.function;
1513
1514 ret = cmd_q->ccp->vdata->perform->perform_ecc(&op);
1515 if (ret) {
1516 cmd->engine_error = cmd_q->cmd_error;
1517 goto e_dst;
1518 }
1519
1520 ecc->ecc_result = le16_to_cpup(
1521 (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
1522 if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
1523 ret = -EIO;
1524 goto e_dst;
1525 }
1526
1527 /* Save the ECC result */
1528 ccp_reverse_get_dm_area(&dst, ecc->u.mm.result, CCP_ECC_MODULUS_BYTES);
1529
1530e_dst:
1531 ccp_dm_free(&dst);
1532
1533e_src:
1534 ccp_dm_free(&src);
1535
1536 return ret;
1537}
1538
1539static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1540{
1541 struct ccp_ecc_engine *ecc = &cmd->u.ecc;
1542 struct ccp_dm_workarea src, dst;
1543 struct ccp_op op;
1544 int ret;
1545 u8 *save;
1546
1547 if (!ecc->u.pm.point_1.x ||
1548 (ecc->u.pm.point_1.x_len > CCP_ECC_MODULUS_BYTES) ||
1549 !ecc->u.pm.point_1.y ||
1550 (ecc->u.pm.point_1.y_len > CCP_ECC_MODULUS_BYTES))
1551 return -EINVAL;
1552
1553 if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
1554 if (!ecc->u.pm.point_2.x ||
1555 (ecc->u.pm.point_2.x_len > CCP_ECC_MODULUS_BYTES) ||
1556 !ecc->u.pm.point_2.y ||
1557 (ecc->u.pm.point_2.y_len > CCP_ECC_MODULUS_BYTES))
1558 return -EINVAL;
1559 } else {
1560 if (!ecc->u.pm.domain_a ||
1561 (ecc->u.pm.domain_a_len > CCP_ECC_MODULUS_BYTES))
1562 return -EINVAL;
1563
1564 if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT)
1565 if (!ecc->u.pm.scalar ||
1566 (ecc->u.pm.scalar_len > CCP_ECC_MODULUS_BYTES))
1567 return -EINVAL;
1568 }
1569
1570 if (!ecc->u.pm.result.x ||
1571 (ecc->u.pm.result.x_len < CCP_ECC_MODULUS_BYTES) ||
1572 !ecc->u.pm.result.y ||
1573 (ecc->u.pm.result.y_len < CCP_ECC_MODULUS_BYTES))
1574 return -EINVAL;
1575
1576 memset(&op, 0, sizeof(op));
1577 op.cmd_q = cmd_q;
1578 op.jobid = ccp_gen_jobid(cmd_q->ccp);
1579
1580 /* Concatenate the modulus and the operands. Both the modulus and
1581 * the operands must be in little endian format. Since the input
1582 * is in big endian format it must be converted and placed in a
1583 * fixed length buffer.
1584 */
1585 ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
1586 DMA_TO_DEVICE);
1587 if (ret)
1588 return ret;
1589
1590 /* Save the workarea address since it is updated in order to perform
1591 * the concatenation
1592 */
1593 save = src.address;
1594
1595 /* Copy the ECC modulus */
1596 ret = ccp_reverse_set_dm_area(&src, ecc->mod, ecc->mod_len,
1597 CCP_ECC_OPERAND_SIZE, false);
1598 if (ret)
1599 goto e_src;
1600 src.address += CCP_ECC_OPERAND_SIZE;
1601
1602 /* Copy the first point X and Y coordinate */
1603 ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_1.x,
1604 ecc->u.pm.point_1.x_len,
1605 CCP_ECC_OPERAND_SIZE, false);
1606 if (ret)
1607 goto e_src;
1608 src.address += CCP_ECC_OPERAND_SIZE;
1609 ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_1.y,
1610 ecc->u.pm.point_1.y_len,
1611 CCP_ECC_OPERAND_SIZE, false);
1612 if (ret)
1613 goto e_src;
1614 src.address += CCP_ECC_OPERAND_SIZE;
1615
1616 /* Set the first point Z coordianate to 1 */
1617 *src.address = 0x01;
1618 src.address += CCP_ECC_OPERAND_SIZE;
1619
1620 if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
1621 /* Copy the second point X and Y coordinate */
1622 ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_2.x,
1623 ecc->u.pm.point_2.x_len,
1624 CCP_ECC_OPERAND_SIZE, false);
1625 if (ret)
1626 goto e_src;
1627 src.address += CCP_ECC_OPERAND_SIZE;
1628 ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_2.y,
1629 ecc->u.pm.point_2.y_len,
1630 CCP_ECC_OPERAND_SIZE, false);
1631 if (ret)
1632 goto e_src;
1633 src.address += CCP_ECC_OPERAND_SIZE;
1634
1635 /* Set the second point Z coordianate to 1 */
1636 *src.address = 0x01;
1637 src.address += CCP_ECC_OPERAND_SIZE;
1638 } else {
1639 /* Copy the Domain "a" parameter */
1640 ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.domain_a,
1641 ecc->u.pm.domain_a_len,
1642 CCP_ECC_OPERAND_SIZE, false);
1643 if (ret)
1644 goto e_src;
1645 src.address += CCP_ECC_OPERAND_SIZE;
1646
1647 if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT) {
1648 /* Copy the scalar value */
1649 ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.scalar,
1650 ecc->u.pm.scalar_len,
1651 CCP_ECC_OPERAND_SIZE,
1652 false);
1653 if (ret)
1654 goto e_src;
1655 src.address += CCP_ECC_OPERAND_SIZE;
1656 }
1657 }
1658
1659 /* Restore the workarea address */
1660 src.address = save;
1661
1662 /* Prepare the output area for the operation */
1663 ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
1664 DMA_FROM_DEVICE);
1665 if (ret)
1666 goto e_src;
1667
1668 op.soc = 1;
1669 op.src.u.dma.address = src.dma.address;
1670 op.src.u.dma.offset = 0;
1671 op.src.u.dma.length = src.length;
1672 op.dst.u.dma.address = dst.dma.address;
1673 op.dst.u.dma.offset = 0;
1674 op.dst.u.dma.length = dst.length;
1675
1676 op.u.ecc.function = cmd->u.ecc.function;
1677
1678 ret = cmd_q->ccp->vdata->perform->perform_ecc(&op);
1679 if (ret) {
1680 cmd->engine_error = cmd_q->cmd_error;
1681 goto e_dst;
1682 }
1683
1684 ecc->ecc_result = le16_to_cpup(
1685 (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
1686 if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
1687 ret = -EIO;
1688 goto e_dst;
1689 }
1690
1691 /* Save the workarea address since it is updated as we walk through
1692 * to copy the point math result
1693 */
1694 save = dst.address;
1695
1696 /* Save the ECC result X and Y coordinates */
1697 ccp_reverse_get_dm_area(&dst, ecc->u.pm.result.x,
1698 CCP_ECC_MODULUS_BYTES);
1699 dst.address += CCP_ECC_OUTPUT_SIZE;
1700 ccp_reverse_get_dm_area(&dst, ecc->u.pm.result.y,
1701 CCP_ECC_MODULUS_BYTES);
1702 dst.address += CCP_ECC_OUTPUT_SIZE;
1703
1704 /* Restore the workarea address */
1705 dst.address = save;
1706
1707e_dst:
1708 ccp_dm_free(&dst);
1709
1710e_src:
1711 ccp_dm_free(&src);
1712
1713 return ret;
1714}
1715
1716static int ccp_run_ecc_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1717{
1718 struct ccp_ecc_engine *ecc = &cmd->u.ecc;
1719
1720 ecc->ecc_result = 0;
1721
1722 if (!ecc->mod ||
1723 (ecc->mod_len > CCP_ECC_MODULUS_BYTES))
1724 return -EINVAL;
1725
1726 switch (ecc->function) {
1727 case CCP_ECC_FUNCTION_MMUL_384BIT:
1728 case CCP_ECC_FUNCTION_MADD_384BIT:
1729 case CCP_ECC_FUNCTION_MINV_384BIT:
1730 return ccp_run_ecc_mm_cmd(cmd_q, cmd);
1731
1732 case CCP_ECC_FUNCTION_PADD_384BIT:
1733 case CCP_ECC_FUNCTION_PMUL_384BIT:
1734 case CCP_ECC_FUNCTION_PDBL_384BIT:
1735 return ccp_run_ecc_pm_cmd(cmd_q, cmd);
1736
1737 default:
1738 return -EINVAL;
1739 }
1740}
1741
1742int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1743{
1744 int ret;
1745
1746 cmd->engine_error = 0;
1747 cmd_q->cmd_error = 0;
1748 cmd_q->int_rcvd = 0;
1749 cmd_q->free_slots = CMD_Q_DEPTH(ioread32(cmd_q->reg_status));
1750
1751 switch (cmd->engine) {
1752 case CCP_ENGINE_AES:
1753 ret = ccp_run_aes_cmd(cmd_q, cmd);
1754 break;
1755 case CCP_ENGINE_XTS_AES_128:
1756 ret = ccp_run_xts_aes_cmd(cmd_q, cmd);
1757 break;
1758 case CCP_ENGINE_SHA:
1759 ret = ccp_run_sha_cmd(cmd_q, cmd);
1760 break;
1761 case CCP_ENGINE_RSA:
1762 ret = ccp_run_rsa_cmd(cmd_q, cmd);
1763 break;
1764 case CCP_ENGINE_PASSTHRU:
1765 ret = ccp_run_passthru_cmd(cmd_q, cmd);
1766 break;
1767 case CCP_ENGINE_ECC:
1768 ret = ccp_run_ecc_cmd(cmd_q, cmd);
1769 break;
1770 default:
1771 ret = -EINVAL;
1772 }
1773
1774 return ret;
1775}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * AMD Cryptographic Coprocessor (CCP) driver
4 *
5 * Copyright (C) 2013-2019 Advanced Micro Devices, Inc.
6 *
7 * Author: Tom Lendacky <thomas.lendacky@amd.com>
8 * Author: Gary R Hook <gary.hook@amd.com>
9 */
10
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/interrupt.h>
14#include <crypto/scatterwalk.h>
15#include <crypto/des.h>
16#include <linux/ccp.h>
17
18#include "ccp-dev.h"
19
20/* SHA initial context values */
21static const __be32 ccp_sha1_init[SHA1_DIGEST_SIZE / sizeof(__be32)] = {
22 cpu_to_be32(SHA1_H0), cpu_to_be32(SHA1_H1),
23 cpu_to_be32(SHA1_H2), cpu_to_be32(SHA1_H3),
24 cpu_to_be32(SHA1_H4),
25};
26
27static const __be32 ccp_sha224_init[SHA256_DIGEST_SIZE / sizeof(__be32)] = {
28 cpu_to_be32(SHA224_H0), cpu_to_be32(SHA224_H1),
29 cpu_to_be32(SHA224_H2), cpu_to_be32(SHA224_H3),
30 cpu_to_be32(SHA224_H4), cpu_to_be32(SHA224_H5),
31 cpu_to_be32(SHA224_H6), cpu_to_be32(SHA224_H7),
32};
33
34static const __be32 ccp_sha256_init[SHA256_DIGEST_SIZE / sizeof(__be32)] = {
35 cpu_to_be32(SHA256_H0), cpu_to_be32(SHA256_H1),
36 cpu_to_be32(SHA256_H2), cpu_to_be32(SHA256_H3),
37 cpu_to_be32(SHA256_H4), cpu_to_be32(SHA256_H5),
38 cpu_to_be32(SHA256_H6), cpu_to_be32(SHA256_H7),
39};
40
41static const __be64 ccp_sha384_init[SHA512_DIGEST_SIZE / sizeof(__be64)] = {
42 cpu_to_be64(SHA384_H0), cpu_to_be64(SHA384_H1),
43 cpu_to_be64(SHA384_H2), cpu_to_be64(SHA384_H3),
44 cpu_to_be64(SHA384_H4), cpu_to_be64(SHA384_H5),
45 cpu_to_be64(SHA384_H6), cpu_to_be64(SHA384_H7),
46};
47
48static const __be64 ccp_sha512_init[SHA512_DIGEST_SIZE / sizeof(__be64)] = {
49 cpu_to_be64(SHA512_H0), cpu_to_be64(SHA512_H1),
50 cpu_to_be64(SHA512_H2), cpu_to_be64(SHA512_H3),
51 cpu_to_be64(SHA512_H4), cpu_to_be64(SHA512_H5),
52 cpu_to_be64(SHA512_H6), cpu_to_be64(SHA512_H7),
53};
54
55#define CCP_NEW_JOBID(ccp) ((ccp->vdata->version == CCP_VERSION(3, 0)) ? \
56 ccp_gen_jobid(ccp) : 0)
57
58static u32 ccp_gen_jobid(struct ccp_device *ccp)
59{
60 return atomic_inc_return(&ccp->current_id) & CCP_JOBID_MASK;
61}
62
63static void ccp_sg_free(struct ccp_sg_workarea *wa)
64{
65 if (wa->dma_count)
66 dma_unmap_sg(wa->dma_dev, wa->dma_sg, wa->nents, wa->dma_dir);
67
68 wa->dma_count = 0;
69}
70
71static int ccp_init_sg_workarea(struct ccp_sg_workarea *wa, struct device *dev,
72 struct scatterlist *sg, u64 len,
73 enum dma_data_direction dma_dir)
74{
75 memset(wa, 0, sizeof(*wa));
76
77 wa->sg = sg;
78 if (!sg)
79 return 0;
80
81 wa->nents = sg_nents_for_len(sg, len);
82 if (wa->nents < 0)
83 return wa->nents;
84
85 wa->bytes_left = len;
86 wa->sg_used = 0;
87
88 if (len == 0)
89 return 0;
90
91 if (dma_dir == DMA_NONE)
92 return 0;
93
94 wa->dma_sg = sg;
95 wa->dma_dev = dev;
96 wa->dma_dir = dma_dir;
97 wa->dma_count = dma_map_sg(dev, sg, wa->nents, dma_dir);
98 if (!wa->dma_count)
99 return -ENOMEM;
100
101 return 0;
102}
103
104static void ccp_update_sg_workarea(struct ccp_sg_workarea *wa, unsigned int len)
105{
106 unsigned int nbytes = min_t(u64, len, wa->bytes_left);
107
108 if (!wa->sg)
109 return;
110
111 wa->sg_used += nbytes;
112 wa->bytes_left -= nbytes;
113 if (wa->sg_used == wa->sg->length) {
114 wa->sg = sg_next(wa->sg);
115 wa->sg_used = 0;
116 }
117}
118
119static void ccp_dm_free(struct ccp_dm_workarea *wa)
120{
121 if (wa->length <= CCP_DMAPOOL_MAX_SIZE) {
122 if (wa->address)
123 dma_pool_free(wa->dma_pool, wa->address,
124 wa->dma.address);
125 } else {
126 if (wa->dma.address)
127 dma_unmap_single(wa->dev, wa->dma.address, wa->length,
128 wa->dma.dir);
129 kfree(wa->address);
130 }
131
132 wa->address = NULL;
133 wa->dma.address = 0;
134}
135
136static int ccp_init_dm_workarea(struct ccp_dm_workarea *wa,
137 struct ccp_cmd_queue *cmd_q,
138 unsigned int len,
139 enum dma_data_direction dir)
140{
141 memset(wa, 0, sizeof(*wa));
142
143 if (!len)
144 return 0;
145
146 wa->dev = cmd_q->ccp->dev;
147 wa->length = len;
148
149 if (len <= CCP_DMAPOOL_MAX_SIZE) {
150 wa->dma_pool = cmd_q->dma_pool;
151
152 wa->address = dma_pool_zalloc(wa->dma_pool, GFP_KERNEL,
153 &wa->dma.address);
154 if (!wa->address)
155 return -ENOMEM;
156
157 wa->dma.length = CCP_DMAPOOL_MAX_SIZE;
158
159 } else {
160 wa->address = kzalloc(len, GFP_KERNEL);
161 if (!wa->address)
162 return -ENOMEM;
163
164 wa->dma.address = dma_map_single(wa->dev, wa->address, len,
165 dir);
166 if (dma_mapping_error(wa->dev, wa->dma.address))
167 return -ENOMEM;
168
169 wa->dma.length = len;
170 }
171 wa->dma.dir = dir;
172
173 return 0;
174}
175
176static int ccp_set_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
177 struct scatterlist *sg, unsigned int sg_offset,
178 unsigned int len)
179{
180 WARN_ON(!wa->address);
181
182 if (len > (wa->length - wa_offset))
183 return -EINVAL;
184
185 scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
186 0);
187 return 0;
188}
189
190static void ccp_get_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
191 struct scatterlist *sg, unsigned int sg_offset,
192 unsigned int len)
193{
194 WARN_ON(!wa->address);
195
196 scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
197 1);
198}
199
200static int ccp_reverse_set_dm_area(struct ccp_dm_workarea *wa,
201 unsigned int wa_offset,
202 struct scatterlist *sg,
203 unsigned int sg_offset,
204 unsigned int len)
205{
206 u8 *p, *q;
207 int rc;
208
209 rc = ccp_set_dm_area(wa, wa_offset, sg, sg_offset, len);
210 if (rc)
211 return rc;
212
213 p = wa->address + wa_offset;
214 q = p + len - 1;
215 while (p < q) {
216 *p = *p ^ *q;
217 *q = *p ^ *q;
218 *p = *p ^ *q;
219 p++;
220 q--;
221 }
222 return 0;
223}
224
225static void ccp_reverse_get_dm_area(struct ccp_dm_workarea *wa,
226 unsigned int wa_offset,
227 struct scatterlist *sg,
228 unsigned int sg_offset,
229 unsigned int len)
230{
231 u8 *p, *q;
232
233 p = wa->address + wa_offset;
234 q = p + len - 1;
235 while (p < q) {
236 *p = *p ^ *q;
237 *q = *p ^ *q;
238 *p = *p ^ *q;
239 p++;
240 q--;
241 }
242
243 ccp_get_dm_area(wa, wa_offset, sg, sg_offset, len);
244}
245
246static void ccp_free_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q)
247{
248 ccp_dm_free(&data->dm_wa);
249 ccp_sg_free(&data->sg_wa);
250}
251
252static int ccp_init_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q,
253 struct scatterlist *sg, u64 sg_len,
254 unsigned int dm_len,
255 enum dma_data_direction dir)
256{
257 int ret;
258
259 memset(data, 0, sizeof(*data));
260
261 ret = ccp_init_sg_workarea(&data->sg_wa, cmd_q->ccp->dev, sg, sg_len,
262 dir);
263 if (ret)
264 goto e_err;
265
266 ret = ccp_init_dm_workarea(&data->dm_wa, cmd_q, dm_len, dir);
267 if (ret)
268 goto e_err;
269
270 return 0;
271
272e_err:
273 ccp_free_data(data, cmd_q);
274
275 return ret;
276}
277
278static unsigned int ccp_queue_buf(struct ccp_data *data, unsigned int from)
279{
280 struct ccp_sg_workarea *sg_wa = &data->sg_wa;
281 struct ccp_dm_workarea *dm_wa = &data->dm_wa;
282 unsigned int buf_count, nbytes;
283
284 /* Clear the buffer if setting it */
285 if (!from)
286 memset(dm_wa->address, 0, dm_wa->length);
287
288 if (!sg_wa->sg)
289 return 0;
290
291 /* Perform the copy operation
292 * nbytes will always be <= UINT_MAX because dm_wa->length is
293 * an unsigned int
294 */
295 nbytes = min_t(u64, sg_wa->bytes_left, dm_wa->length);
296 scatterwalk_map_and_copy(dm_wa->address, sg_wa->sg, sg_wa->sg_used,
297 nbytes, from);
298
299 /* Update the structures and generate the count */
300 buf_count = 0;
301 while (sg_wa->bytes_left && (buf_count < dm_wa->length)) {
302 nbytes = min(sg_wa->sg->length - sg_wa->sg_used,
303 dm_wa->length - buf_count);
304 nbytes = min_t(u64, sg_wa->bytes_left, nbytes);
305
306 buf_count += nbytes;
307 ccp_update_sg_workarea(sg_wa, nbytes);
308 }
309
310 return buf_count;
311}
312
313static unsigned int ccp_fill_queue_buf(struct ccp_data *data)
314{
315 return ccp_queue_buf(data, 0);
316}
317
318static unsigned int ccp_empty_queue_buf(struct ccp_data *data)
319{
320 return ccp_queue_buf(data, 1);
321}
322
323static void ccp_prepare_data(struct ccp_data *src, struct ccp_data *dst,
324 struct ccp_op *op, unsigned int block_size,
325 bool blocksize_op)
326{
327 unsigned int sg_src_len, sg_dst_len, op_len;
328
329 /* The CCP can only DMA from/to one address each per operation. This
330 * requires that we find the smallest DMA area between the source
331 * and destination. The resulting len values will always be <= UINT_MAX
332 * because the dma length is an unsigned int.
333 */
334 sg_src_len = sg_dma_len(src->sg_wa.sg) - src->sg_wa.sg_used;
335 sg_src_len = min_t(u64, src->sg_wa.bytes_left, sg_src_len);
336
337 if (dst) {
338 sg_dst_len = sg_dma_len(dst->sg_wa.sg) - dst->sg_wa.sg_used;
339 sg_dst_len = min_t(u64, src->sg_wa.bytes_left, sg_dst_len);
340 op_len = min(sg_src_len, sg_dst_len);
341 } else {
342 op_len = sg_src_len;
343 }
344
345 /* The data operation length will be at least block_size in length
346 * or the smaller of available sg room remaining for the source or
347 * the destination
348 */
349 op_len = max(op_len, block_size);
350
351 /* Unless we have to buffer data, there's no reason to wait */
352 op->soc = 0;
353
354 if (sg_src_len < block_size) {
355 /* Not enough data in the sg element, so it
356 * needs to be buffered into a blocksize chunk
357 */
358 int cp_len = ccp_fill_queue_buf(src);
359
360 op->soc = 1;
361 op->src.u.dma.address = src->dm_wa.dma.address;
362 op->src.u.dma.offset = 0;
363 op->src.u.dma.length = (blocksize_op) ? block_size : cp_len;
364 } else {
365 /* Enough data in the sg element, but we need to
366 * adjust for any previously copied data
367 */
368 op->src.u.dma.address = sg_dma_address(src->sg_wa.sg);
369 op->src.u.dma.offset = src->sg_wa.sg_used;
370 op->src.u.dma.length = op_len & ~(block_size - 1);
371
372 ccp_update_sg_workarea(&src->sg_wa, op->src.u.dma.length);
373 }
374
375 if (dst) {
376 if (sg_dst_len < block_size) {
377 /* Not enough room in the sg element or we're on the
378 * last piece of data (when using padding), so the
379 * output needs to be buffered into a blocksize chunk
380 */
381 op->soc = 1;
382 op->dst.u.dma.address = dst->dm_wa.dma.address;
383 op->dst.u.dma.offset = 0;
384 op->dst.u.dma.length = op->src.u.dma.length;
385 } else {
386 /* Enough room in the sg element, but we need to
387 * adjust for any previously used area
388 */
389 op->dst.u.dma.address = sg_dma_address(dst->sg_wa.sg);
390 op->dst.u.dma.offset = dst->sg_wa.sg_used;
391 op->dst.u.dma.length = op->src.u.dma.length;
392 }
393 }
394}
395
396static void ccp_process_data(struct ccp_data *src, struct ccp_data *dst,
397 struct ccp_op *op)
398{
399 op->init = 0;
400
401 if (dst) {
402 if (op->dst.u.dma.address == dst->dm_wa.dma.address)
403 ccp_empty_queue_buf(dst);
404 else
405 ccp_update_sg_workarea(&dst->sg_wa,
406 op->dst.u.dma.length);
407 }
408}
409
410static int ccp_copy_to_from_sb(struct ccp_cmd_queue *cmd_q,
411 struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
412 u32 byte_swap, bool from)
413{
414 struct ccp_op op;
415
416 memset(&op, 0, sizeof(op));
417
418 op.cmd_q = cmd_q;
419 op.jobid = jobid;
420 op.eom = 1;
421
422 if (from) {
423 op.soc = 1;
424 op.src.type = CCP_MEMTYPE_SB;
425 op.src.u.sb = sb;
426 op.dst.type = CCP_MEMTYPE_SYSTEM;
427 op.dst.u.dma.address = wa->dma.address;
428 op.dst.u.dma.length = wa->length;
429 } else {
430 op.src.type = CCP_MEMTYPE_SYSTEM;
431 op.src.u.dma.address = wa->dma.address;
432 op.src.u.dma.length = wa->length;
433 op.dst.type = CCP_MEMTYPE_SB;
434 op.dst.u.sb = sb;
435 }
436
437 op.u.passthru.byte_swap = byte_swap;
438
439 return cmd_q->ccp->vdata->perform->passthru(&op);
440}
441
442static int ccp_copy_to_sb(struct ccp_cmd_queue *cmd_q,
443 struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
444 u32 byte_swap)
445{
446 return ccp_copy_to_from_sb(cmd_q, wa, jobid, sb, byte_swap, false);
447}
448
449static int ccp_copy_from_sb(struct ccp_cmd_queue *cmd_q,
450 struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
451 u32 byte_swap)
452{
453 return ccp_copy_to_from_sb(cmd_q, wa, jobid, sb, byte_swap, true);
454}
455
456static noinline_for_stack int
457ccp_run_aes_cmac_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
458{
459 struct ccp_aes_engine *aes = &cmd->u.aes;
460 struct ccp_dm_workarea key, ctx;
461 struct ccp_data src;
462 struct ccp_op op;
463 unsigned int dm_offset;
464 int ret;
465
466 if (!((aes->key_len == AES_KEYSIZE_128) ||
467 (aes->key_len == AES_KEYSIZE_192) ||
468 (aes->key_len == AES_KEYSIZE_256)))
469 return -EINVAL;
470
471 if (aes->src_len & (AES_BLOCK_SIZE - 1))
472 return -EINVAL;
473
474 if (aes->iv_len != AES_BLOCK_SIZE)
475 return -EINVAL;
476
477 if (!aes->key || !aes->iv || !aes->src)
478 return -EINVAL;
479
480 if (aes->cmac_final) {
481 if (aes->cmac_key_len != AES_BLOCK_SIZE)
482 return -EINVAL;
483
484 if (!aes->cmac_key)
485 return -EINVAL;
486 }
487
488 BUILD_BUG_ON(CCP_AES_KEY_SB_COUNT != 1);
489 BUILD_BUG_ON(CCP_AES_CTX_SB_COUNT != 1);
490
491 ret = -EIO;
492 memset(&op, 0, sizeof(op));
493 op.cmd_q = cmd_q;
494 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
495 op.sb_key = cmd_q->sb_key;
496 op.sb_ctx = cmd_q->sb_ctx;
497 op.init = 1;
498 op.u.aes.type = aes->type;
499 op.u.aes.mode = aes->mode;
500 op.u.aes.action = aes->action;
501
502 /* All supported key sizes fit in a single (32-byte) SB entry
503 * and must be in little endian format. Use the 256-bit byte
504 * swap passthru option to convert from big endian to little
505 * endian.
506 */
507 ret = ccp_init_dm_workarea(&key, cmd_q,
508 CCP_AES_KEY_SB_COUNT * CCP_SB_BYTES,
509 DMA_TO_DEVICE);
510 if (ret)
511 return ret;
512
513 dm_offset = CCP_SB_BYTES - aes->key_len;
514 ret = ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
515 if (ret)
516 goto e_key;
517 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
518 CCP_PASSTHRU_BYTESWAP_256BIT);
519 if (ret) {
520 cmd->engine_error = cmd_q->cmd_error;
521 goto e_key;
522 }
523
524 /* The AES context fits in a single (32-byte) SB entry and
525 * must be in little endian format. Use the 256-bit byte swap
526 * passthru option to convert from big endian to little endian.
527 */
528 ret = ccp_init_dm_workarea(&ctx, cmd_q,
529 CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
530 DMA_BIDIRECTIONAL);
531 if (ret)
532 goto e_key;
533
534 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
535 ret = ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
536 if (ret)
537 goto e_ctx;
538 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
539 CCP_PASSTHRU_BYTESWAP_256BIT);
540 if (ret) {
541 cmd->engine_error = cmd_q->cmd_error;
542 goto e_ctx;
543 }
544
545 /* Send data to the CCP AES engine */
546 ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
547 AES_BLOCK_SIZE, DMA_TO_DEVICE);
548 if (ret)
549 goto e_ctx;
550
551 while (src.sg_wa.bytes_left) {
552 ccp_prepare_data(&src, NULL, &op, AES_BLOCK_SIZE, true);
553 if (aes->cmac_final && !src.sg_wa.bytes_left) {
554 op.eom = 1;
555
556 /* Push the K1/K2 key to the CCP now */
557 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid,
558 op.sb_ctx,
559 CCP_PASSTHRU_BYTESWAP_256BIT);
560 if (ret) {
561 cmd->engine_error = cmd_q->cmd_error;
562 goto e_src;
563 }
564
565 ret = ccp_set_dm_area(&ctx, 0, aes->cmac_key, 0,
566 aes->cmac_key_len);
567 if (ret)
568 goto e_src;
569 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
570 CCP_PASSTHRU_BYTESWAP_256BIT);
571 if (ret) {
572 cmd->engine_error = cmd_q->cmd_error;
573 goto e_src;
574 }
575 }
576
577 ret = cmd_q->ccp->vdata->perform->aes(&op);
578 if (ret) {
579 cmd->engine_error = cmd_q->cmd_error;
580 goto e_src;
581 }
582
583 ccp_process_data(&src, NULL, &op);
584 }
585
586 /* Retrieve the AES context - convert from LE to BE using
587 * 32-byte (256-bit) byteswapping
588 */
589 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
590 CCP_PASSTHRU_BYTESWAP_256BIT);
591 if (ret) {
592 cmd->engine_error = cmd_q->cmd_error;
593 goto e_src;
594 }
595
596 /* ...but we only need AES_BLOCK_SIZE bytes */
597 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
598 ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
599
600e_src:
601 ccp_free_data(&src, cmd_q);
602
603e_ctx:
604 ccp_dm_free(&ctx);
605
606e_key:
607 ccp_dm_free(&key);
608
609 return ret;
610}
611
612static noinline_for_stack int
613ccp_run_aes_gcm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
614{
615 struct ccp_aes_engine *aes = &cmd->u.aes;
616 struct ccp_dm_workarea key, ctx, final_wa, tag;
617 struct ccp_data src, dst;
618 struct ccp_data aad;
619 struct ccp_op op;
620
621 unsigned long long *final;
622 unsigned int dm_offset;
623 unsigned int authsize;
624 unsigned int jobid;
625 unsigned int ilen;
626 bool in_place = true; /* Default value */
627 int ret;
628
629 struct scatterlist *p_inp, sg_inp[2];
630 struct scatterlist *p_tag, sg_tag[2];
631 struct scatterlist *p_outp, sg_outp[2];
632 struct scatterlist *p_aad;
633
634 if (!aes->iv)
635 return -EINVAL;
636
637 if (!((aes->key_len == AES_KEYSIZE_128) ||
638 (aes->key_len == AES_KEYSIZE_192) ||
639 (aes->key_len == AES_KEYSIZE_256)))
640 return -EINVAL;
641
642 if (!aes->key) /* Gotta have a key SGL */
643 return -EINVAL;
644
645 /* Zero defaults to 16 bytes, the maximum size */
646 authsize = aes->authsize ? aes->authsize : AES_BLOCK_SIZE;
647 switch (authsize) {
648 case 16:
649 case 15:
650 case 14:
651 case 13:
652 case 12:
653 case 8:
654 case 4:
655 break;
656 default:
657 return -EINVAL;
658 }
659
660 /* First, decompose the source buffer into AAD & PT,
661 * and the destination buffer into AAD, CT & tag, or
662 * the input into CT & tag.
663 * It is expected that the input and output SGs will
664 * be valid, even if the AAD and input lengths are 0.
665 */
666 p_aad = aes->src;
667 p_inp = scatterwalk_ffwd(sg_inp, aes->src, aes->aad_len);
668 p_outp = scatterwalk_ffwd(sg_outp, aes->dst, aes->aad_len);
669 if (aes->action == CCP_AES_ACTION_ENCRYPT) {
670 ilen = aes->src_len;
671 p_tag = scatterwalk_ffwd(sg_tag, p_outp, ilen);
672 } else {
673 /* Input length for decryption includes tag */
674 ilen = aes->src_len - authsize;
675 p_tag = scatterwalk_ffwd(sg_tag, p_inp, ilen);
676 }
677
678 jobid = CCP_NEW_JOBID(cmd_q->ccp);
679
680 memset(&op, 0, sizeof(op));
681 op.cmd_q = cmd_q;
682 op.jobid = jobid;
683 op.sb_key = cmd_q->sb_key; /* Pre-allocated */
684 op.sb_ctx = cmd_q->sb_ctx; /* Pre-allocated */
685 op.init = 1;
686 op.u.aes.type = aes->type;
687
688 /* Copy the key to the LSB */
689 ret = ccp_init_dm_workarea(&key, cmd_q,
690 CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
691 DMA_TO_DEVICE);
692 if (ret)
693 return ret;
694
695 dm_offset = CCP_SB_BYTES - aes->key_len;
696 ret = ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
697 if (ret)
698 goto e_key;
699 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
700 CCP_PASSTHRU_BYTESWAP_256BIT);
701 if (ret) {
702 cmd->engine_error = cmd_q->cmd_error;
703 goto e_key;
704 }
705
706 /* Copy the context (IV) to the LSB.
707 * There is an assumption here that the IV is 96 bits in length, plus
708 * a nonce of 32 bits. If no IV is present, use a zeroed buffer.
709 */
710 ret = ccp_init_dm_workarea(&ctx, cmd_q,
711 CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
712 DMA_BIDIRECTIONAL);
713 if (ret)
714 goto e_key;
715
716 dm_offset = CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES - aes->iv_len;
717 ret = ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
718 if (ret)
719 goto e_ctx;
720
721 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
722 CCP_PASSTHRU_BYTESWAP_256BIT);
723 if (ret) {
724 cmd->engine_error = cmd_q->cmd_error;
725 goto e_ctx;
726 }
727
728 op.init = 1;
729 if (aes->aad_len > 0) {
730 /* Step 1: Run a GHASH over the Additional Authenticated Data */
731 ret = ccp_init_data(&aad, cmd_q, p_aad, aes->aad_len,
732 AES_BLOCK_SIZE,
733 DMA_TO_DEVICE);
734 if (ret)
735 goto e_ctx;
736
737 op.u.aes.mode = CCP_AES_MODE_GHASH;
738 op.u.aes.action = CCP_AES_GHASHAAD;
739
740 while (aad.sg_wa.bytes_left) {
741 ccp_prepare_data(&aad, NULL, &op, AES_BLOCK_SIZE, true);
742
743 ret = cmd_q->ccp->vdata->perform->aes(&op);
744 if (ret) {
745 cmd->engine_error = cmd_q->cmd_error;
746 goto e_aad;
747 }
748
749 ccp_process_data(&aad, NULL, &op);
750 op.init = 0;
751 }
752 }
753
754 op.u.aes.mode = CCP_AES_MODE_GCTR;
755 op.u.aes.action = aes->action;
756
757 if (ilen > 0) {
758 /* Step 2: Run a GCTR over the plaintext */
759 in_place = (sg_virt(p_inp) == sg_virt(p_outp)) ? true : false;
760
761 ret = ccp_init_data(&src, cmd_q, p_inp, ilen,
762 AES_BLOCK_SIZE,
763 in_place ? DMA_BIDIRECTIONAL
764 : DMA_TO_DEVICE);
765 if (ret)
766 goto e_ctx;
767
768 if (in_place) {
769 dst = src;
770 } else {
771 ret = ccp_init_data(&dst, cmd_q, p_outp, ilen,
772 AES_BLOCK_SIZE, DMA_FROM_DEVICE);
773 if (ret)
774 goto e_src;
775 }
776
777 op.soc = 0;
778 op.eom = 0;
779 op.init = 1;
780 while (src.sg_wa.bytes_left) {
781 ccp_prepare_data(&src, &dst, &op, AES_BLOCK_SIZE, true);
782 if (!src.sg_wa.bytes_left) {
783 unsigned int nbytes = ilen % AES_BLOCK_SIZE;
784
785 if (nbytes) {
786 op.eom = 1;
787 op.u.aes.size = (nbytes * 8) - 1;
788 }
789 }
790
791 ret = cmd_q->ccp->vdata->perform->aes(&op);
792 if (ret) {
793 cmd->engine_error = cmd_q->cmd_error;
794 goto e_dst;
795 }
796
797 ccp_process_data(&src, &dst, &op);
798 op.init = 0;
799 }
800 }
801
802 /* Step 3: Update the IV portion of the context with the original IV */
803 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
804 CCP_PASSTHRU_BYTESWAP_256BIT);
805 if (ret) {
806 cmd->engine_error = cmd_q->cmd_error;
807 goto e_dst;
808 }
809
810 ret = ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
811 if (ret)
812 goto e_dst;
813
814 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
815 CCP_PASSTHRU_BYTESWAP_256BIT);
816 if (ret) {
817 cmd->engine_error = cmd_q->cmd_error;
818 goto e_dst;
819 }
820
821 /* Step 4: Concatenate the lengths of the AAD and source, and
822 * hash that 16 byte buffer.
823 */
824 ret = ccp_init_dm_workarea(&final_wa, cmd_q, AES_BLOCK_SIZE,
825 DMA_BIDIRECTIONAL);
826 if (ret)
827 goto e_dst;
828 final = (unsigned long long *) final_wa.address;
829 final[0] = cpu_to_be64(aes->aad_len * 8);
830 final[1] = cpu_to_be64(ilen * 8);
831
832 memset(&op, 0, sizeof(op));
833 op.cmd_q = cmd_q;
834 op.jobid = jobid;
835 op.sb_key = cmd_q->sb_key; /* Pre-allocated */
836 op.sb_ctx = cmd_q->sb_ctx; /* Pre-allocated */
837 op.init = 1;
838 op.u.aes.type = aes->type;
839 op.u.aes.mode = CCP_AES_MODE_GHASH;
840 op.u.aes.action = CCP_AES_GHASHFINAL;
841 op.src.type = CCP_MEMTYPE_SYSTEM;
842 op.src.u.dma.address = final_wa.dma.address;
843 op.src.u.dma.length = AES_BLOCK_SIZE;
844 op.dst.type = CCP_MEMTYPE_SYSTEM;
845 op.dst.u.dma.address = final_wa.dma.address;
846 op.dst.u.dma.length = AES_BLOCK_SIZE;
847 op.eom = 1;
848 op.u.aes.size = 0;
849 ret = cmd_q->ccp->vdata->perform->aes(&op);
850 if (ret)
851 goto e_dst;
852
853 if (aes->action == CCP_AES_ACTION_ENCRYPT) {
854 /* Put the ciphered tag after the ciphertext. */
855 ccp_get_dm_area(&final_wa, 0, p_tag, 0, authsize);
856 } else {
857 /* Does this ciphered tag match the input? */
858 ret = ccp_init_dm_workarea(&tag, cmd_q, authsize,
859 DMA_BIDIRECTIONAL);
860 if (ret)
861 goto e_tag;
862 ret = ccp_set_dm_area(&tag, 0, p_tag, 0, authsize);
863 if (ret)
864 goto e_tag;
865
866 ret = crypto_memneq(tag.address, final_wa.address,
867 authsize) ? -EBADMSG : 0;
868 ccp_dm_free(&tag);
869 }
870
871e_tag:
872 ccp_dm_free(&final_wa);
873
874e_dst:
875 if (ilen > 0 && !in_place)
876 ccp_free_data(&dst, cmd_q);
877
878e_src:
879 if (ilen > 0)
880 ccp_free_data(&src, cmd_q);
881
882e_aad:
883 if (aes->aad_len)
884 ccp_free_data(&aad, cmd_q);
885
886e_ctx:
887 ccp_dm_free(&ctx);
888
889e_key:
890 ccp_dm_free(&key);
891
892 return ret;
893}
894
895static noinline_for_stack int
896ccp_run_aes_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
897{
898 struct ccp_aes_engine *aes = &cmd->u.aes;
899 struct ccp_dm_workarea key, ctx;
900 struct ccp_data src, dst;
901 struct ccp_op op;
902 unsigned int dm_offset;
903 bool in_place = false;
904 int ret;
905
906 if (!((aes->key_len == AES_KEYSIZE_128) ||
907 (aes->key_len == AES_KEYSIZE_192) ||
908 (aes->key_len == AES_KEYSIZE_256)))
909 return -EINVAL;
910
911 if (((aes->mode == CCP_AES_MODE_ECB) ||
912 (aes->mode == CCP_AES_MODE_CBC)) &&
913 (aes->src_len & (AES_BLOCK_SIZE - 1)))
914 return -EINVAL;
915
916 if (!aes->key || !aes->src || !aes->dst)
917 return -EINVAL;
918
919 if (aes->mode != CCP_AES_MODE_ECB) {
920 if (aes->iv_len != AES_BLOCK_SIZE)
921 return -EINVAL;
922
923 if (!aes->iv)
924 return -EINVAL;
925 }
926
927 BUILD_BUG_ON(CCP_AES_KEY_SB_COUNT != 1);
928 BUILD_BUG_ON(CCP_AES_CTX_SB_COUNT != 1);
929
930 ret = -EIO;
931 memset(&op, 0, sizeof(op));
932 op.cmd_q = cmd_q;
933 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
934 op.sb_key = cmd_q->sb_key;
935 op.sb_ctx = cmd_q->sb_ctx;
936 op.init = (aes->mode == CCP_AES_MODE_ECB) ? 0 : 1;
937 op.u.aes.type = aes->type;
938 op.u.aes.mode = aes->mode;
939 op.u.aes.action = aes->action;
940
941 /* All supported key sizes fit in a single (32-byte) SB entry
942 * and must be in little endian format. Use the 256-bit byte
943 * swap passthru option to convert from big endian to little
944 * endian.
945 */
946 ret = ccp_init_dm_workarea(&key, cmd_q,
947 CCP_AES_KEY_SB_COUNT * CCP_SB_BYTES,
948 DMA_TO_DEVICE);
949 if (ret)
950 return ret;
951
952 dm_offset = CCP_SB_BYTES - aes->key_len;
953 ret = ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
954 if (ret)
955 goto e_key;
956 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
957 CCP_PASSTHRU_BYTESWAP_256BIT);
958 if (ret) {
959 cmd->engine_error = cmd_q->cmd_error;
960 goto e_key;
961 }
962
963 /* The AES context fits in a single (32-byte) SB entry and
964 * must be in little endian format. Use the 256-bit byte swap
965 * passthru option to convert from big endian to little endian.
966 */
967 ret = ccp_init_dm_workarea(&ctx, cmd_q,
968 CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
969 DMA_BIDIRECTIONAL);
970 if (ret)
971 goto e_key;
972
973 if (aes->mode != CCP_AES_MODE_ECB) {
974 /* Load the AES context - convert to LE */
975 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
976 ret = ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
977 if (ret)
978 goto e_ctx;
979 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
980 CCP_PASSTHRU_BYTESWAP_256BIT);
981 if (ret) {
982 cmd->engine_error = cmd_q->cmd_error;
983 goto e_ctx;
984 }
985 }
986 switch (aes->mode) {
987 case CCP_AES_MODE_CFB: /* CFB128 only */
988 case CCP_AES_MODE_CTR:
989 op.u.aes.size = AES_BLOCK_SIZE * BITS_PER_BYTE - 1;
990 break;
991 default:
992 op.u.aes.size = 0;
993 }
994
995 /* Prepare the input and output data workareas. For in-place
996 * operations we need to set the dma direction to BIDIRECTIONAL
997 * and copy the src workarea to the dst workarea.
998 */
999 if (sg_virt(aes->src) == sg_virt(aes->dst))
1000 in_place = true;
1001
1002 ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
1003 AES_BLOCK_SIZE,
1004 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1005 if (ret)
1006 goto e_ctx;
1007
1008 if (in_place) {
1009 dst = src;
1010 } else {
1011 ret = ccp_init_data(&dst, cmd_q, aes->dst, aes->src_len,
1012 AES_BLOCK_SIZE, DMA_FROM_DEVICE);
1013 if (ret)
1014 goto e_src;
1015 }
1016
1017 /* Send data to the CCP AES engine */
1018 while (src.sg_wa.bytes_left) {
1019 ccp_prepare_data(&src, &dst, &op, AES_BLOCK_SIZE, true);
1020 if (!src.sg_wa.bytes_left) {
1021 op.eom = 1;
1022
1023 /* Since we don't retrieve the AES context in ECB
1024 * mode we have to wait for the operation to complete
1025 * on the last piece of data
1026 */
1027 if (aes->mode == CCP_AES_MODE_ECB)
1028 op.soc = 1;
1029 }
1030
1031 ret = cmd_q->ccp->vdata->perform->aes(&op);
1032 if (ret) {
1033 cmd->engine_error = cmd_q->cmd_error;
1034 goto e_dst;
1035 }
1036
1037 ccp_process_data(&src, &dst, &op);
1038 }
1039
1040 if (aes->mode != CCP_AES_MODE_ECB) {
1041 /* Retrieve the AES context - convert from LE to BE using
1042 * 32-byte (256-bit) byteswapping
1043 */
1044 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1045 CCP_PASSTHRU_BYTESWAP_256BIT);
1046 if (ret) {
1047 cmd->engine_error = cmd_q->cmd_error;
1048 goto e_dst;
1049 }
1050
1051 /* ...but we only need AES_BLOCK_SIZE bytes */
1052 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
1053 ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
1054 }
1055
1056e_dst:
1057 if (!in_place)
1058 ccp_free_data(&dst, cmd_q);
1059
1060e_src:
1061 ccp_free_data(&src, cmd_q);
1062
1063e_ctx:
1064 ccp_dm_free(&ctx);
1065
1066e_key:
1067 ccp_dm_free(&key);
1068
1069 return ret;
1070}
1071
1072static noinline_for_stack int
1073ccp_run_xts_aes_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1074{
1075 struct ccp_xts_aes_engine *xts = &cmd->u.xts;
1076 struct ccp_dm_workarea key, ctx;
1077 struct ccp_data src, dst;
1078 struct ccp_op op;
1079 unsigned int unit_size, dm_offset;
1080 bool in_place = false;
1081 unsigned int sb_count;
1082 enum ccp_aes_type aestype;
1083 int ret;
1084
1085 switch (xts->unit_size) {
1086 case CCP_XTS_AES_UNIT_SIZE_16:
1087 unit_size = 16;
1088 break;
1089 case CCP_XTS_AES_UNIT_SIZE_512:
1090 unit_size = 512;
1091 break;
1092 case CCP_XTS_AES_UNIT_SIZE_1024:
1093 unit_size = 1024;
1094 break;
1095 case CCP_XTS_AES_UNIT_SIZE_2048:
1096 unit_size = 2048;
1097 break;
1098 case CCP_XTS_AES_UNIT_SIZE_4096:
1099 unit_size = 4096;
1100 break;
1101
1102 default:
1103 return -EINVAL;
1104 }
1105
1106 if (xts->key_len == AES_KEYSIZE_128)
1107 aestype = CCP_AES_TYPE_128;
1108 else if (xts->key_len == AES_KEYSIZE_256)
1109 aestype = CCP_AES_TYPE_256;
1110 else
1111 return -EINVAL;
1112
1113 if (!xts->final && (xts->src_len & (AES_BLOCK_SIZE - 1)))
1114 return -EINVAL;
1115
1116 if (xts->iv_len != AES_BLOCK_SIZE)
1117 return -EINVAL;
1118
1119 if (!xts->key || !xts->iv || !xts->src || !xts->dst)
1120 return -EINVAL;
1121
1122 BUILD_BUG_ON(CCP_XTS_AES_KEY_SB_COUNT != 1);
1123 BUILD_BUG_ON(CCP_XTS_AES_CTX_SB_COUNT != 1);
1124
1125 ret = -EIO;
1126 memset(&op, 0, sizeof(op));
1127 op.cmd_q = cmd_q;
1128 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
1129 op.sb_key = cmd_q->sb_key;
1130 op.sb_ctx = cmd_q->sb_ctx;
1131 op.init = 1;
1132 op.u.xts.type = aestype;
1133 op.u.xts.action = xts->action;
1134 op.u.xts.unit_size = xts->unit_size;
1135
1136 /* A version 3 device only supports 128-bit keys, which fits into a
1137 * single SB entry. A version 5 device uses a 512-bit vector, so two
1138 * SB entries.
1139 */
1140 if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0))
1141 sb_count = CCP_XTS_AES_KEY_SB_COUNT;
1142 else
1143 sb_count = CCP5_XTS_AES_KEY_SB_COUNT;
1144 ret = ccp_init_dm_workarea(&key, cmd_q,
1145 sb_count * CCP_SB_BYTES,
1146 DMA_TO_DEVICE);
1147 if (ret)
1148 return ret;
1149
1150 if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) {
1151 /* All supported key sizes must be in little endian format.
1152 * Use the 256-bit byte swap passthru option to convert from
1153 * big endian to little endian.
1154 */
1155 dm_offset = CCP_SB_BYTES - AES_KEYSIZE_128;
1156 ret = ccp_set_dm_area(&key, dm_offset, xts->key, 0, xts->key_len);
1157 if (ret)
1158 goto e_key;
1159 ret = ccp_set_dm_area(&key, 0, xts->key, xts->key_len, xts->key_len);
1160 if (ret)
1161 goto e_key;
1162 } else {
1163 /* Version 5 CCPs use a 512-bit space for the key: each portion
1164 * occupies 256 bits, or one entire slot, and is zero-padded.
1165 */
1166 unsigned int pad;
1167
1168 dm_offset = CCP_SB_BYTES;
1169 pad = dm_offset - xts->key_len;
1170 ret = ccp_set_dm_area(&key, pad, xts->key, 0, xts->key_len);
1171 if (ret)
1172 goto e_key;
1173 ret = ccp_set_dm_area(&key, dm_offset + pad, xts->key,
1174 xts->key_len, xts->key_len);
1175 if (ret)
1176 goto e_key;
1177 }
1178 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
1179 CCP_PASSTHRU_BYTESWAP_256BIT);
1180 if (ret) {
1181 cmd->engine_error = cmd_q->cmd_error;
1182 goto e_key;
1183 }
1184
1185 /* The AES context fits in a single (32-byte) SB entry and
1186 * for XTS is already in little endian format so no byte swapping
1187 * is needed.
1188 */
1189 ret = ccp_init_dm_workarea(&ctx, cmd_q,
1190 CCP_XTS_AES_CTX_SB_COUNT * CCP_SB_BYTES,
1191 DMA_BIDIRECTIONAL);
1192 if (ret)
1193 goto e_key;
1194
1195 ret = ccp_set_dm_area(&ctx, 0, xts->iv, 0, xts->iv_len);
1196 if (ret)
1197 goto e_ctx;
1198 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1199 CCP_PASSTHRU_BYTESWAP_NOOP);
1200 if (ret) {
1201 cmd->engine_error = cmd_q->cmd_error;
1202 goto e_ctx;
1203 }
1204
1205 /* Prepare the input and output data workareas. For in-place
1206 * operations we need to set the dma direction to BIDIRECTIONAL
1207 * and copy the src workarea to the dst workarea.
1208 */
1209 if (sg_virt(xts->src) == sg_virt(xts->dst))
1210 in_place = true;
1211
1212 ret = ccp_init_data(&src, cmd_q, xts->src, xts->src_len,
1213 unit_size,
1214 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1215 if (ret)
1216 goto e_ctx;
1217
1218 if (in_place) {
1219 dst = src;
1220 } else {
1221 ret = ccp_init_data(&dst, cmd_q, xts->dst, xts->src_len,
1222 unit_size, DMA_FROM_DEVICE);
1223 if (ret)
1224 goto e_src;
1225 }
1226
1227 /* Send data to the CCP AES engine */
1228 while (src.sg_wa.bytes_left) {
1229 ccp_prepare_data(&src, &dst, &op, unit_size, true);
1230 if (!src.sg_wa.bytes_left)
1231 op.eom = 1;
1232
1233 ret = cmd_q->ccp->vdata->perform->xts_aes(&op);
1234 if (ret) {
1235 cmd->engine_error = cmd_q->cmd_error;
1236 goto e_dst;
1237 }
1238
1239 ccp_process_data(&src, &dst, &op);
1240 }
1241
1242 /* Retrieve the AES context - convert from LE to BE using
1243 * 32-byte (256-bit) byteswapping
1244 */
1245 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1246 CCP_PASSTHRU_BYTESWAP_256BIT);
1247 if (ret) {
1248 cmd->engine_error = cmd_q->cmd_error;
1249 goto e_dst;
1250 }
1251
1252 /* ...but we only need AES_BLOCK_SIZE bytes */
1253 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
1254 ccp_get_dm_area(&ctx, dm_offset, xts->iv, 0, xts->iv_len);
1255
1256e_dst:
1257 if (!in_place)
1258 ccp_free_data(&dst, cmd_q);
1259
1260e_src:
1261 ccp_free_data(&src, cmd_q);
1262
1263e_ctx:
1264 ccp_dm_free(&ctx);
1265
1266e_key:
1267 ccp_dm_free(&key);
1268
1269 return ret;
1270}
1271
1272static noinline_for_stack int
1273ccp_run_des3_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1274{
1275 struct ccp_des3_engine *des3 = &cmd->u.des3;
1276
1277 struct ccp_dm_workarea key, ctx;
1278 struct ccp_data src, dst;
1279 struct ccp_op op;
1280 unsigned int dm_offset;
1281 unsigned int len_singlekey;
1282 bool in_place = false;
1283 int ret;
1284
1285 /* Error checks */
1286 if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0))
1287 return -EINVAL;
1288
1289 if (!cmd_q->ccp->vdata->perform->des3)
1290 return -EINVAL;
1291
1292 if (des3->key_len != DES3_EDE_KEY_SIZE)
1293 return -EINVAL;
1294
1295 if (((des3->mode == CCP_DES3_MODE_ECB) ||
1296 (des3->mode == CCP_DES3_MODE_CBC)) &&
1297 (des3->src_len & (DES3_EDE_BLOCK_SIZE - 1)))
1298 return -EINVAL;
1299
1300 if (!des3->key || !des3->src || !des3->dst)
1301 return -EINVAL;
1302
1303 if (des3->mode != CCP_DES3_MODE_ECB) {
1304 if (des3->iv_len != DES3_EDE_BLOCK_SIZE)
1305 return -EINVAL;
1306
1307 if (!des3->iv)
1308 return -EINVAL;
1309 }
1310
1311 ret = -EIO;
1312 /* Zero out all the fields of the command desc */
1313 memset(&op, 0, sizeof(op));
1314
1315 /* Set up the Function field */
1316 op.cmd_q = cmd_q;
1317 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
1318 op.sb_key = cmd_q->sb_key;
1319
1320 op.init = (des3->mode == CCP_DES3_MODE_ECB) ? 0 : 1;
1321 op.u.des3.type = des3->type;
1322 op.u.des3.mode = des3->mode;
1323 op.u.des3.action = des3->action;
1324
1325 /*
1326 * All supported key sizes fit in a single (32-byte) KSB entry and
1327 * (like AES) must be in little endian format. Use the 256-bit byte
1328 * swap passthru option to convert from big endian to little endian.
1329 */
1330 ret = ccp_init_dm_workarea(&key, cmd_q,
1331 CCP_DES3_KEY_SB_COUNT * CCP_SB_BYTES,
1332 DMA_TO_DEVICE);
1333 if (ret)
1334 return ret;
1335
1336 /*
1337 * The contents of the key triplet are in the reverse order of what
1338 * is required by the engine. Copy the 3 pieces individually to put
1339 * them where they belong.
1340 */
1341 dm_offset = CCP_SB_BYTES - des3->key_len; /* Basic offset */
1342
1343 len_singlekey = des3->key_len / 3;
1344 ret = ccp_set_dm_area(&key, dm_offset + 2 * len_singlekey,
1345 des3->key, 0, len_singlekey);
1346 if (ret)
1347 goto e_key;
1348 ret = ccp_set_dm_area(&key, dm_offset + len_singlekey,
1349 des3->key, len_singlekey, len_singlekey);
1350 if (ret)
1351 goto e_key;
1352 ret = ccp_set_dm_area(&key, dm_offset,
1353 des3->key, 2 * len_singlekey, len_singlekey);
1354 if (ret)
1355 goto e_key;
1356
1357 /* Copy the key to the SB */
1358 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
1359 CCP_PASSTHRU_BYTESWAP_256BIT);
1360 if (ret) {
1361 cmd->engine_error = cmd_q->cmd_error;
1362 goto e_key;
1363 }
1364
1365 /*
1366 * The DES3 context fits in a single (32-byte) KSB entry and
1367 * must be in little endian format. Use the 256-bit byte swap
1368 * passthru option to convert from big endian to little endian.
1369 */
1370 if (des3->mode != CCP_DES3_MODE_ECB) {
1371 op.sb_ctx = cmd_q->sb_ctx;
1372
1373 ret = ccp_init_dm_workarea(&ctx, cmd_q,
1374 CCP_DES3_CTX_SB_COUNT * CCP_SB_BYTES,
1375 DMA_BIDIRECTIONAL);
1376 if (ret)
1377 goto e_key;
1378
1379 /* Load the context into the LSB */
1380 dm_offset = CCP_SB_BYTES - des3->iv_len;
1381 ret = ccp_set_dm_area(&ctx, dm_offset, des3->iv, 0,
1382 des3->iv_len);
1383 if (ret)
1384 goto e_ctx;
1385
1386 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1387 CCP_PASSTHRU_BYTESWAP_256BIT);
1388 if (ret) {
1389 cmd->engine_error = cmd_q->cmd_error;
1390 goto e_ctx;
1391 }
1392 }
1393
1394 /*
1395 * Prepare the input and output data workareas. For in-place
1396 * operations we need to set the dma direction to BIDIRECTIONAL
1397 * and copy the src workarea to the dst workarea.
1398 */
1399 if (sg_virt(des3->src) == sg_virt(des3->dst))
1400 in_place = true;
1401
1402 ret = ccp_init_data(&src, cmd_q, des3->src, des3->src_len,
1403 DES3_EDE_BLOCK_SIZE,
1404 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1405 if (ret)
1406 goto e_ctx;
1407
1408 if (in_place)
1409 dst = src;
1410 else {
1411 ret = ccp_init_data(&dst, cmd_q, des3->dst, des3->src_len,
1412 DES3_EDE_BLOCK_SIZE, DMA_FROM_DEVICE);
1413 if (ret)
1414 goto e_src;
1415 }
1416
1417 /* Send data to the CCP DES3 engine */
1418 while (src.sg_wa.bytes_left) {
1419 ccp_prepare_data(&src, &dst, &op, DES3_EDE_BLOCK_SIZE, true);
1420 if (!src.sg_wa.bytes_left) {
1421 op.eom = 1;
1422
1423 /* Since we don't retrieve the context in ECB mode
1424 * we have to wait for the operation to complete
1425 * on the last piece of data
1426 */
1427 op.soc = 0;
1428 }
1429
1430 ret = cmd_q->ccp->vdata->perform->des3(&op);
1431 if (ret) {
1432 cmd->engine_error = cmd_q->cmd_error;
1433 goto e_dst;
1434 }
1435
1436 ccp_process_data(&src, &dst, &op);
1437 }
1438
1439 if (des3->mode != CCP_DES3_MODE_ECB) {
1440 /* Retrieve the context and make BE */
1441 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1442 CCP_PASSTHRU_BYTESWAP_256BIT);
1443 if (ret) {
1444 cmd->engine_error = cmd_q->cmd_error;
1445 goto e_dst;
1446 }
1447
1448 /* ...but we only need the last DES3_EDE_BLOCK_SIZE bytes */
1449 ccp_get_dm_area(&ctx, dm_offset, des3->iv, 0,
1450 DES3_EDE_BLOCK_SIZE);
1451 }
1452e_dst:
1453 if (!in_place)
1454 ccp_free_data(&dst, cmd_q);
1455
1456e_src:
1457 ccp_free_data(&src, cmd_q);
1458
1459e_ctx:
1460 if (des3->mode != CCP_DES3_MODE_ECB)
1461 ccp_dm_free(&ctx);
1462
1463e_key:
1464 ccp_dm_free(&key);
1465
1466 return ret;
1467}
1468
1469static noinline_for_stack int
1470ccp_run_sha_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1471{
1472 struct ccp_sha_engine *sha = &cmd->u.sha;
1473 struct ccp_dm_workarea ctx;
1474 struct ccp_data src;
1475 struct ccp_op op;
1476 unsigned int ioffset, ooffset;
1477 unsigned int digest_size;
1478 int sb_count;
1479 const void *init;
1480 u64 block_size;
1481 int ctx_size;
1482 int ret;
1483
1484 switch (sha->type) {
1485 case CCP_SHA_TYPE_1:
1486 if (sha->ctx_len < SHA1_DIGEST_SIZE)
1487 return -EINVAL;
1488 block_size = SHA1_BLOCK_SIZE;
1489 break;
1490 case CCP_SHA_TYPE_224:
1491 if (sha->ctx_len < SHA224_DIGEST_SIZE)
1492 return -EINVAL;
1493 block_size = SHA224_BLOCK_SIZE;
1494 break;
1495 case CCP_SHA_TYPE_256:
1496 if (sha->ctx_len < SHA256_DIGEST_SIZE)
1497 return -EINVAL;
1498 block_size = SHA256_BLOCK_SIZE;
1499 break;
1500 case CCP_SHA_TYPE_384:
1501 if (cmd_q->ccp->vdata->version < CCP_VERSION(4, 0)
1502 || sha->ctx_len < SHA384_DIGEST_SIZE)
1503 return -EINVAL;
1504 block_size = SHA384_BLOCK_SIZE;
1505 break;
1506 case CCP_SHA_TYPE_512:
1507 if (cmd_q->ccp->vdata->version < CCP_VERSION(4, 0)
1508 || sha->ctx_len < SHA512_DIGEST_SIZE)
1509 return -EINVAL;
1510 block_size = SHA512_BLOCK_SIZE;
1511 break;
1512 default:
1513 return -EINVAL;
1514 }
1515
1516 if (!sha->ctx)
1517 return -EINVAL;
1518
1519 if (!sha->final && (sha->src_len & (block_size - 1)))
1520 return -EINVAL;
1521
1522 /* The version 3 device can't handle zero-length input */
1523 if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) {
1524
1525 if (!sha->src_len) {
1526 unsigned int digest_len;
1527 const u8 *sha_zero;
1528
1529 /* Not final, just return */
1530 if (!sha->final)
1531 return 0;
1532
1533 /* CCP can't do a zero length sha operation so the
1534 * caller must buffer the data.
1535 */
1536 if (sha->msg_bits)
1537 return -EINVAL;
1538
1539 /* The CCP cannot perform zero-length sha operations
1540 * so the caller is required to buffer data for the
1541 * final operation. However, a sha operation for a
1542 * message with a total length of zero is valid so
1543 * known values are required to supply the result.
1544 */
1545 switch (sha->type) {
1546 case CCP_SHA_TYPE_1:
1547 sha_zero = sha1_zero_message_hash;
1548 digest_len = SHA1_DIGEST_SIZE;
1549 break;
1550 case CCP_SHA_TYPE_224:
1551 sha_zero = sha224_zero_message_hash;
1552 digest_len = SHA224_DIGEST_SIZE;
1553 break;
1554 case CCP_SHA_TYPE_256:
1555 sha_zero = sha256_zero_message_hash;
1556 digest_len = SHA256_DIGEST_SIZE;
1557 break;
1558 default:
1559 return -EINVAL;
1560 }
1561
1562 scatterwalk_map_and_copy((void *)sha_zero, sha->ctx, 0,
1563 digest_len, 1);
1564
1565 return 0;
1566 }
1567 }
1568
1569 /* Set variables used throughout */
1570 switch (sha->type) {
1571 case CCP_SHA_TYPE_1:
1572 digest_size = SHA1_DIGEST_SIZE;
1573 init = (void *) ccp_sha1_init;
1574 ctx_size = SHA1_DIGEST_SIZE;
1575 sb_count = 1;
1576 if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0))
1577 ooffset = ioffset = CCP_SB_BYTES - SHA1_DIGEST_SIZE;
1578 else
1579 ooffset = ioffset = 0;
1580 break;
1581 case CCP_SHA_TYPE_224:
1582 digest_size = SHA224_DIGEST_SIZE;
1583 init = (void *) ccp_sha224_init;
1584 ctx_size = SHA256_DIGEST_SIZE;
1585 sb_count = 1;
1586 ioffset = 0;
1587 if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0))
1588 ooffset = CCP_SB_BYTES - SHA224_DIGEST_SIZE;
1589 else
1590 ooffset = 0;
1591 break;
1592 case CCP_SHA_TYPE_256:
1593 digest_size = SHA256_DIGEST_SIZE;
1594 init = (void *) ccp_sha256_init;
1595 ctx_size = SHA256_DIGEST_SIZE;
1596 sb_count = 1;
1597 ooffset = ioffset = 0;
1598 break;
1599 case CCP_SHA_TYPE_384:
1600 digest_size = SHA384_DIGEST_SIZE;
1601 init = (void *) ccp_sha384_init;
1602 ctx_size = SHA512_DIGEST_SIZE;
1603 sb_count = 2;
1604 ioffset = 0;
1605 ooffset = 2 * CCP_SB_BYTES - SHA384_DIGEST_SIZE;
1606 break;
1607 case CCP_SHA_TYPE_512:
1608 digest_size = SHA512_DIGEST_SIZE;
1609 init = (void *) ccp_sha512_init;
1610 ctx_size = SHA512_DIGEST_SIZE;
1611 sb_count = 2;
1612 ooffset = ioffset = 0;
1613 break;
1614 default:
1615 ret = -EINVAL;
1616 goto e_data;
1617 }
1618
1619 /* For zero-length plaintext the src pointer is ignored;
1620 * otherwise both parts must be valid
1621 */
1622 if (sha->src_len && !sha->src)
1623 return -EINVAL;
1624
1625 memset(&op, 0, sizeof(op));
1626 op.cmd_q = cmd_q;
1627 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
1628 op.sb_ctx = cmd_q->sb_ctx; /* Pre-allocated */
1629 op.u.sha.type = sha->type;
1630 op.u.sha.msg_bits = sha->msg_bits;
1631
1632 /* For SHA1/224/256 the context fits in a single (32-byte) SB entry;
1633 * SHA384/512 require 2 adjacent SB slots, with the right half in the
1634 * first slot, and the left half in the second. Each portion must then
1635 * be in little endian format: use the 256-bit byte swap option.
1636 */
1637 ret = ccp_init_dm_workarea(&ctx, cmd_q, sb_count * CCP_SB_BYTES,
1638 DMA_BIDIRECTIONAL);
1639 if (ret)
1640 return ret;
1641 if (sha->first) {
1642 switch (sha->type) {
1643 case CCP_SHA_TYPE_1:
1644 case CCP_SHA_TYPE_224:
1645 case CCP_SHA_TYPE_256:
1646 memcpy(ctx.address + ioffset, init, ctx_size);
1647 break;
1648 case CCP_SHA_TYPE_384:
1649 case CCP_SHA_TYPE_512:
1650 memcpy(ctx.address + ctx_size / 2, init,
1651 ctx_size / 2);
1652 memcpy(ctx.address, init + ctx_size / 2,
1653 ctx_size / 2);
1654 break;
1655 default:
1656 ret = -EINVAL;
1657 goto e_ctx;
1658 }
1659 } else {
1660 /* Restore the context */
1661 ret = ccp_set_dm_area(&ctx, 0, sha->ctx, 0,
1662 sb_count * CCP_SB_BYTES);
1663 if (ret)
1664 goto e_ctx;
1665 }
1666
1667 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1668 CCP_PASSTHRU_BYTESWAP_256BIT);
1669 if (ret) {
1670 cmd->engine_error = cmd_q->cmd_error;
1671 goto e_ctx;
1672 }
1673
1674 if (sha->src) {
1675 /* Send data to the CCP SHA engine; block_size is set above */
1676 ret = ccp_init_data(&src, cmd_q, sha->src, sha->src_len,
1677 block_size, DMA_TO_DEVICE);
1678 if (ret)
1679 goto e_ctx;
1680
1681 while (src.sg_wa.bytes_left) {
1682 ccp_prepare_data(&src, NULL, &op, block_size, false);
1683 if (sha->final && !src.sg_wa.bytes_left)
1684 op.eom = 1;
1685
1686 ret = cmd_q->ccp->vdata->perform->sha(&op);
1687 if (ret) {
1688 cmd->engine_error = cmd_q->cmd_error;
1689 goto e_data;
1690 }
1691
1692 ccp_process_data(&src, NULL, &op);
1693 }
1694 } else {
1695 op.eom = 1;
1696 ret = cmd_q->ccp->vdata->perform->sha(&op);
1697 if (ret) {
1698 cmd->engine_error = cmd_q->cmd_error;
1699 goto e_data;
1700 }
1701 }
1702
1703 /* Retrieve the SHA context - convert from LE to BE using
1704 * 32-byte (256-bit) byteswapping to BE
1705 */
1706 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1707 CCP_PASSTHRU_BYTESWAP_256BIT);
1708 if (ret) {
1709 cmd->engine_error = cmd_q->cmd_error;
1710 goto e_data;
1711 }
1712
1713 if (sha->final) {
1714 /* Finishing up, so get the digest */
1715 switch (sha->type) {
1716 case CCP_SHA_TYPE_1:
1717 case CCP_SHA_TYPE_224:
1718 case CCP_SHA_TYPE_256:
1719 ccp_get_dm_area(&ctx, ooffset,
1720 sha->ctx, 0,
1721 digest_size);
1722 break;
1723 case CCP_SHA_TYPE_384:
1724 case CCP_SHA_TYPE_512:
1725 ccp_get_dm_area(&ctx, 0,
1726 sha->ctx, LSB_ITEM_SIZE - ooffset,
1727 LSB_ITEM_SIZE);
1728 ccp_get_dm_area(&ctx, LSB_ITEM_SIZE + ooffset,
1729 sha->ctx, 0,
1730 LSB_ITEM_SIZE - ooffset);
1731 break;
1732 default:
1733 ret = -EINVAL;
1734 goto e_ctx;
1735 }
1736 } else {
1737 /* Stash the context */
1738 ccp_get_dm_area(&ctx, 0, sha->ctx, 0,
1739 sb_count * CCP_SB_BYTES);
1740 }
1741
1742 if (sha->final && sha->opad) {
1743 /* HMAC operation, recursively perform final SHA */
1744 struct ccp_cmd hmac_cmd;
1745 struct scatterlist sg;
1746 u8 *hmac_buf;
1747
1748 if (sha->opad_len != block_size) {
1749 ret = -EINVAL;
1750 goto e_data;
1751 }
1752
1753 hmac_buf = kmalloc(block_size + digest_size, GFP_KERNEL);
1754 if (!hmac_buf) {
1755 ret = -ENOMEM;
1756 goto e_data;
1757 }
1758 sg_init_one(&sg, hmac_buf, block_size + digest_size);
1759
1760 scatterwalk_map_and_copy(hmac_buf, sha->opad, 0, block_size, 0);
1761 switch (sha->type) {
1762 case CCP_SHA_TYPE_1:
1763 case CCP_SHA_TYPE_224:
1764 case CCP_SHA_TYPE_256:
1765 memcpy(hmac_buf + block_size,
1766 ctx.address + ooffset,
1767 digest_size);
1768 break;
1769 case CCP_SHA_TYPE_384:
1770 case CCP_SHA_TYPE_512:
1771 memcpy(hmac_buf + block_size,
1772 ctx.address + LSB_ITEM_SIZE + ooffset,
1773 LSB_ITEM_SIZE);
1774 memcpy(hmac_buf + block_size +
1775 (LSB_ITEM_SIZE - ooffset),
1776 ctx.address,
1777 LSB_ITEM_SIZE);
1778 break;
1779 default:
1780 ret = -EINVAL;
1781 goto e_ctx;
1782 }
1783
1784 memset(&hmac_cmd, 0, sizeof(hmac_cmd));
1785 hmac_cmd.engine = CCP_ENGINE_SHA;
1786 hmac_cmd.u.sha.type = sha->type;
1787 hmac_cmd.u.sha.ctx = sha->ctx;
1788 hmac_cmd.u.sha.ctx_len = sha->ctx_len;
1789 hmac_cmd.u.sha.src = &sg;
1790 hmac_cmd.u.sha.src_len = block_size + digest_size;
1791 hmac_cmd.u.sha.opad = NULL;
1792 hmac_cmd.u.sha.opad_len = 0;
1793 hmac_cmd.u.sha.first = 1;
1794 hmac_cmd.u.sha.final = 1;
1795 hmac_cmd.u.sha.msg_bits = (block_size + digest_size) << 3;
1796
1797 ret = ccp_run_sha_cmd(cmd_q, &hmac_cmd);
1798 if (ret)
1799 cmd->engine_error = hmac_cmd.engine_error;
1800
1801 kfree(hmac_buf);
1802 }
1803
1804e_data:
1805 if (sha->src)
1806 ccp_free_data(&src, cmd_q);
1807
1808e_ctx:
1809 ccp_dm_free(&ctx);
1810
1811 return ret;
1812}
1813
1814static noinline_for_stack int
1815ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1816{
1817 struct ccp_rsa_engine *rsa = &cmd->u.rsa;
1818 struct ccp_dm_workarea exp, src, dst;
1819 struct ccp_op op;
1820 unsigned int sb_count, i_len, o_len;
1821 int ret;
1822
1823 /* Check against the maximum allowable size, in bits */
1824 if (rsa->key_size > cmd_q->ccp->vdata->rsamax)
1825 return -EINVAL;
1826
1827 if (!rsa->exp || !rsa->mod || !rsa->src || !rsa->dst)
1828 return -EINVAL;
1829
1830 memset(&op, 0, sizeof(op));
1831 op.cmd_q = cmd_q;
1832 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
1833
1834 /* The RSA modulus must precede the message being acted upon, so
1835 * it must be copied to a DMA area where the message and the
1836 * modulus can be concatenated. Therefore the input buffer
1837 * length required is twice the output buffer length (which
1838 * must be a multiple of 256-bits). Compute o_len, i_len in bytes.
1839 * Buffer sizes must be a multiple of 32 bytes; rounding up may be
1840 * required.
1841 */
1842 o_len = 32 * ((rsa->key_size + 255) / 256);
1843 i_len = o_len * 2;
1844
1845 sb_count = 0;
1846 if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0)) {
1847 /* sb_count is the number of storage block slots required
1848 * for the modulus.
1849 */
1850 sb_count = o_len / CCP_SB_BYTES;
1851 op.sb_key = cmd_q->ccp->vdata->perform->sballoc(cmd_q,
1852 sb_count);
1853 if (!op.sb_key)
1854 return -EIO;
1855 } else {
1856 /* A version 5 device allows a modulus size that will not fit
1857 * in the LSB, so the command will transfer it from memory.
1858 * Set the sb key to the default, even though it's not used.
1859 */
1860 op.sb_key = cmd_q->sb_key;
1861 }
1862
1863 /* The RSA exponent must be in little endian format. Reverse its
1864 * byte order.
1865 */
1866 ret = ccp_init_dm_workarea(&exp, cmd_q, o_len, DMA_TO_DEVICE);
1867 if (ret)
1868 goto e_sb;
1869
1870 ret = ccp_reverse_set_dm_area(&exp, 0, rsa->exp, 0, rsa->exp_len);
1871 if (ret)
1872 goto e_exp;
1873
1874 if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0)) {
1875 /* Copy the exponent to the local storage block, using
1876 * as many 32-byte blocks as were allocated above. It's
1877 * already little endian, so no further change is required.
1878 */
1879 ret = ccp_copy_to_sb(cmd_q, &exp, op.jobid, op.sb_key,
1880 CCP_PASSTHRU_BYTESWAP_NOOP);
1881 if (ret) {
1882 cmd->engine_error = cmd_q->cmd_error;
1883 goto e_exp;
1884 }
1885 } else {
1886 /* The exponent can be retrieved from memory via DMA. */
1887 op.exp.u.dma.address = exp.dma.address;
1888 op.exp.u.dma.offset = 0;
1889 }
1890
1891 /* Concatenate the modulus and the message. Both the modulus and
1892 * the operands must be in little endian format. Since the input
1893 * is in big endian format it must be converted.
1894 */
1895 ret = ccp_init_dm_workarea(&src, cmd_q, i_len, DMA_TO_DEVICE);
1896 if (ret)
1897 goto e_exp;
1898
1899 ret = ccp_reverse_set_dm_area(&src, 0, rsa->mod, 0, rsa->mod_len);
1900 if (ret)
1901 goto e_src;
1902 ret = ccp_reverse_set_dm_area(&src, o_len, rsa->src, 0, rsa->src_len);
1903 if (ret)
1904 goto e_src;
1905
1906 /* Prepare the output area for the operation */
1907 ret = ccp_init_dm_workarea(&dst, cmd_q, o_len, DMA_FROM_DEVICE);
1908 if (ret)
1909 goto e_src;
1910
1911 op.soc = 1;
1912 op.src.u.dma.address = src.dma.address;
1913 op.src.u.dma.offset = 0;
1914 op.src.u.dma.length = i_len;
1915 op.dst.u.dma.address = dst.dma.address;
1916 op.dst.u.dma.offset = 0;
1917 op.dst.u.dma.length = o_len;
1918
1919 op.u.rsa.mod_size = rsa->key_size;
1920 op.u.rsa.input_len = i_len;
1921
1922 ret = cmd_q->ccp->vdata->perform->rsa(&op);
1923 if (ret) {
1924 cmd->engine_error = cmd_q->cmd_error;
1925 goto e_dst;
1926 }
1927
1928 ccp_reverse_get_dm_area(&dst, 0, rsa->dst, 0, rsa->mod_len);
1929
1930e_dst:
1931 ccp_dm_free(&dst);
1932
1933e_src:
1934 ccp_dm_free(&src);
1935
1936e_exp:
1937 ccp_dm_free(&exp);
1938
1939e_sb:
1940 if (sb_count)
1941 cmd_q->ccp->vdata->perform->sbfree(cmd_q, op.sb_key, sb_count);
1942
1943 return ret;
1944}
1945
1946static noinline_for_stack int
1947ccp_run_passthru_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1948{
1949 struct ccp_passthru_engine *pt = &cmd->u.passthru;
1950 struct ccp_dm_workarea mask;
1951 struct ccp_data src, dst;
1952 struct ccp_op op;
1953 bool in_place = false;
1954 unsigned int i;
1955 int ret = 0;
1956
1957 if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
1958 return -EINVAL;
1959
1960 if (!pt->src || !pt->dst)
1961 return -EINVAL;
1962
1963 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
1964 if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
1965 return -EINVAL;
1966 if (!pt->mask)
1967 return -EINVAL;
1968 }
1969
1970 BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT != 1);
1971
1972 memset(&op, 0, sizeof(op));
1973 op.cmd_q = cmd_q;
1974 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
1975
1976 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
1977 /* Load the mask */
1978 op.sb_key = cmd_q->sb_key;
1979
1980 ret = ccp_init_dm_workarea(&mask, cmd_q,
1981 CCP_PASSTHRU_SB_COUNT *
1982 CCP_SB_BYTES,
1983 DMA_TO_DEVICE);
1984 if (ret)
1985 return ret;
1986
1987 ret = ccp_set_dm_area(&mask, 0, pt->mask, 0, pt->mask_len);
1988 if (ret)
1989 goto e_mask;
1990 ret = ccp_copy_to_sb(cmd_q, &mask, op.jobid, op.sb_key,
1991 CCP_PASSTHRU_BYTESWAP_NOOP);
1992 if (ret) {
1993 cmd->engine_error = cmd_q->cmd_error;
1994 goto e_mask;
1995 }
1996 }
1997
1998 /* Prepare the input and output data workareas. For in-place
1999 * operations we need to set the dma direction to BIDIRECTIONAL
2000 * and copy the src workarea to the dst workarea.
2001 */
2002 if (sg_virt(pt->src) == sg_virt(pt->dst))
2003 in_place = true;
2004
2005 ret = ccp_init_data(&src, cmd_q, pt->src, pt->src_len,
2006 CCP_PASSTHRU_MASKSIZE,
2007 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
2008 if (ret)
2009 goto e_mask;
2010
2011 if (in_place) {
2012 dst = src;
2013 } else {
2014 ret = ccp_init_data(&dst, cmd_q, pt->dst, pt->src_len,
2015 CCP_PASSTHRU_MASKSIZE, DMA_FROM_DEVICE);
2016 if (ret)
2017 goto e_src;
2018 }
2019
2020 /* Send data to the CCP Passthru engine
2021 * Because the CCP engine works on a single source and destination
2022 * dma address at a time, each entry in the source scatterlist
2023 * (after the dma_map_sg call) must be less than or equal to the
2024 * (remaining) length in the destination scatterlist entry and the
2025 * length must be a multiple of CCP_PASSTHRU_BLOCKSIZE
2026 */
2027 dst.sg_wa.sg_used = 0;
2028 for (i = 1; i <= src.sg_wa.dma_count; i++) {
2029 if (!dst.sg_wa.sg ||
2030 (dst.sg_wa.sg->length < src.sg_wa.sg->length)) {
2031 ret = -EINVAL;
2032 goto e_dst;
2033 }
2034
2035 if (i == src.sg_wa.dma_count) {
2036 op.eom = 1;
2037 op.soc = 1;
2038 }
2039
2040 op.src.type = CCP_MEMTYPE_SYSTEM;
2041 op.src.u.dma.address = sg_dma_address(src.sg_wa.sg);
2042 op.src.u.dma.offset = 0;
2043 op.src.u.dma.length = sg_dma_len(src.sg_wa.sg);
2044
2045 op.dst.type = CCP_MEMTYPE_SYSTEM;
2046 op.dst.u.dma.address = sg_dma_address(dst.sg_wa.sg);
2047 op.dst.u.dma.offset = dst.sg_wa.sg_used;
2048 op.dst.u.dma.length = op.src.u.dma.length;
2049
2050 ret = cmd_q->ccp->vdata->perform->passthru(&op);
2051 if (ret) {
2052 cmd->engine_error = cmd_q->cmd_error;
2053 goto e_dst;
2054 }
2055
2056 dst.sg_wa.sg_used += src.sg_wa.sg->length;
2057 if (dst.sg_wa.sg_used == dst.sg_wa.sg->length) {
2058 dst.sg_wa.sg = sg_next(dst.sg_wa.sg);
2059 dst.sg_wa.sg_used = 0;
2060 }
2061 src.sg_wa.sg = sg_next(src.sg_wa.sg);
2062 }
2063
2064e_dst:
2065 if (!in_place)
2066 ccp_free_data(&dst, cmd_q);
2067
2068e_src:
2069 ccp_free_data(&src, cmd_q);
2070
2071e_mask:
2072 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP)
2073 ccp_dm_free(&mask);
2074
2075 return ret;
2076}
2077
2078static noinline_for_stack int
2079ccp_run_passthru_nomap_cmd(struct ccp_cmd_queue *cmd_q,
2080 struct ccp_cmd *cmd)
2081{
2082 struct ccp_passthru_nomap_engine *pt = &cmd->u.passthru_nomap;
2083 struct ccp_dm_workarea mask;
2084 struct ccp_op op;
2085 int ret;
2086
2087 if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
2088 return -EINVAL;
2089
2090 if (!pt->src_dma || !pt->dst_dma)
2091 return -EINVAL;
2092
2093 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
2094 if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
2095 return -EINVAL;
2096 if (!pt->mask)
2097 return -EINVAL;
2098 }
2099
2100 BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT != 1);
2101
2102 memset(&op, 0, sizeof(op));
2103 op.cmd_q = cmd_q;
2104 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
2105
2106 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
2107 /* Load the mask */
2108 op.sb_key = cmd_q->sb_key;
2109
2110 mask.length = pt->mask_len;
2111 mask.dma.address = pt->mask;
2112 mask.dma.length = pt->mask_len;
2113
2114 ret = ccp_copy_to_sb(cmd_q, &mask, op.jobid, op.sb_key,
2115 CCP_PASSTHRU_BYTESWAP_NOOP);
2116 if (ret) {
2117 cmd->engine_error = cmd_q->cmd_error;
2118 return ret;
2119 }
2120 }
2121
2122 /* Send data to the CCP Passthru engine */
2123 op.eom = 1;
2124 op.soc = 1;
2125
2126 op.src.type = CCP_MEMTYPE_SYSTEM;
2127 op.src.u.dma.address = pt->src_dma;
2128 op.src.u.dma.offset = 0;
2129 op.src.u.dma.length = pt->src_len;
2130
2131 op.dst.type = CCP_MEMTYPE_SYSTEM;
2132 op.dst.u.dma.address = pt->dst_dma;
2133 op.dst.u.dma.offset = 0;
2134 op.dst.u.dma.length = pt->src_len;
2135
2136 ret = cmd_q->ccp->vdata->perform->passthru(&op);
2137 if (ret)
2138 cmd->engine_error = cmd_q->cmd_error;
2139
2140 return ret;
2141}
2142
2143static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
2144{
2145 struct ccp_ecc_engine *ecc = &cmd->u.ecc;
2146 struct ccp_dm_workarea src, dst;
2147 struct ccp_op op;
2148 int ret;
2149 u8 *save;
2150
2151 if (!ecc->u.mm.operand_1 ||
2152 (ecc->u.mm.operand_1_len > CCP_ECC_MODULUS_BYTES))
2153 return -EINVAL;
2154
2155 if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT)
2156 if (!ecc->u.mm.operand_2 ||
2157 (ecc->u.mm.operand_2_len > CCP_ECC_MODULUS_BYTES))
2158 return -EINVAL;
2159
2160 if (!ecc->u.mm.result ||
2161 (ecc->u.mm.result_len < CCP_ECC_MODULUS_BYTES))
2162 return -EINVAL;
2163
2164 memset(&op, 0, sizeof(op));
2165 op.cmd_q = cmd_q;
2166 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
2167
2168 /* Concatenate the modulus and the operands. Both the modulus and
2169 * the operands must be in little endian format. Since the input
2170 * is in big endian format it must be converted and placed in a
2171 * fixed length buffer.
2172 */
2173 ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
2174 DMA_TO_DEVICE);
2175 if (ret)
2176 return ret;
2177
2178 /* Save the workarea address since it is updated in order to perform
2179 * the concatenation
2180 */
2181 save = src.address;
2182
2183 /* Copy the ECC modulus */
2184 ret = ccp_reverse_set_dm_area(&src, 0, ecc->mod, 0, ecc->mod_len);
2185 if (ret)
2186 goto e_src;
2187 src.address += CCP_ECC_OPERAND_SIZE;
2188
2189 /* Copy the first operand */
2190 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.mm.operand_1, 0,
2191 ecc->u.mm.operand_1_len);
2192 if (ret)
2193 goto e_src;
2194 src.address += CCP_ECC_OPERAND_SIZE;
2195
2196 if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT) {
2197 /* Copy the second operand */
2198 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.mm.operand_2, 0,
2199 ecc->u.mm.operand_2_len);
2200 if (ret)
2201 goto e_src;
2202 src.address += CCP_ECC_OPERAND_SIZE;
2203 }
2204
2205 /* Restore the workarea address */
2206 src.address = save;
2207
2208 /* Prepare the output area for the operation */
2209 ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
2210 DMA_FROM_DEVICE);
2211 if (ret)
2212 goto e_src;
2213
2214 op.soc = 1;
2215 op.src.u.dma.address = src.dma.address;
2216 op.src.u.dma.offset = 0;
2217 op.src.u.dma.length = src.length;
2218 op.dst.u.dma.address = dst.dma.address;
2219 op.dst.u.dma.offset = 0;
2220 op.dst.u.dma.length = dst.length;
2221
2222 op.u.ecc.function = cmd->u.ecc.function;
2223
2224 ret = cmd_q->ccp->vdata->perform->ecc(&op);
2225 if (ret) {
2226 cmd->engine_error = cmd_q->cmd_error;
2227 goto e_dst;
2228 }
2229
2230 ecc->ecc_result = le16_to_cpup(
2231 (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
2232 if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
2233 ret = -EIO;
2234 goto e_dst;
2235 }
2236
2237 /* Save the ECC result */
2238 ccp_reverse_get_dm_area(&dst, 0, ecc->u.mm.result, 0,
2239 CCP_ECC_MODULUS_BYTES);
2240
2241e_dst:
2242 ccp_dm_free(&dst);
2243
2244e_src:
2245 ccp_dm_free(&src);
2246
2247 return ret;
2248}
2249
2250static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
2251{
2252 struct ccp_ecc_engine *ecc = &cmd->u.ecc;
2253 struct ccp_dm_workarea src, dst;
2254 struct ccp_op op;
2255 int ret;
2256 u8 *save;
2257
2258 if (!ecc->u.pm.point_1.x ||
2259 (ecc->u.pm.point_1.x_len > CCP_ECC_MODULUS_BYTES) ||
2260 !ecc->u.pm.point_1.y ||
2261 (ecc->u.pm.point_1.y_len > CCP_ECC_MODULUS_BYTES))
2262 return -EINVAL;
2263
2264 if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
2265 if (!ecc->u.pm.point_2.x ||
2266 (ecc->u.pm.point_2.x_len > CCP_ECC_MODULUS_BYTES) ||
2267 !ecc->u.pm.point_2.y ||
2268 (ecc->u.pm.point_2.y_len > CCP_ECC_MODULUS_BYTES))
2269 return -EINVAL;
2270 } else {
2271 if (!ecc->u.pm.domain_a ||
2272 (ecc->u.pm.domain_a_len > CCP_ECC_MODULUS_BYTES))
2273 return -EINVAL;
2274
2275 if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT)
2276 if (!ecc->u.pm.scalar ||
2277 (ecc->u.pm.scalar_len > CCP_ECC_MODULUS_BYTES))
2278 return -EINVAL;
2279 }
2280
2281 if (!ecc->u.pm.result.x ||
2282 (ecc->u.pm.result.x_len < CCP_ECC_MODULUS_BYTES) ||
2283 !ecc->u.pm.result.y ||
2284 (ecc->u.pm.result.y_len < CCP_ECC_MODULUS_BYTES))
2285 return -EINVAL;
2286
2287 memset(&op, 0, sizeof(op));
2288 op.cmd_q = cmd_q;
2289 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
2290
2291 /* Concatenate the modulus and the operands. Both the modulus and
2292 * the operands must be in little endian format. Since the input
2293 * is in big endian format it must be converted and placed in a
2294 * fixed length buffer.
2295 */
2296 ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
2297 DMA_TO_DEVICE);
2298 if (ret)
2299 return ret;
2300
2301 /* Save the workarea address since it is updated in order to perform
2302 * the concatenation
2303 */
2304 save = src.address;
2305
2306 /* Copy the ECC modulus */
2307 ret = ccp_reverse_set_dm_area(&src, 0, ecc->mod, 0, ecc->mod_len);
2308 if (ret)
2309 goto e_src;
2310 src.address += CCP_ECC_OPERAND_SIZE;
2311
2312 /* Copy the first point X and Y coordinate */
2313 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_1.x, 0,
2314 ecc->u.pm.point_1.x_len);
2315 if (ret)
2316 goto e_src;
2317 src.address += CCP_ECC_OPERAND_SIZE;
2318 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_1.y, 0,
2319 ecc->u.pm.point_1.y_len);
2320 if (ret)
2321 goto e_src;
2322 src.address += CCP_ECC_OPERAND_SIZE;
2323
2324 /* Set the first point Z coordinate to 1 */
2325 *src.address = 0x01;
2326 src.address += CCP_ECC_OPERAND_SIZE;
2327
2328 if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
2329 /* Copy the second point X and Y coordinate */
2330 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_2.x, 0,
2331 ecc->u.pm.point_2.x_len);
2332 if (ret)
2333 goto e_src;
2334 src.address += CCP_ECC_OPERAND_SIZE;
2335 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_2.y, 0,
2336 ecc->u.pm.point_2.y_len);
2337 if (ret)
2338 goto e_src;
2339 src.address += CCP_ECC_OPERAND_SIZE;
2340
2341 /* Set the second point Z coordinate to 1 */
2342 *src.address = 0x01;
2343 src.address += CCP_ECC_OPERAND_SIZE;
2344 } else {
2345 /* Copy the Domain "a" parameter */
2346 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.domain_a, 0,
2347 ecc->u.pm.domain_a_len);
2348 if (ret)
2349 goto e_src;
2350 src.address += CCP_ECC_OPERAND_SIZE;
2351
2352 if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT) {
2353 /* Copy the scalar value */
2354 ret = ccp_reverse_set_dm_area(&src, 0,
2355 ecc->u.pm.scalar, 0,
2356 ecc->u.pm.scalar_len);
2357 if (ret)
2358 goto e_src;
2359 src.address += CCP_ECC_OPERAND_SIZE;
2360 }
2361 }
2362
2363 /* Restore the workarea address */
2364 src.address = save;
2365
2366 /* Prepare the output area for the operation */
2367 ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
2368 DMA_FROM_DEVICE);
2369 if (ret)
2370 goto e_src;
2371
2372 op.soc = 1;
2373 op.src.u.dma.address = src.dma.address;
2374 op.src.u.dma.offset = 0;
2375 op.src.u.dma.length = src.length;
2376 op.dst.u.dma.address = dst.dma.address;
2377 op.dst.u.dma.offset = 0;
2378 op.dst.u.dma.length = dst.length;
2379
2380 op.u.ecc.function = cmd->u.ecc.function;
2381
2382 ret = cmd_q->ccp->vdata->perform->ecc(&op);
2383 if (ret) {
2384 cmd->engine_error = cmd_q->cmd_error;
2385 goto e_dst;
2386 }
2387
2388 ecc->ecc_result = le16_to_cpup(
2389 (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
2390 if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
2391 ret = -EIO;
2392 goto e_dst;
2393 }
2394
2395 /* Save the workarea address since it is updated as we walk through
2396 * to copy the point math result
2397 */
2398 save = dst.address;
2399
2400 /* Save the ECC result X and Y coordinates */
2401 ccp_reverse_get_dm_area(&dst, 0, ecc->u.pm.result.x, 0,
2402 CCP_ECC_MODULUS_BYTES);
2403 dst.address += CCP_ECC_OUTPUT_SIZE;
2404 ccp_reverse_get_dm_area(&dst, 0, ecc->u.pm.result.y, 0,
2405 CCP_ECC_MODULUS_BYTES);
2406 dst.address += CCP_ECC_OUTPUT_SIZE;
2407
2408 /* Restore the workarea address */
2409 dst.address = save;
2410
2411e_dst:
2412 ccp_dm_free(&dst);
2413
2414e_src:
2415 ccp_dm_free(&src);
2416
2417 return ret;
2418}
2419
2420static noinline_for_stack int
2421ccp_run_ecc_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
2422{
2423 struct ccp_ecc_engine *ecc = &cmd->u.ecc;
2424
2425 ecc->ecc_result = 0;
2426
2427 if (!ecc->mod ||
2428 (ecc->mod_len > CCP_ECC_MODULUS_BYTES))
2429 return -EINVAL;
2430
2431 switch (ecc->function) {
2432 case CCP_ECC_FUNCTION_MMUL_384BIT:
2433 case CCP_ECC_FUNCTION_MADD_384BIT:
2434 case CCP_ECC_FUNCTION_MINV_384BIT:
2435 return ccp_run_ecc_mm_cmd(cmd_q, cmd);
2436
2437 case CCP_ECC_FUNCTION_PADD_384BIT:
2438 case CCP_ECC_FUNCTION_PMUL_384BIT:
2439 case CCP_ECC_FUNCTION_PDBL_384BIT:
2440 return ccp_run_ecc_pm_cmd(cmd_q, cmd);
2441
2442 default:
2443 return -EINVAL;
2444 }
2445}
2446
2447int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
2448{
2449 int ret;
2450
2451 cmd->engine_error = 0;
2452 cmd_q->cmd_error = 0;
2453 cmd_q->int_rcvd = 0;
2454 cmd_q->free_slots = cmd_q->ccp->vdata->perform->get_free_slots(cmd_q);
2455
2456 switch (cmd->engine) {
2457 case CCP_ENGINE_AES:
2458 switch (cmd->u.aes.mode) {
2459 case CCP_AES_MODE_CMAC:
2460 ret = ccp_run_aes_cmac_cmd(cmd_q, cmd);
2461 break;
2462 case CCP_AES_MODE_GCM:
2463 ret = ccp_run_aes_gcm_cmd(cmd_q, cmd);
2464 break;
2465 default:
2466 ret = ccp_run_aes_cmd(cmd_q, cmd);
2467 break;
2468 }
2469 break;
2470 case CCP_ENGINE_XTS_AES_128:
2471 ret = ccp_run_xts_aes_cmd(cmd_q, cmd);
2472 break;
2473 case CCP_ENGINE_DES3:
2474 ret = ccp_run_des3_cmd(cmd_q, cmd);
2475 break;
2476 case CCP_ENGINE_SHA:
2477 ret = ccp_run_sha_cmd(cmd_q, cmd);
2478 break;
2479 case CCP_ENGINE_RSA:
2480 ret = ccp_run_rsa_cmd(cmd_q, cmd);
2481 break;
2482 case CCP_ENGINE_PASSTHRU:
2483 if (cmd->flags & CCP_CMD_PASSTHRU_NO_DMA_MAP)
2484 ret = ccp_run_passthru_nomap_cmd(cmd_q, cmd);
2485 else
2486 ret = ccp_run_passthru_cmd(cmd_q, cmd);
2487 break;
2488 case CCP_ENGINE_ECC:
2489 ret = ccp_run_ecc_cmd(cmd_q, cmd);
2490 break;
2491 default:
2492 ret = -EINVAL;
2493 }
2494
2495 return ret;
2496}