1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2012-2019 ARM Limited (or its affiliates). */
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>
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"
19 #define MAX_SKCIPHER_SEQ_LEN 6
21 #define template_skcipher template_u.skcipher
23 struct cc_user_key_info {
25 dma_addr_t key_dma_addr;
28 struct cc_hw_key_info {
29 enum cc_hw_crypto_key key1_slot;
30 enum cc_hw_crypto_key key2_slot;
33 struct cc_cpp_key_info {
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 */
45 struct cc_cipher_ctx {
46 struct cc_drvdata *drvdata;
51 enum cc_key_type key_type;
52 struct cc_user_key_info user;
54 struct cc_hw_key_info hw;
55 struct cc_cpp_key_info cpp;
57 struct crypto_shash *shash_tfm;
58 struct crypto_skcipher *fallback_tfm;
62 static void cc_cipher_complete(struct device *dev, void *cc_req, int err);
64 static inline enum cc_key_type cc_key_type(struct crypto_tfm *tfm)
66 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
68 return ctx_p->key_type;
71 static int validate_keys_sizes(struct cc_cipher_ctx *ctx_p, u32 size)
73 switch (ctx_p->flow_mode) {
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)
81 case CC_AES_256_BIT_KEY_SIZE:
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)
94 if (size == DES3_EDE_KEY_SIZE || size == DES_KEY_SIZE)
98 if (size == SM4_KEY_SIZE)
106 static int validate_data_size(struct cc_cipher_ctx *ctx_p,
109 switch (ctx_p->flow_mode) {
111 switch (ctx_p->cipher_mode) {
113 case DRV_CIPHER_CBC_CTS:
114 if (size >= AES_BLOCK_SIZE)
122 case DRV_CIPHER_ESSIV:
123 if (IS_ALIGNED(size, AES_BLOCK_SIZE))
131 if (IS_ALIGNED(size, DES_BLOCK_SIZE))
135 switch (ctx_p->cipher_mode) {
140 if (IS_ALIGNED(size, SM4_BLOCK_SIZE))
151 static int cc_cipher_init(struct crypto_tfm *tfm)
153 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
154 struct cc_crypto_alg *cc_alg =
155 container_of(tfm->__crt_alg, struct cc_crypto_alg,
157 struct device *dev = drvdata_to_dev(cc_alg->drvdata);
158 unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
159 unsigned int fallback_req_size = 0;
161 dev_dbg(dev, "Initializing context @%p for %s\n", ctx_p,
162 crypto_tfm_alg_name(tfm));
164 ctx_p->cipher_mode = cc_alg->cipher_mode;
165 ctx_p->flow_mode = cc_alg->flow_mode;
166 ctx_p->drvdata = cc_alg->drvdata;
168 if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
169 const char *name = crypto_tfm_alg_name(tfm);
171 /* Alloc hash tfm for essiv */
172 ctx_p->shash_tfm = crypto_alloc_shash("sha256", 0, 0);
173 if (IS_ERR(ctx_p->shash_tfm)) {
174 dev_err(dev, "Error allocating hash tfm for ESSIV.\n");
175 return PTR_ERR(ctx_p->shash_tfm);
177 max_key_buf_size <<= 1;
179 /* Alloc fallabck tfm or essiv when key size != 256 bit */
180 ctx_p->fallback_tfm =
181 crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC);
183 if (IS_ERR(ctx_p->fallback_tfm)) {
184 /* Note we're still allowing registration with no fallback since it's
185 * better to have most modes supported than none at all.
187 dev_warn(dev, "Error allocating fallback algo %s. Some modes may be available.\n",
189 ctx_p->fallback_tfm = NULL;
191 fallback_req_size = crypto_skcipher_reqsize(ctx_p->fallback_tfm);
195 crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
196 sizeof(struct cipher_req_ctx) + fallback_req_size);
198 /* Allocate key buffer, cache line aligned */
199 ctx_p->user.key = kzalloc(max_key_buf_size, GFP_KERNEL);
200 if (!ctx_p->user.key)
203 dev_dbg(dev, "Allocated key buffer in context. key=@%p\n",
207 ctx_p->user.key_dma_addr = dma_map_single(dev, ctx_p->user.key,
210 if (dma_mapping_error(dev, ctx_p->user.key_dma_addr)) {
211 dev_err(dev, "Mapping Key %u B at va=%pK for DMA failed\n",
212 max_key_buf_size, ctx_p->user.key);
215 dev_dbg(dev, "Mapped key %u B at va=%pK to dma=%pad\n",
216 max_key_buf_size, ctx_p->user.key, &ctx_p->user.key_dma_addr);
221 kfree(ctx_p->user.key);
223 crypto_free_skcipher(ctx_p->fallback_tfm);
224 crypto_free_shash(ctx_p->shash_tfm);
229 static void cc_cipher_exit(struct crypto_tfm *tfm)
231 struct crypto_alg *alg = tfm->__crt_alg;
232 struct cc_crypto_alg *cc_alg =
233 container_of(alg, struct cc_crypto_alg,
235 unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
236 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
237 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
239 dev_dbg(dev, "Clearing context @%p for %s\n",
240 crypto_tfm_ctx(tfm), crypto_tfm_alg_name(tfm));
242 if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
243 /* Free hash tfm for essiv */
244 crypto_free_shash(ctx_p->shash_tfm);
245 ctx_p->shash_tfm = NULL;
246 crypto_free_skcipher(ctx_p->fallback_tfm);
247 ctx_p->fallback_tfm = NULL;
250 /* Unmap key buffer */
251 dma_unmap_single(dev, ctx_p->user.key_dma_addr, max_key_buf_size,
253 dev_dbg(dev, "Unmapped key buffer key_dma_addr=%pad\n",
254 &ctx_p->user.key_dma_addr);
256 /* Free key buffer in context */
257 dev_dbg(dev, "Free key buffer in context. key=@%p\n", ctx_p->user.key);
258 kfree_sensitive(ctx_p->user.key);
262 u8 key1[DES_KEY_SIZE];
263 u8 key2[DES_KEY_SIZE];
264 u8 key3[DES_KEY_SIZE];
267 static enum cc_hw_crypto_key cc_slot_to_hw_key(u8 slot_num)
282 static u8 cc_slot_to_cpp_key(u8 slot_num)
284 return (slot_num - CC_FIRST_CPP_KEY_SLOT);
287 static inline enum cc_key_type cc_slot_to_key_type(u8 slot_num)
289 if (slot_num >= CC_FIRST_HW_KEY_SLOT && slot_num <= CC_LAST_HW_KEY_SLOT)
290 return CC_HW_PROTECTED_KEY;
291 else if (slot_num >= CC_FIRST_CPP_KEY_SLOT &&
292 slot_num <= CC_LAST_CPP_KEY_SLOT)
293 return CC_POLICY_PROTECTED_KEY;
295 return CC_INVALID_PROTECTED_KEY;
298 static int cc_cipher_sethkey(struct crypto_skcipher *sktfm, const u8 *key,
301 struct crypto_tfm *tfm = crypto_skcipher_tfm(sktfm);
302 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
303 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
304 struct cc_hkey_info hki;
306 dev_dbg(dev, "Setting HW key in context @%p for %s. keylen=%u\n",
307 ctx_p, crypto_tfm_alg_name(tfm), keylen);
308 dump_byte_array("key", key, keylen);
310 /* STAT_PHASE_0: Init and sanity checks */
312 /* This check the size of the protected key token */
313 if (keylen != sizeof(hki)) {
314 dev_err(dev, "Unsupported protected key size %d.\n", keylen);
318 memcpy(&hki, key, keylen);
320 /* The real key len for crypto op is the size of the HW key
321 * referenced by the HW key slot, not the hardware key token
325 if (validate_keys_sizes(ctx_p, keylen)) {
326 dev_dbg(dev, "Unsupported key size %d.\n", keylen);
330 ctx_p->keylen = keylen;
331 ctx_p->fallback_on = false;
333 switch (cc_slot_to_key_type(hki.hw_key1)) {
334 case CC_HW_PROTECTED_KEY:
335 if (ctx_p->flow_mode == S_DIN_to_SM4) {
336 dev_err(dev, "Only AES HW protected keys are supported\n");
340 ctx_p->hw.key1_slot = cc_slot_to_hw_key(hki.hw_key1);
341 if (ctx_p->hw.key1_slot == END_OF_KEYS) {
342 dev_err(dev, "Unsupported hw key1 number (%d)\n",
347 if (ctx_p->cipher_mode == DRV_CIPHER_XTS ||
348 ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
349 if (hki.hw_key1 == hki.hw_key2) {
350 dev_err(dev, "Illegal hw key numbers (%d,%d)\n",
351 hki.hw_key1, hki.hw_key2);
355 ctx_p->hw.key2_slot = cc_slot_to_hw_key(hki.hw_key2);
356 if (ctx_p->hw.key2_slot == END_OF_KEYS) {
357 dev_err(dev, "Unsupported hw key2 number (%d)\n",
363 ctx_p->key_type = CC_HW_PROTECTED_KEY;
364 dev_dbg(dev, "HW protected key %d/%d set\n.",
365 ctx_p->hw.key1_slot, ctx_p->hw.key2_slot);
368 case CC_POLICY_PROTECTED_KEY:
369 if (ctx_p->drvdata->hw_rev < CC_HW_REV_713) {
370 dev_err(dev, "CPP keys not supported in this hardware revision.\n");
374 if (ctx_p->cipher_mode != DRV_CIPHER_CBC &&
375 ctx_p->cipher_mode != DRV_CIPHER_CTR) {
376 dev_err(dev, "CPP keys only supported in CBC or CTR modes.\n");
380 ctx_p->cpp.slot = cc_slot_to_cpp_key(hki.hw_key1);
381 if (ctx_p->flow_mode == S_DIN_to_AES)
382 ctx_p->cpp.alg = CC_CPP_AES;
383 else /* Must be SM4 since due to sethkey registration */
384 ctx_p->cpp.alg = CC_CPP_SM4;
385 ctx_p->key_type = CC_POLICY_PROTECTED_KEY;
386 dev_dbg(dev, "policy protected key alg: %d slot: %d.\n",
387 ctx_p->cpp.alg, ctx_p->cpp.slot);
391 dev_err(dev, "Unsupported protected key (%d)\n", hki.hw_key1);
398 static int cc_cipher_setkey(struct crypto_skcipher *sktfm, const u8 *key,
401 struct crypto_tfm *tfm = crypto_skcipher_tfm(sktfm);
402 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
403 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
404 struct cc_crypto_alg *cc_alg =
405 container_of(tfm->__crt_alg, struct cc_crypto_alg,
407 unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
409 dev_dbg(dev, "Setting key in context @%p for %s. keylen=%u\n",
410 ctx_p, crypto_tfm_alg_name(tfm), keylen);
411 dump_byte_array("key", key, keylen);
413 /* STAT_PHASE_0: Init and sanity checks */
415 if (validate_keys_sizes(ctx_p, keylen)) {
416 dev_dbg(dev, "Invalid key size %d.\n", keylen);
420 if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
422 /* We only support 256 bit ESSIV-CBC-AES keys */
423 if (keylen != AES_KEYSIZE_256) {
424 unsigned int flags = crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_MASK;
426 if (likely(ctx_p->fallback_tfm)) {
427 ctx_p->fallback_on = true;
428 crypto_skcipher_clear_flags(ctx_p->fallback_tfm,
429 CRYPTO_TFM_REQ_MASK);
430 crypto_skcipher_clear_flags(ctx_p->fallback_tfm, flags);
431 return crypto_skcipher_setkey(ctx_p->fallback_tfm, key, keylen);
434 dev_dbg(dev, "Unsupported key size %d and no fallback.\n", keylen);
438 /* Internal ESSIV key buffer is double sized */
439 max_key_buf_size <<= 1;
442 ctx_p->fallback_on = false;
443 ctx_p->key_type = CC_UNPROTECTED_KEY;
446 * Verify DES weak keys
447 * Note that we're dropping the expanded key since the
448 * HW does the expansion on its own.
450 if (ctx_p->flow_mode == S_DIN_to_DES) {
451 if ((keylen == DES3_EDE_KEY_SIZE &&
452 verify_skcipher_des3_key(sktfm, key)) ||
453 verify_skcipher_des_key(sktfm, key)) {
454 dev_dbg(dev, "weak DES key");
459 if (ctx_p->cipher_mode == DRV_CIPHER_XTS &&
460 xts_check_key(tfm, key, keylen)) {
461 dev_dbg(dev, "weak XTS key");
465 /* STAT_PHASE_1: Copy key to ctx */
466 dma_sync_single_for_cpu(dev, ctx_p->user.key_dma_addr,
467 max_key_buf_size, DMA_TO_DEVICE);
469 memcpy(ctx_p->user.key, key, keylen);
471 if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
472 /* sha256 for key2 - use sw implementation */
475 err = crypto_shash_tfm_digest(ctx_p->shash_tfm,
476 ctx_p->user.key, keylen,
477 ctx_p->user.key + keylen);
479 dev_err(dev, "Failed to hash ESSIV key.\n");
485 dma_sync_single_for_device(dev, ctx_p->user.key_dma_addr,
486 max_key_buf_size, DMA_TO_DEVICE);
487 ctx_p->keylen = keylen;
489 dev_dbg(dev, "return safely");
493 static int cc_out_setup_mode(struct cc_cipher_ctx *ctx_p)
495 switch (ctx_p->flow_mode) {
497 return S_AES_to_DOUT;
499 return S_DES_to_DOUT;
501 return S_SM4_to_DOUT;
503 return ctx_p->flow_mode;
507 static void cc_setup_readiv_desc(struct crypto_tfm *tfm,
508 struct cipher_req_ctx *req_ctx,
509 unsigned int ivsize, struct cc_hw_desc desc[],
510 unsigned int *seq_size)
512 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
513 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
514 int cipher_mode = ctx_p->cipher_mode;
515 int flow_mode = cc_out_setup_mode(ctx_p);
516 int direction = req_ctx->gen_ctx.op_type;
517 dma_addr_t iv_dma_addr = req_ctx->gen_ctx.iv_dma_addr;
519 if (ctx_p->key_type == CC_POLICY_PROTECTED_KEY)
522 switch (cipher_mode) {
526 case DRV_CIPHER_CBC_CTS:
530 hw_desc_init(&desc[*seq_size]);
531 set_dout_dlli(&desc[*seq_size], iv_dma_addr, ivsize, NS_BIT, 1);
532 set_cipher_config0(&desc[*seq_size], direction);
533 set_flow_mode(&desc[*seq_size], flow_mode);
534 set_cipher_mode(&desc[*seq_size], cipher_mode);
535 if (cipher_mode == DRV_CIPHER_CTR ||
536 cipher_mode == DRV_CIPHER_OFB) {
537 set_setup_mode(&desc[*seq_size], SETUP_WRITE_STATE1);
539 set_setup_mode(&desc[*seq_size], SETUP_WRITE_STATE0);
541 set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
545 case DRV_CIPHER_ESSIV:
547 hw_desc_init(&desc[*seq_size]);
548 set_setup_mode(&desc[*seq_size], SETUP_WRITE_STATE1);
549 set_cipher_mode(&desc[*seq_size], cipher_mode);
550 set_cipher_config0(&desc[*seq_size], direction);
551 set_flow_mode(&desc[*seq_size], flow_mode);
552 set_dout_dlli(&desc[*seq_size], iv_dma_addr, CC_AES_BLOCK_SIZE,
554 set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
558 dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
563 static void cc_setup_state_desc(struct crypto_tfm *tfm,
564 struct cipher_req_ctx *req_ctx,
565 unsigned int ivsize, unsigned int nbytes,
566 struct cc_hw_desc desc[],
567 unsigned int *seq_size)
569 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
570 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
571 int cipher_mode = ctx_p->cipher_mode;
572 int flow_mode = ctx_p->flow_mode;
573 int direction = req_ctx->gen_ctx.op_type;
574 dma_addr_t iv_dma_addr = req_ctx->gen_ctx.iv_dma_addr;
576 switch (cipher_mode) {
580 case DRV_CIPHER_CBC_CTS:
584 hw_desc_init(&desc[*seq_size]);
585 set_din_type(&desc[*seq_size], DMA_DLLI, iv_dma_addr, ivsize,
587 set_cipher_config0(&desc[*seq_size], direction);
588 set_flow_mode(&desc[*seq_size], flow_mode);
589 set_cipher_mode(&desc[*seq_size], cipher_mode);
590 if (cipher_mode == DRV_CIPHER_CTR ||
591 cipher_mode == DRV_CIPHER_OFB) {
592 set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE1);
594 set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE0);
599 case DRV_CIPHER_ESSIV:
602 dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
607 static void cc_setup_xex_state_desc(struct crypto_tfm *tfm,
608 struct cipher_req_ctx *req_ctx,
609 unsigned int ivsize, unsigned int nbytes,
610 struct cc_hw_desc desc[],
611 unsigned int *seq_size)
613 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
614 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
615 int cipher_mode = ctx_p->cipher_mode;
616 int flow_mode = ctx_p->flow_mode;
617 int direction = req_ctx->gen_ctx.op_type;
618 dma_addr_t key_dma_addr = ctx_p->user.key_dma_addr;
619 unsigned int key_len = (ctx_p->keylen / 2);
620 dma_addr_t iv_dma_addr = req_ctx->gen_ctx.iv_dma_addr;
621 unsigned int key_offset = key_len;
623 switch (cipher_mode) {
627 case DRV_CIPHER_CBC_CTS:
632 case DRV_CIPHER_ESSIV:
634 if (cipher_mode == DRV_CIPHER_ESSIV)
635 key_len = SHA256_DIGEST_SIZE;
638 hw_desc_init(&desc[*seq_size]);
639 set_cipher_mode(&desc[*seq_size], cipher_mode);
640 set_cipher_config0(&desc[*seq_size], direction);
641 if (cc_key_type(tfm) == CC_HW_PROTECTED_KEY) {
642 set_hw_crypto_key(&desc[*seq_size],
643 ctx_p->hw.key2_slot);
645 set_din_type(&desc[*seq_size], DMA_DLLI,
646 (key_dma_addr + key_offset),
649 set_xex_data_unit_size(&desc[*seq_size], nbytes);
650 set_flow_mode(&desc[*seq_size], S_DIN_to_AES2);
651 set_key_size_aes(&desc[*seq_size], key_len);
652 set_setup_mode(&desc[*seq_size], SETUP_LOAD_XEX_KEY);
656 hw_desc_init(&desc[*seq_size]);
657 set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE1);
658 set_cipher_mode(&desc[*seq_size], cipher_mode);
659 set_cipher_config0(&desc[*seq_size], direction);
660 set_key_size_aes(&desc[*seq_size], key_len);
661 set_flow_mode(&desc[*seq_size], flow_mode);
662 set_din_type(&desc[*seq_size], DMA_DLLI, iv_dma_addr,
663 CC_AES_BLOCK_SIZE, NS_BIT);
667 dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
671 static int cc_out_flow_mode(struct cc_cipher_ctx *ctx_p)
673 switch (ctx_p->flow_mode) {
681 return ctx_p->flow_mode;
685 static void cc_setup_key_desc(struct crypto_tfm *tfm,
686 struct cipher_req_ctx *req_ctx,
687 unsigned int nbytes, struct cc_hw_desc desc[],
688 unsigned int *seq_size)
690 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
691 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
692 int cipher_mode = ctx_p->cipher_mode;
693 int flow_mode = ctx_p->flow_mode;
694 int direction = req_ctx->gen_ctx.op_type;
695 dma_addr_t key_dma_addr = ctx_p->user.key_dma_addr;
696 unsigned int key_len = ctx_p->keylen;
697 unsigned int din_size;
699 switch (cipher_mode) {
701 case DRV_CIPHER_CBC_CTS:
706 hw_desc_init(&desc[*seq_size]);
707 set_cipher_mode(&desc[*seq_size], cipher_mode);
708 set_cipher_config0(&desc[*seq_size], direction);
710 if (cc_key_type(tfm) == CC_POLICY_PROTECTED_KEY) {
711 /* We use the AES key size coding for all CPP algs */
712 set_key_size_aes(&desc[*seq_size], key_len);
713 set_cpp_crypto_key(&desc[*seq_size], ctx_p->cpp.slot);
714 flow_mode = cc_out_flow_mode(ctx_p);
716 if (flow_mode == S_DIN_to_AES) {
717 if (cc_key_type(tfm) == CC_HW_PROTECTED_KEY) {
718 set_hw_crypto_key(&desc[*seq_size],
719 ctx_p->hw.key1_slot);
721 /* CC_POLICY_UNPROTECTED_KEY
722 * Invalid keys are filtered out in
725 din_size = (key_len == 24) ?
726 AES_MAX_KEY_SIZE : key_len;
728 set_din_type(&desc[*seq_size], DMA_DLLI,
729 key_dma_addr, din_size,
732 set_key_size_aes(&desc[*seq_size], key_len);
735 set_din_type(&desc[*seq_size], DMA_DLLI,
736 key_dma_addr, key_len, NS_BIT);
737 set_key_size_des(&desc[*seq_size], key_len);
739 set_setup_mode(&desc[*seq_size], SETUP_LOAD_KEY0);
741 set_flow_mode(&desc[*seq_size], flow_mode);
745 case DRV_CIPHER_ESSIV:
747 hw_desc_init(&desc[*seq_size]);
748 set_cipher_mode(&desc[*seq_size], cipher_mode);
749 set_cipher_config0(&desc[*seq_size], direction);
750 if (cc_key_type(tfm) == CC_HW_PROTECTED_KEY) {
751 set_hw_crypto_key(&desc[*seq_size],
752 ctx_p->hw.key1_slot);
754 set_din_type(&desc[*seq_size], DMA_DLLI, key_dma_addr,
755 (key_len / 2), NS_BIT);
757 set_key_size_aes(&desc[*seq_size], (key_len / 2));
758 set_flow_mode(&desc[*seq_size], flow_mode);
759 set_setup_mode(&desc[*seq_size], SETUP_LOAD_KEY0);
763 dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
767 static void cc_setup_mlli_desc(struct crypto_tfm *tfm,
768 struct cipher_req_ctx *req_ctx,
769 struct scatterlist *dst, struct scatterlist *src,
770 unsigned int nbytes, void *areq,
771 struct cc_hw_desc desc[], unsigned int *seq_size)
773 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
774 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
776 if (req_ctx->dma_buf_type == CC_DMA_BUF_MLLI) {
778 dev_dbg(dev, " bypass params addr %pad length 0x%X addr 0x%08X\n",
779 &req_ctx->mlli_params.mlli_dma_addr,
780 req_ctx->mlli_params.mlli_len,
781 ctx_p->drvdata->mlli_sram_addr);
782 hw_desc_init(&desc[*seq_size]);
783 set_din_type(&desc[*seq_size], DMA_DLLI,
784 req_ctx->mlli_params.mlli_dma_addr,
785 req_ctx->mlli_params.mlli_len, NS_BIT);
786 set_dout_sram(&desc[*seq_size],
787 ctx_p->drvdata->mlli_sram_addr,
788 req_ctx->mlli_params.mlli_len);
789 set_flow_mode(&desc[*seq_size], BYPASS);
794 static void cc_setup_flow_desc(struct crypto_tfm *tfm,
795 struct cipher_req_ctx *req_ctx,
796 struct scatterlist *dst, struct scatterlist *src,
797 unsigned int nbytes, struct cc_hw_desc desc[],
798 unsigned int *seq_size)
800 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
801 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
802 unsigned int flow_mode = cc_out_flow_mode(ctx_p);
803 bool last_desc = (ctx_p->key_type == CC_POLICY_PROTECTED_KEY ||
804 ctx_p->cipher_mode == DRV_CIPHER_ECB);
807 if (req_ctx->dma_buf_type == CC_DMA_BUF_DLLI) {
808 dev_dbg(dev, " data params addr %pad length 0x%X\n",
809 &sg_dma_address(src), nbytes);
810 dev_dbg(dev, " data params addr %pad length 0x%X\n",
811 &sg_dma_address(dst), nbytes);
812 hw_desc_init(&desc[*seq_size]);
813 set_din_type(&desc[*seq_size], DMA_DLLI, sg_dma_address(src),
815 set_dout_dlli(&desc[*seq_size], sg_dma_address(dst),
816 nbytes, NS_BIT, (!last_desc ? 0 : 1));
818 set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
820 set_flow_mode(&desc[*seq_size], flow_mode);
823 hw_desc_init(&desc[*seq_size]);
824 set_din_type(&desc[*seq_size], DMA_MLLI,
825 ctx_p->drvdata->mlli_sram_addr,
826 req_ctx->in_mlli_nents, NS_BIT);
827 if (req_ctx->out_nents == 0) {
828 dev_dbg(dev, " din/dout params addr 0x%08X addr 0x%08X\n",
829 ctx_p->drvdata->mlli_sram_addr,
830 ctx_p->drvdata->mlli_sram_addr);
831 set_dout_mlli(&desc[*seq_size],
832 ctx_p->drvdata->mlli_sram_addr,
833 req_ctx->in_mlli_nents, NS_BIT,
834 (!last_desc ? 0 : 1));
836 dev_dbg(dev, " din/dout params addr 0x%08X addr 0x%08X\n",
837 ctx_p->drvdata->mlli_sram_addr,
838 ctx_p->drvdata->mlli_sram_addr +
839 (u32)LLI_ENTRY_BYTE_SIZE * req_ctx->in_nents);
840 set_dout_mlli(&desc[*seq_size],
841 (ctx_p->drvdata->mlli_sram_addr +
842 (LLI_ENTRY_BYTE_SIZE *
843 req_ctx->in_mlli_nents)),
844 req_ctx->out_mlli_nents, NS_BIT,
845 (!last_desc ? 0 : 1));
848 set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
850 set_flow_mode(&desc[*seq_size], flow_mode);
855 static void cc_cipher_complete(struct device *dev, void *cc_req, int err)
857 struct skcipher_request *req = (struct skcipher_request *)cc_req;
858 struct scatterlist *dst = req->dst;
859 struct scatterlist *src = req->src;
860 struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
861 struct crypto_skcipher *sk_tfm = crypto_skcipher_reqtfm(req);
862 unsigned int ivsize = crypto_skcipher_ivsize(sk_tfm);
864 if (err != -EINPROGRESS) {
865 /* Not a BACKLOG notification */
866 cc_unmap_cipher_request(dev, req_ctx, ivsize, src, dst);
867 memcpy(req->iv, req_ctx->iv, ivsize);
868 kfree_sensitive(req_ctx->iv);
871 skcipher_request_complete(req, err);
874 static int cc_cipher_process(struct skcipher_request *req,
875 enum drv_crypto_direction direction)
877 struct crypto_skcipher *sk_tfm = crypto_skcipher_reqtfm(req);
878 struct crypto_tfm *tfm = crypto_skcipher_tfm(sk_tfm);
879 struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
880 unsigned int ivsize = crypto_skcipher_ivsize(sk_tfm);
881 struct scatterlist *dst = req->dst;
882 struct scatterlist *src = req->src;
883 unsigned int nbytes = req->cryptlen;
885 struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
886 struct device *dev = drvdata_to_dev(ctx_p->drvdata);
887 struct cc_hw_desc desc[MAX_SKCIPHER_SEQ_LEN];
888 struct cc_crypto_req cc_req = {};
890 unsigned int seq_len = 0;
891 gfp_t flags = cc_gfp_flags(&req->base);
893 dev_dbg(dev, "%s req=%p iv=%p nbytes=%d\n",
894 ((direction == DRV_CRYPTO_DIRECTION_ENCRYPT) ?
895 "Encrypt" : "Decrypt"), req, iv, nbytes);
897 /* STAT_PHASE_0: Init and sanity checks */
899 if (validate_data_size(ctx_p, nbytes)) {
900 dev_dbg(dev, "Unsupported data size %d.\n", nbytes);
905 /* No data to process is valid */
910 if (ctx_p->fallback_on) {
911 struct skcipher_request *subreq = skcipher_request_ctx(req);
914 skcipher_request_set_tfm(subreq, ctx_p->fallback_tfm);
915 if (direction == DRV_CRYPTO_DIRECTION_ENCRYPT)
916 return crypto_skcipher_encrypt(subreq);
918 return crypto_skcipher_decrypt(subreq);
921 /* The IV we are handed may be allocted from the stack so
922 * we must copy it to a DMAable buffer before use.
924 req_ctx->iv = kmemdup(iv, ivsize, flags);
930 /* Setup request structure */
931 cc_req.user_cb = cc_cipher_complete;
932 cc_req.user_arg = req;
934 /* Setup CPP operation details */
935 if (ctx_p->key_type == CC_POLICY_PROTECTED_KEY) {
936 cc_req.cpp.is_cpp = true;
937 cc_req.cpp.alg = ctx_p->cpp.alg;
938 cc_req.cpp.slot = ctx_p->cpp.slot;
941 /* Setup request context */
942 req_ctx->gen_ctx.op_type = direction;
944 /* STAT_PHASE_1: Map buffers */
946 rc = cc_map_cipher_request(ctx_p->drvdata, req_ctx, ivsize, nbytes,
947 req_ctx->iv, src, dst, flags);
949 dev_err(dev, "map_request() failed\n");
953 /* STAT_PHASE_2: Create sequence */
955 /* Setup state (IV) */
956 cc_setup_state_desc(tfm, req_ctx, ivsize, nbytes, desc, &seq_len);
957 /* Setup MLLI line, if needed */
958 cc_setup_mlli_desc(tfm, req_ctx, dst, src, nbytes, req, desc, &seq_len);
960 cc_setup_key_desc(tfm, req_ctx, nbytes, desc, &seq_len);
961 /* Setup state (IV and XEX key) */
962 cc_setup_xex_state_desc(tfm, req_ctx, ivsize, nbytes, desc, &seq_len);
963 /* Data processing */
964 cc_setup_flow_desc(tfm, req_ctx, dst, src, nbytes, desc, &seq_len);
966 cc_setup_readiv_desc(tfm, req_ctx, ivsize, desc, &seq_len);
968 /* STAT_PHASE_3: Lock HW and push sequence */
970 rc = cc_send_request(ctx_p->drvdata, &cc_req, desc, seq_len,
972 if (rc != -EINPROGRESS && rc != -EBUSY) {
973 /* Failed to send the request or request completed
976 cc_unmap_cipher_request(dev, req_ctx, ivsize, src, dst);
980 if (rc != -EINPROGRESS && rc != -EBUSY) {
981 kfree_sensitive(req_ctx->iv);
987 static int cc_cipher_encrypt(struct skcipher_request *req)
989 struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
991 memset(req_ctx, 0, sizeof(*req_ctx));
993 return cc_cipher_process(req, DRV_CRYPTO_DIRECTION_ENCRYPT);
996 static int cc_cipher_decrypt(struct skcipher_request *req)
998 struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
1000 memset(req_ctx, 0, sizeof(*req_ctx));
1002 return cc_cipher_process(req, DRV_CRYPTO_DIRECTION_DECRYPT);
1005 /* Block cipher alg */
1006 static const struct cc_alg_template skcipher_algs[] = {
1008 .name = "xts(paes)",
1009 .driver_name = "xts-paes-ccree",
1011 .template_skcipher = {
1012 .setkey = cc_cipher_sethkey,
1013 .encrypt = cc_cipher_encrypt,
1014 .decrypt = cc_cipher_decrypt,
1015 .min_keysize = CC_HW_KEY_SIZE,
1016 .max_keysize = CC_HW_KEY_SIZE,
1017 .ivsize = AES_BLOCK_SIZE,
1019 .cipher_mode = DRV_CIPHER_XTS,
1020 .flow_mode = S_DIN_to_AES,
1021 .min_hw_rev = CC_HW_REV_630,
1022 .std_body = CC_STD_NIST,
1026 .name = "essiv(cbc(paes),sha256)",
1027 .driver_name = "essiv-paes-ccree",
1028 .blocksize = AES_BLOCK_SIZE,
1029 .template_skcipher = {
1030 .setkey = cc_cipher_sethkey,
1031 .encrypt = cc_cipher_encrypt,
1032 .decrypt = cc_cipher_decrypt,
1033 .min_keysize = CC_HW_KEY_SIZE,
1034 .max_keysize = CC_HW_KEY_SIZE,
1035 .ivsize = AES_BLOCK_SIZE,
1037 .cipher_mode = DRV_CIPHER_ESSIV,
1038 .flow_mode = S_DIN_to_AES,
1039 .min_hw_rev = CC_HW_REV_712,
1040 .std_body = CC_STD_NIST,
1044 .name = "ecb(paes)",
1045 .driver_name = "ecb-paes-ccree",
1046 .blocksize = AES_BLOCK_SIZE,
1047 .template_skcipher = {
1048 .setkey = cc_cipher_sethkey,
1049 .encrypt = cc_cipher_encrypt,
1050 .decrypt = cc_cipher_decrypt,
1051 .min_keysize = CC_HW_KEY_SIZE,
1052 .max_keysize = CC_HW_KEY_SIZE,
1055 .cipher_mode = DRV_CIPHER_ECB,
1056 .flow_mode = S_DIN_to_AES,
1057 .min_hw_rev = CC_HW_REV_712,
1058 .std_body = CC_STD_NIST,
1062 .name = "cbc(paes)",
1063 .driver_name = "cbc-paes-ccree",
1064 .blocksize = AES_BLOCK_SIZE,
1065 .template_skcipher = {
1066 .setkey = cc_cipher_sethkey,
1067 .encrypt = cc_cipher_encrypt,
1068 .decrypt = cc_cipher_decrypt,
1069 .min_keysize = CC_HW_KEY_SIZE,
1070 .max_keysize = CC_HW_KEY_SIZE,
1071 .ivsize = AES_BLOCK_SIZE,
1073 .cipher_mode = DRV_CIPHER_CBC,
1074 .flow_mode = S_DIN_to_AES,
1075 .min_hw_rev = CC_HW_REV_712,
1076 .std_body = CC_STD_NIST,
1080 .name = "ofb(paes)",
1081 .driver_name = "ofb-paes-ccree",
1082 .blocksize = AES_BLOCK_SIZE,
1083 .template_skcipher = {
1084 .setkey = cc_cipher_sethkey,
1085 .encrypt = cc_cipher_encrypt,
1086 .decrypt = cc_cipher_decrypt,
1087 .min_keysize = CC_HW_KEY_SIZE,
1088 .max_keysize = CC_HW_KEY_SIZE,
1089 .ivsize = AES_BLOCK_SIZE,
1091 .cipher_mode = DRV_CIPHER_OFB,
1092 .flow_mode = S_DIN_to_AES,
1093 .min_hw_rev = CC_HW_REV_712,
1094 .std_body = CC_STD_NIST,
1098 .name = "cts(cbc(paes))",
1099 .driver_name = "cts-cbc-paes-ccree",
1100 .blocksize = AES_BLOCK_SIZE,
1101 .template_skcipher = {
1102 .setkey = cc_cipher_sethkey,
1103 .encrypt = cc_cipher_encrypt,
1104 .decrypt = cc_cipher_decrypt,
1105 .min_keysize = CC_HW_KEY_SIZE,
1106 .max_keysize = CC_HW_KEY_SIZE,
1107 .ivsize = AES_BLOCK_SIZE,
1109 .cipher_mode = DRV_CIPHER_CBC_CTS,
1110 .flow_mode = S_DIN_to_AES,
1111 .min_hw_rev = CC_HW_REV_712,
1112 .std_body = CC_STD_NIST,
1116 .name = "ctr(paes)",
1117 .driver_name = "ctr-paes-ccree",
1119 .template_skcipher = {
1120 .setkey = cc_cipher_sethkey,
1121 .encrypt = cc_cipher_encrypt,
1122 .decrypt = cc_cipher_decrypt,
1123 .min_keysize = CC_HW_KEY_SIZE,
1124 .max_keysize = CC_HW_KEY_SIZE,
1125 .ivsize = AES_BLOCK_SIZE,
1127 .cipher_mode = DRV_CIPHER_CTR,
1128 .flow_mode = S_DIN_to_AES,
1129 .min_hw_rev = CC_HW_REV_712,
1130 .std_body = CC_STD_NIST,
1134 /* See https://www.mail-archive.com/linux-crypto@vger.kernel.org/msg40576.html
1135 * for the reason why this differs from the generic
1139 .driver_name = "xts-aes-ccree",
1141 .template_skcipher = {
1142 .setkey = cc_cipher_setkey,
1143 .encrypt = cc_cipher_encrypt,
1144 .decrypt = cc_cipher_decrypt,
1145 .min_keysize = AES_MIN_KEY_SIZE * 2,
1146 .max_keysize = AES_MAX_KEY_SIZE * 2,
1147 .ivsize = AES_BLOCK_SIZE,
1149 .cipher_mode = DRV_CIPHER_XTS,
1150 .flow_mode = S_DIN_to_AES,
1151 .min_hw_rev = CC_HW_REV_630,
1152 .std_body = CC_STD_NIST,
1155 .name = "essiv(cbc(aes),sha256)",
1156 .driver_name = "essiv-aes-ccree",
1157 .blocksize = AES_BLOCK_SIZE,
1158 .template_skcipher = {
1159 .setkey = cc_cipher_setkey,
1160 .encrypt = cc_cipher_encrypt,
1161 .decrypt = cc_cipher_decrypt,
1162 .min_keysize = AES_MIN_KEY_SIZE,
1163 .max_keysize = AES_MAX_KEY_SIZE,
1164 .ivsize = AES_BLOCK_SIZE,
1166 .cipher_mode = DRV_CIPHER_ESSIV,
1167 .flow_mode = S_DIN_to_AES,
1168 .min_hw_rev = CC_HW_REV_712,
1169 .std_body = CC_STD_NIST,
1173 .driver_name = "ecb-aes-ccree",
1174 .blocksize = AES_BLOCK_SIZE,
1175 .template_skcipher = {
1176 .setkey = cc_cipher_setkey,
1177 .encrypt = cc_cipher_encrypt,
1178 .decrypt = cc_cipher_decrypt,
1179 .min_keysize = AES_MIN_KEY_SIZE,
1180 .max_keysize = AES_MAX_KEY_SIZE,
1183 .cipher_mode = DRV_CIPHER_ECB,
1184 .flow_mode = S_DIN_to_AES,
1185 .min_hw_rev = CC_HW_REV_630,
1186 .std_body = CC_STD_NIST,
1190 .driver_name = "cbc-aes-ccree",
1191 .blocksize = AES_BLOCK_SIZE,
1192 .template_skcipher = {
1193 .setkey = cc_cipher_setkey,
1194 .encrypt = cc_cipher_encrypt,
1195 .decrypt = cc_cipher_decrypt,
1196 .min_keysize = AES_MIN_KEY_SIZE,
1197 .max_keysize = AES_MAX_KEY_SIZE,
1198 .ivsize = AES_BLOCK_SIZE,
1200 .cipher_mode = DRV_CIPHER_CBC,
1201 .flow_mode = S_DIN_to_AES,
1202 .min_hw_rev = CC_HW_REV_630,
1203 .std_body = CC_STD_NIST,
1207 .driver_name = "ofb-aes-ccree",
1209 .template_skcipher = {
1210 .setkey = cc_cipher_setkey,
1211 .encrypt = cc_cipher_encrypt,
1212 .decrypt = cc_cipher_decrypt,
1213 .min_keysize = AES_MIN_KEY_SIZE,
1214 .max_keysize = AES_MAX_KEY_SIZE,
1215 .ivsize = AES_BLOCK_SIZE,
1217 .cipher_mode = DRV_CIPHER_OFB,
1218 .flow_mode = S_DIN_to_AES,
1219 .min_hw_rev = CC_HW_REV_630,
1220 .std_body = CC_STD_NIST,
1223 .name = "cts(cbc(aes))",
1224 .driver_name = "cts-cbc-aes-ccree",
1225 .blocksize = AES_BLOCK_SIZE,
1226 .template_skcipher = {
1227 .setkey = cc_cipher_setkey,
1228 .encrypt = cc_cipher_encrypt,
1229 .decrypt = cc_cipher_decrypt,
1230 .min_keysize = AES_MIN_KEY_SIZE,
1231 .max_keysize = AES_MAX_KEY_SIZE,
1232 .ivsize = AES_BLOCK_SIZE,
1234 .cipher_mode = DRV_CIPHER_CBC_CTS,
1235 .flow_mode = S_DIN_to_AES,
1236 .min_hw_rev = CC_HW_REV_630,
1237 .std_body = CC_STD_NIST,
1241 .driver_name = "ctr-aes-ccree",
1243 .template_skcipher = {
1244 .setkey = cc_cipher_setkey,
1245 .encrypt = cc_cipher_encrypt,
1246 .decrypt = cc_cipher_decrypt,
1247 .min_keysize = AES_MIN_KEY_SIZE,
1248 .max_keysize = AES_MAX_KEY_SIZE,
1249 .ivsize = AES_BLOCK_SIZE,
1251 .cipher_mode = DRV_CIPHER_CTR,
1252 .flow_mode = S_DIN_to_AES,
1253 .min_hw_rev = CC_HW_REV_630,
1254 .std_body = CC_STD_NIST,
1257 .name = "cbc(des3_ede)",
1258 .driver_name = "cbc-3des-ccree",
1259 .blocksize = DES3_EDE_BLOCK_SIZE,
1260 .template_skcipher = {
1261 .setkey = cc_cipher_setkey,
1262 .encrypt = cc_cipher_encrypt,
1263 .decrypt = cc_cipher_decrypt,
1264 .min_keysize = DES3_EDE_KEY_SIZE,
1265 .max_keysize = DES3_EDE_KEY_SIZE,
1266 .ivsize = DES3_EDE_BLOCK_SIZE,
1268 .cipher_mode = DRV_CIPHER_CBC,
1269 .flow_mode = S_DIN_to_DES,
1270 .min_hw_rev = CC_HW_REV_630,
1271 .std_body = CC_STD_NIST,
1274 .name = "ecb(des3_ede)",
1275 .driver_name = "ecb-3des-ccree",
1276 .blocksize = DES3_EDE_BLOCK_SIZE,
1277 .template_skcipher = {
1278 .setkey = cc_cipher_setkey,
1279 .encrypt = cc_cipher_encrypt,
1280 .decrypt = cc_cipher_decrypt,
1281 .min_keysize = DES3_EDE_KEY_SIZE,
1282 .max_keysize = DES3_EDE_KEY_SIZE,
1285 .cipher_mode = DRV_CIPHER_ECB,
1286 .flow_mode = S_DIN_to_DES,
1287 .min_hw_rev = CC_HW_REV_630,
1288 .std_body = CC_STD_NIST,
1292 .driver_name = "cbc-des-ccree",
1293 .blocksize = DES_BLOCK_SIZE,
1294 .template_skcipher = {
1295 .setkey = cc_cipher_setkey,
1296 .encrypt = cc_cipher_encrypt,
1297 .decrypt = cc_cipher_decrypt,
1298 .min_keysize = DES_KEY_SIZE,
1299 .max_keysize = DES_KEY_SIZE,
1300 .ivsize = DES_BLOCK_SIZE,
1302 .cipher_mode = DRV_CIPHER_CBC,
1303 .flow_mode = S_DIN_to_DES,
1304 .min_hw_rev = CC_HW_REV_630,
1305 .std_body = CC_STD_NIST,
1309 .driver_name = "ecb-des-ccree",
1310 .blocksize = DES_BLOCK_SIZE,
1311 .template_skcipher = {
1312 .setkey = cc_cipher_setkey,
1313 .encrypt = cc_cipher_encrypt,
1314 .decrypt = cc_cipher_decrypt,
1315 .min_keysize = DES_KEY_SIZE,
1316 .max_keysize = DES_KEY_SIZE,
1319 .cipher_mode = DRV_CIPHER_ECB,
1320 .flow_mode = S_DIN_to_DES,
1321 .min_hw_rev = CC_HW_REV_630,
1322 .std_body = CC_STD_NIST,
1326 .driver_name = "cbc-sm4-ccree",
1327 .blocksize = SM4_BLOCK_SIZE,
1328 .template_skcipher = {
1329 .setkey = cc_cipher_setkey,
1330 .encrypt = cc_cipher_encrypt,
1331 .decrypt = cc_cipher_decrypt,
1332 .min_keysize = SM4_KEY_SIZE,
1333 .max_keysize = SM4_KEY_SIZE,
1334 .ivsize = SM4_BLOCK_SIZE,
1336 .cipher_mode = DRV_CIPHER_CBC,
1337 .flow_mode = S_DIN_to_SM4,
1338 .min_hw_rev = CC_HW_REV_713,
1339 .std_body = CC_STD_OSCCA,
1343 .driver_name = "ecb-sm4-ccree",
1344 .blocksize = SM4_BLOCK_SIZE,
1345 .template_skcipher = {
1346 .setkey = cc_cipher_setkey,
1347 .encrypt = cc_cipher_encrypt,
1348 .decrypt = cc_cipher_decrypt,
1349 .min_keysize = SM4_KEY_SIZE,
1350 .max_keysize = SM4_KEY_SIZE,
1353 .cipher_mode = DRV_CIPHER_ECB,
1354 .flow_mode = S_DIN_to_SM4,
1355 .min_hw_rev = CC_HW_REV_713,
1356 .std_body = CC_STD_OSCCA,
1360 .driver_name = "ctr-sm4-ccree",
1362 .template_skcipher = {
1363 .setkey = cc_cipher_setkey,
1364 .encrypt = cc_cipher_encrypt,
1365 .decrypt = cc_cipher_decrypt,
1366 .min_keysize = SM4_KEY_SIZE,
1367 .max_keysize = SM4_KEY_SIZE,
1368 .ivsize = SM4_BLOCK_SIZE,
1370 .cipher_mode = DRV_CIPHER_CTR,
1371 .flow_mode = S_DIN_to_SM4,
1372 .min_hw_rev = CC_HW_REV_713,
1373 .std_body = CC_STD_OSCCA,
1376 .name = "cbc(psm4)",
1377 .driver_name = "cbc-psm4-ccree",
1378 .blocksize = SM4_BLOCK_SIZE,
1379 .template_skcipher = {
1380 .setkey = cc_cipher_sethkey,
1381 .encrypt = cc_cipher_encrypt,
1382 .decrypt = cc_cipher_decrypt,
1383 .min_keysize = CC_HW_KEY_SIZE,
1384 .max_keysize = CC_HW_KEY_SIZE,
1385 .ivsize = SM4_BLOCK_SIZE,
1387 .cipher_mode = DRV_CIPHER_CBC,
1388 .flow_mode = S_DIN_to_SM4,
1389 .min_hw_rev = CC_HW_REV_713,
1390 .std_body = CC_STD_OSCCA,
1394 .name = "ctr(psm4)",
1395 .driver_name = "ctr-psm4-ccree",
1396 .blocksize = SM4_BLOCK_SIZE,
1397 .template_skcipher = {
1398 .setkey = cc_cipher_sethkey,
1399 .encrypt = cc_cipher_encrypt,
1400 .decrypt = cc_cipher_decrypt,
1401 .min_keysize = CC_HW_KEY_SIZE,
1402 .max_keysize = CC_HW_KEY_SIZE,
1403 .ivsize = SM4_BLOCK_SIZE,
1405 .cipher_mode = DRV_CIPHER_CTR,
1406 .flow_mode = S_DIN_to_SM4,
1407 .min_hw_rev = CC_HW_REV_713,
1408 .std_body = CC_STD_OSCCA,
1413 static struct cc_crypto_alg *cc_create_alg(const struct cc_alg_template *tmpl,
1416 struct cc_crypto_alg *t_alg;
1417 struct skcipher_alg *alg;
1419 t_alg = devm_kzalloc(dev, sizeof(*t_alg), GFP_KERNEL);
1421 return ERR_PTR(-ENOMEM);
1423 alg = &t_alg->skcipher_alg;
1425 memcpy(alg, &tmpl->template_skcipher, sizeof(*alg));
1427 snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", tmpl->name);
1428 snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
1430 alg->base.cra_module = THIS_MODULE;
1431 alg->base.cra_priority = CC_CRA_PRIO;
1432 alg->base.cra_blocksize = tmpl->blocksize;
1433 alg->base.cra_alignmask = 0;
1434 alg->base.cra_ctxsize = sizeof(struct cc_cipher_ctx);
1436 alg->base.cra_init = cc_cipher_init;
1437 alg->base.cra_exit = cc_cipher_exit;
1438 alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
1440 t_alg->cipher_mode = tmpl->cipher_mode;
1441 t_alg->flow_mode = tmpl->flow_mode;
1446 int cc_cipher_free(struct cc_drvdata *drvdata)
1448 struct cc_crypto_alg *t_alg, *n;
1450 /* Remove registered algs */
1451 list_for_each_entry_safe(t_alg, n, &drvdata->alg_list, entry) {
1452 crypto_unregister_skcipher(&t_alg->skcipher_alg);
1453 list_del(&t_alg->entry);
1458 int cc_cipher_alloc(struct cc_drvdata *drvdata)
1460 struct cc_crypto_alg *t_alg;
1461 struct device *dev = drvdata_to_dev(drvdata);
1465 INIT_LIST_HEAD(&drvdata->alg_list);
1468 dev_dbg(dev, "Number of algorithms = %zu\n",
1469 ARRAY_SIZE(skcipher_algs));
1470 for (alg = 0; alg < ARRAY_SIZE(skcipher_algs); alg++) {
1471 if ((skcipher_algs[alg].min_hw_rev > drvdata->hw_rev) ||
1472 !(drvdata->std_bodies & skcipher_algs[alg].std_body) ||
1473 (drvdata->sec_disabled && skcipher_algs[alg].sec_func))
1476 dev_dbg(dev, "creating %s\n", skcipher_algs[alg].driver_name);
1477 t_alg = cc_create_alg(&skcipher_algs[alg], dev);
1478 if (IS_ERR(t_alg)) {
1479 rc = PTR_ERR(t_alg);
1480 dev_err(dev, "%s alg allocation failed\n",
1481 skcipher_algs[alg].driver_name);
1484 t_alg->drvdata = drvdata;
1486 dev_dbg(dev, "registering %s\n",
1487 skcipher_algs[alg].driver_name);
1488 rc = crypto_register_skcipher(&t_alg->skcipher_alg);
1489 dev_dbg(dev, "%s alg registration rc = %x\n",
1490 t_alg->skcipher_alg.base.cra_driver_name, rc);
1492 dev_err(dev, "%s alg registration failed\n",
1493 t_alg->skcipher_alg.base.cra_driver_name);
1497 list_add_tail(&t_alg->entry, &drvdata->alg_list);
1498 dev_dbg(dev, "Registered %s\n",
1499 t_alg->skcipher_alg.base.cra_driver_name);
1504 cc_cipher_free(drvdata);