1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2021 Marvell.
7 static uint8_t zuc_d[32] = {0x44, 0xD7, 0x26, 0xBC, 0x62, 0x6B, 0x13, 0x5E,
8 0x57, 0x89, 0x35, 0xE2, 0x71, 0x35, 0x09, 0xAF,
9 0x4D, 0x78, 0x2F, 0x13, 0x6B, 0xC4, 0x1A, 0xF1,
10 0x5E, 0x26, 0x3C, 0x4D, 0x78, 0x9A, 0x47, 0xAC};
13 cpt_snow3g_key_gen(const uint8_t *ck, uint32_t *keyx)
17 for (i = 0; i < 4; i++) {
19 keyx[3 - i] = (ck[base] << 24) | (ck[base + 1] << 16) |
20 (ck[base + 2] << 8) | (ck[base + 3]);
21 keyx[3 - i] = plt_cpu_to_be_32(keyx[3 - i]);
26 cpt_ciph_aes_key_validate(uint16_t key_len)
39 cpt_ciph_type_set(roc_se_cipher_type type, struct roc_se_ctx *ctx,
45 case ROC_SE_PASSTHROUGH:
46 fc_type = ROC_SE_FC_GEN;
50 fc_type = ROC_SE_FC_GEN;
57 if (unlikely(cpt_ciph_aes_key_validate(key_len) != 0))
59 fc_type = ROC_SE_FC_GEN;
62 fc_type = ROC_SE_FC_GEN;
65 key_len = key_len / 2;
66 if (unlikely(key_len == 24)) {
67 plt_err("Invalid AES key len for XTS");
70 if (unlikely(cpt_ciph_aes_key_validate(key_len) != 0))
72 fc_type = ROC_SE_FC_GEN;
75 /* No support for chained operations */
76 if (unlikely(ctx->hash_type))
78 fc_type = ROC_SE_PDCP;
80 case ROC_SE_SNOW3G_UEA2:
81 if (unlikely(key_len != 16))
83 /* No support for AEAD yet */
84 if (unlikely(ctx->hash_type))
86 fc_type = ROC_SE_PDCP;
88 case ROC_SE_AES_CTR_EEA2:
89 fc_type = ROC_SE_PDCP;
91 case ROC_SE_KASUMI_F8_CBC:
92 case ROC_SE_KASUMI_F8_ECB:
93 if (unlikely(key_len != 16))
95 /* No support for AEAD yet */
96 if (unlikely(ctx->hash_type))
98 fc_type = ROC_SE_KASUMI;
104 ctx->fc_type = fc_type;
109 cpt_ciph_aes_key_type_set(struct roc_se_context *fctx, uint16_t key_len)
111 roc_se_aes_type aes_key_type = 0;
115 aes_key_type = ROC_SE_AES_128_BIT;
118 aes_key_type = ROC_SE_AES_192_BIT;
121 aes_key_type = ROC_SE_AES_256_BIT;
124 /* This should not happen */
125 plt_err("Invalid AES key len");
128 fctx->enc.aes_key = aes_key_type;
132 cpt_pdcp_key_type_set(struct roc_se_zuc_snow3g_ctx *zs_ctx, uint16_t key_len)
134 roc_se_aes_type key_type = 0;
136 if (roc_model_is_cn9k()) {
138 plt_err("Only key len 16 is supported on cn9k");
145 key_type = ROC_SE_AES_128_BIT;
148 key_type = ROC_SE_AES_256_BIT;
151 plt_err("Invalid AES key len");
154 zs_ctx->zuc.otk_ctx.w0.s.key_len = key_type;
159 cpt_pdcp_mac_len_set(struct roc_se_zuc_snow3g_ctx *zs_ctx, uint16_t mac_len)
161 roc_se_pdcp_mac_len_type mac_type = 0;
165 mac_type = ROC_SE_PDCP_MAC_LEN_32_BIT;
168 mac_type = ROC_SE_PDCP_MAC_LEN_64_BIT;
171 mac_type = ROC_SE_PDCP_MAC_LEN_128_BIT;
174 plt_err("Invalid ZUC MAC len");
177 zs_ctx->zuc.otk_ctx.w0.s.mac_len = mac_type;
182 roc_se_auth_key_set(struct roc_se_ctx *se_ctx, roc_se_auth_type type,
183 const uint8_t *key, uint16_t key_len, uint16_t mac_len)
185 struct roc_se_zuc_snow3g_ctx *zs_ctx;
186 struct roc_se_kasumi_ctx *k_ctx;
187 struct roc_se_context *fctx;
193 zs_ctx = &se_ctx->se_ctx.zs_ctx;
194 k_ctx = &se_ctx->se_ctx.k_ctx;
195 fctx = &se_ctx->se_ctx.fctx;
197 if ((type >= ROC_SE_ZUC_EIA3) && (type <= ROC_SE_KASUMI_F9_ECB)) {
205 /* No support for chained operations yet */
206 if (se_ctx->enc_cipher)
209 if (roc_model_is_cn9k()) {
210 ci_key = zs_ctx->zuc.onk_ctx.ci_key;
211 zuc_const = zs_ctx->zuc.onk_ctx.zuc_const;
213 ci_key = zs_ctx->zuc.otk_ctx.ci_key;
214 zuc_const = zs_ctx->zuc.otk_ctx.zuc_const;
217 /* For ZUC/SNOW3G/Kasumi */
219 case ROC_SE_SNOW3G_UIA2:
220 zs_ctx->zuc.otk_ctx.w0.s.alg_type =
221 ROC_SE_PDCP_ALG_TYPE_SNOW3G;
222 zs_ctx->zuc.otk_ctx.w0.s.mac_len =
223 ROC_SE_PDCP_MAC_LEN_32_BIT;
224 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_SNOW3G;
225 cpt_snow3g_key_gen(key, keyx);
226 memcpy(ci_key, keyx, key_len);
227 se_ctx->fc_type = ROC_SE_PDCP;
228 se_ctx->zsk_flags = 0x1;
230 case ROC_SE_ZUC_EIA3:
231 zs_ctx->zuc.otk_ctx.w0.s.alg_type =
232 ROC_SE_PDCP_ALG_TYPE_ZUC;
233 ret = cpt_pdcp_mac_len_set(zs_ctx, mac_len);
236 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_ZUC;
237 memcpy(ci_key, key, key_len);
238 memcpy(zuc_const, zuc_d, 32);
239 se_ctx->fc_type = ROC_SE_PDCP;
240 se_ctx->zsk_flags = 0x1;
242 case ROC_SE_AES_CMAC_EIA2:
243 zs_ctx->zuc.otk_ctx.w0.s.alg_type =
244 ROC_SE_PDCP_ALG_TYPE_AES_CTR;
245 zs_ctx->zuc.otk_ctx.w0.s.mac_len =
246 ROC_SE_PDCP_MAC_LEN_32_BIT;
247 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_AES_CTR;
248 memcpy(ci_key, key, key_len);
249 se_ctx->fc_type = ROC_SE_PDCP;
250 se_ctx->zsk_flags = 0x1;
252 case ROC_SE_KASUMI_F9_ECB:
253 /* Kasumi ECB mode */
255 memcpy(k_ctx->ci_key, key, key_len);
256 se_ctx->fc_type = ROC_SE_KASUMI;
257 se_ctx->zsk_flags = 0x1;
259 case ROC_SE_KASUMI_F9_CBC:
260 memcpy(k_ctx->ci_key, key, key_len);
261 se_ctx->fc_type = ROC_SE_KASUMI;
262 se_ctx->zsk_flags = 0x1;
267 se_ctx->mac_len = mac_len;
268 se_ctx->hash_type = type;
269 if (roc_model_is_cn9k())
270 se_ctx->template_w4.s.opcode_minor =
271 ((1 << 7) | (se_ctx->pdcp_alg_type << 5) | 1);
273 se_ctx->template_w4.s.opcode_minor = ((1 << 4) | 1);
277 if (!se_ctx->fc_type ||
278 (type && type != ROC_SE_GMAC_TYPE && !se_ctx->enc_cipher))
279 se_ctx->fc_type = ROC_SE_HASH_HMAC;
281 if (se_ctx->fc_type == ROC_SE_FC_GEN && key_len > 64)
284 /* For GMAC auth, cipher must be NULL */
285 if (type == ROC_SE_GMAC_TYPE)
286 fctx->enc.enc_cipher = 0;
288 fctx->enc.hash_type = type;
289 se_ctx->hash_type = type;
290 fctx->enc.mac_len = mac_len;
291 se_ctx->mac_len = mac_len;
296 se_ctx->auth_key = plt_zmalloc(key_len, 8);
297 if (se_ctx->auth_key == NULL)
300 memcpy(se_ctx->auth_key, key, key_len);
301 se_ctx->auth_key_len = key_len;
302 memset(fctx->hmac.ipad, 0, sizeof(fctx->hmac.ipad));
303 memset(fctx->hmac.opad, 0, sizeof(fctx->hmac.opad));
306 memcpy(fctx->hmac.opad, key, key_len);
307 fctx->enc.auth_input_type = 1;
313 roc_se_ciph_key_set(struct roc_se_ctx *se_ctx, roc_se_cipher_type type,
314 const uint8_t *key, uint16_t key_len, uint8_t *salt)
316 struct roc_se_zuc_snow3g_ctx *zs_ctx = &se_ctx->se_ctx.zs_ctx;
317 struct roc_se_context *fctx = &se_ctx->se_ctx.fctx;
323 if (roc_model_is_cn9k()) {
324 ci_key = zs_ctx->zuc.onk_ctx.ci_key;
325 zuc_const = zs_ctx->zuc.onk_ctx.zuc_const;
327 ci_key = zs_ctx->zuc.otk_ctx.ci_key;
328 zuc_const = zs_ctx->zuc.otk_ctx.zuc_const;
331 /* For AES-GCM, salt is taken from ctx even if IV source
334 if ((salt != NULL) && (type == ROC_SE_AES_GCM)) {
335 memcpy(fctx->enc.encr_iv, salt, 4);
336 /* Assuming it was just salt update
343 ret = cpt_ciph_type_set(type, se_ctx, key_len);
347 if (se_ctx->fc_type == ROC_SE_FC_GEN) {
349 * We need to always say IV is from DPTR as user can
350 * sometimes iverride IV per operation.
352 fctx->enc.iv_source = ROC_SE_FROM_DPTR;
354 if (se_ctx->auth_key_len > 64)
359 case ROC_SE_PASSTHROUGH:
360 se_ctx->enc_cipher = 0;
361 fctx->enc.enc_cipher = 0;
363 case ROC_SE_DES3_CBC:
364 /* CPT performs DES using 3DES with the 8B DES-key
365 * replicated 2 more times to match the 24B 3DES-key.
366 * Eg. If org. key is "0x0a 0x0b", then new key is
367 * "0x0a 0x0b 0x0a 0x0b 0x0a 0x0b"
370 /* Skipping the first 8B as it will be copied
371 * in the regular code flow
373 memcpy(fctx->enc.encr_key + key_len, key, key_len);
374 memcpy(fctx->enc.encr_key + 2 * key_len, key, key_len);
377 case ROC_SE_DES3_ECB:
378 /* For DES3_ECB IV need to be from CTX. */
379 fctx->enc.iv_source = ROC_SE_FROM_CTX;
385 case ROC_SE_CHACHA20:
386 cpt_ciph_aes_key_type_set(fctx, key_len);
389 cpt_ciph_aes_key_type_set(fctx, key_len);
392 key_len = key_len / 2;
393 cpt_ciph_aes_key_type_set(fctx, key_len);
395 /* Copy key2 for XTS into ipad */
396 memset(fctx->hmac.ipad, 0, sizeof(fctx->hmac.ipad));
397 memcpy(fctx->hmac.ipad, &key[key_len], key_len);
399 case ROC_SE_SNOW3G_UEA2:
400 zs_ctx->zuc.otk_ctx.w0.s.key_len = ROC_SE_AES_128_BIT;
401 zs_ctx->zuc.otk_ctx.w0.s.alg_type = ROC_SE_PDCP_ALG_TYPE_SNOW3G;
402 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_SNOW3G;
403 cpt_snow3g_key_gen(key, keyx);
404 memcpy(ci_key, keyx, key_len);
405 se_ctx->zsk_flags = 0;
407 case ROC_SE_ZUC_EEA3:
408 ret = cpt_pdcp_key_type_set(zs_ctx, key_len);
411 zs_ctx->zuc.otk_ctx.w0.s.alg_type = ROC_SE_PDCP_ALG_TYPE_ZUC;
412 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_ZUC;
413 memcpy(ci_key, key, key_len);
414 memcpy(zuc_const, zuc_d, 32);
415 se_ctx->zsk_flags = 0;
417 case ROC_SE_AES_CTR_EEA2:
418 zs_ctx->zuc.otk_ctx.w0.s.key_len = ROC_SE_AES_128_BIT;
419 zs_ctx->zuc.otk_ctx.w0.s.alg_type =
420 ROC_SE_PDCP_ALG_TYPE_AES_CTR;
421 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_AES_CTR;
422 memcpy(ci_key, key, key_len);
423 se_ctx->zsk_flags = 0;
425 case ROC_SE_KASUMI_F8_ECB:
427 memcpy(se_ctx->se_ctx.k_ctx.ci_key, key, key_len);
428 se_ctx->zsk_flags = 0;
430 case ROC_SE_KASUMI_F8_CBC:
431 memcpy(se_ctx->se_ctx.k_ctx.ci_key, key, key_len);
432 se_ctx->zsk_flags = 0;
438 /* Only for ROC_SE_FC_GEN case */
440 /* For GMAC auth, cipher must be NULL */
441 if (se_ctx->hash_type != ROC_SE_GMAC_TYPE)
442 fctx->enc.enc_cipher = type;
444 memcpy(fctx->enc.encr_key, key, key_len);
447 se_ctx->enc_cipher = type;
448 if (se_ctx->fc_type == ROC_SE_PDCP) {
449 if (roc_model_is_cn9k())
450 se_ctx->template_w4.s.opcode_minor =
451 ((1 << 7) | (se_ctx->pdcp_alg_type << 5) |
452 (se_ctx->zsk_flags & 0x7));
454 se_ctx->template_w4.s.opcode_minor = ((1 << 4));
460 roc_se_ctx_swap(struct roc_se_ctx *se_ctx)
462 struct roc_se_zuc_snow3g_ctx *zs_ctx = &se_ctx->se_ctx.zs_ctx;
464 if (roc_model_is_cn9k())
467 zs_ctx->zuc.otk_ctx.w0.u64 = htobe64(zs_ctx->zuc.otk_ctx.w0.u64);