common/cnxk: add err ctl in SA
[dpdk.git] / drivers / common / cnxk / cnxk_security.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #include <rte_udp.h>
6
7 #include "cnxk_security.h"
8
9 #include "roc_api.h"
10
11 static void
12 ipsec_hmac_opad_ipad_gen(struct rte_crypto_sym_xform *auth_xform,
13                          uint8_t *hmac_opad_ipad)
14 {
15         const uint8_t *key = auth_xform->auth.key.data;
16         uint32_t length = auth_xform->auth.key.length;
17         uint8_t opad[128] = {[0 ... 127] = 0x5c};
18         uint8_t ipad[128] = {[0 ... 127] = 0x36};
19         uint32_t i;
20
21         /* HMAC OPAD and IPAD */
22         for (i = 0; i < 127 && i < length; i++) {
23                 opad[i] = opad[i] ^ key[i];
24                 ipad[i] = ipad[i] ^ key[i];
25         }
26
27         /* Precompute hash of HMAC OPAD and IPAD to avoid
28          * per packet computation
29          */
30         switch (auth_xform->auth.algo) {
31         case RTE_CRYPTO_AUTH_SHA1_HMAC:
32                 roc_hash_sha1_gen(opad, (uint32_t *)&hmac_opad_ipad[0]);
33                 roc_hash_sha1_gen(ipad, (uint32_t *)&hmac_opad_ipad[24]);
34                 break;
35         case RTE_CRYPTO_AUTH_SHA256_HMAC:
36                 roc_hash_sha256_gen(opad, (uint32_t *)&hmac_opad_ipad[0]);
37                 roc_hash_sha256_gen(ipad, (uint32_t *)&hmac_opad_ipad[64]);
38                 break;
39         case RTE_CRYPTO_AUTH_SHA384_HMAC:
40                 roc_hash_sha512_gen(opad, (uint64_t *)&hmac_opad_ipad[0], 384);
41                 roc_hash_sha512_gen(ipad, (uint64_t *)&hmac_opad_ipad[64], 384);
42                 break;
43         case RTE_CRYPTO_AUTH_SHA512_HMAC:
44                 roc_hash_sha512_gen(opad, (uint64_t *)&hmac_opad_ipad[0], 512);
45                 roc_hash_sha512_gen(ipad, (uint64_t *)&hmac_opad_ipad[64], 512);
46                 break;
47         default:
48                 break;
49         }
50 }
51
52 static int
53 ot_ipsec_sa_common_param_fill(union roc_ot_ipsec_sa_word2 *w2,
54                               uint8_t *cipher_key, uint8_t *salt_key,
55                               uint8_t *hmac_opad_ipad,
56                               struct rte_security_ipsec_xform *ipsec_xfrm,
57                               struct rte_crypto_sym_xform *crypto_xfrm)
58 {
59         struct rte_crypto_sym_xform *auth_xfrm, *cipher_xfrm;
60         const uint8_t *key;
61         uint32_t *tmp_salt;
62         uint64_t *tmp_key;
63         int length, i;
64
65         /* Set direction */
66         switch (ipsec_xfrm->direction) {
67         case RTE_SECURITY_IPSEC_SA_DIR_INGRESS:
68                 w2->s.dir = ROC_IE_SA_DIR_INBOUND;
69                 auth_xfrm = crypto_xfrm;
70                 cipher_xfrm = crypto_xfrm->next;
71                 break;
72         case RTE_SECURITY_IPSEC_SA_DIR_EGRESS:
73                 w2->s.dir = ROC_IE_SA_DIR_OUTBOUND;
74                 cipher_xfrm = crypto_xfrm;
75                 auth_xfrm = crypto_xfrm->next;
76                 break;
77         default:
78                 return -EINVAL;
79         }
80
81         /* Set protocol - ESP vs AH */
82         switch (ipsec_xfrm->proto) {
83         case RTE_SECURITY_IPSEC_SA_PROTO_ESP:
84                 w2->s.protocol = ROC_IE_SA_PROTOCOL_ESP;
85                 break;
86         case RTE_SECURITY_IPSEC_SA_PROTO_AH:
87                 w2->s.protocol = ROC_IE_SA_PROTOCOL_AH;
88                 break;
89         default:
90                 return -EINVAL;
91         }
92
93         /* Set mode - transport vs tunnel */
94         switch (ipsec_xfrm->mode) {
95         case RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT:
96                 w2->s.mode = ROC_IE_SA_MODE_TRANSPORT;
97                 break;
98         case RTE_SECURITY_IPSEC_SA_MODE_TUNNEL:
99                 w2->s.mode = ROC_IE_SA_MODE_TUNNEL;
100                 break;
101         default:
102                 return -EINVAL;
103         }
104
105         /* Set encryption algorithm */
106         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
107                 key = crypto_xfrm->aead.key.data;
108                 length = crypto_xfrm->aead.key.length;
109
110                 switch (crypto_xfrm->aead.algo) {
111                 case RTE_CRYPTO_AEAD_AES_GCM:
112                         w2->s.enc_type = ROC_IE_OT_SA_ENC_AES_GCM;
113                         w2->s.auth_type = ROC_IE_OT_SA_AUTH_NULL;
114                         memcpy(salt_key, &ipsec_xfrm->salt, 4);
115                         tmp_salt = (uint32_t *)salt_key;
116                         *tmp_salt = rte_be_to_cpu_32(*tmp_salt);
117                         break;
118                 default:
119                         return -ENOTSUP;
120                 }
121         } else {
122                 switch (cipher_xfrm->cipher.algo) {
123                 case RTE_CRYPTO_CIPHER_NULL:
124                         w2->s.enc_type = ROC_IE_OT_SA_ENC_NULL;
125                         break;
126                 case RTE_CRYPTO_CIPHER_AES_CBC:
127                         w2->s.enc_type = ROC_IE_OT_SA_ENC_AES_CBC;
128                         break;
129                 case RTE_CRYPTO_CIPHER_AES_CTR:
130                         w2->s.enc_type = ROC_IE_OT_SA_ENC_AES_CTR;
131                         break;
132                 default:
133                         return -ENOTSUP;
134                 }
135
136                 switch (auth_xfrm->auth.algo) {
137                 case RTE_CRYPTO_AUTH_NULL:
138                         w2->s.auth_type = ROC_IE_OT_SA_AUTH_NULL;
139                         break;
140                 case RTE_CRYPTO_AUTH_SHA1_HMAC:
141                         w2->s.auth_type = ROC_IE_OT_SA_AUTH_SHA1;
142                         break;
143                 case RTE_CRYPTO_AUTH_SHA256_HMAC:
144                         w2->s.auth_type = ROC_IE_OT_SA_AUTH_SHA2_256;
145                         break;
146                 case RTE_CRYPTO_AUTH_SHA384_HMAC:
147                         w2->s.auth_type = ROC_IE_OT_SA_AUTH_SHA2_384;
148                         break;
149                 case RTE_CRYPTO_AUTH_SHA512_HMAC:
150                         w2->s.auth_type = ROC_IE_OT_SA_AUTH_SHA2_512;
151                         break;
152                 case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
153                         w2->s.auth_type = ROC_IE_OT_SA_AUTH_AES_XCBC_128;
154                         break;
155                 default:
156                         return -ENOTSUP;
157                 }
158
159                 if (auth_xfrm->auth.algo == RTE_CRYPTO_AUTH_AES_XCBC_MAC) {
160                         const uint8_t *auth_key = auth_xfrm->auth.key.data;
161                         roc_aes_xcbc_key_derive(auth_key, hmac_opad_ipad);
162                 } else {
163                         ipsec_hmac_opad_ipad_gen(auth_xfrm, hmac_opad_ipad);
164                 }
165
166                 tmp_key = (uint64_t *)hmac_opad_ipad;
167                 for (i = 0;
168                      i < (int)(ROC_CTX_MAX_OPAD_IPAD_LEN / sizeof(uint64_t));
169                      i++)
170                         tmp_key[i] = rte_be_to_cpu_64(tmp_key[i]);
171
172                 key = cipher_xfrm->cipher.key.data;
173                 length = cipher_xfrm->cipher.key.length;
174         }
175
176         /* Set encapsulation type */
177         if (ipsec_xfrm->options.udp_encap)
178                 w2->s.encap_type = ROC_IE_OT_SA_ENCAP_UDP;
179
180         w2->s.spi = ipsec_xfrm->spi;
181
182         /* Copy encryption key */
183         memcpy(cipher_key, key, length);
184         tmp_key = (uint64_t *)cipher_key;
185         for (i = 0; i < (int)(ROC_CTX_MAX_CKEY_LEN / sizeof(uint64_t)); i++)
186                 tmp_key[i] = rte_be_to_cpu_64(tmp_key[i]);
187
188         /* Set AES key length */
189         if (w2->s.enc_type == ROC_IE_OT_SA_ENC_AES_CBC ||
190             w2->s.enc_type == ROC_IE_OT_SA_ENC_AES_CCM ||
191             w2->s.enc_type == ROC_IE_OT_SA_ENC_AES_CTR ||
192             w2->s.enc_type == ROC_IE_OT_SA_ENC_AES_GCM ||
193             w2->s.auth_type == ROC_IE_OT_SA_AUTH_AES_GMAC) {
194                 switch (length) {
195                 case ROC_CPT_AES128_KEY_LEN:
196                         w2->s.aes_key_len = ROC_IE_SA_AES_KEY_LEN_128;
197                         break;
198                 case ROC_CPT_AES192_KEY_LEN:
199                         w2->s.aes_key_len = ROC_IE_SA_AES_KEY_LEN_192;
200                         break;
201                 case ROC_CPT_AES256_KEY_LEN:
202                         w2->s.aes_key_len = ROC_IE_SA_AES_KEY_LEN_256;
203                         break;
204                 default:
205                         plt_err("Invalid AES key length");
206                         return -EINVAL;
207                 }
208         }
209
210         if (ipsec_xfrm->life.packets_soft_limit != 0 ||
211             ipsec_xfrm->life.packets_hard_limit != 0) {
212                 if (ipsec_xfrm->life.bytes_soft_limit != 0 ||
213                     ipsec_xfrm->life.bytes_hard_limit != 0) {
214                         plt_err("Expiry tracking with both packets & bytes is not supported");
215                         return -EINVAL;
216                 }
217                 w2->s.life_unit = ROC_IE_OT_SA_LIFE_UNIT_PKTS;
218         }
219
220         if (ipsec_xfrm->life.bytes_soft_limit != 0 ||
221             ipsec_xfrm->life.bytes_hard_limit != 0) {
222                 if (ipsec_xfrm->life.packets_soft_limit != 0 ||
223                     ipsec_xfrm->life.packets_hard_limit != 0) {
224                         plt_err("Expiry tracking with both packets & bytes is not supported");
225                         return -EINVAL;
226                 }
227                 w2->s.life_unit = ROC_IE_OT_SA_LIFE_UNIT_OCTETS;
228         }
229
230         return 0;
231 }
232
233 static size_t
234 ot_ipsec_inb_ctx_size(struct roc_ot_ipsec_inb_sa *sa)
235 {
236         size_t size;
237
238         /* Variable based on Anti-replay Window */
239         size = offsetof(struct roc_ot_ipsec_inb_sa, ctx) +
240                offsetof(struct roc_ot_ipsec_inb_ctx_update_reg, ar_winbits);
241
242         if (sa->w0.s.ar_win)
243                 size += (1 << (sa->w0.s.ar_win - 1)) * sizeof(uint64_t);
244
245         return size;
246 }
247
248 static int
249 ot_ipsec_inb_tunnel_hdr_fill(struct roc_ot_ipsec_inb_sa *sa,
250                              struct rte_security_ipsec_xform *ipsec_xfrm)
251 {
252         struct rte_security_ipsec_tunnel_param *tunnel;
253
254         if (ipsec_xfrm->mode != RTE_SECURITY_IPSEC_SA_MODE_TUNNEL)
255                 return 0;
256
257         if (ipsec_xfrm->options.tunnel_hdr_verify == 0)
258                 return 0;
259
260         tunnel = &ipsec_xfrm->tunnel;
261
262         switch (tunnel->type) {
263         case RTE_SECURITY_IPSEC_TUNNEL_IPV4:
264                 sa->w2.s.outer_ip_ver = ROC_IE_SA_IP_VERSION_4;
265                 memcpy(&sa->outer_hdr.ipv4.src_addr, &tunnel->ipv4.src_ip,
266                        sizeof(struct in_addr));
267                 memcpy(&sa->outer_hdr.ipv4.dst_addr, &tunnel->ipv4.dst_ip,
268                        sizeof(struct in_addr));
269
270                 /* IP Source and Dest are in LE/CPU endian */
271                 sa->outer_hdr.ipv4.src_addr =
272                         rte_be_to_cpu_32(sa->outer_hdr.ipv4.src_addr);
273                 sa->outer_hdr.ipv4.dst_addr =
274                         rte_be_to_cpu_32(sa->outer_hdr.ipv4.dst_addr);
275
276                 break;
277         case RTE_SECURITY_IPSEC_TUNNEL_IPV6:
278                 sa->w2.s.outer_ip_ver = ROC_IE_SA_IP_VERSION_6;
279                 memcpy(&sa->outer_hdr.ipv6.src_addr, &tunnel->ipv6.src_addr,
280                        sizeof(struct in6_addr));
281                 memcpy(&sa->outer_hdr.ipv6.dst_addr, &tunnel->ipv6.dst_addr,
282                        sizeof(struct in6_addr));
283
284                 break;
285         default:
286                 return -EINVAL;
287         }
288
289         switch (ipsec_xfrm->options.tunnel_hdr_verify) {
290         case RTE_SECURITY_IPSEC_TUNNEL_VERIFY_DST_ADDR:
291                 sa->w2.s.ip_hdr_verify = ROC_IE_OT_SA_IP_HDR_VERIFY_DST_ADDR;
292                 break;
293         case RTE_SECURITY_IPSEC_TUNNEL_VERIFY_SRC_DST_ADDR:
294                 sa->w2.s.ip_hdr_verify =
295                         ROC_IE_OT_SA_IP_HDR_VERIFY_SRC_DST_ADDR;
296                 break;
297         default:
298                 return -ENOTSUP;
299         }
300
301         return 0;
302 }
303
304 int
305 cnxk_ot_ipsec_inb_sa_fill(struct roc_ot_ipsec_inb_sa *sa,
306                           struct rte_security_ipsec_xform *ipsec_xfrm,
307                           struct rte_crypto_sym_xform *crypto_xfrm)
308 {
309         union roc_ot_ipsec_sa_word2 w2;
310         uint32_t replay_win_sz;
311         size_t offset;
312         int rc;
313
314         w2.u64 = 0;
315         rc = ot_ipsec_sa_common_param_fill(&w2, sa->cipher_key, sa->w8.s.salt,
316                                            sa->hmac_opad_ipad, ipsec_xfrm,
317                                            crypto_xfrm);
318         if (rc)
319                 return rc;
320
321         /* Updata common word2 data */
322         sa->w2.u64 = w2.u64;
323
324         /* Only support power-of-two window sizes supported */
325         replay_win_sz = ipsec_xfrm->replay_win_sz;
326         if (replay_win_sz) {
327                 if (!rte_is_power_of_2(replay_win_sz) ||
328                     replay_win_sz > ROC_AR_WIN_SIZE_MAX)
329                         return -ENOTSUP;
330
331                 sa->w0.s.ar_win = rte_log2_u32(replay_win_sz) - 5;
332         }
333
334         rc = ot_ipsec_inb_tunnel_hdr_fill(sa, ipsec_xfrm);
335         if (rc)
336                 return rc;
337
338         /* Default options for pkt_out and pkt_fmt are with
339          * second pass meta and no defrag.
340          */
341         sa->w0.s.pkt_format = ROC_IE_OT_SA_PKT_FMT_META;
342         sa->w0.s.pkt_output = ROC_IE_OT_SA_PKT_OUTPUT_HW_BASED_DEFRAG;
343         sa->w0.s.pkind = ROC_OT_CPT_META_PKIND;
344
345         /* ESN */
346         sa->w2.s.esn_en = !!ipsec_xfrm->options.esn;
347         if (ipsec_xfrm->options.udp_encap) {
348                 sa->w10.s.udp_src_port = 4500;
349                 sa->w10.s.udp_dst_port = 4500;
350         }
351
352         if (ipsec_xfrm->options.udp_ports_verify)
353                 sa->w2.s.udp_ports_verify = 1;
354
355         offset = offsetof(struct roc_ot_ipsec_inb_sa, ctx);
356         /* Word offset for HW managed SA field */
357         sa->w0.s.hw_ctx_off = offset / 8;
358         /* Context push size for inbound spans up to hw_ctx including
359          * ar_base field, in 8b units
360          */
361         sa->w0.s.ctx_push_size = sa->w0.s.hw_ctx_off + 1;
362         /* Entire context size in 128B units */
363         sa->w0.s.ctx_size =
364                 (PLT_ALIGN_CEIL(ot_ipsec_inb_ctx_size(sa), ROC_CTX_UNIT_128B) /
365                  ROC_CTX_UNIT_128B) -
366                 1;
367
368         /**
369          * CPT MC triggers expiry when counter value changes from 2 to 1. To
370          * mitigate this behaviour add 1 to the life counter values provided.
371          */
372
373         if (ipsec_xfrm->life.bytes_soft_limit) {
374                 sa->ctx.soft_life = ipsec_xfrm->life.bytes_soft_limit + 1;
375                 sa->w0.s.soft_life_dec = 1;
376         }
377
378         if (ipsec_xfrm->life.packets_soft_limit) {
379                 sa->ctx.soft_life = ipsec_xfrm->life.packets_soft_limit + 1;
380                 sa->w0.s.soft_life_dec = 1;
381         }
382
383         if (ipsec_xfrm->life.bytes_hard_limit) {
384                 sa->ctx.hard_life = ipsec_xfrm->life.bytes_hard_limit + 1;
385                 sa->w0.s.hard_life_dec = 1;
386         }
387
388         if (ipsec_xfrm->life.packets_hard_limit) {
389                 sa->ctx.hard_life = ipsec_xfrm->life.packets_hard_limit + 1;
390                 sa->w0.s.hard_life_dec = 1;
391         }
392
393         /* There are two words of CPT_CTX_HW_S for ucode to skip */
394         sa->w0.s.ctx_hdr_size = 1;
395         sa->w0.s.aop_valid = 1;
396         sa->w0.s.et_ovrwr = 1;
397
398         rte_wmb();
399
400         /* Enable SA */
401         sa->w2.s.valid = 1;
402         return 0;
403 }
404
405 int
406 cnxk_ot_ipsec_outb_sa_fill(struct roc_ot_ipsec_outb_sa *sa,
407                            struct rte_security_ipsec_xform *ipsec_xfrm,
408                            struct rte_crypto_sym_xform *crypto_xfrm)
409 {
410         struct rte_security_ipsec_tunnel_param *tunnel = &ipsec_xfrm->tunnel;
411         union roc_ot_ipsec_sa_word2 w2;
412         size_t offset;
413         int rc;
414
415         w2.u64 = 0;
416         rc = ot_ipsec_sa_common_param_fill(&w2, sa->cipher_key, sa->iv.s.salt,
417                                            sa->hmac_opad_ipad, ipsec_xfrm,
418                                            crypto_xfrm);
419         if (rc)
420                 return rc;
421
422         /* Update common word2 data */
423         sa->w2.u64 = w2.u64;
424
425         if (ipsec_xfrm->mode != RTE_SECURITY_IPSEC_SA_MODE_TUNNEL)
426                 goto skip_tunnel_info;
427
428         /* Tunnel header info */
429         switch (tunnel->type) {
430         case RTE_SECURITY_IPSEC_TUNNEL_IPV4:
431                 sa->w2.s.outer_ip_ver = ROC_IE_SA_IP_VERSION_4;
432                 memcpy(&sa->outer_hdr.ipv4.src_addr, &tunnel->ipv4.src_ip,
433                        sizeof(struct in_addr));
434                 memcpy(&sa->outer_hdr.ipv4.dst_addr, &tunnel->ipv4.dst_ip,
435                        sizeof(struct in_addr));
436
437                 /* IP Source and Dest seems to be in LE/CPU endian */
438                 sa->outer_hdr.ipv4.src_addr =
439                         rte_be_to_cpu_32(sa->outer_hdr.ipv4.src_addr);
440                 sa->outer_hdr.ipv4.dst_addr =
441                         rte_be_to_cpu_32(sa->outer_hdr.ipv4.dst_addr);
442
443                 /* Outer header DF bit source */
444                 if (!ipsec_xfrm->options.copy_df) {
445                         sa->w2.s.ipv4_df_src_or_ipv6_flw_lbl_src =
446                                 ROC_IE_OT_SA_COPY_FROM_SA;
447                         sa->w10.s.ipv4_df_or_ipv6_flw_lbl = tunnel->ipv4.df;
448                 } else {
449                         sa->w2.s.ipv4_df_src_or_ipv6_flw_lbl_src =
450                                 ROC_IE_OT_SA_COPY_FROM_INNER_IP_HDR;
451                 }
452
453                 /* Outer header DSCP source */
454                 if (!ipsec_xfrm->options.copy_dscp) {
455                         sa->w2.s.dscp_src = ROC_IE_OT_SA_COPY_FROM_SA;
456                         sa->w10.s.dscp = tunnel->ipv4.dscp;
457                 } else {
458                         sa->w2.s.dscp_src = ROC_IE_OT_SA_COPY_FROM_INNER_IP_HDR;
459                 }
460                 break;
461         case RTE_SECURITY_IPSEC_TUNNEL_IPV6:
462                 sa->w2.s.outer_ip_ver = ROC_IE_SA_IP_VERSION_6;
463                 memcpy(&sa->outer_hdr.ipv6.src_addr, &tunnel->ipv6.src_addr,
464                        sizeof(struct in6_addr));
465                 memcpy(&sa->outer_hdr.ipv6.dst_addr, &tunnel->ipv6.dst_addr,
466                        sizeof(struct in6_addr));
467
468                 /* Outer header flow label source */
469                 if (!ipsec_xfrm->options.copy_flabel) {
470                         sa->w2.s.ipv4_df_src_or_ipv6_flw_lbl_src =
471                                 ROC_IE_OT_SA_COPY_FROM_SA;
472
473                         sa->w10.s.ipv4_df_or_ipv6_flw_lbl = tunnel->ipv6.flabel;
474                 } else {
475                         sa->w2.s.ipv4_df_src_or_ipv6_flw_lbl_src =
476                                 ROC_IE_OT_SA_COPY_FROM_INNER_IP_HDR;
477                 }
478
479                 /* Outer header DSCP source */
480                 if (!ipsec_xfrm->options.copy_dscp) {
481                         sa->w2.s.dscp_src = ROC_IE_OT_SA_COPY_FROM_SA;
482                         sa->w10.s.dscp = tunnel->ipv6.dscp;
483                 } else {
484                         sa->w2.s.dscp_src = ROC_IE_OT_SA_COPY_FROM_INNER_IP_HDR;
485                 }
486                 break;
487         default:
488                 return -EINVAL;
489         }
490
491 skip_tunnel_info:
492         /* ESN */
493         sa->w0.s.esn_en = !!ipsec_xfrm->options.esn;
494
495         if (ipsec_xfrm->options.udp_encap) {
496                 sa->w10.s.udp_src_port = 4500;
497                 sa->w10.s.udp_dst_port = 4500;
498         }
499
500         offset = offsetof(struct roc_ot_ipsec_outb_sa, ctx);
501         /* Word offset for HW managed SA field */
502         sa->w0.s.hw_ctx_off = offset / 8;
503
504         /* Context push size is up to err ctl in HW ctx */
505         sa->w0.s.ctx_push_size = sa->w0.s.hw_ctx_off + 1;
506
507         /* Entire context size in 128B units */
508         offset = sizeof(struct roc_ot_ipsec_outb_sa);
509         sa->w0.s.ctx_size = (PLT_ALIGN_CEIL(offset, ROC_CTX_UNIT_128B) /
510                              ROC_CTX_UNIT_128B) -
511                             1;
512
513         /* IPID gen */
514         sa->w2.s.ipid_gen = 1;
515
516         /**
517          * CPT MC triggers expiry when counter value changes from 2 to 1. To
518          * mitigate this behaviour add 1 to the life counter values provided.
519          */
520
521         if (ipsec_xfrm->life.bytes_soft_limit) {
522                 sa->ctx.soft_life = ipsec_xfrm->life.bytes_soft_limit + 1;
523                 sa->w0.s.soft_life_dec = 1;
524         }
525
526         if (ipsec_xfrm->life.packets_soft_limit) {
527                 sa->ctx.soft_life = ipsec_xfrm->life.packets_soft_limit + 1;
528                 sa->w0.s.soft_life_dec = 1;
529         }
530
531         if (ipsec_xfrm->life.bytes_hard_limit) {
532                 sa->ctx.hard_life = ipsec_xfrm->life.bytes_hard_limit + 1;
533                 sa->w0.s.hard_life_dec = 1;
534         }
535
536         if (ipsec_xfrm->life.packets_hard_limit) {
537                 sa->ctx.hard_life = ipsec_xfrm->life.packets_hard_limit + 1;
538                 sa->w0.s.hard_life_dec = 1;
539         }
540
541         /* There are two words of CPT_CTX_HW_S for ucode to skip */
542         sa->w0.s.ctx_hdr_size = 1;
543         sa->w0.s.aop_valid = 1;
544
545         rte_wmb();
546
547         /* Enable SA */
548         sa->w2.s.valid = 1;
549         return 0;
550 }
551
552 bool
553 cnxk_ot_ipsec_inb_sa_valid(struct roc_ot_ipsec_inb_sa *sa)
554 {
555         return !!sa->w2.s.valid;
556 }
557
558 bool
559 cnxk_ot_ipsec_outb_sa_valid(struct roc_ot_ipsec_outb_sa *sa)
560 {
561         return !!sa->w2.s.valid;
562 }
563
564 static inline int
565 ipsec_xfrm_verify(struct rte_security_ipsec_xform *ipsec_xfrm,
566                   struct rte_crypto_sym_xform *crypto_xfrm)
567 {
568         if (crypto_xfrm->next == NULL)
569                 return -EINVAL;
570
571         if (ipsec_xfrm->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
572                 if (crypto_xfrm->type != RTE_CRYPTO_SYM_XFORM_AUTH ||
573                     crypto_xfrm->next->type != RTE_CRYPTO_SYM_XFORM_CIPHER)
574                         return -EINVAL;
575         } else {
576                 if (crypto_xfrm->type != RTE_CRYPTO_SYM_XFORM_CIPHER ||
577                     crypto_xfrm->next->type != RTE_CRYPTO_SYM_XFORM_AUTH)
578                         return -EINVAL;
579         }
580
581         return 0;
582 }
583
584 static int
585 onf_ipsec_sa_common_param_fill(struct roc_ie_onf_sa_ctl *ctl, uint8_t *salt,
586                                uint8_t *cipher_key, uint8_t *hmac_opad_ipad,
587                                struct rte_security_ipsec_xform *ipsec_xfrm,
588                                struct rte_crypto_sym_xform *crypto_xfrm)
589 {
590         struct rte_crypto_sym_xform *auth_xfrm, *cipher_xfrm;
591         int rc, length, auth_key_len;
592         const uint8_t *key = NULL;
593
594         /* Set direction */
595         switch (ipsec_xfrm->direction) {
596         case RTE_SECURITY_IPSEC_SA_DIR_INGRESS:
597                 ctl->direction = ROC_IE_SA_DIR_INBOUND;
598                 auth_xfrm = crypto_xfrm;
599                 cipher_xfrm = crypto_xfrm->next;
600                 break;
601         case RTE_SECURITY_IPSEC_SA_DIR_EGRESS:
602                 ctl->direction = ROC_IE_SA_DIR_OUTBOUND;
603                 cipher_xfrm = crypto_xfrm;
604                 auth_xfrm = crypto_xfrm->next;
605                 break;
606         default:
607                 return -EINVAL;
608         }
609
610         /* Set protocol - ESP vs AH */
611         switch (ipsec_xfrm->proto) {
612         case RTE_SECURITY_IPSEC_SA_PROTO_ESP:
613                 ctl->ipsec_proto = ROC_IE_SA_PROTOCOL_ESP;
614                 break;
615         case RTE_SECURITY_IPSEC_SA_PROTO_AH:
616                 return -ENOTSUP;
617         default:
618                 return -EINVAL;
619         }
620
621         /* Set mode - transport vs tunnel */
622         switch (ipsec_xfrm->mode) {
623         case RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT:
624                 ctl->ipsec_mode = ROC_IE_SA_MODE_TRANSPORT;
625                 break;
626         case RTE_SECURITY_IPSEC_SA_MODE_TUNNEL:
627                 ctl->ipsec_mode = ROC_IE_SA_MODE_TUNNEL;
628                 break;
629         default:
630                 return -EINVAL;
631         }
632
633         /* Set encryption algorithm */
634         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
635                 length = crypto_xfrm->aead.key.length;
636
637                 switch (crypto_xfrm->aead.algo) {
638                 case RTE_CRYPTO_AEAD_AES_GCM:
639                         ctl->enc_type = ROC_IE_ON_SA_ENC_AES_GCM;
640                         ctl->auth_type = ROC_IE_ON_SA_AUTH_NULL;
641                         memcpy(salt, &ipsec_xfrm->salt, 4);
642                         key = crypto_xfrm->aead.key.data;
643                         break;
644                 default:
645                         return -ENOTSUP;
646                 }
647
648         } else {
649                 rc = ipsec_xfrm_verify(ipsec_xfrm, crypto_xfrm);
650                 if (rc)
651                         return rc;
652
653                 switch (cipher_xfrm->cipher.algo) {
654                 case RTE_CRYPTO_CIPHER_AES_CBC:
655                         ctl->enc_type = ROC_IE_ON_SA_ENC_AES_CBC;
656                         break;
657                 case RTE_CRYPTO_CIPHER_AES_CTR:
658                         ctl->enc_type = ROC_IE_ON_SA_ENC_AES_CTR;
659                         break;
660                 default:
661                         return -ENOTSUP;
662                 }
663
664                 switch (auth_xfrm->auth.algo) {
665                 case RTE_CRYPTO_AUTH_SHA1_HMAC:
666                         ctl->auth_type = ROC_IE_ON_SA_AUTH_SHA1;
667                         break;
668                 default:
669                         return -ENOTSUP;
670                 }
671                 auth_key_len = auth_xfrm->auth.key.length;
672                 if (auth_key_len < 20 || auth_key_len > 64)
673                         return -ENOTSUP;
674
675                 key = cipher_xfrm->cipher.key.data;
676                 length = cipher_xfrm->cipher.key.length;
677
678                 ipsec_hmac_opad_ipad_gen(auth_xfrm, hmac_opad_ipad);
679         }
680
681         switch (length) {
682         case ROC_CPT_AES128_KEY_LEN:
683                 ctl->aes_key_len = ROC_IE_SA_AES_KEY_LEN_128;
684                 break;
685         case ROC_CPT_AES192_KEY_LEN:
686                 ctl->aes_key_len = ROC_IE_SA_AES_KEY_LEN_192;
687                 break;
688         case ROC_CPT_AES256_KEY_LEN:
689                 ctl->aes_key_len = ROC_IE_SA_AES_KEY_LEN_256;
690                 break;
691         default:
692                 return -EINVAL;
693         }
694
695         memcpy(cipher_key, key, length);
696
697         if (ipsec_xfrm->options.esn)
698                 ctl->esn_en = 1;
699
700         ctl->spi = rte_cpu_to_be_32(ipsec_xfrm->spi);
701         return 0;
702 }
703
704 int
705 cnxk_onf_ipsec_inb_sa_fill(struct roc_onf_ipsec_inb_sa *sa,
706                            struct rte_security_ipsec_xform *ipsec_xfrm,
707                            struct rte_crypto_sym_xform *crypto_xfrm)
708 {
709         struct roc_ie_onf_sa_ctl *ctl = &sa->ctl;
710         int rc;
711
712         rc = onf_ipsec_sa_common_param_fill(ctl, sa->nonce, sa->cipher_key,
713                                             sa->hmac_key, ipsec_xfrm,
714                                             crypto_xfrm);
715         if (rc)
716                 return rc;
717
718         rte_wmb();
719
720         /* Enable SA */
721         ctl->valid = 1;
722         return 0;
723 }
724
725 int
726 cnxk_onf_ipsec_outb_sa_fill(struct roc_onf_ipsec_outb_sa *sa,
727                             struct rte_security_ipsec_xform *ipsec_xfrm,
728                             struct rte_crypto_sym_xform *crypto_xfrm)
729 {
730         struct rte_security_ipsec_tunnel_param *tunnel = &ipsec_xfrm->tunnel;
731         struct roc_ie_onf_sa_ctl *ctl = &sa->ctl;
732         int rc;
733
734         /* Fill common params */
735         rc = onf_ipsec_sa_common_param_fill(ctl, sa->nonce, sa->cipher_key,
736                                             sa->hmac_key, ipsec_xfrm,
737                                             crypto_xfrm);
738         if (rc)
739                 return rc;
740
741         if (ipsec_xfrm->mode != RTE_SECURITY_IPSEC_SA_MODE_TUNNEL)
742                 goto skip_tunnel_info;
743
744         /* Tunnel header info */
745         switch (tunnel->type) {
746         case RTE_SECURITY_IPSEC_TUNNEL_IPV4:
747                 memcpy(&sa->ip_src, &tunnel->ipv4.src_ip,
748                        sizeof(struct in_addr));
749                 memcpy(&sa->ip_dst, &tunnel->ipv4.dst_ip,
750                        sizeof(struct in_addr));
751                 break;
752         case RTE_SECURITY_IPSEC_TUNNEL_IPV6:
753                 return -ENOTSUP;
754         default:
755                 return -EINVAL;
756         }
757
758         /* Update udp encap ports */
759         if (ipsec_xfrm->options.udp_encap == 1) {
760                 sa->udp_src = 4500;
761                 sa->udp_dst = 4500;
762         }
763
764 skip_tunnel_info:
765         rte_wmb();
766
767         /* Enable SA */
768         ctl->valid = 1;
769         return 0;
770 }
771
772 bool
773 cnxk_onf_ipsec_inb_sa_valid(struct roc_onf_ipsec_inb_sa *sa)
774 {
775         return !!sa->ctl.valid;
776 }
777
778 bool
779 cnxk_onf_ipsec_outb_sa_valid(struct roc_onf_ipsec_outb_sa *sa)
780 {
781         return !!sa->ctl.valid;
782 }
783
784 uint8_t
785 cnxk_ipsec_ivlen_get(enum rte_crypto_cipher_algorithm c_algo,
786                      enum rte_crypto_auth_algorithm a_algo,
787                      enum rte_crypto_aead_algorithm aead_algo)
788 {
789         uint8_t ivlen = 0;
790
791         if (aead_algo == RTE_CRYPTO_AEAD_AES_GCM)
792                 ivlen = 8;
793
794         switch (c_algo) {
795         case RTE_CRYPTO_CIPHER_AES_CTR:
796                 ivlen = 8;
797                 break;
798         case RTE_CRYPTO_CIPHER_3DES_CBC:
799                 ivlen = ROC_CPT_DES_BLOCK_LENGTH;
800                 break;
801         case RTE_CRYPTO_CIPHER_AES_CBC:
802                 ivlen = ROC_CPT_AES_BLOCK_LENGTH;
803                 break;
804         default:
805                 break;
806         }
807
808         switch (a_algo) {
809         case RTE_CRYPTO_AUTH_AES_GMAC:
810                 ivlen = 8;
811                 break;
812         default:
813                 break;
814         }
815
816         return ivlen;
817 }
818
819 uint8_t
820 cnxk_ipsec_icvlen_get(enum rte_crypto_cipher_algorithm c_algo,
821                       enum rte_crypto_auth_algorithm a_algo,
822                       enum rte_crypto_aead_algorithm aead_algo)
823 {
824         uint8_t icv = 0;
825
826         (void)c_algo;
827
828         switch (a_algo) {
829         case RTE_CRYPTO_AUTH_NULL:
830                 icv = 0;
831                 break;
832         case RTE_CRYPTO_AUTH_SHA1_HMAC:
833                 icv = 12;
834                 break;
835         case RTE_CRYPTO_AUTH_SHA256_HMAC:
836         case RTE_CRYPTO_AUTH_AES_GMAC:
837                 icv = 16;
838                 break;
839         case RTE_CRYPTO_AUTH_SHA384_HMAC:
840                 icv = 24;
841                 break;
842         case RTE_CRYPTO_AUTH_SHA512_HMAC:
843                 icv = 32;
844                 break;
845         case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
846                 icv = 12;
847                 break;
848         default:
849                 break;
850         }
851
852         switch (aead_algo) {
853         case RTE_CRYPTO_AEAD_AES_GCM:
854                 icv = 16;
855                 break;
856         default:
857                 break;
858         }
859
860         return icv;
861 }
862
863 uint8_t
864 cnxk_ipsec_outb_roundup_byte(enum rte_crypto_cipher_algorithm c_algo,
865                              enum rte_crypto_aead_algorithm aead_algo)
866 {
867         uint8_t roundup_byte = 4;
868
869         if (aead_algo == RTE_CRYPTO_AEAD_AES_GCM)
870                 return roundup_byte;
871
872         switch (c_algo) {
873         case RTE_CRYPTO_CIPHER_AES_CTR:
874                 roundup_byte = 4;
875                 break;
876         case RTE_CRYPTO_CIPHER_AES_CBC:
877                 roundup_byte = 16;
878                 break;
879         case RTE_CRYPTO_CIPHER_3DES_CBC:
880                 roundup_byte = 8;
881                 break;
882         case RTE_CRYPTO_CIPHER_NULL:
883                 roundup_byte = 4;
884                 break;
885         default:
886                 break;
887         }
888
889         return roundup_byte;
890 }
891
892 int
893 cnxk_ipsec_outb_rlens_get(struct cnxk_ipsec_outb_rlens *rlens,
894                           struct rte_security_ipsec_xform *ipsec_xfrm,
895                           struct rte_crypto_sym_xform *crypto_xfrm)
896 {
897         struct rte_security_ipsec_tunnel_param *tunnel = &ipsec_xfrm->tunnel;
898         enum rte_crypto_cipher_algorithm c_algo = RTE_CRYPTO_CIPHER_NULL;
899         enum rte_crypto_auth_algorithm a_algo = RTE_CRYPTO_AUTH_NULL;
900         enum rte_crypto_aead_algorithm aead_algo = 0;
901         uint16_t partial_len = 0;
902         uint8_t roundup_byte = 0;
903         int8_t roundup_len = 0;
904
905         memset(rlens, 0, sizeof(struct cnxk_ipsec_outb_rlens));
906
907         /* Get Cipher and Auth algo */
908         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
909                 aead_algo = crypto_xfrm->aead.algo;
910         } else {
911                 if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
912                         c_algo = crypto_xfrm->cipher.algo;
913                 else
914                         a_algo = crypto_xfrm->auth.algo;
915
916                 if (crypto_xfrm->next) {
917                         if (crypto_xfrm->next->type ==
918                             RTE_CRYPTO_SYM_XFORM_CIPHER)
919                                 c_algo = crypto_xfrm->next->cipher.algo;
920                         else
921                                 a_algo = crypto_xfrm->next->auth.algo;
922                 }
923         }
924
925         if (ipsec_xfrm->proto == RTE_SECURITY_IPSEC_SA_PROTO_ESP) {
926                 partial_len = ROC_CPT_ESP_HDR_LEN;
927                 roundup_len = ROC_CPT_ESP_TRL_LEN;
928         } else {
929                 partial_len = ROC_CPT_AH_HDR_LEN;
930         }
931
932         if (ipsec_xfrm->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
933                 if (tunnel->type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
934                         partial_len += ROC_CPT_TUNNEL_IPV4_HDR_LEN;
935                 else
936                         partial_len += ROC_CPT_TUNNEL_IPV6_HDR_LEN;
937         }
938
939         partial_len += cnxk_ipsec_ivlen_get(c_algo, a_algo, aead_algo);
940         partial_len += cnxk_ipsec_icvlen_get(c_algo, a_algo, aead_algo);
941         roundup_byte = cnxk_ipsec_outb_roundup_byte(c_algo, aead_algo);
942
943         if (ipsec_xfrm->options.udp_encap)
944                 partial_len += sizeof(struct rte_udp_hdr);
945
946         rlens->partial_len = partial_len;
947         rlens->roundup_len = roundup_len;
948         rlens->roundup_byte = roundup_byte;
949         rlens->max_extended_len = partial_len + roundup_len + roundup_byte;
950         return 0;
951 }