xref: /openbmc/linux/drivers/crypto/ccree/cc_cipher.c (revision 03963cae)
163ee04c8SGilad Ben-Yossef // SPDX-License-Identifier: GPL-2.0
203963caeSGilad Ben-Yossef /* Copyright (C) 2012-2019 ARM Limited (or its affiliates). */
363ee04c8SGilad Ben-Yossef 
463ee04c8SGilad Ben-Yossef #include <linux/kernel.h>
563ee04c8SGilad Ben-Yossef #include <linux/module.h>
663ee04c8SGilad Ben-Yossef #include <crypto/algapi.h>
763ee04c8SGilad Ben-Yossef #include <crypto/internal/skcipher.h>
863ee04c8SGilad Ben-Yossef #include <crypto/des.h>
963ee04c8SGilad Ben-Yossef #include <crypto/xts.h>
109b8d51f8SGilad Ben-Yossef #include <crypto/sm4.h>
1163ee04c8SGilad Ben-Yossef #include <crypto/scatterwalk.h>
1263ee04c8SGilad Ben-Yossef 
1363ee04c8SGilad Ben-Yossef #include "cc_driver.h"
1463ee04c8SGilad Ben-Yossef #include "cc_lli_defs.h"
1563ee04c8SGilad Ben-Yossef #include "cc_buffer_mgr.h"
1663ee04c8SGilad Ben-Yossef #include "cc_cipher.h"
1763ee04c8SGilad Ben-Yossef #include "cc_request_mgr.h"
1863ee04c8SGilad Ben-Yossef 
1963ee04c8SGilad Ben-Yossef #define MAX_ABLKCIPHER_SEQ_LEN 6
2063ee04c8SGilad Ben-Yossef 
2163ee04c8SGilad Ben-Yossef #define template_skcipher	template_u.skcipher
2263ee04c8SGilad Ben-Yossef 
2363ee04c8SGilad Ben-Yossef struct cc_cipher_handle {
2463ee04c8SGilad Ben-Yossef 	struct list_head alg_list;
2563ee04c8SGilad Ben-Yossef };
2663ee04c8SGilad Ben-Yossef 
2763ee04c8SGilad Ben-Yossef struct cc_user_key_info {
2863ee04c8SGilad Ben-Yossef 	u8 *key;
2963ee04c8SGilad Ben-Yossef 	dma_addr_t key_dma_addr;
3063ee04c8SGilad Ben-Yossef };
3163ee04c8SGilad Ben-Yossef 
3263ee04c8SGilad Ben-Yossef struct cc_hw_key_info {
3363ee04c8SGilad Ben-Yossef 	enum cc_hw_crypto_key key1_slot;
3463ee04c8SGilad Ben-Yossef 	enum cc_hw_crypto_key key2_slot;
3563ee04c8SGilad Ben-Yossef };
3663ee04c8SGilad Ben-Yossef 
3752f42c65SGilad Ben-Yossef struct cc_cpp_key_info {
3852f42c65SGilad Ben-Yossef 	u8 slot;
3952f42c65SGilad Ben-Yossef 	enum cc_cpp_alg alg;
4052f42c65SGilad Ben-Yossef };
4152f42c65SGilad Ben-Yossef 
4252f42c65SGilad Ben-Yossef enum cc_key_type {
4352f42c65SGilad Ben-Yossef 	CC_UNPROTECTED_KEY,		/* User key */
4452f42c65SGilad Ben-Yossef 	CC_HW_PROTECTED_KEY,		/* HW (FDE) key */
4552f42c65SGilad Ben-Yossef 	CC_POLICY_PROTECTED_KEY,	/* CPP key */
4652f42c65SGilad Ben-Yossef 	CC_INVALID_PROTECTED_KEY	/* Invalid key */
4752f42c65SGilad Ben-Yossef };
4852f42c65SGilad Ben-Yossef 
4963ee04c8SGilad Ben-Yossef struct cc_cipher_ctx {
5063ee04c8SGilad Ben-Yossef 	struct cc_drvdata *drvdata;
5163ee04c8SGilad Ben-Yossef 	int keylen;
5263ee04c8SGilad Ben-Yossef 	int key_round_number;
5363ee04c8SGilad Ben-Yossef 	int cipher_mode;
5463ee04c8SGilad Ben-Yossef 	int flow_mode;
5563ee04c8SGilad Ben-Yossef 	unsigned int flags;
5652f42c65SGilad Ben-Yossef 	enum cc_key_type key_type;
5763ee04c8SGilad Ben-Yossef 	struct cc_user_key_info user;
5852f42c65SGilad Ben-Yossef 	union {
5963ee04c8SGilad Ben-Yossef 		struct cc_hw_key_info hw;
6052f42c65SGilad Ben-Yossef 		struct cc_cpp_key_info cpp;
6152f42c65SGilad Ben-Yossef 	};
6263ee04c8SGilad Ben-Yossef 	struct crypto_shash *shash_tfm;
6363ee04c8SGilad Ben-Yossef };
6463ee04c8SGilad Ben-Yossef 
6563ee04c8SGilad Ben-Yossef static void cc_cipher_complete(struct device *dev, void *cc_req, int err);
6663ee04c8SGilad Ben-Yossef 
6752f42c65SGilad Ben-Yossef static inline enum cc_key_type cc_key_type(struct crypto_tfm *tfm)
68a794d8d8SGilad Ben-Yossef {
69a794d8d8SGilad Ben-Yossef 	struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
70a794d8d8SGilad Ben-Yossef 
7152f42c65SGilad Ben-Yossef 	return ctx_p->key_type;
72a794d8d8SGilad Ben-Yossef }
73a794d8d8SGilad Ben-Yossef 
7463ee04c8SGilad Ben-Yossef static int validate_keys_sizes(struct cc_cipher_ctx *ctx_p, u32 size)
7563ee04c8SGilad Ben-Yossef {
7663ee04c8SGilad Ben-Yossef 	switch (ctx_p->flow_mode) {
7763ee04c8SGilad Ben-Yossef 	case S_DIN_to_AES:
7863ee04c8SGilad Ben-Yossef 		switch (size) {
7963ee04c8SGilad Ben-Yossef 		case CC_AES_128_BIT_KEY_SIZE:
8063ee04c8SGilad Ben-Yossef 		case CC_AES_192_BIT_KEY_SIZE:
8163ee04c8SGilad Ben-Yossef 			if (ctx_p->cipher_mode != DRV_CIPHER_XTS &&
8263ee04c8SGilad Ben-Yossef 			    ctx_p->cipher_mode != DRV_CIPHER_ESSIV &&
8363ee04c8SGilad Ben-Yossef 			    ctx_p->cipher_mode != DRV_CIPHER_BITLOCKER)
8463ee04c8SGilad Ben-Yossef 				return 0;
8563ee04c8SGilad Ben-Yossef 			break;
8663ee04c8SGilad Ben-Yossef 		case CC_AES_256_BIT_KEY_SIZE:
8763ee04c8SGilad Ben-Yossef 			return 0;
8863ee04c8SGilad Ben-Yossef 		case (CC_AES_192_BIT_KEY_SIZE * 2):
8963ee04c8SGilad Ben-Yossef 		case (CC_AES_256_BIT_KEY_SIZE * 2):
9063ee04c8SGilad Ben-Yossef 			if (ctx_p->cipher_mode == DRV_CIPHER_XTS ||
9163ee04c8SGilad Ben-Yossef 			    ctx_p->cipher_mode == DRV_CIPHER_ESSIV ||
9263ee04c8SGilad Ben-Yossef 			    ctx_p->cipher_mode == DRV_CIPHER_BITLOCKER)
9363ee04c8SGilad Ben-Yossef 				return 0;
9463ee04c8SGilad Ben-Yossef 			break;
9563ee04c8SGilad Ben-Yossef 		default:
9663ee04c8SGilad Ben-Yossef 			break;
9763ee04c8SGilad Ben-Yossef 		}
98b5be8531SGustavo A. R. Silva 		break;
9963ee04c8SGilad Ben-Yossef 	case S_DIN_to_DES:
10063ee04c8SGilad Ben-Yossef 		if (size == DES3_EDE_KEY_SIZE || size == DES_KEY_SIZE)
10163ee04c8SGilad Ben-Yossef 			return 0;
10263ee04c8SGilad Ben-Yossef 		break;
1039b8d51f8SGilad Ben-Yossef 	case S_DIN_to_SM4:
1049b8d51f8SGilad Ben-Yossef 		if (size == SM4_KEY_SIZE)
1059b8d51f8SGilad Ben-Yossef 			return 0;
10663ee04c8SGilad Ben-Yossef 	default:
10763ee04c8SGilad Ben-Yossef 		break;
10863ee04c8SGilad Ben-Yossef 	}
10963ee04c8SGilad Ben-Yossef 	return -EINVAL;
11063ee04c8SGilad Ben-Yossef }
11163ee04c8SGilad Ben-Yossef 
11263ee04c8SGilad Ben-Yossef static int validate_data_size(struct cc_cipher_ctx *ctx_p,
11363ee04c8SGilad Ben-Yossef 			      unsigned int size)
11463ee04c8SGilad Ben-Yossef {
11563ee04c8SGilad Ben-Yossef 	switch (ctx_p->flow_mode) {
11663ee04c8SGilad Ben-Yossef 	case S_DIN_to_AES:
11763ee04c8SGilad Ben-Yossef 		switch (ctx_p->cipher_mode) {
11863ee04c8SGilad Ben-Yossef 		case DRV_CIPHER_XTS:
119f53ad3e1SGilad Ben-Yossef 			if (size >= AES_BLOCK_SIZE &&
12063ee04c8SGilad Ben-Yossef 			    IS_ALIGNED(size, AES_BLOCK_SIZE))
12163ee04c8SGilad Ben-Yossef 				return 0;
12263ee04c8SGilad Ben-Yossef 			break;
12363ee04c8SGilad Ben-Yossef 		case DRV_CIPHER_CBC_CTS:
12463ee04c8SGilad Ben-Yossef 			if (size >= AES_BLOCK_SIZE)
12563ee04c8SGilad Ben-Yossef 				return 0;
12663ee04c8SGilad Ben-Yossef 			break;
12763ee04c8SGilad Ben-Yossef 		case DRV_CIPHER_OFB:
12863ee04c8SGilad Ben-Yossef 		case DRV_CIPHER_CTR:
12963ee04c8SGilad Ben-Yossef 				return 0;
13063ee04c8SGilad Ben-Yossef 		case DRV_CIPHER_ECB:
13163ee04c8SGilad Ben-Yossef 		case DRV_CIPHER_CBC:
13263ee04c8SGilad Ben-Yossef 		case DRV_CIPHER_ESSIV:
13363ee04c8SGilad Ben-Yossef 		case DRV_CIPHER_BITLOCKER:
13463ee04c8SGilad Ben-Yossef 			if (IS_ALIGNED(size, AES_BLOCK_SIZE))
13563ee04c8SGilad Ben-Yossef 				return 0;
13663ee04c8SGilad Ben-Yossef 			break;
13763ee04c8SGilad Ben-Yossef 		default:
13863ee04c8SGilad Ben-Yossef 			break;
13963ee04c8SGilad Ben-Yossef 		}
14063ee04c8SGilad Ben-Yossef 		break;
14163ee04c8SGilad Ben-Yossef 	case S_DIN_to_DES:
14263ee04c8SGilad Ben-Yossef 		if (IS_ALIGNED(size, DES_BLOCK_SIZE))
14363ee04c8SGilad Ben-Yossef 			return 0;
14463ee04c8SGilad Ben-Yossef 		break;
1459b8d51f8SGilad Ben-Yossef 	case S_DIN_to_SM4:
1469b8d51f8SGilad Ben-Yossef 		switch (ctx_p->cipher_mode) {
1479b8d51f8SGilad Ben-Yossef 		case DRV_CIPHER_CTR:
1489b8d51f8SGilad Ben-Yossef 			return 0;
1499b8d51f8SGilad Ben-Yossef 		case DRV_CIPHER_ECB:
1509b8d51f8SGilad Ben-Yossef 		case DRV_CIPHER_CBC:
1519b8d51f8SGilad Ben-Yossef 			if (IS_ALIGNED(size, SM4_BLOCK_SIZE))
1529b8d51f8SGilad Ben-Yossef 				return 0;
1539b8d51f8SGilad Ben-Yossef 		default:
1549b8d51f8SGilad Ben-Yossef 			break;
1559b8d51f8SGilad Ben-Yossef 		}
15663ee04c8SGilad Ben-Yossef 	default:
15763ee04c8SGilad Ben-Yossef 		break;
15863ee04c8SGilad Ben-Yossef 	}
15963ee04c8SGilad Ben-Yossef 	return -EINVAL;
16063ee04c8SGilad Ben-Yossef }
16163ee04c8SGilad Ben-Yossef 
16263ee04c8SGilad Ben-Yossef static int cc_cipher_init(struct crypto_tfm *tfm)
16363ee04c8SGilad Ben-Yossef {
16463ee04c8SGilad Ben-Yossef 	struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
16563ee04c8SGilad Ben-Yossef 	struct cc_crypto_alg *cc_alg =
16663ee04c8SGilad Ben-Yossef 			container_of(tfm->__crt_alg, struct cc_crypto_alg,
16763ee04c8SGilad Ben-Yossef 				     skcipher_alg.base);
16863ee04c8SGilad Ben-Yossef 	struct device *dev = drvdata_to_dev(cc_alg->drvdata);
16963ee04c8SGilad Ben-Yossef 	unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
17063ee04c8SGilad Ben-Yossef 	int rc = 0;
17163ee04c8SGilad Ben-Yossef 
17263ee04c8SGilad Ben-Yossef 	dev_dbg(dev, "Initializing context @%p for %s\n", ctx_p,
17363ee04c8SGilad Ben-Yossef 		crypto_tfm_alg_name(tfm));
17463ee04c8SGilad Ben-Yossef 
17563ee04c8SGilad Ben-Yossef 	crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
17663ee04c8SGilad Ben-Yossef 				    sizeof(struct cipher_req_ctx));
17763ee04c8SGilad Ben-Yossef 
17863ee04c8SGilad Ben-Yossef 	ctx_p->cipher_mode = cc_alg->cipher_mode;
17963ee04c8SGilad Ben-Yossef 	ctx_p->flow_mode = cc_alg->flow_mode;
18063ee04c8SGilad Ben-Yossef 	ctx_p->drvdata = cc_alg->drvdata;
18163ee04c8SGilad Ben-Yossef 
18263ee04c8SGilad Ben-Yossef 	/* Allocate key buffer, cache line aligned */
18363ee04c8SGilad Ben-Yossef 	ctx_p->user.key = kmalloc(max_key_buf_size, GFP_KERNEL);
18463ee04c8SGilad Ben-Yossef 	if (!ctx_p->user.key)
18563ee04c8SGilad Ben-Yossef 		return -ENOMEM;
18663ee04c8SGilad Ben-Yossef 
18763ee04c8SGilad Ben-Yossef 	dev_dbg(dev, "Allocated key buffer in context. key=@%p\n",
18863ee04c8SGilad Ben-Yossef 		ctx_p->user.key);
18963ee04c8SGilad Ben-Yossef 
19063ee04c8SGilad Ben-Yossef 	/* Map key buffer */
19163ee04c8SGilad Ben-Yossef 	ctx_p->user.key_dma_addr = dma_map_single(dev, (void *)ctx_p->user.key,
19263ee04c8SGilad Ben-Yossef 						  max_key_buf_size,
19363ee04c8SGilad Ben-Yossef 						  DMA_TO_DEVICE);
19463ee04c8SGilad Ben-Yossef 	if (dma_mapping_error(dev, ctx_p->user.key_dma_addr)) {
19563ee04c8SGilad Ben-Yossef 		dev_err(dev, "Mapping Key %u B at va=%pK for DMA failed\n",
19663ee04c8SGilad Ben-Yossef 			max_key_buf_size, ctx_p->user.key);
19763ee04c8SGilad Ben-Yossef 		return -ENOMEM;
19863ee04c8SGilad Ben-Yossef 	}
19963ee04c8SGilad Ben-Yossef 	dev_dbg(dev, "Mapped key %u B at va=%pK to dma=%pad\n",
20063ee04c8SGilad Ben-Yossef 		max_key_buf_size, ctx_p->user.key, &ctx_p->user.key_dma_addr);
20163ee04c8SGilad Ben-Yossef 
20263ee04c8SGilad Ben-Yossef 	if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
20363ee04c8SGilad Ben-Yossef 		/* Alloc hash tfm for essiv */
20463ee04c8SGilad Ben-Yossef 		ctx_p->shash_tfm = crypto_alloc_shash("sha256-generic", 0, 0);
20563ee04c8SGilad Ben-Yossef 		if (IS_ERR(ctx_p->shash_tfm)) {
20663ee04c8SGilad Ben-Yossef 			dev_err(dev, "Error allocating hash tfm for ESSIV.\n");
20763ee04c8SGilad Ben-Yossef 			return PTR_ERR(ctx_p->shash_tfm);
20863ee04c8SGilad Ben-Yossef 		}
20963ee04c8SGilad Ben-Yossef 	}
21063ee04c8SGilad Ben-Yossef 
21163ee04c8SGilad Ben-Yossef 	return rc;
21263ee04c8SGilad Ben-Yossef }
21363ee04c8SGilad Ben-Yossef 
21463ee04c8SGilad Ben-Yossef static void cc_cipher_exit(struct crypto_tfm *tfm)
21563ee04c8SGilad Ben-Yossef {
21663ee04c8SGilad Ben-Yossef 	struct crypto_alg *alg = tfm->__crt_alg;
21763ee04c8SGilad Ben-Yossef 	struct cc_crypto_alg *cc_alg =
21863ee04c8SGilad Ben-Yossef 			container_of(alg, struct cc_crypto_alg,
21963ee04c8SGilad Ben-Yossef 				     skcipher_alg.base);
22063ee04c8SGilad Ben-Yossef 	unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
22163ee04c8SGilad Ben-Yossef 	struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
22263ee04c8SGilad Ben-Yossef 	struct device *dev = drvdata_to_dev(ctx_p->drvdata);
22363ee04c8SGilad Ben-Yossef 
22463ee04c8SGilad Ben-Yossef 	dev_dbg(dev, "Clearing context @%p for %s\n",
22563ee04c8SGilad Ben-Yossef 		crypto_tfm_ctx(tfm), crypto_tfm_alg_name(tfm));
22663ee04c8SGilad Ben-Yossef 
22763ee04c8SGilad Ben-Yossef 	if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
22863ee04c8SGilad Ben-Yossef 		/* Free hash tfm for essiv */
22963ee04c8SGilad Ben-Yossef 		crypto_free_shash(ctx_p->shash_tfm);
23063ee04c8SGilad Ben-Yossef 		ctx_p->shash_tfm = NULL;
23163ee04c8SGilad Ben-Yossef 	}
23263ee04c8SGilad Ben-Yossef 
23363ee04c8SGilad Ben-Yossef 	/* Unmap key buffer */
23463ee04c8SGilad Ben-Yossef 	dma_unmap_single(dev, ctx_p->user.key_dma_addr, max_key_buf_size,
23563ee04c8SGilad Ben-Yossef 			 DMA_TO_DEVICE);
23663ee04c8SGilad Ben-Yossef 	dev_dbg(dev, "Unmapped key buffer key_dma_addr=%pad\n",
23763ee04c8SGilad Ben-Yossef 		&ctx_p->user.key_dma_addr);
23863ee04c8SGilad Ben-Yossef 
23963ee04c8SGilad Ben-Yossef 	/* Free key buffer in context */
24063ee04c8SGilad Ben-Yossef 	kzfree(ctx_p->user.key);
24163ee04c8SGilad Ben-Yossef 	dev_dbg(dev, "Free key buffer in context. key=@%p\n", ctx_p->user.key);
24263ee04c8SGilad Ben-Yossef }
24363ee04c8SGilad Ben-Yossef 
24463ee04c8SGilad Ben-Yossef struct tdes_keys {
24563ee04c8SGilad Ben-Yossef 	u8	key1[DES_KEY_SIZE];
24663ee04c8SGilad Ben-Yossef 	u8	key2[DES_KEY_SIZE];
24763ee04c8SGilad Ben-Yossef 	u8	key3[DES_KEY_SIZE];
24863ee04c8SGilad Ben-Yossef };
24963ee04c8SGilad Ben-Yossef 
25052f42c65SGilad Ben-Yossef static enum cc_hw_crypto_key cc_slot_to_hw_key(u8 slot_num)
25163ee04c8SGilad Ben-Yossef {
25263ee04c8SGilad Ben-Yossef 	switch (slot_num) {
25363ee04c8SGilad Ben-Yossef 	case 0:
25463ee04c8SGilad Ben-Yossef 		return KFDE0_KEY;
25563ee04c8SGilad Ben-Yossef 	case 1:
25663ee04c8SGilad Ben-Yossef 		return KFDE1_KEY;
25763ee04c8SGilad Ben-Yossef 	case 2:
25863ee04c8SGilad Ben-Yossef 		return KFDE2_KEY;
25963ee04c8SGilad Ben-Yossef 	case 3:
26063ee04c8SGilad Ben-Yossef 		return KFDE3_KEY;
26163ee04c8SGilad Ben-Yossef 	}
26263ee04c8SGilad Ben-Yossef 	return END_OF_KEYS;
26363ee04c8SGilad Ben-Yossef }
26463ee04c8SGilad Ben-Yossef 
26552f42c65SGilad Ben-Yossef static u8 cc_slot_to_cpp_key(u8 slot_num)
26652f42c65SGilad Ben-Yossef {
26752f42c65SGilad Ben-Yossef 	return (slot_num - CC_FIRST_CPP_KEY_SLOT);
26852f42c65SGilad Ben-Yossef }
26952f42c65SGilad Ben-Yossef 
27052f42c65SGilad Ben-Yossef static inline enum cc_key_type cc_slot_to_key_type(u8 slot_num)
27152f42c65SGilad Ben-Yossef {
27252f42c65SGilad Ben-Yossef 	if (slot_num >= CC_FIRST_HW_KEY_SLOT && slot_num <= CC_LAST_HW_KEY_SLOT)
27352f42c65SGilad Ben-Yossef 		return CC_HW_PROTECTED_KEY;
27452f42c65SGilad Ben-Yossef 	else if (slot_num >=  CC_FIRST_CPP_KEY_SLOT &&
27552f42c65SGilad Ben-Yossef 		 slot_num <=  CC_LAST_CPP_KEY_SLOT)
27652f42c65SGilad Ben-Yossef 		return CC_POLICY_PROTECTED_KEY;
27752f42c65SGilad Ben-Yossef 	else
27852f42c65SGilad Ben-Yossef 		return CC_INVALID_PROTECTED_KEY;
27952f42c65SGilad Ben-Yossef }
28052f42c65SGilad Ben-Yossef 
281a794d8d8SGilad Ben-Yossef static int cc_cipher_sethkey(struct crypto_skcipher *sktfm, const u8 *key,
282a794d8d8SGilad Ben-Yossef 			     unsigned int keylen)
283a794d8d8SGilad Ben-Yossef {
284a794d8d8SGilad Ben-Yossef 	struct crypto_tfm *tfm = crypto_skcipher_tfm(sktfm);
285a794d8d8SGilad Ben-Yossef 	struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
286a794d8d8SGilad Ben-Yossef 	struct device *dev = drvdata_to_dev(ctx_p->drvdata);
287a794d8d8SGilad Ben-Yossef 	struct cc_hkey_info hki;
288a794d8d8SGilad Ben-Yossef 
289a794d8d8SGilad Ben-Yossef 	dev_dbg(dev, "Setting HW key in context @%p for %s. keylen=%u\n",
290a794d8d8SGilad Ben-Yossef 		ctx_p, crypto_tfm_alg_name(tfm), keylen);
291a794d8d8SGilad Ben-Yossef 	dump_byte_array("key", (u8 *)key, keylen);
292a794d8d8SGilad Ben-Yossef 
293a794d8d8SGilad Ben-Yossef 	/* STAT_PHASE_0: Init and sanity checks */
294a794d8d8SGilad Ben-Yossef 
29552f42c65SGilad Ben-Yossef 	/* This check the size of the protected key token */
296a794d8d8SGilad Ben-Yossef 	if (keylen != sizeof(hki)) {
29752f42c65SGilad Ben-Yossef 		dev_err(dev, "Unsupported protected key size %d.\n", keylen);
298a794d8d8SGilad Ben-Yossef 		crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
299a794d8d8SGilad Ben-Yossef 		return -EINVAL;
300a794d8d8SGilad Ben-Yossef 	}
301a794d8d8SGilad Ben-Yossef 
302a794d8d8SGilad Ben-Yossef 	memcpy(&hki, key, keylen);
303a794d8d8SGilad Ben-Yossef 
304a794d8d8SGilad Ben-Yossef 	/* The real key len for crypto op is the size of the HW key
305a794d8d8SGilad Ben-Yossef 	 * referenced by the HW key slot, not the hardware key token
306a794d8d8SGilad Ben-Yossef 	 */
307a794d8d8SGilad Ben-Yossef 	keylen = hki.keylen;
308a794d8d8SGilad Ben-Yossef 
309a794d8d8SGilad Ben-Yossef 	if (validate_keys_sizes(ctx_p, keylen)) {
310a794d8d8SGilad Ben-Yossef 		dev_err(dev, "Unsupported key size %d.\n", keylen);
311a794d8d8SGilad Ben-Yossef 		crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
312a794d8d8SGilad Ben-Yossef 		return -EINVAL;
313a794d8d8SGilad Ben-Yossef 	}
314a794d8d8SGilad Ben-Yossef 
31552f42c65SGilad Ben-Yossef 	ctx_p->keylen = keylen;
31652f42c65SGilad Ben-Yossef 
31752f42c65SGilad Ben-Yossef 	switch (cc_slot_to_key_type(hki.hw_key1)) {
31852f42c65SGilad Ben-Yossef 	case CC_HW_PROTECTED_KEY:
31952f42c65SGilad Ben-Yossef 		if (ctx_p->flow_mode == S_DIN_to_SM4) {
32052f42c65SGilad Ben-Yossef 			dev_err(dev, "Only AES HW protected keys are supported\n");
32152f42c65SGilad Ben-Yossef 			return -EINVAL;
32252f42c65SGilad Ben-Yossef 		}
32352f42c65SGilad Ben-Yossef 
324a794d8d8SGilad Ben-Yossef 		ctx_p->hw.key1_slot = cc_slot_to_hw_key(hki.hw_key1);
325a794d8d8SGilad Ben-Yossef 		if (ctx_p->hw.key1_slot == END_OF_KEYS) {
32652f42c65SGilad Ben-Yossef 			dev_err(dev, "Unsupported hw key1 number (%d)\n",
32752f42c65SGilad Ben-Yossef 				hki.hw_key1);
328a794d8d8SGilad Ben-Yossef 			return -EINVAL;
329a794d8d8SGilad Ben-Yossef 		}
330a794d8d8SGilad Ben-Yossef 
331a794d8d8SGilad Ben-Yossef 		if (ctx_p->cipher_mode == DRV_CIPHER_XTS ||
332a794d8d8SGilad Ben-Yossef 		    ctx_p->cipher_mode == DRV_CIPHER_ESSIV ||
333a794d8d8SGilad Ben-Yossef 		    ctx_p->cipher_mode == DRV_CIPHER_BITLOCKER) {
334a794d8d8SGilad Ben-Yossef 			if (hki.hw_key1 == hki.hw_key2) {
335a794d8d8SGilad Ben-Yossef 				dev_err(dev, "Illegal hw key numbers (%d,%d)\n",
336a794d8d8SGilad Ben-Yossef 					hki.hw_key1, hki.hw_key2);
337a794d8d8SGilad Ben-Yossef 				return -EINVAL;
338a794d8d8SGilad Ben-Yossef 			}
33952f42c65SGilad Ben-Yossef 
340a794d8d8SGilad Ben-Yossef 			ctx_p->hw.key2_slot = cc_slot_to_hw_key(hki.hw_key2);
341a794d8d8SGilad Ben-Yossef 			if (ctx_p->hw.key2_slot == END_OF_KEYS) {
342a794d8d8SGilad Ben-Yossef 				dev_err(dev, "Unsupported hw key2 number (%d)\n",
343a794d8d8SGilad Ben-Yossef 					hki.hw_key2);
344a794d8d8SGilad Ben-Yossef 				return -EINVAL;
345a794d8d8SGilad Ben-Yossef 			}
346a794d8d8SGilad Ben-Yossef 		}
347a794d8d8SGilad Ben-Yossef 
34852f42c65SGilad Ben-Yossef 		ctx_p->key_type = CC_HW_PROTECTED_KEY;
34952f42c65SGilad Ben-Yossef 		dev_dbg(dev, "HW protected key  %d/%d set\n.",
35052f42c65SGilad Ben-Yossef 			ctx_p->hw.key1_slot, ctx_p->hw.key2_slot);
35152f42c65SGilad Ben-Yossef 		break;
35252f42c65SGilad Ben-Yossef 
35352f42c65SGilad Ben-Yossef 	case CC_POLICY_PROTECTED_KEY:
35452f42c65SGilad Ben-Yossef 		if (ctx_p->drvdata->hw_rev < CC_HW_REV_713) {
35552f42c65SGilad Ben-Yossef 			dev_err(dev, "CPP keys not supported in this hardware revision.\n");
35652f42c65SGilad Ben-Yossef 			return -EINVAL;
35752f42c65SGilad Ben-Yossef 		}
35852f42c65SGilad Ben-Yossef 
35952f42c65SGilad Ben-Yossef 		if (ctx_p->cipher_mode != DRV_CIPHER_CBC &&
36052f42c65SGilad Ben-Yossef 		    ctx_p->cipher_mode != DRV_CIPHER_CTR) {
36152f42c65SGilad Ben-Yossef 			dev_err(dev, "CPP keys only supported in CBC or CTR modes.\n");
36252f42c65SGilad Ben-Yossef 			return -EINVAL;
36352f42c65SGilad Ben-Yossef 		}
36452f42c65SGilad Ben-Yossef 
36552f42c65SGilad Ben-Yossef 		ctx_p->cpp.slot = cc_slot_to_cpp_key(hki.hw_key1);
36652f42c65SGilad Ben-Yossef 		if (ctx_p->flow_mode == S_DIN_to_AES)
36752f42c65SGilad Ben-Yossef 			ctx_p->cpp.alg = CC_CPP_AES;
36852f42c65SGilad Ben-Yossef 		else /* Must be SM4 since due to sethkey registration */
36952f42c65SGilad Ben-Yossef 			ctx_p->cpp.alg = CC_CPP_SM4;
37052f42c65SGilad Ben-Yossef 		ctx_p->key_type = CC_POLICY_PROTECTED_KEY;
37152f42c65SGilad Ben-Yossef 		dev_dbg(dev, "policy protedcted key alg: %d slot: %d.\n",
37252f42c65SGilad Ben-Yossef 			ctx_p->cpp.alg, ctx_p->cpp.slot);
37352f42c65SGilad Ben-Yossef 		break;
37452f42c65SGilad Ben-Yossef 
37552f42c65SGilad Ben-Yossef 	default:
37652f42c65SGilad Ben-Yossef 		dev_err(dev, "Unsupported protected key (%d)\n", hki.hw_key1);
37752f42c65SGilad Ben-Yossef 		return -EINVAL;
37852f42c65SGilad Ben-Yossef 	}
379a794d8d8SGilad Ben-Yossef 
380a794d8d8SGilad Ben-Yossef 	return 0;
381a794d8d8SGilad Ben-Yossef }
382a794d8d8SGilad Ben-Yossef 
38363ee04c8SGilad Ben-Yossef static int cc_cipher_setkey(struct crypto_skcipher *sktfm, const u8 *key,
38463ee04c8SGilad Ben-Yossef 			    unsigned int keylen)
38563ee04c8SGilad Ben-Yossef {
38663ee04c8SGilad Ben-Yossef 	struct crypto_tfm *tfm = crypto_skcipher_tfm(sktfm);
38763ee04c8SGilad Ben-Yossef 	struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
38863ee04c8SGilad Ben-Yossef 	struct device *dev = drvdata_to_dev(ctx_p->drvdata);
38963ee04c8SGilad Ben-Yossef 	struct cc_crypto_alg *cc_alg =
39063ee04c8SGilad Ben-Yossef 			container_of(tfm->__crt_alg, struct cc_crypto_alg,
39163ee04c8SGilad Ben-Yossef 				     skcipher_alg.base);
39263ee04c8SGilad Ben-Yossef 	unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
39363ee04c8SGilad Ben-Yossef 
39463ee04c8SGilad Ben-Yossef 	dev_dbg(dev, "Setting key in context @%p for %s. keylen=%u\n",
39563ee04c8SGilad Ben-Yossef 		ctx_p, crypto_tfm_alg_name(tfm), keylen);
39663ee04c8SGilad Ben-Yossef 	dump_byte_array("key", (u8 *)key, keylen);
39763ee04c8SGilad Ben-Yossef 
39863ee04c8SGilad Ben-Yossef 	/* STAT_PHASE_0: Init and sanity checks */
39963ee04c8SGilad Ben-Yossef 
40063ee04c8SGilad Ben-Yossef 	if (validate_keys_sizes(ctx_p, keylen)) {
40163ee04c8SGilad Ben-Yossef 		dev_err(dev, "Unsupported key size %d.\n", keylen);
40263ee04c8SGilad Ben-Yossef 		crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
40363ee04c8SGilad Ben-Yossef 		return -EINVAL;
40463ee04c8SGilad Ben-Yossef 	}
40563ee04c8SGilad Ben-Yossef 
40652f42c65SGilad Ben-Yossef 	ctx_p->key_type = CC_UNPROTECTED_KEY;
40763ee04c8SGilad Ben-Yossef 
40863ee04c8SGilad Ben-Yossef 	/*
40963ee04c8SGilad Ben-Yossef 	 * Verify DES weak keys
41063ee04c8SGilad Ben-Yossef 	 * Note that we're dropping the expanded key since the
41163ee04c8SGilad Ben-Yossef 	 * HW does the expansion on its own.
41263ee04c8SGilad Ben-Yossef 	 */
41363ee04c8SGilad Ben-Yossef 	if (ctx_p->flow_mode == S_DIN_to_DES) {
4145db46ac2SArnd Bergmann 		u32 tmp[DES3_EDE_EXPKEY_WORDS];
41563ee04c8SGilad Ben-Yossef 		if (keylen == DES3_EDE_KEY_SIZE &&
41663ee04c8SGilad Ben-Yossef 		    __des3_ede_setkey(tmp, &tfm->crt_flags, key,
41763ee04c8SGilad Ben-Yossef 				      DES3_EDE_KEY_SIZE)) {
41863ee04c8SGilad Ben-Yossef 			dev_dbg(dev, "weak 3DES key");
41963ee04c8SGilad Ben-Yossef 			return -EINVAL;
42063ee04c8SGilad Ben-Yossef 		} else if (!des_ekey(tmp, key) &&
421231baecdSEric Biggers 			   (crypto_tfm_get_flags(tfm) &
422231baecdSEric Biggers 			    CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)) {
42363ee04c8SGilad Ben-Yossef 			tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
42463ee04c8SGilad Ben-Yossef 			dev_dbg(dev, "weak DES key");
42563ee04c8SGilad Ben-Yossef 			return -EINVAL;
42663ee04c8SGilad Ben-Yossef 		}
42763ee04c8SGilad Ben-Yossef 	}
42863ee04c8SGilad Ben-Yossef 
42963ee04c8SGilad Ben-Yossef 	if (ctx_p->cipher_mode == DRV_CIPHER_XTS &&
43063ee04c8SGilad Ben-Yossef 	    xts_check_key(tfm, key, keylen)) {
43163ee04c8SGilad Ben-Yossef 		dev_dbg(dev, "weak XTS key");
43263ee04c8SGilad Ben-Yossef 		return -EINVAL;
43363ee04c8SGilad Ben-Yossef 	}
43463ee04c8SGilad Ben-Yossef 
43563ee04c8SGilad Ben-Yossef 	/* STAT_PHASE_1: Copy key to ctx */
43663ee04c8SGilad Ben-Yossef 	dma_sync_single_for_cpu(dev, ctx_p->user.key_dma_addr,
43763ee04c8SGilad Ben-Yossef 				max_key_buf_size, DMA_TO_DEVICE);
43863ee04c8SGilad Ben-Yossef 
43963ee04c8SGilad Ben-Yossef 	memcpy(ctx_p->user.key, key, keylen);
44063ee04c8SGilad Ben-Yossef 	if (keylen == 24)
44163ee04c8SGilad Ben-Yossef 		memset(ctx_p->user.key + 24, 0, CC_AES_KEY_SIZE_MAX - 24);
44263ee04c8SGilad Ben-Yossef 
44363ee04c8SGilad Ben-Yossef 	if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
44463ee04c8SGilad Ben-Yossef 		/* sha256 for key2 - use sw implementation */
44563ee04c8SGilad Ben-Yossef 		int key_len = keylen >> 1;
44663ee04c8SGilad Ben-Yossef 		int err;
44763ee04c8SGilad Ben-Yossef 
44863ee04c8SGilad Ben-Yossef 		SHASH_DESC_ON_STACK(desc, ctx_p->shash_tfm);
44963ee04c8SGilad Ben-Yossef 
45063ee04c8SGilad Ben-Yossef 		desc->tfm = ctx_p->shash_tfm;
45163ee04c8SGilad Ben-Yossef 
45263ee04c8SGilad Ben-Yossef 		err = crypto_shash_digest(desc, ctx_p->user.key, key_len,
45363ee04c8SGilad Ben-Yossef 					  ctx_p->user.key + key_len);
45463ee04c8SGilad Ben-Yossef 		if (err) {
45563ee04c8SGilad Ben-Yossef 			dev_err(dev, "Failed to hash ESSIV key.\n");
45663ee04c8SGilad Ben-Yossef 			return err;
45763ee04c8SGilad Ben-Yossef 		}
45863ee04c8SGilad Ben-Yossef 	}
45963ee04c8SGilad Ben-Yossef 	dma_sync_single_for_device(dev, ctx_p->user.key_dma_addr,
46063ee04c8SGilad Ben-Yossef 				   max_key_buf_size, DMA_TO_DEVICE);
46163ee04c8SGilad Ben-Yossef 	ctx_p->keylen = keylen;
46263ee04c8SGilad Ben-Yossef 
46363ee04c8SGilad Ben-Yossef 	dev_dbg(dev, "return safely");
46463ee04c8SGilad Ben-Yossef 	return 0;
46563ee04c8SGilad Ben-Yossef }
46663ee04c8SGilad Ben-Yossef 
4676f17e00fSGilad Ben-Yossef static int cc_out_setup_mode(struct cc_cipher_ctx *ctx_p)
4686f17e00fSGilad Ben-Yossef {
4696f17e00fSGilad Ben-Yossef 	switch (ctx_p->flow_mode) {
4706f17e00fSGilad Ben-Yossef 	case S_DIN_to_AES:
4716f17e00fSGilad Ben-Yossef 		return S_AES_to_DOUT;
4726f17e00fSGilad Ben-Yossef 	case S_DIN_to_DES:
4736f17e00fSGilad Ben-Yossef 		return S_DES_to_DOUT;
4746f17e00fSGilad Ben-Yossef 	case S_DIN_to_SM4:
4756f17e00fSGilad Ben-Yossef 		return S_SM4_to_DOUT;
4766f17e00fSGilad Ben-Yossef 	default:
4776f17e00fSGilad Ben-Yossef 		return ctx_p->flow_mode;
4786f17e00fSGilad Ben-Yossef 	}
4796f17e00fSGilad Ben-Yossef }
4806f17e00fSGilad Ben-Yossef 
4816f17e00fSGilad Ben-Yossef static void cc_setup_readiv_desc(struct crypto_tfm *tfm,
4826f17e00fSGilad Ben-Yossef 				 struct cipher_req_ctx *req_ctx,
4836f17e00fSGilad Ben-Yossef 				 unsigned int ivsize, struct cc_hw_desc desc[],
4846f17e00fSGilad Ben-Yossef 				 unsigned int *seq_size)
4856f17e00fSGilad Ben-Yossef {
4866f17e00fSGilad Ben-Yossef 	struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
4876f17e00fSGilad Ben-Yossef 	struct device *dev = drvdata_to_dev(ctx_p->drvdata);
4886f17e00fSGilad Ben-Yossef 	int cipher_mode = ctx_p->cipher_mode;
4896f17e00fSGilad Ben-Yossef 	int flow_mode = cc_out_setup_mode(ctx_p);
4906f17e00fSGilad Ben-Yossef 	int direction = req_ctx->gen_ctx.op_type;
4916f17e00fSGilad Ben-Yossef 	dma_addr_t iv_dma_addr = req_ctx->gen_ctx.iv_dma_addr;
4926f17e00fSGilad Ben-Yossef 
4936f17e00fSGilad Ben-Yossef 	if (ctx_p->key_type == CC_POLICY_PROTECTED_KEY)
4946f17e00fSGilad Ben-Yossef 		return;
4956f17e00fSGilad Ben-Yossef 
4966f17e00fSGilad Ben-Yossef 	switch (cipher_mode) {
4976f17e00fSGilad Ben-Yossef 	case DRV_CIPHER_ECB:
4986f17e00fSGilad Ben-Yossef 		break;
4996f17e00fSGilad Ben-Yossef 	case DRV_CIPHER_CBC:
5006f17e00fSGilad Ben-Yossef 	case DRV_CIPHER_CBC_CTS:
5016f17e00fSGilad Ben-Yossef 	case DRV_CIPHER_CTR:
5026f17e00fSGilad Ben-Yossef 	case DRV_CIPHER_OFB:
5036f17e00fSGilad Ben-Yossef 		/* Read next IV */
5046f17e00fSGilad Ben-Yossef 		hw_desc_init(&desc[*seq_size]);
5056f17e00fSGilad Ben-Yossef 		set_dout_dlli(&desc[*seq_size], iv_dma_addr, ivsize, NS_BIT, 1);
5066f17e00fSGilad Ben-Yossef 		set_cipher_config0(&desc[*seq_size], direction);
5076f17e00fSGilad Ben-Yossef 		set_flow_mode(&desc[*seq_size], flow_mode);
5086f17e00fSGilad Ben-Yossef 		set_cipher_mode(&desc[*seq_size], cipher_mode);
5096f17e00fSGilad Ben-Yossef 		if (cipher_mode == DRV_CIPHER_CTR ||
5106f17e00fSGilad Ben-Yossef 		    cipher_mode == DRV_CIPHER_OFB) {
5116f17e00fSGilad Ben-Yossef 			set_setup_mode(&desc[*seq_size], SETUP_WRITE_STATE1);
5126f17e00fSGilad Ben-Yossef 		} else {
5136f17e00fSGilad Ben-Yossef 			set_setup_mode(&desc[*seq_size], SETUP_WRITE_STATE0);
5146f17e00fSGilad Ben-Yossef 		}
5156f17e00fSGilad Ben-Yossef 		set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
5166f17e00fSGilad Ben-Yossef 		(*seq_size)++;
5176f17e00fSGilad Ben-Yossef 		break;
5186f17e00fSGilad Ben-Yossef 	case DRV_CIPHER_XTS:
5196f17e00fSGilad Ben-Yossef 	case DRV_CIPHER_ESSIV:
5206f17e00fSGilad Ben-Yossef 	case DRV_CIPHER_BITLOCKER:
5216f17e00fSGilad Ben-Yossef 		/*  IV */
5226f17e00fSGilad Ben-Yossef 		hw_desc_init(&desc[*seq_size]);
5236f17e00fSGilad Ben-Yossef 		set_setup_mode(&desc[*seq_size], SETUP_WRITE_STATE1);
5246f17e00fSGilad Ben-Yossef 		set_cipher_mode(&desc[*seq_size], cipher_mode);
5256f17e00fSGilad Ben-Yossef 		set_cipher_config0(&desc[*seq_size], direction);
5266f17e00fSGilad Ben-Yossef 		set_flow_mode(&desc[*seq_size], flow_mode);
5276f17e00fSGilad Ben-Yossef 		set_dout_dlli(&desc[*seq_size], iv_dma_addr, CC_AES_BLOCK_SIZE,
5286f17e00fSGilad Ben-Yossef 			     NS_BIT, 1);
5296f17e00fSGilad Ben-Yossef 		set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
5306f17e00fSGilad Ben-Yossef 		(*seq_size)++;
5316f17e00fSGilad Ben-Yossef 		break;
5326f17e00fSGilad Ben-Yossef 	default:
5336f17e00fSGilad Ben-Yossef 		dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
5346f17e00fSGilad Ben-Yossef 	}
5356f17e00fSGilad Ben-Yossef }
5366f17e00fSGilad Ben-Yossef 
537dd8486c7SGilad Ben-Yossef static void cc_setup_state_desc(struct crypto_tfm *tfm,
53863ee04c8SGilad Ben-Yossef 				 struct cipher_req_ctx *req_ctx,
53963ee04c8SGilad Ben-Yossef 				 unsigned int ivsize, unsigned int nbytes,
54063ee04c8SGilad Ben-Yossef 				 struct cc_hw_desc desc[],
54163ee04c8SGilad Ben-Yossef 				 unsigned int *seq_size)
54263ee04c8SGilad Ben-Yossef {
54363ee04c8SGilad Ben-Yossef 	struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
54463ee04c8SGilad Ben-Yossef 	struct device *dev = drvdata_to_dev(ctx_p->drvdata);
54563ee04c8SGilad Ben-Yossef 	int cipher_mode = ctx_p->cipher_mode;
54663ee04c8SGilad Ben-Yossef 	int flow_mode = ctx_p->flow_mode;
54763ee04c8SGilad Ben-Yossef 	int direction = req_ctx->gen_ctx.op_type;
54863ee04c8SGilad Ben-Yossef 	dma_addr_t key_dma_addr = ctx_p->user.key_dma_addr;
54963ee04c8SGilad Ben-Yossef 	unsigned int key_len = ctx_p->keylen;
55063ee04c8SGilad Ben-Yossef 	dma_addr_t iv_dma_addr = req_ctx->gen_ctx.iv_dma_addr;
55163ee04c8SGilad Ben-Yossef 	unsigned int du_size = nbytes;
55263ee04c8SGilad Ben-Yossef 
55363ee04c8SGilad Ben-Yossef 	struct cc_crypto_alg *cc_alg =
55463ee04c8SGilad Ben-Yossef 		container_of(tfm->__crt_alg, struct cc_crypto_alg,
55563ee04c8SGilad Ben-Yossef 			     skcipher_alg.base);
55663ee04c8SGilad Ben-Yossef 
55763ee04c8SGilad Ben-Yossef 	if (cc_alg->data_unit)
55863ee04c8SGilad Ben-Yossef 		du_size = cc_alg->data_unit;
55963ee04c8SGilad Ben-Yossef 
56063ee04c8SGilad Ben-Yossef 	switch (cipher_mode) {
561dd8486c7SGilad Ben-Yossef 	case DRV_CIPHER_ECB:
562dd8486c7SGilad Ben-Yossef 		break;
56363ee04c8SGilad Ben-Yossef 	case DRV_CIPHER_CBC:
56463ee04c8SGilad Ben-Yossef 	case DRV_CIPHER_CBC_CTS:
56563ee04c8SGilad Ben-Yossef 	case DRV_CIPHER_CTR:
56663ee04c8SGilad Ben-Yossef 	case DRV_CIPHER_OFB:
567dd8486c7SGilad Ben-Yossef 		/* Load IV */
56863ee04c8SGilad Ben-Yossef 		hw_desc_init(&desc[*seq_size]);
56963ee04c8SGilad Ben-Yossef 		set_din_type(&desc[*seq_size], DMA_DLLI, iv_dma_addr, ivsize,
57063ee04c8SGilad Ben-Yossef 			     NS_BIT);
57163ee04c8SGilad Ben-Yossef 		set_cipher_config0(&desc[*seq_size], direction);
57263ee04c8SGilad Ben-Yossef 		set_flow_mode(&desc[*seq_size], flow_mode);
57363ee04c8SGilad Ben-Yossef 		set_cipher_mode(&desc[*seq_size], cipher_mode);
57463ee04c8SGilad Ben-Yossef 		if (cipher_mode == DRV_CIPHER_CTR ||
57563ee04c8SGilad Ben-Yossef 		    cipher_mode == DRV_CIPHER_OFB) {
57663ee04c8SGilad Ben-Yossef 			set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE1);
57763ee04c8SGilad Ben-Yossef 		} else {
57863ee04c8SGilad Ben-Yossef 			set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE0);
57963ee04c8SGilad Ben-Yossef 		}
58063ee04c8SGilad Ben-Yossef 		(*seq_size)++;
581dd8486c7SGilad Ben-Yossef 		break;
582dd8486c7SGilad Ben-Yossef 	case DRV_CIPHER_XTS:
583dd8486c7SGilad Ben-Yossef 	case DRV_CIPHER_ESSIV:
584dd8486c7SGilad Ben-Yossef 	case DRV_CIPHER_BITLOCKER:
585dd8486c7SGilad Ben-Yossef 		/* load XEX key */
586dd8486c7SGilad Ben-Yossef 		hw_desc_init(&desc[*seq_size]);
587dd8486c7SGilad Ben-Yossef 		set_cipher_mode(&desc[*seq_size], cipher_mode);
588dd8486c7SGilad Ben-Yossef 		set_cipher_config0(&desc[*seq_size], direction);
58952f42c65SGilad Ben-Yossef 		if (cc_key_type(tfm) == CC_HW_PROTECTED_KEY) {
590dd8486c7SGilad Ben-Yossef 			set_hw_crypto_key(&desc[*seq_size],
591dd8486c7SGilad Ben-Yossef 					  ctx_p->hw.key2_slot);
592dd8486c7SGilad Ben-Yossef 		} else {
593dd8486c7SGilad Ben-Yossef 			set_din_type(&desc[*seq_size], DMA_DLLI,
594dd8486c7SGilad Ben-Yossef 				     (key_dma_addr + (key_len / 2)),
595dd8486c7SGilad Ben-Yossef 				     (key_len / 2), NS_BIT);
596dd8486c7SGilad Ben-Yossef 		}
597dd8486c7SGilad Ben-Yossef 		set_xex_data_unit_size(&desc[*seq_size], du_size);
598dd8486c7SGilad Ben-Yossef 		set_flow_mode(&desc[*seq_size], S_DIN_to_AES2);
599dd8486c7SGilad Ben-Yossef 		set_key_size_aes(&desc[*seq_size], (key_len / 2));
600dd8486c7SGilad Ben-Yossef 		set_setup_mode(&desc[*seq_size], SETUP_LOAD_XEX_KEY);
601dd8486c7SGilad Ben-Yossef 		(*seq_size)++;
602dd8486c7SGilad Ben-Yossef 
603dd8486c7SGilad Ben-Yossef 		/* Load IV */
604dd8486c7SGilad Ben-Yossef 		hw_desc_init(&desc[*seq_size]);
605dd8486c7SGilad Ben-Yossef 		set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE1);
606dd8486c7SGilad Ben-Yossef 		set_cipher_mode(&desc[*seq_size], cipher_mode);
607dd8486c7SGilad Ben-Yossef 		set_cipher_config0(&desc[*seq_size], direction);
608dd8486c7SGilad Ben-Yossef 		set_key_size_aes(&desc[*seq_size], (key_len / 2));
609dd8486c7SGilad Ben-Yossef 		set_flow_mode(&desc[*seq_size], flow_mode);
610dd8486c7SGilad Ben-Yossef 		set_din_type(&desc[*seq_size], DMA_DLLI, iv_dma_addr,
611dd8486c7SGilad Ben-Yossef 			     CC_AES_BLOCK_SIZE, NS_BIT);
612dd8486c7SGilad Ben-Yossef 		(*seq_size)++;
613dd8486c7SGilad Ben-Yossef 		break;
614dd8486c7SGilad Ben-Yossef 	default:
615dd8486c7SGilad Ben-Yossef 		dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
616dd8486c7SGilad Ben-Yossef 	}
617dd8486c7SGilad Ben-Yossef }
618dd8486c7SGilad Ben-Yossef 
619533edf9fSGilad Ben-Yossef static int cc_out_flow_mode(struct cc_cipher_ctx *ctx_p)
620533edf9fSGilad Ben-Yossef {
621533edf9fSGilad Ben-Yossef 	switch (ctx_p->flow_mode) {
622533edf9fSGilad Ben-Yossef 	case S_DIN_to_AES:
623533edf9fSGilad Ben-Yossef 		return DIN_AES_DOUT;
624533edf9fSGilad Ben-Yossef 	case S_DIN_to_DES:
625533edf9fSGilad Ben-Yossef 		return DIN_DES_DOUT;
626533edf9fSGilad Ben-Yossef 	case S_DIN_to_SM4:
627533edf9fSGilad Ben-Yossef 		return DIN_SM4_DOUT;
628533edf9fSGilad Ben-Yossef 	default:
629533edf9fSGilad Ben-Yossef 		return ctx_p->flow_mode;
630533edf9fSGilad Ben-Yossef 	}
631533edf9fSGilad Ben-Yossef }
632dd8486c7SGilad Ben-Yossef 
633dd8486c7SGilad Ben-Yossef static void cc_setup_key_desc(struct crypto_tfm *tfm,
634dd8486c7SGilad Ben-Yossef 			      struct cipher_req_ctx *req_ctx,
635dd8486c7SGilad Ben-Yossef 			      unsigned int nbytes, struct cc_hw_desc desc[],
636dd8486c7SGilad Ben-Yossef 			      unsigned int *seq_size)
637dd8486c7SGilad Ben-Yossef {
638dd8486c7SGilad Ben-Yossef 	struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
639dd8486c7SGilad Ben-Yossef 	struct device *dev = drvdata_to_dev(ctx_p->drvdata);
640dd8486c7SGilad Ben-Yossef 	int cipher_mode = ctx_p->cipher_mode;
641dd8486c7SGilad Ben-Yossef 	int flow_mode = ctx_p->flow_mode;
642dd8486c7SGilad Ben-Yossef 	int direction = req_ctx->gen_ctx.op_type;
643dd8486c7SGilad Ben-Yossef 	dma_addr_t key_dma_addr = ctx_p->user.key_dma_addr;
644dd8486c7SGilad Ben-Yossef 	unsigned int key_len = ctx_p->keylen;
645dd8486c7SGilad Ben-Yossef 	unsigned int du_size = nbytes;
64652f42c65SGilad Ben-Yossef 	unsigned int din_size;
647dd8486c7SGilad Ben-Yossef 
648dd8486c7SGilad Ben-Yossef 	struct cc_crypto_alg *cc_alg =
649dd8486c7SGilad Ben-Yossef 		container_of(tfm->__crt_alg, struct cc_crypto_alg,
650dd8486c7SGilad Ben-Yossef 			     skcipher_alg.base);
651dd8486c7SGilad Ben-Yossef 
652dd8486c7SGilad Ben-Yossef 	if (cc_alg->data_unit)
653dd8486c7SGilad Ben-Yossef 		du_size = cc_alg->data_unit;
654dd8486c7SGilad Ben-Yossef 
655dd8486c7SGilad Ben-Yossef 	switch (cipher_mode) {
656dd8486c7SGilad Ben-Yossef 	case DRV_CIPHER_CBC:
657dd8486c7SGilad Ben-Yossef 	case DRV_CIPHER_CBC_CTS:
658dd8486c7SGilad Ben-Yossef 	case DRV_CIPHER_CTR:
659dd8486c7SGilad Ben-Yossef 	case DRV_CIPHER_OFB:
66063ee04c8SGilad Ben-Yossef 	case DRV_CIPHER_ECB:
66163ee04c8SGilad Ben-Yossef 		/* Load key */
66263ee04c8SGilad Ben-Yossef 		hw_desc_init(&desc[*seq_size]);
66363ee04c8SGilad Ben-Yossef 		set_cipher_mode(&desc[*seq_size], cipher_mode);
66463ee04c8SGilad Ben-Yossef 		set_cipher_config0(&desc[*seq_size], direction);
665533edf9fSGilad Ben-Yossef 
666533edf9fSGilad Ben-Yossef 		if (cc_key_type(tfm) == CC_POLICY_PROTECTED_KEY) {
667533edf9fSGilad Ben-Yossef 			/* We use the AES key size coding for all CPP algs */
668533edf9fSGilad Ben-Yossef 			set_key_size_aes(&desc[*seq_size], key_len);
669533edf9fSGilad Ben-Yossef 			set_cpp_crypto_key(&desc[*seq_size], ctx_p->cpp.slot);
670533edf9fSGilad Ben-Yossef 			flow_mode = cc_out_flow_mode(ctx_p);
671533edf9fSGilad Ben-Yossef 		} else {
67263ee04c8SGilad Ben-Yossef 			if (flow_mode == S_DIN_to_AES) {
67352f42c65SGilad Ben-Yossef 				if (cc_key_type(tfm) == CC_HW_PROTECTED_KEY) {
67463ee04c8SGilad Ben-Yossef 					set_hw_crypto_key(&desc[*seq_size],
67563ee04c8SGilad Ben-Yossef 							  ctx_p->hw.key1_slot);
67663ee04c8SGilad Ben-Yossef 				} else {
67752f42c65SGilad Ben-Yossef 					/* CC_POLICY_UNPROTECTED_KEY
67852f42c65SGilad Ben-Yossef 					 * Invalid keys are filtered out in
67952f42c65SGilad Ben-Yossef 					 * sethkey()
68052f42c65SGilad Ben-Yossef 					 */
68152f42c65SGilad Ben-Yossef 					din_size = (key_len == 24) ?
68252f42c65SGilad Ben-Yossef 						AES_MAX_KEY_SIZE : key_len;
68352f42c65SGilad Ben-Yossef 
68463ee04c8SGilad Ben-Yossef 					set_din_type(&desc[*seq_size], DMA_DLLI,
68552f42c65SGilad Ben-Yossef 						     key_dma_addr, din_size,
68652f42c65SGilad Ben-Yossef 						     NS_BIT);
68763ee04c8SGilad Ben-Yossef 				}
68863ee04c8SGilad Ben-Yossef 				set_key_size_aes(&desc[*seq_size], key_len);
68963ee04c8SGilad Ben-Yossef 			} else {
69063ee04c8SGilad Ben-Yossef 				/*des*/
69152f42c65SGilad Ben-Yossef 				set_din_type(&desc[*seq_size], DMA_DLLI,
69252f42c65SGilad Ben-Yossef 					     key_dma_addr, key_len, NS_BIT);
69363ee04c8SGilad Ben-Yossef 				set_key_size_des(&desc[*seq_size], key_len);
69463ee04c8SGilad Ben-Yossef 			}
69563ee04c8SGilad Ben-Yossef 			set_setup_mode(&desc[*seq_size], SETUP_LOAD_KEY0);
69652f42c65SGilad Ben-Yossef 		}
697533edf9fSGilad Ben-Yossef 		set_flow_mode(&desc[*seq_size], flow_mode);
69863ee04c8SGilad Ben-Yossef 		(*seq_size)++;
69963ee04c8SGilad Ben-Yossef 		break;
70063ee04c8SGilad Ben-Yossef 	case DRV_CIPHER_XTS:
70163ee04c8SGilad Ben-Yossef 	case DRV_CIPHER_ESSIV:
70263ee04c8SGilad Ben-Yossef 	case DRV_CIPHER_BITLOCKER:
70363ee04c8SGilad Ben-Yossef 		/* Load AES key */
70463ee04c8SGilad Ben-Yossef 		hw_desc_init(&desc[*seq_size]);
70563ee04c8SGilad Ben-Yossef 		set_cipher_mode(&desc[*seq_size], cipher_mode);
70663ee04c8SGilad Ben-Yossef 		set_cipher_config0(&desc[*seq_size], direction);
70752f42c65SGilad Ben-Yossef 		if (cc_key_type(tfm) == CC_HW_PROTECTED_KEY) {
70863ee04c8SGilad Ben-Yossef 			set_hw_crypto_key(&desc[*seq_size],
70963ee04c8SGilad Ben-Yossef 					  ctx_p->hw.key1_slot);
71063ee04c8SGilad Ben-Yossef 		} else {
71163ee04c8SGilad Ben-Yossef 			set_din_type(&desc[*seq_size], DMA_DLLI, key_dma_addr,
71263ee04c8SGilad Ben-Yossef 				     (key_len / 2), NS_BIT);
71363ee04c8SGilad Ben-Yossef 		}
71463ee04c8SGilad Ben-Yossef 		set_key_size_aes(&desc[*seq_size], (key_len / 2));
71563ee04c8SGilad Ben-Yossef 		set_flow_mode(&desc[*seq_size], flow_mode);
71663ee04c8SGilad Ben-Yossef 		set_setup_mode(&desc[*seq_size], SETUP_LOAD_KEY0);
71763ee04c8SGilad Ben-Yossef 		(*seq_size)++;
71863ee04c8SGilad Ben-Yossef 		break;
71963ee04c8SGilad Ben-Yossef 	default:
72063ee04c8SGilad Ben-Yossef 		dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
72163ee04c8SGilad Ben-Yossef 	}
72263ee04c8SGilad Ben-Yossef }
72363ee04c8SGilad Ben-Yossef 
7244b1d7debSGilad Ben-Yossef static void cc_setup_mlli_desc(struct crypto_tfm *tfm,
72563ee04c8SGilad Ben-Yossef 			       struct cipher_req_ctx *req_ctx,
7264b1d7debSGilad Ben-Yossef 			       struct scatterlist *dst, struct scatterlist *src,
7274b1d7debSGilad Ben-Yossef 			       unsigned int nbytes, void *areq,
7284b1d7debSGilad Ben-Yossef 			       struct cc_hw_desc desc[], unsigned int *seq_size)
7294b1d7debSGilad Ben-Yossef {
7304b1d7debSGilad Ben-Yossef 	struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
7314b1d7debSGilad Ben-Yossef 	struct device *dev = drvdata_to_dev(ctx_p->drvdata);
7324b1d7debSGilad Ben-Yossef 
7334b1d7debSGilad Ben-Yossef 	if (req_ctx->dma_buf_type == CC_DMA_BUF_MLLI) {
7344b1d7debSGilad Ben-Yossef 		/* bypass */
7354b1d7debSGilad Ben-Yossef 		dev_dbg(dev, " bypass params addr %pad length 0x%X addr 0x%08X\n",
7364b1d7debSGilad Ben-Yossef 			&req_ctx->mlli_params.mlli_dma_addr,
7374b1d7debSGilad Ben-Yossef 			req_ctx->mlli_params.mlli_len,
7384b1d7debSGilad Ben-Yossef 			(unsigned int)ctx_p->drvdata->mlli_sram_addr);
7394b1d7debSGilad Ben-Yossef 		hw_desc_init(&desc[*seq_size]);
7404b1d7debSGilad Ben-Yossef 		set_din_type(&desc[*seq_size], DMA_DLLI,
7414b1d7debSGilad Ben-Yossef 			     req_ctx->mlli_params.mlli_dma_addr,
7424b1d7debSGilad Ben-Yossef 			     req_ctx->mlli_params.mlli_len, NS_BIT);
7434b1d7debSGilad Ben-Yossef 		set_dout_sram(&desc[*seq_size],
7444b1d7debSGilad Ben-Yossef 			      ctx_p->drvdata->mlli_sram_addr,
7454b1d7debSGilad Ben-Yossef 			      req_ctx->mlli_params.mlli_len);
7464b1d7debSGilad Ben-Yossef 		set_flow_mode(&desc[*seq_size], BYPASS);
7474b1d7debSGilad Ben-Yossef 		(*seq_size)++;
7484b1d7debSGilad Ben-Yossef 	}
7494b1d7debSGilad Ben-Yossef }
7504b1d7debSGilad Ben-Yossef 
7514b1d7debSGilad Ben-Yossef static void cc_setup_flow_desc(struct crypto_tfm *tfm,
7524b1d7debSGilad Ben-Yossef 			       struct cipher_req_ctx *req_ctx,
7534b1d7debSGilad Ben-Yossef 			       struct scatterlist *dst, struct scatterlist *src,
7546f17e00fSGilad Ben-Yossef 			       unsigned int nbytes, struct cc_hw_desc desc[],
7556f17e00fSGilad Ben-Yossef 			       unsigned int *seq_size)
75663ee04c8SGilad Ben-Yossef {
75763ee04c8SGilad Ben-Yossef 	struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
75863ee04c8SGilad Ben-Yossef 	struct device *dev = drvdata_to_dev(ctx_p->drvdata);
759533edf9fSGilad Ben-Yossef 	unsigned int flow_mode = cc_out_flow_mode(ctx_p);
7606f17e00fSGilad Ben-Yossef 	bool last_desc = (ctx_p->key_type == CC_POLICY_PROTECTED_KEY ||
7616f17e00fSGilad Ben-Yossef 			  ctx_p->cipher_mode == DRV_CIPHER_ECB);
76263ee04c8SGilad Ben-Yossef 
76363ee04c8SGilad Ben-Yossef 	/* Process */
76463ee04c8SGilad Ben-Yossef 	if (req_ctx->dma_buf_type == CC_DMA_BUF_DLLI) {
76563ee04c8SGilad Ben-Yossef 		dev_dbg(dev, " data params addr %pad length 0x%X\n",
76663ee04c8SGilad Ben-Yossef 			&sg_dma_address(src), nbytes);
76763ee04c8SGilad Ben-Yossef 		dev_dbg(dev, " data params addr %pad length 0x%X\n",
76863ee04c8SGilad Ben-Yossef 			&sg_dma_address(dst), nbytes);
76963ee04c8SGilad Ben-Yossef 		hw_desc_init(&desc[*seq_size]);
77063ee04c8SGilad Ben-Yossef 		set_din_type(&desc[*seq_size], DMA_DLLI, sg_dma_address(src),
77163ee04c8SGilad Ben-Yossef 			     nbytes, NS_BIT);
77263ee04c8SGilad Ben-Yossef 		set_dout_dlli(&desc[*seq_size], sg_dma_address(dst),
7736f17e00fSGilad Ben-Yossef 			      nbytes, NS_BIT, (!last_desc ? 0 : 1));
7746f17e00fSGilad Ben-Yossef 		if (last_desc)
77527b3b22dSGilad Ben-Yossef 			set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
77663ee04c8SGilad Ben-Yossef 
77763ee04c8SGilad Ben-Yossef 		set_flow_mode(&desc[*seq_size], flow_mode);
77863ee04c8SGilad Ben-Yossef 		(*seq_size)++;
77963ee04c8SGilad Ben-Yossef 	} else {
78063ee04c8SGilad Ben-Yossef 		hw_desc_init(&desc[*seq_size]);
78163ee04c8SGilad Ben-Yossef 		set_din_type(&desc[*seq_size], DMA_MLLI,
78263ee04c8SGilad Ben-Yossef 			     ctx_p->drvdata->mlli_sram_addr,
78363ee04c8SGilad Ben-Yossef 			     req_ctx->in_mlli_nents, NS_BIT);
78463ee04c8SGilad Ben-Yossef 		if (req_ctx->out_nents == 0) {
78563ee04c8SGilad Ben-Yossef 			dev_dbg(dev, " din/dout params addr 0x%08X addr 0x%08X\n",
78663ee04c8SGilad Ben-Yossef 				(unsigned int)ctx_p->drvdata->mlli_sram_addr,
78763ee04c8SGilad Ben-Yossef 				(unsigned int)ctx_p->drvdata->mlli_sram_addr);
78863ee04c8SGilad Ben-Yossef 			set_dout_mlli(&desc[*seq_size],
78963ee04c8SGilad Ben-Yossef 				      ctx_p->drvdata->mlli_sram_addr,
79063ee04c8SGilad Ben-Yossef 				      req_ctx->in_mlli_nents, NS_BIT,
7916f17e00fSGilad Ben-Yossef 				      (!last_desc ? 0 : 1));
79263ee04c8SGilad Ben-Yossef 		} else {
79363ee04c8SGilad Ben-Yossef 			dev_dbg(dev, " din/dout params addr 0x%08X addr 0x%08X\n",
79463ee04c8SGilad Ben-Yossef 				(unsigned int)ctx_p->drvdata->mlli_sram_addr,
79563ee04c8SGilad Ben-Yossef 				(unsigned int)ctx_p->drvdata->mlli_sram_addr +
79663ee04c8SGilad Ben-Yossef 				(u32)LLI_ENTRY_BYTE_SIZE * req_ctx->in_nents);
79763ee04c8SGilad Ben-Yossef 			set_dout_mlli(&desc[*seq_size],
79863ee04c8SGilad Ben-Yossef 				      (ctx_p->drvdata->mlli_sram_addr +
79963ee04c8SGilad Ben-Yossef 				       (LLI_ENTRY_BYTE_SIZE *
80063ee04c8SGilad Ben-Yossef 					req_ctx->in_mlli_nents)),
80163ee04c8SGilad Ben-Yossef 				      req_ctx->out_mlli_nents, NS_BIT,
8026f17e00fSGilad Ben-Yossef 				      (!last_desc ? 0 : 1));
80363ee04c8SGilad Ben-Yossef 		}
8046f17e00fSGilad Ben-Yossef 		if (last_desc)
80527b3b22dSGilad Ben-Yossef 			set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
80663ee04c8SGilad Ben-Yossef 
80763ee04c8SGilad Ben-Yossef 		set_flow_mode(&desc[*seq_size], flow_mode);
80863ee04c8SGilad Ben-Yossef 		(*seq_size)++;
80963ee04c8SGilad Ben-Yossef 	}
81063ee04c8SGilad Ben-Yossef }
81163ee04c8SGilad Ben-Yossef 
81263ee04c8SGilad Ben-Yossef static void cc_cipher_complete(struct device *dev, void *cc_req, int err)
81363ee04c8SGilad Ben-Yossef {
81463ee04c8SGilad Ben-Yossef 	struct skcipher_request *req = (struct skcipher_request *)cc_req;
81563ee04c8SGilad Ben-Yossef 	struct scatterlist *dst = req->dst;
81663ee04c8SGilad Ben-Yossef 	struct scatterlist *src = req->src;
81763ee04c8SGilad Ben-Yossef 	struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
81800904aa0SGilad Ben-Yossef 	struct crypto_skcipher *sk_tfm = crypto_skcipher_reqtfm(req);
81900904aa0SGilad Ben-Yossef 	unsigned int ivsize = crypto_skcipher_ivsize(sk_tfm);
82063ee04c8SGilad Ben-Yossef 
821a108f931SGilad Ben-Yossef 	if (err != -EINPROGRESS) {
822a108f931SGilad Ben-Yossef 		/* Not a BACKLOG notification */
823c139c72eSGilad Ben-Yossef 		cc_unmap_cipher_request(dev, req_ctx, ivsize, src, dst);
8246f17e00fSGilad Ben-Yossef 		memcpy(req->iv, req_ctx->iv, ivsize);
82500904aa0SGilad Ben-Yossef 		kzfree(req_ctx->iv);
826a108f931SGilad Ben-Yossef 	}
827a108f931SGilad Ben-Yossef 
82863ee04c8SGilad Ben-Yossef 	skcipher_request_complete(req, err);
82963ee04c8SGilad Ben-Yossef }
83063ee04c8SGilad Ben-Yossef 
83163ee04c8SGilad Ben-Yossef static int cc_cipher_process(struct skcipher_request *req,
83263ee04c8SGilad Ben-Yossef 			     enum drv_crypto_direction direction)
83363ee04c8SGilad Ben-Yossef {
83463ee04c8SGilad Ben-Yossef 	struct crypto_skcipher *sk_tfm = crypto_skcipher_reqtfm(req);
83563ee04c8SGilad Ben-Yossef 	struct crypto_tfm *tfm = crypto_skcipher_tfm(sk_tfm);
83663ee04c8SGilad Ben-Yossef 	struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
83763ee04c8SGilad Ben-Yossef 	unsigned int ivsize = crypto_skcipher_ivsize(sk_tfm);
83863ee04c8SGilad Ben-Yossef 	struct scatterlist *dst = req->dst;
83963ee04c8SGilad Ben-Yossef 	struct scatterlist *src = req->src;
84063ee04c8SGilad Ben-Yossef 	unsigned int nbytes = req->cryptlen;
84163ee04c8SGilad Ben-Yossef 	void *iv = req->iv;
84263ee04c8SGilad Ben-Yossef 	struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
84363ee04c8SGilad Ben-Yossef 	struct device *dev = drvdata_to_dev(ctx_p->drvdata);
84463ee04c8SGilad Ben-Yossef 	struct cc_hw_desc desc[MAX_ABLKCIPHER_SEQ_LEN];
84563ee04c8SGilad Ben-Yossef 	struct cc_crypto_req cc_req = {};
84684f366daSGilad Ben-Yossef 	int rc;
84763ee04c8SGilad Ben-Yossef 	unsigned int seq_len = 0;
84863ee04c8SGilad Ben-Yossef 	gfp_t flags = cc_gfp_flags(&req->base);
84963ee04c8SGilad Ben-Yossef 
85063ee04c8SGilad Ben-Yossef 	dev_dbg(dev, "%s req=%p iv=%p nbytes=%d\n",
85163ee04c8SGilad Ben-Yossef 		((direction == DRV_CRYPTO_DIRECTION_ENCRYPT) ?
85263ee04c8SGilad Ben-Yossef 		"Encrypt" : "Decrypt"), req, iv, nbytes);
85363ee04c8SGilad Ben-Yossef 
85463ee04c8SGilad Ben-Yossef 	/* STAT_PHASE_0: Init and sanity checks */
85563ee04c8SGilad Ben-Yossef 
85663ee04c8SGilad Ben-Yossef 	/* TODO: check data length according to mode */
85763ee04c8SGilad Ben-Yossef 	if (validate_data_size(ctx_p, nbytes)) {
85863ee04c8SGilad Ben-Yossef 		dev_err(dev, "Unsupported data size %d.\n", nbytes);
85963ee04c8SGilad Ben-Yossef 		crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_BLOCK_LEN);
86063ee04c8SGilad Ben-Yossef 		rc = -EINVAL;
86163ee04c8SGilad Ben-Yossef 		goto exit_process;
86263ee04c8SGilad Ben-Yossef 	}
86363ee04c8SGilad Ben-Yossef 	if (nbytes == 0) {
86463ee04c8SGilad Ben-Yossef 		/* No data to process is valid */
86563ee04c8SGilad Ben-Yossef 		rc = 0;
86663ee04c8SGilad Ben-Yossef 		goto exit_process;
86763ee04c8SGilad Ben-Yossef 	}
86863ee04c8SGilad Ben-Yossef 
86963ee04c8SGilad Ben-Yossef 	/* The IV we are handed may be allocted from the stack so
87063ee04c8SGilad Ben-Yossef 	 * we must copy it to a DMAable buffer before use.
87163ee04c8SGilad Ben-Yossef 	 */
87201745706SFengguang Wu 	req_ctx->iv = kmemdup(iv, ivsize, flags);
87363ee04c8SGilad Ben-Yossef 	if (!req_ctx->iv) {
87463ee04c8SGilad Ben-Yossef 		rc = -ENOMEM;
87563ee04c8SGilad Ben-Yossef 		goto exit_process;
87663ee04c8SGilad Ben-Yossef 	}
87763ee04c8SGilad Ben-Yossef 
87863ee04c8SGilad Ben-Yossef 	/* Setup request structure */
87963ee04c8SGilad Ben-Yossef 	cc_req.user_cb = (void *)cc_cipher_complete;
88063ee04c8SGilad Ben-Yossef 	cc_req.user_arg = (void *)req;
88163ee04c8SGilad Ben-Yossef 
88252f42c65SGilad Ben-Yossef 	/* Setup CPP operation details */
88352f42c65SGilad Ben-Yossef 	if (ctx_p->key_type == CC_POLICY_PROTECTED_KEY) {
88452f42c65SGilad Ben-Yossef 		cc_req.cpp.is_cpp = true;
88552f42c65SGilad Ben-Yossef 		cc_req.cpp.alg = ctx_p->cpp.alg;
88652f42c65SGilad Ben-Yossef 		cc_req.cpp.slot = ctx_p->cpp.slot;
88752f42c65SGilad Ben-Yossef 	}
88852f42c65SGilad Ben-Yossef 
88963ee04c8SGilad Ben-Yossef 	/* Setup request context */
89063ee04c8SGilad Ben-Yossef 	req_ctx->gen_ctx.op_type = direction;
89163ee04c8SGilad Ben-Yossef 
89263ee04c8SGilad Ben-Yossef 	/* STAT_PHASE_1: Map buffers */
89363ee04c8SGilad Ben-Yossef 
89463ee04c8SGilad Ben-Yossef 	rc = cc_map_cipher_request(ctx_p->drvdata, req_ctx, ivsize, nbytes,
89563ee04c8SGilad Ben-Yossef 				      req_ctx->iv, src, dst, flags);
89663ee04c8SGilad Ben-Yossef 	if (rc) {
89763ee04c8SGilad Ben-Yossef 		dev_err(dev, "map_request() failed\n");
89863ee04c8SGilad Ben-Yossef 		goto exit_process;
89963ee04c8SGilad Ben-Yossef 	}
90063ee04c8SGilad Ben-Yossef 
90163ee04c8SGilad Ben-Yossef 	/* STAT_PHASE_2: Create sequence */
90263ee04c8SGilad Ben-Yossef 
903dd8486c7SGilad Ben-Yossef 	/* Setup IV and XEX key used */
904dd8486c7SGilad Ben-Yossef 	cc_setup_state_desc(tfm, req_ctx, ivsize, nbytes, desc, &seq_len);
9054b1d7debSGilad Ben-Yossef 	/* Setup MLLI line, if needed */
9064b1d7debSGilad Ben-Yossef 	cc_setup_mlli_desc(tfm, req_ctx, dst, src, nbytes, req, desc, &seq_len);
907dd8486c7SGilad Ben-Yossef 	/* Setup key */
908dd8486c7SGilad Ben-Yossef 	cc_setup_key_desc(tfm, req_ctx, nbytes, desc, &seq_len);
90963ee04c8SGilad Ben-Yossef 	/* Data processing */
9106f17e00fSGilad Ben-Yossef 	cc_setup_flow_desc(tfm, req_ctx, dst, src, nbytes, desc, &seq_len);
9116f17e00fSGilad Ben-Yossef 	/* Read next IV */
9126f17e00fSGilad Ben-Yossef 	cc_setup_readiv_desc(tfm, req_ctx, ivsize, desc, &seq_len);
91363ee04c8SGilad Ben-Yossef 
91463ee04c8SGilad Ben-Yossef 	/* STAT_PHASE_3: Lock HW and push sequence */
91563ee04c8SGilad Ben-Yossef 
91663ee04c8SGilad Ben-Yossef 	rc = cc_send_request(ctx_p->drvdata, &cc_req, desc, seq_len,
91763ee04c8SGilad Ben-Yossef 			     &req->base);
91863ee04c8SGilad Ben-Yossef 	if (rc != -EINPROGRESS && rc != -EBUSY) {
91963ee04c8SGilad Ben-Yossef 		/* Failed to send the request or request completed
92063ee04c8SGilad Ben-Yossef 		 * synchronously
92163ee04c8SGilad Ben-Yossef 		 */
92263ee04c8SGilad Ben-Yossef 		cc_unmap_cipher_request(dev, req_ctx, ivsize, src, dst);
92363ee04c8SGilad Ben-Yossef 	}
92463ee04c8SGilad Ben-Yossef 
92563ee04c8SGilad Ben-Yossef exit_process:
92663ee04c8SGilad Ben-Yossef 	if (rc != -EINPROGRESS && rc != -EBUSY) {
92763ee04c8SGilad Ben-Yossef 		kzfree(req_ctx->iv);
92863ee04c8SGilad Ben-Yossef 	}
92963ee04c8SGilad Ben-Yossef 
93063ee04c8SGilad Ben-Yossef 	return rc;
93163ee04c8SGilad Ben-Yossef }
93263ee04c8SGilad Ben-Yossef 
93363ee04c8SGilad Ben-Yossef static int cc_cipher_encrypt(struct skcipher_request *req)
93463ee04c8SGilad Ben-Yossef {
93563ee04c8SGilad Ben-Yossef 	struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
93663ee04c8SGilad Ben-Yossef 
937e30368f3SGilad Ben-Yossef 	memset(req_ctx, 0, sizeof(*req_ctx));
93863ee04c8SGilad Ben-Yossef 
93963ee04c8SGilad Ben-Yossef 	return cc_cipher_process(req, DRV_CRYPTO_DIRECTION_ENCRYPT);
94063ee04c8SGilad Ben-Yossef }
94163ee04c8SGilad Ben-Yossef 
94263ee04c8SGilad Ben-Yossef static int cc_cipher_decrypt(struct skcipher_request *req)
94363ee04c8SGilad Ben-Yossef {
94463ee04c8SGilad Ben-Yossef 	struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
94563ee04c8SGilad Ben-Yossef 
946e30368f3SGilad Ben-Yossef 	memset(req_ctx, 0, sizeof(*req_ctx));
947e30368f3SGilad Ben-Yossef 
94863ee04c8SGilad Ben-Yossef 	return cc_cipher_process(req, DRV_CRYPTO_DIRECTION_DECRYPT);
94963ee04c8SGilad Ben-Yossef }
95063ee04c8SGilad Ben-Yossef 
95163ee04c8SGilad Ben-Yossef /* Block cipher alg */
95263ee04c8SGilad Ben-Yossef static const struct cc_alg_template skcipher_algs[] = {
95363ee04c8SGilad Ben-Yossef 	{
954a794d8d8SGilad Ben-Yossef 		.name = "xts(paes)",
955a794d8d8SGilad Ben-Yossef 		.driver_name = "xts-paes-ccree",
956a794d8d8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
957a794d8d8SGilad Ben-Yossef 		.template_skcipher = {
958a794d8d8SGilad Ben-Yossef 			.setkey = cc_cipher_sethkey,
959a794d8d8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
960a794d8d8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
961a794d8d8SGilad Ben-Yossef 			.min_keysize = CC_HW_KEY_SIZE,
962a794d8d8SGilad Ben-Yossef 			.max_keysize = CC_HW_KEY_SIZE,
963a794d8d8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
964a794d8d8SGilad Ben-Yossef 			},
965a794d8d8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_XTS,
966a794d8d8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
967a794d8d8SGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_630,
9681c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
969f98f6e21SGilad Ben-Yossef 		.sec_func = true,
970a794d8d8SGilad Ben-Yossef 	},
971a794d8d8SGilad Ben-Yossef 	{
972a794d8d8SGilad Ben-Yossef 		.name = "xts512(paes)",
973a794d8d8SGilad Ben-Yossef 		.driver_name = "xts-paes-du512-ccree",
974a794d8d8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
975a794d8d8SGilad Ben-Yossef 		.template_skcipher = {
976a794d8d8SGilad Ben-Yossef 			.setkey = cc_cipher_sethkey,
977a794d8d8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
978a794d8d8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
979a794d8d8SGilad Ben-Yossef 			.min_keysize = CC_HW_KEY_SIZE,
980a794d8d8SGilad Ben-Yossef 			.max_keysize = CC_HW_KEY_SIZE,
981a794d8d8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
982a794d8d8SGilad Ben-Yossef 			},
983a794d8d8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_XTS,
984a794d8d8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
985a794d8d8SGilad Ben-Yossef 		.data_unit = 512,
986a794d8d8SGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
9871c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
988f98f6e21SGilad Ben-Yossef 		.sec_func = true,
989a794d8d8SGilad Ben-Yossef 	},
990a794d8d8SGilad Ben-Yossef 	{
991a794d8d8SGilad Ben-Yossef 		.name = "xts4096(paes)",
992a794d8d8SGilad Ben-Yossef 		.driver_name = "xts-paes-du4096-ccree",
993a794d8d8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
994a794d8d8SGilad Ben-Yossef 		.template_skcipher = {
995a794d8d8SGilad Ben-Yossef 			.setkey = cc_cipher_sethkey,
996a794d8d8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
997a794d8d8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
998a794d8d8SGilad Ben-Yossef 			.min_keysize = CC_HW_KEY_SIZE,
999a794d8d8SGilad Ben-Yossef 			.max_keysize = CC_HW_KEY_SIZE,
1000a794d8d8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
1001a794d8d8SGilad Ben-Yossef 			},
1002a794d8d8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_XTS,
1003a794d8d8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
1004a794d8d8SGilad Ben-Yossef 		.data_unit = 4096,
1005a794d8d8SGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
10061c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
1007f98f6e21SGilad Ben-Yossef 		.sec_func = true,
1008a794d8d8SGilad Ben-Yossef 	},
1009a794d8d8SGilad Ben-Yossef 	{
1010a794d8d8SGilad Ben-Yossef 		.name = "essiv(paes)",
1011a794d8d8SGilad Ben-Yossef 		.driver_name = "essiv-paes-ccree",
1012a794d8d8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
1013a794d8d8SGilad Ben-Yossef 		.template_skcipher = {
1014a794d8d8SGilad Ben-Yossef 			.setkey = cc_cipher_sethkey,
1015a794d8d8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
1016a794d8d8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
1017a794d8d8SGilad Ben-Yossef 			.min_keysize = CC_HW_KEY_SIZE,
1018a794d8d8SGilad Ben-Yossef 			.max_keysize = CC_HW_KEY_SIZE,
1019a794d8d8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
1020a794d8d8SGilad Ben-Yossef 			},
1021a794d8d8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_ESSIV,
1022a794d8d8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
1023a794d8d8SGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
10241c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
1025f98f6e21SGilad Ben-Yossef 		.sec_func = true,
1026a794d8d8SGilad Ben-Yossef 	},
1027a794d8d8SGilad Ben-Yossef 	{
1028a794d8d8SGilad Ben-Yossef 		.name = "essiv512(paes)",
1029a794d8d8SGilad Ben-Yossef 		.driver_name = "essiv-paes-du512-ccree",
1030a794d8d8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
1031a794d8d8SGilad Ben-Yossef 		.template_skcipher = {
1032a794d8d8SGilad Ben-Yossef 			.setkey = cc_cipher_sethkey,
1033a794d8d8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
1034a794d8d8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
1035a794d8d8SGilad Ben-Yossef 			.min_keysize = CC_HW_KEY_SIZE,
1036a794d8d8SGilad Ben-Yossef 			.max_keysize = CC_HW_KEY_SIZE,
1037a794d8d8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
1038a794d8d8SGilad Ben-Yossef 			},
1039a794d8d8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_ESSIV,
1040a794d8d8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
1041a794d8d8SGilad Ben-Yossef 		.data_unit = 512,
1042a794d8d8SGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
10431c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
1044f98f6e21SGilad Ben-Yossef 		.sec_func = true,
1045a794d8d8SGilad Ben-Yossef 	},
1046a794d8d8SGilad Ben-Yossef 	{
1047a794d8d8SGilad Ben-Yossef 		.name = "essiv4096(paes)",
1048a794d8d8SGilad Ben-Yossef 		.driver_name = "essiv-paes-du4096-ccree",
1049a794d8d8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
1050a794d8d8SGilad Ben-Yossef 		.template_skcipher = {
1051a794d8d8SGilad Ben-Yossef 			.setkey = cc_cipher_sethkey,
1052a794d8d8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
1053a794d8d8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
1054a794d8d8SGilad Ben-Yossef 			.min_keysize = CC_HW_KEY_SIZE,
1055a794d8d8SGilad Ben-Yossef 			.max_keysize = CC_HW_KEY_SIZE,
1056a794d8d8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
1057a794d8d8SGilad Ben-Yossef 			},
1058a794d8d8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_ESSIV,
1059a794d8d8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
1060a794d8d8SGilad Ben-Yossef 		.data_unit = 4096,
1061a794d8d8SGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
10621c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
1063f98f6e21SGilad Ben-Yossef 		.sec_func = true,
1064a794d8d8SGilad Ben-Yossef 	},
1065a794d8d8SGilad Ben-Yossef 	{
1066a794d8d8SGilad Ben-Yossef 		.name = "bitlocker(paes)",
1067a794d8d8SGilad Ben-Yossef 		.driver_name = "bitlocker-paes-ccree",
1068a794d8d8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
1069a794d8d8SGilad Ben-Yossef 		.template_skcipher = {
1070a794d8d8SGilad Ben-Yossef 			.setkey = cc_cipher_sethkey,
1071a794d8d8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
1072a794d8d8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
1073a794d8d8SGilad Ben-Yossef 			.min_keysize = CC_HW_KEY_SIZE,
1074a794d8d8SGilad Ben-Yossef 			.max_keysize = CC_HW_KEY_SIZE,
1075a794d8d8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
1076a794d8d8SGilad Ben-Yossef 			},
1077a794d8d8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_BITLOCKER,
1078a794d8d8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
1079a794d8d8SGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
10801c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
1081f98f6e21SGilad Ben-Yossef 		.sec_func = true,
1082a794d8d8SGilad Ben-Yossef 	},
1083a794d8d8SGilad Ben-Yossef 	{
1084a794d8d8SGilad Ben-Yossef 		.name = "bitlocker512(paes)",
1085a794d8d8SGilad Ben-Yossef 		.driver_name = "bitlocker-paes-du512-ccree",
1086a794d8d8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
1087a794d8d8SGilad Ben-Yossef 		.template_skcipher = {
1088a794d8d8SGilad Ben-Yossef 			.setkey = cc_cipher_sethkey,
1089a794d8d8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
1090a794d8d8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
1091a794d8d8SGilad Ben-Yossef 			.min_keysize = CC_HW_KEY_SIZE,
1092a794d8d8SGilad Ben-Yossef 			.max_keysize = CC_HW_KEY_SIZE,
1093a794d8d8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
1094a794d8d8SGilad Ben-Yossef 			},
1095a794d8d8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_BITLOCKER,
1096a794d8d8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
1097a794d8d8SGilad Ben-Yossef 		.data_unit = 512,
1098a794d8d8SGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
10991c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
1100f98f6e21SGilad Ben-Yossef 		.sec_func = true,
1101a794d8d8SGilad Ben-Yossef 	},
1102a794d8d8SGilad Ben-Yossef 	{
1103a794d8d8SGilad Ben-Yossef 		.name = "bitlocker4096(paes)",
1104a794d8d8SGilad Ben-Yossef 		.driver_name = "bitlocker-paes-du4096-ccree",
1105a794d8d8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
1106a794d8d8SGilad Ben-Yossef 		.template_skcipher = {
1107a794d8d8SGilad Ben-Yossef 			.setkey = cc_cipher_sethkey,
1108a794d8d8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
1109a794d8d8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
1110a794d8d8SGilad Ben-Yossef 			.min_keysize = CC_HW_KEY_SIZE,
1111a794d8d8SGilad Ben-Yossef 			.max_keysize =  CC_HW_KEY_SIZE,
1112a794d8d8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
1113a794d8d8SGilad Ben-Yossef 			},
1114a794d8d8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_BITLOCKER,
1115a794d8d8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
1116a794d8d8SGilad Ben-Yossef 		.data_unit = 4096,
1117a794d8d8SGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
11181c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
1119f98f6e21SGilad Ben-Yossef 		.sec_func = true,
1120a794d8d8SGilad Ben-Yossef 	},
1121a794d8d8SGilad Ben-Yossef 	{
1122a794d8d8SGilad Ben-Yossef 		.name = "ecb(paes)",
1123a794d8d8SGilad Ben-Yossef 		.driver_name = "ecb-paes-ccree",
1124a794d8d8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
1125a794d8d8SGilad Ben-Yossef 		.template_skcipher = {
1126a794d8d8SGilad Ben-Yossef 			.setkey = cc_cipher_sethkey,
1127a794d8d8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
1128a794d8d8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
1129a794d8d8SGilad Ben-Yossef 			.min_keysize = CC_HW_KEY_SIZE,
1130a794d8d8SGilad Ben-Yossef 			.max_keysize = CC_HW_KEY_SIZE,
1131a794d8d8SGilad Ben-Yossef 			.ivsize = 0,
1132a794d8d8SGilad Ben-Yossef 			},
1133a794d8d8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_ECB,
1134a794d8d8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
1135a794d8d8SGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
11361c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
1137f98f6e21SGilad Ben-Yossef 		.sec_func = true,
1138a794d8d8SGilad Ben-Yossef 	},
1139a794d8d8SGilad Ben-Yossef 	{
1140a794d8d8SGilad Ben-Yossef 		.name = "cbc(paes)",
1141a794d8d8SGilad Ben-Yossef 		.driver_name = "cbc-paes-ccree",
1142a794d8d8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
1143a794d8d8SGilad Ben-Yossef 		.template_skcipher = {
1144a794d8d8SGilad Ben-Yossef 			.setkey = cc_cipher_sethkey,
1145a794d8d8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
1146a794d8d8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
1147a794d8d8SGilad Ben-Yossef 			.min_keysize = CC_HW_KEY_SIZE,
1148a794d8d8SGilad Ben-Yossef 			.max_keysize = CC_HW_KEY_SIZE,
1149a794d8d8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
1150a794d8d8SGilad Ben-Yossef 		},
1151a794d8d8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_CBC,
1152a794d8d8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
1153a794d8d8SGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
11541c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
1155f98f6e21SGilad Ben-Yossef 		.sec_func = true,
1156a794d8d8SGilad Ben-Yossef 	},
1157a794d8d8SGilad Ben-Yossef 	{
1158a794d8d8SGilad Ben-Yossef 		.name = "ofb(paes)",
1159a794d8d8SGilad Ben-Yossef 		.driver_name = "ofb-paes-ccree",
1160a794d8d8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
1161a794d8d8SGilad Ben-Yossef 		.template_skcipher = {
1162a794d8d8SGilad Ben-Yossef 			.setkey = cc_cipher_sethkey,
1163a794d8d8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
1164a794d8d8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
1165a794d8d8SGilad Ben-Yossef 			.min_keysize = CC_HW_KEY_SIZE,
1166a794d8d8SGilad Ben-Yossef 			.max_keysize = CC_HW_KEY_SIZE,
1167a794d8d8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
1168a794d8d8SGilad Ben-Yossef 			},
1169a794d8d8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_OFB,
1170a794d8d8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
1171a794d8d8SGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
11721c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
1173f98f6e21SGilad Ben-Yossef 		.sec_func = true,
1174a794d8d8SGilad Ben-Yossef 	},
1175a794d8d8SGilad Ben-Yossef 	{
117684f366daSGilad Ben-Yossef 		.name = "cts(cbc(paes))",
117784f366daSGilad Ben-Yossef 		.driver_name = "cts-cbc-paes-ccree",
1178a794d8d8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
1179a794d8d8SGilad Ben-Yossef 		.template_skcipher = {
1180a794d8d8SGilad Ben-Yossef 			.setkey = cc_cipher_sethkey,
1181a794d8d8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
1182a794d8d8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
1183a794d8d8SGilad Ben-Yossef 			.min_keysize = CC_HW_KEY_SIZE,
1184a794d8d8SGilad Ben-Yossef 			.max_keysize = CC_HW_KEY_SIZE,
1185a794d8d8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
1186a794d8d8SGilad Ben-Yossef 			},
1187a794d8d8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_CBC_CTS,
1188a794d8d8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
1189a794d8d8SGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
11901c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
1191f98f6e21SGilad Ben-Yossef 		.sec_func = true,
1192a794d8d8SGilad Ben-Yossef 	},
1193a794d8d8SGilad Ben-Yossef 	{
1194a794d8d8SGilad Ben-Yossef 		.name = "ctr(paes)",
1195a794d8d8SGilad Ben-Yossef 		.driver_name = "ctr-paes-ccree",
1196a794d8d8SGilad Ben-Yossef 		.blocksize = 1,
1197a794d8d8SGilad Ben-Yossef 		.template_skcipher = {
1198a794d8d8SGilad Ben-Yossef 			.setkey = cc_cipher_sethkey,
1199a794d8d8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
1200a794d8d8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
1201a794d8d8SGilad Ben-Yossef 			.min_keysize = CC_HW_KEY_SIZE,
1202a794d8d8SGilad Ben-Yossef 			.max_keysize = CC_HW_KEY_SIZE,
1203a794d8d8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
1204a794d8d8SGilad Ben-Yossef 			},
1205a794d8d8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_CTR,
1206a794d8d8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
1207a794d8d8SGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
12081c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
1209f98f6e21SGilad Ben-Yossef 		.sec_func = true,
1210a794d8d8SGilad Ben-Yossef 	},
1211a794d8d8SGilad Ben-Yossef 	{
121263ee04c8SGilad Ben-Yossef 		.name = "xts(aes)",
121363ee04c8SGilad Ben-Yossef 		.driver_name = "xts-aes-ccree",
121463ee04c8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
121563ee04c8SGilad Ben-Yossef 		.template_skcipher = {
121663ee04c8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
121763ee04c8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
121863ee04c8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
121963ee04c8SGilad Ben-Yossef 			.min_keysize = AES_MIN_KEY_SIZE * 2,
122063ee04c8SGilad Ben-Yossef 			.max_keysize = AES_MAX_KEY_SIZE * 2,
122163ee04c8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
122263ee04c8SGilad Ben-Yossef 			},
122363ee04c8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_XTS,
122463ee04c8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
122527b3b22dSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_630,
12261c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
122763ee04c8SGilad Ben-Yossef 	},
122863ee04c8SGilad Ben-Yossef 	{
122963ee04c8SGilad Ben-Yossef 		.name = "xts512(aes)",
123063ee04c8SGilad Ben-Yossef 		.driver_name = "xts-aes-du512-ccree",
123163ee04c8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
123263ee04c8SGilad Ben-Yossef 		.template_skcipher = {
123363ee04c8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
123463ee04c8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
123563ee04c8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
123663ee04c8SGilad Ben-Yossef 			.min_keysize = AES_MIN_KEY_SIZE * 2,
123763ee04c8SGilad Ben-Yossef 			.max_keysize = AES_MAX_KEY_SIZE * 2,
123863ee04c8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
123963ee04c8SGilad Ben-Yossef 			},
124063ee04c8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_XTS,
124163ee04c8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
124263ee04c8SGilad Ben-Yossef 		.data_unit = 512,
124327b3b22dSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
12441c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
124563ee04c8SGilad Ben-Yossef 	},
124663ee04c8SGilad Ben-Yossef 	{
124763ee04c8SGilad Ben-Yossef 		.name = "xts4096(aes)",
124863ee04c8SGilad Ben-Yossef 		.driver_name = "xts-aes-du4096-ccree",
124963ee04c8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
125063ee04c8SGilad Ben-Yossef 		.template_skcipher = {
125163ee04c8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
125263ee04c8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
125363ee04c8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
125463ee04c8SGilad Ben-Yossef 			.min_keysize = AES_MIN_KEY_SIZE * 2,
125563ee04c8SGilad Ben-Yossef 			.max_keysize = AES_MAX_KEY_SIZE * 2,
125663ee04c8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
125763ee04c8SGilad Ben-Yossef 			},
125863ee04c8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_XTS,
125963ee04c8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
126063ee04c8SGilad Ben-Yossef 		.data_unit = 4096,
126127b3b22dSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
12621c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
126363ee04c8SGilad Ben-Yossef 	},
126463ee04c8SGilad Ben-Yossef 	{
126563ee04c8SGilad Ben-Yossef 		.name = "essiv(aes)",
126663ee04c8SGilad Ben-Yossef 		.driver_name = "essiv-aes-ccree",
126763ee04c8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
126863ee04c8SGilad Ben-Yossef 		.template_skcipher = {
126963ee04c8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
127063ee04c8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
127163ee04c8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
127263ee04c8SGilad Ben-Yossef 			.min_keysize = AES_MIN_KEY_SIZE * 2,
127363ee04c8SGilad Ben-Yossef 			.max_keysize = AES_MAX_KEY_SIZE * 2,
127463ee04c8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
127563ee04c8SGilad Ben-Yossef 			},
127663ee04c8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_ESSIV,
127763ee04c8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
127827b3b22dSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
12791c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
128063ee04c8SGilad Ben-Yossef 	},
128163ee04c8SGilad Ben-Yossef 	{
128263ee04c8SGilad Ben-Yossef 		.name = "essiv512(aes)",
128363ee04c8SGilad Ben-Yossef 		.driver_name = "essiv-aes-du512-ccree",
128463ee04c8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
128563ee04c8SGilad Ben-Yossef 		.template_skcipher = {
128663ee04c8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
128763ee04c8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
128863ee04c8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
128963ee04c8SGilad Ben-Yossef 			.min_keysize = AES_MIN_KEY_SIZE * 2,
129063ee04c8SGilad Ben-Yossef 			.max_keysize = AES_MAX_KEY_SIZE * 2,
129163ee04c8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
129263ee04c8SGilad Ben-Yossef 			},
129363ee04c8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_ESSIV,
129463ee04c8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
129563ee04c8SGilad Ben-Yossef 		.data_unit = 512,
129627b3b22dSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
12971c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
129863ee04c8SGilad Ben-Yossef 	},
129963ee04c8SGilad Ben-Yossef 	{
130063ee04c8SGilad Ben-Yossef 		.name = "essiv4096(aes)",
130163ee04c8SGilad Ben-Yossef 		.driver_name = "essiv-aes-du4096-ccree",
130263ee04c8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
130363ee04c8SGilad Ben-Yossef 		.template_skcipher = {
130463ee04c8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
130563ee04c8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
130663ee04c8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
130763ee04c8SGilad Ben-Yossef 			.min_keysize = AES_MIN_KEY_SIZE * 2,
130863ee04c8SGilad Ben-Yossef 			.max_keysize = AES_MAX_KEY_SIZE * 2,
130963ee04c8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
131063ee04c8SGilad Ben-Yossef 			},
131163ee04c8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_ESSIV,
131263ee04c8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
131363ee04c8SGilad Ben-Yossef 		.data_unit = 4096,
131427b3b22dSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
13151c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
131663ee04c8SGilad Ben-Yossef 	},
131763ee04c8SGilad Ben-Yossef 	{
131863ee04c8SGilad Ben-Yossef 		.name = "bitlocker(aes)",
131963ee04c8SGilad Ben-Yossef 		.driver_name = "bitlocker-aes-ccree",
132063ee04c8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
132163ee04c8SGilad Ben-Yossef 		.template_skcipher = {
132263ee04c8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
132363ee04c8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
132463ee04c8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
132563ee04c8SGilad Ben-Yossef 			.min_keysize = AES_MIN_KEY_SIZE * 2,
132663ee04c8SGilad Ben-Yossef 			.max_keysize = AES_MAX_KEY_SIZE * 2,
132763ee04c8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
132863ee04c8SGilad Ben-Yossef 			},
132963ee04c8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_BITLOCKER,
133063ee04c8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
133127b3b22dSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
13321c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
133363ee04c8SGilad Ben-Yossef 	},
133463ee04c8SGilad Ben-Yossef 	{
133563ee04c8SGilad Ben-Yossef 		.name = "bitlocker512(aes)",
133663ee04c8SGilad Ben-Yossef 		.driver_name = "bitlocker-aes-du512-ccree",
133763ee04c8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
133863ee04c8SGilad Ben-Yossef 		.template_skcipher = {
133963ee04c8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
134063ee04c8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
134163ee04c8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
134263ee04c8SGilad Ben-Yossef 			.min_keysize = AES_MIN_KEY_SIZE * 2,
134363ee04c8SGilad Ben-Yossef 			.max_keysize = AES_MAX_KEY_SIZE * 2,
134463ee04c8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
134563ee04c8SGilad Ben-Yossef 			},
134663ee04c8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_BITLOCKER,
134763ee04c8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
134863ee04c8SGilad Ben-Yossef 		.data_unit = 512,
134927b3b22dSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
13501c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
135163ee04c8SGilad Ben-Yossef 	},
135263ee04c8SGilad Ben-Yossef 	{
135363ee04c8SGilad Ben-Yossef 		.name = "bitlocker4096(aes)",
135463ee04c8SGilad Ben-Yossef 		.driver_name = "bitlocker-aes-du4096-ccree",
135563ee04c8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
135663ee04c8SGilad Ben-Yossef 		.template_skcipher = {
135763ee04c8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
135863ee04c8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
135963ee04c8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
136063ee04c8SGilad Ben-Yossef 			.min_keysize = AES_MIN_KEY_SIZE * 2,
136163ee04c8SGilad Ben-Yossef 			.max_keysize = AES_MAX_KEY_SIZE * 2,
136263ee04c8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
136363ee04c8SGilad Ben-Yossef 			},
136463ee04c8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_BITLOCKER,
136563ee04c8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
136663ee04c8SGilad Ben-Yossef 		.data_unit = 4096,
136727b3b22dSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_712,
13681c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
136963ee04c8SGilad Ben-Yossef 	},
137063ee04c8SGilad Ben-Yossef 	{
137163ee04c8SGilad Ben-Yossef 		.name = "ecb(aes)",
137263ee04c8SGilad Ben-Yossef 		.driver_name = "ecb-aes-ccree",
137363ee04c8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
137463ee04c8SGilad Ben-Yossef 		.template_skcipher = {
137563ee04c8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
137663ee04c8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
137763ee04c8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
137863ee04c8SGilad Ben-Yossef 			.min_keysize = AES_MIN_KEY_SIZE,
137963ee04c8SGilad Ben-Yossef 			.max_keysize = AES_MAX_KEY_SIZE,
138063ee04c8SGilad Ben-Yossef 			.ivsize = 0,
138163ee04c8SGilad Ben-Yossef 			},
138263ee04c8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_ECB,
138363ee04c8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
138427b3b22dSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_630,
13851c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
138663ee04c8SGilad Ben-Yossef 	},
138763ee04c8SGilad Ben-Yossef 	{
138863ee04c8SGilad Ben-Yossef 		.name = "cbc(aes)",
138963ee04c8SGilad Ben-Yossef 		.driver_name = "cbc-aes-ccree",
139063ee04c8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
139163ee04c8SGilad Ben-Yossef 		.template_skcipher = {
139263ee04c8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
139363ee04c8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
139463ee04c8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
139563ee04c8SGilad Ben-Yossef 			.min_keysize = AES_MIN_KEY_SIZE,
139663ee04c8SGilad Ben-Yossef 			.max_keysize = AES_MAX_KEY_SIZE,
139763ee04c8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
139863ee04c8SGilad Ben-Yossef 		},
139963ee04c8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_CBC,
140063ee04c8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
140127b3b22dSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_630,
14021c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
140363ee04c8SGilad Ben-Yossef 	},
140463ee04c8SGilad Ben-Yossef 	{
140563ee04c8SGilad Ben-Yossef 		.name = "ofb(aes)",
140663ee04c8SGilad Ben-Yossef 		.driver_name = "ofb-aes-ccree",
140763ee04c8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
140863ee04c8SGilad Ben-Yossef 		.template_skcipher = {
140963ee04c8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
141063ee04c8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
141163ee04c8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
141263ee04c8SGilad Ben-Yossef 			.min_keysize = AES_MIN_KEY_SIZE,
141363ee04c8SGilad Ben-Yossef 			.max_keysize = AES_MAX_KEY_SIZE,
141463ee04c8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
141563ee04c8SGilad Ben-Yossef 			},
141663ee04c8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_OFB,
141763ee04c8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
141827b3b22dSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_630,
14191c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
142063ee04c8SGilad Ben-Yossef 	},
142163ee04c8SGilad Ben-Yossef 	{
142284f366daSGilad Ben-Yossef 		.name = "cts(cbc(aes))",
142384f366daSGilad Ben-Yossef 		.driver_name = "cts-cbc-aes-ccree",
142463ee04c8SGilad Ben-Yossef 		.blocksize = AES_BLOCK_SIZE,
142563ee04c8SGilad Ben-Yossef 		.template_skcipher = {
142663ee04c8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
142763ee04c8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
142863ee04c8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
142963ee04c8SGilad Ben-Yossef 			.min_keysize = AES_MIN_KEY_SIZE,
143063ee04c8SGilad Ben-Yossef 			.max_keysize = AES_MAX_KEY_SIZE,
143163ee04c8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
143263ee04c8SGilad Ben-Yossef 			},
143363ee04c8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_CBC_CTS,
143463ee04c8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
143527b3b22dSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_630,
14361c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
143763ee04c8SGilad Ben-Yossef 	},
143863ee04c8SGilad Ben-Yossef 	{
143963ee04c8SGilad Ben-Yossef 		.name = "ctr(aes)",
144063ee04c8SGilad Ben-Yossef 		.driver_name = "ctr-aes-ccree",
144163ee04c8SGilad Ben-Yossef 		.blocksize = 1,
144263ee04c8SGilad Ben-Yossef 		.template_skcipher = {
144363ee04c8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
144463ee04c8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
144563ee04c8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
144663ee04c8SGilad Ben-Yossef 			.min_keysize = AES_MIN_KEY_SIZE,
144763ee04c8SGilad Ben-Yossef 			.max_keysize = AES_MAX_KEY_SIZE,
144863ee04c8SGilad Ben-Yossef 			.ivsize = AES_BLOCK_SIZE,
144963ee04c8SGilad Ben-Yossef 			},
145063ee04c8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_CTR,
145163ee04c8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_AES,
145227b3b22dSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_630,
14531c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
145463ee04c8SGilad Ben-Yossef 	},
145563ee04c8SGilad Ben-Yossef 	{
145663ee04c8SGilad Ben-Yossef 		.name = "cbc(des3_ede)",
145763ee04c8SGilad Ben-Yossef 		.driver_name = "cbc-3des-ccree",
145863ee04c8SGilad Ben-Yossef 		.blocksize = DES3_EDE_BLOCK_SIZE,
145963ee04c8SGilad Ben-Yossef 		.template_skcipher = {
146063ee04c8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
146163ee04c8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
146263ee04c8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
146363ee04c8SGilad Ben-Yossef 			.min_keysize = DES3_EDE_KEY_SIZE,
146463ee04c8SGilad Ben-Yossef 			.max_keysize = DES3_EDE_KEY_SIZE,
146563ee04c8SGilad Ben-Yossef 			.ivsize = DES3_EDE_BLOCK_SIZE,
146663ee04c8SGilad Ben-Yossef 			},
146763ee04c8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_CBC,
146863ee04c8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_DES,
146927b3b22dSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_630,
14701c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
147163ee04c8SGilad Ben-Yossef 	},
147263ee04c8SGilad Ben-Yossef 	{
147363ee04c8SGilad Ben-Yossef 		.name = "ecb(des3_ede)",
147463ee04c8SGilad Ben-Yossef 		.driver_name = "ecb-3des-ccree",
147563ee04c8SGilad Ben-Yossef 		.blocksize = DES3_EDE_BLOCK_SIZE,
147663ee04c8SGilad Ben-Yossef 		.template_skcipher = {
147763ee04c8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
147863ee04c8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
147963ee04c8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
148063ee04c8SGilad Ben-Yossef 			.min_keysize = DES3_EDE_KEY_SIZE,
148163ee04c8SGilad Ben-Yossef 			.max_keysize = DES3_EDE_KEY_SIZE,
148263ee04c8SGilad Ben-Yossef 			.ivsize = 0,
148363ee04c8SGilad Ben-Yossef 			},
148463ee04c8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_ECB,
148563ee04c8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_DES,
148627b3b22dSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_630,
14871c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
148863ee04c8SGilad Ben-Yossef 	},
148963ee04c8SGilad Ben-Yossef 	{
149063ee04c8SGilad Ben-Yossef 		.name = "cbc(des)",
149163ee04c8SGilad Ben-Yossef 		.driver_name = "cbc-des-ccree",
149263ee04c8SGilad Ben-Yossef 		.blocksize = DES_BLOCK_SIZE,
149363ee04c8SGilad Ben-Yossef 		.template_skcipher = {
149463ee04c8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
149563ee04c8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
149663ee04c8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
149763ee04c8SGilad Ben-Yossef 			.min_keysize = DES_KEY_SIZE,
149863ee04c8SGilad Ben-Yossef 			.max_keysize = DES_KEY_SIZE,
149963ee04c8SGilad Ben-Yossef 			.ivsize = DES_BLOCK_SIZE,
150063ee04c8SGilad Ben-Yossef 			},
150163ee04c8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_CBC,
150263ee04c8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_DES,
150327b3b22dSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_630,
15041c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
150563ee04c8SGilad Ben-Yossef 	},
150663ee04c8SGilad Ben-Yossef 	{
150763ee04c8SGilad Ben-Yossef 		.name = "ecb(des)",
150863ee04c8SGilad Ben-Yossef 		.driver_name = "ecb-des-ccree",
150963ee04c8SGilad Ben-Yossef 		.blocksize = DES_BLOCK_SIZE,
151063ee04c8SGilad Ben-Yossef 		.template_skcipher = {
151163ee04c8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
151263ee04c8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
151363ee04c8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
151463ee04c8SGilad Ben-Yossef 			.min_keysize = DES_KEY_SIZE,
151563ee04c8SGilad Ben-Yossef 			.max_keysize = DES_KEY_SIZE,
151663ee04c8SGilad Ben-Yossef 			.ivsize = 0,
151763ee04c8SGilad Ben-Yossef 			},
151863ee04c8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_ECB,
151963ee04c8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_DES,
152027b3b22dSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_630,
15211c876a90SGilad Ben-Yossef 		.std_body = CC_STD_NIST,
152263ee04c8SGilad Ben-Yossef 	},
15239b8d51f8SGilad Ben-Yossef 	{
15249b8d51f8SGilad Ben-Yossef 		.name = "cbc(sm4)",
15259b8d51f8SGilad Ben-Yossef 		.driver_name = "cbc-sm4-ccree",
15269b8d51f8SGilad Ben-Yossef 		.blocksize = SM4_BLOCK_SIZE,
15279b8d51f8SGilad Ben-Yossef 		.template_skcipher = {
15289b8d51f8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
15299b8d51f8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
15309b8d51f8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
15319b8d51f8SGilad Ben-Yossef 			.min_keysize = SM4_KEY_SIZE,
15329b8d51f8SGilad Ben-Yossef 			.max_keysize = SM4_KEY_SIZE,
15339b8d51f8SGilad Ben-Yossef 			.ivsize = SM4_BLOCK_SIZE,
15349b8d51f8SGilad Ben-Yossef 			},
15359b8d51f8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_CBC,
15369b8d51f8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_SM4,
15379b8d51f8SGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_713,
15381c876a90SGilad Ben-Yossef 		.std_body = CC_STD_OSCCA,
15399b8d51f8SGilad Ben-Yossef 	},
15409b8d51f8SGilad Ben-Yossef 	{
15419b8d51f8SGilad Ben-Yossef 		.name = "ecb(sm4)",
15429b8d51f8SGilad Ben-Yossef 		.driver_name = "ecb-sm4-ccree",
15439b8d51f8SGilad Ben-Yossef 		.blocksize = SM4_BLOCK_SIZE,
15449b8d51f8SGilad Ben-Yossef 		.template_skcipher = {
15459b8d51f8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
15469b8d51f8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
15479b8d51f8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
15489b8d51f8SGilad Ben-Yossef 			.min_keysize = SM4_KEY_SIZE,
15499b8d51f8SGilad Ben-Yossef 			.max_keysize = SM4_KEY_SIZE,
15509b8d51f8SGilad Ben-Yossef 			.ivsize = 0,
15519b8d51f8SGilad Ben-Yossef 			},
15529b8d51f8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_ECB,
15539b8d51f8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_SM4,
15549b8d51f8SGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_713,
15551c876a90SGilad Ben-Yossef 		.std_body = CC_STD_OSCCA,
15569b8d51f8SGilad Ben-Yossef 	},
15579b8d51f8SGilad Ben-Yossef 	{
15589b8d51f8SGilad Ben-Yossef 		.name = "ctr(sm4)",
15599b8d51f8SGilad Ben-Yossef 		.driver_name = "ctr-sm4-ccree",
15609b8d51f8SGilad Ben-Yossef 		.blocksize = SM4_BLOCK_SIZE,
15619b8d51f8SGilad Ben-Yossef 		.template_skcipher = {
15629b8d51f8SGilad Ben-Yossef 			.setkey = cc_cipher_setkey,
15639b8d51f8SGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
15649b8d51f8SGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
15659b8d51f8SGilad Ben-Yossef 			.min_keysize = SM4_KEY_SIZE,
15669b8d51f8SGilad Ben-Yossef 			.max_keysize = SM4_KEY_SIZE,
15679b8d51f8SGilad Ben-Yossef 			.ivsize = SM4_BLOCK_SIZE,
15689b8d51f8SGilad Ben-Yossef 			},
15699b8d51f8SGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_CTR,
15709b8d51f8SGilad Ben-Yossef 		.flow_mode = S_DIN_to_SM4,
15719b8d51f8SGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_713,
15721c876a90SGilad Ben-Yossef 		.std_body = CC_STD_OSCCA,
15739b8d51f8SGilad Ben-Yossef 	},
1574bee711faSGilad Ben-Yossef 	{
1575bee711faSGilad Ben-Yossef 		.name = "cbc(psm4)",
1576bee711faSGilad Ben-Yossef 		.driver_name = "cbc-psm4-ccree",
1577bee711faSGilad Ben-Yossef 		.blocksize = SM4_BLOCK_SIZE,
1578bee711faSGilad Ben-Yossef 		.template_skcipher = {
1579bee711faSGilad Ben-Yossef 			.setkey = cc_cipher_sethkey,
1580bee711faSGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
1581bee711faSGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
1582bee711faSGilad Ben-Yossef 			.min_keysize = CC_HW_KEY_SIZE,
1583bee711faSGilad Ben-Yossef 			.max_keysize = CC_HW_KEY_SIZE,
1584bee711faSGilad Ben-Yossef 			.ivsize = SM4_BLOCK_SIZE,
1585bee711faSGilad Ben-Yossef 			},
1586bee711faSGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_CBC,
1587bee711faSGilad Ben-Yossef 		.flow_mode = S_DIN_to_SM4,
1588bee711faSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_713,
1589bee711faSGilad Ben-Yossef 		.std_body = CC_STD_OSCCA,
1590bee711faSGilad Ben-Yossef 		.sec_func = true,
1591bee711faSGilad Ben-Yossef 	},
1592bee711faSGilad Ben-Yossef 	{
1593bee711faSGilad Ben-Yossef 		.name = "ctr(psm4)",
1594bee711faSGilad Ben-Yossef 		.driver_name = "ctr-psm4-ccree",
1595bee711faSGilad Ben-Yossef 		.blocksize = SM4_BLOCK_SIZE,
1596bee711faSGilad Ben-Yossef 		.template_skcipher = {
1597bee711faSGilad Ben-Yossef 			.setkey = cc_cipher_sethkey,
1598bee711faSGilad Ben-Yossef 			.encrypt = cc_cipher_encrypt,
1599bee711faSGilad Ben-Yossef 			.decrypt = cc_cipher_decrypt,
1600bee711faSGilad Ben-Yossef 			.min_keysize = CC_HW_KEY_SIZE,
1601bee711faSGilad Ben-Yossef 			.max_keysize = CC_HW_KEY_SIZE,
1602bee711faSGilad Ben-Yossef 			.ivsize = SM4_BLOCK_SIZE,
1603bee711faSGilad Ben-Yossef 			},
1604bee711faSGilad Ben-Yossef 		.cipher_mode = DRV_CIPHER_CTR,
1605bee711faSGilad Ben-Yossef 		.flow_mode = S_DIN_to_SM4,
1606bee711faSGilad Ben-Yossef 		.min_hw_rev = CC_HW_REV_713,
1607bee711faSGilad Ben-Yossef 		.std_body = CC_STD_OSCCA,
1608bee711faSGilad Ben-Yossef 		.sec_func = true,
1609bee711faSGilad Ben-Yossef 	},
161063ee04c8SGilad Ben-Yossef };
161163ee04c8SGilad Ben-Yossef 
161263ee04c8SGilad Ben-Yossef static struct cc_crypto_alg *cc_create_alg(const struct cc_alg_template *tmpl,
161363ee04c8SGilad Ben-Yossef 					   struct device *dev)
161463ee04c8SGilad Ben-Yossef {
161563ee04c8SGilad Ben-Yossef 	struct cc_crypto_alg *t_alg;
161663ee04c8SGilad Ben-Yossef 	struct skcipher_alg *alg;
161763ee04c8SGilad Ben-Yossef 
161863ee04c8SGilad Ben-Yossef 	t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
161963ee04c8SGilad Ben-Yossef 	if (!t_alg)
162063ee04c8SGilad Ben-Yossef 		return ERR_PTR(-ENOMEM);
162163ee04c8SGilad Ben-Yossef 
162263ee04c8SGilad Ben-Yossef 	alg = &t_alg->skcipher_alg;
162363ee04c8SGilad Ben-Yossef 
162463ee04c8SGilad Ben-Yossef 	memcpy(alg, &tmpl->template_skcipher, sizeof(*alg));
162563ee04c8SGilad Ben-Yossef 
162663ee04c8SGilad Ben-Yossef 	snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", tmpl->name);
162763ee04c8SGilad Ben-Yossef 	snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
162863ee04c8SGilad Ben-Yossef 		 tmpl->driver_name);
162963ee04c8SGilad Ben-Yossef 	alg->base.cra_module = THIS_MODULE;
163063ee04c8SGilad Ben-Yossef 	alg->base.cra_priority = CC_CRA_PRIO;
163163ee04c8SGilad Ben-Yossef 	alg->base.cra_blocksize = tmpl->blocksize;
163263ee04c8SGilad Ben-Yossef 	alg->base.cra_alignmask = 0;
163363ee04c8SGilad Ben-Yossef 	alg->base.cra_ctxsize = sizeof(struct cc_cipher_ctx);
163463ee04c8SGilad Ben-Yossef 
163563ee04c8SGilad Ben-Yossef 	alg->base.cra_init = cc_cipher_init;
163663ee04c8SGilad Ben-Yossef 	alg->base.cra_exit = cc_cipher_exit;
16372c95e6d9SEric Biggers 	alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
163863ee04c8SGilad Ben-Yossef 
163963ee04c8SGilad Ben-Yossef 	t_alg->cipher_mode = tmpl->cipher_mode;
164063ee04c8SGilad Ben-Yossef 	t_alg->flow_mode = tmpl->flow_mode;
164163ee04c8SGilad Ben-Yossef 	t_alg->data_unit = tmpl->data_unit;
164263ee04c8SGilad Ben-Yossef 
164363ee04c8SGilad Ben-Yossef 	return t_alg;
164463ee04c8SGilad Ben-Yossef }
164563ee04c8SGilad Ben-Yossef 
164663ee04c8SGilad Ben-Yossef int cc_cipher_free(struct cc_drvdata *drvdata)
164763ee04c8SGilad Ben-Yossef {
164863ee04c8SGilad Ben-Yossef 	struct cc_crypto_alg *t_alg, *n;
164963ee04c8SGilad Ben-Yossef 	struct cc_cipher_handle *cipher_handle = drvdata->cipher_handle;
165063ee04c8SGilad Ben-Yossef 
165163ee04c8SGilad Ben-Yossef 	if (cipher_handle) {
165263ee04c8SGilad Ben-Yossef 		/* Remove registered algs */
165363ee04c8SGilad Ben-Yossef 		list_for_each_entry_safe(t_alg, n, &cipher_handle->alg_list,
165463ee04c8SGilad Ben-Yossef 					 entry) {
165563ee04c8SGilad Ben-Yossef 			crypto_unregister_skcipher(&t_alg->skcipher_alg);
165663ee04c8SGilad Ben-Yossef 			list_del(&t_alg->entry);
165763ee04c8SGilad Ben-Yossef 			kfree(t_alg);
165863ee04c8SGilad Ben-Yossef 		}
165963ee04c8SGilad Ben-Yossef 		kfree(cipher_handle);
166063ee04c8SGilad Ben-Yossef 		drvdata->cipher_handle = NULL;
166163ee04c8SGilad Ben-Yossef 	}
166263ee04c8SGilad Ben-Yossef 	return 0;
166363ee04c8SGilad Ben-Yossef }
166463ee04c8SGilad Ben-Yossef 
166563ee04c8SGilad Ben-Yossef int cc_cipher_alloc(struct cc_drvdata *drvdata)
166663ee04c8SGilad Ben-Yossef {
166763ee04c8SGilad Ben-Yossef 	struct cc_cipher_handle *cipher_handle;
166863ee04c8SGilad Ben-Yossef 	struct cc_crypto_alg *t_alg;
166963ee04c8SGilad Ben-Yossef 	struct device *dev = drvdata_to_dev(drvdata);
167063ee04c8SGilad Ben-Yossef 	int rc = -ENOMEM;
167163ee04c8SGilad Ben-Yossef 	int alg;
167263ee04c8SGilad Ben-Yossef 
167363ee04c8SGilad Ben-Yossef 	cipher_handle = kmalloc(sizeof(*cipher_handle), GFP_KERNEL);
167463ee04c8SGilad Ben-Yossef 	if (!cipher_handle)
167563ee04c8SGilad Ben-Yossef 		return -ENOMEM;
167663ee04c8SGilad Ben-Yossef 
167763ee04c8SGilad Ben-Yossef 	INIT_LIST_HEAD(&cipher_handle->alg_list);
167863ee04c8SGilad Ben-Yossef 	drvdata->cipher_handle = cipher_handle;
167963ee04c8SGilad Ben-Yossef 
168063ee04c8SGilad Ben-Yossef 	/* Linux crypto */
168163ee04c8SGilad Ben-Yossef 	dev_dbg(dev, "Number of algorithms = %zu\n",
168263ee04c8SGilad Ben-Yossef 		ARRAY_SIZE(skcipher_algs));
168363ee04c8SGilad Ben-Yossef 	for (alg = 0; alg < ARRAY_SIZE(skcipher_algs); alg++) {
16841c876a90SGilad Ben-Yossef 		if ((skcipher_algs[alg].min_hw_rev > drvdata->hw_rev) ||
1685f98f6e21SGilad Ben-Yossef 		    !(drvdata->std_bodies & skcipher_algs[alg].std_body) ||
1686f98f6e21SGilad Ben-Yossef 		    (drvdata->sec_disabled && skcipher_algs[alg].sec_func))
168727b3b22dSGilad Ben-Yossef 			continue;
168827b3b22dSGilad Ben-Yossef 
168963ee04c8SGilad Ben-Yossef 		dev_dbg(dev, "creating %s\n", skcipher_algs[alg].driver_name);
169063ee04c8SGilad Ben-Yossef 		t_alg = cc_create_alg(&skcipher_algs[alg], dev);
169163ee04c8SGilad Ben-Yossef 		if (IS_ERR(t_alg)) {
169263ee04c8SGilad Ben-Yossef 			rc = PTR_ERR(t_alg);
169363ee04c8SGilad Ben-Yossef 			dev_err(dev, "%s alg allocation failed\n",
169463ee04c8SGilad Ben-Yossef 				skcipher_algs[alg].driver_name);
169563ee04c8SGilad Ben-Yossef 			goto fail0;
169663ee04c8SGilad Ben-Yossef 		}
169763ee04c8SGilad Ben-Yossef 		t_alg->drvdata = drvdata;
169863ee04c8SGilad Ben-Yossef 
169963ee04c8SGilad Ben-Yossef 		dev_dbg(dev, "registering %s\n",
170063ee04c8SGilad Ben-Yossef 			skcipher_algs[alg].driver_name);
170163ee04c8SGilad Ben-Yossef 		rc = crypto_register_skcipher(&t_alg->skcipher_alg);
170263ee04c8SGilad Ben-Yossef 		dev_dbg(dev, "%s alg registration rc = %x\n",
170363ee04c8SGilad Ben-Yossef 			t_alg->skcipher_alg.base.cra_driver_name, rc);
170463ee04c8SGilad Ben-Yossef 		if (rc) {
170563ee04c8SGilad Ben-Yossef 			dev_err(dev, "%s alg registration failed\n",
170663ee04c8SGilad Ben-Yossef 				t_alg->skcipher_alg.base.cra_driver_name);
170763ee04c8SGilad Ben-Yossef 			kfree(t_alg);
170863ee04c8SGilad Ben-Yossef 			goto fail0;
170963ee04c8SGilad Ben-Yossef 		} else {
171063ee04c8SGilad Ben-Yossef 			list_add_tail(&t_alg->entry,
171163ee04c8SGilad Ben-Yossef 				      &cipher_handle->alg_list);
171263ee04c8SGilad Ben-Yossef 			dev_dbg(dev, "Registered %s\n",
171363ee04c8SGilad Ben-Yossef 				t_alg->skcipher_alg.base.cra_driver_name);
171463ee04c8SGilad Ben-Yossef 		}
171563ee04c8SGilad Ben-Yossef 	}
171663ee04c8SGilad Ben-Yossef 	return 0;
171763ee04c8SGilad Ben-Yossef 
171863ee04c8SGilad Ben-Yossef fail0:
171963ee04c8SGilad Ben-Yossef 	cc_cipher_free(drvdata);
172063ee04c8SGilad Ben-Yossef 	return rc;
172163ee04c8SGilad Ben-Yossef }
1722