common/cnxk: update to v1.13 ZUC API
[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         switch (mac_len) {
164         case 4:
165                 mac_type = ROC_SE_PDCP_MAC_LEN_32_BIT;
166                 break;
167         case 8:
168                 mac_type = ROC_SE_PDCP_MAC_LEN_64_BIT;
169                 break;
170         case 16:
171                 mac_type = ROC_SE_PDCP_MAC_LEN_128_BIT;
172                 break;
173         default:
174                 plt_err("Invalid ZUC MAC len");
175                 return -ENOTSUP;
176         }
177         zs_ctx->zuc.otk_ctx.w0.s.mac_len = mac_type;
178         return 0;
179 }
180
181 int
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)
184 {
185         struct roc_se_zuc_snow3g_ctx *zs_ctx;
186         struct roc_se_kasumi_ctx *k_ctx;
187         struct roc_se_context *fctx;
188         int ret;
189
190         if (se_ctx == NULL)
191                 return -1;
192
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;
196
197         if ((type >= ROC_SE_ZUC_EIA3) && (type <= ROC_SE_KASUMI_F9_ECB)) {
198                 uint8_t *zuc_const;
199                 uint32_t keyx[4];
200                 uint8_t *ci_key;
201
202                 if (!key_len)
203                         return -1;
204
205                 /* No support for chained operations yet */
206                 if (se_ctx->enc_cipher)
207                         return -1;
208
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;
212                 } else {
213                         ci_key = zs_ctx->zuc.otk_ctx.ci_key;
214                         zuc_const = zs_ctx->zuc.otk_ctx.zuc_const;
215                 }
216
217                 /* For ZUC/SNOW3G/Kasumi */
218                 switch (type) {
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;
229                         break;
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);
234                         if (ret)
235                                 return ret;
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;
241                         break;
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;
251                         break;
252                 case ROC_SE_KASUMI_F9_ECB:
253                         /* Kasumi ECB mode */
254                         se_ctx->k_ecb = 1;
255                         memcpy(k_ctx->ci_key, key, key_len);
256                         se_ctx->fc_type = ROC_SE_KASUMI;
257                         se_ctx->zsk_flags = 0x1;
258                         break;
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;
263                         break;
264                 default:
265                         return -1;
266                 }
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);
272                 else
273                         se_ctx->template_w4.s.opcode_minor = ((1 << 4) | 1);
274                 return 0;
275         }
276
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;
280
281         if (se_ctx->fc_type == ROC_SE_FC_GEN && key_len > 64)
282                 return -1;
283
284         /* For GMAC auth, cipher must be NULL */
285         if (type == ROC_SE_GMAC_TYPE)
286                 fctx->enc.enc_cipher = 0;
287
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;
292
293         if (key_len) {
294                 se_ctx->hmac = 1;
295
296                 se_ctx->auth_key = plt_zmalloc(key_len, 8);
297                 if (se_ctx->auth_key == NULL)
298                         return -1;
299
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));
304
305                 if (key_len <= 64)
306                         memcpy(fctx->hmac.opad, key, key_len);
307                 fctx->enc.auth_input_type = 1;
308         }
309         return 0;
310 }
311
312 int
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)
315 {
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;
318         uint8_t *zuc_const;
319         uint32_t keyx[4];
320         uint8_t *ci_key;
321         int ret;
322
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;
326         } else {
327                 ci_key = zs_ctx->zuc.otk_ctx.ci_key;
328                 zuc_const = zs_ctx->zuc.otk_ctx.zuc_const;
329         }
330
331         /* For AES-GCM, salt is taken from ctx even if IV source
332          * is from DPTR
333          */
334         if ((salt != NULL) && (type == ROC_SE_AES_GCM)) {
335                 memcpy(fctx->enc.encr_iv, salt, 4);
336                 /* Assuming it was just salt update
337                  * and nothing else
338                  */
339                 if (key == NULL)
340                         return 0;
341         }
342
343         ret = cpt_ciph_type_set(type, se_ctx, key_len);
344         if (unlikely(ret))
345                 return -1;
346
347         if (se_ctx->fc_type == ROC_SE_FC_GEN) {
348                 /*
349                  * We need to always say IV is from DPTR as user can
350                  * sometimes iverride IV per operation.
351                  */
352                 fctx->enc.iv_source = ROC_SE_FROM_DPTR;
353
354                 if (se_ctx->auth_key_len > 64)
355                         return -1;
356         }
357
358         switch (type) {
359         case ROC_SE_PASSTHROUGH:
360                 se_ctx->enc_cipher = 0;
361                 fctx->enc.enc_cipher = 0;
362                 goto success;
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"
368                  */
369                 if (key_len == 8) {
370                         /* Skipping the first 8B as it will be copied
371                          * in the regular code flow
372                          */
373                         memcpy(fctx->enc.encr_key + key_len, key, key_len);
374                         memcpy(fctx->enc.encr_key + 2 * key_len, key, key_len);
375                 }
376                 break;
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;
380                 break;
381         case ROC_SE_AES_CBC:
382         case ROC_SE_AES_ECB:
383         case ROC_SE_AES_CFB:
384         case ROC_SE_AES_CTR:
385         case ROC_SE_CHACHA20:
386                 cpt_ciph_aes_key_type_set(fctx, key_len);
387                 break;
388         case ROC_SE_AES_GCM:
389                 cpt_ciph_aes_key_type_set(fctx, key_len);
390                 break;
391         case ROC_SE_AES_XTS:
392                 key_len = key_len / 2;
393                 cpt_ciph_aes_key_type_set(fctx, key_len);
394
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);
398                 break;
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;
406                 goto success;
407         case ROC_SE_ZUC_EEA3:
408                 ret = cpt_pdcp_key_type_set(zs_ctx, key_len);
409                 if (ret)
410                         return ret;
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;
416                 goto success;
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;
424                 goto success;
425         case ROC_SE_KASUMI_F8_ECB:
426                 se_ctx->k_ecb = 1;
427                 memcpy(se_ctx->se_ctx.k_ctx.ci_key, key, key_len);
428                 se_ctx->zsk_flags = 0;
429                 goto success;
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;
433                 goto success;
434         default:
435                 return -1;
436         }
437
438         /* Only for ROC_SE_FC_GEN case */
439
440         /* For GMAC auth, cipher must be NULL */
441         if (se_ctx->hash_type != ROC_SE_GMAC_TYPE)
442                 fctx->enc.enc_cipher = type;
443
444         memcpy(fctx->enc.encr_key, key, key_len);
445
446 success:
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));
453                 else
454                         se_ctx->template_w4.s.opcode_minor = ((1 << 4));
455         }
456         return 0;
457 }
458
459 void
460 roc_se_ctx_swap(struct roc_se_ctx *se_ctx)
461 {
462         struct roc_se_zuc_snow3g_ctx *zs_ctx = &se_ctx->se_ctx.zs_ctx;
463
464         if (roc_model_is_cn9k())
465                 return;
466
467         zs_ctx->zuc.otk_ctx.w0.u64 = htobe64(zs_ctx->zuc.otk_ctx.w0.u64);
468 }