xref: /dpdk/drivers/common/cnxk/roc_se.c (revision df22fe2946d93dc42ca81eb8009535f1dd6a2022)
15e076b60SAnkur Dwivedi /* SPDX-License-Identifier: BSD-3-Clause
25e076b60SAnkur Dwivedi  * Copyright(C) 2021 Marvell.
35e076b60SAnkur Dwivedi  */
45e076b60SAnkur Dwivedi 
55e076b60SAnkur Dwivedi #include "roc_api.h"
65e076b60SAnkur Dwivedi 
766a8a26fSVidya Sagar Velumuri static uint8_t zuc_key128[32] = {
866a8a26fSVidya Sagar Velumuri 	0x44, 0xD7, 0x26, 0xBC, 0x62, 0x6B, 0x13, 0x5E, 0x57, 0x89, 0x35,
966a8a26fSVidya Sagar Velumuri 	0xE2, 0x71, 0x35, 0x09, 0xAF, 0x4D, 0x78, 0x2F, 0x13, 0x6B, 0xC4,
1066a8a26fSVidya Sagar Velumuri 	0x1A, 0xF1, 0x5E, 0x26, 0x3C, 0x4D, 0x78, 0x9A, 0x47, 0xAC};
1166a8a26fSVidya Sagar Velumuri 
1266a8a26fSVidya Sagar Velumuri static uint8_t zuc_key256[16] = {0x22, 0x2f, 0x24, 0x2a, 0x6d, 0x40,
1366a8a26fSVidya Sagar Velumuri 				 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
1466a8a26fSVidya Sagar Velumuri 				 0x40, 0x52, 0x10, 0x30};
1566a8a26fSVidya Sagar Velumuri 
1666a8a26fSVidya Sagar Velumuri static uint8_t zuc_key256_mac4[16] = {0x22, 0x2f, 0x25, 0x2a, 0x6d, 0x40,
1766a8a26fSVidya Sagar Velumuri 				      0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
1866a8a26fSVidya Sagar Velumuri 				      0x40, 0x52, 0x10, 0x30};
1966a8a26fSVidya Sagar Velumuri 
2066a8a26fSVidya Sagar Velumuri static uint8_t zuc_key256_mac8[16] = {0x23, 0x2f, 0x24, 0x2a, 0x6d, 0x40,
2166a8a26fSVidya Sagar Velumuri 				      0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
2266a8a26fSVidya Sagar Velumuri 				      0x40, 0x52, 0x10, 0x30};
2366a8a26fSVidya Sagar Velumuri 
2466a8a26fSVidya Sagar Velumuri static uint8_t zuc_key256_mac16[16] = {0x23, 0x2f, 0x25, 0x2a, 0x6d, 0x40,
2566a8a26fSVidya Sagar Velumuri 				       0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
2666a8a26fSVidya Sagar Velumuri 				       0x40, 0x52, 0x10, 0x30};
275e076b60SAnkur Dwivedi 
285e076b60SAnkur Dwivedi static inline void
cpt_snow3g_key_gen(const uint8_t * ck,uint32_t * keyx)295e076b60SAnkur Dwivedi cpt_snow3g_key_gen(const uint8_t *ck, uint32_t *keyx)
305e076b60SAnkur Dwivedi {
315e076b60SAnkur Dwivedi 	int i, base;
325e076b60SAnkur Dwivedi 
335e076b60SAnkur Dwivedi 	for (i = 0; i < 4; i++) {
345e076b60SAnkur Dwivedi 		base = 4 * i;
355e076b60SAnkur Dwivedi 		keyx[3 - i] = (ck[base] << 24) | (ck[base + 1] << 16) |
365e076b60SAnkur Dwivedi 			      (ck[base + 2] << 8) | (ck[base + 3]);
375e076b60SAnkur Dwivedi 		keyx[3 - i] = plt_cpu_to_be_32(keyx[3 - i]);
385e076b60SAnkur Dwivedi 	}
395e076b60SAnkur Dwivedi }
405e076b60SAnkur Dwivedi 
415e076b60SAnkur Dwivedi static inline int
cpt_ciph_aes_key_validate(uint16_t key_len)425e076b60SAnkur Dwivedi cpt_ciph_aes_key_validate(uint16_t key_len)
435e076b60SAnkur Dwivedi {
445e076b60SAnkur Dwivedi 	switch (key_len) {
455e076b60SAnkur Dwivedi 	case 16:
465e076b60SAnkur Dwivedi 	case 24:
475e076b60SAnkur Dwivedi 	case 32:
485e076b60SAnkur Dwivedi 		return 0;
495e076b60SAnkur Dwivedi 	default:
505e076b60SAnkur Dwivedi 		return -1;
515e076b60SAnkur Dwivedi 	}
525e076b60SAnkur Dwivedi }
535e076b60SAnkur Dwivedi 
545e076b60SAnkur Dwivedi static inline int
cpt_ciph_type_set(roc_se_cipher_type type,struct roc_se_ctx * ctx,uint16_t key_len)55e332f197STejasree Kondoj cpt_ciph_type_set(roc_se_cipher_type type, struct roc_se_ctx *ctx, uint16_t key_len)
565e076b60SAnkur Dwivedi {
57e332f197STejasree Kondoj 	bool chained_op = ctx->ciph_then_auth || ctx->auth_then_ciph;
585e076b60SAnkur Dwivedi 	int fc_type = 0;
595e076b60SAnkur Dwivedi 
605e076b60SAnkur Dwivedi 	switch (type) {
615e076b60SAnkur Dwivedi 	case ROC_SE_DES3_CBC:
625e076b60SAnkur Dwivedi 	case ROC_SE_DES3_ECB:
6387d39616SVolodymyr Fialko 	case ROC_SE_DES_DOCSISBPI:
645e076b60SAnkur Dwivedi 		fc_type = ROC_SE_FC_GEN;
655e076b60SAnkur Dwivedi 		break;
665e076b60SAnkur Dwivedi 	case ROC_SE_AES_CBC:
675e076b60SAnkur Dwivedi 	case ROC_SE_AES_ECB:
685e076b60SAnkur Dwivedi 	case ROC_SE_AES_CFB:
695e076b60SAnkur Dwivedi 	case ROC_SE_AES_CTR:
705e076b60SAnkur Dwivedi 	case ROC_SE_AES_GCM:
71d15bc634STejasree Kondoj 	case ROC_SE_AES_CCM:
7287d39616SVolodymyr Fialko 	case ROC_SE_AES_DOCSISBPI:
735e076b60SAnkur Dwivedi 		if (unlikely(cpt_ciph_aes_key_validate(key_len) != 0))
745e076b60SAnkur Dwivedi 			return -1;
755e076b60SAnkur Dwivedi 		fc_type = ROC_SE_FC_GEN;
765e076b60SAnkur Dwivedi 		break;
775e076b60SAnkur Dwivedi 	case ROC_SE_CHACHA20:
785e076b60SAnkur Dwivedi 		fc_type = ROC_SE_FC_GEN;
795e076b60SAnkur Dwivedi 		break;
805e076b60SAnkur Dwivedi 	case ROC_SE_AES_XTS:
815e076b60SAnkur Dwivedi 		key_len = key_len / 2;
825e076b60SAnkur Dwivedi 		if (unlikely(key_len == 24)) {
835e076b60SAnkur Dwivedi 			plt_err("Invalid AES key len for XTS");
845e076b60SAnkur Dwivedi 			return -1;
855e076b60SAnkur Dwivedi 		}
865e076b60SAnkur Dwivedi 		if (unlikely(cpt_ciph_aes_key_validate(key_len) != 0))
875e076b60SAnkur Dwivedi 			return -1;
885e076b60SAnkur Dwivedi 		fc_type = ROC_SE_FC_GEN;
895e076b60SAnkur Dwivedi 		break;
905e076b60SAnkur Dwivedi 	case ROC_SE_ZUC_EEA3:
916855d510STejasree Kondoj 		if (unlikely(key_len != 16)) {
926855d510STejasree Kondoj 			/*
936855d510STejasree Kondoj 			 * ZUC 256 is not supported with older microcode
946855d510STejasree Kondoj 			 * where pdcp_iv_offset is 16
956855d510STejasree Kondoj 			 */
966855d510STejasree Kondoj 			if (chained_op || (ctx->pdcp_iv_offset == 16)) {
976855d510STejasree Kondoj 				plt_err("ZUC 256 is not supported with chained operations");
98e332f197STejasree Kondoj 				return -1;
99e332f197STejasree Kondoj 			}
1006855d510STejasree Kondoj 		}
1016855d510STejasree Kondoj 		if (chained_op)
1026855d510STejasree Kondoj 			fc_type = ROC_SE_PDCP_CHAIN;
1036855d510STejasree Kondoj 		else
1046855d510STejasree Kondoj 			fc_type = ROC_SE_PDCP;
105a07d1d4dSVidya Sagar Velumuri 		break;
1065e076b60SAnkur Dwivedi 	case ROC_SE_SNOW3G_UEA2:
1075e076b60SAnkur Dwivedi 		if (unlikely(key_len != 16))
1085e076b60SAnkur Dwivedi 			return -1;
109e332f197STejasree Kondoj 		if (chained_op)
1100fbb3e6cSTejasree Kondoj 			fc_type = ROC_SE_PDCP_CHAIN;
1110fbb3e6cSTejasree Kondoj 		else
1125e076b60SAnkur Dwivedi 			fc_type = ROC_SE_PDCP;
1135e076b60SAnkur Dwivedi 		break;
1145e076b60SAnkur Dwivedi 	case ROC_SE_AES_CTR_EEA2:
115e332f197STejasree Kondoj 		if (chained_op)
1160fbb3e6cSTejasree Kondoj 			fc_type = ROC_SE_PDCP_CHAIN;
1170fbb3e6cSTejasree Kondoj 		else
1185e076b60SAnkur Dwivedi 			fc_type = ROC_SE_PDCP;
1195e076b60SAnkur Dwivedi 		break;
1205e076b60SAnkur Dwivedi 	case ROC_SE_KASUMI_F8_CBC:
1215e076b60SAnkur Dwivedi 	case ROC_SE_KASUMI_F8_ECB:
1225e076b60SAnkur Dwivedi 		if (unlikely(key_len != 16))
1235e076b60SAnkur Dwivedi 			return -1;
1245e076b60SAnkur Dwivedi 		/* No support for AEAD yet */
1255e076b60SAnkur Dwivedi 		if (unlikely(ctx->hash_type))
1265e076b60SAnkur Dwivedi 			return -1;
1275e076b60SAnkur Dwivedi 		fc_type = ROC_SE_KASUMI;
1285e076b60SAnkur Dwivedi 		break;
1295e076b60SAnkur Dwivedi 	default:
1305e076b60SAnkur Dwivedi 		return -1;
1315e076b60SAnkur Dwivedi 	}
1325e076b60SAnkur Dwivedi 
1335e076b60SAnkur Dwivedi 	ctx->fc_type = fc_type;
1345e076b60SAnkur Dwivedi 	return 0;
1355e076b60SAnkur Dwivedi }
1365e076b60SAnkur Dwivedi 
1375e076b60SAnkur Dwivedi static inline void
cpt_ciph_aes_key_type_set(struct roc_se_context * fctx,uint16_t key_len)1385e076b60SAnkur Dwivedi cpt_ciph_aes_key_type_set(struct roc_se_context *fctx, uint16_t key_len)
1395e076b60SAnkur Dwivedi {
1405e076b60SAnkur Dwivedi 	roc_se_aes_type aes_key_type = 0;
1415e076b60SAnkur Dwivedi 
1425e076b60SAnkur Dwivedi 	switch (key_len) {
1435e076b60SAnkur Dwivedi 	case 16:
1445e076b60SAnkur Dwivedi 		aes_key_type = ROC_SE_AES_128_BIT;
1455e076b60SAnkur Dwivedi 		break;
1465e076b60SAnkur Dwivedi 	case 24:
1475e076b60SAnkur Dwivedi 		aes_key_type = ROC_SE_AES_192_BIT;
1485e076b60SAnkur Dwivedi 		break;
1495e076b60SAnkur Dwivedi 	case 32:
1505e076b60SAnkur Dwivedi 		aes_key_type = ROC_SE_AES_256_BIT;
1515e076b60SAnkur Dwivedi 		break;
1525e076b60SAnkur Dwivedi 	default:
1535e076b60SAnkur Dwivedi 		/* This should not happen */
1545e076b60SAnkur Dwivedi 		plt_err("Invalid AES key len");
1555e076b60SAnkur Dwivedi 		return;
1565e076b60SAnkur Dwivedi 	}
1575e076b60SAnkur Dwivedi 	fctx->enc.aes_key = aes_key_type;
1585e076b60SAnkur Dwivedi }
1595e076b60SAnkur Dwivedi 
160*df22fe29SVidya Sagar Velumuri void
roc_se_hmac_opad_ipad_gen(roc_se_auth_type auth_type,const uint8_t * key,uint16_t length,uint8_t * opad_ipad,roc_se_op_type op_type)161*df22fe29SVidya Sagar Velumuri roc_se_hmac_opad_ipad_gen(roc_se_auth_type auth_type, const uint8_t *key, uint16_t length,
162*df22fe29SVidya Sagar Velumuri 			  uint8_t *opad_ipad, roc_se_op_type op_type)
163470071c8SAakash Sasidharan {
164470071c8SAakash Sasidharan 	uint8_t opad[128] = {[0 ... 127] = 0x5c};
165470071c8SAakash Sasidharan 	uint8_t ipad[128] = {[0 ... 127] = 0x36};
166*df22fe29SVidya Sagar Velumuri 	uint8_t ipad_offset, opad_offset;
167470071c8SAakash Sasidharan 	uint32_t i;
168470071c8SAakash Sasidharan 
169*df22fe29SVidya Sagar Velumuri 	if (op_type == ROC_SE_IPSEC) {
170*df22fe29SVidya Sagar Velumuri 		if ((auth_type == ROC_SE_MD5_TYPE) || (auth_type == ROC_SE_SHA1_TYPE))
171*df22fe29SVidya Sagar Velumuri 			ipad_offset = 24;
172*df22fe29SVidya Sagar Velumuri 		else
173*df22fe29SVidya Sagar Velumuri 			ipad_offset = 64;
174*df22fe29SVidya Sagar Velumuri 		opad_offset = 0;
175*df22fe29SVidya Sagar Velumuri 	} else if (op_type == ROC_SE_TLS) {
176*df22fe29SVidya Sagar Velumuri 		ipad_offset = 64;
177*df22fe29SVidya Sagar Velumuri 		opad_offset = 0;
178*df22fe29SVidya Sagar Velumuri 	} else {
179*df22fe29SVidya Sagar Velumuri 		ipad_offset = 0;
180*df22fe29SVidya Sagar Velumuri 		opad_offset = 64;
181*df22fe29SVidya Sagar Velumuri 	}
182*df22fe29SVidya Sagar Velumuri 
183470071c8SAakash Sasidharan 	/* HMAC OPAD and IPAD */
184470071c8SAakash Sasidharan 	for (i = 0; i < 128 && i < length; i++) {
185470071c8SAakash Sasidharan 		opad[i] = opad[i] ^ key[i];
186470071c8SAakash Sasidharan 		ipad[i] = ipad[i] ^ key[i];
187470071c8SAakash Sasidharan 	}
188470071c8SAakash Sasidharan 
189470071c8SAakash Sasidharan 	/* Precompute hash of HMAC OPAD and IPAD to avoid
190470071c8SAakash Sasidharan 	 * per packet computation
191470071c8SAakash Sasidharan 	 */
192470071c8SAakash Sasidharan 	switch (auth_type) {
193470071c8SAakash Sasidharan 	case ROC_SE_MD5_TYPE:
194*df22fe29SVidya Sagar Velumuri 		roc_hash_md5_gen(opad, (uint32_t *)&opad_ipad[opad_offset]);
195*df22fe29SVidya Sagar Velumuri 		roc_hash_md5_gen(ipad, (uint32_t *)&opad_ipad[ipad_offset]);
196470071c8SAakash Sasidharan 		break;
197470071c8SAakash Sasidharan 	case ROC_SE_SHA1_TYPE:
198*df22fe29SVidya Sagar Velumuri 		roc_hash_sha1_gen(opad, (uint32_t *)&opad_ipad[opad_offset]);
199*df22fe29SVidya Sagar Velumuri 		roc_hash_sha1_gen(ipad, (uint32_t *)&opad_ipad[ipad_offset]);
200470071c8SAakash Sasidharan 		break;
201470071c8SAakash Sasidharan 	case ROC_SE_SHA2_SHA224:
202*df22fe29SVidya Sagar Velumuri 		roc_hash_sha256_gen(opad, (uint32_t *)&opad_ipad[opad_offset], 224);
203*df22fe29SVidya Sagar Velumuri 		roc_hash_sha256_gen(ipad, (uint32_t *)&opad_ipad[ipad_offset], 224);
204470071c8SAakash Sasidharan 		break;
205470071c8SAakash Sasidharan 	case ROC_SE_SHA2_SHA256:
206*df22fe29SVidya Sagar Velumuri 		roc_hash_sha256_gen(opad, (uint32_t *)&opad_ipad[opad_offset], 256);
207*df22fe29SVidya Sagar Velumuri 		roc_hash_sha256_gen(ipad, (uint32_t *)&opad_ipad[ipad_offset], 256);
208470071c8SAakash Sasidharan 		break;
209470071c8SAakash Sasidharan 	case ROC_SE_SHA2_SHA384:
210*df22fe29SVidya Sagar Velumuri 		roc_hash_sha512_gen(opad, (uint64_t *)&opad_ipad[opad_offset], 384);
211*df22fe29SVidya Sagar Velumuri 		roc_hash_sha512_gen(ipad, (uint64_t *)&opad_ipad[ipad_offset], 384);
212470071c8SAakash Sasidharan 		break;
213470071c8SAakash Sasidharan 	case ROC_SE_SHA2_SHA512:
214*df22fe29SVidya Sagar Velumuri 		roc_hash_sha512_gen(opad, (uint64_t *)&opad_ipad[opad_offset], 512);
215*df22fe29SVidya Sagar Velumuri 		roc_hash_sha512_gen(ipad, (uint64_t *)&opad_ipad[ipad_offset], 512);
216470071c8SAakash Sasidharan 		break;
217470071c8SAakash Sasidharan 	default:
218470071c8SAakash Sasidharan 		break;
219470071c8SAakash Sasidharan 	}
220470071c8SAakash Sasidharan }
221470071c8SAakash Sasidharan 
222a07d1d4dSVidya Sagar Velumuri static int
cpt_pdcp_chain_key_type_get(uint16_t key_len)2230fbb3e6cSTejasree Kondoj cpt_pdcp_chain_key_type_get(uint16_t key_len)
2240fbb3e6cSTejasree Kondoj {
2250fbb3e6cSTejasree Kondoj 	roc_se_aes_type key_type;
2260fbb3e6cSTejasree Kondoj 
2270fbb3e6cSTejasree Kondoj 	switch (key_len) {
2280fbb3e6cSTejasree Kondoj 	case 16:
2290fbb3e6cSTejasree Kondoj 		key_type = ROC_SE_AES_128_BIT;
2300fbb3e6cSTejasree Kondoj 		break;
2310fbb3e6cSTejasree Kondoj 	case 24:
2320fbb3e6cSTejasree Kondoj 		key_type = ROC_SE_AES_192_BIT;
2330fbb3e6cSTejasree Kondoj 		break;
2340fbb3e6cSTejasree Kondoj 	case 32:
2350fbb3e6cSTejasree Kondoj 		key_type = ROC_SE_AES_256_BIT;
2360fbb3e6cSTejasree Kondoj 		break;
2370fbb3e6cSTejasree Kondoj 	default:
2380fbb3e6cSTejasree Kondoj 		plt_err("Invalid key len");
2390fbb3e6cSTejasree Kondoj 		return -ENOTSUP;
2400fbb3e6cSTejasree Kondoj 	}
2410fbb3e6cSTejasree Kondoj 
2420fbb3e6cSTejasree Kondoj 	return key_type;
2430fbb3e6cSTejasree Kondoj }
2440fbb3e6cSTejasree Kondoj 
24566a8a26fSVidya Sagar Velumuri static void
cpt_zuc_const_update(uint8_t * zuc_const,int key_len,int mac_len)2460fbb3e6cSTejasree Kondoj cpt_zuc_const_update(uint8_t *zuc_const, int key_len, int mac_len)
24766a8a26fSVidya Sagar Velumuri {
24866a8a26fSVidya Sagar Velumuri 	if (key_len == 16) {
24966a8a26fSVidya Sagar Velumuri 		memcpy(zuc_const, zuc_key128, 32);
25066a8a26fSVidya Sagar Velumuri 	} else if (key_len == 32) {
25166a8a26fSVidya Sagar Velumuri 		switch (mac_len) {
25266a8a26fSVidya Sagar Velumuri 		case 4:
25366a8a26fSVidya Sagar Velumuri 			memcpy(zuc_const, zuc_key256_mac4, 16);
25466a8a26fSVidya Sagar Velumuri 			break;
25566a8a26fSVidya Sagar Velumuri 		case 8:
25666a8a26fSVidya Sagar Velumuri 			memcpy(zuc_const, zuc_key256_mac8, 16);
25766a8a26fSVidya Sagar Velumuri 			break;
25866a8a26fSVidya Sagar Velumuri 		case 16:
25966a8a26fSVidya Sagar Velumuri 			memcpy(zuc_const, zuc_key256_mac16, 16);
26066a8a26fSVidya Sagar Velumuri 			break;
26166a8a26fSVidya Sagar Velumuri 		default:
26266a8a26fSVidya Sagar Velumuri 			plt_err("Unsupported mac len");
26366a8a26fSVidya Sagar Velumuri 		}
26466a8a26fSVidya Sagar Velumuri 	}
26566a8a26fSVidya Sagar Velumuri }
26666a8a26fSVidya Sagar Velumuri 
2675e076b60SAnkur Dwivedi int
roc_se_auth_key_set(struct roc_se_ctx * se_ctx,roc_se_auth_type type,const uint8_t * key,uint16_t key_len,uint16_t mac_len)2686855d510STejasree Kondoj roc_se_auth_key_set(struct roc_se_ctx *se_ctx, roc_se_auth_type type, const uint8_t *key,
2696855d510STejasree Kondoj 		    uint16_t key_len, uint16_t mac_len)
2705e076b60SAnkur Dwivedi {
2715e076b60SAnkur Dwivedi 	struct roc_se_kasumi_ctx *k_ctx;
2726855d510STejasree Kondoj 	struct roc_se_pdcp_ctx *pctx;
2735e076b60SAnkur Dwivedi 	struct roc_se_context *fctx;
2740fbb3e6cSTejasree Kondoj 	uint8_t opcode_minor;
2758b063580SVolodymyr Fialko 	bool chained_op;
2765e076b60SAnkur Dwivedi 
2775e076b60SAnkur Dwivedi 	if (se_ctx == NULL)
2785e076b60SAnkur Dwivedi 		return -1;
2795e076b60SAnkur Dwivedi 
2806855d510STejasree Kondoj 	pctx = &se_ctx->se_ctx.pctx;
2815e076b60SAnkur Dwivedi 	k_ctx = &se_ctx->se_ctx.k_ctx;
2825e076b60SAnkur Dwivedi 	fctx = &se_ctx->se_ctx.fctx;
2835e076b60SAnkur Dwivedi 
2848b063580SVolodymyr Fialko 	chained_op = se_ctx->ciph_then_auth || se_ctx->auth_then_ciph;
2858b063580SVolodymyr Fialko 
2865e076b60SAnkur Dwivedi 	if ((type >= ROC_SE_ZUC_EIA3) && (type <= ROC_SE_KASUMI_F9_ECB)) {
2875e076b60SAnkur Dwivedi 		uint32_t keyx[4];
2886855d510STejasree Kondoj 		int key_type;
2895e076b60SAnkur Dwivedi 
290a07d1d4dSVidya Sagar Velumuri 		if (!key_len)
2915e076b60SAnkur Dwivedi 			return -1;
292a07d1d4dSVidya Sagar Velumuri 
293b5c32495STejasree Kondoj 		if (se_ctx->fc_type == ROC_SE_FC_GEN) {
294b5c32495STejasree Kondoj 			plt_err("Cipher and Auth algorithm combination is not supported");
295b5c32495STejasree Kondoj 			return -1;
296b5c32495STejasree Kondoj 		}
297b5c32495STejasree Kondoj 
2985e076b60SAnkur Dwivedi 		/* For ZUC/SNOW3G/Kasumi */
2995e076b60SAnkur Dwivedi 		switch (type) {
3005e076b60SAnkur Dwivedi 		case ROC_SE_SNOW3G_UIA2:
3016855d510STejasree Kondoj 			pctx->w0.s.state_conf = ROC_SE_PDCP_CHAIN_CTX_KEY_IV;
3026855d510STejasree Kondoj 			pctx->w0.s.auth_type = ROC_SE_PDCP_CHAIN_ALG_TYPE_SNOW3G;
3036855d510STejasree Kondoj 			pctx->w0.s.mac_len = mac_len;
3046855d510STejasree Kondoj 			pctx->w0.s.auth_key_len = key_len;
3050fbb3e6cSTejasree Kondoj 			se_ctx->fc_type = ROC_SE_PDCP_CHAIN;
3060fbb3e6cSTejasree Kondoj 			cpt_snow3g_key_gen(key, keyx);
3076855d510STejasree Kondoj 			memcpy(pctx->st.auth_key, keyx, key_len);
3086855d510STejasree Kondoj 
3096855d510STejasree Kondoj 			if (!chained_op)
3105e076b60SAnkur Dwivedi 				se_ctx->fc_type = ROC_SE_PDCP;
3110fbb3e6cSTejasree Kondoj 			se_ctx->pdcp_auth_alg = ROC_SE_PDCP_ALG_TYPE_SNOW3G;
3125e076b60SAnkur Dwivedi 			se_ctx->zsk_flags = 0x1;
3135e076b60SAnkur Dwivedi 			break;
3145e076b60SAnkur Dwivedi 		case ROC_SE_ZUC_EIA3:
3156855d510STejasree Kondoj 			if (unlikely(key_len != 16)) {
3166855d510STejasree Kondoj 				/*
3176855d510STejasree Kondoj 				 * ZUC 256 is not supported with older microcode
3186855d510STejasree Kondoj 				 * where pdcp_iv_offset is 16
3196855d510STejasree Kondoj 				 */
3206855d510STejasree Kondoj 				if (chained_op || (se_ctx->pdcp_iv_offset == 16)) {
3216855d510STejasree Kondoj 					plt_err("ZUC 256 is not supported with chained operations");
3226855d510STejasree Kondoj 					return -1;
3230fbb3e6cSTejasree Kondoj 				}
3246855d510STejasree Kondoj 			}
3256855d510STejasree Kondoj 			key_type = cpt_pdcp_chain_key_type_get(key_len);
3266855d510STejasree Kondoj 			if (key_type < 0)
3276855d510STejasree Kondoj 				return key_type;
3286855d510STejasree Kondoj 			pctx->w0.s.auth_key_len = key_type;
3296855d510STejasree Kondoj 			pctx->w0.s.state_conf = ROC_SE_PDCP_CHAIN_CTX_KEY_IV;
3306855d510STejasree Kondoj 			pctx->w0.s.auth_type = ROC_SE_PDCP_CHAIN_ALG_TYPE_ZUC;
3316855d510STejasree Kondoj 			pctx->w0.s.mac_len = mac_len;
3326855d510STejasree Kondoj 			memcpy(pctx->st.auth_key, key, key_len);
3336855d510STejasree Kondoj 			if (key_len == 32)
3346855d510STejasree Kondoj 				roc_se_zuc_bytes_swap(pctx->st.auth_key, key_len);
3356855d510STejasree Kondoj 			cpt_zuc_const_update(pctx->st.auth_zuc_const, key_len, mac_len);
3366855d510STejasree Kondoj 			se_ctx->fc_type = ROC_SE_PDCP_CHAIN;
3376855d510STejasree Kondoj 
3386855d510STejasree Kondoj 			if (!chained_op)
3396855d510STejasree Kondoj 				se_ctx->fc_type = ROC_SE_PDCP;
3400fbb3e6cSTejasree Kondoj 			se_ctx->pdcp_auth_alg = ROC_SE_PDCP_ALG_TYPE_ZUC;
3415e076b60SAnkur Dwivedi 			se_ctx->zsk_flags = 0x1;
3425e076b60SAnkur Dwivedi 			break;
3435e076b60SAnkur Dwivedi 		case ROC_SE_AES_CMAC_EIA2:
3440fbb3e6cSTejasree Kondoj 			key_type = cpt_pdcp_chain_key_type_get(key_len);
3450fbb3e6cSTejasree Kondoj 			if (key_type < 0)
3460fbb3e6cSTejasree Kondoj 				return key_type;
3476855d510STejasree Kondoj 			pctx->w0.s.auth_key_len = key_type;
3486855d510STejasree Kondoj 			pctx->w0.s.state_conf = ROC_SE_PDCP_CHAIN_CTX_KEY_IV;
3496855d510STejasree Kondoj 			pctx->w0.s.auth_type = ROC_SE_PDCP_ALG_TYPE_AES_CTR;
3506855d510STejasree Kondoj 			pctx->w0.s.mac_len = mac_len;
3516855d510STejasree Kondoj 			memcpy(pctx->st.auth_key, key, key_len);
3520fbb3e6cSTejasree Kondoj 			se_ctx->fc_type = ROC_SE_PDCP_CHAIN;
3536855d510STejasree Kondoj 
3546855d510STejasree Kondoj 			if (!chained_op)
3555e076b60SAnkur Dwivedi 				se_ctx->fc_type = ROC_SE_PDCP;
3560fbb3e6cSTejasree Kondoj 			se_ctx->pdcp_auth_alg = ROC_SE_PDCP_ALG_TYPE_AES_CMAC;
357e332f197STejasree Kondoj 			se_ctx->eia2 = 1;
3585e076b60SAnkur Dwivedi 			se_ctx->zsk_flags = 0x1;
3595e076b60SAnkur Dwivedi 			break;
3605e076b60SAnkur Dwivedi 		case ROC_SE_KASUMI_F9_ECB:
3615e076b60SAnkur Dwivedi 			/* Kasumi ECB mode */
3625e076b60SAnkur Dwivedi 			se_ctx->k_ecb = 1;
3635e076b60SAnkur Dwivedi 			memcpy(k_ctx->ci_key, key, key_len);
3645e076b60SAnkur Dwivedi 			se_ctx->fc_type = ROC_SE_KASUMI;
3655e076b60SAnkur Dwivedi 			se_ctx->zsk_flags = 0x1;
3665e076b60SAnkur Dwivedi 			break;
3675e076b60SAnkur Dwivedi 		case ROC_SE_KASUMI_F9_CBC:
3685e076b60SAnkur Dwivedi 			memcpy(k_ctx->ci_key, key, key_len);
3695e076b60SAnkur Dwivedi 			se_ctx->fc_type = ROC_SE_KASUMI;
3705e076b60SAnkur Dwivedi 			se_ctx->zsk_flags = 0x1;
3715e076b60SAnkur Dwivedi 			break;
3725e076b60SAnkur Dwivedi 		default:
3735e076b60SAnkur Dwivedi 			return -1;
3745e076b60SAnkur Dwivedi 		}
375e332f197STejasree Kondoj 
376e332f197STejasree Kondoj 		if ((se_ctx->fc_type == ROC_SE_PDCP_CHAIN) && (mac_len != 4)) {
377e332f197STejasree Kondoj 			plt_err("Only digest length of 4 is supported with PDCP chain");
378e332f197STejasree Kondoj 			return -1;
379e332f197STejasree Kondoj 		}
380e332f197STejasree Kondoj 
381a07d1d4dSVidya Sagar Velumuri 		se_ctx->mac_len = mac_len;
3825e076b60SAnkur Dwivedi 		se_ctx->hash_type = type;
383e332f197STejasree Kondoj 		if (chained_op)
3840fbb3e6cSTejasree Kondoj 			opcode_minor = se_ctx->ciph_then_auth ? 2 : 3;
3850fbb3e6cSTejasree Kondoj 		else
3860fbb3e6cSTejasree Kondoj 			opcode_minor = ((1 << 4) | 1);
3870fbb3e6cSTejasree Kondoj 
3880fbb3e6cSTejasree Kondoj 		se_ctx->template_w4.s.opcode_minor = opcode_minor;
3895e076b60SAnkur Dwivedi 		return 0;
3905e076b60SAnkur Dwivedi 	}
3915e076b60SAnkur Dwivedi 
392b5c32495STejasree Kondoj 	if (!se_ctx->fc_type || (type && type != ROC_SE_GMAC_TYPE && !se_ctx->enc_cipher))
3935e076b60SAnkur Dwivedi 		se_ctx->fc_type = ROC_SE_HASH_HMAC;
3945e076b60SAnkur Dwivedi 
395b5c32495STejasree Kondoj 	if (se_ctx->fc_type == ROC_SE_FC_GEN && key_len > 64) {
396b5c32495STejasree Kondoj 		plt_err("Maximum auth key length supported is 64");
3975e076b60SAnkur Dwivedi 		return -1;
398b5c32495STejasree Kondoj 	}
3995e076b60SAnkur Dwivedi 
4005e076b60SAnkur Dwivedi 	/* For GMAC auth, cipher must be NULL */
4019242aa77STejasree Kondoj 	if (type == ROC_SE_GMAC_TYPE) {
4025e076b60SAnkur Dwivedi 		fctx->enc.enc_cipher = 0;
4039242aa77STejasree Kondoj 		se_ctx->template_w4.s.opcode_minor = BIT(5);
4049242aa77STejasree Kondoj 	}
4055e076b60SAnkur Dwivedi 
4065e076b60SAnkur Dwivedi 	fctx->enc.hash_type = type;
4075e076b60SAnkur Dwivedi 	se_ctx->hash_type = type;
4085e076b60SAnkur Dwivedi 	fctx->enc.mac_len = mac_len;
4095e076b60SAnkur Dwivedi 	se_ctx->mac_len = mac_len;
4105e076b60SAnkur Dwivedi 
4115e076b60SAnkur Dwivedi 	if (key_len) {
4128b063580SVolodymyr Fialko 		/*
4138b063580SVolodymyr Fialko 		 * Chained operation (FC opcode) requires precomputed ipad and opad hashes, but for
4148b063580SVolodymyr Fialko 		 * auth only (HMAC opcode) this is not required
4158b063580SVolodymyr Fialko 		 */
4168b063580SVolodymyr Fialko 		if (chained_op) {
4178b063580SVolodymyr Fialko 			memset(fctx->hmac.ipad, 0, sizeof(fctx->hmac.ipad));
4188b063580SVolodymyr Fialko 			memset(fctx->hmac.opad, 0, sizeof(fctx->hmac.opad));
419*df22fe29SVidya Sagar Velumuri 			roc_se_hmac_opad_ipad_gen(type, key, key_len, &fctx->hmac.ipad[0],
420*df22fe29SVidya Sagar Velumuri 						  ROC_SE_FC);
4218b063580SVolodymyr Fialko 			fctx->enc.auth_input_type = 0;
4228b063580SVolodymyr Fialko 		} else {
4235e076b60SAnkur Dwivedi 			se_ctx->hmac = 1;
424252947b9SAnoob Joseph 
425252947b9SAnoob Joseph 			se_ctx->auth_key = plt_zmalloc(key_len, 8);
426252947b9SAnoob Joseph 			if (se_ctx->auth_key == NULL)
427252947b9SAnoob Joseph 				return -1;
428252947b9SAnoob Joseph 
4295e076b60SAnkur Dwivedi 			memcpy(se_ctx->auth_key, key, key_len);
4305e076b60SAnkur Dwivedi 			se_ctx->auth_key_len = key_len;
4318b063580SVolodymyr Fialko 		}
4325e076b60SAnkur Dwivedi 	}
4335e076b60SAnkur Dwivedi 	return 0;
4345e076b60SAnkur Dwivedi }
4355e076b60SAnkur Dwivedi 
4365e076b60SAnkur Dwivedi int
roc_se_ciph_key_set(struct roc_se_ctx * se_ctx,roc_se_cipher_type type,const uint8_t * key,uint16_t key_len)437aa2478adSAnoob Joseph roc_se_ciph_key_set(struct roc_se_ctx *se_ctx, roc_se_cipher_type type, const uint8_t *key,
438aa2478adSAnoob Joseph 		    uint16_t key_len)
4395e076b60SAnkur Dwivedi {
4405e076b60SAnkur Dwivedi 	struct roc_se_context *fctx = &se_ctx->se_ctx.fctx;
4416855d510STejasree Kondoj 	struct roc_se_pdcp_ctx *pctx;
442e332f197STejasree Kondoj 	uint8_t opcode_minor = 0;
4435e076b60SAnkur Dwivedi 	uint32_t keyx[4];
4446855d510STejasree Kondoj 	int key_type;
44587d39616SVolodymyr Fialko 	int i, ret;
4465e076b60SAnkur Dwivedi 
4477a2f9b06SAnoob Joseph 	/* For NULL cipher, no processing required. */
4487a2f9b06SAnoob Joseph 	if (type == ROC_SE_PASSTHROUGH)
4497a2f9b06SAnoob Joseph 		return 0;
4507a2f9b06SAnoob Joseph 
4516855d510STejasree Kondoj 	pctx = &se_ctx->se_ctx.pctx;
452a07d1d4dSVidya Sagar Velumuri 
453d15bc634STejasree Kondoj 	if ((type == ROC_SE_AES_GCM) || (type == ROC_SE_AES_CCM))
4549242aa77STejasree Kondoj 		se_ctx->template_w4.s.opcode_minor = BIT(5);
4559242aa77STejasree Kondoj 
4565e076b60SAnkur Dwivedi 	ret = cpt_ciph_type_set(type, se_ctx, key_len);
4575e076b60SAnkur Dwivedi 	if (unlikely(ret))
4585e076b60SAnkur Dwivedi 		return -1;
4595e076b60SAnkur Dwivedi 
4605e076b60SAnkur Dwivedi 	if (se_ctx->fc_type == ROC_SE_FC_GEN) {
4615e076b60SAnkur Dwivedi 		/*
4625e076b60SAnkur Dwivedi 		 * We need to always say IV is from DPTR as user can
4638dbdbee2STomasz Duszynski 		 * sometimes override IV per operation.
4645e076b60SAnkur Dwivedi 		 */
4655e076b60SAnkur Dwivedi 		fctx->enc.iv_source = ROC_SE_FROM_DPTR;
4665e076b60SAnkur Dwivedi 
4675e076b60SAnkur Dwivedi 		if (se_ctx->auth_key_len > 64)
4685e076b60SAnkur Dwivedi 			return -1;
4695e076b60SAnkur Dwivedi 	}
4705e076b60SAnkur Dwivedi 
4715e076b60SAnkur Dwivedi 	switch (type) {
4725e076b60SAnkur Dwivedi 	case ROC_SE_DES3_CBC:
4735e076b60SAnkur Dwivedi 		/* CPT performs DES using 3DES with the 8B DES-key
4745e076b60SAnkur Dwivedi 		 * replicated 2 more times to match the 24B 3DES-key.
4755e076b60SAnkur Dwivedi 		 * Eg. If org. key is "0x0a 0x0b", then new key is
4765e076b60SAnkur Dwivedi 		 * "0x0a 0x0b 0x0a 0x0b 0x0a 0x0b"
4775e076b60SAnkur Dwivedi 		 */
4785e076b60SAnkur Dwivedi 		if (key_len == 8) {
4795e076b60SAnkur Dwivedi 			/* Skipping the first 8B as it will be copied
4805e076b60SAnkur Dwivedi 			 * in the regular code flow
4815e076b60SAnkur Dwivedi 			 */
4825e076b60SAnkur Dwivedi 			memcpy(fctx->enc.encr_key + key_len, key, key_len);
4835e076b60SAnkur Dwivedi 			memcpy(fctx->enc.encr_key + 2 * key_len, key, key_len);
4845e076b60SAnkur Dwivedi 		}
4855e076b60SAnkur Dwivedi 		break;
4865e076b60SAnkur Dwivedi 	case ROC_SE_DES3_ECB:
4875e076b60SAnkur Dwivedi 		/* For DES3_ECB IV need to be from CTX. */
4885e076b60SAnkur Dwivedi 		fctx->enc.iv_source = ROC_SE_FROM_CTX;
4895e076b60SAnkur Dwivedi 		break;
4905e076b60SAnkur Dwivedi 	case ROC_SE_AES_CBC:
4915e076b60SAnkur Dwivedi 	case ROC_SE_AES_ECB:
4925e076b60SAnkur Dwivedi 	case ROC_SE_AES_CFB:
4935e076b60SAnkur Dwivedi 	case ROC_SE_AES_CTR:
4945e076b60SAnkur Dwivedi 	case ROC_SE_CHACHA20:
4955e076b60SAnkur Dwivedi 		cpt_ciph_aes_key_type_set(fctx, key_len);
4965e076b60SAnkur Dwivedi 		break;
4975e076b60SAnkur Dwivedi 	case ROC_SE_AES_GCM:
498d15bc634STejasree Kondoj 	case ROC_SE_AES_CCM:
4995e076b60SAnkur Dwivedi 		cpt_ciph_aes_key_type_set(fctx, key_len);
5005e076b60SAnkur Dwivedi 		break;
5015e076b60SAnkur Dwivedi 	case ROC_SE_AES_XTS:
5025e076b60SAnkur Dwivedi 		key_len = key_len / 2;
5035e076b60SAnkur Dwivedi 		cpt_ciph_aes_key_type_set(fctx, key_len);
5045e076b60SAnkur Dwivedi 
5055e076b60SAnkur Dwivedi 		/* Copy key2 for XTS into ipad */
5065e076b60SAnkur Dwivedi 		memset(fctx->hmac.ipad, 0, sizeof(fctx->hmac.ipad));
5075e076b60SAnkur Dwivedi 		memcpy(fctx->hmac.ipad, &key[key_len], key_len);
5085e076b60SAnkur Dwivedi 		break;
50987d39616SVolodymyr Fialko 	case ROC_SE_AES_DOCSISBPI:
51087d39616SVolodymyr Fialko 		/*
51187d39616SVolodymyr Fialko 		 * DOCSIS uses the combination of AES-CBC and residual termination blocks that are
51287d39616SVolodymyr Fialko 		 * less than 128. Pass it as regular AES-CBC cipher to CPT, but keep type in
51387d39616SVolodymyr Fialko 		 * se_ctx as AES_DOCSISBPI to skip block size checks in instruction preparation.
51487d39616SVolodymyr Fialko 		 */
51587d39616SVolodymyr Fialko 		cpt_ciph_aes_key_type_set(fctx, key_len);
51687d39616SVolodymyr Fialko 		fctx->enc.enc_cipher = ROC_SE_AES_CBC;
51787d39616SVolodymyr Fialko 		memcpy(fctx->enc.encr_key, key, key_len);
51887d39616SVolodymyr Fialko 		goto success;
51987d39616SVolodymyr Fialko 	case ROC_SE_DES_DOCSISBPI:
52087d39616SVolodymyr Fialko 		/* See case ROC_SE_DES3_CBC: for explanation */
52187d39616SVolodymyr Fialko 		for (i = 0; i < 3; i++)
52287d39616SVolodymyr Fialko 			memcpy(fctx->enc.encr_key + key_len * i, key, key_len);
52387d39616SVolodymyr Fialko 		/*
52487d39616SVolodymyr Fialko 		 * DOCSIS uses DES-CBC mode with special handling of residual termination blocks
52587d39616SVolodymyr Fialko 		 * that are less than 64 bits. Pass it as regular DES-CBC, but keep type in
52687d39616SVolodymyr Fialko 		 * se_ctx as DES_DOCSISBPI to skip block size checks in instruction preparation.
52787d39616SVolodymyr Fialko 		 */
52887d39616SVolodymyr Fialko 		fctx->enc.enc_cipher = ROC_SE_DES3_CBC;
52987d39616SVolodymyr Fialko 		goto success;
5305e076b60SAnkur Dwivedi 	case ROC_SE_SNOW3G_UEA2:
5316855d510STejasree Kondoj 		pctx->w0.s.state_conf = ROC_SE_PDCP_CHAIN_CTX_KEY_IV;
5326855d510STejasree Kondoj 		pctx->w0.s.cipher_type = ROC_SE_PDCP_CHAIN_ALG_TYPE_SNOW3G;
5336855d510STejasree Kondoj 		pctx->w0.s.ci_key_len = key_len;
5340fbb3e6cSTejasree Kondoj 		cpt_snow3g_key_gen(key, keyx);
5356855d510STejasree Kondoj 		memcpy(pctx->st.ci_key, keyx, key_len);
5360fbb3e6cSTejasree Kondoj 		se_ctx->pdcp_ci_alg = ROC_SE_PDCP_ALG_TYPE_SNOW3G;
5375e076b60SAnkur Dwivedi 		se_ctx->zsk_flags = 0;
5385e076b60SAnkur Dwivedi 		goto success;
5395e076b60SAnkur Dwivedi 	case ROC_SE_ZUC_EEA3:
5406855d510STejasree Kondoj 		key_type = cpt_pdcp_chain_key_type_get(key_len);
5416855d510STejasree Kondoj 		if (key_type < 0)
5426855d510STejasree Kondoj 			return key_type;
5436855d510STejasree Kondoj 		pctx->w0.s.ci_key_len = key_type;
5446855d510STejasree Kondoj 		pctx->w0.s.state_conf = ROC_SE_PDCP_CHAIN_CTX_KEY_IV;
5456855d510STejasree Kondoj 		pctx->w0.s.cipher_type = ROC_SE_PDCP_CHAIN_ALG_TYPE_ZUC;
5466855d510STejasree Kondoj 		memcpy(pctx->st.ci_key, key, key_len);
5475242d8dbSAnkur Dwivedi 		if (key_len == 32) {
5486855d510STejasree Kondoj 			roc_se_zuc_bytes_swap(pctx->st.ci_key, key_len);
5496855d510STejasree Kondoj 			memcpy(pctx->st.ci_zuc_const, zuc_key256, 16);
5505242d8dbSAnkur Dwivedi 		} else
5516855d510STejasree Kondoj 			memcpy(pctx->st.ci_zuc_const, zuc_key128, 32);
5520fbb3e6cSTejasree Kondoj 		se_ctx->pdcp_ci_alg = ROC_SE_PDCP_ALG_TYPE_ZUC;
5535e076b60SAnkur Dwivedi 		se_ctx->zsk_flags = 0;
5545e076b60SAnkur Dwivedi 		goto success;
5555e076b60SAnkur Dwivedi 	case ROC_SE_AES_CTR_EEA2:
5560fbb3e6cSTejasree Kondoj 		key_type = cpt_pdcp_chain_key_type_get(key_len);
5570fbb3e6cSTejasree Kondoj 		if (key_type < 0)
5580fbb3e6cSTejasree Kondoj 			return key_type;
5596855d510STejasree Kondoj 		pctx->w0.s.ci_key_len = key_type;
5606855d510STejasree Kondoj 		pctx->w0.s.state_conf = ROC_SE_PDCP_CHAIN_CTX_KEY_IV;
5616855d510STejasree Kondoj 		pctx->w0.s.cipher_type = ROC_SE_PDCP_ALG_TYPE_AES_CTR;
5626855d510STejasree Kondoj 		memcpy(pctx->st.ci_key, key, key_len);
5630fbb3e6cSTejasree Kondoj 		se_ctx->pdcp_ci_alg = ROC_SE_PDCP_ALG_TYPE_AES_CTR;
5645e076b60SAnkur Dwivedi 		se_ctx->zsk_flags = 0;
5655e076b60SAnkur Dwivedi 		goto success;
5665e076b60SAnkur Dwivedi 	case ROC_SE_KASUMI_F8_ECB:
5675e076b60SAnkur Dwivedi 		se_ctx->k_ecb = 1;
5685e076b60SAnkur Dwivedi 		memcpy(se_ctx->se_ctx.k_ctx.ci_key, key, key_len);
5695e076b60SAnkur Dwivedi 		se_ctx->zsk_flags = 0;
5705e076b60SAnkur Dwivedi 		goto success;
5715e076b60SAnkur Dwivedi 	case ROC_SE_KASUMI_F8_CBC:
5725e076b60SAnkur Dwivedi 		memcpy(se_ctx->se_ctx.k_ctx.ci_key, key, key_len);
5735e076b60SAnkur Dwivedi 		se_ctx->zsk_flags = 0;
5745e076b60SAnkur Dwivedi 		goto success;
5755e076b60SAnkur Dwivedi 	default:
5765e076b60SAnkur Dwivedi 		return -1;
5775e076b60SAnkur Dwivedi 	}
5785e076b60SAnkur Dwivedi 
5795e076b60SAnkur Dwivedi 	/* Only for ROC_SE_FC_GEN case */
5805e076b60SAnkur Dwivedi 
5815e076b60SAnkur Dwivedi 	/* For GMAC auth, cipher must be NULL */
5825e076b60SAnkur Dwivedi 	if (se_ctx->hash_type != ROC_SE_GMAC_TYPE)
5835e076b60SAnkur Dwivedi 		fctx->enc.enc_cipher = type;
5845e076b60SAnkur Dwivedi 
5855e076b60SAnkur Dwivedi 	memcpy(fctx->enc.encr_key, key, key_len);
5865e076b60SAnkur Dwivedi 
5875e076b60SAnkur Dwivedi success:
5885e076b60SAnkur Dwivedi 	se_ctx->enc_cipher = type;
589e332f197STejasree Kondoj 	if (se_ctx->fc_type == ROC_SE_PDCP_CHAIN) {
590e332f197STejasree Kondoj 		se_ctx->template_w4.s.opcode_minor = se_ctx->ciph_then_auth ? 2 : 3;
591e332f197STejasree Kondoj 	} else if (se_ctx->fc_type == ROC_SE_PDCP) {
592a07d1d4dSVidya Sagar Velumuri 		if (roc_model_is_cn9k())
5930fbb3e6cSTejasree Kondoj 			opcode_minor =
594e332f197STejasree Kondoj 				((1 << 7) | (se_ctx->pdcp_ci_alg << 5) | (se_ctx->zsk_flags & 0x7));
595a07d1d4dSVidya Sagar Velumuri 		else
5960fbb3e6cSTejasree Kondoj 			opcode_minor = ((1 << 4));
5970fbb3e6cSTejasree Kondoj 		se_ctx->template_w4.s.opcode_minor = opcode_minor;
598a07d1d4dSVidya Sagar Velumuri 	}
5995e076b60SAnkur Dwivedi 	return 0;
6005e076b60SAnkur Dwivedi }
601a07d1d4dSVidya Sagar Velumuri 
602a07d1d4dSVidya Sagar Velumuri void
roc_se_ctx_init(struct roc_se_ctx * roc_se_ctx)6032cbfe3d5STejasree Kondoj roc_se_ctx_init(struct roc_se_ctx *roc_se_ctx)
6042cbfe3d5STejasree Kondoj {
6052cbfe3d5STejasree Kondoj 	struct se_ctx_s *ctx = &roc_se_ctx->se_ctx;
6062cbfe3d5STejasree Kondoj 	uint64_t ctx_len, *uc_ctx;
6072cbfe3d5STejasree Kondoj 	uint8_t i;
6082cbfe3d5STejasree Kondoj 
6092cbfe3d5STejasree Kondoj 	switch (roc_se_ctx->fc_type) {
6102cbfe3d5STejasree Kondoj 	case ROC_SE_FC_GEN:
6112cbfe3d5STejasree Kondoj 		ctx_len = sizeof(struct roc_se_context);
6122cbfe3d5STejasree Kondoj 		break;
6136855d510STejasree Kondoj 	case ROC_SE_PDCP_CHAIN:
6142cbfe3d5STejasree Kondoj 	case ROC_SE_PDCP:
6156855d510STejasree Kondoj 		ctx_len = sizeof(struct roc_se_pdcp_ctx);
6162cbfe3d5STejasree Kondoj 		break;
6172cbfe3d5STejasree Kondoj 	case ROC_SE_KASUMI:
6182cbfe3d5STejasree Kondoj 		ctx_len = sizeof(struct roc_se_kasumi_ctx);
6192cbfe3d5STejasree Kondoj 		break;
62021c1c303SVidya Sagar Velumuri 	case ROC_SE_SM:
62121c1c303SVidya Sagar Velumuri 		ctx_len = sizeof(struct roc_se_sm_context);
62221c1c303SVidya Sagar Velumuri 		break;
6232cbfe3d5STejasree Kondoj 	default:
6242cbfe3d5STejasree Kondoj 		ctx_len = 0;
6252cbfe3d5STejasree Kondoj 	}
6262cbfe3d5STejasree Kondoj 
6272cbfe3d5STejasree Kondoj 	ctx_len = PLT_ALIGN_CEIL(ctx_len, 8);
6282cbfe3d5STejasree Kondoj 
6292cbfe3d5STejasree Kondoj 	/* Skip w0 for swap */
6302cbfe3d5STejasree Kondoj 	uc_ctx = PLT_PTR_ADD(ctx, sizeof(ctx->w0));
6312cbfe3d5STejasree Kondoj 	for (i = 0; i < (ctx_len / 8); i++)
6322cbfe3d5STejasree Kondoj 		uc_ctx[i] = plt_cpu_to_be_64(((uint64_t *)uc_ctx)[i]);
6332cbfe3d5STejasree Kondoj 
6342cbfe3d5STejasree Kondoj 	/* Include w0 */
6352cbfe3d5STejasree Kondoj 	ctx_len += sizeof(ctx->w0);
6362cbfe3d5STejasree Kondoj 	ctx_len = PLT_ALIGN_CEIL(ctx_len, 8);
6372cbfe3d5STejasree Kondoj 
6382cbfe3d5STejasree Kondoj 	ctx->w0.s.aop_valid = 1;
6392cbfe3d5STejasree Kondoj 	ctx->w0.s.ctx_hdr_size = 0;
6402cbfe3d5STejasree Kondoj 
6412cbfe3d5STejasree Kondoj 	ctx->w0.s.ctx_size = PLT_ALIGN_FLOOR(ctx_len, 128);
6422cbfe3d5STejasree Kondoj 	if (ctx->w0.s.ctx_size == 0)
6432cbfe3d5STejasree Kondoj 		ctx->w0.s.ctx_size = 1;
6442cbfe3d5STejasree Kondoj 
6452cbfe3d5STejasree Kondoj 	ctx->w0.s.ctx_push_size = ctx_len / 8;
6462cbfe3d5STejasree Kondoj 	if (ctx->w0.s.ctx_push_size > 32)
6472cbfe3d5STejasree Kondoj 		ctx->w0.s.ctx_push_size = 32;
6482cbfe3d5STejasree Kondoj }
649