blob: 10e3be8d93ced233ff25272eb77053690b46513c [file] [log] [blame]
Tom Lendacky63b94502013-11-12 11:46:16 -06001/*
2 * AMD Cryptographic Coprocessor (CCP) driver
3 *
Gary R Hook68cc6522017-07-17 15:00:49 -05004 * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
Tom Lendacky63b94502013-11-12 11:46:16 -06005 *
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
Gary R Hooka43eb982016-07-26 19:09:31 -05007 * Author: Gary R Hook <gary.hook@amd.com>
Tom Lendacky63b94502013-11-12 11:46:16 -06008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/pci.h>
Tom Lendacky63b94502013-11-12 11:46:16 -060017#include <linux/interrupt.h>
Tom Lendacky63b94502013-11-12 11:46:16 -060018#include <crypto/scatterwalk.h>
Gary R Hook990672d2017-03-15 13:20:52 -050019#include <crypto/des.h>
Gary R Hookea0375a2016-03-01 13:49:25 -060020#include <linux/ccp.h>
Tom Lendacky63b94502013-11-12 11:46:16 -060021
22#include "ccp-dev.h"
23
Tom Lendackyc11baa02014-01-24 16:18:02 -060024/* SHA initial context values */
Gary R Hook4b394a22016-07-26 19:10:21 -050025static const __be32 ccp_sha1_init[SHA1_DIGEST_SIZE / sizeof(__be32)] = {
Tom Lendackyc11baa02014-01-24 16:18:02 -060026 cpu_to_be32(SHA1_H0), cpu_to_be32(SHA1_H1),
27 cpu_to_be32(SHA1_H2), cpu_to_be32(SHA1_H3),
Gary R Hook4b394a22016-07-26 19:10:21 -050028 cpu_to_be32(SHA1_H4),
Tom Lendackyc11baa02014-01-24 16:18:02 -060029};
30
Gary R Hook4b394a22016-07-26 19:10:21 -050031static const __be32 ccp_sha224_init[SHA256_DIGEST_SIZE / sizeof(__be32)] = {
Tom Lendackyc11baa02014-01-24 16:18:02 -060032 cpu_to_be32(SHA224_H0), cpu_to_be32(SHA224_H1),
33 cpu_to_be32(SHA224_H2), cpu_to_be32(SHA224_H3),
34 cpu_to_be32(SHA224_H4), cpu_to_be32(SHA224_H5),
35 cpu_to_be32(SHA224_H6), cpu_to_be32(SHA224_H7),
36};
37
Gary R Hook4b394a22016-07-26 19:10:21 -050038static const __be32 ccp_sha256_init[SHA256_DIGEST_SIZE / sizeof(__be32)] = {
Tom Lendackyc11baa02014-01-24 16:18:02 -060039 cpu_to_be32(SHA256_H0), cpu_to_be32(SHA256_H1),
40 cpu_to_be32(SHA256_H2), cpu_to_be32(SHA256_H3),
41 cpu_to_be32(SHA256_H4), cpu_to_be32(SHA256_H5),
42 cpu_to_be32(SHA256_H6), cpu_to_be32(SHA256_H7),
43};
44
Gary R Hookccebcf32017-03-15 13:20:43 -050045static const __be64 ccp_sha384_init[SHA512_DIGEST_SIZE / sizeof(__be64)] = {
46 cpu_to_be64(SHA384_H0), cpu_to_be64(SHA384_H1),
47 cpu_to_be64(SHA384_H2), cpu_to_be64(SHA384_H3),
48 cpu_to_be64(SHA384_H4), cpu_to_be64(SHA384_H5),
49 cpu_to_be64(SHA384_H6), cpu_to_be64(SHA384_H7),
50};
51
52static const __be64 ccp_sha512_init[SHA512_DIGEST_SIZE / sizeof(__be64)] = {
53 cpu_to_be64(SHA512_H0), cpu_to_be64(SHA512_H1),
54 cpu_to_be64(SHA512_H2), cpu_to_be64(SHA512_H3),
55 cpu_to_be64(SHA512_H4), cpu_to_be64(SHA512_H5),
56 cpu_to_be64(SHA512_H6), cpu_to_be64(SHA512_H7),
57};
58
Gary R Hook4b394a22016-07-26 19:10:21 -050059#define CCP_NEW_JOBID(ccp) ((ccp->vdata->version == CCP_VERSION(3, 0)) ? \
60 ccp_gen_jobid(ccp) : 0)
61
Tom Lendacky63b94502013-11-12 11:46:16 -060062static u32 ccp_gen_jobid(struct ccp_device *ccp)
63{
64 return atomic_inc_return(&ccp->current_id) & CCP_JOBID_MASK;
65}
66
67static void ccp_sg_free(struct ccp_sg_workarea *wa)
68{
69 if (wa->dma_count)
70 dma_unmap_sg(wa->dma_dev, wa->dma_sg, wa->nents, wa->dma_dir);
71
72 wa->dma_count = 0;
73}
74
75static int ccp_init_sg_workarea(struct ccp_sg_workarea *wa, struct device *dev,
Tom Lendacky81a59f02014-01-06 13:34:17 -060076 struct scatterlist *sg, u64 len,
Tom Lendacky63b94502013-11-12 11:46:16 -060077 enum dma_data_direction dma_dir)
78{
79 memset(wa, 0, sizeof(*wa));
80
81 wa->sg = sg;
82 if (!sg)
83 return 0;
84
Tom Lendackyfb43f692015-06-01 11:15:53 -050085 wa->nents = sg_nents_for_len(sg, len);
86 if (wa->nents < 0)
87 return wa->nents;
88
Tom Lendacky63b94502013-11-12 11:46:16 -060089 wa->bytes_left = len;
90 wa->sg_used = 0;
91
92 if (len == 0)
93 return 0;
94
95 if (dma_dir == DMA_NONE)
96 return 0;
97
98 wa->dma_sg = sg;
99 wa->dma_dev = dev;
100 wa->dma_dir = dma_dir;
101 wa->dma_count = dma_map_sg(dev, sg, wa->nents, dma_dir);
102 if (!wa->dma_count)
103 return -ENOMEM;
104
Tom Lendacky63b94502013-11-12 11:46:16 -0600105 return 0;
106}
107
108static void ccp_update_sg_workarea(struct ccp_sg_workarea *wa, unsigned int len)
109{
Tom Lendacky81a59f02014-01-06 13:34:17 -0600110 unsigned int nbytes = min_t(u64, len, wa->bytes_left);
Tom Lendacky63b94502013-11-12 11:46:16 -0600111
112 if (!wa->sg)
113 return;
114
115 wa->sg_used += nbytes;
116 wa->bytes_left -= nbytes;
117 if (wa->sg_used == wa->sg->length) {
118 wa->sg = sg_next(wa->sg);
119 wa->sg_used = 0;
120 }
121}
122
123static void ccp_dm_free(struct ccp_dm_workarea *wa)
124{
125 if (wa->length <= CCP_DMAPOOL_MAX_SIZE) {
126 if (wa->address)
127 dma_pool_free(wa->dma_pool, wa->address,
128 wa->dma.address);
129 } else {
130 if (wa->dma.address)
131 dma_unmap_single(wa->dev, wa->dma.address, wa->length,
132 wa->dma.dir);
133 kfree(wa->address);
134 }
135
136 wa->address = NULL;
137 wa->dma.address = 0;
138}
139
140static int ccp_init_dm_workarea(struct ccp_dm_workarea *wa,
141 struct ccp_cmd_queue *cmd_q,
142 unsigned int len,
143 enum dma_data_direction dir)
144{
145 memset(wa, 0, sizeof(*wa));
146
147 if (!len)
148 return 0;
149
150 wa->dev = cmd_q->ccp->dev;
151 wa->length = len;
152
153 if (len <= CCP_DMAPOOL_MAX_SIZE) {
154 wa->dma_pool = cmd_q->dma_pool;
155
156 wa->address = dma_pool_alloc(wa->dma_pool, GFP_KERNEL,
157 &wa->dma.address);
158 if (!wa->address)
159 return -ENOMEM;
160
161 wa->dma.length = CCP_DMAPOOL_MAX_SIZE;
162
163 memset(wa->address, 0, CCP_DMAPOOL_MAX_SIZE);
164 } else {
165 wa->address = kzalloc(len, GFP_KERNEL);
166 if (!wa->address)
167 return -ENOMEM;
168
169 wa->dma.address = dma_map_single(wa->dev, wa->address, len,
170 dir);
171 if (!wa->dma.address)
172 return -ENOMEM;
173
174 wa->dma.length = len;
175 }
176 wa->dma.dir = dir;
177
178 return 0;
179}
180
181static void ccp_set_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
182 struct scatterlist *sg, unsigned int sg_offset,
183 unsigned int len)
184{
185 WARN_ON(!wa->address);
186
187 scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
188 0);
189}
190
191static void ccp_get_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
192 struct scatterlist *sg, unsigned int sg_offset,
193 unsigned int len)
194{
195 WARN_ON(!wa->address);
196
197 scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
198 1);
199}
200
Tom Lendacky355eba52015-10-01 16:32:31 -0500201static int ccp_reverse_set_dm_area(struct ccp_dm_workarea *wa,
Gary R Hook83d650a2017-02-09 15:50:08 -0600202 unsigned int wa_offset,
Tom Lendacky355eba52015-10-01 16:32:31 -0500203 struct scatterlist *sg,
Gary R Hook83d650a2017-02-09 15:50:08 -0600204 unsigned int sg_offset,
205 unsigned int len)
Tom Lendacky63b94502013-11-12 11:46:16 -0600206{
Gary R Hook83d650a2017-02-09 15:50:08 -0600207 u8 *p, *q;
Tom Lendacky63b94502013-11-12 11:46:16 -0600208
Gary R Hook83d650a2017-02-09 15:50:08 -0600209 ccp_set_dm_area(wa, wa_offset, sg, sg_offset, len);
Tom Lendacky63b94502013-11-12 11:46:16 -0600210
Gary R Hook83d650a2017-02-09 15:50:08 -0600211 p = wa->address + wa_offset;
212 q = p + len - 1;
213 while (p < q) {
214 *p = *p ^ *q;
215 *q = *p ^ *q;
216 *p = *p ^ *q;
217 p++;
218 q--;
Tom Lendacky63b94502013-11-12 11:46:16 -0600219 }
Tom Lendacky355eba52015-10-01 16:32:31 -0500220 return 0;
Tom Lendacky63b94502013-11-12 11:46:16 -0600221}
222
223static void ccp_reverse_get_dm_area(struct ccp_dm_workarea *wa,
Gary R Hook83d650a2017-02-09 15:50:08 -0600224 unsigned int wa_offset,
Tom Lendacky63b94502013-11-12 11:46:16 -0600225 struct scatterlist *sg,
Gary R Hook83d650a2017-02-09 15:50:08 -0600226 unsigned int sg_offset,
Tom Lendacky63b94502013-11-12 11:46:16 -0600227 unsigned int len)
228{
Gary R Hook83d650a2017-02-09 15:50:08 -0600229 u8 *p, *q;
Tom Lendacky63b94502013-11-12 11:46:16 -0600230
Gary R Hook83d650a2017-02-09 15:50:08 -0600231 p = wa->address + wa_offset;
232 q = p + len - 1;
233 while (p < q) {
234 *p = *p ^ *q;
235 *q = *p ^ *q;
236 *p = *p ^ *q;
237 p++;
238 q--;
Tom Lendacky63b94502013-11-12 11:46:16 -0600239 }
Gary R Hook83d650a2017-02-09 15:50:08 -0600240
241 ccp_get_dm_area(wa, wa_offset, sg, sg_offset, len);
Tom Lendacky63b94502013-11-12 11:46:16 -0600242}
243
244static void ccp_free_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q)
245{
246 ccp_dm_free(&data->dm_wa);
247 ccp_sg_free(&data->sg_wa);
248}
249
250static int ccp_init_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q,
Tom Lendacky81a59f02014-01-06 13:34:17 -0600251 struct scatterlist *sg, u64 sg_len,
Tom Lendacky63b94502013-11-12 11:46:16 -0600252 unsigned int dm_len,
253 enum dma_data_direction dir)
254{
255 int ret;
256
257 memset(data, 0, sizeof(*data));
258
259 ret = ccp_init_sg_workarea(&data->sg_wa, cmd_q->ccp->dev, sg, sg_len,
260 dir);
261 if (ret)
262 goto e_err;
263
264 ret = ccp_init_dm_workarea(&data->dm_wa, cmd_q, dm_len, dir);
265 if (ret)
266 goto e_err;
267
268 return 0;
269
270e_err:
271 ccp_free_data(data, cmd_q);
272
273 return ret;
274}
275
276static unsigned int ccp_queue_buf(struct ccp_data *data, unsigned int from)
277{
278 struct ccp_sg_workarea *sg_wa = &data->sg_wa;
279 struct ccp_dm_workarea *dm_wa = &data->dm_wa;
280 unsigned int buf_count, nbytes;
281
282 /* Clear the buffer if setting it */
283 if (!from)
284 memset(dm_wa->address, 0, dm_wa->length);
285
286 if (!sg_wa->sg)
287 return 0;
288
Tom Lendacky81a59f02014-01-06 13:34:17 -0600289 /* Perform the copy operation
290 * nbytes will always be <= UINT_MAX because dm_wa->length is
291 * an unsigned int
292 */
293 nbytes = min_t(u64, sg_wa->bytes_left, dm_wa->length);
Tom Lendacky63b94502013-11-12 11:46:16 -0600294 scatterwalk_map_and_copy(dm_wa->address, sg_wa->sg, sg_wa->sg_used,
295 nbytes, from);
296
297 /* Update the structures and generate the count */
298 buf_count = 0;
299 while (sg_wa->bytes_left && (buf_count < dm_wa->length)) {
Tom Lendacky81a59f02014-01-06 13:34:17 -0600300 nbytes = min(sg_wa->sg->length - sg_wa->sg_used,
301 dm_wa->length - buf_count);
302 nbytes = min_t(u64, sg_wa->bytes_left, nbytes);
Tom Lendacky63b94502013-11-12 11:46:16 -0600303
304 buf_count += nbytes;
305 ccp_update_sg_workarea(sg_wa, nbytes);
306 }
307
308 return buf_count;
309}
310
311static unsigned int ccp_fill_queue_buf(struct ccp_data *data)
312{
313 return ccp_queue_buf(data, 0);
314}
315
316static unsigned int ccp_empty_queue_buf(struct ccp_data *data)
317{
318 return ccp_queue_buf(data, 1);
319}
320
321static void ccp_prepare_data(struct ccp_data *src, struct ccp_data *dst,
322 struct ccp_op *op, unsigned int block_size,
323 bool blocksize_op)
324{
325 unsigned int sg_src_len, sg_dst_len, op_len;
326
327 /* The CCP can only DMA from/to one address each per operation. This
328 * requires that we find the smallest DMA area between the source
Tom Lendacky81a59f02014-01-06 13:34:17 -0600329 * and destination. The resulting len values will always be <= UINT_MAX
330 * because the dma length is an unsigned int.
Tom Lendacky63b94502013-11-12 11:46:16 -0600331 */
Tom Lendacky81a59f02014-01-06 13:34:17 -0600332 sg_src_len = sg_dma_len(src->sg_wa.sg) - src->sg_wa.sg_used;
333 sg_src_len = min_t(u64, src->sg_wa.bytes_left, sg_src_len);
Tom Lendacky63b94502013-11-12 11:46:16 -0600334
335 if (dst) {
Tom Lendacky81a59f02014-01-06 13:34:17 -0600336 sg_dst_len = sg_dma_len(dst->sg_wa.sg) - dst->sg_wa.sg_used;
337 sg_dst_len = min_t(u64, src->sg_wa.bytes_left, sg_dst_len);
Tom Lendacky63b94502013-11-12 11:46:16 -0600338 op_len = min(sg_src_len, sg_dst_len);
Tom Lendacky8db88462015-02-03 13:07:05 -0600339 } else {
Tom Lendacky63b94502013-11-12 11:46:16 -0600340 op_len = sg_src_len;
Tom Lendacky8db88462015-02-03 13:07:05 -0600341 }
Tom Lendacky63b94502013-11-12 11:46:16 -0600342
343 /* The data operation length will be at least block_size in length
344 * or the smaller of available sg room remaining for the source or
345 * the destination
346 */
347 op_len = max(op_len, block_size);
348
349 /* Unless we have to buffer data, there's no reason to wait */
350 op->soc = 0;
351
352 if (sg_src_len < block_size) {
353 /* Not enough data in the sg element, so it
354 * needs to be buffered into a blocksize chunk
355 */
356 int cp_len = ccp_fill_queue_buf(src);
357
358 op->soc = 1;
359 op->src.u.dma.address = src->dm_wa.dma.address;
360 op->src.u.dma.offset = 0;
361 op->src.u.dma.length = (blocksize_op) ? block_size : cp_len;
362 } else {
363 /* Enough data in the sg element, but we need to
364 * adjust for any previously copied data
365 */
366 op->src.u.dma.address = sg_dma_address(src->sg_wa.sg);
367 op->src.u.dma.offset = src->sg_wa.sg_used;
368 op->src.u.dma.length = op_len & ~(block_size - 1);
369
370 ccp_update_sg_workarea(&src->sg_wa, op->src.u.dma.length);
371 }
372
373 if (dst) {
374 if (sg_dst_len < block_size) {
375 /* Not enough room in the sg element or we're on the
376 * last piece of data (when using padding), so the
377 * output needs to be buffered into a blocksize chunk
378 */
379 op->soc = 1;
380 op->dst.u.dma.address = dst->dm_wa.dma.address;
381 op->dst.u.dma.offset = 0;
382 op->dst.u.dma.length = op->src.u.dma.length;
383 } else {
384 /* Enough room in the sg element, but we need to
385 * adjust for any previously used area
386 */
387 op->dst.u.dma.address = sg_dma_address(dst->sg_wa.sg);
388 op->dst.u.dma.offset = dst->sg_wa.sg_used;
389 op->dst.u.dma.length = op->src.u.dma.length;
390 }
391 }
392}
393
394static void ccp_process_data(struct ccp_data *src, struct ccp_data *dst,
395 struct ccp_op *op)
396{
397 op->init = 0;
398
399 if (dst) {
400 if (op->dst.u.dma.address == dst->dm_wa.dma.address)
401 ccp_empty_queue_buf(dst);
402 else
403 ccp_update_sg_workarea(&dst->sg_wa,
404 op->dst.u.dma.length);
405 }
406}
407
Gary R Hook956ee212016-07-26 19:09:40 -0500408static int ccp_copy_to_from_sb(struct ccp_cmd_queue *cmd_q,
409 struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
410 u32 byte_swap, bool from)
Tom Lendacky63b94502013-11-12 11:46:16 -0600411{
412 struct ccp_op op;
413
414 memset(&op, 0, sizeof(op));
415
416 op.cmd_q = cmd_q;
417 op.jobid = jobid;
418 op.eom = 1;
419
420 if (from) {
421 op.soc = 1;
Gary R Hook956ee212016-07-26 19:09:40 -0500422 op.src.type = CCP_MEMTYPE_SB;
423 op.src.u.sb = sb;
Tom Lendacky63b94502013-11-12 11:46:16 -0600424 op.dst.type = CCP_MEMTYPE_SYSTEM;
425 op.dst.u.dma.address = wa->dma.address;
426 op.dst.u.dma.length = wa->length;
427 } else {
428 op.src.type = CCP_MEMTYPE_SYSTEM;
429 op.src.u.dma.address = wa->dma.address;
430 op.src.u.dma.length = wa->length;
Gary R Hook956ee212016-07-26 19:09:40 -0500431 op.dst.type = CCP_MEMTYPE_SB;
432 op.dst.u.sb = sb;
Tom Lendacky63b94502013-11-12 11:46:16 -0600433 }
434
435 op.u.passthru.byte_swap = byte_swap;
436
Gary R Hooka43eb982016-07-26 19:09:31 -0500437 return cmd_q->ccp->vdata->perform->passthru(&op);
Tom Lendacky63b94502013-11-12 11:46:16 -0600438}
439
Gary R Hook956ee212016-07-26 19:09:40 -0500440static int ccp_copy_to_sb(struct ccp_cmd_queue *cmd_q,
441 struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
442 u32 byte_swap)
Tom Lendacky63b94502013-11-12 11:46:16 -0600443{
Gary R Hook956ee212016-07-26 19:09:40 -0500444 return ccp_copy_to_from_sb(cmd_q, wa, jobid, sb, byte_swap, false);
Tom Lendacky63b94502013-11-12 11:46:16 -0600445}
446
Gary R Hook956ee212016-07-26 19:09:40 -0500447static int ccp_copy_from_sb(struct ccp_cmd_queue *cmd_q,
448 struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
449 u32 byte_swap)
Tom Lendacky63b94502013-11-12 11:46:16 -0600450{
Gary R Hook956ee212016-07-26 19:09:40 -0500451 return ccp_copy_to_from_sb(cmd_q, wa, jobid, sb, byte_swap, true);
Tom Lendacky63b94502013-11-12 11:46:16 -0600452}
453
454static int ccp_run_aes_cmac_cmd(struct ccp_cmd_queue *cmd_q,
455 struct ccp_cmd *cmd)
456{
457 struct ccp_aes_engine *aes = &cmd->u.aes;
458 struct ccp_dm_workarea key, ctx;
459 struct ccp_data src;
460 struct ccp_op op;
461 unsigned int dm_offset;
462 int ret;
463
464 if (!((aes->key_len == AES_KEYSIZE_128) ||
465 (aes->key_len == AES_KEYSIZE_192) ||
466 (aes->key_len == AES_KEYSIZE_256)))
467 return -EINVAL;
468
469 if (aes->src_len & (AES_BLOCK_SIZE - 1))
470 return -EINVAL;
471
472 if (aes->iv_len != AES_BLOCK_SIZE)
473 return -EINVAL;
474
475 if (!aes->key || !aes->iv || !aes->src)
476 return -EINVAL;
477
478 if (aes->cmac_final) {
479 if (aes->cmac_key_len != AES_BLOCK_SIZE)
480 return -EINVAL;
481
482 if (!aes->cmac_key)
483 return -EINVAL;
484 }
485
Gary R Hook956ee212016-07-26 19:09:40 -0500486 BUILD_BUG_ON(CCP_AES_KEY_SB_COUNT != 1);
487 BUILD_BUG_ON(CCP_AES_CTX_SB_COUNT != 1);
Tom Lendacky63b94502013-11-12 11:46:16 -0600488
489 ret = -EIO;
490 memset(&op, 0, sizeof(op));
491 op.cmd_q = cmd_q;
Gary R Hook4b394a22016-07-26 19:10:21 -0500492 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
Gary R Hook956ee212016-07-26 19:09:40 -0500493 op.sb_key = cmd_q->sb_key;
494 op.sb_ctx = cmd_q->sb_ctx;
Tom Lendacky63b94502013-11-12 11:46:16 -0600495 op.init = 1;
496 op.u.aes.type = aes->type;
497 op.u.aes.mode = aes->mode;
498 op.u.aes.action = aes->action;
499
Gary R Hook956ee212016-07-26 19:09:40 -0500500 /* All supported key sizes fit in a single (32-byte) SB entry
Tom Lendacky63b94502013-11-12 11:46:16 -0600501 * and must be in little endian format. Use the 256-bit byte
502 * swap passthru option to convert from big endian to little
503 * endian.
504 */
505 ret = ccp_init_dm_workarea(&key, cmd_q,
Gary R Hook956ee212016-07-26 19:09:40 -0500506 CCP_AES_KEY_SB_COUNT * CCP_SB_BYTES,
Tom Lendacky63b94502013-11-12 11:46:16 -0600507 DMA_TO_DEVICE);
508 if (ret)
509 return ret;
510
Gary R Hook956ee212016-07-26 19:09:40 -0500511 dm_offset = CCP_SB_BYTES - aes->key_len;
Tom Lendacky63b94502013-11-12 11:46:16 -0600512 ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
Gary R Hook956ee212016-07-26 19:09:40 -0500513 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
514 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -0600515 if (ret) {
516 cmd->engine_error = cmd_q->cmd_error;
517 goto e_key;
518 }
519
Gary R Hook956ee212016-07-26 19:09:40 -0500520 /* The AES context fits in a single (32-byte) SB entry and
Tom Lendacky63b94502013-11-12 11:46:16 -0600521 * must be in little endian format. Use the 256-bit byte swap
522 * passthru option to convert from big endian to little endian.
523 */
524 ret = ccp_init_dm_workarea(&ctx, cmd_q,
Gary R Hook956ee212016-07-26 19:09:40 -0500525 CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
Tom Lendacky63b94502013-11-12 11:46:16 -0600526 DMA_BIDIRECTIONAL);
527 if (ret)
528 goto e_key;
529
Gary R Hook956ee212016-07-26 19:09:40 -0500530 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
Tom Lendacky63b94502013-11-12 11:46:16 -0600531 ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
Gary R Hook956ee212016-07-26 19:09:40 -0500532 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
533 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -0600534 if (ret) {
535 cmd->engine_error = cmd_q->cmd_error;
536 goto e_ctx;
537 }
538
539 /* Send data to the CCP AES engine */
540 ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
541 AES_BLOCK_SIZE, DMA_TO_DEVICE);
542 if (ret)
543 goto e_ctx;
544
545 while (src.sg_wa.bytes_left) {
546 ccp_prepare_data(&src, NULL, &op, AES_BLOCK_SIZE, true);
547 if (aes->cmac_final && !src.sg_wa.bytes_left) {
548 op.eom = 1;
549
550 /* Push the K1/K2 key to the CCP now */
Gary R Hook956ee212016-07-26 19:09:40 -0500551 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid,
552 op.sb_ctx,
553 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -0600554 if (ret) {
555 cmd->engine_error = cmd_q->cmd_error;
556 goto e_src;
557 }
558
559 ccp_set_dm_area(&ctx, 0, aes->cmac_key, 0,
560 aes->cmac_key_len);
Gary R Hook956ee212016-07-26 19:09:40 -0500561 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
562 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -0600563 if (ret) {
564 cmd->engine_error = cmd_q->cmd_error;
565 goto e_src;
566 }
567 }
568
Gary R Hooka43eb982016-07-26 19:09:31 -0500569 ret = cmd_q->ccp->vdata->perform->aes(&op);
Tom Lendacky63b94502013-11-12 11:46:16 -0600570 if (ret) {
571 cmd->engine_error = cmd_q->cmd_error;
572 goto e_src;
573 }
574
575 ccp_process_data(&src, NULL, &op);
576 }
577
578 /* Retrieve the AES context - convert from LE to BE using
579 * 32-byte (256-bit) byteswapping
580 */
Gary R Hook956ee212016-07-26 19:09:40 -0500581 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
582 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -0600583 if (ret) {
584 cmd->engine_error = cmd_q->cmd_error;
585 goto e_src;
586 }
587
588 /* ...but we only need AES_BLOCK_SIZE bytes */
Gary R Hook956ee212016-07-26 19:09:40 -0500589 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
Tom Lendacky63b94502013-11-12 11:46:16 -0600590 ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
591
592e_src:
593 ccp_free_data(&src, cmd_q);
594
595e_ctx:
596 ccp_dm_free(&ctx);
597
598e_key:
599 ccp_dm_free(&key);
600
601 return ret;
602}
603
Gary R Hook36cf5152017-03-15 13:21:01 -0500604static int ccp_run_aes_gcm_cmd(struct ccp_cmd_queue *cmd_q,
605 struct ccp_cmd *cmd)
606{
607 struct ccp_aes_engine *aes = &cmd->u.aes;
608 struct ccp_dm_workarea key, ctx, final_wa, tag;
609 struct ccp_data src, dst;
610 struct ccp_data aad;
611 struct ccp_op op;
612
613 unsigned long long *final;
614 unsigned int dm_offset;
615 unsigned int ilen;
616 bool in_place = true; /* Default value */
617 int ret;
618
619 struct scatterlist *p_inp, sg_inp[2];
620 struct scatterlist *p_tag, sg_tag[2];
621 struct scatterlist *p_outp, sg_outp[2];
622 struct scatterlist *p_aad;
623
624 if (!aes->iv)
625 return -EINVAL;
626
627 if (!((aes->key_len == AES_KEYSIZE_128) ||
628 (aes->key_len == AES_KEYSIZE_192) ||
629 (aes->key_len == AES_KEYSIZE_256)))
630 return -EINVAL;
631
632 if (!aes->key) /* Gotta have a key SGL */
633 return -EINVAL;
634
635 /* First, decompose the source buffer into AAD & PT,
636 * and the destination buffer into AAD, CT & tag, or
637 * the input into CT & tag.
638 * It is expected that the input and output SGs will
639 * be valid, even if the AAD and input lengths are 0.
640 */
641 p_aad = aes->src;
642 p_inp = scatterwalk_ffwd(sg_inp, aes->src, aes->aad_len);
643 p_outp = scatterwalk_ffwd(sg_outp, aes->dst, aes->aad_len);
644 if (aes->action == CCP_AES_ACTION_ENCRYPT) {
645 ilen = aes->src_len;
646 p_tag = scatterwalk_ffwd(sg_tag, p_outp, ilen);
647 } else {
648 /* Input length for decryption includes tag */
649 ilen = aes->src_len - AES_BLOCK_SIZE;
650 p_tag = scatterwalk_ffwd(sg_tag, p_inp, ilen);
651 }
652
653 memset(&op, 0, sizeof(op));
654 op.cmd_q = cmd_q;
655 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
656 op.sb_key = cmd_q->sb_key; /* Pre-allocated */
657 op.sb_ctx = cmd_q->sb_ctx; /* Pre-allocated */
658 op.init = 1;
659 op.u.aes.type = aes->type;
660
661 /* Copy the key to the LSB */
662 ret = ccp_init_dm_workarea(&key, cmd_q,
663 CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
664 DMA_TO_DEVICE);
665 if (ret)
666 return ret;
667
668 dm_offset = CCP_SB_BYTES - aes->key_len;
669 ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
670 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
671 CCP_PASSTHRU_BYTESWAP_256BIT);
672 if (ret) {
673 cmd->engine_error = cmd_q->cmd_error;
674 goto e_key;
675 }
676
677 /* Copy the context (IV) to the LSB.
678 * There is an assumption here that the IV is 96 bits in length, plus
679 * a nonce of 32 bits. If no IV is present, use a zeroed buffer.
680 */
681 ret = ccp_init_dm_workarea(&ctx, cmd_q,
682 CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
683 DMA_BIDIRECTIONAL);
684 if (ret)
685 goto e_key;
686
687 dm_offset = CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES - aes->iv_len;
688 ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
689
690 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
691 CCP_PASSTHRU_BYTESWAP_256BIT);
692 if (ret) {
693 cmd->engine_error = cmd_q->cmd_error;
694 goto e_ctx;
695 }
696
697 op.init = 1;
698 if (aes->aad_len > 0) {
699 /* Step 1: Run a GHASH over the Additional Authenticated Data */
700 ret = ccp_init_data(&aad, cmd_q, p_aad, aes->aad_len,
701 AES_BLOCK_SIZE,
702 DMA_TO_DEVICE);
703 if (ret)
704 goto e_ctx;
705
706 op.u.aes.mode = CCP_AES_MODE_GHASH;
707 op.u.aes.action = CCP_AES_GHASHAAD;
708
709 while (aad.sg_wa.bytes_left) {
710 ccp_prepare_data(&aad, NULL, &op, AES_BLOCK_SIZE, true);
711
712 ret = cmd_q->ccp->vdata->perform->aes(&op);
713 if (ret) {
714 cmd->engine_error = cmd_q->cmd_error;
715 goto e_aad;
716 }
717
718 ccp_process_data(&aad, NULL, &op);
719 op.init = 0;
720 }
721 }
722
723 op.u.aes.mode = CCP_AES_MODE_GCTR;
724 op.u.aes.action = aes->action;
725
726 if (ilen > 0) {
727 /* Step 2: Run a GCTR over the plaintext */
728 in_place = (sg_virt(p_inp) == sg_virt(p_outp)) ? true : false;
729
730 ret = ccp_init_data(&src, cmd_q, p_inp, ilen,
731 AES_BLOCK_SIZE,
732 in_place ? DMA_BIDIRECTIONAL
733 : DMA_TO_DEVICE);
734 if (ret)
735 goto e_ctx;
736
737 if (in_place) {
738 dst = src;
739 } else {
740 ret = ccp_init_data(&dst, cmd_q, p_outp, ilen,
741 AES_BLOCK_SIZE, DMA_FROM_DEVICE);
742 if (ret)
743 goto e_src;
744 }
745
746 op.soc = 0;
747 op.eom = 0;
748 op.init = 1;
749 while (src.sg_wa.bytes_left) {
750 ccp_prepare_data(&src, &dst, &op, AES_BLOCK_SIZE, true);
751 if (!src.sg_wa.bytes_left) {
752 unsigned int nbytes = aes->src_len
753 % AES_BLOCK_SIZE;
754
755 if (nbytes) {
756 op.eom = 1;
757 op.u.aes.size = (nbytes * 8) - 1;
758 }
759 }
760
761 ret = cmd_q->ccp->vdata->perform->aes(&op);
762 if (ret) {
763 cmd->engine_error = cmd_q->cmd_error;
764 goto e_dst;
765 }
766
767 ccp_process_data(&src, &dst, &op);
768 op.init = 0;
769 }
770 }
771
772 /* Step 3: Update the IV portion of the context with the original IV */
773 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
774 CCP_PASSTHRU_BYTESWAP_256BIT);
775 if (ret) {
776 cmd->engine_error = cmd_q->cmd_error;
777 goto e_dst;
778 }
779
780 ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
781
782 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
783 CCP_PASSTHRU_BYTESWAP_256BIT);
784 if (ret) {
785 cmd->engine_error = cmd_q->cmd_error;
786 goto e_dst;
787 }
788
789 /* Step 4: Concatenate the lengths of the AAD and source, and
790 * hash that 16 byte buffer.
791 */
792 ret = ccp_init_dm_workarea(&final_wa, cmd_q, AES_BLOCK_SIZE,
793 DMA_BIDIRECTIONAL);
794 if (ret)
795 goto e_dst;
796 final = (unsigned long long *) final_wa.address;
797 final[0] = cpu_to_be64(aes->aad_len * 8);
798 final[1] = cpu_to_be64(ilen * 8);
799
800 op.u.aes.mode = CCP_AES_MODE_GHASH;
801 op.u.aes.action = CCP_AES_GHASHFINAL;
802 op.src.type = CCP_MEMTYPE_SYSTEM;
803 op.src.u.dma.address = final_wa.dma.address;
804 op.src.u.dma.length = AES_BLOCK_SIZE;
805 op.dst.type = CCP_MEMTYPE_SYSTEM;
806 op.dst.u.dma.address = final_wa.dma.address;
807 op.dst.u.dma.length = AES_BLOCK_SIZE;
808 op.eom = 1;
809 op.u.aes.size = 0;
810 ret = cmd_q->ccp->vdata->perform->aes(&op);
811 if (ret)
812 goto e_dst;
813
814 if (aes->action == CCP_AES_ACTION_ENCRYPT) {
815 /* Put the ciphered tag after the ciphertext. */
816 ccp_get_dm_area(&final_wa, 0, p_tag, 0, AES_BLOCK_SIZE);
817 } else {
818 /* Does this ciphered tag match the input? */
819 ret = ccp_init_dm_workarea(&tag, cmd_q, AES_BLOCK_SIZE,
820 DMA_BIDIRECTIONAL);
821 if (ret)
822 goto e_tag;
823 ccp_set_dm_area(&tag, 0, p_tag, 0, AES_BLOCK_SIZE);
824
825 ret = memcmp(tag.address, final_wa.address, AES_BLOCK_SIZE);
826 ccp_dm_free(&tag);
827 }
828
829e_tag:
830 ccp_dm_free(&final_wa);
831
832e_dst:
833 if (aes->src_len && !in_place)
834 ccp_free_data(&dst, cmd_q);
835
836e_src:
837 if (aes->src_len)
838 ccp_free_data(&src, cmd_q);
839
840e_aad:
841 if (aes->aad_len)
842 ccp_free_data(&aad, cmd_q);
843
844e_ctx:
845 ccp_dm_free(&ctx);
846
847e_key:
848 ccp_dm_free(&key);
849
850 return ret;
851}
852
Tom Lendacky63b94502013-11-12 11:46:16 -0600853static int ccp_run_aes_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
854{
855 struct ccp_aes_engine *aes = &cmd->u.aes;
856 struct ccp_dm_workarea key, ctx;
857 struct ccp_data src, dst;
858 struct ccp_op op;
859 unsigned int dm_offset;
860 bool in_place = false;
861 int ret;
862
863 if (aes->mode == CCP_AES_MODE_CMAC)
864 return ccp_run_aes_cmac_cmd(cmd_q, cmd);
865
Gary R Hook36cf5152017-03-15 13:21:01 -0500866 if (aes->mode == CCP_AES_MODE_GCM)
867 return ccp_run_aes_gcm_cmd(cmd_q, cmd);
868
Tom Lendacky63b94502013-11-12 11:46:16 -0600869 if (!((aes->key_len == AES_KEYSIZE_128) ||
870 (aes->key_len == AES_KEYSIZE_192) ||
871 (aes->key_len == AES_KEYSIZE_256)))
872 return -EINVAL;
873
874 if (((aes->mode == CCP_AES_MODE_ECB) ||
875 (aes->mode == CCP_AES_MODE_CBC) ||
876 (aes->mode == CCP_AES_MODE_CFB)) &&
877 (aes->src_len & (AES_BLOCK_SIZE - 1)))
878 return -EINVAL;
879
880 if (!aes->key || !aes->src || !aes->dst)
881 return -EINVAL;
882
883 if (aes->mode != CCP_AES_MODE_ECB) {
884 if (aes->iv_len != AES_BLOCK_SIZE)
885 return -EINVAL;
886
887 if (!aes->iv)
888 return -EINVAL;
889 }
890
Gary R Hook956ee212016-07-26 19:09:40 -0500891 BUILD_BUG_ON(CCP_AES_KEY_SB_COUNT != 1);
892 BUILD_BUG_ON(CCP_AES_CTX_SB_COUNT != 1);
Tom Lendacky63b94502013-11-12 11:46:16 -0600893
894 ret = -EIO;
895 memset(&op, 0, sizeof(op));
896 op.cmd_q = cmd_q;
Gary R Hook4b394a22016-07-26 19:10:21 -0500897 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
Gary R Hook956ee212016-07-26 19:09:40 -0500898 op.sb_key = cmd_q->sb_key;
899 op.sb_ctx = cmd_q->sb_ctx;
Tom Lendacky63b94502013-11-12 11:46:16 -0600900 op.init = (aes->mode == CCP_AES_MODE_ECB) ? 0 : 1;
901 op.u.aes.type = aes->type;
902 op.u.aes.mode = aes->mode;
903 op.u.aes.action = aes->action;
904
Gary R Hook956ee212016-07-26 19:09:40 -0500905 /* All supported key sizes fit in a single (32-byte) SB entry
Tom Lendacky63b94502013-11-12 11:46:16 -0600906 * and must be in little endian format. Use the 256-bit byte
907 * swap passthru option to convert from big endian to little
908 * endian.
909 */
910 ret = ccp_init_dm_workarea(&key, cmd_q,
Gary R Hook956ee212016-07-26 19:09:40 -0500911 CCP_AES_KEY_SB_COUNT * CCP_SB_BYTES,
Tom Lendacky63b94502013-11-12 11:46:16 -0600912 DMA_TO_DEVICE);
913 if (ret)
914 return ret;
915
Gary R Hook956ee212016-07-26 19:09:40 -0500916 dm_offset = CCP_SB_BYTES - aes->key_len;
Tom Lendacky63b94502013-11-12 11:46:16 -0600917 ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
Gary R Hook956ee212016-07-26 19:09:40 -0500918 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
919 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -0600920 if (ret) {
921 cmd->engine_error = cmd_q->cmd_error;
922 goto e_key;
923 }
924
Gary R Hook956ee212016-07-26 19:09:40 -0500925 /* The AES context fits in a single (32-byte) SB entry and
Tom Lendacky63b94502013-11-12 11:46:16 -0600926 * must be in little endian format. Use the 256-bit byte swap
927 * passthru option to convert from big endian to little endian.
928 */
929 ret = ccp_init_dm_workarea(&ctx, cmd_q,
Gary R Hook956ee212016-07-26 19:09:40 -0500930 CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
Tom Lendacky63b94502013-11-12 11:46:16 -0600931 DMA_BIDIRECTIONAL);
932 if (ret)
933 goto e_key;
934
935 if (aes->mode != CCP_AES_MODE_ECB) {
Gary R Hook4b394a22016-07-26 19:10:21 -0500936 /* Load the AES context - convert to LE */
Gary R Hook956ee212016-07-26 19:09:40 -0500937 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
Tom Lendacky63b94502013-11-12 11:46:16 -0600938 ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
Gary R Hook956ee212016-07-26 19:09:40 -0500939 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
940 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -0600941 if (ret) {
942 cmd->engine_error = cmd_q->cmd_error;
943 goto e_ctx;
944 }
945 }
Gary R Hookf7cc02b32017-02-08 13:07:06 -0600946 switch (aes->mode) {
947 case CCP_AES_MODE_CFB: /* CFB128 only */
948 case CCP_AES_MODE_CTR:
949 op.u.aes.size = AES_BLOCK_SIZE * BITS_PER_BYTE - 1;
950 break;
951 default:
952 op.u.aes.size = 0;
953 }
Tom Lendacky63b94502013-11-12 11:46:16 -0600954
955 /* Prepare the input and output data workareas. For in-place
956 * operations we need to set the dma direction to BIDIRECTIONAL
957 * and copy the src workarea to the dst workarea.
958 */
959 if (sg_virt(aes->src) == sg_virt(aes->dst))
960 in_place = true;
961
962 ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
963 AES_BLOCK_SIZE,
964 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
965 if (ret)
966 goto e_ctx;
967
Tom Lendacky8db88462015-02-03 13:07:05 -0600968 if (in_place) {
Tom Lendacky63b94502013-11-12 11:46:16 -0600969 dst = src;
Tom Lendacky8db88462015-02-03 13:07:05 -0600970 } else {
Tom Lendacky63b94502013-11-12 11:46:16 -0600971 ret = ccp_init_data(&dst, cmd_q, aes->dst, aes->src_len,
972 AES_BLOCK_SIZE, DMA_FROM_DEVICE);
973 if (ret)
974 goto e_src;
975 }
976
977 /* Send data to the CCP AES engine */
978 while (src.sg_wa.bytes_left) {
979 ccp_prepare_data(&src, &dst, &op, AES_BLOCK_SIZE, true);
980 if (!src.sg_wa.bytes_left) {
981 op.eom = 1;
982
983 /* Since we don't retrieve the AES context in ECB
984 * mode we have to wait for the operation to complete
985 * on the last piece of data
986 */
987 if (aes->mode == CCP_AES_MODE_ECB)
988 op.soc = 1;
989 }
990
Gary R Hooka43eb982016-07-26 19:09:31 -0500991 ret = cmd_q->ccp->vdata->perform->aes(&op);
Tom Lendacky63b94502013-11-12 11:46:16 -0600992 if (ret) {
993 cmd->engine_error = cmd_q->cmd_error;
994 goto e_dst;
995 }
996
997 ccp_process_data(&src, &dst, &op);
998 }
999
1000 if (aes->mode != CCP_AES_MODE_ECB) {
1001 /* Retrieve the AES context - convert from LE to BE using
1002 * 32-byte (256-bit) byteswapping
1003 */
Gary R Hook956ee212016-07-26 19:09:40 -05001004 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1005 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -06001006 if (ret) {
1007 cmd->engine_error = cmd_q->cmd_error;
1008 goto e_dst;
1009 }
1010
1011 /* ...but we only need AES_BLOCK_SIZE bytes */
Gary R Hook956ee212016-07-26 19:09:40 -05001012 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
Tom Lendacky63b94502013-11-12 11:46:16 -06001013 ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
1014 }
1015
1016e_dst:
1017 if (!in_place)
1018 ccp_free_data(&dst, cmd_q);
1019
1020e_src:
1021 ccp_free_data(&src, cmd_q);
1022
1023e_ctx:
1024 ccp_dm_free(&ctx);
1025
1026e_key:
1027 ccp_dm_free(&key);
1028
1029 return ret;
1030}
1031
1032static int ccp_run_xts_aes_cmd(struct ccp_cmd_queue *cmd_q,
1033 struct ccp_cmd *cmd)
1034{
1035 struct ccp_xts_aes_engine *xts = &cmd->u.xts;
1036 struct ccp_dm_workarea key, ctx;
1037 struct ccp_data src, dst;
1038 struct ccp_op op;
1039 unsigned int unit_size, dm_offset;
1040 bool in_place = false;
Gary R Hooke6523992017-07-25 14:12:11 -05001041 unsigned int sb_count;
1042 enum ccp_aes_type aestype;
Tom Lendacky63b94502013-11-12 11:46:16 -06001043 int ret;
1044
1045 switch (xts->unit_size) {
1046 case CCP_XTS_AES_UNIT_SIZE_16:
1047 unit_size = 16;
1048 break;
1049 case CCP_XTS_AES_UNIT_SIZE_512:
1050 unit_size = 512;
1051 break;
1052 case CCP_XTS_AES_UNIT_SIZE_1024:
1053 unit_size = 1024;
1054 break;
1055 case CCP_XTS_AES_UNIT_SIZE_2048:
1056 unit_size = 2048;
1057 break;
1058 case CCP_XTS_AES_UNIT_SIZE_4096:
1059 unit_size = 4096;
1060 break;
1061
1062 default:
1063 return -EINVAL;
1064 }
1065
Gary R Hooke6523992017-07-25 14:12:11 -05001066 if (xts->key_len == AES_KEYSIZE_128)
1067 aestype = CCP_AES_TYPE_128;
1068 else
Tom Lendacky63b94502013-11-12 11:46:16 -06001069 return -EINVAL;
1070
1071 if (!xts->final && (xts->src_len & (AES_BLOCK_SIZE - 1)))
1072 return -EINVAL;
1073
1074 if (xts->iv_len != AES_BLOCK_SIZE)
1075 return -EINVAL;
1076
1077 if (!xts->key || !xts->iv || !xts->src || !xts->dst)
1078 return -EINVAL;
1079
Gary R Hook956ee212016-07-26 19:09:40 -05001080 BUILD_BUG_ON(CCP_XTS_AES_KEY_SB_COUNT != 1);
1081 BUILD_BUG_ON(CCP_XTS_AES_CTX_SB_COUNT != 1);
Tom Lendacky63b94502013-11-12 11:46:16 -06001082
1083 ret = -EIO;
1084 memset(&op, 0, sizeof(op));
1085 op.cmd_q = cmd_q;
Gary R Hook4b394a22016-07-26 19:10:21 -05001086 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
Gary R Hook956ee212016-07-26 19:09:40 -05001087 op.sb_key = cmd_q->sb_key;
1088 op.sb_ctx = cmd_q->sb_ctx;
Tom Lendacky63b94502013-11-12 11:46:16 -06001089 op.init = 1;
Gary R Hooke6523992017-07-25 14:12:11 -05001090 op.u.xts.type = aestype;
Tom Lendacky63b94502013-11-12 11:46:16 -06001091 op.u.xts.action = xts->action;
1092 op.u.xts.unit_size = xts->unit_size;
1093
Gary R Hooke6523992017-07-25 14:12:11 -05001094 /* A version 3 device only supports 128-bit keys, which fits into a
1095 * single SB entry. A version 5 device uses a 512-bit vector, so two
1096 * SB entries.
Tom Lendacky63b94502013-11-12 11:46:16 -06001097 */
Gary R Hooke6523992017-07-25 14:12:11 -05001098 if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0))
1099 sb_count = CCP_XTS_AES_KEY_SB_COUNT;
1100 else
1101 sb_count = CCP5_XTS_AES_KEY_SB_COUNT;
Tom Lendacky63b94502013-11-12 11:46:16 -06001102 ret = ccp_init_dm_workarea(&key, cmd_q,
Gary R Hooke6523992017-07-25 14:12:11 -05001103 sb_count * CCP_SB_BYTES,
Tom Lendacky63b94502013-11-12 11:46:16 -06001104 DMA_TO_DEVICE);
1105 if (ret)
1106 return ret;
1107
Gary R Hooke6523992017-07-25 14:12:11 -05001108 if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) {
1109 /* All supported key sizes must be in little endian format.
1110 * Use the 256-bit byte swap passthru option to convert from
1111 * big endian to little endian.
1112 */
1113 dm_offset = CCP_SB_BYTES - AES_KEYSIZE_128;
1114 ccp_set_dm_area(&key, dm_offset, xts->key, 0, xts->key_len);
1115 ccp_set_dm_area(&key, 0, xts->key, xts->key_len, xts->key_len);
1116 } else {
1117 /* Version 5 CCPs use a 512-bit space for the key: each portion
1118 * occupies 256 bits, or one entire slot, and is zero-padded.
1119 */
1120 unsigned int pad;
1121
1122 dm_offset = CCP_SB_BYTES;
1123 pad = dm_offset - xts->key_len;
1124 ccp_set_dm_area(&key, pad, xts->key, 0, xts->key_len);
1125 ccp_set_dm_area(&key, dm_offset + pad, xts->key, xts->key_len,
1126 xts->key_len);
1127 }
Gary R Hook956ee212016-07-26 19:09:40 -05001128 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
1129 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -06001130 if (ret) {
1131 cmd->engine_error = cmd_q->cmd_error;
1132 goto e_key;
1133 }
1134
Gary R Hook956ee212016-07-26 19:09:40 -05001135 /* The AES context fits in a single (32-byte) SB entry and
Tom Lendacky63b94502013-11-12 11:46:16 -06001136 * for XTS is already in little endian format so no byte swapping
1137 * is needed.
1138 */
1139 ret = ccp_init_dm_workarea(&ctx, cmd_q,
Gary R Hook956ee212016-07-26 19:09:40 -05001140 CCP_XTS_AES_CTX_SB_COUNT * CCP_SB_BYTES,
Tom Lendacky63b94502013-11-12 11:46:16 -06001141 DMA_BIDIRECTIONAL);
1142 if (ret)
1143 goto e_key;
1144
1145 ccp_set_dm_area(&ctx, 0, xts->iv, 0, xts->iv_len);
Gary R Hook956ee212016-07-26 19:09:40 -05001146 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1147 CCP_PASSTHRU_BYTESWAP_NOOP);
Tom Lendacky63b94502013-11-12 11:46:16 -06001148 if (ret) {
1149 cmd->engine_error = cmd_q->cmd_error;
1150 goto e_ctx;
1151 }
1152
1153 /* Prepare the input and output data workareas. For in-place
1154 * operations we need to set the dma direction to BIDIRECTIONAL
1155 * and copy the src workarea to the dst workarea.
1156 */
1157 if (sg_virt(xts->src) == sg_virt(xts->dst))
1158 in_place = true;
1159
1160 ret = ccp_init_data(&src, cmd_q, xts->src, xts->src_len,
1161 unit_size,
1162 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1163 if (ret)
1164 goto e_ctx;
1165
Tom Lendacky8db88462015-02-03 13:07:05 -06001166 if (in_place) {
Tom Lendacky63b94502013-11-12 11:46:16 -06001167 dst = src;
Tom Lendacky8db88462015-02-03 13:07:05 -06001168 } else {
Tom Lendacky63b94502013-11-12 11:46:16 -06001169 ret = ccp_init_data(&dst, cmd_q, xts->dst, xts->src_len,
1170 unit_size, DMA_FROM_DEVICE);
1171 if (ret)
1172 goto e_src;
1173 }
1174
1175 /* Send data to the CCP AES engine */
1176 while (src.sg_wa.bytes_left) {
1177 ccp_prepare_data(&src, &dst, &op, unit_size, true);
1178 if (!src.sg_wa.bytes_left)
1179 op.eom = 1;
1180
Gary R Hooka43eb982016-07-26 19:09:31 -05001181 ret = cmd_q->ccp->vdata->perform->xts_aes(&op);
Tom Lendacky63b94502013-11-12 11:46:16 -06001182 if (ret) {
1183 cmd->engine_error = cmd_q->cmd_error;
1184 goto e_dst;
1185 }
1186
1187 ccp_process_data(&src, &dst, &op);
1188 }
1189
1190 /* Retrieve the AES context - convert from LE to BE using
1191 * 32-byte (256-bit) byteswapping
1192 */
Gary R Hook956ee212016-07-26 19:09:40 -05001193 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1194 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -06001195 if (ret) {
1196 cmd->engine_error = cmd_q->cmd_error;
1197 goto e_dst;
1198 }
1199
1200 /* ...but we only need AES_BLOCK_SIZE bytes */
Gary R Hook956ee212016-07-26 19:09:40 -05001201 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
Tom Lendacky63b94502013-11-12 11:46:16 -06001202 ccp_get_dm_area(&ctx, dm_offset, xts->iv, 0, xts->iv_len);
1203
1204e_dst:
1205 if (!in_place)
1206 ccp_free_data(&dst, cmd_q);
1207
1208e_src:
1209 ccp_free_data(&src, cmd_q);
1210
1211e_ctx:
1212 ccp_dm_free(&ctx);
1213
1214e_key:
1215 ccp_dm_free(&key);
1216
1217 return ret;
1218}
1219
Gary R Hook990672d2017-03-15 13:20:52 -05001220static int ccp_run_des3_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1221{
1222 struct ccp_des3_engine *des3 = &cmd->u.des3;
1223
1224 struct ccp_dm_workarea key, ctx;
1225 struct ccp_data src, dst;
1226 struct ccp_op op;
1227 unsigned int dm_offset;
1228 unsigned int len_singlekey;
1229 bool in_place = false;
1230 int ret;
1231
1232 /* Error checks */
1233 if (!cmd_q->ccp->vdata->perform->des3)
1234 return -EINVAL;
1235
1236 if (des3->key_len != DES3_EDE_KEY_SIZE)
1237 return -EINVAL;
1238
1239 if (((des3->mode == CCP_DES3_MODE_ECB) ||
1240 (des3->mode == CCP_DES3_MODE_CBC)) &&
1241 (des3->src_len & (DES3_EDE_BLOCK_SIZE - 1)))
1242 return -EINVAL;
1243
1244 if (!des3->key || !des3->src || !des3->dst)
1245 return -EINVAL;
1246
1247 if (des3->mode != CCP_DES3_MODE_ECB) {
1248 if (des3->iv_len != DES3_EDE_BLOCK_SIZE)
1249 return -EINVAL;
1250
1251 if (!des3->iv)
1252 return -EINVAL;
1253 }
1254
1255 ret = -EIO;
1256 /* Zero out all the fields of the command desc */
1257 memset(&op, 0, sizeof(op));
1258
1259 /* Set up the Function field */
1260 op.cmd_q = cmd_q;
1261 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
1262 op.sb_key = cmd_q->sb_key;
1263
1264 op.init = (des3->mode == CCP_DES3_MODE_ECB) ? 0 : 1;
1265 op.u.des3.type = des3->type;
1266 op.u.des3.mode = des3->mode;
1267 op.u.des3.action = des3->action;
1268
1269 /*
1270 * All supported key sizes fit in a single (32-byte) KSB entry and
1271 * (like AES) must be in little endian format. Use the 256-bit byte
1272 * swap passthru option to convert from big endian to little endian.
1273 */
1274 ret = ccp_init_dm_workarea(&key, cmd_q,
1275 CCP_DES3_KEY_SB_COUNT * CCP_SB_BYTES,
1276 DMA_TO_DEVICE);
1277 if (ret)
1278 return ret;
1279
1280 /*
1281 * The contents of the key triplet are in the reverse order of what
1282 * is required by the engine. Copy the 3 pieces individually to put
1283 * them where they belong.
1284 */
1285 dm_offset = CCP_SB_BYTES - des3->key_len; /* Basic offset */
1286
1287 len_singlekey = des3->key_len / 3;
1288 ccp_set_dm_area(&key, dm_offset + 2 * len_singlekey,
1289 des3->key, 0, len_singlekey);
1290 ccp_set_dm_area(&key, dm_offset + len_singlekey,
1291 des3->key, len_singlekey, len_singlekey);
1292 ccp_set_dm_area(&key, dm_offset,
1293 des3->key, 2 * len_singlekey, len_singlekey);
1294
1295 /* Copy the key to the SB */
1296 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
1297 CCP_PASSTHRU_BYTESWAP_256BIT);
1298 if (ret) {
1299 cmd->engine_error = cmd_q->cmd_error;
1300 goto e_key;
1301 }
1302
1303 /*
1304 * The DES3 context fits in a single (32-byte) KSB entry and
1305 * must be in little endian format. Use the 256-bit byte swap
1306 * passthru option to convert from big endian to little endian.
1307 */
1308 if (des3->mode != CCP_DES3_MODE_ECB) {
1309 u32 load_mode;
1310
1311 op.sb_ctx = cmd_q->sb_ctx;
1312
1313 ret = ccp_init_dm_workarea(&ctx, cmd_q,
1314 CCP_DES3_CTX_SB_COUNT * CCP_SB_BYTES,
1315 DMA_BIDIRECTIONAL);
1316 if (ret)
1317 goto e_key;
1318
1319 /* Load the context into the LSB */
1320 dm_offset = CCP_SB_BYTES - des3->iv_len;
1321 ccp_set_dm_area(&ctx, dm_offset, des3->iv, 0, des3->iv_len);
1322
1323 if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0))
1324 load_mode = CCP_PASSTHRU_BYTESWAP_NOOP;
1325 else
1326 load_mode = CCP_PASSTHRU_BYTESWAP_256BIT;
1327 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1328 load_mode);
1329 if (ret) {
1330 cmd->engine_error = cmd_q->cmd_error;
1331 goto e_ctx;
1332 }
1333 }
1334
1335 /*
1336 * Prepare the input and output data workareas. For in-place
1337 * operations we need to set the dma direction to BIDIRECTIONAL
1338 * and copy the src workarea to the dst workarea.
1339 */
1340 if (sg_virt(des3->src) == sg_virt(des3->dst))
1341 in_place = true;
1342
1343 ret = ccp_init_data(&src, cmd_q, des3->src, des3->src_len,
1344 DES3_EDE_BLOCK_SIZE,
1345 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1346 if (ret)
1347 goto e_ctx;
1348
1349 if (in_place)
1350 dst = src;
1351 else {
1352 ret = ccp_init_data(&dst, cmd_q, des3->dst, des3->src_len,
1353 DES3_EDE_BLOCK_SIZE, DMA_FROM_DEVICE);
1354 if (ret)
1355 goto e_src;
1356 }
1357
1358 /* Send data to the CCP DES3 engine */
1359 while (src.sg_wa.bytes_left) {
1360 ccp_prepare_data(&src, &dst, &op, DES3_EDE_BLOCK_SIZE, true);
1361 if (!src.sg_wa.bytes_left) {
1362 op.eom = 1;
1363
1364 /* Since we don't retrieve the context in ECB mode
1365 * we have to wait for the operation to complete
1366 * on the last piece of data
1367 */
1368 op.soc = 0;
1369 }
1370
1371 ret = cmd_q->ccp->vdata->perform->des3(&op);
1372 if (ret) {
1373 cmd->engine_error = cmd_q->cmd_error;
1374 goto e_dst;
1375 }
1376
1377 ccp_process_data(&src, &dst, &op);
1378 }
1379
1380 if (des3->mode != CCP_DES3_MODE_ECB) {
1381 /* Retrieve the context and make BE */
1382 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1383 CCP_PASSTHRU_BYTESWAP_256BIT);
1384 if (ret) {
1385 cmd->engine_error = cmd_q->cmd_error;
1386 goto e_dst;
1387 }
1388
1389 /* ...but we only need the last DES3_EDE_BLOCK_SIZE bytes */
1390 if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0))
1391 dm_offset = CCP_SB_BYTES - des3->iv_len;
1392 else
1393 dm_offset = 0;
1394 ccp_get_dm_area(&ctx, dm_offset, des3->iv, 0,
1395 DES3_EDE_BLOCK_SIZE);
1396 }
1397e_dst:
1398 if (!in_place)
1399 ccp_free_data(&dst, cmd_q);
1400
1401e_src:
1402 ccp_free_data(&src, cmd_q);
1403
1404e_ctx:
1405 if (des3->mode != CCP_DES3_MODE_ECB)
1406 ccp_dm_free(&ctx);
1407
1408e_key:
1409 ccp_dm_free(&key);
1410
1411 return ret;
1412}
1413
Tom Lendacky63b94502013-11-12 11:46:16 -06001414static int ccp_run_sha_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1415{
1416 struct ccp_sha_engine *sha = &cmd->u.sha;
1417 struct ccp_dm_workarea ctx;
1418 struct ccp_data src;
1419 struct ccp_op op;
Gary R Hook4b394a22016-07-26 19:10:21 -05001420 unsigned int ioffset, ooffset;
1421 unsigned int digest_size;
1422 int sb_count;
1423 const void *init;
1424 u64 block_size;
1425 int ctx_size;
Tom Lendacky63b94502013-11-12 11:46:16 -06001426 int ret;
1427
Gary R Hook4b394a22016-07-26 19:10:21 -05001428 switch (sha->type) {
1429 case CCP_SHA_TYPE_1:
1430 if (sha->ctx_len < SHA1_DIGEST_SIZE)
1431 return -EINVAL;
1432 block_size = SHA1_BLOCK_SIZE;
1433 break;
1434 case CCP_SHA_TYPE_224:
1435 if (sha->ctx_len < SHA224_DIGEST_SIZE)
1436 return -EINVAL;
1437 block_size = SHA224_BLOCK_SIZE;
1438 break;
1439 case CCP_SHA_TYPE_256:
1440 if (sha->ctx_len < SHA256_DIGEST_SIZE)
1441 return -EINVAL;
1442 block_size = SHA256_BLOCK_SIZE;
1443 break;
Gary R Hookccebcf32017-03-15 13:20:43 -05001444 case CCP_SHA_TYPE_384:
1445 if (cmd_q->ccp->vdata->version < CCP_VERSION(4, 0)
1446 || sha->ctx_len < SHA384_DIGEST_SIZE)
1447 return -EINVAL;
1448 block_size = SHA384_BLOCK_SIZE;
1449 break;
1450 case CCP_SHA_TYPE_512:
1451 if (cmd_q->ccp->vdata->version < CCP_VERSION(4, 0)
1452 || sha->ctx_len < SHA512_DIGEST_SIZE)
1453 return -EINVAL;
1454 block_size = SHA512_BLOCK_SIZE;
1455 break;
Gary R Hook4b394a22016-07-26 19:10:21 -05001456 default:
Tom Lendacky63b94502013-11-12 11:46:16 -06001457 return -EINVAL;
Gary R Hook4b394a22016-07-26 19:10:21 -05001458 }
Tom Lendacky63b94502013-11-12 11:46:16 -06001459
1460 if (!sha->ctx)
1461 return -EINVAL;
1462
Gary R Hook4b394a22016-07-26 19:10:21 -05001463 if (!sha->final && (sha->src_len & (block_size - 1)))
Tom Lendacky63b94502013-11-12 11:46:16 -06001464 return -EINVAL;
1465
Gary R Hook4b394a22016-07-26 19:10:21 -05001466 /* The version 3 device can't handle zero-length input */
1467 if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) {
Tom Lendacky63b94502013-11-12 11:46:16 -06001468
Gary R Hook4b394a22016-07-26 19:10:21 -05001469 if (!sha->src_len) {
1470 unsigned int digest_len;
1471 const u8 *sha_zero;
1472
1473 /* Not final, just return */
1474 if (!sha->final)
1475 return 0;
1476
1477 /* CCP can't do a zero length sha operation so the
1478 * caller must buffer the data.
1479 */
1480 if (sha->msg_bits)
1481 return -EINVAL;
1482
1483 /* The CCP cannot perform zero-length sha operations
1484 * so the caller is required to buffer data for the
1485 * final operation. However, a sha operation for a
1486 * message with a total length of zero is valid so
1487 * known values are required to supply the result.
1488 */
1489 switch (sha->type) {
1490 case CCP_SHA_TYPE_1:
1491 sha_zero = sha1_zero_message_hash;
1492 digest_len = SHA1_DIGEST_SIZE;
1493 break;
1494 case CCP_SHA_TYPE_224:
1495 sha_zero = sha224_zero_message_hash;
1496 digest_len = SHA224_DIGEST_SIZE;
1497 break;
1498 case CCP_SHA_TYPE_256:
1499 sha_zero = sha256_zero_message_hash;
1500 digest_len = SHA256_DIGEST_SIZE;
1501 break;
1502 default:
1503 return -EINVAL;
1504 }
1505
1506 scatterwalk_map_and_copy((void *)sha_zero, sha->ctx, 0,
1507 digest_len, 1);
1508
Tom Lendacky63b94502013-11-12 11:46:16 -06001509 return 0;
Tom Lendacky63b94502013-11-12 11:46:16 -06001510 }
Tom Lendacky63b94502013-11-12 11:46:16 -06001511 }
1512
Gary R Hook4b394a22016-07-26 19:10:21 -05001513 /* Set variables used throughout */
1514 switch (sha->type) {
1515 case CCP_SHA_TYPE_1:
1516 digest_size = SHA1_DIGEST_SIZE;
1517 init = (void *) ccp_sha1_init;
1518 ctx_size = SHA1_DIGEST_SIZE;
1519 sb_count = 1;
1520 if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0))
1521 ooffset = ioffset = CCP_SB_BYTES - SHA1_DIGEST_SIZE;
1522 else
1523 ooffset = ioffset = 0;
1524 break;
1525 case CCP_SHA_TYPE_224:
1526 digest_size = SHA224_DIGEST_SIZE;
1527 init = (void *) ccp_sha224_init;
1528 ctx_size = SHA256_DIGEST_SIZE;
1529 sb_count = 1;
1530 ioffset = 0;
1531 if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0))
1532 ooffset = CCP_SB_BYTES - SHA224_DIGEST_SIZE;
1533 else
1534 ooffset = 0;
1535 break;
1536 case CCP_SHA_TYPE_256:
1537 digest_size = SHA256_DIGEST_SIZE;
1538 init = (void *) ccp_sha256_init;
1539 ctx_size = SHA256_DIGEST_SIZE;
1540 sb_count = 1;
1541 ooffset = ioffset = 0;
1542 break;
Gary R Hookccebcf32017-03-15 13:20:43 -05001543 case CCP_SHA_TYPE_384:
1544 digest_size = SHA384_DIGEST_SIZE;
1545 init = (void *) ccp_sha384_init;
1546 ctx_size = SHA512_DIGEST_SIZE;
1547 sb_count = 2;
1548 ioffset = 0;
1549 ooffset = 2 * CCP_SB_BYTES - SHA384_DIGEST_SIZE;
1550 break;
1551 case CCP_SHA_TYPE_512:
1552 digest_size = SHA512_DIGEST_SIZE;
1553 init = (void *) ccp_sha512_init;
1554 ctx_size = SHA512_DIGEST_SIZE;
1555 sb_count = 2;
1556 ooffset = ioffset = 0;
1557 break;
Gary R Hook4b394a22016-07-26 19:10:21 -05001558 default:
1559 ret = -EINVAL;
1560 goto e_data;
1561 }
Tom Lendacky63b94502013-11-12 11:46:16 -06001562
Gary R Hook4b394a22016-07-26 19:10:21 -05001563 /* For zero-length plaintext the src pointer is ignored;
1564 * otherwise both parts must be valid
1565 */
1566 if (sha->src_len && !sha->src)
1567 return -EINVAL;
Tom Lendacky63b94502013-11-12 11:46:16 -06001568
1569 memset(&op, 0, sizeof(op));
1570 op.cmd_q = cmd_q;
Gary R Hook4b394a22016-07-26 19:10:21 -05001571 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
1572 op.sb_ctx = cmd_q->sb_ctx; /* Pre-allocated */
Tom Lendacky63b94502013-11-12 11:46:16 -06001573 op.u.sha.type = sha->type;
1574 op.u.sha.msg_bits = sha->msg_bits;
1575
Gary R Hookccebcf32017-03-15 13:20:43 -05001576 /* For SHA1/224/256 the context fits in a single (32-byte) SB entry;
1577 * SHA384/512 require 2 adjacent SB slots, with the right half in the
1578 * first slot, and the left half in the second. Each portion must then
1579 * be in little endian format: use the 256-bit byte swap option.
1580 */
Gary R Hook4b394a22016-07-26 19:10:21 -05001581 ret = ccp_init_dm_workarea(&ctx, cmd_q, sb_count * CCP_SB_BYTES,
Tom Lendacky63b94502013-11-12 11:46:16 -06001582 DMA_BIDIRECTIONAL);
1583 if (ret)
1584 return ret;
Tom Lendackyc11baa02014-01-24 16:18:02 -06001585 if (sha->first) {
Tom Lendackyc11baa02014-01-24 16:18:02 -06001586 switch (sha->type) {
1587 case CCP_SHA_TYPE_1:
Tom Lendackyc11baa02014-01-24 16:18:02 -06001588 case CCP_SHA_TYPE_224:
Tom Lendackyc11baa02014-01-24 16:18:02 -06001589 case CCP_SHA_TYPE_256:
Gary R Hook4b394a22016-07-26 19:10:21 -05001590 memcpy(ctx.address + ioffset, init, ctx_size);
Tom Lendackyc11baa02014-01-24 16:18:02 -06001591 break;
Gary R Hookccebcf32017-03-15 13:20:43 -05001592 case CCP_SHA_TYPE_384:
1593 case CCP_SHA_TYPE_512:
1594 memcpy(ctx.address + ctx_size / 2, init,
1595 ctx_size / 2);
1596 memcpy(ctx.address, init + ctx_size / 2,
1597 ctx_size / 2);
1598 break;
Tom Lendackyc11baa02014-01-24 16:18:02 -06001599 default:
1600 ret = -EINVAL;
1601 goto e_ctx;
1602 }
Tom Lendacky8db88462015-02-03 13:07:05 -06001603 } else {
Gary R Hook4b394a22016-07-26 19:10:21 -05001604 /* Restore the context */
1605 ccp_set_dm_area(&ctx, 0, sha->ctx, 0,
1606 sb_count * CCP_SB_BYTES);
Tom Lendacky8db88462015-02-03 13:07:05 -06001607 }
Tom Lendackyc11baa02014-01-24 16:18:02 -06001608
Gary R Hook956ee212016-07-26 19:09:40 -05001609 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1610 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -06001611 if (ret) {
1612 cmd->engine_error = cmd_q->cmd_error;
1613 goto e_ctx;
1614 }
1615
Gary R Hook4b394a22016-07-26 19:10:21 -05001616 if (sha->src) {
1617 /* Send data to the CCP SHA engine; block_size is set above */
1618 ret = ccp_init_data(&src, cmd_q, sha->src, sha->src_len,
1619 block_size, DMA_TO_DEVICE);
1620 if (ret)
1621 goto e_ctx;
Tom Lendacky63b94502013-11-12 11:46:16 -06001622
Gary R Hook4b394a22016-07-26 19:10:21 -05001623 while (src.sg_wa.bytes_left) {
1624 ccp_prepare_data(&src, NULL, &op, block_size, false);
1625 if (sha->final && !src.sg_wa.bytes_left)
1626 op.eom = 1;
Tom Lendacky63b94502013-11-12 11:46:16 -06001627
Gary R Hook4b394a22016-07-26 19:10:21 -05001628 ret = cmd_q->ccp->vdata->perform->sha(&op);
1629 if (ret) {
1630 cmd->engine_error = cmd_q->cmd_error;
1631 goto e_data;
1632 }
1633
1634 ccp_process_data(&src, NULL, &op);
1635 }
1636 } else {
1637 op.eom = 1;
Gary R Hooka43eb982016-07-26 19:09:31 -05001638 ret = cmd_q->ccp->vdata->perform->sha(&op);
Tom Lendacky63b94502013-11-12 11:46:16 -06001639 if (ret) {
1640 cmd->engine_error = cmd_q->cmd_error;
1641 goto e_data;
1642 }
Tom Lendacky63b94502013-11-12 11:46:16 -06001643 }
1644
1645 /* Retrieve the SHA context - convert from LE to BE using
1646 * 32-byte (256-bit) byteswapping to BE
1647 */
Gary R Hook956ee212016-07-26 19:09:40 -05001648 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1649 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -06001650 if (ret) {
1651 cmd->engine_error = cmd_q->cmd_error;
1652 goto e_data;
1653 }
1654
Gary R Hook4b394a22016-07-26 19:10:21 -05001655 if (sha->final) {
1656 /* Finishing up, so get the digest */
1657 switch (sha->type) {
1658 case CCP_SHA_TYPE_1:
1659 case CCP_SHA_TYPE_224:
1660 case CCP_SHA_TYPE_256:
1661 ccp_get_dm_area(&ctx, ooffset,
1662 sha->ctx, 0,
1663 digest_size);
1664 break;
Gary R Hookccebcf32017-03-15 13:20:43 -05001665 case CCP_SHA_TYPE_384:
1666 case CCP_SHA_TYPE_512:
1667 ccp_get_dm_area(&ctx, 0,
1668 sha->ctx, LSB_ITEM_SIZE - ooffset,
1669 LSB_ITEM_SIZE);
1670 ccp_get_dm_area(&ctx, LSB_ITEM_SIZE + ooffset,
1671 sha->ctx, 0,
1672 LSB_ITEM_SIZE - ooffset);
1673 break;
Gary R Hook4b394a22016-07-26 19:10:21 -05001674 default:
1675 ret = -EINVAL;
1676 goto e_ctx;
1677 }
1678 } else {
1679 /* Stash the context */
1680 ccp_get_dm_area(&ctx, 0, sha->ctx, 0,
1681 sb_count * CCP_SB_BYTES);
1682 }
Tom Lendacky63b94502013-11-12 11:46:16 -06001683
Tom Lendackyc11baa02014-01-24 16:18:02 -06001684 if (sha->final && sha->opad) {
1685 /* HMAC operation, recursively perform final SHA */
1686 struct ccp_cmd hmac_cmd;
1687 struct scatterlist sg;
Tom Lendackyc11baa02014-01-24 16:18:02 -06001688 u8 *hmac_buf;
1689
Tom Lendackyc11baa02014-01-24 16:18:02 -06001690 if (sha->opad_len != block_size) {
1691 ret = -EINVAL;
1692 goto e_data;
1693 }
1694
1695 hmac_buf = kmalloc(block_size + digest_size, GFP_KERNEL);
1696 if (!hmac_buf) {
1697 ret = -ENOMEM;
1698 goto e_data;
1699 }
1700 sg_init_one(&sg, hmac_buf, block_size + digest_size);
1701
1702 scatterwalk_map_and_copy(hmac_buf, sha->opad, 0, block_size, 0);
Gary R Hook4b394a22016-07-26 19:10:21 -05001703 switch (sha->type) {
1704 case CCP_SHA_TYPE_1:
1705 case CCP_SHA_TYPE_224:
1706 case CCP_SHA_TYPE_256:
1707 memcpy(hmac_buf + block_size,
1708 ctx.address + ooffset,
1709 digest_size);
1710 break;
Gary R Hookccebcf32017-03-15 13:20:43 -05001711 case CCP_SHA_TYPE_384:
1712 case CCP_SHA_TYPE_512:
1713 memcpy(hmac_buf + block_size,
1714 ctx.address + LSB_ITEM_SIZE + ooffset,
1715 LSB_ITEM_SIZE);
1716 memcpy(hmac_buf + block_size +
1717 (LSB_ITEM_SIZE - ooffset),
1718 ctx.address,
1719 LSB_ITEM_SIZE);
1720 break;
Gary R Hook4b394a22016-07-26 19:10:21 -05001721 default:
1722 ret = -EINVAL;
1723 goto e_ctx;
1724 }
Tom Lendackyc11baa02014-01-24 16:18:02 -06001725
1726 memset(&hmac_cmd, 0, sizeof(hmac_cmd));
1727 hmac_cmd.engine = CCP_ENGINE_SHA;
1728 hmac_cmd.u.sha.type = sha->type;
1729 hmac_cmd.u.sha.ctx = sha->ctx;
1730 hmac_cmd.u.sha.ctx_len = sha->ctx_len;
1731 hmac_cmd.u.sha.src = &sg;
1732 hmac_cmd.u.sha.src_len = block_size + digest_size;
1733 hmac_cmd.u.sha.opad = NULL;
1734 hmac_cmd.u.sha.opad_len = 0;
1735 hmac_cmd.u.sha.first = 1;
1736 hmac_cmd.u.sha.final = 1;
1737 hmac_cmd.u.sha.msg_bits = (block_size + digest_size) << 3;
1738
1739 ret = ccp_run_sha_cmd(cmd_q, &hmac_cmd);
1740 if (ret)
1741 cmd->engine_error = hmac_cmd.engine_error;
1742
1743 kfree(hmac_buf);
1744 }
1745
Tom Lendacky63b94502013-11-12 11:46:16 -06001746e_data:
Gary R Hook4b394a22016-07-26 19:10:21 -05001747 if (sha->src)
1748 ccp_free_data(&src, cmd_q);
Tom Lendacky63b94502013-11-12 11:46:16 -06001749
1750e_ctx:
1751 ccp_dm_free(&ctx);
1752
1753 return ret;
1754}
1755
1756static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1757{
1758 struct ccp_rsa_engine *rsa = &cmd->u.rsa;
Gary R Hook6ba46c72017-07-17 15:16:13 -05001759 struct ccp_dm_workarea exp, src, dst;
Tom Lendacky63b94502013-11-12 11:46:16 -06001760 struct ccp_op op;
Gary R Hook956ee212016-07-26 19:09:40 -05001761 unsigned int sb_count, i_len, o_len;
Tom Lendacky63b94502013-11-12 11:46:16 -06001762 int ret;
1763
Gary R Hooke28c1902017-07-17 15:16:42 -05001764 /* Check against the maximum allowable size, in bits */
1765 if (rsa->key_size > cmd_q->ccp->vdata->rsamax)
Tom Lendacky63b94502013-11-12 11:46:16 -06001766 return -EINVAL;
1767
1768 if (!rsa->exp || !rsa->mod || !rsa->src || !rsa->dst)
1769 return -EINVAL;
1770
Gary R Hook6ba46c72017-07-17 15:16:13 -05001771 memset(&op, 0, sizeof(op));
1772 op.cmd_q = cmd_q;
1773 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
1774
Tom Lendacky63b94502013-11-12 11:46:16 -06001775 /* The RSA modulus must precede the message being acted upon, so
1776 * it must be copied to a DMA area where the message and the
1777 * modulus can be concatenated. Therefore the input buffer
1778 * length required is twice the output buffer length (which
Gary R Hook6ba46c72017-07-17 15:16:13 -05001779 * must be a multiple of 256-bits). Compute o_len, i_len in bytes.
1780 * Buffer sizes must be a multiple of 32 bytes; rounding up may be
1781 * required.
Tom Lendacky63b94502013-11-12 11:46:16 -06001782 */
Gary R Hook6ba46c72017-07-17 15:16:13 -05001783 o_len = 32 * ((rsa->key_size + 255) / 256);
Tom Lendacky63b94502013-11-12 11:46:16 -06001784 i_len = o_len * 2;
1785
Gary R Hook6ba46c72017-07-17 15:16:13 -05001786 if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0)) {
1787 /* sb_count is the number of storage block slots required
1788 * for the modulus.
1789 */
1790 sb_count = o_len / CCP_SB_BYTES;
1791 op.sb_key = cmd_q->ccp->vdata->perform->sballoc(cmd_q,
1792 sb_count);
1793 if (!op.sb_key)
1794 return -EIO;
1795 } else {
1796 /* A version 5 device allows a modulus size that will not fit
1797 * in the LSB, so the command will transfer it from memory.
1798 * Set the sb key to the default, even though it's not used.
1799 */
1800 op.sb_key = cmd_q->sb_key;
1801 }
Tom Lendacky63b94502013-11-12 11:46:16 -06001802
Gary R Hook6ba46c72017-07-17 15:16:13 -05001803 /* The RSA exponent must be in little endian format. Reverse its
1804 * byte order.
Tom Lendacky63b94502013-11-12 11:46:16 -06001805 */
1806 ret = ccp_init_dm_workarea(&exp, cmd_q, o_len, DMA_TO_DEVICE);
1807 if (ret)
Gary R Hook956ee212016-07-26 19:09:40 -05001808 goto e_sb;
Tom Lendacky63b94502013-11-12 11:46:16 -06001809
Gary R Hook83d650a2017-02-09 15:50:08 -06001810 ret = ccp_reverse_set_dm_area(&exp, 0, rsa->exp, 0, rsa->exp_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05001811 if (ret)
1812 goto e_exp;
Gary R Hook6ba46c72017-07-17 15:16:13 -05001813
1814 if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0)) {
1815 /* Copy the exponent to the local storage block, using
1816 * as many 32-byte blocks as were allocated above. It's
1817 * already little endian, so no further change is required.
1818 */
1819 ret = ccp_copy_to_sb(cmd_q, &exp, op.jobid, op.sb_key,
1820 CCP_PASSTHRU_BYTESWAP_NOOP);
1821 if (ret) {
1822 cmd->engine_error = cmd_q->cmd_error;
1823 goto e_exp;
1824 }
1825 } else {
1826 /* The exponent can be retrieved from memory via DMA. */
1827 op.exp.u.dma.address = exp.dma.address;
1828 op.exp.u.dma.offset = 0;
Tom Lendacky63b94502013-11-12 11:46:16 -06001829 }
1830
1831 /* Concatenate the modulus and the message. Both the modulus and
1832 * the operands must be in little endian format. Since the input
1833 * is in big endian format it must be converted.
1834 */
1835 ret = ccp_init_dm_workarea(&src, cmd_q, i_len, DMA_TO_DEVICE);
1836 if (ret)
1837 goto e_exp;
1838
Gary R Hook83d650a2017-02-09 15:50:08 -06001839 ret = ccp_reverse_set_dm_area(&src, 0, rsa->mod, 0, rsa->mod_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05001840 if (ret)
1841 goto e_src;
Gary R Hook83d650a2017-02-09 15:50:08 -06001842 ret = ccp_reverse_set_dm_area(&src, o_len, rsa->src, 0, rsa->src_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05001843 if (ret)
1844 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06001845
1846 /* Prepare the output area for the operation */
Gary R Hook6ba46c72017-07-17 15:16:13 -05001847 ret = ccp_init_dm_workarea(&dst, cmd_q, o_len, DMA_FROM_DEVICE);
Tom Lendacky63b94502013-11-12 11:46:16 -06001848 if (ret)
1849 goto e_src;
1850
1851 op.soc = 1;
1852 op.src.u.dma.address = src.dma.address;
1853 op.src.u.dma.offset = 0;
1854 op.src.u.dma.length = i_len;
Gary R Hook6ba46c72017-07-17 15:16:13 -05001855 op.dst.u.dma.address = dst.dma.address;
Tom Lendacky63b94502013-11-12 11:46:16 -06001856 op.dst.u.dma.offset = 0;
1857 op.dst.u.dma.length = o_len;
1858
1859 op.u.rsa.mod_size = rsa->key_size;
1860 op.u.rsa.input_len = i_len;
1861
Gary R Hooka43eb982016-07-26 19:09:31 -05001862 ret = cmd_q->ccp->vdata->perform->rsa(&op);
Tom Lendacky63b94502013-11-12 11:46:16 -06001863 if (ret) {
1864 cmd->engine_error = cmd_q->cmd_error;
1865 goto e_dst;
1866 }
1867
Gary R Hook6ba46c72017-07-17 15:16:13 -05001868 ccp_reverse_get_dm_area(&dst, 0, rsa->dst, 0, rsa->mod_len);
Tom Lendacky63b94502013-11-12 11:46:16 -06001869
1870e_dst:
Gary R Hook6ba46c72017-07-17 15:16:13 -05001871 ccp_dm_free(&dst);
Tom Lendacky63b94502013-11-12 11:46:16 -06001872
1873e_src:
1874 ccp_dm_free(&src);
1875
1876e_exp:
1877 ccp_dm_free(&exp);
1878
Gary R Hook956ee212016-07-26 19:09:40 -05001879e_sb:
Gary R Hook6ba46c72017-07-17 15:16:13 -05001880 if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0))
1881 cmd_q->ccp->vdata->perform->sbfree(cmd_q, op.sb_key, sb_count);
Tom Lendacky63b94502013-11-12 11:46:16 -06001882
1883 return ret;
1884}
1885
1886static int ccp_run_passthru_cmd(struct ccp_cmd_queue *cmd_q,
1887 struct ccp_cmd *cmd)
1888{
1889 struct ccp_passthru_engine *pt = &cmd->u.passthru;
1890 struct ccp_dm_workarea mask;
1891 struct ccp_data src, dst;
1892 struct ccp_op op;
1893 bool in_place = false;
1894 unsigned int i;
Gary R Hook4b394a22016-07-26 19:10:21 -05001895 int ret = 0;
Tom Lendacky63b94502013-11-12 11:46:16 -06001896
1897 if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
1898 return -EINVAL;
1899
1900 if (!pt->src || !pt->dst)
1901 return -EINVAL;
1902
1903 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
1904 if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
1905 return -EINVAL;
1906 if (!pt->mask)
1907 return -EINVAL;
1908 }
1909
Gary R Hook956ee212016-07-26 19:09:40 -05001910 BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT != 1);
Tom Lendacky63b94502013-11-12 11:46:16 -06001911
1912 memset(&op, 0, sizeof(op));
1913 op.cmd_q = cmd_q;
Gary R Hook4b394a22016-07-26 19:10:21 -05001914 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
Tom Lendacky63b94502013-11-12 11:46:16 -06001915
1916 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
1917 /* Load the mask */
Gary R Hook956ee212016-07-26 19:09:40 -05001918 op.sb_key = cmd_q->sb_key;
Tom Lendacky63b94502013-11-12 11:46:16 -06001919
1920 ret = ccp_init_dm_workarea(&mask, cmd_q,
Gary R Hook956ee212016-07-26 19:09:40 -05001921 CCP_PASSTHRU_SB_COUNT *
1922 CCP_SB_BYTES,
Tom Lendacky63b94502013-11-12 11:46:16 -06001923 DMA_TO_DEVICE);
1924 if (ret)
1925 return ret;
1926
1927 ccp_set_dm_area(&mask, 0, pt->mask, 0, pt->mask_len);
Gary R Hook956ee212016-07-26 19:09:40 -05001928 ret = ccp_copy_to_sb(cmd_q, &mask, op.jobid, op.sb_key,
1929 CCP_PASSTHRU_BYTESWAP_NOOP);
Tom Lendacky63b94502013-11-12 11:46:16 -06001930 if (ret) {
1931 cmd->engine_error = cmd_q->cmd_error;
1932 goto e_mask;
1933 }
1934 }
1935
1936 /* Prepare the input and output data workareas. For in-place
1937 * operations we need to set the dma direction to BIDIRECTIONAL
1938 * and copy the src workarea to the dst workarea.
1939 */
1940 if (sg_virt(pt->src) == sg_virt(pt->dst))
1941 in_place = true;
1942
1943 ret = ccp_init_data(&src, cmd_q, pt->src, pt->src_len,
1944 CCP_PASSTHRU_MASKSIZE,
1945 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1946 if (ret)
1947 goto e_mask;
1948
Tom Lendacky8db88462015-02-03 13:07:05 -06001949 if (in_place) {
Tom Lendacky63b94502013-11-12 11:46:16 -06001950 dst = src;
Tom Lendacky8db88462015-02-03 13:07:05 -06001951 } else {
Tom Lendacky63b94502013-11-12 11:46:16 -06001952 ret = ccp_init_data(&dst, cmd_q, pt->dst, pt->src_len,
1953 CCP_PASSTHRU_MASKSIZE, DMA_FROM_DEVICE);
1954 if (ret)
1955 goto e_src;
1956 }
1957
1958 /* Send data to the CCP Passthru engine
1959 * Because the CCP engine works on a single source and destination
1960 * dma address at a time, each entry in the source scatterlist
1961 * (after the dma_map_sg call) must be less than or equal to the
1962 * (remaining) length in the destination scatterlist entry and the
1963 * length must be a multiple of CCP_PASSTHRU_BLOCKSIZE
1964 */
1965 dst.sg_wa.sg_used = 0;
1966 for (i = 1; i <= src.sg_wa.dma_count; i++) {
1967 if (!dst.sg_wa.sg ||
1968 (dst.sg_wa.sg->length < src.sg_wa.sg->length)) {
1969 ret = -EINVAL;
1970 goto e_dst;
1971 }
1972
1973 if (i == src.sg_wa.dma_count) {
1974 op.eom = 1;
1975 op.soc = 1;
1976 }
1977
1978 op.src.type = CCP_MEMTYPE_SYSTEM;
1979 op.src.u.dma.address = sg_dma_address(src.sg_wa.sg);
1980 op.src.u.dma.offset = 0;
1981 op.src.u.dma.length = sg_dma_len(src.sg_wa.sg);
1982
1983 op.dst.type = CCP_MEMTYPE_SYSTEM;
1984 op.dst.u.dma.address = sg_dma_address(dst.sg_wa.sg);
Dave Jones80e84c12014-02-09 09:59:14 +08001985 op.dst.u.dma.offset = dst.sg_wa.sg_used;
1986 op.dst.u.dma.length = op.src.u.dma.length;
Tom Lendacky63b94502013-11-12 11:46:16 -06001987
Gary R Hooka43eb982016-07-26 19:09:31 -05001988 ret = cmd_q->ccp->vdata->perform->passthru(&op);
Tom Lendacky63b94502013-11-12 11:46:16 -06001989 if (ret) {
1990 cmd->engine_error = cmd_q->cmd_error;
1991 goto e_dst;
1992 }
1993
1994 dst.sg_wa.sg_used += src.sg_wa.sg->length;
1995 if (dst.sg_wa.sg_used == dst.sg_wa.sg->length) {
1996 dst.sg_wa.sg = sg_next(dst.sg_wa.sg);
1997 dst.sg_wa.sg_used = 0;
1998 }
1999 src.sg_wa.sg = sg_next(src.sg_wa.sg);
2000 }
2001
2002e_dst:
2003 if (!in_place)
2004 ccp_free_data(&dst, cmd_q);
2005
2006e_src:
2007 ccp_free_data(&src, cmd_q);
2008
2009e_mask:
2010 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP)
2011 ccp_dm_free(&mask);
2012
2013 return ret;
2014}
2015
Gary R Hook58ea8ab2016-04-18 09:21:44 -05002016static int ccp_run_passthru_nomap_cmd(struct ccp_cmd_queue *cmd_q,
2017 struct ccp_cmd *cmd)
2018{
2019 struct ccp_passthru_nomap_engine *pt = &cmd->u.passthru_nomap;
2020 struct ccp_dm_workarea mask;
2021 struct ccp_op op;
2022 int ret;
2023
2024 if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
2025 return -EINVAL;
2026
2027 if (!pt->src_dma || !pt->dst_dma)
2028 return -EINVAL;
2029
2030 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
2031 if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
2032 return -EINVAL;
2033 if (!pt->mask)
2034 return -EINVAL;
2035 }
2036
Gary R Hook956ee212016-07-26 19:09:40 -05002037 BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT != 1);
Gary R Hook58ea8ab2016-04-18 09:21:44 -05002038
2039 memset(&op, 0, sizeof(op));
2040 op.cmd_q = cmd_q;
Gary R Hookbce386a2017-06-27 08:58:16 -05002041 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
Gary R Hook58ea8ab2016-04-18 09:21:44 -05002042
2043 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
2044 /* Load the mask */
Gary R Hook956ee212016-07-26 19:09:40 -05002045 op.sb_key = cmd_q->sb_key;
Gary R Hook58ea8ab2016-04-18 09:21:44 -05002046
2047 mask.length = pt->mask_len;
2048 mask.dma.address = pt->mask;
2049 mask.dma.length = pt->mask_len;
2050
Gary R Hook956ee212016-07-26 19:09:40 -05002051 ret = ccp_copy_to_sb(cmd_q, &mask, op.jobid, op.sb_key,
Gary R Hook58ea8ab2016-04-18 09:21:44 -05002052 CCP_PASSTHRU_BYTESWAP_NOOP);
2053 if (ret) {
2054 cmd->engine_error = cmd_q->cmd_error;
2055 return ret;
2056 }
2057 }
2058
2059 /* Send data to the CCP Passthru engine */
2060 op.eom = 1;
2061 op.soc = 1;
2062
2063 op.src.type = CCP_MEMTYPE_SYSTEM;
2064 op.src.u.dma.address = pt->src_dma;
2065 op.src.u.dma.offset = 0;
2066 op.src.u.dma.length = pt->src_len;
2067
2068 op.dst.type = CCP_MEMTYPE_SYSTEM;
2069 op.dst.u.dma.address = pt->dst_dma;
2070 op.dst.u.dma.offset = 0;
2071 op.dst.u.dma.length = pt->src_len;
2072
Gary R Hooka43eb982016-07-26 19:09:31 -05002073 ret = cmd_q->ccp->vdata->perform->passthru(&op);
Gary R Hook58ea8ab2016-04-18 09:21:44 -05002074 if (ret)
2075 cmd->engine_error = cmd_q->cmd_error;
2076
2077 return ret;
2078}
2079
Tom Lendacky63b94502013-11-12 11:46:16 -06002080static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
2081{
2082 struct ccp_ecc_engine *ecc = &cmd->u.ecc;
2083 struct ccp_dm_workarea src, dst;
2084 struct ccp_op op;
2085 int ret;
2086 u8 *save;
2087
2088 if (!ecc->u.mm.operand_1 ||
2089 (ecc->u.mm.operand_1_len > CCP_ECC_MODULUS_BYTES))
2090 return -EINVAL;
2091
2092 if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT)
2093 if (!ecc->u.mm.operand_2 ||
2094 (ecc->u.mm.operand_2_len > CCP_ECC_MODULUS_BYTES))
2095 return -EINVAL;
2096
2097 if (!ecc->u.mm.result ||
2098 (ecc->u.mm.result_len < CCP_ECC_MODULUS_BYTES))
2099 return -EINVAL;
2100
2101 memset(&op, 0, sizeof(op));
2102 op.cmd_q = cmd_q;
Gary R Hook4b394a22016-07-26 19:10:21 -05002103 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
Tom Lendacky63b94502013-11-12 11:46:16 -06002104
2105 /* Concatenate the modulus and the operands. Both the modulus and
2106 * the operands must be in little endian format. Since the input
2107 * is in big endian format it must be converted and placed in a
2108 * fixed length buffer.
2109 */
2110 ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
2111 DMA_TO_DEVICE);
2112 if (ret)
2113 return ret;
2114
2115 /* Save the workarea address since it is updated in order to perform
2116 * the concatenation
2117 */
2118 save = src.address;
2119
2120 /* Copy the ECC modulus */
Gary R Hook83d650a2017-02-09 15:50:08 -06002121 ret = ccp_reverse_set_dm_area(&src, 0, ecc->mod, 0, ecc->mod_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002122 if (ret)
2123 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002124 src.address += CCP_ECC_OPERAND_SIZE;
2125
2126 /* Copy the first operand */
Gary R Hook83d650a2017-02-09 15:50:08 -06002127 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.mm.operand_1, 0,
2128 ecc->u.mm.operand_1_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002129 if (ret)
2130 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002131 src.address += CCP_ECC_OPERAND_SIZE;
2132
2133 if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT) {
2134 /* Copy the second operand */
Gary R Hook83d650a2017-02-09 15:50:08 -06002135 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.mm.operand_2, 0,
2136 ecc->u.mm.operand_2_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002137 if (ret)
2138 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002139 src.address += CCP_ECC_OPERAND_SIZE;
2140 }
2141
2142 /* Restore the workarea address */
2143 src.address = save;
2144
2145 /* Prepare the output area for the operation */
2146 ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
2147 DMA_FROM_DEVICE);
2148 if (ret)
2149 goto e_src;
2150
2151 op.soc = 1;
2152 op.src.u.dma.address = src.dma.address;
2153 op.src.u.dma.offset = 0;
2154 op.src.u.dma.length = src.length;
2155 op.dst.u.dma.address = dst.dma.address;
2156 op.dst.u.dma.offset = 0;
2157 op.dst.u.dma.length = dst.length;
2158
2159 op.u.ecc.function = cmd->u.ecc.function;
2160
Gary R Hooka43eb982016-07-26 19:09:31 -05002161 ret = cmd_q->ccp->vdata->perform->ecc(&op);
Tom Lendacky63b94502013-11-12 11:46:16 -06002162 if (ret) {
2163 cmd->engine_error = cmd_q->cmd_error;
2164 goto e_dst;
2165 }
2166
2167 ecc->ecc_result = le16_to_cpup(
2168 (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
2169 if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
2170 ret = -EIO;
2171 goto e_dst;
2172 }
2173
2174 /* Save the ECC result */
Gary R Hook83d650a2017-02-09 15:50:08 -06002175 ccp_reverse_get_dm_area(&dst, 0, ecc->u.mm.result, 0,
2176 CCP_ECC_MODULUS_BYTES);
Tom Lendacky63b94502013-11-12 11:46:16 -06002177
2178e_dst:
2179 ccp_dm_free(&dst);
2180
2181e_src:
2182 ccp_dm_free(&src);
2183
2184 return ret;
2185}
2186
2187static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
2188{
2189 struct ccp_ecc_engine *ecc = &cmd->u.ecc;
2190 struct ccp_dm_workarea src, dst;
2191 struct ccp_op op;
2192 int ret;
2193 u8 *save;
2194
2195 if (!ecc->u.pm.point_1.x ||
2196 (ecc->u.pm.point_1.x_len > CCP_ECC_MODULUS_BYTES) ||
2197 !ecc->u.pm.point_1.y ||
2198 (ecc->u.pm.point_1.y_len > CCP_ECC_MODULUS_BYTES))
2199 return -EINVAL;
2200
2201 if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
2202 if (!ecc->u.pm.point_2.x ||
2203 (ecc->u.pm.point_2.x_len > CCP_ECC_MODULUS_BYTES) ||
2204 !ecc->u.pm.point_2.y ||
2205 (ecc->u.pm.point_2.y_len > CCP_ECC_MODULUS_BYTES))
2206 return -EINVAL;
2207 } else {
2208 if (!ecc->u.pm.domain_a ||
2209 (ecc->u.pm.domain_a_len > CCP_ECC_MODULUS_BYTES))
2210 return -EINVAL;
2211
2212 if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT)
2213 if (!ecc->u.pm.scalar ||
2214 (ecc->u.pm.scalar_len > CCP_ECC_MODULUS_BYTES))
2215 return -EINVAL;
2216 }
2217
2218 if (!ecc->u.pm.result.x ||
2219 (ecc->u.pm.result.x_len < CCP_ECC_MODULUS_BYTES) ||
2220 !ecc->u.pm.result.y ||
2221 (ecc->u.pm.result.y_len < CCP_ECC_MODULUS_BYTES))
2222 return -EINVAL;
2223
2224 memset(&op, 0, sizeof(op));
2225 op.cmd_q = cmd_q;
Gary R Hook4b394a22016-07-26 19:10:21 -05002226 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
Tom Lendacky63b94502013-11-12 11:46:16 -06002227
2228 /* Concatenate the modulus and the operands. Both the modulus and
2229 * the operands must be in little endian format. Since the input
2230 * is in big endian format it must be converted and placed in a
2231 * fixed length buffer.
2232 */
2233 ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
2234 DMA_TO_DEVICE);
2235 if (ret)
2236 return ret;
2237
2238 /* Save the workarea address since it is updated in order to perform
2239 * the concatenation
2240 */
2241 save = src.address;
2242
2243 /* Copy the ECC modulus */
Gary R Hook83d650a2017-02-09 15:50:08 -06002244 ret = ccp_reverse_set_dm_area(&src, 0, ecc->mod, 0, ecc->mod_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002245 if (ret)
2246 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002247 src.address += CCP_ECC_OPERAND_SIZE;
2248
2249 /* Copy the first point X and Y coordinate */
Gary R Hook83d650a2017-02-09 15:50:08 -06002250 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_1.x, 0,
2251 ecc->u.pm.point_1.x_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002252 if (ret)
2253 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002254 src.address += CCP_ECC_OPERAND_SIZE;
Gary R Hook83d650a2017-02-09 15:50:08 -06002255 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_1.y, 0,
2256 ecc->u.pm.point_1.y_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002257 if (ret)
2258 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002259 src.address += CCP_ECC_OPERAND_SIZE;
2260
Gary R Hook4b394a22016-07-26 19:10:21 -05002261 /* Set the first point Z coordinate to 1 */
Tom Lendacky8db88462015-02-03 13:07:05 -06002262 *src.address = 0x01;
Tom Lendacky63b94502013-11-12 11:46:16 -06002263 src.address += CCP_ECC_OPERAND_SIZE;
2264
2265 if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
2266 /* Copy the second point X and Y coordinate */
Gary R Hook83d650a2017-02-09 15:50:08 -06002267 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_2.x, 0,
2268 ecc->u.pm.point_2.x_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002269 if (ret)
2270 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002271 src.address += CCP_ECC_OPERAND_SIZE;
Gary R Hook83d650a2017-02-09 15:50:08 -06002272 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_2.y, 0,
2273 ecc->u.pm.point_2.y_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002274 if (ret)
2275 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002276 src.address += CCP_ECC_OPERAND_SIZE;
2277
Gary R Hook4b394a22016-07-26 19:10:21 -05002278 /* Set the second point Z coordinate to 1 */
Tom Lendacky8db88462015-02-03 13:07:05 -06002279 *src.address = 0x01;
Tom Lendacky63b94502013-11-12 11:46:16 -06002280 src.address += CCP_ECC_OPERAND_SIZE;
2281 } else {
2282 /* Copy the Domain "a" parameter */
Gary R Hook83d650a2017-02-09 15:50:08 -06002283 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.domain_a, 0,
2284 ecc->u.pm.domain_a_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002285 if (ret)
2286 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002287 src.address += CCP_ECC_OPERAND_SIZE;
2288
2289 if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT) {
2290 /* Copy the scalar value */
Gary R Hook83d650a2017-02-09 15:50:08 -06002291 ret = ccp_reverse_set_dm_area(&src, 0,
2292 ecc->u.pm.scalar, 0,
2293 ecc->u.pm.scalar_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002294 if (ret)
2295 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002296 src.address += CCP_ECC_OPERAND_SIZE;
2297 }
2298 }
2299
2300 /* Restore the workarea address */
2301 src.address = save;
2302
2303 /* Prepare the output area for the operation */
2304 ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
2305 DMA_FROM_DEVICE);
2306 if (ret)
2307 goto e_src;
2308
2309 op.soc = 1;
2310 op.src.u.dma.address = src.dma.address;
2311 op.src.u.dma.offset = 0;
2312 op.src.u.dma.length = src.length;
2313 op.dst.u.dma.address = dst.dma.address;
2314 op.dst.u.dma.offset = 0;
2315 op.dst.u.dma.length = dst.length;
2316
2317 op.u.ecc.function = cmd->u.ecc.function;
2318
Gary R Hooka43eb982016-07-26 19:09:31 -05002319 ret = cmd_q->ccp->vdata->perform->ecc(&op);
Tom Lendacky63b94502013-11-12 11:46:16 -06002320 if (ret) {
2321 cmd->engine_error = cmd_q->cmd_error;
2322 goto e_dst;
2323 }
2324
2325 ecc->ecc_result = le16_to_cpup(
2326 (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
2327 if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
2328 ret = -EIO;
2329 goto e_dst;
2330 }
2331
2332 /* Save the workarea address since it is updated as we walk through
2333 * to copy the point math result
2334 */
2335 save = dst.address;
2336
2337 /* Save the ECC result X and Y coordinates */
Gary R Hook83d650a2017-02-09 15:50:08 -06002338 ccp_reverse_get_dm_area(&dst, 0, ecc->u.pm.result.x, 0,
Tom Lendacky63b94502013-11-12 11:46:16 -06002339 CCP_ECC_MODULUS_BYTES);
2340 dst.address += CCP_ECC_OUTPUT_SIZE;
Gary R Hook83d650a2017-02-09 15:50:08 -06002341 ccp_reverse_get_dm_area(&dst, 0, ecc->u.pm.result.y, 0,
Tom Lendacky63b94502013-11-12 11:46:16 -06002342 CCP_ECC_MODULUS_BYTES);
2343 dst.address += CCP_ECC_OUTPUT_SIZE;
2344
2345 /* Restore the workarea address */
2346 dst.address = save;
2347
2348e_dst:
2349 ccp_dm_free(&dst);
2350
2351e_src:
2352 ccp_dm_free(&src);
2353
2354 return ret;
2355}
2356
2357static int ccp_run_ecc_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
2358{
2359 struct ccp_ecc_engine *ecc = &cmd->u.ecc;
2360
2361 ecc->ecc_result = 0;
2362
2363 if (!ecc->mod ||
2364 (ecc->mod_len > CCP_ECC_MODULUS_BYTES))
2365 return -EINVAL;
2366
2367 switch (ecc->function) {
2368 case CCP_ECC_FUNCTION_MMUL_384BIT:
2369 case CCP_ECC_FUNCTION_MADD_384BIT:
2370 case CCP_ECC_FUNCTION_MINV_384BIT:
2371 return ccp_run_ecc_mm_cmd(cmd_q, cmd);
2372
2373 case CCP_ECC_FUNCTION_PADD_384BIT:
2374 case CCP_ECC_FUNCTION_PMUL_384BIT:
2375 case CCP_ECC_FUNCTION_PDBL_384BIT:
2376 return ccp_run_ecc_pm_cmd(cmd_q, cmd);
2377
2378 default:
2379 return -EINVAL;
2380 }
2381}
2382
2383int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
2384{
2385 int ret;
2386
2387 cmd->engine_error = 0;
2388 cmd_q->cmd_error = 0;
2389 cmd_q->int_rcvd = 0;
Gary R Hookbb4e89b2016-07-26 19:10:13 -05002390 cmd_q->free_slots = cmd_q->ccp->vdata->perform->get_free_slots(cmd_q);
Tom Lendacky63b94502013-11-12 11:46:16 -06002391
2392 switch (cmd->engine) {
2393 case CCP_ENGINE_AES:
2394 ret = ccp_run_aes_cmd(cmd_q, cmd);
2395 break;
2396 case CCP_ENGINE_XTS_AES_128:
2397 ret = ccp_run_xts_aes_cmd(cmd_q, cmd);
2398 break;
Gary R Hook990672d2017-03-15 13:20:52 -05002399 case CCP_ENGINE_DES3:
2400 ret = ccp_run_des3_cmd(cmd_q, cmd);
2401 break;
Tom Lendacky63b94502013-11-12 11:46:16 -06002402 case CCP_ENGINE_SHA:
2403 ret = ccp_run_sha_cmd(cmd_q, cmd);
2404 break;
2405 case CCP_ENGINE_RSA:
2406 ret = ccp_run_rsa_cmd(cmd_q, cmd);
2407 break;
2408 case CCP_ENGINE_PASSTHRU:
Gary R Hook58ea8ab2016-04-18 09:21:44 -05002409 if (cmd->flags & CCP_CMD_PASSTHRU_NO_DMA_MAP)
2410 ret = ccp_run_passthru_nomap_cmd(cmd_q, cmd);
2411 else
2412 ret = ccp_run_passthru_cmd(cmd_q, cmd);
Tom Lendacky63b94502013-11-12 11:46:16 -06002413 break;
2414 case CCP_ENGINE_ECC:
2415 ret = ccp_run_ecc_cmd(cmd_q, cmd);
2416 break;
2417 default:
2418 ret = -EINVAL;
2419 }
2420
2421 return ret;
2422}