eda9f19738d381328ad78a89abc7bf1a123eb2fb
[dpdk.git] / drivers / crypto / octeontx2 / otx2_ipsec_po.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2020 Marvell International Ltd.
3  */
4
5 #ifndef __OTX2_IPSEC_PO_H__
6 #define __OTX2_IPSEC_PO_H__
7
8 #include <rte_crypto_sym.h>
9 #include <rte_ip.h>
10 #include <rte_security.h>
11
12 #define OTX2_IPSEC_PO_AES_GCM_INB_CTX_LEN    0x09
13
14 #define OTX2_IPSEC_PO_PER_PKT_IV  BIT(11)
15
16 #define OTX2_IPSEC_PO_WRITE_IPSEC_OUTB     0x20
17 #define OTX2_IPSEC_PO_WRITE_IPSEC_INB      0x21
18 #define OTX2_IPSEC_PO_PROCESS_IPSEC_OUTB   0x23
19 #define OTX2_IPSEC_PO_PROCESS_IPSEC_INB    0x24
20
21 #define OTX2_IPSEC_PO_INB_RPTR_HDR         0x8
22
23 enum otx2_ipsec_po_comp_e {
24         OTX2_IPSEC_PO_CC_SUCCESS = 0x00,
25         OTX2_IPSEC_PO_CC_AUTH_UNSUPPORTED = 0xB0,
26         OTX2_IPSEC_PO_CC_ENCRYPT_UNSUPPORTED = 0xB1,
27 };
28
29 enum {
30         OTX2_IPSEC_PO_SA_DIRECTION_INBOUND = 0,
31         OTX2_IPSEC_PO_SA_DIRECTION_OUTBOUND = 1,
32 };
33
34 enum {
35         OTX2_IPSEC_PO_SA_IP_VERSION_4 = 0,
36         OTX2_IPSEC_PO_SA_IP_VERSION_6 = 1,
37 };
38
39 enum {
40         OTX2_IPSEC_PO_SA_MODE_TRANSPORT = 0,
41         OTX2_IPSEC_PO_SA_MODE_TUNNEL = 1,
42 };
43
44 enum {
45         OTX2_IPSEC_PO_SA_PROTOCOL_AH = 0,
46         OTX2_IPSEC_PO_SA_PROTOCOL_ESP = 1,
47 };
48
49 enum {
50         OTX2_IPSEC_PO_SA_AES_KEY_LEN_128 = 1,
51         OTX2_IPSEC_PO_SA_AES_KEY_LEN_192 = 2,
52         OTX2_IPSEC_PO_SA_AES_KEY_LEN_256 = 3,
53 };
54
55 enum {
56         OTX2_IPSEC_PO_SA_ENC_NULL = 0,
57         OTX2_IPSEC_PO_SA_ENC_DES_CBC = 1,
58         OTX2_IPSEC_PO_SA_ENC_3DES_CBC = 2,
59         OTX2_IPSEC_PO_SA_ENC_AES_CBC = 3,
60         OTX2_IPSEC_PO_SA_ENC_AES_CTR = 4,
61         OTX2_IPSEC_PO_SA_ENC_AES_GCM = 5,
62         OTX2_IPSEC_PO_SA_ENC_AES_CCM = 6,
63 };
64
65 enum {
66         OTX2_IPSEC_PO_SA_AUTH_NULL = 0,
67         OTX2_IPSEC_PO_SA_AUTH_MD5 = 1,
68         OTX2_IPSEC_PO_SA_AUTH_SHA1 = 2,
69         OTX2_IPSEC_PO_SA_AUTH_SHA2_224 = 3,
70         OTX2_IPSEC_PO_SA_AUTH_SHA2_256 = 4,
71         OTX2_IPSEC_PO_SA_AUTH_SHA2_384 = 5,
72         OTX2_IPSEC_PO_SA_AUTH_SHA2_512 = 6,
73         OTX2_IPSEC_PO_SA_AUTH_AES_GMAC = 7,
74         OTX2_IPSEC_PO_SA_AUTH_AES_XCBC_128 = 8,
75 };
76
77 enum {
78         OTX2_IPSEC_PO_SA_FRAG_POST = 0,
79         OTX2_IPSEC_PO_SA_FRAG_PRE = 1,
80 };
81
82 enum {
83         OTX2_IPSEC_PO_SA_ENCAP_NONE = 0,
84         OTX2_IPSEC_PO_SA_ENCAP_UDP = 1,
85 };
86
87 struct otx2_ipsec_po_out_hdr {
88         uint32_t ip_id;
89         uint32_t seq;
90         uint8_t iv[16];
91 };
92
93 union otx2_ipsec_po_bit_perfect_iv {
94         uint8_t aes_iv[16];
95         uint8_t des_iv[8];
96         struct {
97                 uint8_t nonce[4];
98                 uint8_t iv[8];
99                 uint8_t counter[4];
100         } gcm;
101 };
102
103 struct otx2_ipsec_po_traffic_selector {
104         rte_be16_t src_port[2];
105         rte_be16_t dst_port[2];
106         RTE_STD_C11
107         union {
108                 struct {
109                         rte_be32_t src_addr[2];
110                         rte_be32_t dst_addr[2];
111                 } ipv4;
112                 struct {
113                         uint8_t src_addr[32];
114                         uint8_t dst_addr[32];
115                 } ipv6;
116         };
117 };
118
119 struct otx2_ipsec_po_sa_ctl {
120         rte_be32_t spi          : 32;
121         uint64_t exp_proto_inter_frag : 8;
122         uint64_t rsvd_42_40   : 3;
123         uint64_t esn_en       : 1;
124         uint64_t rsvd_45_44   : 2;
125         uint64_t encap_type   : 2;
126         uint64_t enc_type     : 3;
127         uint64_t rsvd_48      : 1;
128         uint64_t auth_type    : 4;
129         uint64_t valid        : 1;
130         uint64_t direction    : 1;
131         uint64_t outer_ip_ver : 1;
132         uint64_t inner_ip_ver : 1;
133         uint64_t ipsec_mode   : 1;
134         uint64_t ipsec_proto  : 1;
135         uint64_t aes_key_len  : 2;
136 };
137
138 struct otx2_ipsec_po_in_sa {
139         /* w0 */
140         struct otx2_ipsec_po_sa_ctl ctl;
141
142         /* w1-w4 */
143         uint8_t cipher_key[32];
144
145         /* w5-w6 */
146         union otx2_ipsec_po_bit_perfect_iv iv;
147
148         /* w7 */
149         uint32_t esn_hi;
150         uint32_t esn_low;
151
152         /* w8 */
153         uint8_t udp_encap[8];
154
155         /* w9-w33 */
156         union {
157                 struct {
158                         uint8_t hmac_key[48];
159                         struct otx2_ipsec_po_traffic_selector selector;
160                 } aes_gcm;
161                 struct {
162                         uint8_t hmac_key[64];
163                         uint8_t hmac_iv[64];
164                         struct otx2_ipsec_po_traffic_selector selector;
165                 } sha2;
166         };
167         union {
168                 struct otx2_ipsec_replay *replay;
169                 uint64_t replay64;
170         };
171         uint32_t replay_win_sz;
172 };
173
174 struct otx2_ipsec_po_ip_template {
175         RTE_STD_C11
176         union {
177                 struct {
178                         struct rte_ipv4_hdr ipv4_hdr;
179                         uint16_t udp_src;
180                         uint16_t udp_dst;
181                 } ip4;
182                 struct {
183                         struct rte_ipv6_hdr ipv6_hdr;
184                         uint16_t udp_src;
185                         uint16_t udp_dst;
186                 } ip6;
187         };
188 };
189
190 struct otx2_ipsec_po_out_sa {
191         /* w0 */
192         struct otx2_ipsec_po_sa_ctl ctl;
193
194         /* w1-w4 */
195         uint8_t cipher_key[32];
196
197         /* w5-w6 */
198         union otx2_ipsec_po_bit_perfect_iv iv;
199
200         /* w7 */
201         uint32_t esn_hi;
202         uint32_t esn_low;
203
204         /* w8-w55 */
205         union {
206                 struct {
207                         struct otx2_ipsec_po_ip_template template;
208                 } aes_gcm;
209                 struct {
210                         uint8_t hmac_key[24];
211                         uint8_t unused[24];
212                         struct otx2_ipsec_po_ip_template template;
213                 } sha1;
214                 struct {
215                         uint8_t hmac_key[64];
216                         uint8_t hmac_iv[64];
217                         struct otx2_ipsec_po_ip_template template;
218                 } sha2;
219         };
220 };
221
222 static inline int
223 ipsec_po_xform_cipher_verify(struct rte_crypto_sym_xform *xform)
224 {
225         if (xform->cipher.algo == RTE_CRYPTO_CIPHER_AES_CBC) {
226                 switch (xform->cipher.key.length) {
227                 case 16:
228                 case 24:
229                 case 32:
230                         break;
231                 default:
232                         return -ENOTSUP;
233                 }
234                 return 0;
235         }
236
237         return -ENOTSUP;
238 }
239
240 static inline int
241 ipsec_po_xform_auth_verify(struct rte_crypto_sym_xform *xform)
242 {
243         uint16_t keylen = xform->auth.key.length;
244
245         if (xform->auth.algo == RTE_CRYPTO_AUTH_SHA1_HMAC) {
246                 if (keylen >= 20 && keylen <= 64)
247                         return 0;
248         } else if (xform->auth.algo == RTE_CRYPTO_AUTH_SHA256_HMAC) {
249                 if (keylen >= 32 && keylen <= 64)
250                         return 0;
251         }
252
253         return -ENOTSUP;
254 }
255
256 static inline int
257 ipsec_po_xform_aead_verify(struct rte_security_ipsec_xform *ipsec,
258                            struct rte_crypto_sym_xform *xform)
259 {
260         if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS &&
261             xform->aead.op != RTE_CRYPTO_AEAD_OP_ENCRYPT)
262                 return -EINVAL;
263
264         if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS &&
265             xform->aead.op != RTE_CRYPTO_AEAD_OP_DECRYPT)
266                 return -EINVAL;
267
268         if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
269                 switch (xform->aead.key.length) {
270                 case 16:
271                 case 24:
272                 case 32:
273                         break;
274                 default:
275                         return -EINVAL;
276                 }
277                 return 0;
278         }
279
280         return -ENOTSUP;
281 }
282
283 static inline int
284 ipsec_po_xform_verify(struct rte_security_ipsec_xform *ipsec,
285                       struct rte_crypto_sym_xform *xform)
286 {
287         struct rte_crypto_sym_xform *auth_xform, *cipher_xform;
288         int ret;
289
290         if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD)
291                 return ipsec_po_xform_aead_verify(ipsec, xform);
292
293         if (xform->next == NULL)
294                 return -EINVAL;
295
296         if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
297                 /* Ingress */
298                 if (xform->type != RTE_CRYPTO_SYM_XFORM_AUTH ||
299                     xform->next->type != RTE_CRYPTO_SYM_XFORM_CIPHER)
300                         return -EINVAL;
301                 auth_xform = xform;
302                 cipher_xform = xform->next;
303         } else {
304                 /* Egress */
305                 if (xform->type != RTE_CRYPTO_SYM_XFORM_CIPHER ||
306                     xform->next->type != RTE_CRYPTO_SYM_XFORM_AUTH)
307                         return -EINVAL;
308                 cipher_xform = xform;
309                 auth_xform = xform->next;
310         }
311
312         ret = ipsec_po_xform_cipher_verify(cipher_xform);
313         if (ret)
314                 return ret;
315
316         ret = ipsec_po_xform_auth_verify(auth_xform);
317         if (ret)
318                 return ret;
319
320         return 0;
321 }
322
323 static inline int
324 ipsec_po_sa_ctl_set(struct rte_security_ipsec_xform *ipsec,
325                     struct rte_crypto_sym_xform *xform,
326                     struct otx2_ipsec_po_sa_ctl *ctl)
327 {
328         struct rte_crypto_sym_xform *cipher_xform, *auth_xform;
329         int aes_key_len;
330
331         if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
332                 ctl->direction = OTX2_IPSEC_PO_SA_DIRECTION_OUTBOUND;
333                 cipher_xform = xform;
334                 auth_xform = xform->next;
335         } else if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
336                 ctl->direction = OTX2_IPSEC_PO_SA_DIRECTION_INBOUND;
337                 auth_xform = xform;
338                 cipher_xform = xform->next;
339         } else {
340                 return -EINVAL;
341         }
342
343         if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
344                 if (ipsec->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
345                         ctl->outer_ip_ver = OTX2_IPSEC_PO_SA_IP_VERSION_4;
346                 else if (ipsec->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV6)
347                         ctl->outer_ip_ver = OTX2_IPSEC_PO_SA_IP_VERSION_6;
348                 else
349                         return -EINVAL;
350         }
351
352         ctl->inner_ip_ver = ctl->outer_ip_ver;
353
354         if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT)
355                 ctl->ipsec_mode = OTX2_IPSEC_PO_SA_MODE_TRANSPORT;
356         else if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL)
357                 ctl->ipsec_mode = OTX2_IPSEC_PO_SA_MODE_TUNNEL;
358         else
359                 return -EINVAL;
360
361         if (ipsec->proto == RTE_SECURITY_IPSEC_SA_PROTO_AH)
362                 ctl->ipsec_proto = OTX2_IPSEC_PO_SA_PROTOCOL_AH;
363         else if (ipsec->proto == RTE_SECURITY_IPSEC_SA_PROTO_ESP)
364                 ctl->ipsec_proto = OTX2_IPSEC_PO_SA_PROTOCOL_ESP;
365         else
366                 return -EINVAL;
367
368         if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
369                 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
370                         ctl->enc_type = OTX2_IPSEC_PO_SA_ENC_AES_GCM;
371                         aes_key_len = xform->aead.key.length;
372                 } else {
373                         return -ENOTSUP;
374                 }
375         } else if (cipher_xform->cipher.algo == RTE_CRYPTO_CIPHER_AES_CBC) {
376                 ctl->enc_type = OTX2_IPSEC_PO_SA_ENC_AES_CBC;
377                 aes_key_len = cipher_xform->cipher.key.length;
378         } else {
379                 return -ENOTSUP;
380         }
381
382
383         switch (aes_key_len) {
384         case 16:
385                 ctl->aes_key_len = OTX2_IPSEC_PO_SA_AES_KEY_LEN_128;
386                 break;
387         case 24:
388                 ctl->aes_key_len = OTX2_IPSEC_PO_SA_AES_KEY_LEN_192;
389                 break;
390         case 32:
391                 ctl->aes_key_len = OTX2_IPSEC_PO_SA_AES_KEY_LEN_256;
392                 break;
393         default:
394                 return -EINVAL;
395         }
396
397         if (xform->type != RTE_CRYPTO_SYM_XFORM_AEAD) {
398                 switch (auth_xform->auth.algo) {
399                 case RTE_CRYPTO_AUTH_NULL:
400                         ctl->auth_type = OTX2_IPSEC_PO_SA_AUTH_NULL;
401                         break;
402                 case RTE_CRYPTO_AUTH_MD5_HMAC:
403                         ctl->auth_type = OTX2_IPSEC_PO_SA_AUTH_MD5;
404                         break;
405                 case RTE_CRYPTO_AUTH_SHA1_HMAC:
406                         ctl->auth_type = OTX2_IPSEC_PO_SA_AUTH_SHA1;
407                         break;
408                 case RTE_CRYPTO_AUTH_SHA224_HMAC:
409                         ctl->auth_type = OTX2_IPSEC_PO_SA_AUTH_SHA2_224;
410                         break;
411                 case RTE_CRYPTO_AUTH_SHA256_HMAC:
412                         ctl->auth_type = OTX2_IPSEC_PO_SA_AUTH_SHA2_256;
413                         break;
414                 case RTE_CRYPTO_AUTH_SHA384_HMAC:
415                         ctl->auth_type = OTX2_IPSEC_PO_SA_AUTH_SHA2_384;
416                         break;
417                 case RTE_CRYPTO_AUTH_SHA512_HMAC:
418                         ctl->auth_type = OTX2_IPSEC_PO_SA_AUTH_SHA2_512;
419                         break;
420                 case RTE_CRYPTO_AUTH_AES_GMAC:
421                         ctl->auth_type = OTX2_IPSEC_PO_SA_AUTH_AES_GMAC;
422                         break;
423                 case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
424                         ctl->auth_type = OTX2_IPSEC_PO_SA_AUTH_AES_XCBC_128;
425                         break;
426                 default:
427                         return -ENOTSUP;
428                 }
429         }
430
431         if (ipsec->options.esn)
432                 ctl->esn_en = 1;
433
434         if (ipsec->options.udp_encap == 1)
435                 ctl->encap_type = OTX2_IPSEC_PO_SA_ENCAP_UDP;
436
437         ctl->spi = rte_cpu_to_be_32(ipsec->spi);
438         ctl->valid = 1;
439
440         return 0;
441 }
442
443 #endif /* __OTX2_IPSEC_PO_H__ */