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