common/cnxk: add new PCI IDs to supported devices
[dpdk.git] / drivers / common / cnxk / roc_se.c
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_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};
11
12 static inline void
13 cpt_snow3g_key_gen(const uint8_t *ck, uint32_t *keyx)
14 {
15         int i, base;
16
17         for (i = 0; i < 4; i++) {
18                 base = 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]);
22         }
23 }
24
25 static inline int
26 cpt_ciph_aes_key_validate(uint16_t key_len)
27 {
28         switch (key_len) {
29         case 16:
30         case 24:
31         case 32:
32                 return 0;
33         default:
34                 return -1;
35         }
36 }
37
38 static inline int
39 cpt_ciph_type_set(roc_se_cipher_type type, struct roc_se_ctx *ctx,
40                   uint16_t key_len)
41 {
42         int fc_type = 0;
43
44         switch (type) {
45         case ROC_SE_PASSTHROUGH:
46                 fc_type = ROC_SE_FC_GEN;
47                 break;
48         case ROC_SE_DES3_CBC:
49         case ROC_SE_DES3_ECB:
50                 fc_type = ROC_SE_FC_GEN;
51                 break;
52         case ROC_SE_AES_CBC:
53         case ROC_SE_AES_ECB:
54         case ROC_SE_AES_CFB:
55         case ROC_SE_AES_CTR:
56         case ROC_SE_AES_GCM:
57                 if (unlikely(cpt_ciph_aes_key_validate(key_len) != 0))
58                         return -1;
59                 fc_type = ROC_SE_FC_GEN;
60                 break;
61         case ROC_SE_CHACHA20:
62                 fc_type = ROC_SE_FC_GEN;
63                 break;
64         case ROC_SE_AES_XTS:
65                 key_len = key_len / 2;
66                 if (unlikely(key_len == 24)) {
67                         plt_err("Invalid AES key len for XTS");
68                         return -1;
69                 }
70                 if (unlikely(cpt_ciph_aes_key_validate(key_len) != 0))
71                         return -1;
72                 fc_type = ROC_SE_FC_GEN;
73                 break;
74         case ROC_SE_ZUC_EEA3:
75                 /* No support for chained operations */
76                 if (unlikely(ctx->hash_type))
77                         return -1;
78                 fc_type = ROC_SE_PDCP;
79                 break;
80         case ROC_SE_SNOW3G_UEA2:
81                 if (unlikely(key_len != 16))
82                         return -1;
83                 /* No support for AEAD yet */
84                 if (unlikely(ctx->hash_type))
85                         return -1;
86                 fc_type = ROC_SE_PDCP;
87                 break;
88         case ROC_SE_AES_CTR_EEA2:
89                 fc_type = ROC_SE_PDCP;
90                 break;
91         case ROC_SE_KASUMI_F8_CBC:
92         case ROC_SE_KASUMI_F8_ECB:
93                 if (unlikely(key_len != 16))
94                         return -1;
95                 /* No support for AEAD yet */
96                 if (unlikely(ctx->hash_type))
97                         return -1;
98                 fc_type = ROC_SE_KASUMI;
99                 break;
100         default:
101                 return -1;
102         }
103
104         ctx->fc_type = fc_type;
105         return 0;
106 }
107
108 static inline void
109 cpt_ciph_aes_key_type_set(struct roc_se_context *fctx, uint16_t key_len)
110 {
111         roc_se_aes_type aes_key_type = 0;
112
113         switch (key_len) {
114         case 16:
115                 aes_key_type = ROC_SE_AES_128_BIT;
116                 break;
117         case 24:
118                 aes_key_type = ROC_SE_AES_192_BIT;
119                 break;
120         case 32:
121                 aes_key_type = ROC_SE_AES_256_BIT;
122                 break;
123         default:
124                 /* This should not happen */
125                 plt_err("Invalid AES key len");
126                 return;
127         }
128         fctx->enc.aes_key = aes_key_type;
129 }
130
131 static int
132 cpt_pdcp_key_type_set(struct roc_se_zuc_snow3g_ctx *zs_ctx, uint16_t key_len)
133 {
134         roc_se_aes_type key_type = 0;
135
136         if (roc_model_is_cn9k()) {
137                 if (key_len != 16) {
138                         plt_err("Only key len 16 is supported on cn9k");
139                         return -ENOTSUP;
140                 }
141         }
142
143         switch (key_len) {
144         case 16:
145                 key_type = ROC_SE_AES_128_BIT;
146                 break;
147         case 32:
148                 key_type = ROC_SE_AES_256_BIT;
149                 break;
150         default:
151                 plt_err("Invalid AES key len");
152                 return -ENOTSUP;
153         }
154         zs_ctx->zuc.otk_ctx.w0.s.key_len = key_type;
155         return 0;
156 }
157
158 static int
159 cpt_pdcp_mac_len_set(struct roc_se_zuc_snow3g_ctx *zs_ctx, uint16_t mac_len)
160 {
161         roc_se_pdcp_mac_len_type mac_type = 0;
162
163         if (roc_model_is_cn9k()) {
164                 if (mac_len != 4) {
165                         plt_err("Only mac len 4 is supported on cn9k");
166                         return -ENOTSUP;
167                 }
168         }
169
170         switch (mac_len) {
171         case 4:
172                 mac_type = ROC_SE_PDCP_MAC_LEN_32_BIT;
173                 break;
174         case 8:
175                 mac_type = ROC_SE_PDCP_MAC_LEN_64_BIT;
176                 break;
177         case 16:
178                 mac_type = ROC_SE_PDCP_MAC_LEN_128_BIT;
179                 break;
180         default:
181                 plt_err("Invalid ZUC MAC len");
182                 return -ENOTSUP;
183         }
184         zs_ctx->zuc.otk_ctx.w0.s.mac_len = mac_type;
185         return 0;
186 }
187
188 int
189 roc_se_auth_key_set(struct roc_se_ctx *se_ctx, roc_se_auth_type type,
190                     const uint8_t *key, uint16_t key_len, uint16_t mac_len)
191 {
192         struct roc_se_zuc_snow3g_ctx *zs_ctx;
193         struct roc_se_kasumi_ctx *k_ctx;
194         struct roc_se_context *fctx;
195         int ret;
196
197         if (se_ctx == NULL)
198                 return -1;
199
200         zs_ctx = &se_ctx->se_ctx.zs_ctx;
201         k_ctx = &se_ctx->se_ctx.k_ctx;
202         fctx = &se_ctx->se_ctx.fctx;
203
204         if ((type >= ROC_SE_ZUC_EIA3) && (type <= ROC_SE_KASUMI_F9_ECB)) {
205                 uint8_t *zuc_const;
206                 uint32_t keyx[4];
207                 uint8_t *ci_key;
208
209                 if (!key_len)
210                         return -1;
211
212                 /* No support for chained operations yet */
213                 if (se_ctx->enc_cipher)
214                         return -1;
215
216                 if (roc_model_is_cn9k()) {
217                         ci_key = zs_ctx->zuc.onk_ctx.ci_key;
218                         zuc_const = zs_ctx->zuc.onk_ctx.zuc_const;
219                 } else {
220                         ci_key = zs_ctx->zuc.otk_ctx.ci_key;
221                         zuc_const = zs_ctx->zuc.otk_ctx.zuc_const;
222                 }
223
224                 /* For ZUC/SNOW3G/Kasumi */
225                 switch (type) {
226                 case ROC_SE_SNOW3G_UIA2:
227                         zs_ctx->zuc.otk_ctx.w0.s.alg_type =
228                                 ROC_SE_PDCP_ALG_TYPE_SNOW3G;
229                         zs_ctx->zuc.otk_ctx.w0.s.mac_len =
230                                 ROC_SE_PDCP_MAC_LEN_32_BIT;
231                         se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_SNOW3G;
232                         cpt_snow3g_key_gen(key, keyx);
233                         memcpy(ci_key, keyx, key_len);
234                         se_ctx->fc_type = ROC_SE_PDCP;
235                         se_ctx->zsk_flags = 0x1;
236                         break;
237                 case ROC_SE_ZUC_EIA3:
238                         zs_ctx->zuc.otk_ctx.w0.s.alg_type =
239                                 ROC_SE_PDCP_ALG_TYPE_ZUC;
240                         ret = cpt_pdcp_key_type_set(zs_ctx, key_len);
241                         if (ret)
242                                 return ret;
243                         ret = cpt_pdcp_mac_len_set(zs_ctx, mac_len);
244                         if (ret)
245                                 return ret;
246                         se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_ZUC;
247                         memcpy(ci_key, key, key_len);
248                         memcpy(zuc_const, zuc_d, 32);
249                         se_ctx->fc_type = ROC_SE_PDCP;
250                         se_ctx->zsk_flags = 0x1;
251                         break;
252                 case ROC_SE_AES_CMAC_EIA2:
253                         zs_ctx->zuc.otk_ctx.w0.s.alg_type =
254                                 ROC_SE_PDCP_ALG_TYPE_AES_CTR;
255                         zs_ctx->zuc.otk_ctx.w0.s.mac_len =
256                                 ROC_SE_PDCP_MAC_LEN_32_BIT;
257                         se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_AES_CTR;
258                         memcpy(ci_key, key, key_len);
259                         se_ctx->fc_type = ROC_SE_PDCP;
260                         se_ctx->zsk_flags = 0x1;
261                         break;
262                 case ROC_SE_KASUMI_F9_ECB:
263                         /* Kasumi ECB mode */
264                         se_ctx->k_ecb = 1;
265                         memcpy(k_ctx->ci_key, key, key_len);
266                         se_ctx->fc_type = ROC_SE_KASUMI;
267                         se_ctx->zsk_flags = 0x1;
268                         break;
269                 case ROC_SE_KASUMI_F9_CBC:
270                         memcpy(k_ctx->ci_key, key, key_len);
271                         se_ctx->fc_type = ROC_SE_KASUMI;
272                         se_ctx->zsk_flags = 0x1;
273                         break;
274                 default:
275                         return -1;
276                 }
277                 se_ctx->mac_len = mac_len;
278                 se_ctx->hash_type = type;
279                 if (roc_model_is_cn9k())
280                         se_ctx->template_w4.s.opcode_minor =
281                                 ((1 << 7) | (se_ctx->pdcp_alg_type << 5) | 1);
282                 else
283                         se_ctx->template_w4.s.opcode_minor = ((1 << 4) | 1);
284                 return 0;
285         }
286
287         if (!se_ctx->fc_type ||
288             (type && type != ROC_SE_GMAC_TYPE && !se_ctx->enc_cipher))
289                 se_ctx->fc_type = ROC_SE_HASH_HMAC;
290
291         if (se_ctx->fc_type == ROC_SE_FC_GEN && key_len > 64)
292                 return -1;
293
294         /* For GMAC auth, cipher must be NULL */
295         if (type == ROC_SE_GMAC_TYPE)
296                 fctx->enc.enc_cipher = 0;
297
298         fctx->enc.hash_type = type;
299         se_ctx->hash_type = type;
300         fctx->enc.mac_len = mac_len;
301         se_ctx->mac_len = mac_len;
302
303         if (key_len) {
304                 se_ctx->hmac = 1;
305
306                 se_ctx->auth_key = plt_zmalloc(key_len, 8);
307                 if (se_ctx->auth_key == NULL)
308                         return -1;
309
310                 memcpy(se_ctx->auth_key, key, key_len);
311                 se_ctx->auth_key_len = key_len;
312                 memset(fctx->hmac.ipad, 0, sizeof(fctx->hmac.ipad));
313                 memset(fctx->hmac.opad, 0, sizeof(fctx->hmac.opad));
314
315                 if (key_len <= 64)
316                         memcpy(fctx->hmac.opad, key, key_len);
317                 fctx->enc.auth_input_type = 1;
318         }
319         return 0;
320 }
321
322 int
323 roc_se_ciph_key_set(struct roc_se_ctx *se_ctx, roc_se_cipher_type type,
324                     const uint8_t *key, uint16_t key_len, uint8_t *salt)
325 {
326         struct roc_se_zuc_snow3g_ctx *zs_ctx = &se_ctx->se_ctx.zs_ctx;
327         struct roc_se_context *fctx = &se_ctx->se_ctx.fctx;
328         uint8_t *zuc_const;
329         uint32_t keyx[4];
330         uint8_t *ci_key;
331         int ret;
332
333         if (roc_model_is_cn9k()) {
334                 ci_key = zs_ctx->zuc.onk_ctx.ci_key;
335                 zuc_const = zs_ctx->zuc.onk_ctx.zuc_const;
336         } else {
337                 ci_key = zs_ctx->zuc.otk_ctx.ci_key;
338                 zuc_const = zs_ctx->zuc.otk_ctx.zuc_const;
339         }
340
341         /* For AES-GCM, salt is taken from ctx even if IV source
342          * is from DPTR
343          */
344         if ((salt != NULL) && (type == ROC_SE_AES_GCM)) {
345                 memcpy(fctx->enc.encr_iv, salt, 4);
346                 /* Assuming it was just salt update
347                  * and nothing else
348                  */
349                 if (key == NULL)
350                         return 0;
351         }
352
353         ret = cpt_ciph_type_set(type, se_ctx, key_len);
354         if (unlikely(ret))
355                 return -1;
356
357         if (se_ctx->fc_type == ROC_SE_FC_GEN) {
358                 /*
359                  * We need to always say IV is from DPTR as user can
360                  * sometimes iverride IV per operation.
361                  */
362                 fctx->enc.iv_source = ROC_SE_FROM_DPTR;
363
364                 if (se_ctx->auth_key_len > 64)
365                         return -1;
366         }
367
368         switch (type) {
369         case ROC_SE_PASSTHROUGH:
370                 se_ctx->enc_cipher = 0;
371                 fctx->enc.enc_cipher = 0;
372                 goto success;
373         case ROC_SE_DES3_CBC:
374                 /* CPT performs DES using 3DES with the 8B DES-key
375                  * replicated 2 more times to match the 24B 3DES-key.
376                  * Eg. If org. key is "0x0a 0x0b", then new key is
377                  * "0x0a 0x0b 0x0a 0x0b 0x0a 0x0b"
378                  */
379                 if (key_len == 8) {
380                         /* Skipping the first 8B as it will be copied
381                          * in the regular code flow
382                          */
383                         memcpy(fctx->enc.encr_key + key_len, key, key_len);
384                         memcpy(fctx->enc.encr_key + 2 * key_len, key, key_len);
385                 }
386                 break;
387         case ROC_SE_DES3_ECB:
388                 /* For DES3_ECB IV need to be from CTX. */
389                 fctx->enc.iv_source = ROC_SE_FROM_CTX;
390                 break;
391         case ROC_SE_AES_CBC:
392         case ROC_SE_AES_ECB:
393         case ROC_SE_AES_CFB:
394         case ROC_SE_AES_CTR:
395         case ROC_SE_CHACHA20:
396                 cpt_ciph_aes_key_type_set(fctx, key_len);
397                 break;
398         case ROC_SE_AES_GCM:
399                 cpt_ciph_aes_key_type_set(fctx, key_len);
400                 break;
401         case ROC_SE_AES_XTS:
402                 key_len = key_len / 2;
403                 cpt_ciph_aes_key_type_set(fctx, key_len);
404
405                 /* Copy key2 for XTS into ipad */
406                 memset(fctx->hmac.ipad, 0, sizeof(fctx->hmac.ipad));
407                 memcpy(fctx->hmac.ipad, &key[key_len], key_len);
408                 break;
409         case ROC_SE_SNOW3G_UEA2:
410                 zs_ctx->zuc.otk_ctx.w0.s.key_len = ROC_SE_AES_128_BIT;
411                 zs_ctx->zuc.otk_ctx.w0.s.alg_type = ROC_SE_PDCP_ALG_TYPE_SNOW3G;
412                 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_SNOW3G;
413                 cpt_snow3g_key_gen(key, keyx);
414                 memcpy(ci_key, keyx, key_len);
415                 se_ctx->zsk_flags = 0;
416                 goto success;
417         case ROC_SE_ZUC_EEA3:
418                 ret = cpt_pdcp_key_type_set(zs_ctx, key_len);
419                 if (ret)
420                         return ret;
421                 zs_ctx->zuc.otk_ctx.w0.s.alg_type = ROC_SE_PDCP_ALG_TYPE_ZUC;
422                 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_ZUC;
423                 memcpy(ci_key, key, key_len);
424                 memcpy(zuc_const, zuc_d, 32);
425                 se_ctx->zsk_flags = 0;
426                 goto success;
427         case ROC_SE_AES_CTR_EEA2:
428                 zs_ctx->zuc.otk_ctx.w0.s.key_len = ROC_SE_AES_128_BIT;
429                 zs_ctx->zuc.otk_ctx.w0.s.alg_type =
430                         ROC_SE_PDCP_ALG_TYPE_AES_CTR;
431                 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_AES_CTR;
432                 memcpy(ci_key, key, key_len);
433                 se_ctx->zsk_flags = 0;
434                 goto success;
435         case ROC_SE_KASUMI_F8_ECB:
436                 se_ctx->k_ecb = 1;
437                 memcpy(se_ctx->se_ctx.k_ctx.ci_key, key, key_len);
438                 se_ctx->zsk_flags = 0;
439                 goto success;
440         case ROC_SE_KASUMI_F8_CBC:
441                 memcpy(se_ctx->se_ctx.k_ctx.ci_key, key, key_len);
442                 se_ctx->zsk_flags = 0;
443                 goto success;
444         default:
445                 return -1;
446         }
447
448         /* Only for ROC_SE_FC_GEN case */
449
450         /* For GMAC auth, cipher must be NULL */
451         if (se_ctx->hash_type != ROC_SE_GMAC_TYPE)
452                 fctx->enc.enc_cipher = type;
453
454         memcpy(fctx->enc.encr_key, key, key_len);
455
456 success:
457         se_ctx->enc_cipher = type;
458         if (se_ctx->fc_type == ROC_SE_PDCP) {
459                 if (roc_model_is_cn9k())
460                         se_ctx->template_w4.s.opcode_minor =
461                                 ((1 << 7) | (se_ctx->pdcp_alg_type << 5) |
462                                  (se_ctx->zsk_flags & 0x7));
463                 else
464                         se_ctx->template_w4.s.opcode_minor = ((1 << 4));
465         }
466         return 0;
467 }
468
469 void
470 roc_se_ctx_swap(struct roc_se_ctx *se_ctx)
471 {
472         struct roc_se_zuc_snow3g_ctx *zs_ctx = &se_ctx->se_ctx.zs_ctx;
473
474         if (roc_model_is_cn9k())
475                 return;
476
477         zs_ctx->zuc.otk_ctx.w0.u64 = htobe64(zs_ctx->zuc.otk_ctx.w0.u64);
478 }