common/cnxk: allocate auth key dynamically
[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         case ROC_SE_SNOW3G_UEA2:
76                 if (unlikely(key_len != 16))
77                         return -1;
78                 /* No support for AEAD yet */
79                 if (unlikely(ctx->hash_type))
80                         return -1;
81                 fc_type = ROC_SE_PDCP;
82                 break;
83         case ROC_SE_AES_CTR_EEA2:
84                 fc_type = ROC_SE_PDCP;
85                 break;
86         case ROC_SE_KASUMI_F8_CBC:
87         case ROC_SE_KASUMI_F8_ECB:
88                 if (unlikely(key_len != 16))
89                         return -1;
90                 /* No support for AEAD yet */
91                 if (unlikely(ctx->hash_type))
92                         return -1;
93                 fc_type = ROC_SE_KASUMI;
94                 break;
95         default:
96                 return -1;
97         }
98
99         ctx->fc_type = fc_type;
100         return 0;
101 }
102
103 static inline void
104 cpt_ciph_aes_key_type_set(struct roc_se_context *fctx, uint16_t key_len)
105 {
106         roc_se_aes_type aes_key_type = 0;
107
108         switch (key_len) {
109         case 16:
110                 aes_key_type = ROC_SE_AES_128_BIT;
111                 break;
112         case 24:
113                 aes_key_type = ROC_SE_AES_192_BIT;
114                 break;
115         case 32:
116                 aes_key_type = ROC_SE_AES_256_BIT;
117                 break;
118         default:
119                 /* This should not happen */
120                 plt_err("Invalid AES key len");
121                 return;
122         }
123         fctx->enc.aes_key = aes_key_type;
124 }
125
126 int
127 roc_se_auth_key_set(struct roc_se_ctx *se_ctx, roc_se_auth_type type,
128                     const uint8_t *key, uint16_t key_len, uint16_t mac_len)
129 {
130         struct roc_se_zuc_snow3g_ctx *zs_ctx;
131         struct roc_se_kasumi_ctx *k_ctx;
132         struct roc_se_context *fctx;
133
134         if (se_ctx == NULL)
135                 return -1;
136
137         zs_ctx = &se_ctx->se_ctx.zs_ctx;
138         k_ctx = &se_ctx->se_ctx.k_ctx;
139         fctx = &se_ctx->se_ctx.fctx;
140
141         if ((type >= ROC_SE_ZUC_EIA3) && (type <= ROC_SE_KASUMI_F9_ECB)) {
142                 uint32_t keyx[4];
143
144                 if (key_len != 16)
145                         return -1;
146                 /* No support for AEAD yet */
147                 if (se_ctx->enc_cipher)
148                         return -1;
149                 /* For ZUC/SNOW3G/Kasumi */
150                 switch (type) {
151                 case ROC_SE_SNOW3G_UIA2:
152                         se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_SNOW3G;
153                         cpt_snow3g_key_gen(key, keyx);
154                         memcpy(zs_ctx->ci_key, keyx, key_len);
155                         se_ctx->fc_type = ROC_SE_PDCP;
156                         se_ctx->zsk_flags = 0x1;
157                         break;
158                 case ROC_SE_ZUC_EIA3:
159                         se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_ZUC;
160                         memcpy(zs_ctx->ci_key, key, key_len);
161                         memcpy(zs_ctx->zuc_const, zuc_d, 32);
162                         se_ctx->fc_type = ROC_SE_PDCP;
163                         se_ctx->zsk_flags = 0x1;
164                         break;
165                 case ROC_SE_AES_CMAC_EIA2:
166                         se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_AES_CTR;
167                         memcpy(zs_ctx->ci_key, key, key_len);
168                         se_ctx->fc_type = ROC_SE_PDCP;
169                         se_ctx->zsk_flags = 0x1;
170                         break;
171                 case ROC_SE_KASUMI_F9_ECB:
172                         /* Kasumi ECB mode */
173                         se_ctx->k_ecb = 1;
174                         memcpy(k_ctx->ci_key, key, key_len);
175                         se_ctx->fc_type = ROC_SE_KASUMI;
176                         se_ctx->zsk_flags = 0x1;
177                         break;
178                 case ROC_SE_KASUMI_F9_CBC:
179                         memcpy(k_ctx->ci_key, key, key_len);
180                         se_ctx->fc_type = ROC_SE_KASUMI;
181                         se_ctx->zsk_flags = 0x1;
182                         break;
183                 default:
184                         return -1;
185                 }
186                 se_ctx->mac_len = 4;
187                 se_ctx->hash_type = type;
188                 return 0;
189         }
190
191         if (!se_ctx->fc_type ||
192             (type && type != ROC_SE_GMAC_TYPE && !se_ctx->enc_cipher))
193                 se_ctx->fc_type = ROC_SE_HASH_HMAC;
194
195         if (se_ctx->fc_type == ROC_SE_FC_GEN && key_len > 64)
196                 return -1;
197
198         /* For GMAC auth, cipher must be NULL */
199         if (type == ROC_SE_GMAC_TYPE)
200                 fctx->enc.enc_cipher = 0;
201
202         fctx->enc.hash_type = type;
203         se_ctx->hash_type = type;
204         fctx->enc.mac_len = mac_len;
205         se_ctx->mac_len = mac_len;
206
207         if (key_len) {
208                 se_ctx->hmac = 1;
209
210                 se_ctx->auth_key = plt_zmalloc(key_len, 8);
211                 if (se_ctx->auth_key == NULL)
212                         return -1;
213
214                 memcpy(se_ctx->auth_key, key, key_len);
215                 se_ctx->auth_key_len = key_len;
216                 memset(fctx->hmac.ipad, 0, sizeof(fctx->hmac.ipad));
217                 memset(fctx->hmac.opad, 0, sizeof(fctx->hmac.opad));
218
219                 if (key_len <= 64)
220                         memcpy(fctx->hmac.opad, key, key_len);
221                 fctx->enc.auth_input_type = 1;
222         }
223         return 0;
224 }
225
226 int
227 roc_se_ciph_key_set(struct roc_se_ctx *se_ctx, roc_se_cipher_type type,
228                     const uint8_t *key, uint16_t key_len, uint8_t *salt)
229 {
230         struct roc_se_context *fctx = &se_ctx->se_ctx.fctx;
231         struct roc_se_zuc_snow3g_ctx *zs_ctx;
232         uint32_t keyx[4];
233         int ret;
234
235         /* For AES-GCM, salt is taken from ctx even if IV source
236          * is from DPTR
237          */
238         if ((salt != NULL) && (type == ROC_SE_AES_GCM)) {
239                 memcpy(fctx->enc.encr_iv, salt, 4);
240                 /* Assuming it was just salt update
241                  * and nothing else
242                  */
243                 if (key == NULL)
244                         return 0;
245         }
246
247         ret = cpt_ciph_type_set(type, se_ctx, key_len);
248         if (unlikely(ret))
249                 return -1;
250
251         if (se_ctx->fc_type == ROC_SE_FC_GEN) {
252                 /*
253                  * We need to always say IV is from DPTR as user can
254                  * sometimes iverride IV per operation.
255                  */
256                 fctx->enc.iv_source = ROC_SE_FROM_DPTR;
257
258                 if (se_ctx->auth_key_len > 64)
259                         return -1;
260         }
261
262         switch (type) {
263         case ROC_SE_PASSTHROUGH:
264                 se_ctx->enc_cipher = 0;
265                 fctx->enc.enc_cipher = 0;
266                 goto success;
267         case ROC_SE_DES3_CBC:
268                 /* CPT performs DES using 3DES with the 8B DES-key
269                  * replicated 2 more times to match the 24B 3DES-key.
270                  * Eg. If org. key is "0x0a 0x0b", then new key is
271                  * "0x0a 0x0b 0x0a 0x0b 0x0a 0x0b"
272                  */
273                 if (key_len == 8) {
274                         /* Skipping the first 8B as it will be copied
275                          * in the regular code flow
276                          */
277                         memcpy(fctx->enc.encr_key + key_len, key, key_len);
278                         memcpy(fctx->enc.encr_key + 2 * key_len, key, key_len);
279                 }
280                 break;
281         case ROC_SE_DES3_ECB:
282                 /* For DES3_ECB IV need to be from CTX. */
283                 fctx->enc.iv_source = ROC_SE_FROM_CTX;
284                 break;
285         case ROC_SE_AES_CBC:
286         case ROC_SE_AES_ECB:
287         case ROC_SE_AES_CFB:
288         case ROC_SE_AES_CTR:
289         case ROC_SE_CHACHA20:
290                 cpt_ciph_aes_key_type_set(fctx, key_len);
291                 break;
292         case ROC_SE_AES_GCM:
293                 cpt_ciph_aes_key_type_set(fctx, key_len);
294                 break;
295         case ROC_SE_AES_XTS:
296                 key_len = key_len / 2;
297                 cpt_ciph_aes_key_type_set(fctx, key_len);
298
299                 /* Copy key2 for XTS into ipad */
300                 memset(fctx->hmac.ipad, 0, sizeof(fctx->hmac.ipad));
301                 memcpy(fctx->hmac.ipad, &key[key_len], key_len);
302                 break;
303         case ROC_SE_SNOW3G_UEA2:
304                 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_SNOW3G;
305                 cpt_snow3g_key_gen(key, keyx);
306                 memcpy(se_ctx->se_ctx.zs_ctx.ci_key, keyx, key_len);
307                 se_ctx->zsk_flags = 0;
308                 goto success;
309         case ROC_SE_ZUC_EEA3:
310                 zs_ctx = &se_ctx->se_ctx.zs_ctx;
311                 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_ZUC;
312                 memcpy(zs_ctx->ci_key, key, key_len);
313                 memcpy(zs_ctx->zuc_const, zuc_d, 32);
314                 se_ctx->zsk_flags = 0;
315                 goto success;
316         case ROC_SE_AES_CTR_EEA2:
317                 se_ctx->pdcp_alg_type = ROC_SE_PDCP_ALG_TYPE_AES_CTR;
318                 memcpy(se_ctx->se_ctx.zs_ctx.ci_key, key, key_len);
319                 se_ctx->zsk_flags = 0;
320                 goto success;
321         case ROC_SE_KASUMI_F8_ECB:
322                 se_ctx->k_ecb = 1;
323                 memcpy(se_ctx->se_ctx.k_ctx.ci_key, key, key_len);
324                 se_ctx->zsk_flags = 0;
325                 goto success;
326         case ROC_SE_KASUMI_F8_CBC:
327                 memcpy(se_ctx->se_ctx.k_ctx.ci_key, key, key_len);
328                 se_ctx->zsk_flags = 0;
329                 goto success;
330         default:
331                 return -1;
332         }
333
334         /* Only for ROC_SE_FC_GEN case */
335
336         /* For GMAC auth, cipher must be NULL */
337         if (se_ctx->hash_type != ROC_SE_GMAC_TYPE)
338                 fctx->enc.enc_cipher = type;
339
340         memcpy(fctx->enc.encr_key, key, key_len);
341
342 success:
343         se_ctx->enc_cipher = type;
344
345         return 0;
346 }