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);
287 roc_se_zuc_bytes_swap(ci_key, key_len);
288 cpt_pdcp_update_zuc_const(zuc_const, key_len, mac_len);
289 se_ctx->fc_type = ROC_SE_PDCP;
290 se_ctx->zsk_flags = 0x1;
292 case ROC_SE_AES_CMAC_EIA2:
293 zs_ctx->zuc.otk_ctx.w0.s.alg_type =
294 ROC_SE_PDCP_ALG_TYPE_AES_CTR;
295 zs_ctx->zuc.otk_ctx.w0.s.mac_len =
296 ROC_SE_PDCP_MAC_LEN_32_BIT;
297 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_AES_CTR;
298 memcpy(ci_key, key, key_len);
299 se_ctx->fc_type = ROC_SE_PDCP;
300 se_ctx->zsk_flags = 0x1;
302 case ROC_SE_KASUMI_F9_ECB:
303 /* Kasumi ECB mode */
305 memcpy(k_ctx->ci_key, key, key_len);
306 se_ctx->fc_type = ROC_SE_KASUMI;
307 se_ctx->zsk_flags = 0x1;
309 case ROC_SE_KASUMI_F9_CBC:
310 memcpy(k_ctx->ci_key, key, key_len);
311 se_ctx->fc_type = ROC_SE_KASUMI;
312 se_ctx->zsk_flags = 0x1;
317 se_ctx->mac_len = mac_len;
318 se_ctx->hash_type = type;
319 if (roc_model_is_cn9k())
320 se_ctx->template_w4.s.opcode_minor =
321 ((1 << 7) | (se_ctx->pdcp_alg_type << 5) | 1);
323 se_ctx->template_w4.s.opcode_minor = ((1 << 4) | 1);
327 if (!se_ctx->fc_type ||
328 (type && type != ROC_SE_GMAC_TYPE && !se_ctx->enc_cipher))
329 se_ctx->fc_type = ROC_SE_HASH_HMAC;
331 if (se_ctx->fc_type == ROC_SE_FC_GEN && key_len > 64)
334 /* For GMAC auth, cipher must be NULL */
335 if (type == ROC_SE_GMAC_TYPE)
336 fctx->enc.enc_cipher = 0;
338 fctx->enc.hash_type = type;
339 se_ctx->hash_type = type;
340 fctx->enc.mac_len = mac_len;
341 se_ctx->mac_len = mac_len;
346 se_ctx->auth_key = plt_zmalloc(key_len, 8);
347 if (se_ctx->auth_key == NULL)
350 memcpy(se_ctx->auth_key, key, key_len);
351 se_ctx->auth_key_len = key_len;
352 memset(fctx->hmac.ipad, 0, sizeof(fctx->hmac.ipad));
353 memset(fctx->hmac.opad, 0, sizeof(fctx->hmac.opad));
356 memcpy(fctx->hmac.opad, key, key_len);
357 fctx->enc.auth_input_type = 1;
363 roc_se_ciph_key_set(struct roc_se_ctx *se_ctx, roc_se_cipher_type type,
364 const uint8_t *key, uint16_t key_len, uint8_t *salt)
366 struct roc_se_zuc_snow3g_ctx *zs_ctx = &se_ctx->se_ctx.zs_ctx;
367 struct roc_se_context *fctx = &se_ctx->se_ctx.fctx;
373 if (roc_model_is_cn9k()) {
374 ci_key = zs_ctx->zuc.onk_ctx.ci_key;
375 zuc_const = zs_ctx->zuc.onk_ctx.zuc_const;
377 ci_key = zs_ctx->zuc.otk_ctx.ci_key;
378 zuc_const = zs_ctx->zuc.otk_ctx.zuc_const;
381 /* For AES-GCM, salt is taken from ctx even if IV source
384 if ((salt != NULL) && (type == ROC_SE_AES_GCM)) {
385 memcpy(fctx->enc.encr_iv, salt, 4);
386 /* Assuming it was just salt update
393 ret = cpt_ciph_type_set(type, se_ctx, key_len);
397 if (se_ctx->fc_type == ROC_SE_FC_GEN) {
399 * We need to always say IV is from DPTR as user can
400 * sometimes override IV per operation.
402 fctx->enc.iv_source = ROC_SE_FROM_DPTR;
404 if (se_ctx->auth_key_len > 64)
409 case ROC_SE_PASSTHROUGH:
410 se_ctx->enc_cipher = 0;
411 fctx->enc.enc_cipher = 0;
413 case ROC_SE_DES3_CBC:
414 /* CPT performs DES using 3DES with the 8B DES-key
415 * replicated 2 more times to match the 24B 3DES-key.
416 * Eg. If org. key is "0x0a 0x0b", then new key is
417 * "0x0a 0x0b 0x0a 0x0b 0x0a 0x0b"
420 /* Skipping the first 8B as it will be copied
421 * in the regular code flow
423 memcpy(fctx->enc.encr_key + key_len, key, key_len);
424 memcpy(fctx->enc.encr_key + 2 * key_len, key, key_len);
427 case ROC_SE_DES3_ECB:
428 /* For DES3_ECB IV need to be from CTX. */
429 fctx->enc.iv_source = ROC_SE_FROM_CTX;
435 case ROC_SE_CHACHA20:
436 cpt_ciph_aes_key_type_set(fctx, key_len);
439 cpt_ciph_aes_key_type_set(fctx, key_len);
442 key_len = key_len / 2;
443 cpt_ciph_aes_key_type_set(fctx, key_len);
445 /* Copy key2 for XTS into ipad */
446 memset(fctx->hmac.ipad, 0, sizeof(fctx->hmac.ipad));
447 memcpy(fctx->hmac.ipad, &key[key_len], key_len);
449 case ROC_SE_SNOW3G_UEA2:
450 zs_ctx->zuc.otk_ctx.w0.s.key_len = ROC_SE_AES_128_BIT;
451 zs_ctx->zuc.otk_ctx.w0.s.alg_type = ROC_SE_PDCP_ALG_TYPE_SNOW3G;
452 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_SNOW3G;
453 cpt_snow3g_key_gen(key, keyx);
454 memcpy(ci_key, keyx, key_len);
455 se_ctx->zsk_flags = 0;
457 case ROC_SE_ZUC_EEA3:
458 ret = cpt_pdcp_key_type_set(zs_ctx, key_len);
461 zs_ctx->zuc.otk_ctx.w0.s.alg_type = ROC_SE_PDCP_ALG_TYPE_ZUC;
462 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_ZUC;
463 memcpy(ci_key, key, key_len);
465 roc_se_zuc_bytes_swap(ci_key, key_len);
466 memcpy(zuc_const, zuc_key256, 16);
468 memcpy(zuc_const, zuc_key128, 32);
470 se_ctx->zsk_flags = 0;
472 case ROC_SE_AES_CTR_EEA2:
473 zs_ctx->zuc.otk_ctx.w0.s.key_len = ROC_SE_AES_128_BIT;
474 zs_ctx->zuc.otk_ctx.w0.s.alg_type =
475 ROC_SE_PDCP_ALG_TYPE_AES_CTR;
476 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_AES_CTR;
477 memcpy(ci_key, key, key_len);
478 se_ctx->zsk_flags = 0;
480 case ROC_SE_KASUMI_F8_ECB:
482 memcpy(se_ctx->se_ctx.k_ctx.ci_key, key, key_len);
483 se_ctx->zsk_flags = 0;
485 case ROC_SE_KASUMI_F8_CBC:
486 memcpy(se_ctx->se_ctx.k_ctx.ci_key, key, key_len);
487 se_ctx->zsk_flags = 0;
493 /* Only for ROC_SE_FC_GEN case */
495 /* For GMAC auth, cipher must be NULL */
496 if (se_ctx->hash_type != ROC_SE_GMAC_TYPE)
497 fctx->enc.enc_cipher = type;
499 memcpy(fctx->enc.encr_key, key, key_len);
502 se_ctx->enc_cipher = type;
503 if (se_ctx->fc_type == ROC_SE_PDCP) {
504 if (roc_model_is_cn9k())
505 se_ctx->template_w4.s.opcode_minor =
506 ((1 << 7) | (se_ctx->pdcp_alg_type << 5) |
507 (se_ctx->zsk_flags & 0x7));
509 se_ctx->template_w4.s.opcode_minor = ((1 << 4));
515 roc_se_ctx_swap(struct roc_se_ctx *se_ctx)
517 struct roc_se_zuc_snow3g_ctx *zs_ctx = &se_ctx->se_ctx.zs_ctx;
519 if (roc_model_is_cn9k())
522 zs_ctx->zuc.otk_ctx.w0.u64 = htobe64(zs_ctx->zuc.otk_ctx.w0.u64);