1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2021 Marvell.
3 */
4
5 #include "roc_api.h"
6
7 static uint8_t zuc_key128[32] = {
8 0x44, 0xD7, 0x26, 0xBC, 0x62, 0x6B, 0x13, 0x5E, 0x57, 0x89, 0x35,
9 0xE2, 0x71, 0x35, 0x09, 0xAF, 0x4D, 0x78, 0x2F, 0x13, 0x6B, 0xC4,
10 0x1A, 0xF1, 0x5E, 0x26, 0x3C, 0x4D, 0x78, 0x9A, 0x47, 0xAC};
11
12 static uint8_t zuc_key256[16] = {0x22, 0x2f, 0x24, 0x2a, 0x6d, 0x40,
13 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
14 0x40, 0x52, 0x10, 0x30};
15
16 static uint8_t zuc_key256_mac4[16] = {0x22, 0x2f, 0x25, 0x2a, 0x6d, 0x40,
17 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
18 0x40, 0x52, 0x10, 0x30};
19
20 static uint8_t zuc_key256_mac8[16] = {0x23, 0x2f, 0x24, 0x2a, 0x6d, 0x40,
21 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
22 0x40, 0x52, 0x10, 0x30};
23
24 static uint8_t zuc_key256_mac16[16] = {0x23, 0x2f, 0x25, 0x2a, 0x6d, 0x40,
25 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
26 0x40, 0x52, 0x10, 0x30};
27
28 static inline void
cpt_snow3g_key_gen(const uint8_t * ck,uint32_t * keyx)29 cpt_snow3g_key_gen(const uint8_t *ck, uint32_t *keyx)
30 {
31 int i, base;
32
33 for (i = 0; i < 4; i++) {
34 base = 4 * i;
35 keyx[3 - i] = (ck[base] << 24) | (ck[base + 1] << 16) |
36 (ck[base + 2] << 8) | (ck[base + 3]);
37 keyx[3 - i] = plt_cpu_to_be_32(keyx[3 - i]);
38 }
39 }
40
41 static inline int
cpt_ciph_aes_key_validate(uint16_t key_len)42 cpt_ciph_aes_key_validate(uint16_t key_len)
43 {
44 switch (key_len) {
45 case 16:
46 case 24:
47 case 32:
48 return 0;
49 default:
50 return -1;
51 }
52 }
53
54 static inline int
cpt_ciph_type_set(roc_se_cipher_type type,struct roc_se_ctx * ctx,uint16_t key_len)55 cpt_ciph_type_set(roc_se_cipher_type type, struct roc_se_ctx *ctx, uint16_t key_len)
56 {
57 bool chained_op = ctx->ciph_then_auth || ctx->auth_then_ciph;
58 int fc_type = 0;
59
60 switch (type) {
61 case ROC_SE_DES3_CBC:
62 case ROC_SE_DES3_ECB:
63 case ROC_SE_DES_DOCSISBPI:
64 fc_type = ROC_SE_FC_GEN;
65 break;
66 case ROC_SE_AES_CBC:
67 case ROC_SE_AES_ECB:
68 case ROC_SE_AES_CFB:
69 case ROC_SE_AES_CTR:
70 case ROC_SE_AES_GCM:
71 case ROC_SE_AES_CCM:
72 case ROC_SE_AES_DOCSISBPI:
73 if (unlikely(cpt_ciph_aes_key_validate(key_len) != 0))
74 return -1;
75 fc_type = ROC_SE_FC_GEN;
76 break;
77 case ROC_SE_CHACHA20:
78 fc_type = ROC_SE_FC_GEN;
79 break;
80 case ROC_SE_AES_XTS:
81 key_len = key_len / 2;
82 if (unlikely(key_len == 24)) {
83 plt_err("Invalid AES key len for XTS");
84 return -1;
85 }
86 if (unlikely(cpt_ciph_aes_key_validate(key_len) != 0))
87 return -1;
88 fc_type = ROC_SE_FC_GEN;
89 break;
90 case ROC_SE_ZUC_EEA3:
91 if (unlikely(key_len != 16)) {
92 /*
93 * ZUC 256 is not supported with older microcode
94 * where pdcp_iv_offset is 16
95 */
96 if (chained_op || (ctx->pdcp_iv_offset == 16)) {
97 plt_err("ZUC 256 is not supported with chained operations");
98 return -1;
99 }
100 }
101 if (chained_op)
102 fc_type = ROC_SE_PDCP_CHAIN;
103 else
104 fc_type = ROC_SE_PDCP;
105 break;
106 case ROC_SE_SNOW3G_UEA2:
107 if (unlikely(key_len != 16))
108 return -1;
109 if (chained_op)
110 fc_type = ROC_SE_PDCP_CHAIN;
111 else
112 fc_type = ROC_SE_PDCP;
113 break;
114 case ROC_SE_AES_CTR_EEA2:
115 if (chained_op)
116 fc_type = ROC_SE_PDCP_CHAIN;
117 else
118 fc_type = ROC_SE_PDCP;
119 break;
120 case ROC_SE_KASUMI_F8_CBC:
121 case ROC_SE_KASUMI_F8_ECB:
122 if (unlikely(key_len != 16))
123 return -1;
124 /* No support for AEAD yet */
125 if (unlikely(ctx->hash_type))
126 return -1;
127 fc_type = ROC_SE_KASUMI;
128 break;
129 default:
130 return -1;
131 }
132
133 ctx->fc_type = fc_type;
134 return 0;
135 }
136
137 static inline void
cpt_ciph_aes_key_type_set(struct roc_se_context * fctx,uint16_t key_len)138 cpt_ciph_aes_key_type_set(struct roc_se_context *fctx, uint16_t key_len)
139 {
140 roc_se_aes_type aes_key_type = 0;
141
142 switch (key_len) {
143 case 16:
144 aes_key_type = ROC_SE_AES_128_BIT;
145 break;
146 case 24:
147 aes_key_type = ROC_SE_AES_192_BIT;
148 break;
149 case 32:
150 aes_key_type = ROC_SE_AES_256_BIT;
151 break;
152 default:
153 /* This should not happen */
154 plt_err("Invalid AES key len");
155 return;
156 }
157 fctx->enc.aes_key = aes_key_type;
158 }
159
160 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 roc_se_hmac_opad_ipad_gen(roc_se_auth_type auth_type, const uint8_t *key, uint16_t length,
162 uint8_t *opad_ipad, roc_se_op_type op_type)
163 {
164 uint8_t opad[128] = {[0 ... 127] = 0x5c};
165 uint8_t ipad[128] = {[0 ... 127] = 0x36};
166 uint8_t ipad_offset, opad_offset;
167 uint32_t i;
168
169 if (op_type == ROC_SE_IPSEC) {
170 if ((auth_type == ROC_SE_MD5_TYPE) || (auth_type == ROC_SE_SHA1_TYPE))
171 ipad_offset = 24;
172 else
173 ipad_offset = 64;
174 opad_offset = 0;
175 } else if (op_type == ROC_SE_TLS) {
176 ipad_offset = 64;
177 opad_offset = 0;
178 } else {
179 ipad_offset = 0;
180 opad_offset = 64;
181 }
182
183 /* HMAC OPAD and IPAD */
184 for (i = 0; i < 128 && i < length; i++) {
185 opad[i] = opad[i] ^ key[i];
186 ipad[i] = ipad[i] ^ key[i];
187 }
188
189 /* Precompute hash of HMAC OPAD and IPAD to avoid
190 * per packet computation
191 */
192 switch (auth_type) {
193 case ROC_SE_MD5_TYPE:
194 roc_hash_md5_gen(opad, (uint32_t *)&opad_ipad[opad_offset]);
195 roc_hash_md5_gen(ipad, (uint32_t *)&opad_ipad[ipad_offset]);
196 break;
197 case ROC_SE_SHA1_TYPE:
198 roc_hash_sha1_gen(opad, (uint32_t *)&opad_ipad[opad_offset]);
199 roc_hash_sha1_gen(ipad, (uint32_t *)&opad_ipad[ipad_offset]);
200 break;
201 case ROC_SE_SHA2_SHA224:
202 roc_hash_sha256_gen(opad, (uint32_t *)&opad_ipad[opad_offset], 224);
203 roc_hash_sha256_gen(ipad, (uint32_t *)&opad_ipad[ipad_offset], 224);
204 break;
205 case ROC_SE_SHA2_SHA256:
206 roc_hash_sha256_gen(opad, (uint32_t *)&opad_ipad[opad_offset], 256);
207 roc_hash_sha256_gen(ipad, (uint32_t *)&opad_ipad[ipad_offset], 256);
208 break;
209 case ROC_SE_SHA2_SHA384:
210 roc_hash_sha512_gen(opad, (uint64_t *)&opad_ipad[opad_offset], 384);
211 roc_hash_sha512_gen(ipad, (uint64_t *)&opad_ipad[ipad_offset], 384);
212 break;
213 case ROC_SE_SHA2_SHA512:
214 roc_hash_sha512_gen(opad, (uint64_t *)&opad_ipad[opad_offset], 512);
215 roc_hash_sha512_gen(ipad, (uint64_t *)&opad_ipad[ipad_offset], 512);
216 break;
217 default:
218 break;
219 }
220 }
221
222 static int
cpt_pdcp_chain_key_type_get(uint16_t key_len)223 cpt_pdcp_chain_key_type_get(uint16_t key_len)
224 {
225 roc_se_aes_type key_type;
226
227 switch (key_len) {
228 case 16:
229 key_type = ROC_SE_AES_128_BIT;
230 break;
231 case 24:
232 key_type = ROC_SE_AES_192_BIT;
233 break;
234 case 32:
235 key_type = ROC_SE_AES_256_BIT;
236 break;
237 default:
238 plt_err("Invalid key len");
239 return -ENOTSUP;
240 }
241
242 return key_type;
243 }
244
245 static void
cpt_zuc_const_update(uint8_t * zuc_const,int key_len,int mac_len)246 cpt_zuc_const_update(uint8_t *zuc_const, int key_len, int mac_len)
247 {
248 if (key_len == 16) {
249 memcpy(zuc_const, zuc_key128, 32);
250 } else if (key_len == 32) {
251 switch (mac_len) {
252 case 4:
253 memcpy(zuc_const, zuc_key256_mac4, 16);
254 break;
255 case 8:
256 memcpy(zuc_const, zuc_key256_mac8, 16);
257 break;
258 case 16:
259 memcpy(zuc_const, zuc_key256_mac16, 16);
260 break;
261 default:
262 plt_err("Unsupported mac len");
263 }
264 }
265 }
266
267 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)268 roc_se_auth_key_set(struct roc_se_ctx *se_ctx, roc_se_auth_type type, const uint8_t *key,
269 uint16_t key_len, uint16_t mac_len)
270 {
271 struct roc_se_kasumi_ctx *k_ctx;
272 struct roc_se_pdcp_ctx *pctx;
273 struct roc_se_context *fctx;
274 uint8_t opcode_minor;
275 bool chained_op;
276
277 if (se_ctx == NULL)
278 return -1;
279
280 pctx = &se_ctx->se_ctx.pctx;
281 k_ctx = &se_ctx->se_ctx.k_ctx;
282 fctx = &se_ctx->se_ctx.fctx;
283
284 chained_op = se_ctx->ciph_then_auth || se_ctx->auth_then_ciph;
285
286 if ((type >= ROC_SE_ZUC_EIA3) && (type <= ROC_SE_KASUMI_F9_ECB)) {
287 uint32_t keyx[4];
288 int key_type;
289
290 if (!key_len)
291 return -1;
292
293 if (se_ctx->fc_type == ROC_SE_FC_GEN) {
294 plt_err("Cipher and Auth algorithm combination is not supported");
295 return -1;
296 }
297
298 /* For ZUC/SNOW3G/Kasumi */
299 switch (type) {
300 case ROC_SE_SNOW3G_UIA2:
301 pctx->w0.s.state_conf = ROC_SE_PDCP_CHAIN_CTX_KEY_IV;
302 pctx->w0.s.auth_type = ROC_SE_PDCP_CHAIN_ALG_TYPE_SNOW3G;
303 pctx->w0.s.mac_len = mac_len;
304 pctx->w0.s.auth_key_len = key_len;
305 se_ctx->fc_type = ROC_SE_PDCP_CHAIN;
306 cpt_snow3g_key_gen(key, keyx);
307 memcpy(pctx->st.auth_key, keyx, key_len);
308
309 if (!chained_op)
310 se_ctx->fc_type = ROC_SE_PDCP;
311 se_ctx->pdcp_auth_alg = ROC_SE_PDCP_ALG_TYPE_SNOW3G;
312 se_ctx->zsk_flags = 0x1;
313 break;
314 case ROC_SE_ZUC_EIA3:
315 if (unlikely(key_len != 16)) {
316 /*
317 * ZUC 256 is not supported with older microcode
318 * where pdcp_iv_offset is 16
319 */
320 if (chained_op || (se_ctx->pdcp_iv_offset == 16)) {
321 plt_err("ZUC 256 is not supported with chained operations");
322 return -1;
323 }
324 }
325 key_type = cpt_pdcp_chain_key_type_get(key_len);
326 if (key_type < 0)
327 return key_type;
328 pctx->w0.s.auth_key_len = key_type;
329 pctx->w0.s.state_conf = ROC_SE_PDCP_CHAIN_CTX_KEY_IV;
330 pctx->w0.s.auth_type = ROC_SE_PDCP_CHAIN_ALG_TYPE_ZUC;
331 pctx->w0.s.mac_len = mac_len;
332 memcpy(pctx->st.auth_key, key, key_len);
333 if (key_len == 32)
334 roc_se_zuc_bytes_swap(pctx->st.auth_key, key_len);
335 cpt_zuc_const_update(pctx->st.auth_zuc_const, key_len, mac_len);
336 se_ctx->fc_type = ROC_SE_PDCP_CHAIN;
337
338 if (!chained_op)
339 se_ctx->fc_type = ROC_SE_PDCP;
340 se_ctx->pdcp_auth_alg = ROC_SE_PDCP_ALG_TYPE_ZUC;
341 se_ctx->zsk_flags = 0x1;
342 break;
343 case ROC_SE_AES_CMAC_EIA2:
344 key_type = cpt_pdcp_chain_key_type_get(key_len);
345 if (key_type < 0)
346 return key_type;
347 pctx->w0.s.auth_key_len = key_type;
348 pctx->w0.s.state_conf = ROC_SE_PDCP_CHAIN_CTX_KEY_IV;
349 pctx->w0.s.auth_type = ROC_SE_PDCP_ALG_TYPE_AES_CTR;
350 pctx->w0.s.mac_len = mac_len;
351 memcpy(pctx->st.auth_key, key, key_len);
352 se_ctx->fc_type = ROC_SE_PDCP_CHAIN;
353
354 if (!chained_op)
355 se_ctx->fc_type = ROC_SE_PDCP;
356 se_ctx->pdcp_auth_alg = ROC_SE_PDCP_ALG_TYPE_AES_CMAC;
357 se_ctx->eia2 = 1;
358 se_ctx->zsk_flags = 0x1;
359 break;
360 case ROC_SE_KASUMI_F9_ECB:
361 /* Kasumi ECB mode */
362 se_ctx->k_ecb = 1;
363 memcpy(k_ctx->ci_key, key, key_len);
364 se_ctx->fc_type = ROC_SE_KASUMI;
365 se_ctx->zsk_flags = 0x1;
366 break;
367 case ROC_SE_KASUMI_F9_CBC:
368 memcpy(k_ctx->ci_key, key, key_len);
369 se_ctx->fc_type = ROC_SE_KASUMI;
370 se_ctx->zsk_flags = 0x1;
371 break;
372 default:
373 return -1;
374 }
375
376 if ((se_ctx->fc_type == ROC_SE_PDCP_CHAIN) && (mac_len != 4)) {
377 plt_err("Only digest length of 4 is supported with PDCP chain");
378 return -1;
379 }
380
381 se_ctx->mac_len = mac_len;
382 se_ctx->hash_type = type;
383 if (chained_op)
384 opcode_minor = se_ctx->ciph_then_auth ? 2 : 3;
385 else
386 opcode_minor = ((1 << 4) | 1);
387
388 se_ctx->template_w4.s.opcode_minor = opcode_minor;
389 return 0;
390 }
391
392 if (!se_ctx->fc_type || (type && type != ROC_SE_GMAC_TYPE && !se_ctx->enc_cipher))
393 se_ctx->fc_type = ROC_SE_HASH_HMAC;
394
395 if (se_ctx->fc_type == ROC_SE_FC_GEN && key_len > 64) {
396 plt_err("Maximum auth key length supported is 64");
397 return -1;
398 }
399
400 /* For GMAC auth, cipher must be NULL */
401 if (type == ROC_SE_GMAC_TYPE) {
402 fctx->enc.enc_cipher = 0;
403 se_ctx->template_w4.s.opcode_minor = BIT(5);
404 }
405
406 fctx->enc.hash_type = type;
407 se_ctx->hash_type = type;
408 fctx->enc.mac_len = mac_len;
409 se_ctx->mac_len = mac_len;
410
411 if (key_len) {
412 /*
413 * Chained operation (FC opcode) requires precomputed ipad and opad hashes, but for
414 * auth only (HMAC opcode) this is not required
415 */
416 if (chained_op) {
417 memset(fctx->hmac.ipad, 0, sizeof(fctx->hmac.ipad));
418 memset(fctx->hmac.opad, 0, sizeof(fctx->hmac.opad));
419 roc_se_hmac_opad_ipad_gen(type, key, key_len, &fctx->hmac.ipad[0],
420 ROC_SE_FC);
421 fctx->enc.auth_input_type = 0;
422 } else {
423 se_ctx->hmac = 1;
424
425 se_ctx->auth_key = plt_zmalloc(key_len, 8);
426 if (se_ctx->auth_key == NULL)
427 return -1;
428
429 memcpy(se_ctx->auth_key, key, key_len);
430 se_ctx->auth_key_len = key_len;
431 }
432 }
433 return 0;
434 }
435
436 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)437 roc_se_ciph_key_set(struct roc_se_ctx *se_ctx, roc_se_cipher_type type, const uint8_t *key,
438 uint16_t key_len)
439 {
440 struct roc_se_context *fctx = &se_ctx->se_ctx.fctx;
441 struct roc_se_pdcp_ctx *pctx;
442 uint8_t opcode_minor = 0;
443 uint32_t keyx[4];
444 int key_type;
445 int i, ret;
446
447 /* For NULL cipher, no processing required. */
448 if (type == ROC_SE_PASSTHROUGH)
449 return 0;
450
451 pctx = &se_ctx->se_ctx.pctx;
452
453 if ((type == ROC_SE_AES_GCM) || (type == ROC_SE_AES_CCM))
454 se_ctx->template_w4.s.opcode_minor = BIT(5);
455
456 ret = cpt_ciph_type_set(type, se_ctx, key_len);
457 if (unlikely(ret))
458 return -1;
459
460 if (se_ctx->fc_type == ROC_SE_FC_GEN) {
461 /*
462 * We need to always say IV is from DPTR as user can
463 * sometimes override IV per operation.
464 */
465 fctx->enc.iv_source = ROC_SE_FROM_DPTR;
466
467 if (se_ctx->auth_key_len > 64)
468 return -1;
469 }
470
471 switch (type) {
472 case ROC_SE_DES3_CBC:
473 /* CPT performs DES using 3DES with the 8B DES-key
474 * replicated 2 more times to match the 24B 3DES-key.
475 * Eg. If org. key is "0x0a 0x0b", then new key is
476 * "0x0a 0x0b 0x0a 0x0b 0x0a 0x0b"
477 */
478 if (key_len == 8) {
479 /* Skipping the first 8B as it will be copied
480 * in the regular code flow
481 */
482 memcpy(fctx->enc.encr_key + key_len, key, key_len);
483 memcpy(fctx->enc.encr_key + 2 * key_len, key, key_len);
484 }
485 break;
486 case ROC_SE_DES3_ECB:
487 /* For DES3_ECB IV need to be from CTX. */
488 fctx->enc.iv_source = ROC_SE_FROM_CTX;
489 break;
490 case ROC_SE_AES_CBC:
491 case ROC_SE_AES_ECB:
492 case ROC_SE_AES_CFB:
493 case ROC_SE_AES_CTR:
494 case ROC_SE_CHACHA20:
495 cpt_ciph_aes_key_type_set(fctx, key_len);
496 break;
497 case ROC_SE_AES_GCM:
498 case ROC_SE_AES_CCM:
499 cpt_ciph_aes_key_type_set(fctx, key_len);
500 break;
501 case ROC_SE_AES_XTS:
502 key_len = key_len / 2;
503 cpt_ciph_aes_key_type_set(fctx, key_len);
504
505 /* Copy key2 for XTS into ipad */
506 memset(fctx->hmac.ipad, 0, sizeof(fctx->hmac.ipad));
507 memcpy(fctx->hmac.ipad, &key[key_len], key_len);
508 break;
509 case ROC_SE_AES_DOCSISBPI:
510 /*
511 * DOCSIS uses the combination of AES-CBC and residual termination blocks that are
512 * less than 128. Pass it as regular AES-CBC cipher to CPT, but keep type in
513 * se_ctx as AES_DOCSISBPI to skip block size checks in instruction preparation.
514 */
515 cpt_ciph_aes_key_type_set(fctx, key_len);
516 fctx->enc.enc_cipher = ROC_SE_AES_CBC;
517 memcpy(fctx->enc.encr_key, key, key_len);
518 goto success;
519 case ROC_SE_DES_DOCSISBPI:
520 /* See case ROC_SE_DES3_CBC: for explanation */
521 for (i = 0; i < 3; i++)
522 memcpy(fctx->enc.encr_key + key_len * i, key, key_len);
523 /*
524 * DOCSIS uses DES-CBC mode with special handling of residual termination blocks
525 * that are less than 64 bits. Pass it as regular DES-CBC, but keep type in
526 * se_ctx as DES_DOCSISBPI to skip block size checks in instruction preparation.
527 */
528 fctx->enc.enc_cipher = ROC_SE_DES3_CBC;
529 goto success;
530 case ROC_SE_SNOW3G_UEA2:
531 pctx->w0.s.state_conf = ROC_SE_PDCP_CHAIN_CTX_KEY_IV;
532 pctx->w0.s.cipher_type = ROC_SE_PDCP_CHAIN_ALG_TYPE_SNOW3G;
533 pctx->w0.s.ci_key_len = key_len;
534 cpt_snow3g_key_gen(key, keyx);
535 memcpy(pctx->st.ci_key, keyx, key_len);
536 se_ctx->pdcp_ci_alg = ROC_SE_PDCP_ALG_TYPE_SNOW3G;
537 se_ctx->zsk_flags = 0;
538 goto success;
539 case ROC_SE_ZUC_EEA3:
540 key_type = cpt_pdcp_chain_key_type_get(key_len);
541 if (key_type < 0)
542 return key_type;
543 pctx->w0.s.ci_key_len = key_type;
544 pctx->w0.s.state_conf = ROC_SE_PDCP_CHAIN_CTX_KEY_IV;
545 pctx->w0.s.cipher_type = ROC_SE_PDCP_CHAIN_ALG_TYPE_ZUC;
546 memcpy(pctx->st.ci_key, key, key_len);
547 if (key_len == 32) {
548 roc_se_zuc_bytes_swap(pctx->st.ci_key, key_len);
549 memcpy(pctx->st.ci_zuc_const, zuc_key256, 16);
550 } else
551 memcpy(pctx->st.ci_zuc_const, zuc_key128, 32);
552 se_ctx->pdcp_ci_alg = ROC_SE_PDCP_ALG_TYPE_ZUC;
553 se_ctx->zsk_flags = 0;
554 goto success;
555 case ROC_SE_AES_CTR_EEA2:
556 key_type = cpt_pdcp_chain_key_type_get(key_len);
557 if (key_type < 0)
558 return key_type;
559 pctx->w0.s.ci_key_len = key_type;
560 pctx->w0.s.state_conf = ROC_SE_PDCP_CHAIN_CTX_KEY_IV;
561 pctx->w0.s.cipher_type = ROC_SE_PDCP_ALG_TYPE_AES_CTR;
562 memcpy(pctx->st.ci_key, key, key_len);
563 se_ctx->pdcp_ci_alg = ROC_SE_PDCP_ALG_TYPE_AES_CTR;
564 se_ctx->zsk_flags = 0;
565 goto success;
566 case ROC_SE_KASUMI_F8_ECB:
567 se_ctx->k_ecb = 1;
568 memcpy(se_ctx->se_ctx.k_ctx.ci_key, key, key_len);
569 se_ctx->zsk_flags = 0;
570 goto success;
571 case ROC_SE_KASUMI_F8_CBC:
572 memcpy(se_ctx->se_ctx.k_ctx.ci_key, key, key_len);
573 se_ctx->zsk_flags = 0;
574 goto success;
575 default:
576 return -1;
577 }
578
579 /* Only for ROC_SE_FC_GEN case */
580
581 /* For GMAC auth, cipher must be NULL */
582 if (se_ctx->hash_type != ROC_SE_GMAC_TYPE)
583 fctx->enc.enc_cipher = type;
584
585 memcpy(fctx->enc.encr_key, key, key_len);
586
587 success:
588 se_ctx->enc_cipher = type;
589 if (se_ctx->fc_type == ROC_SE_PDCP_CHAIN) {
590 se_ctx->template_w4.s.opcode_minor = se_ctx->ciph_then_auth ? 2 : 3;
591 } else if (se_ctx->fc_type == ROC_SE_PDCP) {
592 if (roc_model_is_cn9k())
593 opcode_minor =
594 ((1 << 7) | (se_ctx->pdcp_ci_alg << 5) | (se_ctx->zsk_flags & 0x7));
595 else
596 opcode_minor = ((1 << 4));
597 se_ctx->template_w4.s.opcode_minor = opcode_minor;
598 }
599 return 0;
600 }
601
602 void
roc_se_ctx_init(struct roc_se_ctx * roc_se_ctx)603 roc_se_ctx_init(struct roc_se_ctx *roc_se_ctx)
604 {
605 struct se_ctx_s *ctx = &roc_se_ctx->se_ctx;
606 uint64_t ctx_len, *uc_ctx;
607 uint8_t i;
608
609 switch (roc_se_ctx->fc_type) {
610 case ROC_SE_FC_GEN:
611 ctx_len = sizeof(struct roc_se_context);
612 break;
613 case ROC_SE_PDCP_CHAIN:
614 case ROC_SE_PDCP:
615 ctx_len = sizeof(struct roc_se_pdcp_ctx);
616 break;
617 case ROC_SE_KASUMI:
618 ctx_len = sizeof(struct roc_se_kasumi_ctx);
619 break;
620 case ROC_SE_SM:
621 ctx_len = sizeof(struct roc_se_sm_context);
622 break;
623 default:
624 ctx_len = 0;
625 }
626
627 ctx_len = PLT_ALIGN_CEIL(ctx_len, 8);
628
629 /* Skip w0 for swap */
630 uc_ctx = PLT_PTR_ADD(ctx, sizeof(ctx->w0));
631 for (i = 0; i < (ctx_len / 8); i++)
632 uc_ctx[i] = plt_cpu_to_be_64(((uint64_t *)uc_ctx)[i]);
633
634 /* Include w0 */
635 ctx_len += sizeof(ctx->w0);
636 ctx_len = PLT_ALIGN_CEIL(ctx_len, 8);
637
638 ctx->w0.s.aop_valid = 1;
639 ctx->w0.s.ctx_hdr_size = 0;
640
641 ctx->w0.s.ctx_size = PLT_ALIGN_FLOOR(ctx_len, 128);
642 if (ctx->w0.s.ctx_size == 0)
643 ctx->w0.s.ctx_size = 1;
644
645 ctx->w0.s.ctx_push_size = ctx_len / 8;
646 if (ctx->w0.s.ctx_push_size > 32)
647 ctx->w0.s.ctx_push_size = 32;
648 }
649