1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2021 Marvell.
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};
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};
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};
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};
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};
29 cpt_snow3g_key_gen(const uint8_t *ck, uint32_t *keyx)
33 for (i = 0; i < 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]);
42 cpt_ciph_aes_key_validate(uint16_t key_len)
55 cpt_ciph_type_set(roc_se_cipher_type type, struct roc_se_ctx *ctx,
61 case ROC_SE_PASSTHROUGH:
62 fc_type = ROC_SE_FC_GEN;
66 fc_type = ROC_SE_FC_GEN;
73 if (unlikely(cpt_ciph_aes_key_validate(key_len) != 0))
75 fc_type = ROC_SE_FC_GEN;
78 fc_type = ROC_SE_FC_GEN;
81 key_len = key_len / 2;
82 if (unlikely(key_len == 24)) {
83 plt_err("Invalid AES key len for XTS");
86 if (unlikely(cpt_ciph_aes_key_validate(key_len) != 0))
88 fc_type = ROC_SE_FC_GEN;
91 /* No support for chained operations */
92 if (unlikely(ctx->hash_type))
94 fc_type = ROC_SE_PDCP;
96 case ROC_SE_SNOW3G_UEA2:
97 if (unlikely(key_len != 16))
99 /* No support for AEAD yet */
100 if (unlikely(ctx->hash_type))
102 fc_type = ROC_SE_PDCP;
104 case ROC_SE_AES_CTR_EEA2:
105 fc_type = ROC_SE_PDCP;
107 case ROC_SE_KASUMI_F8_CBC:
108 case ROC_SE_KASUMI_F8_ECB:
109 if (unlikely(key_len != 16))
111 /* No support for AEAD yet */
112 if (unlikely(ctx->hash_type))
114 fc_type = ROC_SE_KASUMI;
120 ctx->fc_type = fc_type;
125 cpt_ciph_aes_key_type_set(struct roc_se_context *fctx, uint16_t key_len)
127 roc_se_aes_type aes_key_type = 0;
131 aes_key_type = ROC_SE_AES_128_BIT;
134 aes_key_type = ROC_SE_AES_192_BIT;
137 aes_key_type = ROC_SE_AES_256_BIT;
140 /* This should not happen */
141 plt_err("Invalid AES key len");
144 fctx->enc.aes_key = aes_key_type;
148 cpt_pdcp_key_type_set(struct roc_se_zuc_snow3g_ctx *zs_ctx, uint16_t key_len)
150 roc_se_aes_type key_type = 0;
152 if (roc_model_is_cn9k()) {
154 plt_err("Only key len 16 is supported on cn9k");
161 key_type = ROC_SE_AES_128_BIT;
164 key_type = ROC_SE_AES_256_BIT;
167 plt_err("Invalid AES key len");
170 zs_ctx->zuc.otk_ctx.w0.s.key_len = key_type;
175 cpt_pdcp_mac_len_set(struct roc_se_zuc_snow3g_ctx *zs_ctx, uint16_t mac_len)
177 roc_se_pdcp_mac_len_type mac_type = 0;
179 if (roc_model_is_cn9k()) {
181 plt_err("Only mac len 4 is supported on cn9k");
188 mac_type = ROC_SE_PDCP_MAC_LEN_32_BIT;
191 mac_type = ROC_SE_PDCP_MAC_LEN_64_BIT;
194 mac_type = ROC_SE_PDCP_MAC_LEN_128_BIT;
197 plt_err("Invalid ZUC MAC len");
200 zs_ctx->zuc.otk_ctx.w0.s.mac_len = mac_type;
205 cpt_pdcp_update_zuc_const(uint8_t *zuc_const, int key_len, int mac_len)
208 memcpy(zuc_const, zuc_key128, 32);
209 } else if (key_len == 32) {
212 memcpy(zuc_const, zuc_key256_mac4, 16);
215 memcpy(zuc_const, zuc_key256_mac8, 16);
218 memcpy(zuc_const, zuc_key256_mac16, 16);
221 plt_err("Unsupported mac len");
227 roc_se_auth_key_set(struct roc_se_ctx *se_ctx, roc_se_auth_type type,
228 const uint8_t *key, uint16_t key_len, uint16_t mac_len)
230 struct roc_se_zuc_snow3g_ctx *zs_ctx;
231 struct roc_se_kasumi_ctx *k_ctx;
232 struct roc_se_context *fctx;
238 zs_ctx = &se_ctx->se_ctx.zs_ctx;
239 k_ctx = &se_ctx->se_ctx.k_ctx;
240 fctx = &se_ctx->se_ctx.fctx;
242 if ((type >= ROC_SE_ZUC_EIA3) && (type <= ROC_SE_KASUMI_F9_ECB)) {
250 /* No support for chained operations yet */
251 if (se_ctx->enc_cipher)
254 if (roc_model_is_cn9k()) {
255 ci_key = zs_ctx->zuc.onk_ctx.ci_key;
256 zuc_const = zs_ctx->zuc.onk_ctx.zuc_const;
258 ci_key = zs_ctx->zuc.otk_ctx.ci_key;
259 zuc_const = zs_ctx->zuc.otk_ctx.zuc_const;
262 /* For ZUC/SNOW3G/Kasumi */
264 case ROC_SE_SNOW3G_UIA2:
265 zs_ctx->zuc.otk_ctx.w0.s.alg_type =
266 ROC_SE_PDCP_ALG_TYPE_SNOW3G;
267 zs_ctx->zuc.otk_ctx.w0.s.mac_len =
268 ROC_SE_PDCP_MAC_LEN_32_BIT;
269 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_SNOW3G;
270 cpt_snow3g_key_gen(key, keyx);
271 memcpy(ci_key, keyx, key_len);
272 se_ctx->fc_type = ROC_SE_PDCP;
273 se_ctx->zsk_flags = 0x1;
275 case ROC_SE_ZUC_EIA3:
276 zs_ctx->zuc.otk_ctx.w0.s.alg_type =
277 ROC_SE_PDCP_ALG_TYPE_ZUC;
278 ret = cpt_pdcp_key_type_set(zs_ctx, key_len);
281 ret = cpt_pdcp_mac_len_set(zs_ctx, mac_len);
284 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_ZUC;
285 memcpy(ci_key, key, key_len);
286 cpt_pdcp_update_zuc_const(zuc_const, key_len, mac_len);
287 se_ctx->fc_type = ROC_SE_PDCP;
288 se_ctx->zsk_flags = 0x1;
290 case ROC_SE_AES_CMAC_EIA2:
291 zs_ctx->zuc.otk_ctx.w0.s.alg_type =
292 ROC_SE_PDCP_ALG_TYPE_AES_CTR;
293 zs_ctx->zuc.otk_ctx.w0.s.mac_len =
294 ROC_SE_PDCP_MAC_LEN_32_BIT;
295 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_AES_CTR;
296 memcpy(ci_key, key, key_len);
297 se_ctx->fc_type = ROC_SE_PDCP;
298 se_ctx->zsk_flags = 0x1;
300 case ROC_SE_KASUMI_F9_ECB:
301 /* Kasumi ECB mode */
303 memcpy(k_ctx->ci_key, key, key_len);
304 se_ctx->fc_type = ROC_SE_KASUMI;
305 se_ctx->zsk_flags = 0x1;
307 case ROC_SE_KASUMI_F9_CBC:
308 memcpy(k_ctx->ci_key, key, key_len);
309 se_ctx->fc_type = ROC_SE_KASUMI;
310 se_ctx->zsk_flags = 0x1;
315 se_ctx->mac_len = mac_len;
316 se_ctx->hash_type = type;
317 if (roc_model_is_cn9k())
318 se_ctx->template_w4.s.opcode_minor =
319 ((1 << 7) | (se_ctx->pdcp_alg_type << 5) | 1);
321 se_ctx->template_w4.s.opcode_minor = ((1 << 4) | 1);
325 if (!se_ctx->fc_type ||
326 (type && type != ROC_SE_GMAC_TYPE && !se_ctx->enc_cipher))
327 se_ctx->fc_type = ROC_SE_HASH_HMAC;
329 if (se_ctx->fc_type == ROC_SE_FC_GEN && key_len > 64)
332 /* For GMAC auth, cipher must be NULL */
333 if (type == ROC_SE_GMAC_TYPE)
334 fctx->enc.enc_cipher = 0;
336 fctx->enc.hash_type = type;
337 se_ctx->hash_type = type;
338 fctx->enc.mac_len = mac_len;
339 se_ctx->mac_len = mac_len;
344 se_ctx->auth_key = plt_zmalloc(key_len, 8);
345 if (se_ctx->auth_key == NULL)
348 memcpy(se_ctx->auth_key, key, key_len);
349 se_ctx->auth_key_len = key_len;
350 memset(fctx->hmac.ipad, 0, sizeof(fctx->hmac.ipad));
351 memset(fctx->hmac.opad, 0, sizeof(fctx->hmac.opad));
354 memcpy(fctx->hmac.opad, key, key_len);
355 fctx->enc.auth_input_type = 1;
361 roc_se_ciph_key_set(struct roc_se_ctx *se_ctx, roc_se_cipher_type type,
362 const uint8_t *key, uint16_t key_len, uint8_t *salt)
364 struct roc_se_zuc_snow3g_ctx *zs_ctx = &se_ctx->se_ctx.zs_ctx;
365 struct roc_se_context *fctx = &se_ctx->se_ctx.fctx;
371 if (roc_model_is_cn9k()) {
372 ci_key = zs_ctx->zuc.onk_ctx.ci_key;
373 zuc_const = zs_ctx->zuc.onk_ctx.zuc_const;
375 ci_key = zs_ctx->zuc.otk_ctx.ci_key;
376 zuc_const = zs_ctx->zuc.otk_ctx.zuc_const;
379 /* For AES-GCM, salt is taken from ctx even if IV source
382 if ((salt != NULL) && (type == ROC_SE_AES_GCM)) {
383 memcpy(fctx->enc.encr_iv, salt, 4);
384 /* Assuming it was just salt update
391 ret = cpt_ciph_type_set(type, se_ctx, key_len);
395 if (se_ctx->fc_type == ROC_SE_FC_GEN) {
397 * We need to always say IV is from DPTR as user can
398 * sometimes override IV per operation.
400 fctx->enc.iv_source = ROC_SE_FROM_DPTR;
402 if (se_ctx->auth_key_len > 64)
407 case ROC_SE_PASSTHROUGH:
408 se_ctx->enc_cipher = 0;
409 fctx->enc.enc_cipher = 0;
411 case ROC_SE_DES3_CBC:
412 /* CPT performs DES using 3DES with the 8B DES-key
413 * replicated 2 more times to match the 24B 3DES-key.
414 * Eg. If org. key is "0x0a 0x0b", then new key is
415 * "0x0a 0x0b 0x0a 0x0b 0x0a 0x0b"
418 /* Skipping the first 8B as it will be copied
419 * in the regular code flow
421 memcpy(fctx->enc.encr_key + key_len, key, key_len);
422 memcpy(fctx->enc.encr_key + 2 * key_len, key, key_len);
425 case ROC_SE_DES3_ECB:
426 /* For DES3_ECB IV need to be from CTX. */
427 fctx->enc.iv_source = ROC_SE_FROM_CTX;
433 case ROC_SE_CHACHA20:
434 cpt_ciph_aes_key_type_set(fctx, key_len);
437 cpt_ciph_aes_key_type_set(fctx, key_len);
440 key_len = key_len / 2;
441 cpt_ciph_aes_key_type_set(fctx, key_len);
443 /* Copy key2 for XTS into ipad */
444 memset(fctx->hmac.ipad, 0, sizeof(fctx->hmac.ipad));
445 memcpy(fctx->hmac.ipad, &key[key_len], key_len);
447 case ROC_SE_SNOW3G_UEA2:
448 zs_ctx->zuc.otk_ctx.w0.s.key_len = ROC_SE_AES_128_BIT;
449 zs_ctx->zuc.otk_ctx.w0.s.alg_type = ROC_SE_PDCP_ALG_TYPE_SNOW3G;
450 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_SNOW3G;
451 cpt_snow3g_key_gen(key, keyx);
452 memcpy(ci_key, keyx, key_len);
453 se_ctx->zsk_flags = 0;
455 case ROC_SE_ZUC_EEA3:
456 ret = cpt_pdcp_key_type_set(zs_ctx, key_len);
459 zs_ctx->zuc.otk_ctx.w0.s.alg_type = ROC_SE_PDCP_ALG_TYPE_ZUC;
460 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_ZUC;
461 memcpy(ci_key, key, key_len);
463 memcpy(zuc_const, zuc_key256, 16);
465 memcpy(zuc_const, zuc_key128, 32);
467 se_ctx->zsk_flags = 0;
469 case ROC_SE_AES_CTR_EEA2:
470 zs_ctx->zuc.otk_ctx.w0.s.key_len = ROC_SE_AES_128_BIT;
471 zs_ctx->zuc.otk_ctx.w0.s.alg_type =
472 ROC_SE_PDCP_ALG_TYPE_AES_CTR;
473 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_AES_CTR;
474 memcpy(ci_key, key, key_len);
475 se_ctx->zsk_flags = 0;
477 case ROC_SE_KASUMI_F8_ECB:
479 memcpy(se_ctx->se_ctx.k_ctx.ci_key, key, key_len);
480 se_ctx->zsk_flags = 0;
482 case ROC_SE_KASUMI_F8_CBC:
483 memcpy(se_ctx->se_ctx.k_ctx.ci_key, key, key_len);
484 se_ctx->zsk_flags = 0;
490 /* Only for ROC_SE_FC_GEN case */
492 /* For GMAC auth, cipher must be NULL */
493 if (se_ctx->hash_type != ROC_SE_GMAC_TYPE)
494 fctx->enc.enc_cipher = type;
496 memcpy(fctx->enc.encr_key, key, key_len);
499 se_ctx->enc_cipher = type;
500 if (se_ctx->fc_type == ROC_SE_PDCP) {
501 if (roc_model_is_cn9k())
502 se_ctx->template_w4.s.opcode_minor =
503 ((1 << 7) | (se_ctx->pdcp_alg_type << 5) |
504 (se_ctx->zsk_flags & 0x7));
506 se_ctx->template_w4.s.opcode_minor = ((1 << 4));
512 roc_se_ctx_swap(struct roc_se_ctx *se_ctx)
514 struct roc_se_zuc_snow3g_ctx *zs_ctx = &se_ctx->se_ctx.zs_ctx;
516 if (roc_model_is_cn9k())
519 zs_ctx->zuc.otk_ctx.w0.u64 = htobe64(zs_ctx->zuc.otk_ctx.w0.u64);