1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2020 Marvell International Ltd.
5 #ifndef __OTX2_IPSEC_PO_H__
6 #define __OTX2_IPSEC_PO_H__
8 #include <rte_crypto_sym.h>
10 #include <rte_security.h>
12 #define OTX2_IPSEC_PO_AES_GCM_INB_CTX_LEN 0x09
14 #define OTX2_IPSEC_PO_PER_PKT_IV BIT(11)
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
21 #define OTX2_IPSEC_PO_INB_RPTR_HDR 0x8
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,
30 OTX2_IPSEC_PO_SA_DIRECTION_INBOUND = 0,
31 OTX2_IPSEC_PO_SA_DIRECTION_OUTBOUND = 1,
35 OTX2_IPSEC_PO_SA_IP_VERSION_4 = 0,
36 OTX2_IPSEC_PO_SA_IP_VERSION_6 = 1,
40 OTX2_IPSEC_PO_SA_MODE_TRANSPORT = 0,
41 OTX2_IPSEC_PO_SA_MODE_TUNNEL = 1,
45 OTX2_IPSEC_PO_SA_PROTOCOL_AH = 0,
46 OTX2_IPSEC_PO_SA_PROTOCOL_ESP = 1,
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,
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,
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,
78 OTX2_IPSEC_PO_SA_FRAG_POST = 0,
79 OTX2_IPSEC_PO_SA_FRAG_PRE = 1,
83 OTX2_IPSEC_PO_SA_ENCAP_NONE = 0,
84 OTX2_IPSEC_PO_SA_ENCAP_UDP = 1,
87 struct otx2_ipsec_po_out_hdr {
93 union otx2_ipsec_po_bit_perfect_iv {
103 struct otx2_ipsec_po_traffic_selector {
104 rte_be16_t src_port[2];
105 rte_be16_t dst_port[2];
109 rte_be32_t src_addr[2];
110 rte_be32_t dst_addr[2];
113 uint8_t src_addr[32];
114 uint8_t dst_addr[32];
119 struct otx2_ipsec_po_sa_ctl {
121 uint64_t exp_proto_inter_frag : 8;
122 uint64_t rsvd_42_40 : 3;
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;
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;
138 struct otx2_ipsec_po_in_sa {
140 struct otx2_ipsec_po_sa_ctl ctl;
143 uint8_t cipher_key[32];
146 union otx2_ipsec_po_bit_perfect_iv iv;
153 uint8_t udp_encap[8];
158 uint8_t hmac_key[48];
159 struct otx2_ipsec_po_traffic_selector selector;
162 uint8_t hmac_key[64];
164 struct otx2_ipsec_po_traffic_selector selector;
168 struct otx2_ipsec_replay *replay;
171 uint32_t replay_win_sz;
174 struct otx2_ipsec_po_ip_template {
178 struct rte_ipv4_hdr ipv4_hdr;
183 struct rte_ipv6_hdr ipv6_hdr;
190 struct otx2_ipsec_po_out_sa {
192 struct otx2_ipsec_po_sa_ctl ctl;
195 uint8_t cipher_key[32];
198 union otx2_ipsec_po_bit_perfect_iv iv;
208 struct otx2_ipsec_po_ip_template template;
211 uint8_t hmac_key[24];
213 struct otx2_ipsec_po_ip_template template;
216 uint8_t hmac_key[64];
218 struct otx2_ipsec_po_ip_template template;
224 ipsec_po_xform_cipher_verify(struct rte_crypto_sym_xform *xform)
226 if (xform->cipher.algo == RTE_CRYPTO_CIPHER_AES_CBC) {
227 switch (xform->cipher.key.length) {
242 ipsec_po_xform_auth_verify(struct rte_crypto_sym_xform *xform)
244 uint16_t keylen = xform->auth.key.length;
246 if (xform->auth.algo == RTE_CRYPTO_AUTH_SHA1_HMAC) {
247 if (keylen >= 20 && keylen <= 64)
249 } else if (xform->auth.algo == RTE_CRYPTO_AUTH_SHA256_HMAC) {
250 if (keylen >= 32 && keylen <= 64)
258 ipsec_po_xform_aead_verify(struct rte_security_ipsec_xform *ipsec,
259 struct rte_crypto_sym_xform *xform)
261 if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS &&
262 xform->aead.op != RTE_CRYPTO_AEAD_OP_ENCRYPT)
265 if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS &&
266 xform->aead.op != RTE_CRYPTO_AEAD_OP_DECRYPT)
269 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
270 switch (xform->aead.key.length) {
285 ipsec_po_xform_verify(struct rte_security_ipsec_xform *ipsec,
286 struct rte_crypto_sym_xform *xform)
288 struct rte_crypto_sym_xform *auth_xform, *cipher_xform;
291 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD)
292 return ipsec_po_xform_aead_verify(ipsec, xform);
294 if (xform->next == NULL)
297 if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
299 if (xform->type != RTE_CRYPTO_SYM_XFORM_AUTH ||
300 xform->next->type != RTE_CRYPTO_SYM_XFORM_CIPHER)
303 cipher_xform = xform->next;
306 if (xform->type != RTE_CRYPTO_SYM_XFORM_CIPHER ||
307 xform->next->type != RTE_CRYPTO_SYM_XFORM_AUTH)
309 cipher_xform = xform;
310 auth_xform = xform->next;
313 ret = ipsec_po_xform_cipher_verify(cipher_xform);
317 ret = ipsec_po_xform_auth_verify(auth_xform);
325 ipsec_po_sa_ctl_set(struct rte_security_ipsec_xform *ipsec,
326 struct rte_crypto_sym_xform *xform,
327 struct otx2_ipsec_po_sa_ctl *ctl)
329 struct rte_crypto_sym_xform *cipher_xform, *auth_xform;
332 if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
333 ctl->direction = OTX2_IPSEC_PO_SA_DIRECTION_OUTBOUND;
334 cipher_xform = xform;
335 auth_xform = xform->next;
336 } else if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
337 ctl->direction = OTX2_IPSEC_PO_SA_DIRECTION_INBOUND;
339 cipher_xform = xform->next;
344 if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
345 if (ipsec->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
346 ctl->outer_ip_ver = OTX2_IPSEC_PO_SA_IP_VERSION_4;
347 else if (ipsec->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV6)
348 ctl->outer_ip_ver = OTX2_IPSEC_PO_SA_IP_VERSION_6;
353 ctl->inner_ip_ver = ctl->outer_ip_ver;
355 if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT)
356 ctl->ipsec_mode = OTX2_IPSEC_PO_SA_MODE_TRANSPORT;
357 else if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL)
358 ctl->ipsec_mode = OTX2_IPSEC_PO_SA_MODE_TUNNEL;
362 if (ipsec->proto == RTE_SECURITY_IPSEC_SA_PROTO_AH)
363 ctl->ipsec_proto = OTX2_IPSEC_PO_SA_PROTOCOL_AH;
364 else if (ipsec->proto == RTE_SECURITY_IPSEC_SA_PROTO_ESP)
365 ctl->ipsec_proto = OTX2_IPSEC_PO_SA_PROTOCOL_ESP;
369 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
370 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
371 ctl->enc_type = OTX2_IPSEC_PO_SA_ENC_AES_GCM;
372 aes_key_len = xform->aead.key.length;
376 } else if (cipher_xform->cipher.algo == RTE_CRYPTO_CIPHER_AES_CBC) {
377 ctl->enc_type = OTX2_IPSEC_PO_SA_ENC_AES_CBC;
378 aes_key_len = cipher_xform->cipher.key.length;
384 switch (aes_key_len) {
386 ctl->aes_key_len = OTX2_IPSEC_PO_SA_AES_KEY_LEN_128;
389 ctl->aes_key_len = OTX2_IPSEC_PO_SA_AES_KEY_LEN_192;
392 ctl->aes_key_len = OTX2_IPSEC_PO_SA_AES_KEY_LEN_256;
398 if (xform->type != RTE_CRYPTO_SYM_XFORM_AEAD) {
399 switch (auth_xform->auth.algo) {
400 case RTE_CRYPTO_AUTH_NULL:
401 ctl->auth_type = OTX2_IPSEC_PO_SA_AUTH_NULL;
403 case RTE_CRYPTO_AUTH_MD5_HMAC:
404 ctl->auth_type = OTX2_IPSEC_PO_SA_AUTH_MD5;
406 case RTE_CRYPTO_AUTH_SHA1_HMAC:
407 ctl->auth_type = OTX2_IPSEC_PO_SA_AUTH_SHA1;
409 case RTE_CRYPTO_AUTH_SHA224_HMAC:
410 ctl->auth_type = OTX2_IPSEC_PO_SA_AUTH_SHA2_224;
412 case RTE_CRYPTO_AUTH_SHA256_HMAC:
413 ctl->auth_type = OTX2_IPSEC_PO_SA_AUTH_SHA2_256;
415 case RTE_CRYPTO_AUTH_SHA384_HMAC:
416 ctl->auth_type = OTX2_IPSEC_PO_SA_AUTH_SHA2_384;
418 case RTE_CRYPTO_AUTH_SHA512_HMAC:
419 ctl->auth_type = OTX2_IPSEC_PO_SA_AUTH_SHA2_512;
421 case RTE_CRYPTO_AUTH_AES_GMAC:
422 ctl->auth_type = OTX2_IPSEC_PO_SA_AUTH_AES_GMAC;
424 case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
425 ctl->auth_type = OTX2_IPSEC_PO_SA_AUTH_AES_XCBC_128;
432 if (ipsec->options.esn)
435 if (ipsec->options.udp_encap == 1)
436 ctl->encap_type = OTX2_IPSEC_PO_SA_ENCAP_UDP;
438 ctl->spi = rte_cpu_to_be_32(ipsec->spi);
444 #endif /* __OTX2_IPSEC_PO_H__ */