Loading...
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (C) 2012-2019 ARM Limited (or its affiliates). */
3
4#include <linux/kernel.h>
5#include <linux/module.h>
6#include <crypto/algapi.h>
7#include <crypto/internal/skcipher.h>
8#include <crypto/internal/des.h>
9#include <crypto/xts.h>
10#include <crypto/sm4.h>
11#include <crypto/scatterwalk.h>
12
13#include "cc_driver.h"
14#include "cc_lli_defs.h"
15#include "cc_buffer_mgr.h"
16#include "cc_cipher.h"
17#include "cc_request_mgr.h"
18
19#define MAX_SKCIPHER_SEQ_LEN 6
20
21#define template_skcipher template_u.skcipher
22
23struct cc_user_key_info {
24 u8 *key;
25 dma_addr_t key_dma_addr;
26};
27
28struct cc_hw_key_info {
29 enum cc_hw_crypto_key key1_slot;
30 enum cc_hw_crypto_key key2_slot;
31};
32
33struct cc_cpp_key_info {
34 u8 slot;
35 enum cc_cpp_alg alg;
36};
37
38enum cc_key_type {
39 CC_UNPROTECTED_KEY, /* User key */
40 CC_HW_PROTECTED_KEY, /* HW (FDE) key */
41 CC_POLICY_PROTECTED_KEY, /* CPP key */
42 CC_INVALID_PROTECTED_KEY /* Invalid key */
43};
44
45struct cc_cipher_ctx {
46 struct cc_drvdata *drvdata;
47 int keylen;
48 int cipher_mode;
49 int flow_mode;
50 unsigned int flags;
51 enum cc_key_type key_type;
52 struct cc_user_key_info user;
53 union {
54 struct cc_hw_key_info hw;
55 struct cc_cpp_key_info cpp;
56 };
57 struct crypto_shash *shash_tfm;
58 struct crypto_skcipher *fallback_tfm;
59 bool fallback_on;
60};
61
62static void cc_cipher_complete(struct device *dev, void *cc_req, int err);
63
64static inline enum cc_key_type cc_key_type(struct crypto_tfm *tfm)
65{
66 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
67
68 return ctx_p->key_type;
69}
70
71static int validate_keys_sizes(struct cc_cipher_ctx *ctx_p, u32 size)
72{
73 switch (ctx_p->flow_mode) {
74 case S_DIN_to_AES:
75 switch (size) {
76 case CC_AES_128_BIT_KEY_SIZE:
77 case CC_AES_192_BIT_KEY_SIZE:
78 if (ctx_p->cipher_mode != DRV_CIPHER_XTS)
79 return 0;
80 break;
81 case CC_AES_256_BIT_KEY_SIZE:
82 return 0;
83 case (CC_AES_192_BIT_KEY_SIZE * 2):
84 case (CC_AES_256_BIT_KEY_SIZE * 2):
85 if (ctx_p->cipher_mode == DRV_CIPHER_XTS ||
86 ctx_p->cipher_mode == DRV_CIPHER_ESSIV)
87 return 0;
88 break;
89 default:
90 break;
91 }
92 break;
93 case S_DIN_to_DES:
94 if (size == DES3_EDE_KEY_SIZE || size == DES_KEY_SIZE)
95 return 0;
96 break;
97 case S_DIN_to_SM4:
98 if (size == SM4_KEY_SIZE)
99 return 0;
100 break;
101 default:
102 break;
103 }
104 return -EINVAL;
105}
106
107static int validate_data_size(struct cc_cipher_ctx *ctx_p,
108 unsigned int size)
109{
110 switch (ctx_p->flow_mode) {
111 case S_DIN_to_AES:
112 switch (ctx_p->cipher_mode) {
113 case DRV_CIPHER_XTS:
114 case DRV_CIPHER_CBC_CTS:
115 if (size >= AES_BLOCK_SIZE)
116 return 0;
117 break;
118 case DRV_CIPHER_OFB:
119 case DRV_CIPHER_CTR:
120 return 0;
121 case DRV_CIPHER_ECB:
122 case DRV_CIPHER_CBC:
123 case DRV_CIPHER_ESSIV:
124 if (IS_ALIGNED(size, AES_BLOCK_SIZE))
125 return 0;
126 break;
127 default:
128 break;
129 }
130 break;
131 case S_DIN_to_DES:
132 if (IS_ALIGNED(size, DES_BLOCK_SIZE))
133 return 0;
134 break;
135 case S_DIN_to_SM4:
136 switch (ctx_p->cipher_mode) {
137 case DRV_CIPHER_CTR:
138 return 0;
139 case DRV_CIPHER_ECB:
140 case DRV_CIPHER_CBC:
141 if (IS_ALIGNED(size, SM4_BLOCK_SIZE))
142 return 0;
143 break;
144 default:
145 break;
146 }
147 break;
148 default:
149 break;
150 }
151 return -EINVAL;
152}
153
154static int cc_cipher_init(struct crypto_tfm *tfm)
155{
156 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
157 struct cc_crypto_alg *cc_alg =
158 container_of(tfm->__crt_alg, struct cc_crypto_alg,
159 skcipher_alg.base);
160 struct device *dev = drvdata_to_dev(cc_alg->drvdata);
161 unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
162 unsigned int fallback_req_size = 0;
163
164 dev_dbg(dev, "Initializing context @%p for %s\n", ctx_p,
165 crypto_tfm_alg_name(tfm));
166
167 ctx_p->cipher_mode = cc_alg->cipher_mode;
168 ctx_p->flow_mode = cc_alg->flow_mode;
169 ctx_p->drvdata = cc_alg->drvdata;
170
171 if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
172 const char *name = crypto_tfm_alg_name(tfm);
173
174 /* Alloc hash tfm for essiv */
175 ctx_p->shash_tfm = crypto_alloc_shash("sha256", 0, 0);
176 if (IS_ERR(ctx_p->shash_tfm)) {
177 dev_err(dev, "Error allocating hash tfm for ESSIV.\n");
178 return PTR_ERR(ctx_p->shash_tfm);
179 }
180 max_key_buf_size <<= 1;
181
182 /* Alloc fallback tfm or essiv when key size != 256 bit */
183 ctx_p->fallback_tfm =
184 crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC);
185
186 if (IS_ERR(ctx_p->fallback_tfm)) {
187 /* Note we're still allowing registration with no fallback since it's
188 * better to have most modes supported than none at all.
189 */
190 dev_warn(dev, "Error allocating fallback algo %s. Some modes may be available.\n",
191 name);
192 ctx_p->fallback_tfm = NULL;
193 } else {
194 fallback_req_size = crypto_skcipher_reqsize(ctx_p->fallback_tfm);
195 }
196 }
197
198 crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
199 sizeof(struct cipher_req_ctx) + fallback_req_size);
200
201 /* Allocate key buffer, cache line aligned */
202 ctx_p->user.key = kzalloc(max_key_buf_size, GFP_KERNEL);
203 if (!ctx_p->user.key)
204 goto free_fallback;
205
206 dev_dbg(dev, "Allocated key buffer in context. key=@%p\n",
207 ctx_p->user.key);
208
209 /* Map key buffer */
210 ctx_p->user.key_dma_addr = dma_map_single(dev, ctx_p->user.key,
211 max_key_buf_size,
212 DMA_TO_DEVICE);
213 if (dma_mapping_error(dev, ctx_p->user.key_dma_addr)) {
214 dev_err(dev, "Mapping Key %u B at va=%pK for DMA failed\n",
215 max_key_buf_size, ctx_p->user.key);
216 goto free_key;
217 }
218 dev_dbg(dev, "Mapped key %u B at va=%pK to dma=%pad\n",
219 max_key_buf_size, ctx_p->user.key, &ctx_p->user.key_dma_addr);
220
221 return 0;
222
223free_key:
224 kfree(ctx_p->user.key);
225free_fallback:
226 crypto_free_skcipher(ctx_p->fallback_tfm);
227 crypto_free_shash(ctx_p->shash_tfm);
228
229 return -ENOMEM;
230}
231
232static void cc_cipher_exit(struct crypto_tfm *tfm)
233{
234 struct crypto_alg *alg = tfm->__crt_alg;
235 struct cc_crypto_alg *cc_alg =
236 container_of(alg, struct cc_crypto_alg,
237 skcipher_alg.base);
238 unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
239 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
240 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
241
242 dev_dbg(dev, "Clearing context @%p for %s\n",
243 crypto_tfm_ctx(tfm), crypto_tfm_alg_name(tfm));
244
245 if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
246 /* Free hash tfm for essiv */
247 crypto_free_shash(ctx_p->shash_tfm);
248 ctx_p->shash_tfm = NULL;
249 crypto_free_skcipher(ctx_p->fallback_tfm);
250 ctx_p->fallback_tfm = NULL;
251 }
252
253 /* Unmap key buffer */
254 dma_unmap_single(dev, ctx_p->user.key_dma_addr, max_key_buf_size,
255 DMA_TO_DEVICE);
256 dev_dbg(dev, "Unmapped key buffer key_dma_addr=%pad\n",
257 &ctx_p->user.key_dma_addr);
258
259 /* Free key buffer in context */
260 dev_dbg(dev, "Free key buffer in context. key=@%p\n", ctx_p->user.key);
261 kfree_sensitive(ctx_p->user.key);
262}
263
264static enum cc_hw_crypto_key cc_slot_to_hw_key(u8 slot_num)
265{
266 switch (slot_num) {
267 case 0:
268 return KFDE0_KEY;
269 case 1:
270 return KFDE1_KEY;
271 case 2:
272 return KFDE2_KEY;
273 case 3:
274 return KFDE3_KEY;
275 }
276 return END_OF_KEYS;
277}
278
279static u8 cc_slot_to_cpp_key(u8 slot_num)
280{
281 return (slot_num - CC_FIRST_CPP_KEY_SLOT);
282}
283
284static inline enum cc_key_type cc_slot_to_key_type(u8 slot_num)
285{
286 if (slot_num >= CC_FIRST_HW_KEY_SLOT && slot_num <= CC_LAST_HW_KEY_SLOT)
287 return CC_HW_PROTECTED_KEY;
288 else if (slot_num >= CC_FIRST_CPP_KEY_SLOT &&
289 slot_num <= CC_LAST_CPP_KEY_SLOT)
290 return CC_POLICY_PROTECTED_KEY;
291 else
292 return CC_INVALID_PROTECTED_KEY;
293}
294
295static int cc_cipher_sethkey(struct crypto_skcipher *sktfm, const u8 *key,
296 unsigned int keylen)
297{
298 struct crypto_tfm *tfm = crypto_skcipher_tfm(sktfm);
299 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
300 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
301 struct cc_hkey_info hki;
302
303 dev_dbg(dev, "Setting HW key in context @%p for %s. keylen=%u\n",
304 ctx_p, crypto_tfm_alg_name(tfm), keylen);
305 dump_byte_array("key", key, keylen);
306
307 /* STAT_PHASE_0: Init and sanity checks */
308
309 /* This check the size of the protected key token */
310 if (keylen != sizeof(hki)) {
311 dev_err(dev, "Unsupported protected key size %d.\n", keylen);
312 return -EINVAL;
313 }
314
315 memcpy(&hki, key, keylen);
316
317 /* The real key len for crypto op is the size of the HW key
318 * referenced by the HW key slot, not the hardware key token
319 */
320 keylen = hki.keylen;
321
322 if (validate_keys_sizes(ctx_p, keylen)) {
323 dev_dbg(dev, "Unsupported key size %d.\n", keylen);
324 return -EINVAL;
325 }
326
327 ctx_p->keylen = keylen;
328 ctx_p->fallback_on = false;
329
330 switch (cc_slot_to_key_type(hki.hw_key1)) {
331 case CC_HW_PROTECTED_KEY:
332 if (ctx_p->flow_mode == S_DIN_to_SM4) {
333 dev_err(dev, "Only AES HW protected keys are supported\n");
334 return -EINVAL;
335 }
336
337 ctx_p->hw.key1_slot = cc_slot_to_hw_key(hki.hw_key1);
338 if (ctx_p->hw.key1_slot == END_OF_KEYS) {
339 dev_err(dev, "Unsupported hw key1 number (%d)\n",
340 hki.hw_key1);
341 return -EINVAL;
342 }
343
344 if (ctx_p->cipher_mode == DRV_CIPHER_XTS ||
345 ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
346 if (hki.hw_key1 == hki.hw_key2) {
347 dev_err(dev, "Illegal hw key numbers (%d,%d)\n",
348 hki.hw_key1, hki.hw_key2);
349 return -EINVAL;
350 }
351
352 ctx_p->hw.key2_slot = cc_slot_to_hw_key(hki.hw_key2);
353 if (ctx_p->hw.key2_slot == END_OF_KEYS) {
354 dev_err(dev, "Unsupported hw key2 number (%d)\n",
355 hki.hw_key2);
356 return -EINVAL;
357 }
358 }
359
360 ctx_p->key_type = CC_HW_PROTECTED_KEY;
361 dev_dbg(dev, "HW protected key %d/%d set\n.",
362 ctx_p->hw.key1_slot, ctx_p->hw.key2_slot);
363 break;
364
365 case CC_POLICY_PROTECTED_KEY:
366 if (ctx_p->drvdata->hw_rev < CC_HW_REV_713) {
367 dev_err(dev, "CPP keys not supported in this hardware revision.\n");
368 return -EINVAL;
369 }
370
371 if (ctx_p->cipher_mode != DRV_CIPHER_CBC &&
372 ctx_p->cipher_mode != DRV_CIPHER_CTR) {
373 dev_err(dev, "CPP keys only supported in CBC or CTR modes.\n");
374 return -EINVAL;
375 }
376
377 ctx_p->cpp.slot = cc_slot_to_cpp_key(hki.hw_key1);
378 if (ctx_p->flow_mode == S_DIN_to_AES)
379 ctx_p->cpp.alg = CC_CPP_AES;
380 else /* Must be SM4 since due to sethkey registration */
381 ctx_p->cpp.alg = CC_CPP_SM4;
382 ctx_p->key_type = CC_POLICY_PROTECTED_KEY;
383 dev_dbg(dev, "policy protected key alg: %d slot: %d.\n",
384 ctx_p->cpp.alg, ctx_p->cpp.slot);
385 break;
386
387 default:
388 dev_err(dev, "Unsupported protected key (%d)\n", hki.hw_key1);
389 return -EINVAL;
390 }
391
392 return 0;
393}
394
395static int cc_cipher_setkey(struct crypto_skcipher *sktfm, const u8 *key,
396 unsigned int keylen)
397{
398 struct crypto_tfm *tfm = crypto_skcipher_tfm(sktfm);
399 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
400 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
401 struct cc_crypto_alg *cc_alg =
402 container_of(tfm->__crt_alg, struct cc_crypto_alg,
403 skcipher_alg.base);
404 unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
405
406 dev_dbg(dev, "Setting key in context @%p for %s. keylen=%u\n",
407 ctx_p, crypto_tfm_alg_name(tfm), keylen);
408 dump_byte_array("key", key, keylen);
409
410 /* STAT_PHASE_0: Init and sanity checks */
411
412 if (validate_keys_sizes(ctx_p, keylen)) {
413 dev_dbg(dev, "Invalid key size %d.\n", keylen);
414 return -EINVAL;
415 }
416
417 if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
418
419 /* We only support 256 bit ESSIV-CBC-AES keys */
420 if (keylen != AES_KEYSIZE_256) {
421 unsigned int flags = crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_MASK;
422
423 if (likely(ctx_p->fallback_tfm)) {
424 ctx_p->fallback_on = true;
425 crypto_skcipher_clear_flags(ctx_p->fallback_tfm,
426 CRYPTO_TFM_REQ_MASK);
427 crypto_skcipher_clear_flags(ctx_p->fallback_tfm, flags);
428 return crypto_skcipher_setkey(ctx_p->fallback_tfm, key, keylen);
429 }
430
431 dev_dbg(dev, "Unsupported key size %d and no fallback.\n", keylen);
432 return -EINVAL;
433 }
434
435 /* Internal ESSIV key buffer is double sized */
436 max_key_buf_size <<= 1;
437 }
438
439 ctx_p->fallback_on = false;
440 ctx_p->key_type = CC_UNPROTECTED_KEY;
441
442 /*
443 * Verify DES weak keys
444 * Note that we're dropping the expanded key since the
445 * HW does the expansion on its own.
446 */
447 if (ctx_p->flow_mode == S_DIN_to_DES) {
448 if ((keylen == DES3_EDE_KEY_SIZE &&
449 verify_skcipher_des3_key(sktfm, key)) ||
450 verify_skcipher_des_key(sktfm, key)) {
451 dev_dbg(dev, "weak DES key");
452 return -EINVAL;
453 }
454 }
455
456 if (ctx_p->cipher_mode == DRV_CIPHER_XTS &&
457 xts_verify_key(sktfm, key, keylen)) {
458 dev_dbg(dev, "weak XTS key");
459 return -EINVAL;
460 }
461
462 /* STAT_PHASE_1: Copy key to ctx */
463 dma_sync_single_for_cpu(dev, ctx_p->user.key_dma_addr,
464 max_key_buf_size, DMA_TO_DEVICE);
465
466 memcpy(ctx_p->user.key, key, keylen);
467
468 if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
469 /* sha256 for key2 - use sw implementation */
470 int err;
471
472 err = crypto_shash_tfm_digest(ctx_p->shash_tfm,
473 ctx_p->user.key, keylen,
474 ctx_p->user.key + keylen);
475 if (err) {
476 dev_err(dev, "Failed to hash ESSIV key.\n");
477 return err;
478 }
479
480 keylen <<= 1;
481 }
482 dma_sync_single_for_device(dev, ctx_p->user.key_dma_addr,
483 max_key_buf_size, DMA_TO_DEVICE);
484 ctx_p->keylen = keylen;
485
486 dev_dbg(dev, "return safely");
487 return 0;
488}
489
490static int cc_out_setup_mode(struct cc_cipher_ctx *ctx_p)
491{
492 switch (ctx_p->flow_mode) {
493 case S_DIN_to_AES:
494 return S_AES_to_DOUT;
495 case S_DIN_to_DES:
496 return S_DES_to_DOUT;
497 case S_DIN_to_SM4:
498 return S_SM4_to_DOUT;
499 default:
500 return ctx_p->flow_mode;
501 }
502}
503
504static void cc_setup_readiv_desc(struct crypto_tfm *tfm,
505 struct cipher_req_ctx *req_ctx,
506 unsigned int ivsize, struct cc_hw_desc desc[],
507 unsigned int *seq_size)
508{
509 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
510 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
511 int cipher_mode = ctx_p->cipher_mode;
512 int flow_mode = cc_out_setup_mode(ctx_p);
513 int direction = req_ctx->gen_ctx.op_type;
514 dma_addr_t iv_dma_addr = req_ctx->gen_ctx.iv_dma_addr;
515
516 if (ctx_p->key_type == CC_POLICY_PROTECTED_KEY)
517 return;
518
519 switch (cipher_mode) {
520 case DRV_CIPHER_ECB:
521 break;
522 case DRV_CIPHER_CBC:
523 case DRV_CIPHER_CBC_CTS:
524 case DRV_CIPHER_CTR:
525 case DRV_CIPHER_OFB:
526 /* Read next IV */
527 hw_desc_init(&desc[*seq_size]);
528 set_dout_dlli(&desc[*seq_size], iv_dma_addr, ivsize, NS_BIT, 1);
529 set_cipher_config0(&desc[*seq_size], direction);
530 set_flow_mode(&desc[*seq_size], flow_mode);
531 set_cipher_mode(&desc[*seq_size], cipher_mode);
532 if (cipher_mode == DRV_CIPHER_CTR ||
533 cipher_mode == DRV_CIPHER_OFB) {
534 set_setup_mode(&desc[*seq_size], SETUP_WRITE_STATE1);
535 } else {
536 set_setup_mode(&desc[*seq_size], SETUP_WRITE_STATE0);
537 }
538 set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
539 (*seq_size)++;
540 break;
541 case DRV_CIPHER_XTS:
542 case DRV_CIPHER_ESSIV:
543 /* IV */
544 hw_desc_init(&desc[*seq_size]);
545 set_setup_mode(&desc[*seq_size], SETUP_WRITE_STATE1);
546 set_cipher_mode(&desc[*seq_size], cipher_mode);
547 set_cipher_config0(&desc[*seq_size], direction);
548 set_flow_mode(&desc[*seq_size], flow_mode);
549 set_dout_dlli(&desc[*seq_size], iv_dma_addr, CC_AES_BLOCK_SIZE,
550 NS_BIT, 1);
551 set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
552 (*seq_size)++;
553 break;
554 default:
555 dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
556 }
557}
558
559
560static void cc_setup_state_desc(struct crypto_tfm *tfm,
561 struct cipher_req_ctx *req_ctx,
562 unsigned int ivsize, unsigned int nbytes,
563 struct cc_hw_desc desc[],
564 unsigned int *seq_size)
565{
566 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
567 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
568 int cipher_mode = ctx_p->cipher_mode;
569 int flow_mode = ctx_p->flow_mode;
570 int direction = req_ctx->gen_ctx.op_type;
571 dma_addr_t iv_dma_addr = req_ctx->gen_ctx.iv_dma_addr;
572
573 switch (cipher_mode) {
574 case DRV_CIPHER_ECB:
575 break;
576 case DRV_CIPHER_CBC:
577 case DRV_CIPHER_CBC_CTS:
578 case DRV_CIPHER_CTR:
579 case DRV_CIPHER_OFB:
580 /* Load IV */
581 hw_desc_init(&desc[*seq_size]);
582 set_din_type(&desc[*seq_size], DMA_DLLI, iv_dma_addr, ivsize,
583 NS_BIT);
584 set_cipher_config0(&desc[*seq_size], direction);
585 set_flow_mode(&desc[*seq_size], flow_mode);
586 set_cipher_mode(&desc[*seq_size], cipher_mode);
587 if (cipher_mode == DRV_CIPHER_CTR ||
588 cipher_mode == DRV_CIPHER_OFB) {
589 set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE1);
590 } else {
591 set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE0);
592 }
593 (*seq_size)++;
594 break;
595 case DRV_CIPHER_XTS:
596 case DRV_CIPHER_ESSIV:
597 break;
598 default:
599 dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
600 }
601}
602
603
604static void cc_setup_xex_state_desc(struct crypto_tfm *tfm,
605 struct cipher_req_ctx *req_ctx,
606 unsigned int ivsize, unsigned int nbytes,
607 struct cc_hw_desc desc[],
608 unsigned int *seq_size)
609{
610 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
611 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
612 int cipher_mode = ctx_p->cipher_mode;
613 int flow_mode = ctx_p->flow_mode;
614 int direction = req_ctx->gen_ctx.op_type;
615 dma_addr_t key_dma_addr = ctx_p->user.key_dma_addr;
616 unsigned int key_len = (ctx_p->keylen / 2);
617 dma_addr_t iv_dma_addr = req_ctx->gen_ctx.iv_dma_addr;
618 unsigned int key_offset = key_len;
619
620 switch (cipher_mode) {
621 case DRV_CIPHER_ECB:
622 break;
623 case DRV_CIPHER_CBC:
624 case DRV_CIPHER_CBC_CTS:
625 case DRV_CIPHER_CTR:
626 case DRV_CIPHER_OFB:
627 break;
628 case DRV_CIPHER_XTS:
629 case DRV_CIPHER_ESSIV:
630
631 if (cipher_mode == DRV_CIPHER_ESSIV)
632 key_len = SHA256_DIGEST_SIZE;
633
634 /* load XEX key */
635 hw_desc_init(&desc[*seq_size]);
636 set_cipher_mode(&desc[*seq_size], cipher_mode);
637 set_cipher_config0(&desc[*seq_size], direction);
638 if (cc_key_type(tfm) == CC_HW_PROTECTED_KEY) {
639 set_hw_crypto_key(&desc[*seq_size],
640 ctx_p->hw.key2_slot);
641 } else {
642 set_din_type(&desc[*seq_size], DMA_DLLI,
643 (key_dma_addr + key_offset),
644 key_len, NS_BIT);
645 }
646 set_xex_data_unit_size(&desc[*seq_size], nbytes);
647 set_flow_mode(&desc[*seq_size], S_DIN_to_AES2);
648 set_key_size_aes(&desc[*seq_size], key_len);
649 set_setup_mode(&desc[*seq_size], SETUP_LOAD_XEX_KEY);
650 (*seq_size)++;
651
652 /* Load IV */
653 hw_desc_init(&desc[*seq_size]);
654 set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE1);
655 set_cipher_mode(&desc[*seq_size], cipher_mode);
656 set_cipher_config0(&desc[*seq_size], direction);
657 set_key_size_aes(&desc[*seq_size], key_len);
658 set_flow_mode(&desc[*seq_size], flow_mode);
659 set_din_type(&desc[*seq_size], DMA_DLLI, iv_dma_addr,
660 CC_AES_BLOCK_SIZE, NS_BIT);
661 (*seq_size)++;
662 break;
663 default:
664 dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
665 }
666}
667
668static int cc_out_flow_mode(struct cc_cipher_ctx *ctx_p)
669{
670 switch (ctx_p->flow_mode) {
671 case S_DIN_to_AES:
672 return DIN_AES_DOUT;
673 case S_DIN_to_DES:
674 return DIN_DES_DOUT;
675 case S_DIN_to_SM4:
676 return DIN_SM4_DOUT;
677 default:
678 return ctx_p->flow_mode;
679 }
680}
681
682static void cc_setup_key_desc(struct crypto_tfm *tfm,
683 struct cipher_req_ctx *req_ctx,
684 unsigned int nbytes, struct cc_hw_desc desc[],
685 unsigned int *seq_size)
686{
687 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
688 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
689 int cipher_mode = ctx_p->cipher_mode;
690 int flow_mode = ctx_p->flow_mode;
691 int direction = req_ctx->gen_ctx.op_type;
692 dma_addr_t key_dma_addr = ctx_p->user.key_dma_addr;
693 unsigned int key_len = ctx_p->keylen;
694 unsigned int din_size;
695
696 switch (cipher_mode) {
697 case DRV_CIPHER_CBC:
698 case DRV_CIPHER_CBC_CTS:
699 case DRV_CIPHER_CTR:
700 case DRV_CIPHER_OFB:
701 case DRV_CIPHER_ECB:
702 /* Load key */
703 hw_desc_init(&desc[*seq_size]);
704 set_cipher_mode(&desc[*seq_size], cipher_mode);
705 set_cipher_config0(&desc[*seq_size], direction);
706
707 if (cc_key_type(tfm) == CC_POLICY_PROTECTED_KEY) {
708 /* We use the AES key size coding for all CPP algs */
709 set_key_size_aes(&desc[*seq_size], key_len);
710 set_cpp_crypto_key(&desc[*seq_size], ctx_p->cpp.slot);
711 flow_mode = cc_out_flow_mode(ctx_p);
712 } else {
713 if (flow_mode == S_DIN_to_AES) {
714 if (cc_key_type(tfm) == CC_HW_PROTECTED_KEY) {
715 set_hw_crypto_key(&desc[*seq_size],
716 ctx_p->hw.key1_slot);
717 } else {
718 /* CC_POLICY_UNPROTECTED_KEY
719 * Invalid keys are filtered out in
720 * sethkey()
721 */
722 din_size = (key_len == 24) ?
723 AES_MAX_KEY_SIZE : key_len;
724
725 set_din_type(&desc[*seq_size], DMA_DLLI,
726 key_dma_addr, din_size,
727 NS_BIT);
728 }
729 set_key_size_aes(&desc[*seq_size], key_len);
730 } else {
731 /*des*/
732 set_din_type(&desc[*seq_size], DMA_DLLI,
733 key_dma_addr, key_len, NS_BIT);
734 set_key_size_des(&desc[*seq_size], key_len);
735 }
736 set_setup_mode(&desc[*seq_size], SETUP_LOAD_KEY0);
737 }
738 set_flow_mode(&desc[*seq_size], flow_mode);
739 (*seq_size)++;
740 break;
741 case DRV_CIPHER_XTS:
742 case DRV_CIPHER_ESSIV:
743 /* Load AES key */
744 hw_desc_init(&desc[*seq_size]);
745 set_cipher_mode(&desc[*seq_size], cipher_mode);
746 set_cipher_config0(&desc[*seq_size], direction);
747 if (cc_key_type(tfm) == CC_HW_PROTECTED_KEY) {
748 set_hw_crypto_key(&desc[*seq_size],
749 ctx_p->hw.key1_slot);
750 } else {
751 set_din_type(&desc[*seq_size], DMA_DLLI, key_dma_addr,
752 (key_len / 2), NS_BIT);
753 }
754 set_key_size_aes(&desc[*seq_size], (key_len / 2));
755 set_flow_mode(&desc[*seq_size], flow_mode);
756 set_setup_mode(&desc[*seq_size], SETUP_LOAD_KEY0);
757 (*seq_size)++;
758 break;
759 default:
760 dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
761 }
762}
763
764static void cc_setup_mlli_desc(struct crypto_tfm *tfm,
765 struct cipher_req_ctx *req_ctx,
766 struct scatterlist *dst, struct scatterlist *src,
767 unsigned int nbytes, void *areq,
768 struct cc_hw_desc desc[], unsigned int *seq_size)
769{
770 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
771 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
772
773 if (req_ctx->dma_buf_type == CC_DMA_BUF_MLLI) {
774 /* bypass */
775 dev_dbg(dev, " bypass params addr %pad length 0x%X addr 0x%08X\n",
776 &req_ctx->mlli_params.mlli_dma_addr,
777 req_ctx->mlli_params.mlli_len,
778 ctx_p->drvdata->mlli_sram_addr);
779 hw_desc_init(&desc[*seq_size]);
780 set_din_type(&desc[*seq_size], DMA_DLLI,
781 req_ctx->mlli_params.mlli_dma_addr,
782 req_ctx->mlli_params.mlli_len, NS_BIT);
783 set_dout_sram(&desc[*seq_size],
784 ctx_p->drvdata->mlli_sram_addr,
785 req_ctx->mlli_params.mlli_len);
786 set_flow_mode(&desc[*seq_size], BYPASS);
787 (*seq_size)++;
788 }
789}
790
791static void cc_setup_flow_desc(struct crypto_tfm *tfm,
792 struct cipher_req_ctx *req_ctx,
793 struct scatterlist *dst, struct scatterlist *src,
794 unsigned int nbytes, struct cc_hw_desc desc[],
795 unsigned int *seq_size)
796{
797 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
798 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
799 unsigned int flow_mode = cc_out_flow_mode(ctx_p);
800 bool last_desc = (ctx_p->key_type == CC_POLICY_PROTECTED_KEY ||
801 ctx_p->cipher_mode == DRV_CIPHER_ECB);
802
803 /* Process */
804 if (req_ctx->dma_buf_type == CC_DMA_BUF_DLLI) {
805 dev_dbg(dev, " data params addr %pad length 0x%X\n",
806 &sg_dma_address(src), nbytes);
807 dev_dbg(dev, " data params addr %pad length 0x%X\n",
808 &sg_dma_address(dst), nbytes);
809 hw_desc_init(&desc[*seq_size]);
810 set_din_type(&desc[*seq_size], DMA_DLLI, sg_dma_address(src),
811 nbytes, NS_BIT);
812 set_dout_dlli(&desc[*seq_size], sg_dma_address(dst),
813 nbytes, NS_BIT, (!last_desc ? 0 : 1));
814 if (last_desc)
815 set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
816
817 set_flow_mode(&desc[*seq_size], flow_mode);
818 (*seq_size)++;
819 } else {
820 hw_desc_init(&desc[*seq_size]);
821 set_din_type(&desc[*seq_size], DMA_MLLI,
822 ctx_p->drvdata->mlli_sram_addr,
823 req_ctx->in_mlli_nents, NS_BIT);
824 if (req_ctx->out_nents == 0) {
825 dev_dbg(dev, " din/dout params addr 0x%08X addr 0x%08X\n",
826 ctx_p->drvdata->mlli_sram_addr,
827 ctx_p->drvdata->mlli_sram_addr);
828 set_dout_mlli(&desc[*seq_size],
829 ctx_p->drvdata->mlli_sram_addr,
830 req_ctx->in_mlli_nents, NS_BIT,
831 (!last_desc ? 0 : 1));
832 } else {
833 dev_dbg(dev, " din/dout params addr 0x%08X addr 0x%08X\n",
834 ctx_p->drvdata->mlli_sram_addr,
835 ctx_p->drvdata->mlli_sram_addr +
836 (u32)LLI_ENTRY_BYTE_SIZE * req_ctx->in_nents);
837 set_dout_mlli(&desc[*seq_size],
838 (ctx_p->drvdata->mlli_sram_addr +
839 (LLI_ENTRY_BYTE_SIZE *
840 req_ctx->in_mlli_nents)),
841 req_ctx->out_mlli_nents, NS_BIT,
842 (!last_desc ? 0 : 1));
843 }
844 if (last_desc)
845 set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
846
847 set_flow_mode(&desc[*seq_size], flow_mode);
848 (*seq_size)++;
849 }
850}
851
852static void cc_cipher_complete(struct device *dev, void *cc_req, int err)
853{
854 struct skcipher_request *req = (struct skcipher_request *)cc_req;
855 struct scatterlist *dst = req->dst;
856 struct scatterlist *src = req->src;
857 struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
858 struct crypto_skcipher *sk_tfm = crypto_skcipher_reqtfm(req);
859 unsigned int ivsize = crypto_skcipher_ivsize(sk_tfm);
860
861 if (err != -EINPROGRESS) {
862 /* Not a BACKLOG notification */
863 cc_unmap_cipher_request(dev, req_ctx, ivsize, src, dst);
864 memcpy(req->iv, req_ctx->iv, ivsize);
865 kfree_sensitive(req_ctx->iv);
866 }
867
868 skcipher_request_complete(req, err);
869}
870
871static int cc_cipher_process(struct skcipher_request *req,
872 enum drv_crypto_direction direction)
873{
874 struct crypto_skcipher *sk_tfm = crypto_skcipher_reqtfm(req);
875 struct crypto_tfm *tfm = crypto_skcipher_tfm(sk_tfm);
876 struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
877 unsigned int ivsize = crypto_skcipher_ivsize(sk_tfm);
878 struct scatterlist *dst = req->dst;
879 struct scatterlist *src = req->src;
880 unsigned int nbytes = req->cryptlen;
881 void *iv = req->iv;
882 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
883 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
884 struct cc_hw_desc desc[MAX_SKCIPHER_SEQ_LEN];
885 struct cc_crypto_req cc_req = {};
886 int rc;
887 unsigned int seq_len = 0;
888 gfp_t flags = cc_gfp_flags(&req->base);
889
890 dev_dbg(dev, "%s req=%p iv=%p nbytes=%d\n",
891 ((direction == DRV_CRYPTO_DIRECTION_ENCRYPT) ?
892 "Encrypt" : "Decrypt"), req, iv, nbytes);
893
894 /* STAT_PHASE_0: Init and sanity checks */
895
896 if (validate_data_size(ctx_p, nbytes)) {
897 dev_dbg(dev, "Unsupported data size %d.\n", nbytes);
898 rc = -EINVAL;
899 goto exit_process;
900 }
901 if (nbytes == 0) {
902 /* No data to process is valid */
903 rc = 0;
904 goto exit_process;
905 }
906
907 if (ctx_p->fallback_on) {
908 struct skcipher_request *subreq = skcipher_request_ctx(req);
909
910 *subreq = *req;
911 skcipher_request_set_tfm(subreq, ctx_p->fallback_tfm);
912 if (direction == DRV_CRYPTO_DIRECTION_ENCRYPT)
913 return crypto_skcipher_encrypt(subreq);
914 else
915 return crypto_skcipher_decrypt(subreq);
916 }
917
918 /* The IV we are handed may be allocated from the stack so
919 * we must copy it to a DMAable buffer before use.
920 */
921 req_ctx->iv = kmemdup(iv, ivsize, flags);
922 if (!req_ctx->iv) {
923 rc = -ENOMEM;
924 goto exit_process;
925 }
926
927 /* Setup request structure */
928 cc_req.user_cb = cc_cipher_complete;
929 cc_req.user_arg = req;
930
931 /* Setup CPP operation details */
932 if (ctx_p->key_type == CC_POLICY_PROTECTED_KEY) {
933 cc_req.cpp.is_cpp = true;
934 cc_req.cpp.alg = ctx_p->cpp.alg;
935 cc_req.cpp.slot = ctx_p->cpp.slot;
936 }
937
938 /* Setup request context */
939 req_ctx->gen_ctx.op_type = direction;
940
941 /* STAT_PHASE_1: Map buffers */
942
943 rc = cc_map_cipher_request(ctx_p->drvdata, req_ctx, ivsize, nbytes,
944 req_ctx->iv, src, dst, flags);
945 if (rc) {
946 dev_err(dev, "map_request() failed\n");
947 goto exit_process;
948 }
949
950 /* STAT_PHASE_2: Create sequence */
951
952 /* Setup state (IV) */
953 cc_setup_state_desc(tfm, req_ctx, ivsize, nbytes, desc, &seq_len);
954 /* Setup MLLI line, if needed */
955 cc_setup_mlli_desc(tfm, req_ctx, dst, src, nbytes, req, desc, &seq_len);
956 /* Setup key */
957 cc_setup_key_desc(tfm, req_ctx, nbytes, desc, &seq_len);
958 /* Setup state (IV and XEX key) */
959 cc_setup_xex_state_desc(tfm, req_ctx, ivsize, nbytes, desc, &seq_len);
960 /* Data processing */
961 cc_setup_flow_desc(tfm, req_ctx, dst, src, nbytes, desc, &seq_len);
962 /* Read next IV */
963 cc_setup_readiv_desc(tfm, req_ctx, ivsize, desc, &seq_len);
964
965 /* STAT_PHASE_3: Lock HW and push sequence */
966
967 rc = cc_send_request(ctx_p->drvdata, &cc_req, desc, seq_len,
968 &req->base);
969 if (rc != -EINPROGRESS && rc != -EBUSY) {
970 /* Failed to send the request or request completed
971 * synchronously
972 */
973 cc_unmap_cipher_request(dev, req_ctx, ivsize, src, dst);
974 }
975
976exit_process:
977 if (rc != -EINPROGRESS && rc != -EBUSY) {
978 kfree_sensitive(req_ctx->iv);
979 }
980
981 return rc;
982}
983
984static int cc_cipher_encrypt(struct skcipher_request *req)
985{
986 struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
987
988 memset(req_ctx, 0, sizeof(*req_ctx));
989
990 return cc_cipher_process(req, DRV_CRYPTO_DIRECTION_ENCRYPT);
991}
992
993static int cc_cipher_decrypt(struct skcipher_request *req)
994{
995 struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
996
997 memset(req_ctx, 0, sizeof(*req_ctx));
998
999 return cc_cipher_process(req, DRV_CRYPTO_DIRECTION_DECRYPT);
1000}
1001
1002/* Block cipher alg */
1003static const struct cc_alg_template skcipher_algs[] = {
1004 {
1005 .name = "xts(paes)",
1006 .driver_name = "xts-paes-ccree",
1007 .blocksize = 1,
1008 .template_skcipher = {
1009 .setkey = cc_cipher_sethkey,
1010 .encrypt = cc_cipher_encrypt,
1011 .decrypt = cc_cipher_decrypt,
1012 .min_keysize = CC_HW_KEY_SIZE,
1013 .max_keysize = CC_HW_KEY_SIZE,
1014 .ivsize = AES_BLOCK_SIZE,
1015 },
1016 .cipher_mode = DRV_CIPHER_XTS,
1017 .flow_mode = S_DIN_to_AES,
1018 .min_hw_rev = CC_HW_REV_630,
1019 .std_body = CC_STD_NIST,
1020 .sec_func = true,
1021 },
1022 {
1023 .name = "essiv(cbc(paes),sha256)",
1024 .driver_name = "essiv-paes-ccree",
1025 .blocksize = AES_BLOCK_SIZE,
1026 .template_skcipher = {
1027 .setkey = cc_cipher_sethkey,
1028 .encrypt = cc_cipher_encrypt,
1029 .decrypt = cc_cipher_decrypt,
1030 .min_keysize = CC_HW_KEY_SIZE,
1031 .max_keysize = CC_HW_KEY_SIZE,
1032 .ivsize = AES_BLOCK_SIZE,
1033 },
1034 .cipher_mode = DRV_CIPHER_ESSIV,
1035 .flow_mode = S_DIN_to_AES,
1036 .min_hw_rev = CC_HW_REV_712,
1037 .std_body = CC_STD_NIST,
1038 .sec_func = true,
1039 },
1040 {
1041 .name = "ecb(paes)",
1042 .driver_name = "ecb-paes-ccree",
1043 .blocksize = AES_BLOCK_SIZE,
1044 .template_skcipher = {
1045 .setkey = cc_cipher_sethkey,
1046 .encrypt = cc_cipher_encrypt,
1047 .decrypt = cc_cipher_decrypt,
1048 .min_keysize = CC_HW_KEY_SIZE,
1049 .max_keysize = CC_HW_KEY_SIZE,
1050 .ivsize = 0,
1051 },
1052 .cipher_mode = DRV_CIPHER_ECB,
1053 .flow_mode = S_DIN_to_AES,
1054 .min_hw_rev = CC_HW_REV_712,
1055 .std_body = CC_STD_NIST,
1056 .sec_func = true,
1057 },
1058 {
1059 .name = "cbc(paes)",
1060 .driver_name = "cbc-paes-ccree",
1061 .blocksize = AES_BLOCK_SIZE,
1062 .template_skcipher = {
1063 .setkey = cc_cipher_sethkey,
1064 .encrypt = cc_cipher_encrypt,
1065 .decrypt = cc_cipher_decrypt,
1066 .min_keysize = CC_HW_KEY_SIZE,
1067 .max_keysize = CC_HW_KEY_SIZE,
1068 .ivsize = AES_BLOCK_SIZE,
1069 },
1070 .cipher_mode = DRV_CIPHER_CBC,
1071 .flow_mode = S_DIN_to_AES,
1072 .min_hw_rev = CC_HW_REV_712,
1073 .std_body = CC_STD_NIST,
1074 .sec_func = true,
1075 },
1076 {
1077 .name = "cts(cbc(paes))",
1078 .driver_name = "cts-cbc-paes-ccree",
1079 .blocksize = AES_BLOCK_SIZE,
1080 .template_skcipher = {
1081 .setkey = cc_cipher_sethkey,
1082 .encrypt = cc_cipher_encrypt,
1083 .decrypt = cc_cipher_decrypt,
1084 .min_keysize = CC_HW_KEY_SIZE,
1085 .max_keysize = CC_HW_KEY_SIZE,
1086 .ivsize = AES_BLOCK_SIZE,
1087 },
1088 .cipher_mode = DRV_CIPHER_CBC_CTS,
1089 .flow_mode = S_DIN_to_AES,
1090 .min_hw_rev = CC_HW_REV_712,
1091 .std_body = CC_STD_NIST,
1092 .sec_func = true,
1093 },
1094 {
1095 .name = "ctr(paes)",
1096 .driver_name = "ctr-paes-ccree",
1097 .blocksize = 1,
1098 .template_skcipher = {
1099 .setkey = cc_cipher_sethkey,
1100 .encrypt = cc_cipher_encrypt,
1101 .decrypt = cc_cipher_decrypt,
1102 .min_keysize = CC_HW_KEY_SIZE,
1103 .max_keysize = CC_HW_KEY_SIZE,
1104 .ivsize = AES_BLOCK_SIZE,
1105 },
1106 .cipher_mode = DRV_CIPHER_CTR,
1107 .flow_mode = S_DIN_to_AES,
1108 .min_hw_rev = CC_HW_REV_712,
1109 .std_body = CC_STD_NIST,
1110 .sec_func = true,
1111 },
1112 {
1113 /* See https://www.mail-archive.com/linux-crypto@vger.kernel.org/msg40576.html
1114 * for the reason why this differs from the generic
1115 * implementation.
1116 */
1117 .name = "xts(aes)",
1118 .driver_name = "xts-aes-ccree",
1119 .blocksize = 1,
1120 .template_skcipher = {
1121 .setkey = cc_cipher_setkey,
1122 .encrypt = cc_cipher_encrypt,
1123 .decrypt = cc_cipher_decrypt,
1124 .min_keysize = AES_MIN_KEY_SIZE * 2,
1125 .max_keysize = AES_MAX_KEY_SIZE * 2,
1126 .ivsize = AES_BLOCK_SIZE,
1127 },
1128 .cipher_mode = DRV_CIPHER_XTS,
1129 .flow_mode = S_DIN_to_AES,
1130 .min_hw_rev = CC_HW_REV_630,
1131 .std_body = CC_STD_NIST,
1132 },
1133 {
1134 .name = "essiv(cbc(aes),sha256)",
1135 .driver_name = "essiv-aes-ccree",
1136 .blocksize = AES_BLOCK_SIZE,
1137 .template_skcipher = {
1138 .setkey = cc_cipher_setkey,
1139 .encrypt = cc_cipher_encrypt,
1140 .decrypt = cc_cipher_decrypt,
1141 .min_keysize = AES_MIN_KEY_SIZE,
1142 .max_keysize = AES_MAX_KEY_SIZE,
1143 .ivsize = AES_BLOCK_SIZE,
1144 },
1145 .cipher_mode = DRV_CIPHER_ESSIV,
1146 .flow_mode = S_DIN_to_AES,
1147 .min_hw_rev = CC_HW_REV_712,
1148 .std_body = CC_STD_NIST,
1149 },
1150 {
1151 .name = "ecb(aes)",
1152 .driver_name = "ecb-aes-ccree",
1153 .blocksize = AES_BLOCK_SIZE,
1154 .template_skcipher = {
1155 .setkey = cc_cipher_setkey,
1156 .encrypt = cc_cipher_encrypt,
1157 .decrypt = cc_cipher_decrypt,
1158 .min_keysize = AES_MIN_KEY_SIZE,
1159 .max_keysize = AES_MAX_KEY_SIZE,
1160 .ivsize = 0,
1161 },
1162 .cipher_mode = DRV_CIPHER_ECB,
1163 .flow_mode = S_DIN_to_AES,
1164 .min_hw_rev = CC_HW_REV_630,
1165 .std_body = CC_STD_NIST,
1166 },
1167 {
1168 .name = "cbc(aes)",
1169 .driver_name = "cbc-aes-ccree",
1170 .blocksize = AES_BLOCK_SIZE,
1171 .template_skcipher = {
1172 .setkey = cc_cipher_setkey,
1173 .encrypt = cc_cipher_encrypt,
1174 .decrypt = cc_cipher_decrypt,
1175 .min_keysize = AES_MIN_KEY_SIZE,
1176 .max_keysize = AES_MAX_KEY_SIZE,
1177 .ivsize = AES_BLOCK_SIZE,
1178 },
1179 .cipher_mode = DRV_CIPHER_CBC,
1180 .flow_mode = S_DIN_to_AES,
1181 .min_hw_rev = CC_HW_REV_630,
1182 .std_body = CC_STD_NIST,
1183 },
1184 {
1185 .name = "cts(cbc(aes))",
1186 .driver_name = "cts-cbc-aes-ccree",
1187 .blocksize = AES_BLOCK_SIZE,
1188 .template_skcipher = {
1189 .setkey = cc_cipher_setkey,
1190 .encrypt = cc_cipher_encrypt,
1191 .decrypt = cc_cipher_decrypt,
1192 .min_keysize = AES_MIN_KEY_SIZE,
1193 .max_keysize = AES_MAX_KEY_SIZE,
1194 .ivsize = AES_BLOCK_SIZE,
1195 },
1196 .cipher_mode = DRV_CIPHER_CBC_CTS,
1197 .flow_mode = S_DIN_to_AES,
1198 .min_hw_rev = CC_HW_REV_630,
1199 .std_body = CC_STD_NIST,
1200 },
1201 {
1202 .name = "ctr(aes)",
1203 .driver_name = "ctr-aes-ccree",
1204 .blocksize = 1,
1205 .template_skcipher = {
1206 .setkey = cc_cipher_setkey,
1207 .encrypt = cc_cipher_encrypt,
1208 .decrypt = cc_cipher_decrypt,
1209 .min_keysize = AES_MIN_KEY_SIZE,
1210 .max_keysize = AES_MAX_KEY_SIZE,
1211 .ivsize = AES_BLOCK_SIZE,
1212 },
1213 .cipher_mode = DRV_CIPHER_CTR,
1214 .flow_mode = S_DIN_to_AES,
1215 .min_hw_rev = CC_HW_REV_630,
1216 .std_body = CC_STD_NIST,
1217 },
1218 {
1219 .name = "cbc(des3_ede)",
1220 .driver_name = "cbc-3des-ccree",
1221 .blocksize = DES3_EDE_BLOCK_SIZE,
1222 .template_skcipher = {
1223 .setkey = cc_cipher_setkey,
1224 .encrypt = cc_cipher_encrypt,
1225 .decrypt = cc_cipher_decrypt,
1226 .min_keysize = DES3_EDE_KEY_SIZE,
1227 .max_keysize = DES3_EDE_KEY_SIZE,
1228 .ivsize = DES3_EDE_BLOCK_SIZE,
1229 },
1230 .cipher_mode = DRV_CIPHER_CBC,
1231 .flow_mode = S_DIN_to_DES,
1232 .min_hw_rev = CC_HW_REV_630,
1233 .std_body = CC_STD_NIST,
1234 },
1235 {
1236 .name = "ecb(des3_ede)",
1237 .driver_name = "ecb-3des-ccree",
1238 .blocksize = DES3_EDE_BLOCK_SIZE,
1239 .template_skcipher = {
1240 .setkey = cc_cipher_setkey,
1241 .encrypt = cc_cipher_encrypt,
1242 .decrypt = cc_cipher_decrypt,
1243 .min_keysize = DES3_EDE_KEY_SIZE,
1244 .max_keysize = DES3_EDE_KEY_SIZE,
1245 .ivsize = 0,
1246 },
1247 .cipher_mode = DRV_CIPHER_ECB,
1248 .flow_mode = S_DIN_to_DES,
1249 .min_hw_rev = CC_HW_REV_630,
1250 .std_body = CC_STD_NIST,
1251 },
1252 {
1253 .name = "cbc(des)",
1254 .driver_name = "cbc-des-ccree",
1255 .blocksize = DES_BLOCK_SIZE,
1256 .template_skcipher = {
1257 .setkey = cc_cipher_setkey,
1258 .encrypt = cc_cipher_encrypt,
1259 .decrypt = cc_cipher_decrypt,
1260 .min_keysize = DES_KEY_SIZE,
1261 .max_keysize = DES_KEY_SIZE,
1262 .ivsize = DES_BLOCK_SIZE,
1263 },
1264 .cipher_mode = DRV_CIPHER_CBC,
1265 .flow_mode = S_DIN_to_DES,
1266 .min_hw_rev = CC_HW_REV_630,
1267 .std_body = CC_STD_NIST,
1268 },
1269 {
1270 .name = "ecb(des)",
1271 .driver_name = "ecb-des-ccree",
1272 .blocksize = DES_BLOCK_SIZE,
1273 .template_skcipher = {
1274 .setkey = cc_cipher_setkey,
1275 .encrypt = cc_cipher_encrypt,
1276 .decrypt = cc_cipher_decrypt,
1277 .min_keysize = DES_KEY_SIZE,
1278 .max_keysize = DES_KEY_SIZE,
1279 .ivsize = 0,
1280 },
1281 .cipher_mode = DRV_CIPHER_ECB,
1282 .flow_mode = S_DIN_to_DES,
1283 .min_hw_rev = CC_HW_REV_630,
1284 .std_body = CC_STD_NIST,
1285 },
1286 {
1287 .name = "cbc(sm4)",
1288 .driver_name = "cbc-sm4-ccree",
1289 .blocksize = SM4_BLOCK_SIZE,
1290 .template_skcipher = {
1291 .setkey = cc_cipher_setkey,
1292 .encrypt = cc_cipher_encrypt,
1293 .decrypt = cc_cipher_decrypt,
1294 .min_keysize = SM4_KEY_SIZE,
1295 .max_keysize = SM4_KEY_SIZE,
1296 .ivsize = SM4_BLOCK_SIZE,
1297 },
1298 .cipher_mode = DRV_CIPHER_CBC,
1299 .flow_mode = S_DIN_to_SM4,
1300 .min_hw_rev = CC_HW_REV_713,
1301 .std_body = CC_STD_OSCCA,
1302 },
1303 {
1304 .name = "ecb(sm4)",
1305 .driver_name = "ecb-sm4-ccree",
1306 .blocksize = SM4_BLOCK_SIZE,
1307 .template_skcipher = {
1308 .setkey = cc_cipher_setkey,
1309 .encrypt = cc_cipher_encrypt,
1310 .decrypt = cc_cipher_decrypt,
1311 .min_keysize = SM4_KEY_SIZE,
1312 .max_keysize = SM4_KEY_SIZE,
1313 .ivsize = 0,
1314 },
1315 .cipher_mode = DRV_CIPHER_ECB,
1316 .flow_mode = S_DIN_to_SM4,
1317 .min_hw_rev = CC_HW_REV_713,
1318 .std_body = CC_STD_OSCCA,
1319 },
1320 {
1321 .name = "ctr(sm4)",
1322 .driver_name = "ctr-sm4-ccree",
1323 .blocksize = 1,
1324 .template_skcipher = {
1325 .setkey = cc_cipher_setkey,
1326 .encrypt = cc_cipher_encrypt,
1327 .decrypt = cc_cipher_decrypt,
1328 .min_keysize = SM4_KEY_SIZE,
1329 .max_keysize = SM4_KEY_SIZE,
1330 .ivsize = SM4_BLOCK_SIZE,
1331 },
1332 .cipher_mode = DRV_CIPHER_CTR,
1333 .flow_mode = S_DIN_to_SM4,
1334 .min_hw_rev = CC_HW_REV_713,
1335 .std_body = CC_STD_OSCCA,
1336 },
1337 {
1338 .name = "cbc(psm4)",
1339 .driver_name = "cbc-psm4-ccree",
1340 .blocksize = SM4_BLOCK_SIZE,
1341 .template_skcipher = {
1342 .setkey = cc_cipher_sethkey,
1343 .encrypt = cc_cipher_encrypt,
1344 .decrypt = cc_cipher_decrypt,
1345 .min_keysize = CC_HW_KEY_SIZE,
1346 .max_keysize = CC_HW_KEY_SIZE,
1347 .ivsize = SM4_BLOCK_SIZE,
1348 },
1349 .cipher_mode = DRV_CIPHER_CBC,
1350 .flow_mode = S_DIN_to_SM4,
1351 .min_hw_rev = CC_HW_REV_713,
1352 .std_body = CC_STD_OSCCA,
1353 .sec_func = true,
1354 },
1355 {
1356 .name = "ctr(psm4)",
1357 .driver_name = "ctr-psm4-ccree",
1358 .blocksize = SM4_BLOCK_SIZE,
1359 .template_skcipher = {
1360 .setkey = cc_cipher_sethkey,
1361 .encrypt = cc_cipher_encrypt,
1362 .decrypt = cc_cipher_decrypt,
1363 .min_keysize = CC_HW_KEY_SIZE,
1364 .max_keysize = CC_HW_KEY_SIZE,
1365 .ivsize = SM4_BLOCK_SIZE,
1366 },
1367 .cipher_mode = DRV_CIPHER_CTR,
1368 .flow_mode = S_DIN_to_SM4,
1369 .min_hw_rev = CC_HW_REV_713,
1370 .std_body = CC_STD_OSCCA,
1371 .sec_func = true,
1372 },
1373};
1374
1375static struct cc_crypto_alg *cc_create_alg(const struct cc_alg_template *tmpl,
1376 struct device *dev)
1377{
1378 struct cc_crypto_alg *t_alg;
1379 struct skcipher_alg *alg;
1380
1381 t_alg = devm_kzalloc(dev, sizeof(*t_alg), GFP_KERNEL);
1382 if (!t_alg)
1383 return ERR_PTR(-ENOMEM);
1384
1385 alg = &t_alg->skcipher_alg;
1386
1387 memcpy(alg, &tmpl->template_skcipher, sizeof(*alg));
1388
1389 if (snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s",
1390 tmpl->name) >= CRYPTO_MAX_ALG_NAME)
1391 return ERR_PTR(-EINVAL);
1392 if (snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
1393 tmpl->driver_name) >= CRYPTO_MAX_ALG_NAME)
1394 return ERR_PTR(-EINVAL);
1395
1396 alg->base.cra_module = THIS_MODULE;
1397 alg->base.cra_priority = CC_CRA_PRIO;
1398 alg->base.cra_blocksize = tmpl->blocksize;
1399 alg->base.cra_alignmask = 0;
1400 alg->base.cra_ctxsize = sizeof(struct cc_cipher_ctx);
1401
1402 alg->base.cra_init = cc_cipher_init;
1403 alg->base.cra_exit = cc_cipher_exit;
1404 alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
1405
1406 t_alg->cipher_mode = tmpl->cipher_mode;
1407 t_alg->flow_mode = tmpl->flow_mode;
1408
1409 return t_alg;
1410}
1411
1412int cc_cipher_free(struct cc_drvdata *drvdata)
1413{
1414 struct cc_crypto_alg *t_alg, *n;
1415
1416 /* Remove registered algs */
1417 list_for_each_entry_safe(t_alg, n, &drvdata->alg_list, entry) {
1418 crypto_unregister_skcipher(&t_alg->skcipher_alg);
1419 list_del(&t_alg->entry);
1420 }
1421 return 0;
1422}
1423
1424int cc_cipher_alloc(struct cc_drvdata *drvdata)
1425{
1426 struct cc_crypto_alg *t_alg;
1427 struct device *dev = drvdata_to_dev(drvdata);
1428 int rc = -ENOMEM;
1429 int alg;
1430
1431 INIT_LIST_HEAD(&drvdata->alg_list);
1432
1433 /* Linux crypto */
1434 dev_dbg(dev, "Number of algorithms = %zu\n",
1435 ARRAY_SIZE(skcipher_algs));
1436 for (alg = 0; alg < ARRAY_SIZE(skcipher_algs); alg++) {
1437 if ((skcipher_algs[alg].min_hw_rev > drvdata->hw_rev) ||
1438 !(drvdata->std_bodies & skcipher_algs[alg].std_body) ||
1439 (drvdata->sec_disabled && skcipher_algs[alg].sec_func))
1440 continue;
1441
1442 dev_dbg(dev, "creating %s\n", skcipher_algs[alg].driver_name);
1443 t_alg = cc_create_alg(&skcipher_algs[alg], dev);
1444 if (IS_ERR(t_alg)) {
1445 rc = PTR_ERR(t_alg);
1446 dev_err(dev, "%s alg allocation failed\n",
1447 skcipher_algs[alg].driver_name);
1448 goto fail0;
1449 }
1450 t_alg->drvdata = drvdata;
1451
1452 dev_dbg(dev, "registering %s\n",
1453 skcipher_algs[alg].driver_name);
1454 rc = crypto_register_skcipher(&t_alg->skcipher_alg);
1455 dev_dbg(dev, "%s alg registration rc = %x\n",
1456 t_alg->skcipher_alg.base.cra_driver_name, rc);
1457 if (rc) {
1458 dev_err(dev, "%s alg registration failed\n",
1459 t_alg->skcipher_alg.base.cra_driver_name);
1460 goto fail0;
1461 }
1462
1463 list_add_tail(&t_alg->entry, &drvdata->alg_list);
1464 dev_dbg(dev, "Registered %s\n",
1465 t_alg->skcipher_alg.base.cra_driver_name);
1466 }
1467 return 0;
1468
1469fail0:
1470 cc_cipher_free(drvdata);
1471 return rc;
1472}
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (C) 2012-2019 ARM Limited (or its affiliates). */
3
4#include <linux/kernel.h>
5#include <linux/module.h>
6#include <crypto/algapi.h>
7#include <crypto/internal/skcipher.h>
8#include <crypto/internal/des.h>
9#include <crypto/xts.h>
10#include <crypto/sm4.h>
11#include <crypto/scatterwalk.h>
12
13#include "cc_driver.h"
14#include "cc_lli_defs.h"
15#include "cc_buffer_mgr.h"
16#include "cc_cipher.h"
17#include "cc_request_mgr.h"
18
19#define MAX_ABLKCIPHER_SEQ_LEN 6
20
21#define template_skcipher template_u.skcipher
22
23struct cc_cipher_handle {
24 struct list_head alg_list;
25};
26
27struct cc_user_key_info {
28 u8 *key;
29 dma_addr_t key_dma_addr;
30};
31
32struct cc_hw_key_info {
33 enum cc_hw_crypto_key key1_slot;
34 enum cc_hw_crypto_key key2_slot;
35};
36
37struct cc_cpp_key_info {
38 u8 slot;
39 enum cc_cpp_alg alg;
40};
41
42enum cc_key_type {
43 CC_UNPROTECTED_KEY, /* User key */
44 CC_HW_PROTECTED_KEY, /* HW (FDE) key */
45 CC_POLICY_PROTECTED_KEY, /* CPP key */
46 CC_INVALID_PROTECTED_KEY /* Invalid key */
47};
48
49struct cc_cipher_ctx {
50 struct cc_drvdata *drvdata;
51 int keylen;
52 int key_round_number;
53 int cipher_mode;
54 int flow_mode;
55 unsigned int flags;
56 enum cc_key_type key_type;
57 struct cc_user_key_info user;
58 union {
59 struct cc_hw_key_info hw;
60 struct cc_cpp_key_info cpp;
61 };
62 struct crypto_shash *shash_tfm;
63};
64
65static void cc_cipher_complete(struct device *dev, void *cc_req, int err);
66
67static inline enum cc_key_type cc_key_type(struct crypto_tfm *tfm)
68{
69 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
70
71 return ctx_p->key_type;
72}
73
74static int validate_keys_sizes(struct cc_cipher_ctx *ctx_p, u32 size)
75{
76 switch (ctx_p->flow_mode) {
77 case S_DIN_to_AES:
78 switch (size) {
79 case CC_AES_128_BIT_KEY_SIZE:
80 case CC_AES_192_BIT_KEY_SIZE:
81 if (ctx_p->cipher_mode != DRV_CIPHER_XTS &&
82 ctx_p->cipher_mode != DRV_CIPHER_ESSIV &&
83 ctx_p->cipher_mode != DRV_CIPHER_BITLOCKER)
84 return 0;
85 break;
86 case CC_AES_256_BIT_KEY_SIZE:
87 return 0;
88 case (CC_AES_192_BIT_KEY_SIZE * 2):
89 case (CC_AES_256_BIT_KEY_SIZE * 2):
90 if (ctx_p->cipher_mode == DRV_CIPHER_XTS ||
91 ctx_p->cipher_mode == DRV_CIPHER_ESSIV ||
92 ctx_p->cipher_mode == DRV_CIPHER_BITLOCKER)
93 return 0;
94 break;
95 default:
96 break;
97 }
98 break;
99 case S_DIN_to_DES:
100 if (size == DES3_EDE_KEY_SIZE || size == DES_KEY_SIZE)
101 return 0;
102 break;
103 case S_DIN_to_SM4:
104 if (size == SM4_KEY_SIZE)
105 return 0;
106 default:
107 break;
108 }
109 return -EINVAL;
110}
111
112static int validate_data_size(struct cc_cipher_ctx *ctx_p,
113 unsigned int size)
114{
115 switch (ctx_p->flow_mode) {
116 case S_DIN_to_AES:
117 switch (ctx_p->cipher_mode) {
118 case DRV_CIPHER_XTS:
119 case DRV_CIPHER_CBC_CTS:
120 if (size >= AES_BLOCK_SIZE)
121 return 0;
122 break;
123 case DRV_CIPHER_OFB:
124 case DRV_CIPHER_CTR:
125 return 0;
126 case DRV_CIPHER_ECB:
127 case DRV_CIPHER_CBC:
128 case DRV_CIPHER_ESSIV:
129 case DRV_CIPHER_BITLOCKER:
130 if (IS_ALIGNED(size, AES_BLOCK_SIZE))
131 return 0;
132 break;
133 default:
134 break;
135 }
136 break;
137 case S_DIN_to_DES:
138 if (IS_ALIGNED(size, DES_BLOCK_SIZE))
139 return 0;
140 break;
141 case S_DIN_to_SM4:
142 switch (ctx_p->cipher_mode) {
143 case DRV_CIPHER_CTR:
144 return 0;
145 case DRV_CIPHER_ECB:
146 case DRV_CIPHER_CBC:
147 if (IS_ALIGNED(size, SM4_BLOCK_SIZE))
148 return 0;
149 default:
150 break;
151 }
152 default:
153 break;
154 }
155 return -EINVAL;
156}
157
158static int cc_cipher_init(struct crypto_tfm *tfm)
159{
160 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
161 struct cc_crypto_alg *cc_alg =
162 container_of(tfm->__crt_alg, struct cc_crypto_alg,
163 skcipher_alg.base);
164 struct device *dev = drvdata_to_dev(cc_alg->drvdata);
165 unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
166 int rc = 0;
167
168 dev_dbg(dev, "Initializing context @%p for %s\n", ctx_p,
169 crypto_tfm_alg_name(tfm));
170
171 crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
172 sizeof(struct cipher_req_ctx));
173
174 ctx_p->cipher_mode = cc_alg->cipher_mode;
175 ctx_p->flow_mode = cc_alg->flow_mode;
176 ctx_p->drvdata = cc_alg->drvdata;
177
178 /* Allocate key buffer, cache line aligned */
179 ctx_p->user.key = kmalloc(max_key_buf_size, GFP_KERNEL);
180 if (!ctx_p->user.key)
181 return -ENOMEM;
182
183 dev_dbg(dev, "Allocated key buffer in context. key=@%p\n",
184 ctx_p->user.key);
185
186 /* Map key buffer */
187 ctx_p->user.key_dma_addr = dma_map_single(dev, (void *)ctx_p->user.key,
188 max_key_buf_size,
189 DMA_TO_DEVICE);
190 if (dma_mapping_error(dev, ctx_p->user.key_dma_addr)) {
191 dev_err(dev, "Mapping Key %u B at va=%pK for DMA failed\n",
192 max_key_buf_size, ctx_p->user.key);
193 return -ENOMEM;
194 }
195 dev_dbg(dev, "Mapped key %u B at va=%pK to dma=%pad\n",
196 max_key_buf_size, ctx_p->user.key, &ctx_p->user.key_dma_addr);
197
198 if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
199 /* Alloc hash tfm for essiv */
200 ctx_p->shash_tfm = crypto_alloc_shash("sha256-generic", 0, 0);
201 if (IS_ERR(ctx_p->shash_tfm)) {
202 dev_err(dev, "Error allocating hash tfm for ESSIV.\n");
203 return PTR_ERR(ctx_p->shash_tfm);
204 }
205 }
206
207 return rc;
208}
209
210static void cc_cipher_exit(struct crypto_tfm *tfm)
211{
212 struct crypto_alg *alg = tfm->__crt_alg;
213 struct cc_crypto_alg *cc_alg =
214 container_of(alg, struct cc_crypto_alg,
215 skcipher_alg.base);
216 unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
217 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
218 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
219
220 dev_dbg(dev, "Clearing context @%p for %s\n",
221 crypto_tfm_ctx(tfm), crypto_tfm_alg_name(tfm));
222
223 if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
224 /* Free hash tfm for essiv */
225 crypto_free_shash(ctx_p->shash_tfm);
226 ctx_p->shash_tfm = NULL;
227 }
228
229 /* Unmap key buffer */
230 dma_unmap_single(dev, ctx_p->user.key_dma_addr, max_key_buf_size,
231 DMA_TO_DEVICE);
232 dev_dbg(dev, "Unmapped key buffer key_dma_addr=%pad\n",
233 &ctx_p->user.key_dma_addr);
234
235 /* Free key buffer in context */
236 kzfree(ctx_p->user.key);
237 dev_dbg(dev, "Free key buffer in context. key=@%p\n", ctx_p->user.key);
238}
239
240struct tdes_keys {
241 u8 key1[DES_KEY_SIZE];
242 u8 key2[DES_KEY_SIZE];
243 u8 key3[DES_KEY_SIZE];
244};
245
246static enum cc_hw_crypto_key cc_slot_to_hw_key(u8 slot_num)
247{
248 switch (slot_num) {
249 case 0:
250 return KFDE0_KEY;
251 case 1:
252 return KFDE1_KEY;
253 case 2:
254 return KFDE2_KEY;
255 case 3:
256 return KFDE3_KEY;
257 }
258 return END_OF_KEYS;
259}
260
261static u8 cc_slot_to_cpp_key(u8 slot_num)
262{
263 return (slot_num - CC_FIRST_CPP_KEY_SLOT);
264}
265
266static inline enum cc_key_type cc_slot_to_key_type(u8 slot_num)
267{
268 if (slot_num >= CC_FIRST_HW_KEY_SLOT && slot_num <= CC_LAST_HW_KEY_SLOT)
269 return CC_HW_PROTECTED_KEY;
270 else if (slot_num >= CC_FIRST_CPP_KEY_SLOT &&
271 slot_num <= CC_LAST_CPP_KEY_SLOT)
272 return CC_POLICY_PROTECTED_KEY;
273 else
274 return CC_INVALID_PROTECTED_KEY;
275}
276
277static int cc_cipher_sethkey(struct crypto_skcipher *sktfm, const u8 *key,
278 unsigned int keylen)
279{
280 struct crypto_tfm *tfm = crypto_skcipher_tfm(sktfm);
281 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
282 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
283 struct cc_hkey_info hki;
284
285 dev_dbg(dev, "Setting HW key in context @%p for %s. keylen=%u\n",
286 ctx_p, crypto_tfm_alg_name(tfm), keylen);
287 dump_byte_array("key", (u8 *)key, keylen);
288
289 /* STAT_PHASE_0: Init and sanity checks */
290
291 /* This check the size of the protected key token */
292 if (keylen != sizeof(hki)) {
293 dev_err(dev, "Unsupported protected key size %d.\n", keylen);
294 crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
295 return -EINVAL;
296 }
297
298 memcpy(&hki, key, keylen);
299
300 /* The real key len for crypto op is the size of the HW key
301 * referenced by the HW key slot, not the hardware key token
302 */
303 keylen = hki.keylen;
304
305 if (validate_keys_sizes(ctx_p, keylen)) {
306 dev_err(dev, "Unsupported key size %d.\n", keylen);
307 crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
308 return -EINVAL;
309 }
310
311 ctx_p->keylen = keylen;
312
313 switch (cc_slot_to_key_type(hki.hw_key1)) {
314 case CC_HW_PROTECTED_KEY:
315 if (ctx_p->flow_mode == S_DIN_to_SM4) {
316 dev_err(dev, "Only AES HW protected keys are supported\n");
317 return -EINVAL;
318 }
319
320 ctx_p->hw.key1_slot = cc_slot_to_hw_key(hki.hw_key1);
321 if (ctx_p->hw.key1_slot == END_OF_KEYS) {
322 dev_err(dev, "Unsupported hw key1 number (%d)\n",
323 hki.hw_key1);
324 return -EINVAL;
325 }
326
327 if (ctx_p->cipher_mode == DRV_CIPHER_XTS ||
328 ctx_p->cipher_mode == DRV_CIPHER_ESSIV ||
329 ctx_p->cipher_mode == DRV_CIPHER_BITLOCKER) {
330 if (hki.hw_key1 == hki.hw_key2) {
331 dev_err(dev, "Illegal hw key numbers (%d,%d)\n",
332 hki.hw_key1, hki.hw_key2);
333 return -EINVAL;
334 }
335
336 ctx_p->hw.key2_slot = cc_slot_to_hw_key(hki.hw_key2);
337 if (ctx_p->hw.key2_slot == END_OF_KEYS) {
338 dev_err(dev, "Unsupported hw key2 number (%d)\n",
339 hki.hw_key2);
340 return -EINVAL;
341 }
342 }
343
344 ctx_p->key_type = CC_HW_PROTECTED_KEY;
345 dev_dbg(dev, "HW protected key %d/%d set\n.",
346 ctx_p->hw.key1_slot, ctx_p->hw.key2_slot);
347 break;
348
349 case CC_POLICY_PROTECTED_KEY:
350 if (ctx_p->drvdata->hw_rev < CC_HW_REV_713) {
351 dev_err(dev, "CPP keys not supported in this hardware revision.\n");
352 return -EINVAL;
353 }
354
355 if (ctx_p->cipher_mode != DRV_CIPHER_CBC &&
356 ctx_p->cipher_mode != DRV_CIPHER_CTR) {
357 dev_err(dev, "CPP keys only supported in CBC or CTR modes.\n");
358 return -EINVAL;
359 }
360
361 ctx_p->cpp.slot = cc_slot_to_cpp_key(hki.hw_key1);
362 if (ctx_p->flow_mode == S_DIN_to_AES)
363 ctx_p->cpp.alg = CC_CPP_AES;
364 else /* Must be SM4 since due to sethkey registration */
365 ctx_p->cpp.alg = CC_CPP_SM4;
366 ctx_p->key_type = CC_POLICY_PROTECTED_KEY;
367 dev_dbg(dev, "policy protected key alg: %d slot: %d.\n",
368 ctx_p->cpp.alg, ctx_p->cpp.slot);
369 break;
370
371 default:
372 dev_err(dev, "Unsupported protected key (%d)\n", hki.hw_key1);
373 return -EINVAL;
374 }
375
376 return 0;
377}
378
379static int cc_cipher_setkey(struct crypto_skcipher *sktfm, const u8 *key,
380 unsigned int keylen)
381{
382 struct crypto_tfm *tfm = crypto_skcipher_tfm(sktfm);
383 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
384 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
385 struct cc_crypto_alg *cc_alg =
386 container_of(tfm->__crt_alg, struct cc_crypto_alg,
387 skcipher_alg.base);
388 unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
389
390 dev_dbg(dev, "Setting key in context @%p for %s. keylen=%u\n",
391 ctx_p, crypto_tfm_alg_name(tfm), keylen);
392 dump_byte_array("key", (u8 *)key, keylen);
393
394 /* STAT_PHASE_0: Init and sanity checks */
395
396 if (validate_keys_sizes(ctx_p, keylen)) {
397 dev_err(dev, "Unsupported key size %d.\n", keylen);
398 crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
399 return -EINVAL;
400 }
401
402 ctx_p->key_type = CC_UNPROTECTED_KEY;
403
404 /*
405 * Verify DES weak keys
406 * Note that we're dropping the expanded key since the
407 * HW does the expansion on its own.
408 */
409 if (ctx_p->flow_mode == S_DIN_to_DES) {
410 if ((keylen == DES3_EDE_KEY_SIZE &&
411 verify_skcipher_des3_key(sktfm, key)) ||
412 verify_skcipher_des_key(sktfm, key)) {
413 dev_dbg(dev, "weak DES key");
414 return -EINVAL;
415 }
416 }
417
418 if (ctx_p->cipher_mode == DRV_CIPHER_XTS &&
419 xts_check_key(tfm, key, keylen)) {
420 dev_dbg(dev, "weak XTS key");
421 return -EINVAL;
422 }
423
424 /* STAT_PHASE_1: Copy key to ctx */
425 dma_sync_single_for_cpu(dev, ctx_p->user.key_dma_addr,
426 max_key_buf_size, DMA_TO_DEVICE);
427
428 memcpy(ctx_p->user.key, key, keylen);
429 if (keylen == 24)
430 memset(ctx_p->user.key + 24, 0, CC_AES_KEY_SIZE_MAX - 24);
431
432 if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
433 /* sha256 for key2 - use sw implementation */
434 int key_len = keylen >> 1;
435 int err;
436
437 SHASH_DESC_ON_STACK(desc, ctx_p->shash_tfm);
438
439 desc->tfm = ctx_p->shash_tfm;
440
441 err = crypto_shash_digest(desc, ctx_p->user.key, key_len,
442 ctx_p->user.key + key_len);
443 if (err) {
444 dev_err(dev, "Failed to hash ESSIV key.\n");
445 return err;
446 }
447 }
448 dma_sync_single_for_device(dev, ctx_p->user.key_dma_addr,
449 max_key_buf_size, DMA_TO_DEVICE);
450 ctx_p->keylen = keylen;
451
452 dev_dbg(dev, "return safely");
453 return 0;
454}
455
456static int cc_out_setup_mode(struct cc_cipher_ctx *ctx_p)
457{
458 switch (ctx_p->flow_mode) {
459 case S_DIN_to_AES:
460 return S_AES_to_DOUT;
461 case S_DIN_to_DES:
462 return S_DES_to_DOUT;
463 case S_DIN_to_SM4:
464 return S_SM4_to_DOUT;
465 default:
466 return ctx_p->flow_mode;
467 }
468}
469
470static void cc_setup_readiv_desc(struct crypto_tfm *tfm,
471 struct cipher_req_ctx *req_ctx,
472 unsigned int ivsize, struct cc_hw_desc desc[],
473 unsigned int *seq_size)
474{
475 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
476 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
477 int cipher_mode = ctx_p->cipher_mode;
478 int flow_mode = cc_out_setup_mode(ctx_p);
479 int direction = req_ctx->gen_ctx.op_type;
480 dma_addr_t iv_dma_addr = req_ctx->gen_ctx.iv_dma_addr;
481
482 if (ctx_p->key_type == CC_POLICY_PROTECTED_KEY)
483 return;
484
485 switch (cipher_mode) {
486 case DRV_CIPHER_ECB:
487 break;
488 case DRV_CIPHER_CBC:
489 case DRV_CIPHER_CBC_CTS:
490 case DRV_CIPHER_CTR:
491 case DRV_CIPHER_OFB:
492 /* Read next IV */
493 hw_desc_init(&desc[*seq_size]);
494 set_dout_dlli(&desc[*seq_size], iv_dma_addr, ivsize, NS_BIT, 1);
495 set_cipher_config0(&desc[*seq_size], direction);
496 set_flow_mode(&desc[*seq_size], flow_mode);
497 set_cipher_mode(&desc[*seq_size], cipher_mode);
498 if (cipher_mode == DRV_CIPHER_CTR ||
499 cipher_mode == DRV_CIPHER_OFB) {
500 set_setup_mode(&desc[*seq_size], SETUP_WRITE_STATE1);
501 } else {
502 set_setup_mode(&desc[*seq_size], SETUP_WRITE_STATE0);
503 }
504 set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
505 (*seq_size)++;
506 break;
507 case DRV_CIPHER_XTS:
508 case DRV_CIPHER_ESSIV:
509 case DRV_CIPHER_BITLOCKER:
510 /* IV */
511 hw_desc_init(&desc[*seq_size]);
512 set_setup_mode(&desc[*seq_size], SETUP_WRITE_STATE1);
513 set_cipher_mode(&desc[*seq_size], cipher_mode);
514 set_cipher_config0(&desc[*seq_size], direction);
515 set_flow_mode(&desc[*seq_size], flow_mode);
516 set_dout_dlli(&desc[*seq_size], iv_dma_addr, CC_AES_BLOCK_SIZE,
517 NS_BIT, 1);
518 set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
519 (*seq_size)++;
520 break;
521 default:
522 dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
523 }
524}
525
526static void cc_setup_state_desc(struct crypto_tfm *tfm,
527 struct cipher_req_ctx *req_ctx,
528 unsigned int ivsize, unsigned int nbytes,
529 struct cc_hw_desc desc[],
530 unsigned int *seq_size)
531{
532 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
533 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
534 int cipher_mode = ctx_p->cipher_mode;
535 int flow_mode = ctx_p->flow_mode;
536 int direction = req_ctx->gen_ctx.op_type;
537 dma_addr_t key_dma_addr = ctx_p->user.key_dma_addr;
538 unsigned int key_len = ctx_p->keylen;
539 dma_addr_t iv_dma_addr = req_ctx->gen_ctx.iv_dma_addr;
540 unsigned int du_size = nbytes;
541
542 struct cc_crypto_alg *cc_alg =
543 container_of(tfm->__crt_alg, struct cc_crypto_alg,
544 skcipher_alg.base);
545
546 if (cc_alg->data_unit)
547 du_size = cc_alg->data_unit;
548
549 switch (cipher_mode) {
550 case DRV_CIPHER_ECB:
551 break;
552 case DRV_CIPHER_CBC:
553 case DRV_CIPHER_CBC_CTS:
554 case DRV_CIPHER_CTR:
555 case DRV_CIPHER_OFB:
556 /* Load IV */
557 hw_desc_init(&desc[*seq_size]);
558 set_din_type(&desc[*seq_size], DMA_DLLI, iv_dma_addr, ivsize,
559 NS_BIT);
560 set_cipher_config0(&desc[*seq_size], direction);
561 set_flow_mode(&desc[*seq_size], flow_mode);
562 set_cipher_mode(&desc[*seq_size], cipher_mode);
563 if (cipher_mode == DRV_CIPHER_CTR ||
564 cipher_mode == DRV_CIPHER_OFB) {
565 set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE1);
566 } else {
567 set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE0);
568 }
569 (*seq_size)++;
570 break;
571 case DRV_CIPHER_XTS:
572 case DRV_CIPHER_ESSIV:
573 case DRV_CIPHER_BITLOCKER:
574 /* load XEX key */
575 hw_desc_init(&desc[*seq_size]);
576 set_cipher_mode(&desc[*seq_size], cipher_mode);
577 set_cipher_config0(&desc[*seq_size], direction);
578 if (cc_key_type(tfm) == CC_HW_PROTECTED_KEY) {
579 set_hw_crypto_key(&desc[*seq_size],
580 ctx_p->hw.key2_slot);
581 } else {
582 set_din_type(&desc[*seq_size], DMA_DLLI,
583 (key_dma_addr + (key_len / 2)),
584 (key_len / 2), NS_BIT);
585 }
586 set_xex_data_unit_size(&desc[*seq_size], du_size);
587 set_flow_mode(&desc[*seq_size], S_DIN_to_AES2);
588 set_key_size_aes(&desc[*seq_size], (key_len / 2));
589 set_setup_mode(&desc[*seq_size], SETUP_LOAD_XEX_KEY);
590 (*seq_size)++;
591
592 /* Load IV */
593 hw_desc_init(&desc[*seq_size]);
594 set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE1);
595 set_cipher_mode(&desc[*seq_size], cipher_mode);
596 set_cipher_config0(&desc[*seq_size], direction);
597 set_key_size_aes(&desc[*seq_size], (key_len / 2));
598 set_flow_mode(&desc[*seq_size], flow_mode);
599 set_din_type(&desc[*seq_size], DMA_DLLI, iv_dma_addr,
600 CC_AES_BLOCK_SIZE, NS_BIT);
601 (*seq_size)++;
602 break;
603 default:
604 dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
605 }
606}
607
608static int cc_out_flow_mode(struct cc_cipher_ctx *ctx_p)
609{
610 switch (ctx_p->flow_mode) {
611 case S_DIN_to_AES:
612 return DIN_AES_DOUT;
613 case S_DIN_to_DES:
614 return DIN_DES_DOUT;
615 case S_DIN_to_SM4:
616 return DIN_SM4_DOUT;
617 default:
618 return ctx_p->flow_mode;
619 }
620}
621
622static void cc_setup_key_desc(struct crypto_tfm *tfm,
623 struct cipher_req_ctx *req_ctx,
624 unsigned int nbytes, struct cc_hw_desc desc[],
625 unsigned int *seq_size)
626{
627 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
628 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
629 int cipher_mode = ctx_p->cipher_mode;
630 int flow_mode = ctx_p->flow_mode;
631 int direction = req_ctx->gen_ctx.op_type;
632 dma_addr_t key_dma_addr = ctx_p->user.key_dma_addr;
633 unsigned int key_len = ctx_p->keylen;
634 unsigned int din_size;
635
636 switch (cipher_mode) {
637 case DRV_CIPHER_CBC:
638 case DRV_CIPHER_CBC_CTS:
639 case DRV_CIPHER_CTR:
640 case DRV_CIPHER_OFB:
641 case DRV_CIPHER_ECB:
642 /* Load key */
643 hw_desc_init(&desc[*seq_size]);
644 set_cipher_mode(&desc[*seq_size], cipher_mode);
645 set_cipher_config0(&desc[*seq_size], direction);
646
647 if (cc_key_type(tfm) == CC_POLICY_PROTECTED_KEY) {
648 /* We use the AES key size coding for all CPP algs */
649 set_key_size_aes(&desc[*seq_size], key_len);
650 set_cpp_crypto_key(&desc[*seq_size], ctx_p->cpp.slot);
651 flow_mode = cc_out_flow_mode(ctx_p);
652 } else {
653 if (flow_mode == S_DIN_to_AES) {
654 if (cc_key_type(tfm) == CC_HW_PROTECTED_KEY) {
655 set_hw_crypto_key(&desc[*seq_size],
656 ctx_p->hw.key1_slot);
657 } else {
658 /* CC_POLICY_UNPROTECTED_KEY
659 * Invalid keys are filtered out in
660 * sethkey()
661 */
662 din_size = (key_len == 24) ?
663 AES_MAX_KEY_SIZE : key_len;
664
665 set_din_type(&desc[*seq_size], DMA_DLLI,
666 key_dma_addr, din_size,
667 NS_BIT);
668 }
669 set_key_size_aes(&desc[*seq_size], key_len);
670 } else {
671 /*des*/
672 set_din_type(&desc[*seq_size], DMA_DLLI,
673 key_dma_addr, key_len, NS_BIT);
674 set_key_size_des(&desc[*seq_size], key_len);
675 }
676 set_setup_mode(&desc[*seq_size], SETUP_LOAD_KEY0);
677 }
678 set_flow_mode(&desc[*seq_size], flow_mode);
679 (*seq_size)++;
680 break;
681 case DRV_CIPHER_XTS:
682 case DRV_CIPHER_ESSIV:
683 case DRV_CIPHER_BITLOCKER:
684 /* Load AES key */
685 hw_desc_init(&desc[*seq_size]);
686 set_cipher_mode(&desc[*seq_size], cipher_mode);
687 set_cipher_config0(&desc[*seq_size], direction);
688 if (cc_key_type(tfm) == CC_HW_PROTECTED_KEY) {
689 set_hw_crypto_key(&desc[*seq_size],
690 ctx_p->hw.key1_slot);
691 } else {
692 set_din_type(&desc[*seq_size], DMA_DLLI, key_dma_addr,
693 (key_len / 2), NS_BIT);
694 }
695 set_key_size_aes(&desc[*seq_size], (key_len / 2));
696 set_flow_mode(&desc[*seq_size], flow_mode);
697 set_setup_mode(&desc[*seq_size], SETUP_LOAD_KEY0);
698 (*seq_size)++;
699 break;
700 default:
701 dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
702 }
703}
704
705static void cc_setup_mlli_desc(struct crypto_tfm *tfm,
706 struct cipher_req_ctx *req_ctx,
707 struct scatterlist *dst, struct scatterlist *src,
708 unsigned int nbytes, void *areq,
709 struct cc_hw_desc desc[], unsigned int *seq_size)
710{
711 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
712 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
713
714 if (req_ctx->dma_buf_type == CC_DMA_BUF_MLLI) {
715 /* bypass */
716 dev_dbg(dev, " bypass params addr %pad length 0x%X addr 0x%08X\n",
717 &req_ctx->mlli_params.mlli_dma_addr,
718 req_ctx->mlli_params.mlli_len,
719 (unsigned int)ctx_p->drvdata->mlli_sram_addr);
720 hw_desc_init(&desc[*seq_size]);
721 set_din_type(&desc[*seq_size], DMA_DLLI,
722 req_ctx->mlli_params.mlli_dma_addr,
723 req_ctx->mlli_params.mlli_len, NS_BIT);
724 set_dout_sram(&desc[*seq_size],
725 ctx_p->drvdata->mlli_sram_addr,
726 req_ctx->mlli_params.mlli_len);
727 set_flow_mode(&desc[*seq_size], BYPASS);
728 (*seq_size)++;
729 }
730}
731
732static void cc_setup_flow_desc(struct crypto_tfm *tfm,
733 struct cipher_req_ctx *req_ctx,
734 struct scatterlist *dst, struct scatterlist *src,
735 unsigned int nbytes, struct cc_hw_desc desc[],
736 unsigned int *seq_size)
737{
738 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
739 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
740 unsigned int flow_mode = cc_out_flow_mode(ctx_p);
741 bool last_desc = (ctx_p->key_type == CC_POLICY_PROTECTED_KEY ||
742 ctx_p->cipher_mode == DRV_CIPHER_ECB);
743
744 /* Process */
745 if (req_ctx->dma_buf_type == CC_DMA_BUF_DLLI) {
746 dev_dbg(dev, " data params addr %pad length 0x%X\n",
747 &sg_dma_address(src), nbytes);
748 dev_dbg(dev, " data params addr %pad length 0x%X\n",
749 &sg_dma_address(dst), nbytes);
750 hw_desc_init(&desc[*seq_size]);
751 set_din_type(&desc[*seq_size], DMA_DLLI, sg_dma_address(src),
752 nbytes, NS_BIT);
753 set_dout_dlli(&desc[*seq_size], sg_dma_address(dst),
754 nbytes, NS_BIT, (!last_desc ? 0 : 1));
755 if (last_desc)
756 set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
757
758 set_flow_mode(&desc[*seq_size], flow_mode);
759 (*seq_size)++;
760 } else {
761 hw_desc_init(&desc[*seq_size]);
762 set_din_type(&desc[*seq_size], DMA_MLLI,
763 ctx_p->drvdata->mlli_sram_addr,
764 req_ctx->in_mlli_nents, NS_BIT);
765 if (req_ctx->out_nents == 0) {
766 dev_dbg(dev, " din/dout params addr 0x%08X addr 0x%08X\n",
767 (unsigned int)ctx_p->drvdata->mlli_sram_addr,
768 (unsigned int)ctx_p->drvdata->mlli_sram_addr);
769 set_dout_mlli(&desc[*seq_size],
770 ctx_p->drvdata->mlli_sram_addr,
771 req_ctx->in_mlli_nents, NS_BIT,
772 (!last_desc ? 0 : 1));
773 } else {
774 dev_dbg(dev, " din/dout params addr 0x%08X addr 0x%08X\n",
775 (unsigned int)ctx_p->drvdata->mlli_sram_addr,
776 (unsigned int)ctx_p->drvdata->mlli_sram_addr +
777 (u32)LLI_ENTRY_BYTE_SIZE * req_ctx->in_nents);
778 set_dout_mlli(&desc[*seq_size],
779 (ctx_p->drvdata->mlli_sram_addr +
780 (LLI_ENTRY_BYTE_SIZE *
781 req_ctx->in_mlli_nents)),
782 req_ctx->out_mlli_nents, NS_BIT,
783 (!last_desc ? 0 : 1));
784 }
785 if (last_desc)
786 set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
787
788 set_flow_mode(&desc[*seq_size], flow_mode);
789 (*seq_size)++;
790 }
791}
792
793static void cc_cipher_complete(struct device *dev, void *cc_req, int err)
794{
795 struct skcipher_request *req = (struct skcipher_request *)cc_req;
796 struct scatterlist *dst = req->dst;
797 struct scatterlist *src = req->src;
798 struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
799 struct crypto_skcipher *sk_tfm = crypto_skcipher_reqtfm(req);
800 unsigned int ivsize = crypto_skcipher_ivsize(sk_tfm);
801
802 if (err != -EINPROGRESS) {
803 /* Not a BACKLOG notification */
804 cc_unmap_cipher_request(dev, req_ctx, ivsize, src, dst);
805 memcpy(req->iv, req_ctx->iv, ivsize);
806 kzfree(req_ctx->iv);
807 }
808
809 skcipher_request_complete(req, err);
810}
811
812static int cc_cipher_process(struct skcipher_request *req,
813 enum drv_crypto_direction direction)
814{
815 struct crypto_skcipher *sk_tfm = crypto_skcipher_reqtfm(req);
816 struct crypto_tfm *tfm = crypto_skcipher_tfm(sk_tfm);
817 struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
818 unsigned int ivsize = crypto_skcipher_ivsize(sk_tfm);
819 struct scatterlist *dst = req->dst;
820 struct scatterlist *src = req->src;
821 unsigned int nbytes = req->cryptlen;
822 void *iv = req->iv;
823 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
824 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
825 struct cc_hw_desc desc[MAX_ABLKCIPHER_SEQ_LEN];
826 struct cc_crypto_req cc_req = {};
827 int rc;
828 unsigned int seq_len = 0;
829 gfp_t flags = cc_gfp_flags(&req->base);
830
831 dev_dbg(dev, "%s req=%p iv=%p nbytes=%d\n",
832 ((direction == DRV_CRYPTO_DIRECTION_ENCRYPT) ?
833 "Encrypt" : "Decrypt"), req, iv, nbytes);
834
835 /* STAT_PHASE_0: Init and sanity checks */
836
837 /* TODO: check data length according to mode */
838 if (validate_data_size(ctx_p, nbytes)) {
839 dev_err(dev, "Unsupported data size %d.\n", nbytes);
840 crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_BLOCK_LEN);
841 rc = -EINVAL;
842 goto exit_process;
843 }
844 if (nbytes == 0) {
845 /* No data to process is valid */
846 rc = 0;
847 goto exit_process;
848 }
849
850 /* The IV we are handed may be allocted from the stack so
851 * we must copy it to a DMAable buffer before use.
852 */
853 req_ctx->iv = kmemdup(iv, ivsize, flags);
854 if (!req_ctx->iv) {
855 rc = -ENOMEM;
856 goto exit_process;
857 }
858
859 /* Setup request structure */
860 cc_req.user_cb = (void *)cc_cipher_complete;
861 cc_req.user_arg = (void *)req;
862
863 /* Setup CPP operation details */
864 if (ctx_p->key_type == CC_POLICY_PROTECTED_KEY) {
865 cc_req.cpp.is_cpp = true;
866 cc_req.cpp.alg = ctx_p->cpp.alg;
867 cc_req.cpp.slot = ctx_p->cpp.slot;
868 }
869
870 /* Setup request context */
871 req_ctx->gen_ctx.op_type = direction;
872
873 /* STAT_PHASE_1: Map buffers */
874
875 rc = cc_map_cipher_request(ctx_p->drvdata, req_ctx, ivsize, nbytes,
876 req_ctx->iv, src, dst, flags);
877 if (rc) {
878 dev_err(dev, "map_request() failed\n");
879 goto exit_process;
880 }
881
882 /* STAT_PHASE_2: Create sequence */
883
884 /* Setup IV and XEX key used */
885 cc_setup_state_desc(tfm, req_ctx, ivsize, nbytes, desc, &seq_len);
886 /* Setup MLLI line, if needed */
887 cc_setup_mlli_desc(tfm, req_ctx, dst, src, nbytes, req, desc, &seq_len);
888 /* Setup key */
889 cc_setup_key_desc(tfm, req_ctx, nbytes, desc, &seq_len);
890 /* Data processing */
891 cc_setup_flow_desc(tfm, req_ctx, dst, src, nbytes, desc, &seq_len);
892 /* Read next IV */
893 cc_setup_readiv_desc(tfm, req_ctx, ivsize, desc, &seq_len);
894
895 /* STAT_PHASE_3: Lock HW and push sequence */
896
897 rc = cc_send_request(ctx_p->drvdata, &cc_req, desc, seq_len,
898 &req->base);
899 if (rc != -EINPROGRESS && rc != -EBUSY) {
900 /* Failed to send the request or request completed
901 * synchronously
902 */
903 cc_unmap_cipher_request(dev, req_ctx, ivsize, src, dst);
904 }
905
906exit_process:
907 if (rc != -EINPROGRESS && rc != -EBUSY) {
908 kzfree(req_ctx->iv);
909 }
910
911 return rc;
912}
913
914static int cc_cipher_encrypt(struct skcipher_request *req)
915{
916 struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
917
918 memset(req_ctx, 0, sizeof(*req_ctx));
919
920 return cc_cipher_process(req, DRV_CRYPTO_DIRECTION_ENCRYPT);
921}
922
923static int cc_cipher_decrypt(struct skcipher_request *req)
924{
925 struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
926
927 memset(req_ctx, 0, sizeof(*req_ctx));
928
929 return cc_cipher_process(req, DRV_CRYPTO_DIRECTION_DECRYPT);
930}
931
932/* Block cipher alg */
933static const struct cc_alg_template skcipher_algs[] = {
934 {
935 .name = "xts(paes)",
936 .driver_name = "xts-paes-ccree",
937 .blocksize = 1,
938 .template_skcipher = {
939 .setkey = cc_cipher_sethkey,
940 .encrypt = cc_cipher_encrypt,
941 .decrypt = cc_cipher_decrypt,
942 .min_keysize = CC_HW_KEY_SIZE,
943 .max_keysize = CC_HW_KEY_SIZE,
944 .ivsize = AES_BLOCK_SIZE,
945 },
946 .cipher_mode = DRV_CIPHER_XTS,
947 .flow_mode = S_DIN_to_AES,
948 .min_hw_rev = CC_HW_REV_630,
949 .std_body = CC_STD_NIST,
950 .sec_func = true,
951 },
952 {
953 .name = "xts512(paes)",
954 .driver_name = "xts-paes-du512-ccree",
955 .blocksize = 1,
956 .template_skcipher = {
957 .setkey = cc_cipher_sethkey,
958 .encrypt = cc_cipher_encrypt,
959 .decrypt = cc_cipher_decrypt,
960 .min_keysize = CC_HW_KEY_SIZE,
961 .max_keysize = CC_HW_KEY_SIZE,
962 .ivsize = AES_BLOCK_SIZE,
963 },
964 .cipher_mode = DRV_CIPHER_XTS,
965 .flow_mode = S_DIN_to_AES,
966 .data_unit = 512,
967 .min_hw_rev = CC_HW_REV_712,
968 .std_body = CC_STD_NIST,
969 .sec_func = true,
970 },
971 {
972 .name = "xts4096(paes)",
973 .driver_name = "xts-paes-du4096-ccree",
974 .blocksize = 1,
975 .template_skcipher = {
976 .setkey = cc_cipher_sethkey,
977 .encrypt = cc_cipher_encrypt,
978 .decrypt = cc_cipher_decrypt,
979 .min_keysize = CC_HW_KEY_SIZE,
980 .max_keysize = CC_HW_KEY_SIZE,
981 .ivsize = AES_BLOCK_SIZE,
982 },
983 .cipher_mode = DRV_CIPHER_XTS,
984 .flow_mode = S_DIN_to_AES,
985 .data_unit = 4096,
986 .min_hw_rev = CC_HW_REV_712,
987 .std_body = CC_STD_NIST,
988 .sec_func = true,
989 },
990 {
991 .name = "essiv(paes)",
992 .driver_name = "essiv-paes-ccree",
993 .blocksize = AES_BLOCK_SIZE,
994 .template_skcipher = {
995 .setkey = cc_cipher_sethkey,
996 .encrypt = cc_cipher_encrypt,
997 .decrypt = cc_cipher_decrypt,
998 .min_keysize = CC_HW_KEY_SIZE,
999 .max_keysize = CC_HW_KEY_SIZE,
1000 .ivsize = AES_BLOCK_SIZE,
1001 },
1002 .cipher_mode = DRV_CIPHER_ESSIV,
1003 .flow_mode = S_DIN_to_AES,
1004 .min_hw_rev = CC_HW_REV_712,
1005 .std_body = CC_STD_NIST,
1006 .sec_func = true,
1007 },
1008 {
1009 .name = "essiv512(paes)",
1010 .driver_name = "essiv-paes-du512-ccree",
1011 .blocksize = AES_BLOCK_SIZE,
1012 .template_skcipher = {
1013 .setkey = cc_cipher_sethkey,
1014 .encrypt = cc_cipher_encrypt,
1015 .decrypt = cc_cipher_decrypt,
1016 .min_keysize = CC_HW_KEY_SIZE,
1017 .max_keysize = CC_HW_KEY_SIZE,
1018 .ivsize = AES_BLOCK_SIZE,
1019 },
1020 .cipher_mode = DRV_CIPHER_ESSIV,
1021 .flow_mode = S_DIN_to_AES,
1022 .data_unit = 512,
1023 .min_hw_rev = CC_HW_REV_712,
1024 .std_body = CC_STD_NIST,
1025 .sec_func = true,
1026 },
1027 {
1028 .name = "essiv4096(paes)",
1029 .driver_name = "essiv-paes-du4096-ccree",
1030 .blocksize = AES_BLOCK_SIZE,
1031 .template_skcipher = {
1032 .setkey = cc_cipher_sethkey,
1033 .encrypt = cc_cipher_encrypt,
1034 .decrypt = cc_cipher_decrypt,
1035 .min_keysize = CC_HW_KEY_SIZE,
1036 .max_keysize = CC_HW_KEY_SIZE,
1037 .ivsize = AES_BLOCK_SIZE,
1038 },
1039 .cipher_mode = DRV_CIPHER_ESSIV,
1040 .flow_mode = S_DIN_to_AES,
1041 .data_unit = 4096,
1042 .min_hw_rev = CC_HW_REV_712,
1043 .std_body = CC_STD_NIST,
1044 .sec_func = true,
1045 },
1046 {
1047 .name = "bitlocker(paes)",
1048 .driver_name = "bitlocker-paes-ccree",
1049 .blocksize = AES_BLOCK_SIZE,
1050 .template_skcipher = {
1051 .setkey = cc_cipher_sethkey,
1052 .encrypt = cc_cipher_encrypt,
1053 .decrypt = cc_cipher_decrypt,
1054 .min_keysize = CC_HW_KEY_SIZE,
1055 .max_keysize = CC_HW_KEY_SIZE,
1056 .ivsize = AES_BLOCK_SIZE,
1057 },
1058 .cipher_mode = DRV_CIPHER_BITLOCKER,
1059 .flow_mode = S_DIN_to_AES,
1060 .min_hw_rev = CC_HW_REV_712,
1061 .std_body = CC_STD_NIST,
1062 .sec_func = true,
1063 },
1064 {
1065 .name = "bitlocker512(paes)",
1066 .driver_name = "bitlocker-paes-du512-ccree",
1067 .blocksize = AES_BLOCK_SIZE,
1068 .template_skcipher = {
1069 .setkey = cc_cipher_sethkey,
1070 .encrypt = cc_cipher_encrypt,
1071 .decrypt = cc_cipher_decrypt,
1072 .min_keysize = CC_HW_KEY_SIZE,
1073 .max_keysize = CC_HW_KEY_SIZE,
1074 .ivsize = AES_BLOCK_SIZE,
1075 },
1076 .cipher_mode = DRV_CIPHER_BITLOCKER,
1077 .flow_mode = S_DIN_to_AES,
1078 .data_unit = 512,
1079 .min_hw_rev = CC_HW_REV_712,
1080 .std_body = CC_STD_NIST,
1081 .sec_func = true,
1082 },
1083 {
1084 .name = "bitlocker4096(paes)",
1085 .driver_name = "bitlocker-paes-du4096-ccree",
1086 .blocksize = AES_BLOCK_SIZE,
1087 .template_skcipher = {
1088 .setkey = cc_cipher_sethkey,
1089 .encrypt = cc_cipher_encrypt,
1090 .decrypt = cc_cipher_decrypt,
1091 .min_keysize = CC_HW_KEY_SIZE,
1092 .max_keysize = CC_HW_KEY_SIZE,
1093 .ivsize = AES_BLOCK_SIZE,
1094 },
1095 .cipher_mode = DRV_CIPHER_BITLOCKER,
1096 .flow_mode = S_DIN_to_AES,
1097 .data_unit = 4096,
1098 .min_hw_rev = CC_HW_REV_712,
1099 .std_body = CC_STD_NIST,
1100 .sec_func = true,
1101 },
1102 {
1103 .name = "ecb(paes)",
1104 .driver_name = "ecb-paes-ccree",
1105 .blocksize = AES_BLOCK_SIZE,
1106 .template_skcipher = {
1107 .setkey = cc_cipher_sethkey,
1108 .encrypt = cc_cipher_encrypt,
1109 .decrypt = cc_cipher_decrypt,
1110 .min_keysize = CC_HW_KEY_SIZE,
1111 .max_keysize = CC_HW_KEY_SIZE,
1112 .ivsize = 0,
1113 },
1114 .cipher_mode = DRV_CIPHER_ECB,
1115 .flow_mode = S_DIN_to_AES,
1116 .min_hw_rev = CC_HW_REV_712,
1117 .std_body = CC_STD_NIST,
1118 .sec_func = true,
1119 },
1120 {
1121 .name = "cbc(paes)",
1122 .driver_name = "cbc-paes-ccree",
1123 .blocksize = AES_BLOCK_SIZE,
1124 .template_skcipher = {
1125 .setkey = cc_cipher_sethkey,
1126 .encrypt = cc_cipher_encrypt,
1127 .decrypt = cc_cipher_decrypt,
1128 .min_keysize = CC_HW_KEY_SIZE,
1129 .max_keysize = CC_HW_KEY_SIZE,
1130 .ivsize = AES_BLOCK_SIZE,
1131 },
1132 .cipher_mode = DRV_CIPHER_CBC,
1133 .flow_mode = S_DIN_to_AES,
1134 .min_hw_rev = CC_HW_REV_712,
1135 .std_body = CC_STD_NIST,
1136 .sec_func = true,
1137 },
1138 {
1139 .name = "ofb(paes)",
1140 .driver_name = "ofb-paes-ccree",
1141 .blocksize = AES_BLOCK_SIZE,
1142 .template_skcipher = {
1143 .setkey = cc_cipher_sethkey,
1144 .encrypt = cc_cipher_encrypt,
1145 .decrypt = cc_cipher_decrypt,
1146 .min_keysize = CC_HW_KEY_SIZE,
1147 .max_keysize = CC_HW_KEY_SIZE,
1148 .ivsize = AES_BLOCK_SIZE,
1149 },
1150 .cipher_mode = DRV_CIPHER_OFB,
1151 .flow_mode = S_DIN_to_AES,
1152 .min_hw_rev = CC_HW_REV_712,
1153 .std_body = CC_STD_NIST,
1154 .sec_func = true,
1155 },
1156 {
1157 .name = "cts(cbc(paes))",
1158 .driver_name = "cts-cbc-paes-ccree",
1159 .blocksize = AES_BLOCK_SIZE,
1160 .template_skcipher = {
1161 .setkey = cc_cipher_sethkey,
1162 .encrypt = cc_cipher_encrypt,
1163 .decrypt = cc_cipher_decrypt,
1164 .min_keysize = CC_HW_KEY_SIZE,
1165 .max_keysize = CC_HW_KEY_SIZE,
1166 .ivsize = AES_BLOCK_SIZE,
1167 },
1168 .cipher_mode = DRV_CIPHER_CBC_CTS,
1169 .flow_mode = S_DIN_to_AES,
1170 .min_hw_rev = CC_HW_REV_712,
1171 .std_body = CC_STD_NIST,
1172 .sec_func = true,
1173 },
1174 {
1175 .name = "ctr(paes)",
1176 .driver_name = "ctr-paes-ccree",
1177 .blocksize = 1,
1178 .template_skcipher = {
1179 .setkey = cc_cipher_sethkey,
1180 .encrypt = cc_cipher_encrypt,
1181 .decrypt = cc_cipher_decrypt,
1182 .min_keysize = CC_HW_KEY_SIZE,
1183 .max_keysize = CC_HW_KEY_SIZE,
1184 .ivsize = AES_BLOCK_SIZE,
1185 },
1186 .cipher_mode = DRV_CIPHER_CTR,
1187 .flow_mode = S_DIN_to_AES,
1188 .min_hw_rev = CC_HW_REV_712,
1189 .std_body = CC_STD_NIST,
1190 .sec_func = true,
1191 },
1192 {
1193 .name = "xts(aes)",
1194 .driver_name = "xts-aes-ccree",
1195 .blocksize = 1,
1196 .template_skcipher = {
1197 .setkey = cc_cipher_setkey,
1198 .encrypt = cc_cipher_encrypt,
1199 .decrypt = cc_cipher_decrypt,
1200 .min_keysize = AES_MIN_KEY_SIZE * 2,
1201 .max_keysize = AES_MAX_KEY_SIZE * 2,
1202 .ivsize = AES_BLOCK_SIZE,
1203 },
1204 .cipher_mode = DRV_CIPHER_XTS,
1205 .flow_mode = S_DIN_to_AES,
1206 .min_hw_rev = CC_HW_REV_630,
1207 .std_body = CC_STD_NIST,
1208 },
1209 {
1210 .name = "xts512(aes)",
1211 .driver_name = "xts-aes-du512-ccree",
1212 .blocksize = 1,
1213 .template_skcipher = {
1214 .setkey = cc_cipher_setkey,
1215 .encrypt = cc_cipher_encrypt,
1216 .decrypt = cc_cipher_decrypt,
1217 .min_keysize = AES_MIN_KEY_SIZE * 2,
1218 .max_keysize = AES_MAX_KEY_SIZE * 2,
1219 .ivsize = AES_BLOCK_SIZE,
1220 },
1221 .cipher_mode = DRV_CIPHER_XTS,
1222 .flow_mode = S_DIN_to_AES,
1223 .data_unit = 512,
1224 .min_hw_rev = CC_HW_REV_712,
1225 .std_body = CC_STD_NIST,
1226 },
1227 {
1228 .name = "xts4096(aes)",
1229 .driver_name = "xts-aes-du4096-ccree",
1230 .blocksize = 1,
1231 .template_skcipher = {
1232 .setkey = cc_cipher_setkey,
1233 .encrypt = cc_cipher_encrypt,
1234 .decrypt = cc_cipher_decrypt,
1235 .min_keysize = AES_MIN_KEY_SIZE * 2,
1236 .max_keysize = AES_MAX_KEY_SIZE * 2,
1237 .ivsize = AES_BLOCK_SIZE,
1238 },
1239 .cipher_mode = DRV_CIPHER_XTS,
1240 .flow_mode = S_DIN_to_AES,
1241 .data_unit = 4096,
1242 .min_hw_rev = CC_HW_REV_712,
1243 .std_body = CC_STD_NIST,
1244 },
1245 {
1246 .name = "essiv(aes)",
1247 .driver_name = "essiv-aes-ccree",
1248 .blocksize = AES_BLOCK_SIZE,
1249 .template_skcipher = {
1250 .setkey = cc_cipher_setkey,
1251 .encrypt = cc_cipher_encrypt,
1252 .decrypt = cc_cipher_decrypt,
1253 .min_keysize = AES_MIN_KEY_SIZE * 2,
1254 .max_keysize = AES_MAX_KEY_SIZE * 2,
1255 .ivsize = AES_BLOCK_SIZE,
1256 },
1257 .cipher_mode = DRV_CIPHER_ESSIV,
1258 .flow_mode = S_DIN_to_AES,
1259 .min_hw_rev = CC_HW_REV_712,
1260 .std_body = CC_STD_NIST,
1261 },
1262 {
1263 .name = "essiv512(aes)",
1264 .driver_name = "essiv-aes-du512-ccree",
1265 .blocksize = AES_BLOCK_SIZE,
1266 .template_skcipher = {
1267 .setkey = cc_cipher_setkey,
1268 .encrypt = cc_cipher_encrypt,
1269 .decrypt = cc_cipher_decrypt,
1270 .min_keysize = AES_MIN_KEY_SIZE * 2,
1271 .max_keysize = AES_MAX_KEY_SIZE * 2,
1272 .ivsize = AES_BLOCK_SIZE,
1273 },
1274 .cipher_mode = DRV_CIPHER_ESSIV,
1275 .flow_mode = S_DIN_to_AES,
1276 .data_unit = 512,
1277 .min_hw_rev = CC_HW_REV_712,
1278 .std_body = CC_STD_NIST,
1279 },
1280 {
1281 .name = "essiv4096(aes)",
1282 .driver_name = "essiv-aes-du4096-ccree",
1283 .blocksize = AES_BLOCK_SIZE,
1284 .template_skcipher = {
1285 .setkey = cc_cipher_setkey,
1286 .encrypt = cc_cipher_encrypt,
1287 .decrypt = cc_cipher_decrypt,
1288 .min_keysize = AES_MIN_KEY_SIZE * 2,
1289 .max_keysize = AES_MAX_KEY_SIZE * 2,
1290 .ivsize = AES_BLOCK_SIZE,
1291 },
1292 .cipher_mode = DRV_CIPHER_ESSIV,
1293 .flow_mode = S_DIN_to_AES,
1294 .data_unit = 4096,
1295 .min_hw_rev = CC_HW_REV_712,
1296 .std_body = CC_STD_NIST,
1297 },
1298 {
1299 .name = "bitlocker(aes)",
1300 .driver_name = "bitlocker-aes-ccree",
1301 .blocksize = AES_BLOCK_SIZE,
1302 .template_skcipher = {
1303 .setkey = cc_cipher_setkey,
1304 .encrypt = cc_cipher_encrypt,
1305 .decrypt = cc_cipher_decrypt,
1306 .min_keysize = AES_MIN_KEY_SIZE * 2,
1307 .max_keysize = AES_MAX_KEY_SIZE * 2,
1308 .ivsize = AES_BLOCK_SIZE,
1309 },
1310 .cipher_mode = DRV_CIPHER_BITLOCKER,
1311 .flow_mode = S_DIN_to_AES,
1312 .min_hw_rev = CC_HW_REV_712,
1313 .std_body = CC_STD_NIST,
1314 },
1315 {
1316 .name = "bitlocker512(aes)",
1317 .driver_name = "bitlocker-aes-du512-ccree",
1318 .blocksize = AES_BLOCK_SIZE,
1319 .template_skcipher = {
1320 .setkey = cc_cipher_setkey,
1321 .encrypt = cc_cipher_encrypt,
1322 .decrypt = cc_cipher_decrypt,
1323 .min_keysize = AES_MIN_KEY_SIZE * 2,
1324 .max_keysize = AES_MAX_KEY_SIZE * 2,
1325 .ivsize = AES_BLOCK_SIZE,
1326 },
1327 .cipher_mode = DRV_CIPHER_BITLOCKER,
1328 .flow_mode = S_DIN_to_AES,
1329 .data_unit = 512,
1330 .min_hw_rev = CC_HW_REV_712,
1331 .std_body = CC_STD_NIST,
1332 },
1333 {
1334 .name = "bitlocker4096(aes)",
1335 .driver_name = "bitlocker-aes-du4096-ccree",
1336 .blocksize = AES_BLOCK_SIZE,
1337 .template_skcipher = {
1338 .setkey = cc_cipher_setkey,
1339 .encrypt = cc_cipher_encrypt,
1340 .decrypt = cc_cipher_decrypt,
1341 .min_keysize = AES_MIN_KEY_SIZE * 2,
1342 .max_keysize = AES_MAX_KEY_SIZE * 2,
1343 .ivsize = AES_BLOCK_SIZE,
1344 },
1345 .cipher_mode = DRV_CIPHER_BITLOCKER,
1346 .flow_mode = S_DIN_to_AES,
1347 .data_unit = 4096,
1348 .min_hw_rev = CC_HW_REV_712,
1349 .std_body = CC_STD_NIST,
1350 },
1351 {
1352 .name = "ecb(aes)",
1353 .driver_name = "ecb-aes-ccree",
1354 .blocksize = AES_BLOCK_SIZE,
1355 .template_skcipher = {
1356 .setkey = cc_cipher_setkey,
1357 .encrypt = cc_cipher_encrypt,
1358 .decrypt = cc_cipher_decrypt,
1359 .min_keysize = AES_MIN_KEY_SIZE,
1360 .max_keysize = AES_MAX_KEY_SIZE,
1361 .ivsize = 0,
1362 },
1363 .cipher_mode = DRV_CIPHER_ECB,
1364 .flow_mode = S_DIN_to_AES,
1365 .min_hw_rev = CC_HW_REV_630,
1366 .std_body = CC_STD_NIST,
1367 },
1368 {
1369 .name = "cbc(aes)",
1370 .driver_name = "cbc-aes-ccree",
1371 .blocksize = AES_BLOCK_SIZE,
1372 .template_skcipher = {
1373 .setkey = cc_cipher_setkey,
1374 .encrypt = cc_cipher_encrypt,
1375 .decrypt = cc_cipher_decrypt,
1376 .min_keysize = AES_MIN_KEY_SIZE,
1377 .max_keysize = AES_MAX_KEY_SIZE,
1378 .ivsize = AES_BLOCK_SIZE,
1379 },
1380 .cipher_mode = DRV_CIPHER_CBC,
1381 .flow_mode = S_DIN_to_AES,
1382 .min_hw_rev = CC_HW_REV_630,
1383 .std_body = CC_STD_NIST,
1384 },
1385 {
1386 .name = "ofb(aes)",
1387 .driver_name = "ofb-aes-ccree",
1388 .blocksize = AES_BLOCK_SIZE,
1389 .template_skcipher = {
1390 .setkey = cc_cipher_setkey,
1391 .encrypt = cc_cipher_encrypt,
1392 .decrypt = cc_cipher_decrypt,
1393 .min_keysize = AES_MIN_KEY_SIZE,
1394 .max_keysize = AES_MAX_KEY_SIZE,
1395 .ivsize = AES_BLOCK_SIZE,
1396 },
1397 .cipher_mode = DRV_CIPHER_OFB,
1398 .flow_mode = S_DIN_to_AES,
1399 .min_hw_rev = CC_HW_REV_630,
1400 .std_body = CC_STD_NIST,
1401 },
1402 {
1403 .name = "cts(cbc(aes))",
1404 .driver_name = "cts-cbc-aes-ccree",
1405 .blocksize = AES_BLOCK_SIZE,
1406 .template_skcipher = {
1407 .setkey = cc_cipher_setkey,
1408 .encrypt = cc_cipher_encrypt,
1409 .decrypt = cc_cipher_decrypt,
1410 .min_keysize = AES_MIN_KEY_SIZE,
1411 .max_keysize = AES_MAX_KEY_SIZE,
1412 .ivsize = AES_BLOCK_SIZE,
1413 },
1414 .cipher_mode = DRV_CIPHER_CBC_CTS,
1415 .flow_mode = S_DIN_to_AES,
1416 .min_hw_rev = CC_HW_REV_630,
1417 .std_body = CC_STD_NIST,
1418 },
1419 {
1420 .name = "ctr(aes)",
1421 .driver_name = "ctr-aes-ccree",
1422 .blocksize = 1,
1423 .template_skcipher = {
1424 .setkey = cc_cipher_setkey,
1425 .encrypt = cc_cipher_encrypt,
1426 .decrypt = cc_cipher_decrypt,
1427 .min_keysize = AES_MIN_KEY_SIZE,
1428 .max_keysize = AES_MAX_KEY_SIZE,
1429 .ivsize = AES_BLOCK_SIZE,
1430 },
1431 .cipher_mode = DRV_CIPHER_CTR,
1432 .flow_mode = S_DIN_to_AES,
1433 .min_hw_rev = CC_HW_REV_630,
1434 .std_body = CC_STD_NIST,
1435 },
1436 {
1437 .name = "cbc(des3_ede)",
1438 .driver_name = "cbc-3des-ccree",
1439 .blocksize = DES3_EDE_BLOCK_SIZE,
1440 .template_skcipher = {
1441 .setkey = cc_cipher_setkey,
1442 .encrypt = cc_cipher_encrypt,
1443 .decrypt = cc_cipher_decrypt,
1444 .min_keysize = DES3_EDE_KEY_SIZE,
1445 .max_keysize = DES3_EDE_KEY_SIZE,
1446 .ivsize = DES3_EDE_BLOCK_SIZE,
1447 },
1448 .cipher_mode = DRV_CIPHER_CBC,
1449 .flow_mode = S_DIN_to_DES,
1450 .min_hw_rev = CC_HW_REV_630,
1451 .std_body = CC_STD_NIST,
1452 },
1453 {
1454 .name = "ecb(des3_ede)",
1455 .driver_name = "ecb-3des-ccree",
1456 .blocksize = DES3_EDE_BLOCK_SIZE,
1457 .template_skcipher = {
1458 .setkey = cc_cipher_setkey,
1459 .encrypt = cc_cipher_encrypt,
1460 .decrypt = cc_cipher_decrypt,
1461 .min_keysize = DES3_EDE_KEY_SIZE,
1462 .max_keysize = DES3_EDE_KEY_SIZE,
1463 .ivsize = 0,
1464 },
1465 .cipher_mode = DRV_CIPHER_ECB,
1466 .flow_mode = S_DIN_to_DES,
1467 .min_hw_rev = CC_HW_REV_630,
1468 .std_body = CC_STD_NIST,
1469 },
1470 {
1471 .name = "cbc(des)",
1472 .driver_name = "cbc-des-ccree",
1473 .blocksize = DES_BLOCK_SIZE,
1474 .template_skcipher = {
1475 .setkey = cc_cipher_setkey,
1476 .encrypt = cc_cipher_encrypt,
1477 .decrypt = cc_cipher_decrypt,
1478 .min_keysize = DES_KEY_SIZE,
1479 .max_keysize = DES_KEY_SIZE,
1480 .ivsize = DES_BLOCK_SIZE,
1481 },
1482 .cipher_mode = DRV_CIPHER_CBC,
1483 .flow_mode = S_DIN_to_DES,
1484 .min_hw_rev = CC_HW_REV_630,
1485 .std_body = CC_STD_NIST,
1486 },
1487 {
1488 .name = "ecb(des)",
1489 .driver_name = "ecb-des-ccree",
1490 .blocksize = DES_BLOCK_SIZE,
1491 .template_skcipher = {
1492 .setkey = cc_cipher_setkey,
1493 .encrypt = cc_cipher_encrypt,
1494 .decrypt = cc_cipher_decrypt,
1495 .min_keysize = DES_KEY_SIZE,
1496 .max_keysize = DES_KEY_SIZE,
1497 .ivsize = 0,
1498 },
1499 .cipher_mode = DRV_CIPHER_ECB,
1500 .flow_mode = S_DIN_to_DES,
1501 .min_hw_rev = CC_HW_REV_630,
1502 .std_body = CC_STD_NIST,
1503 },
1504 {
1505 .name = "cbc(sm4)",
1506 .driver_name = "cbc-sm4-ccree",
1507 .blocksize = SM4_BLOCK_SIZE,
1508 .template_skcipher = {
1509 .setkey = cc_cipher_setkey,
1510 .encrypt = cc_cipher_encrypt,
1511 .decrypt = cc_cipher_decrypt,
1512 .min_keysize = SM4_KEY_SIZE,
1513 .max_keysize = SM4_KEY_SIZE,
1514 .ivsize = SM4_BLOCK_SIZE,
1515 },
1516 .cipher_mode = DRV_CIPHER_CBC,
1517 .flow_mode = S_DIN_to_SM4,
1518 .min_hw_rev = CC_HW_REV_713,
1519 .std_body = CC_STD_OSCCA,
1520 },
1521 {
1522 .name = "ecb(sm4)",
1523 .driver_name = "ecb-sm4-ccree",
1524 .blocksize = SM4_BLOCK_SIZE,
1525 .template_skcipher = {
1526 .setkey = cc_cipher_setkey,
1527 .encrypt = cc_cipher_encrypt,
1528 .decrypt = cc_cipher_decrypt,
1529 .min_keysize = SM4_KEY_SIZE,
1530 .max_keysize = SM4_KEY_SIZE,
1531 .ivsize = 0,
1532 },
1533 .cipher_mode = DRV_CIPHER_ECB,
1534 .flow_mode = S_DIN_to_SM4,
1535 .min_hw_rev = CC_HW_REV_713,
1536 .std_body = CC_STD_OSCCA,
1537 },
1538 {
1539 .name = "ctr(sm4)",
1540 .driver_name = "ctr-sm4-ccree",
1541 .blocksize = SM4_BLOCK_SIZE,
1542 .template_skcipher = {
1543 .setkey = cc_cipher_setkey,
1544 .encrypt = cc_cipher_encrypt,
1545 .decrypt = cc_cipher_decrypt,
1546 .min_keysize = SM4_KEY_SIZE,
1547 .max_keysize = SM4_KEY_SIZE,
1548 .ivsize = SM4_BLOCK_SIZE,
1549 },
1550 .cipher_mode = DRV_CIPHER_CTR,
1551 .flow_mode = S_DIN_to_SM4,
1552 .min_hw_rev = CC_HW_REV_713,
1553 .std_body = CC_STD_OSCCA,
1554 },
1555 {
1556 .name = "cbc(psm4)",
1557 .driver_name = "cbc-psm4-ccree",
1558 .blocksize = SM4_BLOCK_SIZE,
1559 .template_skcipher = {
1560 .setkey = cc_cipher_sethkey,
1561 .encrypt = cc_cipher_encrypt,
1562 .decrypt = cc_cipher_decrypt,
1563 .min_keysize = CC_HW_KEY_SIZE,
1564 .max_keysize = CC_HW_KEY_SIZE,
1565 .ivsize = SM4_BLOCK_SIZE,
1566 },
1567 .cipher_mode = DRV_CIPHER_CBC,
1568 .flow_mode = S_DIN_to_SM4,
1569 .min_hw_rev = CC_HW_REV_713,
1570 .std_body = CC_STD_OSCCA,
1571 .sec_func = true,
1572 },
1573 {
1574 .name = "ctr(psm4)",
1575 .driver_name = "ctr-psm4-ccree",
1576 .blocksize = SM4_BLOCK_SIZE,
1577 .template_skcipher = {
1578 .setkey = cc_cipher_sethkey,
1579 .encrypt = cc_cipher_encrypt,
1580 .decrypt = cc_cipher_decrypt,
1581 .min_keysize = CC_HW_KEY_SIZE,
1582 .max_keysize = CC_HW_KEY_SIZE,
1583 .ivsize = SM4_BLOCK_SIZE,
1584 },
1585 .cipher_mode = DRV_CIPHER_CTR,
1586 .flow_mode = S_DIN_to_SM4,
1587 .min_hw_rev = CC_HW_REV_713,
1588 .std_body = CC_STD_OSCCA,
1589 .sec_func = true,
1590 },
1591};
1592
1593static struct cc_crypto_alg *cc_create_alg(const struct cc_alg_template *tmpl,
1594 struct device *dev)
1595{
1596 struct cc_crypto_alg *t_alg;
1597 struct skcipher_alg *alg;
1598
1599 t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
1600 if (!t_alg)
1601 return ERR_PTR(-ENOMEM);
1602
1603 alg = &t_alg->skcipher_alg;
1604
1605 memcpy(alg, &tmpl->template_skcipher, sizeof(*alg));
1606
1607 snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", tmpl->name);
1608 snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
1609 tmpl->driver_name);
1610 alg->base.cra_module = THIS_MODULE;
1611 alg->base.cra_priority = CC_CRA_PRIO;
1612 alg->base.cra_blocksize = tmpl->blocksize;
1613 alg->base.cra_alignmask = 0;
1614 alg->base.cra_ctxsize = sizeof(struct cc_cipher_ctx);
1615
1616 alg->base.cra_init = cc_cipher_init;
1617 alg->base.cra_exit = cc_cipher_exit;
1618 alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
1619
1620 t_alg->cipher_mode = tmpl->cipher_mode;
1621 t_alg->flow_mode = tmpl->flow_mode;
1622 t_alg->data_unit = tmpl->data_unit;
1623
1624 return t_alg;
1625}
1626
1627int cc_cipher_free(struct cc_drvdata *drvdata)
1628{
1629 struct cc_crypto_alg *t_alg, *n;
1630 struct cc_cipher_handle *cipher_handle = drvdata->cipher_handle;
1631
1632 if (cipher_handle) {
1633 /* Remove registered algs */
1634 list_for_each_entry_safe(t_alg, n, &cipher_handle->alg_list,
1635 entry) {
1636 crypto_unregister_skcipher(&t_alg->skcipher_alg);
1637 list_del(&t_alg->entry);
1638 kfree(t_alg);
1639 }
1640 kfree(cipher_handle);
1641 drvdata->cipher_handle = NULL;
1642 }
1643 return 0;
1644}
1645
1646int cc_cipher_alloc(struct cc_drvdata *drvdata)
1647{
1648 struct cc_cipher_handle *cipher_handle;
1649 struct cc_crypto_alg *t_alg;
1650 struct device *dev = drvdata_to_dev(drvdata);
1651 int rc = -ENOMEM;
1652 int alg;
1653
1654 cipher_handle = kmalloc(sizeof(*cipher_handle), GFP_KERNEL);
1655 if (!cipher_handle)
1656 return -ENOMEM;
1657
1658 INIT_LIST_HEAD(&cipher_handle->alg_list);
1659 drvdata->cipher_handle = cipher_handle;
1660
1661 /* Linux crypto */
1662 dev_dbg(dev, "Number of algorithms = %zu\n",
1663 ARRAY_SIZE(skcipher_algs));
1664 for (alg = 0; alg < ARRAY_SIZE(skcipher_algs); alg++) {
1665 if ((skcipher_algs[alg].min_hw_rev > drvdata->hw_rev) ||
1666 !(drvdata->std_bodies & skcipher_algs[alg].std_body) ||
1667 (drvdata->sec_disabled && skcipher_algs[alg].sec_func))
1668 continue;
1669
1670 dev_dbg(dev, "creating %s\n", skcipher_algs[alg].driver_name);
1671 t_alg = cc_create_alg(&skcipher_algs[alg], dev);
1672 if (IS_ERR(t_alg)) {
1673 rc = PTR_ERR(t_alg);
1674 dev_err(dev, "%s alg allocation failed\n",
1675 skcipher_algs[alg].driver_name);
1676 goto fail0;
1677 }
1678 t_alg->drvdata = drvdata;
1679
1680 dev_dbg(dev, "registering %s\n",
1681 skcipher_algs[alg].driver_name);
1682 rc = crypto_register_skcipher(&t_alg->skcipher_alg);
1683 dev_dbg(dev, "%s alg registration rc = %x\n",
1684 t_alg->skcipher_alg.base.cra_driver_name, rc);
1685 if (rc) {
1686 dev_err(dev, "%s alg registration failed\n",
1687 t_alg->skcipher_alg.base.cra_driver_name);
1688 kfree(t_alg);
1689 goto fail0;
1690 } else {
1691 list_add_tail(&t_alg->entry,
1692 &cipher_handle->alg_list);
1693 dev_dbg(dev, "Registered %s\n",
1694 t_alg->skcipher_alg.base.cra_driver_name);
1695 }
1696 }
1697 return 0;
1698
1699fail0:
1700 cc_cipher_free(drvdata);
1701 return rc;
1702}